Skip to content

Commit 8cbd16c

Browse files
authored
refactor (datafile manager): Update Headers type to only store at-most 1 value per header name (#5)
Summary: Change `Headers` type to have at-most 1 value per header name. This is sufficient for the current requirements (which only use If-Modified-Since and Last-Modified), and simplifies the code. We can introduce a richer type if we need to deal with multiple values for headers. Test plan: Existing unit tests. This should be a pure refactor with no behavior change.
1 parent 2eec4e8 commit 8cbd16c

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

packages/datafile-manager/src/http.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17+
/**
18+
* Headers is the interface that bridges between the abstract datafile manager and
19+
* any Node-or-browser-specific http header types.
20+
* It's simplified and can only store one value per header name.
21+
* We can extend or replace this type if requirements change and we need
22+
* to work with multiple values per header name.
23+
*/
1724
export interface Headers {
18-
[header: string]: string | string[] | undefined;
25+
[header: string]: string | undefined
1926
}
2027

2128
export interface Response {

packages/datafile-manager/src/httpPollingDatafileManager.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,9 @@ export default abstract class HTTPPollingDatafileManager implements DatafileMana
262262

263263
private trySavingLastModified(headers: Headers): void {
264264
const lastModifiedHeader = headers['last-modified'] || headers['Last-Modified']
265-
if (typeof lastModifiedHeader === 'string') {
265+
if (typeof lastModifiedHeader !== 'undefined') {
266266
this.lastResponseLastModified = lastModifiedHeader
267267
logger.debug('Saved last modified header value from response: %s', this.lastResponseLastModified)
268-
} else if (typeof lastModifiedHeader === 'undefined') {
269-
} else { // array
270-
if (lastModifiedHeader.length === 1) {
271-
this.lastResponseLastModified = lastModifiedHeader[0]
272-
logger.debug('Saved last modified header value from response: %s', this.lastResponseLastModified)
273-
}
274268
}
275269
}
276270
}

packages/datafile-manager/src/nodeRequest.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,35 @@ function getRequestOptionsFromUrl(url: url.UrlWithStringQuery): http.RequestOpti
3030
}
3131
}
3232

33+
/**
34+
* Convert incomingMessage.headers (which has type http.IncomingHttpHeaders) into our Headers type defined in src/http.ts.
35+
*
36+
* Our Headers type is simplified and can't represent mutliple values for the same header name.
37+
*
38+
* We don't currently need multiple values support, and the consumer code becomes simpler if it can assume at-most 1 value
39+
* per header name.
40+
*
41+
*/
42+
function createHeadersFromNodeIncomingMessage(
43+
incomingMessage: http.IncomingMessage,
44+
): Headers {
45+
const headers: Headers = {}
46+
Object.keys(incomingMessage.headers).forEach(headerName => {
47+
const headerValue = incomingMessage.headers[headerName]
48+
if (typeof headerValue === 'string') {
49+
headers[headerName] = headerValue
50+
} else if (typeof headerValue === 'undefined') {
51+
} else {
52+
// array
53+
if (headerValue.length > 0) {
54+
// We don't care about multiple values - just take the first one
55+
headers[headerName] = headerValue[0]
56+
}
57+
}
58+
})
59+
return headers
60+
}
61+
3362
function getResponseFromRequest(request: http.ClientRequest): Promise<Response> {
3463
// TODO: When we drop support for Node 6, consider using util.promisify instead of
3564
// constructing own Promise
@@ -56,7 +85,7 @@ function getResponseFromRequest(request: http.ClientRequest): Promise<Response>
5685
resolve({
5786
statusCode: incomingMessage.statusCode,
5887
body: responseData,
59-
headers: incomingMessage.headers,
88+
headers: createHeadersFromNodeIncomingMessage(incomingMessage),
6089
})
6190
})
6291
})

0 commit comments

Comments
 (0)