@venizia/ignis-worker 0.2.0-2 → 0.2.0-4
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/cjs/transport/fetch-bridge.d.ts +35 -0
- package/dist/cjs/transport/fetch-bridge.d.ts.map +1 -0
- package/dist/cjs/transport/fetch-bridge.js +95 -0
- package/dist/cjs/transport/fetch-bridge.js.map +1 -0
- package/dist/cjs/transport/index.d.ts +2 -0
- package/dist/cjs/transport/index.d.ts.map +1 -1
- package/dist/cjs/transport/index.js +2 -0
- package/dist/cjs/transport/index.js.map +1 -1
- package/dist/cjs/transport/shared.d.ts +102 -0
- package/dist/cjs/transport/shared.d.ts.map +1 -0
- package/dist/cjs/transport/shared.js +396 -0
- package/dist/cjs/transport/shared.js.map +1 -0
- package/dist/cjs/transport/worker.d.ts +16 -2
- package/dist/cjs/transport/worker.d.ts.map +1 -1
- package/dist/cjs/transport/worker.js +65 -7
- package/dist/cjs/transport/worker.js.map +1 -1
- package/dist/esm/transport/fetch-bridge.d.ts +35 -0
- package/dist/esm/transport/fetch-bridge.d.ts.map +1 -0
- package/dist/esm/transport/fetch-bridge.js +91 -0
- package/dist/esm/transport/fetch-bridge.js.map +1 -0
- package/dist/esm/transport/index.d.ts +2 -0
- package/dist/esm/transport/index.d.ts.map +1 -1
- package/dist/esm/transport/index.js +2 -0
- package/dist/esm/transport/index.js.map +1 -1
- package/dist/esm/transport/shared.d.ts +102 -0
- package/dist/esm/transport/shared.d.ts.map +1 -0
- package/dist/esm/transport/shared.js +391 -0
- package/dist/esm/transport/shared.js.map +1 -0
- package/dist/esm/transport/worker.d.ts +16 -2
- package/dist/esm/transport/worker.d.ts.map +1 -1
- package/dist/esm/transport/worker.js +65 -7
- package/dist/esm/transport/worker.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SharedBffTransport = exports.BffRoles = void 0;
|
|
4
|
+
const core_1 = require("@venizia/ignis-helpers/core");
|
|
5
|
+
const encode_1 = require("../envelope/encode");
|
|
6
|
+
const worker_1 = require("./worker");
|
|
7
|
+
const DEFAULT_CHANNEL_NAME = 'ignis.bff';
|
|
8
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
9
|
+
/** Which part this tab plays. `ELECTING` is the brief window before the lock answers. */
|
|
10
|
+
class BffRoles {
|
|
11
|
+
static { this.ELECTING = 'electing'; }
|
|
12
|
+
static { this.LEADER = 'leader'; }
|
|
13
|
+
static { this.FOLLOWER = 'follower'; }
|
|
14
|
+
}
|
|
15
|
+
exports.BffRoles = BffRoles;
|
|
16
|
+
class ChannelMessageKinds {
|
|
17
|
+
static { this.REQUEST = 'ignis.bff.request'; }
|
|
18
|
+
static { this.RESPONSE = 'ignis.bff.response'; }
|
|
19
|
+
static { this.ERROR = 'ignis.bff.error'; }
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* One BFF for every tab of an origin, instead of one per tab.
|
|
23
|
+
*
|
|
24
|
+
* The problem it solves is not a framework limitation, it is a storage one. PGlite in
|
|
25
|
+
* `opfs-ahp://` mode holds an exclusive access handle on the database file, and OPFS access handles
|
|
26
|
+
* are exclusive PER ORIGIN. Measured in Chromium with two tabs of the same page: the first tab
|
|
27
|
+
* works, and the second never boots its database -
|
|
28
|
+
* `Failed to execute 'createSyncAccessHandle' on 'FileSystemFileHandle': Access Handles cannot be
|
|
29
|
+
* created if there is another open Access Handle or Writable stream associated with the same file`.
|
|
30
|
+
* The first tab is unaffected, and closing it lets the second recover on reload - so the failure is
|
|
31
|
+
* contained and self-healing, but the second tab is simply dead until then.
|
|
32
|
+
*
|
|
33
|
+
* So exactly one tab may own the database. This transport elects that tab with the Web Locks API,
|
|
34
|
+
* gives it the Worker, and forwards every other tab's request to it over a `BroadcastChannel`,
|
|
35
|
+
* carried in the same envelope the Worker itself speaks. When the leader's tab closes, the lock is
|
|
36
|
+
* released by the browser and a follower is promoted automatically - no heartbeat, no timeout, no
|
|
37
|
+
* stale-leader window to reason about.
|
|
38
|
+
*
|
|
39
|
+
* A host with no `navigator.locks` runs single-tab, exactly as before. That is not a compromise:
|
|
40
|
+
* measured on a plain-http origin, `navigator.locks` and `navigator.storage.getDirectory` are BOTH
|
|
41
|
+
* undefined, because both are secure-context only. Wherever OPFS works the lock exists, and where it
|
|
42
|
+
* does not the database could not have started either.
|
|
43
|
+
*/
|
|
44
|
+
class SharedBffTransport extends core_1.BaseHelper {
|
|
45
|
+
constructor(opts) {
|
|
46
|
+
super({ scope: opts.scope ?? SharedBffTransport.name });
|
|
47
|
+
this.pendingRequests = new Map();
|
|
48
|
+
this.role = BffRoles.ELECTING;
|
|
49
|
+
this.isClosed = false;
|
|
50
|
+
/** Drops this tab out of the lock QUEUE on close, so a closed transport cannot be promoted. */
|
|
51
|
+
this.leadershipQueueAbort = new AbortController();
|
|
52
|
+
/** Callers parked in `fetch()` while the election is still running. */
|
|
53
|
+
this.roleWaiters = [];
|
|
54
|
+
/**
|
|
55
|
+
* A `BroadcastChannel` never delivers a message to the instance that sent it, so a leader does not
|
|
56
|
+
* hear its own answers and a follower does not hear its own requests. Every tab does hear every
|
|
57
|
+
* OTHER tab, which is why a response is matched by id and silently ignored when it belongs to
|
|
58
|
+
* someone else.
|
|
59
|
+
*/
|
|
60
|
+
this.handleChannelMessage = (event) => {
|
|
61
|
+
const message = event.data;
|
|
62
|
+
switch (message?.kind) {
|
|
63
|
+
case ChannelMessageKinds.REQUEST: {
|
|
64
|
+
if (this.role !== BffRoles.LEADER) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
// `serveAsLeader` answers its own failures on the channel; this catch is for the one case
|
|
68
|
+
// it cannot - the channel itself refusing the message, e.g. a body that will not clone.
|
|
69
|
+
this.serveAsLeader({ envelope: message.envelope }).catch((error) => {
|
|
70
|
+
this.logger
|
|
71
|
+
.for(this.serveAsLeader.name)
|
|
72
|
+
.error('Could not answer a follower at all | id: %s | error: %s', message.envelope.id, error);
|
|
73
|
+
});
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
// Both decodes are guarded. The channel is origin-wide and its name is the application's, not
|
|
77
|
+
// a secret, and version skew is the realistic trigger: a long-lived tab on v1 and a new tab
|
|
78
|
+
// on v2 share the same channel and lock BY DESIGN. An envelope this build cannot read must
|
|
79
|
+
// fail its caller, never leave the promise unsettled for the full timeout.
|
|
80
|
+
case ChannelMessageKinds.RESPONSE: {
|
|
81
|
+
const pending = this.takePending({ id: message.envelope.id });
|
|
82
|
+
if (!pending) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
pending.resolve(encode_1.BffEnvelope.decodeResponse({ envelope: message.envelope }));
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
this.logger
|
|
90
|
+
.for(this.handleChannelMessage.name)
|
|
91
|
+
.error('Undecodable response envelope | id: %s | error: %s', message.envelope.id, error);
|
|
92
|
+
pending.reject((0, core_1.getError)({
|
|
93
|
+
message: `[SharedBffTransport] The leader's response could not be decoded | id: ${message.envelope.id}`,
|
|
94
|
+
cause: error,
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
case ChannelMessageKinds.ERROR: {
|
|
100
|
+
const pending = this.takePending({ id: message.envelope.id });
|
|
101
|
+
if (!pending) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
pending.reject(encode_1.BffEnvelope.decodeError({ envelope: message.envelope }));
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
pending.reject((0, core_1.getError)({
|
|
109
|
+
message: `[SharedBffTransport] The leader's error envelope could not be decoded | id: ${message.envelope.id}`,
|
|
110
|
+
cause: error,
|
|
111
|
+
}));
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
default: {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
const channelName = opts.channelName ?? DEFAULT_CHANNEL_NAME;
|
|
121
|
+
this.createWorker = opts.createWorker;
|
|
122
|
+
this.timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
123
|
+
this.lockName = `${channelName}.leader`;
|
|
124
|
+
this.requestIdGenerator = new core_1.RequestIdGenerator({ scope: SharedBffTransport.name });
|
|
125
|
+
// Attached BEFORE the election starts: a tab that becomes leader a moment later must not miss a
|
|
126
|
+
// request a follower posted while it was still deciding.
|
|
127
|
+
this.channel = new BroadcastChannel(channelName);
|
|
128
|
+
this.channel.addEventListener('message', this.handleChannelMessage);
|
|
129
|
+
// A failed election must not leave `fetch()` parked forever waiting for a role that never
|
|
130
|
+
// arrives: this tab falls back to serving itself, which is the pre-election behaviour.
|
|
131
|
+
this.elect().catch((error) => {
|
|
132
|
+
// `close()` aborts the queued lock request, and the rejection that produces is the expected
|
|
133
|
+
// end of the election - not a failure to report.
|
|
134
|
+
if (this.isClosed) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
this.logger
|
|
138
|
+
.for(this.elect.name)
|
|
139
|
+
.error('Leader election failed, falling back to single-tab | error: %s', error);
|
|
140
|
+
if (this.role === BffRoles.ELECTING) {
|
|
141
|
+
this.becomeLeader();
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
getRole() {
|
|
146
|
+
return this.role;
|
|
147
|
+
}
|
|
148
|
+
async fetch(opts) {
|
|
149
|
+
if (this.isClosed) {
|
|
150
|
+
throw (0, core_1.getError)({
|
|
151
|
+
message: '[SharedBffTransport] The transport is closed | open a new one to make requests',
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
await this.whenRoleSettled();
|
|
155
|
+
// Re-checked AFTER the await: `close()` can land while the election is still running, and
|
|
156
|
+
// posting then reaches a channel that is already closed - the caller would get a raw
|
|
157
|
+
// `DOMException` instead of this error, and a pending entry plus a live timer would be left
|
|
158
|
+
// behind on a transport nothing can settle.
|
|
159
|
+
if (this.isClosed) {
|
|
160
|
+
throw (0, core_1.getError)({
|
|
161
|
+
message: '[SharedBffTransport] The transport is closed | open a new one to make requests',
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
if (this.leaderStartupError) {
|
|
165
|
+
throw (0, core_1.getError)({
|
|
166
|
+
message: '[SharedBffTransport] This tab leads but its BFF worker never started | reload the page',
|
|
167
|
+
cause: this.leaderStartupError,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
if (this.role === BffRoles.LEADER && this.leaderTransport) {
|
|
171
|
+
return this.leaderTransport.fetch(opts);
|
|
172
|
+
}
|
|
173
|
+
return this.fetchViaLeader(opts);
|
|
174
|
+
}
|
|
175
|
+
close() {
|
|
176
|
+
this.isClosed = true;
|
|
177
|
+
// A follower is parked in the lock QUEUE rather than holding anything. Without this it stays
|
|
178
|
+
// queued after close, and the browser would eventually hand leadership to a dead transport -
|
|
179
|
+
// which returns straight away, but only after the tab that could have served was skipped.
|
|
180
|
+
this.leadershipQueueAbort.abort();
|
|
181
|
+
// ORDER IS THE POINT, and it is the opposite of the obvious one. The worker has to be GONE
|
|
182
|
+
// before the lock is released: releasing first promotes another tab, which opens the same OPFS
|
|
183
|
+
// database while this worker still holds the exclusive access handle - the exact failure this
|
|
184
|
+
// transport exists to prevent, reintroduced by its own teardown.
|
|
185
|
+
//
|
|
186
|
+
// `terminate()` is abrupt by design. It is no worse than the tab-close path the browser already
|
|
187
|
+
// takes on every leader, and leaving the worker alive is strictly worse than an abrupt stop.
|
|
188
|
+
this.leaderTransport?.close();
|
|
189
|
+
this.leaderTransport = undefined;
|
|
190
|
+
this.leaderWorker?.terminate();
|
|
191
|
+
this.leaderWorker = undefined;
|
|
192
|
+
// Only now: the next tab may open the database.
|
|
193
|
+
this.releaseLeadership?.();
|
|
194
|
+
this.releaseLeadership = undefined;
|
|
195
|
+
this.channel.removeEventListener('message', this.handleChannelMessage);
|
|
196
|
+
this.channel.close();
|
|
197
|
+
this.rejectAllPending({ reason: 'the transport was closed' });
|
|
198
|
+
this.settleRoleWaiters();
|
|
199
|
+
}
|
|
200
|
+
async elect() {
|
|
201
|
+
const locks = globalThis.navigator?.locks;
|
|
202
|
+
if (!locks) {
|
|
203
|
+
this.logger
|
|
204
|
+
.for(this.elect.name)
|
|
205
|
+
.warn('navigator.locks is unavailable - running single-tab | a second tab cannot open the same OPFS database');
|
|
206
|
+
this.becomeLeader();
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
// `ifAvailable` answers NOW instead of queueing, which is what lets this tab find out it is a
|
|
210
|
+
// follower rather than hanging until the current leader goes away.
|
|
211
|
+
const hasWon = await locks.request(this.lockName, { ifAvailable: true }, async (lock) => {
|
|
212
|
+
if (!lock) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
// Symmetric with the queued branch below, and not defensive padding. A real `LockManager`
|
|
216
|
+
// grants from a queued task - it never runs this callback synchronously inside `request()` -
|
|
217
|
+
// so `close()` can land between the constructor and this line. Without the guard a CLOSED
|
|
218
|
+
// transport starts a worker, reports itself leader, and then holds the lock for the lifetime
|
|
219
|
+
// of the page: `close()` has already read and cleared `releaseLeadership`, so the assignment
|
|
220
|
+
// `heldUntilClosed()` makes below can never be resolved by anything. No other tab is ever
|
|
221
|
+
// promoted, and this one answers nobody - its channel listener is already detached.
|
|
222
|
+
//
|
|
223
|
+
// Returning true, not false: this tab DID hold the lock. Returning releases it immediately,
|
|
224
|
+
// which is exactly what a closed transport should do with it.
|
|
225
|
+
if (this.isClosed) {
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
228
|
+
this.becomeLeader();
|
|
229
|
+
await this.heldUntilClosed();
|
|
230
|
+
return true;
|
|
231
|
+
});
|
|
232
|
+
if (hasWon || this.isClosed) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
this.becomeFollower();
|
|
236
|
+
// Queues behind the current leader. The browser releases its lock when that tab goes away - a
|
|
237
|
+
// crash included - so this resolves without anything having to detect the death.
|
|
238
|
+
await locks.request(this.lockName, { signal: this.leadershipQueueAbort.signal }, async () => {
|
|
239
|
+
if (this.isClosed) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
this.logger
|
|
243
|
+
.for(this.elect.name)
|
|
244
|
+
.info('Promoted to leader | previous leader released the lock');
|
|
245
|
+
this.becomeLeader();
|
|
246
|
+
await this.heldUntilClosed();
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
becomeLeader() {
|
|
250
|
+
// Anything this tab had in flight was addressed to the leader that just went away. It cannot be
|
|
251
|
+
// replayed - a write may well have been applied - so it is failed explicitly rather than left
|
|
252
|
+
// to run out its timeout under a leader that will never answer it.
|
|
253
|
+
this.rejectAllPending({
|
|
254
|
+
reason: 'the leader serving it went away before this tab was promoted',
|
|
255
|
+
});
|
|
256
|
+
try {
|
|
257
|
+
this.leaderWorker = this.createWorker();
|
|
258
|
+
this.leaderTransport = new worker_1.WorkerBffTransport({
|
|
259
|
+
worker: this.leaderWorker,
|
|
260
|
+
timeoutMs: this.timeoutMs,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
catch (error) {
|
|
264
|
+
// A CSP refusal or a bad worker URL must still SETTLE the role. `whenRoleSettled()` has no
|
|
265
|
+
// timeout of its own and the per-request timer is armed only inside `fetchViaLeader()`, which
|
|
266
|
+
// this tab never reaches - so leaving the role unsettled hangs every caller forever, with no
|
|
267
|
+
// error and no log per request.
|
|
268
|
+
this.leaderStartupError = error;
|
|
269
|
+
this.logger
|
|
270
|
+
.for(this.becomeLeader.name)
|
|
271
|
+
.error('Could not start the BFF worker | error: %s', error);
|
|
272
|
+
}
|
|
273
|
+
this.role = BffRoles.LEADER;
|
|
274
|
+
this.settleRoleWaiters();
|
|
275
|
+
}
|
|
276
|
+
becomeFollower() {
|
|
277
|
+
this.role = BffRoles.FOLLOWER;
|
|
278
|
+
this.settleRoleWaiters();
|
|
279
|
+
}
|
|
280
|
+
heldUntilClosed() {
|
|
281
|
+
return new Promise(resolve => {
|
|
282
|
+
this.releaseLeadership = resolve;
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
whenRoleSettled() {
|
|
286
|
+
if (this.role !== BffRoles.ELECTING) {
|
|
287
|
+
return Promise.resolve();
|
|
288
|
+
}
|
|
289
|
+
return new Promise(resolve => {
|
|
290
|
+
this.roleWaiters.push(resolve);
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
settleRoleWaiters() {
|
|
294
|
+
const waiters = this.roleWaiters;
|
|
295
|
+
this.roleWaiters = [];
|
|
296
|
+
for (const waiter of waiters) {
|
|
297
|
+
waiter();
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* The caller's `AbortSignal` is honoured HERE as well as in `WorkerBffTransport`. Without it the
|
|
302
|
+
* same page code behaves differently depending on which tab won the lock: abort works in the
|
|
303
|
+
* leader and silently does nothing in every follower, so a data provider that aborts on unmount
|
|
304
|
+
* leaves promises pending for the full timeout in every tab but one.
|
|
305
|
+
*
|
|
306
|
+
* Aborting settles the CALLER. It does not cancel the leader's work - there is no cancel message,
|
|
307
|
+
* and a write already dispatched cannot be taken back.
|
|
308
|
+
*/
|
|
309
|
+
async fetchViaLeader(opts) {
|
|
310
|
+
const { signal } = opts.request;
|
|
311
|
+
if (signal?.aborted) {
|
|
312
|
+
throw this.toAbortError({ signal });
|
|
313
|
+
}
|
|
314
|
+
const id = this.requestIdGenerator.nextId();
|
|
315
|
+
const envelope = await encode_1.BffEnvelope.encodeRequest({
|
|
316
|
+
request: opts.request,
|
|
317
|
+
id,
|
|
318
|
+
url: encode_1.BffEnvelope.toSyntheticUrl({ url: opts.request.url }),
|
|
319
|
+
});
|
|
320
|
+
return new Promise((resolve, reject) => {
|
|
321
|
+
const timer = setTimeout(() => {
|
|
322
|
+
this.takePending({ id })?.reject((0, core_1.getError)({
|
|
323
|
+
message: `[SharedBffTransport] Timed out waiting for the leader tab | id: ${id} | timeoutMs: ${this.timeoutMs}`,
|
|
324
|
+
}));
|
|
325
|
+
}, this.timeoutMs);
|
|
326
|
+
const handleAbort = () => {
|
|
327
|
+
this.takePending({ id })?.reject(this.toAbortError({ signal }));
|
|
328
|
+
};
|
|
329
|
+
signal?.addEventListener('abort', handleAbort);
|
|
330
|
+
this.pendingRequests.set(id, {
|
|
331
|
+
resolve,
|
|
332
|
+
reject,
|
|
333
|
+
release: () => {
|
|
334
|
+
clearTimeout(timer);
|
|
335
|
+
signal?.removeEventListener('abort', handleAbort);
|
|
336
|
+
},
|
|
337
|
+
});
|
|
338
|
+
// Buffering the body took an await, and `addEventListener('abort')` never fires on a signal
|
|
339
|
+
// that aborted before it was attached - without this re-read that window loses the abort.
|
|
340
|
+
if (signal?.aborted) {
|
|
341
|
+
handleAbort();
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
this.channel.postMessage({
|
|
345
|
+
kind: ChannelMessageKinds.REQUEST,
|
|
346
|
+
envelope,
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
/** `signal.reason` first, the way `globalThis.fetch` rejects, so a custom abort reason survives. */
|
|
351
|
+
toAbortError(opts) {
|
|
352
|
+
return (opts.signal?.reason ??
|
|
353
|
+
(0, core_1.getError)({ message: '[SharedBffTransport] The request was aborted by its caller' }));
|
|
354
|
+
}
|
|
355
|
+
takePending(opts) {
|
|
356
|
+
const pending = this.pendingRequests.get(opts.id);
|
|
357
|
+
if (!pending) {
|
|
358
|
+
return undefined;
|
|
359
|
+
}
|
|
360
|
+
this.pendingRequests.delete(opts.id);
|
|
361
|
+
pending.release();
|
|
362
|
+
return pending;
|
|
363
|
+
}
|
|
364
|
+
rejectAllPending(opts) {
|
|
365
|
+
for (const id of [...this.pendingRequests.keys()]) {
|
|
366
|
+
this.takePending({ id })?.reject((0, core_1.getError)({
|
|
367
|
+
message: `[SharedBffTransport] Request abandoned | id: ${id} | reason: ${opts.reason}`,
|
|
368
|
+
}));
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
async serveAsLeader(opts) {
|
|
372
|
+
const { envelope } = opts;
|
|
373
|
+
try {
|
|
374
|
+
const response = await this.leaderTransport.fetch({
|
|
375
|
+
request: encode_1.BffEnvelope.decodeRequest({ envelope }),
|
|
376
|
+
});
|
|
377
|
+
this.channel.postMessage({
|
|
378
|
+
kind: ChannelMessageKinds.RESPONSE,
|
|
379
|
+
envelope: await encode_1.BffEnvelope.encodeResponse({ response, id: envelope.id }),
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
catch (error) {
|
|
383
|
+
// The follower gets the failure as a failure, rather than waiting out a timeout that would
|
|
384
|
+
// tell it nothing about what went wrong.
|
|
385
|
+
this.logger
|
|
386
|
+
.for(this.serveAsLeader.name)
|
|
387
|
+
.error('Failed to serve a follower request | id: %s | error: %s', envelope.id, error);
|
|
388
|
+
this.channel.postMessage({
|
|
389
|
+
kind: ChannelMessageKinds.ERROR,
|
|
390
|
+
envelope: encode_1.BffEnvelope.encodeError({ id: envelope.id, error }),
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
exports.SharedBffTransport = SharedBffTransport;
|
|
396
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../../../src/transport/shared.ts"],"names":[],"mappings":";;;AAAA,sDAAuF;AAEvF,8CAAgD;AAOhD,qCAA8C;AAE9C,MAAM,oBAAoB,GAAG,WAAW,CAAC;AACzC,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAElC,yFAAyF;AACzF,MAAa,QAAQ;aACH,aAAQ,GAAG,UAAU,CAAC;aACtB,WAAM,GAAG,QAAQ,CAAC;aAClB,aAAQ,GAAG,UAAU,CAAC;;AAHxC,4BAIC;AAID,MAAM,mBAA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O,EAAE,oBAAW,CAAC,aAAa,CAAC,EAAE,QAAQ,EAAE,CAAC;aACjD,CAAC,CAAC;YAEH,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC;gBACvB,IAAI,EAAE,mBAAmB,CAAC,QAAQ;gBAClC,QAAQ,EAAE,MAAM,oBAAW,CAAC,cAAc,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,QAAQ,CAAC,EAAE,EAAE,CAAC;aAChD,CAAC,CAAC;QAC/B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,2FAA2F;YAC3F,yCAAyC;YACzC,IAAI,CAAC,MAAM;iBACR,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC;iBAC5B,KAAK,CAAC,yDAAyD,EAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;YAExF,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC;gBACvB,IAAI,EAAE,mBAAmB,CAAC,KAAK;gBAC/B,QAAQ,EAAE,oBAAW,CAAC,WAAW,CAAC,EAAE,EAAE,EAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC;aACpC,CAAC,CAAC;QAC/B,CAAC;IACH,CAAC;CACF;AArcD,gDAqcC"}
|
|
@@ -15,6 +15,10 @@ export declare class WorkerBffTransport extends BaseHelper implements IBffTransp
|
|
|
15
15
|
private readonly timeoutMs;
|
|
16
16
|
private readonly requestIdGenerator;
|
|
17
17
|
private readonly pendingRequests;
|
|
18
|
+
/** Set by `close()`, so a later `fetch()` is refused instead of reaching a worker nothing listens to. */
|
|
19
|
+
private isClosed;
|
|
20
|
+
/** Outstanding `handleWorkerError` grace timers, cleared by `close()` so none outlives the transport. */
|
|
21
|
+
private readonly graceTimers;
|
|
18
22
|
constructor(opts: {
|
|
19
23
|
worker: Worker;
|
|
20
24
|
timeoutMs?: number;
|
|
@@ -23,10 +27,20 @@ export declare class WorkerBffTransport extends BaseHelper implements IBffTransp
|
|
|
23
27
|
private readonly takePending;
|
|
24
28
|
private readonly handleMessage;
|
|
25
29
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
30
|
+
* An `error` event does NOT mean the worker is dead.
|
|
31
|
+
*
|
|
32
|
+
* Measured in Chromium: a synchronous uncaught throw anywhere in the worker - a third-party timer,
|
|
33
|
+
* say - fires this event while the worker keeps serving, and the genuine responses to the
|
|
34
|
+
* in-flight requests arrive a moment later. Rejecting immediately reported a committed POST as
|
|
35
|
+
* failed and then dropped its real 200 on the floor, because nothing was left waiting for it.
|
|
36
|
+
*
|
|
37
|
+
* So the event is logged at once and the requests are given a grace window. Whatever the worker
|
|
38
|
+
* still answers inside it settles normally; whatever is left after it really is lost, and is
|
|
39
|
+
* rejected with the diagnostics rather than left to run out its own timeout.
|
|
28
40
|
*/
|
|
29
41
|
private readonly handleWorkerError;
|
|
42
|
+
/** `ErrorEvent` carries the useful fields; a plain `Event` does not, so both shapes are handled. */
|
|
43
|
+
private describeErrorEvent;
|
|
30
44
|
/**
|
|
31
45
|
* `messageerror` means exactly ONE posted message failed to deserialise, and the event carries
|
|
32
46
|
* nothing that says which. With a single request in flight that is unambiguous, so it settles
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../../src/transport/worker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAY,MAAM,6BAA6B,CAAC;AAInE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../../src/transport/worker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAY,MAAM,6BAA6B,CAAC;AAInE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAoBpD;;;;;;;;;GASG;AACH,qBAAa,kBAAmB,SAAQ,UAAW,YAAW,aAAa;IACzE,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAEhC;IACH,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAsC;IAEtE,yGAAyG;IACzG,OAAO,CAAC,QAAQ,CAAS;IAEzB,yGAAyG;IACzG,OAAO,CAAC,QAAQ,CAAC,WAAW,CAA4C;gBAE5D,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE;IAWxD,wHAAwH;IACxH,OAAO,CAAC,QAAQ,CAAC,WAAW,CAS1B;IAEF,OAAO,CAAC,QAAQ,CAAC,aAAa,CAgC5B;IAEF;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CA+BhC;IAEF,oGAAoG;IACpG,OAAO,CAAC,kBAAkB;IAe1B;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAkBjC;IAEI,KAAK,CAAC,IAAI,EAAE;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,CAAC,QAAQ,CAAC;IA0E1D,2HAA2H;IAC3H,OAAO,CAAC,YAAY;IAOpB;;;OAGG;IACH,KAAK,IAAI,IAAI;CAmBd"}
|
|
@@ -5,6 +5,8 @@ const core_1 = require("@venizia/ignis-helpers/core");
|
|
|
5
5
|
const core_2 = require("@venizia/ignis-helpers/core");
|
|
6
6
|
const encode_1 = require("../envelope/encode");
|
|
7
7
|
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
8
|
+
/** How long a still-serving worker gets to answer after it reports an error - see `handleWorkerError`. */
|
|
9
|
+
const WORKER_ERROR_GRACE_MS = 250;
|
|
8
10
|
const isErrorEnvelope = (data) => {
|
|
9
11
|
return 'error' in data;
|
|
10
12
|
};
|
|
@@ -25,6 +27,10 @@ class WorkerBffTransport extends core_1.BaseHelper {
|
|
|
25
27
|
scope: WorkerBffTransport.name,
|
|
26
28
|
});
|
|
27
29
|
this.pendingRequests = new Map();
|
|
30
|
+
/** Set by `close()`, so a later `fetch()` is refused instead of reaching a worker nothing listens to. */
|
|
31
|
+
this.isClosed = false;
|
|
32
|
+
/** Outstanding `handleWorkerError` grace timers, cleared by `close()` so none outlives the transport. */
|
|
33
|
+
this.graceTimers = new Set();
|
|
28
34
|
/** Removes a pending request from the map and releases its timer and abort listener, so nothing can settle it twice. */
|
|
29
35
|
this.takePending = (opts) => {
|
|
30
36
|
const pending = this.pendingRequests.get(opts.id);
|
|
@@ -65,15 +71,37 @@ class WorkerBffTransport extends core_1.BaseHelper {
|
|
|
65
71
|
}
|
|
66
72
|
};
|
|
67
73
|
/**
|
|
68
|
-
*
|
|
69
|
-
*
|
|
74
|
+
* An `error` event does NOT mean the worker is dead.
|
|
75
|
+
*
|
|
76
|
+
* Measured in Chromium: a synchronous uncaught throw anywhere in the worker - a third-party timer,
|
|
77
|
+
* say - fires this event while the worker keeps serving, and the genuine responses to the
|
|
78
|
+
* in-flight requests arrive a moment later. Rejecting immediately reported a committed POST as
|
|
79
|
+
* failed and then dropped its real 200 on the floor, because nothing was left waiting for it.
|
|
80
|
+
*
|
|
81
|
+
* So the event is logged at once and the requests are given a grace window. Whatever the worker
|
|
82
|
+
* still answers inside it settles normally; whatever is left after it really is lost, and is
|
|
83
|
+
* rejected with the diagnostics rather than left to run out its own timeout.
|
|
70
84
|
*/
|
|
71
85
|
this.handleWorkerError = (event) => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
86
|
+
// `String(event)` is always `[object ErrorEvent]` - it says nothing. Read the fields a browser
|
|
87
|
+
// actually populates, so the log names the file and line that failed.
|
|
88
|
+
const detail = this.describeErrorEvent(event);
|
|
89
|
+
this.logger
|
|
90
|
+
.for('handleWorkerError')
|
|
91
|
+
.error('Worker reported an error | pending: %s | detail: %s', this.pendingRequests.size, detail);
|
|
92
|
+
const affected = [...this.pendingRequests.keys()];
|
|
93
|
+
// Held so `close()` can clear it. A grace timer that outlives the transport keeps the process
|
|
94
|
+
// (or a test runner) awake and fires against a map nobody is watching any more.
|
|
95
|
+
const timer = setTimeout(() => {
|
|
96
|
+
this.graceTimers.delete(timer);
|
|
97
|
+
for (const id of affected) {
|
|
98
|
+
// `takePending` answers undefined for anything the worker managed to settle in the window.
|
|
99
|
+
this.takePending({ id })?.reject((0, core_1.getError)({
|
|
100
|
+
message: `[WorkerBffTransport] Worker failed while awaiting a response | id: ${id} | detail: ${detail}`,
|
|
101
|
+
}));
|
|
102
|
+
}
|
|
103
|
+
}, WORKER_ERROR_GRACE_MS);
|
|
104
|
+
this.graceTimers.add(timer);
|
|
77
105
|
};
|
|
78
106
|
/**
|
|
79
107
|
* `messageerror` means exactly ONE posted message failed to deserialise, and the event carries
|
|
@@ -99,9 +127,25 @@ class WorkerBffTransport extends core_1.BaseHelper {
|
|
|
99
127
|
this.worker.addEventListener('error', this.handleWorkerError);
|
|
100
128
|
this.worker.addEventListener('messageerror', this.handleMessageError);
|
|
101
129
|
}
|
|
130
|
+
/** `ErrorEvent` carries the useful fields; a plain `Event` does not, so both shapes are handled. */
|
|
131
|
+
describeErrorEvent(event) {
|
|
132
|
+
const candidate = event;
|
|
133
|
+
if (!candidate.message) {
|
|
134
|
+
return event.type;
|
|
135
|
+
}
|
|
136
|
+
return `${candidate.message} (${candidate.filename ?? 'unknown'}:${candidate.lineno ?? 0}:${candidate.colno ?? 0})`;
|
|
137
|
+
}
|
|
102
138
|
async fetch(opts) {
|
|
103
139
|
const { request } = opts;
|
|
104
140
|
const { signal } = request;
|
|
141
|
+
// Refused, not queued. `close()` detaches the message listener, so a request posted after it
|
|
142
|
+
// still reached the worker - which EXECUTED it, writes included - while the answer had nowhere
|
|
143
|
+
// to land and the caller waited out the full timeout.
|
|
144
|
+
if (this.isClosed) {
|
|
145
|
+
throw (0, core_1.getError)({
|
|
146
|
+
message: '[WorkerBffTransport] The transport is closed | open a new one to make requests',
|
|
147
|
+
});
|
|
148
|
+
}
|
|
105
149
|
if (signal?.aborted) {
|
|
106
150
|
throw this.toAbortError({ signal });
|
|
107
151
|
}
|
|
@@ -138,6 +182,15 @@ class WorkerBffTransport extends core_1.BaseHelper {
|
|
|
138
182
|
handleAbort();
|
|
139
183
|
return;
|
|
140
184
|
}
|
|
185
|
+
// `close()` can land during that same await. The entry guard above is then stale, and posting
|
|
186
|
+
// reaches a worker that EXECUTES the request - writes included - while the answer has nowhere
|
|
187
|
+
// to land, because `close()` already detached the listener.
|
|
188
|
+
if (this.isClosed) {
|
|
189
|
+
this.takePending({ id })?.reject((0, core_1.getError)({
|
|
190
|
+
message: `[WorkerBffTransport] The transport closed while the request was being encoded | id: ${id}`,
|
|
191
|
+
}));
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
141
194
|
this.worker.postMessage(requestEnvelope, requestEnvelope.body ? [requestEnvelope.body] : []);
|
|
142
195
|
});
|
|
143
196
|
}
|
|
@@ -151,6 +204,11 @@ class WorkerBffTransport extends core_1.BaseHelper {
|
|
|
151
204
|
* left alone - this transport did not create it, and a page may hand the same one to another.
|
|
152
205
|
*/
|
|
153
206
|
close() {
|
|
207
|
+
this.isClosed = true;
|
|
208
|
+
for (const timer of this.graceTimers) {
|
|
209
|
+
clearTimeout(timer);
|
|
210
|
+
}
|
|
211
|
+
this.graceTimers.clear();
|
|
154
212
|
this.worker.removeEventListener('message', this.handleMessage);
|
|
155
213
|
this.worker.removeEventListener('error', this.handleWorkerError);
|
|
156
214
|
this.worker.removeEventListener('messageerror', this.handleMessageError);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../../../src/transport/worker.ts"],"names":[],"mappings":";;;AAAA,sDAAmE;AACnE,sDAAiE;AACjE,8CAAgD;AAIhD,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAElC,MAAM,eAAe,GAAG,CACtB,IAA8C,EACnB,EAAE;IAC7B,OAAO,OAAO,IAAI,IAAI,CAAC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AAE,MAAM,CAC9B,IAAA,eAAQ,EAAC;gBACP,OAAO,EAAE,iFAAiF,EAAE,EAAE;aAC/F,CAAC,CACH,CAAC;QACJ,CAAC;IACH,CAAC;CACF;AAvQD,gDAuQC"}
|
|
@@ -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"}
|