@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,112 @@
1
+ import { fromError, getError, isApplicationError } from '@venizia/ignis-helpers/core';
2
+ import { HTTP } from '@venizia/ignis-helpers/common';
3
+ /**
4
+ * Every request built inside the Worker uses this origin. Never `self.location.href`: a `blob:`
5
+ * worker origin is opaque and `new URL(path, blobUrl)` throws, and Hono's `getPath()` mis-parses
6
+ * `file://`. Hono never resolves the host - it string-scans from `indexOf(':') + 4`.
7
+ */
8
+ export const BFF_SYNTHETIC_ORIGIN = 'http://ignis.internal';
9
+ /** Turns `Request`/`Response`/`Error` into plain structured-cloneable shapes and back - none of the three survives `postMessage` as itself. */
10
+ export class BffEnvelope {
11
+ /**
12
+ * Statuses the Fetch spec forbids a body on - `new Response(body, { status })` throws if `body`
13
+ * is anything but `null` for one of these, even an empty `ArrayBuffer`.
14
+ */
15
+ static { this.NULL_BODY_STATUSES = new Set([204, 205, 304]); }
16
+ /**
17
+ * Moves a URL onto the synthetic origin, keeping everything below it. A caller builds a request
18
+ * against the page's real origin (or a relative path resolved against it); `decodeRequest` on the
19
+ * far side always reconstructs on `BFF_SYNTHETIC_ORIGIN`.
20
+ */
21
+ static toSyntheticUrl(opts) {
22
+ const url = new URL(opts.url);
23
+ return `${BFF_SYNTHETIC_ORIGIN}${url.pathname}${url.search}${url.hash}`;
24
+ }
25
+ /**
26
+ * `url` overrides the request's own, so the origin rewrite costs nothing: rebuilding the `Request`
27
+ * to change its URL would read the body a second time, and a 10 MB upload would hold 20 MB on the
28
+ * UI thread. Reading the body here disturbs the caller's request exactly as `globalThis.fetch`
29
+ * does with the one it is handed.
30
+ */
31
+ static async encodeRequest(opts) {
32
+ const { request, id, url } = opts;
33
+ const hasBody = request.method !== 'GET' && request.method !== 'HEAD';
34
+ return {
35
+ id,
36
+ method: request.method,
37
+ url: url ?? request.url,
38
+ headers: [...request.headers.entries()],
39
+ body: hasBody ? await request.arrayBuffer() : undefined,
40
+ };
41
+ }
42
+ static decodeRequest(opts) {
43
+ const { envelope } = opts;
44
+ return new Request(envelope.url, {
45
+ method: envelope.method,
46
+ headers: envelope.headers,
47
+ body: envelope.body,
48
+ });
49
+ }
50
+ static async encodeResponse(opts) {
51
+ const { response, id } = opts;
52
+ const hasBody = !BffEnvelope.NULL_BODY_STATUSES.has(response.status);
53
+ return {
54
+ id,
55
+ status: response.status,
56
+ statusText: response.statusText,
57
+ headers: [...response.headers.entries()],
58
+ body: hasBody ? await response.arrayBuffer() : undefined,
59
+ };
60
+ }
61
+ static decodeResponse(opts) {
62
+ const { envelope } = opts;
63
+ const body = BffEnvelope.NULL_BODY_STATUSES.has(envelope.status)
64
+ ? null
65
+ : (envelope.body ?? null);
66
+ return new Response(body, {
67
+ status: envelope.status,
68
+ statusText: envelope.statusText,
69
+ headers: envelope.headers,
70
+ });
71
+ }
72
+ /**
73
+ * Builds the shape an error crosses `postMessage` as. An `ApplicationError` cannot cross as itself
74
+ * - a custom `Error` subclass loses its name, its own properties and its prototype through
75
+ * structured clone - so it crosses normalised and `decodeError` rebuilds it on the far side.
76
+ *
77
+ * A foreign error gets `500`, matching what the framework's error middleware gives an UNEXPECTED
78
+ * one on the server. Anything reaching here already escaped the whole HTTP layer.
79
+ */
80
+ static encodeError(opts) {
81
+ const { id, error } = opts;
82
+ const applicationError = isApplicationError(error)
83
+ ? error
84
+ : getError({
85
+ statusCode: HTTP.ResultCodes.RS_5.InternalServerError,
86
+ message: error instanceof Error ? error.message : String(error),
87
+ });
88
+ return {
89
+ id,
90
+ statusCode: applicationError.statusCode,
91
+ error: {
92
+ text: applicationError.normalized.text,
93
+ code: applicationError.normalized.code,
94
+ args: applicationError.normalized.args,
95
+ },
96
+ };
97
+ }
98
+ static decodeError(opts) {
99
+ const { envelope } = opts;
100
+ return fromError({
101
+ error: {
102
+ statusCode: envelope.statusCode,
103
+ normalized: {
104
+ text: envelope.error.text,
105
+ code: envelope.error.code,
106
+ args: envelope.error.args,
107
+ },
108
+ },
109
+ });
110
+ }
111
+ }
112
+ //# sourceMappingURL=encode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encode.js","sourceRoot":"","sources":["../../../src/envelope/encode.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,6BAA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AAC;YAChD,CAAC,CAAC,KAAK;YACP,CAAC,CAAC,QAAQ,CAAC;gBACP,UAAU,EAAE,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,mBAAmB;gBACrD,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;aAChE,CAAC,CAAC;QAEP,OAAO;YACL,EAAE;YACF,UAAU,EAAE,gBAAgB,CAAC,UAAU;YACvC,KAAK,EAAE;gBACL,IAAI,EAAE,gBAAgB,CAAC,UAAU,CAAC,IAAI;gBACtC,IAAI,EAAE,gBAAgB,CAAC,UAAU,CAAC,IAAI;gBACtC,IAAI,EAAE,gBAAgB,CAAC,UAAU,CAAC,IAAI;aACvC;SACF,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,WAAW,CAAC,IAAqC;QACtD,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC;QAE1B,OAAO,SAAS,CAAC;YACf,KAAK,EAAE;gBACL,UAAU,EAAE,QAAQ,CAAC,UAAU;gBAC/B,UAAU,EAAE;oBACV,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC,IAAI;oBACzB,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC,IAAI;oBACzB,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC,IAA2C;iBACjE;aACF;SACF,CAAC,CAAC;IACL,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from './encode.js';
2
+ export * from './types.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/envelope/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from './encode.js';
2
+ export * from './types.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/envelope/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC"}
@@ -0,0 +1,37 @@
1
+ /** Headers are carried as tuples: a `Headers` instance is not structured-cloneable. */
2
+ export type TEnvelopeHeaders = Array<[string, string]>;
3
+ export interface IBffRequestEnvelope {
4
+ id: string;
5
+ method: string;
6
+ url: string;
7
+ headers: TEnvelopeHeaders;
8
+ /** Buffered, never a stream - `ReadableStream` does not transfer in Safari. */
9
+ body?: ArrayBuffer;
10
+ }
11
+ export interface IBffResponseEnvelope {
12
+ id: string;
13
+ status: number;
14
+ statusText?: string;
15
+ headers: TEnvelopeHeaders;
16
+ body?: ArrayBuffer;
17
+ }
18
+ /**
19
+ * A custom `Error` subclass loses its name, its own properties and its prototype through structured
20
+ * clone, so an `ApplicationError` cannot cross as itself. It crosses as its normalised shape and is
21
+ * rebuilt with `fromError` on the far side.
22
+ */
23
+ export interface IBffErrorEnvelope {
24
+ id: string;
25
+ /**
26
+ * Carried explicitly, because `fromError` defaults a missing one to 400. This envelope is only
27
+ * ever produced when something escaped Hono's `onError` - the 500-class failures a UI must not
28
+ * classify as its own bad request.
29
+ */
30
+ statusCode: number;
31
+ error: {
32
+ text: string;
33
+ code?: string;
34
+ args?: unknown;
35
+ };
36
+ }
37
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/envelope/types.ts"],"names":[],"mappings":"AAAA,uFAAuF;AACvF,MAAM,MAAM,gBAAgB,GAAG,KAAK,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAEvD,MAAM,WAAW,mBAAmB;IAClC,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,gBAAgB,CAAC;IAC1B,+EAA+E;IAC/E,IAAI,CAAC,EAAE,WAAW,CAAC;CACpB;AAED,MAAM,WAAW,oBAAoB;IACnC,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,gBAAgB,CAAC;IAC1B,IAAI,CAAC,EAAE,WAAW,CAAC;CACpB;AAED;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;IACX;;;;OAIG;IACH,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;CACxD"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/envelope/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ import 'reflect-metadata';
2
+ export * from './applications/index.js';
3
+ export * from './envelope/index.js';
4
+ export * from './transport/index.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,5 @@
1
+ import 'reflect-metadata';
2
+ export * from './applications/index.js';
3
+ export * from './envelope/index.js';
4
+ export * from './transport/index.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/transport/common/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/transport/common/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC"}
@@ -0,0 +1,6 @@
1
+ export interface IBffTransport {
2
+ fetch(opts: {
3
+ request: Request;
4
+ }): Promise<Response>;
5
+ }
6
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/transport/common/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,KAAK,CAAC,IAAI,EAAE;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;CACtD"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# 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.js';
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,91 @@
1
+ import { getError } from '@venizia/ignis-helpers/core';
2
+ /**
3
+ * Marks the patched `fetch` so a second install is refused rather than silently stacking. `Symbol.for`
4
+ * and not a private symbol: two copies of this package in one page must still see each other's mark.
5
+ */
6
+ const BRIDGE_MARKER = Symbol.for('@venizia/ignis-worker/bff-fetch');
7
+ /**
8
+ * Resolves a request's absolute URL WITHOUT constructing a `Request`.
9
+ *
10
+ * This is the whole reason this helper exists rather than one obvious line. `new Request(input)`,
11
+ * where `input` is a `Request` carrying a body, marks that body disturbed - so building one just to
12
+ * read `.url` breaks the very pass-through it is deciding about: the next line hands the same, now
13
+ * unreadable, request to the network.
14
+ *
15
+ * Measured, because the runtimes disagree. Chromium:
16
+ *
17
+ * original.bodyUsed // false
18
+ * new Request(original)
19
+ * original.bodyUsed // true
20
+ * await original.text() // TypeError: body stream already read
21
+ *
22
+ * Bun does NOT disturb it, so the unit tests pass either way and cannot guard this. The browser is
23
+ * where this code runs, so the browser is the behaviour to write for.
24
+ */
25
+ const resolveRequestUrl = (input) => {
26
+ if (typeof input === 'string') {
27
+ return new URL(input, globalThis.location?.href).href;
28
+ }
29
+ if (input instanceof URL) {
30
+ return input.href;
31
+ }
32
+ return input.url;
33
+ };
34
+ const toPrefixList = (basePath) => {
35
+ const prefixes = (Array.isArray(basePath) ? basePath : [basePath]).filter(prefix => prefix.length > 0);
36
+ if (prefixes.length === 0) {
37
+ throw getError({
38
+ message: '[installBffFetch] Invalid basePath | At least one non-empty path prefix is required',
39
+ });
40
+ }
41
+ return prefixes;
42
+ };
43
+ /**
44
+ * Routes the page's own `fetch` into a BFF transport for anything under `basePath`, and leaves every
45
+ * other call on the network untouched.
46
+ *
47
+ * This is the seam that lets an existing HTTP client talk to an in-browser IGNIS application without
48
+ * knowing one exists. A data provider, an SDK or a generated client reaches the network through the
49
+ * global `fetch` and usually accepts no custom fetcher, so answering that `fetch` is the only place
50
+ * an in-browser backend becomes a drop-in swap rather than a fork of the client.
51
+ *
52
+ * Install it BEFORE the application that will use it boots: a request issued while the original
53
+ * `fetch` is still in place leaves the page and 404s against whatever is serving it.
54
+ *
55
+ * @returns The uninstall function. It restores the previous `fetch` only if this bridge is still the
56
+ * installed one - if something patched `fetch` afterwards, restoring would silently discard that.
57
+ */
58
+ export const installBffFetch = (opts) => {
59
+ const { transport, basePath } = opts;
60
+ const carrier = opts.carrier ?? globalThis;
61
+ const prefixes = toPrefixList(basePath);
62
+ const previousFetch = carrier.fetch;
63
+ if (Object.getOwnPropertyDescriptor(previousFetch, BRIDGE_MARKER)) {
64
+ throw getError({
65
+ message: '[installBffFetch] A BFF fetch bridge is already installed | Uninstall it first, or pass every prefix to a single install',
66
+ });
67
+ }
68
+ const networkFetch = previousFetch.bind(carrier);
69
+ const bridged = async (input, init) => {
70
+ const { pathname } = new URL(resolveRequestUrl(input));
71
+ if (!prefixes.some(prefix => pathname.startsWith(prefix))) {
72
+ return networkFetch(input, init);
73
+ }
74
+ // Constructed only now, on the branch that owns the request - so the pass-through above never
75
+ // disturbs a body it is about to hand to the network.
76
+ return transport.fetch({ request: new Request(input, init) });
77
+ };
78
+ // Whatever the runtime hung on `fetch` comes along - Bun puts `preconnect` there, and a
79
+ // replacement that dropped it would break any caller reaching for it. Copying the descriptors is
80
+ // also what makes this satisfy `typeof fetch` rather than merely resembling it.
81
+ Object.defineProperties(bridged, Object.getOwnPropertyDescriptors(previousFetch));
82
+ Object.defineProperty(bridged, BRIDGE_MARKER, { value: true });
83
+ carrier.fetch = bridged;
84
+ return () => {
85
+ if (carrier.fetch !== bridged) {
86
+ return;
87
+ }
88
+ carrier.fetch = previousFetch;
89
+ };
90
+ };
91
+ //# 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,OAAO,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAC;AAGvD;;;GAGG;AACH,MAAM,aAAa,GAAG,MAAM,CAAC,GAAG,CAAC,iCAAiC,CAAC,CAAC;AAqBpE;;;;;;;;;;;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CAAC;YACb,OAAO,EACL,0HAA0H;SAC7H,CAAC,CAAC;IACL,CAAC;IAED,MAAM,YAAY,GAAG,aAAa,CAAC,IAAI,CAAC,OAAO,CAAW,CAAC;IAE3D,MAAM,OAAO,GAAG,KAAK,EAAE,KAAwB,EAAE,IAAkB,EAAqB,EAAE;QACxF,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,GAAG,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC,CAAC;QAEvD,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,QAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC;YAC1D,OAAO,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QACnC,CAAC;QAED,8FAA8F;QAC9F,sDAAsD;QACtD,OAAO,SAAS,CAAC,KAAK,CAAC,EAAE,OAAO,EAAE,IAAI,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;IAChE,CAAC,CAAC;IAEF,wFAAwF;IACxF,iGAAiG;IACjG,gFAAgF;IAChF,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,MAAM,CAAC,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"}
@@ -0,0 +1,23 @@
1
+ import { BaseHelper } from '@venizia/ignis-helpers/core';
2
+ import type { IBffTransport } from './common/types.js';
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,43 @@
1
+ import { BaseHelper, RequestIdGenerator } from '@venizia/ignis-helpers/core';
2
+ import { BffEnvelope } from '../envelope/encode.js';
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 class InProcessBffTransport extends BaseHelper {
14
+ constructor(opts) {
15
+ super({ scope: InProcessBffTransport.name });
16
+ this.requestIdGenerator = new RequestIdGenerator({
17
+ scope: InProcessBffTransport.name,
18
+ });
19
+ this.handler = opts.handler;
20
+ }
21
+ async fetch(opts) {
22
+ const id = this.requestIdGenerator.nextId();
23
+ const requestEnvelope = await BffEnvelope.encodeRequest({
24
+ request: opts.request,
25
+ id,
26
+ url: BffEnvelope.toSyntheticUrl({ url: opts.request.url }),
27
+ });
28
+ let responseEnvelope;
29
+ try {
30
+ const response = await this.handler(BffEnvelope.decodeRequest({ envelope: requestEnvelope }));
31
+ responseEnvelope = await BffEnvelope.encodeResponse({ response, id });
32
+ }
33
+ catch (error) {
34
+ this.logger.for(this.fetch.name).error('Handler failed | id: %s | error: %s', id, error);
35
+ // Encoded and decoded rather than rethrown: what a UI receives over a real Worker is a
36
+ // rebuilt `ApplicationError` carrying only the normalised shape - never the original object,
37
+ // its stack or its `extra`.
38
+ throw BffEnvelope.decodeError({ envelope: BffEnvelope.encodeError({ id, error }) });
39
+ }
40
+ return BffEnvelope.decodeResponse({ envelope: responseEnvelope });
41
+ }
42
+ }
43
+ //# 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,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAC;AAC7E,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIhD;;;;;;;;;GASG;AACH,MAAM,OAAO,qBAAsB,SAAQ,UAAU;IAMnD,YAAY,I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AW,CAAC,cAAc,CAAC,EAAE,QAAQ,EAAE,gBAAgB,EAAE,CAAC,CAAC;IACpE,CAAC;CACF"}
@@ -0,0 +1,6 @@
1
+ export * from './common/index.js';
2
+ export * from './fetch-bridge.js';
3
+ export * from './in-process.js';
4
+ export * from './shared.js';
5
+ export * from './worker.js';
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,6 @@
1
+ export * from './common/index.js';
2
+ export * from './fetch-bridge.js';
3
+ export * from './in-process.js';
4
+ export * from './shared.js';
5
+ export * from './worker.js';
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","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,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.js';
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,gBAA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