diff --git a/ext/node/polyfills/internal/buffer.mjs b/ext/node/polyfills/internal/buffer.mjs index 4a2bba7f4b..2e0e132c2e 100644 --- a/ext/node/polyfills/internal/buffer.mjs +++ b/ext/node/polyfills/internal/buffer.mjs @@ -119,7 +119,9 @@ const MAX_UINT32 = 2 ** 32; const customInspectSymbol = SymbolFor("nodejs.util.inspect.custom"); -export const INSPECT_MAX_BYTES = 50; +let INSPECT_MAX_BYTES_ = 50; + +export const INSPECT_MAX_BYTES = INSPECT_MAX_BYTES_; export const constants = { MAX_LENGTH: kMaxLength, @@ -611,17 +613,17 @@ Buffer.prototype[customInspectSymbol] = Buffer.prototype.inspect = function inspect() { let str = ""; - const max = INSPECT_MAX_BYTES; str = StringPrototypeTrim( StringPrototypeReplace( // deno-lint-ignore prefer-primordials - this.toString("hex", 0, max), + this.toString("hex", 0, INSPECT_MAX_BYTES_), SPACER_PATTERN, "$1 ", ), ); - if (this.length > max) { - str += " ... "; + if (this.length > INSPECT_MAX_BYTES_) { + const remaining = this.length - INSPECT_MAX_BYTES_; + str += ` ... ${remaining} more byte${remaining > 1 ? "s" : ""}`; } return ""; }; @@ -2766,7 +2768,7 @@ export function transcode(source, fromEnco, toEnco) { } } -export default { +const mod = { atob, btoa, Blob, @@ -2774,9 +2776,21 @@ export default { constants, isAscii, isUtf8, - INSPECT_MAX_BYTES, + get INSPECT_MAX_BYTES() { + return INSPECT_MAX_BYTES_; + }, + set INSPECT_MAX_BYTES(val) { + validateNumber(val, "INSPECT_MAX_BYTES", 0); + INSPECT_MAX_BYTES_ = val; + }, kMaxLength, kStringMaxLength, SlowBuffer, transcode, }; + +// NB(bartlomieju): we want to have a default exports from this module for ES imports, +// as well as make it work with `require` in such a way that getters/setters +// for `INSPECT_MAX_BYTES` work correctly - using `as "module.exports"` ensures +// that `require`ing this module does that. +export { mod as "module.exports", mod as default }; diff --git a/tests/node_compat/config.jsonc b/tests/node_compat/config.jsonc index f152510c8b..ee684ea1ff 100644 --- a/tests/node_compat/config.jsonc +++ b/tests/node_compat/config.jsonc @@ -298,6 +298,7 @@ "test-buffer-readint.js", "test-buffer-readuint.js", "test-buffer-safe-unsafe.js", + "test-buffer-set-inspect-max-bytes.js", "test-buffer-sharedarraybuffer.js", "test-buffer-slice.js", "test-buffer-slow.js", diff --git a/tests/node_compat/runner/TODO.md b/tests/node_compat/runner/TODO.md index 13d27deec8..9a77243e31 100644 --- a/tests/node_compat/runner/TODO.md +++ b/tests/node_compat/runner/TODO.md @@ -1,7 +1,7 @@ # Remaining Node Tests -1176 tests out of 3993 have been ported from Node 23.9.0 (29.45% ported, 71.07% remaining). +1177 tests out of 3993 have been ported from Node 23.9.0 (29.48% ported, 71.05% remaining). NOTE: This file should not be manually edited. Please edit `tests/node_compat/config.json` and run `deno task setup` in `tests/node_compat/runner` dir instead. @@ -292,7 +292,6 @@ NOTE: This file should not be manually edited. Please edit `tests/node_compat/co - [parallel/test-buffer-pool-untransferable.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-pool-untransferable.js) - [parallel/test-buffer-prototype-inspect.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-prototype-inspect.js) - [parallel/test-buffer-resizable.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-resizable.js) -- [parallel/test-buffer-set-inspect-max-bytes.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-set-inspect-max-bytes.js) - [parallel/test-buffer-write-fast.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-write-fast.js) - [parallel/test-c-ares.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-c-ares.js) - [parallel/test-child-process-advanced-serialization-largebuffer.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-child-process-advanced-serialization-largebuffer.js) diff --git a/tests/node_compat/test/parallel/test-buffer-set-inspect-max-bytes.js b/tests/node_compat/test/parallel/test-buffer-set-inspect-max-bytes.js new file mode 100644 index 0000000000..c49a3c2fbc --- /dev/null +++ b/tests/node_compat/test/parallel/test-buffer-set-inspect-max-bytes.js @@ -0,0 +1,41 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +// Copyright Joyent and Node contributors. All rights reserved. MIT license. +// Taken from Node 23.9.0 +// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually. + +'use strict'; + +require('../common'); +const assert = require('assert'); +const buffer = require('buffer'); + +const rangeErrorObjs = [NaN, -1]; +const typeErrorObj = 'and even this'; + +for (const obj of rangeErrorObjs) { + assert.throws( + () => buffer.INSPECT_MAX_BYTES = obj, + { + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + } + ); + + assert.throws( + () => buffer.INSPECT_MAX_BYTES = obj, + { + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + } + ); +} + +assert.throws( + () => buffer.INSPECT_MAX_BYTES = typeErrorObj, + { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + } +);