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

TypeError: Expected Sink code: "ERR_INVALID_THIS" #10632

Open
yus-ham opened this issue Apr 29, 2024 · 0 comments · May be fixed by #15234
Open

TypeError: Expected Sink code: "ERR_INVALID_THIS" #10632

yus-ham opened this issue Apr 29, 2024 · 0 comments · May be fixed by #15234
Labels
bug Something isn't working nice-repro An unusually helpful reproduction is provided

Comments

@yus-ham
Copy link
Contributor

yus-ham commented Apr 29, 2024

What version of Bun is running?

v1.1.6

What platform is your computer?

any

What steps can reproduce the bug?

export class EventStreamWriter {
    #sink;

    static create(opts) {
        const writer = new EventStreamWriter()

        const stream = new ReadableStream({
            type: 'direct',
            async pull(ctrl) {
                writer.#sink = ctrl
            },
        })

        const response = new Response(stream)

        return { writer, response }
    }

    write(message) {
        console.info('write message:', message = `event: ${message.event}\n\n`)
        message = new TextEncoder().encode(message)
        this.#sink.write(message)
        this.#sink.flush()
    }
}



const server = Bun.serve({
    async fetch(req) {
        const { writer, response } = EventStreamWriter.create({})

        setTimeout(async () => {
            let i = 3
            while (i--) {
                writer.write({event: "hello "+ i})
                await Bun.sleep(1000)
            }
        }, 1000)
        
        return response
    },
})

console.info(`Server started on ${server.url}`)





//// TEST
let response = await fetch(server.url)
let chunk, reader = response.body.getReader();

console.info('[JS]', {status: response.status})

while ((chunk = await reader.read()).value) {
  console.info({value: new TextDecoder().decode(chunk.value).slice(0, 30)})
}

What is the expected behavior?

'direct' ReadableStream sink should can be extracted and use in somewhere async calls

What do you see instead?

22 |         this.#sink.write(message)
             ^
TypeError: Expected Sink
 code: "ERR_INVALID_THIS"

Additional information

No response

@yus-ham yus-ham added the bug Something isn't working label Apr 29, 2024
@paperdave paperdave added the nice-repro An unusually helpful reproduction is provided label Nov 19, 2024
@paperdave paperdave linked a pull request Nov 19, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working nice-repro An unusually helpful reproduction is provided
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants