1 | var _a, _b;
|
2 |
|
3 | import * as dntShim from "./_dnt.shims.js";
|
4 | import { asyncIterToStream, streamToAsyncIter } from 'whatwg-stream-to-async-iter';
|
5 | import { concatUint8Arrays } from 'typed-array-utils';
|
6 | import { aMap, aJoin, collect, promiseToStream } from './iter.js';
|
7 | const maybeAsyncIterToStream = (x) => x instanceof ReadableStream ? x : asyncIterToStream(x);
|
8 | const maybeStreamToAsyncIter = (x) => x instanceof ReadableStream ? streamToAsyncIter(x) : x;
|
9 |
|
10 | const isCFWorkers = ((_b = (_a = globalThis.navigator) === null || _a === void 0 ? void 0 : _a.userAgent) === null || _b === void 0 ? void 0 : _b.includes('Cloudflare-Workers'))
|
11 | || !('TextEncoderStream' in dntShim.dntGlobalThis);
|
12 |
|
13 |
|
14 | const stringStreamToByteStream = isCFWorkers
|
15 | ? body => {
|
16 | const encoder = new TextEncoder();
|
17 | return asyncIterToStream(aMap(maybeStreamToAsyncIter(body), x => encoder.encode(x)));
|
18 | }
|
19 | : body => maybeAsyncIterToStream(body).pipeThrough(new TextEncoderStream());
|
20 | const CONTENT_TYPE = 'content-type';
|
21 | const OCTET_STREAM = 'application/octet-stream';
|
22 | export class StreamResponse extends Response {
|
23 | constructor(body, init) {
|
24 | super(body && stringStreamToByteStream(body), init);
|
25 | if (!this.headers.has(CONTENT_TYPE))
|
26 | this.headers.set(CONTENT_TYPE, OCTET_STREAM);
|
27 | }
|
28 | }
|
29 | export class ByteStreamResponse extends Response {
|
30 | constructor(body, init) {
|
31 | super(body && maybeAsyncIterToStream(body), init);
|
32 | if (!this.headers.has(CONTENT_TYPE))
|
33 | this.headers.set(CONTENT_TYPE, OCTET_STREAM);
|
34 | }
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | export class BufferedStreamResponse extends Response {
|
42 | constructor(body, init) {
|
43 | super(body && promiseToStream(aJoin(maybeStreamToAsyncIter(body)).then(str => new TextEncoder().encode(str))), init);
|
44 | if (!this.headers.has(CONTENT_TYPE))
|
45 | this.headers.set(CONTENT_TYPE, OCTET_STREAM);
|
46 | }
|
47 | }
|
48 | export class BufferedByteStreamResponse extends Response {
|
49 | constructor(body, init) {
|
50 | super(body && promiseToStream(collect(maybeStreamToAsyncIter(body)).then(chunks => concatUint8Arrays(...chunks))), init);
|
51 | if (!this.headers.has(CONTENT_TYPE))
|
52 | this.headers.set(CONTENT_TYPE, OCTET_STREAM);
|
53 | }
|
54 | }
|
55 | export { BufferedStreamResponse as BufferedResponse };
|
56 | export class StreamRequest extends Request {
|
57 | constructor(input, init) {
|
58 | const { body, ...rest } = init || {};
|
59 | super(input, {
|
60 | ...body ? { body: stringStreamToByteStream(body) } : {},
|
61 | ...rest,
|
62 | });
|
63 | if (body && !this.headers.has(CONTENT_TYPE))
|
64 | this.headers.set(CONTENT_TYPE, OCTET_STREAM);
|
65 | }
|
66 | }
|
67 | export class ByteStreamRequest extends Request {
|
68 | constructor(input, init) {
|
69 | const { body, ...rest } = init || {};
|
70 | super(input, {
|
71 | ...body ? { body: maybeAsyncIterToStream(body) } : {},
|
72 | ...rest,
|
73 | });
|
74 | if (body && !this.headers.has(CONTENT_TYPE))
|
75 | this.headers.set(CONTENT_TYPE, OCTET_STREAM);
|
76 | }
|
77 | }
|
78 |
|
79 |
|
80 |
|
\ | No newline at end of file |