UNPKG

fastify-uws

Version:

A performant HTTP and WebSocket server for Fastify with uWebSockets.

7 lines (6 loc) 1.56 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const d=require("fastify-plugin"),i=require("it-pushable"),l=require("it-to-stream"),h=d(async(e,t)=>{e.decorateReply("sse",function(r){var s;if(!this.raw.headersSent){this.sseContext={source:i.pushable({objectMode:!0})};const c=this.getHeaders();for(const[u,f]of Object.entries(c))this.raw.setHeader(u,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(y(r))return n(this,r);(s=this.sseContext)!=null&&s.source||(this.sseContext={source:i.pushable({objectMode:!0})},n(this,this.sseContext.source)),this.sseContext.source.push(r)})},{fastify:"5.x",name:"@fastify/eventsource"});function n(e,t){l(o(t)).pipe(e.raw)}async function*o(e){for await(const t of e)yield w(t)}function y(e){return e==null||typeof e!="object"?!1:Symbol.asyncIterator in e}const p=e=>typeof e>"u",b=e=>p(e)||e===null,v=e=>!b(e)&&typeof e=="object";function a(e){return v(e)?a(JSON.stringify(e)):e.split(/\r\n|\r|\n/).map(t=>`data: ${t} `).join("")}function w(e){let t=e.event?`event: ${e.event} `:"";return t+=e.id?`id: ${e.id} `:"",t+=e.retry?`retry: ${e.retry} `:"",t+=e.data?a(e.data):"",t+=` `,t}exports.default=h;exports.transformAsyncIterable=o;