Skip to content

Commit c05582d

Browse files
jasnelladuh95
authored andcommitted
buffer: make Buffer work with resizable ArrayBuffer
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>
1 parent 3b2b3a8 commit c05582d

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/buffer.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
504504
if (maxLength < 0)
505505
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
506506

507-
if (length === undefined) {
508-
length = maxLength;
509-
} else {
507+
if (length !== undefined) {
510508
// Convert length to non-negative integer.
511509
length = +length;
512510
if (length > 0) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Flags: --no-warnings
2+
'use strict';
3+
4+
require('../common');
5+
const { Buffer } = require('node:buffer');
6+
const { strictEqual } = require('node:assert');
7+
const { describe, it } = require('node:test');
8+
9+
describe('Using resizable ArrayBuffer with Buffer...', () => {
10+
it('works as expected', () => {
11+
const ab = new ArrayBuffer(10, { maxByteLength: 20 });
12+
const buffer = Buffer.from(ab, 1);
13+
strictEqual(buffer.byteLength, 9);
14+
ab.resize(15);
15+
strictEqual(buffer.byteLength, 14);
16+
ab.resize(5);
17+
strictEqual(buffer.byteLength, 4);
18+
});
19+
20+
it('works with the deprecated constructor also', () => {
21+
const ab = new ArrayBuffer(10, { maxByteLength: 20 });
22+
const buffer = new Buffer(ab, 1);
23+
strictEqual(buffer.byteLength, 9);
24+
ab.resize(15);
25+
strictEqual(buffer.byteLength, 14);
26+
ab.resize(5);
27+
strictEqual(buffer.byteLength, 4);
28+
});
29+
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy