Skip to content

Commit

Permalink
buffer: make Buffer work with resizable ArrayBuffer
Browse files Browse the repository at this point in the history
Fixes: #52195
PR-URL: #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>
  • Loading branch information
jasnell authored and aduh95 committed Oct 19, 2024
1 parent 3b2b3a8 commit c05582d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
if (maxLength < 0)
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');

if (length === undefined) {
length = maxLength;
} else {
if (length !== undefined) {
// Convert length to non-negative integer.
length = +length;
if (length > 0) {
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-buffer-resizable.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit c05582d

Please sign in to comment.