|
| 1 | +import { ExecutionResult } from 'graphql' |
| 2 | +import { isAsyncIterable } from '@envelop/core' |
| 3 | + |
| 4 | +import { getResponseInitByRespectingErrors } from '../../error.js' |
| 5 | +import { FetchAPI, MaybeArray } from '../../types.js' |
| 6 | +import { ResultProcessor, ResultProcessorInput } from '../types.js' |
| 7 | +import { jsonStringifyResultWithoutInternals } from './stringify.js' |
| 8 | + |
| 9 | +export interface SSEProcessorOptions { |
| 10 | + legacySSE: boolean |
| 11 | +} |
| 12 | + |
| 13 | +export function getSSEProcessor(opts: SSEProcessorOptions): ResultProcessor { |
| 14 | + return function processSSEResult( |
| 15 | + result: ResultProcessorInput, |
| 16 | + fetchAPI: FetchAPI, |
| 17 | + ): Response { |
| 18 | + let pingIntervalMs = 12_000 |
| 19 | + |
| 20 | + // for testing the pings, reduce the timeout |
| 21 | + if (globalThis.process?.env?.NODE_ENV === 'test') { |
| 22 | + pingIntervalMs = 300 |
| 23 | + } |
| 24 | + |
| 25 | + const headersInit = { |
| 26 | + 'Content-Type': 'text/event-stream', |
| 27 | + Connection: 'keep-alive', |
| 28 | + 'Cache-Control': 'no-cache', |
| 29 | + 'Content-Encoding': 'none', |
| 30 | + } |
| 31 | + |
| 32 | + const responseInit = getResponseInitByRespectingErrors(result, headersInit) |
| 33 | + |
| 34 | + let iterator: AsyncIterator<MaybeArray<ExecutionResult>> |
| 35 | + |
| 36 | + let pingInterval: number |
| 37 | + const textEncoder = new fetchAPI.TextEncoder() |
| 38 | + const readableStream = new fetchAPI.ReadableStream({ |
| 39 | + start(controller) { |
| 40 | + // ping client every 12 seconds to keep the connection alive |
| 41 | + pingInterval = setInterval(() => { |
| 42 | + if (!controller.desiredSize) { |
| 43 | + clearInterval(pingInterval) |
| 44 | + return |
| 45 | + } |
| 46 | + controller.enqueue(textEncoder.encode(':\n\n')) |
| 47 | + }, pingIntervalMs) as unknown as number |
| 48 | + |
| 49 | + if (isAsyncIterable(result)) { |
| 50 | + iterator = result[Symbol.asyncIterator]() |
| 51 | + } else { |
| 52 | + let finished = false |
| 53 | + iterator = { |
| 54 | + next: () => { |
| 55 | + if (finished) { |
| 56 | + return Promise.resolve({ done: true, value: null }) |
| 57 | + } |
| 58 | + finished = true |
| 59 | + return Promise.resolve({ done: false, value: result }) |
| 60 | + }, |
| 61 | + } |
| 62 | + } |
| 63 | + }, |
| 64 | + async pull(controller) { |
| 65 | + const { done, value } = await iterator.next() |
| 66 | + if (value != null) { |
| 67 | + if (!opts.legacySSE) { |
| 68 | + controller.enqueue(textEncoder.encode(`event: next\n`)) |
| 69 | + } |
| 70 | + const chunk = jsonStringifyResultWithoutInternals(value) |
| 71 | + controller.enqueue(textEncoder.encode(`data: ${chunk}\n\n`)) |
| 72 | + } |
| 73 | + if (done) { |
| 74 | + controller.enqueue(textEncoder.encode(`event: complete\n\n`)) |
| 75 | + clearInterval(pingInterval) |
| 76 | + controller.close() |
| 77 | + } |
| 78 | + }, |
| 79 | + async cancel(e) { |
| 80 | + clearInterval(pingInterval) |
| 81 | + await iterator.return?.(e) |
| 82 | + }, |
| 83 | + }) |
| 84 | + return new fetchAPI.Response(readableStream, responseInit) |
| 85 | + } |
| 86 | +} |
0 commit comments