Revert "buffer: move SlowBuffer to EOL"

This reverts commit 647175ee0b8ca19d6f315216c879b1dc89ad2759.

PR-URL: https://github.com/nodejs/node/pull/58211
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
This commit is contained in:
Filip Skokan 2025-05-07 20:31:21 +01:00 committed by GitHub
parent 5f252a45bc
commit da69d13623
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
23 changed files with 118 additions and 61 deletions

View File

@ -1,5 +1,5 @@
'use strict'; 'use strict';
const { Buffer } = require('buffer'); const SlowBuffer = require('buffer').SlowBuffer;
const common = require('../common.js'); const common = require('../common.js');
const assert = require('assert'); const assert = require('assert');
@ -19,7 +19,7 @@ const methods = {
function main({ size, type, method, n }) { function main({ size, type, method, n }) {
const buffer = type === 'fast' ? const buffer = type === 'fast' ?
Buffer.alloc(size) : Buffer.alloc(size) :
Buffer.allocUnsafeSlow(size).fill(0); SlowBuffer(size).fill(0);
const fn = methods[method]; const fn = methods[method];

View File

@ -1,6 +1,5 @@
'use strict'; 'use strict';
const common = require('../common.js'); const common = require('../common.js');
const { Buffer } = require('buffer');
const types = [ const types = [
'IntBE', 'IntBE',
@ -19,7 +18,7 @@ const bench = common.createBenchmark(main, {
function main({ n, buf, type, byteLength }) { function main({ n, buf, type, byteLength }) {
const buff = buf === 'fast' ? const buff = buf === 'fast' ?
Buffer.alloc(8) : Buffer.alloc(8) :
Buffer.allocUnsafeSlow(8); require('buffer').SlowBuffer(8);
const fn = `read${type}`; const fn = `read${type}`;
buff.writeDoubleLE(0, 0); buff.writeDoubleLE(0, 0);

View File

@ -1,6 +1,5 @@
'use strict'; 'use strict';
const common = require('../common.js'); const common = require('../common.js');
const { Buffer } = require('buffer');
const types = [ const types = [
'BigUInt64LE', 'BigUInt64LE',
@ -28,7 +27,7 @@ const bench = common.createBenchmark(main, {
function main({ n, buf, type }) { function main({ n, buf, type }) {
const buff = buf === 'fast' ? const buff = buf === 'fast' ?
Buffer.alloc(8) : Buffer.alloc(8) :
Buffer.allocUnsafeSlow(8); require('buffer').SlowBuffer(8);
const fn = `read${type}`; const fn = `read${type}`;
buff.writeDoubleLE(0, 0); buff.writeDoubleLE(0, 0);

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const common = require('../common.js'); const common = require('../common.js');
const { Buffer } = require('buffer'); const SlowBuffer = require('buffer').SlowBuffer;
const bench = common.createBenchmark(main, { const bench = common.createBenchmark(main, {
type: ['fast', 'slow', 'subarray'], type: ['fast', 'slow', 'subarray'],
@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, {
}); });
const buf = Buffer.allocUnsafe(1024); const buf = Buffer.allocUnsafe(1024);
const slowBuf = Buffer.allocUnsafeSlow(1024); const slowBuf = new SlowBuffer(1024);
function main({ n, type }) { function main({ n, type }) {
const b = type === 'slow' ? slowBuf : buf; const b = type === 'slow' ? slowBuf : buf;

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const common = require('../common.js'); const common = require('../common.js');
const { Buffer } = require('buffer');
const types = [ const types = [
'BigUInt64LE', 'BigUInt64LE',
'BigUInt64BE', 'BigUInt64BE',
@ -73,7 +73,7 @@ const byteLength = {
function main({ n, buf, type }) { function main({ n, buf, type }) {
const buff = buf === 'fast' ? const buff = buf === 'fast' ?
Buffer.alloc(8) : Buffer.alloc(8) :
Buffer.allocUnsafeSlow(8); require('buffer').SlowBuffer(8);
const fn = `write${type}`; const fn = `write${type}`;
if (!/\d/.test(fn)) if (!/\d/.test(fn))

View File

@ -5340,6 +5340,28 @@ console.log(newBuf.toString('ascii'));
Because the Euro (`€`) sign is not representable in US-ASCII, it is replaced Because the Euro (`€`) sign is not representable in US-ASCII, it is replaced
with `?` in the transcoded `Buffer`. with `?` in the transcoded `Buffer`.
### Class: `SlowBuffer`
<!-- YAML
deprecated: v6.0.0
-->
> Stability: 0 - Deprecated: Use [`Buffer.allocUnsafeSlow()`][] instead.
See [`Buffer.allocUnsafeSlow()`][]. This was never a class in the sense that
the constructor always returned a `Buffer` instance, rather than a `SlowBuffer`
instance.
#### `new SlowBuffer(size)`
<!-- YAML
deprecated: v6.0.0
-->
* `size` {integer} The desired length of the new `SlowBuffer`.
See [`Buffer.allocUnsafeSlow()`][].
### Buffer constants ### Buffer constants
<!-- YAML <!-- YAML
@ -5472,11 +5494,11 @@ added: v5.10.0
Node.js can be started using the `--zero-fill-buffers` command-line option to Node.js can be started using the `--zero-fill-buffers` command-line option to
cause all newly-allocated `Buffer` instances to be zero-filled upon creation by cause all newly-allocated `Buffer` instances to be zero-filled upon creation by
default. Without the option, buffers created with [`Buffer.allocUnsafe()`][] and default. Without the option, buffers created with [`Buffer.allocUnsafe()`][],
[`Buffer.allocUnsafeSlow()`][] are not zero-filled. Use of this flag can have a [`Buffer.allocUnsafeSlow()`][], and `new SlowBuffer(size)` are not zero-filled.
measurable negative impact on performance. Use the `--zero-fill-buffers` option Use of this flag can have a measurable negative impact on performance. Use the
only when necessary to enforce that newly allocated `Buffer` instances cannot `--zero-fill-buffers` option only when necessary to enforce that newly allocated
contain old data that is potentially sensitive. `Buffer` instances cannot contain old data that is potentially sensitive.
```console ```console
$ node --zero-fill-buffers $ node --zero-fill-buffers

View File

@ -3120,7 +3120,8 @@ node --watch --watch-preserve-output test.js
added: v6.0.0 added: v6.0.0
--> -->
Automatically zero-fills all newly allocated [`Buffer`][] instances. Automatically zero-fills all newly allocated [`Buffer`][] and [`SlowBuffer`][]
instances.
## Environment variables ## Environment variables
@ -3895,6 +3896,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
[`ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX`]: errors.md#err_unsupported_typescript_syntax [`ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX`]: errors.md#err_unsupported_typescript_syntax
[`NODE_OPTIONS`]: #node_optionsoptions [`NODE_OPTIONS`]: #node_optionsoptions
[`NO_COLOR`]: https://no-color.org [`NO_COLOR`]: https://no-color.org
[`SlowBuffer`]: buffer.md#class-slowbuffer
[`Web Storage`]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API [`Web Storage`]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
[`WebSocket`]: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket [`WebSocket`]: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
[`YoungGenerationSizeFromSemiSpaceSize`]: https://chromium.googlesource.com/v8/v8.git/+/refs/tags/10.3.129/src/heap/heap.cc#328 [`YoungGenerationSizeFromSemiSpaceSize`]: https://chromium.googlesource.com/v8/v8.git/+/refs/tags/10.3.129/src/heap/heap.cc#328

View File

@ -695,9 +695,6 @@ Type: End-of-Life
<!-- YAML <!-- YAML
changes: changes:
- version: v24.0.0
pr-url: https://github.com/nodejs/node/pull/58008
description: End-of-Life.
- version: v24.0.0 - version: v24.0.0
pr-url: https://github.com/nodejs/node/pull/55175 pr-url: https://github.com/nodejs/node/pull/55175
description: Runtime deprecation. description: Runtime deprecation.
@ -709,9 +706,9 @@ changes:
description: Documentation-only deprecation. description: Documentation-only deprecation.
--> -->
Type: End-of-Life Type: Runtime
The `SlowBuffer` class has been removed. Please use The [`SlowBuffer`][] class is deprecated. Please use
[`Buffer.allocUnsafeSlow(size)`][] instead. [`Buffer.allocUnsafeSlow(size)`][] instead.
### DEP0031: `ecdh.setPublicKey()` ### DEP0031: `ecdh.setPublicKey()`
@ -3925,6 +3922,7 @@ upon `require('node:module').builtinModules`.
[`ReadStream.open()`]: fs.md#class-fsreadstream [`ReadStream.open()`]: fs.md#class-fsreadstream
[`Server.getConnections()`]: net.md#servergetconnectionscallback [`Server.getConnections()`]: net.md#servergetconnectionscallback
[`Server.listen({fd: <number>})`]: net.md#serverlistenhandle-backlog-callback [`Server.listen({fd: <number>})`]: net.md#serverlistenhandle-backlog-callback
[`SlowBuffer`]: buffer.md#class-slowbuffer
[`String.prototype.toWellFormed`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toWellFormed [`String.prototype.toWellFormed`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toWellFormed
[`WriteStream.open()`]: fs.md#class-fswritestream [`WriteStream.open()`]: fs.md#class-fswritestream
[`assert`]: assert.md [`assert`]: assert.md

View File

@ -572,7 +572,7 @@ the code inside the `main` function if it's more than just declaration.
```js ```js
'use strict'; 'use strict';
const common = require('../common.js'); const common = require('../common.js');
const { Buffer } = require('node:buffer'); const { SlowBuffer } = require('node:buffer');
const configs = { const configs = {
// Number of operations, specified here so they show up in the report. // Number of operations, specified here so they show up in the report.
@ -603,11 +603,10 @@ function main(conf) {
bench.start(); bench.start();
// Do operations here // Do operations here
const BufferConstructor = conf.type === 'fast' ? Buffer : SlowBuffer;
for (let i = 0; i < conf.n; i++) { for (let i = 0; i < conf.n; i++) {
conf.type === 'fast' ? new BufferConstructor(conf.size);
Buffer.allocUnsafe(conf.size) :
Buffer.allocUnsafeSlow(conf.size);
} }
// End the timer, pass in the number of operations // End the timer, pass in the number of operations

View File

@ -619,7 +619,7 @@ If set to 0 then V8 will choose an appropriate size of the thread pool based on
If the value provided is larger than V8's maximum, then the largest value will be chosen. If the value provided is larger than V8's maximum, then the largest value will be chosen.
. .
.It Fl -zero-fill-buffers .It Fl -zero-fill-buffers
Automatically zero-fills all newly allocated Buffer instances. Automatically zero-fills all newly allocated Buffer and SlowBuffer instances.
. .
.It Fl c , Fl -check .It Fl c , Fl -check
Check the script's syntax without executing it. Check the script's syntax without executing it.

View File

@ -52,6 +52,7 @@ const {
TypedArrayPrototypeSet, TypedArrayPrototypeSet,
TypedArrayPrototypeSlice, TypedArrayPrototypeSlice,
Uint8Array, Uint8Array,
Uint8ArrayPrototype,
} = primordials; } = primordials;
const { const {
@ -88,6 +89,7 @@ const {
kIsEncodingSymbol, kIsEncodingSymbol,
defineLazyProperties, defineLazyProperties,
encodingsMap, encodingsMap,
deprecate,
} = require('internal/util'); } = require('internal/util');
const { const {
isAnyArrayBuffer, isAnyArrayBuffer,
@ -409,15 +411,25 @@ Buffer.allocUnsafe = function allocUnsafe(size) {
}; };
/** /**
* By default creates a non-zero-filled Buffer instance that is not allocated * Equivalent to SlowBuffer(num), by default creates a non-zero-filled
* off the pre-initialized pool. If `--zero-fill-buffers` is set, will zero-fill * Buffer instance that is not allocated off the pre-initialized pool.
* the buffer. * If `--zero-fill-buffers` is set, will zero-fill the buffer.
*/ */
Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) { Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) {
validateNumber(size, 'size', 0, kMaxLength); validateNumber(size, 'size', 0, kMaxLength);
return createUnsafeBuffer(size); return createUnsafeBuffer(size);
}; };
// If --zero-fill-buffers command line argument is set, a zero-filled
// buffer is returned.
function SlowBuffer(size) {
validateNumber(size, 'size', 0, kMaxLength);
return createUnsafeBuffer(size);
}
ObjectSetPrototypeOf(SlowBuffer.prototype, Uint8ArrayPrototype);
ObjectSetPrototypeOf(SlowBuffer, Uint8Array);
function allocate(size) { function allocate(size) {
if (size <= 0) { if (size <= 0) {
return new FastBuffer(); return new FastBuffer();
@ -1319,6 +1331,10 @@ function isAscii(input) {
module.exports = { module.exports = {
Buffer, Buffer,
SlowBuffer: deprecate(
SlowBuffer,
'SlowBuffer() is deprecated. Please use Buffer.allocUnsafeSlow()',
'DEP0030'),
transcode, transcode,
isUtf8, isUtf8,
isAscii, isAscii,

View File

@ -1052,7 +1052,8 @@ PerProcessOptionsParser::PerProcessOptionsParser(
&PerProcessOptions::v8_thread_pool_size, &PerProcessOptions::v8_thread_pool_size,
kAllowedInEnvvar); kAllowedInEnvvar);
AddOption("--zero-fill-buffers", AddOption("--zero-fill-buffers",
"automatically zero-fill all newly allocated Buffer instances", "automatically zero-fill all newly allocated Buffer and "
"SlowBuffer instances",
&PerProcessOptions::zero_fill_all_buffers, &PerProcessOptions::zero_fill_all_buffers,
kAllowedInEnvvar); kAllowedInEnvvar);
AddOption("--debug-arraybuffer-allocations", AddOption("--debug-arraybuffer-allocations",

View File

@ -5,7 +5,7 @@ const assert = require('assert');
const vm = require('vm'); const vm = require('vm');
const { const {
Buffer, SlowBuffer,
kMaxLength, kMaxLength,
} = require('buffer'); } = require('buffer');
@ -1104,6 +1104,9 @@ assert.throws(() => Buffer.from(null), {
// Test prototype getters don't throw // Test prototype getters don't throw
assert.strictEqual(Buffer.prototype.parent, undefined); assert.strictEqual(Buffer.prototype.parent, undefined);
assert.strictEqual(Buffer.prototype.offset, undefined); assert.strictEqual(Buffer.prototype.offset, undefined);
assert.strictEqual(SlowBuffer.prototype.parent, undefined);
assert.strictEqual(SlowBuffer.prototype.offset, undefined);
{ {
// Test that large negative Buffer length inputs don't affect the pool offset. // Test that large negative Buffer length inputs don't affect the pool offset.
@ -1136,7 +1139,7 @@ assert.throws(() => {
a.copy(b, 0, 0x100000000, 0x100000001); a.copy(b, 0, 0x100000000, 0x100000001);
}, outOfRangeError); }, outOfRangeError);
// Unpooled buffer // Unpooled buffer (replaces SlowBuffer)
{ {
const ubuf = Buffer.allocUnsafeSlow(10); const ubuf = Buffer.allocUnsafeSlow(10);
assert(ubuf); assert(ubuf);

View File

@ -2,7 +2,7 @@
const common = require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const { Buffer } = require('buffer'); const SlowBuffer = require('buffer').SlowBuffer;
const vm = require('vm'); const vm = require('vm');
[ [
@ -24,6 +24,7 @@ const vm = require('vm');
}); });
assert(ArrayBuffer.isView(new Buffer(10))); assert(ArrayBuffer.isView(new Buffer(10)));
assert(ArrayBuffer.isView(new SlowBuffer(10)));
assert(ArrayBuffer.isView(Buffer.alloc(10))); assert(ArrayBuffer.isView(Buffer.alloc(10)));
assert(ArrayBuffer.isView(Buffer.allocUnsafe(10))); assert(ArrayBuffer.isView(Buffer.allocUnsafe(10)));
assert(ArrayBuffer.isView(Buffer.allocUnsafeSlow(10))); assert(ArrayBuffer.isView(Buffer.allocUnsafeSlow(10)));

View File

@ -2,7 +2,7 @@
require('../common'); require('../common');
const assert = require('assert'); const assert = require('assert');
const { Buffer } = require('buffer'); const SlowBuffer = require('buffer').SlowBuffer;
// Test failed or zero-sized Buffer allocations not affecting typed arrays. // Test failed or zero-sized Buffer allocations not affecting typed arrays.
// This test exists because of a regression that occurred. Because Buffer // This test exists because of a regression that occurred. Because Buffer
@ -15,6 +15,7 @@ const zeroArray = new Uint32Array(10).fill(0);
const sizes = [1e20, 0, 0.1, -1, 'a', undefined, null, NaN]; const sizes = [1e20, 0, 0.1, -1, 'a', undefined, null, NaN];
const allocators = [ const allocators = [
Buffer, Buffer,
SlowBuffer,
Buffer.alloc, Buffer.alloc,
Buffer.allocUnsafe, Buffer.allocUnsafe,
Buffer.allocUnsafeSlow, Buffer.allocUnsafeSlow,

View File

@ -30,7 +30,7 @@ buffer.INSPECT_MAX_BYTES = 2;
let b = Buffer.allocUnsafe(4); let b = Buffer.allocUnsafe(4);
b.fill('1234'); b.fill('1234');
let s = Buffer.allocUnsafeSlow(4); let s = buffer.SlowBuffer(4);
s.fill('1234'); s.fill('1234');
let expected = '<Buffer 31 32 ... 2 more bytes>'; let expected = '<Buffer 31 32 ... 2 more bytes>';
@ -41,7 +41,7 @@ assert.strictEqual(util.inspect(s), expected);
b = Buffer.allocUnsafe(2); b = Buffer.allocUnsafe(2);
b.fill('12'); b.fill('12');
s = Buffer.allocUnsafeSlow(2); s = buffer.SlowBuffer(2);
s.fill('12'); s.fill('12');
expected = '<Buffer 31 32>'; expected = '<Buffer 31 32>';

View File

@ -2,6 +2,7 @@
require('../common'); require('../common');
const assert = require('assert'); const assert = require('assert');
const { SlowBuffer } = require('buffer');
const msg = { const msg = {
code: 'ERR_OUT_OF_RANGE', code: 'ERR_OUT_OF_RANGE',
@ -29,3 +30,8 @@ assert.throws(() => Buffer.allocUnsafeSlow(-Buffer.poolSize), msg);
assert.throws(() => Buffer.allocUnsafeSlow(-100), msg); assert.throws(() => Buffer.allocUnsafeSlow(-100), msg);
assert.throws(() => Buffer.allocUnsafeSlow(-1), msg); assert.throws(() => Buffer.allocUnsafeSlow(-1), msg);
assert.throws(() => Buffer.allocUnsafeSlow(NaN), msg); assert.throws(() => Buffer.allocUnsafeSlow(NaN), msg);
assert.throws(() => SlowBuffer(-Buffer.poolSize), msg);
assert.throws(() => SlowBuffer(-100), msg);
assert.throws(() => SlowBuffer(-1), msg);
assert.throws(() => SlowBuffer(NaN), msg);

View File

@ -4,6 +4,7 @@ require('../common');
const assert = require('assert'); const assert = require('assert');
const buffer = require('buffer'); const buffer = require('buffer');
const SlowBuffer = buffer.SlowBuffer;
const kMaxLength = buffer.kMaxLength; const kMaxLength = buffer.kMaxLength;
const bufferMaxSizeMsg = { const bufferMaxSizeMsg = {
@ -12,6 +13,7 @@ const bufferMaxSizeMsg = {
}; };
assert.throws(() => Buffer(kMaxLength + 1), bufferMaxSizeMsg); assert.throws(() => Buffer(kMaxLength + 1), bufferMaxSizeMsg);
assert.throws(() => SlowBuffer(kMaxLength + 1), bufferMaxSizeMsg);
assert.throws(() => Buffer.alloc(kMaxLength + 1), bufferMaxSizeMsg); assert.throws(() => Buffer.alloc(kMaxLength + 1), bufferMaxSizeMsg);
assert.throws(() => Buffer.allocUnsafe(kMaxLength + 1), bufferMaxSizeMsg); assert.throws(() => Buffer.allocUnsafe(kMaxLength + 1), bufferMaxSizeMsg);
assert.throws(() => Buffer.allocUnsafeSlow(kMaxLength + 1), bufferMaxSizeMsg); assert.throws(() => Buffer.allocUnsafeSlow(kMaxLength + 1), bufferMaxSizeMsg);

View File

@ -1,13 +1,20 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const { Buffer, kMaxLength } = require('buffer'); const buffer = require('buffer');
const SlowBuffer = buffer.SlowBuffer;
const ones = [1, 1, 1, 1]; const ones = [1, 1, 1, 1];
common.expectWarning(
'DeprecationWarning',
'SlowBuffer() is deprecated. Please use Buffer.allocUnsafeSlow()',
'DEP0030'
);
// Should create a Buffer // Should create a Buffer
let sb = Buffer.allocUnsafeSlow(4); let sb = SlowBuffer(4);
assert(sb instanceof Buffer); assert(sb instanceof Buffer);
assert.strictEqual(sb.length, 4); assert.strictEqual(sb.length, 4);
sb.fill(1); sb.fill(1);
@ -19,7 +26,7 @@ for (const [key, value] of sb.entries()) {
assert.strictEqual(sb.buffer.byteLength, 4); assert.strictEqual(sb.buffer.byteLength, 4);
// Should work without new // Should work without new
sb = Buffer.allocUnsafeSlow(4); sb = SlowBuffer(4);
assert(sb instanceof Buffer); assert(sb instanceof Buffer);
assert.strictEqual(sb.length, 4); assert.strictEqual(sb.length, 4);
sb.fill(1); sb.fill(1);
@ -28,7 +35,7 @@ for (const [key, value] of sb.entries()) {
} }
// Should work with edge cases // Should work with edge cases
assert.strictEqual(Buffer.allocUnsafeSlow(0).length, 0); assert.strictEqual(SlowBuffer(0).length, 0);
// Should throw with invalid length type // Should throw with invalid length type
const bufferInvalidTypeMsg = { const bufferInvalidTypeMsg = {
@ -36,17 +43,17 @@ const bufferInvalidTypeMsg = {
name: 'TypeError', name: 'TypeError',
message: /^The "size" argument must be of type number/, message: /^The "size" argument must be of type number/,
}; };
assert.throws(() => Buffer.allocUnsafeSlow(), bufferInvalidTypeMsg); assert.throws(() => SlowBuffer(), bufferInvalidTypeMsg);
assert.throws(() => Buffer.allocUnsafeSlow({}), bufferInvalidTypeMsg); assert.throws(() => SlowBuffer({}), bufferInvalidTypeMsg);
assert.throws(() => Buffer.allocUnsafeSlow('6'), bufferInvalidTypeMsg); assert.throws(() => SlowBuffer('6'), bufferInvalidTypeMsg);
assert.throws(() => Buffer.allocUnsafeSlow(true), bufferInvalidTypeMsg); assert.throws(() => SlowBuffer(true), bufferInvalidTypeMsg);
// Should throw with invalid length value // Should throw with invalid length value
const bufferMaxSizeMsg = { const bufferMaxSizeMsg = {
code: 'ERR_OUT_OF_RANGE', code: 'ERR_OUT_OF_RANGE',
name: 'RangeError', name: 'RangeError',
}; };
assert.throws(() => Buffer.allocUnsafeSlow(NaN), bufferMaxSizeMsg); assert.throws(() => SlowBuffer(NaN), bufferMaxSizeMsg);
assert.throws(() => Buffer.allocUnsafeSlow(Infinity), bufferMaxSizeMsg); assert.throws(() => SlowBuffer(Infinity), bufferMaxSizeMsg);
assert.throws(() => Buffer.allocUnsafeSlow(-1), bufferMaxSizeMsg); assert.throws(() => SlowBuffer(-1), bufferMaxSizeMsg);
assert.throws(() => Buffer.allocUnsafeSlow(kMaxLength + 1), bufferMaxSizeMsg); assert.throws(() => SlowBuffer(buffer.kMaxLength + 1), bufferMaxSizeMsg);

View File

@ -12,7 +12,7 @@ if (!common.enoughTestMem) {
const assert = require('assert'); const assert = require('assert');
const { const {
Buffer, SlowBuffer,
constants: { constants: {
MAX_STRING_LENGTH, MAX_STRING_LENGTH,
}, },
@ -24,7 +24,7 @@ const message = {
name: 'Error', name: 'Error',
}; };
assert.throws(() => Buffer(len).toString('utf8'), message); assert.throws(() => Buffer(len).toString('utf8'), message);
assert.throws(() => Buffer.allocUnsafeSlow(len).toString('utf8'), message); assert.throws(() => SlowBuffer(len).toString('utf8'), message);
assert.throws(() => Buffer.alloc(len).toString('utf8'), message); assert.throws(() => Buffer.alloc(len).toString('utf8'), message);
assert.throws(() => Buffer.allocUnsafe(len).toString('utf8'), message); assert.throws(() => Buffer.allocUnsafe(len).toString('utf8'), message);
assert.throws(() => Buffer.allocUnsafeSlow(len).toString('utf8'), message); assert.throws(() => Buffer.allocUnsafeSlow(len).toString('utf8'), message);

View File

@ -1,11 +1,11 @@
'use strict'; 'use strict';
// Flags: --zero-fill-buffers // Flags: --zero-fill-buffers
// when using --zero-fill-buffers, every Buffer // when using --zero-fill-buffers, every Buffer and SlowBuffer
// instance must be zero filled upon creation // instance must be zero filled upon creation
require('../common'); require('../common');
const { Buffer } = require('buffer'); const SlowBuffer = require('buffer').SlowBuffer;
const assert = require('assert'); const assert = require('assert');
function isZeroFilled(buf) { function isZeroFilled(buf) {
@ -22,8 +22,9 @@ for (let i = 0; i < 50; i++) {
const bufs = [ const bufs = [
Buffer.alloc(20), Buffer.alloc(20),
Buffer.allocUnsafe(20), Buffer.allocUnsafe(20),
Buffer.allocUnsafeSlow(20), SlowBuffer(20),
Buffer(20), Buffer(20),
new SlowBuffer(20),
]; ];
for (const buf of bufs) { for (const buf of bufs) {
assert(isZeroFilled(buf)); assert(isZeroFilled(buf));

View File

@ -86,5 +86,5 @@ assert.deepStrictEqual(
// Test that it doesn't crash // Test that it doesn't crash
{ {
buffer.transcode(new buffer.Buffer.allocUnsafeSlow(1), 'utf16le', 'ucs2'); buffer.transcode(new buffer.SlowBuffer(1), 'utf16le', 'ucs2');
} }

View File

@ -5,18 +5,18 @@ const common = require('../common');
common.skipIf32Bits(); common.skipIf32Bits();
const assert = require('node:assert'); const assert = require('node:assert');
const { Buffer } = require('node:buffer'); const {
SlowBuffer,
} = require('node:buffer');
const size = 2 ** 31; const size = 2 ** 31;
// Test slow Buffer with size larger than integer range // Test SlowBuffer with size larger than integer range
try { try {
assert.throws(() => Buffer.allocUnsafeSlow(size).toString('utf8'), { assert.throws(() => SlowBuffer(size).toString('utf8'), { code: 'ERR_STRING_TOO_LONG' });
code: 'ERR_STRING_TOO_LONG',
});
} catch (e) { } catch (e) {
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') { if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
throw e; throw e;
} }
common.skip('insufficient space for slow Buffer allocation'); common.skip('insufficient space for SlowBuffer');
} }