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 <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
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
thrownewERR_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+
constab=newArrayBuffer(10,{maxByteLength: 20});
12+
constbuffer=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+
constab=newArrayBuffer(10,{maxByteLength: 20});
22+
constbuffer=newBuffer(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)