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

new Response() does not accept Node.js stream #21063

Closed
jcbhmr opened this issue Nov 2, 2023 · 4 comments
Closed

new Response() does not accept Node.js stream #21063

jcbhmr opened this issue Nov 2, 2023 · 4 comments

Comments

@jcbhmr
Copy link
Contributor

jcbhmr commented Nov 2, 2023

The Response constructor is a very easy way to convert a stream to text via .text() or JSON via .json(). The following behaviour seems to differ in Node.js and Deno:

console.log(await new Response(process.stdin).text())

Node.js:

echo hi | node -e 'new Response(process.stdin).text().then(console.log)'
hi

Deno:

echo hi | deno eval 'import process from "node:process";new Response(process.stdin).text().then(console.log)'
[object Object]
@jcbhmr
Copy link
Contributor Author

jcbhmr commented Nov 2, 2023

I can also confirm that this also happens when inside the Node.js compat layer. So if an npm:mypackage uses this trick to read from stdin, it will fail.

jcbhmr@PIG-2016:~/Documents/deno-response-test$ tree -a
.
├── deno.json
├── deno.lock
├── main.ts
├── main_test.ts
├── node_modules
│   ├── .deno
│   │   ├── .deno.lock
│   │   ├── .deno.lock.poll
│   │   ├── .setup-cache.bin
│   │   ├── nocode@1.0.0
│   │   │   ├── .initialized
│   │   │   └── node_modules
│   │   │       └── nocode
│   │   │           ├── nocode.js
│   │   │           └── package.json
│   │   └── node_modules
│   ├── .package-lock.json
│   └── nocode -> /home/jcbhmr/Documents/deno-response-test/node_modules/.deno/nocode@1.0.0/node_modules/nocode
├── package-lock.json
└── package.json

7 directories, 13 files

manually edited the nocode.js file outside of deno's knowledge like this:

jcbhmr@PIG-2016:~/Documents/deno-response-test$ cat node_modules/.deno/nocode@1.0.0/node_modules/nocode/nocode.js
new Response(process.stdin).text().then(console.log)
jcbhmr@PIG-2016:~/Documents/deno-response-test$ echo hi | deno run main.ts
[object Object]

@lucacasonato
Copy link
Member

Node is spec incompatible here and supports a behavior that is not standard. You can do new Response(ReadableStream.from(...)).text() instead.

@petamoriken
Copy link
Contributor

Node.js's Response seems to have extension to convert from AsyncIterable.

https://github.com/nodejs/node/blob/a450eedffade72d31624dbbd9171ac87b28c436a/deps/undici/src/lib/fetch/response.js#L542-L546

@jcbhmr
Copy link
Contributor Author

jcbhmr commented Nov 3, 2023

Would this be possible to do only in npm: scopes similar to the setTimeout and setInterval shadowing? 🤔 Or is this too niche and likely to be removed from nodejs that this is a no-go? 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants