fastify-uws
Version:
A performant HTTP and WebSocket server for Fastify with uWebSockets.
55 lines (54 loc) • 1.82 kB
JavaScript
import u from "fastify-plugin";
import { pushable as o } from "it-pushable";
import d from "it-to-stream";
const C = u(
async (t, e) => {
t.decorateReply("sse", function(r) {
var s;
if (!this.raw.headersSent) {
this.sseContext = { source: o({ objectMode: !0 }) };
const a = this.getHeaders();
for (const [c, f] of Object.entries(a))
this.raw.setHeader(c, f ?? "");
this.raw.setHeader("Content-Type", "text/event-stream; charset=utf-8"), this.raw.setHeader("Connection", "keep-alive"), this.raw.setHeader(
"Cache-Control",
"private, no-cache, no-store, must-revalidate, max-age=0, no-transform"
), this.raw.setHeader("Pragma", "no-cache"), this.raw.setHeader("Expire", "0"), this.raw.setHeader("X-Accel-Buffering", "no"), n(this, this.sseContext.source);
}
if (l(r))
return n(this, r);
(s = this.sseContext) != null && s.source || (this.sseContext = { source: o({ objectMode: !0 }) }, n(this, this.sseContext.source)), this.sseContext.source.push(r);
});
},
{
fastify: "5.x",
name: "@fastify/eventsource"
}
);
function n(t, e) {
d(h(e)).pipe(t.raw);
}
async function* h(t) {
for await (const e of t)
yield m(e);
}
function l(t) {
return t == null || typeof t != "object" ? !1 : Symbol.asyncIterator in t;
}
const p = (t) => typeof t > "u", y = (t) => p(t) || t === null, b = (t) => !y(t) && typeof t == "object";
function i(t) {
return b(t) ? i(JSON.stringify(t)) : t.split(/\r\n|\r|\n/).map((e) => `data: ${e}
`).join("");
}
function m(t) {
let e = t.event ? `event: ${t.event}
` : "";
return e += t.id ? `id: ${t.id}
` : "", e += t.retry ? `retry: ${t.retry}
` : "", e += t.data ? i(t.data) : "", e += `
`, e;
}
export {
C as default,
h as transformAsyncIterable
};