-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
96 lines (81 loc) · 3.93 KB
/
Copy pathindex.ts
File metadata and controls
96 lines (81 loc) · 3.93 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// deno-lint-ignore-file no-explicit-any
import { asyncIterToStream, streamToAsyncIter } from 'https://ghuc.cc/qwtel/whatwg-stream-to-async-iter/index.ts';
import { concatUint8Arrays } from 'https://ghuc.cc/qwtel/typed-array-utils/index.ts'
import { aMap, aJoin, collect, promiseToStream, ForAwaitable } from './iter.ts';
export type StreamBodyInit = ForAwaitable<string> | ReadableStream<string>;
export type ByteStreamBodyInit = ForAwaitable<Uint8Array> | ReadableStream<Uint8Array>;
const maybeAsyncIterToStream = <T>(x: ForAwaitable<T> | ReadableStream<T>) =>
x instanceof ReadableStream ? x : asyncIterToStream(x);
const maybeStreamToAsyncIter = <T>(x: ForAwaitable<T> | ReadableStream<T>) =>
x instanceof ReadableStream ? streamToAsyncIter(x) : x;
// FIXME: add exception for newer versions that support streams correctly!?
const isCFWorkers = (<any>globalThis.navigator)?.userAgent?.includes('Cloudflare-Workers')
|| !('TextEncoderStream' in globalThis)
// CF Workers doesn't support non-binary Transform Streams,
// so we use a version that does the byte encoding in a async iterator instead:
const stringStreamToByteStream: (body: StreamBodyInit) => ReadableStream<Uint8Array> = isCFWorkers
? body => {
const encoder = new TextEncoder();
return asyncIterToStream(aMap(maybeStreamToAsyncIter(body), x => encoder.encode(x)))
}
: body => maybeAsyncIterToStream(body).pipeThrough(new TextEncoderStream())
const CONTENT_TYPE = 'content-type'
const OCTET_STREAM = 'application/octet-stream'
export class StreamResponse extends Response {
constructor(body?: StreamBodyInit | null, init?: ResponseInit) {
super(body && stringStreamToByteStream(body), init)
if (!this.headers.has(CONTENT_TYPE)) this.headers.set(CONTENT_TYPE, OCTET_STREAM)
}
}
export class ByteStreamResponse extends Response {
constructor(body?: ByteStreamBodyInit | null, init?: ResponseInit) {
super(body && maybeAsyncIterToStream(body), init)
if (!this.headers.has(CONTENT_TYPE)) this.headers.set(CONTENT_TYPE, OCTET_STREAM)
}
}
/**
* 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 BufferedStreamResponse extends Response {
constructor(body?: StreamBodyInit | null, init?: ResponseInit) {
super(body && promiseToStream(
aJoin(maybeStreamToAsyncIter(body)).then(str => new TextEncoder().encode(str))
), init);
if (!this.headers.has(CONTENT_TYPE)) this.headers.set(CONTENT_TYPE, OCTET_STREAM)
}
}
export class BufferedByteStreamResponse extends Response {
constructor(body?: ByteStreamBodyInit | null, init?: ResponseInit) {
super(body && promiseToStream(
collect(maybeStreamToAsyncIter(body)).then(chunks => concatUint8Arrays(...chunks))
), init);
if (!this.headers.has(CONTENT_TYPE)) this.headers.set(CONTENT_TYPE, OCTET_STREAM)
}
}
export { BufferedStreamResponse as BufferedResponse }
export type StreamRequestInit = Omit<RequestInit, 'body'> & { body?: StreamBodyInit }
export type ByteStreamRequestInit = Omit<RequestInit, 'body'> & { body?: ByteStreamBodyInit }
export class StreamRequest extends Request {
constructor(input: RequestInfo, init?: StreamRequestInit) {
const { body, ...rest } = init || {};
super(input, {
...body ? { body: stringStreamToByteStream(body) } : {},
...rest,
});
if (body && !this.headers.has(CONTENT_TYPE)) this.headers.set(CONTENT_TYPE, OCTET_STREAM)
}
}
export class ByteStreamRequest extends Request {
constructor(input: RequestInfo, init?: ByteStreamRequestInit) {
const { body, ...rest } = init || {};
super(input, {
...body ? { body: maybeAsyncIterToStream(body) } : {},
...rest,
});
if (body && !this.headers.has(CONTENT_TYPE)) this.headers.set(CONTENT_TYPE, OCTET_STREAM)
}
}
// TODO: BufferedStreamRequest...
// TODO: BufferedByteStreamRequest...