@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
|
@@ -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
|
}
|
|
@@ -1746,21 +1779,24 @@ var Negotiator = class {
|
|
|
1746
1779
|
* @typeParam TState - The consumer's opaque per-request state type
|
|
1747
1780
|
*
|
|
1748
1781
|
* @remarks
|
|
1749
|
-
* - **Lifecycle (AGENTS §10).** `start()` builds the underlying `node:http`
|
|
1782
|
+
* - **Lifecycle (AGENTS §10).** `start(signal?)` builds the underlying `node:http`
|
|
1750
1783
|
* server, binds the configured {@link ServerOptions.host} / {@link
|
|
1751
1784
|
* ServerOptions.port} (omitted/`0` port ⇒ EPHEMERAL, resolved from the
|
|
1752
|
-
* bound address),
|
|
1753
|
-
*
|
|
1754
|
-
*
|
|
1755
|
-
*
|
|
1756
|
-
* `
|
|
1785
|
+
* bound address), observes caller cancellation plus the configured
|
|
1786
|
+
* `timeouts.start` deadline while the bind is pending, and transitions
|
|
1787
|
+
* `idle → starting → listening`. A cancelled or expired bind closes its
|
|
1788
|
+
* partial server and resets to `idle` for another start. `stop()` transitions
|
|
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.
|
|
1757
1793
|
* - **Per request.** In-flight is tracked (finished on response `finish` or
|
|
1758
1794
|
* `close`); a `Request` is built via the router's `buildRequest`, its
|
|
1759
1795
|
* `signal` LINKED to this run's stop signal via `@orkestrel/abort`'s
|
|
1760
1796
|
* `linkSignal` (a fresh `Request` is constructed with the linked signal —
|
|
1761
|
-
* `buildRequest`'s own abort,
|
|
1762
|
-
* server's stop signal via `AbortSignal.any`,
|
|
1763
|
-
* `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
|
|
1764
1800
|
* {@link ServerOptions.state} from the connection facts; the composed
|
|
1765
1801
|
* middleware onion runs, terminating in `dispatcher.handle`; the result is
|
|
1766
1802
|
* written back via `sendResponse`.
|
|
@@ -1796,6 +1832,7 @@ var Server = class {
|
|
|
1796
1832
|
#expose;
|
|
1797
1833
|
#report;
|
|
1798
1834
|
#timeouts;
|
|
1835
|
+
#sockets;
|
|
1799
1836
|
#http;
|
|
1800
1837
|
#abort = (0, _orkestrel_abort.createAbort)();
|
|
1801
1838
|
#wakeup = (0, _orkestrel_abort.createAbort)();
|
|
@@ -1812,6 +1849,8 @@ var Server = class {
|
|
|
1812
1849
|
const timeouts = options.timeouts ?? {};
|
|
1813
1850
|
for (const [name, value] of Object.entries(timeouts)) if (value !== void 0 && (!(0, _orkestrel_contract.isFiniteNumber)(value) || value < 0)) throw new TypeError(`ServerOptions.timeouts.${name} must be a non-negative finite number`);
|
|
1814
1851
|
if (timeouts.headers !== void 0 && timeouts.keepalive !== void 0 && timeouts.headers > timeouts.keepalive) throw new TypeError("ServerOptions.timeouts.headers must not exceed timeouts.keepalive");
|
|
1852
|
+
const sockets = options.sockets ?? {};
|
|
1853
|
+
for (const [name, value] of Object.entries(sockets)) if (value !== void 0 && (!(0, _orkestrel_contract.isInteger)(value) || value < 0)) throw new TypeError(`ServerOptions.sockets.${name} must be a non-negative integer`);
|
|
1815
1854
|
this.#dispatcher = options.dispatcher;
|
|
1816
1855
|
this.#state = options.state;
|
|
1817
1856
|
this.#middleware = options.middleware === void 0 ? [] : [...options.middleware];
|
|
@@ -1822,6 +1861,7 @@ var Server = class {
|
|
|
1822
1861
|
this.#expose = options.expose ?? false;
|
|
1823
1862
|
this.#report = options.report;
|
|
1824
1863
|
this.#timeouts = timeouts;
|
|
1864
|
+
this.#sockets = sockets;
|
|
1825
1865
|
this.#emitter = new _orkestrel_emitter.Emitter({
|
|
1826
1866
|
...options.on === void 0 ? {} : { on: options.on },
|
|
1827
1867
|
...options.error === void 0 ? {} : { error: options.error }
|
|
@@ -1846,7 +1886,7 @@ var Server = class {
|
|
|
1846
1886
|
upgrade(handler) {
|
|
1847
1887
|
this.#upgradeHandlers.push(handler);
|
|
1848
1888
|
}
|
|
1849
|
-
start() {
|
|
1889
|
+
start(signal) {
|
|
1850
1890
|
if (this.#status !== "idle" && this.#status !== "stopped") return Promise.reject(/* @__PURE__ */ new Error(`server cannot start from '${this.#status}'`));
|
|
1851
1891
|
this.#status = "starting";
|
|
1852
1892
|
this.#abort = (0, _orkestrel_abort.createAbort)();
|
|
@@ -1854,9 +1894,12 @@ var Server = class {
|
|
|
1854
1894
|
if (this.#timeouts.request !== void 0) server.requestTimeout = this.#timeouts.request;
|
|
1855
1895
|
if (this.#timeouts.headers !== void 0) server.headersTimeout = this.#timeouts.headers;
|
|
1856
1896
|
if (this.#timeouts.keepalive !== void 0) server.keepAliveTimeout = this.#timeouts.keepalive;
|
|
1897
|
+
if (this.#sockets.connections !== void 0) server.maxConnections = this.#sockets.connections;
|
|
1898
|
+
if (this.#sockets.headers !== void 0) server.maxHeadersCount = this.#sockets.headers;
|
|
1899
|
+
if (this.#sockets.requests !== void 0) server.maxRequestsPerSocket = this.#sockets.requests;
|
|
1857
1900
|
server.on("upgrade", (request, socket, head) => this.#onUpgrade(request, socket, head));
|
|
1858
1901
|
this.#http = server;
|
|
1859
|
-
return this.#listen(server);
|
|
1902
|
+
return this.#listen(server, signal);
|
|
1860
1903
|
}
|
|
1861
1904
|
async stop() {
|
|
1862
1905
|
if (this.#status !== "listening") return;
|
|
@@ -1897,7 +1940,7 @@ var Server = class {
|
|
|
1897
1940
|
async #accept(message, response) {
|
|
1898
1941
|
let raw;
|
|
1899
1942
|
try {
|
|
1900
|
-
raw = (0, _orkestrel_router_server.buildRequest)(message);
|
|
1943
|
+
raw = (0, _orkestrel_router_server.buildRequest)(message, { response });
|
|
1901
1944
|
} catch {
|
|
1902
1945
|
await this.#respond(this.#boundary(new HTTPError(400, "invalid request")), response);
|
|
1903
1946
|
return;
|
|
@@ -1981,15 +2024,41 @@ var Server = class {
|
|
|
1981
2024
|
if (isAddressInfo(address)) return address.port;
|
|
1982
2025
|
return 0;
|
|
1983
2026
|
}
|
|
1984
|
-
async #listen(server) {
|
|
2027
|
+
async #listen(server, signal) {
|
|
2028
|
+
const deadline = this.#timeouts.start === void 0 ? void 0 : (0, _orkestrel_timeout.createTimeout)({ ms: this.#timeouts.start });
|
|
2029
|
+
const startup = deadline === void 0 ? signal : (0, _orkestrel_abort.linkSignal)(deadline.signal, signal);
|
|
2030
|
+
const binding = (0, _orkestrel_abort.createAbort)();
|
|
2031
|
+
const relay = startup === void 0 ? void 0 : (0, node_events.addAbortListener)(startup, () => binding.abort(startup.reason));
|
|
2032
|
+
let listening;
|
|
1985
2033
|
try {
|
|
1986
|
-
|
|
1987
|
-
|
|
2034
|
+
signal?.throwIfAborted();
|
|
2035
|
+
deadline?.start();
|
|
2036
|
+
if (deadline?.ms === 0 && startup !== void 0) await (0, node_events.once)(startup, "abort");
|
|
2037
|
+
startup?.throwIfAborted();
|
|
2038
|
+
listening = (0, node_events.once)(server, "listening", { signal: binding.signal });
|
|
2039
|
+
if (startup === void 0) server.listen(this.#configuredPort ?? 0, this.#host);
|
|
2040
|
+
else server.listen({
|
|
2041
|
+
port: this.#configuredPort ?? 0,
|
|
2042
|
+
...this.#host === void 0 ? {} : { host: this.#host },
|
|
2043
|
+
signal: binding.signal
|
|
2044
|
+
});
|
|
1988
2045
|
await listening;
|
|
1989
2046
|
} catch (error) {
|
|
2047
|
+
const expired = deadline?.expired === true;
|
|
2048
|
+
const cancelled = startup?.aborted === true;
|
|
2049
|
+
server.on("error", () => void 0);
|
|
2050
|
+
binding.abort(error);
|
|
2051
|
+
if (listening !== void 0) await listening.catch(() => void 0);
|
|
2052
|
+
await this.#close(server, true);
|
|
1990
2053
|
this.#http = void 0;
|
|
2054
|
+
this.#port = void 0;
|
|
1991
2055
|
this.#status = "idle";
|
|
2056
|
+
if (expired && deadline !== void 0) throw new DOMException(`Server startup exceeded ${deadline.ms} milliseconds`, "TimeoutError");
|
|
2057
|
+
if (cancelled && startup !== void 0) throw startup.reason;
|
|
1992
2058
|
throw error;
|
|
2059
|
+
} finally {
|
|
2060
|
+
relay?.[Symbol.dispose]();
|
|
2061
|
+
deadline?.clear();
|
|
1993
2062
|
}
|
|
1994
2063
|
const port = this.#resolvePort(server);
|
|
1995
2064
|
this.#port = port;
|