@orkestrel/server 0.0.29 → 0.0.30
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.
|
@@ -2162,7 +2162,7 @@ export declare interface ServerInterface<TState> {
|
|
|
2162
2162
|
* claimed upgraded sockets, and connections that still carry an open
|
|
2163
2163
|
* request exchange this long to finish before forcing every remaining
|
|
2164
2164
|
* socket closed. Defaults to `DEFAULT_DRAIN_MS`. Must be an integer from
|
|
2165
|
-
* 0 through 2_147_483_647; construction throws a `
|
|
2165
|
+
* 0 through 2_147_483_647; construction throws a `ContractError` of code `range` otherwise.
|
|
2166
2166
|
* A long-lived upgraded socket that nothing closes therefore costs
|
|
2167
2167
|
* `stop()` this whole budget, so a WebSocket handler closes its sockets
|
|
2168
2168
|
* on the `stop` event to settle sooner.
|
|
@@ -2214,7 +2214,7 @@ export declare interface ServerInterface<TState> {
|
|
|
2214
2214
|
* `keepalive` an integer from 0 through 2_147_482_647, so that Node's
|
|
2215
2215
|
* timer of `keepalive` plus its buffer stays within that limit; `headers`
|
|
2216
2216
|
* must not exceed `request` while `request` is positive, Node's own
|
|
2217
|
-
* relation. Construction throws a `
|
|
2217
|
+
* relation. Construction throws a `ContractError` of code `range` otherwise. A response deadline belongs to a middleware, such as
|
|
2218
2218
|
* `@orkestrel/middleware`'s `createDeadline`, which aborts the exchange
|
|
2219
2219
|
* through `context.abort`. A startup expiry rejects with a `DOMException`
|
|
2220
2220
|
* named `TimeoutError`; caller cancellation rejects with the caller
|
|
@@ -2162,7 +2162,7 @@ export declare interface ServerInterface<TState> {
|
|
|
2162
2162
|
* claimed upgraded sockets, and connections that still carry an open
|
|
2163
2163
|
* request exchange this long to finish before forcing every remaining
|
|
2164
2164
|
* socket closed. Defaults to `DEFAULT_DRAIN_MS`. Must be an integer from
|
|
2165
|
-
* 0 through 2_147_483_647; construction throws a `
|
|
2165
|
+
* 0 through 2_147_483_647; construction throws a `ContractError` of code `range` otherwise.
|
|
2166
2166
|
* A long-lived upgraded socket that nothing closes therefore costs
|
|
2167
2167
|
* `stop()` this whole budget, so a WebSocket handler closes its sockets
|
|
2168
2168
|
* on the `stop` event to settle sooner.
|
|
@@ -2214,7 +2214,7 @@ export declare interface ServerInterface<TState> {
|
|
|
2214
2214
|
* `keepalive` an integer from 0 through 2_147_482_647, so that Node's
|
|
2215
2215
|
* timer of `keepalive` plus its buffer stays within that limit; `headers`
|
|
2216
2216
|
* must not exceed `request` while `request` is positive, Node's own
|
|
2217
|
-
* relation. Construction throws a `
|
|
2217
|
+
* relation. Construction throws a `ContractError` of code `range` otherwise. A response deadline belongs to a middleware, such as
|
|
2218
2218
|
* `@orkestrel/middleware`'s `createDeadline`, which aborts the exchange
|
|
2219
2219
|
* through `context.abort`. A startup expiry rejects with a `DOMException`
|
|
2220
2220
|
* named `TimeoutError`; caller cancellation rejects with the caller
|
package/dist/src/server/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { INTRINSICS, attempt, holds, isArray, isError, isFiniteNumber, isFunction, isInstance, isInteger, isNumber, isObject, isPromise, isRecord, isString, parseJSON } from "@orkestrel/contract";
|
|
1
|
+
import { INTRINSICS, attempt, holds, isArray, isCodedError, isError, isFiniteNumber, isFunction, isInstance, isInteger, isNumber, isObject, isPromise, isRecord, isSafeInteger, isString, parseJSON, refuseValue, settle } from "@orkestrel/contract";
|
|
2
2
|
import { addAbortListener, once } from "node:events";
|
|
3
3
|
import { createServer as createServer$1 } from "node:net";
|
|
4
4
|
import { decodeBase64URL, encodeBase64URL, encodeHex } from "@orkestrel/codec";
|
|
@@ -1268,7 +1268,7 @@ function matchesETag(header, etag) {
|
|
|
1268
1268
|
* ```
|
|
1269
1269
|
*/
|
|
1270
1270
|
function parseRange(header, size) {
|
|
1271
|
-
if (header === void 0 || !
|
|
1271
|
+
if (header === void 0 || !isSafeInteger(size) || size < 0) return void 0;
|
|
1272
1272
|
const field = stripOWS(header);
|
|
1273
1273
|
const equals = field.indexOf("=");
|
|
1274
1274
|
if (equals < 0 || field.slice(0, equals).toLowerCase() !== "bytes") return void 0;
|
|
@@ -1547,7 +1547,7 @@ function serializeEvent(message) {
|
|
|
1547
1547
|
const { event, id, retry } = message;
|
|
1548
1548
|
if (event !== void 0 && /[\r\n]/.test(event)) throw new ServerError("EVENT", "an SSE event type must be one line", { field: "event" });
|
|
1549
1549
|
if (id !== void 0 && /[\r\n\0]/.test(id)) throw new ServerError("EVENT", "an SSE id must be one line with no NUL", { field: "id" });
|
|
1550
|
-
if (retry !== void 0 && !(
|
|
1550
|
+
if (retry !== void 0 && !(isSafeInteger(retry) && retry >= 0)) throw new ServerError("EVENT", "an SSE retry must be a non-negative safe integer", { field: "retry" });
|
|
1551
1551
|
return `${[
|
|
1552
1552
|
...event === void 0 ? [] : [`event: ${event}`],
|
|
1553
1553
|
...id === void 0 ? [] : [`id: ${id}`],
|
|
@@ -1882,12 +1882,10 @@ async function probePort(port) {
|
|
|
1882
1882
|
*/
|
|
1883
1883
|
async function discoverPort(preferred) {
|
|
1884
1884
|
if (preferred === void 0) return probePort(0);
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
throw error;
|
|
1890
|
-
}
|
|
1885
|
+
const probed = await settle(() => probePort(preferred));
|
|
1886
|
+
if (probed.success) return probed.value;
|
|
1887
|
+
if (isCodedError(probed.error) && probed.error.code === "EADDRINUSE") return probePort(0);
|
|
1888
|
+
throw probed.error;
|
|
1891
1889
|
}
|
|
1892
1890
|
/**
|
|
1893
1891
|
* Builds the response an error boundary sends for a thrown value: an `HTTPError` whose status the
|
|
@@ -2140,23 +2138,75 @@ var Server = class {
|
|
|
2140
2138
|
#port;
|
|
2141
2139
|
#stopping;
|
|
2142
2140
|
constructor(options) {
|
|
2143
|
-
|
|
2144
|
-
|
|
2141
|
+
refuseValue(options.state, isFunction, "Server", {
|
|
2142
|
+
path: ["options", "state"],
|
|
2143
|
+
limit: "a function",
|
|
2144
|
+
code: "placement"
|
|
2145
|
+
});
|
|
2146
|
+
if (options.report !== void 0) refuseValue(options.report, isFunction, "Server", {
|
|
2147
|
+
path: ["options", "report"],
|
|
2148
|
+
limit: "a function",
|
|
2149
|
+
code: "placement"
|
|
2150
|
+
});
|
|
2145
2151
|
const drain = options.drain ?? 1e4;
|
|
2146
|
-
|
|
2152
|
+
refuseValue(drain, isTimeoutDuration, "Server", {
|
|
2153
|
+
path: ["options", "drain"],
|
|
2154
|
+
limit: "an integer from 0 through 2_147_483_647",
|
|
2155
|
+
code: "range"
|
|
2156
|
+
});
|
|
2147
2157
|
const limit = options.limit ?? 1048576;
|
|
2148
|
-
|
|
2158
|
+
refuseValue(limit, (value) => isFiniteNumber(value) && value >= 0, "Server", {
|
|
2159
|
+
path: ["options", "limit"],
|
|
2160
|
+
limit: "a non-negative finite number",
|
|
2161
|
+
code: "range"
|
|
2162
|
+
});
|
|
2149
2163
|
const timeouts = options.timeouts ?? {};
|
|
2150
2164
|
for (const [name, value] of Object.entries(timeouts)) {
|
|
2151
|
-
|
|
2152
|
-
if (
|
|
2165
|
+
if (value === void 0) continue;
|
|
2166
|
+
if (name === "keepalive") refuseValue(value, (candidate) => isTimeoutDuration(candidate) && isTimeoutDuration(candidate + 1e3), "Server", {
|
|
2167
|
+
path: [
|
|
2168
|
+
"options",
|
|
2169
|
+
"timeouts",
|
|
2170
|
+
name
|
|
2171
|
+
],
|
|
2172
|
+
limit: "an integer from 0 through 2_147_482_647",
|
|
2173
|
+
code: "range"
|
|
2174
|
+
});
|
|
2175
|
+
else refuseValue(value, isTimeoutDuration, "Server", {
|
|
2176
|
+
path: [
|
|
2177
|
+
"options",
|
|
2178
|
+
"timeouts",
|
|
2179
|
+
name
|
|
2180
|
+
],
|
|
2181
|
+
limit: "an integer from 0 through 2_147_483_647",
|
|
2182
|
+
code: "range"
|
|
2183
|
+
});
|
|
2153
2184
|
}
|
|
2154
2185
|
const request = timeouts.request ?? 3e5;
|
|
2155
2186
|
const headers = timeouts.headers ?? Math.min(6e4, request);
|
|
2156
|
-
|
|
2187
|
+
refuseValue(headers, (value) => !(request > 0 && Number(value) > request), "Server", {
|
|
2188
|
+
path: [
|
|
2189
|
+
"options",
|
|
2190
|
+
"timeouts",
|
|
2191
|
+
"headers"
|
|
2192
|
+
],
|
|
2193
|
+
limit: "no greater than options.timeouts.request (300_000 when omitted) while it is positive",
|
|
2194
|
+
code: "range"
|
|
2195
|
+
});
|
|
2157
2196
|
const checked = [headers, request].filter((ms) => ms > 0);
|
|
2158
2197
|
const sockets = options.sockets ?? {};
|
|
2159
|
-
for (const [name, value] of Object.entries(sockets))
|
|
2198
|
+
for (const [name, value] of Object.entries(sockets)) {
|
|
2199
|
+
if (value === void 0) continue;
|
|
2200
|
+
refuseValue(value, (candidate) => isInteger(candidate) && candidate >= 0, "Server", {
|
|
2201
|
+
path: [
|
|
2202
|
+
"options",
|
|
2203
|
+
"sockets",
|
|
2204
|
+
name
|
|
2205
|
+
],
|
|
2206
|
+
limit: "a non-negative integer",
|
|
2207
|
+
code: "range"
|
|
2208
|
+
});
|
|
2209
|
+
}
|
|
2160
2210
|
this.#id = crypto.randomUUID();
|
|
2161
2211
|
this.#dispatcher = options.dispatcher;
|
|
2162
2212
|
this.#state = options.state;
|
|
@@ -2347,11 +2397,10 @@ var Server = class {
|
|
|
2347
2397
|
}
|
|
2348
2398
|
}
|
|
2349
2399
|
async #respond(result, response, line, disconnect) {
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
} catch (error) {
|
|
2400
|
+
const sent = await settle(() => sendResponse(result, response));
|
|
2401
|
+
if (!sent.success) {
|
|
2353
2402
|
response.destroy();
|
|
2354
|
-
if (disconnect?.aborted !== true) this.#fault(error, line);
|
|
2403
|
+
if (disconnect?.aborted !== true) this.#fault(sent.error, line);
|
|
2355
2404
|
return false;
|
|
2356
2405
|
}
|
|
2357
2406
|
return response.writableFinished;
|
|
@@ -2452,15 +2501,15 @@ var Server = class {
|
|
|
2452
2501
|
const open = /* @__PURE__ */ new Map();
|
|
2453
2502
|
this.#exchanges.set(socket, open);
|
|
2454
2503
|
socket.once("close", () => {
|
|
2455
|
-
for (const [response,
|
|
2504
|
+
for (const [response, release] of open) if (response.socket === null) release();
|
|
2456
2505
|
this.#exchanges.delete(socket);
|
|
2457
2506
|
});
|
|
2458
2507
|
}
|
|
2459
|
-
async #trackExchange(message, response,
|
|
2508
|
+
async #trackExchange(message, response, release) {
|
|
2460
2509
|
const socket = message.socket;
|
|
2461
2510
|
const open = this.#exchanges.get(socket);
|
|
2462
2511
|
if (open === void 0) return;
|
|
2463
|
-
open.set(response,
|
|
2512
|
+
open.set(response, release);
|
|
2464
2513
|
await Promise.all([new Promise((resolve) => message.once("close", resolve)), new Promise((resolve) => response.once("close", resolve))]);
|
|
2465
2514
|
open.delete(response);
|
|
2466
2515
|
if (open.size === 0 && this.#exchanges.get(socket) === open && this.#closed) socket.destroy();
|