@orkestrel/server 0.0.1 → 0.0.3

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.
@@ -1,6 +1,21 @@
1
+ import { isNumber, isRecord, isString, parseJSON } from "@orkestrel/contract";
1
2
  //#region src/core/constants.ts
2
3
  /** Default graceful-stop deadline (ms) the server gives in-flight requests on `stop()`. */
3
4
  var DEFAULT_DRAIN_MS = 1e4;
5
+ /**
6
+ * The `Symbol.for`-keyed brand `HTTPError` carries so `isHTTPError` recognizes
7
+ * an instance thrown by ANOTHER copy of this package (the dual-package
8
+ * hazard — a version-skewed or workspace-linked duplicate install), where
9
+ * `instanceof` alone fails because the two copies' `HTTPError` constructors
10
+ * are distinct objects.
11
+ *
12
+ * @remarks
13
+ * `Symbol.for` interns the symbol on the GLOBAL symbol registry, so every
14
+ * copy of this package that evaluates this key resolves the SAME symbol
15
+ * instance — unlike a locally-scoped `Symbol()`, which would mint a fresh,
16
+ * unequal symbol per copy and defeat the whole point of a cross-copy brand.
17
+ */
18
+ var HTTP_ERROR_BRAND = Symbol.for("@orkestrel/server.HTTPError");
4
19
  /** Default maximum request body size (bytes) `readBody` accepts before a 413. */
5
20
  var DEFAULT_BODY_LIMIT = 1048576;
6
21
  /**
@@ -114,6 +129,7 @@ var DEFAULT_ENCODINGS = Object.freeze(["gzip", "deflate"]);
114
129
  var HTTPError = class extends Error {
115
130
  status;
116
131
  context;
132
+ [HTTP_ERROR_BRAND] = true;
117
133
  constructor(status, message, context) {
118
134
  super(message);
119
135
  this.name = "HTTPError";
@@ -155,6 +171,16 @@ var ContentTooLargeError = class extends HTTPError {
155
171
  * @param value - The value to test (typically a `catch` binding)
156
172
  * @returns `true` when `value` is an {@link HTTPError}
157
173
  *
174
+ * @remarks
175
+ * Tries `instanceof` first, then falls back to a total structural check for
176
+ * an instance built by a DIFFERENT copy of this package (the dual-package
177
+ * hazard — version skew, a linked workspace duplicate) whose `HTTPError`
178
+ * constructor is a distinct object from this copy's: the value must carry
179
+ * the cross-copy brand (interned via `Symbol.for`, so every copy resolves the
180
+ * same key) AND expose the exact fields the server's error boundary reads off
181
+ * a recognized `HTTPError` — a numeric `status` and a string `message`. A
182
+ * plain object that merely carries a `status` WITHOUT the brand is rejected.
183
+ *
158
184
  * @example
159
185
  * ```ts
160
186
  * import { isHTTPError } from '@src/core'
@@ -167,124 +193,11 @@ var ContentTooLargeError = class extends HTTPError {
167
193
  * ```
168
194
  */
169
195
  function isHTTPError(value) {
170
- return value instanceof HTTPError;
171
- }
172
- Object.freeze([
173
- "null",
174
- "boolean",
175
- "object",
176
- "array",
177
- "number",
178
- "integer",
179
- "string"
180
- ]);
181
- /** Determine whether a value is a string. */
182
- function isString(value) {
183
- return typeof value === "string";
184
- }
185
- /**
186
- * Determine whether a value is a non-null object.
187
- *
188
- * @remarks
189
- * `true` for arrays, class instances, plain objects, `Map`, `Set`, etc. — use
190
- * {@link isRecord} when you need a plain-record check.
191
- */
192
- function isObject(value) {
193
- return typeof value === "object" && value !== null;
194
- }
195
- /**
196
- * Determine whether a value is a plain record (object literal or null-prototype),
197
- * not an array or class instance.
198
- *
199
- * @remarks
200
- * Use instead of {@link isObject} to distinguish a plain `{}` /
201
- * `Object.create(null)` from arrays, `Date`, `Map`, etc. The prototype-chain
202
- * test is realm-agnostic: rather than comparing against the current realm's
203
- * `Object.prototype` (which a plain object from another `vm.Context`, iframe,
204
- * or worker would fail), it accepts any value whose prototype is `null`, OR
205
- * whose prototype's own prototype is `null` — the shape every plain object
206
- * has in every realm, since `Object.prototype` itself always sits one step
207
- * above `null`. Arrays and class instances are still rejected: an array's
208
- * prototype chain runs through `Array.prototype` before `null`, and a class
209
- * instance's runs through the class's own prototype. The whole body runs
210
- * inside `attempt` (AGENTS §14) so a revoked `Proxy` or a hostile
211
- * `getPrototypeOf` trap cannot escape as a thrown error.
212
- */
213
- function isRecord(value) {
214
- const outcome = attempt(() => {
215
- if (!isObject(value) || isArray(value)) return false;
216
- const prototype = Object.getPrototypeOf(value);
217
- return prototype === null || Object.getPrototypeOf(prototype) === null;
218
- });
219
- return outcome.success && outcome.value;
220
- }
221
- /** Determine whether a value is an array. */
222
- function isArray(value) {
223
- return Array.isArray(value);
224
- }
225
- /**
226
- * Invoke a callback and capture its outcome as a {@link Result}, never letting
227
- * a throw escape.
228
- *
229
- * @remarks
230
- * The single sanctioned never-throw boundary for the guards (AGENTS §14). The
231
- * `whereOf`, `lazyOf`, and `transformOf` combinators invoke caller-supplied
232
- * callbacks *inside* a guard body, yet a guard must NEVER throw — it returns a
233
- * `boolean`. This converts a throwing callback into a `Failure` so the
234
- * surrounding guard can treat it as a non-match instead of propagating the
235
- * exception, written once and shared rather than copy-pasted as ad-hoc
236
- * `try`/`catch`.
237
- *
238
- * @param callback - The callback to invoke with no arguments
239
- * @returns A `Success` carrying the return value, or a `Failure` carrying the
240
- * thrown reason normalised to an `Error`
241
- *
242
- * @example
243
- * ```ts
244
- * const outcome = attempt(() => predicate(value))
245
- * return outcome.success && outcome.value
246
- * ```
247
- */
248
- function attempt(callback) {
249
- try {
250
- return {
251
- success: true,
252
- value: callback()
253
- };
254
- } catch (reason) {
255
- if (reason instanceof Error) return {
256
- success: false,
257
- error: reason
258
- };
259
- let message = "Unknown thrown value";
260
- try {
261
- message = String(reason);
262
- } catch {}
263
- return {
264
- success: false,
265
- error: new Error(message)
266
- };
267
- }
268
- }
269
- /**
270
- * Parse a JSON string, returning `undefined` instead of throwing.
271
- *
272
- * @remarks
273
- * The safe boundary for untrusted JSON text: a malformed string yields
274
- * `undefined`, never an exception. Returns `unknown` — a successful parse proves
275
- * nothing about shape, so narrow the result with a guard (or use
276
- * {@link parseJSONAs}). A large document is not walked here; parsing is shallow
277
- * and lazy validation is the caller's to compose.
278
- *
279
- * @param value - The JSON string to parse
280
- * @returns The parsed value, or `undefined` when `value` is not valid JSON
281
- */
282
- function parseJSON(value) {
283
- try {
284
- return JSON.parse(value);
285
- } catch {
286
- return;
287
- }
196
+ if (value instanceof HTTPError) return true;
197
+ if (typeof value !== "object" || value === null) return false;
198
+ if (!(HTTP_ERROR_BRAND in value) || value[HTTP_ERROR_BRAND] !== true) return false;
199
+ if (!("status" in value) || !("message" in value)) return false;
200
+ return isNumber(value.status) && isString(value.message);
288
201
  }
289
202
  //#endregion
290
203
  //#region src/core/helpers.ts
@@ -1723,6 +1636,6 @@ function createNegotiator() {
1723
1636
  return new Negotiator();
1724
1637
  }
1725
1638
  //#endregion
1726
- export { COMPRESSIBLE_TYPES, ContentTooLargeError, DEFAULT_BODY_LIMIT, DEFAULT_DECOMPRESSED_LIMIT, DEFAULT_DRAIN_MS, DEFAULT_ENCODINGS, HTTPError, Negotiator, REQUEST_ID_PATTERN, SSE_HEADERS, appendCookie, clearCookie, clientRateKey, codingQuality, collectRequestBody, compose, computeBodyETag, createNegotiator, decodeBase64Url, decodeCookieValue, decodeTokenPayload, decompressRequestBody, encodeBase64Url, ipv6Network, isCompressibleType, isCookieAttribute, isCookieName, isDangerousKey, isHTTPError, isValidRequestId, languageQuality, matchMediaType, matchesETag, mergeVary, negotiateEncoding, normalizeSecret, openStream, parseAcceptHeader, parseCookies, parseRange, readBody, readSignedCookie, requestEncoding, resolveOrigin, resolveSecure, resolveSecurityHeader, scrubPrototype, serializeCookie, serializeEvent, signToken, unwrapETag, verifyToken, writeSignedCookie };
1639
+ export { COMPRESSIBLE_TYPES, ContentTooLargeError, DEFAULT_BODY_LIMIT, DEFAULT_DECOMPRESSED_LIMIT, DEFAULT_DRAIN_MS, DEFAULT_ENCODINGS, HTTPError, HTTP_ERROR_BRAND, Negotiator, REQUEST_ID_PATTERN, SSE_HEADERS, appendCookie, clearCookie, clientRateKey, codingQuality, collectRequestBody, compose, computeBodyETag, createNegotiator, decodeBase64Url, decodeCookieValue, decodeTokenPayload, decompressRequestBody, encodeBase64Url, ipv6Network, isCompressibleType, isCookieAttribute, isCookieName, isDangerousKey, isHTTPError, isValidRequestId, languageQuality, matchMediaType, matchesETag, mergeVary, negotiateEncoding, normalizeSecret, openStream, parseAcceptHeader, parseCookies, parseRange, readBody, readSignedCookie, requestEncoding, resolveOrigin, resolveSecure, resolveSecurityHeader, scrubPrototype, serializeCookie, serializeEvent, signToken, unwrapETag, verifyToken, writeSignedCookie };
1727
1640
 
1728
1641
  //# sourceMappingURL=index.js.map