everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
71 lines (69 loc) • 2.9 kB
JavaScript
import { keyPathOf } from "./keys.mjs";
import { registryClientShape } from "./client.mjs";
//#region src/services/registry/confirm.ts
const envMs = (name) => {
const raw = Number(process.env[name]);
return Number.isFinite(raw) && raw > 0 ? raw : void 0;
};
const deepEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
async function confirmPublish(opts) {
const timeoutMs = opts.timeoutMs ?? envMs("BOS_PUBLISH_CONFIRMATION_TIMEOUT_MS") ?? 12e4;
const intervalMs = opts.intervalMs ?? envMs("BOS_PUBLISH_CONFIRMATION_INTERVAL_MS") ?? 3e3;
const read = opts.read ?? ((key) => registryClientShape.read(key));
const history = opts.history ?? ((key, limit) => registryClientShape.history(key, limit));
const now = opts.now ?? Date.now;
const sleep = opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
const verified = /* @__PURE__ */ new Set();
const pending = () => opts.legs.filter((leg) => !verified.has(leg.kind));
const lastErrors = /* @__PURE__ */ new Map();
const TX_PROBE_SKIP_LIMIT = 2;
const probeLeg = opts.legs.find((leg) => "key" in leg);
let trackByTx = opts.txHash !== void 0 && opts.history !== void 0 && probeLeg !== void 0;
let txProbeSkips = 0;
let indexed = false;
const startedAt = now();
while (now() - startedAt < timeoutMs) {
if (trackByTx && !indexed) try {
if ((await history(probeLeg.key, 5)).some((entry) => entry.txHash === opts.txHash)) {
indexed = true;
txProbeSkips = 0;
} else {
txProbeSkips += 1;
if (txProbeSkips >= TX_PROBE_SKIP_LIMIT) trackByTx = false;
await sleep(intervalMs);
continue;
}
} catch {
trackByTx = false;
}
for (const leg of pending()) try {
const served = "read" in leg ? await leg.read() : await read(leg.key);
if (deepEqual(served, leg.expected)) {
verified.add(leg.kind);
lastErrors.delete(leg.kind);
} else lastErrors.set(leg.kind, "registry serves a different value");
} catch (error) {
lastErrors.set(leg.kind, error instanceof Error ? error.message : String(error));
}
if (pending().length === 0) return {
ok: true,
verified: [...verified]
};
await sleep(intervalMs);
}
const unverified = pending().map((leg) => leg.kind);
const lastError = [...lastErrors.values()].at(-1);
return {
ok: false,
unverified,
...lastError ? { lastError } : {}
};
}
/** The train-facing failure message — the wire form of `TimedOut` (#422's
* confirm channel): the legs' actual wire keys, named. */
function describeConfirmTimeout(legs, verdict) {
return `Timed out waiting for publish confirmation — unverified: ${legs.filter((leg) => verdict.unverified.includes(leg.kind)).map((leg) => "wireKey" in leg ? leg.wireKey : keyPathOf(leg.key)).join(", ")}` + (verdict.lastError ? ` (last error: ${verdict.lastError})` : "");
}
//#endregion
export { confirmPublish, describeConfirmTimeout };
//# sourceMappingURL=confirm.mjs.map