Skip to content

Commit

Permalink
http2: refactor to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: #36142
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
aduh95 committed Nov 20, 2020
1 parent 700612f commit 514f464
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 66 deletions.
31 changes: 19 additions & 12 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

const {
ArrayIsArray,
ArrayPrototypePush,
Boolean,
FunctionPrototypeBind,
ObjectAssign,
ObjectCreate,
ObjectKeys,
ObjectPrototypeHasOwnProperty,
ReflectApply,
ReflectGetPrototypeOf,
StringPrototypeToLowerCase,
StringPrototypeTrim,
Symbol,
} = primordials;

Expand Down Expand Up @@ -138,7 +143,7 @@ function onStreamTrailers(trailers, flags, rawTrailers) {
const request = this[kRequest];
if (request !== undefined) {
ObjectAssign(request[kTrailers], trailers);
request[kRawTrailers].push(...rawTrailers);
ArrayPrototypePush(request[kRawTrailers], ...rawTrailers);
}
}

Expand Down Expand Up @@ -200,7 +205,7 @@ const proxySocketHandler = {
case 'end':
case 'emit':
case 'destroy':
return stream[prop].bind(stream);
return FunctionPrototypeBind(stream[prop], stream);
case 'writable':
case 'destroyed':
return stream[prop];
Expand All @@ -212,8 +217,8 @@ const proxySocketHandler = {
case 'setTimeout':
const session = stream.session;
if (session !== undefined)
return session.setTimeout.bind(session);
return stream.setTimeout.bind(stream);
return FunctionPrototypeBind(session.setTimeout, session);
return FunctionPrototypeBind(stream.setTimeout, stream);
case 'write':
case 'read':
case 'pause':
Expand All @@ -223,7 +228,9 @@ const proxySocketHandler = {
const ref = stream.session !== undefined ?
stream.session[kSocket] : stream;
const value = ref[prop];
return typeof value === 'function' ? value.bind(ref) : value;
return typeof value === 'function' ?
FunctionPrototypeBind(value, ref) :
value;
}
},
getPrototypeOf(stream) {
Expand Down Expand Up @@ -394,7 +401,7 @@ class Http2ServerRequest extends Readable {

set method(method) {
validateString(method, 'method');
if (method.trim() === '')
if (StringPrototypeTrim(method) === '')
throw new ERR_INVALID_ARG_VALUE('method', method);

this[kHeaders][HTTP2_HEADER_METHOD] = method;
Expand Down Expand Up @@ -554,7 +561,7 @@ class Http2ServerResponse extends Stream {

setTrailer(name, value) {
validateString(name, 'name');
name = name.trim().toLowerCase();
name = StringPrototypeToLowerCase(StringPrototypeTrim(name));
assertValidHeader(name, value);
this[kTrailers][name] = value;
}
Expand All @@ -570,7 +577,7 @@ class Http2ServerResponse extends Stream {

getHeader(name) {
validateString(name, 'name');
name = name.trim().toLowerCase();
name = StringPrototypeToLowerCase(StringPrototypeTrim(name));
return this[kHeaders][name];
}

Expand All @@ -585,7 +592,7 @@ class Http2ServerResponse extends Stream {

hasHeader(name) {
validateString(name, 'name');
name = name.trim().toLowerCase();
name = StringPrototypeToLowerCase(StringPrototypeTrim(name));
return ObjectPrototypeHasOwnProperty(this[kHeaders], name);
}

Expand All @@ -594,7 +601,7 @@ class Http2ServerResponse extends Stream {
if (this[kStream].headersSent)
throw new ERR_HTTP2_HEADERS_SENT();

name = name.trim().toLowerCase();
name = StringPrototypeToLowerCase(StringPrototypeTrim(name));

if (name === 'date') {
this[kState].sendDate = false;
Expand All @@ -614,7 +621,7 @@ class Http2ServerResponse extends Stream {
}

[kSetHeader](name, value) {
name = name.trim().toLowerCase();
name = StringPrototypeToLowerCase(StringPrototypeTrim(name));
assertValidHeader(name, value);

if (!isConnectionHeaderAllowed(name, value)) {
Expand Down Expand Up @@ -755,7 +762,7 @@ class Http2ServerResponse extends Stream {
this.writeHead(this[kState].statusCode);

if (this[kState].closed || stream.destroyed)
onStreamCloseResponse.call(stream);
ReflectApply(onStreamCloseResponse, stream, []);
else
stream.end();

Expand Down
Loading

0 comments on commit 514f464

Please sign in to comment.