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
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const object = require("./object-_zq9luMS.js");
2
+ const object = require("./object-DldjPOTT.js");
3
3
  const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
4
4
  __proto__: null,
5
5
  calculateSkipsLeft: object.calculateSkipsLeft,
@@ -46,4 +46,4 @@ const Document = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProp
46
46
  }, Symbol.toStringTag, { value: "Module" }));
47
47
  exports.Document = Document;
48
48
  exports.index = index;
49
- //# sourceMappingURL=index-DxD7Ha1j.js.map
49
+ //# sourceMappingURL=index-frw-qprx.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index-frw-qprx.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -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;
@@ -3532,6 +3412,129 @@ const documentHelpers = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.def
3532
3412
  sortOperations: sortOperations$1,
3533
3413
  split
3534
3414
  }, Symbol.toStringTag, { value: "Module" }));
3415
+ const createZip = async (document) => {
3416
+ const zip = new JSZip();
3417
+ const { name, revision, documentType, created, lastModified } = document;
3418
+ const header = {
3419
+ name,
3420
+ revision,
3421
+ documentType,
3422
+ created,
3423
+ lastModified
3424
+ };
3425
+ zip.file("header.json", JSON.stringify(header, null, 2));
3426
+ zip.file(
3427
+ "state.json",
3428
+ JSON.stringify(document.initialState || {}, null, 2)
3429
+ );
3430
+ zip.file("operations.json", JSON.stringify(document.operations, null, 2));
3431
+ if (document.attachments) {
3432
+ const attachments = Object.keys(document.attachments);
3433
+ attachments.forEach((key) => {
3434
+ const { data, ...attributes } = document.attachments[key];
3435
+ zip.file(key, data, {
3436
+ base64: true,
3437
+ createFolders: true,
3438
+ comment: JSON.stringify(attributes)
3439
+ });
3440
+ });
3441
+ }
3442
+ return zip;
3443
+ };
3444
+ const saveToFile = async (document, path, extension, name) => {
3445
+ const zip = await createZip(document);
3446
+ await zip.generateAsync({
3447
+ type: "uint8array",
3448
+ streamFiles: true
3449
+ });
3450
+ const fileName = name ?? document.name;
3451
+ const fileExtension = `.${extension}.zip`;
3452
+ return writeFile(
3453
+ path,
3454
+ fileName.endsWith(fileExtension) ? fileName : `${fileName}${fileExtension}`
3455
+ );
3456
+ };
3457
+ const saveToFileHandle = async (document, input) => {
3458
+ const zip = await createZip(document);
3459
+ const blob = await zip.generateAsync({ type: "blob" });
3460
+ const writable = await input.createWritable();
3461
+ await writable.write(blob);
3462
+ await writable.close();
3463
+ };
3464
+ const loadFromFile = async (path, reducer, options) => {
3465
+ const file = readFile();
3466
+ return loadFromInput(file, reducer, options);
3467
+ };
3468
+ const loadFromInput = async (input, reducer, options) => {
3469
+ const zip = new JSZip();
3470
+ await zip.loadAsync(input);
3471
+ return loadFromZip(zip, reducer, options);
3472
+ };
3473
+ async function loadFromZip(zip, reducer, options) {
3474
+ const initialStateZip = zip.file("state.json");
3475
+ if (!initialStateZip) {
3476
+ throw new Error("Initial state not found");
3477
+ }
3478
+ const initialStateStr = await initialStateZip.async("string");
3479
+ const initialState = JSON.parse(initialStateStr);
3480
+ const headerZip = zip.file("header.json");
3481
+ let header = void 0;
3482
+ if (headerZip) {
3483
+ header = JSON.parse(await headerZip.async("string"));
3484
+ }
3485
+ const operationsZip = zip.file("operations.json");
3486
+ if (!operationsZip) {
3487
+ throw new Error("Operations history not found");
3488
+ }
3489
+ const operations = JSON.parse(
3490
+ await operationsZip.async("string")
3491
+ );
3492
+ const clearedOperations = garbageCollectDocumentOperations(operations);
3493
+ const operationsError = validateOperations(clearedOperations);
3494
+ if (operationsError.length) {
3495
+ const errorMessages = operationsError.map((err) => err.message);
3496
+ throw new Error(errorMessages.join("\n"));
3497
+ }
3498
+ let result = replayDocument(
3499
+ initialState,
3500
+ clearedOperations,
3501
+ reducer,
3502
+ void 0,
3503
+ header,
3504
+ {},
3505
+ options
3506
+ );
3507
+ if (header) {
3508
+ result = {
3509
+ ...result,
3510
+ ...header
3511
+ };
3512
+ }
3513
+ return result;
3514
+ }
3515
+ function getFileAttributes(file) {
3516
+ const extension = file.replace(/^.*\./, "") || void 0;
3517
+ const fileName = file.replace(/^.*[/\\]/, "") || void 0;
3518
+ return { extension, fileName };
3519
+ }
3520
+ async function getRemoteFile(url) {
3521
+ const { buffer: buffer2, mimeType = "application/octet-stream" } = await fetchFile();
3522
+ const attributes = getFileAttributes(url);
3523
+ const data = buffer2.toString("base64");
3524
+ return {
3525
+ data,
3526
+ hash: hash(data),
3527
+ mimeType,
3528
+ ...attributes
3529
+ };
3530
+ }
3531
+ async function getLocalFile(path) {
3532
+ const buffer2 = await getFile();
3533
+ const mimeType = mime.getType(path) || "application/octet-stream";
3534
+ const attributes = getFileAttributes(path);
3535
+ const data = buffer2.toString("base64");
3536
+ return { data, hash: hash(data), mimeType, ...attributes };
3537
+ }
3535
3538
  function getNextRevision(document, action) {
3536
3539
  let latestOperation;
3537
3540
  if ("index" in action) {
@@ -4330,4 +4333,4 @@ exports.updateDocument = updateDocument;
4330
4333
  exports.updateHeader = updateHeader;
4331
4334
  exports.validateOperations = validateOperations;
4332
4335
  exports.zod = zod;
4333
- //# sourceMappingURL=object-CRlmL8Nt.js.map
4336
+ //# sourceMappingURL=object-DldjPOTT.js.map