better-call 0.0.0-experimental.01ff4b77

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.
Files changed (115) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +265 -0
  3. package/dist/capability.cjs +417 -0
  4. package/dist/capability.cjs.map +1 -0
  5. package/dist/capability.d.cts +189 -0
  6. package/dist/capability.d.mts +189 -0
  7. package/dist/capability.mjs +401 -0
  8. package/dist/capability.mjs.map +1 -0
  9. package/dist/error.cjs +76 -0
  10. package/dist/error.cjs.map +1 -0
  11. package/dist/error.d.cts +51 -0
  12. package/dist/error.d.mts +51 -0
  13. package/dist/error.mjs +72 -0
  14. package/dist/error.mjs.map +1 -0
  15. package/dist/fn.cjs +350 -0
  16. package/dist/fn.cjs.map +1 -0
  17. package/dist/fn.d.cts +378 -0
  18. package/dist/fn.d.mts +378 -0
  19. package/dist/fn.mjs +350 -0
  20. package/dist/fn.mjs.map +1 -0
  21. package/dist/index.cjs +43 -0
  22. package/dist/index.cjs.map +1 -0
  23. package/dist/index.d.cts +50 -0
  24. package/dist/index.d.mts +50 -0
  25. package/dist/index.mjs +24 -0
  26. package/dist/index.mjs.map +1 -0
  27. package/dist/module.cjs +176 -0
  28. package/dist/module.cjs.map +1 -0
  29. package/dist/module.d.cts +283 -0
  30. package/dist/module.d.mts +283 -0
  31. package/dist/module.mjs +166 -0
  32. package/dist/module.mjs.map +1 -0
  33. package/dist/plugins/db.cjs +24 -0
  34. package/dist/plugins/db.cjs.map +1 -0
  35. package/dist/plugins/db.d.cts +18 -0
  36. package/dist/plugins/db.d.mts +18 -0
  37. package/dist/plugins/db.mjs +19 -0
  38. package/dist/plugins/db.mjs.map +1 -0
  39. package/dist/plugins/http/attrs.cjs +91 -0
  40. package/dist/plugins/http/attrs.cjs.map +1 -0
  41. package/dist/plugins/http/attrs.d.cts +26 -0
  42. package/dist/plugins/http/attrs.d.mts +26 -0
  43. package/dist/plugins/http/attrs.mjs +87 -0
  44. package/dist/plugins/http/attrs.mjs.map +1 -0
  45. package/dist/plugins/http/cookie.cjs +117 -0
  46. package/dist/plugins/http/cookie.cjs.map +1 -0
  47. package/dist/plugins/http/cookie.d.cts +637 -0
  48. package/dist/plugins/http/cookie.d.mts +637 -0
  49. package/dist/plugins/http/cookie.mjs +113 -0
  50. package/dist/plugins/http/cookie.mjs.map +1 -0
  51. package/dist/plugins/http/error.cjs +39 -0
  52. package/dist/plugins/http/error.cjs.map +1 -0
  53. package/dist/plugins/http/error.d.cts +38 -0
  54. package/dist/plugins/http/error.d.mts +38 -0
  55. package/dist/plugins/http/error.mjs +35 -0
  56. package/dist/plugins/http/error.mjs.map +1 -0
  57. package/dist/plugins/http/handle.cjs +73 -0
  58. package/dist/plugins/http/handle.cjs.map +1 -0
  59. package/dist/plugins/http/handle.d.cts +955 -0
  60. package/dist/plugins/http/handle.d.mts +955 -0
  61. package/dist/plugins/http/handle.mjs +72 -0
  62. package/dist/plugins/http/handle.mjs.map +1 -0
  63. package/dist/plugins/http/redirect.cjs +82 -0
  64. package/dist/plugins/http/redirect.cjs.map +1 -0
  65. package/dist/plugins/http/redirect.d.cts +67 -0
  66. package/dist/plugins/http/redirect.d.mts +67 -0
  67. package/dist/plugins/http/redirect.mjs +79 -0
  68. package/dist/plugins/http/redirect.mjs.map +1 -0
  69. package/dist/plugins/http/request.cjs +63 -0
  70. package/dist/plugins/http/request.cjs.map +1 -0
  71. package/dist/plugins/http/request.d.cts +142 -0
  72. package/dist/plugins/http/request.d.mts +142 -0
  73. package/dist/plugins/http/request.mjs +61 -0
  74. package/dist/plugins/http/request.mjs.map +1 -0
  75. package/dist/plugins/http/response.cjs +13 -0
  76. package/dist/plugins/http/response.cjs.map +1 -0
  77. package/dist/plugins/http/response.d.cts +18 -0
  78. package/dist/plugins/http/response.d.mts +18 -0
  79. package/dist/plugins/http/response.mjs +13 -0
  80. package/dist/plugins/http/response.mjs.map +1 -0
  81. package/dist/plugins/http.cjs +64 -0
  82. package/dist/plugins/http.cjs.map +1 -0
  83. package/dist/plugins/http.d.cts +945 -0
  84. package/dist/plugins/http.d.mts +945 -0
  85. package/dist/plugins/http.mjs +37 -0
  86. package/dist/plugins/http.mjs.map +1 -0
  87. package/dist/plugins/read-only.cjs +19 -0
  88. package/dist/plugins/read-only.cjs.map +1 -0
  89. package/dist/plugins/read-only.d.cts +17 -0
  90. package/dist/plugins/read-only.d.mts +17 -0
  91. package/dist/plugins/read-only.mjs +19 -0
  92. package/dist/plugins/read-only.mjs.map +1 -0
  93. package/dist/schema.cjs +245 -0
  94. package/dist/schema.cjs.map +1 -0
  95. package/dist/schema.d.cts +411 -0
  96. package/dist/schema.d.mts +411 -0
  97. package/dist/schema.mjs +235 -0
  98. package/dist/schema.mjs.map +1 -0
  99. package/dist/scope.d.cts +31 -0
  100. package/dist/scope.d.mts +31 -0
  101. package/dist/storage.cjs +308 -0
  102. package/dist/storage.cjs.map +1 -0
  103. package/dist/storage.d.cts +220 -0
  104. package/dist/storage.d.mts +220 -0
  105. package/dist/storage.mjs +302 -0
  106. package/dist/storage.mjs.map +1 -0
  107. package/dist/types.d.cts +8 -0
  108. package/dist/types.d.mts +8 -0
  109. package/dist/var.cjs +186 -0
  110. package/dist/var.cjs.map +1 -0
  111. package/dist/var.d.cts +46 -0
  112. package/dist/var.d.mts +46 -0
  113. package/dist/var.mjs +177 -0
  114. package/dist/var.mjs.map +1 -0
  115. package/package.json +114 -0
@@ -0,0 +1,189 @@
1
+ import { TypeDefination } from "./schema.cjs";
2
+ import { OnEntry } from "./module.cjs";
3
+ import { FnDefination } from "./fn.cjs";
4
+ import { VarDefination } from "./var.cjs";
5
+ //#region src/capability.d.ts
6
+ type Keypair = {
7
+ id: string;
8
+ sign: (payload: string) => Promise<string>;
9
+ };
10
+ declare const generateKeypair: () => Promise<Keypair>;
11
+ /** The name of a right: a fn key, optionally pinned to exact input
12
+ * fields. Never signed on its own - delegations carry caps, invocations
13
+ * spend them. */
14
+ type Cap = string | {
15
+ fn: string;
16
+ input?: Record<string, unknown>;
17
+ };
18
+ declare const fnOf: (cap: Cap) => string;
19
+ declare const fmtCap: (cap: Cap) => string;
20
+ /** Does holding `parent` justify holding `child`? Same fn, and every
21
+ * field the parent pins must be pinned identically by the child. */
22
+ declare const covers: (parent: Cap, child: Cap) => boolean;
23
+ /** Do these held caps authorize calling `fn` with `input`? */
24
+ declare const permits: (caps: Cap[], fn: string, input: unknown) => boolean;
25
+ type Delegation = {
26
+ typ: "dlg";
27
+ iss: string;
28
+ aud: string;
29
+ sub: string;
30
+ caps: Cap[];
31
+ exp: number;
32
+ prf?: Delegation;
33
+ sig: string;
34
+ };
35
+ type Invocation = {
36
+ typ: "inv";
37
+ iss: string;
38
+ call: string;
39
+ input?: unknown;
40
+ exp: number;
41
+ nonce: string;
42
+ prf: Delegation;
43
+ sig: string;
44
+ };
45
+ declare const mintDelegation: (issuer: Keypair, body: Omit<Delegation, "typ" | "iss" | "sig">) => Promise<Delegation>;
46
+ declare const mintInvocation: (holder: Keypair, call: string, proof: Delegation, input?: unknown) => Promise<Invocation>;
47
+ declare class CapabilityError extends Error {
48
+ constructor(message: string);
49
+ }
50
+ /** A crafted chain of self-signed links (valid until the root check at the
51
+ * very bottom) would otherwise force unbounded recursion and signature
52
+ * verifications - a cheap denial-of-service. Cap the depth. */
53
+ declare const MAX_DELEGATION_DEPTH = 16;
54
+ /** Walk the chain: every link signed, attenuating, unexpired, rooted. */
55
+ declare const verifyDelegation: (root: string, link: Delegation, depth?: number) => Promise<Delegation>;
56
+ declare const verifyInvocation: (root: string, token: Invocation) => Promise<{
57
+ subject: string;
58
+ caps: Cap[];
59
+ holder: string;
60
+ }>;
61
+ type Attestation = {
62
+ typ: "idt";
63
+ iss: string;
64
+ sub: string;
65
+ exp: number;
66
+ sig: string;
67
+ };
68
+ declare const verifyAttestation: (root: string, token: Attestation) => Promise<string>;
69
+ declare const capability: VarDefination<"capability", {
70
+ subject: string;
71
+ caps: Cap[];
72
+ holder: string;
73
+ entry: string | null;
74
+ } | null, undefined, never>;
75
+ declare const validateCaller: (fnKey: string) => OnEntry<string, unknown>;
76
+ type Challenge = {
77
+ id: string;
78
+ subject: string;
79
+ holder: string;
80
+ caps: Cap[];
81
+ goal?: string;
82
+ status: "pending" | "approved" | "denied";
83
+ };
84
+ /**
85
+ * Authority never checks a call - `validateCaller` owns that. It answers
86
+ * the questions that come BEFORE any call: what references does a caller
87
+ * start with, what proves WHO a caller acts for, and how are requests
88
+ * for more settled?
89
+ */
90
+ type Authority = {
91
+ /**
92
+ * The first grant: what a request with no caps named pays out, given
93
+ * who was proven - and `null` IS an answer. Nobody's defaults should
94
+ * be just enough to go earn attestation (here: sign in); there is no
95
+ * separate "bootstrap caps" list.
96
+ */
97
+ defaults: (subject: string | null) => Cap[];
98
+ /**
99
+ * Reads a proven subject out of ANY fn's result: the moment authority
100
+ * learns WHO, attested proof rides back alongside the result.
101
+ */
102
+ identify: (result: unknown) => string | null;
103
+ /**
104
+ * Turns a subject into the portable attestation handed back. Defaults
105
+ * to a token this server signs itself; swap together with `verify` to
106
+ * lean on an external IDP instead.
107
+ */
108
+ attest?: (subject: string) => unknown | Promise<unknown>;
109
+ /** Decides whether a presented attestation passes. */
110
+ verify?: (attestation: unknown) => string | null | Promise<string | null>;
111
+ /** How widening requests are settled. Default: challenge everything. */
112
+ decide?: (request: {
113
+ subject: string;
114
+ caps: Cap[];
115
+ goal?: string;
116
+ }) => "approve" | "challenge" | "deny";
117
+ /** Delegation and attestation lifetime, seconds. */
118
+ ttl?: number;
119
+ };
120
+ /**
121
+ * The remote boundary - the ONLY place a reference is ever reified or
122
+ * verified, and the only construction in the whole model. Capabilities
123
+ * are not registered or created anywhere: the fns you serve ARE the
124
+ * capability space, one per key, and EVERY one of them validates its
125
+ * caller - there is no public list. Everything crosses as data: a fn
126
+ * key, JSON input, and a freshly minted invocation. Verification sets
127
+ * `c.capability`; `validateCaller` on every fn does the rest.
128
+ */
129
+ declare const serve: (modules: Record<string, unknown>, authority: Authority) => Promise<{
130
+ exec: FnDefination<{
131
+ readonly call: string;
132
+ readonly input: unknown;
133
+ readonly token: unknown;
134
+ }, Promise<unknown>, "capability.exec", {
135
+ readonly call: TypeDefination<string, string, never>;
136
+ readonly input: TypeDefination<unknown, unknown, never>;
137
+ readonly token: TypeDefination<unknown, unknown, never>;
138
+ }, readonly [], {}, {
139
+ capability?: ({
140
+ subject: string;
141
+ caps: (string | {
142
+ fn: string;
143
+ input?: {
144
+ [x: string]: unknown;
145
+ } | undefined;
146
+ })[];
147
+ holder: string;
148
+ entry: string | null;
149
+ } & {
150
+ subject: string;
151
+ caps: Cap[];
152
+ holder: string;
153
+ entry: string | null;
154
+ }) | null | undefined;
155
+ }, unknown>;
156
+ decide: (id: string, approve: boolean) => Challenge | undefined;
157
+ challenges: Map<string, Challenge>;
158
+ key: string;
159
+ }>;
160
+ type Transport = (message: {
161
+ call: string;
162
+ input?: unknown;
163
+ token?: Invocation;
164
+ }) => Promise<any>;
165
+ /**
166
+ * An agent: a keypair plus whatever reified references it holds. Born
167
+ * asking - creation requests the default caps. Without attestation, the
168
+ * answer is the bootstrap challenge: a delegation whose only power is
169
+ * signing in. With attestation (passed in, or earned by a call whose
170
+ * result attests), the answer is the defaults. EVERY call re-mints a
171
+ * single-use invocation over the fn AND the input; nothing is public.
172
+ */
173
+ declare const createAgent: (transport: Transport, options?: {
174
+ attestation?: unknown;
175
+ }) => Promise<{
176
+ id: string;
177
+ attestation: () => unknown;
178
+ hold: (delegation: Delegation) => void;
179
+ held: () => Delegation | null;
180
+ call: (call: string, input?: unknown) => Promise<any>;
181
+ request: (caps: Cap[], goal?: string) => Promise<any>;
182
+ /** Hand a SLICE of held references to another agent, re-minted for
183
+ * their key - fewer fns, or the same fn pinned to narrower input.
184
+ * The other agent spends with their own signature. */
185
+ delegate: (toAgentId: string, caps: Cap[]) => Promise<Delegation>;
186
+ }>;
187
+ //#endregion
188
+ export { Attestation, Authority, Cap, CapabilityError, Challenge, Delegation, Invocation, Keypair, MAX_DELEGATION_DEPTH, Transport, capability, covers, createAgent, fmtCap, fnOf, generateKeypair, mintDelegation, mintInvocation, permits, serve, validateCaller, verifyAttestation, verifyDelegation, verifyInvocation };
189
+ //# sourceMappingURL=capability.d.cts.map
@@ -0,0 +1,189 @@
1
+ import { TypeDefination } from "./schema.mjs";
2
+ import { OnEntry } from "./module.mjs";
3
+ import { FnDefination } from "./fn.mjs";
4
+ import { VarDefination } from "./var.mjs";
5
+ //#region src/capability.d.ts
6
+ type Keypair = {
7
+ id: string;
8
+ sign: (payload: string) => Promise<string>;
9
+ };
10
+ declare const generateKeypair: () => Promise<Keypair>;
11
+ /** The name of a right: a fn key, optionally pinned to exact input
12
+ * fields. Never signed on its own - delegations carry caps, invocations
13
+ * spend them. */
14
+ type Cap = string | {
15
+ fn: string;
16
+ input?: Record<string, unknown>;
17
+ };
18
+ declare const fnOf: (cap: Cap) => string;
19
+ declare const fmtCap: (cap: Cap) => string;
20
+ /** Does holding `parent` justify holding `child`? Same fn, and every
21
+ * field the parent pins must be pinned identically by the child. */
22
+ declare const covers: (parent: Cap, child: Cap) => boolean;
23
+ /** Do these held caps authorize calling `fn` with `input`? */
24
+ declare const permits: (caps: Cap[], fn: string, input: unknown) => boolean;
25
+ type Delegation = {
26
+ typ: "dlg";
27
+ iss: string;
28
+ aud: string;
29
+ sub: string;
30
+ caps: Cap[];
31
+ exp: number;
32
+ prf?: Delegation;
33
+ sig: string;
34
+ };
35
+ type Invocation = {
36
+ typ: "inv";
37
+ iss: string;
38
+ call: string;
39
+ input?: unknown;
40
+ exp: number;
41
+ nonce: string;
42
+ prf: Delegation;
43
+ sig: string;
44
+ };
45
+ declare const mintDelegation: (issuer: Keypair, body: Omit<Delegation, "typ" | "iss" | "sig">) => Promise<Delegation>;
46
+ declare const mintInvocation: (holder: Keypair, call: string, proof: Delegation, input?: unknown) => Promise<Invocation>;
47
+ declare class CapabilityError extends Error {
48
+ constructor(message: string);
49
+ }
50
+ /** A crafted chain of self-signed links (valid until the root check at the
51
+ * very bottom) would otherwise force unbounded recursion and signature
52
+ * verifications - a cheap denial-of-service. Cap the depth. */
53
+ declare const MAX_DELEGATION_DEPTH = 16;
54
+ /** Walk the chain: every link signed, attenuating, unexpired, rooted. */
55
+ declare const verifyDelegation: (root: string, link: Delegation, depth?: number) => Promise<Delegation>;
56
+ declare const verifyInvocation: (root: string, token: Invocation) => Promise<{
57
+ subject: string;
58
+ caps: Cap[];
59
+ holder: string;
60
+ }>;
61
+ type Attestation = {
62
+ typ: "idt";
63
+ iss: string;
64
+ sub: string;
65
+ exp: number;
66
+ sig: string;
67
+ };
68
+ declare const verifyAttestation: (root: string, token: Attestation) => Promise<string>;
69
+ declare const capability: VarDefination<"capability", {
70
+ subject: string;
71
+ caps: Cap[];
72
+ holder: string;
73
+ entry: string | null;
74
+ } | null, undefined, never>;
75
+ declare const validateCaller: (fnKey: string) => OnEntry<string, unknown>;
76
+ type Challenge = {
77
+ id: string;
78
+ subject: string;
79
+ holder: string;
80
+ caps: Cap[];
81
+ goal?: string;
82
+ status: "pending" | "approved" | "denied";
83
+ };
84
+ /**
85
+ * Authority never checks a call - `validateCaller` owns that. It answers
86
+ * the questions that come BEFORE any call: what references does a caller
87
+ * start with, what proves WHO a caller acts for, and how are requests
88
+ * for more settled?
89
+ */
90
+ type Authority = {
91
+ /**
92
+ * The first grant: what a request with no caps named pays out, given
93
+ * who was proven - and `null` IS an answer. Nobody's defaults should
94
+ * be just enough to go earn attestation (here: sign in); there is no
95
+ * separate "bootstrap caps" list.
96
+ */
97
+ defaults: (subject: string | null) => Cap[];
98
+ /**
99
+ * Reads a proven subject out of ANY fn's result: the moment authority
100
+ * learns WHO, attested proof rides back alongside the result.
101
+ */
102
+ identify: (result: unknown) => string | null;
103
+ /**
104
+ * Turns a subject into the portable attestation handed back. Defaults
105
+ * to a token this server signs itself; swap together with `verify` to
106
+ * lean on an external IDP instead.
107
+ */
108
+ attest?: (subject: string) => unknown | Promise<unknown>;
109
+ /** Decides whether a presented attestation passes. */
110
+ verify?: (attestation: unknown) => string | null | Promise<string | null>;
111
+ /** How widening requests are settled. Default: challenge everything. */
112
+ decide?: (request: {
113
+ subject: string;
114
+ caps: Cap[];
115
+ goal?: string;
116
+ }) => "approve" | "challenge" | "deny";
117
+ /** Delegation and attestation lifetime, seconds. */
118
+ ttl?: number;
119
+ };
120
+ /**
121
+ * The remote boundary - the ONLY place a reference is ever reified or
122
+ * verified, and the only construction in the whole model. Capabilities
123
+ * are not registered or created anywhere: the fns you serve ARE the
124
+ * capability space, one per key, and EVERY one of them validates its
125
+ * caller - there is no public list. Everything crosses as data: a fn
126
+ * key, JSON input, and a freshly minted invocation. Verification sets
127
+ * `c.capability`; `validateCaller` on every fn does the rest.
128
+ */
129
+ declare const serve: (modules: Record<string, unknown>, authority: Authority) => Promise<{
130
+ exec: FnDefination<{
131
+ readonly call: string;
132
+ readonly input: unknown;
133
+ readonly token: unknown;
134
+ }, Promise<unknown>, "capability.exec", {
135
+ readonly call: TypeDefination<string, string, never>;
136
+ readonly input: TypeDefination<unknown, unknown, never>;
137
+ readonly token: TypeDefination<unknown, unknown, never>;
138
+ }, readonly [], {}, {
139
+ capability?: ({
140
+ subject: string;
141
+ caps: (string | {
142
+ fn: string;
143
+ input?: {
144
+ [x: string]: unknown;
145
+ } | undefined;
146
+ })[];
147
+ holder: string;
148
+ entry: string | null;
149
+ } & {
150
+ subject: string;
151
+ caps: Cap[];
152
+ holder: string;
153
+ entry: string | null;
154
+ }) | null | undefined;
155
+ }, unknown>;
156
+ decide: (id: string, approve: boolean) => Challenge | undefined;
157
+ challenges: Map<string, Challenge>;
158
+ key: string;
159
+ }>;
160
+ type Transport = (message: {
161
+ call: string;
162
+ input?: unknown;
163
+ token?: Invocation;
164
+ }) => Promise<any>;
165
+ /**
166
+ * An agent: a keypair plus whatever reified references it holds. Born
167
+ * asking - creation requests the default caps. Without attestation, the
168
+ * answer is the bootstrap challenge: a delegation whose only power is
169
+ * signing in. With attestation (passed in, or earned by a call whose
170
+ * result attests), the answer is the defaults. EVERY call re-mints a
171
+ * single-use invocation over the fn AND the input; nothing is public.
172
+ */
173
+ declare const createAgent: (transport: Transport, options?: {
174
+ attestation?: unknown;
175
+ }) => Promise<{
176
+ id: string;
177
+ attestation: () => unknown;
178
+ hold: (delegation: Delegation) => void;
179
+ held: () => Delegation | null;
180
+ call: (call: string, input?: unknown) => Promise<any>;
181
+ request: (caps: Cap[], goal?: string) => Promise<any>;
182
+ /** Hand a SLICE of held references to another agent, re-minted for
183
+ * their key - fewer fns, or the same fn pinned to narrower input.
184
+ * The other agent spends with their own signature. */
185
+ delegate: (toAgentId: string, caps: Cap[]) => Promise<Delegation>;
186
+ }>;
187
+ //#endregion
188
+ export { Attestation, Authority, Cap, CapabilityError, Challenge, Delegation, Invocation, Keypair, MAX_DELEGATION_DEPTH, Transport, capability, covers, createAgent, fmtCap, fnOf, generateKeypair, mintDelegation, mintInvocation, permits, serve, validateCaller, verifyAttestation, verifyDelegation, verifyInvocation };
189
+ //# sourceMappingURL=capability.d.mts.map