function getContextOptions(options) { if (options) { validateObject(options.contextCodeGeneration, 'options.contextCodeGeneration'); const contextOptions = { name: options.contextName, origin: options.contextOrigin, codeGeneration: typeof options.contextCodeGeneration === 'object' ? { strings: options.contextCodeGeneration.strings, wasm: options.contextCodeGeneration.wasm, } : undefined, }; if (contextOptions.name !== undefined) validateString(contextOptions.name, 'options.contextName'); if (contextOptions.origin !== undefined) validateString(contextOptions.origin, 'options.contextOrigin'); if (contextOptions.codeGeneration) { validateBool(contextOptions.codeGeneration.strings, 'options.contextCodeGeneration.strings'); validateBool(contextOptions.codeGeneration.wasm, 'options.contextCodeGeneration.wasm'); } return contextOptions; } return {}; }
function createContext(sandbox = {}, options = {}) { if (isContext(sandbox)) { return sandbox; } if (typeof options !== 'object' || options === null) { throw new ERR_INVALID_ARG_TYPE('options', 'Object', options); } const { name = `VM Context ${defaultContextNameIndex++}`, origin, codeGeneration } = options; validateString(name, 'options.name'); if (origin !== undefined) validateString(origin, 'options.origin'); validateObject(codeGeneration, 'options.codeGeneration'); let strings = true; let wasm = true; if (codeGeneration !== undefined) { ({ strings = true, wasm = true } = codeGeneration); validateBool(strings, 'options.codeGeneration.strings'); validateBool(wasm, 'options.codeGeneration.wasm'); } makeContext(sandbox, name, origin, strings, wasm); return sandbox; }
post(method, params, callback) { validateString(method, 'method'); if (!callback && util.isFunction(params)) { callback = params; params = null; } if (params && typeof params !== 'object') { throw new ERR_INVALID_ARG_TYPE('params', 'Object', params); } if (callback && typeof callback !== 'function') { throw new ERR_INVALID_CALLBACK(); } if (!this[connectionSymbol]) { throw new ERR_INSPECTOR_NOT_CONNECTED(); } const id = this[nextIdSymbol]++; const message = { id, method }; if (params) { message.params = params; } if (callback) { this[messageCallbacksSymbol].set(id, callback); } this[connectionSymbol].dispatch(JSON.stringify(message)); }
OutgoingMessage.prototype.removeHeader = function removeHeader(name) { validateString(name, 'name'); if (this._header) { throw new ERR_HTTP_HEADERS_SENT('remove'); } const key = name.toLowerCase(); switch (key) { case 'connection': this._removedConnection = true; break; case 'content-length': this._removedContLen = true; break; case 'transfer-encoding': this._removedTE = true; break; case 'date': this.sendDate = false; break; } if (this[outHeadersKey] !== null) { delete this[outHeadersKey][key]; } };
setHeader(name, value) { validateString(name, 'name'); if (this[kStream].headersSent) throw new ERR_HTTP2_HEADERS_SENT(); this[kSetHeader](name, value); }
function verifyOneShot(algorithm, data, key, signature) { if (algorithm != null) validateString(algorithm, 'algorithm'); if (!isArrayBufferView(data)) { throw new ERR_INVALID_ARG_TYPE( 'data', ['Buffer', 'TypedArray', 'DataView'], data ); } const { data: keyData, format: keyFormat, type: keyType, passphrase: keyPassphrase } = preparePublicOrPrivateKey(key); // Options specific to RSA const rsaPadding = getPadding(key); const pssSaltLength = getSaltLength(key); if (!isArrayBufferView(signature)) { throw new ERR_INVALID_ARG_TYPE( 'signature', ['Buffer', 'TypedArray', 'DataView'], signature ); } return _verifyOneShot(keyData, keyFormat, keyType, keyPassphrase, signature, data, algorithm, rsaPadding, pssSaltLength); }
constructor(type, opts = {}) { validateString(type, 'type'); if (typeof opts === 'number') { opts = { triggerAsyncId: opts, requireManualDestroy: false }; } else if (opts.triggerAsyncId === undefined) { opts.triggerAsyncId = getDefaultTriggerAsyncId(); } // Unlike emitInitScript, AsyncResource doesn't supports null as the // triggerAsyncId. const triggerAsyncId = opts.triggerAsyncId; if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < -1) { throw new ERR_INVALID_ASYNC_ID('triggerAsyncId', triggerAsyncId); } const asyncId = newAsyncId(); this[async_id_symbol] = asyncId; this[trigger_async_id_symbol] = triggerAsyncId; // This prop name (destroyed) has to be synchronized with C++ const destroyed = { destroyed: false }; this[destroyedSymbol] = destroyed; if (initHooksExist()) { emitInit(asyncId, type, triggerAsyncId, this); } if (!opts.requireManualDestroy) { registerDestroyHook(this, asyncId, destroyed); } }
function signOneShot(algorithm, data, key) { if (algorithm != null) validateString(algorithm, 'algorithm'); if (!isArrayBufferView(data)) { throw new ERR_INVALID_ARG_TYPE( 'data', ['Buffer', 'TypedArray', 'DataView'], data ); } if (!key) throw new ERR_CRYPTO_SIGN_KEY_REQUIRED(); const { data: keyData, format: keyFormat, type: keyType, passphrase: keyPassphrase } = preparePrivateKey(key); // Options specific to RSA const rsaPadding = getPadding(key); const pssSaltLength = getSaltLength(key); return _signOneShot(keyData, keyFormat, keyType, keyPassphrase, data, algorithm, rsaPadding, pssSaltLength); }
Interface.prototype._writeToOutput = function _writeToOutput(stringToWrite) { validateString(stringToWrite, 'stringToWrite'); if (this.output !== null && this.output !== undefined) { this.output.write(stringToWrite); } };
Socket.prototype.connect = function(port, address, callback) { port = validatePort(port); if (typeof address === 'function') { callback = address; address = ''; } else if (address === undefined) { address = ''; } validateString(address, 'address'); const state = this[kStateSymbol]; if (state.connectState !== CONNECT_STATE_DISCONNECTED) throw new ERR_SOCKET_DGRAM_IS_CONNECTED(); state.connectState = CONNECT_STATE_CONNECTING; if (state.bindState === BIND_STATE_UNBOUND) this.bind({ port: 0, exclusive: true }, null); if (state.bindState !== BIND_STATE_BOUND) { enqueue(this, _connect.bind(this, port, address, callback)); return; } _connect.call(this, port, address, callback); };
function ECDH(curve) { if (!(this instanceof ECDH)) return new ECDH(curve); validateString(curve, 'curve'); this[kHandle] = new _ECDH(curve); }
set method(method) { validateString(method, 'method'); if (method.trim() === '') throw new ERR_INVALID_ARG_VALUE('method', method); this[kHeaders][HTTP2_HEADER_METHOD] = method; }
removeHeader(name) { validateString(name, 'name'); if (this[kStream].headersSent) throw new ERR_HTTP2_HEADERS_SENT(); name = name.trim().toLowerCase(); delete this[kHeaders][name]; }
set(url, job) { validateString(url, 'url'); if (job instanceof ModuleJob !== true) { throw new ERR_INVALID_ARG_TYPE('job', 'ModuleJob', job); } debug(`Storing ${url} in ModuleMap`); return super.set(url, job); }
Module.prototype.require = function(id) { validateString(id, 'id'); if (id === '') { throw new ERR_INVALID_ARG_VALUE('id', id, 'must be a non-empty string'); } return Module._load(id, this, /* isMain */ false); };
function createCipherWithIV(cipher, key, options, decipher, iv) { validateString(cipher, 'cipher'); key = prepareSecretKey(key); iv = toBuf(iv); if (iv !== null && !isArrayBufferView(iv)) { throw invalidArrayBufferView('iv', iv); } createCipherBase.call(this, cipher, key, options, decipher, iv); }
Socket.prototype.setMulticastInterface = function(interfaceAddress) { healthCheck(this); validateString(interfaceAddress, 'interfaceAddress'); const err = this[kStateSymbol].handle.setMulticastInterface(interfaceAddress); if (err) { throw errnoException(err, 'setMulticastInterface'); } };
function Verify(algorithm, options) { if (!(this instanceof Verify)) return new Verify(algorithm, options); validateString(algorithm, 'algorithm'); this[kHandle] = new _Verify(); this[kHandle].init(algorithm); Writable.call(this, options); }
function Sign(algorithm, options) { if (!(this instanceof Sign)) return new Sign(algorithm, options); validateString(algorithm, 'algorithm'); this[kHandle] = new _Sign(); this[kHandle].init(algorithm); Writable.call(this, options); }
function createCipher(cipher, password, options, decipher) { validateString(cipher, 'cipher'); password = toBuf(password); if (!isArrayBufferView(password)) { throw invalidArrayBufferView('password', password); } createCipherBase.call(this, cipher, password, options, decipher); }
TLSSocket.prototype.setServername = function(name) { validateString(name, 'name'); if (this._tlsOptions.isServer) { throw new ERR_TLS_SNI_FROM_SERVER(); } this._handle.setServername(name); };
OutgoingMessage.prototype.getHeader = function getHeader(name) { validateString(name, 'name'); const headers = this[outHeadersKey]; if (headers === null) return; const entry = headers[name.toLowerCase()]; return entry && entry[1]; };
function Hash(algorithm, options) { if (!(this instanceof Hash)) return new Hash(algorithm, options); validateString(algorithm, 'algorithm'); this[kHandle] = new _Hash(algorithm); this[kState] = { [kFinalized]: false }; LazyTransform.call(this, options); }
Socket.prototype.connect = function(...args) { let normalized; // If passed an array, it's treated as an array of arguments that have // already been normalized (so we don't normalize more than once). This has // been solved before in https://github.com/nodejs/node/pull/12342, but was // reverted as it had unintended side effects. if (Array.isArray(args[0]) && args[0][normalizedArgsSymbol]) { normalized = args[0]; } else { normalized = normalizeArgs(args); } var options = normalized[0]; var cb = normalized[1]; if (this.write !== Socket.prototype.write) this.write = Socket.prototype.write; if (this.destroyed) { this._undestroy(); this._handle = null; this._peername = null; this._sockname = null; } const { path } = options; var pipe = !!path; debug('pipe', pipe, path); if (!this._handle) { this._handle = pipe ? new Pipe(PipeConstants.SOCKET) : new TCP(TCPConstants.SOCKET); initSocketHandle(this); } if (cb !== null) { this.once('connect', cb); } this._unrefTimer(); this.connecting = true; this.writable = true; if (pipe) { validateString(path, 'options.path'); defaultTriggerAsyncIdScope( this[async_id_symbol], internalConnect, this, path ); } else { lookupAndConnect(this, options); } return this; };
function Hmac(hmac, key, options) { if (!(this instanceof Hmac)) return new Hmac(hmac, key, options); validateString(hmac, 'hmac'); key = prepareSecretKey(key); this[kHandle] = new _Hmac(); this[kHandle].init(hmac, toBuf(key)); this[kState] = { [kFinalized]: false }; LazyTransform.call(this, options); }
function setEngine(id, flags) { validateString(id, 'id'); if (flags && typeof flags !== 'number') throw new ERR_INVALID_ARG_TYPE('flags', 'number', flags); flags = flags >>> 0; // Use provided engine for everything by default if (flags === 0) flags = ENGINE_METHOD_ALL; if (!_setEngine(id, flags)) throw new ERR_CRYPTO_ENGINE_UNKNOWN(id); }
Socket.prototype.sendto = function(buffer, offset, length, port, address, callback) { validateNumber(offset, 'offset'); validateNumber(length, 'length'); validateNumber(port, 'port'); validateString(address, 'address'); this.send(buffer, offset, length, port, address, callback); };
ECDH.convertKey = function convertKey(key, curve, inEnc, outEnc, format) { if (typeof key !== 'string' && !isArrayBufferView(key)) { throw new ERR_INVALID_ARG_TYPE( 'key', ['string', 'Buffer', 'TypedArray', 'DataView'], key ); } validateString(curve, 'curve'); const encoding = getDefaultEncoding(); inEnc = inEnc || encoding; outEnc = outEnc || encoding; const f = getFormat(format); const convertedKey = _ECDHConvertKey(toBuf(key, inEnc), curve, f); return encode(convertedKey, outEnc); };
function query(name, /* options, */ callback) { var options; if (arguments.length > 2) { options = callback; callback = arguments[2]; } validateString(name, 'name'); if (typeof callback !== 'function') { throw new ERR_INVALID_CALLBACK(); } var req = new QueryReqWrap(); req.bindingName = bindingName; req.callback = callback; req.hostname = name; req.oncomplete = onresolve; req.ttl = !!(options && options.ttl); var err = this._handle[bindingName](req, name); if (err) throw dnsException(err, bindingName, name); return req; }
OutgoingMessage.prototype.hasHeader = function hasHeader(name) { validateString(name, 'name'); return this[outHeadersKey] !== null && !!this[outHeadersKey][name.toLowerCase()]; };