Skip to content

Commit

Permalink
lib: use destructuring for some constants
Browse files Browse the repository at this point in the history
This change is to unify the declaration for constants into using
destructuring on the top-level-module scope, reducing some redundant
code.

PR-URL: #16063
Backport-PR-URL: #16494
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
  • Loading branch information
starkwang authored and gibfahn committed Oct 30, 2017
1 parent 9ff29eb commit 1909430
Show file tree
Hide file tree
Showing 34 changed files with 131 additions and 134 deletions.
4 changes: 2 additions & 2 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const net = require('net');
const util = require('util');
const EventEmitter = require('events');
const debug = util.debuglog('http');
const async_id_symbol = process.binding('async_wrap').async_id_symbol;
const nextTick = require('internal/process/next_tick').nextTick;
const { async_id_symbol } = process.binding('async_wrap');
const { nextTick } = require('internal/process/next_tick');

// New Agent code.

Expand Down
24 changes: 13 additions & 11 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@
const util = require('util');
const net = require('net');
const url = require('url');
const HTTPParser = process.binding('http_parser').HTTPParser;
const { HTTPParser } = process.binding('http_parser');
const assert = require('assert').ok;
const common = require('_http_common');
const httpSocketSetup = common.httpSocketSetup;
const parsers = common.parsers;
const freeParser = common.freeParser;
const debug = common.debug;
const OutgoingMessage = require('_http_outgoing').OutgoingMessage;
const {
_checkIsHttpToken: checkIsHttpToken,
debug,
freeParser,
httpSocketSetup,
parsers
} = require('_http_common');
const { OutgoingMessage } = require('_http_outgoing');
const Agent = require('_http_agent');
const Buffer = require('buffer').Buffer;
const { Buffer } = require('buffer');
const { urlToOptions, searchParamsSymbol } = require('internal/url');
const outHeadersKey = require('internal/http').outHeadersKey;
const nextTick = require('internal/process/next_tick').nextTick;
const { outHeadersKey } = require('internal/http');
const { nextTick } = require('internal/process/next_tick');

// The actual list of disallowed characters in regexp form is more like:
// /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
Expand Down Expand Up @@ -149,7 +151,7 @@ function ClientRequest(options, cb) {
}

if (methodIsString && method) {
if (!common._checkIsHttpToken(method)) {
if (!checkIsHttpToken(method)) {
throw new TypeError('Method must be a valid HTTP token');
}
method = this.method = method.toUpperCase();
Expand Down
15 changes: 8 additions & 7 deletions lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@
'use strict';

const binding = process.binding('http_parser');
const methods = binding.methods;
const HTTPParser = binding.HTTPParser;
const { methods, HTTPParser } = binding;

const FreeList = require('internal/freelist');
const ondrain = require('internal/http').ondrain;
const { ondrain } = require('internal/http');
const incoming = require('_http_incoming');
const emitDestroy = require('async_hooks').emitDestroy;
const IncomingMessage = incoming.IncomingMessage;
const readStart = incoming.readStart;
const readStop = incoming.readStop;
const { emitDestroy } = require('async_hooks');
const {
IncomingMessage,
readStart,
readStop
} = incoming;

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

Expand Down
13 changes: 6 additions & 7 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ const Stream = require('stream');
const util = require('util');
const internalUtil = require('internal/util');
const internalHttp = require('internal/http');
const Buffer = require('buffer').Buffer;
const { Buffer } = require('buffer');
const common = require('_http_common');
const checkIsHttpToken = common._checkIsHttpToken;
const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
const outHeadersKey = require('internal/http').outHeadersKey;
const async_id_symbol = process.binding('async_wrap').async_id_symbol;
const nextTick = require('internal/process/next_tick').nextTick;
const { outHeadersKey } = require('internal/http');
const { async_id_symbol } = process.binding('async_wrap');
const { nextTick } = require('internal/process/next_tick');

const CRLF = common.CRLF;
const debug = common.debug;
const utcDate = internalHttp.utcDate;
const { CRLF, debug } = common;
const { utcDate } = internalHttp;

var RE_FIELDS =
/^(?:Connection|Transfer-Encoding|Content-Length|Date|Expect|Trailer|Upgrade)$/i;
Expand Down
24 changes: 13 additions & 11 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@

const util = require('util');
const net = require('net');
const HTTPParser = process.binding('http_parser').HTTPParser;
const { HTTPParser } = process.binding('http_parser');
const assert = require('assert').ok;
const common = require('_http_common');
const parsers = common.parsers;
const freeParser = common.freeParser;
const debug = common.debug;
const CRLF = common.CRLF;
const continueExpression = common.continueExpression;
const chunkExpression = common.chunkExpression;
const httpSocketSetup = common.httpSocketSetup;
const OutgoingMessage = require('_http_outgoing').OutgoingMessage;
const {
parsers,
freeParser,
debug,
CRLF,
continueExpression,
chunkExpression,
httpSocketSetup,
_checkInvalidHeaderChar: checkInvalidHeaderChar
} = require('_http_common');
const { OutgoingMessage } = require('_http_outgoing');
const { outHeadersKey, ondrain } = require('internal/http');

const STATUS_CODES = {
Expand Down Expand Up @@ -222,7 +224,7 @@ function writeHead(statusCode, reason, obj) {
headers = obj;
}

if (common._checkInvalidHeaderChar(this.statusMessage))
if (checkInvalidHeaderChar(this.statusMessage))
throw new Error('Invalid character in statusMessage.');

var statusLine = 'HTTP/1.1 ' + statusCode + ' ' + this.statusMessage + CRLF;
Expand Down
2 changes: 1 addition & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Readable.ReadableState = ReadableState;

const EE = require('events');
const Stream = require('stream');
const Buffer = require('buffer').Buffer;
const { Buffer } = require('buffer');
const util = require('util');
const debug = util.debuglog('stream');
const BufferList = require('internal/streams/BufferList');
Expand Down
4 changes: 2 additions & 2 deletions lib/_stream_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const assert = require('assert');
const util = require('util');
const Socket = require('net').Socket;
const JSStream = process.binding('js_stream').JSStream;
// TODO(bmeurer): Change this back to const once hole checks are
// properly optimized away early in Ignition+TurboFan.
var Buffer = require('buffer').Buffer;
const { Socket } = require('net');
const { JSStream } = process.binding('js_stream');
const uv = process.binding('uv');
const debug = util.debuglog('stream_wrap');

Expand Down
2 changes: 1 addition & 1 deletion lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Writable.WritableState = WritableState;
const util = require('util');
const internalUtil = require('internal/util');
const Stream = require('stream');
const Buffer = require('buffer').Buffer;
const { Buffer } = require('buffer');
const destroyImpl = require('internal/streams/destroy');

util.inherits(Writable, Stream);
Expand Down
3 changes: 1 addition & 2 deletions lib/_tls_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@

const tls = require('tls');

const SSL_OP_CIPHER_SERVER_PREFERENCE =
process.binding('constants').crypto.SSL_OP_CIPHER_SERVER_PREFERENCE;
const { SSL_OP_CIPHER_SERVER_PREFERENCE } = process.binding('constants').crypto;

// Lazily loaded
var crypto = null;
Expand Down
6 changes: 3 additions & 3 deletions lib/_tls_legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ const internalUtil = require('internal/util');
internalUtil.assertCrypto();

const assert = require('assert');
const Buffer = require('buffer').Buffer;
const { Buffer } = require('buffer');
const common = require('_tls_common');
const Connection = process.binding('crypto').Connection;
const { Connection } = process.binding('crypto');
const EventEmitter = require('events');
const stream = require('stream');
const Timer = process.binding('timer_wrap').Timer;
const { Timer } = process.binding('timer_wrap');
const tls = require('tls');
const util = require('util');

Expand Down
10 changes: 5 additions & 5 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ const net = require('net');
const tls = require('tls');
const util = require('util');
const common = require('_tls_common');
const StreamWrap = require('_stream_wrap').StreamWrap;
const Buffer = require('buffer').Buffer;
const { StreamWrap } = require('_stream_wrap');
const { Buffer } = require('buffer');
const debug = util.debuglog('tls');
const Timer = process.binding('timer_wrap').Timer;
const { Timer } = process.binding('timer_wrap');
const tls_wrap = process.binding('tls_wrap');
const TCP = process.binding('tcp_wrap').TCP;
const Pipe = process.binding('pipe_wrap').Pipe;
const { TCP } = process.binding('tcp_wrap');
const { Pipe } = process.binding('pipe_wrap');
const kDisableRenegotiation = Symbol('disable-renegotiation');

function onhandshakestart() {
Expand Down
13 changes: 8 additions & 5 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ const debug = util.debuglog('child_process');

const uv = process.binding('uv');
const spawn_sync = process.binding('spawn_sync');
const Buffer = require('buffer').Buffer;
const Pipe = process.binding('pipe_wrap').Pipe;
const { Buffer } = require('buffer');
const { Pipe } = process.binding('pipe_wrap');
const child_process = require('internal/child_process');
const {
_validateStdio,
setupChannel,
ChildProcess
} = child_process;

const errnoException = util._errnoException;
const _validateStdio = child_process._validateStdio;
const setupChannel = child_process.setupChannel;
const ChildProcess = exports.ChildProcess = child_process.ChildProcess;
exports.ChildProcess = ChildProcess;

function stdioStringToArray(option) {
switch (option) {
Expand Down
13 changes: 6 additions & 7 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@

const assert = require('assert');
const errors = require('internal/errors');
const Buffer = require('buffer').Buffer;
const { Buffer } = require('buffer');
const dns = require('dns');
const util = require('util');
const { isUint8Array } = require('internal/util/types');
const EventEmitter = require('events');
const setInitTriggerId = require('async_hooks').setInitTriggerId;
const UV_UDP_REUSEADDR = process.binding('constants').os.UV_UDP_REUSEADDR;
const async_id_symbol = process.binding('async_wrap').async_id_symbol;
const nextTick = require('internal/process/next_tick').nextTick;
const { setInitTriggerId } = require('async_hooks');
const { UV_UDP_REUSEADDR } = process.binding('constants').os;
const { async_id_symbol } = process.binding('async_wrap');
const { nextTick } = require('internal/process/next_tick');

const UDP = process.binding('udp_wrap').UDP;
const SendWrap = process.binding('udp_wrap').SendWrap;
const { UDP, SendWrap } = process.binding('udp_wrap');

const BIND_STATE_UNBOUND = 0;
const BIND_STATE_BINDING = 1;
Expand Down
5 changes: 1 addition & 4 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const util = require('util');

const cares = process.binding('cares_wrap');
const uv = process.binding('uv');
const internalNet = require('internal/net');
const { isLegalPort } = require('internal/net');
const { customPromisifyArgs } = require('internal/util');

const {
Expand All @@ -36,9 +36,6 @@ const {
isIP
} = cares;

const isLegalPort = internalNet.isLegalPort;


function errnoException(err, syscall, hostname) {
// FIXME(bnoordhuis) Remove this backwards compatibility nonsense and pass
// the true error to the user. ENOTFOUND is not even a proper POSIX error!
Expand Down
2 changes: 1 addition & 1 deletion lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

const util = require('util');
const EventEmitter = require('events');
const inherits = util.inherits;
const { inherits } = util;

// communicate with events module, but don't require that
// module to have to load this one, since this module has
Expand Down
22 changes: 10 additions & 12 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,28 @@ const { createPromise, promiseResolve } = process.binding('util');

const binding = process.binding('fs');
const fs = exports;
const Buffer = require('buffer').Buffer;
const { Buffer } = require('buffer');
const errors = require('internal/errors');
const Stream = require('stream').Stream;
const { Readable, Writable } = require('stream');
const EventEmitter = require('events');
const FSReqWrap = binding.FSReqWrap;
const FSEvent = process.binding('fs_event_wrap').FSEvent;
const { FSReqWrap } = binding;
const { FSEvent } = process.binding('fs_event_wrap');
const internalFS = require('internal/fs');
const internalURL = require('internal/url');
const { getPathFromURL } = require('internal/url');
const internalUtil = require('internal/util');
const assertEncoding = internalFS.assertEncoding;
const stringToFlags = internalFS.stringToFlags;
const getPathFromURL = internalURL.getPathFromURL;
const {
assertEncoding,
stringToFlags
} = internalFS;

Object.defineProperty(exports, 'constants', {
configurable: false,
enumerable: true,
value: constants
});

const Readable = Stream.Readable;
const Writable = Stream.Writable;

const kMinPoolSpace = 128;
const kMaxLength = require('buffer').kMaxLength;
const { kMaxLength } = require('buffer');

const isWindows = process.platform === 'win32';

Expand Down
5 changes: 2 additions & 3 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
'use strict';

const agent = require('_http_agent');
const client = require('_http_client');
const { ClientRequest } = require('_http_client');
const common = require('_http_common');
const incoming = require('_http_incoming');
const outgoing = require('_http_outgoing');
const server = require('_http_server');

const Server = server.Server;
const ClientRequest = client.ClientRequest;
const { Server } = server;

function createServer(requestListener) {
return new Server(requestListener);
Expand Down
18 changes: 8 additions & 10 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
'use strict';

const errors = require('internal/errors');
const StringDecoder = require('string_decoder').StringDecoder;
const { StringDecoder } = require('string_decoder');
const EventEmitter = require('events');
const net = require('net');
const dgram = require('dgram');
const util = require('util');
const assert = require('assert');

const Process = process.binding('process_wrap').Process;
const WriteWrap = process.binding('stream_wrap').WriteWrap;
const uv = process.binding('uv');
const Pipe = process.binding('pipe_wrap').Pipe;
const TTY = process.binding('tty_wrap').TTY;
const TCP = process.binding('tcp_wrap').TCP;
const UDP = process.binding('udp_wrap').UDP;
const { Process } = process.binding('process_wrap');
const { WriteWrap } = process.binding('stream_wrap');
const { Pipe } = process.binding('pipe_wrap');
const { TTY } = process.binding('tty_wrap');
const { TCP } = process.binding('tcp_wrap');
const { UDP } = process.binding('udp_wrap');
const SocketList = require('internal/socket_list');
const { convertToValidSignal } = require('internal/util');
const { isUint8Array } = require('internal/util/types');

const errnoException = util._errnoException;
const SocketListSend = SocketList.SocketListSend;
const SocketListReceive = SocketList.SocketListReceive;
const { SocketListSend, SocketListReceive } = SocketList;

const MAX_HANDLE_RETRANSMISSIONS = 3;

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/cluster/master.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
const assert = require('assert');
const fork = require('child_process').fork;
const { fork } = require('child_process');
const util = require('util');
const EventEmitter = require('events');
const RoundRobinHandle = require('internal/cluster/round_robin_handle');
Expand Down
Loading

0 comments on commit 1909430

Please sign in to comment.