@orkestrel/server 0.0.8 → 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.
|
@@ -1307,20 +1307,35 @@ function enqueueStreamText(controller, encoder, closed, text) {
|
|
|
1307
1307
|
* Builds the `Response` immediately with {@link import('./constants.js').SSE_HEADERS}
|
|
1308
1308
|
* merged under any `options.headers` (a seam-owned key is never overridden),
|
|
1309
1309
|
* at `options.status` (default `200`). The returned {@link StreamInterface}'s
|
|
1310
|
-
* `write` serializes one message ({@link serializeEvent})
|
|
1310
|
+
* `write` serializes one message ({@link serializeEvent}), enqueues it, and
|
|
1311
|
+
* returns whether the controller still has positive desired size. A producer
|
|
1312
|
+
* receiving `false` can await `drain()`; its event-driven promise resolves on
|
|
1313
|
+
* the next consumer pull that restores capacity, or on stream closure.
|
|
1311
1314
|
* `comment` writes a `: text` keep-alive line a conforming parser ignores;
|
|
1312
1315
|
* `end` closes the stream. Every method is a SAFE NO-OP once `closed` (ended
|
|
1313
1316
|
* by `end()`, or the consumer cancelled the stream), so a late write never
|
|
1314
1317
|
* throws.
|
|
1315
1318
|
*
|
|
1319
|
+
* This readiness reflects only the process-local `ReadableStream` queue — it
|
|
1320
|
+
* is not proof that a remote peer consumed bytes. When the response pump
|
|
1321
|
+
* honors its socket sink's drain state, socket pressure stops body pulls and
|
|
1322
|
+
* therefore keeps this queue full, allowing a cooperative producer to bound
|
|
1323
|
+
* buffering. Ignoring the return value preserves the prior unconditional-
|
|
1324
|
+
* enqueue behavior. The default stream strategy counts chunks rather than
|
|
1325
|
+
* their byte length, so a byte-bounded producer must also cap each message.
|
|
1326
|
+
* Return `response` before awaiting a `false` write, because the consumer
|
|
1327
|
+
* cannot pull until it receives the response.
|
|
1328
|
+
*
|
|
1316
1329
|
* @param options - Optional `status` + extra `headers`; see {@link StreamOptions}
|
|
1317
1330
|
* @returns The {@link StreamInterface} handle
|
|
1318
1331
|
*
|
|
1319
1332
|
* @example
|
|
1320
1333
|
* ```ts
|
|
1321
1334
|
* const stream = openStream()
|
|
1322
|
-
*
|
|
1323
|
-
* stream.
|
|
1335
|
+
* void Promise.resolve().then(async () => {
|
|
1336
|
+
* if (!stream.write({ event: 'token', data: 'hello' })) await stream.drain()
|
|
1337
|
+
* stream.end()
|
|
1338
|
+
* })
|
|
1324
1339
|
* // return stream.response from the route handler
|
|
1325
1340
|
* ```
|
|
1326
1341
|
*/
|
|
@@ -1328,12 +1343,20 @@ function openStream(options) {
|
|
|
1328
1343
|
const encoder = new TextEncoder();
|
|
1329
1344
|
let closed = false;
|
|
1330
1345
|
let controller;
|
|
1346
|
+
let wakeup;
|
|
1331
1347
|
const body = new ReadableStream({
|
|
1332
1348
|
start(streamController) {
|
|
1333
1349
|
controller = streamController;
|
|
1334
1350
|
},
|
|
1351
|
+
pull(streamController) {
|
|
1352
|
+
if (streamController.desiredSize === null || streamController.desiredSize <= 0) return;
|
|
1353
|
+
wakeup?.resolve();
|
|
1354
|
+
wakeup = void 0;
|
|
1355
|
+
},
|
|
1335
1356
|
cancel() {
|
|
1336
1357
|
closed = true;
|
|
1358
|
+
wakeup?.resolve();
|
|
1359
|
+
wakeup = void 0;
|
|
1337
1360
|
}
|
|
1338
1361
|
});
|
|
1339
1362
|
const headers = new Headers({
|
|
@@ -1349,15 +1372,25 @@ function openStream(options) {
|
|
|
1349
1372
|
return closed;
|
|
1350
1373
|
},
|
|
1351
1374
|
write(message) {
|
|
1375
|
+
if (closed || controller === void 0) return false;
|
|
1352
1376
|
enqueueStreamText(controller, encoder, closed, serializeEvent(message));
|
|
1377
|
+
return controller.desiredSize !== null && controller.desiredSize > 0;
|
|
1353
1378
|
},
|
|
1354
1379
|
comment(text) {
|
|
1355
1380
|
enqueueStreamText(controller, encoder, closed, `: ${text}\n\n`);
|
|
1356
1381
|
},
|
|
1382
|
+
drain() {
|
|
1383
|
+
const desired = controller?.desiredSize;
|
|
1384
|
+
if (closed || desired !== void 0 && desired !== null && desired > 0) return Promise.resolve();
|
|
1385
|
+
wakeup ??= Promise.withResolvers();
|
|
1386
|
+
return wakeup.promise;
|
|
1387
|
+
},
|
|
1357
1388
|
end() {
|
|
1358
1389
|
if (closed) return;
|
|
1359
1390
|
closed = true;
|
|
1360
1391
|
controller?.close();
|
|
1392
|
+
wakeup?.resolve();
|
|
1393
|
+
wakeup = void 0;
|
|
1361
1394
|
}
|
|
1362
1395
|
};
|
|
1363
1396
|
}
|
|
@@ -1761,9 +1794,9 @@ var Negotiator = class {
|
|
|
1761
1794
|
* `close`); a `Request` is built via the router's `buildRequest`, its
|
|
1762
1795
|
* `signal` LINKED to this run's stop signal via `@orkestrel/abort`'s
|
|
1763
1796
|
* `linkSignal` (a fresh `Request` is constructed with the linked signal —
|
|
1764
|
-
* `buildRequest`'s own abort,
|
|
1765
|
-
* server's stop signal via `AbortSignal.any`,
|
|
1766
|
-
* `request.signal` observes BOTH); `context.state` is built via
|
|
1797
|
+
* `buildRequest`'s own abort, armed by BOTH request-side and response-side
|
|
1798
|
+
* teardown, composes with the server's stop signal via `AbortSignal.any`,
|
|
1799
|
+
* so a handler awaiting `request.signal` observes BOTH); `context.state` is built via
|
|
1767
1800
|
* {@link ServerOptions.state} from the connection facts; the composed
|
|
1768
1801
|
* middleware onion runs, terminating in `dispatcher.handle`; the result is
|
|
1769
1802
|
* written back via `sendResponse`.
|
|
@@ -1907,7 +1940,7 @@ var Server = class {
|
|
|
1907
1940
|
async #accept(message, response) {
|
|
1908
1941
|
let raw;
|
|
1909
1942
|
try {
|
|
1910
|
-
raw = (0, _orkestrel_router_server.buildRequest)(message);
|
|
1943
|
+
raw = (0, _orkestrel_router_server.buildRequest)(message, { response });
|
|
1911
1944
|
} catch {
|
|
1912
1945
|
await this.#respond(this.#boundary(new HTTPError(400, "invalid request")), response);
|
|
1913
1946
|
return;
|