diff options
Diffstat (limited to 'ext/node/polyfills/internal')
-rw-r--r-- | ext/node/polyfills/internal/buffer.mjs | 31 | ||||
-rw-r--r-- | ext/node/polyfills/internal/cli_table.ts | 5 | ||||
-rw-r--r-- | ext/node/polyfills/internal/fs/utils.mjs | 31 | ||||
-rw-r--r-- | ext/node/polyfills/internal/validators.mjs | 15 |
4 files changed, 46 insertions, 36 deletions
diff --git a/ext/node/polyfills/internal/buffer.mjs b/ext/node/polyfills/internal/buffer.mjs index 0521c56aa..a051965a3 100644 --- a/ext/node/polyfills/internal/buffer.mjs +++ b/ext/node/polyfills/internal/buffer.mjs @@ -32,7 +32,7 @@ import { import { normalizeEncoding } from "ext:deno_node/internal/util.mjs"; import { validateBuffer } from "ext:deno_node/internal/validators.mjs"; import { isUint8Array } from "ext:deno_node/internal/util/types.ts"; -import { ERR_INVALID_STATE } from "ext:deno_node/internal/errors.ts"; +import { ERR_INVALID_STATE, NodeError } from "ext:deno_node/internal/errors.ts"; import { forgivingBase64Encode, forgivingBase64UrlEncode, @@ -167,10 +167,7 @@ Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype); Object.setPrototypeOf(Buffer, Uint8Array); function assertSize(size) { - validateNumber(size, "size"); - if (!(size >= 0 && size <= kMaxLength)) { - throw new codes.ERR_INVALID_ARG_VALUE.RangeError("size", size); - } + validateNumber(size, "size", 0, kMaxLength); } function _alloc(size, fill, encoding) { @@ -852,7 +849,14 @@ function _base64Slice(buf, start, end) { const decoder = new TextDecoder(); function _utf8Slice(buf, start, end) { - return decoder.decode(buf.slice(start, end)); + try { + return decoder.decode(buf.slice(start, end)); + } catch (err) { + if (err instanceof TypeError) { + throw new NodeError("ERR_STRING_TOO_LONG", "String too long"); + } + throw err; + } } function _latin1Slice(buf, start, end) { @@ -2297,10 +2301,23 @@ export function boundsError(value, length, type) { ); } -export function validateNumber(value, name) { +export function validateNumber(value, name, min = undefined, max) { if (typeof value !== "number") { throw new codes.ERR_INVALID_ARG_TYPE(name, "number", value); } + + if ( + (min != null && value < min) || (max != null && value > max) || + ((min != null || max != null) && Number.isNaN(value)) + ) { + throw new codes.ERR_OUT_OF_RANGE( + name, + `${min != null ? `>= ${min}` : ""}${ + min != null && max != null ? " && " : "" + }${max != null ? `<= ${max}` : ""}`, + value, + ); + } } function checkInt(value, min, max, buf, offset, byteLength) { diff --git a/ext/node/polyfills/internal/cli_table.ts b/ext/node/polyfills/internal/cli_table.ts index 574081ba4..9826e524f 100644 --- a/ext/node/polyfills/internal/cli_table.ts +++ b/ext/node/polyfills/internal/cli_table.ts @@ -27,11 +27,10 @@ const renderRow = (row: string[], columnWidths: number[]) => { for (let i = 0; i < row.length; i++) { const cell = row[i]; const len = getStringWidth(cell); - const needed = (columnWidths[i] - len) / 2; + const needed = columnWidths[i] - len; // round(needed) + ceil(needed) will always add up to the amount // of spaces we need while also left justifying the output. - out += " ".repeat(needed) + cell + - " ".repeat(Math.ceil(needed)); + out += cell + " ".repeat(Math.ceil(needed)); if (i !== row.length - 1) { out += tableChars.middle; } diff --git a/ext/node/polyfills/internal/fs/utils.mjs b/ext/node/polyfills/internal/fs/utils.mjs index a1823bb32..ec379ed99 100644 --- a/ext/node/polyfills/internal/fs/utils.mjs +++ b/ext/node/polyfills/internal/fs/utils.mjs @@ -23,7 +23,6 @@ import { isUint8Array, } from "ext:deno_node/internal/util/types.ts"; import { once } from "ext:deno_node/internal/util.mjs"; -import { deprecate } from "node:util"; import { toPathIfFileURL } from "ext:deno_node/internal/url.ts"; import { validateAbortSignal, @@ -959,24 +958,13 @@ export const getValidMode = hideStackFrames((mode, type) => { export const validateStringAfterArrayBufferView = hideStackFrames( (buffer, name) => { - if (typeof buffer === "string") { - return; - } - - if ( - typeof buffer === "object" && - buffer !== null && - typeof buffer.toString === "function" && - Object.prototype.hasOwnProperty.call(buffer, "toString") - ) { - return; + if (typeof buffer !== "string") { + throw new ERR_INVALID_ARG_TYPE( + name, + ["string", "Buffer", "TypedArray", "DataView"], + buffer, + ); } - - throw new ERR_INVALID_ARG_TYPE( - name, - ["string", "Buffer", "TypedArray", "DataView"], - buffer, - ); }, ); @@ -1005,12 +993,6 @@ export const constants = { kWriteFileMaxChunkSize, }; -export const showStringCoercionDeprecation = deprecate( - () => {}, - "Implicit coercion of objects with own toString property is deprecated.", - "DEP0162", -); - export default { constants, assertEncoding, @@ -1030,7 +1012,6 @@ export default { preprocessSymlinkDestination, realpathCacheKey, getStatsFromBinding, - showStringCoercionDeprecation, stringToFlags, stringToSymlinkType, Stats, diff --git a/ext/node/polyfills/internal/validators.mjs b/ext/node/polyfills/internal/validators.mjs index d4cd95546..58b1a97d7 100644 --- a/ext/node/polyfills/internal/validators.mjs +++ b/ext/node/polyfills/internal/validators.mjs @@ -171,10 +171,23 @@ function validateString(value, name) { * @param {unknown} value * @param {string} name */ -function validateNumber(value, name) { +function validateNumber(value, name, min = undefined, max) { if (typeof value !== "number") { throw new codes.ERR_INVALID_ARG_TYPE(name, "number", value); } + + if ( + (min != null && value < min) || (max != null && value > max) || + ((min != null || max != null) && Number.isNaN(value)) + ) { + throw new codes.ERR_OUT_OF_RANGE( + name, + `${min != null ? `>= ${min}` : ""}${ + min != null && max != null ? " && " : "" + }${max != null ? `<= ${max}` : ""}`, + value, + ); + } } /** |