jadq-x-vue
Version:
Craft AI-driven interfaces effortlessly
65 lines (63 loc) • 1.59 kB
JavaScript
const u = (e) => (e ?? "").trim() !== "";
function T() {
let e = "";
return new TransformStream({
transform(t, o) {
e += t;
const r = e.split(`
`);
r.slice(0, -1).forEach((n) => {
u(n) && o.enqueue(n);
}), e = r[r.length - 1];
},
flush(t) {
u(e) && t.enqueue(e);
}
});
}
function f() {
return new TransformStream({
transform(e, t) {
const r = e.split(`
`).reduce((n, s) => {
const a = s.indexOf(":");
if (a === -1)
throw new Error(
'The key-value separator ":" is not found in the sse line chunk!'
);
const i = s.slice(0, a);
if (!u(i)) return n;
const c = s.slice(a + 1);
return { ...n, [i]: c };
}, {});
Object.keys(r).length !== 0 && t.enqueue(r);
}
});
}
function A(e) {
const { readableStream: t, transformStream: o } = e;
if (!(t instanceof ReadableStream))
throw new Error("The options.readableStream must be an instance of ReadableStream.");
const r = new TextDecoderStream(), n = o ? (
/**
* Uint8Array binary -> string -> Output
*/
t.pipeThrough(r).pipeThrough(o)
) : (
/**
* Uint8Array binary -> string -> SSE part string -> Default Output {@link SSEOutput}
*/
t.pipeThrough(r).pipeThrough(T()).pipeThrough(f())
);
return n[Symbol.asyncIterator] = async function* () {
const s = this.getReader();
for (; ; ) {
const { done: a, value: i } = await s.read();
if (a) break;
i && (yield i);
}
}, n;
}
export {
A as default
};