@venizia/ignis-worker 0.2.0-10

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.
Files changed (105) hide show
  1. package/dist/cjs/applications/index.d.ts +2 -0
  2. package/dist/cjs/applications/index.d.ts.map +1 -0
  3. package/dist/cjs/applications/index.js +18 -0
  4. package/dist/cjs/applications/index.js.map +1 -0
  5. package/dist/cjs/applications/worker.d.ts +67 -0
  6. package/dist/cjs/applications/worker.d.ts.map +1 -0
  7. package/dist/cjs/applications/worker.js +175 -0
  8. package/dist/cjs/applications/worker.js.map +1 -0
  9. package/dist/cjs/envelope/encode.d.ts +61 -0
  10. package/dist/cjs/envelope/encode.d.ts.map +1 -0
  11. package/dist/cjs/envelope/encode.js +116 -0
  12. package/dist/cjs/envelope/encode.js.map +1 -0
  13. package/dist/cjs/envelope/index.d.ts +3 -0
  14. package/dist/cjs/envelope/index.d.ts.map +1 -0
  15. package/dist/cjs/envelope/index.js +19 -0
  16. package/dist/cjs/envelope/index.js.map +1 -0
  17. package/dist/cjs/envelope/types.d.ts +37 -0
  18. package/dist/cjs/envelope/types.d.ts.map +1 -0
  19. package/dist/cjs/envelope/types.js +3 -0
  20. package/dist/cjs/envelope/types.js.map +1 -0
  21. package/dist/cjs/index.d.ts +5 -0
  22. package/dist/cjs/index.d.ts.map +1 -0
  23. package/dist/cjs/index.js +21 -0
  24. package/dist/cjs/index.js.map +1 -0
  25. package/dist/cjs/transport/common/index.d.ts +2 -0
  26. package/dist/cjs/transport/common/index.d.ts.map +1 -0
  27. package/dist/cjs/transport/common/index.js +18 -0
  28. package/dist/cjs/transport/common/index.js.map +1 -0
  29. package/dist/cjs/transport/common/types.d.ts +6 -0
  30. package/dist/cjs/transport/common/types.d.ts.map +1 -0
  31. package/dist/cjs/transport/common/types.js +3 -0
  32. package/dist/cjs/transport/common/types.js.map +1 -0
  33. package/dist/cjs/transport/fetch-bridge.d.ts +35 -0
  34. package/dist/cjs/transport/fetch-bridge.d.ts.map +1 -0
  35. package/dist/cjs/transport/fetch-bridge.js +95 -0
  36. package/dist/cjs/transport/fetch-bridge.js.map +1 -0
  37. package/dist/cjs/transport/in-process.d.ts +23 -0
  38. package/dist/cjs/transport/in-process.d.ts.map +1 -0
  39. package/dist/cjs/transport/in-process.js +47 -0
  40. package/dist/cjs/transport/in-process.js.map +1 -0
  41. package/dist/cjs/transport/index.d.ts +6 -0
  42. package/dist/cjs/transport/index.d.ts.map +1 -0
  43. package/dist/cjs/transport/index.js +22 -0
  44. package/dist/cjs/transport/index.js.map +1 -0
  45. package/dist/cjs/transport/shared.d.ts +102 -0
  46. package/dist/cjs/transport/shared.d.ts.map +1 -0
  47. package/dist/cjs/transport/shared.js +396 -0
  48. package/dist/cjs/transport/shared.js.map +1 -0
  49. package/dist/cjs/transport/worker.d.ts +62 -0
  50. package/dist/cjs/transport/worker.d.ts.map +1 -0
  51. package/dist/cjs/transport/worker.js +223 -0
  52. package/dist/cjs/transport/worker.js.map +1 -0
  53. package/dist/esm/applications/index.d.ts +2 -0
  54. package/dist/esm/applications/index.d.ts.map +1 -0
  55. package/dist/esm/applications/index.js +2 -0
  56. package/dist/esm/applications/index.js.map +1 -0
  57. package/dist/esm/applications/worker.d.ts +67 -0
  58. package/dist/esm/applications/worker.d.ts.map +1 -0
  59. package/dist/esm/applications/worker.js +171 -0
  60. package/dist/esm/applications/worker.js.map +1 -0
  61. package/dist/esm/envelope/encode.d.ts +61 -0
  62. package/dist/esm/envelope/encode.d.ts.map +1 -0
  63. package/dist/esm/envelope/encode.js +112 -0
  64. package/dist/esm/envelope/encode.js.map +1 -0
  65. package/dist/esm/envelope/index.d.ts +3 -0
  66. package/dist/esm/envelope/index.d.ts.map +1 -0
  67. package/dist/esm/envelope/index.js +3 -0
  68. package/dist/esm/envelope/index.js.map +1 -0
  69. package/dist/esm/envelope/types.d.ts +37 -0
  70. package/dist/esm/envelope/types.d.ts.map +1 -0
  71. package/dist/esm/envelope/types.js +2 -0
  72. package/dist/esm/envelope/types.js.map +1 -0
  73. package/dist/esm/index.d.ts +5 -0
  74. package/dist/esm/index.d.ts.map +1 -0
  75. package/dist/esm/index.js +5 -0
  76. package/dist/esm/index.js.map +1 -0
  77. package/dist/esm/transport/common/index.d.ts +2 -0
  78. package/dist/esm/transport/common/index.d.ts.map +1 -0
  79. package/dist/esm/transport/common/index.js +2 -0
  80. package/dist/esm/transport/common/index.js.map +1 -0
  81. package/dist/esm/transport/common/types.d.ts +6 -0
  82. package/dist/esm/transport/common/types.d.ts.map +1 -0
  83. package/dist/esm/transport/common/types.js +2 -0
  84. package/dist/esm/transport/common/types.js.map +1 -0
  85. package/dist/esm/transport/fetch-bridge.d.ts +35 -0
  86. package/dist/esm/transport/fetch-bridge.d.ts.map +1 -0
  87. package/dist/esm/transport/fetch-bridge.js +91 -0
  88. package/dist/esm/transport/fetch-bridge.js.map +1 -0
  89. package/dist/esm/transport/in-process.d.ts +23 -0
  90. package/dist/esm/transport/in-process.d.ts.map +1 -0
  91. package/dist/esm/transport/in-process.js +43 -0
  92. package/dist/esm/transport/in-process.js.map +1 -0
  93. package/dist/esm/transport/index.d.ts +6 -0
  94. package/dist/esm/transport/index.d.ts.map +1 -0
  95. package/dist/esm/transport/index.js +6 -0
  96. package/dist/esm/transport/index.js.map +1 -0
  97. package/dist/esm/transport/shared.d.ts +102 -0
  98. package/dist/esm/transport/shared.d.ts.map +1 -0
  99. package/dist/esm/transport/shared.js +391 -0
  100. package/dist/esm/transport/shared.js.map +1 -0
  101. package/dist/esm/transport/worker.d.ts +62 -0
  102. package/dist/esm/transport/worker.d.ts.map +1 -0
  103. package/dist/esm/transport/worker.js +219 -0
  104. package/dist/esm/transport/worker.js.map +1 -0
  105. package/package.json +94 -0
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/transport/common/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,35 @@
1
+ import type { IBffTransport } from './common/types';
2
+ type TFetch = typeof globalThis.fetch;
3
+ /** The carrier whose `fetch` is replaced - `globalThis` in a page, an object a test controls otherwise. */
4
+ interface IFetchCarrier {
5
+ fetch: TFetch;
6
+ }
7
+ export interface IInstallBffFetchOptions {
8
+ /** Anything implementing the transport contract, so an in-process transport works in a test. */
9
+ transport: IBffTransport;
10
+ /**
11
+ * Path prefixes the BFF owns, matched against the request's PATHNAME. Everything else is passed
12
+ * to the original `fetch` untouched.
13
+ */
14
+ basePath: string | Array<string>;
15
+ /** Defaults to `globalThis`. */
16
+ carrier?: IFetchCarrier;
17
+ }
18
+ /**
19
+ * Routes the page's own `fetch` into a BFF transport for anything under `basePath`, and leaves every
20
+ * other call on the network untouched.
21
+ *
22
+ * This is the seam that lets an existing HTTP client talk to an in-browser IGNIS application without
23
+ * knowing one exists. A data provider, an SDK or a generated client reaches the network through the
24
+ * global `fetch` and usually accepts no custom fetcher, so answering that `fetch` is the only place
25
+ * an in-browser backend becomes a drop-in swap rather than a fork of the client.
26
+ *
27
+ * Install it BEFORE the application that will use it boots: a request issued while the original
28
+ * `fetch` is still in place leaves the page and 404s against whatever is serving it.
29
+ *
30
+ * @returns The uninstall function. It restores the previous `fetch` only if this bridge is still the
31
+ * installed one - if something patched `fetch` afterwards, restoring would silently discard that.
32
+ */
33
+ export declare const installBffFetch: (opts: IInstallBffFetchOptions) => (() => void);
34
+ export {};
35
+ //# sourceMappingURL=fetch-bridge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch-bridge.d.ts","sourceRoot":"","sources":["../../../src/transport/fetch-bridge.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAQpD,KAAK,MAAM,GAAG,OAAO,UAAU,CAAC,KAAK,CAAC;AAEtC,2GAA2G;AAC3G,UAAU,aAAa;IACrB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,uBAAuB;IACtC,gGAAgG;IAChG,SAAS,EAAE,aAAa,CAAC;IACzB;;;OAGG;IACH,QAAQ,EAAE,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC;IACjC,gCAAgC;IAChC,OAAO,CAAC,EAAE,aAAa,CAAC;CACzB;AA+CD;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,eAAe,GAAI,MAAM,uBAAuB,KAAG,CAAC,MAAM,IAAI,CA2C1E,CAAC"}
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.installBffFetch = void 0;
4
+ const core_1 = require("@venizia/ignis-helpers/core");
5
+ /**
6
+ * Marks the patched `fetch` so a second install is refused rather than silently stacking. `Symbol.for`
7
+ * and not a private symbol: two copies of this package in one page must still see each other's mark.
8
+ */
9
+ const BRIDGE_MARKER = Symbol.for('@venizia/ignis-worker/bff-fetch');
10
+ /**
11
+ * Resolves a request's absolute URL WITHOUT constructing a `Request`.
12
+ *
13
+ * This is the whole reason this helper exists rather than one obvious line. `new Request(input)`,
14
+ * where `input` is a `Request` carrying a body, marks that body disturbed - so building one just to
15
+ * read `.url` breaks the very pass-through it is deciding about: the next line hands the same, now
16
+ * unreadable, request to the network.
17
+ *
18
+ * Measured, because the runtimes disagree. Chromium:
19
+ *
20
+ * original.bodyUsed // false
21
+ * new Request(original)
22
+ * original.bodyUsed // true
23
+ * await original.text() // TypeError: body stream already read
24
+ *
25
+ * Bun does NOT disturb it, so the unit tests pass either way and cannot guard this. The browser is
26
+ * where this code runs, so the browser is the behaviour to write for.
27
+ */
28
+ const resolveRequestUrl = (input) => {
29
+ if (typeof input === 'string') {
30
+ return new URL(input, globalThis.location?.href).href;
31
+ }
32
+ if (input instanceof URL) {
33
+ return input.href;
34
+ }
35
+ return input.url;
36
+ };
37
+ const toPrefixList = (basePath) => {
38
+ const prefixes = (Array.isArray(basePath) ? basePath : [basePath]).filter(prefix => prefix.length > 0);
39
+ if (prefixes.length === 0) {
40
+ throw (0, core_1.getError)({
41
+ message: '[installBffFetch] Invalid basePath | At least one non-empty path prefix is required',
42
+ });
43
+ }
44
+ return prefixes;
45
+ };
46
+ /**
47
+ * Routes the page's own `fetch` into a BFF transport for anything under `basePath`, and leaves every
48
+ * other call on the network untouched.
49
+ *
50
+ * This is the seam that lets an existing HTTP client talk to an in-browser IGNIS application without
51
+ * knowing one exists. A data provider, an SDK or a generated client reaches the network through the
52
+ * global `fetch` and usually accepts no custom fetcher, so answering that `fetch` is the only place
53
+ * an in-browser backend becomes a drop-in swap rather than a fork of the client.
54
+ *
55
+ * Install it BEFORE the application that will use it boots: a request issued while the original
56
+ * `fetch` is still in place leaves the page and 404s against whatever is serving it.
57
+ *
58
+ * @returns The uninstall function. It restores the previous `fetch` only if this bridge is still the
59
+ * installed one - if something patched `fetch` afterwards, restoring would silently discard that.
60
+ */
61
+ const installBffFetch = (opts) => {
62
+ const { transport, basePath } = opts;
63
+ const carrier = opts.carrier ?? globalThis;
64
+ const prefixes = toPrefixList(basePath);
65
+ const previousFetch = carrier.fetch;
66
+ if (Object.getOwnPropertyDescriptor(previousFetch, BRIDGE_MARKER)) {
67
+ throw (0, core_1.getError)({
68
+ message: '[installBffFetch] A BFF fetch bridge is already installed | Uninstall it first, or pass every prefix to a single install',
69
+ });
70
+ }
71
+ const networkFetch = previousFetch.bind(carrier);
72
+ const bridged = async (input, init) => {
73
+ const { pathname } = new URL(resolveRequestUrl(input));
74
+ if (!prefixes.some(prefix => pathname.startsWith(prefix))) {
75
+ return networkFetch(input, init);
76
+ }
77
+ // Constructed only now, on the branch that owns the request - so the pass-through above never
78
+ // disturbs a body it is about to hand to the network.
79
+ return transport.fetch({ request: new Request(input, init) });
80
+ };
81
+ // Whatever the runtime hung on `fetch` comes along - Bun puts `preconnect` there, and a
82
+ // replacement that dropped it would break any caller reaching for it. Copying the descriptors is
83
+ // also what makes this satisfy `typeof fetch` rather than merely resembling it.
84
+ Object.defineProperties(bridged, Object.getOwnPropertyDescriptors(previousFetch));
85
+ Object.defineProperty(bridged, BRIDGE_MARKER, { value: true });
86
+ carrier.fetch = bridged;
87
+ return () => {
88
+ if (carrier.fetch !== bridged) {
89
+ return;
90
+ }
91
+ carrier.fetch = previousFetch;
92
+ };
93
+ };
94
+ exports.installBffFetch = installBffFetch;
95
+ //# sourceMappingURL=fetch-bridge.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch-bridge.js","sourceRoot":"","sources":["../../../src/transport/fetch-bridge.ts"],"names":[],"mappings":";;;AAAA,sDAAuD;AAGvD;;;GAGG;AACH,MAAM,aAAa,GAAG,MAAM,CAAC,GAAG,CAAC,iCAAiC,CAAC,CAAC;AAqBpE;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,iBAAiB,GAAG,CAAC,KAAwB,EAAU,EAAE;IAC7D,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,IAAI,GAAG,CAAC,KAAK,EAAE,UAAU,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC;IACxD,CAAC;IAED,IAAI,KAAK,YAAY,GAAG,EAAE,CAAC;QACzB,OAAO,KAAK,CAAC,IAAI,CAAC;IACpB,CAAC;IAED,OAAO,KAAK,CAAC,GAAG,CAAC;AACnB,CAAC,CAAC;AAEF,MAAM,YAAY,GAAG,CAAC,QAAgC,EAAiB,EAAE;IACvE,MAAM,QAAQ,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CACvE,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAC5B,CAAC;IAEF,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC1B,MAAM,IAAA,eAAQ,EAAC;YACb,OAAO,EAC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yBAAyB,CAAC,aAAa,CAAC,CAAC,CAAC;IAClF,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,aAAa,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAE/D,OAAO,CAAC,KAAK,GAAG,OAAiB,CAAC;IAElC,OAAO,GAAG,EAAE;QACV,IAAI,OAAO,CAAC,KAAK,KAAK,OAAO,EAAE,CAAC;YAC9B,OAAO;QACT,CAAC;QAED,OAAO,CAAC,KAAK,GAAG,aAAa,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC,CAAC;AA3CW,QAAA,eAAe,mBA2C1B"}
@@ -0,0 +1,23 @@
1
+ import { BaseHelper } from '@venizia/ignis-helpers/core';
2
+ import type { IBffTransport } from './common/types';
3
+ /**
4
+ * No worker, no browser - the seam that makes a BFF testable in-process. It still runs the full
5
+ * envelope round trip, and that is the whole point: a bare pass-through lets a route pass here and
6
+ * hang over a real Worker, because only the envelope applies the synthetic origin, buffers a
7
+ * streaming body, and reduces a thrown error to `{ text, code, args }`. A test is worth what its
8
+ * differences from production are worth.
9
+ *
10
+ * What it deliberately does NOT mirror is the timeout: in-process there is no message that can be
11
+ * lost, so a handler either settles or the caller's own test timeout fires.
12
+ */
13
+ export declare class InProcessBffTransport extends BaseHelper implements IBffTransport {
14
+ private readonly handler;
15
+ private readonly requestIdGenerator;
16
+ constructor(opts: {
17
+ handler: (request: Request) => Promise<Response>;
18
+ });
19
+ fetch(opts: {
20
+ request: Request;
21
+ }): Promise<Response>;
22
+ }
23
+ //# sourceMappingURL=in-process.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-process.d.ts","sourceRoot":"","sources":["../../../src/transport/in-process.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAsB,MAAM,6BAA6B,CAAC;AAG7E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAEpD;;;;;;;;;GASG;AACH,qBAAa,qBAAsB,SAAQ,UAAW,YAAW,aAAa;IAC5E,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA0C;IAClE,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAEhC;gBAES,IAAI,EAAE;QAAE,OAAO,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;KAAE;IAMhE,KAAK,CAAC,IAAI,EAAE;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,CAAC,QAAQ,CAAC;CAyB3D"}
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InProcessBffTransport = void 0;
4
+ const core_1 = require("@venizia/ignis-helpers/core");
5
+ const encode_1 = require("../envelope/encode");
6
+ /**
7
+ * No worker, no browser - the seam that makes a BFF testable in-process. It still runs the full
8
+ * envelope round trip, and that is the whole point: a bare pass-through lets a route pass here and
9
+ * hang over a real Worker, because only the envelope applies the synthetic origin, buffers a
10
+ * streaming body, and reduces a thrown error to `{ text, code, args }`. A test is worth what its
11
+ * differences from production are worth.
12
+ *
13
+ * What it deliberately does NOT mirror is the timeout: in-process there is no message that can be
14
+ * lost, so a handler either settles or the caller's own test timeout fires.
15
+ */
16
+ class InProcessBffTransport extends core_1.BaseHelper {
17
+ constructor(opts) {
18
+ super({ scope: InProcessBffTransport.name });
19
+ this.requestIdGenerator = new core_1.RequestIdGenerator({
20
+ scope: InProcessBffTransport.name,
21
+ });
22
+ this.handler = opts.handler;
23
+ }
24
+ async fetch(opts) {
25
+ const id = this.requestIdGenerator.nextId();
26
+ const requestEnvelope = await encode_1.BffEnvelope.encodeRequest({
27
+ request: opts.request,
28
+ id,
29
+ url: encode_1.BffEnvelope.toSyntheticUrl({ url: opts.request.url }),
30
+ });
31
+ let responseEnvelope;
32
+ try {
33
+ const response = await this.handler(encode_1.BffEnvelope.decodeRequest({ envelope: requestEnvelope }));
34
+ responseEnvelope = await encode_1.BffEnvelope.encodeResponse({ response, id });
35
+ }
36
+ catch (error) {
37
+ this.logger.for(this.fetch.name).error('Handler failed | id: %s | error: %s', id, error);
38
+ // Encoded and decoded rather than rethrown: what a UI receives over a real Worker is a
39
+ // rebuilt `ApplicationError` carrying only the normalised shape - never the original object,
40
+ // its stack or its `extra`.
41
+ throw encode_1.BffEnvelope.decodeError({ envelope: encode_1.BffEnvelope.encodeError({ id, error }) });
42
+ }
43
+ return encode_1.BffEnvelope.decodeResponse({ envelope: responseEnvelope });
44
+ }
45
+ }
46
+ exports.InProcessBffTransport = InProcessBffTransport;
47
+ //# sourceMappingURL=in-process.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-process.js","sourceRoot":"","sources":["../../../src/transport/in-process.ts"],"names":[],"mappings":";;;AAAA,sDAA6E;AAC7E,8CAAgD;AAIhD;;;;;;;;;GASG;AACH,MAAa,qBAAsB,SAAQ,iBAAU;IAMnD,YAAY,IAA0D;QACpE,KAAK,CAAC,EAAE,KAAK,EAAE,qBAAqB,CAAC,IAAI,EAAE,CAAC,CAAC;QAL9B,uBAAkB,GAAG,IAAI,yBAAkB,CAAC;YAC3D,KAAK,EAAE,qBAAqB,CAAC,IAAI;SAClC,CAAC,CAAC;QAKD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;IAC9B,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,IAA0B;QACpC,MAAM,EAAE,GAAG,IAAI,CAAC,kBAAkB,CAAC,MAAM,EAAE,CAAC;QAE5C,MAAM,eAAe,GAAG,MAAM,oBAAW,CAAC,aAAa,CAAC;YACtD,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,EAAE;YACF,GAAG,EAAE,oBAAW,CAAC,cAAc,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC;SAC3D,CAAC,CAAC;QAEH,IAAI,gBAAsC,CAAC;QAE3C,IAAI,CAAC;YACH,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,oBAAW,CAAC,aAAa,CAAC,EAAE,QAAQ,EAAE,eAAe,EAAE,CAAC,CAAC,CAAC;YAC9F,gBAAgB,GAAG,MAAM,oBAAW,CAAC,cAAc,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,CAAC;QACxE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,qCAAqC,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;YAEzF,uFAAuF;YACvF,6FAA6F;YAC7F,4BAA4B;YAC5B,MAAM,oBAAW,CAAC,WAAW,CAAC,EAAE,QAAQ,EAAE,oBAAW,CAAC,WAAW,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;QACtF,CAAC;QAED,OAAO,oBAAW,CAAC,cAAc,CAAC,EAAE,QAAQ,EAAE,gBAAgB,EAAE,CAAC,CAAC;IACpE,CAAC;CACF;AArCD,sDAqCC"}
@@ -0,0 +1,6 @@
1
+ export * from './common';
2
+ export * from './fetch-bridge';
3
+ export * from './in-process';
4
+ export * from './shared';
5
+ export * from './worker';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/transport/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC"}
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./common"), exports);
18
+ __exportStar(require("./fetch-bridge"), exports);
19
+ __exportStar(require("./in-process"), exports);
20
+ __exportStar(require("./shared"), exports);
21
+ __exportStar(require("./worker"), exports);
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/transport/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,iDAA+B;AAC/B,+CAA6B;AAC7B,2CAAyB;AACzB,2CAAyB"}
@@ -0,0 +1,102 @@
1
+ import { BaseHelper } from '@venizia/ignis-helpers/core';
2
+ import type { TConstValue } from '@venizia/ignis-helpers/common';
3
+ import type { IBffTransport } from './common/types';
4
+ /** Which part this tab plays. `ELECTING` is the brief window before the lock answers. */
5
+ export declare class BffRoles {
6
+ static readonly ELECTING = "electing";
7
+ static readonly LEADER = "leader";
8
+ static readonly FOLLOWER = "follower";
9
+ }
10
+ export type TBffRole = TConstValue<typeof BffRoles>;
11
+ export interface ISharedBffTransportOptions {
12
+ /** Builds the Worker. Called ONLY by whichever tab wins the election - the others never start one. */
13
+ createWorker: () => Worker;
14
+ /** Distinguishes independent BFFs on one origin. The lock name is derived from it. */
15
+ channelName?: string;
16
+ timeoutMs?: number;
17
+ scope?: string;
18
+ }
19
+ /**
20
+ * One BFF for every tab of an origin, instead of one per tab.
21
+ *
22
+ * The problem it solves is not a framework limitation, it is a storage one. PGlite in
23
+ * `opfs-ahp://` mode holds an exclusive access handle on the database file, and OPFS access handles
24
+ * are exclusive PER ORIGIN. Measured in Chromium with two tabs of the same page: the first tab
25
+ * works, and the second never boots its database -
26
+ * `Failed to execute 'createSyncAccessHandle' on 'FileSystemFileHandle': Access Handles cannot be
27
+ * created if there is another open Access Handle or Writable stream associated with the same file`.
28
+ * The first tab is unaffected, and closing it lets the second recover on reload - so the failure is
29
+ * contained and self-healing, but the second tab is simply dead until then.
30
+ *
31
+ * So exactly one tab may own the database. This transport elects that tab with the Web Locks API,
32
+ * gives it the Worker, and forwards every other tab's request to it over a `BroadcastChannel`,
33
+ * carried in the same envelope the Worker itself speaks. When the leader's tab closes, the lock is
34
+ * released by the browser and a follower is promoted automatically - no heartbeat, no timeout, no
35
+ * stale-leader window to reason about.
36
+ *
37
+ * A host with no `navigator.locks` runs single-tab, exactly as before. That is not a compromise:
38
+ * measured on a plain-http origin, `navigator.locks` and `navigator.storage.getDirectory` are BOTH
39
+ * undefined, because both are secure-context only. Wherever OPFS works the lock exists, and where it
40
+ * does not the database could not have started either.
41
+ */
42
+ export declare class SharedBffTransport extends BaseHelper implements IBffTransport {
43
+ private readonly createWorker;
44
+ private readonly channel;
45
+ private readonly lockName;
46
+ private readonly timeoutMs;
47
+ private readonly requestIdGenerator;
48
+ private readonly pendingRequests;
49
+ private role;
50
+ private leaderTransport?;
51
+ private isClosed;
52
+ /**
53
+ * Held because THIS class created it. `WorkerBffTransport.close()` deliberately leaves a worker
54
+ * running - it did not create the one it was handed - so without this reference the worker would
55
+ * outlive `close()` still holding the exclusive OPFS access handle, while the lock has already
56
+ * gone to another tab that opens the same database.
57
+ */
58
+ private leaderWorker?;
59
+ /** Set when `createWorker()` throws, so callers get that failure instead of parking forever. */
60
+ private leaderStartupError?;
61
+ /** Resolving this releases the Web Lock, which is what promotes a follower. */
62
+ private releaseLeadership?;
63
+ /** Drops this tab out of the lock QUEUE on close, so a closed transport cannot be promoted. */
64
+ private readonly leadershipQueueAbort;
65
+ /** Callers parked in `fetch()` while the election is still running. */
66
+ private roleWaiters;
67
+ constructor(opts: ISharedBffTransportOptions);
68
+ getRole(): TBffRole;
69
+ fetch(opts: {
70
+ request: Request;
71
+ }): Promise<Response>;
72
+ close(): void;
73
+ private elect;
74
+ private becomeLeader;
75
+ private becomeFollower;
76
+ private heldUntilClosed;
77
+ private whenRoleSettled;
78
+ private settleRoleWaiters;
79
+ /**
80
+ * The caller's `AbortSignal` is honoured HERE as well as in `WorkerBffTransport`. Without it the
81
+ * same page code behaves differently depending on which tab won the lock: abort works in the
82
+ * leader and silently does nothing in every follower, so a data provider that aborts on unmount
83
+ * leaves promises pending for the full timeout in every tab but one.
84
+ *
85
+ * Aborting settles the CALLER. It does not cancel the leader's work - there is no cancel message,
86
+ * and a write already dispatched cannot be taken back.
87
+ */
88
+ private fetchViaLeader;
89
+ /** `signal.reason` first, the way `globalThis.fetch` rejects, so a custom abort reason survives. */
90
+ private toAbortError;
91
+ private takePending;
92
+ private rejectAllPending;
93
+ /**
94
+ * A `BroadcastChannel` never delivers a message to the instance that sent it, so a leader does not
95
+ * hear its own answers and a follower does not hear its own requests. Every tab does hear every
96
+ * OTHER tab, which is why a response is matched by id and silently ignored when it belongs to
97
+ * someone else.
98
+ */
99
+ private readonly handleChannelMessage;
100
+ private serveAsLeader;
101
+ }
102
+ //# sourceMappingURL=shared.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../../src/transport/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAgC,MAAM,6BAA6B,CAAC;AACvF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAOjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAMpD,yFAAyF;AACzF,qBAAa,QAAQ;IACnB,MAAM,CAAC,QAAQ,CAAC,QAAQ,cAAc;IACtC,MAAM,CAAC,QAAQ,CAAC,MAAM,YAAY;IAClC,MAAM,CAAC,QAAQ,CAAC,QAAQ,cAAc;CACvC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