mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
buffer: make Buffer work with resizable ArrayBuffer
Fixes: https://github.com/nodejs/node/issues/52195 PR-URL: https://github.com/nodejs/node/pull/55377 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
0f375db9c6
commit
231d5e4437
@ -504,9 +504,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
|
|||||||
if (maxLength < 0)
|
if (maxLength < 0)
|
||||||
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
|
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
|
||||||
|
|
||||||
if (length === undefined) {
|
if (length !== undefined) {
|
||||||
length = maxLength;
|
|
||||||
} else {
|
|
||||||
// Convert length to non-negative integer.
|
// Convert length to non-negative integer.
|
||||||
length = +length;
|
length = +length;
|
||||||
if (length > 0) {
|
if (length > 0) {
|
||||||
|
29
test/parallel/test-buffer-resizable.js
Normal file
29
test/parallel/test-buffer-resizable.js
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// Flags: --no-warnings
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
require('../common');
|
||||||
|
const { Buffer } = require('node:buffer');
|
||||||
|
const { strictEqual } = require('node:assert');
|
||||||
|
const { describe, it } = require('node:test');
|
||||||
|
|
||||||
|
describe('Using resizable ArrayBuffer with Buffer...', () => {
|
||||||
|
it('works as expected', () => {
|
||||||
|
const ab = new ArrayBuffer(10, { maxByteLength: 20 });
|
||||||
|
const buffer = Buffer.from(ab, 1);
|
||||||
|
strictEqual(buffer.byteLength, 9);
|
||||||
|
ab.resize(15);
|
||||||
|
strictEqual(buffer.byteLength, 14);
|
||||||
|
ab.resize(5);
|
||||||
|
strictEqual(buffer.byteLength, 4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works with the deprecated constructor also', () => {
|
||||||
|
const ab = new ArrayBuffer(10, { maxByteLength: 20 });
|
||||||
|
const buffer = new Buffer(ab, 1);
|
||||||
|
strictEqual(buffer.byteLength, 9);
|
||||||
|
ab.resize(15);
|
||||||
|
strictEqual(buffer.byteLength, 14);
|
||||||
|
ab.resize(5);
|
||||||
|
strictEqual(buffer.byteLength, 4);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user