UNPKG

3.6 kBJavaScriptView Raw
1var _a, _b;
2// deno-lint-ignore-file no-explicit-any
3import * as dntShim from "./_dnt.shims.js";
4import { asyncIterToStream, streamToAsyncIter } from 'whatwg-stream-to-async-iter';
5import { concatUint8Arrays } from 'typed-array-utils';
6import { aMap, aJoin, collect, promiseToStream } from './iter.js';
7const maybeAsyncIterToStream = (x) => x instanceof ReadableStream ? x : asyncIterToStream(x);
8const maybeStreamToAsyncIter = (x) => x instanceof ReadableStream ? streamToAsyncIter(x) : x;
9// FIXME: add exception for newer versions that support streams correctly!?
10const 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// CF Workers doesn't support non-binary Transform Streams,
13// so we use a version that does the byte encoding in a async iterator instead:
14const 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());
20const CONTENT_TYPE = 'content-type';
21const OCTET_STREAM = 'application/octet-stream';
22export 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}
29export 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 * If for any reason you don't want to use streaming response bodies,
38 * you can use this class instead, which will buffer the entire body before releasing it to the network.
39 * Note that headers will still be sent immediately.
40 */
41export 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}
48export 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}
55export { BufferedStreamResponse as BufferedResponse };
56export 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}
67export 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// TODO: BufferedStreamRequest...
79// TODO: BufferedByteStreamRequest...
80//# sourceMappingURL=index.js.map
\No newline at end of file