Repository: denoland/deno
Pull request: fix(ext/node): node:buffer validates INSPECT_MAX_BYTES


Changed files:
filename: ext/node/polyfills/internal/buffer.mjs
patch: @@ -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 "<Buffer " + str + ">";
     };
@@ -2766,17 +2768,29 @@ export function transcode(source, fromEnco, toEnco) {
   }
 }
 
-export default {
+const mod = {
   atob,
   btoa,
   Blob,
   Buffer,
   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 };

filename: tests/node_compat/config.jsonc
patch: @@ -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",

filename: tests/node_compat/runner/TODO.md
patch: @@ -1,7 +1,7 @@
 <!-- deno-fmt-ignore-file -->
 # 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)

filename: tests/node_compat/test/parallel/test-buffer-set-inspect-max-bytes.js
patch: @@ -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',
+  }
+);