document-model 1.4.0 → 1.5.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.
Files changed (44) hide show
  1. package/dist/browser/cjs/document-model.js +2 -2
  2. package/dist/browser/cjs/document.js +2 -2
  3. package/dist/browser/cjs/index.js +2 -2
  4. package/dist/browser/cjs/internal/{index-DdOJi6Ua.js → index-BoiSedJ2.js} +2 -2
  5. package/dist/browser/cjs/internal/{index-DdOJi6Ua.js.map → index-BoiSedJ2.js.map} +1 -1
  6. package/dist/{node/cjs/internal/index-DxD7Ha1j.js → browser/cjs/internal/index-frw-qprx.js} +2 -2
  7. package/dist/browser/cjs/internal/index-frw-qprx.js.map +1 -0
  8. package/dist/browser/cjs/internal/{object-CRlmL8Nt.js → object-DldjPOTT.js} +127 -124
  9. package/dist/browser/cjs/internal/object-DldjPOTT.js.map +1 -0
  10. package/dist/browser/es/document-model.js +2 -2
  11. package/dist/browser/es/document.js +2 -2
  12. package/dist/browser/es/index.js +2 -2
  13. package/dist/browser/es/internal/{index-C1HKYMZA.js → index-B7idSyw8.js} +2 -2
  14. package/dist/browser/es/internal/{index-C1HKYMZA.js.map → index-B7idSyw8.js.map} +1 -1
  15. package/dist/browser/es/internal/{index-BsGRYydP.js → index-D2kKzCXD.js} +2 -2
  16. package/dist/browser/es/internal/index-D2kKzCXD.js.map +1 -0
  17. package/dist/browser/es/internal/{object-B562qKhQ.js → object-I6vKsK3r.js} +127 -124
  18. package/dist/browser/{cjs/internal/object-CRlmL8Nt.js.map → es/internal/object-I6vKsK3r.js.map} +1 -1
  19. package/dist/node/cjs/document-model.js +2 -2
  20. package/dist/node/cjs/document.js +2 -2
  21. package/dist/node/cjs/index.js +2 -2
  22. package/dist/{browser/cjs/internal/index-pAtJQrtD.js → node/cjs/internal/index-RX6RcsNg.js} +2 -2
  23. package/dist/node/cjs/internal/index-RX6RcsNg.js.map +1 -0
  24. package/dist/node/cjs/internal/{index-BWc0K1Dk.js → index-WVYoLyly.js} +2 -2
  25. package/dist/node/cjs/internal/{index-BWc0K1Dk.js.map → index-WVYoLyly.js.map} +1 -1
  26. package/dist/node/cjs/internal/{object-_zq9luMS.js → object-7GplcYr4.js} +128 -125
  27. package/dist/node/cjs/internal/object-7GplcYr4.js.map +1 -0
  28. package/dist/node/es/document-model.js +2 -2
  29. package/dist/node/es/document.js +2 -2
  30. package/dist/node/es/index.js +2 -2
  31. package/dist/node/es/internal/{index-D8WTToX1.js → index-BFRJ-0Rp.js} +2 -2
  32. package/dist/node/es/internal/{index-D8WTToX1.js.map → index-BFRJ-0Rp.js.map} +1 -1
  33. package/dist/node/es/internal/{index-DkkBVgni.js → index-NWZKlhKu.js} +2 -2
  34. package/dist/node/es/internal/index-NWZKlhKu.js.map +1 -0
  35. package/dist/node/es/internal/{object-D3jjs7tr.js → object-Cu2Dz-iF.js} +128 -125
  36. package/dist/node/es/internal/object-Cu2Dz-iF.js.map +1 -0
  37. package/package.json +1 -1
  38. package/dist/browser/cjs/internal/index-pAtJQrtD.js.map +0 -1
  39. package/dist/browser/es/internal/index-BsGRYydP.js.map +0 -1
  40. package/dist/browser/es/internal/object-B562qKhQ.js.map +0 -1
  41. package/dist/node/cjs/internal/index-DxD7Ha1j.js.map +0 -1
  42. package/dist/node/cjs/internal/object-_zq9luMS.js.map +0 -1
  43. package/dist/node/es/internal/index-DkkBVgni.js.map +0 -1
  44. package/dist/node/es/internal/object-D3jjs7tr.js.map +0 -1
@@ -1061,8 +1061,10 @@ function validateOperations(operations) {
1061
1061
  const scopes = Object.keys(operations);
1062
1062
  for (const scope of scopes) {
1063
1063
  const ops = operations[scope].sort((a, b) => a.index - b.index);
1064
+ let opIndex = -1;
1064
1065
  for (let i = 0; i < ops.length; i++) {
1065
- if (ops[i].index !== i) {
1066
+ opIndex = opIndex + 1 + ops[i].skip;
1067
+ if (ops[i].index !== opIndex) {
1066
1068
  errors.push({
1067
1069
  message: `Invalid operation index ${ops[i].index} at position ${i}`,
1068
1070
  details: {
@@ -1076,129 +1078,6 @@ function validateOperations(operations) {
1076
1078
  }
1077
1079
  return errors;
1078
1080
  }
1079
- const createZip = async (document) => {
1080
- const zip = new JSZip();
1081
- const { name, revision, documentType, created, lastModified } = document;
1082
- const header = {
1083
- name,
1084
- revision,
1085
- documentType,
1086
- created,
1087
- lastModified
1088
- };
1089
- zip.file("header.json", JSON.stringify(header, null, 2));
1090
- zip.file(
1091
- "state.json",
1092
- JSON.stringify(document.initialState || {}, null, 2)
1093
- );
1094
- zip.file("operations.json", JSON.stringify(document.operations, null, 2));
1095
- if (document.attachments) {
1096
- const attachments = Object.keys(document.attachments);
1097
- attachments.forEach((key) => {
1098
- const { data, ...attributes } = document.attachments[key];
1099
- zip.file(key, data, {
1100
- base64: true,
1101
- createFolders: true,
1102
- comment: JSON.stringify(attributes)
1103
- });
1104
- });
1105
- }
1106
- return zip;
1107
- };
1108
- const saveToFile = async (document, path2, extension, name) => {
1109
- const zip = await createZip(document);
1110
- const file = await zip.generateAsync({
1111
- type: "uint8array",
1112
- streamFiles: true
1113
- });
1114
- const fileName = name ?? document.name;
1115
- const fileExtension = `.${extension}.zip`;
1116
- return writeFile(
1117
- path2,
1118
- fileName.endsWith(fileExtension) ? fileName : `${fileName}${fileExtension}`,
1119
- file
1120
- );
1121
- };
1122
- const saveToFileHandle = async (document, input) => {
1123
- const zip = await createZip(document);
1124
- const blob = await zip.generateAsync({ type: "blob" });
1125
- const writable = await input.createWritable();
1126
- await writable.write(blob);
1127
- await writable.close();
1128
- };
1129
- const loadFromFile = async (path2, reducer, options) => {
1130
- const file = readFile(path2);
1131
- return loadFromInput(file, reducer, options);
1132
- };
1133
- const loadFromInput = async (input, reducer, options) => {
1134
- const zip = new JSZip();
1135
- await zip.loadAsync(input);
1136
- return loadFromZip(zip, reducer, options);
1137
- };
1138
- async function loadFromZip(zip, reducer, options) {
1139
- const initialStateZip = zip.file("state.json");
1140
- if (!initialStateZip) {
1141
- throw new Error("Initial state not found");
1142
- }
1143
- const initialStateStr = await initialStateZip.async("string");
1144
- const initialState = JSON.parse(initialStateStr);
1145
- const headerZip = zip.file("header.json");
1146
- let header = void 0;
1147
- if (headerZip) {
1148
- header = JSON.parse(await headerZip.async("string"));
1149
- }
1150
- const operationsZip = zip.file("operations.json");
1151
- if (!operationsZip) {
1152
- throw new Error("Operations history not found");
1153
- }
1154
- const operations = JSON.parse(
1155
- await operationsZip.async("string")
1156
- );
1157
- const operationsError = validateOperations(operations);
1158
- if (operationsError.length) {
1159
- const errorMessages = operationsError.map((err) => err.message);
1160
- throw new Error(errorMessages.join("\n"));
1161
- }
1162
- let result = replayDocument(
1163
- initialState,
1164
- operations,
1165
- reducer,
1166
- void 0,
1167
- header,
1168
- {},
1169
- options
1170
- );
1171
- if (header) {
1172
- result = {
1173
- ...result,
1174
- ...header
1175
- };
1176
- }
1177
- return result;
1178
- }
1179
- function getFileAttributes(file) {
1180
- const extension = file.replace(/^.*\./, "") || void 0;
1181
- const fileName = file.replace(/^.*[/\\]/, "") || void 0;
1182
- return { extension, fileName };
1183
- }
1184
- async function getRemoteFile(url) {
1185
- const { buffer, mimeType = "application/octet-stream" } = await fetchFile(url);
1186
- const attributes = getFileAttributes(url);
1187
- const data = buffer.toString("base64");
1188
- return {
1189
- data,
1190
- hash: hash(data),
1191
- mimeType,
1192
- ...attributes
1193
- };
1194
- }
1195
- async function getLocalFile(path2) {
1196
- const buffer = await getFile(path2);
1197
- const mimeType = mime.getType(path2) || "application/octet-stream";
1198
- const attributes = getFileAttributes(path2);
1199
- const data = buffer.toString("base64");
1200
- return { data, hash: hash(data), mimeType, ...attributes };
1201
- }
1202
1081
  var IntegrityIssueType = /* @__PURE__ */ ((IntegrityIssueType2) => {
1203
1082
  IntegrityIssueType2["UNEXPECTED_INDEX"] = "UNEXPECTED_INDEX";
1204
1083
  return IntegrityIssueType2;
@@ -1534,6 +1413,130 @@ const documentHelpers = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.def
1534
1413
  sortOperations: sortOperations$1,
1535
1414
  split
1536
1415
  }, Symbol.toStringTag, { value: "Module" }));
1416
+ const createZip = async (document) => {
1417
+ const zip = new JSZip();
1418
+ const { name, revision, documentType, created, lastModified } = document;
1419
+ const header = {
1420
+ name,
1421
+ revision,
1422
+ documentType,
1423
+ created,
1424
+ lastModified
1425
+ };
1426
+ zip.file("header.json", JSON.stringify(header, null, 2));
1427
+ zip.file(
1428
+ "state.json",
1429
+ JSON.stringify(document.initialState || {}, null, 2)
1430
+ );
1431
+ zip.file("operations.json", JSON.stringify(document.operations, null, 2));
1432
+ if (document.attachments) {
1433
+ const attachments = Object.keys(document.attachments);
1434
+ attachments.forEach((key) => {
1435
+ const { data, ...attributes } = document.attachments[key];
1436
+ zip.file(key, data, {
1437
+ base64: true,
1438
+ createFolders: true,
1439
+ comment: JSON.stringify(attributes)
1440
+ });
1441
+ });
1442
+ }
1443
+ return zip;
1444
+ };
1445
+ const saveToFile = async (document, path2, extension, name) => {
1446
+ const zip = await createZip(document);
1447
+ const file = await zip.generateAsync({
1448
+ type: "uint8array",
1449
+ streamFiles: true
1450
+ });
1451
+ const fileName = name ?? document.name;
1452
+ const fileExtension = `.${extension}.zip`;
1453
+ return writeFile(
1454
+ path2,
1455
+ fileName.endsWith(fileExtension) ? fileName : `${fileName}${fileExtension}`,
1456
+ file
1457
+ );
1458
+ };
1459
+ const saveToFileHandle = async (document, input) => {
1460
+ const zip = await createZip(document);
1461
+ const blob = await zip.generateAsync({ type: "blob" });
1462
+ const writable = await input.createWritable();
1463
+ await writable.write(blob);
1464
+ await writable.close();
1465
+ };
1466
+ const loadFromFile = async (path2, reducer, options) => {
1467
+ const file = readFile(path2);
1468
+ return loadFromInput(file, reducer, options);
1469
+ };
1470
+ const loadFromInput = async (input, reducer, options) => {
1471
+ const zip = new JSZip();
1472
+ await zip.loadAsync(input);
1473
+ return loadFromZip(zip, reducer, options);
1474
+ };
1475
+ async function loadFromZip(zip, reducer, options) {
1476
+ const initialStateZip = zip.file("state.json");
1477
+ if (!initialStateZip) {
1478
+ throw new Error("Initial state not found");
1479
+ }
1480
+ const initialStateStr = await initialStateZip.async("string");
1481
+ const initialState = JSON.parse(initialStateStr);
1482
+ const headerZip = zip.file("header.json");
1483
+ let header = void 0;
1484
+ if (headerZip) {
1485
+ header = JSON.parse(await headerZip.async("string"));
1486
+ }
1487
+ const operationsZip = zip.file("operations.json");
1488
+ if (!operationsZip) {
1489
+ throw new Error("Operations history not found");
1490
+ }
1491
+ const operations = JSON.parse(
1492
+ await operationsZip.async("string")
1493
+ );
1494
+ const clearedOperations = garbageCollectDocumentOperations(operations);
1495
+ const operationsError = validateOperations(clearedOperations);
1496
+ if (operationsError.length) {
1497
+ const errorMessages = operationsError.map((err) => err.message);
1498
+ throw new Error(errorMessages.join("\n"));
1499
+ }
1500
+ let result = replayDocument(
1501
+ initialState,
1502
+ clearedOperations,
1503
+ reducer,
1504
+ void 0,
1505
+ header,
1506
+ {},
1507
+ options
1508
+ );
1509
+ if (header) {
1510
+ result = {
1511
+ ...result,
1512
+ ...header
1513
+ };
1514
+ }
1515
+ return result;
1516
+ }
1517
+ function getFileAttributes(file) {
1518
+ const extension = file.replace(/^.*\./, "") || void 0;
1519
+ const fileName = file.replace(/^.*[/\\]/, "") || void 0;
1520
+ return { extension, fileName };
1521
+ }
1522
+ async function getRemoteFile(url) {
1523
+ const { buffer, mimeType = "application/octet-stream" } = await fetchFile(url);
1524
+ const attributes = getFileAttributes(url);
1525
+ const data = buffer.toString("base64");
1526
+ return {
1527
+ data,
1528
+ hash: hash(data),
1529
+ mimeType,
1530
+ ...attributes
1531
+ };
1532
+ }
1533
+ async function getLocalFile(path2) {
1534
+ const buffer = await getFile(path2);
1535
+ const mimeType = mime.getType(path2) || "application/octet-stream";
1536
+ const attributes = getFileAttributes(path2);
1537
+ const data = buffer.toString("base64");
1538
+ return { data, hash: hash(data), mimeType, ...attributes };
1539
+ }
1537
1540
  function getNextRevision(document, action) {
1538
1541
  let latestOperation;
1539
1542
  if ("index" in action) {
@@ -2332,4 +2335,4 @@ exports.updateDocument = updateDocument;
2332
2335
  exports.updateHeader = updateHeader;
2333
2336
  exports.validateOperations = validateOperations;
2334
2337
  exports.zod = zod;
2335
- //# sourceMappingURL=object-_zq9luMS.js.map
2338
+ //# sourceMappingURL=object-7GplcYr4.js.map