@rotorsoft/act-sse 1.0.1 → 1.1.1

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/dist/index.js CHANGED
@@ -1,667 +1,54 @@
1
- var __defProp = Object.defineProperty;
2
- var __export = (target, all) => {
3
- for (var name in all)
4
- __defProp(target, name, { get: all[name], enumerable: true });
1
+ // src/patch.ts
2
+ var UNMERGEABLES = [
3
+ RegExp,
4
+ Date,
5
+ Array,
6
+ Map,
7
+ Set,
8
+ WeakMap,
9
+ WeakSet,
10
+ ArrayBuffer,
11
+ ...typeof SharedArrayBuffer !== "undefined" ? [SharedArrayBuffer] : [],
12
+ DataView,
13
+ Int8Array,
14
+ Uint8Array,
15
+ Uint8ClampedArray,
16
+ Int16Array,
17
+ Uint16Array,
18
+ Int32Array,
19
+ Uint32Array,
20
+ Float32Array,
21
+ Float64Array
22
+ ];
23
+ var is_mergeable = (value) => !!value && typeof value === "object" && !UNMERGEABLES.some((t) => value instanceof t);
24
+ var patch = (original, patches) => {
25
+ const copy = {};
26
+ Object.keys({ ...original, ...patches }).forEach((key) => {
27
+ const patched_value = patches[key];
28
+ const original_value = original[key];
29
+ const patched = patches && key in patches;
30
+ const deleted = patched && (typeof patched_value === "undefined" || patched_value === null);
31
+ const value = patched && !deleted ? patched_value : original_value;
32
+ !deleted && (copy[key] = is_mergeable(value) ? patch(original_value || {}, patched_value || {}) : value);
33
+ });
34
+ return copy;
5
35
  };
6
36
 
7
- // ../../node_modules/.pnpm/fast-json-patch@3.1.1/node_modules/fast-json-patch/module/core.mjs
8
- var core_exports = {};
9
- __export(core_exports, {
10
- JsonPatchError: () => JsonPatchError,
11
- _areEquals: () => _areEquals,
12
- applyOperation: () => applyOperation,
13
- applyPatch: () => applyPatch,
14
- applyReducer: () => applyReducer,
15
- deepClone: () => deepClone,
16
- getValueByPointer: () => getValueByPointer,
17
- validate: () => validate,
18
- validator: () => validator
19
- });
20
-
21
- // ../../node_modules/.pnpm/fast-json-patch@3.1.1/node_modules/fast-json-patch/module/helpers.mjs
22
- var __extends = /* @__PURE__ */ (function() {
23
- var extendStatics = function(d, b) {
24
- extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(d2, b2) {
25
- d2.__proto__ = b2;
26
- } || function(d2, b2) {
27
- for (var p in b2) if (b2.hasOwnProperty(p)) d2[p] = b2[p];
28
- };
29
- return extendStatics(d, b);
30
- };
31
- return function(d, b) {
32
- extendStatics(d, b);
33
- function __() {
34
- this.constructor = d;
35
- }
36
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
37
- };
38
- })();
39
- var _hasOwnProperty = Object.prototype.hasOwnProperty;
40
- function hasOwnProperty(obj, key) {
41
- return _hasOwnProperty.call(obj, key);
42
- }
43
- function _objectKeys(obj) {
44
- if (Array.isArray(obj)) {
45
- var keys_1 = new Array(obj.length);
46
- for (var k = 0; k < keys_1.length; k++) {
47
- keys_1[k] = "" + k;
48
- }
49
- return keys_1;
50
- }
51
- if (Object.keys) {
52
- return Object.keys(obj);
53
- }
54
- var keys = [];
55
- for (var i in obj) {
56
- if (hasOwnProperty(obj, i)) {
57
- keys.push(i);
58
- }
59
- }
60
- return keys;
61
- }
62
- function _deepClone(obj) {
63
- switch (typeof obj) {
64
- case "object":
65
- return JSON.parse(JSON.stringify(obj));
66
- //Faster than ES5 clone - http://jsperf.com/deep-cloning-of-objects/5
67
- case "undefined":
68
- return null;
69
- //this is how JSON.stringify behaves for array items
70
- default:
71
- return obj;
72
- }
73
- }
74
- function isInteger(str) {
75
- var i = 0;
76
- var len = str.length;
77
- var charCode;
78
- while (i < len) {
79
- charCode = str.charCodeAt(i);
80
- if (charCode >= 48 && charCode <= 57) {
81
- i++;
82
- continue;
83
- }
84
- return false;
85
- }
86
- return true;
87
- }
88
- function escapePathComponent(path) {
89
- if (path.indexOf("/") === -1 && path.indexOf("~") === -1)
90
- return path;
91
- return path.replace(/~/g, "~0").replace(/\//g, "~1");
92
- }
93
- function unescapePathComponent(path) {
94
- return path.replace(/~1/g, "/").replace(/~0/g, "~");
95
- }
96
- function hasUndefined(obj) {
97
- if (obj === void 0) {
98
- return true;
99
- }
100
- if (obj) {
101
- if (Array.isArray(obj)) {
102
- for (var i_1 = 0, len = obj.length; i_1 < len; i_1++) {
103
- if (hasUndefined(obj[i_1])) {
104
- return true;
105
- }
106
- }
107
- } else if (typeof obj === "object") {
108
- var objKeys = _objectKeys(obj);
109
- var objKeysLength = objKeys.length;
110
- for (var i = 0; i < objKeysLength; i++) {
111
- if (hasUndefined(obj[objKeys[i]])) {
112
- return true;
113
- }
114
- }
115
- }
116
- }
117
- return false;
118
- }
119
- function patchErrorMessageFormatter(message, args) {
120
- var messageParts = [message];
121
- for (var key in args) {
122
- var value = typeof args[key] === "object" ? JSON.stringify(args[key], null, 2) : args[key];
123
- if (typeof value !== "undefined") {
124
- messageParts.push(key + ": " + value);
125
- }
126
- }
127
- return messageParts.join("\n");
128
- }
129
- var PatchError = (
130
- /** @class */
131
- (function(_super) {
132
- __extends(PatchError2, _super);
133
- function PatchError2(message, name, index, operation, tree) {
134
- var _newTarget = this.constructor;
135
- var _this = _super.call(this, patchErrorMessageFormatter(message, { name, index, operation, tree })) || this;
136
- _this.name = name;
137
- _this.index = index;
138
- _this.operation = operation;
139
- _this.tree = tree;
140
- Object.setPrototypeOf(_this, _newTarget.prototype);
141
- _this.message = patchErrorMessageFormatter(message, { name, index, operation, tree });
142
- return _this;
143
- }
144
- return PatchError2;
145
- })(Error)
146
- );
147
-
148
- // ../../node_modules/.pnpm/fast-json-patch@3.1.1/node_modules/fast-json-patch/module/core.mjs
149
- var JsonPatchError = PatchError;
150
- var deepClone = _deepClone;
151
- var objOps = {
152
- add: function(obj, key, document) {
153
- obj[key] = this.value;
154
- return { newDocument: document };
155
- },
156
- remove: function(obj, key, document) {
157
- var removed = obj[key];
158
- delete obj[key];
159
- return { newDocument: document, removed };
160
- },
161
- replace: function(obj, key, document) {
162
- var removed = obj[key];
163
- obj[key] = this.value;
164
- return { newDocument: document, removed };
165
- },
166
- move: function(obj, key, document) {
167
- var removed = getValueByPointer(document, this.path);
168
- if (removed) {
169
- removed = _deepClone(removed);
170
- }
171
- var originalValue = applyOperation(document, { op: "remove", path: this.from }).removed;
172
- applyOperation(document, { op: "add", path: this.path, value: originalValue });
173
- return { newDocument: document, removed };
174
- },
175
- copy: function(obj, key, document) {
176
- var valueToCopy = getValueByPointer(document, this.from);
177
- applyOperation(document, { op: "add", path: this.path, value: _deepClone(valueToCopy) });
178
- return { newDocument: document };
179
- },
180
- test: function(obj, key, document) {
181
- return { newDocument: document, test: _areEquals(obj[key], this.value) };
182
- },
183
- _get: function(obj, key, document) {
184
- this.value = obj[key];
185
- return { newDocument: document };
186
- }
187
- };
188
- var arrOps = {
189
- add: function(arr, i, document) {
190
- if (isInteger(i)) {
191
- arr.splice(i, 0, this.value);
192
- } else {
193
- arr[i] = this.value;
194
- }
195
- return { newDocument: document, index: i };
196
- },
197
- remove: function(arr, i, document) {
198
- var removedList = arr.splice(i, 1);
199
- return { newDocument: document, removed: removedList[0] };
200
- },
201
- replace: function(arr, i, document) {
202
- var removed = arr[i];
203
- arr[i] = this.value;
204
- return { newDocument: document, removed };
205
- },
206
- move: objOps.move,
207
- copy: objOps.copy,
208
- test: objOps.test,
209
- _get: objOps._get
210
- };
211
- function getValueByPointer(document, pointer) {
212
- if (pointer == "") {
213
- return document;
214
- }
215
- var getOriginalDestination = { op: "_get", path: pointer };
216
- applyOperation(document, getOriginalDestination);
217
- return getOriginalDestination.value;
218
- }
219
- function applyOperation(document, operation, validateOperation, mutateDocument, banPrototypeModifications, index) {
220
- if (validateOperation === void 0) {
221
- validateOperation = false;
222
- }
223
- if (mutateDocument === void 0) {
224
- mutateDocument = true;
225
- }
226
- if (banPrototypeModifications === void 0) {
227
- banPrototypeModifications = true;
228
- }
229
- if (index === void 0) {
230
- index = 0;
231
- }
232
- if (validateOperation) {
233
- if (typeof validateOperation == "function") {
234
- validateOperation(operation, 0, document, operation.path);
235
- } else {
236
- validator(operation, 0);
237
- }
238
- }
239
- if (operation.path === "") {
240
- var returnValue = { newDocument: document };
241
- if (operation.op === "add") {
242
- returnValue.newDocument = operation.value;
243
- return returnValue;
244
- } else if (operation.op === "replace") {
245
- returnValue.newDocument = operation.value;
246
- returnValue.removed = document;
247
- return returnValue;
248
- } else if (operation.op === "move" || operation.op === "copy") {
249
- returnValue.newDocument = getValueByPointer(document, operation.from);
250
- if (operation.op === "move") {
251
- returnValue.removed = document;
252
- }
253
- return returnValue;
254
- } else if (operation.op === "test") {
255
- returnValue.test = _areEquals(document, operation.value);
256
- if (returnValue.test === false) {
257
- throw new JsonPatchError("Test operation failed", "TEST_OPERATION_FAILED", index, operation, document);
258
- }
259
- returnValue.newDocument = document;
260
- return returnValue;
261
- } else if (operation.op === "remove") {
262
- returnValue.removed = document;
263
- returnValue.newDocument = null;
264
- return returnValue;
265
- } else if (operation.op === "_get") {
266
- operation.value = document;
267
- return returnValue;
268
- } else {
269
- if (validateOperation) {
270
- throw new JsonPatchError("Operation `op` property is not one of operations defined in RFC-6902", "OPERATION_OP_INVALID", index, operation, document);
271
- } else {
272
- return returnValue;
273
- }
274
- }
275
- } else {
276
- if (!mutateDocument) {
277
- document = _deepClone(document);
278
- }
279
- var path = operation.path || "";
280
- var keys = path.split("/");
281
- var obj = document;
282
- var t = 1;
283
- var len = keys.length;
284
- var existingPathFragment = void 0;
285
- var key = void 0;
286
- var validateFunction = void 0;
287
- if (typeof validateOperation == "function") {
288
- validateFunction = validateOperation;
289
- } else {
290
- validateFunction = validator;
291
- }
292
- while (true) {
293
- key = keys[t];
294
- if (key && key.indexOf("~") != -1) {
295
- key = unescapePathComponent(key);
296
- }
297
- if (banPrototypeModifications && (key == "__proto__" || key == "prototype" && t > 0 && keys[t - 1] == "constructor")) {
298
- throw new TypeError("JSON-Patch: modifying `__proto__` or `constructor/prototype` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README");
299
- }
300
- if (validateOperation) {
301
- if (existingPathFragment === void 0) {
302
- if (obj[key] === void 0) {
303
- existingPathFragment = keys.slice(0, t).join("/");
304
- } else if (t == len - 1) {
305
- existingPathFragment = operation.path;
306
- }
307
- if (existingPathFragment !== void 0) {
308
- validateFunction(operation, 0, document, existingPathFragment);
309
- }
310
- }
311
- }
312
- t++;
313
- if (Array.isArray(obj)) {
314
- if (key === "-") {
315
- key = obj.length;
316
- } else {
317
- if (validateOperation && !isInteger(key)) {
318
- throw new JsonPatchError("Expected an unsigned base-10 integer value, making the new referenced value the array element with the zero-based index", "OPERATION_PATH_ILLEGAL_ARRAY_INDEX", index, operation, document);
319
- } else if (isInteger(key)) {
320
- key = ~~key;
321
- }
322
- }
323
- if (t >= len) {
324
- if (validateOperation && operation.op === "add" && key > obj.length) {
325
- throw new JsonPatchError("The specified index MUST NOT be greater than the number of elements in the array", "OPERATION_VALUE_OUT_OF_BOUNDS", index, operation, document);
326
- }
327
- var returnValue = arrOps[operation.op].call(operation, obj, key, document);
328
- if (returnValue.test === false) {
329
- throw new JsonPatchError("Test operation failed", "TEST_OPERATION_FAILED", index, operation, document);
330
- }
331
- return returnValue;
332
- }
333
- } else {
334
- if (t >= len) {
335
- var returnValue = objOps[operation.op].call(operation, obj, key, document);
336
- if (returnValue.test === false) {
337
- throw new JsonPatchError("Test operation failed", "TEST_OPERATION_FAILED", index, operation, document);
338
- }
339
- return returnValue;
340
- }
341
- }
342
- obj = obj[key];
343
- if (validateOperation && t < len && (!obj || typeof obj !== "object")) {
344
- throw new JsonPatchError("Cannot perform operation at the desired path", "OPERATION_PATH_UNRESOLVABLE", index, operation, document);
345
- }
346
- }
347
- }
348
- }
349
- function applyPatch(document, patch, validateOperation, mutateDocument, banPrototypeModifications) {
350
- if (mutateDocument === void 0) {
351
- mutateDocument = true;
352
- }
353
- if (banPrototypeModifications === void 0) {
354
- banPrototypeModifications = true;
355
- }
356
- if (validateOperation) {
357
- if (!Array.isArray(patch)) {
358
- throw new JsonPatchError("Patch sequence must be an array", "SEQUENCE_NOT_AN_ARRAY");
359
- }
360
- }
361
- if (!mutateDocument) {
362
- document = _deepClone(document);
363
- }
364
- var results = new Array(patch.length);
365
- for (var i = 0, length_1 = patch.length; i < length_1; i++) {
366
- results[i] = applyOperation(document, patch[i], validateOperation, true, banPrototypeModifications, i);
367
- document = results[i].newDocument;
368
- }
369
- results.newDocument = document;
370
- return results;
371
- }
372
- function applyReducer(document, operation, index) {
373
- var operationResult = applyOperation(document, operation);
374
- if (operationResult.test === false) {
375
- throw new JsonPatchError("Test operation failed", "TEST_OPERATION_FAILED", index, operation, document);
376
- }
377
- return operationResult.newDocument;
378
- }
379
- function validator(operation, index, document, existingPathFragment) {
380
- if (typeof operation !== "object" || operation === null || Array.isArray(operation)) {
381
- throw new JsonPatchError("Operation is not an object", "OPERATION_NOT_AN_OBJECT", index, operation, document);
382
- } else if (!objOps[operation.op]) {
383
- throw new JsonPatchError("Operation `op` property is not one of operations defined in RFC-6902", "OPERATION_OP_INVALID", index, operation, document);
384
- } else if (typeof operation.path !== "string") {
385
- throw new JsonPatchError("Operation `path` property is not a string", "OPERATION_PATH_INVALID", index, operation, document);
386
- } else if (operation.path.indexOf("/") !== 0 && operation.path.length > 0) {
387
- throw new JsonPatchError('Operation `path` property must start with "/"', "OPERATION_PATH_INVALID", index, operation, document);
388
- } else if ((operation.op === "move" || operation.op === "copy") && typeof operation.from !== "string") {
389
- throw new JsonPatchError("Operation `from` property is not present (applicable in `move` and `copy` operations)", "OPERATION_FROM_REQUIRED", index, operation, document);
390
- } else if ((operation.op === "add" || operation.op === "replace" || operation.op === "test") && operation.value === void 0) {
391
- throw new JsonPatchError("Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)", "OPERATION_VALUE_REQUIRED", index, operation, document);
392
- } else if ((operation.op === "add" || operation.op === "replace" || operation.op === "test") && hasUndefined(operation.value)) {
393
- throw new JsonPatchError("Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)", "OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED", index, operation, document);
394
- } else if (document) {
395
- if (operation.op == "add") {
396
- var pathLen = operation.path.split("/").length;
397
- var existingPathLen = existingPathFragment.split("/").length;
398
- if (pathLen !== existingPathLen + 1 && pathLen !== existingPathLen) {
399
- throw new JsonPatchError("Cannot perform an `add` operation at the desired path", "OPERATION_PATH_CANNOT_ADD", index, operation, document);
400
- }
401
- } else if (operation.op === "replace" || operation.op === "remove" || operation.op === "_get") {
402
- if (operation.path !== existingPathFragment) {
403
- throw new JsonPatchError("Cannot perform the operation at a path that does not exist", "OPERATION_PATH_UNRESOLVABLE", index, operation, document);
404
- }
405
- } else if (operation.op === "move" || operation.op === "copy") {
406
- var existingValue = { op: "_get", path: operation.from, value: void 0 };
407
- var error = validate([existingValue], document);
408
- if (error && error.name === "OPERATION_PATH_UNRESOLVABLE") {
409
- throw new JsonPatchError("Cannot perform the operation from a path that does not exist", "OPERATION_FROM_UNRESOLVABLE", index, operation, document);
410
- }
411
- }
412
- }
413
- }
414
- function validate(sequence, document, externalValidator) {
415
- try {
416
- if (!Array.isArray(sequence)) {
417
- throw new JsonPatchError("Patch sequence must be an array", "SEQUENCE_NOT_AN_ARRAY");
418
- }
419
- if (document) {
420
- applyPatch(_deepClone(document), _deepClone(sequence), externalValidator || true);
421
- } else {
422
- externalValidator = externalValidator || validator;
423
- for (var i = 0; i < sequence.length; i++) {
424
- externalValidator(sequence[i], i, document, void 0);
425
- }
426
- }
427
- } catch (e) {
428
- if (e instanceof JsonPatchError) {
429
- return e;
430
- } else {
431
- throw e;
432
- }
433
- }
434
- }
435
- function _areEquals(a, b) {
436
- if (a === b)
437
- return true;
438
- if (a && b && typeof a == "object" && typeof b == "object") {
439
- var arrA = Array.isArray(a), arrB = Array.isArray(b), i, length, key;
440
- if (arrA && arrB) {
441
- length = a.length;
442
- if (length != b.length)
443
- return false;
444
- for (i = length; i-- !== 0; )
445
- if (!_areEquals(a[i], b[i]))
446
- return false;
447
- return true;
448
- }
449
- if (arrA != arrB)
450
- return false;
451
- var keys = Object.keys(a);
452
- length = keys.length;
453
- if (length !== Object.keys(b).length)
454
- return false;
455
- for (i = length; i-- !== 0; )
456
- if (!b.hasOwnProperty(keys[i]))
457
- return false;
458
- for (i = length; i-- !== 0; ) {
459
- key = keys[i];
460
- if (!_areEquals(a[key], b[key]))
461
- return false;
462
- }
463
- return true;
464
- }
465
- return a !== a && b !== b;
466
- }
467
-
468
- // ../../node_modules/.pnpm/fast-json-patch@3.1.1/node_modules/fast-json-patch/module/duplex.mjs
469
- var duplex_exports = {};
470
- __export(duplex_exports, {
471
- compare: () => compare,
472
- generate: () => generate,
473
- observe: () => observe,
474
- unobserve: () => unobserve
475
- });
476
- var beforeDict = /* @__PURE__ */ new WeakMap();
477
- var Mirror = (
478
- /** @class */
479
- /* @__PURE__ */ (function() {
480
- function Mirror2(obj) {
481
- this.observers = /* @__PURE__ */ new Map();
482
- this.obj = obj;
483
- }
484
- return Mirror2;
485
- })()
486
- );
487
- var ObserverInfo = (
488
- /** @class */
489
- /* @__PURE__ */ (function() {
490
- function ObserverInfo2(callback, observer) {
491
- this.callback = callback;
492
- this.observer = observer;
493
- }
494
- return ObserverInfo2;
495
- })()
496
- );
497
- function getMirror(obj) {
498
- return beforeDict.get(obj);
499
- }
500
- function getObserverFromMirror(mirror, callback) {
501
- return mirror.observers.get(callback);
502
- }
503
- function removeObserverFromMirror(mirror, observer) {
504
- mirror.observers.delete(observer.callback);
505
- }
506
- function unobserve(root, observer) {
507
- observer.unobserve();
508
- }
509
- function observe(obj, callback) {
510
- var patches = [];
511
- var observer;
512
- var mirror = getMirror(obj);
513
- if (!mirror) {
514
- mirror = new Mirror(obj);
515
- beforeDict.set(obj, mirror);
516
- } else {
517
- var observerInfo = getObserverFromMirror(mirror, callback);
518
- observer = observerInfo && observerInfo.observer;
519
- }
520
- if (observer) {
521
- return observer;
522
- }
523
- observer = {};
524
- mirror.value = _deepClone(obj);
525
- if (callback) {
526
- observer.callback = callback;
527
- observer.next = null;
528
- var dirtyCheck = function() {
529
- generate(observer);
530
- };
531
- var fastCheck = function() {
532
- clearTimeout(observer.next);
533
- observer.next = setTimeout(dirtyCheck);
534
- };
535
- if (typeof window !== "undefined") {
536
- window.addEventListener("mouseup", fastCheck);
537
- window.addEventListener("keyup", fastCheck);
538
- window.addEventListener("mousedown", fastCheck);
539
- window.addEventListener("keydown", fastCheck);
540
- window.addEventListener("change", fastCheck);
541
- }
542
- }
543
- observer.patches = patches;
544
- observer.object = obj;
545
- observer.unobserve = function() {
546
- generate(observer);
547
- clearTimeout(observer.next);
548
- removeObserverFromMirror(mirror, observer);
549
- if (typeof window !== "undefined") {
550
- window.removeEventListener("mouseup", fastCheck);
551
- window.removeEventListener("keyup", fastCheck);
552
- window.removeEventListener("mousedown", fastCheck);
553
- window.removeEventListener("keydown", fastCheck);
554
- window.removeEventListener("change", fastCheck);
555
- }
556
- };
557
- mirror.observers.set(callback, new ObserverInfo(callback, observer));
558
- return observer;
559
- }
560
- function generate(observer, invertible) {
561
- if (invertible === void 0) {
562
- invertible = false;
563
- }
564
- var mirror = beforeDict.get(observer.object);
565
- _generate(mirror.value, observer.object, observer.patches, "", invertible);
566
- if (observer.patches.length) {
567
- applyPatch(mirror.value, observer.patches);
568
- }
569
- var temp = observer.patches;
570
- if (temp.length > 0) {
571
- observer.patches = [];
572
- if (observer.callback) {
573
- observer.callback(temp);
574
- }
575
- }
576
- return temp;
577
- }
578
- function _generate(mirror, obj, patches, path, invertible) {
579
- if (obj === mirror) {
580
- return;
581
- }
582
- if (typeof obj.toJSON === "function") {
583
- obj = obj.toJSON();
584
- }
585
- var newKeys = _objectKeys(obj);
586
- var oldKeys = _objectKeys(mirror);
587
- var changed = false;
588
- var deleted = false;
589
- for (var t = oldKeys.length - 1; t >= 0; t--) {
590
- var key = oldKeys[t];
591
- var oldVal = mirror[key];
592
- if (hasOwnProperty(obj, key) && !(obj[key] === void 0 && oldVal !== void 0 && Array.isArray(obj) === false)) {
593
- var newVal = obj[key];
594
- if (typeof oldVal == "object" && oldVal != null && typeof newVal == "object" && newVal != null && Array.isArray(oldVal) === Array.isArray(newVal)) {
595
- _generate(oldVal, newVal, patches, path + "/" + escapePathComponent(key), invertible);
596
- } else {
597
- if (oldVal !== newVal) {
598
- changed = true;
599
- if (invertible) {
600
- patches.push({ op: "test", path: path + "/" + escapePathComponent(key), value: _deepClone(oldVal) });
601
- }
602
- patches.push({ op: "replace", path: path + "/" + escapePathComponent(key), value: _deepClone(newVal) });
603
- }
604
- }
605
- } else if (Array.isArray(mirror) === Array.isArray(obj)) {
606
- if (invertible) {
607
- patches.push({ op: "test", path: path + "/" + escapePathComponent(key), value: _deepClone(oldVal) });
608
- }
609
- patches.push({ op: "remove", path: path + "/" + escapePathComponent(key) });
610
- deleted = true;
611
- } else {
612
- if (invertible) {
613
- patches.push({ op: "test", path, value: mirror });
614
- }
615
- patches.push({ op: "replace", path, value: obj });
616
- changed = true;
617
- }
618
- }
619
- if (!deleted && newKeys.length == oldKeys.length) {
620
- return;
621
- }
622
- for (var t = 0; t < newKeys.length; t++) {
623
- var key = newKeys[t];
624
- if (!hasOwnProperty(mirror, key) && obj[key] !== void 0) {
625
- patches.push({ op: "add", path: path + "/" + escapePathComponent(key), value: _deepClone(obj[key]) });
626
- }
627
- }
628
- }
629
- function compare(tree1, tree2, invertible) {
630
- if (invertible === void 0) {
631
- invertible = false;
632
- }
633
- var patches = [];
634
- _generate(tree1, tree2, patches, "", invertible);
635
- return patches;
636
- }
637
-
638
- // ../../node_modules/.pnpm/fast-json-patch@3.1.1/node_modules/fast-json-patch/index.mjs
639
- var fast_json_patch_default = Object.assign({}, core_exports, duplex_exports, {
640
- JsonPatchError: PatchError,
641
- deepClone: _deepClone,
642
- escapePathComponent,
643
- unescapePathComponent
644
- });
645
-
646
37
  // src/apply-patch.ts
647
- function applyBroadcastMessage(msg, cached) {
38
+ function applyPatchMessage(msg, cached) {
648
39
  const cachedV = cached?._v ?? 0;
649
- if (msg._type === "full") {
650
- if (msg._v < cachedV) return { ok: false, reason: "stale" };
651
- const { _type, ...state } = msg;
652
- return { ok: true, state };
653
- }
654
- if (msg._baseV < cachedV) return { ok: false, reason: "stale" };
655
- if (msg._baseV > cachedV) return { ok: false, reason: "behind" };
656
- if (!cached) return { ok: false, reason: "behind" };
657
- try {
658
- const clone = structuredClone(cached);
659
- applyPatch(clone, msg._patch, true);
660
- clone._v = msg._v;
661
- return { ok: true, state: clone };
662
- } catch {
663
- return { ok: false, reason: "patch-failed" };
664
- }
40
+ const versions = Object.keys(msg).map(Number).sort((a, b) => a - b);
41
+ if (!versions.length) return { ok: false, reason: "stale" };
42
+ const minV = versions[0];
43
+ const maxV = versions[versions.length - 1];
44
+ if (maxV <= cachedV) return { ok: false, reason: "stale" };
45
+ if (!cached || minV > cachedV + 1) return { ok: false, reason: "behind" };
46
+ let state = cached;
47
+ for (const v of versions) {
48
+ if (v <= cachedV) continue;
49
+ state = { ...patch(state, msg[v]), _v: v };
50
+ }
51
+ return { ok: true, state };
665
52
  }
666
53
 
667
54
  // src/state-cache.ts
@@ -707,27 +94,27 @@ var StateCache = class {
707
94
  };
708
95
 
709
96
  // src/broadcast.ts
710
- var DEFAULT_MAX_PATCH_OPS = 50;
711
97
  var BroadcastChannel = class {
712
98
  channels = /* @__PURE__ */ new Map();
713
99
  stateCache;
714
- maxPatchOps;
715
100
  constructor(options) {
716
101
  this.stateCache = new StateCache(options?.cacheSize ?? 50);
717
- this.maxPatchOps = options?.maxPatchOps ?? DEFAULT_MAX_PATCH_OPS;
718
102
  }
719
103
  /**
720
- * Publish new state for a stream. Computes a patch against the previously
721
- * cached state and pushes to all subscribers.
104
+ * Publish domain patches from a commit.
105
+ * patches[i] corresponds to version baseV + i + 1.
722
106
  *
723
107
  * @param streamId - The event store stream ID
724
108
  * @param state - Full state with `_v` set from `snap.event.version`
725
- * @returns The broadcast message that was sent (or undefined if no subscribers and no cache change)
109
+ * @param patches - Array of domain patches, one per emitted event
726
110
  */
727
- publish(streamId, state) {
728
- const prev = this.stateCache.get(streamId);
111
+ publish(streamId, state, patches = []) {
729
112
  this.stateCache.set(streamId, state);
730
- const msg = this.computeMessage(prev, state);
113
+ const baseV = state._v - patches.length;
114
+ const msg = {};
115
+ patches.forEach((p, i) => {
116
+ msg[baseV + i + 1] = p;
117
+ });
731
118
  const subs = this.channels.get(streamId);
732
119
  if (subs?.size) {
733
120
  for (const cb of subs) cb(msg);
@@ -737,13 +124,14 @@ var BroadcastChannel = class {
737
124
  /**
738
125
  * Publish a state update that doesn't change the event version
739
126
  * (e.g. presence overlay, computed field refresh).
740
- * Uses the same version as the cached state for _baseV and _v.
127
+ * Uses the same version as the cached state, single entry.
741
128
  */
742
- publishOverlay(streamId, state) {
129
+ publishOverlay(streamId, overlayPatch) {
743
130
  const prev = this.stateCache.get(streamId);
744
131
  if (!prev) return void 0;
132
+ const state = patch(prev, overlayPatch);
745
133
  this.stateCache.set(streamId, state);
746
- const msg = this.computeMessage(prev, state);
134
+ const msg = { [state._v]: overlayPatch };
747
135
  const subs = this.channels.get(streamId);
748
136
  if (subs?.size) {
749
137
  for (const cb of subs) cb(msg);
@@ -776,27 +164,6 @@ var BroadcastChannel = class {
776
164
  get cache() {
777
165
  return this.stateCache;
778
166
  }
779
- // --- internals ---
780
- computeMessage(prev, next) {
781
- const serverTime = (/* @__PURE__ */ new Date()).toISOString();
782
- if (!prev) {
783
- return { _type: "full", ...next, serverTime };
784
- }
785
- const ops = compare(prev, next);
786
- if (ops.length === 0) {
787
- return { _type: "full", ...next, serverTime };
788
- }
789
- if (ops.length > this.maxPatchOps) {
790
- return { _type: "full", ...next, serverTime };
791
- }
792
- return {
793
- _type: "patch",
794
- _v: next._v,
795
- _baseV: prev._v,
796
- _patch: ops,
797
- serverTime
798
- };
799
- }
800
167
  };
801
168
 
802
169
  // src/presence.ts
@@ -831,22 +198,7 @@ export {
831
198
  BroadcastChannel,
832
199
  PresenceTracker,
833
200
  StateCache,
834
- applyBroadcastMessage
201
+ applyPatchMessage,
202
+ patch
835
203
  };
836
- /*! Bundled license information:
837
-
838
- fast-json-patch/module/helpers.mjs:
839
- (*!
840
- * https://github.com/Starcounter-Jack/JSON-Patch
841
- * (c) 2017-2022 Joachim Wester
842
- * MIT licensed
843
- *)
844
-
845
- fast-json-patch/module/duplex.mjs:
846
- (*!
847
- * https://github.com/Starcounter-Jack/JSON-Patch
848
- * (c) 2017-2021 Joachim Wester
849
- * MIT license
850
- *)
851
- */
852
204
  //# sourceMappingURL=index.js.map