Skip to content

Commit

Permalink
https: refactor to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: #36195
Reviewed-By: Andrey Pechkurov <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
  • Loading branch information
aduh95 authored and targos committed Jun 11, 2021
1 parent 01a71dd commit c90f1db
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@
'use strict';

const {
ArrayPrototypeIndexOf,
ArrayPrototypePush,
ArrayPrototypeShift,
ArrayPrototypeSplice,
ArrayPrototypeUnshift,
FunctionPrototypeCall,
JSONStringify,
ObjectAssign,
ObjectSetPrototypeOf,
JSONStringify,
ReflectConstruct,
} = primordials;

require('internal/util').assertCrypto();
Expand Down Expand Up @@ -64,7 +71,7 @@ function Server(opts, requestListener) {
this[kIncomingMessage] = opts.IncomingMessage || IncomingMessage;
this[kServerResponse] = opts.ServerResponse || ServerResponse;

tls.Server.call(this, opts, _connectionListener);
FunctionPrototypeCall(tls.Server, this, opts, _connectionListener);

this.httpAllowHalfOpen = false;

Expand Down Expand Up @@ -150,7 +157,7 @@ function Agent(options) {
if (!(this instanceof Agent))
return new Agent(options);

HttpAgent.call(this, options);
FunctionPrototypeCall(HttpAgent, this, options);
this.defaultPort = 443;
this.protocol = 'https:';
this.maxCachedSessions = this.options.maxCachedSessions;
Expand All @@ -167,7 +174,7 @@ ObjectSetPrototypeOf(Agent, HttpAgent);
Agent.prototype.createConnection = createConnection;

Agent.prototype.getName = function getName(options) {
let name = HttpAgent.prototype.getName.call(this, options);
let name = FunctionPrototypeCall(HttpAgent.prototype.getName, this, options);

name += ':';
if (options.ca)
Expand Down Expand Up @@ -269,21 +276,21 @@ Agent.prototype._cacheSession = function _cacheSession(key, session) {

// Put new entry
if (this._sessionCache.list.length >= this.maxCachedSessions) {
const oldKey = this._sessionCache.list.shift();
const oldKey = ArrayPrototypeShift(this._sessionCache.list);
debug('evicting %j', oldKey);
delete this._sessionCache.map[oldKey];
}

this._sessionCache.list.push(key);
ArrayPrototypePush(this._sessionCache.list, key);
this._sessionCache.map[key] = session;
};

Agent.prototype._evictSession = function _evictSession(key) {
const index = this._sessionCache.list.indexOf(key);
const index = ArrayPrototypeIndexOf(this._sessionCache.list, key);
if (index === -1)
return;

this._sessionCache.list.splice(index, 1);
ArrayPrototypeSplice(this._sessionCache.list, index, 1);
delete this._sessionCache.map[key];
};

Expand All @@ -294,7 +301,7 @@ function request(...args) {
let options = {};

if (typeof args[0] === 'string') {
const urlStr = args.shift();
const urlStr = ArrayPrototypeShift(args);
try {
options = urlToOptions(new URL(urlStr));
} catch (err) {
Expand All @@ -313,17 +320,17 @@ function request(...args) {
} else if (args[0] && args[0][searchParamsSymbol] &&
args[0][searchParamsSymbol][searchParamsSymbol]) {
// url.URL instance
options = urlToOptions(args.shift());
options = urlToOptions(ArrayPrototypeShift(args));
}

if (args[0] && typeof args[0] !== 'function') {
ObjectAssign(options, args.shift());
ObjectAssign(options, ArrayPrototypeShift(args));
}

options._defaultAgent = module.exports.globalAgent;
args.unshift(options);
ArrayPrototypeUnshift(args, options);

return new ClientRequest(...args);
return ReflectConstruct(ClientRequest, args);
}

function get(input, options, cb) {
Expand Down

0 comments on commit c90f1db

Please sign in to comment.