Skip to content
Open
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
14 changes: 14 additions & 0 deletions packages/sdk/js/script/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ if (sseTypesPatched === sseTypesSource) {
}
await Bun.write(sseTypesPath, sseTypesPatched)

// Abort handlers cannot await ReadableStreamDefaultReader.cancel(), but the
// returned promise can reject after the body stream has already errored.
const sseClientPath = "./src/v2/gen/core/serverSentEvents.gen.ts"
const sseClientFile = Bun.file(sseClientPath)
const sseClientSource = await sseClientFile.text()
const sseClientPatched = sseClientSource.replace(
" reader.cancel()",
" void reader.cancel().catch(() => {})",
)
if (sseClientPatched === sseClientSource) {
throw new Error(`SSE cancel patch did not apply; @hey-api/openapi-ts output may have changed (${sseClientPath})`)
}
await Bun.write(sseClientPath, sseClientPatched)

await $`bun prettier --write src/gen`
await $`bun prettier --write src/v2`
await $`rm -rf dist`
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/js/src/gen/core/serverSentEvents.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const createSseClient = <TData = unknown>({

const abortHandler = () => {
try {
void reader.cancel()
void reader.cancel().catch(() => {})
} catch {
// noop
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/js/src/v2/gen/core/serverSentEvents.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const createSseClient = <TData = unknown>({

const abortHandler = () => {
try {
reader.cancel()
void reader.cancel().catch(() => {})
} catch {
// noop
}
Expand Down
30 changes: 30 additions & 0 deletions packages/sdk/js/test/server-sent-events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect, test } from "bun:test"
import { createSseClient } from "../src/v2/gen/core/serverSentEvents.gen"

test("observes reader cancellation errors when an SSE request is aborted", async () => {
const abort = new AbortController()
const result = createSseClient({
fetch: async () =>
new Response(
new ReadableStream({
start: (controller) =>
abort.signal.addEventListener("abort", () =>
controller.error(new DOMException("The operation was aborted", "AbortError")),
),
}),
{ headers: { "content-type": "text/event-stream" } },
),
method: "GET",
signal: abort.signal,
sseMaxRetryAttempts: 1,
url: "http://localhost/events",
})

const next = result.stream.next()
// Let the generator park in reader.read() before erroring the body stream.
await Bun.sleep(10)
abort.abort()
expect(await next).toEqual({ done: true, value: undefined })
// Give any unobserved cancellation rejection time to reach Bun's test runner.
await Bun.sleep(10)
})
Loading