/
index.ts
52 lines (47 loc) · 1.79 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { asyncIterableToStream } from 'https://ghuc.cc/qwtel/whatwg-stream-to-async-iter/index.ts';
import { aMap, aJoin, promiseToAsyncIterable, ForOfAwaitable } from './iter.ts';
// deno-lint-ignore no-explicit-any
const isCFWorkers = (<any>self.navigator)?.userAgent?.includes('Cloudflare-Workers')
|| !('TextEncoderStream' in self)
/**
* TBD
*/
export class StreamResponse extends Response {
constructor(body: ForOfAwaitable<string>, init?: ResponseInit) {
// CF Workers doesn't support non-binary Transform Streams,
// so we use a version that does the byte encoding in a async iterator instead:
super(isCFWorkers
? (() => {
const encoder = new TextEncoder();
return asyncIterableToStream(aMap(body, x => encoder.encode(x)))
})()
: asyncIterableToStream(body).pipeThrough(new TextEncoderStream()), init)
}
}
/**
* TBD
*/
export class ByteStreamResponse extends Response {
constructor(body: ForOfAwaitable<Uint8Array>, init?: ResponseInit) {
super(asyncIterableToStream(body), init)
}
}
/**
* If for any reason you don't want to use streaming response bodies,
* you can use this class instead, which will buffer the entire body before releasing it to the network.
* Note that headers will still be sent immediately.
*/
export class BufferedResponse extends Response {
constructor(body: ForOfAwaitable<string>, init?: ResponseInit) {
super(asyncIterableToStream(promiseToAsyncIterable(
aJoin(body).then(str => new TextEncoder().encode(str)))
), init);
}
}
// export class BufferedByteResponse extends Response {
// constructor(body: AsyncIterable<Uint8Array>, init?: ResponseInit) {
// super(asyncIterableToStream(promiseToAsyncIterable(
// aConsume(body).then(xs => concatUint8Arrays(...xs)))
// ), init);
// }
// }