@orkestrel/server 0.0.11 → 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.
@@ -187,7 +187,7 @@ export declare function collectRequestBody(request: Request, limit: number): Pro
187
187
  * const handle = compose([withHeader], async () => new Response('ok'))
188
188
  * ```
189
189
  */
190
- export declare function compose<TState>(middleware: readonly MiddlewareHandler<TState>[], terminal: (request: Request, context: MiddlewareContext<TState>) => Promise<Response>): (request: Request, context: MiddlewareContext<TState>) => Promise<Response>;
190
+ export declare function compose<TState>(middleware: ReadonlyArray<MiddlewareHandler<TState>>, terminal: (request: Request, context: MiddlewareContext<TState>) => Promise<Response>): (request: Request, context: MiddlewareContext<TState>) => Promise<Response>;
191
191
 
192
192
  /**
193
193
  * The set of bare `Content-Type`s `isCompressibleType` treats as
@@ -1481,9 +1481,10 @@ export declare function serializeEvent(message: SSEMessage): string;
1481
1481
  * `idle → starting → listening`. A cancelled or expired bind closes its
1482
1482
  * partial server and resets to `idle` for another start. `stop()` transitions
1483
1483
  * to `stopping`: refuses new connections, fires a fresh-per-run stop signal
1484
- * so in-flight handlers observe cancellation, drains up to the `drain`
1485
- * deadline (event-driven, no busy-loop), then closes → `stopped`. `destroy()`
1486
- * is the idempotent final teardown.
1484
+ * so in-flight handlers observe cancellation, drains in-flight requests AND
1485
+ * claimed upgraded sockets up to the `drain` deadline (event-driven, no
1486
+ * busy-loop), then closes → `stopped`, forcing whatever the deadline caught
1487
+ * still open. `destroy()` is the idempotent final teardown.
1487
1488
  * - **Per request.** In-flight is tracked (finished on response `finish` or
1488
1489
  * `close`); a `Request` is built via the router's `buildRequest`, its
1489
1490
  * `signal` LINKED to this run's stop signal via `@orkestrel/abort`'s
@@ -1508,6 +1509,9 @@ export declare function serializeEvent(message: SSEMessage): string;
1508
1509
  * - **Upgrade fan-out** — verbatim old semantics (first-claimer-wins, a
1509
1510
  * throwing handler is treated as declined and surfaced on `error`, an
1510
1511
  * unclaimed upgrade destroys the socket), bound per-run to this instance.
1512
+ * A CLAIMED socket joins `#upgraded` until it closes: the claimant still
1513
+ * owns it, and the tracking exists because node detaches an upgraded socket
1514
+ * from the set its own close calls walk, so nothing else can end it.
1511
1515
  * - **Observable (§13).** Owns an {@link Emitter} over {@link ServerEventMap}
1512
1516
  * exposed as `readonly emitter`; the emitter isolates a listener throw and
1513
1517
  * routes it to the `error` OPTION (not the domain `error` event).
@@ -1521,7 +1525,7 @@ export declare class Server<TState> implements ServerInterface<TState> {
1521
1525
  get dispatcher(): DispatcherInterface<TState>;
1522
1526
  get emitter(): EmitterInterface<ServerEventMap>;
1523
1527
  use(middleware: MiddlewareHandler<TState>): void;
1524
- use(middleware: readonly MiddlewareHandler<TState>[]): void;
1528
+ use(middleware: ReadonlyArray<MiddlewareHandler<TState>>): void;
1525
1529
  upgrade(handler: UpgradeHandler): void;
1526
1530
  start(signal?: AbortSignal): Promise<number>;
1527
1531
  stop(): Promise<void>;
@@ -1544,9 +1548,13 @@ export declare class Server<TState> implements ServerInterface<TState> {
1544
1548
  * the per-request path; `undefined` for an upgrade-handler throw (no fetch
1545
1549
  * `Request` exists on that path — only a raw `IncomingMessage`) or a listen
1546
1550
  * failure.
1547
- * - `stop` — `stop()` began (status just moved to `'stopping'`).
1551
+ * - `stop` — `stop()` began (status just moved to `'stopping'`). An upgrade
1552
+ * handler that owns a long-lived socket closes it from here, so the drain
1553
+ * below settles instead of running out the deadline.
1548
1554
  * - `drain` — the graceful drain settled (deadline hit or all finished);
1549
- * carries the still-pending request count (`0` on a clean drain).
1555
+ * carries the still-pending request count AND the still-attached upgraded
1556
+ * socket count. Both `0` is a clean drain; either non-zero means the close
1557
+ * that follows was FORCED and cut that work.
1550
1558
  * - `response` — fired after the response has been sent, for every request
1551
1559
  * that reaches the middleware pipeline (the success path and the
1552
1560
  * outer-boundary error path); carries the method, parsed pathname, final
@@ -1564,7 +1572,7 @@ export declare type ServerEventMap = {
1564
1572
  url: URL;
1565
1573
  }];
1566
1574
  readonly stop: readonly [];
1567
- readonly drain: readonly [pending: number];
1575
+ readonly drain: readonly [pending: number, upgraded: number];
1568
1576
  readonly response: readonly [
1569
1577
  event: {
1570
1578
  method: string;
@@ -1590,7 +1598,8 @@ export declare type ServerEventMap = {
1590
1598
  * while binding, and resolves the actually-bound port. A cancelled or expired
1591
1599
  * bind closes its partial server and resets to `idle`. `stop()` refuses new
1592
1600
  * connections, fires the stop signal so in-flight handlers can observe it,
1593
- * drains up to the configured deadline, then closes. `destroy()` is the final
1601
+ * drains in-flight requests and claimed upgraded sockets up to the configured
1602
+ * deadline, then closes — forcing whatever is left. `destroy()` is the final
1594
1603
  * idempotent teardown. Per request: a `Request` is built via the router's
1595
1604
  * `buildRequest` (its signal linked to the server's stop signal), the composed
1596
1605
  * middleware onion runs terminating in `dispatcher.handle`, and the result is
@@ -1605,7 +1614,7 @@ export declare interface ServerInterface<TState> {
1605
1614
  readonly dispatcher: DispatcherInterface<TState>;
1606
1615
  readonly emitter: EmitterInterface<ServerEventMap>;
1607
1616
  use(middleware: MiddlewareHandler<TState>): void;
1608
- use(middleware: readonly MiddlewareHandler<TState>[]): void;
1617
+ use(middleware: ReadonlyArray<MiddlewareHandler<TState>>): void;
1609
1618
  upgrade(handler: UpgradeHandler): void;
1610
1619
  /**
1611
1620
  * Bind the configured listener and resolve its actually-bound port.
@@ -1615,7 +1624,26 @@ export declare interface ServerInterface<TState> {
1615
1624
  * @returns The actually-bound TCP port
1616
1625
  */
1617
1626
  start(signal?: AbortSignal): Promise<number>;
1627
+ /**
1628
+ * Stop gracefully: refuse new connections, fire the stop signal, drain, close.
1629
+ *
1630
+ * @remarks
1631
+ * Drainable work is every in-flight request PLUS every upgraded socket a
1632
+ * handler claimed. The drain parks on that work reaching zero or the
1633
+ * `drain` deadline expiring, emits `drain` with both remaining counts, and
1634
+ * then closes — dropping idle keep-alive sockets on a clean drain, and
1635
+ * destroying every open socket (including the claimed upgraded ones node's
1636
+ * own force-close cannot reach) when either count is still non-zero. It
1637
+ * therefore always resolves; the `drain` counts say whether anything was cut.
1638
+ *
1639
+ * @returns Resolves once the listener is closed and the status is `'stopped'`
1640
+ */
1618
1641
  stop(): Promise<void>;
1642
+ /**
1643
+ * Tear down for good: force-close the listener and every socket, then the emitter.
1644
+ *
1645
+ * @returns Resolves once nothing is left open; idempotent from any state
1646
+ */
1619
1647
  destroy(): Promise<void>;
1620
1648
  }
1621
1649
 
@@ -1639,9 +1667,12 @@ export declare interface ServerInterface<TState> {
1639
1667
  * `EADDRINUSE` — no silent ephemeral fallback (use `discoverPort` to pick a
1640
1668
  * guaranteed-free port up front).
1641
1669
  * @param drain - The graceful-stop deadline in milliseconds: on `stop()` the
1642
- * server stops accepting new connections and gives in-flight requests this
1643
- * long to finish before forcing sockets closed. Defaults to
1644
- * `DEFAULT_DRAIN_MS`. Must be a non-negative finite number.
1670
+ * server stops accepting new connections and gives in-flight requests AND
1671
+ * claimed upgraded sockets this long to finish before forcing every
1672
+ * remaining socket closed. Defaults to `DEFAULT_DRAIN_MS`. Must be a
1673
+ * non-negative finite number. A long-lived upgraded socket that nothing
1674
+ * closes therefore costs `stop()` this whole budget, so a WebSocket
1675
+ * handler closes its sockets on the `stop` event to settle sooner.
1645
1676
  * @param limit - The default request-body byte cap the context's `body()`
1646
1677
  * reads through. Defaults to `DEFAULT_BODY_LIMIT`. Must be a non-negative
1647
1678
  * finite number.
@@ -1675,7 +1706,7 @@ export declare interface ServerInterface<TState> {
1675
1706
  export declare interface ServerOptions<TState> {
1676
1707
  readonly dispatcher: DispatcherInterface<TState>;
1677
1708
  readonly state: ConnectionStateFunction<TState>;
1678
- readonly middleware?: readonly MiddlewareHandler<TState>[];
1709
+ readonly middleware?: ReadonlyArray<MiddlewareHandler<TState>>;
1679
1710
  readonly host?: string;
1680
1711
  readonly port?: number;
1681
1712
  readonly drain?: number;
@@ -1907,6 +1938,16 @@ export declare function unwrapETag(tag: string): string;
1907
1938
  * connection. `request` / `socket` / `head` are node's own raw values, handed
1908
1939
  * over verbatim — no assertion at this boundary (AGENTS §14).
1909
1940
  *
1941
+ * A CLAIMED socket is TRACKED until it closes. The handler still owns it —
1942
+ * the server only watches — but `stop()` now drains that socket like an
1943
+ * in-flight request and destroys it if the `drain` deadline expires first.
1944
+ * Node detaches an upgraded socket from its own connection set, so neither
1945
+ * `closeIdleConnections()` nor `closeAllConnections()` reaches it and this
1946
+ * tracking is what lets `stop()` and `destroy()` finish at all. A handler
1947
+ * that wants a protocol-clean goodbye (a WebSocket close frame) sends it on
1948
+ * the server's `stop` event and closes the socket; the drain settles the
1949
+ * moment the last one goes.
1950
+ *
1910
1951
  * @param request - The raw `node:http` upgrade request
1911
1952
  * @param socket - The raw, now-detached `Duplex` connection
1912
1953
  * @param head - The first packet of the upgraded stream, if any
@@ -187,7 +187,7 @@ export declare function collectRequestBody(request: Request, limit: number): Pro
187
187
  * const handle = compose([withHeader], async () => new Response('ok'))
188
188
  * ```
189
189
  */
190
- export declare function compose<TState>(middleware: readonly MiddlewareHandler<TState>[], terminal: (request: Request, context: MiddlewareContext<TState>) => Promise<Response>): (request: Request, context: MiddlewareContext<TState>) => Promise<Response>;
190
+ export declare function compose<TState>(middleware: ReadonlyArray<MiddlewareHandler<TState>>, terminal: (request: Request, context: MiddlewareContext<TState>) => Promise<Response>): (request: Request, context: MiddlewareContext<TState>) => Promise<Response>;
191
191
 
192
192
  /**
193
193
  * The set of bare `Content-Type`s `isCompressibleType` treats as
@@ -1481,9 +1481,10 @@ export declare function serializeEvent(message: SSEMessage): string;
1481
1481
  * `idle → starting → listening`. A cancelled or expired bind closes its
1482
1482
  * partial server and resets to `idle` for another start. `stop()` transitions
1483
1483
  * to `stopping`: refuses new connections, fires a fresh-per-run stop signal
1484
- * so in-flight handlers observe cancellation, drains up to the `drain`
1485
- * deadline (event-driven, no busy-loop), then closes → `stopped`. `destroy()`
1486
- * is the idempotent final teardown.
1484
+ * so in-flight handlers observe cancellation, drains in-flight requests AND
1485
+ * claimed upgraded sockets up to the `drain` deadline (event-driven, no
1486
+ * busy-loop), then closes → `stopped`, forcing whatever the deadline caught
1487
+ * still open. `destroy()` is the idempotent final teardown.
1487
1488
  * - **Per request.** In-flight is tracked (finished on response `finish` or
1488
1489
  * `close`); a `Request` is built via the router's `buildRequest`, its
1489
1490
  * `signal` LINKED to this run's stop signal via `@orkestrel/abort`'s
@@ -1508,6 +1509,9 @@ export declare function serializeEvent(message: SSEMessage): string;
1508
1509
  * - **Upgrade fan-out** — verbatim old semantics (first-claimer-wins, a
1509
1510
  * throwing handler is treated as declined and surfaced on `error`, an
1510
1511
  * unclaimed upgrade destroys the socket), bound per-run to this instance.
1512
+ * A CLAIMED socket joins `#upgraded` until it closes: the claimant still
1513
+ * owns it, and the tracking exists because node detaches an upgraded socket
1514
+ * from the set its own close calls walk, so nothing else can end it.
1511
1515
  * - **Observable (§13).** Owns an {@link Emitter} over {@link ServerEventMap}
1512
1516
  * exposed as `readonly emitter`; the emitter isolates a listener throw and
1513
1517
  * routes it to the `error` OPTION (not the domain `error` event).
@@ -1521,7 +1525,7 @@ export declare class Server<TState> implements ServerInterface<TState> {
1521
1525
  get dispatcher(): DispatcherInterface<TState>;
1522
1526
  get emitter(): EmitterInterface<ServerEventMap>;
1523
1527
  use(middleware: MiddlewareHandler<TState>): void;
1524
- use(middleware: readonly MiddlewareHandler<TState>[]): void;
1528
+ use(middleware: ReadonlyArray<MiddlewareHandler<TState>>): void;
1525
1529
  upgrade(handler: UpgradeHandler): void;
1526
1530
  start(signal?: AbortSignal): Promise<number>;
1527
1531
  stop(): Promise<void>;
@@ -1544,9 +1548,13 @@ export declare class Server<TState> implements ServerInterface<TState> {
1544
1548
  * the per-request path; `undefined` for an upgrade-handler throw (no fetch
1545
1549
  * `Request` exists on that path — only a raw `IncomingMessage`) or a listen
1546
1550
  * failure.
1547
- * - `stop` — `stop()` began (status just moved to `'stopping'`).
1551
+ * - `stop` — `stop()` began (status just moved to `'stopping'`). An upgrade
1552
+ * handler that owns a long-lived socket closes it from here, so the drain
1553
+ * below settles instead of running out the deadline.
1548
1554
  * - `drain` — the graceful drain settled (deadline hit or all finished);
1549
- * carries the still-pending request count (`0` on a clean drain).
1555
+ * carries the still-pending request count AND the still-attached upgraded
1556
+ * socket count. Both `0` is a clean drain; either non-zero means the close
1557
+ * that follows was FORCED and cut that work.
1550
1558
  * - `response` — fired after the response has been sent, for every request
1551
1559
  * that reaches the middleware pipeline (the success path and the
1552
1560
  * outer-boundary error path); carries the method, parsed pathname, final
@@ -1564,7 +1572,7 @@ export declare type ServerEventMap = {
1564
1572
  url: URL;
1565
1573
  }];
1566
1574
  readonly stop: readonly [];
1567
- readonly drain: readonly [pending: number];
1575
+ readonly drain: readonly [pending: number, upgraded: number];
1568
1576
  readonly response: readonly [
1569
1577
  event: {
1570
1578
  method: string;
@@ -1590,7 +1598,8 @@ export declare type ServerEventMap = {
1590
1598
  * while binding, and resolves the actually-bound port. A cancelled or expired
1591
1599
  * bind closes its partial server and resets to `idle`. `stop()` refuses new
1592
1600
  * connections, fires the stop signal so in-flight handlers can observe it,
1593
- * drains up to the configured deadline, then closes. `destroy()` is the final
1601
+ * drains in-flight requests and claimed upgraded sockets up to the configured
1602
+ * deadline, then closes — forcing whatever is left. `destroy()` is the final
1594
1603
  * idempotent teardown. Per request: a `Request` is built via the router's
1595
1604
  * `buildRequest` (its signal linked to the server's stop signal), the composed
1596
1605
  * middleware onion runs terminating in `dispatcher.handle`, and the result is
@@ -1605,7 +1614,7 @@ export declare interface ServerInterface<TState> {
1605
1614
  readonly dispatcher: DispatcherInterface<TState>;
1606
1615
  readonly emitter: EmitterInterface<ServerEventMap>;
1607
1616
  use(middleware: MiddlewareHandler<TState>): void;
1608
- use(middleware: readonly MiddlewareHandler<TState>[]): void;
1617
+ use(middleware: ReadonlyArray<MiddlewareHandler<TState>>): void;
1609
1618
  upgrade(handler: UpgradeHandler): void;
1610
1619
  /**
1611
1620
  * Bind the configured listener and resolve its actually-bound port.
@@ -1615,7 +1624,26 @@ export declare interface ServerInterface<TState> {
1615
1624
  * @returns The actually-bound TCP port
1616
1625
  */
1617
1626
  start(signal?: AbortSignal): Promise<number>;
1627
+ /**
1628
+ * Stop gracefully: refuse new connections, fire the stop signal, drain, close.
1629
+ *
1630
+ * @remarks
1631
+ * Drainable work is every in-flight request PLUS every upgraded socket a
1632
+ * handler claimed. The drain parks on that work reaching zero or the
1633
+ * `drain` deadline expiring, emits `drain` with both remaining counts, and
1634
+ * then closes — dropping idle keep-alive sockets on a clean drain, and
1635
+ * destroying every open socket (including the claimed upgraded ones node's
1636
+ * own force-close cannot reach) when either count is still non-zero. It
1637
+ * therefore always resolves; the `drain` counts say whether anything was cut.
1638
+ *
1639
+ * @returns Resolves once the listener is closed and the status is `'stopped'`
1640
+ */
1618
1641
  stop(): Promise<void>;
1642
+ /**
1643
+ * Tear down for good: force-close the listener and every socket, then the emitter.
1644
+ *
1645
+ * @returns Resolves once nothing is left open; idempotent from any state
1646
+ */
1619
1647
  destroy(): Promise<void>;
1620
1648
  }
1621
1649
 
@@ -1639,9 +1667,12 @@ export declare interface ServerInterface<TState> {
1639
1667
  * `EADDRINUSE` — no silent ephemeral fallback (use `discoverPort` to pick a
1640
1668
  * guaranteed-free port up front).
1641
1669
  * @param drain - The graceful-stop deadline in milliseconds: on `stop()` the
1642
- * server stops accepting new connections and gives in-flight requests this
1643
- * long to finish before forcing sockets closed. Defaults to
1644
- * `DEFAULT_DRAIN_MS`. Must be a non-negative finite number.
1670
+ * server stops accepting new connections and gives in-flight requests AND
1671
+ * claimed upgraded sockets this long to finish before forcing every
1672
+ * remaining socket closed. Defaults to `DEFAULT_DRAIN_MS`. Must be a
1673
+ * non-negative finite number. A long-lived upgraded socket that nothing
1674
+ * closes therefore costs `stop()` this whole budget, so a WebSocket
1675
+ * handler closes its sockets on the `stop` event to settle sooner.
1645
1676
  * @param limit - The default request-body byte cap the context's `body()`
1646
1677
  * reads through. Defaults to `DEFAULT_BODY_LIMIT`. Must be a non-negative
1647
1678
  * finite number.
@@ -1675,7 +1706,7 @@ export declare interface ServerInterface<TState> {
1675
1706
  export declare interface ServerOptions<TState> {
1676
1707
  readonly dispatcher: DispatcherInterface<TState>;
1677
1708
  readonly state: ConnectionStateFunction<TState>;
1678
- readonly middleware?: readonly MiddlewareHandler<TState>[];
1709
+ readonly middleware?: ReadonlyArray<MiddlewareHandler<TState>>;
1679
1710
  readonly host?: string;
1680
1711
  readonly port?: number;
1681
1712
  readonly drain?: number;
@@ -1907,6 +1938,16 @@ export declare function unwrapETag(tag: string): string;
1907
1938
  * connection. `request` / `socket` / `head` are node's own raw values, handed
1908
1939
  * over verbatim — no assertion at this boundary (AGENTS §14).
1909
1940
  *
1941
+ * A CLAIMED socket is TRACKED until it closes. The handler still owns it —
1942
+ * the server only watches — but `stop()` now drains that socket like an
1943
+ * in-flight request and destroys it if the `drain` deadline expires first.
1944
+ * Node detaches an upgraded socket from its own connection set, so neither
1945
+ * `closeIdleConnections()` nor `closeAllConnections()` reaches it and this
1946
+ * tracking is what lets `stop()` and `destroy()` finish at all. A handler
1947
+ * that wants a protocol-clean goodbye (a WebSocket close frame) sends it on
1948
+ * the server's `stop` event and closes the socket; the drain settles the
1949
+ * moment the last one goes.
1950
+ *
1910
1951
  * @param request - The raw `node:http` upgrade request
1911
1952
  * @param socket - The raw, now-detached `Duplex` connection
1912
1953
  * @param head - The first packet of the upgraded stream, if any
@@ -1786,9 +1786,10 @@ var Negotiator = class {
1786
1786
  * `idle → starting → listening`. A cancelled or expired bind closes its
1787
1787
  * partial server and resets to `idle` for another start. `stop()` transitions
1788
1788
  * to `stopping`: refuses new connections, fires a fresh-per-run stop signal
1789
- * so in-flight handlers observe cancellation, drains up to the `drain`
1790
- * deadline (event-driven, no busy-loop), then closes → `stopped`. `destroy()`
1791
- * is the idempotent final teardown.
1789
+ * so in-flight handlers observe cancellation, drains in-flight requests AND
1790
+ * claimed upgraded sockets up to the `drain` deadline (event-driven, no
1791
+ * busy-loop), then closes → `stopped`, forcing whatever the deadline caught
1792
+ * still open. `destroy()` is the idempotent final teardown.
1792
1793
  * - **Per request.** In-flight is tracked (finished on response `finish` or
1793
1794
  * `close`); a `Request` is built via the router's `buildRequest`, its
1794
1795
  * `signal` LINKED to this run's stop signal via `@orkestrel/abort`'s
@@ -1813,6 +1814,9 @@ var Negotiator = class {
1813
1814
  * - **Upgrade fan-out** — verbatim old semantics (first-claimer-wins, a
1814
1815
  * throwing handler is treated as declined and surfaced on `error`, an
1815
1816
  * unclaimed upgrade destroys the socket), bound per-run to this instance.
1817
+ * A CLAIMED socket joins `#upgraded` until it closes: the claimant still
1818
+ * owns it, and the tracking exists because node detaches an upgraded socket
1819
+ * from the set its own close calls walk, so nothing else can end it.
1816
1820
  * - **Observable (§13).** Owns an {@link Emitter} over {@link ServerEventMap}
1817
1821
  * exposed as `readonly emitter`; the emitter isolates a listener throw and
1818
1822
  * routes it to the `error` OPTION (not the domain `error` event).
@@ -1823,6 +1827,7 @@ var Server = class {
1823
1827
  #state;
1824
1828
  #middleware;
1825
1829
  #upgradeHandlers = [];
1830
+ #upgraded = /* @__PURE__ */ new Set();
1826
1831
  #emitter;
1827
1832
  #host;
1828
1833
  #configuredPort;
@@ -1866,6 +1871,9 @@ var Server = class {
1866
1871
  ...options.error === void 0 ? {} : { error: options.error }
1867
1872
  });
1868
1873
  }
1874
+ get #inflight() {
1875
+ return this.#pending + this.#upgraded.size;
1876
+ }
1869
1877
  get status() {
1870
1878
  return this.#status;
1871
1879
  }
@@ -1911,8 +1919,9 @@ var Server = class {
1911
1919
  await this.#drainPending(deadline.signal);
1912
1920
  deadline.clear();
1913
1921
  const pending = this.#pending;
1914
- this.#emitter.emit("drain", pending);
1915
- if (server !== void 0) await this.#close(server, pending > 0);
1922
+ const upgraded = this.#upgraded.size;
1923
+ this.#emitter.emit("drain", pending, upgraded);
1924
+ if (server !== void 0) await this.#close(server, pending + upgraded > 0);
1916
1925
  this.#http = void 0;
1917
1926
  this.#port = void 0;
1918
1927
  this.#status = "stopped";
@@ -2015,7 +2024,8 @@ var Server = class {
2015
2024
  } catch (error) {
2016
2025
  this.#emitter.emit("error", error);
2017
2026
  }
2018
- if (!handled) socket.destroy();
2027
+ if (handled) this.#trackSocket(socket);
2028
+ else socket.destroy();
2019
2029
  this.#emitter.emit("upgrade", request, handled);
2020
2030
  }
2021
2031
  #resolvePort(server) {
@@ -2075,23 +2085,40 @@ var Server = class {
2075
2085
  #close(server, force) {
2076
2086
  return new Promise((resolve) => {
2077
2087
  server.close(() => resolve());
2078
- if (force) server.closeAllConnections();
2079
- else server.closeIdleConnections();
2088
+ if (force) {
2089
+ server.closeAllConnections();
2090
+ for (const socket of this.#upgraded) socket.destroy();
2091
+ } else server.closeIdleConnections();
2080
2092
  });
2081
2093
  }
2094
+ #enter() {
2095
+ if (this.#inflight === 0) this.#wakeup = createAbort();
2096
+ }
2097
+ #settle() {
2098
+ if (this.#inflight === 0) this.#wakeup.abort();
2099
+ }
2082
2100
  #trackStart() {
2083
- if (this.#pending === 0) this.#wakeup = createAbort();
2101
+ this.#enter();
2084
2102
  this.#pending += 1;
2085
2103
  let finished = false;
2086
2104
  return () => {
2087
2105
  if (finished) return;
2088
2106
  finished = true;
2089
2107
  this.#pending -= 1;
2090
- if (this.#pending === 0) this.#wakeup.abort();
2108
+ this.#settle();
2091
2109
  };
2092
2110
  }
2111
+ #trackSocket(socket) {
2112
+ if (socket.destroyed || this.#upgraded.has(socket)) return;
2113
+ this.#enter();
2114
+ this.#upgraded.add(socket);
2115
+ socket.once("close", () => {
2116
+ this.#upgraded.delete(socket);
2117
+ this.#settle();
2118
+ });
2119
+ }
2093
2120
  async #drainPending(signal) {
2094
- if (this.#pending === 0 || signal.aborted) return;
2121
+ if (this.#inflight === 0 || signal.aborted) return;
2095
2122
  await once(AbortSignal.any([signal, this.#wakeup.signal]), "abort");
2096
2123
  }
2097
2124
  };