@fabrika/app 0.9.0-dev.13 → 0.9.0-dev.15
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.
- package/package.json +14 -9
- package/skill/SKILL.md +3 -2
- package/skill/references/app-setup.md +17 -9
- package/skill/references/endpoint.md +15 -2
- package/skill/references/errors.md +7 -6
- package/skill/references/jobs.md +10 -0
- package/skill/references/relations.md +145 -21
- package/skill/references/table-and-migration.md +4 -1
- package/skill/references/webhooks-links-connectors.md +2 -1
- package/src/build-contract-child.ts +16 -0
- package/src/build.ts +48 -2
- package/src/bun.ts +10 -0
- package/src/client/contract.ts +6 -4
- package/src/cloudflare/application.ts +3 -0
- package/src/contract/artifact-schema.ts +367 -299
- package/src/contract/attachment.ts +4 -3
- package/src/contract/build.ts +76 -18
- package/src/contract/compile.ts +3 -2
- package/src/contract/context.ts +2 -3
- package/src/contract/define.ts +19 -6
- package/src/contract/group.ts +18 -8
- package/src/contract/index.ts +1 -1
- package/src/contract/kit.ts +4 -3
- package/src/contract/problems.ts +4 -0
- package/src/contract/schema-shape.ts +14 -2
- package/src/contract/token.ts +4 -3
- package/src/disk-assets.ts +15 -3
- package/src/from-row.ts +12 -6
- package/src/implement/approved.ts +57 -0
- package/src/implement/attachment.ts +2 -1
- package/src/implement/boot.ts +118 -17
- package/src/implement/capability.ts +82 -12
- package/src/implement/dispatch.ts +265 -45
- package/src/implement/egress.ts +410 -9
- package/src/implement/faults.ts +1 -0
- package/src/implement/implement.ts +23 -3
- package/src/implement/invocation.ts +10 -9
- package/src/implement/project.ts +3 -1
- package/src/implement/tokens.ts +3 -2
- package/src/implement/types.ts +33 -10
- package/src/implement/webhook.ts +8 -0
- package/src/jobs/runner.ts +7 -2
- package/src/react/hooks.ts +12 -11
- package/src/react/provider.tsx +2 -1
- package/src/react/types.ts +2 -2
- package/src/schema/builder.ts +146 -0
- package/src/schema/data-schema.ts +365 -0
- package/src/schema/index.ts +18 -0
- package/src/schema/replay.ts +623 -0
- package/src/source-map-path.ts +21 -0
- package/src/testing/caller.ts +20 -9
- package/src/testing/kit.ts +3 -3
- package/src/testing/membership.ts +9 -5
- package/types/src/build-contract-child.d.ts +1 -0
- package/types/src/build.d.ts +16 -1
- package/types/src/client/contract.d.ts +3 -4
- package/types/src/cloudflare/application.d.ts +2 -0
- package/types/src/contract/artifact-schema.d.ts +8 -0
- package/types/src/contract/attachment.d.ts +3 -2
- package/types/src/contract/context.d.ts +2 -3
- package/types/src/contract/define.d.ts +16 -6
- package/types/src/contract/group.d.ts +14 -7
- package/types/src/contract/index.d.ts +1 -1
- package/types/src/contract/kit.d.ts +4 -5
- package/types/src/contract/problems.d.ts +1 -0
- package/types/src/contract/schema-shape.d.ts +3 -2
- package/types/src/contract/token.d.ts +3 -3
- package/types/src/from-row.d.ts +9 -5
- package/types/src/implement/approved.d.ts +11 -0
- package/types/src/implement/boot.d.ts +12 -0
- package/types/src/implement/capability.d.ts +14 -5
- package/types/src/implement/dispatch.d.ts +9 -1
- package/types/src/implement/egress.d.ts +36 -1
- package/types/src/implement/faults.d.ts +1 -0
- package/types/src/implement/implement.d.ts +19 -3
- package/types/src/implement/invocation.d.ts +3 -3
- package/types/src/implement/project.d.ts +2 -1
- package/types/src/implement/types.d.ts +17 -8
- package/types/src/jobs/runner.d.ts +3 -0
- package/types/src/react/hooks.d.ts +4 -5
- package/types/src/react/types.d.ts +2 -2
- package/types/src/schema/builder.d.ts +45 -0
- package/types/src/schema/data-schema.d.ts +124 -0
- package/types/src/schema/index.d.ts +3 -0
- package/types/src/schema/replay.d.ts +27 -0
- package/types/src/source-map-path.d.ts +4 -0
- package/types/src/testing/caller.d.ts +6 -1
- package/types/src/testing/kit.d.ts +2 -3
- package/types/src/testing/membership.d.ts +1 -1
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
// One step of a `z` chain of the contract subset, replayed against zod itself: a factory, or a method on a receiver that
|
|
2
|
+
// an earlier step built. `DataSchema` and the static contract reader both build zod only through here, so a recorded
|
|
3
|
+
// chain and the evaluated module hold the same schema.
|
|
4
|
+
|
|
5
|
+
import { z } from 'zod/v4'
|
|
6
|
+
|
|
7
|
+
/** A plain argument of a step, with every schema in it already zod. */
|
|
8
|
+
export type ReplayArg =
|
|
9
|
+
| string
|
|
10
|
+
| number
|
|
11
|
+
| boolean
|
|
12
|
+
| null
|
|
13
|
+
| RegExp
|
|
14
|
+
| z.ZodType
|
|
15
|
+
| Unreadable
|
|
16
|
+
| readonly ReplayArg[]
|
|
17
|
+
| { readonly [key: string]: ReplayArg }
|
|
18
|
+
|
|
19
|
+
/** An argument a reader of source read that is no value of the subset (a builder call, say): refused by its kind. */
|
|
20
|
+
export class Unreadable {
|
|
21
|
+
readonly kind: string
|
|
22
|
+
|
|
23
|
+
constructor(kind: string) {
|
|
24
|
+
this.kind = kind
|
|
25
|
+
Object.freeze(this)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** `z.<factory>(…)`, `z.iso.<factory>(…)` or `.<method>(…)` on a receiver. */
|
|
30
|
+
export type ReplayStep = { readonly factory: string; readonly iso: boolean } | { readonly method: string; readonly receiver: z.ZodType }
|
|
31
|
+
|
|
32
|
+
export const SCHEMA_SUBSET = 'a schema of the supported z subset (packages/check/CLAUDE.md)'
|
|
33
|
+
|
|
34
|
+
/** A step zod would refuse, or one outside the contract subset: what is wrong and what is allowed there. */
|
|
35
|
+
export class SchemaRefusal extends Error {
|
|
36
|
+
override readonly name = 'SchemaRefusal'
|
|
37
|
+
readonly allowed: string
|
|
38
|
+
|
|
39
|
+
constructor(message: string, allowed: string) {
|
|
40
|
+
super(message)
|
|
41
|
+
this.allowed = allowed
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The zod schema `step` builds from `args`; a step outside the subset, or one zod refuses, throws a `SchemaRefusal`. */
|
|
46
|
+
export function replayStep(step: ReplayStep, args: readonly ReplayArg[]): z.ZodType {
|
|
47
|
+
try {
|
|
48
|
+
return 'factory' in step ? (step.iso ? isoFormat(step.factory, args) : factory(step.factory, args)) : method(step.method, step.receiver, args)
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (error instanceof SchemaRefusal) {
|
|
51
|
+
throw error
|
|
52
|
+
}
|
|
53
|
+
// zod refuses some arguments while building, e.g. `.pick()` of a key the object does not have.
|
|
54
|
+
const message = error instanceof Error ? (error.message.split('\n')[0] ?? error.message) : 'zod refused the schema'
|
|
55
|
+
throw new SchemaRefusal(`zod refuses this schema: ${message}`, SCHEMA_SUBSET)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function factory(name: string, args: readonly ReplayArg[]): z.ZodType {
|
|
60
|
+
const call = `z.${name}()`
|
|
61
|
+
switch (name) {
|
|
62
|
+
case 'string':
|
|
63
|
+
return z.string(messageParams(call, only(call, args)))
|
|
64
|
+
case 'number':
|
|
65
|
+
return z.number(messageParams(call, only(call, args)))
|
|
66
|
+
case 'int':
|
|
67
|
+
return z.int(messageParams(call, only(call, args)))
|
|
68
|
+
case 'boolean':
|
|
69
|
+
return z.boolean(messageParams(call, only(call, args)))
|
|
70
|
+
case 'null':
|
|
71
|
+
return z.null(messageParams(call, only(call, args)))
|
|
72
|
+
case 'uuid':
|
|
73
|
+
return z.uuid(uuidParams(call, only(call, args)))
|
|
74
|
+
case 'email':
|
|
75
|
+
return z.email(messageParams(call, only(call, args)))
|
|
76
|
+
case 'url':
|
|
77
|
+
return z.url(messageParams(call, only(call, args)))
|
|
78
|
+
case 'literal': {
|
|
79
|
+
const [value, params] = between(call, args, 1, 2)
|
|
80
|
+
const literal = literalValues(call, value)
|
|
81
|
+
return Array.isArray(literal) ? z.literal(literal, messageParams(call, params)) : z.literal(literal, messageParams(call, params))
|
|
82
|
+
}
|
|
83
|
+
case 'enum': {
|
|
84
|
+
const [values, params] = between(call, args, 1, 2)
|
|
85
|
+
const entries = enumEntries(call, values)
|
|
86
|
+
return Array.isArray(entries) ? z.enum(entries, messageParams(call, params)) : z.enum(entries, messageParams(call, params))
|
|
87
|
+
}
|
|
88
|
+
case 'strictObject': {
|
|
89
|
+
const [shape, params] = between(call, args, 1, 2)
|
|
90
|
+
return z.strictObject(schemaShape(call, shape), messageParams(call, params))
|
|
91
|
+
}
|
|
92
|
+
case 'array': {
|
|
93
|
+
const [element, params] = between(call, args, 1, 2)
|
|
94
|
+
return z.array(schema(call, element), messageParams(call, params))
|
|
95
|
+
}
|
|
96
|
+
case 'tuple':
|
|
97
|
+
return tuple(call, args)
|
|
98
|
+
case 'union': {
|
|
99
|
+
const [options, params] = between(call, args, 1, 2)
|
|
100
|
+
return z.union(schemaList(call, options), messageParams(call, params))
|
|
101
|
+
}
|
|
102
|
+
case 'discriminatedUnion': {
|
|
103
|
+
const [discriminator, options, params] = between(call, args, 2, 3)
|
|
104
|
+
const [first, ...rest] = schemaList(call, options).map((option) => objectSchema(call, option))
|
|
105
|
+
if (first === undefined) {
|
|
106
|
+
throw new SchemaRefusal(`${call} has no option`, 'z.discriminatedUnion(<key>, [z.strictObject({ … }), …])')
|
|
107
|
+
}
|
|
108
|
+
return z.discriminatedUnion(text(call, discriminator), [first, ...rest], messageParams(call, params))
|
|
109
|
+
}
|
|
110
|
+
case 'record': {
|
|
111
|
+
const [key, value, params] = between(call, args, 2, 3)
|
|
112
|
+
return z.record(recordKey(call, schema(call, key)), schema(call, value), messageParams(call, params))
|
|
113
|
+
}
|
|
114
|
+
case 'optional':
|
|
115
|
+
return z.optional(schema(call, exactly(call, args, 1)[0]))
|
|
116
|
+
case 'nullable':
|
|
117
|
+
return z.nullable(schema(call, exactly(call, args, 1)[0]))
|
|
118
|
+
default:
|
|
119
|
+
throw new SchemaRefusal(`'${name}' is not in the supported schema subset`, SCHEMA_SUBSET)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function isoFormat(name: string, args: readonly ReplayArg[]): z.ZodType {
|
|
124
|
+
const call = `z.iso.${name}()`
|
|
125
|
+
switch (name) {
|
|
126
|
+
case 'date':
|
|
127
|
+
return z.iso.date(messageParams(call, only(call, args)))
|
|
128
|
+
case 'datetime':
|
|
129
|
+
return z.iso.datetime(datetimeParams(call, only(call, args)))
|
|
130
|
+
case 'time':
|
|
131
|
+
return z.iso.time(timeParams(call, only(call, args)))
|
|
132
|
+
case 'duration':
|
|
133
|
+
return z.iso.duration(messageParams(call, only(call, args)))
|
|
134
|
+
default:
|
|
135
|
+
throw new SchemaRefusal(`'iso.${name}' is not in the supported schema subset`, SCHEMA_SUBSET)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function method(name: string, receiver: z.ZodType, args: readonly ReplayArg[]): z.ZodType {
|
|
140
|
+
const call = `.${name}()`
|
|
141
|
+
switch (name) {
|
|
142
|
+
case 'min':
|
|
143
|
+
case 'max':
|
|
144
|
+
case 'length': {
|
|
145
|
+
const [bound, params] = between(call, args, 1, 2)
|
|
146
|
+
const value = number(call, bound)
|
|
147
|
+
const message = messageParams(call, params)
|
|
148
|
+
if (receiver instanceof z.ZodString || receiver instanceof z.ZodStringFormat) {
|
|
149
|
+
return name === 'min' ? receiver.min(value, message) : name === 'max' ? receiver.max(value, message) : receiver.length(value, message)
|
|
150
|
+
}
|
|
151
|
+
if (receiver instanceof z.ZodArray) {
|
|
152
|
+
return name === 'min' ? receiver.min(value, message) : name === 'max' ? receiver.max(value, message) : receiver.length(value, message)
|
|
153
|
+
}
|
|
154
|
+
if (receiver instanceof z.ZodNumber && name !== 'length') {
|
|
155
|
+
return name === 'min' ? receiver.min(value, message) : receiver.max(value, message)
|
|
156
|
+
}
|
|
157
|
+
throw unsupported(call, receiver)
|
|
158
|
+
}
|
|
159
|
+
case 'nonempty': {
|
|
160
|
+
const message = messageParams(call, only(call, args))
|
|
161
|
+
if (receiver instanceof z.ZodString || receiver instanceof z.ZodStringFormat || receiver instanceof z.ZodArray) {
|
|
162
|
+
return receiver.nonempty(message)
|
|
163
|
+
}
|
|
164
|
+
throw unsupported(call, receiver)
|
|
165
|
+
}
|
|
166
|
+
case 'email':
|
|
167
|
+
case 'url':
|
|
168
|
+
case 'uuid': {
|
|
169
|
+
const params = only(call, args)
|
|
170
|
+
if (!(receiver instanceof z.ZodString)) {
|
|
171
|
+
throw unsupported(call, receiver)
|
|
172
|
+
}
|
|
173
|
+
return name === 'email'
|
|
174
|
+
? receiver.email(messageParams(call, params))
|
|
175
|
+
: name === 'url'
|
|
176
|
+
? receiver.url(messageParams(call, params))
|
|
177
|
+
: receiver.uuid(uuidParams(call, params))
|
|
178
|
+
}
|
|
179
|
+
case 'startsWith':
|
|
180
|
+
case 'endsWith':
|
|
181
|
+
case 'includes':
|
|
182
|
+
case 'regex': {
|
|
183
|
+
const [argument, params] = between(call, args, 1, 2)
|
|
184
|
+
if (!(receiver instanceof z.ZodString || receiver instanceof z.ZodStringFormat)) {
|
|
185
|
+
throw unsupported(call, receiver)
|
|
186
|
+
}
|
|
187
|
+
switch (name) {
|
|
188
|
+
case 'startsWith':
|
|
189
|
+
return receiver.startsWith(text(call, argument), messageParams(call, params))
|
|
190
|
+
case 'endsWith':
|
|
191
|
+
return receiver.endsWith(text(call, argument), messageParams(call, params))
|
|
192
|
+
case 'includes':
|
|
193
|
+
return receiver.includes(text(call, argument), includesParams(call, params))
|
|
194
|
+
default:
|
|
195
|
+
return receiver.regex(regex(call, argument), messageParams(call, params))
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
case 'int':
|
|
199
|
+
case 'positive':
|
|
200
|
+
case 'negative':
|
|
201
|
+
case 'nonnegative':
|
|
202
|
+
case 'nonpositive': {
|
|
203
|
+
const message = messageParams(call, only(call, args))
|
|
204
|
+
if (!(receiver instanceof z.ZodNumber)) {
|
|
205
|
+
throw unsupported(call, receiver)
|
|
206
|
+
}
|
|
207
|
+
switch (name) {
|
|
208
|
+
case 'int':
|
|
209
|
+
return receiver.int(message)
|
|
210
|
+
case 'positive':
|
|
211
|
+
return receiver.positive(message)
|
|
212
|
+
case 'negative':
|
|
213
|
+
return receiver.negative(message)
|
|
214
|
+
case 'nonnegative':
|
|
215
|
+
return receiver.nonnegative(message)
|
|
216
|
+
default:
|
|
217
|
+
return receiver.nonpositive(message)
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
case 'gt':
|
|
221
|
+
case 'gte':
|
|
222
|
+
case 'lt':
|
|
223
|
+
case 'lte':
|
|
224
|
+
case 'multipleOf': {
|
|
225
|
+
const [bound, params] = between(call, args, 1, 2)
|
|
226
|
+
const value = number(call, bound)
|
|
227
|
+
const message = messageParams(call, params)
|
|
228
|
+
if (!(receiver instanceof z.ZodNumber)) {
|
|
229
|
+
throw unsupported(call, receiver)
|
|
230
|
+
}
|
|
231
|
+
switch (name) {
|
|
232
|
+
case 'gt':
|
|
233
|
+
return receiver.gt(value, message)
|
|
234
|
+
case 'gte':
|
|
235
|
+
return receiver.gte(value, message)
|
|
236
|
+
case 'lt':
|
|
237
|
+
return receiver.lt(value, message)
|
|
238
|
+
case 'lte':
|
|
239
|
+
return receiver.lte(value, message)
|
|
240
|
+
default:
|
|
241
|
+
return receiver.multipleOf(value, message)
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
case 'optional':
|
|
245
|
+
exactly(call, args, 0)
|
|
246
|
+
return receiver.optional()
|
|
247
|
+
case 'nullable':
|
|
248
|
+
exactly(call, args, 0)
|
|
249
|
+
return receiver.nullable()
|
|
250
|
+
case 'nullish':
|
|
251
|
+
exactly(call, args, 0)
|
|
252
|
+
return receiver.nullish()
|
|
253
|
+
case 'array':
|
|
254
|
+
exactly(call, args, 0)
|
|
255
|
+
return receiver.array()
|
|
256
|
+
case 'default':
|
|
257
|
+
return receiver.default(plain(call, exactly(call, args, 1)[0]))
|
|
258
|
+
case 'describe':
|
|
259
|
+
return receiver.describe(text(call, exactly(call, args, 1)[0]))
|
|
260
|
+
case 'or':
|
|
261
|
+
return receiver.or(schema(call, exactly(call, args, 1)[0]))
|
|
262
|
+
case 'extend':
|
|
263
|
+
case 'pick':
|
|
264
|
+
case 'omit':
|
|
265
|
+
case 'partial': {
|
|
266
|
+
if (!(receiver instanceof z.ZodObject)) {
|
|
267
|
+
throw unsupported(call, receiver)
|
|
268
|
+
}
|
|
269
|
+
if (name === 'extend') {
|
|
270
|
+
return receiver.extend(schemaShape(call, exactly(call, args, 1)[0]))
|
|
271
|
+
}
|
|
272
|
+
if (name === 'partial') {
|
|
273
|
+
const [mask] = between(call, args, 0, 1)
|
|
274
|
+
return mask === undefined ? receiver.partial() : receiver.partial(keyMask(call, mask))
|
|
275
|
+
}
|
|
276
|
+
const mask = keyMask(call, exactly(call, args, 1)[0])
|
|
277
|
+
return name === 'pick' ? receiver.pick(mask) : receiver.omit(mask)
|
|
278
|
+
}
|
|
279
|
+
case 'exclude':
|
|
280
|
+
case 'extract': {
|
|
281
|
+
// Outside the contract subset, which no contract module calls: application code narrows an enum with them.
|
|
282
|
+
const [keys, params] = between(call, args, 1, 2)
|
|
283
|
+
if (!(receiver instanceof z.ZodEnum)) {
|
|
284
|
+
throw unsupported(call, receiver)
|
|
285
|
+
}
|
|
286
|
+
const names = textList(call, keys)
|
|
287
|
+
return name === 'exclude' ? receiver.exclude(names, messageParams(call, params)) : receiver.extract(names, messageParams(call, params))
|
|
288
|
+
}
|
|
289
|
+
default:
|
|
290
|
+
throw new SchemaRefusal(`'${name}' is not in the supported schema subset`, SCHEMA_SUBSET)
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function tuple(call: string, args: readonly ReplayArg[]): z.ZodType {
|
|
295
|
+
const [items, second, third] = between(call, args, 1, 3)
|
|
296
|
+
const [first, ...rest] = schemaList(call, items)
|
|
297
|
+
const restSchema = second instanceof z.ZodType ? second : undefined
|
|
298
|
+
const params = messageParams(call, restSchema === undefined ? second : third)
|
|
299
|
+
if (restSchema === undefined && third !== undefined) {
|
|
300
|
+
throw new SchemaRefusal(`${call} takes its items, an optional rest schema and options`, 'z.tuple([<schemas>], <rest schema>)')
|
|
301
|
+
}
|
|
302
|
+
if (first === undefined) {
|
|
303
|
+
if (restSchema !== undefined) {
|
|
304
|
+
throw new SchemaRefusal(`${call} with a rest and no item`, 'z.array(<schema>)')
|
|
305
|
+
}
|
|
306
|
+
return z.tuple([], params)
|
|
307
|
+
}
|
|
308
|
+
return restSchema === undefined ? z.tuple([first, ...rest], params) : z.tuple([first, ...rest], restSchema, params)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// ── Arguments ───────────────────────────────────────────────────────────────────────────────────
|
|
312
|
+
|
|
313
|
+
type Entries = { readonly [key: string]: ReplayArg }
|
|
314
|
+
|
|
315
|
+
/** What the literal subset calls an argument's kind, for a refusal that names it. */
|
|
316
|
+
function kindOf(value: ReplayArg): string {
|
|
317
|
+
if (value === null) {
|
|
318
|
+
return 'null'
|
|
319
|
+
}
|
|
320
|
+
if (value instanceof RegExp) {
|
|
321
|
+
return 'regex'
|
|
322
|
+
}
|
|
323
|
+
if (value instanceof z.ZodType) {
|
|
324
|
+
return 'schema'
|
|
325
|
+
}
|
|
326
|
+
if (value instanceof Unreadable) {
|
|
327
|
+
return value.kind
|
|
328
|
+
}
|
|
329
|
+
if (Array.isArray(value)) {
|
|
330
|
+
return 'array'
|
|
331
|
+
}
|
|
332
|
+
return typeof value
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function entriesOf(value: ReplayArg | undefined): Entries | undefined {
|
|
336
|
+
if (
|
|
337
|
+
typeof value !== 'object' || value === null || value instanceof RegExp || value instanceof z.ZodType || value instanceof Unreadable || isList(value)
|
|
338
|
+
) {
|
|
339
|
+
return undefined
|
|
340
|
+
}
|
|
341
|
+
const prototype: unknown = Object.getPrototypeOf(value)
|
|
342
|
+
return prototype === Object.prototype || prototype === null ? value : undefined
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function isList(value: ReplayArg): value is readonly ReplayArg[] {
|
|
346
|
+
return Array.isArray(value)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function listOf(value: ReplayArg | undefined): readonly ReplayArg[] | undefined {
|
|
350
|
+
return value !== undefined && isList(value) ? value : undefined
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function between(call: string, args: readonly ReplayArg[], min: number, max: number): readonly (ReplayArg | undefined)[] {
|
|
354
|
+
if (args.length < min || args.length > max) {
|
|
355
|
+
const count = min === max ? `${min}` : `${min} to ${max}`
|
|
356
|
+
throw new SchemaRefusal(`${call} takes ${count} arguments, not ${args.length}`, SCHEMA_SUBSET)
|
|
357
|
+
}
|
|
358
|
+
return Array.from({ length: max }, (_, index) => args[index])
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function exactly(call: string, args: readonly ReplayArg[], count: number): readonly (ReplayArg | undefined)[] {
|
|
362
|
+
return between(call, args, count, count)
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** The one optional argument of a call that takes only options. */
|
|
366
|
+
function only(call: string, args: readonly ReplayArg[]): ReplayArg | undefined {
|
|
367
|
+
return between(call, args, 0, 1)[0]
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function expected(call: string, what: string, value: ReplayArg | undefined): SchemaRefusal {
|
|
371
|
+
return new SchemaRefusal(`${call} takes ${what}${value === undefined ? '' : `, not a ${kindOf(value)}`}`, SCHEMA_SUBSET)
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function schema(call: string, value: ReplayArg | undefined): z.ZodType {
|
|
375
|
+
if (!(value instanceof z.ZodType)) {
|
|
376
|
+
throw expected(call, 'a schema', value)
|
|
377
|
+
}
|
|
378
|
+
return value
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function number(call: string, value: ReplayArg | undefined): number {
|
|
382
|
+
if (typeof value !== 'number') {
|
|
383
|
+
throw expected(call, 'a number', value)
|
|
384
|
+
}
|
|
385
|
+
return value
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function text(call: string, value: ReplayArg | undefined): string {
|
|
389
|
+
if (typeof value !== 'string') {
|
|
390
|
+
throw expected(call, 'a string', value)
|
|
391
|
+
}
|
|
392
|
+
return value
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function textList(call: string, value: ReplayArg | undefined): string[] {
|
|
396
|
+
const items = listOf(value)
|
|
397
|
+
if (items === undefined) {
|
|
398
|
+
throw expected(call, 'an array of strings', value)
|
|
399
|
+
}
|
|
400
|
+
return items.map((item) => text(call, item))
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function regex(call: string, value: ReplayArg | undefined): RegExp {
|
|
404
|
+
if (!(value instanceof RegExp)) {
|
|
405
|
+
throw expected(call, 'a regular expression literal', value)
|
|
406
|
+
}
|
|
407
|
+
return value
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function schemaList(call: string, value: ReplayArg | undefined): z.ZodType[] {
|
|
411
|
+
const items = listOf(value)
|
|
412
|
+
if (items === undefined) {
|
|
413
|
+
throw expected(call, 'an array of schemas', value)
|
|
414
|
+
}
|
|
415
|
+
return items.map((item) => schema(call, item))
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function schemaShape(call: string, value: ReplayArg | undefined): Record<string, z.ZodType> {
|
|
419
|
+
const entries = entriesOf(value)
|
|
420
|
+
if (entries === undefined) {
|
|
421
|
+
throw expected(call, 'an object of schemas', value)
|
|
422
|
+
}
|
|
423
|
+
return Object.fromEntries(Object.entries(entries).map(([key, field]) => [key, schema(call, field)]))
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function keyMask(call: string, value: ReplayArg | undefined): Record<string, true> {
|
|
427
|
+
const entries = entriesOf(value)
|
|
428
|
+
if (entries === undefined || Object.values(entries).some((entry) => entry !== true)) {
|
|
429
|
+
throw expected(call, 'an object of keys set to true', value)
|
|
430
|
+
}
|
|
431
|
+
return Object.fromEntries(Object.keys(entries).map((key): [string, true] => [key, true]))
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function objectSchema(call: string, option: z.ZodType): z.ZodObject {
|
|
435
|
+
if (!(option instanceof z.ZodObject)) {
|
|
436
|
+
throw new SchemaRefusal(`${call} takes object options`, 'z.discriminatedUnion(<key>, [z.strictObject({ … }), …])')
|
|
437
|
+
}
|
|
438
|
+
return option
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function recordKey(call: string, key: z.ZodType): z.ZodString | z.ZodStringFormat | z.ZodEnum {
|
|
442
|
+
if (key instanceof z.ZodString || key instanceof z.ZodStringFormat || key instanceof z.ZodEnum) {
|
|
443
|
+
return key
|
|
444
|
+
}
|
|
445
|
+
throw new SchemaRefusal(`${call} takes a string or enum key`, 'z.record(z.string(), <schema>)')
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
type Primitive = string | number | boolean | null
|
|
449
|
+
|
|
450
|
+
function primitive(call: string, value: ReplayArg | undefined): Primitive {
|
|
451
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || value === null) {
|
|
452
|
+
return value
|
|
453
|
+
}
|
|
454
|
+
throw expected(call, 'a string, number, boolean or null', value)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function literalValues(call: string, value: ReplayArg | undefined): Primitive | Primitive[] {
|
|
458
|
+
const items = listOf(value)
|
|
459
|
+
return items === undefined ? primitive(call, value) : items.map((item) => primitive(call, item))
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function enumEntries(call: string, value: ReplayArg | undefined): string[] | Record<string, string | number> {
|
|
463
|
+
const items = listOf(value)
|
|
464
|
+
if (items !== undefined) {
|
|
465
|
+
return items.map((item) => text(call, item))
|
|
466
|
+
}
|
|
467
|
+
const entries = entriesOf(value)
|
|
468
|
+
if (entries !== undefined) {
|
|
469
|
+
return Object.fromEntries(Object.entries(entries).map(([key, entry]) => [key, typeof entry === 'number' ? entry : text(call, entry)]))
|
|
470
|
+
}
|
|
471
|
+
throw expected(call, 'an array of strings or an object', value)
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/** A value with no schema in it, as the evaluated literal would be. */
|
|
475
|
+
function plain(call: string, value: ReplayArg | undefined): unknown {
|
|
476
|
+
const items = listOf(value)
|
|
477
|
+
if (items !== undefined) {
|
|
478
|
+
return items.map((item) => plain(call, item))
|
|
479
|
+
}
|
|
480
|
+
const entries = entriesOf(value)
|
|
481
|
+
if (entries !== undefined) {
|
|
482
|
+
return Object.fromEntries(Object.entries(entries).map(([key, entry]) => [key, plain(call, entry)]))
|
|
483
|
+
}
|
|
484
|
+
return primitive(call, value)
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// ── Options ─────────────────────────────────────────────────────────────────────────────────────
|
|
488
|
+
|
|
489
|
+
/** An error message changes no shape; `error` and `message` are the only options every step takes. */
|
|
490
|
+
interface MessageParams {
|
|
491
|
+
message?: string
|
|
492
|
+
error?: string
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
const MESSAGE_KEYS = ['message', 'error'] as const
|
|
496
|
+
|
|
497
|
+
function options(call: string, value: ReplayArg | undefined, keys: readonly string[]): Entries | string | undefined {
|
|
498
|
+
if (value === undefined || typeof value === 'string') {
|
|
499
|
+
return value
|
|
500
|
+
}
|
|
501
|
+
const entries = entriesOf(value)
|
|
502
|
+
if (entries === undefined) {
|
|
503
|
+
throw expected(call, 'an error message or an options object', value)
|
|
504
|
+
}
|
|
505
|
+
for (const key of Object.keys(entries)) {
|
|
506
|
+
if (!keys.includes(key)) {
|
|
507
|
+
throw new SchemaRefusal(`'${key}' is not an option the contract reads for ${call}`, `options among ${keys.join(', ')}`)
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return entries
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function messageFields(call: string, entries: Entries): MessageParams {
|
|
514
|
+
const params: MessageParams = {}
|
|
515
|
+
const message = entries['message']
|
|
516
|
+
const error = entries['error']
|
|
517
|
+
if (message !== undefined) {
|
|
518
|
+
params.message = text(call, message)
|
|
519
|
+
}
|
|
520
|
+
if (error !== undefined) {
|
|
521
|
+
params.error = text(call, error)
|
|
522
|
+
}
|
|
523
|
+
return params
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function messageParams(call: string, value: ReplayArg | undefined): string | MessageParams | undefined {
|
|
527
|
+
const read = options(call, value, MESSAGE_KEYS)
|
|
528
|
+
return read === undefined || typeof read === 'string' ? read : messageFields(call, read)
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
const UUID_VERSIONS = ['v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8'] as const
|
|
532
|
+
|
|
533
|
+
type UuidVersion = (typeof UUID_VERSIONS)[number]
|
|
534
|
+
|
|
535
|
+
function isUuidVersion(version: string): version is UuidVersion {
|
|
536
|
+
return UUID_VERSIONS.some((known) => known === version)
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function uuidParams(call: string, value: ReplayArg | undefined): string | (MessageParams & { version?: UuidVersion }) | undefined {
|
|
540
|
+
const read = options(call, value, [...MESSAGE_KEYS, 'version'])
|
|
541
|
+
if (read === undefined || typeof read === 'string') {
|
|
542
|
+
return read
|
|
543
|
+
}
|
|
544
|
+
const params: MessageParams & { version?: UuidVersion } = messageFields(call, read)
|
|
545
|
+
const version = read['version']
|
|
546
|
+
if (version !== undefined) {
|
|
547
|
+
const name = text(call, version)
|
|
548
|
+
if (!isUuidVersion(name)) {
|
|
549
|
+
throw new SchemaRefusal(`'${name}' is not a UUID version`, `one of ${UUID_VERSIONS.join(', ')}`)
|
|
550
|
+
}
|
|
551
|
+
params.version = name
|
|
552
|
+
}
|
|
553
|
+
return params
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function precision(call: string, value: ReplayArg): number | null {
|
|
557
|
+
return value === null ? null : number(call, value)
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
function datetimeParams(
|
|
561
|
+
call: string,
|
|
562
|
+
value: ReplayArg | undefined,
|
|
563
|
+
): string | (MessageParams & { offset?: boolean; local?: boolean; precision?: number | null }) | undefined {
|
|
564
|
+
const read = options(call, value, [...MESSAGE_KEYS, 'offset', 'local', 'precision'])
|
|
565
|
+
if (read === undefined || typeof read === 'string') {
|
|
566
|
+
return read
|
|
567
|
+
}
|
|
568
|
+
const params: MessageParams & { offset?: boolean; local?: boolean; precision?: number | null } = messageFields(call, read)
|
|
569
|
+
const offset = read['offset']
|
|
570
|
+
const local = read['local']
|
|
571
|
+
const digits = read['precision']
|
|
572
|
+
if (offset !== undefined) {
|
|
573
|
+
params.offset = flag(call, offset)
|
|
574
|
+
}
|
|
575
|
+
if (local !== undefined) {
|
|
576
|
+
params.local = flag(call, local)
|
|
577
|
+
}
|
|
578
|
+
if (digits !== undefined) {
|
|
579
|
+
params.precision = precision(call, digits)
|
|
580
|
+
}
|
|
581
|
+
return params
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function timeParams(call: string, value: ReplayArg | undefined): string | (MessageParams & { precision?: number | null }) | undefined {
|
|
585
|
+
const read = options(call, value, [...MESSAGE_KEYS, 'precision'])
|
|
586
|
+
if (read === undefined || typeof read === 'string') {
|
|
587
|
+
return read
|
|
588
|
+
}
|
|
589
|
+
const params: MessageParams & { precision?: number | null } = messageFields(call, read)
|
|
590
|
+
const digits = read['precision']
|
|
591
|
+
if (digits !== undefined) {
|
|
592
|
+
params.precision = precision(call, digits)
|
|
593
|
+
}
|
|
594
|
+
return params
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/** `.includes()` takes its options only as an object. */
|
|
598
|
+
function includesParams(call: string, value: ReplayArg | undefined): (MessageParams & { position?: number }) | undefined {
|
|
599
|
+
const read = options(call, value, [...MESSAGE_KEYS, 'position'])
|
|
600
|
+
if (typeof read === 'string') {
|
|
601
|
+
throw new SchemaRefusal(`${call} takes its options as an object`, "{ message: '…' }")
|
|
602
|
+
}
|
|
603
|
+
if (read === undefined) {
|
|
604
|
+
return undefined
|
|
605
|
+
}
|
|
606
|
+
const params: MessageParams & { position?: number } = messageFields(call, read)
|
|
607
|
+
const position = read['position']
|
|
608
|
+
if (position !== undefined) {
|
|
609
|
+
params.position = number(call, position)
|
|
610
|
+
}
|
|
611
|
+
return params
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function flag(call: string, value: ReplayArg): boolean {
|
|
615
|
+
if (typeof value !== 'boolean') {
|
|
616
|
+
throw expected(call, 'a boolean option', value)
|
|
617
|
+
}
|
|
618
|
+
return value
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function unsupported(call: string, receiver: z.ZodType): SchemaRefusal {
|
|
622
|
+
return new SchemaRefusal(`${call} is not a method of a ${receiver._zod.def.type} schema in the contract subset`, SCHEMA_SUBSET)
|
|
623
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Whether a request names a source map. An SPA mount never serves one: the build sends its sources to Operations. The
|
|
2
|
+
// decision reads the path the way the asset servers resolve it, not the way it was spelled: each segment decoded, empty
|
|
3
|
+
// segments dropped, and any case, because a case-insensitive filesystem opens `x.js.MAP` as `x.js.map`.
|
|
4
|
+
|
|
5
|
+
/** The last non-empty segment of `pathname`, decoded, names a `.map` file; an undecodable one is refused too. */
|
|
6
|
+
export function namesSourceMap(pathname: string): boolean {
|
|
7
|
+
const last = pathname.split('/').filter((segment) => segment !== '').at(-1)
|
|
8
|
+
if (last === undefined) return false
|
|
9
|
+
let decoded: string
|
|
10
|
+
try {
|
|
11
|
+
decoded = decodeURIComponent(last)
|
|
12
|
+
} catch {
|
|
13
|
+
return true
|
|
14
|
+
}
|
|
15
|
+
return isSourceMapName(decoded)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A file name, already decoded, that is a source map in any case. */
|
|
19
|
+
export function isSourceMapName(name: string): boolean {
|
|
20
|
+
return name.toLowerCase().endsWith('.map')
|
|
21
|
+
}
|
package/src/testing/caller.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Test callers: a principal of the test IAM with the permissions its grants resolve to, as IAM resolves them, and
|
|
2
2
|
// the credential it presents.
|
|
3
3
|
|
|
4
|
-
import { type PermissionEntry, type PrincipalAffiliation, RELATION_SCOPE_TYPE } from '@fabrika/auth-core'
|
|
4
|
+
import { type PermissionEntry, type PrincipalAffiliation, RELATION_SCOPE_TYPE, relationInsideScope, type Scope } from '@fabrika/auth-core'
|
|
5
5
|
import type { ActionOf, AnyContract, DimensionOf, RoleOf } from '../contract/define'
|
|
6
6
|
|
|
7
7
|
/** `app` is the connector of another application: its key reaches only the exports that application consumes. */
|
|
@@ -118,13 +118,24 @@ export function resolveGrants<C extends AnyContract>(contract: C, grants: readon
|
|
|
118
118
|
if (role.member !== undefined && grant.scope?.type !== role.member.scope) {
|
|
119
119
|
throw new Error(`'${grant.role}' is a membership role: grant it in one ${role.member.scope} (scope: { type: '${role.member.scope}', value })`)
|
|
120
120
|
}
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
for (const [relation, held] of Object.entries(role.via ?? {})) {
|
|
125
|
-
for (const action of held) entries.push({ action, scope: { type: RELATION_SCOPE_TYPE, value: relation }, source: 'grant' })
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return entries
|
|
121
|
+
const scope = scopeOf(grant.scope)
|
|
122
|
+
const entries: PermissionEntry[] = role.grants.map((action) => ({ action, scope, source: 'grant' }))
|
|
123
|
+
return role.external === true ? entries : [...entries, ...relationEntries(role.via, scope)]
|
|
129
124
|
})
|
|
130
125
|
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The entries a grant of a role with `via` adds at `scope`, as IAM resolves them: each action through its relation,
|
|
129
|
+
* anywhere (`~rel`) for a global grant and only inside the node (`~relin`) for a scoped one. A name holding `:` adds none.
|
|
130
|
+
*/
|
|
131
|
+
export function relationEntries(via: Readonly<Record<string, readonly string[]>> | undefined, scope: Scope | null): PermissionEntry[] {
|
|
132
|
+
const entries: PermissionEntry[] = []
|
|
133
|
+
for (const [relation, actions] of Object.entries(via ?? {})) {
|
|
134
|
+
if (scope !== null && (relation.includes(':') || scope.type.includes(':'))) continue
|
|
135
|
+
const held = scope === null
|
|
136
|
+
? { type: RELATION_SCOPE_TYPE, value: relation }
|
|
137
|
+
: relationInsideScope({ relation, dimension: scope.type, node: scope.value })
|
|
138
|
+
for (const action of actions) entries.push({ action, scope: held, source: 'grant' })
|
|
139
|
+
}
|
|
140
|
+
return entries
|
|
141
|
+
}
|