UNPKG

@worker-tools/middleware

Version:

A suite of standalone HTTP server middlewares for Worker Runtimes.

28 lines 1.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.flushed = exports.combine = void 0; var ts_functional_pipe_1 = require("ts-functional-pipe"); Object.defineProperty(exports, "combine", { enumerable: true, get: function () { return ts_functional_pipe_1.pipe; } }); const resolvable_promise_1 = require("@worker-tools/resolvable-promise"); class FlushCallbackStream extends TransformStream { constructor(flushCallback) { super({ flush() { flushCallback(); } }); } } const flushed = () => async (ax) => { const x = await ax; const flush = new resolvable_promise_1.ResolvablePromise(); const flushed = Promise.resolve(flush); x.effects.push(res => { const ref = {}; const cb = () => flush.resolve(ref.res); const { status, statusText, headers, body } = res; ref.res = new Response(body != null ? body.pipeThrough(new FlushCallbackStream(cb)) : (x.handled.then(cb), null), { status, statusText, headers }); return ref.res; }); return Object.assign(x, { flushed }); }; exports.flushed = flushed; //# sourceMappingURL=flushed.js.map