fix: add support for polyfilled node-fetch on Vercel

pull/52/head
Travis Fischer 2022-12-07 14:44:30 -06:00
rodzic d4f8abb1e7
commit dc94e9368a
1 zmienionych plików z 20 dodań i 3 usunięć

Wyświetl plik

@ -19,8 +19,25 @@ export async function fetchSSE(
}
})
for await (const chunk of streamAsyncIterable(res.body)) {
const str = new TextDecoder().decode(chunk)
parser.feed(str)
if (!res.body.getReader) {
// Vercel polyfills `fetch` with `node-fetch`, which doesn't conform to
// web standards, so this is a workaround...
const body: NodeJS.ReadableStream = res.body as any
if (body.on || !body.read) {
throw new Error('unsupported "fetch" implementation')
}
body.on('readable', () => {
let chunk: string | Buffer
while (null !== (chunk = body.read())) {
parser.feed(chunk.toString())
}
})
} else {
for await (const chunk of streamAsyncIterable(res.body)) {
const str = new TextDecoder().decode(chunk)
parser.feed(str)
}
}
}