@orkestrel/server 0.0.21 → 0.0.23
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/dist/src/server/index.cjs +91 -30
- package/dist/src/server/index.cjs.map +1 -1
- package/dist/src/server/index.d.cts +78 -31
- package/dist/src/server/index.d.ts +78 -31
- package/dist/src/server/index.js +92 -31
- package/dist/src/server/index.js.map +1 -1
- package/package.json +16 -16
|
@@ -4,6 +4,7 @@ let node_events = require("node:events");
|
|
|
4
4
|
let node_net = require("node:net");
|
|
5
5
|
let _orkestrel_codec = require("@orkestrel/codec");
|
|
6
6
|
let node_http = require("node:http");
|
|
7
|
+
let node_stream_promises = require("node:stream/promises");
|
|
7
8
|
let _orkestrel_abort = require("@orkestrel/abort");
|
|
8
9
|
let _orkestrel_timeout = require("@orkestrel/timeout");
|
|
9
10
|
let _orkestrel_router_server = require("@orkestrel/router/server");
|
|
@@ -1744,11 +1745,20 @@ var Negotiator = class {
|
|
|
1744
1745
|
* bind is pending, and transitions `idle → starting → listening`. A
|
|
1745
1746
|
* cancelled or expired bind closes its partial server and resets to `idle` for
|
|
1746
1747
|
* another start. `stop()` transitions
|
|
1747
|
-
* to `stopping`:
|
|
1748
|
-
*
|
|
1748
|
+
* to `stopping`: fires a fresh-per-run stop signal so in-flight handlers
|
|
1749
|
+
* observe cancellation (a request that arrives during the drain is served
|
|
1750
|
+
* with that signal already aborted), drains in-flight requests and
|
|
1749
1751
|
* claimed upgraded sockets up to the `drain` deadline (event-driven, no
|
|
1750
|
-
* busy-loop), then closes → `stopped
|
|
1751
|
-
*
|
|
1752
|
+
* busy-loop), then closes → `stopped`. After a clean drain the close
|
|
1753
|
+
* destroys every connection that carries no open request exchange, and a
|
|
1754
|
+
* connection that still carries one ends when its last exchange completes
|
|
1755
|
+
* or the same `drain` deadline expires, whichever comes first.
|
|
1756
|
+
* A request or an upgrade that such a connection sends after the listener
|
|
1757
|
+
* closes is refused: its connection is destroyed before the request is
|
|
1758
|
+
* counted or a handler sees it, so the `stop()` call waits only on work
|
|
1759
|
+
* counted before the close. After an expired drain the close destroys every
|
|
1760
|
+
* socket. A `stop()` call made while a stop runs returns that stop's
|
|
1761
|
+
* promise. `destroy()` is the idempotent final teardown.
|
|
1752
1762
|
* - **Per request.** In-flight is tracked (finished on response `finish` or
|
|
1753
1763
|
* `close`); a `Request` is built through the router's `buildRequest`, its
|
|
1754
1764
|
* `signal` linked to this run's stop signal through `@orkestrel/abort`'s
|
|
@@ -1774,7 +1784,7 @@ var Negotiator = class {
|
|
|
1774
1784
|
* throwing handler is treated as declined and surfaced on `error`, an
|
|
1775
1785
|
* unclaimed upgrade destroys the socket), bound per-run to this instance.
|
|
1776
1786
|
* A claimed socket joins `#upgraded` until it closes: the claimant still
|
|
1777
|
-
* owns it, and the tracking exists because
|
|
1787
|
+
* owns it, and the tracking exists because Node detaches an upgraded socket
|
|
1778
1788
|
* from the set its own close calls walk, so nothing else can end it.
|
|
1779
1789
|
* - **Observable.** Owns an {@link Emitter} over {@link ServerEventMap}
|
|
1780
1790
|
* exposed as `readonly emitter`; the emitter isolates a listener throw and
|
|
@@ -1787,6 +1797,7 @@ var Server = class {
|
|
|
1787
1797
|
#middleware;
|
|
1788
1798
|
#upgradeHandlers = [];
|
|
1789
1799
|
#upgraded = /* @__PURE__ */ new Set();
|
|
1800
|
+
#exchanges = /* @__PURE__ */ new Map();
|
|
1790
1801
|
#emitter;
|
|
1791
1802
|
#host;
|
|
1792
1803
|
#configuredPort;
|
|
@@ -1802,14 +1813,16 @@ var Server = class {
|
|
|
1802
1813
|
#status = "idle";
|
|
1803
1814
|
#port;
|
|
1804
1815
|
#pending = 0;
|
|
1816
|
+
#stopping;
|
|
1805
1817
|
constructor(options) {
|
|
1806
1818
|
if (!(0, _orkestrel_contract.isFunction)(options.state)) throw new TypeError("ServerOptions.state must be a function");
|
|
1807
1819
|
if (options.report !== void 0 && !(0, _orkestrel_contract.isFunction)(options.report)) throw new TypeError("ServerOptions.report must be a function");
|
|
1808
1820
|
const drain = options.drain ?? 1e4;
|
|
1809
|
-
if (!(0,
|
|
1821
|
+
if (!(0, _orkestrel_timeout.isTimeoutDuration)(drain)) throw new TypeError("ServerOptions.drain must be an integer from 0 through 2_147_483_647");
|
|
1810
1822
|
const limit = options.limit ?? 1048576;
|
|
1811
1823
|
if (!(0, _orkestrel_contract.isFiniteNumber)(limit) || limit < 0) throw new TypeError("ServerOptions.limit must be a non-negative finite number");
|
|
1812
1824
|
const timeouts = options.timeouts ?? {};
|
|
1825
|
+
if (timeouts.start !== void 0 && !(0, _orkestrel_timeout.isTimeoutDuration)(timeouts.start)) throw new TypeError("ServerOptions.timeouts.start must be an integer from 0 through 2_147_483_647");
|
|
1813
1826
|
for (const [name, value] of Object.entries(timeouts)) if (value !== void 0 && (!(0, _orkestrel_contract.isFiniteNumber)(value) || value < 0)) throw new TypeError(`ServerOptions.timeouts.${name} must be a non-negative finite number`);
|
|
1814
1827
|
if (timeouts.headers !== void 0 && timeouts.keepalive !== void 0 && timeouts.headers > timeouts.keepalive) throw new TypeError("ServerOptions.timeouts.headers must not exceed timeouts.keepalive");
|
|
1815
1828
|
const sockets = options.sockets ?? {};
|
|
@@ -1869,26 +1882,18 @@ var Server = class {
|
|
|
1869
1882
|
if (this.#sockets.headers !== void 0) server.maxHeadersCount = this.#sockets.headers;
|
|
1870
1883
|
if (this.#sockets.requests !== void 0) server.maxRequestsPerSocket = this.#sockets.requests;
|
|
1871
1884
|
server.on("upgrade", (request, socket, head) => this.#onUpgrade(request, socket, head));
|
|
1885
|
+
server.on("connection", (socket) => this.#trackConnection(socket));
|
|
1872
1886
|
this.#http = server;
|
|
1873
1887
|
return this.#listen(server, signal);
|
|
1874
1888
|
}
|
|
1875
|
-
|
|
1876
|
-
if (this.#status !== "listening") return;
|
|
1877
|
-
|
|
1878
|
-
this.#
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
await this.#drainPending(deadline.signal);
|
|
1884
|
-
deadline.clear();
|
|
1885
|
-
const pending = this.#pending;
|
|
1886
|
-
const upgraded = this.#upgraded.size;
|
|
1887
|
-
this.#emitter.emit("drain", pending, upgraded);
|
|
1888
|
-
if (server !== void 0) await this.#close(server, pending + upgraded > 0);
|
|
1889
|
-
this.#http = void 0;
|
|
1890
|
-
this.#port = void 0;
|
|
1891
|
-
this.#status = "stopped";
|
|
1889
|
+
stop() {
|
|
1890
|
+
if (this.#status !== "listening") return this.#stopping ?? Promise.resolve();
|
|
1891
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
1892
|
+
this.#stopping = promise;
|
|
1893
|
+
this.#stopGracefully().finally(() => {
|
|
1894
|
+
if (this.#stopping === promise) this.#stopping = void 0;
|
|
1895
|
+
}).then(resolve, reject);
|
|
1896
|
+
return promise;
|
|
1892
1897
|
}
|
|
1893
1898
|
async destroy() {
|
|
1894
1899
|
if (this.#status === "stopped" && this.#http === void 0) {
|
|
@@ -1903,10 +1908,42 @@ var Server = class {
|
|
|
1903
1908
|
this.#status = "stopped";
|
|
1904
1909
|
this.#emitter.destroy();
|
|
1905
1910
|
}
|
|
1911
|
+
async #stopGracefully() {
|
|
1912
|
+
this.#status = "stopping";
|
|
1913
|
+
this.#emitter.emit("stop");
|
|
1914
|
+
const server = this.#http;
|
|
1915
|
+
this.#abort.abort();
|
|
1916
|
+
const deadline = (0, _orkestrel_timeout.createTimeout)({ ms: this.#drain });
|
|
1917
|
+
deadline.start();
|
|
1918
|
+
try {
|
|
1919
|
+
await this.#drainPending(deadline.signal);
|
|
1920
|
+
const pending = this.#pending;
|
|
1921
|
+
const upgraded = this.#upgraded.size;
|
|
1922
|
+
this.#emitter.emit("drain", pending, upgraded);
|
|
1923
|
+
if (server !== void 0) {
|
|
1924
|
+
const relay = (0, node_events.addAbortListener)(deadline.signal, () => this.#cut(server));
|
|
1925
|
+
try {
|
|
1926
|
+
await this.#close(server, pending + upgraded > 0);
|
|
1927
|
+
} finally {
|
|
1928
|
+
relay[Symbol.dispose]();
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
} finally {
|
|
1932
|
+
deadline.clear();
|
|
1933
|
+
}
|
|
1934
|
+
this.#http = void 0;
|
|
1935
|
+
this.#port = void 0;
|
|
1936
|
+
this.#status = "stopped";
|
|
1937
|
+
}
|
|
1906
1938
|
#handle(message, response) {
|
|
1939
|
+
if (this.#closed) {
|
|
1940
|
+
message.socket.destroy();
|
|
1941
|
+
return;
|
|
1942
|
+
}
|
|
1907
1943
|
const finish = this.#trackStart();
|
|
1908
1944
|
response.once("finish", finish);
|
|
1909
1945
|
response.once("close", finish);
|
|
1946
|
+
this.#trackExchange(message, response);
|
|
1910
1947
|
this.#accept(message, response);
|
|
1911
1948
|
}
|
|
1912
1949
|
async #accept(message, response) {
|
|
@@ -1979,6 +2016,10 @@ var Server = class {
|
|
|
1979
2016
|
return new Response(message, { status: 500 });
|
|
1980
2017
|
}
|
|
1981
2018
|
#onUpgrade(request, socket, head) {
|
|
2019
|
+
if (this.#closed) {
|
|
2020
|
+
socket.destroy();
|
|
2021
|
+
return;
|
|
2022
|
+
}
|
|
1982
2023
|
let handled = false;
|
|
1983
2024
|
for (const handler of this.#upgradeHandlers) try {
|
|
1984
2025
|
if (handler(request, socket, head)) {
|
|
@@ -2047,12 +2088,32 @@ var Server = class {
|
|
|
2047
2088
|
#close(server, force) {
|
|
2048
2089
|
return new Promise((resolve) => {
|
|
2049
2090
|
server.close(() => resolve());
|
|
2050
|
-
if (force)
|
|
2051
|
-
|
|
2052
|
-
for (const socket of this.#upgraded) socket.destroy();
|
|
2053
|
-
} else server.closeIdleConnections();
|
|
2091
|
+
if (force) this.#cut(server);
|
|
2092
|
+
else for (const [socket, open] of this.#exchanges) if (open === 0) socket.destroy();
|
|
2054
2093
|
});
|
|
2055
2094
|
}
|
|
2095
|
+
#cut(server) {
|
|
2096
|
+
server.closeAllConnections();
|
|
2097
|
+
for (const socket of this.#upgraded) socket.destroy();
|
|
2098
|
+
}
|
|
2099
|
+
#trackConnection(socket) {
|
|
2100
|
+
this.#exchanges.set(socket, 0);
|
|
2101
|
+
socket.once("close", () => this.#exchanges.delete(socket));
|
|
2102
|
+
}
|
|
2103
|
+
async #trackExchange(message, response) {
|
|
2104
|
+
const socket = message.socket;
|
|
2105
|
+
const open = this.#exchanges.get(socket);
|
|
2106
|
+
if (open === void 0) return;
|
|
2107
|
+
this.#exchanges.set(socket, open + 1);
|
|
2108
|
+
await Promise.allSettled([(0, node_stream_promises.finished)(message), (0, node_stream_promises.finished)(response)]);
|
|
2109
|
+
const left = this.#exchanges.get(socket);
|
|
2110
|
+
if (left === void 0) return;
|
|
2111
|
+
this.#exchanges.set(socket, left - 1);
|
|
2112
|
+
if (left === 1 && this.#closed) socket.destroy();
|
|
2113
|
+
}
|
|
2114
|
+
get #closed() {
|
|
2115
|
+
return this.#status === "stopping" && this.#http?.listening === false;
|
|
2116
|
+
}
|
|
2056
2117
|
get #inflight() {
|
|
2057
2118
|
return this.#pending + this.#upgraded.size;
|
|
2058
2119
|
}
|
|
@@ -2065,10 +2126,10 @@ var Server = class {
|
|
|
2065
2126
|
#trackStart() {
|
|
2066
2127
|
this.#enter();
|
|
2067
2128
|
this.#pending += 1;
|
|
2068
|
-
let
|
|
2129
|
+
let done = false;
|
|
2069
2130
|
return () => {
|
|
2070
|
-
if (
|
|
2071
|
-
|
|
2131
|
+
if (done) return;
|
|
2132
|
+
done = true;
|
|
2072
2133
|
this.#pending -= 1;
|
|
2073
2134
|
this.#settle();
|
|
2074
2135
|
};
|