Skip to content

Commit 74c1e02

Browse files
committed
http: replace uses of self
PR-URL: #11594 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 5425e0d commit 74c1e02

File tree

2 files changed

+67
-78
lines changed

2 files changed

+67
-78
lines changed

lib/_http_client.js

+64-75
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ function isInvalidPath(s) {
6666
}
6767

6868
function ClientRequest(options, cb) {
69-
var self = this;
70-
OutgoingMessage.call(self);
69+
OutgoingMessage.call(this);
7170

7271
if (typeof options === 'string') {
7372
options = url.parse(options);
@@ -95,12 +94,12 @@ function ClientRequest(options, cb) {
9594
'Agent option must be an instance of http.Agent, undefined or false.'
9695
);
9796
}
98-
self.agent = agent;
97+
this.agent = agent;
9998

10099
var protocol = options.protocol || defaultAgent.protocol;
101100
var expectedProtocol = defaultAgent.protocol;
102-
if (self.agent && self.agent.protocol)
103-
expectedProtocol = self.agent.protocol;
101+
if (this.agent && this.agent.protocol)
102+
expectedProtocol = this.agent.protocol;
104103

105104
var path;
106105
if (options.path) {
@@ -121,15 +120,15 @@ function ClientRequest(options, cb) {
121120
}
122121

123122
const defaultPort = options.defaultPort ||
124-
self.agent && self.agent.defaultPort;
123+
this.agent && this.agent.defaultPort;
125124

126125
var port = options.port = options.port || defaultPort || 80;
127126
var host = options.host = options.hostname || options.host || 'localhost';
128127

129128
var setHost = (options.setHost === undefined);
130129

131-
self.socketPath = options.socketPath;
132-
self.timeout = options.timeout;
130+
this.socketPath = options.socketPath;
131+
this.timeout = options.timeout;
133132

134133
var method = options.method;
135134
var methodIsString = (typeof method === 'string');
@@ -141,14 +140,14 @@ function ClientRequest(options, cb) {
141140
if (!common._checkIsHttpToken(method)) {
142141
throw new TypeError('Method must be a valid HTTP token');
143142
}
144-
method = self.method = method.toUpperCase();
143+
method = this.method = method.toUpperCase();
145144
} else {
146-
method = self.method = 'GET';
145+
method = this.method = 'GET';
147146
}
148147

149-
self.path = options.path || '/';
148+
this.path = options.path || '/';
150149
if (cb) {
151-
self.once('response', cb);
150+
this.once('response', cb);
152151
}
153152

154153
var headersArray = Array.isArray(options.headers);
@@ -157,7 +156,7 @@ function ClientRequest(options, cb) {
157156
var keys = Object.keys(options.headers);
158157
for (var i = 0; i < keys.length; i++) {
159158
var key = keys[i];
160-
self.setHeader(key, options.headers[key]);
159+
this.setHeader(key, options.headers[key]);
161160
}
162161
}
163162
if (host && !this.getHeader('host') && setHost) {
@@ -190,21 +189,22 @@ function ClientRequest(options, cb) {
190189
method === 'DELETE' ||
191190
method === 'OPTIONS' ||
192191
method === 'CONNECT') {
193-
self.useChunkedEncodingByDefault = false;
192+
this.useChunkedEncodingByDefault = false;
194193
} else {
195-
self.useChunkedEncodingByDefault = true;
194+
this.useChunkedEncodingByDefault = true;
196195
}
197196

198197
if (headersArray) {
199-
self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n',
198+
this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
200199
options.headers);
201-
} else if (self.getHeader('expect')) {
202-
if (self._header) {
200+
} else if (this.getHeader('expect')) {
201+
if (this._header) {
203202
throw new Error('Can\'t render headers after they are sent to the ' +
204203
'client');
205204
}
206-
self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n',
207-
self[outHeadersKey]);
205+
206+
this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
207+
this[outHeadersKey]);
208208
}
209209

210210
this._ended = false;
@@ -216,72 +216,65 @@ function ClientRequest(options, cb) {
216216
this.maxHeadersCount = null;
217217

218218
var called = false;
219-
if (self.socketPath) {
220-
self._last = true;
221-
self.shouldKeepAlive = false;
219+
220+
const oncreate = (err, socket) => {
221+
if (called)
222+
return;
223+
called = true;
224+
if (err) {
225+
process.nextTick(() => this.emit('error', err));
226+
return;
227+
}
228+
this.onSocket(socket);
229+
this._deferToConnect(null, null, () => this._flush());
230+
};
231+
232+
if (this.socketPath) {
233+
this._last = true;
234+
this.shouldKeepAlive = false;
222235
const optionsPath = {
223-
path: self.socketPath,
224-
timeout: self.timeout
236+
path: this.socketPath,
237+
timeout: this.timeout
225238
};
226-
const newSocket = self.agent.createConnection(optionsPath, oncreate);
239+
const newSocket = this.agent.createConnection(optionsPath, oncreate);
227240
if (newSocket && !called) {
228241
called = true;
229-
self.onSocket(newSocket);
242+
this.onSocket(newSocket);
230243
} else {
231244
return;
232245
}
233-
} else if (self.agent) {
246+
} else if (this.agent) {
234247
// If there is an agent we should default to Connection:keep-alive,
235248
// but only if the Agent will actually reuse the connection!
236249
// If it's not a keepAlive agent, and the maxSockets==Infinity, then
237250
// there's never a case where this socket will actually be reused
238-
if (!self.agent.keepAlive && !Number.isFinite(self.agent.maxSockets)) {
239-
self._last = true;
240-
self.shouldKeepAlive = false;
251+
if (!this.agent.keepAlive && !Number.isFinite(this.agent.maxSockets)) {
252+
this._last = true;
253+
this.shouldKeepAlive = false;
241254
} else {
242-
self._last = false;
243-
self.shouldKeepAlive = true;
255+
this._last = false;
256+
this.shouldKeepAlive = true;
244257
}
245-
self.agent.addRequest(self, options);
258+
this.agent.addRequest(this, options);
246259
} else {
247260
// No agent, default to Connection:close.
248-
self._last = true;
249-
self.shouldKeepAlive = false;
261+
this._last = true;
262+
this.shouldKeepAlive = false;
250263
if (typeof options.createConnection === 'function') {
251264
const newSocket = options.createConnection(options, oncreate);
252265
if (newSocket && !called) {
253266
called = true;
254-
self.onSocket(newSocket);
267+
this.onSocket(newSocket);
255268
} else {
256269
return;
257270
}
258271
} else {
259272
debug('CLIENT use net.createConnection', options);
260-
self.onSocket(net.createConnection(options));
261-
}
262-
}
263-
264-
function oncreate(err, socket) {
265-
if (called)
266-
return;
267-
called = true;
268-
if (err) {
269-
process.nextTick(function() {
270-
self.emit('error', err);
271-
});
272-
return;
273+
this.onSocket(net.createConnection(options));
273274
}
274-
self.onSocket(socket);
275-
self._deferToConnect(null, null, function() {
276-
self._flush();
277-
self = null;
278-
});
279275
}
280276

281-
self._deferToConnect(null, null, function() {
282-
self._flush();
283-
self = null;
284-
});
277+
this._deferToConnect(null, null, () => this._flush());
285278
}
286279

287280
util.inherits(ClientRequest, OutgoingMessage);
@@ -304,7 +297,7 @@ ClientRequest.prototype._implicitHeader = function _implicitHeader() {
304297

305298
ClientRequest.prototype.abort = function abort() {
306299
if (!this.aborted) {
307-
process.nextTick(emitAbortNT, this);
300+
process.nextTick(emitAbortNT.bind(this));
308301
}
309302
// Mark as aborting so we can avoid sending queued request data
310303
// This is used as a truthy flag elsewhere. The use of Date.now is for
@@ -328,8 +321,8 @@ ClientRequest.prototype.abort = function abort() {
328321
};
329322

330323

331-
function emitAbortNT(self) {
332-
self.emit('abort');
324+
function emitAbortNT() {
325+
this.emit('abort');
333326
}
334327

335328

@@ -681,26 +674,25 @@ function _deferToConnect(method, arguments_, cb) {
681674
// calls that happen either now (when a socket is assigned) or
682675
// in the future (when a socket gets assigned out of the pool and is
683676
// eventually writable).
684-
var self = this;
685677

686-
function callSocketMethod() {
678+
const callSocketMethod = () => {
687679
if (method)
688-
self.socket[method].apply(self.socket, arguments_);
680+
this.socket[method].apply(this.socket, arguments_);
689681

690682
if (typeof cb === 'function')
691683
cb();
692-
}
684+
};
693685

694-
var onSocket = function onSocket() {
695-
if (self.socket.writable) {
686+
const onSocket = () => {
687+
if (this.socket.writable) {
696688
callSocketMethod();
697689
} else {
698-
self.socket.once('connect', callSocketMethod);
690+
this.socket.once('connect', callSocketMethod);
699691
}
700692
};
701693

702-
if (!self.socket) {
703-
self.once('socket', onSocket);
694+
if (!this.socket) {
695+
this.once('socket', onSocket);
704696
} else {
705697
onSocket();
706698
}
@@ -709,10 +701,7 @@ function _deferToConnect(method, arguments_, cb) {
709701
ClientRequest.prototype.setTimeout = function setTimeout(msecs, callback) {
710702
if (callback) this.once('timeout', callback);
711703

712-
var self = this;
713-
function emitTimeout() {
714-
self.emit('timeout');
715-
}
704+
const emitTimeout = () => this.emit('timeout');
716705

717706
if (this.socket && this.socket.writable) {
718707
if (this.timeoutCb)

lib/_http_outgoing.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ const crlf_buf = Buffer.from('\r\n');
625625
OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
626626
if (this.finished) {
627627
var err = new Error('write after end');
628-
process.nextTick(writeAfterEndNT, this, err, callback);
628+
process.nextTick(writeAfterEndNT.bind(this), err, callback);
629629

630630
return true;
631631
}
@@ -684,8 +684,8 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
684684
};
685685

686686

687-
function writeAfterEndNT(self, err, callback) {
688-
self.emit('error', err);
687+
function writeAfterEndNT(err, callback) {
688+
this.emit('error', err);
689689
if (callback) callback(err);
690690
}
691691

0 commit comments

Comments
 (0)