Skip to content

Commit

Permalink
buffer: make Buffer work with resizable ArrayBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Oct 13, 2024
1 parent ac49b20 commit a732b8d
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 a732b8d

Please sign in to comment.