@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/src/bundle.ts CHANGED
@@ -2,7 +2,12 @@ import { ContentError, IpfsStore, openContent } from "@ipab/ipfs-store";
2
2
  import type { UnixfsNode } from "@ipab/ipfs-store";
3
3
  import { CID } from "multiformats/cid";
4
4
  import { ContentBundleError } from "./errors.js";
5
- import { parseBundleManifest, parseData, safePath } from "./parsing.js";
5
+ import {
6
+ entryValidation,
7
+ parseBundleManifest,
8
+ parseData,
9
+ safePath,
10
+ } from "./parsing.js";
6
11
  import {
7
12
  decodeSchema,
8
13
  encodeSchema,
@@ -339,15 +344,23 @@ async function operation(
339
344
  "Schema CID differs from the explicit validator.",
340
345
  );
341
346
  const path = manifest.entry.path;
347
+ const validation = entryValidation(manifest.entry);
342
348
  try {
343
- const value = parseData(
344
- await bytesAt(path, limits.documentBytes),
345
- manifest.entry.contentType,
346
- limits.nodes,
347
- limits.jsonDepth,
348
- charge,
349
- );
350
- validateValue(schema, value, { charge, depth: limits.validationDepth });
349
+ const bytes = await bytesAt(path, limits.documentBytes);
350
+ if (validation.status !== "unsupported") {
351
+ const value = parseData(
352
+ bytes,
353
+ manifest.entry.contentType.toLowerCase(),
354
+ limits.nodes,
355
+ limits.jsonDepth,
356
+ charge,
357
+ );
358
+ if (manifest.entry.schema !== undefined)
359
+ validateValue(schema, value, {
360
+ charge,
361
+ depth: limits.validationDepth,
362
+ });
363
+ }
351
364
  } catch (cause) {
352
365
  throw contextual(cause, path, "", [], options.signal);
353
366
  }
@@ -359,6 +372,7 @@ async function operation(
359
372
  manifest,
360
373
  schemaKey: schema.cid.toString(),
361
374
  schema,
375
+ validation,
362
376
  readFile: reader(cid, files, options, limits),
363
377
  });
364
378
  } catch (cause) {
package/src/data.ts CHANGED
@@ -1,7 +1,13 @@
1
- export { parseBundleManifest, parseData, canonicalJson } from "./parsing.js";
1
+ export {
2
+ parseBundleManifest,
3
+ parseData,
4
+ canonicalJson,
5
+ safePath,
6
+ } from "./parsing.js";
2
7
  export {
3
8
  encodeSchema,
4
9
  inspectSchema,
10
+ schemaDialect,
5
11
  validateValue as validateSchemaValue,
6
12
  } from "./schema.js";
7
13
  export type { SchemaOptions, ValidationContext } from "./schema.js";
package/src/index.ts CHANGED
@@ -17,6 +17,7 @@ export {
17
17
  encodeSchema,
18
18
  decodeSchema,
19
19
  schemaBlockBytes,
20
+ schemaDialect,
20
21
  declaresBundleSchema,
21
22
  validateValue as validateSchemaValue,
22
23
  } from "./schema.js";
@@ -24,6 +25,7 @@ export type { SchemaOptions, ValidationContext } from "./schema.js";
24
25
  export { ContentBundleError } from "./errors.js";
25
26
  export type {
26
27
  ContentBundle,
28
+ EntryValidation,
27
29
  OpenBundleOptions,
28
30
  BundleLimits,
29
31
  BundleManifest,
package/src/parsing.ts CHANGED
@@ -1,6 +1,31 @@
1
1
  import { isMap, isSeq, parseAllDocuments } from "yaml";
2
2
  import { ContentBundleError } from "./errors.js";
3
- import type { BundleManifest, ContentType } from "./types.js";
3
+ import type { BundleManifest, ContentType, EntryValidation } from "./types.js";
4
+
5
+ const mimeToken = "[!#$%&'*+.^_`|~0-9A-Za-z-]+";
6
+ const mime = new RegExp(
7
+ `^${mimeToken}/${mimeToken}(?:[ \\t]*;[ \\t]*${mimeToken}[ \\t]*=[ \\t]*(?:${mimeToken}|"(?:[\\x20\\x21\\x23-\\x5b\\x5d-\\x7e]|\\\\[\\x20-\\x7e])*"))*$`,
8
+ "u",
9
+ );
10
+ const dataTypes = new Set([
11
+ "application/json",
12
+ "application/yaml",
13
+ "application/jsonl",
14
+ "text/markdown",
15
+ ]);
16
+
17
+ export function entryValidation(
18
+ entry: BundleManifest["entry"],
19
+ ): EntryValidation {
20
+ return Object.freeze({
21
+ status: dataTypes.has(entry.contentType.toLowerCase())
22
+ ? entry.schema === undefined
23
+ ? "schema-absent"
24
+ : "validated"
25
+ : "unsupported",
26
+ contentType: entry.contentType,
27
+ });
28
+ }
4
29
 
5
30
  export function record(value: unknown): value is Record<string, unknown> {
6
31
  return value !== null && typeof value === "object" && !Array.isArray(value);
@@ -187,6 +212,24 @@ export function canonicalJson(
187
212
  return new TextEncoder().encode(serialize(value));
188
213
  }
189
214
 
215
+ function markdownFrontmatter(source: string): string {
216
+ const opening = /^---(?:\r?\n|$)/u.exec(source);
217
+ if (!opening) return "{}";
218
+ const closing = /\n---(?:\r?\n|$)/gu;
219
+ closing.lastIndex = opening[0].length - 1;
220
+ const end = closing.exec(source);
221
+ if (!end)
222
+ throw new ContentBundleError(
223
+ "CONTENT_INVALID",
224
+ "Markdown frontmatter requires a closing --- line.",
225
+ );
226
+ const frontmatter = source.slice(opening[0].length, end.index + 1);
227
+ const empty = frontmatter
228
+ .split(/\r?\n|\r/u)
229
+ .every((line) => /^[ \t]*(?:#[^\r\n]*)?$/u.test(line));
230
+ return empty ? "{}" : frontmatter;
231
+ }
232
+
190
233
  export function parseData(
191
234
  bytes: Uint8Array,
192
235
  type: ContentType,
@@ -195,8 +238,16 @@ export function parseData(
195
238
  charge: () => void = () => {},
196
239
  ): unknown {
197
240
  jsonLimits(nodes, depth);
241
+ if (!dataTypes.has(type.toLowerCase()))
242
+ throw new ContentBundleError(
243
+ "CONTENT_INVALID",
244
+ `No data parser is available for content type ${type}.`,
245
+ );
246
+ type = type.toLowerCase();
198
247
  try {
199
- const source = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
248
+ const decoded = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
249
+ const source =
250
+ type === "text/markdown" ? markdownFrontmatter(decoded) : decoded;
200
251
  let value: unknown;
201
252
  if (type === "application/json") value = json(source, depth);
202
253
  else if (type === "application/jsonl") {
@@ -253,6 +304,11 @@ export function parseData(
253
304
  value = document.toJS({ maxAliasCount: 100 });
254
305
  }
255
306
  assertJson(value, nodes, depth, charge);
307
+ if (type === "text/markdown" && !record(value))
308
+ throw new ContentBundleError(
309
+ "CONTENT_INVALID",
310
+ "Markdown frontmatter must be an object.",
311
+ );
256
312
  return value;
257
313
  } catch (cause) {
258
314
  if (cause instanceof ContentBundleError) throw cause;
@@ -268,6 +324,14 @@ export function parseData(
268
324
  }
269
325
 
270
326
  export function normalizeBundleManifest(value: unknown): BundleManifest {
327
+ if (
328
+ record(value) &&
329
+ record(value.entry) &&
330
+ Object.hasOwn(value.entry, "defaultValue")
331
+ )
332
+ throw new Error(
333
+ "entry.defaultValue is no longer supported; initialize content through a Template Bundle.",
334
+ );
271
335
  if (
272
336
  !record(value) ||
273
337
  Object.keys(value).some(
@@ -279,20 +343,20 @@ export function normalizeBundleManifest(value: unknown): BundleManifest {
279
343
  typeof value.description !== "string") ||
280
344
  !record(value.entry) ||
281
345
  Object.keys(value.entry).some(
282
- (key) => !["path", "schema", "contentType", "defaultValue"].includes(key),
346
+ (key) => !["path", "schema", "contentType"].includes(key),
283
347
  )
284
348
  )
285
349
  throw new Error(
286
350
  "Expected name, optional description and a complete entry definition.",
287
351
  );
288
- const { path, schema, contentType, defaultValue } = value.entry;
352
+ const { path, schema, contentType } = value.entry;
289
353
  if (
290
354
  typeof path !== "string" ||
291
- (typeof schema !== "boolean" && !record(schema)) ||
292
- !Object.hasOwn(value.entry, "defaultValue")
355
+ (schema !== undefined && typeof schema !== "boolean" && !record(schema)) ||
356
+ (Object.hasOwn(value.entry, "schema") && schema === undefined)
293
357
  )
294
358
  throw new Error(
295
- "Entry requires path, schema, contentType and defaultValue.",
359
+ "Entry requires path and contentType, with an optional schema.",
296
360
  );
297
361
  safePath(path);
298
362
  if (
@@ -301,22 +365,15 @@ export function normalizeBundleManifest(value: unknown): BundleManifest {
301
365
  )
302
366
  )
303
367
  throw new Error("Entry path conflicts with a bundle control file.");
304
- if (
305
- contentType !== "application/json" &&
306
- contentType !== "application/yaml" &&
307
- contentType !== "application/jsonl"
308
- )
309
- throw new Error("Unsupported content type.");
310
- if (contentType === "application/jsonl" && !Array.isArray(defaultValue))
311
- throw new Error("JSONL defaultValue must be an array.");
368
+ if (typeof contentType !== "string" || !mime.test(contentType))
369
+ throw new Error("contentType must be a valid MIME type.");
312
370
  return Object.freeze({
313
371
  name: value.name,
314
372
  description: value.description ?? "",
315
373
  entry: Object.freeze({
316
374
  path,
317
- schema,
375
+ ...(schema === undefined ? {} : { schema }),
318
376
  contentType,
319
- defaultValue,
320
377
  } satisfies BundleManifest["entry"]),
321
378
  });
322
379
  }