@orkestrel/server 0.0.29 → 0.0.31

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.
@@ -1269,7 +1269,7 @@ function matchesETag(header, etag) {
1269
1269
  * ```
1270
1270
  */
1271
1271
  function parseRange(header, size) {
1272
- if (header === void 0 || !Number.isSafeInteger(size) || size < 0) return void 0;
1272
+ if (header === void 0 || !(0, _orkestrel_contract.isSafeInteger)(size) || size < 0) return void 0;
1273
1273
  const field = stripOWS(header);
1274
1274
  const equals = field.indexOf("=");
1275
1275
  if (equals < 0 || field.slice(0, equals).toLowerCase() !== "bytes") return void 0;
@@ -1548,7 +1548,7 @@ function serializeEvent(message) {
1548
1548
  const { event, id, retry } = message;
1549
1549
  if (event !== void 0 && /[\r\n]/.test(event)) throw new ServerError("EVENT", "an SSE event type must be one line", { field: "event" });
1550
1550
  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" });
1551
- if (retry !== void 0 && !(Number.isSafeInteger(retry) && retry >= 0)) throw new ServerError("EVENT", "an SSE retry must be a non-negative safe integer", { field: "retry" });
1551
+ if (retry !== void 0 && !((0, _orkestrel_contract.isSafeInteger)(retry) && retry >= 0)) throw new ServerError("EVENT", "an SSE retry must be a non-negative safe integer", { field: "retry" });
1552
1552
  return `${[
1553
1553
  ...event === void 0 ? [] : [`event: ${event}`],
1554
1554
  ...id === void 0 ? [] : [`id: ${id}`],
@@ -1883,12 +1883,10 @@ async function probePort(port) {
1883
1883
  */
1884
1884
  async function discoverPort(preferred) {
1885
1885
  if (preferred === void 0) return probePort(0);
1886
- try {
1887
- return await probePort(preferred);
1888
- } catch (error) {
1889
- if ((0, _orkestrel_contract.isError)(error) && "code" in error && error.code === "EADDRINUSE") return probePort(0);
1890
- throw error;
1891
- }
1886
+ const probed = await (0, _orkestrel_contract.settle)(() => probePort(preferred));
1887
+ if (probed.success) return probed.value;
1888
+ if ((0, _orkestrel_contract.isCodedError)(probed.error) && probed.error.code === "EADDRINUSE") return probePort(0);
1889
+ throw probed.error;
1892
1890
  }
1893
1891
  /**
1894
1892
  * Builds the response an error boundary sends for a thrown value: an `HTTPError` whose status the
@@ -2141,23 +2139,75 @@ var Server = class {
2141
2139
  #port;
2142
2140
  #stopping;
2143
2141
  constructor(options) {
2144
- if (!(0, _orkestrel_contract.isFunction)(options.state)) throw new TypeError("ServerOptions.state must be a function");
2145
- if (options.report !== void 0 && !(0, _orkestrel_contract.isFunction)(options.report)) throw new TypeError("ServerOptions.report must be a function");
2142
+ (0, _orkestrel_contract.refuseValue)(options.state, _orkestrel_contract.isFunction, "Server", {
2143
+ path: ["options", "state"],
2144
+ limit: "a function",
2145
+ code: "placement"
2146
+ });
2147
+ if (options.report !== void 0) (0, _orkestrel_contract.refuseValue)(options.report, _orkestrel_contract.isFunction, "Server", {
2148
+ path: ["options", "report"],
2149
+ limit: "a function",
2150
+ code: "placement"
2151
+ });
2146
2152
  const drain = options.drain ?? 1e4;
2147
- if (!(0, _orkestrel_timeout.isTimeoutDuration)(drain)) throw new TypeError("ServerOptions.drain must be an integer from 0 through 2_147_483_647");
2153
+ (0, _orkestrel_contract.refuseValue)(drain, _orkestrel_timeout.isTimeoutDuration, "Server", {
2154
+ path: ["options", "drain"],
2155
+ limit: "an integer from 0 through 2_147_483_647",
2156
+ code: "range"
2157
+ });
2148
2158
  const limit = options.limit ?? 1048576;
2149
- if (!(0, _orkestrel_contract.isFiniteNumber)(limit) || limit < 0) throw new TypeError("ServerOptions.limit must be a non-negative finite number");
2159
+ (0, _orkestrel_contract.refuseValue)(limit, (value) => (0, _orkestrel_contract.isFiniteNumber)(value) && value >= 0, "Server", {
2160
+ path: ["options", "limit"],
2161
+ limit: "a non-negative finite number",
2162
+ code: "range"
2163
+ });
2150
2164
  const timeouts = options.timeouts ?? {};
2151
2165
  for (const [name, value] of Object.entries(timeouts)) {
2152
- const keepalive = name === "keepalive";
2153
- if (value !== void 0 && !((0, _orkestrel_timeout.isTimeoutDuration)(value) && (!keepalive || (0, _orkestrel_timeout.isTimeoutDuration)(value + 1e3)))) throw new TypeError(`ServerOptions.timeouts.${name} must be an integer from 0 through ${keepalive ? "2_147_482_647" : "2_147_483_647"}`);
2166
+ if (value === void 0) continue;
2167
+ if (name === "keepalive") (0, _orkestrel_contract.refuseValue)(value, (candidate) => (0, _orkestrel_timeout.isTimeoutDuration)(candidate) && (0, _orkestrel_timeout.isTimeoutDuration)(candidate + 1e3), "Server", {
2168
+ path: [
2169
+ "options",
2170
+ "timeouts",
2171
+ name
2172
+ ],
2173
+ limit: "an integer from 0 through 2_147_482_647",
2174
+ code: "range"
2175
+ });
2176
+ else (0, _orkestrel_contract.refuseValue)(value, _orkestrel_timeout.isTimeoutDuration, "Server", {
2177
+ path: [
2178
+ "options",
2179
+ "timeouts",
2180
+ name
2181
+ ],
2182
+ limit: "an integer from 0 through 2_147_483_647",
2183
+ code: "range"
2184
+ });
2154
2185
  }
2155
2186
  const request = timeouts.request ?? 3e5;
2156
2187
  const headers = timeouts.headers ?? Math.min(6e4, request);
2157
- if (request > 0 && headers > request) throw new TypeError("ServerOptions.timeouts.headers must not exceed ServerOptions.timeouts.request (300_000 when omitted) while timeouts.request is positive");
2188
+ (0, _orkestrel_contract.refuseValue)(headers, (value) => !(request > 0 && Number(value) > request), "Server", {
2189
+ path: [
2190
+ "options",
2191
+ "timeouts",
2192
+ "headers"
2193
+ ],
2194
+ limit: "no greater than options.timeouts.request (300_000 when omitted) while it is positive",
2195
+ code: "range"
2196
+ });
2158
2197
  const checked = [headers, request].filter((ms) => ms > 0);
2159
2198
  const sockets = options.sockets ?? {};
2160
- 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`);
2199
+ for (const [name, value] of Object.entries(sockets)) {
2200
+ if (value === void 0) continue;
2201
+ (0, _orkestrel_contract.refuseValue)(value, (candidate) => (0, _orkestrel_contract.isInteger)(candidate) && candidate >= 0, "Server", {
2202
+ path: [
2203
+ "options",
2204
+ "sockets",
2205
+ name
2206
+ ],
2207
+ limit: "a non-negative integer",
2208
+ code: "range"
2209
+ });
2210
+ }
2161
2211
  this.#id = crypto.randomUUID();
2162
2212
  this.#dispatcher = options.dispatcher;
2163
2213
  this.#state = options.state;
@@ -2348,11 +2398,10 @@ var Server = class {
2348
2398
  }
2349
2399
  }
2350
2400
  async #respond(result, response, line, disconnect) {
2351
- try {
2352
- await (0, _orkestrel_router_server.sendResponse)(result, response);
2353
- } catch (error) {
2401
+ const sent = await (0, _orkestrel_contract.settle)(() => (0, _orkestrel_router_server.sendResponse)(result, response));
2402
+ if (!sent.success) {
2354
2403
  response.destroy();
2355
- if (disconnect?.aborted !== true) this.#fault(error, line);
2404
+ if (disconnect?.aborted !== true) this.#fault(sent.error, line);
2356
2405
  return false;
2357
2406
  }
2358
2407
  return response.writableFinished;
@@ -2453,15 +2502,15 @@ var Server = class {
2453
2502
  const open = /* @__PURE__ */ new Map();
2454
2503
  this.#exchanges.set(socket, open);
2455
2504
  socket.once("close", () => {
2456
- for (const [response, settle] of open) if (response.socket === null) settle();
2505
+ for (const [response, release] of open) if (response.socket === null) release();
2457
2506
  this.#exchanges.delete(socket);
2458
2507
  });
2459
2508
  }
2460
- async #trackExchange(message, response, settle) {
2509
+ async #trackExchange(message, response, release) {
2461
2510
  const socket = message.socket;
2462
2511
  const open = this.#exchanges.get(socket);
2463
2512
  if (open === void 0) return;
2464
- open.set(response, settle);
2513
+ open.set(response, release);
2465
2514
  await Promise.all([new Promise((resolve) => message.once("close", resolve)), new Promise((resolve) => response.once("close", resolve))]);
2466
2515
  open.delete(response);
2467
2516
  if (open.size === 0 && this.#exchanges.get(socket) === open && this.#closed) socket.destroy();