Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/jsx/streaming.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -881,4 +881,108 @@ d.replaceWith(c.content)
)
})
})

it('should not throw ERR_INVALID_STATE when reader is cancelled during nested Suspense streaming', async () => {
const unhandled: unknown[] = []
const onRejection = (e: unknown) => unhandled.push(e)
process.on('unhandledRejection', onRejection)

const SubContent = async () => <h2>World</h2>
const Content = async () => (
<>
<h1>Hello</h1>
<Suspense fallback={<p>Loading sub...</p>}>
<SubContent />
</Suspense>
</>
)

const onError = vi.fn()
const stream = renderToReadableStream(
<Suspense fallback={<p>Loading...</p>}>
<Content />
</Suspense>,
onError
)

const reader = stream.getReader()
const firstChunk = await reader.read()
expect(firstChunk.done).toBe(false)

// Simulate client disconnect
await reader.cancel()

// Wait for nested Suspense callbacks to fire against the closed controller
await new Promise((resolve) => setTimeout(resolve))

expect(unhandled).toHaveLength(0)
expect(onError).not.toHaveBeenCalled()

process.off('unhandledRejection', onRejection)
})

it('should not call onError when reader is cancelled during a slow callback resolution', async () => {
const unhandled: unknown[] = []
const onRejection = (e: unknown) => unhandled.push(e)
process.on('unhandledRejection', onRejection)

let signalCallbackStarted!: () => void
const callbackStarted = new Promise<void>((r) => {
signalCallbackStarted = r
})

const Content = async () =>
raw('<p>content</p>', [
((opts: any) => {
if (opts.phase === HtmlEscapedCallbackPhase.BeforeStream) {
signalCallbackStarted()
return new Promise<string>((r) => setTimeout(() => r('')))
}
return undefined
}) as any,
])

const onError = vi.fn()
const stream = renderToReadableStream(
<Suspense fallback={<p>Loading...</p>}>
<Content />
</Suspense>,
onError
)

const reader = stream.getReader()
await reader.read()

await callbackStarted
await reader.cancel()

await new Promise((resolve) => setTimeout(resolve))

expect(unhandled).toHaveLength(0)
expect(onError).not.toHaveBeenCalled()

process.off('unhandledRejection', onRejection)
})

it('should not throw when cancelled before initial content resolves', async () => {
const unhandled: unknown[] = []
const onRejection = (e: unknown) => unhandled.push(e)
process.on('unhandledRejection', onRejection)

const onError = vi.fn()
const stream = renderToReadableStream(
Promise.resolve(raw('<p>slow content</p>') as HtmlEscapedString),
onError
)

const reader = stream.getReader()
await reader.cancel()

await new Promise((resolve) => setTimeout(resolve))

expect(unhandled).toHaveLength(0)
expect(onError).not.toHaveBeenCalled()

process.off('unhandledRejection', onRejection)
})
})
16 changes: 13 additions & 3 deletions src/jsx/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export const renderToReadableStream = (
content: HtmlEscapedString | JSXNode | Promise<HtmlEscapedString>,
onError: (e: unknown) => string | void = console.trace
): ReadableStream<Uint8Array> => {
let cancelled = false
const reader = new ReadableStream<Uint8Array>({
async start(controller) {
try {
Expand All @@ -157,7 +158,9 @@ export const renderToReadableStream = (
true,
context
)
controller.enqueue(textEncoder.encode(resolved))
if (!cancelled) {
controller.enqueue(textEncoder.encode(resolved))
}

let resolvedCount = 0
const callbacks: Promise<void>[] = []
Expand All @@ -182,7 +185,9 @@ export const renderToReadableStream = (
.filter<Promise<string>>(Boolean as any)
.forEach(then)
resolvedCount++
controller.enqueue(textEncoder.encode(res))
if (!cancelled) {
controller.enqueue(textEncoder.encode(res))
}
})
)
}
Expand All @@ -199,7 +204,12 @@ export const renderToReadableStream = (
onError(e)
}

controller.close()
if (!cancelled) {
controller.close()
}
},
cancel() {
cancelled = true
},
})
return reader
Expand Down
Loading