Skip to content

Commit 5425e0d

Browse files
committed
http: use more efficient module.exports pattern
PR-URL: #11594 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 2a4a5f0 commit 5425e0d

7 files changed

+70
-48
lines changed

lib/_http_agent.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ function Agent(options) {
105105
}
106106

107107
util.inherits(Agent, EventEmitter);
108-
exports.Agent = Agent;
109108

110109
Agent.defaultMaxSockets = Infinity;
111110

@@ -314,4 +313,7 @@ Agent.prototype.destroy = function destroy() {
314313
}
315314
};
316315

317-
exports.globalAgent = new Agent();
316+
module.exports = {
317+
Agent,
318+
globalAgent: new Agent()
319+
};

lib/_http_client.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ function ClientRequest(options, cb) {
286286

287287
util.inherits(ClientRequest, OutgoingMessage);
288288

289-
exports.ClientRequest = ClientRequest;
290289

291290
ClientRequest.prototype._finish = function _finish() {
292291
DTRACE_HTTP_CLIENT_REQUEST(this, this.connection);
@@ -752,3 +751,7 @@ ClientRequest.prototype.setSocketKeepAlive =
752751
ClientRequest.prototype.clearTimeout = function clearTimeout(cb) {
753752
this.setTimeout(0, cb);
754753
};
754+
755+
module.exports = {
756+
ClientRequest
757+
};

lib/_http_common.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ const readStart = incoming.readStart;
3232
const readStop = incoming.readStop;
3333

3434
const debug = require('util').debuglog('http');
35-
exports.debug = debug;
36-
37-
exports.CRLF = '\r\n';
38-
exports.chunkExpression = /(?:^|\W)chunked(?:$|\W)/i;
39-
exports.continueExpression = /(?:^|\W)100-continue(?:$|\W)/i;
40-
exports.methods = methods;
4135

4236
const kOnHeaders = HTTPParser.kOnHeaders | 0;
4337
const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
@@ -194,7 +188,6 @@ var parsers = new FreeList('parsers', 1000, function() {
194188

195189
return parser;
196190
});
197-
exports.parsers = parsers;
198191

199192

200193
// Free the parser and also break any links that it
@@ -227,7 +220,6 @@ function freeParser(parser, req, socket) {
227220
socket.parser = null;
228221
}
229222
}
230-
exports.freeParser = freeParser;
231223

232224

233225
function ondrain() {
@@ -239,7 +231,6 @@ function httpSocketSetup(socket) {
239231
socket.removeListener('drain', ondrain);
240232
socket.on('drain', ondrain);
241233
}
242-
exports.httpSocketSetup = httpSocketSetup;
243234

244235
/**
245236
* Verifies that the given val is a valid HTTP token
@@ -306,7 +297,6 @@ function checkIsHttpToken(val) {
306297
}
307298
return true;
308299
}
309-
exports._checkIsHttpToken = checkIsHttpToken;
310300

311301
/**
312302
* True if val contains an invalid field-vchar
@@ -360,4 +350,16 @@ function checkInvalidHeaderChar(val) {
360350
}
361351
return false;
362352
}
363-
exports._checkInvalidHeaderChar = checkInvalidHeaderChar;
353+
354+
module.exports = {
355+
_checkInvalidHeaderChar: checkInvalidHeaderChar,
356+
_checkIsHttpToken: checkIsHttpToken,
357+
chunkExpression: /(?:^|\W)chunked(?:$|\W)/i,
358+
continueExpression: /(?:^|\W)100-continue(?:$|\W)/i,
359+
CRLF: '\r\n',
360+
debug,
361+
freeParser,
362+
httpSocketSetup,
363+
methods,
364+
parsers
365+
};

lib/_http_incoming.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@ function readStart(socket) {
2828
if (socket && !socket._paused && socket.readable)
2929
socket.resume();
3030
}
31-
exports.readStart = readStart;
3231

3332
function readStop(socket) {
3433
if (socket)
3534
socket.pause();
3635
}
37-
exports.readStop = readStop;
38-
3936

4037
/* Abstract base class for ServerRequest and ClientResponse. */
4138
function IncomingMessage(socket) {
@@ -83,9 +80,6 @@ function IncomingMessage(socket) {
8380
util.inherits(IncomingMessage, Stream.Readable);
8481

8582

86-
exports.IncomingMessage = IncomingMessage;
87-
88-
8983
IncomingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
9084
if (callback)
9185
this.on('timeout', callback);
@@ -324,3 +318,9 @@ IncomingMessage.prototype._dump = function _dump() {
324318
this.resume();
325319
}
326320
};
321+
322+
module.exports = {
323+
IncomingMessage,
324+
readStart,
325+
readStop
326+
};

lib/_http_outgoing.js

+5
Original file line numberDiff line numberDiff line change
@@ -887,3 +887,8 @@ OutgoingMessage.prototype.flushHeaders = function flushHeaders() {
887887
OutgoingMessage.prototype.flush = internalUtil.deprecate(function() {
888888
this.flushHeaders();
889889
}, 'OutgoingMessage.flush is deprecated. Use flushHeaders instead.', 'DEP0001');
890+
891+
892+
module.exports = {
893+
OutgoingMessage
894+
};

lib/_http_server.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const httpSocketSetup = common.httpSocketSetup;
3636
const OutgoingMessage = require('_http_outgoing').OutgoingMessage;
3737
const outHeadersKey = require('internal/http').outHeadersKey;
3838

39-
const STATUS_CODES = exports.STATUS_CODES = {
39+
const STATUS_CODES = {
4040
100: 'Continue',
4141
101: 'Switching Protocols',
4242
102: 'Processing', // RFC 2518, obsoleted by RFC 4918
@@ -128,8 +128,6 @@ ServerResponse.prototype._finish = function _finish() {
128128
};
129129

130130

131-
exports.ServerResponse = ServerResponse;
132-
133131
ServerResponse.prototype.statusCode = 200;
134132
ServerResponse.prototype.statusMessage = undefined;
135133

@@ -290,9 +288,6 @@ Server.prototype.setTimeout = function setTimeout(msecs, callback) {
290288
};
291289

292290

293-
exports.Server = Server;
294-
295-
296291
function connectionListener(socket) {
297292
debug('SERVER new http connection');
298293

@@ -363,7 +358,7 @@ function connectionListener(socket) {
363358

364359
socket._paused = false;
365360
}
366-
exports._connectionListener = connectionListener;
361+
367362

368363
function updateOutgoingData(socket, state, delta) {
369364
state.outgoingData += delta;
@@ -640,3 +635,10 @@ function socketOnWrap(ev, fn) {
640635

641636
return res;
642637
}
638+
639+
module.exports = {
640+
STATUS_CODES,
641+
Server,
642+
ServerResponse,
643+
_connectionListener: connectionListener
644+
};

lib/http.js

+29-21
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,43 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
exports.IncomingMessage = require('_http_incoming').IncomingMessage;
24-
25-
exports.OutgoingMessage = require('_http_outgoing').OutgoingMessage;
26-
27-
exports.METHODS = require('_http_common').methods.slice().sort();
2823

2924
const agent = require('_http_agent');
30-
exports.Agent = agent.Agent;
31-
exports.globalAgent = agent.globalAgent;
32-
25+
const client = require('_http_client');
26+
const common = require('_http_common');
27+
const incoming = require('_http_incoming');
28+
const outgoing = require('_http_outgoing');
3329
const server = require('_http_server');
34-
exports.ServerResponse = server.ServerResponse;
35-
exports.STATUS_CODES = server.STATUS_CODES;
36-
exports._connectionListener = server._connectionListener;
37-
const Server = exports.Server = server.Server;
3830

39-
exports.createServer = function createServer(requestListener) {
40-
return new Server(requestListener);
41-
};
31+
const Server = server.Server;
32+
const ClientRequest = client.ClientRequest;
4233

43-
const client = require('_http_client');
44-
const ClientRequest = exports.ClientRequest = client.ClientRequest;
34+
function createServer(requestListener) {
35+
return new Server(requestListener);
36+
}
4537

46-
exports.request = function request(options, cb) {
38+
function request(options, cb) {
4739
return new ClientRequest(options, cb);
48-
};
40+
}
4941

50-
exports.get = function get(options, cb) {
51-
var req = exports.request(options, cb);
42+
function get(options, cb) {
43+
var req = request(options, cb);
5244
req.end();
5345
return req;
46+
}
47+
48+
module.exports = {
49+
_connectionListener: server._connectionListener,
50+
METHODS: common.methods.slice().sort(),
51+
STATUS_CODES: server.STATUS_CODES,
52+
Agent: agent.Agent,
53+
ClientRequest,
54+
globalAgent: agent.globalAgent,
55+
IncomingMessage: incoming.IncomingMessage,
56+
OutgoingMessage: outgoing.OutgoingMessage,
57+
Server,
58+
ServerResponse: server.ServerResponse,
59+
createServer,
60+
get,
61+
request
5462
};

0 commit comments

Comments
 (0)