Skip to content

Commit a4dfe3b

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 <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent cbd5026 commit a4dfe3b

File tree

117 files changed

+1308
-937
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

+1308
-937
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');
@@ -107,7 +111,7 @@ function ClientRequest(input, options, cb) {
107111
cb = options;
108112
options = input || {};
109113
} else {
110-
options = Object.assign(input || {}, options);
114+
options = ObjectAssign(input || {}, options);
111115
}
112116

113117
let agent = options.agent;
@@ -216,7 +220,7 @@ function ClientRequest(input, options, cb) {
216220
const headersArray = Array.isArray(options.headers);
217221
if (!headersArray) {
218222
if (options.headers) {
219-
const keys = Object.keys(options.headers);
223+
const keys = ObjectKeys(options.headers);
220224
for (let i = 0; i < keys.length; i++) {
221225
const key = keys[i];
222226
this.setHeader(key, options.headers[key]);
@@ -295,8 +299,8 @@ function ClientRequest(input, options, cb) {
295299

296300
this._deferToConnect(null, null, () => this._flush());
297301
}
298-
Object.setPrototypeOf(ClientRequest.prototype, OutgoingMessage.prototype);
299-
Object.setPrototypeOf(ClientRequest, OutgoingMessage);
302+
ObjectSetPrototypeOf(ClientRequest.prototype, OutgoingMessage.prototype);
303+
ObjectSetPrototypeOf(ClientRequest, OutgoingMessage);
300304

301305
ClientRequest.prototype._finish = function _finish() {
302306
DTRACE_HTTP_CLIENT_REQUEST(this, this.socket);

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 { methods, HTTPParser } = internalBinding('http_parser');
@@ -95,7 +97,7 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
9597

9698
// If parser.maxHeaderPairs <= 0 assume that there's no limit.
9799
if (parser.maxHeaderPairs > 0)
98-
n = Math.min(n, parser.maxHeaderPairs);
100+
n = MathMin(n, parser.maxHeaderPairs);
99101

100102
incoming._addHeaderLines(headers, n);
101103

lib/_http_incoming.js

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

2222
'use strict';
2323

24-
const { Object } = primordials;
24+
const {
25+
ObjectDefineProperty,
26+
ObjectSetPrototypeOf,
27+
} = primordials;
2528

2629
const Stream = require('stream');
2730

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

86-
Object.defineProperty(IncomingMessage.prototype, 'connection', {
89+
ObjectDefineProperty(IncomingMessage.prototype, 'connection', {
8790
get: function() {
8891
return this.socket;
8992
},

lib/_http_outgoing.js

+31-25
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,7 +171,7 @@ Object.defineProperty(OutgoingMessage.prototype, '_headers', {
165171
}, 'OutgoingMessage.prototype._headers is deprecated', 'DEP0066')
166172
});
167173

168-
Object.defineProperty(OutgoingMessage.prototype, 'connection', {
174+
ObjectDefineProperty(OutgoingMessage.prototype, 'connection', {
169175
get: function() {
170176
return this.socket;
171177
},
@@ -174,12 +180,12 @@ Object.defineProperty(OutgoingMessage.prototype, 'connection', {
174180
}
175181
});
176182

177-
Object.defineProperty(OutgoingMessage.prototype, '_headerNames', {
183+
ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
178184
get: internalUtil.deprecate(function() {
179185
const headers = this[kOutHeaders];
180186
if (headers !== null) {
181-
const out = Object.create(null);
182-
const keys = Object.keys(headers);
187+
const out = ObjectCreate(null);
188+
const keys = ObjectKeys(headers);
183189
for (var i = 0; i < keys.length; ++i) {
184190
const key = keys[i];
185191
const val = headers[key][0];
@@ -194,7 +200,7 @@ Object.defineProperty(OutgoingMessage.prototype, '_headerNames', {
194200
const headers = this[kOutHeaders];
195201
if (!headers)
196202
return;
197-
const keys = Object.keys(val);
203+
const keys = ObjectKeys(val);
198204
for (var i = 0; i < keys.length; ++i) {
199205
const header = headers[keys[i]];
200206
if (header)
@@ -214,7 +220,7 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
214220
const headers = {};
215221

216222
if (headersMap !== null) {
217-
const keys = Object.keys(headersMap);
223+
const keys = ObjectKeys(headersMap);
218224
for (var i = 0, l = keys.length; i < l; i++) {
219225
const key = keys[i];
220226
headers[headersMap[key][0]] = headersMap[key][1];
@@ -359,7 +365,7 @@ function _storeHeader(firstLine, headers) {
359365
}
360366
} else {
361367
for (const key in headers) {
362-
if (ObjectPrototype.hasOwnProperty(headers, key)) {
368+
if (ObjectPrototypeHasOwnProperty(headers, key)) {
363369
processHeader(this, state, key, headers[key], true);
364370
}
365371
}
@@ -520,7 +526,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
520526

521527
let headers = this[kOutHeaders];
522528
if (headers === null)
523-
this[kOutHeaders] = headers = Object.create(null);
529+
this[kOutHeaders] = headers = ObjectCreate(null);
524530

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

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

546552

547553
// Returns a shallow copy of the current outgoing headers.
548554
OutgoingMessage.prototype.getHeaders = function getHeaders() {
549555
const headers = this[kOutHeaders];
550-
const ret = Object.create(null);
556+
const ret = ObjectCreate(null);
551557
if (headers) {
552-
const keys = Object.keys(headers);
558+
const keys = ObjectKeys(headers);
553559
for (var i = 0; i < keys.length; ++i) {
554560
const key = keys[i];
555561
const val = headers[key][1];
@@ -601,13 +607,13 @@ OutgoingMessage.prototype._implicitHeader = function _implicitHeader() {
601607
this.emit('error', new ERR_METHOD_NOT_IMPLEMENTED('_implicitHeader()'));
602608
};
603609

604-
Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {
610+
ObjectDefineProperty(OutgoingMessage.prototype, 'headersSent', {
605611
configurable: true,
606612
enumerable: true,
607613
get: function() { return !!this._header; }
608614
});
609615

610-
Object.defineProperty(OutgoingMessage.prototype, 'writableEnded', {
616+
ObjectDefineProperty(OutgoingMessage.prototype, 'writableEnded', {
611617
get: function() { return this.finished; }
612618
});
613619

@@ -688,7 +694,7 @@ function connectionCorkNT(conn) {
688694

689695
OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
690696
this._trailer = '';
691-
const keys = Object.keys(headers);
697+
const keys = ObjectKeys(headers);
692698
const isArray = Array.isArray(headers);
693699
var field, value;
694700
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');
@@ -260,7 +258,7 @@ function writeHead(statusCode, reason, obj) {
260258
let k;
261259
if (obj) {
262260
const keys = ObjectKeys(obj);
263-
for (var i = 0; i < keys.length; i++) {
261+
for (let i = 0; i < keys.length; i++) {
264262
k = keys[i];
265263
if (k) this.setHeader(k, obj[k]);
266264
}

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)