@orkestrel/server 0.0.7 → 0.0.8

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.
@@ -1746,14 +1746,17 @@ var Negotiator = class {
1746
1746
  * @typeParam TState - The consumer's opaque per-request state type
1747
1747
  *
1748
1748
  * @remarks
1749
- * - **Lifecycle (AGENTS §10).** `start()` builds the underlying `node:http`
1749
+ * - **Lifecycle (AGENTS §10).** `start(signal?)` builds the underlying `node:http`
1750
1750
  * server, binds the configured {@link ServerOptions.host} / {@link
1751
1751
  * ServerOptions.port} (omitted/`0` port ⇒ EPHEMERAL, resolved from the
1752
- * bound address), and transitions `idle → starting → listening`. `stop()`
1753
- * transitions to `stopping`: refuses new connections, fires a fresh-per-run
1754
- * stop signal so in-flight handlers observe cancellation, drains up to the
1755
- * `drain` deadline (event-driven, no busy-loop), then closes → `stopped`.
1756
- * `destroy()` is the idempotent final teardown.
1752
+ * bound address), observes caller cancellation plus the configured
1753
+ * `timeouts.start` deadline while the bind is pending, and transitions
1754
+ * `idle → starting → listening`. A cancelled or expired bind closes its
1755
+ * partial server and resets to `idle` for another start. `stop()` transitions
1756
+ * to `stopping`: refuses new connections, fires a fresh-per-run stop signal
1757
+ * so in-flight handlers observe cancellation, drains up to the `drain`
1758
+ * deadline (event-driven, no busy-loop), then closes → `stopped`. `destroy()`
1759
+ * is the idempotent final teardown.
1757
1760
  * - **Per request.** In-flight is tracked (finished on response `finish` or
1758
1761
  * `close`); a `Request` is built via the router's `buildRequest`, its
1759
1762
  * `signal` LINKED to this run's stop signal via `@orkestrel/abort`'s
@@ -1796,6 +1799,7 @@ var Server = class {
1796
1799
  #expose;
1797
1800
  #report;
1798
1801
  #timeouts;
1802
+ #sockets;
1799
1803
  #http;
1800
1804
  #abort = (0, _orkestrel_abort.createAbort)();
1801
1805
  #wakeup = (0, _orkestrel_abort.createAbort)();
@@ -1812,6 +1816,8 @@ var Server = class {
1812
1816
  const timeouts = options.timeouts ?? {};
1813
1817
  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
1818
  if (timeouts.headers !== void 0 && timeouts.keepalive !== void 0 && timeouts.headers > timeouts.keepalive) throw new TypeError("ServerOptions.timeouts.headers must not exceed timeouts.keepalive");
1819
+ const sockets = options.sockets ?? {};
1820
+ for (const [name, value] of Object.entries(sockets)) if (value !== void 0 && (!(0, _orkestrel_contract.isInteger)(value) || value < 0)) throw new TypeError(`ServerOptions.sockets.${name} must be a non-negative integer`);
1815
1821
  this.#dispatcher = options.dispatcher;
1816
1822
  this.#state = options.state;
1817
1823
  this.#middleware = options.middleware === void 0 ? [] : [...options.middleware];
@@ -1822,6 +1828,7 @@ var Server = class {
1822
1828
  this.#expose = options.expose ?? false;
1823
1829
  this.#report = options.report;
1824
1830
  this.#timeouts = timeouts;
1831
+ this.#sockets = sockets;
1825
1832
  this.#emitter = new _orkestrel_emitter.Emitter({
1826
1833
  ...options.on === void 0 ? {} : { on: options.on },
1827
1834
  ...options.error === void 0 ? {} : { error: options.error }
@@ -1846,7 +1853,7 @@ var Server = class {
1846
1853
  upgrade(handler) {
1847
1854
  this.#upgradeHandlers.push(handler);
1848
1855
  }
1849
- start() {
1856
+ start(signal) {
1850
1857
  if (this.#status !== "idle" && this.#status !== "stopped") return Promise.reject(/* @__PURE__ */ new Error(`server cannot start from '${this.#status}'`));
1851
1858
  this.#status = "starting";
1852
1859
  this.#abort = (0, _orkestrel_abort.createAbort)();
@@ -1854,9 +1861,12 @@ var Server = class {
1854
1861
  if (this.#timeouts.request !== void 0) server.requestTimeout = this.#timeouts.request;
1855
1862
  if (this.#timeouts.headers !== void 0) server.headersTimeout = this.#timeouts.headers;
1856
1863
  if (this.#timeouts.keepalive !== void 0) server.keepAliveTimeout = this.#timeouts.keepalive;
1864
+ if (this.#sockets.connections !== void 0) server.maxConnections = this.#sockets.connections;
1865
+ if (this.#sockets.headers !== void 0) server.maxHeadersCount = this.#sockets.headers;
1866
+ if (this.#sockets.requests !== void 0) server.maxRequestsPerSocket = this.#sockets.requests;
1857
1867
  server.on("upgrade", (request, socket, head) => this.#onUpgrade(request, socket, head));
1858
1868
  this.#http = server;
1859
- return this.#listen(server);
1869
+ return this.#listen(server, signal);
1860
1870
  }
1861
1871
  async stop() {
1862
1872
  if (this.#status !== "listening") return;
@@ -1981,15 +1991,41 @@ var Server = class {
1981
1991
  if (isAddressInfo(address)) return address.port;
1982
1992
  return 0;
1983
1993
  }
1984
- async #listen(server) {
1994
+ async #listen(server, signal) {
1995
+ const deadline = this.#timeouts.start === void 0 ? void 0 : (0, _orkestrel_timeout.createTimeout)({ ms: this.#timeouts.start });
1996
+ const startup = deadline === void 0 ? signal : (0, _orkestrel_abort.linkSignal)(deadline.signal, signal);
1997
+ const binding = (0, _orkestrel_abort.createAbort)();
1998
+ const relay = startup === void 0 ? void 0 : (0, node_events.addAbortListener)(startup, () => binding.abort(startup.reason));
1999
+ let listening;
1985
2000
  try {
1986
- const listening = (0, node_events.once)(server, "listening");
1987
- server.listen(this.#configuredPort ?? 0, this.#host);
2001
+ signal?.throwIfAborted();
2002
+ deadline?.start();
2003
+ if (deadline?.ms === 0 && startup !== void 0) await (0, node_events.once)(startup, "abort");
2004
+ startup?.throwIfAborted();
2005
+ listening = (0, node_events.once)(server, "listening", { signal: binding.signal });
2006
+ if (startup === void 0) server.listen(this.#configuredPort ?? 0, this.#host);
2007
+ else server.listen({
2008
+ port: this.#configuredPort ?? 0,
2009
+ ...this.#host === void 0 ? {} : { host: this.#host },
2010
+ signal: binding.signal
2011
+ });
1988
2012
  await listening;
1989
2013
  } catch (error) {
2014
+ const expired = deadline?.expired === true;
2015
+ const cancelled = startup?.aborted === true;
2016
+ server.on("error", () => void 0);
2017
+ binding.abort(error);
2018
+ if (listening !== void 0) await listening.catch(() => void 0);
2019
+ await this.#close(server, true);
1990
2020
  this.#http = void 0;
2021
+ this.#port = void 0;
1991
2022
  this.#status = "idle";
2023
+ if (expired && deadline !== void 0) throw new DOMException(`Server startup exceeded ${deadline.ms} milliseconds`, "TimeoutError");
2024
+ if (cancelled && startup !== void 0) throw startup.reason;
1992
2025
  throw error;
2026
+ } finally {
2027
+ relay?.[Symbol.dispose]();
2028
+ deadline?.clear();
1993
2029
  }
1994
2030
  const port = this.#resolvePort(server);
1995
2031
  this.#port = port;