Skip to content

Commit 1b6736d

Browse files
fix(ext/node): node:buffer validates INSPECT_MAX_BYTES (#29469)
Co-authored-by: David Sherret <[email protected]>
1 parent e3bf5ee commit 1b6736d

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

ext/node/polyfills/internal/buffer.mjs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ const MAX_UINT32 = 2 ** 32;
119119

120120
const customInspectSymbol = SymbolFor("nodejs.util.inspect.custom");
121121

122-
export const INSPECT_MAX_BYTES = 50;
122+
let INSPECT_MAX_BYTES_ = 50;
123+
124+
export const INSPECT_MAX_BYTES = INSPECT_MAX_BYTES_;
123125

124126
export const constants = {
125127
MAX_LENGTH: kMaxLength,
@@ -611,17 +613,17 @@ Buffer.prototype[customInspectSymbol] =
611613
Buffer.prototype.inspect =
612614
function inspect() {
613615
let str = "";
614-
const max = INSPECT_MAX_BYTES;
615616
str = StringPrototypeTrim(
616617
StringPrototypeReplace(
617618
// deno-lint-ignore prefer-primordials
618-
this.toString("hex", 0, max),
619+
this.toString("hex", 0, INSPECT_MAX_BYTES_),
619620
SPACER_PATTERN,
620621
"$1 ",
621622
),
622623
);
623-
if (this.length > max) {
624-
str += " ... ";
624+
if (this.length > INSPECT_MAX_BYTES_) {
625+
const remaining = this.length - INSPECT_MAX_BYTES_;
626+
str += ` ... ${remaining} more byte${remaining > 1 ? "s" : ""}`;
625627
}
626628
return "<Buffer " + str + ">";
627629
};
@@ -2766,17 +2768,29 @@ export function transcode(source, fromEnco, toEnco) {
27662768
}
27672769
}
27682770

2769-
export default {
2771+
const mod = {
27702772
atob,
27712773
btoa,
27722774
Blob,
27732775
Buffer,
27742776
constants,
27752777
isAscii,
27762778
isUtf8,
2777-
INSPECT_MAX_BYTES,
2779+
get INSPECT_MAX_BYTES() {
2780+
return INSPECT_MAX_BYTES_;
2781+
},
2782+
set INSPECT_MAX_BYTES(val) {
2783+
validateNumber(val, "INSPECT_MAX_BYTES", 0);
2784+
INSPECT_MAX_BYTES_ = val;
2785+
},
27782786
kMaxLength,
27792787
kStringMaxLength,
27802788
SlowBuffer,
27812789
transcode,
27822790
};
2791+
2792+
// NB(bartlomieju): we want to have a default exports from this module for ES imports,
2793+
// as well as make it work with `require` in such a way that getters/setters
2794+
// for `INSPECT_MAX_BYTES` work correctly - using `as "module.exports"` ensures
2795+
// that `require`ing this module does that.
2796+
export { mod as "module.exports", mod as default };

tests/node_compat/config.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@
298298
"test-buffer-readint.js",
299299
"test-buffer-readuint.js",
300300
"test-buffer-safe-unsafe.js",
301+
"test-buffer-set-inspect-max-bytes.js",
301302
"test-buffer-sharedarraybuffer.js",
302303
"test-buffer-slice.js",
303304
"test-buffer-slow.js",

tests/node_compat/runner/TODO.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!-- deno-fmt-ignore-file -->
22
# Remaining Node Tests
33

4-
1176 tests out of 3993 have been ported from Node 23.9.0 (29.45% ported, 71.07% remaining).
4+
1177 tests out of 3993 have been ported from Node 23.9.0 (29.48% ported, 71.05% remaining).
55

66
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.
77

@@ -292,7 +292,6 @@ NOTE: This file should not be manually edited. Please edit `tests/node_compat/co
292292
- [parallel/test-buffer-pool-untransferable.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-pool-untransferable.js)
293293
- [parallel/test-buffer-prototype-inspect.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-prototype-inspect.js)
294294
- [parallel/test-buffer-resizable.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-resizable.js)
295-
- [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)
296295
- [parallel/test-buffer-write-fast.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-write-fast.js)
297296
- [parallel/test-c-ares.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-c-ares.js)
298297
- [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)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// deno-fmt-ignore-file
2+
// deno-lint-ignore-file
3+
4+
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
5+
// Taken from Node 23.9.0
6+
// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
7+
8+
'use strict';
9+
10+
require('../common');
11+
const assert = require('assert');
12+
const buffer = require('buffer');
13+
14+
const rangeErrorObjs = [NaN, -1];
15+
const typeErrorObj = 'and even this';
16+
17+
for (const obj of rangeErrorObjs) {
18+
assert.throws(
19+
() => buffer.INSPECT_MAX_BYTES = obj,
20+
{
21+
code: 'ERR_OUT_OF_RANGE',
22+
name: 'RangeError',
23+
}
24+
);
25+
26+
assert.throws(
27+
() => buffer.INSPECT_MAX_BYTES = obj,
28+
{
29+
code: 'ERR_OUT_OF_RANGE',
30+
name: 'RangeError',
31+
}
32+
);
33+
}
34+
35+
assert.throws(
36+
() => buffer.INSPECT_MAX_BYTES = typeErrorObj,
37+
{
38+
code: 'ERR_INVALID_ARG_TYPE',
39+
name: 'TypeError',
40+
}
41+
);

0 commit comments

Comments
 (0)