Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http2: refactor to use more primordials #36142

Merged
merged 1 commit into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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