@worker-tools/stream-response
Version:
Fetch API Response objects made from async generators. Build streaming HTML responses or SSE with JS sugar.
112 lines • 5.35 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var _a, _b;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ByteStreamRequest = exports.StreamRequest = exports.BufferedResponse = exports.BufferedByteStreamResponse = exports.BufferedStreamResponse = exports.ByteStreamResponse = exports.StreamResponse = void 0;
// deno-lint-ignore-file no-explicit-any
const dntShim = __importStar(require("./_dnt.shims.js"));
const whatwg_stream_to_async_iter_1 = require("whatwg-stream-to-async-iter");
const typed_array_utils_1 = require("typed-array-utils");
const iter_js_1 = require("./iter.js");
const maybeAsyncIterToStream = (x) => x instanceof ReadableStream ? x : (0, whatwg_stream_to_async_iter_1.asyncIterToStream)(x);
const maybeStreamToAsyncIter = (x) => x instanceof ReadableStream ? (0, whatwg_stream_to_async_iter_1.streamToAsyncIter)(x) : x;
// FIXME: add exception for newer versions that support streams correctly!?
const isCFWorkers = ((_b = (_a = globalThis.navigator) === null || _a === void 0 ? void 0 : _a.userAgent) === null || _b === void 0 ? void 0 : _b.includes('Cloudflare-Workers'))
|| !('TextEncoderStream' in dntShim.dntGlobalThis);
// 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 = isCFWorkers
? body => {
const encoder = new TextEncoder();
return (0, whatwg_stream_to_async_iter_1.asyncIterToStream)((0, iter_js_1.aMap)(maybeStreamToAsyncIter(body), x => encoder.encode(x)));
}
: body => maybeAsyncIterToStream(body).pipeThrough(new TextEncoderStream());
const CONTENT_TYPE = 'content-type';
const OCTET_STREAM = 'application/octet-stream';
class StreamResponse extends Response {
constructor(body, init) {
super(body && stringStreamToByteStream(body), init);
if (!this.headers.has(CONTENT_TYPE))
this.headers.set(CONTENT_TYPE, OCTET_STREAM);
}
}
exports.StreamResponse = StreamResponse;
class ByteStreamResponse extends Response {
constructor(body, init) {
super(body && maybeAsyncIterToStream(body), init);
if (!this.headers.has(CONTENT_TYPE))
this.headers.set(CONTENT_TYPE, OCTET_STREAM);
}
}
exports.ByteStreamResponse = ByteStreamResponse;
/**
* 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.
*/
class BufferedStreamResponse extends Response {
constructor(body, init) {
super(body && (0, iter_js_1.promiseToStream)((0, iter_js_1.aJoin)(maybeStreamToAsyncIter(body)).then(str => new TextEncoder().encode(str))), init);
if (!this.headers.has(CONTENT_TYPE))
this.headers.set(CONTENT_TYPE, OCTET_STREAM);
}
}
exports.BufferedStreamResponse = BufferedStreamResponse;
exports.BufferedResponse = BufferedStreamResponse;
class BufferedByteStreamResponse extends Response {
constructor(body, init) {
super(body && (0, iter_js_1.promiseToStream)((0, iter_js_1.collect)(maybeStreamToAsyncIter(body)).then(chunks => (0, typed_array_utils_1.concatUint8Arrays)(...chunks))), init);
if (!this.headers.has(CONTENT_TYPE))
this.headers.set(CONTENT_TYPE, OCTET_STREAM);
}
}
exports.BufferedByteStreamResponse = BufferedByteStreamResponse;
class StreamRequest extends Request {
constructor(input, init) {
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);
}
}
exports.StreamRequest = StreamRequest;
class ByteStreamRequest extends Request {
constructor(input, init) {
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);
}
}
exports.ByteStreamRequest = ByteStreamRequest;
// TODO: BufferedStreamRequest...
// TODO: BufferedByteStreamRequest...
//# sourceMappingURL=index.js.map