@tearleads/client-sdk 0.1.208 → 0.1.209

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.
@@ -7,23 +7,32 @@ function restoreText(doc, text) {
7
7
  current.update(text);
8
8
  return true;
9
9
  }
10
+ /** A field's own value: never a method every object inherits. */
11
+ function ownField(fields, key) {
12
+ return Object.hasOwn(fields, key) ? fields[key] : undefined;
13
+ }
10
14
  function restoreStructuredFields(doc, target, registry) {
11
15
  const current = readStoredDocumentState(doc, registry);
12
16
  // A note has no structured fields to restore.
13
17
  if (current.documentKind === "note")
14
18
  return false;
15
- const patch = {};
19
+ // Read both sides by own key only. A field named `constructor` or
20
+ // `toString` that the restored version lacks would otherwise read as the
21
+ // inherited function, which is neither "absent" nor a value Loro can store.
22
+ const changes = [];
16
23
  for (const key of new Set([
17
24
  ...Object.keys(current.structuredFields),
18
25
  ...Object.keys(target),
19
26
  ])) {
20
- if (current.structuredFields[key] !== target[key]) {
21
- patch[key] = target[key];
27
+ const wanted = ownField(target, key);
28
+ if (ownField(current.structuredFields, key) !== wanted) {
29
+ changes.push([key, wanted]);
22
30
  }
23
31
  }
24
- if (Object.keys(patch).length === 0)
32
+ if (changes.length === 0)
25
33
  return false;
26
- writeStoredDocumentFields(doc, current.documentKind, patch, registry);
34
+ // fromEntries defines every key as an own property, `__proto__` included.
35
+ writeStoredDocumentFields(doc, current.documentKind, Object.fromEntries(changes), registry);
27
36
  return true;
28
37
  }
29
38
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"documentRestore.js","sourceRoot":"","sources":["documentRestore.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,uBAAuB,EAEvB,yBAAyB,GAC1B,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,cAAc,EACd,mBAAmB,EAEnB,gBAAgB,EAChB,iBAAiB,EACjB,wBAAwB,GACzB,MAAM,mBAAmB,CAAC;AA0B3B,SAAS,WAAW,CAAC,GAAuB,EAAE,IA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CAC3C,GAAG,EACH,OAAO,CAAC,gBAAgB,EACxB,QAAQ,CACT,CAAC;IACF,MAAM,WAAW,GAAG,WAAW,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;IAChE,OAAO,WAAW,IAAI,aAAa,IAAI,WAAW,CAAC;AACrD,CAAC","sourcesContent":["import {\n type DocumentProjectorRegistryInput,\n readStoredDocumentState,\n type StructuredDocumentShape,\n writeStoredDocumentFields,\n} from \"./documentKinds\";\nimport {\n addDocumentRow,\n createDocumentRowId,\n type DocumentRowAttribution,\n listDocumentRows,\n removeDocumentRow,\n replaceDocumentRowFields,\n} from \"./documentRowList\";\n\n/**\n * The content of a document that a restore writes back: its text, structured\n * fields, and rows. Attachments are not part of it. Their bytes are not kept\n * once detached, so a restore leaves the document's attachments as they are.\n */\nexport interface DocumentRestoreContent {\n readonly rows: ReadonlyArray<{\n readonly fields: Readonly<Record<string, string>>;\n readonly id: string;\n }>;\n readonly structuredFields: Readonly<Record<string, string>>;\n readonly text: string;\n}\n\ntype RestoreRow = DocumentRestoreContent[\"rows\"][number];\n\n/** A document whose text can be replaced, as a Loro document's can. */\ntype RestorableDocument = Omit<StructuredDocumentShape, \"getText\"> & {\n getText: (key: string) => {\n toString: () => string;\n update: (text: string) => void;\n };\n};\n\nfunction restoreText(doc: RestorableDocument, text: string): boolean {\n const current = doc.getText(\"text\");\n if (current.toString() === text) return false;\n current.update(text);\n return true;\n}\n\nfunction restoreStructuredFields(\n doc: StructuredDocumentShape,\n target: Readonly<Record<string, string>>,\n registry: DocumentProjectorRegistryInput | undefined,\n): boolean {\n const current = readStoredDocumentState(doc, registry);\n // A note has no structured fields to restore.\n if (current.documentKind === \"note\") return false;\n const patch: Record<string, string | undefined> = {};\n for (const key of new Set([\n ...Object.keys(current.structuredFields),\n ...Object.keys(target),\n ])) {\n if (current.structuredFields[key] !== target[key]) {\n patch[key] = target[key];\n }\n }\n if (Object.keys(patch).length === 0) return false;\n writeStoredDocumentFields(doc, current.documentKind, patch, registry);\n return true;\n}\n\n/**\n * Make the document's rows equal `target`. A row that still exists is patched\n * cell by cell under its own id, a row that is gone from `target` is removed,\n * and a row the document no longer has is added back as a new row with a new\n * id, at the end.\n *\n * The new id is what keeps a restore safe to merge: two devices restoring the\n * same version at once each add their own copy, and both copies stay distinct\n * rows that can be edited or removed. Re-adding under the old id would leave\n * two rows sharing it, with writes reaching only the first. Row order is not\n * restored, because a row cannot be moved.\n */\nfunction restoreRows(\n doc: StructuredDocumentShape,\n target: ReadonlyArray<RestoreRow>,\n attribution: DocumentRowAttribution,\n): boolean {\n const current = listDocumentRows(doc);\n const targetById = new Map(target.map((row) => [row.id, row]));\n const currentIds = new Set(current.map((row) => row.id));\n let changed = false;\n for (const row of current) {\n const wanted = targetById.get(row.id);\n if (wanted === undefined) {\n changed = removeDocumentRow(doc, row.id) || changed;\n } else {\n changed =\n replaceDocumentRowFields(doc, row.id, wanted.fields, attribution) ||\n changed;\n }\n }\n for (const row of target) {\n if (!currentIds.has(row.id)) {\n addDocumentRow(doc, createDocumentRowId(), row.fields, attribution);\n changed = true;\n }\n }\n return changed;\n}\n\n/**\n * Write `content` into the document as ordinary edits and report whether\n * anything changed. Nothing is rewound: the document keeps its whole history\n * and gains the operations that carry it back to this content, so the restore\n * syncs and merges like any other edit.\n */\nexport function restoreDocumentContent(\n doc: RestorableDocument,\n content: DocumentRestoreContent,\n attribution: DocumentRowAttribution,\n registry?: DocumentProjectorRegistryInput | undefined,\n): boolean {\n const textChanged = restoreText(doc, content.text);\n const fieldsChanged = restoreStructuredFields(\n doc,\n content.structuredFields,\n registry,\n );\n const rowsChanged = restoreRows(doc, content.rows, attribution);\n return textChanged || fieldsChanged || rowsChanged;\n}\n"]}
1
+ {"version":3,"file":"documentRestore.js","sourceRoot":"","sources":["documentRestore.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,uBAAuB,EAEvB,yBAAyB,GAC1B,MAAM,iBAAiB,CAAC;AACzB,OAAO,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QACzB,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC;YAC5B,cAAc,CAAC,GAAG,EAAE,mBAAmB,EAAE,EAAE,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;YACpE,OAAO,GAAG,IAAI,CAAC;QACjB,CAAC;IACH,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CACpC,GAAuB,EACvB,OAA+B,EAC/B,WAAmC,EACnC,QAAqD;IAErD,MAAM,WAAW,GAAG,WAAW,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACnD,MAAM,aAAa,GAAG,uBAAuB,CAC3C,GAAG,EACH,OAAO,CAAC,gBAAgB,EACxB,QAAQ,CACT,CAAC;IACF,MAAM,WAAW,GAAG,WAAW,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;IAChE,OAAO,WAAW,IAAI,aAAa,IAAI,WAAW,CAAC;AACrD,CAAC","sourcesContent":["import {\n type DocumentProjectorRegistryInput,\n readStoredDocumentState,\n type StructuredDocumentShape,\n writeStoredDocumentFields,\n} from \"./documentKinds\";\nimport {\n addDocumentRow,\n createDocumentRowId,\n type DocumentRowAttribution,\n listDocumentRows,\n removeDocumentRow,\n replaceDocumentRowFields,\n} from \"./documentRowList\";\n\n/**\n * The content of a document that a restore writes back: its text, structured\n * fields, and rows. Attachments are not part of it. Their bytes are not kept\n * once detached, so a restore leaves the document's attachments as they are.\n */\nexport interface DocumentRestoreContent {\n readonly rows: ReadonlyArray<{\n readonly fields: Readonly<Record<string, string>>;\n readonly id: string;\n }>;\n readonly structuredFields: Readonly<Record<string, string>>;\n readonly text: string;\n}\n\ntype RestoreRow = DocumentRestoreContent[\"rows\"][number];\n\n/** A document whose text can be replaced, as a Loro document's can. */\ntype RestorableDocument = Omit<StructuredDocumentShape, \"getText\"> & {\n getText: (key: string) => {\n toString: () => string;\n update: (text: string) => void;\n };\n};\n\nfunction restoreText(doc: RestorableDocument, text: string): boolean {\n const current = doc.getText(\"text\");\n if (current.toString() === text) return false;\n current.update(text);\n return true;\n}\n\n/** A field's own value: never a method every object inherits. */\nfunction ownField(\n fields: Readonly<Record<string, string>>,\n key: string,\n): string | undefined {\n return Object.hasOwn(fields, key) ? fields[key] : undefined;\n}\n\nfunction restoreStructuredFields(\n doc: StructuredDocumentShape,\n target: Readonly<Record<string, string>>,\n registry: DocumentProjectorRegistryInput | undefined,\n): boolean {\n const current = readStoredDocumentState(doc, registry);\n // A note has no structured fields to restore.\n if (current.documentKind === \"note\") return false;\n // Read both sides by own key only. A field named `constructor` or\n // `toString` that the restored version lacks would otherwise read as the\n // inherited function, which is neither \"absent\" nor a value Loro can store.\n const changes: Array<[string, string | undefined]> = [];\n for (const key of new Set([\n ...Object.keys(current.structuredFields),\n ...Object.keys(target),\n ])) {\n const wanted = ownField(target, key);\n if (ownField(current.structuredFields, key) !== wanted) {\n changes.push([key, wanted]);\n }\n }\n if (changes.length === 0) return false;\n // fromEntries defines every key as an own property, `__proto__` included.\n writeStoredDocumentFields(\n doc,\n current.documentKind,\n Object.fromEntries(changes),\n registry,\n );\n return true;\n}\n\n/**\n * Make the document's rows equal `target`. A row that still exists is patched\n * cell by cell under its own id, a row that is gone from `target` is removed,\n * and a row the document no longer has is added back as a new row with a new\n * id, at the end.\n *\n * The new id is what keeps a restore safe to merge: two devices restoring the\n * same version at once each add their own copy, and both copies stay distinct\n * rows that can be edited or removed. Re-adding under the old id would leave\n * two rows sharing it, with writes reaching only the first. Row order is not\n * restored, because a row cannot be moved.\n */\nfunction restoreRows(\n doc: StructuredDocumentShape,\n target: ReadonlyArray<RestoreRow>,\n attribution: DocumentRowAttribution,\n): boolean {\n const current = listDocumentRows(doc);\n const targetById = new Map(target.map((row) => [row.id, row]));\n const currentIds = new Set(current.map((row) => row.id));\n let changed = false;\n for (const row of current) {\n const wanted = targetById.get(row.id);\n if (wanted === undefined) {\n changed = removeDocumentRow(doc, row.id) || changed;\n } else {\n changed =\n replaceDocumentRowFields(doc, row.id, wanted.fields, attribution) ||\n changed;\n }\n }\n for (const row of target) {\n if (!currentIds.has(row.id)) {\n addDocumentRow(doc, createDocumentRowId(), row.fields, attribution);\n changed = true;\n }\n }\n return changed;\n}\n\n/**\n * Write `content` into the document as ordinary edits and report whether\n * anything changed. Nothing is rewound: the document keeps its whole history\n * and gains the operations that carry it back to this content, so the restore\n * syncs and merges like any other edit.\n */\nexport function restoreDocumentContent(\n doc: RestorableDocument,\n content: DocumentRestoreContent,\n attribution: DocumentRowAttribution,\n registry?: DocumentProjectorRegistryInput | undefined,\n): boolean {\n const textChanged = restoreText(doc, content.text);\n const fieldsChanged = restoreStructuredFields(\n doc,\n content.structuredFields,\n registry,\n );\n const rowsChanged = restoreRows(doc, content.rows, attribution);\n return textChanged || fieldsChanged || rowsChanged;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tearleads/client-sdk",
3
- "version": "0.1.208",
3
+ "version": "0.1.209",
4
4
  "description": "React-free Tearleads client runtime: local SQLite persistence, keys, sync, encrypted blobs, and domain workflows.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {