Version
v20.12.0
Platform
Linux ljysjk 6.1.43 #1 SMP PREEMPT_DYNAMIC Sun Aug 6 20:05:33 UTC 2023 x86_64 GNU/Linux
Subsystem
No response
What steps will reproduce the bug?
- Open the CodeSandbox (see code below)
- Observe the
ERR_INVALID_ARG_TYPE error below
CodeSandbox demo: https://codesandbox.io/p/devbox/filehandle-readablewebstream-with-new-response-ljysjk?file=%2Findex.js
importfsfrom"node:fs/promises";importhttpfrom"node:http";importpathfrom"node:path";import{Readable}from"node:stream";constfilePath="./image.jpg";constserver=http.createServer(async(nodeRequest,nodeResponse)=>{conststats=awaitfs.stat(filePath);constfileHandle=awaitfs.open(filePath);constwebStream=fileHandle.readableWebStream();nodeResponse.writeHead(200,{"Content-Disposition": `attachment; filename=${path.basename(filePath)}`,"Content-Type": "image/jpeg","Content-Length": String(stats.size),});constnodeStream=Readable.fromWeb(webStream);nodeStream.pipe(nodeResponse);});constport=3000;server.listen(port,()=>{console.log(`Server running at http://localhost:${port}/`);});Error:
Server running at http://localhost:3000/
node:events:496
throw er; // Unhandled 'error' event
^
TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of ArrayBuffer
at readableAddChunkPushByteMode (node:internal/streams/readable:480:28)
at Readable.push (node:internal/streams/readable:390:5)
at node:internal/webstreams/adapters:517:22
Emitted 'error' event on Readable instance at:
at emitErrorNT (node:internal/streams/destroy:169:8)
at emitErrorCloseNT (node:internal/streams/destroy:128:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: 'ERR_INVALID_ARG_TYPE'
}
Node.js v20.12.0
Failed running 'index.js'

Alternative version with new Response():
importfsfrom"node:fs/promises";importhttpfrom"node:http";importpathfrom"node:path";constfilePath="./image.jpg";constserver=http.createServer(async(nodeRequest,nodeResponse)=>{conststats=awaitfs.stat(filePath);constfileHandle=awaitfs.open(filePath);constwebStream=fileHandle.readableWebStream();// Version with new Response()constwebResponse=newResponse(webStream,{status: 200,headers: newHeaders({"content-disposition": `attachment; filename=${path.basename(filePath)}`,"content-type": "image/jpeg","content-length": String(stats.size),}),});nodeResponse.writeHead(webResponse.status,Object.fromEntries(webResponse.headers.entries()));webResponse.body.pipeTo(newWritableStream({write(chunk){nodeResponse.write(chunk);},close(){nodeResponse.end();},}));});constport=3000;server.listen(port,()=>{console.log(`Server running at http://localhost:${port}/`);});How often does it reproduce? Is there a required condition?
Always
What is the expected behavior? Why is that the expected behavior?
Web streams created by fileHandle.readableWebStream() should be compatible with Readable.fromWeb()
What do you see instead?
Web streams created by fileHandle.readableWebStream() are incompatible with Readable.fromWeb()
Additional information
Also reported over here:
cc @jasnell
Version
v20.12.0
Platform
Subsystem
No response
What steps will reproduce the bug?
ERR_INVALID_ARG_TYPEerror belowCodeSandbox demo: https://codesandbox.io/p/devbox/filehandle-readablewebstream-with-new-response-ljysjk?file=%2Findex.js
Error:
Alternative version with
new Response():How often does it reproduce? Is there a required condition?
Always
What is the expected behavior? Why is that the expected behavior?
Web streams created by
fileHandle.readableWebStream()should be compatible withReadable.fromWeb()What do you see instead?
Web streams created by
fileHandle.readableWebStream()are incompatible withReadable.fromWeb()Additional information
Also reported over here:
cc @jasnell