lib: use destructuring for some constants

This change is to unify the declaration for constants into using
destructuring on the top-level-module scope, reducing some redundant
code.

PR-URL: https://github.com/nodejs/node/pull/16063
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>
This commit is contained in:
Weijia Wang 2017-10-07 22:50:42 +08:00 committed by Tobias Nießen
parent a3a106865a
commit 212de3c5ec
No known key found for this signature in database
GPG Key ID: 718207F8FD156B70
35 changed files with 132 additions and 135 deletions

View File

@ -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.

View File

@ -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');
const errors = require('internal/errors');
// The actual list of disallowed characters in regexp form is more like:
@ -149,7 +151,7 @@ function ClientRequest(options, cb) {
}
if (methodIsString && method) {
if (!common._checkIsHttpToken(method)) {
if (!checkIsHttpToken(method)) {
throw new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Method', method);
}
method = this.method = method.toUpperCase();

View File

@ -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');

View File

@ -26,18 +26,17 @@ 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 errors = require('internal/errors');
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;

View File

@ -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 errors = require('internal/errors');
@ -223,7 +225,7 @@ function writeHead(statusCode, reason, obj) {
headers = obj;
}
if (common._checkInvalidHeaderChar(this.statusMessage))
if (checkInvalidHeaderChar(this.statusMessage))
throw new errors.Error('ERR_INVALID_CHAR', 'statusMessage');
var statusLine = 'HTTP/1.1 ' + statusCode + ' ' + this.statusMessage + CRLF;

View File

@ -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');

View File

@ -2,8 +2,8 @@
const assert = require('assert');
const util = require('util');
const Socket = require('net').Socket;
const JSStream = process.binding('js_stream').JSStream;
const { Socket } = require('net');
const { JSStream } = process.binding('js_stream');
const uv = process.binding('uv');
const debug = util.debuglog('stream_wrap');
const errors = require('internal/errors');

View File

@ -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');
const errors = require('internal/errors');

View File

@ -26,8 +26,7 @@ const { isArrayBufferView } = require('internal/util/types');
const tls = require('tls');
const errors = require('internal/errors');
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;

View File

@ -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');

View File

@ -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 errors = require('internal/errors');
const kConnectOptions = Symbol('connect-options');
const kDisableRenegotiation = Symbol('disable-renegotiation');

View File

@ -27,15 +27,17 @@ const { isUint8Array } = require('internal/util/types');
const { createPromise,
promiseResolve, promiseReject } = process.binding('util');
const debug = util.debuglog('child_process');
const Buffer = require('buffer').Buffer;
const Pipe = process.binding('pipe_wrap').Pipe;
const { Buffer } = require('buffer');
const { Pipe } = process.binding('pipe_wrap');
const { errname } = process.binding('uv');
const child_process = require('internal/child_process');
const {
_validateStdio,
setupChannel,
ChildProcess
} = child_process;
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) {

View File

@ -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;

View File

@ -24,7 +24,7 @@
const util = require('util');
const cares = process.binding('cares_wrap');
const internalNet = require('internal/net');
const { isLegalPort } = require('internal/net');
const { customPromisifyArgs } = require('internal/util');
const errors = require('internal/errors');
const {
@ -41,9 +41,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!

View File

@ -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

View File

@ -33,18 +33,19 @@ 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,
@ -52,11 +53,8 @@ Object.defineProperty(exports, 'constants', {
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';

View File

@ -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);

View File

@ -27,7 +27,7 @@ const tls = require('tls');
const url = require('url');
const http = require('http');
const util = require('util');
const inherits = util.inherits;
const { inherits } = util;
const debug = util.debuglog('https');
const { urlToOptions, searchParamsSymbol } = require('internal/url');
const errors = require('internal/errors');

View File

@ -1,19 +1,19 @@
'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 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');
@ -30,8 +30,7 @@ const {
} = process.binding('uv');
const errnoException = util._errnoException;
const SocketListSend = SocketList.SocketListSend;
const SocketListReceive = SocketList.SocketListReceive;
const { SocketListSend, SocketListReceive } = SocketList;
const MAX_HANDLE_RETRANSMISSIONS = 3;

View File

@ -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');

View File

@ -1,7 +1,7 @@
'use strict';
const Buffer = require('buffer').Buffer;
const Writable = require('stream').Writable;
const { Buffer } = require('buffer');
const { Writable } = require('stream');
const errors = require('internal/errors');
const fs = require('fs');
const util = require('util');

View File

@ -6,7 +6,7 @@ require('internal/util').assertCrypto();
const binding = process.binding('http2');
const assert = require('assert');
const Buffer = require('buffer').Buffer;
const { Buffer } = require('buffer');
const EventEmitter = require('events');
const net = require('net');
const tls = require('tls');

View File

@ -1,9 +1,7 @@
'use strict';
const process = require('process');
const path = require('path');
const fs = require('fs');
const mkdirSync = fs.mkdirSync;
const writeFileSync = fs.writeFileSync;
const { mkdirSync, writeFileSync } = require('fs');
function writeCoverage() {
if (!global.__coverage__) {

View File

@ -1,6 +1,6 @@
'use strict';
const Interface = require('readline').Interface;
const { Interface } = require('readline');
const REPL = require('repl');
const path = require('path');
const fs = require('fs');

View File

@ -1,6 +1,6 @@
'use strict';
const Buffer = require('buffer').Buffer;
const { Buffer } = require('buffer');
function copyBuffer(src, target, offset) {
Buffer.prototype.copy.call(src, target, offset);

View File

@ -2,7 +2,7 @@
const errors = require('internal/errors');
const binding = process.binding('util');
const signals = process.binding('constants').os.signals;
const { signals } = process.binding('constants').os;
const { createPromise, promiseResolve, promiseReject } = binding;

View File

@ -30,8 +30,10 @@ const assert = require('assert').ok;
const fs = require('fs');
const internalFS = require('internal/fs');
const path = require('path');
const internalModuleReadFile = process.binding('fs').internalModuleReadFile;
const internalModuleStat = process.binding('fs').internalModuleStat;
const {
internalModuleReadFile,
internalModuleStat
} = process.binding('fs');
const preserveSymlinks = !!process.binding('config').preserveSymlinks;
const experimentalModules = !!process.binding('config').experimentalModules;

View File

@ -26,7 +26,7 @@ const stream = require('stream');
const timers = require('timers');
const util = require('util');
const internalUtil = require('internal/util');
const internalNet = require('internal/net');
const { isLegalPort, normalizedArgsSymbol } = require('internal/net');
const assert = require('assert');
const cares = process.binding('cares_wrap');
const {
@ -35,17 +35,16 @@ const {
UV_EOF
} = process.binding('uv');
const Buffer = require('buffer').Buffer;
const { Buffer } = require('buffer');
const TTYWrap = process.binding('tty_wrap');
const TCP = process.binding('tcp_wrap').TCP;
const Pipe = process.binding('pipe_wrap').Pipe;
const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
const PipeConnectWrap = process.binding('pipe_wrap').PipeConnectWrap;
const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap;
const WriteWrap = process.binding('stream_wrap').WriteWrap;
const async_id_symbol = process.binding('async_wrap').async_id_symbol;
const { TCP } = process.binding('tcp_wrap');
const { Pipe } = process.binding('pipe_wrap');
const { TCPConnectWrap } = process.binding('tcp_wrap');
const { PipeConnectWrap } = process.binding('pipe_wrap');
const { ShutdownWrap, WriteWrap } = process.binding('stream_wrap');
const { async_id_symbol } = process.binding('async_wrap');
const { newUid, setInitTriggerId } = require('async_hooks');
const nextTick = require('internal/process/next_tick').nextTick;
const { nextTick } = require('internal/process/next_tick');
const errors = require('internal/errors');
const dns = require('dns');
@ -55,8 +54,6 @@ var cluster = null;
const errnoException = util._errnoException;
const exceptionWithHostPort = util._exceptionWithHostPort;
const isLegalPort = internalNet.isLegalPort;
const normalizedArgsSymbol = internalNet.normalizedArgsSymbol;
function noop() {}

View File

@ -21,10 +21,10 @@
'use strict';
const pushValToArrayMax = process.binding('util').pushValToArrayMax;
const { pushValToArrayMax } = process.binding('util');
const constants = process.binding('constants').os;
const deprecate = require('internal/util').deprecate;
const getCIDRSuffix = require('internal/os').getCIDRSuffix;
const { deprecate } = require('internal/util');
const { getCIDRSuffix } = require('internal/os');
const isWindows = process.platform === 'win32';
const {

View File

@ -48,7 +48,7 @@ const {
kClearScreenDown
} = CSI;
const now = process.binding('timer_wrap').Timer.now;
const { now } = process.binding('timer_wrap').Timer;
const kHistorySize = 30;
const kMincrlfDelay = 100;

View File

@ -47,13 +47,13 @@ const internalUtil = require('internal/util');
const { isTypedArray } = require('internal/util/types');
const util = require('util');
const utilBinding = process.binding('util');
const inherits = util.inherits;
const { inherits } = util;
const Stream = require('stream');
const vm = require('vm');
const path = require('path');
const fs = require('fs');
const Interface = require('readline').Interface;
const Console = require('console').Console;
const { Interface } = require('readline');
const { Console } = require('console');
const Module = require('module');
const domain = require('domain');
const debug = util.debuglog('repl');

View File

@ -21,7 +21,7 @@
'use strict';
const Buffer = require('buffer').Buffer;
const { Buffer } = require('buffer');
// Note: export Stream before Readable/Writable/Duplex/...
// to avoid a cross-reference(require) issues

View File

@ -21,7 +21,7 @@
'use strict';
const Buffer = require('buffer').Buffer;
const { Buffer } = require('buffer');
const internalUtil = require('internal/util');
const errors = require('internal/errors');
const isEncoding = Buffer[internalUtil.kIsEncodingSymbol];

View File

@ -26,17 +26,21 @@ const TimerWrap = process.binding('timer_wrap').Timer;
const L = require('internal/linkedlist');
const internalUtil = require('internal/util');
const { createPromise, promiseResolve } = process.binding('util');
const async_hooks = require('async_hooks');
const assert = require('assert');
const util = require('util');
const errors = require('internal/errors');
const debug = util.debuglog('timer');
const kOnTimeout = TimerWrap.kOnTimeout | 0;
const initTriggerId = async_hooks.initTriggerId;
// Two arrays that share state between C++ and JS.
const { async_hook_fields, async_id_fields } = async_wrap;
// The needed emit*() functions.
const { emitInit, emitBefore, emitAfter, emitDestroy } = async_hooks;
const {
initTriggerId,
// The needed emit*() functions.
emitInit,
emitBefore,
emitAfter,
emitDestroy
} = require('async_hooks');
// Grab the constants necessary for working with internal arrays.
const { kInit, kDestroy, kAsyncIdCounter } = async_wrap.constants;
// Symbols for storing async id state.

View File

@ -23,9 +23,8 @@
const util = require('util');
const net = require('net');
const TTY = process.binding('tty_wrap').TTY;
const isTTY = process.binding('tty_wrap').isTTY;
const inherits = util.inherits;
const { TTY, isTTY } = process.binding('tty_wrap');
const { inherits } = util;
const errnoException = util._errnoException;
const errors = require('internal/errors');
const readline = require('readline');