@ipab/bundle 0.0.0-stage → 0.1.0-rc.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.
- package/LICENSE +21 -0
- package/README.md +156 -2
- package/dist/bundle.d.ts +8 -0
- package/dist/bundle.d.ts.map +1 -0
- package/dist/bundle.js +274 -0
- package/dist/bundle.js.map +1 -0
- package/dist/data.d.ts +5 -0
- package/dist/data.d.ts.map +1 -0
- package/dist/data.js +3 -0
- package/dist/data.js.map +1 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +15 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/parsing.d.ts +9 -0
- package/dist/parsing.d.ts.map +1 -0
- package/dist/parsing.js +250 -0
- package/dist/parsing.js.map +1 -0
- package/dist/schema.d.ts +25 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +314 -0
- package/dist/schema.js.map +1 -0
- package/dist/types.d.ts +57 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +61 -3
- package/src/bundle.ts +430 -0
- package/src/data.ts +12 -0
- package/src/errors.ts +16 -0
- package/src/index.ts +35 -0
- package/src/parsing.ts +349 -0
- package/src/schema.ts +462 -0
- package/src/types.ts +64 -0
package/src/schema.ts
ADDED
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
import { ContentBundleError } from "./errors.js";
|
|
2
|
+
import { assertJson, record, normalizeBundleManifest } from "./parsing.js";
|
|
3
|
+
import * as dagCbor from "@ipld/dag-cbor";
|
|
4
|
+
import { CID } from "multiformats/cid";
|
|
5
|
+
import { sha256 } from "multiformats/hashes/sha2";
|
|
6
|
+
import type {
|
|
7
|
+
BundleManifest,
|
|
8
|
+
JsonSchema,
|
|
9
|
+
SchemaDefinitions,
|
|
10
|
+
SchemaBlock,
|
|
11
|
+
} from "./types.js";
|
|
12
|
+
|
|
13
|
+
export const schemaBlockBytes = 1024 * 1024;
|
|
14
|
+
export function declaresBundleSchema(bytes: Uint8Array): boolean {
|
|
15
|
+
const value: unknown = dagCbor.decode(bytes);
|
|
16
|
+
return record(value) && Object.hasOwn(value, "entry");
|
|
17
|
+
}
|
|
18
|
+
export interface SchemaOptions {
|
|
19
|
+
nodes?: number;
|
|
20
|
+
depth?: number;
|
|
21
|
+
jsonDepth?: number;
|
|
22
|
+
schemaBytes?: number;
|
|
23
|
+
charge?: () => void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function pointerPart(value: string): string {
|
|
27
|
+
return value.replaceAll("~", "~0").replaceAll("/", "~1");
|
|
28
|
+
}
|
|
29
|
+
export function absoluteId(value: unknown): value is string {
|
|
30
|
+
return (
|
|
31
|
+
typeof value === "string" &&
|
|
32
|
+
/^[A-Za-z][A-Za-z0-9+.-]*:[A-Za-z0-9._~:/?[\]@!$&'()*+,;=%-]+$/u.test(
|
|
33
|
+
value,
|
|
34
|
+
) &&
|
|
35
|
+
!/%(?![0-9A-Fa-f]{2})/u.test(value)
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
function invalid(message: string, pointer = ""): never {
|
|
39
|
+
throw new ContentBundleError("SCHEMA_INVALID", message, "", pointer);
|
|
40
|
+
}
|
|
41
|
+
const keywords = new Set([
|
|
42
|
+
"$id",
|
|
43
|
+
"$ref",
|
|
44
|
+
"$defs",
|
|
45
|
+
"type",
|
|
46
|
+
"enum",
|
|
47
|
+
"const",
|
|
48
|
+
"properties",
|
|
49
|
+
"required",
|
|
50
|
+
"additionalProperties",
|
|
51
|
+
"items",
|
|
52
|
+
"anyOf",
|
|
53
|
+
"title",
|
|
54
|
+
"description",
|
|
55
|
+
]);
|
|
56
|
+
const types = new Set([
|
|
57
|
+
"null",
|
|
58
|
+
"boolean",
|
|
59
|
+
"string",
|
|
60
|
+
"number",
|
|
61
|
+
"integer",
|
|
62
|
+
"object",
|
|
63
|
+
"array",
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
export function inspectSchema(
|
|
67
|
+
value: unknown,
|
|
68
|
+
options: SchemaOptions = {},
|
|
69
|
+
): SchemaDefinitions {
|
|
70
|
+
for (const [key, value] of Object.entries(options))
|
|
71
|
+
if (
|
|
72
|
+
key === "charge"
|
|
73
|
+
? typeof value !== "function"
|
|
74
|
+
: !["nodes", "depth", "jsonDepth", "schemaBytes"].includes(key) ||
|
|
75
|
+
!Number.isSafeInteger(value) ||
|
|
76
|
+
(value as number) <= 0
|
|
77
|
+
)
|
|
78
|
+
throw new ContentBundleError(
|
|
79
|
+
"OPTIONS_INVALID",
|
|
80
|
+
"Schema limits must be positive safe integers.",
|
|
81
|
+
);
|
|
82
|
+
const charge = options.charge ?? (() => {});
|
|
83
|
+
try {
|
|
84
|
+
assertJson(value, options.nodes, options.jsonDepth, charge);
|
|
85
|
+
} catch (cause) {
|
|
86
|
+
if (cause instanceof ContentBundleError && cause.code === "CONTENT_INVALID")
|
|
87
|
+
invalid(cause.message);
|
|
88
|
+
throw cause;
|
|
89
|
+
}
|
|
90
|
+
const definitions = new Map<string, JsonSchema>();
|
|
91
|
+
const references: string[] = [];
|
|
92
|
+
const visit = (
|
|
93
|
+
schema: unknown,
|
|
94
|
+
root: boolean,
|
|
95
|
+
pointer: string,
|
|
96
|
+
depth: number,
|
|
97
|
+
): void => {
|
|
98
|
+
charge();
|
|
99
|
+
if (depth > (options.depth ?? 64))
|
|
100
|
+
throw new ContentBundleError("LIMIT_EXCEEDED", "Schema depth exceeded.");
|
|
101
|
+
if (typeof schema === "boolean") {
|
|
102
|
+
definitions.set(pointer, schema);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (!record(schema))
|
|
106
|
+
invalid("Expected a schema object or boolean.", pointer);
|
|
107
|
+
for (const key of Object.keys(schema))
|
|
108
|
+
if (!keywords.has(key))
|
|
109
|
+
invalid(`Unsupported schema keyword ${key}.`, pointer);
|
|
110
|
+
if (!root && Object.hasOwn(schema, "$id"))
|
|
111
|
+
invalid("Nested $id is not supported.", pointer);
|
|
112
|
+
if (root && schema.$id !== undefined && !absoluteId(schema.$id))
|
|
113
|
+
invalid("$id must be an absolute URI.", pointer);
|
|
114
|
+
definitions.set(pointer, schema);
|
|
115
|
+
if (
|
|
116
|
+
schema.type !== undefined &&
|
|
117
|
+
(typeof schema.type !== "string" || !types.has(schema.type))
|
|
118
|
+
)
|
|
119
|
+
invalid("type must be one structural type name.", pointer);
|
|
120
|
+
for (const key of ["title", "description"])
|
|
121
|
+
if (schema[key] !== undefined && typeof schema[key] !== "string")
|
|
122
|
+
invalid(`Invalid ${key}.`, pointer);
|
|
123
|
+
if (
|
|
124
|
+
schema.enum !== undefined &&
|
|
125
|
+
(!Array.isArray(schema.enum) || !schema.enum.length)
|
|
126
|
+
)
|
|
127
|
+
invalid("enum requires a nonempty array.", pointer);
|
|
128
|
+
if (
|
|
129
|
+
Array.isArray(schema.enum) &&
|
|
130
|
+
schema.enum.some((value, index, values) =>
|
|
131
|
+
values
|
|
132
|
+
.slice(0, index)
|
|
133
|
+
.some((previous) => equal(value, previous, charge)),
|
|
134
|
+
)
|
|
135
|
+
)
|
|
136
|
+
invalid("enum values must be unique.", pointer);
|
|
137
|
+
if (schema.$ref !== undefined) {
|
|
138
|
+
if (
|
|
139
|
+
typeof schema.$ref !== "string" ||
|
|
140
|
+
(schema.$ref !== "#" && !/^#\/(?:[^~%]|~[01])*$/u.test(schema.$ref))
|
|
141
|
+
)
|
|
142
|
+
invalid("$ref must be a local JSON Pointer.", pointer);
|
|
143
|
+
references.push(schema.$ref);
|
|
144
|
+
}
|
|
145
|
+
for (const key of ["properties", "$defs"])
|
|
146
|
+
if (schema[key] !== undefined) {
|
|
147
|
+
if (!record(schema[key]))
|
|
148
|
+
invalid(`${key} requires a schema map.`, pointer);
|
|
149
|
+
for (const [name, child] of Object.entries(schema[key]))
|
|
150
|
+
visit(
|
|
151
|
+
child,
|
|
152
|
+
false,
|
|
153
|
+
`${pointer}/${key}/${pointerPart(name)}`,
|
|
154
|
+
depth + 1,
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
if (
|
|
158
|
+
schema.required !== undefined &&
|
|
159
|
+
(!Array.isArray(schema.required) ||
|
|
160
|
+
schema.required.some((name) => typeof name !== "string") ||
|
|
161
|
+
new Set(schema.required).size !== schema.required.length)
|
|
162
|
+
)
|
|
163
|
+
invalid("required requires unique property names.", pointer);
|
|
164
|
+
for (const key of ["items", "additionalProperties"])
|
|
165
|
+
if (schema[key] !== undefined)
|
|
166
|
+
visit(schema[key], false, `${pointer}/${key}`, depth + 1);
|
|
167
|
+
if (schema.anyOf !== undefined) {
|
|
168
|
+
if (!Array.isArray(schema.anyOf) || !schema.anyOf.length)
|
|
169
|
+
invalid("anyOf requires nonempty full branches.", pointer);
|
|
170
|
+
for (const [index, child] of schema.anyOf.entries())
|
|
171
|
+
visit(child, false, `${pointer}/anyOf/${String(index)}`, depth + 1);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
visit(value, true, "#", 0);
|
|
175
|
+
for (const ref of references)
|
|
176
|
+
if (!definitions.has(ref)) invalid(`Unknown schema pointer ${ref}.`);
|
|
177
|
+
const active = new Set<JsonSchema>();
|
|
178
|
+
const done = new Set<JsonSchema>();
|
|
179
|
+
const productive = (schema: JsonSchema, depth = 0): void => {
|
|
180
|
+
charge();
|
|
181
|
+
if (depth > (options.depth ?? 64))
|
|
182
|
+
throw new ContentBundleError(
|
|
183
|
+
"LIMIT_EXCEEDED",
|
|
184
|
+
"Schema reference depth exceeded.",
|
|
185
|
+
);
|
|
186
|
+
if (typeof schema === "boolean" || done.has(schema)) return;
|
|
187
|
+
if (active.has(schema)) invalid("Nonproductive schema reference cycle.");
|
|
188
|
+
active.add(schema);
|
|
189
|
+
if (typeof schema.$ref === "string")
|
|
190
|
+
productive(definitions.get(schema.$ref)!, depth + 1);
|
|
191
|
+
if (Array.isArray(schema.anyOf))
|
|
192
|
+
for (const child of schema.anyOf)
|
|
193
|
+
productive(child as JsonSchema, depth + 1);
|
|
194
|
+
active.delete(schema);
|
|
195
|
+
done.add(schema);
|
|
196
|
+
};
|
|
197
|
+
for (const schema of definitions.values()) productive(schema);
|
|
198
|
+
return {
|
|
199
|
+
schema: definitions.get("#")!,
|
|
200
|
+
definitions,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function checkSize(bytes: Uint8Array, options: SchemaOptions): void {
|
|
205
|
+
if (
|
|
206
|
+
bytes.byteLength >
|
|
207
|
+
Math.min(schemaBlockBytes, options.schemaBytes ?? schemaBlockBytes)
|
|
208
|
+
)
|
|
209
|
+
throw new ContentBundleError(
|
|
210
|
+
"LIMIT_EXCEEDED",
|
|
211
|
+
"Schema exceeds its single-block byte limit.",
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function schemaBlock(
|
|
216
|
+
cid: CID,
|
|
217
|
+
bytes: Uint8Array,
|
|
218
|
+
definitions: SchemaDefinitions & { manifest: BundleManifest },
|
|
219
|
+
): SchemaBlock {
|
|
220
|
+
const identity = Uint8Array.from(cid.bytes);
|
|
221
|
+
const encoded = Uint8Array.from(bytes);
|
|
222
|
+
return Object.freeze({
|
|
223
|
+
...definitions,
|
|
224
|
+
get cid() {
|
|
225
|
+
return CID.decode(Uint8Array.from(identity));
|
|
226
|
+
},
|
|
227
|
+
get bytes() {
|
|
228
|
+
return Uint8Array.from(encoded);
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function inspectBundleSchema(
|
|
234
|
+
value: unknown,
|
|
235
|
+
options: SchemaOptions = {},
|
|
236
|
+
): SchemaDefinitions & { manifest: BundleManifest } {
|
|
237
|
+
let manifest: BundleManifest;
|
|
238
|
+
try {
|
|
239
|
+
assertJson(value, options.nodes, options.jsonDepth, options.charge);
|
|
240
|
+
manifest = normalizeBundleManifest(value);
|
|
241
|
+
} catch (cause) {
|
|
242
|
+
if (cause instanceof ContentBundleError && cause.code !== "CONTENT_INVALID")
|
|
243
|
+
throw cause;
|
|
244
|
+
throw new ContentBundleError(
|
|
245
|
+
"SCHEMA_INVALID",
|
|
246
|
+
`Invalid bundle type definition: ${cause instanceof Error ? cause.message : "invalid data"}`,
|
|
247
|
+
"",
|
|
248
|
+
"",
|
|
249
|
+
[],
|
|
250
|
+
{ cause },
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
const definitions = inspectSchema(manifest.entry.schema, options);
|
|
254
|
+
try {
|
|
255
|
+
validateValue(definitions, manifest.entry.defaultValue, {
|
|
256
|
+
charge: options.charge ?? (() => {}),
|
|
257
|
+
depth: options.depth ?? 64,
|
|
258
|
+
});
|
|
259
|
+
} catch (cause) {
|
|
260
|
+
if (
|
|
261
|
+
!(cause instanceof ContentBundleError) ||
|
|
262
|
+
cause.code !== "CONTENT_INVALID"
|
|
263
|
+
)
|
|
264
|
+
throw cause;
|
|
265
|
+
throw new ContentBundleError(
|
|
266
|
+
"SCHEMA_INVALID",
|
|
267
|
+
"defaultValue does not satisfy entry.schema.",
|
|
268
|
+
"",
|
|
269
|
+
`/entry/defaultValue${cause.pointer}`,
|
|
270
|
+
[],
|
|
271
|
+
{ cause },
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
return { ...definitions, manifest };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export async function encodeSchema(
|
|
278
|
+
value: unknown,
|
|
279
|
+
options: SchemaOptions = {},
|
|
280
|
+
): Promise<SchemaBlock> {
|
|
281
|
+
const definitions = inspectBundleSchema(value, options);
|
|
282
|
+
const bytes = dagCbor.encode(definitions.manifest);
|
|
283
|
+
checkSize(bytes, options);
|
|
284
|
+
const cid = CID.createV1(dagCbor.code, await sha256.digest(bytes));
|
|
285
|
+
return schemaBlock(
|
|
286
|
+
cid,
|
|
287
|
+
bytes,
|
|
288
|
+
inspectBundleSchema(dagCbor.decode(bytes), options),
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export async function decodeSchema(
|
|
293
|
+
cid: CID,
|
|
294
|
+
bytes: Uint8Array,
|
|
295
|
+
options: SchemaOptions = {},
|
|
296
|
+
): Promise<SchemaBlock> {
|
|
297
|
+
if (
|
|
298
|
+
cid.version !== 1 ||
|
|
299
|
+
cid.code !== dagCbor.code ||
|
|
300
|
+
cid.multihash.code !== sha256.code
|
|
301
|
+
)
|
|
302
|
+
invalid("Schema must be a CIDv1 DAG-CBOR SHA-256 block.");
|
|
303
|
+
checkSize(bytes, options);
|
|
304
|
+
const digest = await sha256.digest(bytes);
|
|
305
|
+
if (
|
|
306
|
+
digest.digest.length !== cid.multihash.digest.length ||
|
|
307
|
+
!digest.digest.every((byte, index) => byte === cid.multihash.digest[index])
|
|
308
|
+
)
|
|
309
|
+
throw new ContentBundleError(
|
|
310
|
+
"CONTENT_UNAVAILABLE",
|
|
311
|
+
"Corrupt schema block.",
|
|
312
|
+
);
|
|
313
|
+
let value: unknown;
|
|
314
|
+
try {
|
|
315
|
+
value = dagCbor.decode(bytes);
|
|
316
|
+
} catch (cause) {
|
|
317
|
+
throw new ContentBundleError(
|
|
318
|
+
"SCHEMA_INVALID",
|
|
319
|
+
"Invalid DAG-CBOR schema.",
|
|
320
|
+
"",
|
|
321
|
+
"",
|
|
322
|
+
[],
|
|
323
|
+
{ cause },
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
const definitions = inspectBundleSchema(value, options);
|
|
327
|
+
const canonical = dagCbor.encode(definitions.manifest);
|
|
328
|
+
if (
|
|
329
|
+
canonical.length !== bytes.length ||
|
|
330
|
+
!canonical.every((byte, index) => byte === bytes[index])
|
|
331
|
+
)
|
|
332
|
+
invalid("Schema block must use canonical DAG-CBOR encoding.");
|
|
333
|
+
return schemaBlock(cid, bytes, definitions);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function equal(a: unknown, b: unknown, charge: () => void): boolean {
|
|
337
|
+
charge();
|
|
338
|
+
if (a === b) return true;
|
|
339
|
+
if (Array.isArray(a) && Array.isArray(b))
|
|
340
|
+
return (
|
|
341
|
+
a.length === b.length &&
|
|
342
|
+
a.every((item, index) => equal(item, b[index], charge))
|
|
343
|
+
);
|
|
344
|
+
if (record(a) && record(b))
|
|
345
|
+
return (
|
|
346
|
+
Object.keys(a).length === Object.keys(b).length &&
|
|
347
|
+
Object.keys(a).every(
|
|
348
|
+
(key) => Object.hasOwn(b, key) && equal(a[key], b[key], charge),
|
|
349
|
+
)
|
|
350
|
+
);
|
|
351
|
+
return false;
|
|
352
|
+
}
|
|
353
|
+
export interface ValidationContext {
|
|
354
|
+
charge(this: void): void;
|
|
355
|
+
depth: number;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export function validateValue(
|
|
359
|
+
definitions: SchemaDefinitions,
|
|
360
|
+
value: unknown,
|
|
361
|
+
context: ValidationContext,
|
|
362
|
+
): void {
|
|
363
|
+
const fail = (pointer: string): never => {
|
|
364
|
+
throw new ContentBundleError(
|
|
365
|
+
"CONTENT_INVALID",
|
|
366
|
+
"Data does not satisfy its schema.",
|
|
367
|
+
"",
|
|
368
|
+
pointer,
|
|
369
|
+
);
|
|
370
|
+
};
|
|
371
|
+
const visit = (
|
|
372
|
+
schema: JsonSchema,
|
|
373
|
+
data: unknown,
|
|
374
|
+
pointer: string,
|
|
375
|
+
depth: number,
|
|
376
|
+
): void => {
|
|
377
|
+
context.charge();
|
|
378
|
+
if (depth > context.depth)
|
|
379
|
+
throw new ContentBundleError(
|
|
380
|
+
"LIMIT_EXCEEDED",
|
|
381
|
+
"Validation depth exceeded.",
|
|
382
|
+
"",
|
|
383
|
+
pointer,
|
|
384
|
+
);
|
|
385
|
+
if (schema === true) return;
|
|
386
|
+
if (schema === false) return fail(pointer);
|
|
387
|
+
const type = schema.type;
|
|
388
|
+
if (type !== undefined) {
|
|
389
|
+
const matches =
|
|
390
|
+
type === "null"
|
|
391
|
+
? data === null
|
|
392
|
+
: type === "array"
|
|
393
|
+
? Array.isArray(data)
|
|
394
|
+
: type === "object"
|
|
395
|
+
? record(data)
|
|
396
|
+
: type === "integer"
|
|
397
|
+
? typeof data === "number" && Number.isSafeInteger(data)
|
|
398
|
+
: type === "number"
|
|
399
|
+
? typeof data === "number" && Number.isFinite(data)
|
|
400
|
+
: typeof data === type;
|
|
401
|
+
if (!matches) fail(pointer);
|
|
402
|
+
}
|
|
403
|
+
if (
|
|
404
|
+
Object.hasOwn(schema, "const") &&
|
|
405
|
+
!equal(schema.const, data, context.charge)
|
|
406
|
+
)
|
|
407
|
+
fail(pointer);
|
|
408
|
+
if (
|
|
409
|
+
Array.isArray(schema.enum) &&
|
|
410
|
+
!schema.enum.some((item) => equal(item, data, context.charge))
|
|
411
|
+
)
|
|
412
|
+
fail(pointer);
|
|
413
|
+
if (typeof schema.$ref === "string")
|
|
414
|
+
visit(
|
|
415
|
+
definitions.definitions.get(schema.$ref)!,
|
|
416
|
+
data,
|
|
417
|
+
pointer,
|
|
418
|
+
depth + 1,
|
|
419
|
+
);
|
|
420
|
+
if (record(data)) {
|
|
421
|
+
if (
|
|
422
|
+
Array.isArray(schema.required) &&
|
|
423
|
+
schema.required.some((name) => !Object.hasOwn(data, name as string))
|
|
424
|
+
)
|
|
425
|
+
fail(pointer);
|
|
426
|
+
const properties = record(schema.properties) ? schema.properties : {};
|
|
427
|
+
for (const [name, child] of Object.entries(data)) {
|
|
428
|
+
const rule = Object.hasOwn(properties, name)
|
|
429
|
+
? (properties[name] as JsonSchema)
|
|
430
|
+
: (schema.additionalProperties as JsonSchema | undefined);
|
|
431
|
+
if (rule !== undefined)
|
|
432
|
+
visit(rule, child, `${pointer}/${pointerPart(name)}`, depth + 1);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
if (Array.isArray(data) && schema.items !== undefined)
|
|
436
|
+
for (const [index, child] of data.entries())
|
|
437
|
+
visit(
|
|
438
|
+
schema.items as JsonSchema,
|
|
439
|
+
child,
|
|
440
|
+
`${pointer}/${String(index)}`,
|
|
441
|
+
depth + 1,
|
|
442
|
+
);
|
|
443
|
+
if (Array.isArray(schema.anyOf)) {
|
|
444
|
+
let success = false;
|
|
445
|
+
for (const child of schema.anyOf) {
|
|
446
|
+
try {
|
|
447
|
+
visit(child as JsonSchema, data, pointer, depth + 1);
|
|
448
|
+
success = true;
|
|
449
|
+
break;
|
|
450
|
+
} catch (cause) {
|
|
451
|
+
if (
|
|
452
|
+
!(cause instanceof ContentBundleError) ||
|
|
453
|
+
cause.code !== "CONTENT_INVALID"
|
|
454
|
+
)
|
|
455
|
+
throw cause;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
if (!success) fail(pointer);
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
visit(definitions.schema, value, "", 0);
|
|
462
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { CID } from "multiformats/cid";
|
|
2
|
+
|
|
3
|
+
export type JsonSchema = boolean | { readonly [key: string]: unknown };
|
|
4
|
+
export type ContentType =
|
|
5
|
+
"application/json" | "application/yaml" | "application/jsonl";
|
|
6
|
+
export interface BundleManifest {
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly description: string;
|
|
9
|
+
readonly entry: {
|
|
10
|
+
readonly path: string;
|
|
11
|
+
readonly schema: JsonSchema;
|
|
12
|
+
readonly contentType: ContentType;
|
|
13
|
+
readonly defaultValue: unknown;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export interface SchemaDefinitions {
|
|
17
|
+
readonly schema: JsonSchema;
|
|
18
|
+
readonly definitions: ReadonlyMap<string, JsonSchema>;
|
|
19
|
+
}
|
|
20
|
+
export interface SchemaBlock extends SchemaDefinitions {
|
|
21
|
+
readonly manifest: BundleManifest;
|
|
22
|
+
readonly cid: CID;
|
|
23
|
+
readonly bytes: Uint8Array;
|
|
24
|
+
}
|
|
25
|
+
export interface BundleLimits {
|
|
26
|
+
bytes: number;
|
|
27
|
+
blocks: number;
|
|
28
|
+
blockBytes: number;
|
|
29
|
+
parsingBytes: number;
|
|
30
|
+
files: number;
|
|
31
|
+
depth: number;
|
|
32
|
+
jsonDepth: number;
|
|
33
|
+
validationDepth: number;
|
|
34
|
+
manifestBytes: number;
|
|
35
|
+
schemaBytes: number;
|
|
36
|
+
documentBytes: number;
|
|
37
|
+
nodes: number;
|
|
38
|
+
}
|
|
39
|
+
export interface OpenBundleOptions {
|
|
40
|
+
resolveCid(cid: CID, options: { signal?: AbortSignal }): Promise<Uint8Array>;
|
|
41
|
+
signal?: AbortSignal;
|
|
42
|
+
limits?: Partial<BundleLimits>;
|
|
43
|
+
}
|
|
44
|
+
export interface ContentBundle {
|
|
45
|
+
readonly kind: "content";
|
|
46
|
+
readonly cid: CID;
|
|
47
|
+
readonly manifest: BundleManifest;
|
|
48
|
+
readonly schemaKey: string;
|
|
49
|
+
readonly schema: SchemaBlock;
|
|
50
|
+
readFile(
|
|
51
|
+
path: string,
|
|
52
|
+
options?: { signal?: AbortSignal },
|
|
53
|
+
): Promise<ReadableStream<Uint8Array>>;
|
|
54
|
+
}
|
|
55
|
+
export type ContentBundleErrorCode =
|
|
56
|
+
| "MANIFEST_INVALID"
|
|
57
|
+
| "PATH_INVALID"
|
|
58
|
+
| "REFERENCE_INVALID"
|
|
59
|
+
| "SCHEMA_INVALID"
|
|
60
|
+
| "CONTENT_INVALID"
|
|
61
|
+
| "CONTENT_UNAVAILABLE"
|
|
62
|
+
| "LIMIT_EXCEEDED"
|
|
63
|
+
| "OPTIONS_INVALID"
|
|
64
|
+
| "ABORTED";
|