everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
103 lines (101 loc) • 4.37 kB
JavaScript
const require_keys = require("./keys.cjs");
const require_client = require("./client.cjs");
//#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);
/** How long a transaction may stay unknown to the chain before the wait is
* declared terminal: a broadcast that landed is visible within seconds; one
* still unknown past this grace was dropped (most often a stale nonce from a
* lagging RPC at broadcast) and will never apply. */
const TX_UNKNOWN_GRACE_MS = 3e4;
const terminalVerdict = (txHash, reason, unverified) => ({
ok: false,
unverified: [...unverified],
lastError: `transaction ${txHash} will never apply (${reason}) — the registry was not written`,
terminalTx: true
});
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) => require_client.registryClientShape.read(key));
const history = opts.history ?? ((key, limit) => require_client.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[0];
let trackByTx = opts.txHash !== void 0 && opts.history !== void 0 && probeLeg !== void 0;
let txProbeSkips = 0;
let indexed = false;
const startedAt = now();
let txUnknownSince;
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;
}
let contentMissed = false;
for (const leg of pending()) try {
const served = leg.read ? 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");
contentMissed = true;
}
} catch (error) {
lastErrors.set(leg.kind, error instanceof Error ? error.message : String(error));
contentMissed = true;
}
if (pending().length === 0) return {
ok: true,
verified: [...verified]
};
if (contentMissed && opts.txHash && opts.txStatus && !indexed) {
let status;
try {
status = await opts.txStatus(opts.txHash);
} catch {
status = "unreachable";
}
if (status === "invalid") return terminalVerdict(opts.txHash, "rejected on-chain (stale nonce, missing publish key, or expired — re-mint with `bos login --key` and update NEAR_PRIVATE_KEY)", pending().map((leg) => leg.kind));
if (status === "unknown") {
if (txUnknownSince === void 0) txUnknownSince = now();
else if (now() - txUnknownSince >= 3e4) return terminalVerdict(opts.txHash, `unknown to the chain past ${TX_UNKNOWN_GRACE_MS / 1e3}s — dropped at broadcast`, pending().map((leg) => leg.kind));
} else txUnknownSince = void 0;
}
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) => require_keys.keyPathOf(leg.key)).join(", ")}` + (verdict.lastError ? ` (last error: ${verdict.lastError})` : "");
}
//#endregion
exports.TX_UNKNOWN_GRACE_MS = TX_UNKNOWN_GRACE_MS;
exports.confirmPublish = confirmPublish;
exports.describeConfirmTimeout = describeConfirmTimeout;
//# sourceMappingURL=confirm.cjs.map