@orkestrel/server 0.0.7 → 0.0.9
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 +87 -18
- package/dist/src/server/index.cjs.map +1 -1
- package/dist/src/server/index.d.cts +100 -33
- package/dist/src/server/index.d.ts +100 -33
- package/dist/src/server/index.js +89 -20
- package/dist/src/server/index.js.map +1 -1
- package/package.json +9 -9
|
@@ -1091,20 +1091,35 @@ export declare function normalizeSecret(secret: TokenSecret): readonly string[];
|
|
|
1091
1091
|
* Builds the `Response` immediately with {@link import('./constants.js').SSE_HEADERS}
|
|
1092
1092
|
* merged under any `options.headers` (a seam-owned key is never overridden),
|
|
1093
1093
|
* at `options.status` (default `200`). The returned {@link StreamInterface}'s
|
|
1094
|
-
* `write` serializes one message ({@link serializeEvent})
|
|
1094
|
+
* `write` serializes one message ({@link serializeEvent}), enqueues it, and
|
|
1095
|
+
* returns whether the controller still has positive desired size. A producer
|
|
1096
|
+
* receiving `false` can await `drain()`; its event-driven promise resolves on
|
|
1097
|
+
* the next consumer pull that restores capacity, or on stream closure.
|
|
1095
1098
|
* `comment` writes a `: text` keep-alive line a conforming parser ignores;
|
|
1096
1099
|
* `end` closes the stream. Every method is a SAFE NO-OP once `closed` (ended
|
|
1097
1100
|
* by `end()`, or the consumer cancelled the stream), so a late write never
|
|
1098
1101
|
* throws.
|
|
1099
1102
|
*
|
|
1103
|
+
* This readiness reflects only the process-local `ReadableStream` queue — it
|
|
1104
|
+
* is not proof that a remote peer consumed bytes. When the response pump
|
|
1105
|
+
* honors its socket sink's drain state, socket pressure stops body pulls and
|
|
1106
|
+
* therefore keeps this queue full, allowing a cooperative producer to bound
|
|
1107
|
+
* buffering. Ignoring the return value preserves the prior unconditional-
|
|
1108
|
+
* enqueue behavior. The default stream strategy counts chunks rather than
|
|
1109
|
+
* their byte length, so a byte-bounded producer must also cap each message.
|
|
1110
|
+
* Return `response` before awaiting a `false` write, because the consumer
|
|
1111
|
+
* cannot pull until it receives the response.
|
|
1112
|
+
*
|
|
1100
1113
|
* @param options - Optional `status` + extra `headers`; see {@link StreamOptions}
|
|
1101
1114
|
* @returns The {@link StreamInterface} handle
|
|
1102
1115
|
*
|
|
1103
1116
|
* @example
|
|
1104
1117
|
* ```ts
|
|
1105
1118
|
* const stream = openStream()
|
|
1106
|
-
*
|
|
1107
|
-
* stream.
|
|
1119
|
+
* void Promise.resolve().then(async () => {
|
|
1120
|
+
* if (!stream.write({ event: 'token', data: 'hello' })) await stream.drain()
|
|
1121
|
+
* stream.end()
|
|
1122
|
+
* })
|
|
1108
1123
|
* // return stream.response from the route handler
|
|
1109
1124
|
* ```
|
|
1110
1125
|
*/
|
|
@@ -1458,21 +1473,24 @@ export declare function serializeEvent(message: SSEMessage): string;
|
|
|
1458
1473
|
* @typeParam TState - The consumer's opaque per-request state type
|
|
1459
1474
|
*
|
|
1460
1475
|
* @remarks
|
|
1461
|
-
* - **Lifecycle (AGENTS §10).** `start()` builds the underlying `node:http`
|
|
1476
|
+
* - **Lifecycle (AGENTS §10).** `start(signal?)` builds the underlying `node:http`
|
|
1462
1477
|
* server, binds the configured {@link ServerOptions.host} / {@link
|
|
1463
1478
|
* ServerOptions.port} (omitted/`0` port ⇒ EPHEMERAL, resolved from the
|
|
1464
|
-
* bound address),
|
|
1465
|
-
*
|
|
1466
|
-
*
|
|
1467
|
-
*
|
|
1468
|
-
* `
|
|
1479
|
+
* bound address), observes caller cancellation plus the configured
|
|
1480
|
+
* `timeouts.start` deadline while the bind is pending, and transitions
|
|
1481
|
+
* `idle → starting → listening`. A cancelled or expired bind closes its
|
|
1482
|
+
* partial server and resets to `idle` for another start. `stop()` transitions
|
|
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.
|
|
1469
1487
|
* - **Per request.** In-flight is tracked (finished on response `finish` or
|
|
1470
1488
|
* `close`); a `Request` is built via the router's `buildRequest`, its
|
|
1471
1489
|
* `signal` LINKED to this run's stop signal via `@orkestrel/abort`'s
|
|
1472
1490
|
* `linkSignal` (a fresh `Request` is constructed with the linked signal —
|
|
1473
|
-
* `buildRequest`'s own abort,
|
|
1474
|
-
* server's stop signal via `AbortSignal.any`,
|
|
1475
|
-
* `request.signal` observes BOTH); `context.state` is built via
|
|
1491
|
+
* `buildRequest`'s own abort, armed by BOTH request-side and response-side
|
|
1492
|
+
* teardown, composes with the server's stop signal via `AbortSignal.any`,
|
|
1493
|
+
* so a handler awaiting `request.signal` observes BOTH); `context.state` is built via
|
|
1476
1494
|
* {@link ServerOptions.state} from the connection facts; the composed
|
|
1477
1495
|
* middleware onion runs, terminating in `dispatcher.handle`; the result is
|
|
1478
1496
|
* written back via `sendResponse`.
|
|
@@ -1505,7 +1523,7 @@ export declare class Server<TState> implements ServerInterface<TState> {
|
|
|
1505
1523
|
use(middleware: MiddlewareHandler<TState>): void;
|
|
1506
1524
|
use(middleware: readonly MiddlewareHandler<TState>[]): void;
|
|
1507
1525
|
upgrade(handler: UpgradeHandler): void;
|
|
1508
|
-
start(): Promise<number>;
|
|
1526
|
+
start(signal?: AbortSignal): Promise<number>;
|
|
1509
1527
|
stop(): Promise<void>;
|
|
1510
1528
|
destroy(): Promise<void>;
|
|
1511
1529
|
}
|
|
@@ -1566,18 +1584,19 @@ export declare type ServerEventMap = {
|
|
|
1566
1584
|
*
|
|
1567
1585
|
* @remarks
|
|
1568
1586
|
* `use` adds middleware and `upgrade` registers a protocol-upgrade claimant,
|
|
1569
|
-
* both configurable before or after `start()`. `start()` binds the
|
|
1570
|
-
* `host`/`port` (an omitted/`0` port ⇒ an EPHEMERAL port, resolved
|
|
1571
|
-
* bound address)
|
|
1587
|
+
* both configurable before or after `start()`. `start(signal?)` binds the
|
|
1588
|
+
* configured `host`/`port` (an omitted/`0` port ⇒ an EPHEMERAL port, resolved
|
|
1589
|
+
* from the bound address), observes caller cancellation plus `timeouts.start`
|
|
1590
|
+
* while binding, and resolves the actually-bound port. A cancelled or expired
|
|
1591
|
+
* bind closes its partial server and resets to `idle`. `stop()` refuses new
|
|
1572
1592
|
* connections, fires the stop signal so in-flight handlers can observe it,
|
|
1573
1593
|
* drains up to the configured deadline, then closes. `destroy()` is the final
|
|
1574
1594
|
* idempotent teardown. Per request: a `Request` is built via the router's
|
|
1575
|
-
* `buildRequest` (its signal linked to the server's stop signal), the
|
|
1576
|
-
*
|
|
1577
|
-
*
|
|
1578
|
-
*
|
|
1579
|
-
* hidden-unless-`expose` `500`) so a handler error can never crash the
|
|
1580
|
-
* process.
|
|
1595
|
+
* `buildRequest` (its signal linked to the server's stop signal), the composed
|
|
1596
|
+
* middleware onion runs terminating in `dispatcher.handle`, and the result is
|
|
1597
|
+
* written back via `sendResponse` — every escaping throw is caught by the
|
|
1598
|
+
* built-in boundary (`HTTPError` → its status; anything else → a
|
|
1599
|
+
* hidden-unless-`expose` `500`) so a handler error can never crash the process.
|
|
1581
1600
|
*/
|
|
1582
1601
|
export declare interface ServerInterface<TState> {
|
|
1583
1602
|
readonly id: string;
|
|
@@ -1588,7 +1607,14 @@ export declare interface ServerInterface<TState> {
|
|
|
1588
1607
|
use(middleware: MiddlewareHandler<TState>): void;
|
|
1589
1608
|
use(middleware: readonly MiddlewareHandler<TState>[]): void;
|
|
1590
1609
|
upgrade(handler: UpgradeHandler): void;
|
|
1591
|
-
|
|
1610
|
+
/**
|
|
1611
|
+
* Bind the configured listener and resolve its actually-bound port.
|
|
1612
|
+
*
|
|
1613
|
+
* @param signal - Optional caller cancellation observed only while startup
|
|
1614
|
+
* is pending; aborting after this method resolves does not stop the server.
|
|
1615
|
+
* @returns The actually-bound TCP port
|
|
1616
|
+
*/
|
|
1617
|
+
start(signal?: AbortSignal): Promise<number>;
|
|
1592
1618
|
stop(): Promise<void>;
|
|
1593
1619
|
destroy(): Promise<void>;
|
|
1594
1620
|
}
|
|
@@ -1627,11 +1653,20 @@ export declare interface ServerInterface<TState> {
|
|
|
1627
1653
|
* request's method + parsed `url` when one is available (absent on an
|
|
1628
1654
|
* upgrade-path fault); its own throw is swallowed so reporting can never
|
|
1629
1655
|
* crash the response.
|
|
1630
|
-
* @param timeouts - `node:http` tuning knobs: `
|
|
1631
|
-
*
|
|
1632
|
-
*
|
|
1633
|
-
*
|
|
1634
|
-
*
|
|
1656
|
+
* @param timeouts - Lifecycle and `node:http` tuning knobs: `start` (maximum
|
|
1657
|
+
* time to bind the listener; `0` permits no startup window), `request` (max
|
|
1658
|
+
* time to fully receive + respond, `requestTimeout`), `headers` (max time
|
|
1659
|
+
* to receive the request headers, `headersTimeout`), and `keepalive` (idle
|
|
1660
|
+
* keep-alive socket timeout, `keepAliveTimeout`). `headers` must not exceed
|
|
1661
|
+
* `keepalive` (the Slowloris footgun) — construction throws a `TypeError`
|
|
1662
|
+
* otherwise. Every present value must be a non-negative finite number. A
|
|
1663
|
+
* startup expiry rejects with a `DOMException` named `TimeoutError`; caller
|
|
1664
|
+
* cancellation rejects with the caller signal's `reason`.
|
|
1665
|
+
* @param sockets - `node:http` socket caps: `connections` maps to
|
|
1666
|
+
* `maxConnections` (`0` rejects every incoming connection), `headers` maps
|
|
1667
|
+
* to `maxHeadersCount` (`0` disables the limit), and `requests` maps to
|
|
1668
|
+
* `maxRequestsPerSocket` (`0` disables the limit). Every present value must
|
|
1669
|
+
* be a non-negative integer. Omitted leaves preserve node's defaults.
|
|
1635
1670
|
* @param on - The reserved {@link EmitterHooks} for {@link ServerEventMap}
|
|
1636
1671
|
* (AGENTS §8), wiring initial lifecycle listeners at construction.
|
|
1637
1672
|
* @param error - The emitter's listener-error handler (AGENTS §13) — a
|
|
@@ -1651,10 +1686,16 @@ export declare interface ServerOptions<TState> {
|
|
|
1651
1686
|
url: URL;
|
|
1652
1687
|
}) => void;
|
|
1653
1688
|
readonly timeouts?: {
|
|
1689
|
+
readonly start?: number;
|
|
1654
1690
|
readonly request?: number;
|
|
1655
1691
|
readonly headers?: number;
|
|
1656
1692
|
readonly keepalive?: number;
|
|
1657
1693
|
};
|
|
1694
|
+
readonly sockets?: {
|
|
1695
|
+
readonly connections?: number;
|
|
1696
|
+
readonly headers?: number;
|
|
1697
|
+
readonly requests?: number;
|
|
1698
|
+
};
|
|
1658
1699
|
readonly on?: EmitterHooks<ServerEventMap>;
|
|
1659
1700
|
readonly error?: EmitterErrorHandler;
|
|
1660
1701
|
}
|
|
@@ -1743,10 +1784,23 @@ export declare interface SSEMessage {
|
|
|
1743
1784
|
* SSEMessage}), so any streaming consumer maps its own events onto `write`.
|
|
1744
1785
|
* `response` is the `Response` to return from the route handler (its body is
|
|
1745
1786
|
* the `ReadableStream` this handle writes into). Each `write` serializes the
|
|
1746
|
-
* message to the wire
|
|
1747
|
-
*
|
|
1748
|
-
*
|
|
1787
|
+
* message to the wire, enqueues it, and reports whether that process-local
|
|
1788
|
+
* queue still has capacity; a producer that receives `false` parks on
|
|
1789
|
+
* `drain()` before writing again. `comment` writes a `: text` keep-alive line
|
|
1790
|
+
* (ignored by a conforming SSE parser — no spurious event); `end` closes the
|
|
1791
|
+
* stream. Every method is a SAFE NO-OP once `closed` is `true`, so a late
|
|
1749
1792
|
* `write` never throws.
|
|
1793
|
+
*
|
|
1794
|
+
* The readiness signal is deliberately local: it reflects the
|
|
1795
|
+
* `ReadableStream` controller's queue, not proof that a remote peer consumed
|
|
1796
|
+
* bytes. With a drain-honoring response pump, that queue stops draining while
|
|
1797
|
+
* the process-local socket sink is backpressured, so a cooperative producer
|
|
1798
|
+
* can bound its contribution to transport buffering without polling. A caller
|
|
1799
|
+
* that ignores the boolean keeps the prior unconditional-enqueue behavior.
|
|
1800
|
+
* The stream's default strategy measures queued CHUNKS, not their byte length,
|
|
1801
|
+
* so a producer seeking a byte bound must also bound each individual message.
|
|
1802
|
+
* Return `response` before awaiting a `false` write: the consumer cannot pull
|
|
1803
|
+
* until it receives the response.
|
|
1750
1804
|
*/
|
|
1751
1805
|
export declare interface StreamInterface {
|
|
1752
1806
|
/** The streaming `Response` to return from the route handler. */
|
|
@@ -1754,17 +1808,30 @@ export declare interface StreamInterface {
|
|
|
1754
1808
|
/** Whether the underlying stream is done (ended, or the consumer disconnected). */
|
|
1755
1809
|
readonly closed: boolean;
|
|
1756
1810
|
/**
|
|
1757
|
-
* Serialize + enqueue one {@link SSEMessage} to the wire
|
|
1811
|
+
* Serialize + enqueue one {@link SSEMessage} to the wire.
|
|
1758
1812
|
*
|
|
1759
1813
|
* @param message - The event to send (its `data` split on `\n` into `data:` lines)
|
|
1814
|
+
* @returns `true` when the process-local stream queue has capacity after
|
|
1815
|
+
* accepting the event; `false` when the queue is full or the stream is
|
|
1816
|
+
* closed. A `false` event was still accepted unless `closed` was already
|
|
1817
|
+
* `true`; await {@link drain} before producing another event.
|
|
1760
1818
|
*/
|
|
1761
|
-
write(message: SSEMessage):
|
|
1819
|
+
write(message: SSEMessage): boolean;
|
|
1762
1820
|
/**
|
|
1763
1821
|
* Write a `: text` SSE comment line — a keep-alive a conforming parser ignores.
|
|
1764
1822
|
*
|
|
1765
1823
|
* @param text - The comment text (sent after the `: ` prefix)
|
|
1766
1824
|
*/
|
|
1767
1825
|
comment(text: string): void;
|
|
1826
|
+
/**
|
|
1827
|
+
* Park until the process-local stream queue has capacity again.
|
|
1828
|
+
*
|
|
1829
|
+
* @returns A promise that resolves when a consumer pull restores positive
|
|
1830
|
+
* desired size, or immediately when capacity is already available or the
|
|
1831
|
+
* stream is closed. The wakeup is event-driven; it does not poll and does
|
|
1832
|
+
* not prove that a remote peer consumed the queued bytes.
|
|
1833
|
+
*/
|
|
1834
|
+
drain(): Promise<void>;
|
|
1768
1835
|
/** End the stream, completing the response (a no-op once already `closed`). */
|
|
1769
1836
|
end(): void;
|
|
1770
1837
|
}
|
|
@@ -1091,20 +1091,35 @@ export declare function normalizeSecret(secret: TokenSecret): readonly string[];
|
|
|
1091
1091
|
* Builds the `Response` immediately with {@link import('./constants.js').SSE_HEADERS}
|
|
1092
1092
|
* merged under any `options.headers` (a seam-owned key is never overridden),
|
|
1093
1093
|
* at `options.status` (default `200`). The returned {@link StreamInterface}'s
|
|
1094
|
-
* `write` serializes one message ({@link serializeEvent})
|
|
1094
|
+
* `write` serializes one message ({@link serializeEvent}), enqueues it, and
|
|
1095
|
+
* returns whether the controller still has positive desired size. A producer
|
|
1096
|
+
* receiving `false` can await `drain()`; its event-driven promise resolves on
|
|
1097
|
+
* the next consumer pull that restores capacity, or on stream closure.
|
|
1095
1098
|
* `comment` writes a `: text` keep-alive line a conforming parser ignores;
|
|
1096
1099
|
* `end` closes the stream. Every method is a SAFE NO-OP once `closed` (ended
|
|
1097
1100
|
* by `end()`, or the consumer cancelled the stream), so a late write never
|
|
1098
1101
|
* throws.
|
|
1099
1102
|
*
|
|
1103
|
+
* This readiness reflects only the process-local `ReadableStream` queue — it
|
|
1104
|
+
* is not proof that a remote peer consumed bytes. When the response pump
|
|
1105
|
+
* honors its socket sink's drain state, socket pressure stops body pulls and
|
|
1106
|
+
* therefore keeps this queue full, allowing a cooperative producer to bound
|
|
1107
|
+
* buffering. Ignoring the return value preserves the prior unconditional-
|
|
1108
|
+
* enqueue behavior. The default stream strategy counts chunks rather than
|
|
1109
|
+
* their byte length, so a byte-bounded producer must also cap each message.
|
|
1110
|
+
* Return `response` before awaiting a `false` write, because the consumer
|
|
1111
|
+
* cannot pull until it receives the response.
|
|
1112
|
+
*
|
|
1100
1113
|
* @param options - Optional `status` + extra `headers`; see {@link StreamOptions}
|
|
1101
1114
|
* @returns The {@link StreamInterface} handle
|
|
1102
1115
|
*
|
|
1103
1116
|
* @example
|
|
1104
1117
|
* ```ts
|
|
1105
1118
|
* const stream = openStream()
|
|
1106
|
-
*
|
|
1107
|
-
* stream.
|
|
1119
|
+
* void Promise.resolve().then(async () => {
|
|
1120
|
+
* if (!stream.write({ event: 'token', data: 'hello' })) await stream.drain()
|
|
1121
|
+
* stream.end()
|
|
1122
|
+
* })
|
|
1108
1123
|
* // return stream.response from the route handler
|
|
1109
1124
|
* ```
|
|
1110
1125
|
*/
|
|
@@ -1458,21 +1473,24 @@ export declare function serializeEvent(message: SSEMessage): string;
|
|
|
1458
1473
|
* @typeParam TState - The consumer's opaque per-request state type
|
|
1459
1474
|
*
|
|
1460
1475
|
* @remarks
|
|
1461
|
-
* - **Lifecycle (AGENTS §10).** `start()` builds the underlying `node:http`
|
|
1476
|
+
* - **Lifecycle (AGENTS §10).** `start(signal?)` builds the underlying `node:http`
|
|
1462
1477
|
* server, binds the configured {@link ServerOptions.host} / {@link
|
|
1463
1478
|
* ServerOptions.port} (omitted/`0` port ⇒ EPHEMERAL, resolved from the
|
|
1464
|
-
* bound address),
|
|
1465
|
-
*
|
|
1466
|
-
*
|
|
1467
|
-
*
|
|
1468
|
-
* `
|
|
1479
|
+
* bound address), observes caller cancellation plus the configured
|
|
1480
|
+
* `timeouts.start` deadline while the bind is pending, and transitions
|
|
1481
|
+
* `idle → starting → listening`. A cancelled or expired bind closes its
|
|
1482
|
+
* partial server and resets to `idle` for another start. `stop()` transitions
|
|
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.
|
|
1469
1487
|
* - **Per request.** In-flight is tracked (finished on response `finish` or
|
|
1470
1488
|
* `close`); a `Request` is built via the router's `buildRequest`, its
|
|
1471
1489
|
* `signal` LINKED to this run's stop signal via `@orkestrel/abort`'s
|
|
1472
1490
|
* `linkSignal` (a fresh `Request` is constructed with the linked signal —
|
|
1473
|
-
* `buildRequest`'s own abort,
|
|
1474
|
-
* server's stop signal via `AbortSignal.any`,
|
|
1475
|
-
* `request.signal` observes BOTH); `context.state` is built via
|
|
1491
|
+
* `buildRequest`'s own abort, armed by BOTH request-side and response-side
|
|
1492
|
+
* teardown, composes with the server's stop signal via `AbortSignal.any`,
|
|
1493
|
+
* so a handler awaiting `request.signal` observes BOTH); `context.state` is built via
|
|
1476
1494
|
* {@link ServerOptions.state} from the connection facts; the composed
|
|
1477
1495
|
* middleware onion runs, terminating in `dispatcher.handle`; the result is
|
|
1478
1496
|
* written back via `sendResponse`.
|
|
@@ -1505,7 +1523,7 @@ export declare class Server<TState> implements ServerInterface<TState> {
|
|
|
1505
1523
|
use(middleware: MiddlewareHandler<TState>): void;
|
|
1506
1524
|
use(middleware: readonly MiddlewareHandler<TState>[]): void;
|
|
1507
1525
|
upgrade(handler: UpgradeHandler): void;
|
|
1508
|
-
start(): Promise<number>;
|
|
1526
|
+
start(signal?: AbortSignal): Promise<number>;
|
|
1509
1527
|
stop(): Promise<void>;
|
|
1510
1528
|
destroy(): Promise<void>;
|
|
1511
1529
|
}
|
|
@@ -1566,18 +1584,19 @@ export declare type ServerEventMap = {
|
|
|
1566
1584
|
*
|
|
1567
1585
|
* @remarks
|
|
1568
1586
|
* `use` adds middleware and `upgrade` registers a protocol-upgrade claimant,
|
|
1569
|
-
* both configurable before or after `start()`. `start()` binds the
|
|
1570
|
-
* `host`/`port` (an omitted/`0` port ⇒ an EPHEMERAL port, resolved
|
|
1571
|
-
* bound address)
|
|
1587
|
+
* both configurable before or after `start()`. `start(signal?)` binds the
|
|
1588
|
+
* configured `host`/`port` (an omitted/`0` port ⇒ an EPHEMERAL port, resolved
|
|
1589
|
+
* from the bound address), observes caller cancellation plus `timeouts.start`
|
|
1590
|
+
* while binding, and resolves the actually-bound port. A cancelled or expired
|
|
1591
|
+
* bind closes its partial server and resets to `idle`. `stop()` refuses new
|
|
1572
1592
|
* connections, fires the stop signal so in-flight handlers can observe it,
|
|
1573
1593
|
* drains up to the configured deadline, then closes. `destroy()` is the final
|
|
1574
1594
|
* idempotent teardown. Per request: a `Request` is built via the router's
|
|
1575
|
-
* `buildRequest` (its signal linked to the server's stop signal), the
|
|
1576
|
-
*
|
|
1577
|
-
*
|
|
1578
|
-
*
|
|
1579
|
-
* hidden-unless-`expose` `500`) so a handler error can never crash the
|
|
1580
|
-
* process.
|
|
1595
|
+
* `buildRequest` (its signal linked to the server's stop signal), the composed
|
|
1596
|
+
* middleware onion runs terminating in `dispatcher.handle`, and the result is
|
|
1597
|
+
* written back via `sendResponse` — every escaping throw is caught by the
|
|
1598
|
+
* built-in boundary (`HTTPError` → its status; anything else → a
|
|
1599
|
+
* hidden-unless-`expose` `500`) so a handler error can never crash the process.
|
|
1581
1600
|
*/
|
|
1582
1601
|
export declare interface ServerInterface<TState> {
|
|
1583
1602
|
readonly id: string;
|
|
@@ -1588,7 +1607,14 @@ export declare interface ServerInterface<TState> {
|
|
|
1588
1607
|
use(middleware: MiddlewareHandler<TState>): void;
|
|
1589
1608
|
use(middleware: readonly MiddlewareHandler<TState>[]): void;
|
|
1590
1609
|
upgrade(handler: UpgradeHandler): void;
|
|
1591
|
-
|
|
1610
|
+
/**
|
|
1611
|
+
* Bind the configured listener and resolve its actually-bound port.
|
|
1612
|
+
*
|
|
1613
|
+
* @param signal - Optional caller cancellation observed only while startup
|
|
1614
|
+
* is pending; aborting after this method resolves does not stop the server.
|
|
1615
|
+
* @returns The actually-bound TCP port
|
|
1616
|
+
*/
|
|
1617
|
+
start(signal?: AbortSignal): Promise<number>;
|
|
1592
1618
|
stop(): Promise<void>;
|
|
1593
1619
|
destroy(): Promise<void>;
|
|
1594
1620
|
}
|
|
@@ -1627,11 +1653,20 @@ export declare interface ServerInterface<TState> {
|
|
|
1627
1653
|
* request's method + parsed `url` when one is available (absent on an
|
|
1628
1654
|
* upgrade-path fault); its own throw is swallowed so reporting can never
|
|
1629
1655
|
* crash the response.
|
|
1630
|
-
* @param timeouts - `node:http` tuning knobs: `
|
|
1631
|
-
*
|
|
1632
|
-
*
|
|
1633
|
-
*
|
|
1634
|
-
*
|
|
1656
|
+
* @param timeouts - Lifecycle and `node:http` tuning knobs: `start` (maximum
|
|
1657
|
+
* time to bind the listener; `0` permits no startup window), `request` (max
|
|
1658
|
+
* time to fully receive + respond, `requestTimeout`), `headers` (max time
|
|
1659
|
+
* to receive the request headers, `headersTimeout`), and `keepalive` (idle
|
|
1660
|
+
* keep-alive socket timeout, `keepAliveTimeout`). `headers` must not exceed
|
|
1661
|
+
* `keepalive` (the Slowloris footgun) — construction throws a `TypeError`
|
|
1662
|
+
* otherwise. Every present value must be a non-negative finite number. A
|
|
1663
|
+
* startup expiry rejects with a `DOMException` named `TimeoutError`; caller
|
|
1664
|
+
* cancellation rejects with the caller signal's `reason`.
|
|
1665
|
+
* @param sockets - `node:http` socket caps: `connections` maps to
|
|
1666
|
+
* `maxConnections` (`0` rejects every incoming connection), `headers` maps
|
|
1667
|
+
* to `maxHeadersCount` (`0` disables the limit), and `requests` maps to
|
|
1668
|
+
* `maxRequestsPerSocket` (`0` disables the limit). Every present value must
|
|
1669
|
+
* be a non-negative integer. Omitted leaves preserve node's defaults.
|
|
1635
1670
|
* @param on - The reserved {@link EmitterHooks} for {@link ServerEventMap}
|
|
1636
1671
|
* (AGENTS §8), wiring initial lifecycle listeners at construction.
|
|
1637
1672
|
* @param error - The emitter's listener-error handler (AGENTS §13) — a
|
|
@@ -1651,10 +1686,16 @@ export declare interface ServerOptions<TState> {
|
|
|
1651
1686
|
url: URL;
|
|
1652
1687
|
}) => void;
|
|
1653
1688
|
readonly timeouts?: {
|
|
1689
|
+
readonly start?: number;
|
|
1654
1690
|
readonly request?: number;
|
|
1655
1691
|
readonly headers?: number;
|
|
1656
1692
|
readonly keepalive?: number;
|
|
1657
1693
|
};
|
|
1694
|
+
readonly sockets?: {
|
|
1695
|
+
readonly connections?: number;
|
|
1696
|
+
readonly headers?: number;
|
|
1697
|
+
readonly requests?: number;
|
|
1698
|
+
};
|
|
1658
1699
|
readonly on?: EmitterHooks<ServerEventMap>;
|
|
1659
1700
|
readonly error?: EmitterErrorHandler;
|
|
1660
1701
|
}
|
|
@@ -1743,10 +1784,23 @@ export declare interface SSEMessage {
|
|
|
1743
1784
|
* SSEMessage}), so any streaming consumer maps its own events onto `write`.
|
|
1744
1785
|
* `response` is the `Response` to return from the route handler (its body is
|
|
1745
1786
|
* the `ReadableStream` this handle writes into). Each `write` serializes the
|
|
1746
|
-
* message to the wire
|
|
1747
|
-
*
|
|
1748
|
-
*
|
|
1787
|
+
* message to the wire, enqueues it, and reports whether that process-local
|
|
1788
|
+
* queue still has capacity; a producer that receives `false` parks on
|
|
1789
|
+
* `drain()` before writing again. `comment` writes a `: text` keep-alive line
|
|
1790
|
+
* (ignored by a conforming SSE parser — no spurious event); `end` closes the
|
|
1791
|
+
* stream. Every method is a SAFE NO-OP once `closed` is `true`, so a late
|
|
1749
1792
|
* `write` never throws.
|
|
1793
|
+
*
|
|
1794
|
+
* The readiness signal is deliberately local: it reflects the
|
|
1795
|
+
* `ReadableStream` controller's queue, not proof that a remote peer consumed
|
|
1796
|
+
* bytes. With a drain-honoring response pump, that queue stops draining while
|
|
1797
|
+
* the process-local socket sink is backpressured, so a cooperative producer
|
|
1798
|
+
* can bound its contribution to transport buffering without polling. A caller
|
|
1799
|
+
* that ignores the boolean keeps the prior unconditional-enqueue behavior.
|
|
1800
|
+
* The stream's default strategy measures queued CHUNKS, not their byte length,
|
|
1801
|
+
* so a producer seeking a byte bound must also bound each individual message.
|
|
1802
|
+
* Return `response` before awaiting a `false` write: the consumer cannot pull
|
|
1803
|
+
* until it receives the response.
|
|
1750
1804
|
*/
|
|
1751
1805
|
export declare interface StreamInterface {
|
|
1752
1806
|
/** The streaming `Response` to return from the route handler. */
|
|
@@ -1754,17 +1808,30 @@ export declare interface StreamInterface {
|
|
|
1754
1808
|
/** Whether the underlying stream is done (ended, or the consumer disconnected). */
|
|
1755
1809
|
readonly closed: boolean;
|
|
1756
1810
|
/**
|
|
1757
|
-
* Serialize + enqueue one {@link SSEMessage} to the wire
|
|
1811
|
+
* Serialize + enqueue one {@link SSEMessage} to the wire.
|
|
1758
1812
|
*
|
|
1759
1813
|
* @param message - The event to send (its `data` split on `\n` into `data:` lines)
|
|
1814
|
+
* @returns `true` when the process-local stream queue has capacity after
|
|
1815
|
+
* accepting the event; `false` when the queue is full or the stream is
|
|
1816
|
+
* closed. A `false` event was still accepted unless `closed` was already
|
|
1817
|
+
* `true`; await {@link drain} before producing another event.
|
|
1760
1818
|
*/
|
|
1761
|
-
write(message: SSEMessage):
|
|
1819
|
+
write(message: SSEMessage): boolean;
|
|
1762
1820
|
/**
|
|
1763
1821
|
* Write a `: text` SSE comment line — a keep-alive a conforming parser ignores.
|
|
1764
1822
|
*
|
|
1765
1823
|
* @param text - The comment text (sent after the `: ` prefix)
|
|
1766
1824
|
*/
|
|
1767
1825
|
comment(text: string): void;
|
|
1826
|
+
/**
|
|
1827
|
+
* Park until the process-local stream queue has capacity again.
|
|
1828
|
+
*
|
|
1829
|
+
* @returns A promise that resolves when a consumer pull restores positive
|
|
1830
|
+
* desired size, or immediately when capacity is already available or the
|
|
1831
|
+
* stream is closed. The wakeup is event-driven; it does not poll and does
|
|
1832
|
+
* not prove that a remote peer consumed the queued bytes.
|
|
1833
|
+
*/
|
|
1834
|
+
drain(): Promise<void>;
|
|
1768
1835
|
/** End the stream, completing the response (a no-op once already `closed`). */
|
|
1769
1836
|
end(): void;
|
|
1770
1837
|
}
|