MCPcopy Index your code
hub / github.com/anomalyco/opencode / wrapSSE

Function wrapSSE

packages/opencode/src/provider/provider.ts:37–83  ·  view source on GitHub ↗
(res: Response, ms: number, ctl: AbortController)

Source from the content-addressed store, hash-verified

35const OPENAI_HEADER_TIMEOUT_DEFAULT = 10_000
36
37function wrapSSE(res: Response, ms: number, ctl: AbortController) {
38 if (typeof ms !== "number" || ms <= 0) return res
39 if (!res.body) return res
40 if (!res.headers.get("content-type")?.includes("text/event-stream")) return res
41
42 const reader = res.body.getReader()
43 const body = new ReadableStream<Uint8Array>({
44 async pull(ctrl) {
45 const part = await new Promise<Awaited<ReturnType<typeof reader.read>>>((resolve, reject) => {
46 const id = setTimeout(() => {
47 const err = new ProviderError.ResponseStreamError("SSE read timed out")
48 ctl.abort(err)
49 void reader.cancel(err)
50 reject(err)
51 }, ms)
52
53 reader.read().then(
54 (part) => {
55 clearTimeout(id)
56 resolve(part)
57 },
58 (err) => {
59 clearTimeout(id)
60 reject(err)
61 },
62 )
63 })
64
65 if (part.done) {
66 ctrl.close()
67 return
68 }
69
70 ctrl.enqueue(part.value)
71 },
72 async cancel(reason) {
73 ctl.abort(reason)
74 await reader.cancel(reason)
75 },
76 })
77
78 return new Response(body, {
79 headers: new Headers(res.headers),
80 status: res.status,
81 statusText: res.statusText,
82 })
83}
84
85function timeoutController(ms: number) {
86 const ctl = new AbortController()

Callers 1

resolveSDKFunction · 0.70

Calls 1

getMethod · 0.65

Tested by

no test coverage detected