@orkestrel/server 0.0.10 → 0.0.12

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.
@@ -1787,9 +1787,10 @@ var Negotiator = class {
1787
1787
  * `idle → starting → listening`. A cancelled or expired bind closes its
1788
1788
  * partial server and resets to `idle` for another start. `stop()` transitions
1789
1789
  * to `stopping`: refuses new connections, fires a fresh-per-run stop signal
1790
- * so in-flight handlers observe cancellation, drains up to the `drain`
1791
- * deadline (event-driven, no busy-loop), then closes → `stopped`. `destroy()`
1792
- * is the idempotent final teardown.
1790
+ * so in-flight handlers observe cancellation, drains in-flight requests AND
1791
+ * claimed upgraded sockets up to the `drain` deadline (event-driven, no
1792
+ * busy-loop), then closes → `stopped`, forcing whatever the deadline caught
1793
+ * still open. `destroy()` is the idempotent final teardown.
1793
1794
  * - **Per request.** In-flight is tracked (finished on response `finish` or
1794
1795
  * `close`); a `Request` is built via the router's `buildRequest`, its
1795
1796
  * `signal` LINKED to this run's stop signal via `@orkestrel/abort`'s
@@ -1814,6 +1815,9 @@ var Negotiator = class {
1814
1815
  * - **Upgrade fan-out** — verbatim old semantics (first-claimer-wins, a
1815
1816
  * throwing handler is treated as declined and surfaced on `error`, an
1816
1817
  * unclaimed upgrade destroys the socket), bound per-run to this instance.
1818
+ * A CLAIMED socket joins `#upgraded` until it closes: the claimant still
1819
+ * owns it, and the tracking exists because node detaches an upgraded socket
1820
+ * from the set its own close calls walk, so nothing else can end it.
1817
1821
  * - **Observable (§13).** Owns an {@link Emitter} over {@link ServerEventMap}
1818
1822
  * exposed as `readonly emitter`; the emitter isolates a listener throw and
1819
1823
  * routes it to the `error` OPTION (not the domain `error` event).
@@ -1824,6 +1828,7 @@ var Server = class {
1824
1828
  #state;
1825
1829
  #middleware;
1826
1830
  #upgradeHandlers = [];
1831
+ #upgraded = /* @__PURE__ */ new Set();
1827
1832
  #emitter;
1828
1833
  #host;
1829
1834
  #configuredPort;
@@ -1867,6 +1872,9 @@ var Server = class {
1867
1872
  ...options.error === void 0 ? {} : { error: options.error }
1868
1873
  });
1869
1874
  }
1875
+ get #inflight() {
1876
+ return this.#pending + this.#upgraded.size;
1877
+ }
1870
1878
  get status() {
1871
1879
  return this.#status;
1872
1880
  }
@@ -1912,8 +1920,9 @@ var Server = class {
1912
1920
  await this.#drainPending(deadline.signal);
1913
1921
  deadline.clear();
1914
1922
  const pending = this.#pending;
1915
- this.#emitter.emit("drain", pending);
1916
- if (server !== void 0) await this.#close(server, pending > 0);
1923
+ const upgraded = this.#upgraded.size;
1924
+ this.#emitter.emit("drain", pending, upgraded);
1925
+ if (server !== void 0) await this.#close(server, pending + upgraded > 0);
1917
1926
  this.#http = void 0;
1918
1927
  this.#port = void 0;
1919
1928
  this.#status = "stopped";
@@ -2016,7 +2025,8 @@ var Server = class {
2016
2025
  } catch (error) {
2017
2026
  this.#emitter.emit("error", error);
2018
2027
  }
2019
- if (!handled) socket.destroy();
2028
+ if (handled) this.#trackSocket(socket);
2029
+ else socket.destroy();
2020
2030
  this.#emitter.emit("upgrade", request, handled);
2021
2031
  }
2022
2032
  #resolvePort(server) {
@@ -2076,23 +2086,40 @@ var Server = class {
2076
2086
  #close(server, force) {
2077
2087
  return new Promise((resolve) => {
2078
2088
  server.close(() => resolve());
2079
- if (force) server.closeAllConnections();
2080
- else server.closeIdleConnections();
2089
+ if (force) {
2090
+ server.closeAllConnections();
2091
+ for (const socket of this.#upgraded) socket.destroy();
2092
+ } else server.closeIdleConnections();
2081
2093
  });
2082
2094
  }
2095
+ #enter() {
2096
+ if (this.#inflight === 0) this.#wakeup = (0, _orkestrel_abort.createAbort)();
2097
+ }
2098
+ #settle() {
2099
+ if (this.#inflight === 0) this.#wakeup.abort();
2100
+ }
2083
2101
  #trackStart() {
2084
- if (this.#pending === 0) this.#wakeup = (0, _orkestrel_abort.createAbort)();
2102
+ this.#enter();
2085
2103
  this.#pending += 1;
2086
2104
  let finished = false;
2087
2105
  return () => {
2088
2106
  if (finished) return;
2089
2107
  finished = true;
2090
2108
  this.#pending -= 1;
2091
- if (this.#pending === 0) this.#wakeup.abort();
2109
+ this.#settle();
2092
2110
  };
2093
2111
  }
2112
+ #trackSocket(socket) {
2113
+ if (socket.destroyed || this.#upgraded.has(socket)) return;
2114
+ this.#enter();
2115
+ this.#upgraded.add(socket);
2116
+ socket.once("close", () => {
2117
+ this.#upgraded.delete(socket);
2118
+ this.#settle();
2119
+ });
2120
+ }
2094
2121
  async #drainPending(signal) {
2095
- if (this.#pending === 0 || signal.aborted) return;
2122
+ if (this.#inflight === 0 || signal.aborted) return;
2096
2123
  await (0, node_events.once)(AbortSignal.any([signal, this.#wakeup.signal]), "abort");
2097
2124
  }
2098
2125
  };