Skip to content

Commit

Permalink
lib: flatten access to primordials
Browse files Browse the repository at this point in the history
Store all primordials as properties of the primordials object.
Static functions are prefixed by the constructor's name and prototype
methods are prefixed by the constructor's name followed by "Prototype".
For example: primordials.Object.keys becomes primordials.ObjectKeys.

Backport-PR-URL: nodejs#30731
PR-URL: nodejs#30610
Refs: nodejs#29766
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
  • Loading branch information
targos committed Nov 30, 2019
1 parent be84cee commit 7fa72d9
Show file tree
Hide file tree
Showing 117 changed files with 1,308 additions and 937 deletions.
8 changes: 3 additions & 5 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
'use strict';

const {
Object: {
setPrototypeOf: ObjectSetPrototypeOf,
keys: ObjectKeys,
values: ObjectValues
}
ObjectKeys,
ObjectSetPrototypeOf,
ObjectValues,
} = primordials;

const net = require('net');
Expand Down
14 changes: 9 additions & 5 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@

'use strict';

const { Object } = primordials;
const {
ObjectAssign,
ObjectKeys,
ObjectSetPrototypeOf,
} = primordials;

const net = require('net');
const url = require('url');
Expand Down Expand Up @@ -107,7 +111,7 @@ function ClientRequest(input, options, cb) {
cb = options;
options = input || {};
} else {
options = Object.assign(input || {}, options);
options = ObjectAssign(input || {}, options);
}

let agent = options.agent;
Expand Down Expand Up @@ -216,7 +220,7 @@ function ClientRequest(input, options, cb) {
const headersArray = Array.isArray(options.headers);
if (!headersArray) {
if (options.headers) {
const keys = Object.keys(options.headers);
const keys = ObjectKeys(options.headers);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
this.setHeader(key, options.headers[key]);
Expand Down Expand Up @@ -295,8 +299,8 @@ function ClientRequest(input, options, cb) {

this._deferToConnect(null, null, () => this._flush());
}
Object.setPrototypeOf(ClientRequest.prototype, OutgoingMessage.prototype);
Object.setPrototypeOf(ClientRequest, OutgoingMessage);
ObjectSetPrototypeOf(ClientRequest.prototype, OutgoingMessage.prototype);
ObjectSetPrototypeOf(ClientRequest, OutgoingMessage);

ClientRequest.prototype._finish = function _finish() {
DTRACE_HTTP_CLIENT_REQUEST(this, this.socket);
Expand Down
6 changes: 4 additions & 2 deletions lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

'use strict';

const { Math } = primordials;
const {
MathMin,
} = primordials;
const { setImmediate } = require('timers');

const { methods, HTTPParser } = internalBinding('http_parser');
Expand Down Expand Up @@ -95,7 +97,7 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,

// If parser.maxHeaderPairs <= 0 assume that there's no limit.
if (parser.maxHeaderPairs > 0)
n = Math.min(n, parser.maxHeaderPairs);
n = MathMin(n, parser.maxHeaderPairs);

incoming._addHeaderLines(headers, n);

Expand Down
11 changes: 7 additions & 4 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@

'use strict';

const { Object } = primordials;
const {
ObjectDefineProperty,
ObjectSetPrototypeOf,
} = primordials;

const Stream = require('stream');

Expand Down Expand Up @@ -80,10 +83,10 @@ function IncomingMessage(socket) {
// read by the user, so there's no point continuing to handle it.
this._dumped = false;
}
Object.setPrototypeOf(IncomingMessage.prototype, Stream.Readable.prototype);
Object.setPrototypeOf(IncomingMessage, Stream.Readable);
ObjectSetPrototypeOf(IncomingMessage.prototype, Stream.Readable.prototype);
ObjectSetPrototypeOf(IncomingMessage, Stream.Readable);

Object.defineProperty(IncomingMessage.prototype, 'connection', {
ObjectDefineProperty(IncomingMessage.prototype, 'connection', {
get: function() {
return this.socket;
},
Expand Down
56 changes: 31 additions & 25 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@

'use strict';

const { Object, ObjectPrototype } = primordials;
const {
ObjectCreate,
ObjectDefineProperty,
ObjectKeys,
ObjectPrototypeHasOwnProperty,
ObjectSetPrototypeOf,
} = primordials;

const { getDefaultHighWaterMark } = require('internal/streams/state');
const assert = require('internal/assert');
Expand Down Expand Up @@ -109,10 +115,10 @@ function OutgoingMessage() {

this._onPendingData = noopPendingOutput;
}
Object.setPrototypeOf(OutgoingMessage.prototype, Stream.prototype);
Object.setPrototypeOf(OutgoingMessage, Stream);
ObjectSetPrototypeOf(OutgoingMessage.prototype, Stream.prototype);
ObjectSetPrototypeOf(OutgoingMessage, Stream);

Object.defineProperty(OutgoingMessage.prototype, 'writableFinished', {
ObjectDefineProperty(OutgoingMessage.prototype, 'writableFinished', {
get() {
return (
this.finished &&
Expand All @@ -122,41 +128,41 @@ Object.defineProperty(OutgoingMessage.prototype, 'writableFinished', {
}
});

Object.defineProperty(OutgoingMessage.prototype, 'writableObjectMode', {
ObjectDefineProperty(OutgoingMessage.prototype, 'writableObjectMode', {
get() {
return false;
}
});

Object.defineProperty(OutgoingMessage.prototype, 'writableLength', {
ObjectDefineProperty(OutgoingMessage.prototype, 'writableLength', {
get() {
return this.outputSize + (this.socket ? this.socket.writableLength : 0);
}
});

Object.defineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
ObjectDefineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
get() {
return this.socket ? this.socket.writableHighWaterMark : HIGH_WATER_MARK;
}
});

Object.defineProperty(OutgoingMessage.prototype, 'writableCorked', {
ObjectDefineProperty(OutgoingMessage.prototype, 'writableCorked', {
get() {
const corked = this.socket ? this.socket.writableCorked : 0;
return corked + this[kCorked];
}
});

Object.defineProperty(OutgoingMessage.prototype, '_headers', {
ObjectDefineProperty(OutgoingMessage.prototype, '_headers', {
get: internalUtil.deprecate(function() {
return this.getHeaders();
}, 'OutgoingMessage.prototype._headers is deprecated', 'DEP0066'),
set: internalUtil.deprecate(function(val) {
if (val == null) {
this[kOutHeaders] = null;
} else if (typeof val === 'object') {
const headers = this[kOutHeaders] = Object.create(null);
const keys = Object.keys(val);
const headers = this[kOutHeaders] = ObjectCreate(null);
const keys = ObjectKeys(val);
for (var i = 0; i < keys.length; ++i) {
const name = keys[i];
headers[name.toLowerCase()] = [name, val[name]];
Expand All @@ -165,7 +171,7 @@ Object.defineProperty(OutgoingMessage.prototype, '_headers', {
}, 'OutgoingMessage.prototype._headers is deprecated', 'DEP0066')
});

Object.defineProperty(OutgoingMessage.prototype, 'connection', {
ObjectDefineProperty(OutgoingMessage.prototype, 'connection', {
get: function() {
return this.socket;
},
Expand All @@ -174,12 +180,12 @@ Object.defineProperty(OutgoingMessage.prototype, 'connection', {
}
});

Object.defineProperty(OutgoingMessage.prototype, '_headerNames', {
ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
get: internalUtil.deprecate(function() {
const headers = this[kOutHeaders];
if (headers !== null) {
const out = Object.create(null);
const keys = Object.keys(headers);
const out = ObjectCreate(null);
const keys = ObjectKeys(headers);
for (var i = 0; i < keys.length; ++i) {
const key = keys[i];
const val = headers[key][0];
Expand All @@ -194,7 +200,7 @@ Object.defineProperty(OutgoingMessage.prototype, '_headerNames', {
const headers = this[kOutHeaders];
if (!headers)
return;
const keys = Object.keys(val);
const keys = ObjectKeys(val);
for (var i = 0; i < keys.length; ++i) {
const header = headers[keys[i]];
if (header)
Expand All @@ -214,7 +220,7 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
const headers = {};

if (headersMap !== null) {
const keys = Object.keys(headersMap);
const keys = ObjectKeys(headersMap);
for (var i = 0, l = keys.length; i < l; i++) {
const key = keys[i];
headers[headersMap[key][0]] = headersMap[key][1];
Expand Down Expand Up @@ -359,7 +365,7 @@ function _storeHeader(firstLine, headers) {
}
} else {
for (const key in headers) {
if (ObjectPrototype.hasOwnProperty(headers, key)) {
if (ObjectPrototypeHasOwnProperty(headers, key)) {
processHeader(this, state, key, headers[key], true);
}
}
Expand Down Expand Up @@ -520,7 +526,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {

let headers = this[kOutHeaders];
if (headers === null)
this[kOutHeaders] = headers = Object.create(null);
this[kOutHeaders] = headers = ObjectCreate(null);

headers[name.toLowerCase()] = [name, value];
};
Expand All @@ -540,16 +546,16 @@ OutgoingMessage.prototype.getHeader = function getHeader(name) {

// Returns an array of the names of the current outgoing headers.
OutgoingMessage.prototype.getHeaderNames = function getHeaderNames() {
return this[kOutHeaders] !== null ? Object.keys(this[kOutHeaders]) : [];
return this[kOutHeaders] !== null ? ObjectKeys(this[kOutHeaders]) : [];
};


// Returns a shallow copy of the current outgoing headers.
OutgoingMessage.prototype.getHeaders = function getHeaders() {
const headers = this[kOutHeaders];
const ret = Object.create(null);
const ret = ObjectCreate(null);
if (headers) {
const keys = Object.keys(headers);
const keys = ObjectKeys(headers);
for (var i = 0; i < keys.length; ++i) {
const key = keys[i];
const val = headers[key][1];
Expand Down Expand Up @@ -601,13 +607,13 @@ OutgoingMessage.prototype._implicitHeader = function _implicitHeader() {
this.emit('error', new ERR_METHOD_NOT_IMPLEMENTED('_implicitHeader()'));
};

Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {
ObjectDefineProperty(OutgoingMessage.prototype, 'headersSent', {
configurable: true,
enumerable: true,
get: function() { return !!this._header; }
});

Object.defineProperty(OutgoingMessage.prototype, 'writableEnded', {
ObjectDefineProperty(OutgoingMessage.prototype, 'writableEnded', {
get: function() { return this.finished; }
});

Expand Down Expand Up @@ -688,7 +694,7 @@ function connectionCorkNT(conn) {

OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
this._trailer = '';
const keys = Object.keys(headers);
const keys = ObjectKeys(headers);
const isArray = Array.isArray(headers);
var field, value;
for (var i = 0, l = keys.length; i < l; i++) {
Expand Down
8 changes: 3 additions & 5 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
'use strict';

const {
Object: {
setPrototypeOf: ObjectSetPrototypeOf,
keys: ObjectKeys,
}
ObjectKeys,
ObjectSetPrototypeOf,
} = primordials;

const net = require('net');
Expand Down Expand Up @@ -260,7 +258,7 @@ function writeHead(statusCode, reason, obj) {
let k;
if (obj) {
const keys = ObjectKeys(obj);
for (var i = 0; i < keys.length; i++) {
for (let i = 0; i < keys.length; i++) {
k = keys[i];
if (k) this.setHeader(k, obj[k]);
}
Expand Down
26 changes: 15 additions & 11 deletions lib/_stream_duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@

'use strict';

const { Object } = primordials;
const {
ObjectDefineProperty,
ObjectKeys,
ObjectSetPrototypeOf,
} = primordials;

module.exports = Duplex;

const Readable = require('_stream_readable');
const Writable = require('_stream_writable');

Object.setPrototypeOf(Duplex.prototype, Readable.prototype);
Object.setPrototypeOf(Duplex, Readable);
ObjectSetPrototypeOf(Duplex.prototype, Readable.prototype);
ObjectSetPrototypeOf(Duplex, Readable);

{
// Allow the keys array to be GC'ed.
const keys = Object.keys(Writable.prototype);
const keys = ObjectKeys(Writable.prototype);
for (let v = 0; v < keys.length; v++) {
const method = keys[v];
if (!Duplex.prototype[method])
Expand Down Expand Up @@ -68,7 +72,7 @@ function Duplex(options) {
}
}

Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', {
ObjectDefineProperty(Duplex.prototype, 'writableHighWaterMark', {
// Making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
Expand All @@ -78,7 +82,7 @@ Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', {
}
});

Object.defineProperty(Duplex.prototype, 'writableBuffer', {
ObjectDefineProperty(Duplex.prototype, 'writableBuffer', {
// Making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
Expand All @@ -88,7 +92,7 @@ Object.defineProperty(Duplex.prototype, 'writableBuffer', {
}
});

Object.defineProperty(Duplex.prototype, 'writableLength', {
ObjectDefineProperty(Duplex.prototype, 'writableLength', {
// Making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
Expand All @@ -98,7 +102,7 @@ Object.defineProperty(Duplex.prototype, 'writableLength', {
}
});

Object.defineProperty(Duplex.prototype, 'writableFinished', {
ObjectDefineProperty(Duplex.prototype, 'writableFinished', {
// Making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
Expand All @@ -108,7 +112,7 @@ Object.defineProperty(Duplex.prototype, 'writableFinished', {
}
});

Object.defineProperty(Duplex.prototype, 'writableCorked', {
ObjectDefineProperty(Duplex.prototype, 'writableCorked', {
// Making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
Expand All @@ -118,7 +122,7 @@ Object.defineProperty(Duplex.prototype, 'writableCorked', {
}
});

Object.defineProperty(Duplex.prototype, 'writableEnded', {
ObjectDefineProperty(Duplex.prototype, 'writableEnded', {
// Making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
Expand All @@ -143,7 +147,7 @@ function onEndNT(self) {
self.end();
}

Object.defineProperty(Duplex.prototype, 'destroyed', {
ObjectDefineProperty(Duplex.prototype, 'destroyed', {
// Making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
Expand Down
Loading

0 comments on commit 7fa72d9

Please sign in to comment.