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