UNPKG

fastify-uws

Version:

A performant HTTP and WebSocket server for Fastify with uWebSockets.

2 lines (1 loc) 3.16 kB
"use strict";var H=Object.defineProperty;var N=(n,o,e)=>o in n?H(n,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[o]=e;var i=(n,o,e)=>N(n,typeof o!="symbol"?o+"":o,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C=require("node:dns/promises"),W=require("node:http"),y=require("eventemitter3"),I=require("ipaddr.js"),m=require("uWebSockets.js"),c=require("./errors.cjs"),M=require("./http-socket.cjs"),P=require("./request.cjs"),x=require("./response.cjs"),s=require("./symbols.cjs");function O(){return m.App()}const V=new Map(W.METHODS.map(n=>[n.toLowerCase(),n])),l={};var E,L,S,g,R,p,_,w,T;class b extends y.EventEmitter{constructor(e,t={}){super();i(this,T);i(this,"timeout");i(this,w);i(this,_);i(this,p);i(this,R);i(this,g);i(this,S);i(this,L);i(this,E);const{connectionTimeout:d=0,https:h=!1}=t;this[s.kHandler]=e,this.timeout=d,this[s.kHttps]=h,this[s.kWs]=null,this[s.kAddress]=null,this[s.kListenSocket]=null,this[s.kApp]=O(),this[s.kClosed]=!1}get encrypted(){return!!this[s.kHttps]}get listening(){return this[s.kListening]}setTimeout(e){this.timeout=e}address(){return this[s.kAddress]}listen(e,t){e!=null&&e.signal&&e.signal.addEventListener("abort",()=>{this.close()}),this[s.kListen](e).then(()=>{t==null||t(),this[s.kListening]=!0,this.emit("listening")}).catch(d=>{this[s.kAddress]=null,process.nextTick(()=>this.emit("error",d))})}closeIdleConnections(){this.close()}close(e=()=>{}){var d;if(this[s.kAddress]=null,this[s.kListening]=!1,this[s.kClosed])return e();const t=(d=this[s.kAddress])==null?void 0:d.port;if(t!==void 0&&l[t]===this&&delete l[t],this[s.kAddress]=null,this[s.kClosed]=!0,this[s.kListenSocket]&&(m.us_listen_socket_close(this[s.kListenSocket]),this[s.kListenSocket]=null),this[s.kWs])for(const h of this[s.kWs].connections)h.close();process.nextTick(()=>{this.emit("close"),e()})}ref(){}unref(){}async[(T=s.kHandler,w=s.kHttps,_=s.kWs,p=s.kAddress,R=s.kListenSocket,g=s.kApp,S=s.kClosed,L=s.kListenAll,E=s.kListening,s.kListen)]({port:e,host:t}){if(this[s.kClosed])throw new c.ERR_SERVER_DESTROYED;if(e!=null&&Number.isNaN(Number(e)))throw new c.ERR_SOCKET_BAD_PORT(e);e=e==null?0:Number(e);const d=await C.lookup(t);if(this[s.kAddress]={...d,port:e},this[s.kAddress].address.startsWith("["))throw new c.ERR_ENOTFOUND(this[s.kAddress].address);const h=I.parse(this[s.kAddress].address);this[s.kAddress].family=h.kind()==="ipv6"?"IPv6":"IPv4";const v=h.toNormalizedString(),a=this[s.kApp],q=(A,u)=>{const k=V.get(u.getMethod()),r=new M.HTTPSocket(this,A,k==="GET"||k==="HEAD");if(!k){r[s.kClientError]=!0,this.emit("clientError",new c.ERR_INVALID_METHOD,r);return}const f=new P.Request(u,r,k),D=new x.Response(r);f.headers.upgrade&&this.emit("upgrade",f,r),this[s.kHandler](f,D)};return a.any("/*",q),e!==0&&l[e]&&(this[s.kWs]=l[e][s.kWs]),this[s.kWs]&&this[s.kWs].addServer(this),new Promise((A,u)=>{const k=r=>{if(!r)return u(new c.ERR_ADDRINUSE(this[s.kAddress].address,e));this[s.kListenSocket]=r,e=this[s.kAddress].port=m.us_socket_local_port(r),l[e]||(l[e]=this),A()};this[s.kListenAll]=t==="localhost",this[s.kListenAll]?a.listen(e,k):a.listen(v,e,k)})}}exports.Server=b;