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.[method] required ArrayBuffer or ArrayBufferView. #43838

Closed
fanhaoyuan opened this issue Jul 15, 2022 · 5 comments
Closed

Response.[method] required ArrayBuffer or ArrayBufferView. #43838

fanhaoyuan opened this issue Jul 15, 2022 · 5 comments

Comments

@fanhaoyuan
Copy link

fanhaoyuan commented Jul 15, 2022

Version

18.6.0

Platform

Darwin MacBook-Pro.local 21.3.0 Darwin Kernel Version 21.3.0: Wed Jan 5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_ARM64_T6000 arm64

Subsystem

No response

What steps will reproduce the bug?

  1. Using node 18.5.0
  2. new Response(ReadableStream<string>).text()
  3. success
  4. Using node 18.6.0
  5. new Response(ReadableStream<string>).text()
  6. fail

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

Success in node 18.6.0

What do you see instead?

TypeError: The "input" argument must be an instance of ArrayBuffer or ArrayBufferView. Received type string ('wauy2vqxwr9mbnpg5o8ln3tw...)

Additional information

No response

@fanhaoyuan
Copy link
Author

(async function test() {
    let index = 0;
    let text = '';
    const cof = 1000;
    const length = 1_000_000;

    let readableStream;

    // eslint-disable-next-line no-implicit-coercion
    if (!isNaN(+length)) {
        while (text.length < length) {
            text += Math.random().toString(36).slice(-5);
        }

        readableStream = new ReadableStream<string>({
            start(controller) {
                (function push() {
                    const currentText = text.slice(index * cof, (index + 1) * cof);

                    if (!currentText) {
                        controller.close();
                        return;
                    }

                    index++;

                    controller.enqueue(currentText);
                    push();
                })();
            },
        });
    }
    // Success in 18.5.0, but fail in 18.6.0
    const data = await new Response(readableStream).text();

    expect(text).toBe(data);
})();

@KhafraDev
Copy link
Member

KhafraDev commented Jul 15, 2022

This is a regression in undici from nodejs/undici#1537

This is actually expected behavior, see https://streams.spec.whatwg.org/#read-loop:

"If chunk is not a Uint8Array object, reject promise with a TypeError and abort these steps."

@fanhaoyuan
Copy link
Author

oh, I got it. So why it run success in node <=18.5.0 ?

@KhafraDev
Copy link
Member

KhafraDev commented Jul 15, 2022

A bug that just so happened to be handled by the PR mentioned above.

You can see that browsers don't support this behavior either with a smaller repo:

var readableStream = new ReadableStream({
    start (controller) {
        controller.enqueue('Non-uint8array')
        controller.close()
    }          
})

new Response(readableStream).text().then(console.log) 

@fanhaoyuan
Copy link
Author

got. thanks

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