@voltro/protocol 0.86.0 → 0.87.0

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.
@@ -9,58 +9,113 @@ var t = (e, t, n) => `${e ?? "(global)"}|${t.toUpperCase()}|${n}`, n = (e, t, n,
9
9
  get message() {
10
10
  return "this mutation requires a live native WebSocket connection";
11
11
  }
12
- }, i = async (e, t, n, r, i, o = a) => {
13
- let s = await e.claim(t, n, i, r, o);
14
- return s === "claimed" ? { kind: "fresh" } : s.status === "completed" && s.response ? {
15
- kind: "replay",
16
- response: s.response
17
- } : { kind: "conflict" };
18
- }, a = 3e4, o = (e, t, n, r = a, i = Date.now) => {
19
- let o = setInterval(() => {
20
- e.renew(t, n, i()).catch(() => {});
21
- }, Math.max(1, Math.floor(r / 3)));
22
- return o.unref?.(), () => clearInterval(o);
23
- }, s = (e, t, n, r, i) => e.complete(t, n, r, i), c = (e, t, n) => e.release(t, n), l = () => {
24
- let e = /* @__PURE__ */ new Map(), t = (e, t) => `${e}\u0000${t}`;
12
+ }, i = async (e, t, n, r, i, o, s = a) => {
13
+ if (!/^[0-9a-f]{64}$/.test(r)) throw Error("Invalid idempotency input fingerprint");
14
+ let c = await e.claim(t, n, r, o, i, s);
15
+ if (c.kind === "claimed") {
16
+ let e = c.claim;
17
+ if (typeof e.id != "string" || e.id.length === 0 || e.scope !== t || e.key !== n || e.inputHash !== r) throw Error("Idempotency store returned an invalid claim identity");
18
+ return {
19
+ kind: "fresh",
20
+ claim: Object.freeze({
21
+ scope: t,
22
+ key: n,
23
+ id: e.id,
24
+ inputHash: r
25
+ })
26
+ };
27
+ }
28
+ if (c.kind === "existing") {
29
+ if (c.record.scope !== t || c.record.key !== n) throw Error("Idempotency store returned an invalid receipt identity");
30
+ return typeof c.record.inputHash == "string" ? c.record.inputHash === r ? c.record.status === "completed" && c.record.response ? {
31
+ kind: "replay",
32
+ response: c.record.response
33
+ } : { kind: "conflict" } : { kind: "input-conflict" } : { kind: "input-unbound" };
34
+ }
35
+ if (c.kind === "conflict") return { kind: "conflict" };
36
+ throw Error("Idempotency store returned an invalid acquisition result");
37
+ }, a = 3e4, o = (e, t, n = a, r = Date.now) => {
38
+ let i = Object.freeze({
39
+ scope: t.scope,
40
+ key: t.key,
41
+ id: t.id,
42
+ inputHash: t.inputHash
43
+ }), o = !1, s = setInterval(() => {
44
+ o || (o = !0, e.renew(i, r()).then((e) => {
45
+ e || clearInterval(s);
46
+ }, () => {}).finally(() => {
47
+ o = !1;
48
+ }));
49
+ }, Math.max(1, Math.floor(n / 3)));
50
+ return s.unref?.(), () => clearInterval(s);
51
+ }, s = (e, t, n, r) => e.complete(t, n, r), c = (e, t) => e.release(t), l = 0n, u = () => {
52
+ let e = /* @__PURE__ */ new Map(), t = (e, t) => JSON.stringify([e, t]), n = (n) => {
53
+ let r = e.get(t(n.scope, n.key));
54
+ return r?.status === "in_flight" && r.id === n.id && r.inputHash === n.inputHash ? r : void 0;
55
+ };
25
56
  return {
26
57
  get: async (n, r) => e.get(t(n, r)) ?? null,
27
- claim: async (n, r, i, a, o) => {
28
- let s = e.get(t(n, r));
29
- return s && i - s.createdAt < u(s.status, a, o) ? s : (e.set(t(n, r), {
58
+ claim: async (n, r, i, a, o, s) => {
59
+ let c = e.get(t(n, r));
60
+ if (c && c.inputHash !== i || c && a - c.createdAt < d(c.status, o, s)) return {
61
+ kind: "existing",
62
+ record: c
63
+ };
64
+ let u = `memory:${(++l).toString(36)}`;
65
+ return e.set(t(n, r), {
66
+ id: u,
30
67
  scope: n,
31
68
  key: r,
69
+ inputHash: i,
32
70
  status: "in_flight",
33
71
  response: null,
34
- createdAt: i
35
- }), "claimed");
72
+ createdAt: a
73
+ }), {
74
+ kind: "claimed",
75
+ claim: Object.freeze({
76
+ id: u,
77
+ scope: n,
78
+ key: r,
79
+ inputHash: i
80
+ })
81
+ };
36
82
  },
37
- complete: async (n, r, i, a) => {
38
- e.set(t(n, r), {
39
- scope: n,
40
- key: r,
83
+ complete: async (r, i, a) => {
84
+ let o = n(r);
85
+ return o ? (e.set(t(r.scope, r.key), {
86
+ ...o,
41
87
  status: "completed",
42
88
  response: i,
43
89
  createdAt: a
44
- });
45
- },
46
- release: async (n, r) => {
47
- e.delete(t(n, r));
90
+ }), !0) : !1;
48
91
  },
49
- renew: async (n, r, i) => {
50
- let a = e.get(t(n, r));
51
- a?.status === "in_flight" && e.set(t(n, r), {
92
+ release: async (r) => n(r) ? e.delete(t(r.scope, r.key)) : !1,
93
+ renew: async (r, i) => {
94
+ let a = n(r);
95
+ return a ? (e.set(t(r.scope, r.key), {
52
96
  ...a,
53
97
  createdAt: i
54
- });
98
+ }), !0) : !1;
55
99
  }
56
100
  };
57
- }, u = (e, t, n) => e === "completed" ? t : Math.min(t, n), d = class extends e.TaggedError()("MutationInFlight", {
101
+ }, d = (e, t, n) => e === "completed" ? t : Math.min(t, n), f = class extends e.TaggedError()("MutationInFlight", {
58
102
  traceId: e.String,
59
103
  retryAfterMs: e.Number
60
104
  }) {
61
105
  get message() {
62
106
  return `a request with this idempotency key is still in flight — replay the same key after ${this.retryAfterMs}ms`;
63
107
  }
64
- }, f = 1e3;
108
+ }, p = 1e3, m = class extends e.TaggedError()("MutationIdempotencyRefused", {
109
+ traceId: e.String,
110
+ reason: e.Literals([
111
+ "input-conflict",
112
+ "input-unbound",
113
+ "input-unavailable"
114
+ ])
115
+ }) {
116
+ get message() {
117
+ return this.reason === "input-unavailable" ? "This request input could not be captured safely. This attempt was stopped before execution." : "This request cannot be safely matched to the original operation. Check its current state before starting another action.";
118
+ }
119
+ };
65
120
  //#endregion
66
- export { a as DEFAULT_IN_FLIGHT_LEASE_MS, f as MUTATION_IN_FLIGHT_RETRY_AFTER_MS, r as MutationConnectionRequired, d as MutationInFlight, i as beginIdempotent, c as failIdempotent, s as finishIdempotent, u as freshFor, o as holdIdempotentClaim, t as idempotencyScope, l as memoryIdempotencyStore, n as wsMutationIdempotencyScope };
121
+ export { a as DEFAULT_IN_FLIGHT_LEASE_MS, p as MUTATION_IN_FLIGHT_RETRY_AFTER_MS, r as MutationConnectionRequired, m as MutationIdempotencyRefused, f as MutationInFlight, i as beginIdempotent, c as failIdempotent, s as finishIdempotent, d as freshFor, o as holdIdempotentClaim, t as idempotencyScope, u as memoryIdempotencyStore, n as wsMutationIdempotencyScope };
@@ -0,0 +1,123 @@
1
+ import { canonicalJson as e } from "./canonicalJson.js";
2
+ import { Schema as t } from "effect";
3
+ //#region src/idempotencyInput.ts
4
+ var n = (t) => {
5
+ let n = /* @__PURE__ */ new WeakMap(), r = 0, i = (e, t = () => !1) => {
6
+ if (Object.getOwnPropertySymbols(e).length) throw Error("Unsupported wire fields");
7
+ return Object.getOwnPropertyNames(e).sort().filter((e) => !t(e)).map((t) => {
8
+ let n = Object.getOwnPropertyDescriptor(e, t);
9
+ if (!("value" in n)) throw Error("Unsupported wire accessor");
10
+ return [t, o(n.value)];
11
+ });
12
+ }, a = (e, t = 0, n = e.byteLength) => {
13
+ let r = "";
14
+ for (let i of new Uint8Array(e, t, n)) r += i.toString(16).padStart(2, "0");
15
+ return r;
16
+ }, o = (e) => {
17
+ if (e === null) return ["null"];
18
+ if (e === void 0) return ["undefined"];
19
+ if (typeof e == "string" || typeof e == "boolean") return [typeof e, e];
20
+ if (typeof e == "number") return ["number", Object.is(e, -0) ? "-0" : String(e)];
21
+ if (typeof e == "bigint") return ["bigint", String(e)];
22
+ if (typeof e != "object") throw Error("Unsupported wire value");
23
+ let t = n.get(e);
24
+ if (t !== void 0) return ["reference", t];
25
+ let s = r++;
26
+ if (n.set(e, s), Array.isArray(e)) {
27
+ if (Object.getPrototypeOf(e) !== Array.prototype) throw Error("Unsupported wire array prototype");
28
+ return [
29
+ "array",
30
+ s,
31
+ e.length,
32
+ i(e, (e) => e === "length")
33
+ ];
34
+ }
35
+ if (Object.getPrototypeOf(e) === Date.prototype) return [
36
+ "date",
37
+ s,
38
+ String(Date.prototype.getTime.call(e)),
39
+ i(e)
40
+ ];
41
+ if (Object.getPrototypeOf(e) === Map.prototype) return [
42
+ "map",
43
+ s,
44
+ Array.from(Map.prototype.entries.call(e), ([e, t]) => [o(e), o(t)]),
45
+ i(e)
46
+ ];
47
+ if (Object.getPrototypeOf(e) === Set.prototype) return [
48
+ "set",
49
+ s,
50
+ Array.from(Set.prototype.values.call(e), o),
51
+ i(e)
52
+ ];
53
+ if (Object.getPrototypeOf(e) === ArrayBuffer.prototype) return [
54
+ "buffer",
55
+ s,
56
+ a(e),
57
+ i(e)
58
+ ];
59
+ if (ArrayBuffer.isView(e)) {
60
+ if (!(e.buffer instanceof ArrayBuffer)) throw Error("Shared wire memory is not a snapshot");
61
+ let t = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(e), "constructor")?.value;
62
+ if (typeof t != "function" || ![
63
+ "DataView",
64
+ "Int8Array",
65
+ "Uint8Array",
66
+ "Uint8ClampedArray",
67
+ "Int16Array",
68
+ "Uint16Array",
69
+ "Int32Array",
70
+ "Uint32Array",
71
+ "Float16Array",
72
+ "Float32Array",
73
+ "Float64Array",
74
+ "BigInt64Array",
75
+ "BigUint64Array"
76
+ ].includes(t.name) || globalThis[t.name] !== t) throw Error("Unsupported wire view prototype");
77
+ return [
78
+ "view",
79
+ s,
80
+ t.name,
81
+ o(e.buffer),
82
+ e.byteOffset,
83
+ e.byteLength,
84
+ i(e, (t) => !(e instanceof DataView) && /^(0|[1-9]\d*)$/.test(t))
85
+ ];
86
+ }
87
+ let c = Object.getPrototypeOf(e);
88
+ if (c !== null && c !== Object.prototype) throw Error("Unsupported wire object");
89
+ return [
90
+ "object",
91
+ s,
92
+ i(e)
93
+ ];
94
+ };
95
+ return e(o(t));
96
+ }, r = async (r, i, a, o = void 0) => {
97
+ try {
98
+ let s = t.encodeUnknownSync(r), c = t.decodeUnknownSync(r), l = s(i), u = n(l), d = c(structuredClone(l));
99
+ if (n(s(d)) !== u) throw Error("Input does not round trip");
100
+ let f = e({
101
+ protocol: "voltro-idempotency-input-v1",
102
+ principal: {
103
+ type: a.type,
104
+ id: a.id,
105
+ tenantId: a.tenantId,
106
+ kind: a.type === "user" ? a.kind ?? null : null,
107
+ service: a.type === "service" ? a.service : null,
108
+ auditActor: a.type === "serviceAccount" ? a.auditActorId === void 0 ? ["implicit"] : ["explicit", a.auditActorId] : null
109
+ },
110
+ wire: u,
111
+ transport: n(o)
112
+ }), p = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(f)), m = "";
113
+ for (let e of new Uint8Array(p)) m += e.toString(16).padStart(2, "0");
114
+ return Object.freeze({
115
+ input: d,
116
+ fingerprint: m
117
+ });
118
+ } catch {
119
+ throw Error("Mutation input cannot be captured for idempotent execution");
120
+ }
121
+ };
122
+ //#endregion
123
+ export { r as prepareIdempotentInput };