document-model 1.4.0 → 1.6.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 (47) 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/{node/cjs/internal/index-DxD7Ha1j.js → browser/cjs/internal/index-BImZF-Wk.js} +2 -2
  5. package/dist/browser/cjs/internal/index-BImZF-Wk.js.map +1 -0
  6. package/dist/browser/cjs/internal/{index-DdOJi6Ua.js → index-vlbA6Asd.js} +2 -2
  7. package/dist/browser/cjs/internal/{index-DdOJi6Ua.js.map → index-vlbA6Asd.js.map} +1 -1
  8. package/dist/browser/cjs/internal/{object-CRlmL8Nt.js → object-9Wvjprnm.js} +151 -124
  9. package/dist/browser/cjs/internal/object-9Wvjprnm.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-CuhiYn-8.js} +2 -2
  14. package/dist/browser/es/internal/{index-C1HKYMZA.js.map → index-CuhiYn-8.js.map} +1 -1
  15. package/dist/browser/es/internal/{index-BsGRYydP.js → index-Drp90r05.js} +2 -2
  16. package/dist/browser/es/internal/index-Drp90r05.js.map +1 -0
  17. package/dist/browser/es/internal/{object-B562qKhQ.js → object-CyAog_F_.js} +151 -124
  18. package/dist/browser/es/internal/object-CyAog_F_.js.map +1 -0
  19. package/dist/browser/src/document/utils/document-helpers.d.ts +1 -0
  20. package/dist/node/cjs/document-model.js +2 -2
  21. package/dist/node/cjs/document.js +2 -2
  22. package/dist/node/cjs/index.js +2 -2
  23. package/dist/{browser/cjs/internal/index-pAtJQrtD.js → node/cjs/internal/index-CJIU8iX-.js} +2 -2
  24. package/dist/node/cjs/internal/index-CJIU8iX-.js.map +1 -0
  25. package/dist/node/cjs/internal/{index-BWc0K1Dk.js → index-DWeC8dwW.js} +2 -2
  26. package/dist/node/cjs/internal/{index-BWc0K1Dk.js.map → index-DWeC8dwW.js.map} +1 -1
  27. package/dist/node/cjs/internal/{object-_zq9luMS.js → object-op6YzhH1.js} +152 -125
  28. package/dist/node/cjs/internal/object-op6YzhH1.js.map +1 -0
  29. package/dist/node/es/document-model.js +2 -2
  30. package/dist/node/es/document.js +2 -2
  31. package/dist/node/es/index.js +2 -2
  32. package/dist/node/es/internal/{index-D8WTToX1.js → index-BNAE-_Hg.js} +2 -2
  33. package/dist/node/es/internal/{index-D8WTToX1.js.map → index-BNAE-_Hg.js.map} +1 -1
  34. package/dist/node/es/internal/{index-DkkBVgni.js → index-B_7z_4EV.js} +2 -2
  35. package/dist/node/es/internal/index-B_7z_4EV.js.map +1 -0
  36. package/dist/node/es/internal/{object-D3jjs7tr.js → object-BuK9PFjs.js} +152 -125
  37. package/dist/node/es/internal/object-BuK9PFjs.js.map +1 -0
  38. package/dist/node/src/document/utils/document-helpers.d.ts +1 -0
  39. package/package.json +1 -1
  40. package/dist/browser/cjs/internal/index-pAtJQrtD.js.map +0 -1
  41. package/dist/browser/cjs/internal/object-CRlmL8Nt.js.map +0 -1
  42. package/dist/browser/es/internal/index-BsGRYydP.js.map +0 -1
  43. package/dist/browser/es/internal/object-B562qKhQ.js.map +0 -1
  44. package/dist/node/cjs/internal/index-DxD7Ha1j.js.map +0 -1
  45. package/dist/node/cjs/internal/object-_zq9luMS.js.map +0 -1
  46. package/dist/node/es/internal/index-DkkBVgni.js.map +0 -1
  47. package/dist/node/es/internal/object-D3jjs7tr.js.map +0 -1
@@ -3060,8 +3060,10 @@ function validateOperations(operations) {
3060
3060
  const scopes = Object.keys(operations);
3061
3061
  for (const scope of scopes) {
3062
3062
  const ops = operations[scope].sort((a, b) => a.index - b.index);
3063
+ let opIndex = -1;
3063
3064
  for (let i = 0; i < ops.length; i++) {
3064
- if (ops[i].index !== i) {
3065
+ opIndex = opIndex + 1 + ops[i].skip;
3066
+ if (ops[i].index !== opIndex) {
3065
3067
  errors.push({
3066
3068
  message: `Invalid operation index ${ops[i].index} at position ${i}`,
3067
3069
  details: {
@@ -3075,128 +3077,6 @@ function validateOperations(operations) {
3075
3077
  }
3076
3078
  return errors;
3077
3079
  }
3078
- const createZip = async (document) => {
3079
- const zip = new JSZip();
3080
- const { name, revision, documentType, created, lastModified } = document;
3081
- const header = {
3082
- name,
3083
- revision,
3084
- documentType,
3085
- created,
3086
- lastModified
3087
- };
3088
- zip.file("header.json", JSON.stringify(header, null, 2));
3089
- zip.file(
3090
- "state.json",
3091
- JSON.stringify(document.initialState || {}, null, 2)
3092
- );
3093
- zip.file("operations.json", JSON.stringify(document.operations, null, 2));
3094
- if (document.attachments) {
3095
- const attachments = Object.keys(document.attachments);
3096
- attachments.forEach((key) => {
3097
- const { data, ...attributes } = document.attachments[key];
3098
- zip.file(key, data, {
3099
- base64: true,
3100
- createFolders: true,
3101
- comment: JSON.stringify(attributes)
3102
- });
3103
- });
3104
- }
3105
- return zip;
3106
- };
3107
- const saveToFile = async (document, path, extension, name) => {
3108
- const zip = await createZip(document);
3109
- await zip.generateAsync({
3110
- type: "uint8array",
3111
- streamFiles: true
3112
- });
3113
- const fileName = name ?? document.name;
3114
- const fileExtension = `.${extension}.zip`;
3115
- return writeFile(
3116
- path,
3117
- fileName.endsWith(fileExtension) ? fileName : `${fileName}${fileExtension}`
3118
- );
3119
- };
3120
- const saveToFileHandle = async (document, input) => {
3121
- const zip = await createZip(document);
3122
- const blob = await zip.generateAsync({ type: "blob" });
3123
- const writable = await input.createWritable();
3124
- await writable.write(blob);
3125
- await writable.close();
3126
- };
3127
- const loadFromFile = async (path, reducer, options) => {
3128
- const file = readFile();
3129
- return loadFromInput(file, reducer, options);
3130
- };
3131
- const loadFromInput = async (input, reducer, options) => {
3132
- const zip = new JSZip();
3133
- await zip.loadAsync(input);
3134
- return loadFromZip(zip, reducer, options);
3135
- };
3136
- async function loadFromZip(zip, reducer, options) {
3137
- const initialStateZip = zip.file("state.json");
3138
- if (!initialStateZip) {
3139
- throw new Error("Initial state not found");
3140
- }
3141
- const initialStateStr = await initialStateZip.async("string");
3142
- const initialState = JSON.parse(initialStateStr);
3143
- const headerZip = zip.file("header.json");
3144
- let header = void 0;
3145
- if (headerZip) {
3146
- header = JSON.parse(await headerZip.async("string"));
3147
- }
3148
- const operationsZip = zip.file("operations.json");
3149
- if (!operationsZip) {
3150
- throw new Error("Operations history not found");
3151
- }
3152
- const operations = JSON.parse(
3153
- await operationsZip.async("string")
3154
- );
3155
- const operationsError = validateOperations(operations);
3156
- if (operationsError.length) {
3157
- const errorMessages = operationsError.map((err) => err.message);
3158
- throw new Error(errorMessages.join("\n"));
3159
- }
3160
- let result = replayDocument(
3161
- initialState,
3162
- operations,
3163
- reducer,
3164
- void 0,
3165
- header,
3166
- {},
3167
- options
3168
- );
3169
- if (header) {
3170
- result = {
3171
- ...result,
3172
- ...header
3173
- };
3174
- }
3175
- return result;
3176
- }
3177
- function getFileAttributes(file) {
3178
- const extension = file.replace(/^.*\./, "") || void 0;
3179
- const fileName = file.replace(/^.*[/\\]/, "") || void 0;
3180
- return { extension, fileName };
3181
- }
3182
- async function getRemoteFile(url) {
3183
- const { buffer: buffer2, mimeType = "application/octet-stream" } = await fetchFile();
3184
- const attributes = getFileAttributes(url);
3185
- const data = buffer2.toString("base64");
3186
- return {
3187
- data,
3188
- hash: hash(data),
3189
- mimeType,
3190
- ...attributes
3191
- };
3192
- }
3193
- async function getLocalFile(path) {
3194
- const buffer2 = await getFile();
3195
- const mimeType = mime.getType(path) || "application/octet-stream";
3196
- const attributes = getFileAttributes(path);
3197
- const data = buffer2.toString("base64");
3198
- return { data, hash: hash(data), mimeType, ...attributes };
3199
- }
3200
3080
  var IntegrityIssueType = /* @__PURE__ */ ((IntegrityIssueType2) => {
3201
3081
  IntegrityIssueType2["UNEXPECTED_INDEX"] = "UNEXPECTED_INDEX";
3202
3082
  return IntegrityIssueType2;
@@ -3508,6 +3388,22 @@ function filterDuplicatedOperations(targetOperations, sourceOperations) {
3508
3388
  return true;
3509
3389
  });
3510
3390
  }
3391
+ function filterDocumentOperationsResultingState(documentOperations) {
3392
+ if (!documentOperations) {
3393
+ return {};
3394
+ }
3395
+ const entries = Object.entries(documentOperations);
3396
+ return entries.reduce(
3397
+ (acc, [scope, operations]) => ({
3398
+ ...acc,
3399
+ [scope]: operations.map((op) => {
3400
+ const { resultingState, ...restProps } = op;
3401
+ return restProps;
3402
+ })
3403
+ }),
3404
+ {}
3405
+ );
3406
+ }
3511
3407
  const documentHelpers = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
3512
3408
  __proto__: null,
3513
3409
  IntegrityIssueSubType,
@@ -3516,6 +3412,7 @@ const documentHelpers = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.def
3516
3412
  attachBranch,
3517
3413
  checkCleanedOperationsIntegrity,
3518
3414
  checkOperationsIntegrity,
3415
+ filterDocumentOperationsResultingState,
3519
3416
  filterDuplicatedOperations,
3520
3417
  garbageCollect,
3521
3418
  garbageCollectDocumentOperations,
@@ -3532,6 +3429,136 @@ const documentHelpers = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.def
3532
3429
  sortOperations: sortOperations$1,
3533
3430
  split
3534
3431
  }, Symbol.toStringTag, { value: "Module" }));
3432
+ const createZip = async (document) => {
3433
+ const zip = new JSZip();
3434
+ const { name, revision, documentType, created, lastModified } = document;
3435
+ const header = {
3436
+ name,
3437
+ revision,
3438
+ documentType,
3439
+ created,
3440
+ lastModified
3441
+ };
3442
+ zip.file("header.json", JSON.stringify(header, null, 2));
3443
+ zip.file(
3444
+ "state.json",
3445
+ JSON.stringify(document.initialState || {}, null, 2)
3446
+ );
3447
+ zip.file(
3448
+ "operations.json",
3449
+ JSON.stringify(
3450
+ filterDocumentOperationsResultingState(document.operations),
3451
+ null,
3452
+ 2
3453
+ )
3454
+ );
3455
+ if (document.attachments) {
3456
+ const attachments = Object.keys(document.attachments);
3457
+ attachments.forEach((key) => {
3458
+ const { data, ...attributes } = document.attachments[key];
3459
+ zip.file(key, data, {
3460
+ base64: true,
3461
+ createFolders: true,
3462
+ comment: JSON.stringify(attributes)
3463
+ });
3464
+ });
3465
+ }
3466
+ return zip;
3467
+ };
3468
+ const saveToFile = async (document, path, extension, name) => {
3469
+ const zip = await createZip(document);
3470
+ await zip.generateAsync({
3471
+ type: "uint8array",
3472
+ streamFiles: true
3473
+ });
3474
+ const fileName = name ?? document.name;
3475
+ const fileExtension = `.${extension}.zip`;
3476
+ return writeFile(
3477
+ path,
3478
+ fileName.endsWith(fileExtension) ? fileName : `${fileName}${fileExtension}`
3479
+ );
3480
+ };
3481
+ const saveToFileHandle = async (document, input) => {
3482
+ const zip = await createZip(document);
3483
+ const blob = await zip.generateAsync({ type: "blob" });
3484
+ const writable = await input.createWritable();
3485
+ await writable.write(blob);
3486
+ await writable.close();
3487
+ };
3488
+ const loadFromFile = async (path, reducer, options) => {
3489
+ const file = readFile();
3490
+ return loadFromInput(file, reducer, options);
3491
+ };
3492
+ const loadFromInput = async (input, reducer, options) => {
3493
+ const zip = new JSZip();
3494
+ await zip.loadAsync(input);
3495
+ return loadFromZip(zip, reducer, options);
3496
+ };
3497
+ async function loadFromZip(zip, reducer, options) {
3498
+ const initialStateZip = zip.file("state.json");
3499
+ if (!initialStateZip) {
3500
+ throw new Error("Initial state not found");
3501
+ }
3502
+ const initialStateStr = await initialStateZip.async("string");
3503
+ const initialState = JSON.parse(initialStateStr);
3504
+ const headerZip = zip.file("header.json");
3505
+ let header = void 0;
3506
+ if (headerZip) {
3507
+ header = JSON.parse(await headerZip.async("string"));
3508
+ }
3509
+ const operationsZip = zip.file("operations.json");
3510
+ if (!operationsZip) {
3511
+ throw new Error("Operations history not found");
3512
+ }
3513
+ const operations = JSON.parse(
3514
+ await operationsZip.async("string")
3515
+ );
3516
+ const clearedOperations = garbageCollectDocumentOperations(operations);
3517
+ const operationsError = validateOperations(clearedOperations);
3518
+ if (operationsError.length) {
3519
+ const errorMessages = operationsError.map((err) => err.message);
3520
+ throw new Error(errorMessages.join("\n"));
3521
+ }
3522
+ let result = replayDocument(
3523
+ initialState,
3524
+ clearedOperations,
3525
+ reducer,
3526
+ void 0,
3527
+ header,
3528
+ {},
3529
+ options
3530
+ );
3531
+ if (header) {
3532
+ result = {
3533
+ ...result,
3534
+ ...header
3535
+ };
3536
+ }
3537
+ return result;
3538
+ }
3539
+ function getFileAttributes(file) {
3540
+ const extension = file.replace(/^.*\./, "") || void 0;
3541
+ const fileName = file.replace(/^.*[/\\]/, "") || void 0;
3542
+ return { extension, fileName };
3543
+ }
3544
+ async function getRemoteFile(url) {
3545
+ const { buffer: buffer2, mimeType = "application/octet-stream" } = await fetchFile();
3546
+ const attributes = getFileAttributes(url);
3547
+ const data = buffer2.toString("base64");
3548
+ return {
3549
+ data,
3550
+ hash: hash(data),
3551
+ mimeType,
3552
+ ...attributes
3553
+ };
3554
+ }
3555
+ async function getLocalFile(path) {
3556
+ const buffer2 = await getFile();
3557
+ const mimeType = mime.getType(path) || "application/octet-stream";
3558
+ const attributes = getFileAttributes(path);
3559
+ const data = buffer2.toString("base64");
3560
+ return { data, hash: hash(data), mimeType, ...attributes };
3561
+ }
3535
3562
  function getNextRevision(document, action) {
3536
3563
  let latestOperation;
3537
3564
  if ("index" in action) {
@@ -4330,4 +4357,4 @@ exports.updateDocument = updateDocument;
4330
4357
  exports.updateHeader = updateHeader;
4331
4358
  exports.validateOperations = validateOperations;
4332
4359
  exports.zod = zod;
4333
- //# sourceMappingURL=object-CRlmL8Nt.js.map
4360
+ //# sourceMappingURL=object-9Wvjprnm.js.map