GeezerVison is showing github.com — http: refactor to use more primordials · nodejs/node@5e499c4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/_http_agent.js +28-17Lines changed: 28 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | 'use strict'; | 23 | 23 |
| 24 | 24 | const { | | 25 | + ArrayPrototypeIncludes, | | 26 | + ArrayPrototypeIndexOf, | | 27 | + ArrayPrototypePop, | | 28 | + ArrayPrototypePush, | | 29 | + ArrayPrototypeShift, | | 30 | + ArrayPrototypeSplice, | | 31 | + FunctionPrototypeCall, | 25 | 32 | NumberIsNaN, | 26 | 33 | ObjectKeys, | 27 | 34 | ObjectSetPrototypeOf, | 28 | 35 | ObjectValues, | | 36 | + StringPrototypeIndexOf, | | 37 | + StringPrototypeSplit, | | 38 | + StringPrototypeStartsWith, | | 39 | + StringPrototypeSubstr, | 29 | 40 | Symbol, | 30 | 41 | } = primordials; | 31 | 42 |
| @@ -78,7 +89,7 @@ function Agent(options) { | 78 | 89 | if (!(this instanceof Agent)) | 79 | 90 | return new Agent(options); | 80 | 91 |
| 81 | | - EventEmitter.call(this); | | 92 | + FunctionPrototypeCall(EventEmitter, this); | 82 | 93 |
| 83 | 94 | this.defaultPort = 80; | 84 | 95 | this.protocol = 'http:'; | @@ -125,7 +136,7 @@ function Agent(options) { | 125 | 136 |
| 126 | 137 | const requests = this.requests[name]; | 127 | 138 | if (requests && requests.length) { | 128 | | - const req = requests.shift(); | | 139 | + const req = ArrayPrototypeShift(requests); | 129 | 140 | const reqAsyncRes = req[kRequestAsyncResource]; | 130 | 141 | if (reqAsyncRes) { | 131 | 142 | // Run request within the original async context. | @@ -171,7 +182,7 @@ function Agent(options) { | 171 | 182 | this.removeSocket(socket, options); | 172 | 183 |
| 173 | 184 | socket.once('error', freeSocketErrorListener); | 174 | | - freeSockets.push(socket); | | 185 | + ArrayPrototypePush(freeSockets, socket); | 175 | 186 | }); | 176 | 187 |
| 177 | 188 | // Don't emit keylog events unless there is a listener for them. | @@ -249,11 +260,11 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */, | 249 | 260 | let socket; | 250 | 261 | if (freeSockets) { | 251 | 262 | while (freeSockets.length && freeSockets[0].destroyed) { | 252 | | - freeSockets.shift(); | | 263 | + ArrayPrototypeShift(freeSockets); | 253 | 264 | } | 254 | 265 | socket = this.scheduling === 'fifo' ? | 255 | | - freeSockets.shift() : | 256 | | - freeSockets.pop(); | | 266 | + ArrayPrototypeShift(freeSockets) : | | 267 | + ArrayPrototypePop(freeSockets); | 257 | 268 | if (!freeSockets.length) | 258 | 269 | delete this.freeSockets[name]; | 259 | 270 | } | @@ -265,7 +276,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */, | 265 | 276 | asyncResetHandle(socket); | 266 | 277 | this.reuseSocket(socket, req); | 267 | 278 | setRequestSocket(this, req, socket); | 268 | | - this.sockets[name].push(socket); | | 279 | + ArrayPrototypePush(this.sockets[name], socket); | 269 | 280 | this.totalSocketCount++; | 270 | 281 | } else if (sockLen < this.maxSockets && | 271 | 282 | this.totalSocketCount < this.maxTotalSockets) { | @@ -289,7 +300,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */, | 289 | 300 | // Used to capture the original async context. | 290 | 301 | req[kRequestAsyncResource] = new AsyncResource('QueuedRequest'); | 291 | 302 |
| 292 | | - this.requests[name].push(req); | | 303 | + ArrayPrototypePush(this.requests[name], req); | 293 | 304 | } | 294 | 305 | }; | 295 | 306 |
| @@ -313,7 +324,7 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) { | 313 | 324 | if (!this.sockets[name]) { | 314 | 325 | this.sockets[name] = []; | 315 | 326 | } | 316 | | - this.sockets[name].push(s); | | 327 | + ArrayPrototypePush(this.sockets[name], s); | 317 | 328 | this.totalSocketCount++; | 318 | 329 | debug('sockets', name, this.sockets[name].length, this.totalSocketCount); | 319 | 330 | installListeners(this, s, options); | @@ -338,16 +349,16 @@ function calculateServerName(options, req) { | 338 | 349 | // abc:123 => abc | 339 | 350 | // [::1] => ::1 | 340 | 351 | // [::1]:123 => ::1 | 341 | | - if (hostHeader.startsWith('[')) { | 342 | | - const index = hostHeader.indexOf(']'); | | 352 | + if (StringPrototypeStartsWith(hostHeader, '[')) { | | 353 | + const index = StringPrototypeIndexOf(hostHeader, ']'); | 343 | 354 | if (index === -1) { | 344 | 355 | // Leading '[', but no ']'. Need to do something... | 345 | 356 | servername = hostHeader; | 346 | 357 | } else { | 347 | | - servername = hostHeader.substr(1, index - 1); | | 358 | + servername = StringPrototypeSubstr(hostHeader, 1, index - 1); | 348 | 359 | } | 349 | 360 | } else { | 350 | | - servername = hostHeader.split(':', 1)[0]; | | 361 | + servername = StringPrototypeSplit(hostHeader, ':', 1)[0]; | 351 | 362 | } | 352 | 363 | } | 353 | 364 | // Don't implicitly set invalid (IP) servernames. | @@ -379,7 +390,7 @@ function installListeners(agent, s, options) { | 379 | 390 | // TODO(ronag): Always destroy, even if not in free list. | 380 | 391 | const sockets = agent.freeSockets; | 381 | 392 | for (const name of ObjectKeys(sockets)) { | 382 | | - if (sockets[name].includes(s)) { | | 393 | + if (ArrayPrototypeIncludes(sockets[name], s)) { | 383 | 394 | return s.destroy(); | 384 | 395 | } | 385 | 396 | } | @@ -411,13 +422,13 @@ Agent.prototype.removeSocket = function removeSocket(s, options) { | 411 | 422 |
| 412 | 423 | // If the socket was destroyed, remove it from the free buffers too. | 413 | 424 | if (!s.writable) | 414 | | - sets.push(this.freeSockets); | | 425 | + ArrayPrototypePush(sets, this.freeSockets); | 415 | 426 |
| 416 | 427 | for (const sockets of sets) { | 417 | 428 | if (sockets[name]) { | 418 | | - const index = sockets[name].indexOf(s); | | 429 | + const index = ArrayPrototypeIndexOf(sockets[name], s); | 419 | 430 | if (index !== -1) { | 420 | | - sockets[name].splice(index, 1); | | 431 | + ArrayPrototypeSplice(sockets[name], index, 1); | 421 | 432 | // Don't leak | 422 | 433 | if (sockets[name].length === 0) | 423 | 434 | delete sockets[name]; | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_client.js +18-10Lines changed: 18 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,12 +25,20 @@ const { | 25 | 25 | ArrayIsArray, | 26 | 26 | Boolean, | 27 | 27 | Error, | | 28 | + FunctionPrototypeCall, | 28 | 29 | NumberIsFinite, | 29 | 30 | ObjectAssign, | 30 | 31 | ObjectKeys, | 31 | 32 | ObjectSetPrototypeOf, | | 33 | + ReflectApply, | | 34 | + RegExpPrototypeTest, | 32 | 35 | String, | 33 | | - Symbol | | 36 | + StringPrototypeCharCodeAt, | | 37 | + StringPrototypeIncludes, | | 38 | + StringPrototypeIndexOf, | | 39 | + StringPrototypeToUpperCase, | | 40 | + Symbol, | | 41 | + TypedArrayPrototypeSlice, | 34 | 42 | } = primordials; | 35 | 43 |
| 36 | 44 | const net = require('net'); | @@ -91,7 +99,7 @@ class HTTPClientAsyncResource { | 91 | 99 |
| 92 | 100 | let urlWarningEmitted = false; | 93 | 101 | function ClientRequest(input, options, cb) { | 94 | | - OutgoingMessage.call(this); | | 102 | + FunctionPrototypeCall(OutgoingMessage, this); | 95 | 103 |
| 96 | 104 | if (typeof input === 'string') { | 97 | 105 | const urlStr = input; | @@ -151,7 +159,7 @@ function ClientRequest(input, options, cb) { | 151 | 159 |
| 152 | 160 | if (options.path) { | 153 | 161 | const path = String(options.path); | 154 | | - if (INVALID_PATH_REGEX.test(path)) | | 162 | + if (RegExpPrototypeTest(INVALID_PATH_REGEX, path)) | 155 | 163 | throw new ERR_UNESCAPED_CHARACTERS('Request path'); | 156 | 164 | } | 157 | 165 |
| @@ -187,7 +195,7 @@ function ClientRequest(input, options, cb) { | 187 | 195 | if (!checkIsHttpToken(method)) { | 188 | 196 | throw new ERR_INVALID_HTTP_TOKEN('Method', method); | 189 | 197 | } | 190 | | - method = this.method = method.toUpperCase(); | | 198 | + method = this.method = StringPrototypeToUpperCase(method); | 191 | 199 | } else { | 192 | 200 | method = this.method = 'GET'; | 193 | 201 | } | @@ -266,10 +274,10 @@ function ClientRequest(input, options, cb) { | 266 | 274 | // For the Host header, ensure that IPv6 addresses are enclosed | 267 | 275 | // in square brackets, as defined by URI formatting | 268 | 276 | // https://tools.ietf.org/html/rfc3986#section-3.2.2 | 269 | | - const posColon = hostHeader.indexOf(':'); | | 277 | + const posColon = StringPrototypeIndexOf(hostHeader, ':'); | 270 | 278 | if (posColon !== -1 && | 271 | | - hostHeader.includes(':', posColon + 1) && | 272 | | - hostHeader.charCodeAt(0) !== 91/* '[' */) { | | 279 | + StringPrototypeIncludes(hostHeader, ':', posColon + 1) && | | 280 | + StringPrototypeCharCodeAt(hostHeader, 0) !== 91/* '[' */) { | 273 | 281 | hostHeader = `[${hostHeader}]`; | 274 | 282 | } | 275 | 283 |
| @@ -337,7 +345,7 @@ ObjectSetPrototypeOf(ClientRequest, OutgoingMessage); | 337 | 345 |
| 338 | 346 | ClientRequest.prototype._finish = function _finish() { | 339 | 347 | DTRACE_HTTP_CLIENT_REQUEST(this, this.socket); | 340 | | - OutgoingMessage.prototype._finish.call(this); | | 348 | + FunctionPrototypeCall(OutgoingMessage.prototype._finish, this); | 341 | 349 | }; | 342 | 350 |
| 343 | 351 | ClientRequest.prototype._implicitHeader = function _implicitHeader() { | @@ -547,7 +555,7 @@ function socketOnData(d) { | 547 | 555 | parser.finish(); | 548 | 556 | freeParser(parser, req, socket); | 549 | 557 |
| 550 | | - const bodyHead = d.slice(bytesParsed, d.length); | | 558 | + const bodyHead = TypedArrayPrototypeSlice(d, bytesParsed, d.length); | 551 | 559 |
| 552 | 560 | const eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade'; | 553 | 561 | if (req.listenerCount(eventName) > 0) { | @@ -848,7 +856,7 @@ function _deferToConnect(method, arguments_, cb) { | 848 | 856 |
| 849 | 857 | const callSocketMethod = () => { | 850 | 858 | if (method) | 851 | | - this.socket[method].apply(this.socket, arguments_); | | 859 | + ReflectApply(this.socket[method], this.socket, arguments_); | 852 | 860 |
| 853 | 861 | if (typeof cb === 'function') | 854 | 862 | cb(); | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_common.js +7-4Lines changed: 7 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | 'use strict'; | 23 | 23 |
| 24 | 24 | const { | | 25 | + ArrayPrototypeConcat, | 25 | 26 | MathMin, | 26 | 27 | Symbol, | | 28 | + RegExpPrototypeTest, | | 29 | + TypedArrayPrototypeSlice, | 27 | 30 | } = primordials; | 28 | 31 | const { setImmediate } = require('timers'); | 29 | 32 |
| @@ -63,7 +66,7 @@ function parserOnHeaders(headers, url) { | 63 | 66 | // Once we exceeded headers limit - stop collecting them | 64 | 67 | if (this.maxHeaderPairs <= 0 || | 65 | 68 | this._headers.length < this.maxHeaderPairs) { | 66 | | - this._headers = this._headers.concat(headers); | | 69 | + this._headers = ArrayPrototypeConcat(this._headers, headers); | 67 | 70 | } | 68 | 71 | this._url += url; | 69 | 72 | } | @@ -135,7 +138,7 @@ function parserOnBody(b, start, len) { | 135 | 138 |
| 136 | 139 | // Pretend this was the result of a stream._read call. | 137 | 140 | if (len > 0 && !stream._dumped) { | 138 | | - const slice = b.slice(start, start + len); | | 141 | + const slice = TypedArrayPrototypeSlice(b, start, start + len); | 139 | 142 | const ret = stream.push(slice); | 140 | 143 | if (!ret) | 141 | 144 | readStop(this.socket); | @@ -217,7 +220,7 @@ const tokenRegExp = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/; | 217 | 220 | * See https://tools.ietf.org/html/rfc7230#section-3.2.6 | 218 | 221 | */ | 219 | 222 | function checkIsHttpToken(val) { | 220 | | - return tokenRegExp.test(val); | | 223 | + return RegExpPrototypeTest(tokenRegExp, val); | 221 | 224 | } | 222 | 225 |
| 223 | 226 | const headerCharRegex = /[^\t\x20-\x7e\x80-\xff]/; | @@ -228,7 +231,7 @@ const headerCharRegex = /[^\t\x20-\x7e\x80-\xff]/; | 228 | 231 | * field-vchar = VCHAR / obs-text | 229 | 232 | */ | 230 | 233 | function checkInvalidHeaderChar(val) { | 231 | | - return headerCharRegex.test(val); | | 234 | + return RegExpPrototypeTest(headerCharRegex, val); | 232 | 235 | } | 233 | 236 |
| 234 | 237 | function cleanParser(parser) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_incoming.js +11-5Lines changed: 11 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | 'use strict'; | 23 | 23 |
| 24 | 24 | const { | | 25 | + ArrayPrototypePush, | | 26 | + FunctionPrototypeCall, | 25 | 27 | ObjectDefineProperty, | 26 | 28 | ObjectSetPrototypeOf, | | 29 | + StringPrototypeCharCodeAt, | | 30 | + StringPrototypeSlice, | | 31 | + StringPrototypeToLowerCase, | 27 | 32 | Symbol | 28 | 33 | } = primordials; | 29 | 34 |
| @@ -54,7 +59,8 @@ function IncomingMessage(socket) { | 54 | 59 | }; | 55 | 60 | } | 56 | 61 |
| 57 | | - Stream.Readable.call(this, { autoDestroy: false, ...streamOptions }); | | 62 | + FunctionPrototypeCall(Stream.Readable, this, | | 63 | + { autoDestroy: false, ...streamOptions }); | 58 | 64 |
| 59 | 65 | this._readableState.readingMore = true; | 60 | 66 |
| @@ -300,7 +306,7 @@ function matchKnownFields(field, lowercased) { | 300 | 306 | if (lowercased) { | 301 | 307 | return '\u0000' + field; | 302 | 308 | } | 303 | | - return matchKnownFields(field.toLowerCase(), true); | | 309 | + return matchKnownFields(StringPrototypeToLowerCase(field), true); | 304 | 310 | } | 305 | 311 | // Add the given (field, value) pair to the message | 306 | 312 | // | @@ -314,9 +320,9 @@ function matchKnownFields(field, lowercased) { | 314 | 320 | IncomingMessage.prototype._addHeaderLine = _addHeaderLine; | 315 | 321 | function _addHeaderLine(field, value, dest) { | 316 | 322 | field = matchKnownFields(field); | 317 | | - const flag = field.charCodeAt(0); | | 323 | + const flag = StringPrototypeCharCodeAt(field, 0); | 318 | 324 | if (flag === 0 || flag === 2) { | 319 | | - field = field.slice(1); | | 325 | + field = StringPrototypeSlice(field, 1); | 320 | 326 | // Make a delimited list | 321 | 327 | if (typeof dest[field] === 'string') { | 322 | 328 | dest[field] += (flag === 0 ? ', ' : '; ') + value; | @@ -326,7 +332,7 @@ function _addHeaderLine(field, value, dest) { | 326 | 332 | } else if (flag === 1) { | 327 | 333 | // Array header -- only Set-Cookie at the moment | 328 | 334 | if (dest['set-cookie'] !== undefined) { | 329 | | - dest['set-cookie'].push(value); | | 335 | + ArrayPrototypePush(dest['set-cookie'], value); | 330 | 336 | } else { | 331 | 337 | dest['set-cookie'] = [value]; | 332 | 338 | } | |
|