@moostjs/event-http 0.7.1 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -618,40 +618,25 @@ function _define_property(obj, key, value) {
618
618
  return obj;
619
619
  }
620
620
  /** Marks the child context of {@link MoostHttp.invoke}. */ const invocationKey = (0, moost.globalKey)("moost-http.invocation");
621
- /**
622
- * Releases of queued (pipelined) responses per connection: one socket 'close' listener per
623
- * connection, however many requests are queued on it.
624
- */ const queuedReleases = /* @__PURE__ */ new WeakMap();
625
- function releaseQueued() {
626
- const releases = queuedReleases.get(this);
627
- queuedReleases.delete(this);
628
- releases?.forEach((release) => release());
629
- }
630
621
  const holdScopeUntilResponseCloses = { init: ({ unscope }) => {
631
622
  const ctx = (0, moost.current)();
632
- const res = ctx.hasOwn(invocationKey) ? void 0 : (0, _wooksjs_event_http.useResponse)(ctx).getRawRes(true);
633
- const connection = res && !res.closed ? res.socket ?? res.req?.socket : void 0;
634
- if (!res || !(connection instanceof net.Socket)) {
623
+ const response = (0, _wooksjs_event_http.useResponse)(ctx);
624
+ if (ctx.hasOwn(invocationKey) || !(response.getRawRes(true).req?.socket instanceof net.Socket)) {
635
625
  unscope();
636
626
  return;
637
627
  }
638
- if (res.socket) {
639
- res.on("close", unscope);
640
- return;
641
- }
642
- let releases = queuedReleases.get(connection);
643
- if (!releases) {
644
- queuedReleases.set(connection, releases = /* @__PURE__ */ new Set());
645
- connection.once("close", releaseQueued);
646
- }
647
- releases.add(unscope);
648
- res.on("close", () => {
649
- releases.delete(unscope);
650
- unscope();
651
- });
628
+ response.onClose(unscope);
652
629
  } };
653
630
  const LOGGER_TITLE = "moost-http";
654
631
  /**
632
+ * The default locale policy: `Accept-Language` negotiated against the translator's locales
633
+ * (the first without a match or header). Nothing to negotiate — no locale or a single one —
634
+ * adds no `Vary`.
635
+ */ function negotiateTranslatorLocale() {
636
+ const locales = (0, moost.getTranslatorLocales)();
637
+ return locales.length > 1 ? (0, _wooksjs_event_http.useAcceptLanguage)().negotiate(locales, locales[0]) : locales[0];
638
+ }
639
+ /**
655
640
  * ## Moost HTTP Adapter
656
641
  *
657
642
  * Moost Adapter for HTTP events
@@ -858,7 +843,14 @@ const LOGGER_TITLE = "moost-http";
858
843
  opts.register(handler, routerBinding.getPath(params), args);
859
844
  }
860
845
  }
861
- constructor(httpApp) {
846
+ /**
847
+ * @param httpApp - A `WooksHttp` to use as is, or the options of the one `MoostHttp` creates.
848
+ * A created app translates error bodies (`translateErrorBody`, before your
849
+ * `transformErrorBody`) and, without your `resolveLocale`, picks each request's locale from
850
+ * the translator's `locales` by `Accept-Language` (see `setTranslator`). Both are no-ops
851
+ * while no translator is registered. Your `transformErrorBody` runs even if the translation
852
+ * throws (it then gets the untranslated body; logged once).
853
+ */ constructor(httpApp) {
862
854
  _define_property(this, "name", "http");
863
855
  _define_property(this, "httpApp", void 0);
864
856
  _define_property(this, "pathBuilders", {});
@@ -868,14 +860,30 @@ const LOGGER_TITLE = "moost-http";
868
860
  image: "https://moost.org/moost-full-logo.svg",
869
861
  link: "https://moost.org/",
870
862
  poweredBy: "moostjs",
871
- version: "0.7.1"
863
+ version: "0.7.3"
872
864
  });
873
865
  if (httpApp && httpApp instanceof _wooksjs_event_http.WooksHttp) this.httpApp = httpApp;
874
- else if (httpApp) this.httpApp = (0, _wooksjs_event_http.createHttpApp)({
875
- ...httpApp,
876
- onNotFound: this.onNotFound.bind(this)
877
- });
878
- else this.httpApp = (0, _wooksjs_event_http.createHttpApp)({ onNotFound: this.onNotFound.bind(this) });
866
+ else {
867
+ const transform = httpApp?.transformErrorBody;
868
+ let failureLogged = false;
869
+ this.httpApp = (0, _wooksjs_event_http.createHttpApp)({
870
+ ...httpApp,
871
+ resolveLocale: httpApp?.resolveLocale ?? negotiateTranslatorLocale,
872
+ transformErrorBody: (body, error, ctx) => {
873
+ let out = body;
874
+ try {
875
+ out = (0, moost.translateErrorBody)(body, ctx);
876
+ } catch (translationError) {
877
+ if (!failureLogged) {
878
+ failureLogged = true;
879
+ (0, moost.useLogger)(LOGGER_TITLE, ctx).error("Error body translation failed, sending it untranslated (logged once)", translationError);
880
+ }
881
+ }
882
+ return transform ? transform(out, error, ctx) : out;
883
+ },
884
+ onNotFound: this.onNotFound.bind(this)
885
+ });
886
+ }
879
887
  }
880
888
  };
881
889
  /** Names this adapter for DI independently of class identity (see `MOOST_ADAPTER_BRAND`). */ _define_property(MoostHttp, moost.MOOST_ADAPTER_BRAND, "@moostjs/event-http/MoostHttp");
package/dist/index.d.ts CHANGED
@@ -481,6 +481,14 @@ declare class MoostHttp implements TMoostAdapter<THttpHandlerMeta> {
481
481
  static readonly [MOOST_ADAPTER_BRAND] = "@moostjs/event-http/MoostHttp";
482
482
  readonly name = "http";
483
483
  protected httpApp: WooksHttp;
484
+ /**
485
+ * @param httpApp - A `WooksHttp` to use as is, or the options of the one `MoostHttp` creates.
486
+ * A created app translates error bodies (`translateErrorBody`, before your
487
+ * `transformErrorBody`) and, without your `resolveLocale`, picks each request's locale from
488
+ * the translator's `locales` by `Accept-Language` (see `setTranslator`). Both are no-ops
489
+ * while no translator is registered. Your `transformErrorBody` runs even if the translation
490
+ * throws (it then gets the untranslated body; logged once).
491
+ */
484
492
  constructor(httpApp?: WooksHttp | TWooksHttpOptions);
485
493
  getHttpApp(): WooksHttp;
486
494
  /**
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { current, eventTypeKey, key } from "@wooksjs/event-core";
2
- import { DEFAULT_HTTP_STATUS, HttpError, HttpError as HttpError$1, WooksHttp, WooksHttpResponse, createHttpApp, getDefaultHttpStatus, httpKind, httpKind as httpKind$1, isCompressibleType, prerenderJson, useAcceptLanguage, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse, useUrlParams } from "@wooksjs/event-http";
3
- import { Before, Intercept, Interceptor, MOOST_ADAPTER_BRAND, Overtake, Resolve, TInterceptorPriority, current as current$1, defineBeforeInterceptor, defineMoostEventHandler, forkEventContext, getMoostMate, globalKey, run } from "moost";
2
+ import { DEFAULT_HTTP_STATUS, HttpError, HttpError as HttpError$1, WooksHttp, WooksHttpResponse, createHttpApp, getDefaultHttpStatus, httpKind, httpKind as httpKind$1, isCompressibleType, prerenderJson, useAcceptLanguage, useAcceptLanguage as useAcceptLanguage$1, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse, useUrlParams } from "@wooksjs/event-http";
3
+ import { Before, Intercept, Interceptor, MOOST_ADAPTER_BRAND, Overtake, Resolve, TInterceptorPriority, current as current$1, defineBeforeInterceptor, defineMoostEventHandler, forkEventContext, getMoostMate, getTranslatorLocales, globalKey, run, translateErrorBody, useLogger } from "moost";
4
4
  import { seedBody, useBody } from "@wooksjs/http-body";
5
5
  import { createProvideRegistry } from "@prostojs/infact";
6
6
  import { Server } from "http";
@@ -617,40 +617,25 @@ function _define_property(obj, key, value) {
617
617
  return obj;
618
618
  }
619
619
  /** Marks the child context of {@link MoostHttp.invoke}. */ const invocationKey = globalKey("moost-http.invocation");
620
- /**
621
- * Releases of queued (pipelined) responses per connection: one socket 'close' listener per
622
- * connection, however many requests are queued on it.
623
- */ const queuedReleases = /* @__PURE__ */ new WeakMap();
624
- function releaseQueued() {
625
- const releases = queuedReleases.get(this);
626
- queuedReleases.delete(this);
627
- releases?.forEach((release) => release());
628
- }
629
620
  const holdScopeUntilResponseCloses = { init: ({ unscope }) => {
630
621
  const ctx = current$1();
631
- const res = ctx.hasOwn(invocationKey) ? void 0 : useResponse(ctx).getRawRes(true);
632
- const connection = res && !res.closed ? res.socket ?? res.req?.socket : void 0;
633
- if (!res || !(connection instanceof Socket)) {
622
+ const response = useResponse(ctx);
623
+ if (ctx.hasOwn(invocationKey) || !(response.getRawRes(true).req?.socket instanceof Socket)) {
634
624
  unscope();
635
625
  return;
636
626
  }
637
- if (res.socket) {
638
- res.on("close", unscope);
639
- return;
640
- }
641
- let releases = queuedReleases.get(connection);
642
- if (!releases) {
643
- queuedReleases.set(connection, releases = /* @__PURE__ */ new Set());
644
- connection.once("close", releaseQueued);
645
- }
646
- releases.add(unscope);
647
- res.on("close", () => {
648
- releases.delete(unscope);
649
- unscope();
650
- });
627
+ response.onClose(unscope);
651
628
  } };
652
629
  const LOGGER_TITLE = "moost-http";
653
630
  /**
631
+ * The default locale policy: `Accept-Language` negotiated against the translator's locales
632
+ * (the first without a match or header). Nothing to negotiate — no locale or a single one —
633
+ * adds no `Vary`.
634
+ */ function negotiateTranslatorLocale() {
635
+ const locales = getTranslatorLocales();
636
+ return locales.length > 1 ? useAcceptLanguage$1().negotiate(locales, locales[0]) : locales[0];
637
+ }
638
+ /**
654
639
  * ## Moost HTTP Adapter
655
640
  *
656
641
  * Moost Adapter for HTTP events
@@ -857,7 +842,14 @@ const LOGGER_TITLE = "moost-http";
857
842
  opts.register(handler, routerBinding.getPath(params), args);
858
843
  }
859
844
  }
860
- constructor(httpApp) {
845
+ /**
846
+ * @param httpApp - A `WooksHttp` to use as is, or the options of the one `MoostHttp` creates.
847
+ * A created app translates error bodies (`translateErrorBody`, before your
848
+ * `transformErrorBody`) and, without your `resolveLocale`, picks each request's locale from
849
+ * the translator's `locales` by `Accept-Language` (see `setTranslator`). Both are no-ops
850
+ * while no translator is registered. Your `transformErrorBody` runs even if the translation
851
+ * throws (it then gets the untranslated body; logged once).
852
+ */ constructor(httpApp) {
861
853
  _define_property(this, "name", "http");
862
854
  _define_property(this, "httpApp", void 0);
863
855
  _define_property(this, "pathBuilders", {});
@@ -867,14 +859,30 @@ const LOGGER_TITLE = "moost-http";
867
859
  image: "https://moost.org/moost-full-logo.svg",
868
860
  link: "https://moost.org/",
869
861
  poweredBy: "moostjs",
870
- version: "0.7.1"
862
+ version: "0.7.3"
871
863
  });
872
864
  if (httpApp && httpApp instanceof WooksHttp) this.httpApp = httpApp;
873
- else if (httpApp) this.httpApp = createHttpApp({
874
- ...httpApp,
875
- onNotFound: this.onNotFound.bind(this)
876
- });
877
- else this.httpApp = createHttpApp({ onNotFound: this.onNotFound.bind(this) });
865
+ else {
866
+ const transform = httpApp?.transformErrorBody;
867
+ let failureLogged = false;
868
+ this.httpApp = createHttpApp({
869
+ ...httpApp,
870
+ resolveLocale: httpApp?.resolveLocale ?? negotiateTranslatorLocale,
871
+ transformErrorBody: (body, error, ctx) => {
872
+ let out = body;
873
+ try {
874
+ out = translateErrorBody(body, ctx);
875
+ } catch (translationError) {
876
+ if (!failureLogged) {
877
+ failureLogged = true;
878
+ useLogger(LOGGER_TITLE, ctx).error("Error body translation failed, sending it untranslated (logged once)", translationError);
879
+ }
880
+ }
881
+ return transform ? transform(out, error, ctx) : out;
882
+ },
883
+ onNotFound: this.onNotFound.bind(this)
884
+ });
885
+ }
878
886
  }
879
887
  };
880
888
  /** Names this adapter for DI independently of class identity (see `MOOST_ADAPTER_BRAND`). */ _define_property(MoostHttp, MOOST_ADAPTER_BRAND, "@moostjs/event-http/MoostHttp");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moostjs/event-http",
3
- "version": "0.7.1",
3
+ "version": "0.7.3",
4
4
  "description": "@moostjs/event-http",
5
5
  "keywords": [
6
6
  "composables",
@@ -38,17 +38,17 @@
38
38
  }
39
39
  },
40
40
  "dependencies": {
41
- "@wooksjs/event-http": "^0.8.0",
42
- "@wooksjs/http-body": "^0.8.0"
41
+ "@wooksjs/event-http": "^0.8.1",
42
+ "@wooksjs/http-body": "^0.8.1"
43
43
  },
44
44
  "devDependencies": {
45
45
  "vitest": "4.1.12",
46
- "@moostjs/event-wf": "^0.7.1"
46
+ "@moostjs/event-wf": "^0.7.3"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "@prostojs/infact": "^0.6.1",
50
- "@wooksjs/event-core": "^0.8.0",
51
- "moost": "^0.7.1"
50
+ "@wooksjs/event-core": "^0.8.1",
51
+ "moost": "^0.7.3"
52
52
  },
53
53
  "scripts": {
54
54
  "pub": "pnpm publish --access public",