UNPKG

fastify-uws

Version:

A performant HTTP and WebSocket server for Fastify with uWebSockets.

133 lines (132 loc) 3.88 kB
var l = Object.defineProperty; var w = (i, r, t) => r in i ? l(i, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[r] = t; var s = (i, r, t) => w(i, typeof r != "symbol" ? r + "" : r, t); import { STATUS_CODES as b } from "node:http"; import { Writable as y } from "streamx"; import { ERR_HEAD_SET as o, ERR_STREAM_DESTROYED as u } from "./errors.js"; import { kHeaders as h, kHead as k } from "./symbols.js"; class g { constructor(r, t) { s(this, "isMultiValue"); s(this, "name"); s(this, "value"); this.isMultiValue = Array.isArray(t), this.name = r, this.value = this.isMultiValue ? t : String(t); } } const p = Buffer.alloc(0); class a { constructor(r, t = !1) { s(this, "chunk"); s(this, "end"); s(this, "byteLength"); this.chunk = r || p, this.empty = !r, this.end = t, this.byteLength = this.empty ? 1 : Buffer.byteLength(this.chunk); } } function E() { this.emit("aborted"); } const L = () => { }, m = { byteLength(i) { return i.byteLength; } }; var c, f; class _ extends (f = y, c = h, f) { constructor(t) { super(m); s(this, "socket"); s(this, "statusCode"); s(this, "statusMessage"); s(this, "headersSent"); s(this, "chunked"); s(this, "contentLength"); s(this, "writableEnded"); s(this, "firstChunk"); s(this, c); this.socket = t, this.statusCode = 200, this.headersSent = !1, this.chunked = !1, this.contentLength = null, this.writableEnded = !1, this.firstChunk = !0, this[h] = /* @__PURE__ */ new Map(); const e = this.destroy.bind(this); this.once("error", L), t.once("error", e), t.once("close", e), t.once("aborted", E.bind(this)); } get aborted() { return this.socket.aborted; } get finished() { return this.socket.writableEnded && !this.socket.aborted; } get status() { return `${this.statusCode} ${this.statusMessage || b[this.statusCode]}`; } get bytesWritten() { return this.socket.bytesWritten; } hasHeader(t) { return this[h].has(t.toLowerCase()); } getHeader(t) { var e; return (e = this[h].get(t.toLowerCase())) == null ? void 0 : e.value; } getHeaders() { const t = {}; return this[h].forEach((e, n) => { t[n] = e.value; }), t; } setHeader(t, e) { if (this.headersSent) throw new o(); const n = t.toLowerCase(); if (n === "content-length") { this.contentLength = Number(e); return; } if (n === "transfer-encoding") { this.chunked = e.includes("chunked"); return; } this[h].set(n, new g(t, e)); } removeHeader(t) { if (this.headersSent) throw new o(); this[h].delete(t.toLowerCase()); } writeHead(t, e, n) { if (this.headersSent) throw new o(); if (this.statusCode = t, typeof e == "object" ? n = e : e && (this.statusMessage = e), n) for (const d of Object.keys(n)) this.setHeader(d, n[d]); } end(t) { if (!this.aborted) { if (this.destroyed) throw new u(); return this.writableEnded = !0, super.end(new a(t, !0)); } } destroy(t) { this.destroyed || this.destroying || this.aborted || this.socket.destroy(t); } write(t) { if (!this.aborted) { if (this.destroyed) throw new u(); return t = new a(t), this.firstChunk && this.contentLength !== null && this.contentLength === t.byteLength ? (t.end = !0, this.writableEnded = !0, super.end(t), !0) : (this.firstChunk = !1, super.write(t)); } } _write(t, e) { if (this.aborted) return e(); if (this.headersSent || (this.headersSent = !0, this.socket[k] = { headers: this[h], status: this.status }), t.end) { this.socket.end(t, null, e); return; } this.socket.write(t, null, e); } _destroy(t) { if (this.socket.destroyed) return t(); this.socket.once("close", t); } } export { _ as Response };