Skip to content

Commit

Permalink
http: use more efficient module.exports pattern
Browse files Browse the repository at this point in the history
PR-URL: nodejs#11594
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
jasnell committed Mar 20, 2017
1 parent 2a4a5f0 commit 5425e0d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 48 deletions.
6 changes: 4 additions & 2 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ function Agent(options) {
}

util.inherits(Agent, EventEmitter);
exports.Agent = Agent;

Agent.defaultMaxSockets = Infinity;

Expand Down Expand Up @@ -314,4 +313,7 @@ Agent.prototype.destroy = function destroy() {
}
};

exports.globalAgent = new Agent();
module.exports = {
Agent,
globalAgent: new Agent()
};
5 changes: 4 additions & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ function ClientRequest(options, cb) {

util.inherits(ClientRequest, OutgoingMessage);

exports.ClientRequest = ClientRequest;

ClientRequest.prototype._finish = function _finish() {
DTRACE_HTTP_CLIENT_REQUEST(this, this.connection);
Expand Down Expand Up @@ -752,3 +751,7 @@ ClientRequest.prototype.setSocketKeepAlive =
ClientRequest.prototype.clearTimeout = function clearTimeout(cb) {
this.setTimeout(0, cb);
};

module.exports = {
ClientRequest
};
24 changes: 13 additions & 11 deletions lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ const readStart = incoming.readStart;
const readStop = incoming.readStop;

const debug = require('util').debuglog('http');
exports.debug = debug;

exports.CRLF = '\r\n';
exports.chunkExpression = /(?:^|\W)chunked(?:$|\W)/i;
exports.continueExpression = /(?:^|\W)100-continue(?:$|\W)/i;
exports.methods = methods;

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

return parser;
});
exports.parsers = parsers;


// Free the parser and also break any links that it
Expand Down Expand Up @@ -227,7 +220,6 @@ function freeParser(parser, req, socket) {
socket.parser = null;
}
}
exports.freeParser = freeParser;


function ondrain() {
Expand All @@ -239,7 +231,6 @@ function httpSocketSetup(socket) {
socket.removeListener('drain', ondrain);
socket.on('drain', ondrain);
}
exports.httpSocketSetup = httpSocketSetup;

/**
* Verifies that the given val is a valid HTTP token
Expand Down Expand Up @@ -306,7 +297,6 @@ function checkIsHttpToken(val) {
}
return true;
}
exports._checkIsHttpToken = checkIsHttpToken;

/**
* True if val contains an invalid field-vchar
Expand Down Expand Up @@ -360,4 +350,16 @@ function checkInvalidHeaderChar(val) {
}
return false;
}
exports._checkInvalidHeaderChar = checkInvalidHeaderChar;

module.exports = {
_checkInvalidHeaderChar: checkInvalidHeaderChar,
_checkIsHttpToken: checkIsHttpToken,
chunkExpression: /(?:^|\W)chunked(?:$|\W)/i,
continueExpression: /(?:^|\W)100-continue(?:$|\W)/i,
CRLF: '\r\n',
debug,
freeParser,
httpSocketSetup,
methods,
parsers
};
12 changes: 6 additions & 6 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ function readStart(socket) {
if (socket && !socket._paused && socket.readable)
socket.resume();
}
exports.readStart = readStart;

function readStop(socket) {
if (socket)
socket.pause();
}
exports.readStop = readStop;


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


exports.IncomingMessage = IncomingMessage;


IncomingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
if (callback)
this.on('timeout', callback);
Expand Down Expand Up @@ -324,3 +318,9 @@ IncomingMessage.prototype._dump = function _dump() {
this.resume();
}
};

module.exports = {
IncomingMessage,
readStart,
readStop
};
5 changes: 5 additions & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -887,3 +887,8 @@ OutgoingMessage.prototype.flushHeaders = function flushHeaders() {
OutgoingMessage.prototype.flush = internalUtil.deprecate(function() {
this.flushHeaders();
}, 'OutgoingMessage.flush is deprecated. Use flushHeaders instead.', 'DEP0001');


module.exports = {
OutgoingMessage
};
16 changes: 9 additions & 7 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const httpSocketSetup = common.httpSocketSetup;
const OutgoingMessage = require('_http_outgoing').OutgoingMessage;
const outHeadersKey = require('internal/http').outHeadersKey;

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


exports.ServerResponse = ServerResponse;

ServerResponse.prototype.statusCode = 200;
ServerResponse.prototype.statusMessage = undefined;

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


exports.Server = Server;


function connectionListener(socket) {
debug('SERVER new http connection');

Expand Down Expand Up @@ -363,7 +358,7 @@ function connectionListener(socket) {

socket._paused = false;
}
exports._connectionListener = connectionListener;


function updateOutgoingData(socket, state, delta) {
state.outgoingData += delta;
Expand Down Expand Up @@ -640,3 +635,10 @@ function socketOnWrap(ev, fn) {

return res;
}

module.exports = {
STATUS_CODES,
Server,
ServerResponse,
_connectionListener: connectionListener
};
50 changes: 29 additions & 21 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,43 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
exports.IncomingMessage = require('_http_incoming').IncomingMessage;

exports.OutgoingMessage = require('_http_outgoing').OutgoingMessage;

exports.METHODS = require('_http_common').methods.slice().sort();

const agent = require('_http_agent');
exports.Agent = agent.Agent;
exports.globalAgent = agent.globalAgent;

const client = require('_http_client');
const common = require('_http_common');
const incoming = require('_http_incoming');
const outgoing = require('_http_outgoing');
const server = require('_http_server');
exports.ServerResponse = server.ServerResponse;
exports.STATUS_CODES = server.STATUS_CODES;
exports._connectionListener = server._connectionListener;
const Server = exports.Server = server.Server;

exports.createServer = function createServer(requestListener) {
return new Server(requestListener);
};
const Server = server.Server;
const ClientRequest = client.ClientRequest;

const client = require('_http_client');
const ClientRequest = exports.ClientRequest = client.ClientRequest;
function createServer(requestListener) {
return new Server(requestListener);
}

exports.request = function request(options, cb) {
function request(options, cb) {
return new ClientRequest(options, cb);
};
}

exports.get = function get(options, cb) {
var req = exports.request(options, cb);
function get(options, cb) {
var req = request(options, cb);
req.end();
return req;
}

module.exports = {
_connectionListener: server._connectionListener,
METHODS: common.methods.slice().sort(),
STATUS_CODES: server.STATUS_CODES,
Agent: agent.Agent,
ClientRequest,
globalAgent: agent.globalAgent,
IncomingMessage: incoming.IncomingMessage,
OutgoingMessage: outgoing.OutgoingMessage,
Server,
ServerResponse: server.ServerResponse,
createServer,
get,
request
};

0 comments on commit 5425e0d

Please sign in to comment.