fastify-uws
Version:
A performant HTTP and WebSocket server for Fastify with uWebSockets.
2 lines (1 loc) • 2.94 kB
JavaScript
"use strict";var b=Object.defineProperty;var E=(d,s,e)=>s in d?b(d,s,{enumerable:!0,configurable:!0,writable:!0,value:e}):d[s]=e;var a=(d,s,e)=>E(d,typeof s!="symbol"?s+"":s,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("eventemitter3");require("streamx");const g=require("uWebSockets.js"),S=require("./http-socket.cjs"),T=require("./request.cjs"),B=require("./response.cjs"),n=require("./symbols.cjs"),q={compression:g.SHARED_COMPRESSOR,maxPayloadLength:16*1024*1024,idleTimeout:16},k="!",y=Buffer.from(k);class p extends l.EventEmitter{constructor(e,r,c={}){super();a(this,"namespace");a(this,"connection");this.namespace=e,this.connection=r,r.websocket=this,this.topics=c,this[n.kEnded]=!1}static allocTopic(e,r){if(r[n.kTopic])return r;const c=Buffer.concat([e,y,Buffer.isBuffer(r)?r:Buffer.from(r)]);return c[n.kTopic]=!0,c}get uws(){return!0}allocTopic(e){return this.topics[e]?this.topics[e]:p.allocTopic(this.namespace,e)}send(e,r,c){if(!this[n.kEnded])return this.connection.send(e,r,c)}publish(e,r,c,t){if(!this[n.kEnded])return this.connection.publish(this.allocTopic(e),r,c,t)}subscribe(e){if(!this[n.kEnded])return this.connection.subscribe(this.allocTopic(e))}unsubscribe(e){if(!this[n.kEnded])return this.connection.unsubscribe(this.allocTopic(e))}isSubscribed(e){return this[n.kEnded]?!1:this.connection.isSubscribed(this.allocTopic(e))}getTopics(){return this[n.kEnded]?[]:this.connection.getTopics().map(e=>e.slice(e.indexOf(k)+1))}close(){if(!this[n.kEnded])return this[n.kEnded]=!0,this.connection.close()}end(e,r){if(!this[n.kEnded])return this[n.kEnded]=!0,this.connection.end(e,r)}cork(e){if(!this[n.kEnded])return this.connection.cork(e)}getBufferedAmount(){return this[n.kEnded]?0:this.connection.getBufferedAmount()}ping(e){if(!this[n.kEnded])return this.connection.ping(e)}}class A extends l.EventEmitter{constructor(s={}){super(),this.options={...s,...q},this.connections=new Set}addServer(s){const{options:e}=this,r=s[n.kApp],c=s[n.kHandler];r.ws("/*",{upgrade:async(t,i,o)=>{const u=i.getMethod().toUpperCase(),f=new S.HTTPSocket(s,t,u==="GET"||u==="HEAD"),h=new T.Request(i,f,u),m=new B.Response(f);h[n.kWs]=o,s.emit("upgrade",h,f),c(h,m)},open:t=>{this.connections.add(t),t.handler(t),this.emit("open",t)},close:(t,i,o)=>{this.connections.delete(t),t.websocket[n.kEnded]=!0,t.req.socket.destroy();const u=o instanceof ArrayBuffer?Buffer.from(o):o;t.websocket.emit("close",i,u),this.emit("close",t,i,u)},drain:t=>{t.websocket.emit("drain"),this.emit("drain",t)},message:(t,i,o)=>{const u=i instanceof ArrayBuffer?Buffer.from(i):i;t.websocket.emit("message",u,o),this.emit("message",t,u,o)},ping:(t,i)=>{const o=i instanceof ArrayBuffer?Buffer.from(i):i;t.websocket.emit("ping",o),this.emit("ping",t,o)},pong:(t,i)=>{const o=i instanceof ArrayBuffer?Buffer.from(i):i;t.websocket.emit("pong",o),this.emit("pong",t,o)},...e})}}exports.WebSocket=p;exports.WebSocketServer=A;