@orkestrel/server 0.0.8 → 0.0.10
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.
|
@@ -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
|
*/
|
|
@@ -1473,9 +1488,9 @@ export declare function serializeEvent(message: SSEMessage): string;
|
|
|
1473
1488
|
* `close`); a `Request` is built via the router's `buildRequest`, its
|
|
1474
1489
|
* `signal` LINKED to this run's stop signal via `@orkestrel/abort`'s
|
|
1475
1490
|
* `linkSignal` (a fresh `Request` is constructed with the linked signal —
|
|
1476
|
-
* `buildRequest`'s own abort,
|
|
1477
|
-
* server's stop signal via `AbortSignal.any`,
|
|
1478
|
-
* `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
|
|
1479
1494
|
* {@link ServerOptions.state} from the connection facts; the composed
|
|
1480
1495
|
* middleware onion runs, terminating in `dispatcher.handle`; the result is
|
|
1481
1496
|
* written back via `sendResponse`.
|
|
@@ -1769,10 +1784,23 @@ export declare interface SSEMessage {
|
|
|
1769
1784
|
* SSEMessage}), so any streaming consumer maps its own events onto `write`.
|
|
1770
1785
|
* `response` is the `Response` to return from the route handler (its body is
|
|
1771
1786
|
* the `ReadableStream` this handle writes into). Each `write` serializes the
|
|
1772
|
-
* message to the wire
|
|
1773
|
-
*
|
|
1774
|
-
*
|
|
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
|
|
1775
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.
|
|
1776
1804
|
*/
|
|
1777
1805
|
export declare interface StreamInterface {
|
|
1778
1806
|
/** The streaming `Response` to return from the route handler. */
|
|
@@ -1780,17 +1808,30 @@ export declare interface StreamInterface {
|
|
|
1780
1808
|
/** Whether the underlying stream is done (ended, or the consumer disconnected). */
|
|
1781
1809
|
readonly closed: boolean;
|
|
1782
1810
|
/**
|
|
1783
|
-
* Serialize + enqueue one {@link SSEMessage} to the wire
|
|
1811
|
+
* Serialize + enqueue one {@link SSEMessage} to the wire.
|
|
1784
1812
|
*
|
|
1785
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.
|
|
1786
1818
|
*/
|
|
1787
|
-
write(message: SSEMessage):
|
|
1819
|
+
write(message: SSEMessage): boolean;
|
|
1788
1820
|
/**
|
|
1789
1821
|
* Write a `: text` SSE comment line — a keep-alive a conforming parser ignores.
|
|
1790
1822
|
*
|
|
1791
1823
|
* @param text - The comment text (sent after the `: ` prefix)
|
|
1792
1824
|
*/
|
|
1793
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>;
|
|
1794
1835
|
/** End the stream, completing the response (a no-op once already `closed`). */
|
|
1795
1836
|
end(): void;
|
|
1796
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
|
*/
|
|
@@ -1473,9 +1488,9 @@ export declare function serializeEvent(message: SSEMessage): string;
|
|
|
1473
1488
|
* `close`); a `Request` is built via the router's `buildRequest`, its
|
|
1474
1489
|
* `signal` LINKED to this run's stop signal via `@orkestrel/abort`'s
|
|
1475
1490
|
* `linkSignal` (a fresh `Request` is constructed with the linked signal —
|
|
1476
|
-
* `buildRequest`'s own abort,
|
|
1477
|
-
* server's stop signal via `AbortSignal.any`,
|
|
1478
|
-
* `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
|
|
1479
1494
|
* {@link ServerOptions.state} from the connection facts; the composed
|
|
1480
1495
|
* middleware onion runs, terminating in `dispatcher.handle`; the result is
|
|
1481
1496
|
* written back via `sendResponse`.
|
|
@@ -1769,10 +1784,23 @@ export declare interface SSEMessage {
|
|
|
1769
1784
|
* SSEMessage}), so any streaming consumer maps its own events onto `write`.
|
|
1770
1785
|
* `response` is the `Response` to return from the route handler (its body is
|
|
1771
1786
|
* the `ReadableStream` this handle writes into). Each `write` serializes the
|
|
1772
|
-
* message to the wire
|
|
1773
|
-
*
|
|
1774
|
-
*
|
|
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
|
|
1775
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.
|
|
1776
1804
|
*/
|
|
1777
1805
|
export declare interface StreamInterface {
|
|
1778
1806
|
/** The streaming `Response` to return from the route handler. */
|
|
@@ -1780,17 +1808,30 @@ export declare interface StreamInterface {
|
|
|
1780
1808
|
/** Whether the underlying stream is done (ended, or the consumer disconnected). */
|
|
1781
1809
|
readonly closed: boolean;
|
|
1782
1810
|
/**
|
|
1783
|
-
* Serialize + enqueue one {@link SSEMessage} to the wire
|
|
1811
|
+
* Serialize + enqueue one {@link SSEMessage} to the wire.
|
|
1784
1812
|
*
|
|
1785
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.
|
|
1786
1818
|
*/
|
|
1787
|
-
write(message: SSEMessage):
|
|
1819
|
+
write(message: SSEMessage): boolean;
|
|
1788
1820
|
/**
|
|
1789
1821
|
* Write a `: text` SSE comment line — a keep-alive a conforming parser ignores.
|
|
1790
1822
|
*
|
|
1791
1823
|
* @param text - The comment text (sent after the `: ` prefix)
|
|
1792
1824
|
*/
|
|
1793
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>;
|
|
1794
1835
|
/** End the stream, completing the response (a no-op once already `closed`). */
|
|
1795
1836
|
end(): void;
|
|
1796
1837
|
}
|
package/dist/src/server/index.js
CHANGED
|
@@ -1306,20 +1306,35 @@ function enqueueStreamText(controller, encoder, closed, text) {
|
|
|
1306
1306
|
* Builds the `Response` immediately with {@link import('./constants.js').SSE_HEADERS}
|
|
1307
1307
|
* merged under any `options.headers` (a seam-owned key is never overridden),
|
|
1308
1308
|
* at `options.status` (default `200`). The returned {@link StreamInterface}'s
|
|
1309
|
-
* `write` serializes one message ({@link serializeEvent})
|
|
1309
|
+
* `write` serializes one message ({@link serializeEvent}), enqueues it, and
|
|
1310
|
+
* returns whether the controller still has positive desired size. A producer
|
|
1311
|
+
* receiving `false` can await `drain()`; its event-driven promise resolves on
|
|
1312
|
+
* the next consumer pull that restores capacity, or on stream closure.
|
|
1310
1313
|
* `comment` writes a `: text` keep-alive line a conforming parser ignores;
|
|
1311
1314
|
* `end` closes the stream. Every method is a SAFE NO-OP once `closed` (ended
|
|
1312
1315
|
* by `end()`, or the consumer cancelled the stream), so a late write never
|
|
1313
1316
|
* throws.
|
|
1314
1317
|
*
|
|
1318
|
+
* This readiness reflects only the process-local `ReadableStream` queue — it
|
|
1319
|
+
* is not proof that a remote peer consumed bytes. When the response pump
|
|
1320
|
+
* honors its socket sink's drain state, socket pressure stops body pulls and
|
|
1321
|
+
* therefore keeps this queue full, allowing a cooperative producer to bound
|
|
1322
|
+
* buffering. Ignoring the return value preserves the prior unconditional-
|
|
1323
|
+
* enqueue behavior. The default stream strategy counts chunks rather than
|
|
1324
|
+
* their byte length, so a byte-bounded producer must also cap each message.
|
|
1325
|
+
* Return `response` before awaiting a `false` write, because the consumer
|
|
1326
|
+
* cannot pull until it receives the response.
|
|
1327
|
+
*
|
|
1315
1328
|
* @param options - Optional `status` + extra `headers`; see {@link StreamOptions}
|
|
1316
1329
|
* @returns The {@link StreamInterface} handle
|
|
1317
1330
|
*
|
|
1318
1331
|
* @example
|
|
1319
1332
|
* ```ts
|
|
1320
1333
|
* const stream = openStream()
|
|
1321
|
-
*
|
|
1322
|
-
* stream.
|
|
1334
|
+
* void Promise.resolve().then(async () => {
|
|
1335
|
+
* if (!stream.write({ event: 'token', data: 'hello' })) await stream.drain()
|
|
1336
|
+
* stream.end()
|
|
1337
|
+
* })
|
|
1323
1338
|
* // return stream.response from the route handler
|
|
1324
1339
|
* ```
|
|
1325
1340
|
*/
|
|
@@ -1327,12 +1342,20 @@ function openStream(options) {
|
|
|
1327
1342
|
const encoder = new TextEncoder();
|
|
1328
1343
|
let closed = false;
|
|
1329
1344
|
let controller;
|
|
1345
|
+
let wakeup;
|
|
1330
1346
|
const body = new ReadableStream({
|
|
1331
1347
|
start(streamController) {
|
|
1332
1348
|
controller = streamController;
|
|
1333
1349
|
},
|
|
1350
|
+
pull(streamController) {
|
|
1351
|
+
if (streamController.desiredSize === null || streamController.desiredSize <= 0) return;
|
|
1352
|
+
wakeup?.resolve();
|
|
1353
|
+
wakeup = void 0;
|
|
1354
|
+
},
|
|
1334
1355
|
cancel() {
|
|
1335
1356
|
closed = true;
|
|
1357
|
+
wakeup?.resolve();
|
|
1358
|
+
wakeup = void 0;
|
|
1336
1359
|
}
|
|
1337
1360
|
});
|
|
1338
1361
|
const headers = new Headers({
|
|
@@ -1348,15 +1371,25 @@ function openStream(options) {
|
|
|
1348
1371
|
return closed;
|
|
1349
1372
|
},
|
|
1350
1373
|
write(message) {
|
|
1374
|
+
if (closed || controller === void 0) return false;
|
|
1351
1375
|
enqueueStreamText(controller, encoder, closed, serializeEvent(message));
|
|
1376
|
+
return controller.desiredSize !== null && controller.desiredSize > 0;
|
|
1352
1377
|
},
|
|
1353
1378
|
comment(text) {
|
|
1354
1379
|
enqueueStreamText(controller, encoder, closed, `: ${text}\n\n`);
|
|
1355
1380
|
},
|
|
1381
|
+
drain() {
|
|
1382
|
+
const desired = controller?.desiredSize;
|
|
1383
|
+
if (closed || desired !== void 0 && desired !== null && desired > 0) return Promise.resolve();
|
|
1384
|
+
wakeup ??= Promise.withResolvers();
|
|
1385
|
+
return wakeup.promise;
|
|
1386
|
+
},
|
|
1356
1387
|
end() {
|
|
1357
1388
|
if (closed) return;
|
|
1358
1389
|
closed = true;
|
|
1359
1390
|
controller?.close();
|
|
1391
|
+
wakeup?.resolve();
|
|
1392
|
+
wakeup = void 0;
|
|
1360
1393
|
}
|
|
1361
1394
|
};
|
|
1362
1395
|
}
|
|
@@ -1760,9 +1793,9 @@ var Negotiator = class {
|
|
|
1760
1793
|
* `close`); a `Request` is built via the router's `buildRequest`, its
|
|
1761
1794
|
* `signal` LINKED to this run's stop signal via `@orkestrel/abort`'s
|
|
1762
1795
|
* `linkSignal` (a fresh `Request` is constructed with the linked signal —
|
|
1763
|
-
* `buildRequest`'s own abort,
|
|
1764
|
-
* server's stop signal via `AbortSignal.any`,
|
|
1765
|
-
* `request.signal` observes BOTH); `context.state` is built via
|
|
1796
|
+
* `buildRequest`'s own abort, armed by BOTH request-side and response-side
|
|
1797
|
+
* teardown, composes with the server's stop signal via `AbortSignal.any`,
|
|
1798
|
+
* so a handler awaiting `request.signal` observes BOTH); `context.state` is built via
|
|
1766
1799
|
* {@link ServerOptions.state} from the connection facts; the composed
|
|
1767
1800
|
* middleware onion runs, terminating in `dispatcher.handle`; the result is
|
|
1768
1801
|
* written back via `sendResponse`.
|
|
@@ -1906,7 +1939,7 @@ var Server = class {
|
|
|
1906
1939
|
async #accept(message, response) {
|
|
1907
1940
|
let raw;
|
|
1908
1941
|
try {
|
|
1909
|
-
raw = buildRequest(message);
|
|
1942
|
+
raw = buildRequest(message, { response });
|
|
1910
1943
|
} catch {
|
|
1911
1944
|
await this.#respond(this.#boundary(new HTTPError(400, "invalid request")), response);
|
|
1912
1945
|
return;
|