@wooksjs/event-http 0.6.5 → 0.7.0

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.
package/dist/index.mjs CHANGED
@@ -1,31 +1,21 @@
1
- import { attachHook, createAsyncEventContext, useAsyncEventContext, useEventId, useEventLogger, useEventLogger as useEventLogger$1, useRouteParams } from "@wooksjs/event-core";
1
+ import { EventContext, cached, cachedBy, current, defineEventKind, defineWook, routeParamsKey, run, slot, useEventId, useLogger, useRouteParams } from "@wooksjs/event-core";
2
2
  import { Buffer as Buffer$1 } from "buffer";
3
3
  import { Readable, pipeline } from "node:stream";
4
4
  import { promisify } from "node:util";
5
5
  import { createBrotliCompress, createBrotliDecompress, createDeflate, createGunzip, createGzip, createInflate } from "node:zlib";
6
6
  import { URLSearchParams } from "url";
7
- import http from "http";
8
- import { WooksAdapterBase } from "wooks";
9
7
  import { Readable as Readable$1 } from "stream";
8
+ import http, { IncomingMessage, ServerResponse } from "http";
9
+ import { WooksAdapterBase } from "wooks";
10
+ import { Socket } from "net";
10
11
 
11
- //#region packages/event-http/src/event-http.ts
12
- /** Creates an async event context for an incoming HTTP request/response pair. */
13
- function createHttpContext(data, options) {
14
- return createAsyncEventContext({
15
- event: {
16
- ...data,
17
- type: "HTTP"
18
- },
19
- options
20
- });
21
- }
22
- /**
23
- * Wrapper on useEventContext with HTTP event types
24
- * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
25
- */
26
- function useHttpContext() {
27
- return useAsyncEventContext("HTTP");
28
- }
12
+ //#region packages/event-http/src/http-kind.ts
13
+ /** Event kind definition for HTTP requests. Provides typed context slots for `req`, `response`, and `requestLimits`. */
14
+ const httpKind = defineEventKind("http", {
15
+ req: slot(),
16
+ response: slot(),
17
+ requestLimits: slot()
18
+ });
29
19
 
30
20
  //#endregion
31
21
  //#region packages/event-http/src/utils/helpers.ts
@@ -45,53 +35,114 @@ function safeDecodeURIComponent(uri) {
45
35
  }
46
36
 
47
37
  //#endregion
48
- //#region packages/event-http/src/utils/time.ts
49
- function convertTime(time, unit = "ms") {
50
- if (typeof time === "number") return time / units[unit];
51
- const rg = /(\d+)(\w+)/gu;
52
- let t = 0;
53
- let r;
54
- while (r = rg.exec(time)) t += Number(r[1]) * (units[r[2]] || 0);
55
- return t / units[unit];
38
+ //#region packages/event-http/src/composables/cookies.ts
39
+ const cookieRegExpCache = /* @__PURE__ */ new Map();
40
+ function getCookieRegExp(name) {
41
+ let re = cookieRegExpCache.get(name);
42
+ if (!re) {
43
+ re = new RegExp(`(?:^|; )${escapeRegex(name)}=(.*?)(?:;?$|; )`, "i");
44
+ cookieRegExpCache.set(name, re);
45
+ }
46
+ return re;
56
47
  }
57
- const units = {
58
- ms: 1,
59
- s: 1e3,
60
- m: 1e3 * 60,
61
- h: 1e3 * 60 * 60,
62
- d: 1e3 * 60 * 60 * 24,
63
- w: 1e3 * 60 * 60 * 24 * 7,
64
- M: 1e3 * 60 * 60 * 24 * 30,
65
- Y: 1e3 * 60 * 60 * 24 * 365
48
+ const parseCookieValue = cachedBy((name, ctx) => {
49
+ const cookie = ctx.get(httpKind.keys.req).headers.cookie;
50
+ if (cookie) {
51
+ const result = getCookieRegExp(name).exec(cookie);
52
+ return result?.[1] ? safeDecodeURIComponent(result[1]) : null;
53
+ }
54
+ return null;
55
+ });
56
+ /**
57
+ * Provides access to parsed request cookies.
58
+ * @example
59
+ * ```ts
60
+ * const { getCookie, rawCookies } = useCookies()
61
+ * const sessionId = getCookie('session_id')
62
+ * ```
63
+ */
64
+ const useCookies = defineWook((ctx) => ({
65
+ rawCookies: ctx.get(httpKind.keys.req).headers.cookie,
66
+ getCookie: (name) => parseCookieValue(name, ctx)
67
+ }));
68
+
69
+ //#endregion
70
+ //#region packages/event-http/src/composables/header-accept.ts
71
+ const ACCEPT_TYPE_MAP = {
72
+ json: "application/json",
73
+ html: "text/html",
74
+ xml: "application/xml",
75
+ text: "text/plain"
66
76
  };
77
+ const acceptsMime = cachedBy((type, ctx) => {
78
+ const accept = ctx.get(httpKind.keys.req).headers.accept;
79
+ const mime = ACCEPT_TYPE_MAP[type] || type;
80
+ return !!(accept && (accept === "*/*" || accept.includes(mime)));
81
+ });
82
+ /** Provides helpers to check the request's Accept header for supported MIME types. */
83
+ const useAccept = defineWook((ctx) => {
84
+ const accept = ctx.get(httpKind.keys.req).headers.accept;
85
+ return {
86
+ accept,
87
+ accepts: (type) => acceptsMime(type, ctx)
88
+ };
89
+ });
67
90
 
68
91
  //#endregion
69
- //#region packages/event-http/src/utils/set-cookie.ts
70
- const COOKIE_NAME_RE = /^[\w!#$%&'*+\-.^`|~]+$/;
71
- function sanitizeCookieAttrValue(v) {
72
- return v.replace(/[;\r\n]/g, "");
73
- }
74
- function renderCookie(key, data) {
75
- if (!COOKIE_NAME_RE.test(key)) throw new TypeError(`Invalid cookie name "${key}"`);
76
- let attrs = "";
77
- for (const [a, v] of Object.entries(data.attrs)) {
78
- const func = cookieAttrFunc[a];
79
- if (typeof func === "function") {
80
- const val = func(v);
81
- attrs += val ? `; ${val}` : "";
82
- } else throw new TypeError(`Unknown Set-Cookie attribute ${a}`);
92
+ //#region packages/event-http/src/composables/header-authorization.ts
93
+ const authTypeSlot = cached((ctx) => {
94
+ const authorization = ctx.get(httpKind.keys.req).headers.authorization;
95
+ if (authorization) {
96
+ const space = authorization.indexOf(" ");
97
+ return authorization.slice(0, space);
83
98
  }
84
- return `${key}=${encodeURIComponent(data.value)}${attrs}`;
85
- }
86
- const cookieAttrFunc = {
87
- expires: (v) => `Expires=${typeof v === "string" || typeof v === "number" ? new Date(v).toUTCString() : v.toUTCString()}`,
88
- maxAge: (v) => `Max-Age=${convertTime(v, "s").toString()}`,
89
- domain: (v) => `Domain=${sanitizeCookieAttrValue(String(v))}`,
90
- path: (v) => `Path=${sanitizeCookieAttrValue(String(v))}`,
91
- secure: (v) => v ? "Secure" : "",
92
- httpOnly: (v) => v ? "HttpOnly" : "",
93
- sameSite: (v) => v ? `SameSite=${typeof v === "string" ? v : "Strict"}` : ""
94
- };
99
+ return null;
100
+ });
101
+ const authCredentialsSlot = cached((ctx) => {
102
+ const authorization = ctx.get(httpKind.keys.req).headers.authorization;
103
+ if (authorization) {
104
+ const space = authorization.indexOf(" ");
105
+ return authorization.slice(space + 1);
106
+ }
107
+ return null;
108
+ });
109
+ const basicCredentialsSlot = cached((ctx) => {
110
+ const authorization = ctx.get(httpKind.keys.req).headers.authorization;
111
+ if (authorization) {
112
+ const type = ctx.get(authTypeSlot);
113
+ if (type?.toLocaleLowerCase() === "basic") {
114
+ const creds = Buffer$1.from(ctx.get(authCredentialsSlot) || "", "base64").toString("ascii");
115
+ const [username, password] = creds.split(":");
116
+ return {
117
+ username,
118
+ password
119
+ };
120
+ }
121
+ }
122
+ return null;
123
+ });
124
+ const authIsSlot = cachedBy((type, ctx) => {
125
+ const authType = ctx.get(authTypeSlot);
126
+ return authType?.toLowerCase() === type.toLowerCase();
127
+ });
128
+ /**
129
+ * Provides parsed access to the Authorization header (type, credentials, Basic decoding).
130
+ * @example
131
+ * ```ts
132
+ * const { authIs, authRawCredentials, basicCredentials } = useAuthorization()
133
+ * if (authIs('bearer')) { const token = authRawCredentials() }
134
+ * ```
135
+ */
136
+ const useAuthorization = defineWook((ctx) => {
137
+ const authorization = ctx.get(httpKind.keys.req).headers.authorization;
138
+ return {
139
+ authorization,
140
+ authType: () => ctx.get(authTypeSlot),
141
+ authRawCredentials: () => ctx.get(authCredentialsSlot),
142
+ authIs: (type) => authIsSlot(type, ctx),
143
+ basicCredentials: () => ctx.get(basicCredentialsSlot)
144
+ };
145
+ });
95
146
 
96
147
  //#endregion
97
148
  //#region packages/event-http/src/compressor/body-compressor.ts
@@ -169,26 +220,9 @@ compressors.deflate.uncompress = async (b) => (await zlib()).inflate(b);
169
220
  compressors.br.compress = async (b) => (await zlib()).brotliCompress(b);
170
221
  compressors.br.uncompress = async (b) => (await zlib()).brotliDecompress(b);
171
222
 
172
- //#endregion
173
- //#region packages/event-http/src/response/renderer.ts
174
- var BaseHttpResponseRenderer = class {
175
- render(response) {
176
- if (typeof response.body === "string" || typeof response.body === "boolean" || typeof response.body === "number") {
177
- if (!response.getContentType()) response.setContentType("text/plain");
178
- return response.body.toString();
179
- }
180
- if (response.body === void 0) return "";
181
- if (response.body instanceof Uint8Array) return response.body;
182
- if (typeof response.body === "object") {
183
- if (!response.getContentType()) response.setContentType("application/json");
184
- return JSON.stringify(response.body);
185
- }
186
- throw new Error(`Unsupported body format "${typeof response.body}"`);
187
- }
188
- };
189
-
190
223
  //#endregion
191
224
  //#region packages/event-http/src/utils/status-codes.ts
225
+ /** Maps numeric HTTP status codes to their human-readable descriptions. */
192
226
  const httpStatusCodes = {
193
227
  100: "Continue",
194
228
  101: "Switching protocols",
@@ -254,6 +288,7 @@ const httpStatusCodes = {
254
288
  510: "Not Extended",
255
289
  511: "Network Authentication Required"
256
290
  };
291
+ /** Enum of all standard HTTP status codes (100–511). */
257
292
  let EHttpStatusCode = /* @__PURE__ */ function(EHttpStatusCode$1) {
258
293
  EHttpStatusCode$1[EHttpStatusCode$1["Continue"] = 100] = "Continue";
259
294
  EHttpStatusCode$1[EHttpStatusCode$1["SwitchingProtocols"] = 101] = "SwitchingProtocols";
@@ -322,45 +357,707 @@ let EHttpStatusCode = /* @__PURE__ */ function(EHttpStatusCode$1) {
322
357
  }({});
323
358
 
324
359
  //#endregion
325
- //#region packages/event-http/src/errors/403.tl.svg
326
- function _403_tl_default(ctx) {
327
- return `<svg height="64" viewBox="0 4 100 96" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="#888888" stroke-width="2">
328
- <path d="M50 90.625C64.4042 87.1875 83.5751 69.8667 83.5751 48.6937V24.0854L50 9.375L16.425 24.0833V48.6937C16.425 69.8667 35.5959 87.1875 50 90.625Z" fill="#ff000050">
329
- <animate attributeName="fill" dur="2s" repeatCount="indefinite"
330
- values="#ff000000;#ff000050;#ff000000" />
331
- </path>
360
+ //#region packages/event-http/src/errors/http-error.ts
361
+ /** Represents an HTTP error with a status code and optional structured body. */
362
+ var HttpError = class extends Error {
363
+ name = "HttpError";
364
+ constructor(code = 500, _body = "") {
365
+ const prev = Error.stackTraceLimit;
366
+ Error.stackTraceLimit = 0;
367
+ super(typeof _body === "string" ? _body : _body.message);
368
+ this.code = code;
369
+ this._body = _body;
370
+ Error.stackTraceLimit = prev;
371
+ }
372
+ get body() {
373
+ return typeof this._body === "string" ? {
374
+ statusCode: this.code,
375
+ message: this.message,
376
+ error: httpStatusCodes[this.code]
377
+ } : {
378
+ ...this._body,
379
+ statusCode: this.code,
380
+ message: this.message,
381
+ error: httpStatusCodes[this.code]
382
+ };
383
+ }
384
+ };
332
385
 
333
- <path d="M61.5395 46.0812H38.4604C37.1061 46.0812 36.0083 47.1791 36.0083 48.5333V65.075C36.0083 66.4292 37.1061 67.5271 38.4604 67.5271H61.5395C62.8938 67.5271 63.9916 66.4292 63.9916 65.075V48.5333C63.9916 47.1791 62.8938 46.0812 61.5395 46.0812Z" />
386
+ //#endregion
387
+ //#region packages/event-http/src/composables/request.ts
388
+ const xForwardedFor = "x-forwarded-for";
389
+ /** Default safety limits for request body reading (size, ratio, timeout). */
390
+ const DEFAULT_LIMITS = {
391
+ maxCompressed: 1 * 1024 * 1024,
392
+ maxInflated: 10 * 1024 * 1024,
393
+ maxRatio: 100,
394
+ readTimeoutMs: 1e4
395
+ };
396
+ const contentEncodingsSlot = cached((ctx) => {
397
+ const req = ctx.get(httpKind.keys.req);
398
+ const contentEncoding = req.headers["content-encoding"];
399
+ return (contentEncoding || "").split(",").map((p) => p.trim()).filter((p) => !!p);
400
+ });
401
+ const isCompressedSlot = cached((ctx) => {
402
+ const parts = ctx.get(contentEncodingsSlot);
403
+ for (const p of parts) if ([
404
+ "deflate",
405
+ "gzip",
406
+ "br"
407
+ ].includes(p)) return true;
408
+ return false;
409
+ });
410
+ /** @internal Exported for test pre-seeding via `ctx.set(rawBodySlot, ...)`. */
411
+ const rawBodySlot = cached(async (ctx) => {
412
+ const req = ctx.get(httpKind.keys.req);
413
+ const encs = ctx.get(contentEncodingsSlot);
414
+ const isZip = ctx.get(isCompressedSlot);
415
+ const streamable = isZip && encodingSupportsStream(encs);
416
+ const limits = ctx.get(httpKind.keys.requestLimits);
417
+ const maxCompressed = limits?.maxCompressed ?? DEFAULT_LIMITS.maxCompressed;
418
+ const maxInflated = limits?.maxInflated ?? DEFAULT_LIMITS.maxInflated;
419
+ const maxRatio = limits?.maxRatio ?? DEFAULT_LIMITS.maxRatio;
420
+ const timeoutMs = limits?.readTimeoutMs ?? DEFAULT_LIMITS.readTimeoutMs;
421
+ const cl = Number(req.headers["content-length"] ?? 0);
422
+ const upfrontLimit = isZip ? maxCompressed : maxInflated;
423
+ if (cl && cl > upfrontLimit) throw new HttpError(413, "Payload Too Large");
424
+ for (const enc of encs) if (!compressors[enc]) throw new HttpError(415, `Unsupported Content-Encoding "${enc}"`);
425
+ let timer = null;
426
+ function resetTimer() {
427
+ if (timeoutMs === 0) return;
428
+ clearTimer();
429
+ timer = setTimeout(() => {
430
+ clearTimer();
431
+ req.destroy();
432
+ }, timeoutMs);
433
+ }
434
+ function clearTimer() {
435
+ if (timer) {
436
+ clearTimeout(timer);
437
+ timer = null;
438
+ }
439
+ }
440
+ let rawBytes = 0;
441
+ async function* limitedCompressed() {
442
+ resetTimer();
443
+ try {
444
+ for await (const chunk of req) {
445
+ rawBytes += chunk.length;
446
+ if (rawBytes > upfrontLimit) {
447
+ req.destroy();
448
+ throw new HttpError(413, "Payload Too Large");
449
+ }
450
+ resetTimer();
451
+ yield chunk;
452
+ }
453
+ } finally {
454
+ clearTimer();
455
+ }
456
+ }
457
+ let stream = limitedCompressed();
458
+ if (streamable) stream = await uncompressBodyStream(encs, stream);
459
+ const chunks = [];
460
+ let inflatedBytes = 0;
461
+ try {
462
+ for await (const chunk of stream) {
463
+ inflatedBytes += chunk.length;
464
+ if (inflatedBytes > maxInflated) throw new HttpError(413, "Inflated body too large");
465
+ chunks.push(chunk);
466
+ }
467
+ } catch (error) {
468
+ if (error instanceof HttpError) throw error;
469
+ throw new HttpError(408, "Request body timeout");
470
+ }
471
+ let body = Buffer$1.concat(chunks);
472
+ if (!streamable && isZip) {
473
+ body = await uncompressBody(encs, body);
474
+ inflatedBytes = body.byteLength;
475
+ if (inflatedBytes > maxInflated) throw new HttpError(413, "Inflated body too large");
476
+ }
477
+ if (isZip && rawBytes > 0 && inflatedBytes / rawBytes > maxRatio) throw new HttpError(413, "Compression ratio too high");
478
+ return body;
479
+ });
480
+ const forwardedIpSlot = cached((ctx) => {
481
+ const req = ctx.get(httpKind.keys.req);
482
+ if (typeof req.headers[xForwardedFor] === "string" && req.headers[xForwardedFor]) return req.headers[xForwardedFor].split(",").shift()?.trim();
483
+ return "";
484
+ });
485
+ const remoteIpSlot = cached((ctx) => {
486
+ const req = ctx.get(httpKind.keys.req);
487
+ return req.socket.remoteAddress || req.connection.remoteAddress || "";
488
+ });
489
+ const ipListSlot = cached((ctx) => {
490
+ const req = ctx.get(httpKind.keys.req);
491
+ return {
492
+ remoteIp: req.socket.remoteAddress || req.connection.remoteAddress || "",
493
+ forwarded: (req.headers[xForwardedFor] || "").split(",").map((s) => s.trim())
494
+ };
495
+ });
496
+ /**
497
+ * Provides access to the incoming HTTP request (method, url, headers, body, IP).
498
+ * @example
499
+ * ```ts
500
+ * const { method, url, rawBody, getIp } = useRequest()
501
+ * const body = await rawBody()
502
+ * ```
503
+ */
504
+ const useRequest = defineWook((ctx) => {
505
+ const req = ctx.get(httpKind.keys.req);
506
+ const limits = () => ctx.get(httpKind.keys.requestLimits);
507
+ const setLimit = (limitKey, value) => {
508
+ let obj = limits();
509
+ if (!obj?.perRequest) {
510
+ obj = {
511
+ ...obj,
512
+ perRequest: true
513
+ };
514
+ ctx.set(httpKind.keys.requestLimits, obj);
515
+ }
516
+ obj[limitKey] = value;
517
+ };
518
+ const getMaxCompressed = () => limits()?.maxCompressed ?? DEFAULT_LIMITS.maxCompressed;
519
+ const setMaxCompressed = (limit) => setLimit("maxCompressed", limit);
520
+ const getMaxInflated = () => limits()?.maxInflated ?? DEFAULT_LIMITS.maxInflated;
521
+ const setMaxInflated = (limit) => setLimit("maxInflated", limit);
522
+ const getMaxRatio = () => limits()?.maxRatio ?? DEFAULT_LIMITS.maxRatio;
523
+ const setMaxRatio = (limit) => setLimit("maxRatio", limit);
524
+ const getReadTimeoutMs = () => limits()?.readTimeoutMs ?? DEFAULT_LIMITS.readTimeoutMs;
525
+ const setReadTimeoutMs = (limit) => setLimit("readTimeoutMs", limit);
526
+ function getIp(options) {
527
+ if (options?.trustProxy) return ctx.get(forwardedIpSlot) || ctx.get(remoteIpSlot);
528
+ return ctx.get(remoteIpSlot);
529
+ }
530
+ return {
531
+ rawRequest: req,
532
+ url: req.url,
533
+ method: req.method,
534
+ headers: req.headers,
535
+ rawBody: () => ctx.get(rawBodySlot),
536
+ reqId: useEventId(ctx).getId,
537
+ getIp,
538
+ getIpList: () => ctx.get(ipListSlot),
539
+ isCompressed: () => ctx.get(isCompressedSlot),
540
+ getMaxCompressed,
541
+ setMaxCompressed,
542
+ getReadTimeoutMs,
543
+ setReadTimeoutMs,
544
+ getMaxInflated,
545
+ setMaxInflated,
546
+ getMaxRatio,
547
+ setMaxRatio
548
+ };
549
+ });
334
550
 
335
- <path d="M41.7834 46.0834V39.6813C41.7834 37.5021 42.6491 35.4121 44.1901 33.8712C45.731 32.3303 47.8209 31.4646 50.0001 31.4646C52.1793 31.4646 54.2693 32.3303 55.8102 33.8712C57.3511 35.4121 58.2168 37.5021 58.2168 39.6813V46.0813" />
336
- </svg>
337
- `;
551
+ //#endregion
552
+ //#region packages/event-http/src/composables/headers.ts
553
+ /**
554
+ * Returns the incoming request headers.
555
+ * @example
556
+ * ```ts
557
+ * const { host, authorization } = useHeaders()
558
+ * ```
559
+ */
560
+ function useHeaders(ctx) {
561
+ return useRequest(ctx).headers;
338
562
  }
339
563
 
340
564
  //#endregion
341
- //#region packages/event-http/src/errors/404.tl.svg
342
- function _404_tl_default(ctx) {
343
- return `<svg height="64" viewBox="0 20 100 64" fill="none" xmlns="http://www.w3.org/2000/svg">
344
- <defs>
345
- <path id="sheet" d="M86.5 36.5V47.5H97.5V92.5H52.5V36.5H86.5ZM63 68.5H87V67.5H63V68.5ZM63 63.5H87V62.5H63V63.5ZM63 58.5H87V57.5H63V58.5ZM96.793 46.5H87.5V37.207L96.793 46.5Z" fill="#88888833" stroke="#888888aa"/>
346
- </defs>
565
+ //#region packages/event-http/src/composables/response.ts
566
+ /**
567
+ * Returns the HttpResponse instance for the current request.
568
+ * All response operations (status, headers, cookies, cache control, sending)
569
+ * are methods on the returned object.
570
+ *
571
+ * @example
572
+ * ```ts
573
+ * const response = useResponse()
574
+ * response.status = 200
575
+ * response.setHeader('x-custom', 'value')
576
+ * response.setCookie('session', 'abc', { httpOnly: true })
577
+ * ```
578
+ */
579
+ function useResponse(ctx) {
580
+ return (ctx ?? current()).get(httpKind.keys.response);
581
+ }
347
582
 
348
- <g id="queue" transform="translate(-5 -10)">
349
- <use href="#sheet" opacity="0">
350
- <animateTransform attributeName="transform" type="translate"
351
- dur="3s" repeatCount="indefinite"
352
- keyTimes="0;0.1;0.32;0.42;1"
353
- values="30 0; -20 0; -20 0; -70 0; -70 0" />
354
- <animate attributeName="opacity"
355
- dur="3s" repeatCount="indefinite"
356
- keyTimes="0;0.1;0.32;0.42;1"
357
- values="0;1;1;0;0" />
358
- </use>
583
+ //#endregion
584
+ //#region packages/event-http/src/utils/url-search-params.ts
585
+ const ILLEGAL_KEYS = new Set([
586
+ "__proto__",
587
+ "constructor",
588
+ "prototype"
589
+ ]);
590
+ /**
591
+ * Extended `URLSearchParams` with safe JSON conversion.
592
+ *
593
+ * Rejects prototype-pollution keys (`__proto__`, `constructor`, `prototype`) and duplicate non-array keys.
594
+ * Array parameters are detected by a trailing `[]` in the key name (e.g. `tags[]=a&tags[]=b`).
595
+ */
596
+ var WooksURLSearchParams = class extends URLSearchParams {
597
+ /** Converts query parameters to a plain object. Array params (keys ending with `[]`) become `string[]`. */
598
+ toJson() {
599
+ const json = Object.create(null);
600
+ for (const [key, value] of this.entries()) if (isArrayParam(key)) {
601
+ const a = json[key] = json[key] || [];
602
+ a.push(value);
603
+ } else {
604
+ if (ILLEGAL_KEYS.has(key)) throw new HttpError(400, `Illegal key name "${key}"`);
605
+ if (key in json) throw new HttpError(400, `Duplicate key "${key}"`);
606
+ json[key] = value;
607
+ }
608
+ return json;
609
+ }
610
+ };
611
+ function isArrayParam(name) {
612
+ return name.endsWith("[]");
613
+ }
359
614
 
360
- <use href="#sheet" opacity="0">
361
- <animateTransform attributeName="transform" type="translate"
362
- dur="3s" begin="1s" repeatCount="indefinite"
363
- keyTimes="0;0.1;0.32;0.42;1"
615
+ //#endregion
616
+ //#region packages/event-http/src/composables/search-params.ts
617
+ const rawSearchParamsSlot = cached((ctx) => {
618
+ const url = ctx.get(httpKind.keys.req).url || "";
619
+ const i = url.indexOf("?");
620
+ return i >= 0 ? url.slice(i) : "";
621
+ });
622
+ const urlSearchParamsSlot = cached((ctx) => new WooksURLSearchParams(ctx.get(rawSearchParamsSlot)));
623
+ /**
624
+ * Provides access to URL search (query) parameters from the request.
625
+ * @example
626
+ * ```ts
627
+ * const { urlSearchParams, jsonSearchParams } = useSearchParams()
628
+ * const page = urlSearchParams().get('page')
629
+ * ```
630
+ */
631
+ const useSearchParams = defineWook((ctx) => ({
632
+ rawSearchParams: () => ctx.get(rawSearchParamsSlot),
633
+ urlSearchParams: () => ctx.get(urlSearchParamsSlot),
634
+ jsonSearchParams: () => ctx.get(urlSearchParamsSlot).toJson()
635
+ }));
636
+
637
+ //#endregion
638
+ //#region packages/event-http/src/utils/time.ts
639
+ function convertTime(time, unit = "ms") {
640
+ if (typeof time === "number") return time / units[unit];
641
+ const rg = /(\d+)(\w+)/gu;
642
+ let t = 0;
643
+ let r;
644
+ while (r = rg.exec(time)) t += Number(r[1]) * (units[r[2]] || 0);
645
+ return t / units[unit];
646
+ }
647
+ const units = {
648
+ ms: 1,
649
+ s: 1e3,
650
+ m: 1e3 * 60,
651
+ h: 1e3 * 60 * 60,
652
+ d: 1e3 * 60 * 60 * 24,
653
+ w: 1e3 * 60 * 60 * 24 * 7,
654
+ M: 1e3 * 60 * 60 * 24 * 30,
655
+ Y: 1e3 * 60 * 60 * 24 * 365
656
+ };
657
+
658
+ //#endregion
659
+ //#region packages/event-http/src/utils/cache-control.ts
660
+ /** Renders a `TCacheControl` object into a `Cache-Control` header string. */
661
+ function renderCacheControl(data) {
662
+ let attrs = "";
663
+ for (const [a, v] of Object.entries(data)) {
664
+ if (v === void 0) continue;
665
+ const func = cacheControlFunc[a];
666
+ if (typeof func === "function") {
667
+ const val = func(v);
668
+ if (val) attrs += attrs ? `, ${val}` : val;
669
+ } else throw new TypeError(`Unknown Cache-Control attribute ${a}`);
670
+ }
671
+ return attrs;
672
+ }
673
+ const cacheControlFunc = {
674
+ mustRevalidate: (v) => v ? "must-revalidate" : "",
675
+ noCache: (v) => v ? typeof v === "string" ? `no-cache="${v}"` : "no-cache" : "",
676
+ noStore: (v) => v ? "no-store" : "",
677
+ noTransform: (v) => v ? "no-transform" : "",
678
+ public: (v) => v ? "public" : "",
679
+ private: (v) => v ? typeof v === "string" ? `private="${v}"` : "private" : "",
680
+ proxyRevalidate: (v) => v ? "proxy-revalidate" : "",
681
+ maxAge: (v) => `max-age=${convertTime(v, "s").toString()}`,
682
+ sMaxage: (v) => `s-maxage=${convertTime(v, "s").toString()}`
683
+ };
684
+
685
+ //#endregion
686
+ //#region packages/event-http/src/utils/set-cookie.ts
687
+ const COOKIE_NAME_RE = /^[\w!#$%&'*+\-.^`|~]+$/;
688
+ function sanitizeCookieAttrValue(v) {
689
+ return v.replace(/[;\r\n]/g, "");
690
+ }
691
+ function renderCookie(key, data) {
692
+ if (!COOKIE_NAME_RE.test(key)) throw new TypeError(`Invalid cookie name "${key}"`);
693
+ let attrs = "";
694
+ for (const [a, v] of Object.entries(data.attrs)) {
695
+ const func = cookieAttrFunc[a];
696
+ if (typeof func === "function") {
697
+ const val = func(v);
698
+ attrs += val ? `; ${val}` : "";
699
+ } else throw new TypeError(`Unknown Set-Cookie attribute ${a}`);
700
+ }
701
+ return `${key}=${encodeURIComponent(data.value)}${attrs}`;
702
+ }
703
+ const cookieAttrFunc = {
704
+ expires: (v) => `Expires=${typeof v === "string" || typeof v === "number" ? new Date(v).toUTCString() : v.toUTCString()}`,
705
+ maxAge: (v) => `Max-Age=${convertTime(v, "s").toString()}`,
706
+ domain: (v) => `Domain=${sanitizeCookieAttrValue(String(v))}`,
707
+ path: (v) => `Path=${sanitizeCookieAttrValue(String(v))}`,
708
+ secure: (v) => v ? "Secure" : "",
709
+ httpOnly: (v) => v ? "HttpOnly" : "",
710
+ sameSite: (v) => v ? `SameSite=${typeof v === "string" ? v : "Strict"}` : ""
711
+ };
712
+
713
+ //#endregion
714
+ //#region packages/event-http/src/response/http-response.ts
715
+ const hasFetchResponse = typeof globalThis.Response === "function";
716
+ const defaultStatus = {
717
+ GET: EHttpStatusCode.OK,
718
+ POST: EHttpStatusCode.Created,
719
+ PUT: EHttpStatusCode.Created,
720
+ PATCH: EHttpStatusCode.Accepted,
721
+ DELETE: EHttpStatusCode.Accepted
722
+ };
723
+ /**
724
+ * Manages response status, headers, cookies, cache control, and body for an HTTP request.
725
+ *
726
+ * All header mutations are accumulated in memory and flushed in a single `writeHead()` call
727
+ * when `send()` is invoked. Setter methods are chainable.
728
+ *
729
+ * @example
730
+ * ```ts
731
+ * const response = useResponse()
732
+ * response.setStatus(200).setHeader('x-custom', 'value')
733
+ * response.setCookie('session', 'abc', { httpOnly: true })
734
+ * ```
735
+ */
736
+ var HttpResponse = class {
737
+ /**
738
+ * @param _res - The underlying Node.js `ServerResponse`.
739
+ * @param _req - The underlying Node.js `IncomingMessage`.
740
+ * @param _logger - Logger instance for error reporting.
741
+ * @param defaultHeaders - Optional headers to pre-populate on this response (e.g. from `securityHeaders()`).
742
+ */
743
+ constructor(_res, _req, _logger, defaultHeaders) {
744
+ this._res = _res;
745
+ this._req = _req;
746
+ this._logger = _logger;
747
+ if (defaultHeaders) for (const key in defaultHeaders) this._headers[key] = defaultHeaders[key];
748
+ }
749
+ _status = 0;
750
+ _body = void 0;
751
+ _headers = {};
752
+ _cookies = {};
753
+ _rawCookies = [];
754
+ _hasCookies = false;
755
+ _responded = false;
756
+ /** The HTTP status code. If not set, it is inferred automatically when `send()` is called. */
757
+ get status() {
758
+ return this._status;
759
+ }
760
+ set status(value) {
761
+ this._status = value;
762
+ }
763
+ /** Sets the HTTP status code (chainable). */
764
+ setStatus(value) {
765
+ this._status = value;
766
+ return this;
767
+ }
768
+ /** The response body. Automatically serialized by `send()` (objects → JSON, strings → text). */
769
+ get body() {
770
+ return this._body;
771
+ }
772
+ set body(value) {
773
+ this._body = value;
774
+ }
775
+ /** Sets the response body (chainable). */
776
+ setBody(value) {
777
+ this._body = value;
778
+ return this;
779
+ }
780
+ /** Sets a single response header (chainable). Arrays produce multi-value headers. */
781
+ setHeader(name, value) {
782
+ this._headers[name] = Array.isArray(value) ? value : value.toString();
783
+ return this;
784
+ }
785
+ /** Batch-sets multiple response headers from a record (chainable). Existing keys are overwritten. */
786
+ setHeaders(headers) {
787
+ for (const key in headers) this._headers[key] = headers[key];
788
+ return this;
789
+ }
790
+ /** Returns the value of a response header, or `undefined` if not set. */
791
+ getHeader(name) {
792
+ return this._headers[name];
793
+ }
794
+ /** Removes a response header (chainable). */
795
+ removeHeader(name) {
796
+ delete this._headers[name];
797
+ return this;
798
+ }
799
+ /** Returns a read-only snapshot of all response headers. */
800
+ headers() {
801
+ return this._headers;
802
+ }
803
+ /** Sets the `Content-Type` response header (chainable). */
804
+ setContentType(value) {
805
+ this._headers["content-type"] = value;
806
+ return this;
807
+ }
808
+ /** Returns the current `Content-Type` header value. */
809
+ getContentType() {
810
+ return this._headers["content-type"];
811
+ }
812
+ /** Sets the `Access-Control-Allow-Origin` header (chainable). Defaults to `'*'`. */
813
+ enableCors(origin = "*") {
814
+ this._headers["access-control-allow-origin"] = origin;
815
+ return this;
816
+ }
817
+ /** Sets an outgoing `Set-Cookie` header with optional attributes (chainable). */
818
+ setCookie(name, value, attrs) {
819
+ this._cookies[name] = {
820
+ value,
821
+ attrs: attrs || {}
822
+ };
823
+ this._hasCookies = true;
824
+ return this;
825
+ }
826
+ /** Returns a previously set cookie's data, or `undefined` if not set. */
827
+ getCookie(name) {
828
+ return this._cookies[name];
829
+ }
830
+ /** Removes a cookie from the outgoing set list (chainable). */
831
+ removeCookie(name) {
832
+ delete this._cookies[name];
833
+ return this;
834
+ }
835
+ /** Removes all outgoing cookies (chainable). */
836
+ clearCookies() {
837
+ this._cookies = {};
838
+ this._rawCookies = [];
839
+ this._hasCookies = false;
840
+ return this;
841
+ }
842
+ /** Appends a raw `Set-Cookie` header string (chainable). Use when you need full control over the cookie format. */
843
+ setCookieRaw(rawValue) {
844
+ this._rawCookies.push(rawValue);
845
+ this._hasCookies = true;
846
+ return this;
847
+ }
848
+ /** Sets the `Cache-Control` header from a directive object (chainable). */
849
+ setCacheControl(data) {
850
+ this._headers["cache-control"] = renderCacheControl(data);
851
+ return this;
852
+ }
853
+ /** Sets the `Age` header in seconds (chainable). Accepts a number or time string (e.g. `'2h 15m'`). */
854
+ setAge(value) {
855
+ this._headers.age = convertTime(value, "s").toString();
856
+ return this;
857
+ }
858
+ /** Sets the `Expires` header (chainable). Accepts a `Date`, date string, or timestamp. */
859
+ setExpires(value) {
860
+ this._headers.expires = typeof value === "string" || typeof value === "number" ? new Date(value).toUTCString() : value.toUTCString();
861
+ return this;
862
+ }
863
+ /** Sets or clears the `Pragma: no-cache` header (chainable). */
864
+ setPragmaNoCache(value = true) {
865
+ this._headers.pragma = value ? "no-cache" : "";
866
+ return this;
867
+ }
868
+ /**
869
+ * Returns the underlying Node.js `ServerResponse`.
870
+ * @param passthrough - If `true`, the framework still manages the response lifecycle. If `false` (default), the response is marked as "responded" and the framework will not touch it.
871
+ */
872
+ getRawRes(passthrough) {
873
+ if (!passthrough) this._responded = true;
874
+ return this._res;
875
+ }
876
+ /** Whether the response has already been sent (or the underlying stream is no longer writable). */
877
+ get responded() {
878
+ return this._responded || !this._res.writable || this._res.writableEnded;
879
+ }
880
+ renderBody() {
881
+ const body = this._body;
882
+ if (body === void 0 || body === null) return "";
883
+ if (typeof body === "string") {
884
+ if (!this._headers["content-type"]) this._headers["content-type"] = "text/plain";
885
+ return body;
886
+ }
887
+ if (typeof body === "boolean" || typeof body === "number") {
888
+ if (!this._headers["content-type"]) this._headers["content-type"] = "text/plain";
889
+ return body.toString();
890
+ }
891
+ if (body instanceof Uint8Array) return body;
892
+ if (typeof body === "object") {
893
+ if (!this._headers["content-type"]) this._headers["content-type"] = "application/json";
894
+ return JSON.stringify(body);
895
+ }
896
+ throw new Error(`Unsupported body format "${typeof body}"`);
897
+ }
898
+ renderError(data, _ctx) {
899
+ this._status = data.statusCode || 500;
900
+ this._headers["content-type"] = "application/json";
901
+ this._body = JSON.stringify(data);
902
+ }
903
+ /** Renders and sends an HTTP error response. Called automatically by the framework when a handler throws an `HttpError`. */
904
+ sendError(error, ctx) {
905
+ const data = error.body;
906
+ this.renderError(data, ctx);
907
+ return this.send();
908
+ }
909
+ /**
910
+ * Finalizes and sends the response.
911
+ *
912
+ * Flushes all accumulated headers (including cookies) in a single `writeHead()` call,
913
+ * then writes the body. Supports `Readable` streams, `fetch` `Response` objects, and regular values.
914
+ *
915
+ * @throws Error if the response was already sent.
916
+ */
917
+ send() {
918
+ if (this._responded) {
919
+ const err = /* @__PURE__ */ new Error("The response was already sent.");
920
+ this._logger.error(err.message, err);
921
+ throw err;
922
+ }
923
+ this._responded = true;
924
+ this.finalizeCookies();
925
+ const body = this._body;
926
+ const method = this._req.method;
927
+ if (body instanceof Readable$1) return this.sendStream(body, method);
928
+ if (hasFetchResponse && body instanceof Response) return this.sendFetchResponse(body, method);
929
+ this.sendRegular(method);
930
+ }
931
+ finalizeCookies() {
932
+ if (!this._hasCookies) return;
933
+ const entries = Object.entries(this._cookies);
934
+ const rendered = [];
935
+ for (const [name, data] of entries) if (data) rendered.push(renderCookie(name, data));
936
+ if (this._rawCookies.length > 0) rendered.push(...this._rawCookies);
937
+ if (rendered.length > 0) {
938
+ const existing = this._headers["set-cookie"];
939
+ if (existing) this._headers["set-cookie"] = [...Array.isArray(existing) ? existing : [existing], ...rendered];
940
+ else this._headers["set-cookie"] = rendered;
941
+ }
942
+ }
943
+ autoStatus(hasBody) {
944
+ if (this._status) return;
945
+ if (!hasBody) {
946
+ this._status = EHttpStatusCode.NoContent;
947
+ return;
948
+ }
949
+ this._status = defaultStatus[this._req.method] || EHttpStatusCode.OK;
950
+ }
951
+ sendStream(stream, method) {
952
+ this.autoStatus(true);
953
+ this._res.writeHead(this._status, this._headers);
954
+ this._req.once("close", () => {
955
+ stream.destroy();
956
+ });
957
+ if (method === "HEAD") {
958
+ stream.destroy();
959
+ this._res.end();
960
+ return Promise.resolve();
961
+ }
962
+ return new Promise((resolve, reject) => {
963
+ stream.on("error", (e) => {
964
+ this._logger.error("Stream error", e);
965
+ stream.destroy();
966
+ this._res.end();
967
+ reject(e);
968
+ });
969
+ stream.on("close", () => {
970
+ stream.destroy();
971
+ resolve();
972
+ });
973
+ stream.pipe(this._res);
974
+ });
975
+ }
976
+ async sendFetchResponse(fetchResponse, method) {
977
+ this._status = this._status || fetchResponse.status;
978
+ const fetchContentLength = fetchResponse.headers.get("content-length");
979
+ if (fetchContentLength) this._headers["content-length"] = fetchContentLength;
980
+ const fetchContentType = fetchResponse.headers.get("content-type");
981
+ if (fetchContentType) this._headers["content-type"] = fetchContentType;
982
+ this._res.writeHead(this._status, this._headers);
983
+ if (method === "HEAD") {
984
+ this._res.end();
985
+ return;
986
+ }
987
+ const fetchBody = fetchResponse.body;
988
+ if (fetchBody) try {
989
+ for await (const chunk of fetchBody) this._res.write(chunk);
990
+ } catch (error) {
991
+ this._logger.error("Error streaming fetch response body", error);
992
+ }
993
+ if (!this._res.writableEnded) this._res.end();
994
+ }
995
+ sendRegular(method) {
996
+ const renderedBody = this.renderBody();
997
+ this.autoStatus(!!renderedBody);
998
+ const contentLength = typeof renderedBody === "string" ? Buffer.byteLength(renderedBody) : renderedBody.byteLength;
999
+ this._headers["content-length"] = contentLength.toString();
1000
+ this._res.writeHead(this._status, this._headers).end(method === "HEAD" ? "" : renderedBody);
1001
+ }
1002
+ };
1003
+
1004
+ //#endregion
1005
+ //#region packages/event-http/src/event-http.ts
1006
+ /** Creates an async event context for an incoming HTTP request/response pair. */
1007
+ function createHttpContext(data, options, ResponseClass = HttpResponse) {
1008
+ const ctx = new EventContext(options);
1009
+ const response = new ResponseClass(data.res, data.req, ctx.logger);
1010
+ return (fn) => run(ctx, () => ctx.seed(httpKind, {
1011
+ req: data.req,
1012
+ response,
1013
+ requestLimits: data.requestLimits
1014
+ }, fn));
1015
+ }
1016
+ /** Returns the current HTTP event context. */
1017
+ function useHttpContext(ctx) {
1018
+ return ctx ?? current();
1019
+ }
1020
+
1021
+ //#endregion
1022
+ //#region packages/event-http/src/errors/403.tl.svg
1023
+ function _403_tl_default(ctx) {
1024
+ return `<svg height="64" viewBox="0 4 100 96" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="#888888" stroke-width="2">
1025
+ <path d="M50 90.625C64.4042 87.1875 83.5751 69.8667 83.5751 48.6937V24.0854L50 9.375L16.425 24.0833V48.6937C16.425 69.8667 35.5959 87.1875 50 90.625Z" fill="#ff000050">
1026
+ <animate attributeName="fill" dur="2s" repeatCount="indefinite"
1027
+ values="#ff000000;#ff000050;#ff000000" />
1028
+ </path>
1029
+
1030
+ <path d="M61.5395 46.0812H38.4604C37.1061 46.0812 36.0083 47.1791 36.0083 48.5333V65.075C36.0083 66.4292 37.1061 67.5271 38.4604 67.5271H61.5395C62.8938 67.5271 63.9916 66.4292 63.9916 65.075V48.5333C63.9916 47.1791 62.8938 46.0812 61.5395 46.0812Z" />
1031
+
1032
+ <path d="M41.7834 46.0834V39.6813C41.7834 37.5021 42.6491 35.4121 44.1901 33.8712C45.731 32.3303 47.8209 31.4646 50.0001 31.4646C52.1793 31.4646 54.2693 32.3303 55.8102 33.8712C57.3511 35.4121 58.2168 37.5021 58.2168 39.6813V46.0813" />
1033
+ </svg>
1034
+ `;
1035
+ }
1036
+
1037
+ //#endregion
1038
+ //#region packages/event-http/src/errors/404.tl.svg
1039
+ function _404_tl_default(ctx) {
1040
+ return `<svg height="64" viewBox="0 20 100 64" fill="none" xmlns="http://www.w3.org/2000/svg">
1041
+ <defs>
1042
+ <path id="sheet" d="M86.5 36.5V47.5H97.5V92.5H52.5V36.5H86.5ZM63 68.5H87V67.5H63V68.5ZM63 63.5H87V62.5H63V63.5ZM63 58.5H87V57.5H63V58.5ZM96.793 46.5H87.5V37.207L96.793 46.5Z" fill="#88888833" stroke="#888888aa"/>
1043
+ </defs>
1044
+
1045
+ <g id="queue" transform="translate(-5 -10)">
1046
+ <use href="#sheet" opacity="0">
1047
+ <animateTransform attributeName="transform" type="translate"
1048
+ dur="3s" repeatCount="indefinite"
1049
+ keyTimes="0;0.1;0.32;0.42;1"
1050
+ values="30 0; -20 0; -20 0; -70 0; -70 0" />
1051
+ <animate attributeName="opacity"
1052
+ dur="3s" repeatCount="indefinite"
1053
+ keyTimes="0;0.1;0.32;0.42;1"
1054
+ values="0;1;1;0;0" />
1055
+ </use>
1056
+
1057
+ <use href="#sheet" opacity="0">
1058
+ <animateTransform attributeName="transform" type="translate"
1059
+ dur="3s" begin="1s" repeatCount="indefinite"
1060
+ keyTimes="0;0.1;0.32;0.42;1"
364
1061
  values="30 0; -20 0; -20 0; -70 0; -70 0" />
365
1062
  <animate attributeName="opacity"
366
1063
  dur="3s" begin="1s" repeatCount="indefinite"
@@ -672,781 +1369,75 @@ function error_tl_default(ctx) {
672
1369
  }
673
1370
 
674
1371
  //#endregion
675
- //#region packages/event-http/src/errors/error-renderer.ts
1372
+ //#region packages/event-http/src/response/wooks-http-response.ts
676
1373
  let framework = {
677
- version: "0.6.4",
678
- poweredBy: `wooksjs`,
679
- link: `https://wooks.moost.org/`,
680
- image: `https://wooks.moost.org/wooks-full-logo.png`
1374
+ version: "0.6.6",
1375
+ poweredBy: "wooksjs",
1376
+ link: "https://wooks.moost.org/",
1377
+ image: "https://wooks.moost.org/wooks-full-logo.png"
681
1378
  };
682
- /** Renders HTTP error responses in HTML, JSON, or plain text based on the Accept header. */
683
- var HttpErrorRenderer = class extends BaseHttpResponseRenderer {
684
- constructor(opts) {
685
- super();
686
- this.opts = opts;
687
- }
688
- icons = {
689
- 401: typeof _403_tl_default === "function" ? _403_tl_default({}) : "",
690
- 403: typeof _403_tl_default === "function" ? _403_tl_default({}) : "",
691
- 404: typeof _404_tl_default === "function" ? _404_tl_default({}) : "",
692
- 500: typeof _500_tl_default === "function" ? _500_tl_default({}) : ""
693
- };
1379
+ const icons = {
1380
+ 401: typeof _403_tl_default === "function" ? _403_tl_default({}) : "",
1381
+ 403: typeof _403_tl_default === "function" ? _403_tl_default({}) : "",
1382
+ 404: typeof _404_tl_default === "function" ? _404_tl_default({}) : "",
1383
+ 500: typeof _500_tl_default === "function" ? _500_tl_default({}) : ""
1384
+ };
1385
+ /**
1386
+ * Default `HttpResponse` subclass used by `createHttpApp`.
1387
+ *
1388
+ * Overrides error rendering to produce content-negotiated responses (JSON, HTML, or plain text)
1389
+ * based on the request's `Accept` header. HTML error pages include SVG icons and framework branding.
1390
+ */
1391
+ var WooksHttpResponse = class extends HttpResponse {
1392
+ /** Registers framework metadata (name, version, link, logo) used in HTML error pages. */
694
1393
  static registerFramework(opts) {
695
1394
  framework = opts;
696
1395
  }
697
- renderHtml(response) {
698
- const data = response.body || {};
699
- response.setContentType("text/html");
700
- const hasDetails = Object.keys(data).length > 3;
701
- const icon = data.statusCode >= 500 ? this.icons[500] : this.icons[data.statusCode] || "";
702
- return typeof error_tl_default === "function" ? error_tl_default({
703
- icon,
704
- statusCode: data.statusCode,
705
- statusMessage: httpStatusCodes[data.statusCode],
706
- message: data.message,
707
- details: hasDetails ? JSON.stringify(data, null, " ") : "",
708
- version: (this.opts || framework).version,
709
- poweredBy: (this.opts || framework).poweredBy,
710
- link: (this.opts || framework).link,
711
- image: (this.opts || framework).image
712
- }) : JSON.stringify(data, null, " ");
713
- }
714
- renderText(response) {
715
- const data = response.body || {};
716
- response.setContentType("text/plain");
717
- const keys = Object.keys(data).filter((key) => ![
718
- "statusCode",
719
- "error",
720
- "message"
721
- ].includes(key));
722
- return `${data.statusCode} ${httpStatusCodes[data.statusCode]}\n${data.message}\n\n${keys.length > 0 ? `${JSON.stringify({
723
- ...data,
724
- statusCode: void 0,
725
- message: void 0,
726
- error: void 0
727
- }, null, " ")}` : ""}`;
728
- }
729
- renderJson(response) {
730
- const data = response.body || {};
731
- response.setContentType("application/json");
732
- const keys = Object.keys(data).filter((key) => ![
733
- "statusCode",
734
- "error",
735
- "message"
736
- ].includes(key));
737
- return `{"statusCode":${escapeQuotes(data.statusCode)},"error":"${escapeQuotes(data.error)}","message":"${escapeQuotes(data.message)}"${keys.length > 0 ? `,${keys.map((k) => `"${escapeQuotes(k)}":${JSON.stringify(data[k])}`).join(",")}` : ""}}`;
738
- }
739
- render(response) {
740
- const { acceptsJson, acceptsText, acceptsHtml } = useAccept();
741
- response.status = response.body?.statusCode || 500;
742
- if (acceptsJson()) return this.renderJson(response);
743
- else if (acceptsHtml()) return this.renderHtml(response);
744
- else if (acceptsText()) return this.renderText(response);
745
- else return this.renderJson(response);
746
- }
747
- };
748
- function escapeQuotes(s) {
749
- return (typeof s === "number" ? s : s || "").toString().replace(/"/gu, "\\\"");
750
- }
751
-
752
- //#endregion
753
- //#region packages/event-http/src/errors/http-error.ts
754
- /** Represents an HTTP error with a status code and optional structured body. */
755
- var HttpError = class extends Error {
756
- name = "HttpError";
757
- constructor(code = 500, _body = "") {
758
- super(typeof _body === "string" ? _body : _body.message);
759
- this.code = code;
760
- this._body = _body;
761
- }
762
- get body() {
763
- return typeof this._body === "string" ? {
764
- statusCode: this.code,
765
- message: this.message,
766
- error: httpStatusCodes[this.code]
767
- } : {
768
- ...this._body,
769
- statusCode: this.code,
770
- message: this.message,
771
- error: httpStatusCodes[this.code]
772
- };
773
- }
774
- renderer;
775
- attachRenderer(renderer) {
776
- this.renderer = renderer;
777
- }
778
- getRenderer() {
779
- return this.renderer;
780
- }
781
- };
782
-
783
- //#endregion
784
- //#region packages/event-http/src/composables/request.ts
785
- const xForwardedFor = "x-forwarded-for";
786
- /** Default safety limits for request body reading (size, ratio, timeout). */
787
- const DEFAULT_LIMITS = {
788
- maxCompressed: 1 * 1024 * 1024,
789
- maxInflated: 10 * 1024 * 1024,
790
- maxRatio: 100,
791
- readTimeoutMs: 1e4
792
- };
793
- /**
794
- * Provides access to the incoming HTTP request (method, url, headers, body, IP).
795
- * @example
796
- * ```ts
797
- * const { method, url, rawBody, getIp } = useRequest()
798
- * const body = await rawBody()
799
- * ```
800
- */
801
- function useRequest() {
802
- const { store } = useHttpContext();
803
- const { init } = store("request");
804
- const event = store("event");
805
- const req = event.get("req");
806
- const contentEncoding = req.headers["content-encoding"];
807
- const contentEncodings = () => init("contentEncodings", () => (contentEncoding || "").split(",").map((p) => p.trim()).filter((p) => !!p));
808
- const isCompressed = () => init("isCompressed", () => {
809
- const parts = contentEncodings();
810
- for (const p of parts) if ([
811
- "deflate",
812
- "gzip",
813
- "br"
814
- ].includes(p)) return true;
815
- return false;
816
- });
817
- const limits = () => event.get("requestLimits");
818
- const setLimit = (key, value) => {
819
- let obj = limits();
820
- if (!obj?.perRequest) {
821
- obj = {
822
- ...obj,
823
- perRequest: true
824
- };
825
- event.set("requestLimits", obj);
826
- }
827
- obj[key] = value;
828
- };
829
- const getMaxCompressed = () => limits()?.maxCompressed ?? DEFAULT_LIMITS.maxCompressed;
830
- const setMaxCompressed = (limit) => setLimit("maxCompressed", limit);
831
- const getMaxInflated = () => limits()?.maxInflated ?? DEFAULT_LIMITS.maxInflated;
832
- const setMaxInflated = (limit) => setLimit("maxInflated", limit);
833
- const getMaxRatio = () => limits()?.maxRatio ?? DEFAULT_LIMITS.maxRatio;
834
- const setMaxRatio = (limit) => setLimit("maxRatio", limit);
835
- const getReadTimeoutMs = () => limits()?.readTimeoutMs ?? DEFAULT_LIMITS.readTimeoutMs;
836
- const setReadTimeoutMs = (limit) => setLimit("readTimeoutMs", limit);
837
- const rawBody = () => init("rawBody", async () => {
838
- const encs = contentEncodings();
839
- const isZip = isCompressed();
840
- const streamable = isZip && encodingSupportsStream(encs);
841
- const maxCompressed = getMaxCompressed();
842
- const maxInflated = getMaxInflated();
843
- const maxRatio = getMaxRatio();
844
- const timeoutMs = getReadTimeoutMs();
845
- const cl = Number(req.headers["content-length"] ?? 0);
846
- const upfrontLimit = isZip ? maxCompressed : maxInflated;
847
- if (cl && cl > upfrontLimit) throw new HttpError(413, "Payload Too Large");
848
- for (const enc of encs) if (!compressors[enc]) throw new HttpError(415, `Unsupported Content-Encoding "${enc}"`);
849
- let timer = null;
850
- function resetTimer() {
851
- if (timeoutMs === 0) return;
852
- clearTimer();
853
- timer = setTimeout(() => {
854
- clearTimer();
855
- req.destroy();
856
- }, timeoutMs);
857
- }
858
- function clearTimer() {
859
- if (timer) {
860
- clearTimeout(timer);
861
- timer = null;
862
- }
863
- }
864
- let rawBytes = 0;
865
- async function* limitedCompressed() {
866
- resetTimer();
867
- try {
868
- for await (const chunk of req) {
869
- rawBytes += chunk.length;
870
- if (rawBytes > upfrontLimit) {
871
- req.destroy();
872
- throw new HttpError(413, "Payload Too Large");
873
- }
874
- resetTimer();
875
- yield chunk;
876
- }
877
- } finally {
878
- clearTimer();
879
- }
880
- }
881
- let stream = limitedCompressed();
882
- if (streamable) stream = await uncompressBodyStream(encs, stream);
883
- const chunks = [];
884
- let inflatedBytes = 0;
885
- try {
886
- for await (const chunk of stream) {
887
- inflatedBytes += chunk.length;
888
- if (inflatedBytes > maxInflated) throw new HttpError(413, "Inflated body too large");
889
- chunks.push(chunk);
890
- }
891
- } catch (error) {
892
- if (error instanceof HttpError) throw error;
893
- throw new HttpError(408, "Request body timeout");
894
- }
895
- let body = Buffer$1.concat(chunks);
896
- if (!streamable && isZip) {
897
- body = await uncompressBody(encs, body);
898
- inflatedBytes = body.byteLength;
899
- if (inflatedBytes > maxInflated) throw new HttpError(413, "Inflated body too large");
900
- }
901
- if (isZip && rawBytes > 0 && inflatedBytes / rawBytes > maxRatio) throw new HttpError(413, "Compression ratio too high");
902
- return body;
903
- });
904
- const reqId = useEventId().getId;
905
- const forwardedIp = () => init("forwardedIp", () => {
906
- if (typeof req.headers[xForwardedFor] === "string" && req.headers[xForwardedFor]) return req.headers[xForwardedFor].split(",").shift()?.trim();
907
- else return "";
908
- });
909
- const remoteIp = () => init("remoteIp", () => req.socket.remoteAddress || req.connection.remoteAddress || "");
910
- function getIp(options) {
911
- if (options?.trustProxy) return forwardedIp() || getIp();
912
- else return remoteIp();
913
- }
914
- const getIpList = () => init("ipList", () => ({
915
- remoteIp: req.socket.remoteAddress || req.connection.remoteAddress || "",
916
- forwarded: (req.headers[xForwardedFor] || "").split(",").map((s) => s.trim())
917
- }));
918
- return {
919
- rawRequest: req,
920
- url: req.url,
921
- method: req.method,
922
- headers: req.headers,
923
- rawBody,
924
- reqId,
925
- getIp,
926
- getIpList,
927
- isCompressed,
928
- getMaxCompressed,
929
- setMaxCompressed,
930
- getReadTimeoutMs,
931
- setReadTimeoutMs,
932
- getMaxInflated,
933
- setMaxInflated,
934
- getMaxRatio,
935
- setMaxRatio
936
- };
937
- }
938
-
939
- //#endregion
940
- //#region packages/event-http/src/composables/headers.ts
941
- /**
942
- * Returns the incoming request headers.
943
- * @example
944
- * ```ts
945
- * const { host, authorization } = useHeaders()
946
- * ```
947
- */
948
- function useHeaders() {
949
- return useRequest().headers;
950
- }
951
- /**
952
- * Provides methods to set, get, and remove outgoing response headers.
953
- * @example
954
- * ```ts
955
- * const { setHeader, setContentType, enableCors } = useSetHeaders()
956
- * setHeader('x-request-id', '123')
957
- * ```
958
- */
959
- function useSetHeaders() {
960
- const { store } = useHttpContext();
961
- const setHeaderStore = store("setHeader");
962
- function setHeader(name, value) {
963
- setHeaderStore.set(name, value.toString());
964
- }
965
- function setContentType(value) {
966
- setHeader("content-type", value);
967
- }
968
- function enableCors(origin = "*") {
969
- setHeader("access-control-allow-origin", origin);
970
- }
971
- return {
972
- setHeader,
973
- getHeader: setHeaderStore.get,
974
- removeHeader: setHeaderStore.del,
975
- setContentType,
976
- headers: () => setHeaderStore.value || {},
977
- enableCors
978
- };
979
- }
980
- /** Returns a hookable accessor for a single outgoing response header by name. */
981
- function useSetHeader(name) {
982
- const { store } = useHttpContext();
983
- const { hook } = store("setHeader");
984
- return hook(name);
985
- }
986
-
987
- //#endregion
988
- //#region packages/event-http/src/composables/cookies.ts
989
- /**
990
- * Provides access to parsed request cookies.
991
- * @example
992
- * ```ts
993
- * const { getCookie, rawCookies } = useCookies()
994
- * const sessionId = getCookie('session_id')
995
- * ```
996
- */
997
- function useCookies() {
998
- const { store } = useHttpContext();
999
- const { cookie } = useHeaders();
1000
- const { init } = store("cookies");
1001
- const getCookie = (name) => init(name, () => {
1002
- if (cookie) {
1003
- const result = new RegExp(`(?:^|; )${escapeRegex(name)}=(.*?)(?:;?$|; )`, "i").exec(cookie);
1004
- return result?.[1] ? safeDecodeURIComponent(result[1]) : null;
1005
- } else return null;
1006
- });
1007
- return {
1008
- rawCookies: cookie,
1009
- getCookie
1010
- };
1011
- }
1012
- /** Provides methods to set, get, remove, and clear outgoing response cookies. */
1013
- function useSetCookies() {
1014
- const { store } = useHttpContext();
1015
- const cookiesStore = store("setCookies");
1016
- function setCookie(name, value, attrs) {
1017
- cookiesStore.set(name, {
1018
- value,
1019
- attrs: attrs || {}
1020
- });
1021
- }
1022
- function cookies() {
1023
- return cookiesStore.entries().filter((a) => !!a[1]).map(([key, value]) => renderCookie(key, value));
1024
- }
1025
- return {
1026
- setCookie,
1027
- getCookie: cookiesStore.get,
1028
- removeCookie: cookiesStore.del,
1029
- clearCookies: cookiesStore.clear,
1030
- cookies
1031
- };
1032
- }
1033
- /** Returns a hookable accessor for a single outgoing cookie by name. */
1034
- function useSetCookie(name) {
1035
- const { setCookie, getCookie } = useSetCookies();
1036
- const valueHook = attachHook({
1037
- name,
1038
- type: "cookie"
1039
- }, {
1040
- get: () => getCookie(name)?.value,
1041
- set: (value) => {
1042
- setCookie(name, value, getCookie(name)?.attrs);
1043
- }
1044
- });
1045
- return attachHook(valueHook, {
1046
- get: () => getCookie(name)?.attrs,
1047
- set: (attrs) => {
1048
- setCookie(name, getCookie(name)?.value || "", attrs);
1049
- }
1050
- }, "attrs");
1051
- }
1052
-
1053
- //#endregion
1054
- //#region packages/event-http/src/composables/header-accept.ts
1055
- /** Provides helpers to check the request's Accept header for supported MIME types. */
1056
- function useAccept() {
1057
- const { store } = useHttpContext();
1058
- const { accept } = useHeaders();
1059
- const accepts = (mime) => {
1060
- const { set, get, has } = store("accept");
1061
- if (!has(mime)) return set(mime, !!(accept && (accept === "*/*" || accept.includes(mime))));
1062
- return get(mime);
1063
- };
1064
- return {
1065
- accept,
1066
- accepts,
1067
- acceptsJson: () => accepts("application/json"),
1068
- acceptsXml: () => accepts("application/xml"),
1069
- acceptsText: () => accepts("text/plain"),
1070
- acceptsHtml: () => accepts("text/html")
1071
- };
1072
- }
1073
-
1074
- //#endregion
1075
- //#region packages/event-http/src/composables/header-authorization.ts
1076
- /**
1077
- * Provides parsed access to the Authorization header (type, credentials, Basic decoding).
1078
- * @example
1079
- * ```ts
1080
- * const { isBearer, authRawCredentials, basicCredentials } = useAuthorization()
1081
- * if (isBearer()) { const token = authRawCredentials() }
1082
- * ```
1083
- */
1084
- function useAuthorization() {
1085
- const { store } = useHttpContext();
1086
- const { authorization } = useHeaders();
1087
- const { init } = store("authorization");
1088
- const authType = () => init("type", () => {
1089
- if (authorization) {
1090
- const space = authorization.indexOf(" ");
1091
- return authorization.slice(0, space);
1092
- }
1093
- return null;
1094
- });
1095
- const authRawCredentials = () => init("credentials", () => {
1096
- if (authorization) {
1097
- const space = authorization.indexOf(" ");
1098
- return authorization.slice(space + 1);
1099
- }
1100
- return null;
1101
- });
1102
- return {
1103
- authorization,
1104
- authType,
1105
- authRawCredentials,
1106
- isBasic: () => authType()?.toLocaleLowerCase() === "basic",
1107
- isBearer: () => authType()?.toLocaleLowerCase() === "bearer",
1108
- basicCredentials: () => init("basicCredentials", () => {
1109
- if (authorization) {
1110
- const type = authType();
1111
- if (type?.toLocaleLowerCase() === "basic") {
1112
- const creds = Buffer$1.from(authRawCredentials() || "", "base64").toString("ascii");
1113
- const [username, password] = creds.split(":");
1114
- return {
1115
- username,
1116
- password
1117
- };
1118
- }
1119
- }
1120
- return null;
1121
- })
1122
- };
1123
- }
1124
-
1125
- //#endregion
1126
- //#region packages/event-http/src/utils/cache-control.ts
1127
- function renderCacheControl(data) {
1128
- let attrs = "";
1129
- for (const [a, v] of Object.entries(data)) {
1130
- if (v === void 0) continue;
1131
- const func = cacheControlFunc[a];
1132
- if (typeof func === "function") {
1133
- const val = func(v);
1134
- if (val) attrs += attrs ? `, ${val}` : val;
1135
- } else throw new TypeError(`Unknown Cache-Control attribute ${a}`);
1136
- }
1137
- return attrs;
1138
- }
1139
- const cacheControlFunc = {
1140
- mustRevalidate: (v) => v ? "must-revalidate" : "",
1141
- noCache: (v) => v ? typeof v === "string" ? `no-cache="${v}"` : "no-cache" : "",
1142
- noStore: (v) => v ? "no-store" : "",
1143
- noTransform: (v) => v ? "no-transform" : "",
1144
- public: (v) => v ? "public" : "",
1145
- private: (v) => v ? typeof v === "string" ? `private="${v}"` : "private" : "",
1146
- proxyRevalidate: (v) => v ? "proxy-revalidate" : "",
1147
- maxAge: (v) => `max-age=${convertTime(v, "s").toString()}`,
1148
- sMaxage: (v) => `s-maxage=${convertTime(v, "s").toString()}`
1149
- };
1150
-
1151
- //#endregion
1152
- //#region packages/event-http/src/composables/header-set-cache-control.ts
1153
- const renderAge = (v) => convertTime(v, "s").toString();
1154
- const renderExpires = (v) => typeof v === "string" || typeof v === "number" ? new Date(v).toUTCString() : v.toUTCString();
1155
- const renderPragmaNoCache = (v) => v ? "no-cache" : "";
1156
- /** Provides helpers to set cache-related response headers (Cache-Control, Expires, Age, Pragma). */
1157
- function useSetCacheControl() {
1158
- const { setHeader } = useSetHeaders();
1159
- const setAge = (value) => {
1160
- setHeader("age", renderAge(value));
1161
- };
1162
- const setExpires = (value) => {
1163
- setHeader("expires", renderExpires(value));
1164
- };
1165
- const setPragmaNoCache = (value = true) => {
1166
- setHeader("pragma", renderPragmaNoCache(value));
1167
- };
1168
- const setCacheControl = (data) => {
1169
- setHeader("cache-control", renderCacheControl(data));
1170
- };
1171
- return {
1172
- setExpires,
1173
- setAge,
1174
- setPragmaNoCache,
1175
- setCacheControl
1176
- };
1177
- }
1178
-
1179
- //#endregion
1180
- //#region packages/event-http/src/composables/response.ts
1181
- /**
1182
- * Provides access to the raw HTTP response and status code management.
1183
- * @example
1184
- * ```ts
1185
- * const { status, rawResponse, hasResponded } = useResponse()
1186
- * status(200)
1187
- * ```
1188
- */
1189
- function useResponse() {
1190
- const { store } = useHttpContext();
1191
- const event = store("event");
1192
- const res = event.get("res");
1193
- const responded = store("response").hook("responded");
1194
- const statusCode = store("status").hook("code");
1195
- function status(code) {
1196
- return statusCode.value = code ? code : statusCode.value;
1197
- }
1198
- const rawResponse = (options) => {
1199
- if (!options || !options.passthrough) responded.value = true;
1200
- return res;
1201
- };
1202
- return {
1203
- rawResponse,
1204
- hasResponded: () => responded.value || !res.writable || res.writableEnded,
1205
- status: attachHook(status, {
1206
- get: () => statusCode.value,
1207
- set: (code) => statusCode.value = code
1208
- })
1209
- };
1210
- }
1211
- /** Returns a hookable accessor for the response status code. */
1212
- function useStatus() {
1213
- const { store } = useHttpContext();
1214
- return store("status").hook("code");
1215
- }
1216
-
1217
- //#endregion
1218
- //#region packages/event-http/src/utils/url-search-params.ts
1219
- const ILLEGAL_KEYS = new Set([
1220
- "__proto__",
1221
- "constructor",
1222
- "prototype"
1223
- ]);
1224
- var WooksURLSearchParams = class extends URLSearchParams {
1225
- toJson() {
1226
- const json = Object.create(null);
1227
- for (const [key, value] of this.entries()) if (isArrayParam(key)) {
1228
- const a = json[key] = json[key] || [];
1229
- a.push(value);
1230
- } else {
1231
- if (ILLEGAL_KEYS.has(key)) throw new HttpError(400, `Illegal key name "${key}"`);
1232
- if (key in json) throw new HttpError(400, `Duplicate key "${key}"`);
1233
- json[key] = value;
1234
- }
1235
- return json;
1236
- }
1237
- };
1238
- function isArrayParam(name) {
1239
- return name.endsWith("[]");
1240
- }
1241
-
1242
- //#endregion
1243
- //#region packages/event-http/src/composables/search-params.ts
1244
- /**
1245
- * Provides access to URL search (query) parameters from the request.
1246
- * @example
1247
- * ```ts
1248
- * const { urlSearchParams, jsonSearchParams } = useSearchParams()
1249
- * const page = urlSearchParams().get('page')
1250
- * ```
1251
- */
1252
- function useSearchParams() {
1253
- const { store } = useHttpContext();
1254
- const url = useRequest().url || "";
1255
- const { init } = store("searchParams");
1256
- const rawSearchParams = () => init("raw", () => {
1257
- const i = url.indexOf("?");
1258
- return i >= 0 ? url.slice(i) : "";
1259
- });
1260
- const urlSearchParams = () => init("urlSearchParams", () => new WooksURLSearchParams(rawSearchParams()));
1261
- return {
1262
- rawSearchParams,
1263
- urlSearchParams,
1264
- jsonSearchParams: () => urlSearchParams().toJson()
1265
- };
1266
- }
1267
-
1268
- //#endregion
1269
- //#region packages/event-http/src/response/core.ts
1270
- const defaultStatus = {
1271
- GET: EHttpStatusCode.OK,
1272
- POST: EHttpStatusCode.Created,
1273
- PUT: EHttpStatusCode.Created,
1274
- PATCH: EHttpStatusCode.Accepted,
1275
- DELETE: EHttpStatusCode.Accepted
1276
- };
1277
- const baseRenderer = new BaseHttpResponseRenderer();
1278
- var BaseHttpResponse = class {
1279
- constructor(renderer = baseRenderer) {
1280
- this.renderer = renderer;
1281
- }
1282
- _status = 0;
1283
- _body;
1284
- _headers = {};
1285
- get status() {
1286
- return this._status;
1287
- }
1288
- set status(value) {
1289
- this._status = value;
1290
- }
1291
- get body() {
1292
- return this._body;
1293
- }
1294
- set body(value) {
1295
- this._body = value;
1296
- }
1297
- setStatus(value) {
1298
- this.status = value;
1299
- return this;
1300
- }
1301
- setBody(value) {
1302
- this.body = value;
1303
- return this;
1304
- }
1305
- getContentType() {
1306
- return this._headers["content-type"];
1307
- }
1308
- setContentType(value) {
1309
- this._headers["content-type"] = value;
1310
- return this;
1311
- }
1312
- enableCors(origin = "*") {
1313
- this._headers["Access-Control-Allow-Origin"] = origin;
1314
- return this;
1315
- }
1316
- setCookie(name, value, attrs) {
1317
- const cookies = this._headers["set-cookie"] = this._headers["set-cookie"] || [];
1318
- cookies.push(renderCookie(name, {
1319
- value,
1320
- attrs: attrs || {}
1321
- }));
1322
- return this;
1323
- }
1324
- setCacheControl(data) {
1325
- this.setHeader("cache-control", renderCacheControl(data));
1326
- }
1327
- setCookieRaw(rawValue) {
1328
- const cookies = this._headers["set-cookie"] = this._headers["set-cookie"] || [];
1329
- cookies.push(rawValue);
1330
- return this;
1331
- }
1332
- header(name, value) {
1333
- this._headers[name] = value;
1334
- return this;
1335
- }
1336
- setHeader(name, value) {
1337
- return this.header(name, value);
1338
- }
1339
- getHeader(name) {
1340
- return this._headers[name];
1341
- }
1342
- mergeHeaders() {
1343
- const { headers } = useSetHeaders();
1344
- const { cookies, removeCookie } = useSetCookies();
1345
- const newCookies = this._headers["set-cookie"] || [];
1346
- for (const cookie of newCookies) removeCookie(cookie.slice(0, cookie.indexOf("=")));
1347
- this._headers = {
1348
- ...headers(),
1349
- ...this._headers
1350
- };
1351
- const setCookie = [...newCookies, ...cookies()];
1352
- if (setCookie.length > 0) this._headers["set-cookie"] = setCookie;
1353
- return this;
1354
- }
1355
- mergeStatus(renderedBody) {
1356
- this.status = this.status || useResponse().status();
1357
- if (!this.status) {
1358
- const { method } = useRequest();
1359
- this.status = renderedBody ? defaultStatus[method] || EHttpStatusCode.OK : EHttpStatusCode.NoContent;
1360
- }
1361
- return this;
1362
- }
1363
- mergeFetchStatus(fetchStatus) {
1364
- this.status = this.status || useResponse().status() || fetchStatus;
1365
- }
1366
- panic(text, logger) {
1367
- const error = new Error(text);
1368
- logger.error(error);
1369
- throw error;
1370
- }
1371
- async respond() {
1372
- const { rawResponse, hasResponded } = useResponse();
1373
- const { method, rawRequest } = useRequest();
1374
- const logger = useEventLogger$1("http-response") || console;
1375
- if (hasResponded()) this.panic("The response was already sent.", logger);
1376
- this.mergeHeaders();
1377
- const res = rawResponse();
1378
- if (this.body instanceof Readable$1) {
1379
- const stream = this.body;
1380
- this.mergeStatus("ok");
1381
- res.writeHead(this.status, { ...this._headers });
1382
- rawRequest.once("close", () => {
1383
- stream.destroy();
1384
- });
1385
- if (method === "HEAD") {
1386
- stream.destroy();
1387
- res.end();
1388
- } else return new Promise((resolve, reject) => {
1389
- stream.on("error", (e) => {
1390
- stream.destroy();
1391
- res.end();
1392
- reject(e);
1393
- });
1394
- stream.on("close", () => {
1395
- stream.destroy();
1396
- resolve(void 0);
1397
- });
1398
- stream.pipe(res);
1399
- });
1400
- } else if (globalThis.Response && this.body instanceof Response) {
1401
- this.mergeFetchStatus(this.body.status);
1402
- if (method === "HEAD") res.end();
1403
- else {
1404
- const additionalHeaders = {};
1405
- if (this.body.headers.get("content-length")) additionalHeaders["content-length"] = this.body.headers.get("content-length");
1406
- if (this.body.headers.get("content-type")) additionalHeaders["content-type"] = this.body.headers.get("content-type");
1407
- res.writeHead(this.status, {
1408
- ...additionalHeaders,
1409
- ...this._headers
1410
- });
1411
- await respondWithFetch(this.body.body, res);
1412
- }
1396
+ renderError(data, ctx) {
1397
+ this._status = data.statusCode || 500;
1398
+ const { accepts } = useAccept(ctx);
1399
+ if (accepts("json")) {
1400
+ this._headers["content-type"] = "application/json";
1401
+ this._body = JSON.stringify(data);
1402
+ } else if (accepts("html")) {
1403
+ this._headers["content-type"] = "text/html";
1404
+ this._body = renderErrorHtml(data);
1405
+ } else if (accepts("text")) {
1406
+ this._headers["content-type"] = "text/plain";
1407
+ this._body = renderErrorText(data);
1413
1408
  } else {
1414
- const renderedBody = this.renderer.render(this);
1415
- this.mergeStatus(renderedBody);
1416
- res.writeHead(this.status, {
1417
- "content-length": Buffer.byteLength(renderedBody),
1418
- ...this._headers
1419
- }).end(method === "HEAD" ? "" : renderedBody);
1409
+ this._headers["content-type"] = "application/json";
1410
+ this._body = JSON.stringify(data);
1420
1411
  }
1421
1412
  }
1422
1413
  };
1423
- async function respondWithFetch(fetchBody, res) {
1424
- if (fetchBody) try {
1425
- for await (const chunk of fetchBody) res.write(chunk);
1426
- } catch {}
1427
- res.end();
1414
+ function renderErrorHtml(data) {
1415
+ const hasDetails = Object.keys(data).length > 3;
1416
+ const icon = data.statusCode >= 500 ? icons[500] : icons[data.statusCode] || "";
1417
+ return typeof error_tl_default === "function" ? error_tl_default({
1418
+ icon,
1419
+ statusCode: data.statusCode,
1420
+ statusMessage: httpStatusCodes[data.statusCode],
1421
+ message: data.message,
1422
+ details: hasDetails ? JSON.stringify(data, null, " ") : "",
1423
+ version: framework.version,
1424
+ poweredBy: framework.poweredBy,
1425
+ link: framework.link,
1426
+ image: framework.image
1427
+ }) : JSON.stringify(data, null, " ");
1428
1428
  }
1429
-
1430
- //#endregion
1431
- //#region packages/event-http/src/response/factory.ts
1432
- function createWooksResponder(renderer = new BaseHttpResponseRenderer(), errorRenderer = new HttpErrorRenderer()) {
1433
- function createResponse(data) {
1434
- const { hasResponded } = useResponse();
1435
- if (hasResponded()) return null;
1436
- if (data instanceof Error) {
1437
- const r = new BaseHttpResponse(errorRenderer);
1438
- let httpError;
1439
- if (data instanceof HttpError) httpError = data;
1440
- else httpError = new HttpError(500, data.message);
1441
- r.setBody(httpError.body);
1442
- return r;
1443
- } else if (data instanceof BaseHttpResponse) return data;
1444
- else return new BaseHttpResponse(renderer).setBody(data);
1445
- }
1446
- return {
1447
- createResponse,
1448
- respond: (data) => createResponse(data)?.respond()
1449
- };
1429
+ function renderErrorText(data) {
1430
+ const keys = Object.keys(data).filter((key) => ![
1431
+ "statusCode",
1432
+ "error",
1433
+ "message"
1434
+ ].includes(key));
1435
+ return `${data.statusCode} ${httpStatusCodes[data.statusCode]}\n${data.message}\n\n${keys.length > 0 ? JSON.stringify({
1436
+ ...data,
1437
+ statusCode: void 0,
1438
+ message: void 0,
1439
+ error: void 0
1440
+ }, null, " ") : ""}`;
1450
1441
  }
1451
1442
 
1452
1443
  //#endregion
@@ -1454,10 +1445,14 @@ function createWooksResponder(renderer = new BaseHttpResponseRenderer(), errorRe
1454
1445
  /** HTTP adapter for Wooks that provides route registration, server lifecycle, and request handling. */
1455
1446
  var WooksHttp = class extends WooksAdapterBase {
1456
1447
  logger;
1448
+ ResponseClass;
1449
+ eventContextOptions;
1457
1450
  constructor(opts, wooks) {
1458
1451
  super(wooks, opts?.logger, opts?.router);
1459
1452
  this.opts = opts;
1460
1453
  this.logger = opts?.logger || this.getLogger(`[wooks-http]`);
1454
+ this.ResponseClass = opts?.responseClass ?? WooksHttpResponse;
1455
+ this.eventContextOptions = this.getEventContextOptions();
1461
1456
  }
1462
1457
  /** Registers a handler for all HTTP methods on the given path. */
1463
1458
  all(path, handler) {
@@ -1491,9 +1486,22 @@ var WooksHttp = class extends WooksAdapterBase {
1491
1486
  options(path, handler) {
1492
1487
  return this.on("OPTIONS", path, handler);
1493
1488
  }
1489
+ /** Registers an UPGRADE route handler for WebSocket upgrade requests. */
1490
+ upgrade(path, handler) {
1491
+ return this.on("UPGRADE", path, handler);
1492
+ }
1493
+ wsHandler;
1494
+ /** Register a WebSocket upgrade handler that implements the WooksUpgradeHandler contract. */
1495
+ ws(handler) {
1496
+ this.wsHandler = handler;
1497
+ }
1494
1498
  server;
1495
1499
  async listen(port, hostname, backlog, listeningListener) {
1496
1500
  const server = this.server = http.createServer(this.getServerCb());
1501
+ if (this.wsHandler) {
1502
+ const upgradeCb = this.getUpgradeCb();
1503
+ server.on("upgrade", upgradeCb);
1504
+ }
1497
1505
  return new Promise((resolve, reject) => {
1498
1506
  server.once("listening", resolve);
1499
1507
  server.once("error", reject);
@@ -1543,11 +1551,14 @@ var WooksHttp = class extends WooksAdapterBase {
1543
1551
  attachServer(server) {
1544
1552
  this.server = server;
1545
1553
  }
1546
- responder = createWooksResponder();
1547
- respond(data) {
1548
- this.responder.respond(data)?.catch((error) => {
1549
- this.logger.error("Uncaught response exception", error);
1550
- });
1554
+ respond(data, response, ctx) {
1555
+ if (response.responded) return;
1556
+ if (data instanceof Error) {
1557
+ const httpError = data instanceof HttpError ? data : new HttpError(500, data.message);
1558
+ return response.sendError(httpError, ctx);
1559
+ }
1560
+ if (data !== response) response.body = data;
1561
+ return response.send();
1551
1562
  }
1552
1563
  /**
1553
1564
  * Returns server callback function
@@ -1562,43 +1573,131 @@ var WooksHttp = class extends WooksAdapterBase {
1562
1573
  * ```
1563
1574
  */
1564
1575
  getServerCb() {
1576
+ const ctxOptions = this.eventContextOptions;
1577
+ const RequestLimits = this.opts?.requestLimits;
1578
+ const notFoundHandler = this.opts?.onNotFound;
1579
+ const defaultHeaders = this.opts?.defaultHeaders;
1565
1580
  return (req, res) => {
1566
- const runInContext = createHttpContext({
1567
- req,
1568
- res,
1569
- requestLimits: this.opts?.requestLimits
1570
- }, this.mergeEventOptions(this.opts?.eventOptions));
1571
- runInContext(async () => {
1572
- const { handlers } = this.wooks.lookup(req.method, req.url);
1573
- if (handlers || this.opts?.onNotFound) try {
1574
- return await this.processHandlers(handlers || [this.opts?.onNotFound]);
1575
- } catch (error) {
1576
- this.logger.error("Internal error, please report", error);
1577
- this.respond(error);
1578
- return error;
1579
- }
1580
- else {
1581
- this.logger.debug(`404 Not found (${req.method})${req.url}`);
1581
+ const ctx = new EventContext(ctxOptions);
1582
+ const response = new this.ResponseClass(res, req, ctx.logger, defaultHeaders);
1583
+ const method = req.method || "";
1584
+ const url = req.url || "";
1585
+ run(ctx, () => {
1586
+ ctx.seed(httpKind, {
1587
+ req,
1588
+ response,
1589
+ requestLimits: RequestLimits
1590
+ });
1591
+ const handlers = this.wooks.lookupHandlers(method, url, ctx);
1592
+ if (handlers || notFoundHandler) {
1593
+ const result = this.processHandlers(handlers || [notFoundHandler], ctx, response);
1594
+ if (result !== null && result !== void 0 && typeof result.then === "function") result.catch((error) => {
1595
+ this.logger.error("Internal error, please report", error);
1596
+ this.respond(error, response, ctx);
1597
+ });
1598
+ } else {
1599
+ this.logger.debug(`404 Not found (${method})${url}`);
1582
1600
  const error = new HttpError(404);
1583
- this.respond(error);
1584
- return error;
1601
+ this.respond(error, response, ctx);
1585
1602
  }
1586
1603
  });
1587
1604
  };
1588
1605
  }
1589
- async processHandlers(handlers) {
1590
- const { store } = useHttpContext();
1591
- for (const [i, handler] of handlers.entries()) {
1592
- const isLastHandler = handlers.length === i + 1;
1606
+ /**
1607
+ * Returns upgrade callback function for the HTTP server's 'upgrade' event.
1608
+ * Creates an HTTP context, seeds it with upgrade data, and routes as method 'UPGRADE'.
1609
+ */
1610
+ getUpgradeCb() {
1611
+ const ctxOptions = this.eventContextOptions;
1612
+ const requestLimits = this.opts?.requestLimits;
1613
+ const wsHandler = this.wsHandler;
1614
+ return (req, socket, head) => {
1615
+ if (!wsHandler) {
1616
+ socket.destroy();
1617
+ return;
1618
+ }
1619
+ const ctx = new EventContext(ctxOptions);
1620
+ const url = req.url || "";
1621
+ run(ctx, () => {
1622
+ ctx.seed(httpKind, {
1623
+ req,
1624
+ response: void 0,
1625
+ requestLimits
1626
+ });
1627
+ ctx.set(wsHandler.reqKey, req);
1628
+ ctx.set(wsHandler.socketKey, socket);
1629
+ ctx.set(wsHandler.headKey, head);
1630
+ const handlers = this.wooks.lookupHandlers("UPGRADE", url, ctx);
1631
+ if (handlers) this.processUpgradeHandlers(handlers, ctx, socket);
1632
+ else wsHandler.handleUpgrade(req, socket, head);
1633
+ });
1634
+ };
1635
+ }
1636
+ processUpgradeHandlers(handlers, ctx, socket) {
1637
+ for (let i = 0; i < handlers.length; i++) {
1638
+ const handler = handlers[i];
1639
+ const isLastHandler = i === handlers.length - 1;
1640
+ try {
1641
+ const result = handler();
1642
+ if (result !== null && result !== void 0 && typeof result.then === "function") {
1643
+ result.catch((error) => {
1644
+ this.logger.error(`Upgrade handler error: ${ctx.get(httpKind.keys.req)?.url || ""}`, error);
1645
+ socket.destroy();
1646
+ });
1647
+ return;
1648
+ }
1649
+ return;
1650
+ } catch (error) {
1651
+ if (!(error instanceof HttpError)) this.logger.error(`Upgrade handler error: ${ctx.get(httpKind.keys.req)?.url || ""}`, error);
1652
+ if (isLastHandler) {
1653
+ socket.destroy();
1654
+ return;
1655
+ }
1656
+ }
1657
+ }
1658
+ }
1659
+ processHandlers(handlers, ctx, response) {
1660
+ for (let i = 0; i < handlers.length; i++) {
1661
+ const handler = handlers[i];
1662
+ const isLastHandler = i === handlers.length - 1;
1663
+ try {
1664
+ const result = handler();
1665
+ if (result !== null && result !== void 0 && typeof result.then === "function") return this.processAsyncResult(result, handlers, i, ctx, response);
1666
+ this.respond(result, response, ctx);
1667
+ return;
1668
+ } catch (error) {
1669
+ if (!(error instanceof HttpError)) this.logger.error(`Uncaught route handler exception: ${ctx.get(httpKind.keys.req)?.url || ""}`, error);
1670
+ if (isLastHandler) {
1671
+ this.respond(error, response, ctx);
1672
+ return;
1673
+ }
1674
+ }
1675
+ }
1676
+ }
1677
+ async processAsyncResult(promise, handlers, startIndex, ctx, response) {
1678
+ try {
1679
+ const result = await promise;
1680
+ await this.respond(result, response, ctx);
1681
+ return result;
1682
+ } catch (error) {
1683
+ const isLastHandler = startIndex === handlers.length - 1;
1684
+ if (!(error instanceof HttpError)) this.logger.error(`Uncaught route handler exception: ${ctx.get(httpKind.keys.req)?.url || ""}`, error);
1685
+ if (isLastHandler) {
1686
+ await this.respond(error, response, ctx);
1687
+ return error;
1688
+ }
1689
+ }
1690
+ for (let i = startIndex + 1; i < handlers.length; i++) {
1691
+ const handler = handlers[i];
1692
+ const isLastHandler = i === handlers.length - 1;
1593
1693
  try {
1594
- const promise = handler();
1595
- const result = await promise;
1596
- this.respond(result);
1694
+ const result = await handler();
1695
+ await this.respond(result, response, ctx);
1597
1696
  return result;
1598
1697
  } catch (error) {
1599
- if (error instanceof HttpError) {} else this.logger.error(`Uncaught route handler exception: ${store("event").get("req")?.url || ""}`, error);
1698
+ if (!(error instanceof HttpError)) this.logger.error(`Uncaught route handler exception: ${ctx.get(httpKind.keys.req)?.url || ""}`, error);
1600
1699
  if (isLastHandler) {
1601
- this.respond(error);
1700
+ await this.respond(error, response, ctx);
1602
1701
  return error;
1603
1702
  }
1604
1703
  }
@@ -1619,4 +1718,100 @@ function createHttpApp(opts, wooks) {
1619
1718
  }
1620
1719
 
1621
1720
  //#endregion
1622
- export { BaseHttpResponse, BaseHttpResponseRenderer, DEFAULT_LIMITS, EHttpStatusCode, HttpError, HttpErrorRenderer, WooksHttp, WooksURLSearchParams, createHttpApp, createHttpContext, createWooksResponder, httpStatusCodes, renderCacheControl, useAccept, useAuthorization, useCookies, useEventLogger, useHeaders, useHttpContext, useRequest, useResponse, useRouteParams, useSearchParams, useSetCacheControl, useSetCookie, useSetCookies, useSetHeader, useSetHeaders, useStatus };
1721
+ //#region packages/event-http/src/utils/security-headers.ts
1722
+ const HEADER_MAP = [
1723
+ [
1724
+ "contentSecurityPolicy",
1725
+ "content-security-policy",
1726
+ "default-src 'self'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'"
1727
+ ],
1728
+ [
1729
+ "crossOriginOpenerPolicy",
1730
+ "cross-origin-opener-policy",
1731
+ "same-origin"
1732
+ ],
1733
+ [
1734
+ "crossOriginResourcePolicy",
1735
+ "cross-origin-resource-policy",
1736
+ "same-origin"
1737
+ ],
1738
+ [
1739
+ "referrerPolicy",
1740
+ "referrer-policy",
1741
+ "no-referrer"
1742
+ ],
1743
+ [
1744
+ "strictTransportSecurity",
1745
+ "strict-transport-security",
1746
+ void 0
1747
+ ],
1748
+ [
1749
+ "xContentTypeOptions",
1750
+ "x-content-type-options",
1751
+ "nosniff"
1752
+ ],
1753
+ [
1754
+ "xFrameOptions",
1755
+ "x-frame-options",
1756
+ "SAMEORIGIN"
1757
+ ]
1758
+ ];
1759
+ /**
1760
+ * Returns a record of recommended HTTP security headers.
1761
+ *
1762
+ * Each option accepts a `string` (override value) or `false` (disable).
1763
+ * Omitting an option uses the default value.
1764
+ *
1765
+ * `strictTransportSecurity` is opt-in only (no default) — HSTS is dangerous if not on HTTPS.
1766
+ */
1767
+ function securityHeaders(opts) {
1768
+ const result = {};
1769
+ for (const [optKey, headerName, defaultValue] of HEADER_MAP) {
1770
+ const value = opts?.[optKey];
1771
+ if (value === false) continue;
1772
+ if (typeof value === "string") result[headerName] = value;
1773
+ else if (defaultValue !== void 0) result[headerName] = defaultValue;
1774
+ }
1775
+ return result;
1776
+ }
1777
+
1778
+ //#endregion
1779
+ //#region packages/event-http/src/testing.ts
1780
+ /**
1781
+ * Creates a fully initialized HTTP event context for testing.
1782
+ *
1783
+ * Sets up an `EventContext` with a fake `IncomingMessage`, `HttpResponse`, route params,
1784
+ * and optional pre-seeded body. Returns a runner function that executes callbacks inside the context scope.
1785
+ *
1786
+ * @example
1787
+ * ```ts
1788
+ * const run = prepareTestHttpContext({ url: '/users/42', params: { id: '42' } })
1789
+ * run(() => {
1790
+ * const { params } = useRouteParams()
1791
+ * expect(params.id).toBe('42')
1792
+ * })
1793
+ * ```
1794
+ */
1795
+ function prepareTestHttpContext(options) {
1796
+ const req = new IncomingMessage(new Socket({}));
1797
+ req.method = options.method || "GET";
1798
+ req.headers = options.headers || {};
1799
+ req.url = options.url;
1800
+ const res = new ServerResponse(req);
1801
+ const response = new HttpResponse(res, req, console, options.defaultHeaders);
1802
+ const ctx = new EventContext({ logger: console });
1803
+ ctx.seed(httpKind, {
1804
+ req,
1805
+ response,
1806
+ requestLimits: options.requestLimits
1807
+ });
1808
+ if (options.params) ctx.set(routeParamsKey, options.params);
1809
+ if (options.rawBody !== void 0) {
1810
+ const buf = Buffer$1.isBuffer(options.rawBody) ? options.rawBody : Buffer$1.from(options.rawBody);
1811
+ ctx.set(rawBodySlot, Promise.resolve(buf));
1812
+ }
1813
+ return (cb) => run(ctx, cb);
1814
+ }
1815
+
1816
+ //#endregion
1817
+ export { DEFAULT_LIMITS, EHttpStatusCode, HttpError, HttpResponse, WooksHttp, WooksHttpResponse, WooksURLSearchParams, createHttpApp, createHttpContext, httpKind, httpStatusCodes, prepareTestHttpContext, rawBodySlot, renderCacheControl, securityHeaders, useAccept, useAuthorization, useCookies, useHeaders, useHttpContext, useLogger, useRequest, useResponse, useRouteParams, useSearchParams };