Skip to content

Commit

Permalink
http2: use Object.create(null) for getHeaders
Browse files Browse the repository at this point in the history
refactor `getHeaders` to initialize headers using
`Object.create(null)`

Refs: nodejs#29829

PR-URL: nodejs#33188
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Sebastiaan Deckers <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
  • Loading branch information
rexagod authored and mcollina committed May 29, 2020
1 parent 6a1df3b commit c095808
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,8 @@ class Http2ServerResponse extends Stream {
}

getHeaders() {
return { ...this[kHeaders] };
const headers = ObjectCreate(null);
return ObjectAssign(headers, this[kHeaders]);
}

hasHeader(name) {
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-http2-compat-serverresponse-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ server.listen(0, common.mustCall(function() {
response.setHeader(real, expectedValue);
const expectedHeaderNames = [real];
assert.deepStrictEqual(response.getHeaderNames(), expectedHeaderNames);
const expectedHeaders = { [real]: expectedValue };
const expectedHeaders = Object.create(null);
expectedHeaders[real] = expectedValue;
assert.deepStrictEqual(response.getHeaders(), expectedHeaders);

response.getHeaders()[fake] = fake;
assert.strictEqual(response.hasHeader(fake), false);
assert.strictEqual(Object.getPrototypeOf(response.getHeaders()), null);

assert.strictEqual(response.sendDate, true);
response.sendDate = false;
Expand Down

0 comments on commit c095808

Please sign in to comment.