forked from oracle/graaljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: use Buffer.from when constructor is a Buffer
When using BYOB streams, it's possible for the constructor in readableByteStreamControllerConvertPullIntoDescriptor to be a node Buffer. If it is, use `Buffer.from` over `new ctor`. Fixes nodejs/node#49245 PR-URL: nodejs/node#49250 Reviewed-By: Debadree Chatterjee <[email protected]> Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
graal-nodejs/test/parallel/test-whatwg-readablebytestreambyob.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
const { | ||
open, | ||
} = require('fs/promises'); | ||
|
||
const { | ||
Buffer, | ||
} = require('buffer'); | ||
|
||
class Source { | ||
async start(controller) { | ||
this.file = await open(__filename); | ||
this.controller = controller; | ||
} | ||
|
||
async pull(controller) { | ||
const byobRequest = controller.byobRequest; | ||
const view = byobRequest.view; | ||
|
||
const { | ||
bytesRead, | ||
} = await this.file.read({ | ||
buffer: view, | ||
offset: view.byteOffset, | ||
length: view.byteLength | ||
}); | ||
|
||
if (bytesRead === 0) { | ||
await this.file.close(); | ||
this.controller.close(); | ||
} | ||
|
||
byobRequest.respond(bytesRead); | ||
} | ||
|
||
get type() { return 'bytes'; } | ||
|
||
get autoAllocateChunkSize() { return 1024; } | ||
} | ||
|
||
(async () => { | ||
const source = new Source(); | ||
const stream = new ReadableStream(source); | ||
|
||
const { emitWarning } = process; | ||
|
||
process.emitWarning = common.mustNotCall(); | ||
|
||
try { | ||
const reader = stream.getReader({ mode: 'byob' }); | ||
|
||
let result; | ||
do { | ||
result = await reader.read(Buffer.alloc(100)); | ||
} while (!result.done); | ||
} finally { | ||
process.emitWarning = emitWarning; | ||
} | ||
})().then(common.mustCall()); |