UNPKG

fastify-uws

Version:

A performant HTTP and WebSocket server for Fastify with uWebSockets.

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