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