@quillmark/wasm 0.80.0 → 0.81.0-rc.2

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 CHANGED
@@ -67,6 +67,86 @@ byte-equal to the original source — YAML quoting, key ordering, and
67
67
  whitespace are normalised. Use `equals` (not string comparison) to test
68
68
  semantic equality.
69
69
 
70
+ ### `doc.toJson()`
71
+ Serialize the document to a versioned storage DTO — a JSON **string**
72
+ carrying a `schema` version. Use this (not `toMarkdown`) to persist a
73
+ document across a process restart or crate upgrade: the wire format is
74
+ frozen per `schema` version, whereas Markdown syntax evolves. Parse-time
75
+ `warnings` are not part of the DTO.
76
+
77
+ The string is produced inside the module by `serde_json`; the JS `JSON`
78
+ global is not involved. It is standard JSON text, so callers may
79
+ `JSON.parse` it to inspect it — but it is intended as an opaque blob you
80
+ persist and hand back.
81
+
82
+ `toJson()` is **deterministic**: a `Document` that is `equals` to another
83
+ serializes to a byte-identical string — across repeated calls, and across
84
+ any crate upgrade that keeps the same `schema` version (every release does until
85
+ the `Document` model changes; see [Storage compatibility](#storage-compatibility-across-versions)).
86
+ Field order is fixed and object key order is preserved, so content hashes
87
+ and string-equality dirty-checks over the output are stable.
88
+
89
+ ### `Document.fromJson(json)`
90
+ Reconstruct a `Document` from a storage DTO string produced by `toJson`.
91
+ Round-trips losslessly:
92
+
93
+ ```ts
94
+ const stored = doc.toJson(); // persist this string
95
+ const restored = Document.fromJson(stored);
96
+ restored.equals(doc); // true
97
+ ```
98
+
99
+ Throws a JS `Error` on malformed JSON, an unknown `schema` version, or a
100
+ malformed payload. The restored document has no parse-time `warnings`.
101
+
102
+ ### `Document.tryFromJson(json)`
103
+ Like `fromJson`, but returns `undefined` instead of throwing when `json` is
104
+ not a valid storage DTO. Use it to branch on format without a heuristic or
105
+ `try`/`catch` as control flow:
106
+
107
+ ```ts
108
+ // "JSON canonical, Markdown fallback" — no exceptions, no string sniffing
109
+ const doc = Document.tryFromJson(content) ?? Document.fromMarkdown(content);
110
+ ```
111
+
112
+ `undefined` means only "not a storage DTO"; `fromMarkdown` still throws on
113
+ genuinely malformed Markdown.
114
+
115
+ ### Storage compatibility across versions
116
+
117
+ The `schema` value (`quillmark/document@0.81.0`) is the **model version**,
118
+ not the running crate version. It is a hand-set constant, bumped only when
119
+ the `Document` model itself changes — so every `0.81.x` patch release reads
120
+ and writes that same value.
121
+
122
+ - **Upgrading is safe.** A newer build always reads documents written by an
123
+ older one. Each schema version's wire format is frozen and never changes;
124
+ when the model does change, the new build ships a migration that converts
125
+ old payloads on `fromJson`. A document you commit as your canonical
126
+ on-disk format keeps loading across crate upgrades — there is no need to
127
+ pin old wasm to read old data.
128
+ - **Downgrading is not.** `fromJson` rejects an *unknown* (i.e. newer)
129
+ `schema` version rather than guessing at a format it predates. Don't feed
130
+ documents written by a newer build back into an older one.
131
+
132
+ To detect a version mismatch before parsing, use the static accessors:
133
+
134
+ ```ts
135
+ const v = Document.schemaVersionOf(blob); // undefined | string
136
+ if (v && v !== Document.currentSchemaVersion()) {
137
+ // payload is from a build with a different model version
138
+ }
139
+ ```
140
+
141
+ `schemaVersionOf` does not validate the payload — it only reads the
142
+ `schema` field, returning `undefined` for non-JSON, non-objects, or
143
+ payloads that don't carry one. Use it to distinguish "wrong version" from
144
+ "corrupt" when `fromJson` throws.
145
+
146
+ In short: persist the `toJson` string, upgrade freely, never downgrade. The
147
+ full design — including how migrations are added — is in
148
+ `prose/canon/DOCUMENT_STORAGE.md`.
149
+
70
150
  ### `doc.equals(other)`
71
151
  Structural equality between two `Document` handles. Compares `main` and
72
152
  `cards` by value; parse-time `warnings` are intentionally excluded.
@@ -193,8 +273,11 @@ compilation failures. The same shape applies to every throw site:
193
273
  The wasm bindings are built with `--weak-refs`, so dropped `Document`,
194
274
  `Quill`, and `RenderSession` handles are reclaimed by `FinalizationRegistry`
195
275
  without manual `.free()` discipline. `.free()` is still emitted as an eager
196
- teardown hook for callers that want deterministic release. Requires
197
- Node 14.6+ / current evergreen browsers (all supported targets).
276
+ teardown hook for callers that want deterministic release.
277
+
278
+ The package requires Node 24+ (`engines: { node: ">=24" }`) and current
279
+ evergreen browsers; `--weak-refs` itself only needs Node 14.6+, but the
280
+ published package floor is 24.
198
281
 
199
282
  For environments where `using` (the [explicit resource management][erm]
200
283
  proposal) hasn't landed, use an explicit `try` / `finally`:
package/bundler/wasm.d.ts CHANGED
@@ -269,6 +269,14 @@ export class Document {
269
269
  * history of either handle.
270
270
  */
271
271
  clone(): Document;
272
+ /**
273
+ * Schema version this build writes via [`toJson`](Document::to_json).
274
+ *
275
+ * Compare a payload's [`schemaVersionOf`](Document::schema_version_of)
276
+ * against this to detect mismatches before calling
277
+ * [`fromJson`](Document::from_json).
278
+ */
279
+ static currentSchemaVersion(): string;
272
280
  /**
273
281
  * Structural equality against another `Document`.
274
282
  *
@@ -280,6 +288,22 @@ export class Document {
280
288
  * `Document` and compare instead of re-parsing on every keystroke.
281
289
  */
282
290
  equals(other: Document): boolean;
291
+ /**
292
+ * Reconstruct a `Document` from its versioned storage DTO string.
293
+ *
294
+ * `json` must be a string produced by [`toJson`](Document::to_json) —
295
+ * the versioned storage DTO. Parsing and schema dispatch happen inside
296
+ * the module via `serde_json`; the JS `JSON` global is not involved.
297
+ * Unknown `schema` tags are rejected.
298
+ *
299
+ * The reconstructed document carries no parse-time warnings — the DTO
300
+ * describes content, not source text — so `.warnings` is always empty.
301
+ *
302
+ * Throws a JS `Error` if `json` is not a valid storage DTO (malformed
303
+ * JSON, unknown `schema`, missing fields, or an unparseable quill
304
+ * reference).
305
+ */
306
+ static fromJson(json: string): Document;
283
307
  /**
284
308
  * Parse markdown into a typed Document.
285
309
  *
@@ -348,6 +372,27 @@ export class Document {
348
372
  * Mutators never modify `warnings`.
349
373
  */
350
374
  replaceBody(body: string): void;
375
+ /**
376
+ * Read the schema version from a raw storage DTO string without
377
+ * performing a full parse, or `undefined`.
378
+ *
379
+ * Returns the `schema` field as-is — including unknown future versions
380
+ * that `fromJson` would reject. Use this to distinguish "this build is
381
+ * too old for the payload" from "the payload is corrupt" when
382
+ * [`fromJson`](Document::from_json) throws:
383
+ *
384
+ * ```js
385
+ * const v = Document.schemaVersionOf(blob);
386
+ * if (v === undefined) {
387
+ * // not a stored DTO at all — try fromMarkdown
388
+ * } else if (v !== Document.currentSchemaVersion()) {
389
+ * // newer (or older unmigrated) schema — prompt the user to upgrade
390
+ * } else {
391
+ * doc = Document.fromJson(blob);
392
+ * }
393
+ * ```
394
+ */
395
+ static schemaVersionOf(json: string): string | undefined;
351
396
  /**
352
397
  * Replace the tag of the composable card at `index`.
353
398
  *
@@ -393,6 +438,25 @@ export class Document {
393
438
  * Mutators never modify `warnings`.
394
439
  */
395
440
  setQuillRef(ref_str: string): void;
441
+ /**
442
+ * Serialize this document to a versioned storage DTO string.
443
+ *
444
+ * Returns the document as a JSON string carrying a `schema` version
445
+ * field. The string is produced inside the module via `serde_json` and
446
+ * round-trips losslessly back to an equal `Document` via
447
+ * [`fromJson`](Document::from_json) — the JS `JSON` global is not
448
+ * involved in either direction.
449
+ *
450
+ * Use this — not [`toMarkdown`](Document::to_markdown) — to persist a
451
+ * document across a process restart or crate upgrade; the wire format
452
+ * is frozen per `schema` version, whereas Markdown syntax evolves.
453
+ * Parse-time `warnings` are not part of the DTO.
454
+ *
455
+ * The result is standard JSON text, so callers that want to inspect it
456
+ * may `JSON.parse` it — but treating it as an opaque blob is the
457
+ * intended use.
458
+ */
459
+ toJson(): string;
396
460
  /**
397
461
  * Emit canonical Quillmark Markdown.
398
462
  *
@@ -401,6 +465,21 @@ export class Document {
401
465
  * produces a `Document` equal to `self` by value and by type.
402
466
  */
403
467
  toMarkdown(): string;
468
+ /**
469
+ * Reconstruct a `Document` from a storage DTO string, or `undefined`.
470
+ *
471
+ * Like [`fromJson`](Document::from_json), but returns `undefined`
472
+ * instead of throwing when `json` is not a valid storage DTO. Use this
473
+ * to detect format and fall back without exceptions as control flow:
474
+ *
475
+ * ```js
476
+ * const doc = Document.tryFromJson(content) ?? Document.fromMarkdown(content);
477
+ * ```
478
+ *
479
+ * `undefined` only ever means "not a storage DTO" — `fromMarkdown`
480
+ * still throws on genuinely malformed markdown.
481
+ */
482
+ static tryFromJson(json: string): Document | undefined;
404
483
  /**
405
484
  * Replace the body of the card at `index`.
406
485
  *
@@ -61,6 +61,30 @@ export class Document {
61
61
  const ret = wasm.document_clone(this.__wbg_ptr);
62
62
  return Document.__wrap(ret);
63
63
  }
64
+ /**
65
+ * Schema version this build writes via [`toJson`](Document::to_json).
66
+ *
67
+ * Compare a payload's [`schemaVersionOf`](Document::schema_version_of)
68
+ * against this to detect mismatches before calling
69
+ * [`fromJson`](Document::from_json).
70
+ * @returns {string}
71
+ */
72
+ static currentSchemaVersion() {
73
+ let deferred1_0;
74
+ let deferred1_1;
75
+ try {
76
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
77
+ wasm.document_currentSchemaVersion(retptr);
78
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
79
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
80
+ deferred1_0 = r0;
81
+ deferred1_1 = r1;
82
+ return getStringFromWasm0(r0, r1);
83
+ } finally {
84
+ wasm.__wbindgen_add_to_stack_pointer(16);
85
+ wasm.__wbindgen_export4(deferred1_0, deferred1_1, 1);
86
+ }
87
+ }
64
88
  /**
65
89
  * Structural equality against another `Document`.
66
90
  *
@@ -78,6 +102,40 @@ export class Document {
78
102
  const ret = wasm.document_equals(this.__wbg_ptr, other.__wbg_ptr);
79
103
  return ret !== 0;
80
104
  }
105
+ /**
106
+ * Reconstruct a `Document` from its versioned storage DTO string.
107
+ *
108
+ * `json` must be a string produced by [`toJson`](Document::to_json) —
109
+ * the versioned storage DTO. Parsing and schema dispatch happen inside
110
+ * the module via `serde_json`; the JS `JSON` global is not involved.
111
+ * Unknown `schema` tags are rejected.
112
+ *
113
+ * The reconstructed document carries no parse-time warnings — the DTO
114
+ * describes content, not source text — so `.warnings` is always empty.
115
+ *
116
+ * Throws a JS `Error` if `json` is not a valid storage DTO (malformed
117
+ * JSON, unknown `schema`, missing fields, or an unparseable quill
118
+ * reference).
119
+ * @param {string} json
120
+ * @returns {Document}
121
+ */
122
+ static fromJson(json) {
123
+ try {
124
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
125
+ const ptr0 = passStringToWasm0(json, wasm.__wbindgen_export, wasm.__wbindgen_export2);
126
+ const len0 = WASM_VECTOR_LEN;
127
+ wasm.document_fromJson(retptr, ptr0, len0);
128
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
129
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
130
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
131
+ if (r2) {
132
+ throw takeObject(r1);
133
+ }
134
+ return Document.__wrap(r0);
135
+ } finally {
136
+ wasm.__wbindgen_add_to_stack_pointer(16);
137
+ }
138
+ }
81
139
  /**
82
140
  * Parse markdown into a typed Document.
83
141
  *
@@ -286,6 +344,46 @@ export class Document {
286
344
  const len0 = WASM_VECTOR_LEN;
287
345
  wasm.document_replaceBody(this.__wbg_ptr, ptr0, len0);
288
346
  }
347
+ /**
348
+ * Read the schema version from a raw storage DTO string without
349
+ * performing a full parse, or `undefined`.
350
+ *
351
+ * Returns the `schema` field as-is — including unknown future versions
352
+ * that `fromJson` would reject. Use this to distinguish "this build is
353
+ * too old for the payload" from "the payload is corrupt" when
354
+ * [`fromJson`](Document::from_json) throws:
355
+ *
356
+ * ```js
357
+ * const v = Document.schemaVersionOf(blob);
358
+ * if (v === undefined) {
359
+ * // not a stored DTO at all — try fromMarkdown
360
+ * } else if (v !== Document.currentSchemaVersion()) {
361
+ * // newer (or older unmigrated) schema — prompt the user to upgrade
362
+ * } else {
363
+ * doc = Document.fromJson(blob);
364
+ * }
365
+ * ```
366
+ * @param {string} json
367
+ * @returns {string | undefined}
368
+ */
369
+ static schemaVersionOf(json) {
370
+ try {
371
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
372
+ const ptr0 = passStringToWasm0(json, wasm.__wbindgen_export, wasm.__wbindgen_export2);
373
+ const len0 = WASM_VECTOR_LEN;
374
+ wasm.document_schemaVersionOf(retptr, ptr0, len0);
375
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
376
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
377
+ let v2;
378
+ if (r0 !== 0) {
379
+ v2 = getStringFromWasm0(r0, r1).slice();
380
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
381
+ }
382
+ return v2;
383
+ } finally {
384
+ wasm.__wbindgen_add_to_stack_pointer(16);
385
+ }
386
+ }
289
387
  /**
290
388
  * Replace the tag of the composable card at `index`.
291
389
  *
@@ -394,6 +492,49 @@ export class Document {
394
492
  wasm.__wbindgen_add_to_stack_pointer(16);
395
493
  }
396
494
  }
495
+ /**
496
+ * Serialize this document to a versioned storage DTO string.
497
+ *
498
+ * Returns the document as a JSON string carrying a `schema` version
499
+ * field. The string is produced inside the module via `serde_json` and
500
+ * round-trips losslessly back to an equal `Document` via
501
+ * [`fromJson`](Document::from_json) — the JS `JSON` global is not
502
+ * involved in either direction.
503
+ *
504
+ * Use this — not [`toMarkdown`](Document::to_markdown) — to persist a
505
+ * document across a process restart or crate upgrade; the wire format
506
+ * is frozen per `schema` version, whereas Markdown syntax evolves.
507
+ * Parse-time `warnings` are not part of the DTO.
508
+ *
509
+ * The result is standard JSON text, so callers that want to inspect it
510
+ * may `JSON.parse` it — but treating it as an opaque blob is the
511
+ * intended use.
512
+ * @returns {string}
513
+ */
514
+ toJson() {
515
+ let deferred2_0;
516
+ let deferred2_1;
517
+ try {
518
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
519
+ wasm.document_toJson(retptr, this.__wbg_ptr);
520
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
521
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
522
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
523
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
524
+ var ptr1 = r0;
525
+ var len1 = r1;
526
+ if (r3) {
527
+ ptr1 = 0; len1 = 0;
528
+ throw takeObject(r2);
529
+ }
530
+ deferred2_0 = ptr1;
531
+ deferred2_1 = len1;
532
+ return getStringFromWasm0(ptr1, len1);
533
+ } finally {
534
+ wasm.__wbindgen_add_to_stack_pointer(16);
535
+ wasm.__wbindgen_export4(deferred2_0, deferred2_1, 1);
536
+ }
537
+ }
397
538
  /**
398
539
  * Emit canonical Quillmark Markdown.
399
540
  *
@@ -418,6 +559,28 @@ export class Document {
418
559
  wasm.__wbindgen_export4(deferred1_0, deferred1_1, 1);
419
560
  }
420
561
  }
562
+ /**
563
+ * Reconstruct a `Document` from a storage DTO string, or `undefined`.
564
+ *
565
+ * Like [`fromJson`](Document::from_json), but returns `undefined`
566
+ * instead of throwing when `json` is not a valid storage DTO. Use this
567
+ * to detect format and fall back without exceptions as control flow:
568
+ *
569
+ * ```js
570
+ * const doc = Document.tryFromJson(content) ?? Document.fromMarkdown(content);
571
+ * ```
572
+ *
573
+ * `undefined` only ever means "not a storage DTO" — `fromMarkdown`
574
+ * still throws on genuinely malformed markdown.
575
+ * @param {string} json
576
+ * @returns {Document | undefined}
577
+ */
578
+ static tryFromJson(json) {
579
+ const ptr0 = passStringToWasm0(json, wasm.__wbindgen_export, wasm.__wbindgen_export2);
580
+ const len0 = WASM_VECTOR_LEN;
581
+ const ret = wasm.document_tryFromJson(ptr0, len0);
582
+ return ret === 0 ? undefined : Document.__wrap(ret);
583
+ }
421
584
  /**
422
585
  * Replace the body of the card at `index`.
423
586
  *
Binary file
@@ -8,7 +8,9 @@ export const __wbg_rendersession_free: (a: number, b: number) => void;
8
8
  export const document_cardCount: (a: number) => number;
9
9
  export const document_cards: (a: number) => number;
10
10
  export const document_clone: (a: number) => number;
11
+ export const document_currentSchemaVersion: (a: number) => void;
11
12
  export const document_equals: (a: number, b: number) => number;
13
+ export const document_fromJson: (a: number, b: number, c: number) => void;
12
14
  export const document_fromMarkdown: (a: number, b: number, c: number) => void;
13
15
  export const document_insertCard: (a: number, b: number, c: number, d: number) => void;
14
16
  export const document_main: (a: number) => number;
@@ -19,11 +21,14 @@ export const document_removeCard: (a: number, b: number) => number;
19
21
  export const document_removeCardField: (a: number, b: number, c: number, d: number, e: number) => void;
20
22
  export const document_removeField: (a: number, b: number, c: number, d: number) => void;
21
23
  export const document_replaceBody: (a: number, b: number, c: number) => void;
24
+ export const document_schemaVersionOf: (a: number, b: number, c: number) => void;
22
25
  export const document_setCardTag: (a: number, b: number, c: number, d: number, e: number) => void;
23
26
  export const document_setField: (a: number, b: number, c: number, d: number, e: number) => void;
24
27
  export const document_setFill: (a: number, b: number, c: number, d: number, e: number) => void;
25
28
  export const document_setQuillRef: (a: number, b: number, c: number, d: number) => void;
29
+ export const document_toJson: (a: number, b: number) => void;
26
30
  export const document_toMarkdown: (a: number, b: number) => void;
31
+ export const document_tryFromJson: (a: number, b: number) => number;
27
32
  export const document_updateCardBody: (a: number, b: number, c: number, d: number, e: number) => void;
28
33
  export const document_updateCardField: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
29
34
  export const document_warnings: (a: number) => number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quillmark/wasm",
3
- "version": "0.80.0",
3
+ "version": "0.81.0-rc.2",
4
4
  "description": "WebAssembly bindings for quillmark",
5
5
  "type": "module",
6
6
  "license": "MIT OR Apache-2.0",