Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: make Buffer work with resizable ArrayBuffer #55377

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading