@ipab/bundle 0.1.0-rc.0 → 0.1.0-rc.1
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/README.md +82 -47
- package/dist/bundle.d.ts.map +1 -1
- package/dist/bundle.js +12 -3
- package/dist/bundle.js.map +1 -1
- package/dist/data.d.ts +2 -2
- package/dist/data.d.ts.map +1 -1
- package/dist/data.js +2 -2
- package/dist/data.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/parsing.d.ts +2 -1
- package/dist/parsing.d.ts.map +1 -1
- package/dist/parsing.js +52 -14
- package/dist/parsing.js.map +1 -1
- package/dist/schema.d.ts +1 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +227 -181
- package/dist/schema.js.map +1 -1
- package/dist/types.d.ts +7 -3
- package/dist/types.d.ts.map +1 -1
- package/package.json +10 -8
- package/src/bundle.ts +23 -9
- package/src/data.ts +7 -1
- package/src/index.ts +2 -0
- package/src/parsing.ts +74 -17
- package/src/schema.ts +292 -244
- package/src/types.ts +7 -4
package/src/schema.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
2
|
+
import { Check, Errors } from "typebox/schema";
|
|
1
3
|
import { ContentBundleError } from "./errors.js";
|
|
2
4
|
import { assertJson, record, normalizeBundleManifest } from "./parsing.js";
|
|
3
5
|
import * as dagCbor from "@ipld/dag-cbor";
|
|
@@ -11,6 +13,9 @@ import type {
|
|
|
11
13
|
} from "./types.js";
|
|
12
14
|
|
|
13
15
|
export const schemaBlockBytes = 1024 * 1024;
|
|
16
|
+
export const schemaDialect = "https://json-schema.org/draft/2020-12/schema";
|
|
17
|
+
const meta = new Ajv2020({ strict: false, validateFormats: false });
|
|
18
|
+
|
|
14
19
|
export function declaresBundleSchema(bytes: Uint8Array): boolean {
|
|
15
20
|
const value: unknown = dagCbor.decode(bytes);
|
|
16
21
|
return record(value) && Object.hasOwn(value, "entry");
|
|
@@ -22,7 +27,6 @@ export interface SchemaOptions {
|
|
|
22
27
|
schemaBytes?: number;
|
|
23
28
|
charge?: () => void;
|
|
24
29
|
}
|
|
25
|
-
|
|
26
30
|
export function pointerPart(value: string): string {
|
|
27
31
|
return value.replaceAll("~", "~0").replaceAll("/", "~1");
|
|
28
32
|
}
|
|
@@ -38,42 +42,59 @@ export function absoluteId(value: unknown): value is string {
|
|
|
38
42
|
function invalid(message: string, pointer = ""): never {
|
|
39
43
|
throw new ContentBundleError("SCHEMA_INVALID", message, "", pointer);
|
|
40
44
|
}
|
|
41
|
-
const
|
|
42
|
-
"$id",
|
|
43
|
-
"$ref",
|
|
45
|
+
const schemaMaps = [
|
|
44
46
|
"$defs",
|
|
45
|
-
"
|
|
46
|
-
"enum",
|
|
47
|
-
"const",
|
|
47
|
+
"definitions",
|
|
48
48
|
"properties",
|
|
49
|
-
"
|
|
49
|
+
"patternProperties",
|
|
50
|
+
"dependentSchemas",
|
|
51
|
+
];
|
|
52
|
+
const schemaArrays = ["allOf", "anyOf", "oneOf", "prefixItems"];
|
|
53
|
+
const schemaChildren = [
|
|
50
54
|
"additionalProperties",
|
|
55
|
+
"unevaluatedProperties",
|
|
56
|
+
"propertyNames",
|
|
51
57
|
"items",
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
58
|
+
"unevaluatedItems",
|
|
59
|
+
"contains",
|
|
60
|
+
"not",
|
|
61
|
+
"if",
|
|
62
|
+
"then",
|
|
63
|
+
"else",
|
|
64
|
+
"contentSchema",
|
|
65
|
+
];
|
|
66
|
+
const annotations = new Set([
|
|
67
|
+
"format",
|
|
68
|
+
"dependencies",
|
|
69
|
+
"additionalItems",
|
|
70
|
+
"id",
|
|
71
|
+
"$recursiveRef",
|
|
72
|
+
"$recursiveAnchor",
|
|
73
|
+
"optional",
|
|
74
|
+
"~kind",
|
|
75
|
+
"~refine",
|
|
64
76
|
]);
|
|
77
|
+
function schemaUri(reference: string, base: string): URL {
|
|
78
|
+
return URL.canParse(reference, base)
|
|
79
|
+
? new URL(reference, base)
|
|
80
|
+
: new URL(
|
|
81
|
+
base === "urn:typebox:root"
|
|
82
|
+
? `${base}:${reference}`
|
|
83
|
+
: `${base.slice(0, base.lastIndexOf(":"))}:${reference}`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
65
86
|
|
|
66
87
|
export function inspectSchema(
|
|
67
88
|
value: unknown,
|
|
68
89
|
options: SchemaOptions = {},
|
|
69
90
|
): SchemaDefinitions {
|
|
70
|
-
for (const [key,
|
|
91
|
+
for (const [key, limit] of Object.entries(options))
|
|
71
92
|
if (
|
|
72
93
|
key === "charge"
|
|
73
|
-
? typeof
|
|
94
|
+
? typeof limit !== "function"
|
|
74
95
|
: !["nodes", "depth", "jsonDepth", "schemaBytes"].includes(key) ||
|
|
75
|
-
!Number.isSafeInteger(
|
|
76
|
-
(
|
|
96
|
+
!Number.isSafeInteger(limit) ||
|
|
97
|
+
(limit as number) <= 0
|
|
77
98
|
)
|
|
78
99
|
throw new ContentBundleError(
|
|
79
100
|
"OPTIONS_INVALID",
|
|
@@ -87,93 +108,200 @@ export function inspectSchema(
|
|
|
87
108
|
invalid(cause.message);
|
|
88
109
|
throw cause;
|
|
89
110
|
}
|
|
111
|
+
if (typeof value !== "boolean" && !record(value))
|
|
112
|
+
invalid("Expected a JSON Schema object or boolean.");
|
|
90
113
|
const definitions = new Map<string, JsonSchema>();
|
|
91
|
-
const
|
|
114
|
+
const anchors = new Map<string, JsonSchema>();
|
|
115
|
+
const resources = new Map<string, string>();
|
|
116
|
+
const references: {
|
|
117
|
+
schema: JsonSchema;
|
|
118
|
+
reference: string;
|
|
119
|
+
pointer: string;
|
|
120
|
+
resource: string;
|
|
121
|
+
base: string;
|
|
122
|
+
}[] = [];
|
|
123
|
+
const targets = new Map<JsonSchema, JsonSchema[]>();
|
|
92
124
|
const visit = (
|
|
93
|
-
schema:
|
|
94
|
-
root: boolean,
|
|
125
|
+
schema: JsonSchema,
|
|
95
126
|
pointer: string,
|
|
96
127
|
depth: number,
|
|
128
|
+
resource = "#",
|
|
129
|
+
base = "urn:typebox:root",
|
|
97
130
|
): void => {
|
|
98
131
|
charge();
|
|
99
132
|
if (depth > (options.depth ?? 64))
|
|
100
133
|
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
134
|
definitions.set(pointer, schema);
|
|
115
|
-
if (
|
|
116
|
-
|
|
117
|
-
(
|
|
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) {
|
|
135
|
+
if (typeof schema === "boolean") return;
|
|
136
|
+
if (pointer === "#" && schema.$id === undefined)
|
|
137
|
+
resources.set(base, pointer);
|
|
138
|
+
if (schema.$id !== undefined) {
|
|
138
139
|
if (
|
|
139
|
-
typeof schema.$
|
|
140
|
-
|
|
140
|
+
typeof schema.$id !== "string" ||
|
|
141
|
+
[...schema.$id].some((character) => character.charCodeAt(0) <= 32) ||
|
|
142
|
+
/%(?![0-9A-Fa-f]{2})/u.test(schema.$id)
|
|
141
143
|
)
|
|
142
|
-
invalid("
|
|
143
|
-
|
|
144
|
+
invalid("Invalid schema resource $id.", pointer);
|
|
145
|
+
try {
|
|
146
|
+
base = schemaUri(schema.$id, base).href;
|
|
147
|
+
} catch (cause) {
|
|
148
|
+
throw new ContentBundleError(
|
|
149
|
+
"SCHEMA_INVALID",
|
|
150
|
+
"Invalid schema resource $id.",
|
|
151
|
+
"",
|
|
152
|
+
pointer,
|
|
153
|
+
[],
|
|
154
|
+
{ cause },
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
if (resources.has(base) && resources.get(base) !== pointer)
|
|
158
|
+
invalid("Duplicate schema resource $id.", pointer);
|
|
159
|
+
resources.set(base, pointer);
|
|
160
|
+
resource = pointer;
|
|
144
161
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
162
|
+
if (schema.$schema !== undefined && schema.$schema !== schemaDialect)
|
|
163
|
+
invalid(
|
|
164
|
+
`Only JSON Schema Draft 2020-12 is supported (${schemaDialect}).`,
|
|
165
|
+
pointer,
|
|
166
|
+
);
|
|
167
|
+
if (record(schema.$vocabulary))
|
|
168
|
+
for (const [vocabulary, required] of Object.entries(schema.$vocabulary))
|
|
169
|
+
if (
|
|
170
|
+
required === true &&
|
|
171
|
+
![
|
|
172
|
+
"core",
|
|
173
|
+
"applicator",
|
|
174
|
+
"unevaluated",
|
|
175
|
+
"validation",
|
|
176
|
+
"meta-data",
|
|
177
|
+
"format-annotation",
|
|
178
|
+
"content",
|
|
179
|
+
].some(
|
|
180
|
+
(name) =>
|
|
181
|
+
vocabulary ===
|
|
182
|
+
`https://json-schema.org/draft/2020-12/vocab/${name}`,
|
|
183
|
+
)
|
|
184
|
+
)
|
|
185
|
+
invalid(
|
|
186
|
+
`Unsupported required schema vocabulary ${vocabulary}.`,
|
|
187
|
+
pointer,
|
|
155
188
|
);
|
|
189
|
+
const patterns = [
|
|
190
|
+
...(typeof schema.pattern === "string" ? [schema.pattern] : []),
|
|
191
|
+
...(record(schema.patternProperties)
|
|
192
|
+
? Object.keys(schema.patternProperties)
|
|
193
|
+
: []),
|
|
194
|
+
];
|
|
195
|
+
for (const pattern of patterns)
|
|
196
|
+
try {
|
|
197
|
+
new RegExp(pattern, "u");
|
|
198
|
+
} catch (cause) {
|
|
199
|
+
throw new ContentBundleError(
|
|
200
|
+
"SCHEMA_INVALID",
|
|
201
|
+
"Invalid schema regular expression.",
|
|
202
|
+
"",
|
|
203
|
+
pointer,
|
|
204
|
+
[],
|
|
205
|
+
{ cause },
|
|
206
|
+
);
|
|
156
207
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
(
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
208
|
+
for (const keyword of ["$ref", "$dynamicRef"]) {
|
|
209
|
+
const reference = schema[keyword];
|
|
210
|
+
if (reference !== undefined) {
|
|
211
|
+
if (typeof reference !== "string")
|
|
212
|
+
invalid("Schema references must be URI strings.", pointer);
|
|
213
|
+
references.push({ schema, reference, pointer, resource, base });
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
for (const keyword of ["$anchor", "$dynamicAnchor"]) {
|
|
217
|
+
const anchor = schema[keyword];
|
|
218
|
+
if (typeof anchor === "string") {
|
|
219
|
+
const key = `${base}#${anchor}`;
|
|
220
|
+
if (anchors.has(key) && anchors.get(key) !== schema)
|
|
221
|
+
invalid("Duplicate schema anchor.", pointer);
|
|
222
|
+
anchors.set(key, schema);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
for (const keyword of schemaMaps) {
|
|
226
|
+
const children = schema[keyword];
|
|
227
|
+
if (record(children))
|
|
228
|
+
for (const [name, child] of Object.entries(children))
|
|
229
|
+
if (typeof child === "boolean" || record(child))
|
|
230
|
+
visit(
|
|
231
|
+
child,
|
|
232
|
+
`${pointer}/${keyword}/${pointerPart(name)}`,
|
|
233
|
+
depth + 1,
|
|
234
|
+
resource,
|
|
235
|
+
base,
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
for (const keyword of schemaArrays) {
|
|
239
|
+
const children = schema[keyword];
|
|
240
|
+
if (Array.isArray(children))
|
|
241
|
+
for (const [index, child] of children.entries())
|
|
242
|
+
if (typeof child === "boolean" || record(child))
|
|
243
|
+
visit(
|
|
244
|
+
child,
|
|
245
|
+
`${pointer}/${keyword}/${String(index)}`,
|
|
246
|
+
depth + 1,
|
|
247
|
+
resource,
|
|
248
|
+
base,
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
for (const keyword of schemaChildren) {
|
|
252
|
+
const child = schema[keyword];
|
|
253
|
+
if (typeof child === "boolean" || record(child))
|
|
254
|
+
visit(child, `${pointer}/${keyword}`, depth + 1, resource, base);
|
|
172
255
|
}
|
|
173
256
|
};
|
|
174
|
-
visit(value,
|
|
175
|
-
|
|
176
|
-
if (!
|
|
257
|
+
visit(value, "#", 0);
|
|
258
|
+
try {
|
|
259
|
+
if (!meta.validateSchema(value)) invalid(meta.errorsText(meta.errors));
|
|
260
|
+
} catch (cause) {
|
|
261
|
+
if (cause instanceof ContentBundleError) throw cause;
|
|
262
|
+
throw new ContentBundleError(
|
|
263
|
+
"SCHEMA_INVALID",
|
|
264
|
+
"Invalid Draft 2020-12 schema.",
|
|
265
|
+
"",
|
|
266
|
+
"",
|
|
267
|
+
[],
|
|
268
|
+
{ cause },
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
for (const { schema, reference, pointer, base } of references) {
|
|
272
|
+
let decoded: string;
|
|
273
|
+
let resource: string | undefined;
|
|
274
|
+
let uri: URL;
|
|
275
|
+
try {
|
|
276
|
+
uri = schemaUri(reference, base);
|
|
277
|
+
decoded = decodeURIComponent(uri.hash || "#");
|
|
278
|
+
uri.hash = "";
|
|
279
|
+
resource = resources.get(uri.href);
|
|
280
|
+
} catch (cause) {
|
|
281
|
+
throw new ContentBundleError(
|
|
282
|
+
"SCHEMA_INVALID",
|
|
283
|
+
"Invalid schema reference encoding.",
|
|
284
|
+
"",
|
|
285
|
+
pointer,
|
|
286
|
+
[],
|
|
287
|
+
{ cause },
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
if (resource === undefined)
|
|
291
|
+
invalid(
|
|
292
|
+
"Schema references must stay within the schema document; external loading is disabled.",
|
|
293
|
+
pointer,
|
|
294
|
+
);
|
|
295
|
+
const target =
|
|
296
|
+
decoded === "#" || decoded.startsWith("#/")
|
|
297
|
+
? definitions.get(`${resource}${decoded.slice(1)}`)
|
|
298
|
+
: anchors.get(`${uri.href}${decoded}`);
|
|
299
|
+
if (target === undefined)
|
|
300
|
+
invalid(`Unknown schema reference ${reference}.`, pointer);
|
|
301
|
+
const found = targets.get(schema) ?? [];
|
|
302
|
+
found.push(target);
|
|
303
|
+
targets.set(schema, found);
|
|
304
|
+
}
|
|
177
305
|
const active = new Set<JsonSchema>();
|
|
178
306
|
const done = new Set<JsonSchema>();
|
|
179
307
|
const productive = (schema: JsonSchema, depth = 0): void => {
|
|
@@ -186,19 +314,24 @@ export function inspectSchema(
|
|
|
186
314
|
if (typeof schema === "boolean" || done.has(schema)) return;
|
|
187
315
|
if (active.has(schema)) invalid("Nonproductive schema reference cycle.");
|
|
188
316
|
active.add(schema);
|
|
189
|
-
|
|
190
|
-
productive(
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
317
|
+
for (const target of targets.get(schema) ?? [])
|
|
318
|
+
productive(target, depth + 1);
|
|
319
|
+
for (const keyword of ["allOf", "anyOf", "oneOf"])
|
|
320
|
+
if (Array.isArray(schema[keyword]))
|
|
321
|
+
for (const child of schema[keyword])
|
|
322
|
+
productive(child as JsonSchema, depth + 1);
|
|
323
|
+
for (const keyword of ["not", "if", "then", "else", "dependentSchemas"])
|
|
324
|
+
if (typeof schema[keyword] === "boolean" || record(schema[keyword])) {
|
|
325
|
+
if (keyword === "dependentSchemas") {
|
|
326
|
+
for (const child of Object.values(schema[keyword]))
|
|
327
|
+
productive(child as JsonSchema, depth + 1);
|
|
328
|
+
} else productive(schema[keyword], depth + 1);
|
|
329
|
+
}
|
|
194
330
|
active.delete(schema);
|
|
195
331
|
done.add(schema);
|
|
196
332
|
};
|
|
197
333
|
for (const schema of definitions.values()) productive(schema);
|
|
198
|
-
return {
|
|
199
|
-
schema: definitions.get("#")!,
|
|
200
|
-
definitions,
|
|
201
|
-
};
|
|
334
|
+
return { schema: value, definitions };
|
|
202
335
|
}
|
|
203
336
|
|
|
204
337
|
function checkSize(bytes: Uint8Array, options: SchemaOptions): void {
|
|
@@ -211,7 +344,6 @@ function checkSize(bytes: Uint8Array, options: SchemaOptions): void {
|
|
|
211
344
|
"Schema exceeds its single-block byte limit.",
|
|
212
345
|
);
|
|
213
346
|
}
|
|
214
|
-
|
|
215
347
|
function schemaBlock(
|
|
216
348
|
cid: CID,
|
|
217
349
|
bytes: Uint8Array,
|
|
@@ -229,7 +361,6 @@ function schemaBlock(
|
|
|
229
361
|
},
|
|
230
362
|
});
|
|
231
363
|
}
|
|
232
|
-
|
|
233
364
|
export function inspectBundleSchema(
|
|
234
365
|
value: unknown,
|
|
235
366
|
options: SchemaOptions = {},
|
|
@@ -250,30 +381,8 @@ export function inspectBundleSchema(
|
|
|
250
381
|
{ cause },
|
|
251
382
|
);
|
|
252
383
|
}
|
|
253
|
-
|
|
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 };
|
|
384
|
+
return { ...inspectSchema(manifest.entry.schema ?? true, options), manifest };
|
|
275
385
|
}
|
|
276
|
-
|
|
277
386
|
export async function encodeSchema(
|
|
278
387
|
value: unknown,
|
|
279
388
|
options: SchemaOptions = {},
|
|
@@ -288,7 +397,6 @@ export async function encodeSchema(
|
|
|
288
397
|
inspectBundleSchema(dagCbor.decode(bytes), options),
|
|
289
398
|
);
|
|
290
399
|
}
|
|
291
|
-
|
|
292
400
|
export async function decodeSchema(
|
|
293
401
|
cid: CID,
|
|
294
402
|
bytes: Uint8Array,
|
|
@@ -332,131 +440,71 @@ export async function decodeSchema(
|
|
|
332
440
|
invalid("Schema block must use canonical DAG-CBOR encoding.");
|
|
333
441
|
return schemaBlock(cid, bytes, definitions);
|
|
334
442
|
}
|
|
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
443
|
export interface ValidationContext {
|
|
354
444
|
charge(this: void): void;
|
|
355
445
|
depth: number;
|
|
356
446
|
}
|
|
357
|
-
|
|
358
447
|
export function validateValue(
|
|
359
448
|
definitions: SchemaDefinitions,
|
|
360
449
|
value: unknown,
|
|
361
450
|
context: ValidationContext,
|
|
362
451
|
): void {
|
|
363
|
-
|
|
452
|
+
assertJson(value, 100_000, context.depth, context.charge);
|
|
453
|
+
const schemas = new Set<object>(
|
|
454
|
+
[...definitions.definitions.values()].filter(record),
|
|
455
|
+
);
|
|
456
|
+
const proxies = new WeakMap<object, object>();
|
|
457
|
+
// Meter interpreter accesses without modifying caller-owned or frozen JSON.
|
|
458
|
+
const track = (value: unknown): unknown => {
|
|
459
|
+
if (value === null || typeof value !== "object") return value;
|
|
460
|
+
const existing = proxies.get(value);
|
|
461
|
+
if (existing) return existing;
|
|
462
|
+
const target = Array.isArray(value)
|
|
463
|
+
? [...(value as unknown[])]
|
|
464
|
+
: { ...value };
|
|
465
|
+
const isSchema = schemas.has(value);
|
|
466
|
+
const proxy = new Proxy(target, {
|
|
467
|
+
get(target, key, receiver): unknown {
|
|
468
|
+
context.charge();
|
|
469
|
+
if (isSchema && typeof key === "string" && annotations.has(key))
|
|
470
|
+
return undefined;
|
|
471
|
+
return track(Reflect.get(target, key, receiver));
|
|
472
|
+
},
|
|
473
|
+
ownKeys(target) {
|
|
474
|
+
context.charge();
|
|
475
|
+
return Reflect.ownKeys(target);
|
|
476
|
+
},
|
|
477
|
+
has(target, key) {
|
|
478
|
+
context.charge();
|
|
479
|
+
return Reflect.has(target, key);
|
|
480
|
+
},
|
|
481
|
+
});
|
|
482
|
+
proxies.set(value, proxy);
|
|
483
|
+
return proxy;
|
|
484
|
+
};
|
|
485
|
+
const trackedSchema = track(definitions.schema);
|
|
486
|
+
if (typeof trackedSchema !== "boolean" && !record(trackedSchema))
|
|
487
|
+
invalid("Expected a JSON Schema object or boolean.");
|
|
488
|
+
try {
|
|
489
|
+
const trackedValue = track(value);
|
|
490
|
+
if (Check(trackedSchema, trackedValue)) return;
|
|
491
|
+
const [, errors] = Errors(trackedSchema, trackedValue);
|
|
492
|
+
const error = errors[0];
|
|
364
493
|
throw new ContentBundleError(
|
|
365
494
|
"CONTENT_INVALID",
|
|
366
|
-
"Data does not satisfy its schema.",
|
|
495
|
+
error?.message ?? "Data does not satisfy its schema.",
|
|
367
496
|
"",
|
|
368
|
-
|
|
497
|
+
error?.instancePath ?? "",
|
|
369
498
|
);
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
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);
|
|
499
|
+
} catch (cause) {
|
|
500
|
+
if (cause instanceof ContentBundleError) throw cause;
|
|
501
|
+
throw new ContentBundleError(
|
|
502
|
+
"SCHEMA_INVALID",
|
|
503
|
+
"Schema evaluation failed.",
|
|
504
|
+
"",
|
|
505
|
+
"",
|
|
506
|
+
[],
|
|
507
|
+
{ cause },
|
|
508
|
+
);
|
|
509
|
+
}
|
|
462
510
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import type { CID } from "multiformats/cid";
|
|
2
2
|
|
|
3
3
|
export type JsonSchema = boolean | { readonly [key: string]: unknown };
|
|
4
|
-
export type ContentType =
|
|
5
|
-
"application/json" | "application/yaml" | "application/jsonl";
|
|
4
|
+
export type ContentType = string;
|
|
6
5
|
export interface BundleManifest {
|
|
7
6
|
readonly name: string;
|
|
8
7
|
readonly description: string;
|
|
9
8
|
readonly entry: {
|
|
10
9
|
readonly path: string;
|
|
11
|
-
readonly schema
|
|
10
|
+
readonly schema?: JsonSchema;
|
|
12
11
|
readonly contentType: ContentType;
|
|
13
|
-
readonly defaultValue: unknown;
|
|
14
12
|
};
|
|
15
13
|
}
|
|
16
14
|
export interface SchemaDefinitions {
|
|
@@ -47,11 +45,16 @@ export interface ContentBundle {
|
|
|
47
45
|
readonly manifest: BundleManifest;
|
|
48
46
|
readonly schemaKey: string;
|
|
49
47
|
readonly schema: SchemaBlock;
|
|
48
|
+
readonly validation: EntryValidation;
|
|
50
49
|
readFile(
|
|
51
50
|
path: string,
|
|
52
51
|
options?: { signal?: AbortSignal },
|
|
53
52
|
): Promise<ReadableStream<Uint8Array>>;
|
|
54
53
|
}
|
|
54
|
+
export type EntryValidation = {
|
|
55
|
+
readonly status: "validated" | "schema-absent" | "unsupported";
|
|
56
|
+
readonly contentType: string;
|
|
57
|
+
};
|
|
55
58
|
export type ContentBundleErrorCode =
|
|
56
59
|
| "MANIFEST_INVALID"
|
|
57
60
|
| "PATH_INVALID"
|