Skip to content

Commit ac7beba

Browse files
committed
lib: flatten access to primordials
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: #30731 PR-URL: #30610 Refs: #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]>
1 parent ee40a00 commit ac7beba

File tree

117 files changed

+1300
-932
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+1300
-932
lines changed

lib/_http_agent.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
'use strict';
2323

2424
const {
25-
Object: {
26-
setPrototypeOf: ObjectSetPrototypeOf,
27-
keys: ObjectKeys,
28-
values: ObjectValues
29-
}
25+
ObjectKeys,
26+
ObjectSetPrototypeOf,
27+
ObjectValues,
3028
} = primordials;
3129

3230
const net = require('net');

lib/_http_client.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121

2222
'use strict';
2323

24-
const { Object } = primordials;
24+
const {
25+
ObjectAssign,
26+
ObjectKeys,
27+
ObjectSetPrototypeOf,
28+
} = primordials;
2529

2630
const net = require('net');
2731
const url = require('url');
@@ -108,7 +112,7 @@ function ClientRequest(input, options, cb) {
108112
cb = options;
109113
options = input || {};
110114
} else {
111-
options = Object.assign(input || {}, options);
115+
options = ObjectAssign(input || {}, options);
112116
}
113117

114118
let agent = options.agent;
@@ -217,7 +221,7 @@ function ClientRequest(input, options, cb) {
217221
const headersArray = Array.isArray(options.headers);
218222
if (!headersArray) {
219223
if (options.headers) {
220-
const keys = Object.keys(options.headers);
224+
const keys = ObjectKeys(options.headers);
221225
for (let i = 0; i < keys.length; i++) {
222226
const key = keys[i];
223227
this.setHeader(key, options.headers[key]);
@@ -296,8 +300,8 @@ function ClientRequest(input, options, cb) {
296300

297301
this._deferToConnect(null, null, () => this._flush());
298302
}
299-
Object.setPrototypeOf(ClientRequest.prototype, OutgoingMessage.prototype);
300-
Object.setPrototypeOf(ClientRequest, OutgoingMessage);
303+
ObjectSetPrototypeOf(ClientRequest.prototype, OutgoingMessage.prototype);
304+
ObjectSetPrototypeOf(ClientRequest, OutgoingMessage);
301305

302306
ClientRequest.prototype._finish = function _finish() {
303307
DTRACE_HTTP_CLIENT_REQUEST(this, this.connection);

lib/_http_common.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
'use strict';
2323

24-
const { Math } = primordials;
24+
const {
25+
MathMin,
26+
} = primordials;
2527
const { setImmediate } = require('timers');
2628

2729
const { getOptionValue } = require('internal/options');
@@ -100,7 +102,7 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
100102

101103
// If parser.maxHeaderPairs <= 0 assume that there's no limit.
102104
if (parser.maxHeaderPairs > 0)
103-
n = Math.min(n, parser.maxHeaderPairs);
105+
n = MathMin(n, parser.maxHeaderPairs);
104106

105107
incoming._addHeaderLines(headers, n);
106108

lib/_http_incoming.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
'use strict';
2323

24-
const { Object } = primordials;
24+
const {
25+
ObjectSetPrototypeOf,
26+
} = primordials;
2527

2628
const Stream = require('stream');
2729

@@ -81,8 +83,8 @@ function IncomingMessage(socket) {
8183
// read by the user, so there's no point continuing to handle it.
8284
this._dumped = false;
8385
}
84-
Object.setPrototypeOf(IncomingMessage.prototype, Stream.Readable.prototype);
85-
Object.setPrototypeOf(IncomingMessage, Stream.Readable);
86+
ObjectSetPrototypeOf(IncomingMessage.prototype, Stream.Readable.prototype);
87+
ObjectSetPrototypeOf(IncomingMessage, Stream.Readable);
8688

8789
IncomingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
8890
if (callback)

lib/_http_outgoing.js

+30-24
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@
2121

2222
'use strict';
2323

24-
const { Object, ObjectPrototype } = primordials;
24+
const {
25+
ObjectCreate,
26+
ObjectDefineProperty,
27+
ObjectKeys,
28+
ObjectPrototypeHasOwnProperty,
29+
ObjectSetPrototypeOf,
30+
} = primordials;
2531

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

110116
this._onPendingData = noopPendingOutput;
111117
}
112-
Object.setPrototypeOf(OutgoingMessage.prototype, Stream.prototype);
113-
Object.setPrototypeOf(OutgoingMessage, Stream);
118+
ObjectSetPrototypeOf(OutgoingMessage.prototype, Stream.prototype);
119+
ObjectSetPrototypeOf(OutgoingMessage, Stream);
114120

115-
Object.defineProperty(OutgoingMessage.prototype, 'writableFinished', {
121+
ObjectDefineProperty(OutgoingMessage.prototype, 'writableFinished', {
116122
get() {
117123
return (
118124
this.finished &&
@@ -122,41 +128,41 @@ Object.defineProperty(OutgoingMessage.prototype, 'writableFinished', {
122128
}
123129
});
124130

125-
Object.defineProperty(OutgoingMessage.prototype, 'writableObjectMode', {
131+
ObjectDefineProperty(OutgoingMessage.prototype, 'writableObjectMode', {
126132
get() {
127133
return false;
128134
}
129135
});
130136

131-
Object.defineProperty(OutgoingMessage.prototype, 'writableLength', {
137+
ObjectDefineProperty(OutgoingMessage.prototype, 'writableLength', {
132138
get() {
133139
return this.outputSize + (this.socket ? this.socket.writableLength : 0);
134140
}
135141
});
136142

137-
Object.defineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
143+
ObjectDefineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
138144
get() {
139145
return this.socket ? this.socket.writableHighWaterMark : HIGH_WATER_MARK;
140146
}
141147
});
142148

143-
Object.defineProperty(OutgoingMessage.prototype, 'writableCorked', {
149+
ObjectDefineProperty(OutgoingMessage.prototype, 'writableCorked', {
144150
get() {
145151
const corked = this.socket ? this.socket.writableCorked : 0;
146152
return corked + this[kCorked];
147153
}
148154
});
149155

150-
Object.defineProperty(OutgoingMessage.prototype, '_headers', {
156+
ObjectDefineProperty(OutgoingMessage.prototype, '_headers', {
151157
get: internalUtil.deprecate(function() {
152158
return this.getHeaders();
153159
}, 'OutgoingMessage.prototype._headers is deprecated', 'DEP0066'),
154160
set: internalUtil.deprecate(function(val) {
155161
if (val == null) {
156162
this[kOutHeaders] = null;
157163
} else if (typeof val === 'object') {
158-
const headers = this[kOutHeaders] = Object.create(null);
159-
const keys = Object.keys(val);
164+
const headers = this[kOutHeaders] = ObjectCreate(null);
165+
const keys = ObjectKeys(val);
160166
for (var i = 0; i < keys.length; ++i) {
161167
const name = keys[i];
162168
headers[name.toLowerCase()] = [name, val[name]];
@@ -165,12 +171,12 @@ Object.defineProperty(OutgoingMessage.prototype, '_headers', {
165171
}, 'OutgoingMessage.prototype._headers is deprecated', 'DEP0066')
166172
});
167173

168-
Object.defineProperty(OutgoingMessage.prototype, '_headerNames', {
174+
ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
169175
get: internalUtil.deprecate(function() {
170176
const headers = this[kOutHeaders];
171177
if (headers !== null) {
172-
const out = Object.create(null);
173-
const keys = Object.keys(headers);
178+
const out = ObjectCreate(null);
179+
const keys = ObjectKeys(headers);
174180
for (var i = 0; i < keys.length; ++i) {
175181
const key = keys[i];
176182
const val = headers[key][0];
@@ -185,7 +191,7 @@ Object.defineProperty(OutgoingMessage.prototype, '_headerNames', {
185191
const headers = this[kOutHeaders];
186192
if (!headers)
187193
return;
188-
const keys = Object.keys(val);
194+
const keys = ObjectKeys(val);
189195
for (var i = 0; i < keys.length; ++i) {
190196
const header = headers[keys[i]];
191197
if (header)
@@ -205,7 +211,7 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
205211
const headers = {};
206212

207213
if (headersMap !== null) {
208-
const keys = Object.keys(headersMap);
214+
const keys = ObjectKeys(headersMap);
209215
for (var i = 0, l = keys.length; i < l; i++) {
210216
const key = keys[i];
211217
headers[headersMap[key][0]] = headersMap[key][1];
@@ -350,7 +356,7 @@ function _storeHeader(firstLine, headers) {
350356
}
351357
} else {
352358
for (const key in headers) {
353-
if (ObjectPrototype.hasOwnProperty(headers, key)) {
359+
if (ObjectPrototypeHasOwnProperty(headers, key)) {
354360
processHeader(this, state, key, headers[key], true);
355361
}
356362
}
@@ -511,7 +517,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
511517

512518
let headers = this[kOutHeaders];
513519
if (headers === null)
514-
this[kOutHeaders] = headers = Object.create(null);
520+
this[kOutHeaders] = headers = ObjectCreate(null);
515521

516522
headers[name.toLowerCase()] = [name, value];
517523
};
@@ -531,16 +537,16 @@ OutgoingMessage.prototype.getHeader = function getHeader(name) {
531537

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

537543

538544
// Returns a shallow copy of the current outgoing headers.
539545
OutgoingMessage.prototype.getHeaders = function getHeaders() {
540546
const headers = this[kOutHeaders];
541-
const ret = Object.create(null);
547+
const ret = ObjectCreate(null);
542548
if (headers) {
543-
const keys = Object.keys(headers);
549+
const keys = ObjectKeys(headers);
544550
for (var i = 0; i < keys.length; ++i) {
545551
const key = keys[i];
546552
const val = headers[key][1];
@@ -592,13 +598,13 @@ OutgoingMessage.prototype._implicitHeader = function _implicitHeader() {
592598
this.emit('error', new ERR_METHOD_NOT_IMPLEMENTED('_implicitHeader()'));
593599
};
594600

595-
Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {
601+
ObjectDefineProperty(OutgoingMessage.prototype, 'headersSent', {
596602
configurable: true,
597603
enumerable: true,
598604
get: function() { return !!this._header; }
599605
});
600606

601-
Object.defineProperty(OutgoingMessage.prototype, 'writableEnded', {
607+
ObjectDefineProperty(OutgoingMessage.prototype, 'writableEnded', {
602608
get: function() { return this.finished; }
603609
});
604610

@@ -679,7 +685,7 @@ function connectionCorkNT(conn) {
679685

680686
OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
681687
this._trailer = '';
682-
const keys = Object.keys(headers);
688+
const keys = ObjectKeys(headers);
683689
const isArray = Array.isArray(headers);
684690
var field, value;
685691
for (var i = 0, l = keys.length; i < l; i++) {

lib/_http_server.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
'use strict';
2323

2424
const {
25-
Object: {
26-
setPrototypeOf: ObjectSetPrototypeOf,
27-
keys: ObjectKeys,
28-
}
25+
ObjectKeys,
26+
ObjectSetPrototypeOf,
2927
} = primordials;
3028

3129
const net = require('net');
@@ -265,7 +263,7 @@ function writeHead(statusCode, reason, obj) {
265263
let k;
266264
if (obj) {
267265
const keys = ObjectKeys(obj);
268-
for (var i = 0; i < keys.length; i++) {
266+
for (let i = 0; i < keys.length; i++) {
269267
k = keys[i];
270268
if (k) this.setHeader(k, obj[k]);
271269
}

lib/_stream_duplex.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,23 @@
2626

2727
'use strict';
2828

29-
const { Object } = primordials;
29+
const {
30+
ObjectDefineProperty,
31+
ObjectKeys,
32+
ObjectSetPrototypeOf,
33+
} = primordials;
3034

3135
module.exports = Duplex;
3236

3337
const Readable = require('_stream_readable');
3438
const Writable = require('_stream_writable');
3539

36-
Object.setPrototypeOf(Duplex.prototype, Readable.prototype);
37-
Object.setPrototypeOf(Duplex, Readable);
40+
ObjectSetPrototypeOf(Duplex.prototype, Readable.prototype);
41+
ObjectSetPrototypeOf(Duplex, Readable);
3842

3943
{
4044
// Allow the keys array to be GC'ed.
41-
const keys = Object.keys(Writable.prototype);
45+
const keys = ObjectKeys(Writable.prototype);
4246
for (let v = 0; v < keys.length; v++) {
4347
const method = keys[v];
4448
if (!Duplex.prototype[method])
@@ -68,7 +72,7 @@ function Duplex(options) {
6872
}
6973
}
7074

71-
Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', {
75+
ObjectDefineProperty(Duplex.prototype, 'writableHighWaterMark', {
7276
// Making it explicit this property is not enumerable
7377
// because otherwise some prototype manipulation in
7478
// userland will fail
@@ -78,7 +82,7 @@ Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', {
7882
}
7983
});
8084

81-
Object.defineProperty(Duplex.prototype, 'writableBuffer', {
85+
ObjectDefineProperty(Duplex.prototype, 'writableBuffer', {
8286
// Making it explicit this property is not enumerable
8387
// because otherwise some prototype manipulation in
8488
// userland will fail
@@ -88,7 +92,7 @@ Object.defineProperty(Duplex.prototype, 'writableBuffer', {
8892
}
8993
});
9094

91-
Object.defineProperty(Duplex.prototype, 'writableLength', {
95+
ObjectDefineProperty(Duplex.prototype, 'writableLength', {
9296
// Making it explicit this property is not enumerable
9397
// because otherwise some prototype manipulation in
9498
// userland will fail
@@ -98,7 +102,7 @@ Object.defineProperty(Duplex.prototype, 'writableLength', {
98102
}
99103
});
100104

101-
Object.defineProperty(Duplex.prototype, 'writableFinished', {
105+
ObjectDefineProperty(Duplex.prototype, 'writableFinished', {
102106
// Making it explicit this property is not enumerable
103107
// because otherwise some prototype manipulation in
104108
// userland will fail
@@ -108,7 +112,7 @@ Object.defineProperty(Duplex.prototype, 'writableFinished', {
108112
}
109113
});
110114

111-
Object.defineProperty(Duplex.prototype, 'writableCorked', {
115+
ObjectDefineProperty(Duplex.prototype, 'writableCorked', {
112116
// Making it explicit this property is not enumerable
113117
// because otherwise some prototype manipulation in
114118
// userland will fail
@@ -118,7 +122,7 @@ Object.defineProperty(Duplex.prototype, 'writableCorked', {
118122
}
119123
});
120124

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

146-
Object.defineProperty(Duplex.prototype, 'destroyed', {
150+
ObjectDefineProperty(Duplex.prototype, 'destroyed', {
147151
// Making it explicit this property is not enumerable
148152
// because otherwise some prototype manipulation in
149153
// userland will fail

0 commit comments

Comments
 (0)