@voltro/protocol 0.83.1 → 0.85.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.
@@ -1,34 +1,34 @@
1
- import { Schema as e } from "effect";
2
- import * as t from "effect/SchemaAST";
3
- import * as n from "effect/Option";
1
+ import { Schema as e, SchemaGetter as t, SchemaTransformation as n } from "effect";
2
+ import { RpcSchema as r } from "effect/rpc";
3
+ import * as i from "effect/SchemaAST";
4
4
  //#region src/idempotency.ts
5
- var r = (e, t, n) => `${e ?? "(global)"}|${t.toUpperCase()}|${n}`, i = (e, t, n, r) => r === void 0 ? `${e ?? "(global)"}|${t ?? "(anon)"}|WS|${n}` : `WS-connection:${JSON.stringify([
5
+ var a = (e, t, n) => `${e ?? "(global)"}|${t.toUpperCase()}|${n}`, o = (e, t, n, r) => r === void 0 ? `${e ?? "(global)"}|${t ?? "(anon)"}|WS|${n}` : `WS-connection:${JSON.stringify([
6
6
  e ?? null,
7
7
  t ?? null,
8
8
  n,
9
9
  r
10
- ])}`, a = class extends e.TaggedError()("MutationConnectionRequired", { traceId: e.String }) {
10
+ ])}`, s = class extends e.TaggedError()("MutationConnectionRequired", { traceId: e.String }) {
11
11
  get message() {
12
12
  return "this mutation requires a live native WebSocket connection";
13
13
  }
14
- }, o = async (e, t, n, r, i, a = s) => {
14
+ }, c = async (e, t, n, r, i, a = l) => {
15
15
  let o = await e.claim(t, n, i, r, a);
16
16
  return o === "claimed" ? { kind: "fresh" } : o.status === "completed" && o.response ? {
17
17
  kind: "replay",
18
18
  response: o.response
19
19
  } : { kind: "conflict" };
20
- }, s = 3e4, c = (e, t, n, r = s, i = Date.now) => {
20
+ }, l = 3e4, u = (e, t, n, r = l, i = Date.now) => {
21
21
  let a = setInterval(() => {
22
22
  e.renew(t, n, i()).catch(() => {});
23
23
  }, Math.max(1, Math.floor(r / 3)));
24
24
  return a.unref?.(), () => clearInterval(a);
25
- }, l = (e, t, n, r, i) => e.complete(t, n, r, i), u = (e, t, n) => e.release(t, n), d = () => {
25
+ }, d = (e, t, n, r, i) => e.complete(t, n, r, i), f = (e, t, n) => e.release(t, n), p = () => {
26
26
  let e = /* @__PURE__ */ new Map(), t = (e, t) => `${e}\u0000${t}`;
27
27
  return {
28
28
  get: async (n, r) => e.get(t(n, r)) ?? null,
29
29
  claim: async (n, r, i, a, o) => {
30
30
  let s = e.get(t(n, r));
31
- return s && i - s.createdAt < f(s.status, a, o) ? s : (e.set(t(n, r), {
31
+ return s && i - s.createdAt < m(s.status, a, o) ? s : (e.set(t(n, r), {
32
32
  scope: n,
33
33
  key: r,
34
34
  status: "in_flight",
@@ -56,59 +56,66 @@ var r = (e, t, n) => `${e ?? "(global)"}|${t.toUpperCase()}|${n}`, i = (e, t, n,
56
56
  });
57
57
  }
58
58
  };
59
- }, f = (e, t, n) => e === "completed" ? t : Math.min(t, n), p = class extends e.TaggedError()("MutationInFlight", {
59
+ }, m = (e, t, n) => e === "completed" ? t : Math.min(t, n), h = class extends e.TaggedError()("MutationInFlight", {
60
60
  traceId: e.String,
61
61
  retryAfterMs: e.Number
62
62
  }) {
63
63
  get message() {
64
64
  return `a request with this idempotency key is still in flight — replay the same key after ${this.retryAfterMs}ms`;
65
65
  }
66
- }, m = 1e3, h = (e) => typeof e == "object" && !!e && typeof e._tag == "string", g = 64, _ = (e, t = 0) => {
67
- if (!h(e)) return String(e);
68
- if (t >= g) return "…";
69
- let n = t + 1, r = (t) => _(e[t], n), i = (t) => Array.isArray(e[t]) ? e[t] : [];
70
- switch (e._tag) {
71
- case "Transformation": return `T(${r("from")}->${r("to")})`;
72
- case "Refinement": return `R(${r("from")})`;
73
- case "Suspend": return "S(…)";
74
- case "TypeLiteral": {
75
- let e = i("propertySignatures").map((e) => {
76
- let t = e;
77
- return `${String(t.name)}${t.isOptional === !0 ? "?" : ""}:${_(t.type, n)}`;
78
- }).join(","), t = i("indexSignatures").map((e) => {
79
- let t = e;
80
- return `[${_(t.parameter, n)}]:${_(t.type, n)}`;
81
- }).join(",");
82
- return t === "" ? `{${e}}` : `{${e};${t}}`;
83
- }
84
- case "Union": return `U(${i("types").map((e) => _(e, n)).join("|")})`;
85
- case "TupleType": {
86
- let e = i("elements").map((e) => {
87
- let t = e;
88
- return `${_(t.type, n)}${t.isOptional === !0 ? "?" : ""}`;
89
- }).join(","), t = i("rest").map((e) => _(e.type, n)).join(",");
90
- return `A(${e}${t === "" ? "" : `...${t}`})`;
66
+ }, g = 1e3, _ = (e) => typeof e == "object" && !!e && typeof e._tag == "string", v = 64, y = (e, t = 0) => {
67
+ if (!_(e)) return String(e);
68
+ if (t >= v) return "…";
69
+ let n = t + 1, r = (t) => Array.isArray(e[t]) ? e[t] : [], i = r("encoding").map((e) => y(e.to, n)).join("->"), a = r("checks").length, o = (e) => _(e) && e.context?.isOptional === !0, s = () => {
70
+ switch (e._tag) {
71
+ case "Suspend": return "S(…)";
72
+ case "Objects": {
73
+ let e = r("propertySignatures").map((e) => {
74
+ let t = e;
75
+ return `${String(t.name)}${o(t.type) ? "?" : ""}:${y(t.type, n)}`;
76
+ }).join(","), t = r("indexSignatures").map((e) => {
77
+ let t = e;
78
+ return `[${y(t.parameter, n)}]:${y(t.type, n)}`;
79
+ }).join(",");
80
+ return t === "" ? `{${e}}` : `{${e};${t}}`;
81
+ }
82
+ case "Union": return `U(${r("types").map((e) => y(e, n)).join("|")})`;
83
+ case "Arrays": {
84
+ let e = r("elements").map((e) => `${y(e, n)}${o(e) ? "?" : ""}`).join(","), t = r("rest").map((e) => y(e, n)).join(",");
85
+ return `A(${e}${t === "" ? "" : `...${t}`})`;
86
+ }
87
+ case "Declaration": return `D(${r("typeParameters").map((e) => y(e, n)).join(",")})`;
88
+ case "Literal": return `L(${String(e.literal)})`;
89
+ case "Enum": return `E(${r("enums").map((e) => e.map(String).join("=")).join(",")})`;
90
+ case "TemplateLiteral": return `TL(${r("parts").map((e) => y(e, n)).join(",")})`;
91
+ case "UniqueSymbol": return `Y(${String(e.symbol)})`;
92
+ default: return String(e);
91
93
  }
92
- case "Declaration": return `D(${i("typeParameters").map((e) => _(e, n)).join(",")})`;
93
- default: return String(e);
94
- }
95
- }, v = (e) => {
94
+ }, c = a > 0 ? `R${a}(${s()})` : s();
95
+ return i === "" ? c : `T(${i}->${c})`;
96
+ }, b = (e) => {
96
97
  let t = 2166136261;
97
98
  for (let n = 0; n < e.length; n++) t ^= e.charCodeAt(n), t = Math.imul(t, 16777619) >>> 0;
98
99
  return t.toString(16).padStart(8, "0");
99
- }, y = (e) => v([
100
- _(e.payloadSchema?.ast),
101
- _(e.successSchema?.ast),
102
- _(e.errorSchema?.ast)
103
- ].join("\0")), b = (e) => {
100
+ }, x = (e) => {
101
+ if (e !== void 0 && r.isStreamSchema(e)) {
102
+ let t = e;
103
+ return `S(${y(t.success.ast)}|${y(t.error.ast)})`;
104
+ }
105
+ return y(e?.ast);
106
+ }, S = (e) => b([
107
+ y(e.payloadSchema?.ast),
108
+ x(e.successSchema),
109
+ y(e.errorSchema?.ast)
110
+ ].join("\0")), C = (e) => {
104
111
  let t = {}, n = e.requests, r = n instanceof Map ? n.entries() : Array.isArray(n) ? n.map((e, t) => [t, e]) : [];
105
112
  for (let [, e] of r) {
106
113
  if (typeof e != "object" && typeof e != "function" || e === null) continue;
107
114
  let n = e;
108
- typeof n._tag == "string" && (t[n._tag] = y(n));
115
+ typeof n._tag == "string" && (t[n._tag] = S(n));
109
116
  }
110
117
  return t;
111
- }, x = (e, t) => {
118
+ }, w = (e, t) => {
112
119
  let n = [], r = [];
113
120
  for (let [i, a] of Object.entries(t)) {
114
121
  let t = e[i];
@@ -118,18 +125,18 @@ var r = (e, t, n) => `${e ?? "(global)"}|${t.toUpperCase()}|${n}`, i = (e, t, n,
118
125
  differing: n.sort(),
119
126
  missingHere: r.sort()
120
127
  };
121
- }, S = (e) => {
128
+ }, T = (e) => {
122
129
  let t = e.error, n = t instanceof Error ? t.name : typeof t?._tag == "string" ? String(t._tag) : "Error", r = [e.name === void 0 ? e.source : `${e.source} '${e.name}'`, ...typeof e.fields?.runId == "string" ? [`run ${e.fields.runId}`] : []].join(", "), i = /* @__PURE__ */ Error(`[message withheld: ${r} is declared encrypted — read it where the run is shown]`);
123
130
  i.name = n;
124
131
  let a = t instanceof Error && typeof t.stack == "string" ? t.stack : "", o = a.search(/^\s+at /m);
125
132
  return i.stack = `${n}: ${i.message}${o === -1 ? "" : `\n${a.slice(o)}`}`, i;
126
- }, C = /* @__PURE__ */ new Set(), w = (e) => {
127
- for (let t of C) try {
133
+ }, E = /* @__PURE__ */ new Set(), D = (e) => {
134
+ for (let t of E) try {
128
135
  t(e);
129
136
  } catch {}
130
- }, T = (e) => (C.add(e), () => {
131
- C.delete(e);
132
- }), E = {
137
+ }, O = (e) => (E.add(e), () => {
138
+ E.delete(e);
139
+ }), k = {
133
140
  rpc: {
134
141
  name: "rpc",
135
142
  dates: "epoch-ms",
@@ -163,16 +170,16 @@ var r = (e, t, n) => `${e ?? "(global)"}|${t.toUpperCase()}|${n}`, i = (e, t, n,
163
170
  naming: "camelCase",
164
171
  etag: !0
165
172
  }
166
- }, D = [
173
+ }, A = [
167
174
  "rpc",
168
175
  "standard",
169
176
  "jsonapi"
170
- ], O = (e, t = void 0) => {
177
+ ], j = (e, t = void 0) => {
171
178
  let n = (e) => {
172
179
  if (e?.profile !== void 0) {
173
180
  if (typeof e.profile == "string") {
174
- let t = E[e.profile];
175
- if (t === void 0) throw Error(`publicApi.profile: unknown profile '${String(e.profile)}' — one of ${D.join(", ")}, or a RestFormat object`);
181
+ let t = k[e.profile];
182
+ if (t === void 0) throw Error(`publicApi.profile: unknown profile '${String(e.profile)}' — one of ${A.join(", ")}, or a RestFormat object`);
176
183
  return t;
177
184
  }
178
185
  return e.profile;
@@ -187,78 +194,65 @@ var r = (e, t, n) => `${e ?? "(global)"}|${t.toUpperCase()}|${n}`, i = (e, t, n,
187
194
  ...t.cursorParam === void 0 ? {} : { cursorParam: t.cursorParam },
188
195
  ...t.naming === void 0 ? {} : { naming: t.naming },
189
196
  ...t.etag === void 0 ? {} : { etag: t.etag }
190
- }, i = r(n(e) ?? E.rpc, e);
197
+ }, i = r(n(e) ?? k.rpc, e);
191
198
  return r(n(t) ?? i, t);
192
- }, k = (e) => e.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/([A-Z])([A-Z][a-z])/g, "$1_$2").toLowerCase(), A = (e, t) => {
199
+ }, M = (e) => e.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/([A-Z])([A-Z][a-z])/g, "$1_$2").toLowerCase(), N = (e, t) => {
193
200
  if (typeof e != "object" || !e || Array.isArray(e)) return e;
194
201
  let n = {};
195
202
  for (let [r, i] of Object.entries(e)) n[t(r)] = i;
196
203
  return n;
197
- }, j = (e) => n.getOrUndefined(t.getIdentifierAnnotation(e)), M = (n, r) => {
204
+ }, P = (e) => i.isDeclaration(e) && e.encoding !== void 0 && e.annotations?.representation?.id === "effect/schema/Date" && i.isNumber(i.toEncoded(e)), F = e.String.annotate({
205
+ identifier: "DateFromString",
206
+ format: "date-time"
207
+ }).pipe(e.decodeTo(e.Date, n.dateFromString)), I = (n, r) => {
198
208
  if (r.dates === "epoch-ms" && r.naming === "camelCase") return n;
199
- let i = /* @__PURE__ */ new Map(), a = (e) => {
200
- let t = i.get(e);
209
+ let a = /* @__PURE__ */ new Map(), o = (e) => {
210
+ let t = a.get(e);
201
211
  if (t !== void 0) return t;
202
- let n = o(e);
203
- return i.set(e, n), n;
204
- }, o = (n) => {
205
- switch (n._tag) {
206
- case "Transformation":
207
- if (r.dates === "iso" && j(n) === "DateFromNumber") {
208
- let r = e.DateFromString.ast;
209
- return new t.Transformation(r.from, r.to, r.transformation, {
210
- ...r.annotations,
211
- ...n.annotations,
212
- [t.IdentifierAnnotationId]: "DateFromString",
213
- [t.JSONSchemaAnnotationId]: {
214
- type: "string",
215
- format: "date-time"
216
- }
217
- });
218
- }
219
- return n;
220
- case "Refinement": return new t.Refinement(a(n.from), n.filter, n.annotations);
221
- case "Union": return t.Union.make(n.types.map(a), n.annotations);
222
- case "TupleType": return new t.TupleType(n.elements.map((e) => new t.OptionalType(a(e.type), e.isOptional, e.annotations)), n.rest.map((e) => new t.Type(a(e.type), e.annotations)), n.isReadonly, n.annotations);
223
- case "Suspend": return new t.Suspend(() => a(n.f()), n.annotations);
224
- case "TypeLiteral": {
225
- let i = n.propertySignatures.map((e) => new t.PropertySignature(e.name, a(e.type), e.isOptional, e.isReadonly, e.annotations)), o = n.indexSignatures.map((e) => new t.IndexSignature(e.parameter, a(e.type), e.isReadonly)), s = new t.TypeLiteral(i, o, n.annotations);
226
- if (r.naming !== "snake_case") return s;
227
- let c = i.map((e) => typeof e.name == "string" ? k(e.name) : e.name);
228
- if (c.every((e, t) => e === i[t].name)) return s;
229
- let l = new t.TypeLiteral(i.map((e, n) => new t.PropertySignature(c[n], t.encodedAST(e.type), e.isOptional, e.isReadonly, e.annotations)), o.map((e) => new t.IndexSignature(e.parameter, t.encodedAST(e.type), e.isReadonly)), n.annotations), u = new Map(i.map((e, t) => [String(c[t]), String(e.name)])), d = new Map(i.map((e, t) => [String(e.name), String(c[t])]));
230
- return e.transform(e.make(l), e.make(s), {
231
- strict: !1,
232
- decode: (e) => A(e, (e) => u.get(e) ?? e),
233
- encode: (e) => A(e, (e) => d.get(e) ?? e)
234
- }).ast;
235
- }
236
- default: return n;
212
+ let n = s(e);
213
+ return a.set(e, n), n;
214
+ }, s = (n) => {
215
+ if (r.dates === "iso" && P(n)) return F.annotate(n.annotations ?? {}).ast;
216
+ if (n.encoding !== void 0) return n;
217
+ if (i.isUnion(n)) return new i.Union(n.types.map(o), n.options, n.annotations, n.checks, void 0, n.context);
218
+ if (i.isArrays(n)) return new i.Arrays(n.isMutable, n.elements.map(o), n.rest.map(o), n.annotations, n.checks, void 0, n.context);
219
+ if (i.isSuspend(n)) return new i.Suspend(() => o(n.thunk()), n.annotations, n.checks, void 0, n.context);
220
+ if (i.isObjects(n)) {
221
+ let a = n.propertySignatures.map((e) => new i.PropertySignature(e.name, o(e.type))), s = n.indexSignatures.map((e) => new i.IndexSignature(e.parameter, o(e.type))), c = new i.Objects(a, s, n.annotations, n.checks, void 0, n.context);
222
+ if (r.naming !== "snake_case") return c;
223
+ let l = a.map((e) => typeof e.name == "string" ? M(e.name) : e.name);
224
+ if (l.every((e, t) => e === a[t].name)) return c;
225
+ let u = new i.Objects(a.map((e, t) => new i.PropertySignature(l[t], i.toEncoded(e.type))), s.map((e) => new i.IndexSignature(e.parameter, i.toEncoded(e.type))), n.annotations, void 0, void 0, n.context), d = new Map(a.map((e, t) => [String(l[t]), String(e.name)])), f = new Map(a.map((e, t) => [String(e.name), String(l[t])]));
226
+ return e.make(u).pipe(e.decodeTo(e.make(c), {
227
+ decode: t.transform((e) => N(e, (e) => d.get(e) ?? e)),
228
+ encode: t.transform((e) => N(e, (e) => f.get(e) ?? e))
229
+ })).ast;
237
230
  }
231
+ return n;
238
232
  };
239
- return e.make(a(n.ast));
240
- }, N = (e) => `urn:voltro:error:${e}`, P = (e) => e.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/([A-Z])([A-Z][a-z])/g, "$1 $2"), F = (e) => ({
241
- type: N(e.tag),
242
- title: P(e.tag),
233
+ return e.make(o(n.ast));
234
+ }, L = (e) => `urn:voltro:error:${e}`, R = (e) => e.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/([A-Z])([A-Z][a-z])/g, "$1 $2"), z = (e) => ({
235
+ type: L(e.tag),
236
+ title: R(e.tag),
243
237
  status: e.status,
244
238
  ...e.message.length > 0 ? { detail: e.message } : {},
245
239
  ...e.fields
246
- }), I = "application/problem+json; charset=utf-8", L = "application/vnd.api+json; charset=utf-8", R = (e, t, n) => e.encodeError === void 0 ? e.errors === "problem-details" ? e.envelope === "jsonapi" ? {
240
+ }), B = "application/problem+json; charset=utf-8", V = "application/vnd.api+json; charset=utf-8", H = (e, t, n) => e.encodeError === void 0 ? e.errors === "problem-details" ? e.envelope === "jsonapi" ? {
247
241
  body: { errors: [{
248
242
  status: String(t.status),
249
243
  code: t.tag,
250
- title: P(t.tag),
244
+ title: R(t.tag),
251
245
  ...t.message.length > 0 ? { detail: t.message } : {},
252
246
  ...Object.keys(t.fields).length > 0 ? { meta: t.fields } : {}
253
247
  }] },
254
- contentType: L
248
+ contentType: V
255
249
  } : {
256
- body: F(t),
257
- contentType: I
250
+ body: z(t),
251
+ contentType: B
258
252
  } : { body: {
259
253
  error: t.tag === "InvalidRequest" || t.tag === "InternalServerError" || t.tag === "Unauthorized" || t.tag === "Forbidden" || t.tag === "Gone" || t.tag === "MethodNotAllowed" || t.tag === "Conflict" ? t.message : t.tag,
260
254
  ...t.fields
261
- } } : e.encodeError(t, n), z = (e, t) => {
255
+ } } : e.encodeError(t, n), U = (e, t) => {
262
256
  if (typeof e != "object" || !e || Array.isArray(e)) return {
263
257
  cursor: void 0,
264
258
  rest: e
@@ -271,10 +265,10 @@ var r = (e, t, n) => `${e ?? "(global)"}|${t.toUpperCase()}|${n}`, i = (e, t, n,
271
265
  cursor: String(n),
272
266
  rest: e
273
267
  };
274
- }, B = (e, t, n) => {
268
+ }, W = (e, t, n) => {
275
269
  let r = new URLSearchParams(e.query);
276
270
  return r.set(n, t), `<${e.path}?${r.toString()}>; rel="next"`;
277
- }, V = (e, t) => {
271
+ }, G = (e, t) => {
278
272
  if (typeof e != "object" || !e || Array.isArray(e)) return e;
279
273
  let { id: n, ...r } = e;
280
274
  return {
@@ -282,9 +276,9 @@ var r = (e, t, n) => `${e ?? "(global)"}|${t.toUpperCase()}|${n}`, i = (e, t, n,
282
276
  ...n === void 0 ? {} : { id: String(n) },
283
277
  attributes: r
284
278
  };
285
- }, H = (e, t, n) => {
279
+ }, K = (e, t, n) => {
286
280
  if (e.encodeSuccess !== void 0) return e.encodeSuccess(t, n);
287
- let { cursor: r } = z(t, e.cursorField), i = e.pagination === "link-header" && r !== void 0 ? { Link: B(n, r, e.cursorParam) } : void 0, a = (e, t) => ({
281
+ let { cursor: r } = U(t, e.cursorField), i = e.pagination === "link-header" && r !== void 0 ? { Link: W(n, r, e.cursorParam) } : void 0, a = (e, t) => ({
288
282
  body: e,
289
283
  ...i ? { headers: i } : {},
290
284
  ...t ? { contentType: t } : {}
@@ -296,9 +290,9 @@ var r = (e, t, n) => `${e ?? "(global)"}|${t.toUpperCase()}|${n}`, i = (e, t, n,
296
290
  });
297
291
  let o = n.source ?? n.tag.split(".")[0] ?? "resource", s = Array.isArray(t) ? t : typeof t == "object" && t && Array.isArray(t.items) ? t.items : void 0;
298
292
  return a({
299
- data: s === void 0 ? V(t, o) : s.map((e) => V(e, o)),
300
- ...r === void 0 ? {} : { links: { next: B(n, r, e.cursorParam).replace(/^<|>; rel="next"$/g, "") } }
301
- }, L);
293
+ data: s === void 0 ? G(t, o) : s.map((e) => G(e, o)),
294
+ ...r === void 0 ? {} : { links: { next: W(n, r, e.cursorParam).replace(/^<|>; rel="next"$/g, "") } }
295
+ }, V);
302
296
  };
303
297
  //#endregion
304
- export { d as A, p as C, f as D, l as E, c as O, a as S, u as T, y as _, H as a, s as b, N as c, k as d, w as f, b as g, x as h, R as i, i as j, r as k, O as l, S as m, I as n, B as o, T as p, D as r, F as s, L as t, M as u, _ as v, o as w, m as x, v as y };
298
+ export { p as A, h as C, m as D, d as E, u as O, s as S, f as T, S as _, K as a, l as b, L as c, M as d, D as f, C as g, w as h, H as i, o as j, a as k, j as l, T as m, B as n, W as o, O as p, A as r, z as s, V as t, I as u, y as v, c as w, g as x, b as y };