GeezerVison is showing github.com — lib: extract validateString validator · nodejs/node@193a048
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/_http_outgoing.js +4-10Lines changed: 4 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,6 +45,7 @@ const { | 45 | 45 | ERR_STREAM_CANNOT_PIPE, | 46 | 46 | ERR_STREAM_WRITE_AFTER_END | 47 | 47 | } = require('internal/errors').codes; | | 48 | +const { validateString } = require('internal/validators'); | 48 | 49 |
| 49 | 50 | const { CRLF, debug } = common; | 50 | 51 |
| @@ -480,9 +481,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) { | 480 | 481 |
| 481 | 482 |
| 482 | 483 | OutgoingMessage.prototype.getHeader = function getHeader(name) { | 483 | | - if (typeof name !== 'string') { | 484 | | - throw new ERR_INVALID_ARG_TYPE('name', 'string', name); | 485 | | - } | | 484 | + validateString(name, 'name'); | 486 | 485 |
| 487 | 486 | const headers = this[outHeadersKey]; | 488 | 487 | if (headers === null) | @@ -516,19 +515,14 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() { | 516 | 515 |
| 517 | 516 |
| 518 | 517 | OutgoingMessage.prototype.hasHeader = function hasHeader(name) { | 519 | | - if (typeof name !== 'string') { | 520 | | - throw new ERR_INVALID_ARG_TYPE('name', 'string', name); | 521 | | - } | 522 | | - | | 518 | + validateString(name, 'name'); | 523 | 519 | return this[outHeadersKey] !== null && | 524 | 520 | !!this[outHeadersKey][name.toLowerCase()]; | 525 | 521 | }; | 526 | 522 |
| 527 | 523 |
| 528 | 524 | OutgoingMessage.prototype.removeHeader = function removeHeader(name) { | 529 | | - if (typeof name !== 'string') { | 530 | | - throw new ERR_INVALID_ARG_TYPE('name', 'string', name); | 531 | | - } | | 525 | + validateString(name, 'name'); | 532 | 526 |
| 533 | 527 | if (this._header) { | 534 | 528 | throw new ERR_HTTP_HEADERS_SENT('remove'); | |
Collapse file Copy file name to clipboardExpand all lines: lib/_tls_wrap.js +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -50,6 +50,7 @@ const { | 50 | 50 | ERR_TLS_SESSION_ATTACK, | 51 | 51 | ERR_TLS_SNI_FROM_SERVER | 52 | 52 | } = require('internal/errors').codes; | | 53 | +const { validateString } = require('internal/validators'); | 53 | 54 | const kConnectOptions = Symbol('connect-options'); | 54 | 55 | const kDisableRenegotiation = Symbol('disable-renegotiation'); | 55 | 56 | const kErrorEmitted = Symbol('error-emitted'); | @@ -645,9 +646,7 @@ TLSSocket.prototype._start = function() { | 645 | 646 | }; | 646 | 647 |
| 647 | 648 | TLSSocket.prototype.setServername = function(name) { | 648 | | - if (typeof name !== 'string') { | 649 | | - throw new ERR_INVALID_ARG_TYPE('name', 'string', name); | 650 | | - } | | 649 | + validateString(name, 'name'); | 651 | 650 |
| 652 | 651 | if (this._tlsOptions.isServer) { | 653 | 652 | throw new ERR_TLS_SNI_FROM_SERVER(); | |
Collapse file Copy file name to clipboardExpand all lines: lib/async_hooks.js +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { | 4 | 4 | ERR_ASYNC_CALLBACK, | 5 | | - ERR_INVALID_ARG_TYPE, | 6 | 5 | ERR_INVALID_ASYNC_ID | 7 | 6 | } = require('internal/errors').codes; | | 7 | +const { validateString } = require('internal/validators'); | 8 | 8 | const internal_async_hooks = require('internal/async_hooks'); | 9 | 9 |
| 10 | 10 | // Get functions | @@ -140,8 +140,7 @@ function showEmitBeforeAfterWarning() { | 140 | 140 |
| 141 | 141 | class AsyncResource { | 142 | 142 | constructor(type, opts = {}) { | 143 | | - if (typeof type !== 'string') | 144 | | - throw new ERR_INVALID_ARG_TYPE('type', 'string', type); | | 143 | + validateString(type, 'type'); | 145 | 144 |
| 146 | 145 | if (typeof opts === 'number') { | 147 | 146 | opts = { triggerAsyncId: opts, requireManualDestroy: false }; | |
Collapse file Copy file name to clipboardExpand all lines: lib/buffer.js +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -69,6 +69,7 @@ const { | 69 | 69 | ERR_NO_LONGER_SUPPORTED, | 70 | 70 | ERR_UNKNOWN_ENCODING | 71 | 71 | } = require('internal/errors').codes; | | 72 | +const { validateString } = require('internal/validators'); | 72 | 73 |
| 73 | 74 | const internalBuffer = require('internal/buffer'); | 74 | 75 |
| @@ -841,9 +842,7 @@ function _fill(buf, val, start, end, encoding) { | 841 | 842 |
| 842 | 843 | const normalizedEncoding = normalizeEncoding(encoding); | 843 | 844 | if (normalizedEncoding === undefined) { | 844 | | - if (typeof encoding !== 'string') { | 845 | | - throw new ERR_INVALID_ARG_TYPE('encoding', 'string', encoding); | 846 | | - } | | 845 | + validateString(encoding, 'encoding'); | 847 | 846 | throw new ERR_UNKNOWN_ENCODING(encoding); | 848 | 847 | } | 849 | 848 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/child_process.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,6 +37,7 @@ const { | 37 | 37 | ERR_INVALID_OPT_VALUE, | 38 | 38 | ERR_OUT_OF_RANGE | 39 | 39 | } = require('internal/errors').codes; | | 40 | +const { validateString } = require('internal/validators'); | 40 | 41 | const child_process = require('internal/child_process'); | 41 | 42 | const { | 42 | 43 | _validateStdio, | @@ -390,8 +391,7 @@ function _convertCustomFds(options) { | 390 | 391 | } | 391 | 392 |
| 392 | 393 | function normalizeSpawnArguments(file, args, options) { | 393 | | - if (typeof file !== 'string') | 394 | | - throw new ERR_INVALID_ARG_TYPE('file', 'string', file); | | 394 | + validateString(file, 'file'); | 395 | 395 |
| 396 | 396 | if (file.length === 0) | 397 | 397 | throw new ERR_INVALID_ARG_VALUE('file', file, 'cannot be empty'); | |
Collapse file Copy file name to clipboardExpand all lines: lib/dgram.js +3-8Lines changed: 3 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,6 +37,7 @@ const { | 37 | 37 | ERR_SOCKET_CANNOT_SEND, | 38 | 38 | ERR_SOCKET_DGRAM_NOT_RUNNING | 39 | 39 | } = errors.codes; | | 40 | +const { validateString } = require('internal/validators'); | 40 | 41 | const { Buffer } = require('buffer'); | 41 | 42 | const util = require('util'); | 42 | 43 | const { isUint8Array } = require('internal/util/types'); | @@ -269,9 +270,7 @@ Socket.prototype.sendto = function(buffer, | 269 | 270 | throw new ERR_INVALID_ARG_TYPE('port', 'number', port); | 270 | 271 | } | 271 | 272 |
| 272 | | - if (typeof address !== 'string') { | 273 | | - throw new ERR_INVALID_ARG_TYPE('address', 'string', address); | 274 | | - } | | 273 | + validateString(address, 'address'); | 275 | 274 |
| 276 | 275 | this.send(buffer, offset, length, port, address, callback); | 277 | 276 | }; | @@ -570,11 +569,7 @@ Socket.prototype.setMulticastLoopback = function(arg) { | 570 | 569 |
| 571 | 570 | Socket.prototype.setMulticastInterface = function(interfaceAddress) { | 572 | 571 | healthCheck(this); | 573 | | - | 574 | | - if (typeof interfaceAddress !== 'string') { | 575 | | - throw new ERR_INVALID_ARG_TYPE( | 576 | | - 'interfaceAddress', 'string', interfaceAddress); | 577 | | - } | | 572 | + validateString(interfaceAddress, 'interfaceAddress'); | 578 | 573 |
| 579 | 574 | const err = this[kStateSymbol].handle.setMulticastInterface(interfaceAddress); | 580 | 575 | if (err) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/dns.js +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,6 +39,7 @@ const { | 39 | 39 | ERR_MISSING_ARGS, | 40 | 40 | ERR_SOCKET_BAD_PORT | 41 | 41 | } = errors.codes; | | 42 | +const { validateString } = require('internal/validators'); | 42 | 43 |
| 43 | 44 | const { | 44 | 45 | GetAddrInfoReqWrap, | @@ -206,9 +207,8 @@ function resolver(bindingName) { | 206 | 207 | callback = arguments[2]; | 207 | 208 | } | 208 | 209 |
| 209 | | - if (typeof name !== 'string') { | 210 | | - throw new ERR_INVALID_ARG_TYPE('name', 'string', name); | 211 | | - } else if (typeof callback !== 'function') { | | 210 | + validateString(name, 'name'); | | 211 | + if (typeof callback !== 'function') { | 212 | 212 | throw new ERR_INVALID_CALLBACK(); | 213 | 213 | } | 214 | 214 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/inspector.js +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | ERR_INVALID_ARG_TYPE, | 10 | 10 | ERR_INVALID_CALLBACK | 11 | 11 | } = require('internal/errors').codes; | | 12 | +const { validateString } = require('internal/validators'); | 12 | 13 | const util = require('util'); | 13 | 14 | const { Connection, open, url } = process.binding('inspector'); | 14 | 15 | const { originalConsole } = require('internal/process/per_thread'); | @@ -58,9 +59,7 @@ class Session extends EventEmitter { | 58 | 59 | } | 59 | 60 |
| 60 | 61 | post(method, params, callback) { | 61 | | - if (typeof method !== 'string') { | 62 | | - throw new ERR_INVALID_ARG_TYPE('method', 'string', method); | 63 | | - } | | 62 | + validateString(method, 'method'); | 64 | 63 | if (!callback && util.isFunction(params)) { | 65 | 64 | callback = params; | 66 | 65 | params = null; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/child_process.js +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,6 +14,7 @@ const { | 14 | 14 | ERR_MISSING_ARGS | 15 | 15 | } | 16 | 16 | } = require('internal/errors'); | | 17 | +const { validateString } = require('internal/validators'); | 17 | 18 | const EventEmitter = require('events'); | 18 | 19 | const net = require('net'); | 19 | 20 | const dgram = require('dgram'); | @@ -317,9 +318,7 @@ ChildProcess.prototype.spawn = function(options) { | 317 | 318 | options.envPairs.push('NODE_CHANNEL_FD=' + ipcFd); | 318 | 319 | } | 319 | 320 |
| 320 | | - if (typeof options.file !== 'string') { | 321 | | - throw new ERR_INVALID_ARG_TYPE('options.file', 'string', options.file); | 322 | | - } | | 321 | + validateString(options.file, 'options.file'); | 323 | 322 | this.spawnfile = options.file; | 324 | 323 |
| 325 | 324 | if (Array.isArray(options.args)) | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/cipher.js +3-6Lines changed: 3 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,6 +10,7 @@ const { | 10 | 10 | ERR_INVALID_ARG_TYPE, | 11 | 11 | ERR_INVALID_OPT_VALUE | 12 | 12 | } = require('internal/errors').codes; | | 13 | +const { validateString } = require('internal/validators'); | 13 | 14 |
| 14 | 15 | const { | 15 | 16 | getDefaultEncoding, | @@ -83,9 +84,7 @@ function createCipherBase(cipher, credential, options, decipher, iv) { | 83 | 84 | } | 84 | 85 |
| 85 | 86 | function createCipher(cipher, password, options, decipher) { | 86 | | - if (typeof cipher !== 'string') | 87 | | - throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher); | 88 | | - | | 87 | + validateString(cipher, 'cipher'); | 89 | 88 | password = toBuf(password); | 90 | 89 | if (!isArrayBufferView(password)) { | 91 | 90 | throw new ERR_INVALID_ARG_TYPE( | @@ -99,9 +98,7 @@ function createCipher(cipher, password, options, decipher) { | 99 | 98 | } | 100 | 99 |
| 101 | 100 | function createCipherWithIV(cipher, key, options, decipher, iv) { | 102 | | - if (typeof cipher !== 'string') | 103 | | - throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher); | 104 | | - | | 101 | + validateString(cipher, 'cipher'); | 105 | 102 | key = toBuf(key); | 106 | 103 | if (!isArrayBufferView(key)) { | 107 | 104 | throw new ERR_INVALID_ARG_TYPE( | |
|