@orkestrel/server 0.0.19 → 0.0.20

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.
@@ -216,8 +216,8 @@ var ContentTooLargeError = class extends HTTPError {
216
216
  * ```
217
217
  */
218
218
  function isHTTPError(value) {
219
- if (value instanceof HTTPError) return true;
220
- if (typeof value !== "object" || value === null) return false;
219
+ if ((0, _orkestrel_contract.isInstance)(value, HTTPError)) return true;
220
+ if (!(0, _orkestrel_contract.isObject)(value)) return false;
221
221
  if (!(HTTP_ERROR_BRAND in value) || value[HTTP_ERROR_BRAND] !== true) return false;
222
222
  if (!("status" in value) || !("message" in value)) return false;
223
223
  return (0, _orkestrel_contract.isNumber)(value.status) && (0, _orkestrel_contract.isString)(value.message);
@@ -285,7 +285,7 @@ var ServerError = class extends Error {
285
285
  * ```
286
286
  */
287
287
  function isServerError(value) {
288
- return value instanceof ServerError;
288
+ return (0, _orkestrel_contract.isInstance)(value, ServerError);
289
289
  }
290
290
  //#endregion
291
291
  //#region src/server/validators.ts
@@ -727,9 +727,8 @@ async function verifyToken(token, secret) {
727
727
  * Decodes the payload with `@orkestrel/codec`'s `decodeBase64URL` (total — a
728
728
  * non-canonical base64url segment answers `undefined` rather than throwing),
729
729
  * reads it as UTF-8 JSON, narrows it to a record with a string `value` without
730
- * `as`, and rejects an expired `exp`. Total — any
731
- * decode/shape/expiry failure yields `undefined`; only `JSON.parse` still
732
- * throws, and its `catch` answers `undefined` too.
730
+ * `as`, and rejects an expired `exp`. Total — any decode/shape/expiry failure
731
+ * yields `undefined`, including invalid JSON (`parseJSON` answers `undefined`).
733
732
  *
734
733
  * @param encoded - The base64url-encoded payload segment (before the last `.`)
735
734
  * @returns The embedded value when the payload is well-shaped + unexpired, else `undefined`
@@ -737,16 +736,12 @@ async function verifyToken(token, secret) {
737
736
  function decodeTokenPayload(encoded) {
738
737
  const bytes = (0, _orkestrel_codec.decodeBase64URL)(encoded);
739
738
  if (bytes === void 0) return void 0;
740
- try {
741
- const payload = JSON.parse(new TextDecoder().decode(bytes));
742
- if (!(0, _orkestrel_contract.isRecord)(payload)) return void 0;
743
- if (typeof payload.value !== "string") return void 0;
744
- if (payload.exp !== void 0 && typeof payload.exp !== "number") return void 0;
745
- if (payload.exp !== void 0 && Date.now() >= payload.exp) return void 0;
746
- return payload.value;
747
- } catch {
748
- return;
749
- }
739
+ const payload = (0, _orkestrel_contract.parseJSON)(new TextDecoder().decode(bytes));
740
+ if (!(0, _orkestrel_contract.isRecord)(payload)) return void 0;
741
+ if (!(0, _orkestrel_contract.isString)(payload.value)) return void 0;
742
+ if (payload.exp !== void 0 && !(0, _orkestrel_contract.isNumber)(payload.exp)) return void 0;
743
+ if (payload.exp !== void 0 && Date.now() >= payload.exp) return void 0;
744
+ return payload.value;
750
745
  }
751
746
  /**
752
747
  * Normalizes a {@link TokenSecret} to a concrete list of usable secrets — the list
@@ -767,7 +762,7 @@ function decodeTokenPayload(encoded) {
767
762
  * ```
768
763
  */
769
764
  function normalizeSecret(secret) {
770
- return (typeof secret === "string" ? [secret] : [...secret]).filter((entry) => entry.trim().length > 0);
765
+ return ((0, _orkestrel_contract.isString)(secret) ? [secret] : [...secret]).filter((entry) => entry.trim().length > 0);
771
766
  }
772
767
  /**
773
768
  * Parses a weighted `Accept` / `Accept-Encoding` / `Accept-Language` header
@@ -805,7 +800,7 @@ function parseAcceptHeader(header) {
805
800
  const match = /;\s*q=(-?[0-9.]+)/i.exec(trimmed.slice(semicolon));
806
801
  if (match !== null) {
807
802
  const parsed = Number(match[1]);
808
- if (Number.isFinite(parsed)) q = Math.min(1, Math.max(0, parsed));
803
+ if ((0, _orkestrel_contract.isFiniteNumber)(parsed)) q = Math.min(1, Math.max(0, parsed));
809
804
  }
810
805
  }
811
806
  entries.push({
@@ -1133,7 +1128,7 @@ function parseRange(header, size) {
1133
1128
  const endText = spec.slice(dash + 1).trim();
1134
1129
  if (startText === "") {
1135
1130
  const suffix = Number(endText);
1136
- if (endText === "" || !Number.isInteger(suffix) || suffix <= 0) return void 0;
1131
+ if (endText === "" || !(0, _orkestrel_contract.isInteger)(suffix) || suffix <= 0) return void 0;
1137
1132
  if (size === 0) return { satisfiable: false };
1138
1133
  return {
1139
1134
  satisfiable: true,
@@ -1142,7 +1137,7 @@ function parseRange(header, size) {
1142
1137
  };
1143
1138
  }
1144
1139
  const start = Number(startText);
1145
- if (!Number.isInteger(start) || start < 0) return void 0;
1140
+ if (!(0, _orkestrel_contract.isInteger)(start) || start < 0) return void 0;
1146
1141
  if (start >= size) return { satisfiable: false };
1147
1142
  if (endText === "") return {
1148
1143
  satisfiable: true,
@@ -1150,7 +1145,7 @@ function parseRange(header, size) {
1150
1145
  end: size - 1
1151
1146
  };
1152
1147
  const end = Number(endText);
1153
- if (!Number.isInteger(end) || end < start) return void 0;
1148
+ if (!(0, _orkestrel_contract.isInteger)(end) || end < start) return void 0;
1154
1149
  return {
1155
1150
  satisfiable: true,
1156
1151
  start,
@@ -1524,7 +1519,7 @@ async function decompressRequestBody(bytes, encoding, cap) {
1524
1519
  size += value.byteLength;
1525
1520
  }
1526
1521
  } catch (error) {
1527
- if (error instanceof ContentTooLargeError) throw error;
1522
+ if ((0, _orkestrel_contract.isInstance)(error, ContentTooLargeError)) throw error;
1528
1523
  throw new HTTPError(400, "malformed compressed request body");
1529
1524
  }
1530
1525
  const merged = new Uint8Array(size);
@@ -1648,7 +1643,7 @@ async function discoverPort(preferred) {
1648
1643
  try {
1649
1644
  return await probePort(preferred);
1650
1645
  } catch (error) {
1651
- if (error instanceof Error && "code" in error && error.code === "EADDRINUSE") return probePort(0);
1646
+ if ((0, _orkestrel_contract.isError)(error) && "code" in error && error.code === "EADDRINUSE") return probePort(0);
1652
1647
  throw error;
1653
1648
  }
1654
1649
  }
@@ -1980,7 +1975,7 @@ var Server = class {
1980
1975
  }
1981
1976
  #boundary(error) {
1982
1977
  if (isHTTPError(error)) return new Response(error.message, { status: error.status });
1983
- const message = this.#expose && error instanceof Error ? error.message : "Internal Server Error";
1978
+ const message = this.#expose && (0, _orkestrel_contract.isError)(error) ? error.message : "Internal Server Error";
1984
1979
  return new Response(message, { status: 500 });
1985
1980
  }
1986
1981
  #onUpgrade(request, socket, head) {