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

Response does not accept an Array as ResponseInit: The value returned from the next() method is not an Uint8Array object #28166

Open
hamidrezakp opened this issue Feb 18, 2025 · 6 comments

Comments

@hamidrezakp
Copy link
Contributor

hamidrezakp commented Feb 18, 2025

Version: Deno 2.1.0

This behavior is introduced in the release v2.1.0, any prior version works just fine.
I suspect the changes in this PR is relevant.

In that PR, the way Array is converted to ReadableStream is changed.

Deno 2.0.6, this works:

> await new Response([1, 2, 3]).body.getReader().read()
{ value: Uint8Array(5) [ 49, 44, 50, 44, 51 ], done: false }

Deno 2.1.0, errors:

> await new Response([1, 2, 3]).body.getReader().read()
Uncaught TypeError: undefined failed to iterate next value: The value returned from the next() method is not an Uint8Array object
    at makeException (ext:deno_webidl/00_webidl.js:93:10)
    at converters.<computed> (ext:deno_webidl/00_webidl.js:562:15)
    at Object.next (ext:deno_webidl/00_webidl.js:1011:31)
    at eventLoopTick (ext:core/01_core.js:175:7)
    at async Object.next (ext:deno_webidl/00_webidl.js:998:32)
    at async ReadableStreamDefaultController.<anonymous> (ext:deno_web/06_streams.js:5186:19)
@lucacasonato
Copy link
Member

Why are you doing this? It seems like what you are doing is a bug in your own code - the array is being stringified (so turns into "1,2,3"), rather than returning a byte array of length 3.

@hamidrezakp
Copy link
Contributor Author

Why are you doing this? It seems like what you are doing is a bug in your own code - the array is being stringified (so turns into "1,2,3"), rather than returning a byte array of length 3.

Okay that was a bad example, returning an string array such as ["One", "Two", "Three"] fails too.

@lucacasonato
Copy link
Member

That that ever worked was luck on your part. You should convert random objects like arrays to strings before passing them to new Response.

@hamidrezakp
Copy link
Contributor Author

That that ever worked was luck on your part. You should convert random objects like arrays to strings before passing them to new Response.

I don't know if that would be the correct behavior, Is there anything specification for this case?

because exact same thing works on Firefox and Chrome consoles and Cloudflare workers.

Worth noting that Bun does not do the conversion into string, but just converting the numerals into bytes.

@hamidrezakp
Copy link
Contributor Author

That that ever worked was luck on your part. You should convert random objects like arrays to strings before passing them to new Response.

IMO it is logical to convert any iteratable like Array into Uint8Array, like implementing serialization for all types.

Although my Javascript knowledge is not great and may misunderstood some things.

@lucacasonato
Copy link
Member

lucacasonato commented Feb 18, 2025

I don't know if that would be the correct behavior, Is there anything specification for this case?

Yes, there is a spec - and the reason for Deno's behavioural change is actually to make it possible to use async iterables as Request / Response bodies. This is a specification proposal that we are incubating.

because exact same thing works on Firefox and Chrome consoles and Cloudflare workers.

Yes, see above. They are using the same behaviour that Deno had previously (and are not yet implementing this change to support async iterables in Request / Response bodies.

IMO it is logical to convert any iteratable like Array into Uint8Array, like implementing serialization for all types.

This requires a spec change. It is very unclear what "serialization for all types" actually means.

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

No branches or pull requests

3 participants