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