@jmealo/fastify-uws 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +315 -0
- package/dist/errors.cjs +1 -0
- package/dist/errors.d.ts +27 -0
- package/dist/errors.js +27 -0
- package/dist/http-socket.cjs +4 -0
- package/dist/http-socket.d.ts +67 -0
- package/dist/http-socket.js +174 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +18 -0
- package/dist/plugin-sse.cjs +10 -0
- package/dist/plugin-sse.d.ts +50 -0
- package/dist/plugin-sse.js +285 -0
- package/dist/plugin-websocket.cjs +1 -0
- package/dist/plugin-websocket.d.ts +9 -0
- package/dist/plugin-websocket.js +67 -0
- package/dist/request.cjs +1 -0
- package/dist/request.d.ts +29 -0
- package/dist/request.js +67 -0
- package/dist/response.cjs +1 -0
- package/dist/response.d.ts +40 -0
- package/dist/response.js +131 -0
- package/dist/server.cjs +1 -0
- package/dist/server.d.ts +51 -0
- package/dist/server.js +155 -0
- package/dist/symbols.cjs +1 -0
- package/dist/symbols.d.ts +27 -0
- package/dist/symbols.js +28 -0
- package/dist/websocket-server.cjs +1 -0
- package/dist/websocket-server.d.ts +82 -0
- package/dist/websocket-server.js +162 -0
- package/package.json +81 -0
package/dist/response.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { STATUS_CODES as u } from "node:http";
|
|
2
|
+
import { Writable as a } from "streamx";
|
|
3
|
+
import { ERR_HEAD_SET as o, ERR_STREAM_DESTROYED as h } from "./errors.js";
|
|
4
|
+
import { kHeaders as n, kHead as d } from "./symbols.js";
|
|
5
|
+
class c {
|
|
6
|
+
isMultiValue;
|
|
7
|
+
name;
|
|
8
|
+
value;
|
|
9
|
+
constructor(t, e) {
|
|
10
|
+
this.isMultiValue = Array.isArray(e), this.name = t, this.value = this.isMultiValue ? e : String(e);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const y = Buffer.alloc(0);
|
|
14
|
+
function f(i, t = !1) {
|
|
15
|
+
return i ? {
|
|
16
|
+
chunk: i,
|
|
17
|
+
empty: !1,
|
|
18
|
+
end: t,
|
|
19
|
+
byteLength: Buffer.isBuffer(i) ? i.byteLength : Buffer.byteLength(i)
|
|
20
|
+
} : { chunk: y, empty: !0, end: t, byteLength: 0 };
|
|
21
|
+
}
|
|
22
|
+
const p = () => {
|
|
23
|
+
}, l = {
|
|
24
|
+
byteLength(i) {
|
|
25
|
+
return i.byteLength;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
class k extends a {
|
|
29
|
+
socket;
|
|
30
|
+
statusCode = 200;
|
|
31
|
+
statusMessage;
|
|
32
|
+
headersSent = !1;
|
|
33
|
+
chunked = !1;
|
|
34
|
+
contentLength = null;
|
|
35
|
+
writableEnded = !1;
|
|
36
|
+
firstChunk = !0;
|
|
37
|
+
_boundEmitDrain;
|
|
38
|
+
[n];
|
|
39
|
+
constructor(t) {
|
|
40
|
+
super(l), this.socket = t, this._boundEmitDrain = this._emitDrain.bind(this), this[n] = /* @__PURE__ */ new Map();
|
|
41
|
+
const e = this.destroy.bind(this);
|
|
42
|
+
this.once("error", p), t.once("error", e), t.once("close", e), t.once("aborted", () => {
|
|
43
|
+
this.emit("aborted");
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
get aborted() {
|
|
47
|
+
return this.socket.aborted;
|
|
48
|
+
}
|
|
49
|
+
get finished() {
|
|
50
|
+
return this.socket.writableEnded && !this.socket.aborted;
|
|
51
|
+
}
|
|
52
|
+
get status() {
|
|
53
|
+
return `${this.statusCode} ${this.statusMessage || u[this.statusCode]}`;
|
|
54
|
+
}
|
|
55
|
+
get bytesWritten() {
|
|
56
|
+
return this.socket.bytesWritten;
|
|
57
|
+
}
|
|
58
|
+
hasHeader(t) {
|
|
59
|
+
return this[n].has(t.toLowerCase());
|
|
60
|
+
}
|
|
61
|
+
getHeader(t) {
|
|
62
|
+
return this[n].get(t.toLowerCase())?.value;
|
|
63
|
+
}
|
|
64
|
+
getHeaders() {
|
|
65
|
+
const t = {};
|
|
66
|
+
return this[n].forEach((e, s) => {
|
|
67
|
+
t[s] = e.value;
|
|
68
|
+
}), t;
|
|
69
|
+
}
|
|
70
|
+
setHeader(t, e) {
|
|
71
|
+
if (this.headersSent) throw new o();
|
|
72
|
+
t = t.replace(/[\r\n]/g, "");
|
|
73
|
+
const s = t.toLowerCase();
|
|
74
|
+
return s === "content-length" ? (this.contentLength = Number(e), this) : s === "transfer-encoding" ? (this.chunked = String(e).includes("chunked"), this) : (typeof e == "string" ? e = e.replace(/[\r\n]/g, "") : Array.isArray(e) && (e = e.map((r) => typeof r == "string" ? r.replace(/[\r\n]/g, "") : r)), this[n].set(s, new c(t, e)), this);
|
|
75
|
+
}
|
|
76
|
+
removeHeader(t) {
|
|
77
|
+
if (this.headersSent) throw new o();
|
|
78
|
+
this[n].delete(t.toLowerCase());
|
|
79
|
+
}
|
|
80
|
+
writeHead(t, e, s) {
|
|
81
|
+
if (this.headersSent) throw new o();
|
|
82
|
+
if (this.statusCode = t, typeof e == "object" ? s = e : e && (this.statusMessage = typeof e == "string" ? e.replace(/[\r\n]/g, "") : e), s)
|
|
83
|
+
for (const r of Object.keys(s))
|
|
84
|
+
this.setHeader(r, s[r]);
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
end(t, e, s) {
|
|
88
|
+
if (typeof t == "function" ? (s = t, t = void 0) : typeof e == "function" && (s = e), this.aborted)
|
|
89
|
+
return s && process.nextTick(s), this;
|
|
90
|
+
if (this.destroyed) throw new h();
|
|
91
|
+
return this.writableEnded = !0, s && this.once("finish", s), super.end(f(t, !0)), this;
|
|
92
|
+
}
|
|
93
|
+
addTrailers() {
|
|
94
|
+
}
|
|
95
|
+
destroy(t) {
|
|
96
|
+
return this.destroyed || this.destroying || this.aborted ? this : (this.socket.destroy(t), this);
|
|
97
|
+
}
|
|
98
|
+
write(t, e, s) {
|
|
99
|
+
if (typeof e == "function" && (s = e), this.aborted)
|
|
100
|
+
return s && process.nextTick(s), !1;
|
|
101
|
+
if (this.destroyed) throw new h();
|
|
102
|
+
const r = f(t);
|
|
103
|
+
return this.firstChunk && this.contentLength !== null && this.contentLength === r.byteLength ? (r.end = !0, this.writableEnded = !0, super.end(r), s && process.nextTick(s), !0) : (this.firstChunk = !1, this.headersSent || (this.headersSent = !0, this.socket[d] = {
|
|
104
|
+
headers: this[n],
|
|
105
|
+
status: this.status
|
|
106
|
+
}), this.socket.write(r, null, () => {
|
|
107
|
+
this._boundEmitDrain(), s && s();
|
|
108
|
+
}), !this.socket.writableNeedDrain);
|
|
109
|
+
}
|
|
110
|
+
_emitDrain() {
|
|
111
|
+
this.emit("drain");
|
|
112
|
+
}
|
|
113
|
+
_write(t, e) {
|
|
114
|
+
if (this.aborted) return e();
|
|
115
|
+
if (this.headersSent || (this.headersSent = !0, this.socket[d] = {
|
|
116
|
+
headers: this[n],
|
|
117
|
+
status: this.status
|
|
118
|
+
}), t.end) {
|
|
119
|
+
this.socket.end(t, null, e);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
this.socket.write(t, null, e);
|
|
123
|
+
}
|
|
124
|
+
_destroy(t) {
|
|
125
|
+
if (this.socket.destroyed) return t();
|
|
126
|
+
this.socket.once("close", t);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
export {
|
|
130
|
+
k as Response
|
|
131
|
+
};
|
package/dist/server.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const k=require("uWebSockets.js"),E=require("node:dns/promises"),S=require("node:fs"),g=require("node:http"),y=require("node:os"),L=require("node:path"),R=require("node:events"),q=require("ipaddr.js"),u=require("./errors.cjs"),w=require("./http-socket.cjs"),D=require("./request.cjs"),H=require("./response.cjs"),e=require("./symbols.cjs");function T(t,s,r){if(t!=null){if(Array.isArray(t)){if(t.length===0)return;if(t.length>1)throw new Error(`fastify-uws: multiple ${s} values are not supported by uWebSockets.js`);t=t[0]}if(typeof t=="string")return t;if(Buffer.isBuffer(t)){const i=L.join(y.tmpdir(),`fastify-uws-${s}-${process.pid}-${Date.now()}.pem`);return S.writeFileSync(i,t,{mode:384}),r.push(i),i}}}function C(t){if(t.https){const s=[],r=T(t.https.key,"key",s),i=T(t.https.cert,"cert",s),o=T(t.https.ca,"ca",s),c={};return r&&(c.key_file_name=r),i&&(c.cert_file_name=i),o&&(c.ca_file_name=o),t.https.passphrase&&(c.passphrase=t.https.passphrase),{app:k.SSLApp(c),tempFiles:s}}return{app:k.App(),tempFiles:[]}}const v=new Map(g.METHODS.map(t=>[t.toLowerCase(),t])),h={};class F extends R.EventEmitter{[e.kHandler];timeout=0;drainTimeout;maxHeadersCount=0;maxRequestsPerSocket=0;headersTimeout=0;keepAliveTimeout=0;requestTimeout=0;[e.kHttps];[e.kWs];[e.kAddress]=null;[e.kListenSocket]=null;[e.kApp];[e.kClosed]=!1;[e.kListenAll]=!1;[e.kListening]=!1;[e.kTempFiles];constructor(s,r={}){super();const{http2:i=!1,https:o=null,connectionTimeout:c=0,drainTimeout:a}=r;this[e.kHandler]=s,this.timeout=c,this.drainTimeout=a,this[e.kHttps]=o,this[e.kWs]=null;const{app:l,tempFiles:f}=C({https:o});this[e.kApp]=l,this[e.kTempFiles]=f}get encrypted(){return!!this[e.kHttps]}get listening(){return!!this[e.kListening]}setTimeout(s,r){return this.timeout=s,r&&this.once("timeout",r),this}address(){return this[e.kAddress]}listen(s,r){s?.signal&&s.signal.addEventListener("abort",()=>{this.close()},{once:!0}),this[e.kListen](s).then(()=>{this[e.kListening]=!0,r?.(),this.emit("listening")}).catch(i=>{this[e.kAddress]=null,process.nextTick(()=>this.emit("error",i))})}closeIdleConnections(){}closeAllConnections(){if(this[e.kWs])for(const s of this[e.kWs].connections)s.close()}close(s=()=>{}){if(this[e.kClosed])return s();const r=this[e.kAddress]?.port;r!==void 0&&h[r]===this&&delete h[r],this[e.kAddress]=null,this[e.kListening]=!1,this[e.kClosed]=!0,this[e.kListenSocket]&&(k.us_listen_socket_close(this[e.kListenSocket]),this[e.kListenSocket]=null),this.closeAllConnections();for(const i of this[e.kTempFiles])try{S.unlinkSync(i)}catch{}this[e.kTempFiles]=[],process.nextTick(()=>{this.emit("close"),s()})}ref(){}unref(){}async[e.kListen]({port:s,host:r}){if(this[e.kClosed])throw new u.ERR_SERVER_DESTROYED;if(s!=null&&Number.isNaN(Number(s)))throw new u.ERR_SOCKET_BAD_PORT(s);let i=s==null?0:Number(s);const o=await E.lookup(r);if(this[e.kAddress]={address:o.address,family:o.family===6?"IPv6":"IPv4",port:i},this[e.kAddress].address.startsWith("["))throw new u.ERR_ENOTFOUND(this[e.kAddress].address);const a=q.parse(this[e.kAddress].address).toNormalizedString(),l=this[e.kApp],f=(m,p)=>{const d=v.get(p.getMethod()),n=new w.HTTPSocket(this,m,d==="GET"||d==="HEAD");if(!d){n[e.kClientError]=!0,this.emit("clientError",new u.ERR_INVALID_METHOD,n);return}const A=new D.Request(p,n,d),_=new H.Response(n);A.headers.upgrade&&this.emit("upgrade",A,n),this[e.kHandler](A,_)};return l.any("/*",f),i!==0&&h[i]&&(this[e.kWs]=h[i][e.kWs]),this[e.kWs]&&this[e.kWs].addServer(this),new Promise((m,p)=>{const d=n=>{if(!n)return p(new u.ERR_ADDRINUSE(this[e.kAddress].address,i));this[e.kListenSocket]=n,i=this[e.kAddress].port=k.us_socket_local_port(n),h[i]||(h[i]=this),m()};this[e.kListenAll]=r==="localhost",this[e.kListenAll]?l.listen(i,d):l.listen(a,i,d)})}}exports.Server=F;
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { default as uws } from 'uWebSockets.js';
|
|
2
|
+
import { ServerOptions } from 'node:https';
|
|
3
|
+
import { AddressInfo } from 'node:net';
|
|
4
|
+
import { EventEmitter } from 'node:events';
|
|
5
|
+
import { FastifyServerFactoryHandler, FastifyServerOptions } from 'fastify';
|
|
6
|
+
import { kAddress, kApp, kClosed, kHandler, kHttps, kListen, kListenAll, kListening, kListenSocket, kTempFiles, kWs } from './symbols';
|
|
7
|
+
import { WebSocketServer } from './websocket-server';
|
|
8
|
+
interface FastifyUwsOptions extends FastifyServerOptions {
|
|
9
|
+
http2?: boolean;
|
|
10
|
+
https?: ServerOptions | null;
|
|
11
|
+
drainTimeout?: number;
|
|
12
|
+
}
|
|
13
|
+
export declare class Server extends EventEmitter {
|
|
14
|
+
[kHandler]: FastifyServerFactoryHandler;
|
|
15
|
+
timeout: number;
|
|
16
|
+
drainTimeout?: number;
|
|
17
|
+
maxHeadersCount: number;
|
|
18
|
+
maxRequestsPerSocket: number;
|
|
19
|
+
headersTimeout: number;
|
|
20
|
+
keepAliveTimeout: number;
|
|
21
|
+
requestTimeout: number;
|
|
22
|
+
[kHttps]?: FastifyUwsOptions['https'];
|
|
23
|
+
[kWs]?: WebSocketServer | null;
|
|
24
|
+
[kAddress]: AddressInfo | null;
|
|
25
|
+
[kListenSocket]: uws.us_listen_socket | null;
|
|
26
|
+
[kApp]: uws.TemplatedApp;
|
|
27
|
+
[kClosed]: boolean;
|
|
28
|
+
[kListenAll]: boolean;
|
|
29
|
+
[kListening]: boolean;
|
|
30
|
+
[kTempFiles]: string[];
|
|
31
|
+
constructor(handler: FastifyServerFactoryHandler, opts?: FastifyUwsOptions);
|
|
32
|
+
get encrypted(): boolean;
|
|
33
|
+
get listening(): boolean;
|
|
34
|
+
setTimeout(timeout: number, cb?: () => void): this;
|
|
35
|
+
address(): AddressInfo | string | null;
|
|
36
|
+
listen(listenOptions: {
|
|
37
|
+
host: string;
|
|
38
|
+
port: number;
|
|
39
|
+
signal?: AbortSignal;
|
|
40
|
+
}, cb?: () => void): void;
|
|
41
|
+
closeIdleConnections(): void;
|
|
42
|
+
closeAllConnections(): void;
|
|
43
|
+
close(cb?: () => void): void;
|
|
44
|
+
ref(): void;
|
|
45
|
+
unref(): void;
|
|
46
|
+
[kListen]({ port: rawPort, host }: {
|
|
47
|
+
port?: number;
|
|
48
|
+
host: string;
|
|
49
|
+
}): Promise<void>;
|
|
50
|
+
}
|
|
51
|
+
export {};
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import E from "uWebSockets.js";
|
|
2
|
+
import H from "node:dns/promises";
|
|
3
|
+
import N from "node:fs";
|
|
4
|
+
import { METHODS as O } from "node:http";
|
|
5
|
+
import v from "node:os";
|
|
6
|
+
import F from "node:path";
|
|
7
|
+
import { EventEmitter as I } from "node:events";
|
|
8
|
+
import x from "ipaddr.js";
|
|
9
|
+
import { ERR_SERVER_DESTROYED as M, ERR_SOCKET_BAD_PORT as q, ERR_ENOTFOUND as B, ERR_INVALID_METHOD as $, ERR_ADDRINUSE as b } from "./errors.js";
|
|
10
|
+
import { HTTPSocket as P } from "./http-socket.js";
|
|
11
|
+
import { Request as V } from "./request.js";
|
|
12
|
+
import { Response as W } from "./response.js";
|
|
13
|
+
import { kHandler as y, kHttps as g, kWs as p, kAddress as r, kListenSocket as m, kApp as w, kClosed as a, kListenAll as S, kListening as d, kTempFiles as k, kListen as L, kClientError as j } from "./symbols.js";
|
|
14
|
+
function D(e, t, i) {
|
|
15
|
+
if (e != null) {
|
|
16
|
+
if (Array.isArray(e)) {
|
|
17
|
+
if (e.length === 0) return;
|
|
18
|
+
if (e.length > 1)
|
|
19
|
+
throw new Error(`fastify-uws: multiple ${t} values are not supported by uWebSockets.js`);
|
|
20
|
+
e = e[0];
|
|
21
|
+
}
|
|
22
|
+
if (typeof e == "string") return e;
|
|
23
|
+
if (Buffer.isBuffer(e)) {
|
|
24
|
+
const s = F.join(v.tmpdir(), `fastify-uws-${t}-${process.pid}-${Date.now()}.pem`);
|
|
25
|
+
return N.writeFileSync(s, e, { mode: 384 }), i.push(s), s;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function U(e) {
|
|
30
|
+
if (e.https) {
|
|
31
|
+
const t = [], i = D(
|
|
32
|
+
e.https.key,
|
|
33
|
+
"key",
|
|
34
|
+
t
|
|
35
|
+
), s = D(
|
|
36
|
+
e.https.cert,
|
|
37
|
+
"cert",
|
|
38
|
+
t
|
|
39
|
+
), n = D(
|
|
40
|
+
e.https.ca,
|
|
41
|
+
"ca",
|
|
42
|
+
t
|
|
43
|
+
), h = {};
|
|
44
|
+
return i && (h.key_file_name = i), s && (h.cert_file_name = s), n && (h.ca_file_name = n), e.https.passphrase && (h.passphrase = e.https.passphrase), { app: E.SSLApp(h), tempFiles: t };
|
|
45
|
+
}
|
|
46
|
+
return { app: E.App(), tempFiles: [] };
|
|
47
|
+
}
|
|
48
|
+
const z = new Map(O.map((e) => [e.toLowerCase(), e])), f = {};
|
|
49
|
+
class nt extends I {
|
|
50
|
+
[y];
|
|
51
|
+
timeout = 0;
|
|
52
|
+
drainTimeout;
|
|
53
|
+
maxHeadersCount = 0;
|
|
54
|
+
maxRequestsPerSocket = 0;
|
|
55
|
+
headersTimeout = 0;
|
|
56
|
+
keepAliveTimeout = 0;
|
|
57
|
+
requestTimeout = 0;
|
|
58
|
+
[g];
|
|
59
|
+
[p];
|
|
60
|
+
[r] = null;
|
|
61
|
+
[m] = null;
|
|
62
|
+
[w];
|
|
63
|
+
[a] = !1;
|
|
64
|
+
[S] = !1;
|
|
65
|
+
[d] = !1;
|
|
66
|
+
[k];
|
|
67
|
+
constructor(t, i = {}) {
|
|
68
|
+
super();
|
|
69
|
+
const { http2: s = !1, https: n = null, connectionTimeout: h = 0, drainTimeout: _ } = i;
|
|
70
|
+
this[y] = t, this.timeout = h, this.drainTimeout = _, this[g] = n, this[p] = null;
|
|
71
|
+
const { app: l, tempFiles: T } = U({ https: n });
|
|
72
|
+
this[w] = l, this[k] = T;
|
|
73
|
+
}
|
|
74
|
+
get encrypted() {
|
|
75
|
+
return !!this[g];
|
|
76
|
+
}
|
|
77
|
+
get listening() {
|
|
78
|
+
return !!this[d];
|
|
79
|
+
}
|
|
80
|
+
setTimeout(t, i) {
|
|
81
|
+
return this.timeout = t, i && this.once("timeout", i), this;
|
|
82
|
+
}
|
|
83
|
+
address() {
|
|
84
|
+
return this[r];
|
|
85
|
+
}
|
|
86
|
+
listen(t, i) {
|
|
87
|
+
t?.signal && t.signal.addEventListener(
|
|
88
|
+
"abort",
|
|
89
|
+
() => {
|
|
90
|
+
this.close();
|
|
91
|
+
},
|
|
92
|
+
{ once: !0 }
|
|
93
|
+
), this[L](t).then(() => {
|
|
94
|
+
this[d] = !0, i?.(), this.emit("listening");
|
|
95
|
+
}).catch((s) => {
|
|
96
|
+
this[r] = null, process.nextTick(() => this.emit("error", s));
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
closeIdleConnections() {
|
|
100
|
+
}
|
|
101
|
+
closeAllConnections() {
|
|
102
|
+
if (this[p])
|
|
103
|
+
for (const t of this[p].connections)
|
|
104
|
+
t.close();
|
|
105
|
+
}
|
|
106
|
+
close(t = () => {
|
|
107
|
+
}) {
|
|
108
|
+
if (this[a]) return t();
|
|
109
|
+
const i = this[r]?.port;
|
|
110
|
+
i !== void 0 && f[i] === this && delete f[i], this[r] = null, this[d] = !1, this[a] = !0, this[m] && (E.us_listen_socket_close(this[m]), this[m] = null), this.closeAllConnections();
|
|
111
|
+
for (const s of this[k])
|
|
112
|
+
try {
|
|
113
|
+
N.unlinkSync(s);
|
|
114
|
+
} catch {
|
|
115
|
+
}
|
|
116
|
+
this[k] = [], process.nextTick(() => {
|
|
117
|
+
this.emit("close"), t();
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
ref() {
|
|
121
|
+
}
|
|
122
|
+
unref() {
|
|
123
|
+
}
|
|
124
|
+
async [L]({ port: t, host: i }) {
|
|
125
|
+
if (this[a]) throw new M();
|
|
126
|
+
if (t != null && Number.isNaN(Number(t)))
|
|
127
|
+
throw new q(t);
|
|
128
|
+
let s = t == null ? 0 : Number(t);
|
|
129
|
+
const n = await H.lookup(i);
|
|
130
|
+
if (this[r] = {
|
|
131
|
+
address: n.address,
|
|
132
|
+
family: n.family === 6 ? "IPv6" : "IPv4",
|
|
133
|
+
port: s
|
|
134
|
+
}, this[r].address.startsWith("[")) throw new B(this[r].address);
|
|
135
|
+
const _ = x.parse(this[r].address).toNormalizedString(), l = this[w], T = (A, u) => {
|
|
136
|
+
const c = z.get(u.getMethod()), o = new P(this, A, c === "GET" || c === "HEAD");
|
|
137
|
+
if (!c) {
|
|
138
|
+
o[j] = !0, this.emit("clientError", new $(), o);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const R = new V(u, o, c), C = new W(o);
|
|
142
|
+
R.headers.upgrade && this.emit("upgrade", R, o), this[y](R, C);
|
|
143
|
+
};
|
|
144
|
+
return l.any("/*", T), s !== 0 && f[s] && (this[p] = f[s][p]), this[p] && this[p].addServer(this), new Promise((A, u) => {
|
|
145
|
+
const c = (o) => {
|
|
146
|
+
if (!o) return u(new b(this[r].address, s));
|
|
147
|
+
this[m] = o, s = this[r].port = E.us_socket_local_port(o), f[s] || (f[s] = this), A();
|
|
148
|
+
};
|
|
149
|
+
this[S] = i === "localhost", this[S] ? l.listen(s, c) : l.listen(_, s, c);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
export {
|
|
154
|
+
nt as Server
|
|
155
|
+
};
|
package/dist/symbols.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=Symbol("uws.https"),e=Symbol("uws.res"),o=Symbol("uws.server"),t=Symbol("uws.headers"),n=Symbol("uws.url"),l=Symbol("uws.address"),k=Symbol("uws.remoteAddress"),d=Symbol("uws.encoding"),r=Symbol("uws.timeoutRef"),c=Symbol("uws.ended"),m=Symbol("uws.readyState"),i=Symbol("uws.writeOnly"),S=Symbol("uws.handler"),u=Symbol("uws.listenSocket"),y=Symbol("uws.listen"),w=Symbol("uws.app"),b=Symbol("uws.closed"),a=Symbol("uws.ws"),p=Symbol("uws.topic"),A=Symbol("uws.uwsRemoteAddress"),R=Symbol("uws.head"),g=Symbol("uws.listenAll"),H=Symbol("uws.listening"),L=Symbol("uws.clientError"),E=Symbol("uws.tempFiles");exports.kAddress=l;exports.kApp=w;exports.kClientError=L;exports.kClosed=b;exports.kEncoding=d;exports.kEnded=c;exports.kHandler=S;exports.kHead=R;exports.kHeaders=t;exports.kHttps=s;exports.kListen=y;exports.kListenAll=g;exports.kListenSocket=u;exports.kListening=H;exports.kReadyState=m;exports.kRemoteAddress=k;exports.kRes=e;exports.kServer=o;exports.kTempFiles=E;exports.kTimeoutRef=r;exports.kTopic=p;exports.kUrl=n;exports.kUwsRemoteAddress=A;exports.kWriteOnly=i;exports.kWs=a;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export declare const kHttps: unique symbol;
|
|
2
|
+
export declare const kRes: unique symbol;
|
|
3
|
+
export declare const kServer: unique symbol;
|
|
4
|
+
export declare const kHeaders: unique symbol;
|
|
5
|
+
export declare const kUrl: unique symbol;
|
|
6
|
+
export declare const kAddress: unique symbol;
|
|
7
|
+
export declare const kRemoteAddress: unique symbol;
|
|
8
|
+
export declare const kEncoding: unique symbol;
|
|
9
|
+
export declare const kTimeoutRef: unique symbol;
|
|
10
|
+
export declare const kEnded: unique symbol;
|
|
11
|
+
export declare const kReadyState: unique symbol;
|
|
12
|
+
export declare const kWriteOnly: unique symbol;
|
|
13
|
+
export declare const kHandler: unique symbol;
|
|
14
|
+
export declare const kListenSocket: unique symbol;
|
|
15
|
+
export declare const kListen: unique symbol;
|
|
16
|
+
export declare const kApp: unique symbol;
|
|
17
|
+
export declare const kClosed: unique symbol;
|
|
18
|
+
export declare const kWs: unique symbol;
|
|
19
|
+
export declare const kTopic: unique symbol;
|
|
20
|
+
export declare const kDestroyError: unique symbol;
|
|
21
|
+
export declare const kUwsRemoteAddress: unique symbol;
|
|
22
|
+
export declare const kHead: unique symbol;
|
|
23
|
+
export declare const kWebSocketOptions: unique symbol;
|
|
24
|
+
export declare const kListenAll: unique symbol;
|
|
25
|
+
export declare const kListening: unique symbol;
|
|
26
|
+
export declare const kClientError: unique symbol;
|
|
27
|
+
export declare const kTempFiles: unique symbol;
|
package/dist/symbols.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const s = /* @__PURE__ */ Symbol("uws.https"), o = /* @__PURE__ */ Symbol("uws.res"), e = /* @__PURE__ */ Symbol("uws.server"), t = /* @__PURE__ */ Symbol("uws.headers"), n = /* @__PURE__ */ Symbol("uws.url"), l = /* @__PURE__ */ Symbol("uws.address"), c = /* @__PURE__ */ Symbol("uws.remoteAddress"), m = /* @__PURE__ */ Symbol("uws.encoding"), S = /* @__PURE__ */ Symbol("uws.timeoutRef"), u = /* @__PURE__ */ Symbol("uws.ended"), w = /* @__PURE__ */ Symbol("uws.readyState"), y = /* @__PURE__ */ Symbol("uws.writeOnly"), d = /* @__PURE__ */ Symbol("uws.handler"), r = /* @__PURE__ */ Symbol("uws.listenSocket"), k = /* @__PURE__ */ Symbol("uws.listen"), b = /* @__PURE__ */ Symbol("uws.app"), i = /* @__PURE__ */ Symbol("uws.closed"), a = /* @__PURE__ */ Symbol("uws.ws"), p = /* @__PURE__ */ Symbol("uws.topic"), A = /* @__PURE__ */ Symbol("uws.uwsRemoteAddress"), R = /* @__PURE__ */ Symbol("uws.head"), g = /* @__PURE__ */ Symbol("uws.listenAll"), h = /* @__PURE__ */ Symbol("uws.listening"), E = /* @__PURE__ */ Symbol("uws.clientError"), H = /* @__PURE__ */ Symbol("uws.tempFiles");
|
|
2
|
+
export {
|
|
3
|
+
l as kAddress,
|
|
4
|
+
b as kApp,
|
|
5
|
+
E as kClientError,
|
|
6
|
+
i as kClosed,
|
|
7
|
+
m as kEncoding,
|
|
8
|
+
u as kEnded,
|
|
9
|
+
d as kHandler,
|
|
10
|
+
R as kHead,
|
|
11
|
+
t as kHeaders,
|
|
12
|
+
s as kHttps,
|
|
13
|
+
k as kListen,
|
|
14
|
+
g as kListenAll,
|
|
15
|
+
r as kListenSocket,
|
|
16
|
+
h as kListening,
|
|
17
|
+
w as kReadyState,
|
|
18
|
+
c as kRemoteAddress,
|
|
19
|
+
o as kRes,
|
|
20
|
+
e as kServer,
|
|
21
|
+
H as kTempFiles,
|
|
22
|
+
S as kTimeoutRef,
|
|
23
|
+
p as kTopic,
|
|
24
|
+
n as kUrl,
|
|
25
|
+
A as kUwsRemoteAddress,
|
|
26
|
+
y as kWriteOnly,
|
|
27
|
+
a as kWs
|
|
28
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=require("uWebSockets.js"),f=require("eventemitter3"),p=require("streamx"),g=require("./http-socket.cjs"),y=require("./request.cjs"),E=require("./response.cjs"),r=require("./symbols.cjs"),S={compression:m.SHARED_COMPRESSOR,maxPayloadLength:16*1024*1024,idleTimeout:16},l="!",B=Buffer.from(l);class h extends f.EventEmitter{namespace;connection;topics;[r.kEnded];static allocTopic(e,t){if(t[r.kTopic])return t;const o=Buffer.concat([e,B,Buffer.isBuffer(t)?t:Buffer.from(t)]);return o[r.kTopic]=!0,o}constructor(e,t,o={}){super(),this.namespace=e,this.connection=t,t.websocket=this,this.topics=o,this[r.kEnded]=!1}get uws(){return!0}allocTopic(e){return this.topics[e]?this.topics[e]:h.allocTopic(this.namespace,e)}send(e,t,o){if(!this[r.kEnded])return this.connection.send(e,t,o)}publish(e,t,o,s){if(!this[r.kEnded])return this.connection.publish(this.allocTopic(e),t,o,s)}subscribe(e){if(!this[r.kEnded])return this.connection.subscribe(this.allocTopic(e))}unsubscribe(e){if(!this[r.kEnded])return this.connection.unsubscribe(this.allocTopic(e))}isSubscribed(e){return this[r.kEnded]?!1:this.connection.isSubscribed(this.allocTopic(e))}getTopics(){return this[r.kEnded]?[]:this.connection.getTopics().map(e=>e.slice(e.indexOf(l)+1))}close(){if(!this[r.kEnded])return this[r.kEnded]=!0,this.connection.close()}end(e,t){if(!this[r.kEnded])return this[r.kEnded]=!0,this.connection.end(e,t)}cork(e){if(!this[r.kEnded])return this.connection.cork(e)}getBufferedAmount(){return this[r.kEnded]?0:this.connection.getBufferedAmount()}ping(e){if(!this[r.kEnded])return this.connection.ping(e)}}class W extends p.Duplex{socket;constructor(e,t={}){const{compress:o=!1}=t;super({highWaterMark:t.highWaterMark,mapReadable:s=>t.mapReadable?t.mapReadable(s):s.data,byteLengthReadable:s=>t.byteLengthReadable?t.byteLengthReadable(s):s.isBinary?s.data.byteLength:1024,mapWritable:s=>t.mapWritable?t.mapWritable(s):{data:s,isBinary:Buffer.isBuffer(s),compress:o},byteLengthWritable:s=>t.byteLengthWritable?t.byteLengthWritable(s):s.isBinary?s.data.byteLength:1024}),this.socket=e,this._onMessage=this._onMessage.bind(this)}_open(e){this.socket.on("message",this._onMessage),e()}_close(e){this.socket.off("message",this._onMessage),this.socket.close(),e()}_onMessage(e,t){this.push({data:e,isBinary:t})}_write(e,t){const o=this.socket.send(e.data,e.isBinary,e.compress);if(o===1)return t();if(o===0){const s=()=>{this.socket.off("close",n),t()},n=()=>{this.socket.off("drain",s),t(new Error("WebSocket closed before drain"))};this.socket.once("drain",s),this.socket.once("close",n);return}if(o===2)return t(new Error("WebSocket send dropped due to backpressure limit"));t(new Error("WebSocket is closed"))}}class T extends f.EventEmitter{options;connections;constructor(e={}){super(),this.options={...S,...e},this.connections=new Set}addServer(e){const{options:t}=this,o=e[r.kApp],s=e[r.kHandler];o.ws("/*",{upgrade:(n,i,c)=>{const u=i.getMethod().toUpperCase(),a=new g.HTTPSocket(e,n,u==="GET"||u==="HEAD"),d=new y.Request(i,a,u),k=new E.Response(a);d[r.kWs]=c,e.emit("upgrade",d,a),s(d,k)},open:n=>{this.connections.add(n),n.handler(n),this.emit("open",n)},close:(n,i,c)=>{this.connections.delete(n);const u=n,a=u.websocket;a&&(a[r.kEnded]=!0),u.req.socket.destroy();const d=c instanceof ArrayBuffer?Buffer.copyBytesFrom(new Uint8Array(c)):c;a?.emit("close",i,d),this.emit("close",n,i,d)},drain:n=>{n.websocket?.emit("drain"),this.emit("drain",n)},message:(n,i,c)=>{const u=i instanceof ArrayBuffer?Buffer.copyBytesFrom(new Uint8Array(i)):i;n.websocket?.emit("message",u,c),this.emit("message",n,u,c)},ping:(n,i)=>{const c=i instanceof ArrayBuffer?Buffer.copyBytesFrom(new Uint8Array(i)):i;n.websocket?.emit("ping",c),this.emit("ping",n,c)},pong:(n,i)=>{const c=i instanceof ArrayBuffer?Buffer.copyBytesFrom(new Uint8Array(i)):i;n.websocket?.emit("pong",c),this.emit("pong",n,c)},...t})}}exports.WebSocket=h;exports.WebSocketServer=T;exports.WebSocketStream=W;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { default as uws } from 'uWebSockets.js';
|
|
2
|
+
import { EventEmitter } from 'eventemitter3';
|
|
3
|
+
import { Duplex } from 'streamx';
|
|
4
|
+
import { Request } from './request';
|
|
5
|
+
import { Server } from './server';
|
|
6
|
+
import { kEnded } from './symbols';
|
|
7
|
+
declare const defaultWebSocketConfig: {
|
|
8
|
+
compression: number;
|
|
9
|
+
maxPayloadLength: number;
|
|
10
|
+
idleTimeout: number;
|
|
11
|
+
};
|
|
12
|
+
export interface WsUserData {
|
|
13
|
+
req: Request;
|
|
14
|
+
handler: (ws: uws.WebSocket<WsUserData>) => void;
|
|
15
|
+
websocket?: WebSocket;
|
|
16
|
+
}
|
|
17
|
+
export declare class WebSocket extends EventEmitter {
|
|
18
|
+
namespace: Buffer;
|
|
19
|
+
connection: uws.WebSocket<WsUserData>;
|
|
20
|
+
topics: Record<string, Buffer>;
|
|
21
|
+
[kEnded]: boolean;
|
|
22
|
+
static allocTopic(namespace: Buffer, topic: Buffer | string): string | Buffer<ArrayBufferLike>;
|
|
23
|
+
constructor(namespace: Buffer, connection: uws.WebSocket<WsUserData>, topics?: Record<string, Buffer>);
|
|
24
|
+
get uws(): boolean;
|
|
25
|
+
allocTopic(topic: Buffer | string): string | Buffer<ArrayBufferLike>;
|
|
26
|
+
send(message: uws.RecognizedString, isBinary: boolean, compress: boolean): number | undefined;
|
|
27
|
+
publish(topic: Buffer | string, message: uws.RecognizedString, isBinary: boolean, compress: boolean): boolean | undefined;
|
|
28
|
+
subscribe(topic: Buffer | string): boolean | undefined;
|
|
29
|
+
unsubscribe(topic: Buffer | string): boolean | undefined;
|
|
30
|
+
isSubscribed(topic: Buffer | string): boolean;
|
|
31
|
+
getTopics(): string[];
|
|
32
|
+
close(): void;
|
|
33
|
+
end(code: number, shortMessage: uws.RecognizedString): void;
|
|
34
|
+
cork(cb: () => void): uws.WebSocket<WsUserData> | undefined;
|
|
35
|
+
getBufferedAmount(): number;
|
|
36
|
+
ping(message: uws.RecognizedString): number | undefined;
|
|
37
|
+
}
|
|
38
|
+
export declare class WebSocketStream extends Duplex {
|
|
39
|
+
socket: WebSocket;
|
|
40
|
+
constructor(socket: WebSocket, opts?: {
|
|
41
|
+
compress?: boolean | false;
|
|
42
|
+
highWaterMark?: number | 16384;
|
|
43
|
+
mapReadable?: (packet: {
|
|
44
|
+
data: any;
|
|
45
|
+
isBinary: boolean;
|
|
46
|
+
}) => any;
|
|
47
|
+
byteLengthReadable?: (packet: {
|
|
48
|
+
data: any;
|
|
49
|
+
isBinary: boolean;
|
|
50
|
+
}) => number | 1024;
|
|
51
|
+
mapWritable?: (data: any) => {
|
|
52
|
+
data: any;
|
|
53
|
+
isBinary: boolean;
|
|
54
|
+
compress: boolean;
|
|
55
|
+
};
|
|
56
|
+
byteLengthWritable?: (packet: {
|
|
57
|
+
data: any;
|
|
58
|
+
isBinary: boolean;
|
|
59
|
+
compress: boolean;
|
|
60
|
+
}) => number | 1024;
|
|
61
|
+
});
|
|
62
|
+
_open(cb: () => void): void;
|
|
63
|
+
_close(cb: () => void): void;
|
|
64
|
+
_onMessage(data: any, isBinary: boolean): void;
|
|
65
|
+
_write(packet: any, cb: (err?: Error | null) => void): void;
|
|
66
|
+
}
|
|
67
|
+
type WSOptions = {
|
|
68
|
+
closeOnBackpressureLimit?: boolean;
|
|
69
|
+
compression?: number;
|
|
70
|
+
idleTimeout?: number;
|
|
71
|
+
maxBackpressure?: number;
|
|
72
|
+
maxLifetime?: number;
|
|
73
|
+
maxPayloadLength?: number;
|
|
74
|
+
sendPingsAutomatically?: boolean;
|
|
75
|
+
};
|
|
76
|
+
export declare class WebSocketServer extends EventEmitter {
|
|
77
|
+
options: WSOptions & typeof defaultWebSocketConfig;
|
|
78
|
+
connections: Set<uws.WebSocket<WsUserData>>;
|
|
79
|
+
constructor(options?: WSOptions);
|
|
80
|
+
addServer(server: Server): void;
|
|
81
|
+
}
|
|
82
|
+
export {};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import g from "uWebSockets.js";
|
|
2
|
+
import { EventEmitter as l } from "eventemitter3";
|
|
3
|
+
import { Duplex as k } from "streamx";
|
|
4
|
+
import { HTTPSocket as y } from "./http-socket.js";
|
|
5
|
+
import { Request as B } from "./request.js";
|
|
6
|
+
import { Response as S } from "./response.js";
|
|
7
|
+
import { kApp as W, kHandler as T, kEnded as o, kWs as w, kTopic as h } from "./symbols.js";
|
|
8
|
+
const A = {
|
|
9
|
+
compression: g.SHARED_COMPRESSOR,
|
|
10
|
+
maxPayloadLength: 16 * 1024 * 1024,
|
|
11
|
+
idleTimeout: 16
|
|
12
|
+
}, m = "!", E = Buffer.from(m);
|
|
13
|
+
class d extends l {
|
|
14
|
+
namespace;
|
|
15
|
+
connection;
|
|
16
|
+
topics;
|
|
17
|
+
[o];
|
|
18
|
+
static allocTopic(e, t) {
|
|
19
|
+
if (t[h]) return t;
|
|
20
|
+
const i = Buffer.concat([
|
|
21
|
+
e,
|
|
22
|
+
E,
|
|
23
|
+
Buffer.isBuffer(t) ? t : Buffer.from(t)
|
|
24
|
+
]);
|
|
25
|
+
return i[h] = !0, i;
|
|
26
|
+
}
|
|
27
|
+
constructor(e, t, i = {}) {
|
|
28
|
+
super(), this.namespace = e, this.connection = t, t.websocket = this, this.topics = i, this[o] = !1;
|
|
29
|
+
}
|
|
30
|
+
get uws() {
|
|
31
|
+
return !0;
|
|
32
|
+
}
|
|
33
|
+
allocTopic(e) {
|
|
34
|
+
return this.topics[e] ? this.topics[e] : d.allocTopic(this.namespace, e);
|
|
35
|
+
}
|
|
36
|
+
send(e, t, i) {
|
|
37
|
+
if (!this[o])
|
|
38
|
+
return this.connection.send(e, t, i);
|
|
39
|
+
}
|
|
40
|
+
publish(e, t, i, r) {
|
|
41
|
+
if (!this[o])
|
|
42
|
+
return this.connection.publish(this.allocTopic(e), t, i, r);
|
|
43
|
+
}
|
|
44
|
+
subscribe(e) {
|
|
45
|
+
if (!this[o])
|
|
46
|
+
return this.connection.subscribe(this.allocTopic(e));
|
|
47
|
+
}
|
|
48
|
+
unsubscribe(e) {
|
|
49
|
+
if (!this[o])
|
|
50
|
+
return this.connection.unsubscribe(this.allocTopic(e));
|
|
51
|
+
}
|
|
52
|
+
isSubscribed(e) {
|
|
53
|
+
return this[o] ? !1 : this.connection.isSubscribed(this.allocTopic(e));
|
|
54
|
+
}
|
|
55
|
+
getTopics() {
|
|
56
|
+
return this[o] ? [] : this.connection.getTopics().map((e) => e.slice(e.indexOf(m) + 1));
|
|
57
|
+
}
|
|
58
|
+
close() {
|
|
59
|
+
if (!this[o])
|
|
60
|
+
return this[o] = !0, this.connection.close();
|
|
61
|
+
}
|
|
62
|
+
end(e, t) {
|
|
63
|
+
if (!this[o])
|
|
64
|
+
return this[o] = !0, this.connection.end(e, t);
|
|
65
|
+
}
|
|
66
|
+
cork(e) {
|
|
67
|
+
if (!this[o])
|
|
68
|
+
return this.connection.cork(e);
|
|
69
|
+
}
|
|
70
|
+
getBufferedAmount() {
|
|
71
|
+
return this[o] ? 0 : this.connection.getBufferedAmount();
|
|
72
|
+
}
|
|
73
|
+
ping(e) {
|
|
74
|
+
if (!this[o])
|
|
75
|
+
return this.connection.ping(e);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
class D extends k {
|
|
79
|
+
socket;
|
|
80
|
+
constructor(e, t = {}) {
|
|
81
|
+
const { compress: i = !1 } = t;
|
|
82
|
+
super({
|
|
83
|
+
highWaterMark: t.highWaterMark,
|
|
84
|
+
mapReadable: (r) => t.mapReadable ? t.mapReadable(r) : r.data,
|
|
85
|
+
byteLengthReadable: (r) => t.byteLengthReadable ? t.byteLengthReadable(r) : r.isBinary ? r.data.byteLength : 1024,
|
|
86
|
+
mapWritable: (r) => t.mapWritable ? t.mapWritable(r) : { data: r, isBinary: Buffer.isBuffer(r), compress: i },
|
|
87
|
+
byteLengthWritable: (r) => t.byteLengthWritable ? t.byteLengthWritable(r) : r.isBinary ? r.data.byteLength : 1024
|
|
88
|
+
}), this.socket = e, this._onMessage = this._onMessage.bind(this);
|
|
89
|
+
}
|
|
90
|
+
_open(e) {
|
|
91
|
+
this.socket.on("message", this._onMessage), e();
|
|
92
|
+
}
|
|
93
|
+
_close(e) {
|
|
94
|
+
this.socket.off("message", this._onMessage), this.socket.close(), e();
|
|
95
|
+
}
|
|
96
|
+
_onMessage(e, t) {
|
|
97
|
+
this.push({ data: e, isBinary: t });
|
|
98
|
+
}
|
|
99
|
+
_write(e, t) {
|
|
100
|
+
const i = this.socket.send(e.data, e.isBinary, e.compress);
|
|
101
|
+
if (i === 1) return t();
|
|
102
|
+
if (i === 0) {
|
|
103
|
+
const r = () => {
|
|
104
|
+
this.socket.off("close", n), t();
|
|
105
|
+
}, n = () => {
|
|
106
|
+
this.socket.off("drain", r), t(new Error("WebSocket closed before drain"));
|
|
107
|
+
};
|
|
108
|
+
this.socket.once("drain", r), this.socket.once("close", n);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (i === 2)
|
|
112
|
+
return t(new Error("WebSocket send dropped due to backpressure limit"));
|
|
113
|
+
t(new Error("WebSocket is closed"));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
class H extends l {
|
|
117
|
+
options;
|
|
118
|
+
connections;
|
|
119
|
+
constructor(e = {}) {
|
|
120
|
+
super(), this.options = { ...A, ...e }, this.connections = /* @__PURE__ */ new Set();
|
|
121
|
+
}
|
|
122
|
+
addServer(e) {
|
|
123
|
+
const { options: t } = this, i = e[W], r = e[T];
|
|
124
|
+
i.ws("/*", {
|
|
125
|
+
upgrade: (n, s, c) => {
|
|
126
|
+
const a = s.getMethod().toUpperCase(), u = new y(e, n, a === "GET" || a === "HEAD"), f = new B(s, u, a), b = new S(u);
|
|
127
|
+
f[w] = c, e.emit("upgrade", f, u), r(f, b);
|
|
128
|
+
},
|
|
129
|
+
open: (n) => {
|
|
130
|
+
this.connections.add(n), n.handler(n), this.emit("open", n);
|
|
131
|
+
},
|
|
132
|
+
close: (n, s, c) => {
|
|
133
|
+
this.connections.delete(n);
|
|
134
|
+
const a = n, u = a.websocket;
|
|
135
|
+
u && (u[o] = !0), a.req.socket.destroy();
|
|
136
|
+
const f = c instanceof ArrayBuffer ? Buffer.copyBytesFrom(new Uint8Array(c)) : c;
|
|
137
|
+
u?.emit("close", s, f), this.emit("close", n, s, f);
|
|
138
|
+
},
|
|
139
|
+
drain: (n) => {
|
|
140
|
+
n.websocket?.emit("drain"), this.emit("drain", n);
|
|
141
|
+
},
|
|
142
|
+
message: (n, s, c) => {
|
|
143
|
+
const a = s instanceof ArrayBuffer ? Buffer.copyBytesFrom(new Uint8Array(s)) : s;
|
|
144
|
+
n.websocket?.emit("message", a, c), this.emit("message", n, a, c);
|
|
145
|
+
},
|
|
146
|
+
ping: (n, s) => {
|
|
147
|
+
const c = s instanceof ArrayBuffer ? Buffer.copyBytesFrom(new Uint8Array(s)) : s;
|
|
148
|
+
n.websocket?.emit("ping", c), this.emit("ping", n, c);
|
|
149
|
+
},
|
|
150
|
+
pong: (n, s) => {
|
|
151
|
+
const c = s instanceof ArrayBuffer ? Buffer.copyBytesFrom(new Uint8Array(s)) : s;
|
|
152
|
+
n.websocket?.emit("pong", c), this.emit("pong", n, c);
|
|
153
|
+
},
|
|
154
|
+
...t
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
export {
|
|
159
|
+
d as WebSocket,
|
|
160
|
+
H as WebSocketServer,
|
|
161
|
+
D as WebSocketStream
|
|
162
|
+
};
|