Browse Source

http: use more efficient module.exports pattern

PR-URL: https://github.com/nodejs/node/pull/11594
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
v6
James M Snell 8 years ago
parent
commit
5425e0dcbe
  1. 6
      lib/_http_agent.js
  2. 5
      lib/_http_client.js
  3. 24
      lib/_http_common.js
  4. 12
      lib/_http_incoming.js
  5. 5
      lib/_http_outgoing.js
  6. 16
      lib/_http_server.js
  7. 50
      lib/http.js

6
lib/_http_agent.js

@ -105,7 +105,6 @@ function Agent(options) {
} }
util.inherits(Agent, EventEmitter); util.inherits(Agent, EventEmitter);
exports.Agent = Agent;
Agent.defaultMaxSockets = Infinity; Agent.defaultMaxSockets = Infinity;
@ -314,4 +313,7 @@ Agent.prototype.destroy = function destroy() {
} }
}; };
exports.globalAgent = new Agent(); module.exports = {
Agent,
globalAgent: new Agent()
};

5
lib/_http_client.js

@ -286,7 +286,6 @@ function ClientRequest(options, cb) {
util.inherits(ClientRequest, OutgoingMessage); util.inherits(ClientRequest, OutgoingMessage);
exports.ClientRequest = ClientRequest;
ClientRequest.prototype._finish = function _finish() { ClientRequest.prototype._finish = function _finish() {
DTRACE_HTTP_CLIENT_REQUEST(this, this.connection); DTRACE_HTTP_CLIENT_REQUEST(this, this.connection);
@ -752,3 +751,7 @@ ClientRequest.prototype.setSocketKeepAlive =
ClientRequest.prototype.clearTimeout = function clearTimeout(cb) { ClientRequest.prototype.clearTimeout = function clearTimeout(cb) {
this.setTimeout(0, cb); this.setTimeout(0, cb);
}; };
module.exports = {
ClientRequest
};

24
lib/_http_common.js

@ -32,12 +32,6 @@ const readStart = incoming.readStart;
const readStop = incoming.readStop; const readStop = incoming.readStop;
const debug = require('util').debuglog('http'); 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 kOnHeaders = HTTPParser.kOnHeaders | 0;
const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0; const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
@ -194,7 +188,6 @@ var parsers = new FreeList('parsers', 1000, function() {
return parser; return parser;
}); });
exports.parsers = parsers;
// Free the parser and also break any links that it // Free the parser and also break any links that it
@ -227,7 +220,6 @@ function freeParser(parser, req, socket) {
socket.parser = null; socket.parser = null;
} }
} }
exports.freeParser = freeParser;
function ondrain() { function ondrain() {
@ -239,7 +231,6 @@ function httpSocketSetup(socket) {
socket.removeListener('drain', ondrain); socket.removeListener('drain', ondrain);
socket.on('drain', ondrain); socket.on('drain', ondrain);
} }
exports.httpSocketSetup = httpSocketSetup;
/** /**
* Verifies that the given val is a valid HTTP token * Verifies that the given val is a valid HTTP token
@ -306,7 +297,6 @@ function checkIsHttpToken(val) {
} }
return true; return true;
} }
exports._checkIsHttpToken = checkIsHttpToken;
/** /**
* True if val contains an invalid field-vchar * True if val contains an invalid field-vchar
@ -360,4 +350,16 @@ function checkInvalidHeaderChar(val) {
} }
return false; 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
lib/_http_incoming.js

@ -28,14 +28,11 @@ function readStart(socket) {
if (socket && !socket._paused && socket.readable) if (socket && !socket._paused && socket.readable)
socket.resume(); socket.resume();
} }
exports.readStart = readStart;
function readStop(socket) { function readStop(socket) {
if (socket) if (socket)
socket.pause(); socket.pause();
} }
exports.readStop = readStop;
/* Abstract base class for ServerRequest and ClientResponse. */ /* Abstract base class for ServerRequest and ClientResponse. */
function IncomingMessage(socket) { function IncomingMessage(socket) {
@ -83,9 +80,6 @@ function IncomingMessage(socket) {
util.inherits(IncomingMessage, Stream.Readable); util.inherits(IncomingMessage, Stream.Readable);
exports.IncomingMessage = IncomingMessage;
IncomingMessage.prototype.setTimeout = function setTimeout(msecs, callback) { IncomingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
if (callback) if (callback)
this.on('timeout', callback); this.on('timeout', callback);
@ -324,3 +318,9 @@ IncomingMessage.prototype._dump = function _dump() {
this.resume(); this.resume();
} }
}; };
module.exports = {
IncomingMessage,
readStart,
readStop
};

5
lib/_http_outgoing.js

@ -887,3 +887,8 @@ OutgoingMessage.prototype.flushHeaders = function flushHeaders() {
OutgoingMessage.prototype.flush = internalUtil.deprecate(function() { OutgoingMessage.prototype.flush = internalUtil.deprecate(function() {
this.flushHeaders(); this.flushHeaders();
}, 'OutgoingMessage.flush is deprecated. Use flushHeaders instead.', 'DEP0001'); }, 'OutgoingMessage.flush is deprecated. Use flushHeaders instead.', 'DEP0001');
module.exports = {
OutgoingMessage
};

16
lib/_http_server.js

@ -36,7 +36,7 @@ const httpSocketSetup = common.httpSocketSetup;
const OutgoingMessage = require('_http_outgoing').OutgoingMessage; const OutgoingMessage = require('_http_outgoing').OutgoingMessage;
const outHeadersKey = require('internal/http').outHeadersKey; const outHeadersKey = require('internal/http').outHeadersKey;
const STATUS_CODES = exports.STATUS_CODES = { const STATUS_CODES = {
100: 'Continue', 100: 'Continue',
101: 'Switching Protocols', 101: 'Switching Protocols',
102: 'Processing', // RFC 2518, obsoleted by RFC 4918 102: 'Processing', // RFC 2518, obsoleted by RFC 4918
@ -128,8 +128,6 @@ ServerResponse.prototype._finish = function _finish() {
}; };
exports.ServerResponse = ServerResponse;
ServerResponse.prototype.statusCode = 200; ServerResponse.prototype.statusCode = 200;
ServerResponse.prototype.statusMessage = undefined; ServerResponse.prototype.statusMessage = undefined;
@ -290,9 +288,6 @@ Server.prototype.setTimeout = function setTimeout(msecs, callback) {
}; };
exports.Server = Server;
function connectionListener(socket) { function connectionListener(socket) {
debug('SERVER new http connection'); debug('SERVER new http connection');
@ -363,7 +358,7 @@ function connectionListener(socket) {
socket._paused = false; socket._paused = false;
} }
exports._connectionListener = connectionListener;
function updateOutgoingData(socket, state, delta) { function updateOutgoingData(socket, state, delta) {
state.outgoingData += delta; state.outgoingData += delta;
@ -640,3 +635,10 @@ function socketOnWrap(ev, fn) {
return res; return res;
} }
module.exports = {
STATUS_CODES,
Server,
ServerResponse,
_connectionListener: connectionListener
};

50
lib/http.js

@ -20,35 +20,43 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE. // USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict'; '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'); const agent = require('_http_agent');
exports.Agent = agent.Agent; const client = require('_http_client');
exports.globalAgent = agent.globalAgent; const common = require('_http_common');
const incoming = require('_http_incoming');
const outgoing = require('_http_outgoing');
const server = require('_http_server'); 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) { const Server = server.Server;
return new Server(requestListener); const ClientRequest = client.ClientRequest;
};
const client = require('_http_client'); function createServer(requestListener) {
const ClientRequest = exports.ClientRequest = client.ClientRequest; return new Server(requestListener);
}
exports.request = function request(options, cb) { function request(options, cb) {
return new ClientRequest(options, cb); return new ClientRequest(options, cb);
}; }
exports.get = function get(options, cb) { function get(options, cb) {
var req = exports.request(options, cb); var req = request(options, cb);
req.end(); req.end();
return req; 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
}; };

Loading…
Cancel
Save