@moostjs/event-http 0.6.49 → 0.6.51

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.cjs CHANGED
@@ -376,13 +376,27 @@ function createRef(getter, setter) {
376
376
  return (0, moost.Resolve)(() => (0, _wooksjs_http_body.useBody)().rawBody(), "body");
377
377
  }
378
378
 
379
+ //#endregion
380
+ //#region packages/event-http/src/decorators/http-only.ts
381
+ /**
382
+ * Wraps the callback of an HTTP-only interceptor so it runs for HTTP events only (an in-process
383
+ * `MoostHttp.invoke()` included). Any other event of a mixed controller — a CLI command, a
384
+ * workflow step, a WebSocket message — skips it, also when it runs as a child of an HTTP request:
385
+ * the parent's request and response are not its own.
386
+ */ function forHttpEvents(fn) {
387
+ return () => {
388
+ const ctx = (0, _wooksjs_event_core.current)();
389
+ if (ctx.has(_wooksjs_event_core.eventTypeKey) && ctx.get(_wooksjs_event_core.eventTypeKey) === _wooksjs_event_http.httpKind.name) fn(ctx);
390
+ };
391
+ }
392
+
379
393
  //#endregion
380
394
  //#region packages/event-http/src/decorators/set.decorator.ts
381
395
  const setHeaderInterceptor = (name, value, opts) => {
382
- const cb = () => {
383
- const response = (0, _wooksjs_event_http.useResponse)();
396
+ const cb = forHttpEvents((ctx) => {
397
+ const response = (0, _wooksjs_event_http.useResponse)(ctx);
384
398
  if ((!response.getHeader(name) || opts?.force) && (!opts?.status || opts.status === response.status)) response.setHeader(name, value);
385
- };
399
+ });
386
400
  const def = { priority: moost.TInterceptorPriority.AFTER_ALL };
387
401
  if (opts?.when !== "error") def.after = cb;
388
402
  if (opts?.when === "always" || opts?.when === "error") def.error = cb;
@@ -428,10 +442,10 @@ const setHeaderInterceptor = (name, value, opts) => {
428
442
  return (0, moost.Intercept)(setHeaderInterceptor(...args));
429
443
  }
430
444
  const setCookieInterceptor = (name, value, attrs) => ({
431
- after() {
432
- const response = (0, _wooksjs_event_http.useResponse)();
445
+ after: forHttpEvents((ctx) => {
446
+ const response = (0, _wooksjs_event_http.useResponse)(ctx);
433
447
  if (!response.getCookie(name)) response.setCookie(name, value, attrs);
434
- },
448
+ }),
435
449
  priority: moost.TInterceptorPriority.AFTER_ALL
436
450
  });
437
451
  /**
@@ -458,10 +472,10 @@ const setCookieInterceptor = (name, value, attrs) => ({
458
472
  return (0, moost.Intercept)(setCookieInterceptor(...args));
459
473
  }
460
474
  const setStatusInterceptor = (code, opts) => ({
461
- after() {
462
- const response = (0, _wooksjs_event_http.useResponse)();
475
+ after: forHttpEvents((ctx) => {
476
+ const response = (0, _wooksjs_event_http.useResponse)(ctx);
463
477
  if (!response.status || opts?.force) response.status = code;
464
- },
478
+ }),
465
479
  priority: moost.TInterceptorPriority.AFTER_ALL
466
480
  });
467
481
  /**
@@ -490,28 +504,31 @@ const setStatusInterceptor = (code, opts) => ({
490
504
  //#region packages/event-http/src/decorators/limits.decorator.ts
491
505
  /**
492
506
  * Creates an interceptor that sets the maximum allowed inflated body size in bytes.
507
+ * Non-HTTP events are left alone.
493
508
  *
494
509
  * @param n - Maximum body size in bytes after decompression.
495
510
  * @returns Interceptor def to enforce the limit.
496
- */ const globalBodySizeLimit = (n) => (0, moost.defineBeforeInterceptor)(() => {
497
- (0, _wooksjs_event_http.useRequest)().setMaxInflated(n);
498
- }, moost.TInterceptorPriority.BEFORE_ALL);
511
+ */ const globalBodySizeLimit = (n) => (0, moost.defineBeforeInterceptor)(forHttpEvents((ctx) => {
512
+ (0, _wooksjs_event_http.useRequest)(ctx).setMaxInflated(n);
513
+ }), moost.TInterceptorPriority.BEFORE_ALL);
499
514
  /**
500
515
  * Creates an interceptor that sets the maximum allowed compressed body size in bytes.
516
+ * Non-HTTP events are left alone.
501
517
  *
502
518
  * @param n - Maximum body size in bytes before decompression.
503
519
  * @returns Interceptor def to enforce the limit.
504
- */ const globalCompressedBodySizeLimit = (n) => (0, moost.defineBeforeInterceptor)(() => {
505
- (0, _wooksjs_event_http.useRequest)().setMaxCompressed(n);
506
- }, moost.TInterceptorPriority.BEFORE_ALL);
520
+ */ const globalCompressedBodySizeLimit = (n) => (0, moost.defineBeforeInterceptor)(forHttpEvents((ctx) => {
521
+ (0, _wooksjs_event_http.useRequest)(ctx).setMaxCompressed(n);
522
+ }), moost.TInterceptorPriority.BEFORE_ALL);
507
523
  /**
508
524
  * Creates an interceptor that sets the timeout for reading the request body.
525
+ * Non-HTTP events are left alone.
509
526
  *
510
527
  * @param n - Timeout in milliseconds.
511
528
  * @returns Interceptor def to enforce the timeout.
512
- */ const globalBodyReadTimeoutMs = (n) => (0, moost.defineBeforeInterceptor)(() => {
513
- (0, _wooksjs_event_http.useRequest)().setReadTimeoutMs(n);
514
- }, moost.TInterceptorPriority.BEFORE_ALL);
529
+ */ const globalBodyReadTimeoutMs = (n) => (0, moost.defineBeforeInterceptor)(forHttpEvents((ctx) => {
530
+ (0, _wooksjs_event_http.useRequest)(ctx).setReadTimeoutMs(n);
531
+ }), moost.TInterceptorPriority.BEFORE_ALL);
515
532
  /**
516
533
  * Decorator to limit the maximum inflated body size for the request. Default: 10 MB
517
534
  *
@@ -530,12 +547,14 @@ const setStatusInterceptor = (code, opts) => ({
530
547
 
531
548
  //#endregion
532
549
  //#region packages/event-http/src/decorators/compress.decorator.ts
550
+ /** Compression settings of the response before the first `@Compress` of the event ran. */ const compressionBase = (0, _wooksjs_event_core.key)("moost.http.compressionBase");
533
551
  /**
534
552
  * Overrides response compression for a handler or a whole controller.
535
553
  *
536
554
  * Calls `useResponse().setCompression(value)` before guards, argument resolution and the
537
555
  * handler run, so the override also applies to error responses (401, 400, …).
538
- * A method-level `@Compress` wins over a class-level one.
556
+ * A method-level `@Compress` replaces a class-level one: it starts from the app settings
557
+ * rather than layering over the class value. Non-HTTP events of a controller are left alone.
539
558
  *
540
559
  * - `true` (default) — compress with the app settings (`new MoostHttp({ compression })`),
541
560
  * or the defaults when the app has compression off.
@@ -564,9 +583,12 @@ const setStatusInterceptor = (code, opts) => ({
564
583
  * ```
565
584
  *
566
585
  * @param value compression override, defaults to `true`
567
- */ const Compress = (value = true) => (0, moost.Intercept)((0, moost.defineBeforeInterceptor)(() => {
568
- (0, _wooksjs_event_http.useResponse)().setCompression(value);
569
- }, moost.TInterceptorPriority.BEFORE_ALL));
586
+ */ const Compress = (value = true) => (0, moost.Intercept)((0, moost.defineBeforeInterceptor)(forHttpEvents((ctx) => {
587
+ const response = (0, _wooksjs_event_http.useResponse)(ctx);
588
+ if (ctx.hasOwn(compressionBase)) response.setCompression(ctx.getOwn(compressionBase));
589
+ else ctx.setOwn(compressionBase, response.compression);
590
+ response.setCompression(value);
591
+ }), moost.TInterceptorPriority.BEFORE_ALL));
570
592
 
571
593
  //#endregion
572
594
  //#region packages/event-http/src/event-http.ts
@@ -620,7 +642,13 @@ const LOGGER_TITLE = "moost-http";
620
642
  getHttpApp() {
621
643
  return this.httpApp;
622
644
  }
623
- getServerCb(onNoMatch) {
645
+ /**
646
+ * Returns a Node.js request listener for a custom server, or a middleware for a host app.
647
+ *
648
+ * With `onNoMatch` (middleware mode) a request no route matches is handed to `onNoMatch`
649
+ * before any Moost event starts — it belongs to the host app, so it gets no event context,
650
+ * DI scope, interceptors or tracing.
651
+ */ getServerCb(onNoMatch) {
624
652
  return this.httpApp.getServerCb(onNoMatch);
625
653
  }
626
654
  /**
@@ -794,7 +822,7 @@ const LOGGER_TITLE = "moost-http";
794
822
  image: "https://moost.org/moost-full-logo.svg",
795
823
  link: "https://moost.org/",
796
824
  poweredBy: "moostjs",
797
- version: "0.6.49"
825
+ version: "0.6.51"
798
826
  });
799
827
  if (httpApp && httpApp instanceof _wooksjs_event_http.WooksHttp) this.httpApp = httpApp;
800
828
  else if (httpApp) this.httpApp = (0, _wooksjs_event_http.createHttpApp)({
package/dist/index.d.ts CHANGED
@@ -328,6 +328,7 @@ declare function SetStatus(...args: Parameters<typeof setStatusInterceptor>): Cl
328
328
 
329
329
  /**
330
330
  * Creates an interceptor that sets the maximum allowed inflated body size in bytes.
331
+ * Non-HTTP events are left alone.
331
332
  *
332
333
  * @param n - Maximum body size in bytes after decompression.
333
334
  * @returns Interceptor def to enforce the limit.
@@ -335,6 +336,7 @@ declare function SetStatus(...args: Parameters<typeof setStatusInterceptor>): Cl
335
336
  declare const globalBodySizeLimit: (n: number) => moost.TInterceptorDef;
336
337
  /**
337
338
  * Creates an interceptor that sets the maximum allowed compressed body size in bytes.
339
+ * Non-HTTP events are left alone.
338
340
  *
339
341
  * @param n - Maximum body size in bytes before decompression.
340
342
  * @returns Interceptor def to enforce the limit.
@@ -342,6 +344,7 @@ declare const globalBodySizeLimit: (n: number) => moost.TInterceptorDef;
342
344
  declare const globalCompressedBodySizeLimit: (n: number) => moost.TInterceptorDef;
343
345
  /**
344
346
  * Creates an interceptor that sets the timeout for reading the request body.
347
+ * Non-HTTP events are left alone.
345
348
  *
346
349
  * @param n - Timeout in milliseconds.
347
350
  * @returns Interceptor def to enforce the timeout.
@@ -371,7 +374,8 @@ declare const BodyReadTimeoutMs: (n: number) => ClassDecorator & MethodDecorator
371
374
  *
372
375
  * Calls `useResponse().setCompression(value)` before guards, argument resolution and the
373
376
  * handler run, so the override also applies to error responses (401, 400, …).
374
- * A method-level `@Compress` wins over a class-level one.
377
+ * A method-level `@Compress` replaces a class-level one: it starts from the app settings
378
+ * rather than layering over the class value. Non-HTTP events of a controller are left alone.
375
379
  *
376
380
  * - `true` (default) — compress with the app settings (`new MoostHttp({ compression })`),
377
381
  * or the defaults when the app has compression off.
@@ -464,6 +468,13 @@ declare class MoostHttp implements TMoostAdapter<THttpHandlerMeta> {
464
468
  protected httpApp: WooksHttp;
465
469
  constructor(httpApp?: WooksHttp | TWooksHttpOptions);
466
470
  getHttpApp(): WooksHttp;
471
+ /**
472
+ * Returns a Node.js request listener for a custom server, or a middleware for a host app.
473
+ *
474
+ * With `onNoMatch` (middleware mode) a request no route matches is handed to `onNoMatch`
475
+ * before any Moost event starts — it belongs to the host app, so it gets no event context,
476
+ * DI scope, interceptors or tracing.
477
+ */
467
478
  getServerCb(onNoMatch?: (req: IncomingMessage, res: ServerResponse) => void): (req: IncomingMessage, res: ServerResponse) => void;
468
479
  /**
469
480
  * Programmatic route invocation using the Web Standard fetch API.
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { current } from "@wooksjs/event-core";
1
+ import { current, eventTypeKey, key } from "@wooksjs/event-core";
2
2
  import { HttpError, HttpError as HttpError$1, WooksHttp, WooksHttpResponse, createHttpApp, httpKind, httpKind as httpKind$1, isCompressibleType, prerenderJson, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse, useUrlParams } from "@wooksjs/event-http";
3
3
  import { Before, Intercept, Interceptor, MOOST_ADAPTER_BRAND, Overtake, Resolve, TInterceptorPriority, current as current$1, defineBeforeInterceptor, defineMoostEventHandler, forkEventContext, getMoostMate, globalKey, run } from "moost";
4
4
  import { seedBody, useBody } from "@wooksjs/http-body";
@@ -375,13 +375,27 @@ function createRef(getter, setter) {
375
375
  return Resolve(() => useBody().rawBody(), "body");
376
376
  }
377
377
 
378
+ //#endregion
379
+ //#region packages/event-http/src/decorators/http-only.ts
380
+ /**
381
+ * Wraps the callback of an HTTP-only interceptor so it runs for HTTP events only (an in-process
382
+ * `MoostHttp.invoke()` included). Any other event of a mixed controller — a CLI command, a
383
+ * workflow step, a WebSocket message — skips it, also when it runs as a child of an HTTP request:
384
+ * the parent's request and response are not its own.
385
+ */ function forHttpEvents(fn) {
386
+ return () => {
387
+ const ctx = current();
388
+ if (ctx.has(eventTypeKey) && ctx.get(eventTypeKey) === httpKind$1.name) fn(ctx);
389
+ };
390
+ }
391
+
378
392
  //#endregion
379
393
  //#region packages/event-http/src/decorators/set.decorator.ts
380
394
  const setHeaderInterceptor = (name, value, opts) => {
381
- const cb = () => {
382
- const response = useResponse();
395
+ const cb = forHttpEvents((ctx) => {
396
+ const response = useResponse(ctx);
383
397
  if ((!response.getHeader(name) || opts?.force) && (!opts?.status || opts.status === response.status)) response.setHeader(name, value);
384
- };
398
+ });
385
399
  const def = { priority: TInterceptorPriority.AFTER_ALL };
386
400
  if (opts?.when !== "error") def.after = cb;
387
401
  if (opts?.when === "always" || opts?.when === "error") def.error = cb;
@@ -427,10 +441,10 @@ const setHeaderInterceptor = (name, value, opts) => {
427
441
  return Intercept(setHeaderInterceptor(...args));
428
442
  }
429
443
  const setCookieInterceptor = (name, value, attrs) => ({
430
- after() {
431
- const response = useResponse();
444
+ after: forHttpEvents((ctx) => {
445
+ const response = useResponse(ctx);
432
446
  if (!response.getCookie(name)) response.setCookie(name, value, attrs);
433
- },
447
+ }),
434
448
  priority: TInterceptorPriority.AFTER_ALL
435
449
  });
436
450
  /**
@@ -457,10 +471,10 @@ const setCookieInterceptor = (name, value, attrs) => ({
457
471
  return Intercept(setCookieInterceptor(...args));
458
472
  }
459
473
  const setStatusInterceptor = (code, opts) => ({
460
- after() {
461
- const response = useResponse();
474
+ after: forHttpEvents((ctx) => {
475
+ const response = useResponse(ctx);
462
476
  if (!response.status || opts?.force) response.status = code;
463
- },
477
+ }),
464
478
  priority: TInterceptorPriority.AFTER_ALL
465
479
  });
466
480
  /**
@@ -489,28 +503,31 @@ const setStatusInterceptor = (code, opts) => ({
489
503
  //#region packages/event-http/src/decorators/limits.decorator.ts
490
504
  /**
491
505
  * Creates an interceptor that sets the maximum allowed inflated body size in bytes.
506
+ * Non-HTTP events are left alone.
492
507
  *
493
508
  * @param n - Maximum body size in bytes after decompression.
494
509
  * @returns Interceptor def to enforce the limit.
495
- */ const globalBodySizeLimit = (n) => defineBeforeInterceptor(() => {
496
- useRequest().setMaxInflated(n);
497
- }, TInterceptorPriority.BEFORE_ALL);
510
+ */ const globalBodySizeLimit = (n) => defineBeforeInterceptor(forHttpEvents((ctx) => {
511
+ useRequest(ctx).setMaxInflated(n);
512
+ }), TInterceptorPriority.BEFORE_ALL);
498
513
  /**
499
514
  * Creates an interceptor that sets the maximum allowed compressed body size in bytes.
515
+ * Non-HTTP events are left alone.
500
516
  *
501
517
  * @param n - Maximum body size in bytes before decompression.
502
518
  * @returns Interceptor def to enforce the limit.
503
- */ const globalCompressedBodySizeLimit = (n) => defineBeforeInterceptor(() => {
504
- useRequest().setMaxCompressed(n);
505
- }, TInterceptorPriority.BEFORE_ALL);
519
+ */ const globalCompressedBodySizeLimit = (n) => defineBeforeInterceptor(forHttpEvents((ctx) => {
520
+ useRequest(ctx).setMaxCompressed(n);
521
+ }), TInterceptorPriority.BEFORE_ALL);
506
522
  /**
507
523
  * Creates an interceptor that sets the timeout for reading the request body.
524
+ * Non-HTTP events are left alone.
508
525
  *
509
526
  * @param n - Timeout in milliseconds.
510
527
  * @returns Interceptor def to enforce the timeout.
511
- */ const globalBodyReadTimeoutMs = (n) => defineBeforeInterceptor(() => {
512
- useRequest().setReadTimeoutMs(n);
513
- }, TInterceptorPriority.BEFORE_ALL);
528
+ */ const globalBodyReadTimeoutMs = (n) => defineBeforeInterceptor(forHttpEvents((ctx) => {
529
+ useRequest(ctx).setReadTimeoutMs(n);
530
+ }), TInterceptorPriority.BEFORE_ALL);
514
531
  /**
515
532
  * Decorator to limit the maximum inflated body size for the request. Default: 10 MB
516
533
  *
@@ -529,12 +546,14 @@ const setStatusInterceptor = (code, opts) => ({
529
546
 
530
547
  //#endregion
531
548
  //#region packages/event-http/src/decorators/compress.decorator.ts
549
+ /** Compression settings of the response before the first `@Compress` of the event ran. */ const compressionBase = key("moost.http.compressionBase");
532
550
  /**
533
551
  * Overrides response compression for a handler or a whole controller.
534
552
  *
535
553
  * Calls `useResponse().setCompression(value)` before guards, argument resolution and the
536
554
  * handler run, so the override also applies to error responses (401, 400, …).
537
- * A method-level `@Compress` wins over a class-level one.
555
+ * A method-level `@Compress` replaces a class-level one: it starts from the app settings
556
+ * rather than layering over the class value. Non-HTTP events of a controller are left alone.
538
557
  *
539
558
  * - `true` (default) — compress with the app settings (`new MoostHttp({ compression })`),
540
559
  * or the defaults when the app has compression off.
@@ -563,9 +582,12 @@ const setStatusInterceptor = (code, opts) => ({
563
582
  * ```
564
583
  *
565
584
  * @param value compression override, defaults to `true`
566
- */ const Compress = (value = true) => Intercept(defineBeforeInterceptor(() => {
567
- useResponse().setCompression(value);
568
- }, TInterceptorPriority.BEFORE_ALL));
585
+ */ const Compress = (value = true) => Intercept(defineBeforeInterceptor(forHttpEvents((ctx) => {
586
+ const response = useResponse(ctx);
587
+ if (ctx.hasOwn(compressionBase)) response.setCompression(ctx.getOwn(compressionBase));
588
+ else ctx.setOwn(compressionBase, response.compression);
589
+ response.setCompression(value);
590
+ }), TInterceptorPriority.BEFORE_ALL));
569
591
 
570
592
  //#endregion
571
593
  //#region packages/event-http/src/event-http.ts
@@ -619,7 +641,13 @@ const LOGGER_TITLE = "moost-http";
619
641
  getHttpApp() {
620
642
  return this.httpApp;
621
643
  }
622
- getServerCb(onNoMatch) {
644
+ /**
645
+ * Returns a Node.js request listener for a custom server, or a middleware for a host app.
646
+ *
647
+ * With `onNoMatch` (middleware mode) a request no route matches is handed to `onNoMatch`
648
+ * before any Moost event starts — it belongs to the host app, so it gets no event context,
649
+ * DI scope, interceptors or tracing.
650
+ */ getServerCb(onNoMatch) {
623
651
  return this.httpApp.getServerCb(onNoMatch);
624
652
  }
625
653
  /**
@@ -793,7 +821,7 @@ const LOGGER_TITLE = "moost-http";
793
821
  image: "https://moost.org/moost-full-logo.svg",
794
822
  link: "https://moost.org/",
795
823
  poweredBy: "moostjs",
796
- version: "0.6.49"
824
+ version: "0.6.51"
797
825
  });
798
826
  if (httpApp && httpApp instanceof WooksHttp) this.httpApp = httpApp;
799
827
  else if (httpApp) this.httpApp = createHttpApp({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moostjs/event-http",
3
- "version": "0.6.49",
3
+ "version": "0.6.51",
4
4
  "description": "@moostjs/event-http",
5
5
  "keywords": [
6
6
  "composables",
@@ -38,16 +38,17 @@
38
38
  }
39
39
  },
40
40
  "dependencies": {
41
- "@wooksjs/event-http": "^0.7.28",
42
- "@wooksjs/http-body": "^0.7.28"
41
+ "@wooksjs/event-http": "^0.7.29",
42
+ "@wooksjs/http-body": "^0.7.29"
43
43
  },
44
44
  "devDependencies": {
45
- "vitest": "3.2.7"
45
+ "vitest": "3.2.7",
46
+ "@moostjs/event-wf": "^0.6.51"
46
47
  },
47
48
  "peerDependencies": {
48
49
  "@prostojs/infact": "^0.6.1",
49
- "@wooksjs/event-core": "^0.7.28",
50
- "moost": "^0.6.49"
50
+ "@wooksjs/event-core": "^0.7.29",
51
+ "moost": "^0.6.51"
51
52
  },
52
53
  "scripts": {
53
54
  "pub": "pnpm publish --access public",