@voltro/protocol 0.32.0 → 0.34.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,90 +0,0 @@
1
- import { Context as e, Schema as t } from "effect";
2
- import { RpcMiddleware as n } from "@effect/rpc";
3
- //#region src/auth.ts
4
- var r = t.Record({
5
- key: t.String,
6
- value: t.Unknown
7
- }), i = t.optional(t.Array(t.String)), a = t.Struct({
8
- type: t.Literal("user"),
9
- id: t.String,
10
- tenantId: t.String,
11
- scopes: i,
12
- metadata: t.optional(r)
13
- }), o = t.Struct({
14
- type: t.Literal("apiKey"),
15
- id: t.String,
16
- tenantId: t.String,
17
- scopes: i,
18
- metadata: t.optional(r)
19
- }), s = t.Struct({
20
- type: t.Literal("serviceAccount"),
21
- id: t.String,
22
- tenantId: t.String,
23
- scopes: i,
24
- metadata: t.optional(r)
25
- }), c = t.Struct({
26
- type: t.Literal("anonymous"),
27
- id: t.Null,
28
- tenantId: t.NullOr(t.String)
29
- }), l = t.Struct({
30
- type: t.Literal("system"),
31
- id: t.String,
32
- tenantId: t.Null,
33
- scopes: i,
34
- metadata: t.optional(r)
35
- }), u = t.Union(a, o, s, c, l), d = (e) => ({
36
- type: "anonymous",
37
- id: null,
38
- tenantId: e
39
- }), f = (e, t = ["admin:full"]) => ({
40
- type: "system",
41
- id: e,
42
- tenantId: null,
43
- scopes: t
44
- }), p = (e, t) => {
45
- let n = e;
46
- return {
47
- type: "serviceAccount",
48
- id: e.id ?? "system",
49
- tenantId: t,
50
- scopes: n.scopes ?? ["admin:full"],
51
- ...n.metadata === void 0 ? {} : { metadata: n.metadata }
52
- };
53
- }, m = (e) => e.type === "system", h = class extends e.Tag("@voltro/Subject")() {}, g = class extends e.Tag("@voltro/ConnectionInfo")() {}, _ = class extends n.Tag()("@voltro/AuthMiddleware", { provides: h }) {}, v = class extends n.Tag()("@voltro/ConnectionInfoMiddleware", { provides: g }) {}, y = (e) => typeof e.mountRoutes == "function", b = (e, t) => {
54
- if (t.length === 0) return e;
55
- let n = [.../* @__PURE__ */ new Set([...e.scopes ?? [], ...t])];
56
- return {
57
- ...e,
58
- scopes: n
59
- };
60
- }, x = (e, t) => async (n) => {
61
- let r = t?.getStore?.(), i = r === void 0 ? n : {
62
- ...n,
63
- store: r
64
- };
65
- for (let n of e) {
66
- let e = await n.resolve(i);
67
- if (e.kind === "matched") {
68
- let n = t?.resolveScopes === void 0 ? e.subject : b(e.subject, await t.resolveScopes(e.subject, i));
69
- return e.credentialExpiresAt === void 0 ? { subject: n } : {
70
- subject: n,
71
- credentialExpiresAt: e.credentialExpiresAt
72
- };
73
- }
74
- if (e.kind === "failed") {
75
- t?.onStrategyFailed?.({
76
- strategyId: n.id,
77
- reason: e.reason
78
- });
79
- break;
80
- }
81
- }
82
- if (t?.fallback) return { subject: await t.fallback(i) };
83
- let a = i.headers["x-tenant"] ?? null;
84
- if (a === null && t?.anonymousTenantRequired === !0) throw new S({ reason: "tenant required (x-tenant header missing)" });
85
- return { subject: d(a) };
86
- }, S = class extends t.TaggedError()("Unauthenticated", { reason: t.optional(t.String) }) {}, C = (e, t) => {
87
- if (e.type === "anonymous") throw new S(t === void 0 ? {} : { reason: t });
88
- };
89
- //#endregion
90
- export { h as a, C as c, m as d, f, u as i, x as l, g as n, S as o, p, v as r, d as s, _ as t, y as u };
@@ -1,157 +0,0 @@
1
- import { Effect as e, Schema as t } from "effect";
2
- //#region src/scopes.ts
3
- var n = "admin:full", r = "apikeys:issue:self", i = "apikeys:issue:org", a = "apikeys:issue:other", o = class extends t.TaggedError()("ScopeError", {
4
- required: t.String,
5
- message: t.String
6
- }) {}, s = (e) => "scopes" in e && e.scopes ? e.scopes : [], c = /* @__PURE__ */ new WeakMap(), l = (e, t) => {
7
- typeof e == "object" && e && c.set(e, t);
8
- }, u = (e) => {
9
- if (typeof e == "object" && e) {
10
- let t = c.get(e);
11
- if (t) return t;
12
- }
13
- return s(e);
14
- }, d = (e, t) => {
15
- let n = u(e);
16
- return n.includes("admin:full") || n.includes(t);
17
- }, f = (e, t) => {
18
- let n = s(e);
19
- return n.includes("admin:full") || n.includes(t);
20
- }, p = (t, n) => d(t, n) ? e.void : e.fail(new o({
21
- required: n,
22
- message: `missing required scope '${n}'`
23
- })), m = (e) => typeof e.action == "string" && typeof e.resourceType == "string", h = (e) => typeof e == "string" ? [e] : e, g, _ = (e) => {
24
- g = e;
25
- }, v = (e) => g === void 0 ? e.filter((e) => (e.guards ?? []).some((e) => !m(e) && e.resource !== void 0)).map((e) => e.tag) : [], y = (e) => [
26
- `${e.length} procedure${e.length === 1 ? "" : "s"} declare a per-resource guard that nothing enforces:`,
27
- ...e.slice(0, 10).map((e) => ` ${e}`),
28
- ...e.length > 10 ? [` … and ${e.length - 10} more`] : [],
29
- "",
30
- " `guards: [{ scope, resource }]` reads as \"may you do this to THIS resource\",",
31
- " but with no resource-scope resolver registered the extractor is advisory and",
32
- " the check runs against the caller's GLOBAL scopes. For an app whose",
33
- " authorization is per-resource, that guard passes callers it should refuse.",
34
- "",
35
- " Fix it one of two ways:",
36
- " • setResourceScopeResolver((req) => Effect<boolean>) — answers \"does this",
37
- " subject hold `req.scope` on `req.resource`\" from your own tables.",
38
- " • Drop the `resource` extractor if a global scope check is what you meant."
39
- ].join("\n"), b = () => g, x, S = (e) => {
40
- x = e;
41
- }, C = () => x, w = (e, t) => {
42
- if (!t || t.length === 0) return null;
43
- for (let n of t) {
44
- if (m(n)) {
45
- if (d(e, "admin:full")) continue;
46
- return new o({
47
- required: `${n.resourceType}:${n.action}`,
48
- message: `relationship guard '${n.action}' on '${n.resourceType}' cannot be checked synchronously`
49
- });
50
- }
51
- let t = h(n.scope);
52
- if (t.length === 0) continue;
53
- let r = t.filter((t) => d(e, t));
54
- if (!((n.mode ?? "all") === "any" ? r.length > 0 : r.length === t.length)) {
55
- let r = (n.mode ?? "all") === "any" ? t[0] : t.find((t) => !d(e, t));
56
- return new o({
57
- required: r,
58
- message: (n.mode ?? "all") === "any" ? `missing all of required scopes [${t.join(", ")}]` : `missing required scope '${r}'`
59
- });
60
- }
61
- }
62
- return null;
63
- }, T = (t, n, r) => {
64
- let i = (e) => new o({
65
- required: `${n.resourceType}:${n.action}`,
66
- message: e
67
- });
68
- if (d(t, "admin:full")) return e.succeed(null);
69
- let a = x;
70
- if (a === void 0) return e.succeed(i(`no policy resolver registered — cannot authorize '${n.action}' on '${n.resourceType}'`));
71
- let s = n.resource(r);
72
- return s === void 0 || s === "" ? e.succeed(i(`input does not identify a '${n.resourceType}' to authorize`)) : e.suspend(() => a({
73
- subject: t,
74
- action: n.action,
75
- resourceType: n.resourceType,
76
- resourceId: s
77
- })).pipe(e.catchAllCause(() => e.succeed(!1)), e.map((e) => e ? null : i(`no '${n.action}' relation on ${n.resourceType}:${s}`)));
78
- }, E = (t, n, r) => {
79
- if (!n || n.length === 0) return e.succeed(null);
80
- let i = g;
81
- return !n.some(m) && (!i || !n.some((e) => e.resource !== void 0)) ? e.succeed(w(t, n)) : e.gen(function* () {
82
- for (let a of n) {
83
- if (m(a)) {
84
- let e = yield* T(t, a, r);
85
- if (e) return e;
86
- continue;
87
- }
88
- let n = h(a.scope);
89
- if (n.length === 0) continue;
90
- let s = a.resource ? a.resource(r) : void 0, c = [];
91
- for (let r of n) {
92
- if (d(t, r)) {
93
- c.push(!0);
94
- continue;
95
- }
96
- if (s !== void 0 && i !== void 0) {
97
- let n = yield* e.suspend(() => i({
98
- subject: t,
99
- scope: r,
100
- resource: s
101
- })).pipe(e.catchAllCause(() => e.succeed(!1)));
102
- c.push(n);
103
- } else c.push(!1);
104
- }
105
- let l = a.mode ?? "all";
106
- if (!(l === "any" ? c.some(Boolean) : c.every(Boolean))) {
107
- let e = l === "any" ? n[0] : n[n.findIndex((e, t) => !c[t])];
108
- return new o({
109
- required: e,
110
- message: l === "any" ? `missing all of required scopes [${n.join(", ")}]` : `missing required scope '${e}'` + (s === void 0 ? "" : ` on resource '${s}'`)
111
- });
112
- }
113
- }
114
- return null;
115
- });
116
- }, D = (e, t, n) => `${e ?? "(global)"}|${t.toUpperCase()}|${n}`, O = (e, t, n) => `${e ?? "(global)"}|${t ?? "(anon)"}|WS|${n}`, k = async (e, t, n, r, i) => {
117
- let a = await e.claim(t, n, i, r);
118
- return a === "claimed" ? { kind: "fresh" } : a.status === "completed" && a.response ? {
119
- kind: "replay",
120
- response: a.response
121
- } : { kind: "conflict" };
122
- }, A = (e, t, n, r, i) => e.complete(t, n, r, i), j = (e, t, n) => e.release(t, n), M = () => {
123
- let e = /* @__PURE__ */ new Map(), t = (e, t) => `${e}\u0000${t}`;
124
- return {
125
- get: async (n, r) => e.get(t(n, r)) ?? null,
126
- claim: async (n, r, i, a) => {
127
- let o = e.get(t(n, r));
128
- return o && i - o.createdAt < a ? o : (e.set(t(n, r), {
129
- scope: n,
130
- key: r,
131
- status: "in_flight",
132
- response: null,
133
- createdAt: i
134
- }), "claimed");
135
- },
136
- complete: async (n, r, i, a) => {
137
- e.set(t(n, r), {
138
- scope: n,
139
- key: r,
140
- status: "completed",
141
- response: i,
142
- createdAt: a
143
- });
144
- },
145
- release: async (n, r) => {
146
- e.delete(t(n, r));
147
- }
148
- };
149
- }, N = /* @__PURE__ */ new Set(), P = (e) => {
150
- for (let t of N) try {
151
- t(e);
152
- } catch {}
153
- }, F = (e) => (N.add(e), () => {
154
- N.delete(e);
155
- });
156
- //#endregion
157
- export { m as C, _ as D, S as E, s as O, f as S, l as T, u as _, A as a, b, O as c, a as d, r as f, E as g, w as h, j as i, n as l, y as m, F as n, D as o, o as p, k as r, M as s, P as t, i as u, v, p as w, d as x, C as y };