cry-synced-db-client 0.1.4 → 0.1.6
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/db/RestProxy.d.ts +3 -3
- package/dist/db/SyncedDb.d.ts +9 -0
- package/dist/index.js +2304 -20
- package/dist/types/I_SyncedDb.d.ts +54 -1
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -274,6 +274,8 @@ class SyncedDb {
|
|
|
274
274
|
onServerWriteResult;
|
|
275
275
|
onFindNewerManyCall;
|
|
276
276
|
onFindNewerCall;
|
|
277
|
+
onDexieWriteRequest;
|
|
278
|
+
onDexieWriteResult;
|
|
277
279
|
autoSyncIntervalMs;
|
|
278
280
|
online = false;
|
|
279
281
|
syncing = false;
|
|
@@ -302,6 +304,8 @@ class SyncedDb {
|
|
|
302
304
|
this.onServerWriteResult = config.onServerWriteResult;
|
|
303
305
|
this.onFindNewerManyCall = config.onFindNewerManyCall;
|
|
304
306
|
this.onFindNewerCall = config.onFindNewerCall;
|
|
307
|
+
this.onDexieWriteRequest = config.onDexieWriteRequest;
|
|
308
|
+
this.onDexieWriteResult = config.onDexieWriteResult;
|
|
305
309
|
this.autoSyncIntervalMs = config.autoSyncIntervalMs;
|
|
306
310
|
this.updaterId = Math.random().toString(36).substring(2, 15);
|
|
307
311
|
for (const col of config.collections) {
|
|
@@ -488,7 +492,7 @@ class SyncedDb {
|
|
|
488
492
|
_localChangedAt: new Date,
|
|
489
493
|
_lastUpdaterId: this.updaterId
|
|
490
494
|
};
|
|
491
|
-
this.schedulePendingChange(collection, id, newData);
|
|
495
|
+
this.schedulePendingChange(collection, id, newData, 0, "save");
|
|
492
496
|
if (existing?._deleted) {} else {
|
|
493
497
|
this.inMemDb.save(collection, id, update);
|
|
494
498
|
}
|
|
@@ -520,7 +524,7 @@ class SyncedDb {
|
|
|
520
524
|
_localChangedAt: new Date,
|
|
521
525
|
_lastUpdaterId: this.updaterId
|
|
522
526
|
};
|
|
523
|
-
this.schedulePendingChange(collection, id, newData);
|
|
527
|
+
this.schedulePendingChange(collection, id, newData, 0, "insert");
|
|
524
528
|
this.inMemDb.insert(collection, this.stripLocalFields(newData));
|
|
525
529
|
return this.stripLocalFields(newData);
|
|
526
530
|
}
|
|
@@ -536,7 +540,7 @@ class SyncedDb {
|
|
|
536
540
|
_localChangedAt: new Date,
|
|
537
541
|
_lastUpdaterId: this.updaterId
|
|
538
542
|
};
|
|
539
|
-
this.schedulePendingChange(collection, id, deleteUpdate);
|
|
543
|
+
this.schedulePendingChange(collection, id, deleteUpdate, 0, "deleteOne");
|
|
540
544
|
this.inMemDb.deleteOne(collection, id);
|
|
541
545
|
return this.stripLocalFields(existing);
|
|
542
546
|
}
|
|
@@ -559,7 +563,7 @@ class SyncedDb {
|
|
|
559
563
|
_localChangedAt: now,
|
|
560
564
|
_lastUpdaterId: this.updaterId
|
|
561
565
|
};
|
|
562
|
-
this.schedulePendingChange(collection, item._id, deleteUpdate);
|
|
566
|
+
this.schedulePendingChange(collection, item._id, deleteUpdate, 0, "deleteMany");
|
|
563
567
|
this.inMemDb.deleteOne(collection, item._id);
|
|
564
568
|
count++;
|
|
565
569
|
}
|
|
@@ -749,6 +753,60 @@ class SyncedDb {
|
|
|
749
753
|
}
|
|
750
754
|
return result;
|
|
751
755
|
}
|
|
756
|
+
async deleteCollectionData(collection, force = false) {
|
|
757
|
+
this.assertCollection(collection);
|
|
758
|
+
await this.flushPendingChangesForCollection(collection);
|
|
759
|
+
let sendSuccess = true;
|
|
760
|
+
if (this.online) {
|
|
761
|
+
try {
|
|
762
|
+
await this.uploadDirtyItemsForCollection(collection);
|
|
763
|
+
} catch (err) {
|
|
764
|
+
console.error(`Failed to send dirty data for ${collection}:`, err);
|
|
765
|
+
sendSuccess = false;
|
|
766
|
+
}
|
|
767
|
+
} else {
|
|
768
|
+
const dirtyItems = await this.dexieDb.getDirty(collection);
|
|
769
|
+
if (dirtyItems.length > 0) {
|
|
770
|
+
sendSuccess = false;
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
if (!sendSuccess && !force) {
|
|
774
|
+
return false;
|
|
775
|
+
}
|
|
776
|
+
await this.dexieDb.deleteCollection(collection);
|
|
777
|
+
this.inMemDb.deleteCollection(collection);
|
|
778
|
+
await this.dexieDb.deleteSyncMeta(collection);
|
|
779
|
+
return true;
|
|
780
|
+
}
|
|
781
|
+
async deleteAllCollectionsData(force = false) {
|
|
782
|
+
await this.flushAllPendingChanges();
|
|
783
|
+
let sendSuccess = true;
|
|
784
|
+
if (this.online) {
|
|
785
|
+
try {
|
|
786
|
+
await this.uploadDirtyItems();
|
|
787
|
+
} catch (err) {
|
|
788
|
+
console.error("Failed to send dirty data:", err);
|
|
789
|
+
sendSuccess = false;
|
|
790
|
+
}
|
|
791
|
+
} else {
|
|
792
|
+
for (const [collectionName] of this.collections) {
|
|
793
|
+
const dirtyItems = await this.dexieDb.getDirty(collectionName);
|
|
794
|
+
if (dirtyItems.length > 0) {
|
|
795
|
+
sendSuccess = false;
|
|
796
|
+
break;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
if (!sendSuccess && !force) {
|
|
801
|
+
return false;
|
|
802
|
+
}
|
|
803
|
+
for (const [collectionName] of this.collections) {
|
|
804
|
+
await this.dexieDb.deleteCollection(collectionName);
|
|
805
|
+
this.inMemDb.deleteCollection(collectionName);
|
|
806
|
+
await this.dexieDb.deleteSyncMeta(collectionName);
|
|
807
|
+
}
|
|
808
|
+
return true;
|
|
809
|
+
}
|
|
752
810
|
assertCollection(name) {
|
|
753
811
|
if (!this.collections.has(name)) {
|
|
754
812
|
throw new Error(`Collection "${name}" not configured`);
|
|
@@ -761,7 +819,7 @@ class SyncedDb {
|
|
|
761
819
|
getPendingKey(collection, id) {
|
|
762
820
|
return `${collection}:${String(id)}`;
|
|
763
821
|
}
|
|
764
|
-
schedulePendingChange(collection, id, data, retryCount = 0) {
|
|
822
|
+
schedulePendingChange(collection, id, data, retryCount = 0, calledFrom) {
|
|
765
823
|
const key = this.getPendingKey(collection, id);
|
|
766
824
|
const existing = this.pendingChanges.get(key);
|
|
767
825
|
if (existing) {
|
|
@@ -773,12 +831,14 @@ class SyncedDb {
|
|
|
773
831
|
this.executePendingChange(key);
|
|
774
832
|
}, this.debounceDexieWritesMs);
|
|
775
833
|
const newRetryCount = retryCount > 0 ? retryCount : existing?.retryCount ?? 0;
|
|
834
|
+
const newCalledFrom = calledFrom ?? existing?.calledFrom;
|
|
776
835
|
this.pendingChanges.set(key, {
|
|
777
836
|
collection,
|
|
778
837
|
id,
|
|
779
838
|
data: fullData,
|
|
780
839
|
timer,
|
|
781
|
-
retryCount: newRetryCount
|
|
840
|
+
retryCount: newRetryCount,
|
|
841
|
+
calledFrom: newCalledFrom
|
|
782
842
|
});
|
|
783
843
|
}
|
|
784
844
|
async executePendingChange(key) {
|
|
@@ -786,8 +846,25 @@ class SyncedDb {
|
|
|
786
846
|
if (!pending)
|
|
787
847
|
return;
|
|
788
848
|
this.pendingChanges.delete(key);
|
|
849
|
+
const startTime = Date.now();
|
|
850
|
+
let isInsert = false;
|
|
789
851
|
try {
|
|
790
852
|
const existing = await this.dexieDb.getById(pending.collection, pending.id);
|
|
853
|
+
isInsert = !existing;
|
|
854
|
+
if (this.onDexieWriteRequest) {
|
|
855
|
+
try {
|
|
856
|
+
this.onDexieWriteRequest({
|
|
857
|
+
collection: pending.collection,
|
|
858
|
+
id: pending.id,
|
|
859
|
+
data: pending.data,
|
|
860
|
+
isInsert,
|
|
861
|
+
timestamp: new Date,
|
|
862
|
+
calledFrom: pending.calledFrom
|
|
863
|
+
});
|
|
864
|
+
} catch (err) {
|
|
865
|
+
console.error("onDexieWriteRequest callback failed:", err);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
791
868
|
if (existing) {
|
|
792
869
|
await this.dexieDb.save(pending.collection, pending.id, pending.data);
|
|
793
870
|
} else {
|
|
@@ -796,16 +873,43 @@ class SyncedDb {
|
|
|
796
873
|
...pending.data
|
|
797
874
|
});
|
|
798
875
|
}
|
|
876
|
+
if (this.onDexieWriteResult) {
|
|
877
|
+
try {
|
|
878
|
+
this.onDexieWriteResult({
|
|
879
|
+
collection: pending.collection,
|
|
880
|
+
id: pending.id,
|
|
881
|
+
durationMs: Date.now() - startTime,
|
|
882
|
+
success: true,
|
|
883
|
+
calledFrom: pending.calledFrom
|
|
884
|
+
});
|
|
885
|
+
} catch (err) {
|
|
886
|
+
console.error("onDexieWriteResult callback failed:", err);
|
|
887
|
+
}
|
|
888
|
+
}
|
|
799
889
|
clearPendingWrite(this.tenant, pending.collection, pending.id);
|
|
800
890
|
this.scheduleRestUpload();
|
|
801
891
|
} catch (err) {
|
|
802
892
|
console.error("Failed to write to Dexie:", err);
|
|
893
|
+
if (this.onDexieWriteResult) {
|
|
894
|
+
try {
|
|
895
|
+
this.onDexieWriteResult({
|
|
896
|
+
collection: pending.collection,
|
|
897
|
+
id: pending.id,
|
|
898
|
+
durationMs: Date.now() - startTime,
|
|
899
|
+
success: false,
|
|
900
|
+
error: err instanceof Error ? err : new Error(String(err)),
|
|
901
|
+
calledFrom: pending.calledFrom
|
|
902
|
+
});
|
|
903
|
+
} catch (callbackErr) {
|
|
904
|
+
console.error("onDexieWriteResult callback failed:", callbackErr);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
803
907
|
const newRetryCount = pending.retryCount + 1;
|
|
804
908
|
if (newRetryCount >= MAX_RETRY_COUNT) {
|
|
805
909
|
console.error(`Max retry count (${MAX_RETRY_COUNT}) reached for pending change ${key}. ` + `Data remains in localStorage for crash recovery.`);
|
|
806
910
|
return;
|
|
807
911
|
}
|
|
808
|
-
this.schedulePendingChange(pending.collection, pending.id, pending.data, newRetryCount);
|
|
912
|
+
this.schedulePendingChange(pending.collection, pending.id, pending.data, newRetryCount, pending.calledFrom);
|
|
809
913
|
}
|
|
810
914
|
}
|
|
811
915
|
scheduleRestUpload() {
|
|
@@ -840,6 +944,55 @@ class SyncedDb {
|
|
|
840
944
|
}
|
|
841
945
|
await Promise.all(promises);
|
|
842
946
|
}
|
|
947
|
+
async flushPendingChangesForCollection(collection) {
|
|
948
|
+
const promises = [];
|
|
949
|
+
for (const [key, pending] of this.pendingChanges) {
|
|
950
|
+
if (pending.collection === collection) {
|
|
951
|
+
clearTimeout(pending.timer);
|
|
952
|
+
promises.push(this.executePendingChange(key));
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
await Promise.all(promises);
|
|
956
|
+
}
|
|
957
|
+
async uploadDirtyItemsForCollection(collection) {
|
|
958
|
+
const dirtyItems = await this.dexieDb.getDirty(collection);
|
|
959
|
+
if (dirtyItems.length === 0) {
|
|
960
|
+
return { sentCount: 0 };
|
|
961
|
+
}
|
|
962
|
+
const updates = [];
|
|
963
|
+
const deletes = [];
|
|
964
|
+
for (const item of dirtyItems) {
|
|
965
|
+
if (item._deleted) {
|
|
966
|
+
deletes.push(item);
|
|
967
|
+
} else {
|
|
968
|
+
updates.push(item);
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
const collectionBatches = [[{
|
|
972
|
+
collection,
|
|
973
|
+
batch: {
|
|
974
|
+
updates: updates.map((item) => ({
|
|
975
|
+
_id: item._id,
|
|
976
|
+
update: this.stripLocalFields(item)
|
|
977
|
+
})),
|
|
978
|
+
deletes: deletes.map((item) => ({ _id: item._id }))
|
|
979
|
+
}
|
|
980
|
+
}]];
|
|
981
|
+
const results = await this.withSyncTimeout(this.restInterface.updateCollections(collectionBatches), "updateCollections");
|
|
982
|
+
let sentCount = 0;
|
|
983
|
+
for (const result of results) {
|
|
984
|
+
const { results: { updatedIds, deletedIds } } = result;
|
|
985
|
+
for (const id of updatedIds) {
|
|
986
|
+
await this.dexieDb.save(collection, id, { _dirty: false });
|
|
987
|
+
sentCount++;
|
|
988
|
+
}
|
|
989
|
+
for (const id of deletedIds) {
|
|
990
|
+
await this.dexieDb.deleteOne(collection, id);
|
|
991
|
+
sentCount++;
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
return { sentCount };
|
|
995
|
+
}
|
|
843
996
|
async recoverPendingWrites() {
|
|
844
997
|
const pending = getPendingWrites(this.tenant);
|
|
845
998
|
for (const write of pending) {
|
|
@@ -1307,17 +1460,2148 @@ class DexieDb extends Dexie {
|
|
|
1307
1460
|
return this.tenant;
|
|
1308
1461
|
}
|
|
1309
1462
|
}
|
|
1310
|
-
//
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1463
|
+
// node_modules/msgpackr/unpack.js
|
|
1464
|
+
var decoder;
|
|
1465
|
+
try {
|
|
1466
|
+
decoder = new TextDecoder;
|
|
1467
|
+
} catch (error) {}
|
|
1468
|
+
var src;
|
|
1469
|
+
var srcEnd;
|
|
1470
|
+
var position = 0;
|
|
1471
|
+
var EMPTY_ARRAY = [];
|
|
1472
|
+
var strings = EMPTY_ARRAY;
|
|
1473
|
+
var stringPosition = 0;
|
|
1474
|
+
var currentUnpackr = {};
|
|
1475
|
+
var currentStructures;
|
|
1476
|
+
var srcString;
|
|
1477
|
+
var srcStringStart = 0;
|
|
1478
|
+
var srcStringEnd = 0;
|
|
1479
|
+
var bundledStrings;
|
|
1480
|
+
var referenceMap;
|
|
1481
|
+
var currentExtensions = [];
|
|
1482
|
+
var dataView;
|
|
1483
|
+
var defaultOptions = {
|
|
1484
|
+
useRecords: false,
|
|
1485
|
+
mapsAsObjects: true
|
|
1486
|
+
};
|
|
1487
|
+
|
|
1488
|
+
class C1Type {
|
|
1489
|
+
}
|
|
1490
|
+
var C1 = new C1Type;
|
|
1491
|
+
C1.name = "MessagePack 0xC1";
|
|
1492
|
+
var sequentialMode = false;
|
|
1493
|
+
var inlineObjectReadThreshold = 2;
|
|
1494
|
+
var readStruct;
|
|
1495
|
+
var onLoadedStructures;
|
|
1496
|
+
var onSaveState;
|
|
1497
|
+
try {
|
|
1498
|
+
new Function("");
|
|
1499
|
+
} catch (error) {
|
|
1500
|
+
inlineObjectReadThreshold = Infinity;
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1503
|
+
class Unpackr {
|
|
1504
|
+
constructor(options) {
|
|
1505
|
+
if (options) {
|
|
1506
|
+
if (options.useRecords === false && options.mapsAsObjects === undefined)
|
|
1507
|
+
options.mapsAsObjects = true;
|
|
1508
|
+
if (options.sequential && options.trusted !== false) {
|
|
1509
|
+
options.trusted = true;
|
|
1510
|
+
if (!options.structures && options.useRecords != false) {
|
|
1511
|
+
options.structures = [];
|
|
1512
|
+
if (!options.maxSharedStructures)
|
|
1513
|
+
options.maxSharedStructures = 0;
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
if (options.structures)
|
|
1517
|
+
options.structures.sharedLength = options.structures.length;
|
|
1518
|
+
else if (options.getStructures) {
|
|
1519
|
+
(options.structures = []).uninitialized = true;
|
|
1520
|
+
options.structures.sharedLength = 0;
|
|
1521
|
+
}
|
|
1522
|
+
if (options.int64AsNumber) {
|
|
1523
|
+
options.int64AsType = "number";
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
Object.assign(this, options);
|
|
1527
|
+
}
|
|
1528
|
+
unpack(source, options) {
|
|
1529
|
+
if (src) {
|
|
1530
|
+
return saveState(() => {
|
|
1531
|
+
clearSource();
|
|
1532
|
+
return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options);
|
|
1533
|
+
});
|
|
1534
|
+
}
|
|
1535
|
+
if (!source.buffer && source.constructor === ArrayBuffer)
|
|
1536
|
+
source = typeof Buffer !== "undefined" ? Buffer.from(source) : new Uint8Array(source);
|
|
1537
|
+
if (typeof options === "object") {
|
|
1538
|
+
srcEnd = options.end || source.length;
|
|
1539
|
+
position = options.start || 0;
|
|
1540
|
+
} else {
|
|
1541
|
+
position = 0;
|
|
1542
|
+
srcEnd = options > -1 ? options : source.length;
|
|
1543
|
+
}
|
|
1544
|
+
stringPosition = 0;
|
|
1545
|
+
srcStringEnd = 0;
|
|
1546
|
+
srcString = null;
|
|
1547
|
+
strings = EMPTY_ARRAY;
|
|
1548
|
+
bundledStrings = null;
|
|
1549
|
+
src = source;
|
|
1550
|
+
try {
|
|
1551
|
+
dataView = source.dataView || (source.dataView = new DataView(source.buffer, source.byteOffset, source.byteLength));
|
|
1552
|
+
} catch (error) {
|
|
1553
|
+
src = null;
|
|
1554
|
+
if (source instanceof Uint8Array)
|
|
1555
|
+
throw error;
|
|
1556
|
+
throw new Error("Source must be a Uint8Array or Buffer but was a " + (source && typeof source == "object" ? source.constructor.name : typeof source));
|
|
1557
|
+
}
|
|
1558
|
+
if (this instanceof Unpackr) {
|
|
1559
|
+
currentUnpackr = this;
|
|
1560
|
+
if (this.structures) {
|
|
1561
|
+
currentStructures = this.structures;
|
|
1562
|
+
return checkedRead(options);
|
|
1563
|
+
} else if (!currentStructures || currentStructures.length > 0) {
|
|
1564
|
+
currentStructures = [];
|
|
1565
|
+
}
|
|
1566
|
+
} else {
|
|
1567
|
+
currentUnpackr = defaultOptions;
|
|
1568
|
+
if (!currentStructures || currentStructures.length > 0)
|
|
1569
|
+
currentStructures = [];
|
|
1570
|
+
}
|
|
1571
|
+
return checkedRead(options);
|
|
1572
|
+
}
|
|
1573
|
+
unpackMultiple(source, forEach) {
|
|
1574
|
+
let values, lastPosition = 0;
|
|
1575
|
+
try {
|
|
1576
|
+
sequentialMode = true;
|
|
1577
|
+
let size = source.length;
|
|
1578
|
+
let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
|
|
1579
|
+
if (forEach) {
|
|
1580
|
+
if (forEach(value, lastPosition, position) === false)
|
|
1581
|
+
return;
|
|
1582
|
+
while (position < size) {
|
|
1583
|
+
lastPosition = position;
|
|
1584
|
+
if (forEach(checkedRead(), lastPosition, position) === false) {
|
|
1585
|
+
return;
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
} else {
|
|
1589
|
+
values = [value];
|
|
1590
|
+
while (position < size) {
|
|
1591
|
+
lastPosition = position;
|
|
1592
|
+
values.push(checkedRead());
|
|
1593
|
+
}
|
|
1594
|
+
return values;
|
|
1595
|
+
}
|
|
1596
|
+
} catch (error) {
|
|
1597
|
+
error.lastPosition = lastPosition;
|
|
1598
|
+
error.values = values;
|
|
1599
|
+
throw error;
|
|
1600
|
+
} finally {
|
|
1601
|
+
sequentialMode = false;
|
|
1602
|
+
clearSource();
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
_mergeStructures(loadedStructures, existingStructures) {
|
|
1606
|
+
if (onLoadedStructures)
|
|
1607
|
+
loadedStructures = onLoadedStructures.call(this, loadedStructures);
|
|
1608
|
+
loadedStructures = loadedStructures || [];
|
|
1609
|
+
if (Object.isFrozen(loadedStructures))
|
|
1610
|
+
loadedStructures = loadedStructures.map((structure) => structure.slice(0));
|
|
1611
|
+
for (let i = 0, l = loadedStructures.length;i < l; i++) {
|
|
1612
|
+
let structure = loadedStructures[i];
|
|
1613
|
+
if (structure) {
|
|
1614
|
+
structure.isShared = true;
|
|
1615
|
+
if (i >= 32)
|
|
1616
|
+
structure.highByte = i - 32 >> 5;
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
loadedStructures.sharedLength = loadedStructures.length;
|
|
1620
|
+
for (let id in existingStructures || []) {
|
|
1621
|
+
if (id >= 0) {
|
|
1622
|
+
let structure = loadedStructures[id];
|
|
1623
|
+
let existing = existingStructures[id];
|
|
1624
|
+
if (existing) {
|
|
1625
|
+
if (structure)
|
|
1626
|
+
(loadedStructures.restoreStructures || (loadedStructures.restoreStructures = []))[id] = structure;
|
|
1627
|
+
loadedStructures[id] = existing;
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
1631
|
+
return this.structures = loadedStructures;
|
|
1632
|
+
}
|
|
1633
|
+
decode(source, options) {
|
|
1634
|
+
return this.unpack(source, options);
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
function checkedRead(options) {
|
|
1638
|
+
try {
|
|
1639
|
+
if (!currentUnpackr.trusted && !sequentialMode) {
|
|
1640
|
+
let sharedLength = currentStructures.sharedLength || 0;
|
|
1641
|
+
if (sharedLength < currentStructures.length)
|
|
1642
|
+
currentStructures.length = sharedLength;
|
|
1643
|
+
}
|
|
1644
|
+
let result;
|
|
1645
|
+
if (currentUnpackr.randomAccessStructure && src[position] < 64 && src[position] >= 32 && readStruct) {
|
|
1646
|
+
result = readStruct(src, position, srcEnd, currentUnpackr);
|
|
1647
|
+
src = null;
|
|
1648
|
+
if (!(options && options.lazy) && result)
|
|
1649
|
+
result = result.toJSON();
|
|
1650
|
+
position = srcEnd;
|
|
1651
|
+
} else
|
|
1652
|
+
result = read();
|
|
1653
|
+
if (bundledStrings) {
|
|
1654
|
+
position = bundledStrings.postBundlePosition;
|
|
1655
|
+
bundledStrings = null;
|
|
1656
|
+
}
|
|
1657
|
+
if (sequentialMode)
|
|
1658
|
+
currentStructures.restoreStructures = null;
|
|
1659
|
+
if (position == srcEnd) {
|
|
1660
|
+
if (currentStructures && currentStructures.restoreStructures)
|
|
1661
|
+
restoreStructures();
|
|
1662
|
+
currentStructures = null;
|
|
1663
|
+
src = null;
|
|
1664
|
+
if (referenceMap)
|
|
1665
|
+
referenceMap = null;
|
|
1666
|
+
} else if (position > srcEnd) {
|
|
1667
|
+
throw new Error("Unexpected end of MessagePack data");
|
|
1668
|
+
} else if (!sequentialMode) {
|
|
1669
|
+
let jsonView;
|
|
1670
|
+
try {
|
|
1671
|
+
jsonView = JSON.stringify(result, (_, value) => typeof value === "bigint" ? `${value}n` : value).slice(0, 100);
|
|
1672
|
+
} catch (error) {
|
|
1673
|
+
jsonView = "(JSON view not available " + error + ")";
|
|
1674
|
+
}
|
|
1675
|
+
throw new Error("Data read, but end of buffer not reached " + jsonView);
|
|
1676
|
+
}
|
|
1677
|
+
return result;
|
|
1678
|
+
} catch (error) {
|
|
1679
|
+
if (currentStructures && currentStructures.restoreStructures)
|
|
1680
|
+
restoreStructures();
|
|
1681
|
+
clearSource();
|
|
1682
|
+
if (error instanceof RangeError || error.message.startsWith("Unexpected end of buffer") || position > srcEnd) {
|
|
1683
|
+
error.incomplete = true;
|
|
1684
|
+
}
|
|
1685
|
+
throw error;
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
function restoreStructures() {
|
|
1689
|
+
for (let id in currentStructures.restoreStructures) {
|
|
1690
|
+
currentStructures[id] = currentStructures.restoreStructures[id];
|
|
1691
|
+
}
|
|
1692
|
+
currentStructures.restoreStructures = null;
|
|
1693
|
+
}
|
|
1694
|
+
function read() {
|
|
1695
|
+
let token = src[position++];
|
|
1696
|
+
if (token < 160) {
|
|
1697
|
+
if (token < 128) {
|
|
1698
|
+
if (token < 64)
|
|
1699
|
+
return token;
|
|
1700
|
+
else {
|
|
1701
|
+
let structure = currentStructures[token & 63] || currentUnpackr.getStructures && loadStructures()[token & 63];
|
|
1702
|
+
if (structure) {
|
|
1703
|
+
if (!structure.read) {
|
|
1704
|
+
structure.read = createStructureReader(structure, token & 63);
|
|
1705
|
+
}
|
|
1706
|
+
return structure.read();
|
|
1707
|
+
} else
|
|
1708
|
+
return token;
|
|
1709
|
+
}
|
|
1710
|
+
} else if (token < 144) {
|
|
1711
|
+
token -= 128;
|
|
1712
|
+
if (currentUnpackr.mapsAsObjects) {
|
|
1713
|
+
let object = {};
|
|
1714
|
+
for (let i = 0;i < token; i++) {
|
|
1715
|
+
let key = readKey();
|
|
1716
|
+
if (key === "__proto__")
|
|
1717
|
+
key = "__proto_";
|
|
1718
|
+
object[key] = read();
|
|
1719
|
+
}
|
|
1720
|
+
return object;
|
|
1721
|
+
} else {
|
|
1722
|
+
let map = new Map;
|
|
1723
|
+
for (let i = 0;i < token; i++) {
|
|
1724
|
+
map.set(read(), read());
|
|
1725
|
+
}
|
|
1726
|
+
return map;
|
|
1727
|
+
}
|
|
1728
|
+
} else {
|
|
1729
|
+
token -= 144;
|
|
1730
|
+
let array = new Array(token);
|
|
1731
|
+
for (let i = 0;i < token; i++) {
|
|
1732
|
+
array[i] = read();
|
|
1733
|
+
}
|
|
1734
|
+
if (currentUnpackr.freezeData)
|
|
1735
|
+
return Object.freeze(array);
|
|
1736
|
+
return array;
|
|
1737
|
+
}
|
|
1738
|
+
} else if (token < 192) {
|
|
1739
|
+
let length = token - 160;
|
|
1740
|
+
if (srcStringEnd >= position) {
|
|
1741
|
+
return srcString.slice(position - srcStringStart, (position += length) - srcStringStart);
|
|
1742
|
+
}
|
|
1743
|
+
if (srcStringEnd == 0 && srcEnd < 140) {
|
|
1744
|
+
let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
|
|
1745
|
+
if (string != null)
|
|
1746
|
+
return string;
|
|
1747
|
+
}
|
|
1748
|
+
return readFixedString(length);
|
|
1749
|
+
} else {
|
|
1750
|
+
let value;
|
|
1751
|
+
switch (token) {
|
|
1752
|
+
case 192:
|
|
1753
|
+
return null;
|
|
1754
|
+
case 193:
|
|
1755
|
+
if (bundledStrings) {
|
|
1756
|
+
value = read();
|
|
1757
|
+
if (value > 0)
|
|
1758
|
+
return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value);
|
|
1759
|
+
else
|
|
1760
|
+
return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 -= value);
|
|
1761
|
+
}
|
|
1762
|
+
return C1;
|
|
1763
|
+
case 194:
|
|
1764
|
+
return false;
|
|
1765
|
+
case 195:
|
|
1766
|
+
return true;
|
|
1767
|
+
case 196:
|
|
1768
|
+
value = src[position++];
|
|
1769
|
+
if (value === undefined)
|
|
1770
|
+
throw new Error("Unexpected end of buffer");
|
|
1771
|
+
return readBin(value);
|
|
1772
|
+
case 197:
|
|
1773
|
+
value = dataView.getUint16(position);
|
|
1774
|
+
position += 2;
|
|
1775
|
+
return readBin(value);
|
|
1776
|
+
case 198:
|
|
1777
|
+
value = dataView.getUint32(position);
|
|
1778
|
+
position += 4;
|
|
1779
|
+
return readBin(value);
|
|
1780
|
+
case 199:
|
|
1781
|
+
return readExt(src[position++]);
|
|
1782
|
+
case 200:
|
|
1783
|
+
value = dataView.getUint16(position);
|
|
1784
|
+
position += 2;
|
|
1785
|
+
return readExt(value);
|
|
1786
|
+
case 201:
|
|
1787
|
+
value = dataView.getUint32(position);
|
|
1788
|
+
position += 4;
|
|
1789
|
+
return readExt(value);
|
|
1790
|
+
case 202:
|
|
1791
|
+
value = dataView.getFloat32(position);
|
|
1792
|
+
if (currentUnpackr.useFloat32 > 2) {
|
|
1793
|
+
let multiplier = mult10[(src[position] & 127) << 1 | src[position + 1] >> 7];
|
|
1794
|
+
position += 4;
|
|
1795
|
+
return (multiplier * value + (value > 0 ? 0.5 : -0.5) >> 0) / multiplier;
|
|
1796
|
+
}
|
|
1797
|
+
position += 4;
|
|
1798
|
+
return value;
|
|
1799
|
+
case 203:
|
|
1800
|
+
value = dataView.getFloat64(position);
|
|
1801
|
+
position += 8;
|
|
1802
|
+
return value;
|
|
1803
|
+
case 204:
|
|
1804
|
+
return src[position++];
|
|
1805
|
+
case 205:
|
|
1806
|
+
value = dataView.getUint16(position);
|
|
1807
|
+
position += 2;
|
|
1808
|
+
return value;
|
|
1809
|
+
case 206:
|
|
1810
|
+
value = dataView.getUint32(position);
|
|
1811
|
+
position += 4;
|
|
1812
|
+
return value;
|
|
1813
|
+
case 207:
|
|
1814
|
+
if (currentUnpackr.int64AsType === "number") {
|
|
1815
|
+
value = dataView.getUint32(position) * 4294967296;
|
|
1816
|
+
value += dataView.getUint32(position + 4);
|
|
1817
|
+
} else if (currentUnpackr.int64AsType === "string") {
|
|
1818
|
+
value = dataView.getBigUint64(position).toString();
|
|
1819
|
+
} else if (currentUnpackr.int64AsType === "auto") {
|
|
1820
|
+
value = dataView.getBigUint64(position);
|
|
1821
|
+
if (value <= BigInt(2) << BigInt(52))
|
|
1822
|
+
value = Number(value);
|
|
1823
|
+
} else
|
|
1824
|
+
value = dataView.getBigUint64(position);
|
|
1825
|
+
position += 8;
|
|
1826
|
+
return value;
|
|
1827
|
+
case 208:
|
|
1828
|
+
return dataView.getInt8(position++);
|
|
1829
|
+
case 209:
|
|
1830
|
+
value = dataView.getInt16(position);
|
|
1831
|
+
position += 2;
|
|
1832
|
+
return value;
|
|
1833
|
+
case 210:
|
|
1834
|
+
value = dataView.getInt32(position);
|
|
1835
|
+
position += 4;
|
|
1836
|
+
return value;
|
|
1837
|
+
case 211:
|
|
1838
|
+
if (currentUnpackr.int64AsType === "number") {
|
|
1839
|
+
value = dataView.getInt32(position) * 4294967296;
|
|
1840
|
+
value += dataView.getUint32(position + 4);
|
|
1841
|
+
} else if (currentUnpackr.int64AsType === "string") {
|
|
1842
|
+
value = dataView.getBigInt64(position).toString();
|
|
1843
|
+
} else if (currentUnpackr.int64AsType === "auto") {
|
|
1844
|
+
value = dataView.getBigInt64(position);
|
|
1845
|
+
if (value >= BigInt(-2) << BigInt(52) && value <= BigInt(2) << BigInt(52))
|
|
1846
|
+
value = Number(value);
|
|
1847
|
+
} else
|
|
1848
|
+
value = dataView.getBigInt64(position);
|
|
1849
|
+
position += 8;
|
|
1850
|
+
return value;
|
|
1851
|
+
case 212:
|
|
1852
|
+
value = src[position++];
|
|
1853
|
+
if (value == 114) {
|
|
1854
|
+
return recordDefinition(src[position++] & 63);
|
|
1855
|
+
} else {
|
|
1856
|
+
let extension = currentExtensions[value];
|
|
1857
|
+
if (extension) {
|
|
1858
|
+
if (extension.read) {
|
|
1859
|
+
position++;
|
|
1860
|
+
return extension.read(read());
|
|
1861
|
+
} else if (extension.noBuffer) {
|
|
1862
|
+
position++;
|
|
1863
|
+
return extension();
|
|
1864
|
+
} else
|
|
1865
|
+
return extension(src.subarray(position, ++position));
|
|
1866
|
+
} else
|
|
1867
|
+
throw new Error("Unknown extension " + value);
|
|
1868
|
+
}
|
|
1869
|
+
case 213:
|
|
1870
|
+
value = src[position];
|
|
1871
|
+
if (value == 114) {
|
|
1872
|
+
position++;
|
|
1873
|
+
return recordDefinition(src[position++] & 63, src[position++]);
|
|
1874
|
+
} else
|
|
1875
|
+
return readExt(2);
|
|
1876
|
+
case 214:
|
|
1877
|
+
return readExt(4);
|
|
1878
|
+
case 215:
|
|
1879
|
+
return readExt(8);
|
|
1880
|
+
case 216:
|
|
1881
|
+
return readExt(16);
|
|
1882
|
+
case 217:
|
|
1883
|
+
value = src[position++];
|
|
1884
|
+
if (srcStringEnd >= position) {
|
|
1885
|
+
return srcString.slice(position - srcStringStart, (position += value) - srcStringStart);
|
|
1886
|
+
}
|
|
1887
|
+
return readString8(value);
|
|
1888
|
+
case 218:
|
|
1889
|
+
value = dataView.getUint16(position);
|
|
1890
|
+
position += 2;
|
|
1891
|
+
if (srcStringEnd >= position) {
|
|
1892
|
+
return srcString.slice(position - srcStringStart, (position += value) - srcStringStart);
|
|
1893
|
+
}
|
|
1894
|
+
return readString16(value);
|
|
1895
|
+
case 219:
|
|
1896
|
+
value = dataView.getUint32(position);
|
|
1897
|
+
position += 4;
|
|
1898
|
+
if (srcStringEnd >= position) {
|
|
1899
|
+
return srcString.slice(position - srcStringStart, (position += value) - srcStringStart);
|
|
1900
|
+
}
|
|
1901
|
+
return readString32(value);
|
|
1902
|
+
case 220:
|
|
1903
|
+
value = dataView.getUint16(position);
|
|
1904
|
+
position += 2;
|
|
1905
|
+
return readArray(value);
|
|
1906
|
+
case 221:
|
|
1907
|
+
value = dataView.getUint32(position);
|
|
1908
|
+
position += 4;
|
|
1909
|
+
return readArray(value);
|
|
1910
|
+
case 222:
|
|
1911
|
+
value = dataView.getUint16(position);
|
|
1912
|
+
position += 2;
|
|
1913
|
+
return readMap(value);
|
|
1914
|
+
case 223:
|
|
1915
|
+
value = dataView.getUint32(position);
|
|
1916
|
+
position += 4;
|
|
1917
|
+
return readMap(value);
|
|
1918
|
+
default:
|
|
1919
|
+
if (token >= 224)
|
|
1920
|
+
return token - 256;
|
|
1921
|
+
if (token === undefined) {
|
|
1922
|
+
let error = new Error("Unexpected end of MessagePack data");
|
|
1923
|
+
error.incomplete = true;
|
|
1924
|
+
throw error;
|
|
1925
|
+
}
|
|
1926
|
+
throw new Error("Unknown MessagePack token " + token);
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
var validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
|
|
1931
|
+
function createStructureReader(structure, firstId) {
|
|
1932
|
+
function readObject() {
|
|
1933
|
+
if (readObject.count++ > inlineObjectReadThreshold) {
|
|
1934
|
+
let readObject2 = structure.read = new Function("r", "return function(){return " + (currentUnpackr.freezeData ? "Object.freeze" : "") + "({" + structure.map((key) => key === "__proto__" ? "__proto_:r()" : validName.test(key) ? key + ":r()" : "[" + JSON.stringify(key) + "]:r()").join(",") + "})}")(read);
|
|
1935
|
+
if (structure.highByte === 0)
|
|
1936
|
+
structure.read = createSecondByteReader(firstId, structure.read);
|
|
1937
|
+
return readObject2();
|
|
1938
|
+
}
|
|
1939
|
+
let object = {};
|
|
1940
|
+
for (let i = 0, l = structure.length;i < l; i++) {
|
|
1941
|
+
let key = structure[i];
|
|
1942
|
+
if (key === "__proto__")
|
|
1943
|
+
key = "__proto_";
|
|
1944
|
+
object[key] = read();
|
|
1945
|
+
}
|
|
1946
|
+
if (currentUnpackr.freezeData)
|
|
1947
|
+
return Object.freeze(object);
|
|
1948
|
+
return object;
|
|
1949
|
+
}
|
|
1950
|
+
readObject.count = 0;
|
|
1951
|
+
if (structure.highByte === 0) {
|
|
1952
|
+
return createSecondByteReader(firstId, readObject);
|
|
1953
|
+
}
|
|
1954
|
+
return readObject;
|
|
1955
|
+
}
|
|
1956
|
+
var createSecondByteReader = (firstId, read0) => {
|
|
1957
|
+
return function() {
|
|
1958
|
+
let highByte = src[position++];
|
|
1959
|
+
if (highByte === 0)
|
|
1960
|
+
return read0();
|
|
1961
|
+
let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
|
|
1962
|
+
let structure = currentStructures[id] || loadStructures()[id];
|
|
1963
|
+
if (!structure) {
|
|
1964
|
+
throw new Error("Record id is not defined for " + id);
|
|
1965
|
+
}
|
|
1966
|
+
if (!structure.read)
|
|
1967
|
+
structure.read = createStructureReader(structure, firstId);
|
|
1968
|
+
return structure.read();
|
|
1969
|
+
};
|
|
1970
|
+
};
|
|
1971
|
+
function loadStructures() {
|
|
1972
|
+
let loadedStructures = saveState(() => {
|
|
1973
|
+
src = null;
|
|
1974
|
+
return currentUnpackr.getStructures();
|
|
1975
|
+
});
|
|
1976
|
+
return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures);
|
|
1977
|
+
}
|
|
1978
|
+
var readFixedString = readStringJS;
|
|
1979
|
+
var readString8 = readStringJS;
|
|
1980
|
+
var readString16 = readStringJS;
|
|
1981
|
+
var readString32 = readStringJS;
|
|
1982
|
+
function readStringJS(length) {
|
|
1983
|
+
let result;
|
|
1984
|
+
if (length < 16) {
|
|
1985
|
+
if (result = shortStringInJS(length))
|
|
1986
|
+
return result;
|
|
1987
|
+
}
|
|
1988
|
+
if (length > 64 && decoder)
|
|
1989
|
+
return decoder.decode(src.subarray(position, position += length));
|
|
1990
|
+
const end = position + length;
|
|
1991
|
+
const units = [];
|
|
1992
|
+
result = "";
|
|
1993
|
+
while (position < end) {
|
|
1994
|
+
const byte1 = src[position++];
|
|
1995
|
+
if ((byte1 & 128) === 0) {
|
|
1996
|
+
units.push(byte1);
|
|
1997
|
+
} else if ((byte1 & 224) === 192) {
|
|
1998
|
+
const byte2 = src[position++] & 63;
|
|
1999
|
+
units.push((byte1 & 31) << 6 | byte2);
|
|
2000
|
+
} else if ((byte1 & 240) === 224) {
|
|
2001
|
+
const byte2 = src[position++] & 63;
|
|
2002
|
+
const byte3 = src[position++] & 63;
|
|
2003
|
+
units.push((byte1 & 31) << 12 | byte2 << 6 | byte3);
|
|
2004
|
+
} else if ((byte1 & 248) === 240) {
|
|
2005
|
+
const byte2 = src[position++] & 63;
|
|
2006
|
+
const byte3 = src[position++] & 63;
|
|
2007
|
+
const byte4 = src[position++] & 63;
|
|
2008
|
+
let unit = (byte1 & 7) << 18 | byte2 << 12 | byte3 << 6 | byte4;
|
|
2009
|
+
if (unit > 65535) {
|
|
2010
|
+
unit -= 65536;
|
|
2011
|
+
units.push(unit >>> 10 & 1023 | 55296);
|
|
2012
|
+
unit = 56320 | unit & 1023;
|
|
2013
|
+
}
|
|
2014
|
+
units.push(unit);
|
|
2015
|
+
} else {
|
|
2016
|
+
units.push(byte1);
|
|
2017
|
+
}
|
|
2018
|
+
if (units.length >= 4096) {
|
|
2019
|
+
result += fromCharCode.apply(String, units);
|
|
2020
|
+
units.length = 0;
|
|
2021
|
+
}
|
|
2022
|
+
}
|
|
2023
|
+
if (units.length > 0) {
|
|
2024
|
+
result += fromCharCode.apply(String, units);
|
|
2025
|
+
}
|
|
2026
|
+
return result;
|
|
2027
|
+
}
|
|
2028
|
+
function readArray(length) {
|
|
2029
|
+
let array = new Array(length);
|
|
2030
|
+
for (let i = 0;i < length; i++) {
|
|
2031
|
+
array[i] = read();
|
|
2032
|
+
}
|
|
2033
|
+
if (currentUnpackr.freezeData)
|
|
2034
|
+
return Object.freeze(array);
|
|
2035
|
+
return array;
|
|
2036
|
+
}
|
|
2037
|
+
function readMap(length) {
|
|
2038
|
+
if (currentUnpackr.mapsAsObjects) {
|
|
2039
|
+
let object = {};
|
|
2040
|
+
for (let i = 0;i < length; i++) {
|
|
2041
|
+
let key = readKey();
|
|
2042
|
+
if (key === "__proto__")
|
|
2043
|
+
key = "__proto_";
|
|
2044
|
+
object[key] = read();
|
|
2045
|
+
}
|
|
2046
|
+
return object;
|
|
2047
|
+
} else {
|
|
2048
|
+
let map = new Map;
|
|
2049
|
+
for (let i = 0;i < length; i++) {
|
|
2050
|
+
map.set(read(), read());
|
|
2051
|
+
}
|
|
2052
|
+
return map;
|
|
2053
|
+
}
|
|
2054
|
+
}
|
|
2055
|
+
var fromCharCode = String.fromCharCode;
|
|
2056
|
+
function longStringInJS(length) {
|
|
2057
|
+
let start = position;
|
|
2058
|
+
let bytes = new Array(length);
|
|
2059
|
+
for (let i = 0;i < length; i++) {
|
|
2060
|
+
const byte = src[position++];
|
|
2061
|
+
if ((byte & 128) > 0) {
|
|
2062
|
+
position = start;
|
|
2063
|
+
return;
|
|
2064
|
+
}
|
|
2065
|
+
bytes[i] = byte;
|
|
2066
|
+
}
|
|
2067
|
+
return fromCharCode.apply(String, bytes);
|
|
2068
|
+
}
|
|
2069
|
+
function shortStringInJS(length) {
|
|
2070
|
+
if (length < 4) {
|
|
2071
|
+
if (length < 2) {
|
|
2072
|
+
if (length === 0)
|
|
2073
|
+
return "";
|
|
2074
|
+
else {
|
|
2075
|
+
let a = src[position++];
|
|
2076
|
+
if ((a & 128) > 1) {
|
|
2077
|
+
position -= 1;
|
|
2078
|
+
return;
|
|
2079
|
+
}
|
|
2080
|
+
return fromCharCode(a);
|
|
2081
|
+
}
|
|
2082
|
+
} else {
|
|
2083
|
+
let a = src[position++];
|
|
2084
|
+
let b = src[position++];
|
|
2085
|
+
if ((a & 128) > 0 || (b & 128) > 0) {
|
|
2086
|
+
position -= 2;
|
|
2087
|
+
return;
|
|
2088
|
+
}
|
|
2089
|
+
if (length < 3)
|
|
2090
|
+
return fromCharCode(a, b);
|
|
2091
|
+
let c = src[position++];
|
|
2092
|
+
if ((c & 128) > 0) {
|
|
2093
|
+
position -= 3;
|
|
2094
|
+
return;
|
|
2095
|
+
}
|
|
2096
|
+
return fromCharCode(a, b, c);
|
|
2097
|
+
}
|
|
2098
|
+
} else {
|
|
2099
|
+
let a = src[position++];
|
|
2100
|
+
let b = src[position++];
|
|
2101
|
+
let c = src[position++];
|
|
2102
|
+
let d = src[position++];
|
|
2103
|
+
if ((a & 128) > 0 || (b & 128) > 0 || (c & 128) > 0 || (d & 128) > 0) {
|
|
2104
|
+
position -= 4;
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
if (length < 6) {
|
|
2108
|
+
if (length === 4)
|
|
2109
|
+
return fromCharCode(a, b, c, d);
|
|
2110
|
+
else {
|
|
2111
|
+
let e = src[position++];
|
|
2112
|
+
if ((e & 128) > 0) {
|
|
2113
|
+
position -= 5;
|
|
2114
|
+
return;
|
|
2115
|
+
}
|
|
2116
|
+
return fromCharCode(a, b, c, d, e);
|
|
2117
|
+
}
|
|
2118
|
+
} else if (length < 8) {
|
|
2119
|
+
let e = src[position++];
|
|
2120
|
+
let f = src[position++];
|
|
2121
|
+
if ((e & 128) > 0 || (f & 128) > 0) {
|
|
2122
|
+
position -= 6;
|
|
2123
|
+
return;
|
|
2124
|
+
}
|
|
2125
|
+
if (length < 7)
|
|
2126
|
+
return fromCharCode(a, b, c, d, e, f);
|
|
2127
|
+
let g = src[position++];
|
|
2128
|
+
if ((g & 128) > 0) {
|
|
2129
|
+
position -= 7;
|
|
2130
|
+
return;
|
|
2131
|
+
}
|
|
2132
|
+
return fromCharCode(a, b, c, d, e, f, g);
|
|
2133
|
+
} else {
|
|
2134
|
+
let e = src[position++];
|
|
2135
|
+
let f = src[position++];
|
|
2136
|
+
let g = src[position++];
|
|
2137
|
+
let h = src[position++];
|
|
2138
|
+
if ((e & 128) > 0 || (f & 128) > 0 || (g & 128) > 0 || (h & 128) > 0) {
|
|
2139
|
+
position -= 8;
|
|
2140
|
+
return;
|
|
2141
|
+
}
|
|
2142
|
+
if (length < 10) {
|
|
2143
|
+
if (length === 8)
|
|
2144
|
+
return fromCharCode(a, b, c, d, e, f, g, h);
|
|
2145
|
+
else {
|
|
2146
|
+
let i = src[position++];
|
|
2147
|
+
if ((i & 128) > 0) {
|
|
2148
|
+
position -= 9;
|
|
2149
|
+
return;
|
|
2150
|
+
}
|
|
2151
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i);
|
|
2152
|
+
}
|
|
2153
|
+
} else if (length < 12) {
|
|
2154
|
+
let i = src[position++];
|
|
2155
|
+
let j = src[position++];
|
|
2156
|
+
if ((i & 128) > 0 || (j & 128) > 0) {
|
|
2157
|
+
position -= 10;
|
|
2158
|
+
return;
|
|
2159
|
+
}
|
|
2160
|
+
if (length < 11)
|
|
2161
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j);
|
|
2162
|
+
let k = src[position++];
|
|
2163
|
+
if ((k & 128) > 0) {
|
|
2164
|
+
position -= 11;
|
|
2165
|
+
return;
|
|
2166
|
+
}
|
|
2167
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
|
|
2168
|
+
} else {
|
|
2169
|
+
let i = src[position++];
|
|
2170
|
+
let j = src[position++];
|
|
2171
|
+
let k = src[position++];
|
|
2172
|
+
let l = src[position++];
|
|
2173
|
+
if ((i & 128) > 0 || (j & 128) > 0 || (k & 128) > 0 || (l & 128) > 0) {
|
|
2174
|
+
position -= 12;
|
|
2175
|
+
return;
|
|
2176
|
+
}
|
|
2177
|
+
if (length < 14) {
|
|
2178
|
+
if (length === 12)
|
|
2179
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
|
|
2180
|
+
else {
|
|
2181
|
+
let m = src[position++];
|
|
2182
|
+
if ((m & 128) > 0) {
|
|
2183
|
+
position -= 13;
|
|
2184
|
+
return;
|
|
2185
|
+
}
|
|
2186
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
|
|
2187
|
+
}
|
|
2188
|
+
} else {
|
|
2189
|
+
let m = src[position++];
|
|
2190
|
+
let n = src[position++];
|
|
2191
|
+
if ((m & 128) > 0 || (n & 128) > 0) {
|
|
2192
|
+
position -= 14;
|
|
2193
|
+
return;
|
|
2194
|
+
}
|
|
2195
|
+
if (length < 15)
|
|
2196
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
|
|
2197
|
+
let o = src[position++];
|
|
2198
|
+
if ((o & 128) > 0) {
|
|
2199
|
+
position -= 15;
|
|
2200
|
+
return;
|
|
2201
|
+
}
|
|
2202
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
|
|
2203
|
+
}
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
}
|
|
2207
|
+
}
|
|
2208
|
+
function readOnlyJSString() {
|
|
2209
|
+
let token = src[position++];
|
|
2210
|
+
let length;
|
|
2211
|
+
if (token < 192) {
|
|
2212
|
+
length = token - 160;
|
|
2213
|
+
} else {
|
|
2214
|
+
switch (token) {
|
|
2215
|
+
case 217:
|
|
2216
|
+
length = src[position++];
|
|
2217
|
+
break;
|
|
2218
|
+
case 218:
|
|
2219
|
+
length = dataView.getUint16(position);
|
|
2220
|
+
position += 2;
|
|
2221
|
+
break;
|
|
2222
|
+
case 219:
|
|
2223
|
+
length = dataView.getUint32(position);
|
|
2224
|
+
position += 4;
|
|
2225
|
+
break;
|
|
2226
|
+
default:
|
|
2227
|
+
throw new Error("Expected string");
|
|
2228
|
+
}
|
|
2229
|
+
}
|
|
2230
|
+
return readStringJS(length);
|
|
2231
|
+
}
|
|
2232
|
+
function readBin(length) {
|
|
2233
|
+
return currentUnpackr.copyBuffers ? Uint8Array.prototype.slice.call(src, position, position += length) : src.subarray(position, position += length);
|
|
2234
|
+
}
|
|
2235
|
+
function readExt(length) {
|
|
2236
|
+
let type = src[position++];
|
|
2237
|
+
if (currentExtensions[type]) {
|
|
2238
|
+
let end;
|
|
2239
|
+
return currentExtensions[type](src.subarray(position, end = position += length), (readPosition) => {
|
|
2240
|
+
position = readPosition;
|
|
2241
|
+
try {
|
|
2242
|
+
return read();
|
|
2243
|
+
} finally {
|
|
2244
|
+
position = end;
|
|
2245
|
+
}
|
|
2246
|
+
});
|
|
2247
|
+
} else
|
|
2248
|
+
throw new Error("Unknown extension type " + type);
|
|
2249
|
+
}
|
|
2250
|
+
var keyCache = new Array(4096);
|
|
2251
|
+
function readKey() {
|
|
2252
|
+
let length = src[position++];
|
|
2253
|
+
if (length >= 160 && length < 192) {
|
|
2254
|
+
length = length - 160;
|
|
2255
|
+
if (srcStringEnd >= position)
|
|
2256
|
+
return srcString.slice(position - srcStringStart, (position += length) - srcStringStart);
|
|
2257
|
+
else if (!(srcStringEnd == 0 && srcEnd < 180))
|
|
2258
|
+
return readFixedString(length);
|
|
2259
|
+
} else {
|
|
2260
|
+
position--;
|
|
2261
|
+
return asSafeString(read());
|
|
2262
|
+
}
|
|
2263
|
+
let key = (length << 5 ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 4095;
|
|
2264
|
+
let entry = keyCache[key];
|
|
2265
|
+
let checkPosition = position;
|
|
2266
|
+
let end = position + length - 3;
|
|
2267
|
+
let chunk;
|
|
2268
|
+
let i = 0;
|
|
2269
|
+
if (entry && entry.bytes == length) {
|
|
2270
|
+
while (checkPosition < end) {
|
|
2271
|
+
chunk = dataView.getUint32(checkPosition);
|
|
2272
|
+
if (chunk != entry[i++]) {
|
|
2273
|
+
checkPosition = 1879048192;
|
|
2274
|
+
break;
|
|
2275
|
+
}
|
|
2276
|
+
checkPosition += 4;
|
|
2277
|
+
}
|
|
2278
|
+
end += 3;
|
|
2279
|
+
while (checkPosition < end) {
|
|
2280
|
+
chunk = src[checkPosition++];
|
|
2281
|
+
if (chunk != entry[i++]) {
|
|
2282
|
+
checkPosition = 1879048192;
|
|
2283
|
+
break;
|
|
2284
|
+
}
|
|
2285
|
+
}
|
|
2286
|
+
if (checkPosition === end) {
|
|
2287
|
+
position = checkPosition;
|
|
2288
|
+
return entry.string;
|
|
2289
|
+
}
|
|
2290
|
+
end -= 3;
|
|
2291
|
+
checkPosition = position;
|
|
2292
|
+
}
|
|
2293
|
+
entry = [];
|
|
2294
|
+
keyCache[key] = entry;
|
|
2295
|
+
entry.bytes = length;
|
|
2296
|
+
while (checkPosition < end) {
|
|
2297
|
+
chunk = dataView.getUint32(checkPosition);
|
|
2298
|
+
entry.push(chunk);
|
|
2299
|
+
checkPosition += 4;
|
|
2300
|
+
}
|
|
2301
|
+
end += 3;
|
|
2302
|
+
while (checkPosition < end) {
|
|
2303
|
+
chunk = src[checkPosition++];
|
|
2304
|
+
entry.push(chunk);
|
|
2305
|
+
}
|
|
2306
|
+
let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
|
|
2307
|
+
if (string != null)
|
|
2308
|
+
return entry.string = string;
|
|
2309
|
+
return entry.string = readFixedString(length);
|
|
2310
|
+
}
|
|
2311
|
+
function asSafeString(property) {
|
|
2312
|
+
if (typeof property === "string")
|
|
2313
|
+
return property;
|
|
2314
|
+
if (typeof property === "number" || typeof property === "boolean" || typeof property === "bigint")
|
|
2315
|
+
return property.toString();
|
|
2316
|
+
if (property == null)
|
|
2317
|
+
return property + "";
|
|
2318
|
+
if (currentUnpackr.allowArraysInMapKeys && Array.isArray(property) && property.flat().every((item) => ["string", "number", "boolean", "bigint"].includes(typeof item))) {
|
|
2319
|
+
return property.flat().toString();
|
|
2320
|
+
}
|
|
2321
|
+
throw new Error(`Invalid property type for record: ${typeof property}`);
|
|
2322
|
+
}
|
|
2323
|
+
var recordDefinition = (id, highByte) => {
|
|
2324
|
+
let structure = read().map(asSafeString);
|
|
2325
|
+
let firstByte = id;
|
|
2326
|
+
if (highByte !== undefined) {
|
|
2327
|
+
id = id < 32 ? -((highByte << 5) + id) : (highByte << 5) + id;
|
|
2328
|
+
structure.highByte = highByte;
|
|
2329
|
+
}
|
|
2330
|
+
let existingStructure = currentStructures[id];
|
|
2331
|
+
if (existingStructure && (existingStructure.isShared || sequentialMode)) {
|
|
2332
|
+
(currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
|
|
2333
|
+
}
|
|
2334
|
+
currentStructures[id] = structure;
|
|
2335
|
+
structure.read = createStructureReader(structure, firstByte);
|
|
2336
|
+
return structure.read();
|
|
2337
|
+
};
|
|
2338
|
+
currentExtensions[0] = () => {};
|
|
2339
|
+
currentExtensions[0].noBuffer = true;
|
|
2340
|
+
currentExtensions[66] = (data) => {
|
|
2341
|
+
let headLength = data.byteLength % 8 || 8;
|
|
2342
|
+
let head = BigInt(data[0] & 128 ? data[0] - 256 : data[0]);
|
|
2343
|
+
for (let i = 1;i < headLength; i++) {
|
|
2344
|
+
head <<= BigInt(8);
|
|
2345
|
+
head += BigInt(data[i]);
|
|
2346
|
+
}
|
|
2347
|
+
if (data.byteLength !== headLength) {
|
|
2348
|
+
let view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
2349
|
+
let decode = (start, end) => {
|
|
2350
|
+
let length = end - start;
|
|
2351
|
+
if (length <= 40) {
|
|
2352
|
+
let out = view.getBigUint64(start);
|
|
2353
|
+
for (let i = start + 8;i < end; i += 8) {
|
|
2354
|
+
out <<= BigInt(64n);
|
|
2355
|
+
out |= view.getBigUint64(i);
|
|
2356
|
+
}
|
|
2357
|
+
return out;
|
|
2358
|
+
}
|
|
2359
|
+
let middle = start + (length >> 4 << 3);
|
|
2360
|
+
let left = decode(start, middle);
|
|
2361
|
+
let right = decode(middle, end);
|
|
2362
|
+
return left << BigInt((end - middle) * 8) | right;
|
|
2363
|
+
};
|
|
2364
|
+
head = head << BigInt((view.byteLength - headLength) * 8) | decode(headLength, view.byteLength);
|
|
2365
|
+
}
|
|
2366
|
+
return head;
|
|
2367
|
+
};
|
|
2368
|
+
var errors = {
|
|
2369
|
+
Error,
|
|
2370
|
+
EvalError,
|
|
2371
|
+
RangeError,
|
|
2372
|
+
ReferenceError,
|
|
2373
|
+
SyntaxError,
|
|
2374
|
+
TypeError,
|
|
2375
|
+
URIError,
|
|
2376
|
+
AggregateError: typeof AggregateError === "function" ? AggregateError : null
|
|
2377
|
+
};
|
|
2378
|
+
currentExtensions[101] = () => {
|
|
2379
|
+
let data = read();
|
|
2380
|
+
if (!errors[data[0]]) {
|
|
2381
|
+
let error = Error(data[1], { cause: data[2] });
|
|
2382
|
+
error.name = data[0];
|
|
2383
|
+
return error;
|
|
2384
|
+
}
|
|
2385
|
+
return errors[data[0]](data[1], { cause: data[2] });
|
|
2386
|
+
};
|
|
2387
|
+
currentExtensions[105] = (data) => {
|
|
2388
|
+
if (currentUnpackr.structuredClone === false)
|
|
2389
|
+
throw new Error("Structured clone extension is disabled");
|
|
2390
|
+
let id = dataView.getUint32(position - 4);
|
|
2391
|
+
if (!referenceMap)
|
|
2392
|
+
referenceMap = new Map;
|
|
2393
|
+
let token = src[position];
|
|
2394
|
+
let target;
|
|
2395
|
+
if (token >= 144 && token < 160 || token == 220 || token == 221)
|
|
2396
|
+
target = [];
|
|
2397
|
+
else if (token >= 128 && token < 144 || token == 222 || token == 223)
|
|
2398
|
+
target = new Map;
|
|
2399
|
+
else if ((token >= 199 && token <= 201 || token >= 212 && token <= 216) && src[position + 1] === 115)
|
|
2400
|
+
target = new Set;
|
|
2401
|
+
else
|
|
2402
|
+
target = {};
|
|
2403
|
+
let refEntry = { target };
|
|
2404
|
+
referenceMap.set(id, refEntry);
|
|
2405
|
+
let targetProperties = read();
|
|
2406
|
+
if (!refEntry.used) {
|
|
2407
|
+
return refEntry.target = targetProperties;
|
|
2408
|
+
} else {
|
|
2409
|
+
Object.assign(target, targetProperties);
|
|
2410
|
+
}
|
|
2411
|
+
if (target instanceof Map)
|
|
2412
|
+
for (let [k, v] of targetProperties.entries())
|
|
2413
|
+
target.set(k, v);
|
|
2414
|
+
if (target instanceof Set)
|
|
2415
|
+
for (let i of Array.from(targetProperties))
|
|
2416
|
+
target.add(i);
|
|
2417
|
+
return target;
|
|
2418
|
+
};
|
|
2419
|
+
currentExtensions[112] = (data) => {
|
|
2420
|
+
if (currentUnpackr.structuredClone === false)
|
|
2421
|
+
throw new Error("Structured clone extension is disabled");
|
|
2422
|
+
let id = dataView.getUint32(position - 4);
|
|
2423
|
+
let refEntry = referenceMap.get(id);
|
|
2424
|
+
refEntry.used = true;
|
|
2425
|
+
return refEntry.target;
|
|
1319
2426
|
};
|
|
1320
|
-
|
|
2427
|
+
currentExtensions[115] = () => new Set(read());
|
|
2428
|
+
var typedArrays = ["Int8", "Uint8", "Uint8Clamped", "Int16", "Uint16", "Int32", "Uint32", "Float32", "Float64", "BigInt64", "BigUint64"].map((type) => type + "Array");
|
|
2429
|
+
var glbl = typeof globalThis === "object" ? globalThis : window;
|
|
2430
|
+
currentExtensions[116] = (data) => {
|
|
2431
|
+
let typeCode = data[0];
|
|
2432
|
+
let buffer = Uint8Array.prototype.slice.call(data, 1).buffer;
|
|
2433
|
+
let typedArrayName = typedArrays[typeCode];
|
|
2434
|
+
if (!typedArrayName) {
|
|
2435
|
+
if (typeCode === 16)
|
|
2436
|
+
return buffer;
|
|
2437
|
+
if (typeCode === 17)
|
|
2438
|
+
return new DataView(buffer);
|
|
2439
|
+
throw new Error("Could not find typed array for code " + typeCode);
|
|
2440
|
+
}
|
|
2441
|
+
return new glbl[typedArrayName](buffer);
|
|
2442
|
+
};
|
|
2443
|
+
currentExtensions[120] = () => {
|
|
2444
|
+
let data = read();
|
|
2445
|
+
return new RegExp(data[0], data[1]);
|
|
2446
|
+
};
|
|
2447
|
+
var TEMP_BUNDLE = [];
|
|
2448
|
+
currentExtensions[98] = (data) => {
|
|
2449
|
+
let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
|
|
2450
|
+
let dataPosition = position;
|
|
2451
|
+
position += dataSize - data.length;
|
|
2452
|
+
bundledStrings = TEMP_BUNDLE;
|
|
2453
|
+
bundledStrings = [readOnlyJSString(), readOnlyJSString()];
|
|
2454
|
+
bundledStrings.position0 = 0;
|
|
2455
|
+
bundledStrings.position1 = 0;
|
|
2456
|
+
bundledStrings.postBundlePosition = position;
|
|
2457
|
+
position = dataPosition;
|
|
2458
|
+
return read();
|
|
2459
|
+
};
|
|
2460
|
+
currentExtensions[255] = (data) => {
|
|
2461
|
+
if (data.length == 4)
|
|
2462
|
+
return new Date((data[0] * 16777216 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000);
|
|
2463
|
+
else if (data.length == 8)
|
|
2464
|
+
return new Date(((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1e6 + ((data[3] & 3) * 4294967296 + data[4] * 16777216 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000);
|
|
2465
|
+
else if (data.length == 12)
|
|
2466
|
+
return new Date(((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1e6 + ((data[4] & 128 ? -281474976710656 : 0) + data[6] * 1099511627776 + data[7] * 4294967296 + data[8] * 16777216 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000);
|
|
2467
|
+
else
|
|
2468
|
+
return new Date("invalid");
|
|
2469
|
+
};
|
|
2470
|
+
function saveState(callback) {
|
|
2471
|
+
if (onSaveState)
|
|
2472
|
+
onSaveState();
|
|
2473
|
+
let savedSrcEnd = srcEnd;
|
|
2474
|
+
let savedPosition = position;
|
|
2475
|
+
let savedStringPosition = stringPosition;
|
|
2476
|
+
let savedSrcStringStart = srcStringStart;
|
|
2477
|
+
let savedSrcStringEnd = srcStringEnd;
|
|
2478
|
+
let savedSrcString = srcString;
|
|
2479
|
+
let savedStrings = strings;
|
|
2480
|
+
let savedReferenceMap = referenceMap;
|
|
2481
|
+
let savedBundledStrings = bundledStrings;
|
|
2482
|
+
let savedSrc = new Uint8Array(src.slice(0, srcEnd));
|
|
2483
|
+
let savedStructures = currentStructures;
|
|
2484
|
+
let savedStructuresContents = currentStructures.slice(0, currentStructures.length);
|
|
2485
|
+
let savedPackr = currentUnpackr;
|
|
2486
|
+
let savedSequentialMode = sequentialMode;
|
|
2487
|
+
let value = callback();
|
|
2488
|
+
srcEnd = savedSrcEnd;
|
|
2489
|
+
position = savedPosition;
|
|
2490
|
+
stringPosition = savedStringPosition;
|
|
2491
|
+
srcStringStart = savedSrcStringStart;
|
|
2492
|
+
srcStringEnd = savedSrcStringEnd;
|
|
2493
|
+
srcString = savedSrcString;
|
|
2494
|
+
strings = savedStrings;
|
|
2495
|
+
referenceMap = savedReferenceMap;
|
|
2496
|
+
bundledStrings = savedBundledStrings;
|
|
2497
|
+
src = savedSrc;
|
|
2498
|
+
sequentialMode = savedSequentialMode;
|
|
2499
|
+
currentStructures = savedStructures;
|
|
2500
|
+
currentStructures.splice(0, currentStructures.length, ...savedStructuresContents);
|
|
2501
|
+
currentUnpackr = savedPackr;
|
|
2502
|
+
dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
|
|
2503
|
+
return value;
|
|
2504
|
+
}
|
|
2505
|
+
function clearSource() {
|
|
2506
|
+
src = null;
|
|
2507
|
+
referenceMap = null;
|
|
2508
|
+
currentStructures = null;
|
|
2509
|
+
}
|
|
2510
|
+
var mult10 = new Array(147);
|
|
2511
|
+
for (let i = 0;i < 256; i++) {
|
|
2512
|
+
mult10[i] = +("1e" + Math.floor(45.15 - i * 0.30103));
|
|
2513
|
+
}
|
|
2514
|
+
var defaultUnpackr = new Unpackr({ useRecords: false });
|
|
2515
|
+
var unpack = defaultUnpackr.unpack;
|
|
2516
|
+
var unpackMultiple = defaultUnpackr.unpackMultiple;
|
|
2517
|
+
var decode = defaultUnpackr.unpack;
|
|
2518
|
+
var f32Array = new Float32Array(1);
|
|
2519
|
+
var u8Array = new Uint8Array(f32Array.buffer, 0, 4);
|
|
2520
|
+
// node_modules/msgpackr/pack.js
|
|
2521
|
+
var textEncoder;
|
|
2522
|
+
try {
|
|
2523
|
+
textEncoder = new TextEncoder;
|
|
2524
|
+
} catch (error) {}
|
|
2525
|
+
var extensions;
|
|
2526
|
+
var extensionClasses;
|
|
2527
|
+
var hasNodeBuffer = typeof Buffer !== "undefined";
|
|
2528
|
+
var ByteArrayAllocate = hasNodeBuffer ? function(length) {
|
|
2529
|
+
return Buffer.allocUnsafeSlow(length);
|
|
2530
|
+
} : Uint8Array;
|
|
2531
|
+
var ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
|
|
2532
|
+
var MAX_BUFFER_SIZE = hasNodeBuffer ? 4294967296 : 2144337920;
|
|
2533
|
+
var target;
|
|
2534
|
+
var keysTarget;
|
|
2535
|
+
var targetView;
|
|
2536
|
+
var position2 = 0;
|
|
2537
|
+
var safeEnd;
|
|
2538
|
+
var bundledStrings2 = null;
|
|
2539
|
+
var writeStructSlots;
|
|
2540
|
+
var MAX_BUNDLE_SIZE = 21760;
|
|
2541
|
+
var hasNonLatin = /[\u0080-\uFFFF]/;
|
|
2542
|
+
var RECORD_SYMBOL = Symbol("record-id");
|
|
2543
|
+
|
|
2544
|
+
class Packr extends Unpackr {
|
|
2545
|
+
constructor(options) {
|
|
2546
|
+
super(options);
|
|
2547
|
+
this.offset = 0;
|
|
2548
|
+
let typeBuffer;
|
|
2549
|
+
let start;
|
|
2550
|
+
let hasSharedUpdate;
|
|
2551
|
+
let structures;
|
|
2552
|
+
let referenceMap2;
|
|
2553
|
+
let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position3) {
|
|
2554
|
+
return target.utf8Write(string, position3, target.byteLength - position3);
|
|
2555
|
+
} : textEncoder && textEncoder.encodeInto ? function(string, position3) {
|
|
2556
|
+
return textEncoder.encodeInto(string, target.subarray(position3)).written;
|
|
2557
|
+
} : false;
|
|
2558
|
+
let packr = this;
|
|
2559
|
+
if (!options)
|
|
2560
|
+
options = {};
|
|
2561
|
+
let isSequential = options && options.sequential;
|
|
2562
|
+
let hasSharedStructures = options.structures || options.saveStructures;
|
|
2563
|
+
let maxSharedStructures = options.maxSharedStructures;
|
|
2564
|
+
if (maxSharedStructures == null)
|
|
2565
|
+
maxSharedStructures = hasSharedStructures ? 32 : 0;
|
|
2566
|
+
if (maxSharedStructures > 8160)
|
|
2567
|
+
throw new Error("Maximum maxSharedStructure is 8160");
|
|
2568
|
+
if (options.structuredClone && options.moreTypes == undefined) {
|
|
2569
|
+
this.moreTypes = true;
|
|
2570
|
+
}
|
|
2571
|
+
let maxOwnStructures = options.maxOwnStructures;
|
|
2572
|
+
if (maxOwnStructures == null)
|
|
2573
|
+
maxOwnStructures = hasSharedStructures ? 32 : 64;
|
|
2574
|
+
if (!this.structures && options.useRecords != false)
|
|
2575
|
+
this.structures = [];
|
|
2576
|
+
let useTwoByteRecords = maxSharedStructures > 32 || maxOwnStructures + maxSharedStructures > 64;
|
|
2577
|
+
let sharedLimitId = maxSharedStructures + 64;
|
|
2578
|
+
let maxStructureId = maxSharedStructures + maxOwnStructures + 64;
|
|
2579
|
+
if (maxStructureId > 8256) {
|
|
2580
|
+
throw new Error("Maximum maxSharedStructure + maxOwnStructure is 8192");
|
|
2581
|
+
}
|
|
2582
|
+
let recordIdsToRemove = [];
|
|
2583
|
+
let transitionsCount = 0;
|
|
2584
|
+
let serializationsSinceTransitionRebuild = 0;
|
|
2585
|
+
this.pack = this.encode = function(value, encodeOptions) {
|
|
2586
|
+
if (!target) {
|
|
2587
|
+
target = new ByteArrayAllocate(8192);
|
|
2588
|
+
targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, 8192));
|
|
2589
|
+
position2 = 0;
|
|
2590
|
+
}
|
|
2591
|
+
safeEnd = target.length - 10;
|
|
2592
|
+
if (safeEnd - position2 < 2048) {
|
|
2593
|
+
target = new ByteArrayAllocate(target.length);
|
|
2594
|
+
targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length));
|
|
2595
|
+
safeEnd = target.length - 10;
|
|
2596
|
+
position2 = 0;
|
|
2597
|
+
} else
|
|
2598
|
+
position2 = position2 + 7 & 2147483640;
|
|
2599
|
+
start = position2;
|
|
2600
|
+
if (encodeOptions & RESERVE_START_SPACE)
|
|
2601
|
+
position2 += encodeOptions & 255;
|
|
2602
|
+
referenceMap2 = packr.structuredClone ? new Map : null;
|
|
2603
|
+
if (packr.bundleStrings && typeof value !== "string") {
|
|
2604
|
+
bundledStrings2 = [];
|
|
2605
|
+
bundledStrings2.size = Infinity;
|
|
2606
|
+
} else
|
|
2607
|
+
bundledStrings2 = null;
|
|
2608
|
+
structures = packr.structures;
|
|
2609
|
+
if (structures) {
|
|
2610
|
+
if (structures.uninitialized)
|
|
2611
|
+
structures = packr._mergeStructures(packr.getStructures());
|
|
2612
|
+
let sharedLength = structures.sharedLength || 0;
|
|
2613
|
+
if (sharedLength > maxSharedStructures) {
|
|
2614
|
+
throw new Error("Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to " + structures.sharedLength);
|
|
2615
|
+
}
|
|
2616
|
+
if (!structures.transitions) {
|
|
2617
|
+
structures.transitions = Object.create(null);
|
|
2618
|
+
for (let i = 0;i < sharedLength; i++) {
|
|
2619
|
+
let keys = structures[i];
|
|
2620
|
+
if (!keys)
|
|
2621
|
+
continue;
|
|
2622
|
+
let nextTransition, transition = structures.transitions;
|
|
2623
|
+
for (let j = 0, l = keys.length;j < l; j++) {
|
|
2624
|
+
let key = keys[j];
|
|
2625
|
+
nextTransition = transition[key];
|
|
2626
|
+
if (!nextTransition) {
|
|
2627
|
+
nextTransition = transition[key] = Object.create(null);
|
|
2628
|
+
}
|
|
2629
|
+
transition = nextTransition;
|
|
2630
|
+
}
|
|
2631
|
+
transition[RECORD_SYMBOL] = i + 64;
|
|
2632
|
+
}
|
|
2633
|
+
this.lastNamedStructuresLength = sharedLength;
|
|
2634
|
+
}
|
|
2635
|
+
if (!isSequential) {
|
|
2636
|
+
structures.nextId = sharedLength + 64;
|
|
2637
|
+
}
|
|
2638
|
+
}
|
|
2639
|
+
if (hasSharedUpdate)
|
|
2640
|
+
hasSharedUpdate = false;
|
|
2641
|
+
let encodingError;
|
|
2642
|
+
try {
|
|
2643
|
+
if (packr.randomAccessStructure && value && typeof value === "object") {
|
|
2644
|
+
if (value.constructor === Object)
|
|
2645
|
+
writeStruct(value);
|
|
2646
|
+
else if (value.constructor !== Map && !Array.isArray(value) && !extensionClasses.some((extClass) => value instanceof extClass)) {
|
|
2647
|
+
writeStruct(value.toJSON ? value.toJSON() : value);
|
|
2648
|
+
} else
|
|
2649
|
+
pack(value);
|
|
2650
|
+
} else
|
|
2651
|
+
pack(value);
|
|
2652
|
+
let lastBundle = bundledStrings2;
|
|
2653
|
+
if (bundledStrings2)
|
|
2654
|
+
writeBundles(start, pack, 0);
|
|
2655
|
+
if (referenceMap2 && referenceMap2.idsToInsert) {
|
|
2656
|
+
let idsToInsert = referenceMap2.idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
|
|
2657
|
+
let i = idsToInsert.length;
|
|
2658
|
+
let incrementPosition = -1;
|
|
2659
|
+
while (lastBundle && i > 0) {
|
|
2660
|
+
let insertionPoint = idsToInsert[--i].offset + start;
|
|
2661
|
+
if (insertionPoint < lastBundle.stringsPosition + start && incrementPosition === -1)
|
|
2662
|
+
incrementPosition = 0;
|
|
2663
|
+
if (insertionPoint > lastBundle.position + start) {
|
|
2664
|
+
if (incrementPosition >= 0)
|
|
2665
|
+
incrementPosition += 6;
|
|
2666
|
+
} else {
|
|
2667
|
+
if (incrementPosition >= 0) {
|
|
2668
|
+
targetView.setUint32(lastBundle.position + start, targetView.getUint32(lastBundle.position + start) + incrementPosition);
|
|
2669
|
+
incrementPosition = -1;
|
|
2670
|
+
}
|
|
2671
|
+
lastBundle = lastBundle.previous;
|
|
2672
|
+
i++;
|
|
2673
|
+
}
|
|
2674
|
+
}
|
|
2675
|
+
if (incrementPosition >= 0 && lastBundle) {
|
|
2676
|
+
targetView.setUint32(lastBundle.position + start, targetView.getUint32(lastBundle.position + start) + incrementPosition);
|
|
2677
|
+
}
|
|
2678
|
+
position2 += idsToInsert.length * 6;
|
|
2679
|
+
if (position2 > safeEnd)
|
|
2680
|
+
makeRoom(position2);
|
|
2681
|
+
packr.offset = position2;
|
|
2682
|
+
let serialized = insertIds(target.subarray(start, position2), idsToInsert);
|
|
2683
|
+
referenceMap2 = null;
|
|
2684
|
+
return serialized;
|
|
2685
|
+
}
|
|
2686
|
+
packr.offset = position2;
|
|
2687
|
+
if (encodeOptions & REUSE_BUFFER_MODE) {
|
|
2688
|
+
target.start = start;
|
|
2689
|
+
target.end = position2;
|
|
2690
|
+
return target;
|
|
2691
|
+
}
|
|
2692
|
+
return target.subarray(start, position2);
|
|
2693
|
+
} catch (error) {
|
|
2694
|
+
encodingError = error;
|
|
2695
|
+
throw error;
|
|
2696
|
+
} finally {
|
|
2697
|
+
if (structures) {
|
|
2698
|
+
resetStructures();
|
|
2699
|
+
if (hasSharedUpdate && packr.saveStructures) {
|
|
2700
|
+
let sharedLength = structures.sharedLength || 0;
|
|
2701
|
+
let returnBuffer = target.subarray(start, position2);
|
|
2702
|
+
let newSharedData = prepareStructures(structures, packr);
|
|
2703
|
+
if (!encodingError) {
|
|
2704
|
+
if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
|
|
2705
|
+
return packr.pack(value, encodeOptions);
|
|
2706
|
+
}
|
|
2707
|
+
packr.lastNamedStructuresLength = sharedLength;
|
|
2708
|
+
if (target.length > 1073741824)
|
|
2709
|
+
target = null;
|
|
2710
|
+
return returnBuffer;
|
|
2711
|
+
}
|
|
2712
|
+
}
|
|
2713
|
+
}
|
|
2714
|
+
if (target.length > 1073741824)
|
|
2715
|
+
target = null;
|
|
2716
|
+
if (encodeOptions & RESET_BUFFER_MODE)
|
|
2717
|
+
position2 = start;
|
|
2718
|
+
}
|
|
2719
|
+
};
|
|
2720
|
+
const resetStructures = () => {
|
|
2721
|
+
if (serializationsSinceTransitionRebuild < 10)
|
|
2722
|
+
serializationsSinceTransitionRebuild++;
|
|
2723
|
+
let sharedLength = structures.sharedLength || 0;
|
|
2724
|
+
if (structures.length > sharedLength && !isSequential)
|
|
2725
|
+
structures.length = sharedLength;
|
|
2726
|
+
if (transitionsCount > 1e4) {
|
|
2727
|
+
structures.transitions = null;
|
|
2728
|
+
serializationsSinceTransitionRebuild = 0;
|
|
2729
|
+
transitionsCount = 0;
|
|
2730
|
+
if (recordIdsToRemove.length > 0)
|
|
2731
|
+
recordIdsToRemove = [];
|
|
2732
|
+
} else if (recordIdsToRemove.length > 0 && !isSequential) {
|
|
2733
|
+
for (let i = 0, l = recordIdsToRemove.length;i < l; i++) {
|
|
2734
|
+
recordIdsToRemove[i][RECORD_SYMBOL] = 0;
|
|
2735
|
+
}
|
|
2736
|
+
recordIdsToRemove = [];
|
|
2737
|
+
}
|
|
2738
|
+
};
|
|
2739
|
+
const packArray = (value) => {
|
|
2740
|
+
var length = value.length;
|
|
2741
|
+
if (length < 16) {
|
|
2742
|
+
target[position2++] = 144 | length;
|
|
2743
|
+
} else if (length < 65536) {
|
|
2744
|
+
target[position2++] = 220;
|
|
2745
|
+
target[position2++] = length >> 8;
|
|
2746
|
+
target[position2++] = length & 255;
|
|
2747
|
+
} else {
|
|
2748
|
+
target[position2++] = 221;
|
|
2749
|
+
targetView.setUint32(position2, length);
|
|
2750
|
+
position2 += 4;
|
|
2751
|
+
}
|
|
2752
|
+
for (let i = 0;i < length; i++) {
|
|
2753
|
+
pack(value[i]);
|
|
2754
|
+
}
|
|
2755
|
+
};
|
|
2756
|
+
const pack = (value) => {
|
|
2757
|
+
if (position2 > safeEnd)
|
|
2758
|
+
target = makeRoom(position2);
|
|
2759
|
+
var type = typeof value;
|
|
2760
|
+
var length;
|
|
2761
|
+
if (type === "string") {
|
|
2762
|
+
let strLength = value.length;
|
|
2763
|
+
if (bundledStrings2 && strLength >= 4 && strLength < 4096) {
|
|
2764
|
+
if ((bundledStrings2.size += strLength) > MAX_BUNDLE_SIZE) {
|
|
2765
|
+
let extStart;
|
|
2766
|
+
let maxBytes2 = (bundledStrings2[0] ? bundledStrings2[0].length * 3 + bundledStrings2[1].length : 0) + 10;
|
|
2767
|
+
if (position2 + maxBytes2 > safeEnd)
|
|
2768
|
+
target = makeRoom(position2 + maxBytes2);
|
|
2769
|
+
let lastBundle;
|
|
2770
|
+
if (bundledStrings2.position) {
|
|
2771
|
+
lastBundle = bundledStrings2;
|
|
2772
|
+
target[position2] = 200;
|
|
2773
|
+
position2 += 3;
|
|
2774
|
+
target[position2++] = 98;
|
|
2775
|
+
extStart = position2 - start;
|
|
2776
|
+
position2 += 4;
|
|
2777
|
+
writeBundles(start, pack, 0);
|
|
2778
|
+
targetView.setUint16(extStart + start - 3, position2 - start - extStart);
|
|
2779
|
+
} else {
|
|
2780
|
+
target[position2++] = 214;
|
|
2781
|
+
target[position2++] = 98;
|
|
2782
|
+
extStart = position2 - start;
|
|
2783
|
+
position2 += 4;
|
|
2784
|
+
}
|
|
2785
|
+
bundledStrings2 = ["", ""];
|
|
2786
|
+
bundledStrings2.previous = lastBundle;
|
|
2787
|
+
bundledStrings2.size = 0;
|
|
2788
|
+
bundledStrings2.position = extStart;
|
|
2789
|
+
}
|
|
2790
|
+
let twoByte = hasNonLatin.test(value);
|
|
2791
|
+
bundledStrings2[twoByte ? 0 : 1] += value;
|
|
2792
|
+
target[position2++] = 193;
|
|
2793
|
+
pack(twoByte ? -strLength : strLength);
|
|
2794
|
+
return;
|
|
2795
|
+
}
|
|
2796
|
+
let headerSize;
|
|
2797
|
+
if (strLength < 32) {
|
|
2798
|
+
headerSize = 1;
|
|
2799
|
+
} else if (strLength < 256) {
|
|
2800
|
+
headerSize = 2;
|
|
2801
|
+
} else if (strLength < 65536) {
|
|
2802
|
+
headerSize = 3;
|
|
2803
|
+
} else {
|
|
2804
|
+
headerSize = 5;
|
|
2805
|
+
}
|
|
2806
|
+
let maxBytes = strLength * 3;
|
|
2807
|
+
if (position2 + maxBytes > safeEnd)
|
|
2808
|
+
target = makeRoom(position2 + maxBytes);
|
|
2809
|
+
if (strLength < 64 || !encodeUtf8) {
|
|
2810
|
+
let i, c1, c2, strPosition = position2 + headerSize;
|
|
2811
|
+
for (i = 0;i < strLength; i++) {
|
|
2812
|
+
c1 = value.charCodeAt(i);
|
|
2813
|
+
if (c1 < 128) {
|
|
2814
|
+
target[strPosition++] = c1;
|
|
2815
|
+
} else if (c1 < 2048) {
|
|
2816
|
+
target[strPosition++] = c1 >> 6 | 192;
|
|
2817
|
+
target[strPosition++] = c1 & 63 | 128;
|
|
2818
|
+
} else if ((c1 & 64512) === 55296 && ((c2 = value.charCodeAt(i + 1)) & 64512) === 56320) {
|
|
2819
|
+
c1 = 65536 + ((c1 & 1023) << 10) + (c2 & 1023);
|
|
2820
|
+
i++;
|
|
2821
|
+
target[strPosition++] = c1 >> 18 | 240;
|
|
2822
|
+
target[strPosition++] = c1 >> 12 & 63 | 128;
|
|
2823
|
+
target[strPosition++] = c1 >> 6 & 63 | 128;
|
|
2824
|
+
target[strPosition++] = c1 & 63 | 128;
|
|
2825
|
+
} else {
|
|
2826
|
+
target[strPosition++] = c1 >> 12 | 224;
|
|
2827
|
+
target[strPosition++] = c1 >> 6 & 63 | 128;
|
|
2828
|
+
target[strPosition++] = c1 & 63 | 128;
|
|
2829
|
+
}
|
|
2830
|
+
}
|
|
2831
|
+
length = strPosition - position2 - headerSize;
|
|
2832
|
+
} else {
|
|
2833
|
+
length = encodeUtf8(value, position2 + headerSize);
|
|
2834
|
+
}
|
|
2835
|
+
if (length < 32) {
|
|
2836
|
+
target[position2++] = 160 | length;
|
|
2837
|
+
} else if (length < 256) {
|
|
2838
|
+
if (headerSize < 2) {
|
|
2839
|
+
target.copyWithin(position2 + 2, position2 + 1, position2 + 1 + length);
|
|
2840
|
+
}
|
|
2841
|
+
target[position2++] = 217;
|
|
2842
|
+
target[position2++] = length;
|
|
2843
|
+
} else if (length < 65536) {
|
|
2844
|
+
if (headerSize < 3) {
|
|
2845
|
+
target.copyWithin(position2 + 3, position2 + 2, position2 + 2 + length);
|
|
2846
|
+
}
|
|
2847
|
+
target[position2++] = 218;
|
|
2848
|
+
target[position2++] = length >> 8;
|
|
2849
|
+
target[position2++] = length & 255;
|
|
2850
|
+
} else {
|
|
2851
|
+
if (headerSize < 5) {
|
|
2852
|
+
target.copyWithin(position2 + 5, position2 + 3, position2 + 3 + length);
|
|
2853
|
+
}
|
|
2854
|
+
target[position2++] = 219;
|
|
2855
|
+
targetView.setUint32(position2, length);
|
|
2856
|
+
position2 += 4;
|
|
2857
|
+
}
|
|
2858
|
+
position2 += length;
|
|
2859
|
+
} else if (type === "number") {
|
|
2860
|
+
if (value >>> 0 === value) {
|
|
2861
|
+
if (value < 32 || value < 128 && this.useRecords === false || value < 64 && !this.randomAccessStructure) {
|
|
2862
|
+
target[position2++] = value;
|
|
2863
|
+
} else if (value < 256) {
|
|
2864
|
+
target[position2++] = 204;
|
|
2865
|
+
target[position2++] = value;
|
|
2866
|
+
} else if (value < 65536) {
|
|
2867
|
+
target[position2++] = 205;
|
|
2868
|
+
target[position2++] = value >> 8;
|
|
2869
|
+
target[position2++] = value & 255;
|
|
2870
|
+
} else {
|
|
2871
|
+
target[position2++] = 206;
|
|
2872
|
+
targetView.setUint32(position2, value);
|
|
2873
|
+
position2 += 4;
|
|
2874
|
+
}
|
|
2875
|
+
} else if (value >> 0 === value) {
|
|
2876
|
+
if (value >= -32) {
|
|
2877
|
+
target[position2++] = 256 + value;
|
|
2878
|
+
} else if (value >= -128) {
|
|
2879
|
+
target[position2++] = 208;
|
|
2880
|
+
target[position2++] = value + 256;
|
|
2881
|
+
} else if (value >= -32768) {
|
|
2882
|
+
target[position2++] = 209;
|
|
2883
|
+
targetView.setInt16(position2, value);
|
|
2884
|
+
position2 += 2;
|
|
2885
|
+
} else {
|
|
2886
|
+
target[position2++] = 210;
|
|
2887
|
+
targetView.setInt32(position2, value);
|
|
2888
|
+
position2 += 4;
|
|
2889
|
+
}
|
|
2890
|
+
} else {
|
|
2891
|
+
let useFloat32;
|
|
2892
|
+
if ((useFloat32 = this.useFloat32) > 0 && value < 4294967296 && value >= -2147483648) {
|
|
2893
|
+
target[position2++] = 202;
|
|
2894
|
+
targetView.setFloat32(position2, value);
|
|
2895
|
+
let xShifted;
|
|
2896
|
+
if (useFloat32 < 4 || (xShifted = value * mult10[(target[position2] & 127) << 1 | target[position2 + 1] >> 7]) >> 0 === xShifted) {
|
|
2897
|
+
position2 += 4;
|
|
2898
|
+
return;
|
|
2899
|
+
} else
|
|
2900
|
+
position2--;
|
|
2901
|
+
}
|
|
2902
|
+
target[position2++] = 203;
|
|
2903
|
+
targetView.setFloat64(position2, value);
|
|
2904
|
+
position2 += 8;
|
|
2905
|
+
}
|
|
2906
|
+
} else if (type === "object" || type === "function") {
|
|
2907
|
+
if (!value)
|
|
2908
|
+
target[position2++] = 192;
|
|
2909
|
+
else {
|
|
2910
|
+
if (referenceMap2) {
|
|
2911
|
+
let referee = referenceMap2.get(value);
|
|
2912
|
+
if (referee) {
|
|
2913
|
+
if (!referee.id) {
|
|
2914
|
+
let idsToInsert = referenceMap2.idsToInsert || (referenceMap2.idsToInsert = []);
|
|
2915
|
+
referee.id = idsToInsert.push(referee);
|
|
2916
|
+
}
|
|
2917
|
+
target[position2++] = 214;
|
|
2918
|
+
target[position2++] = 112;
|
|
2919
|
+
targetView.setUint32(position2, referee.id);
|
|
2920
|
+
position2 += 4;
|
|
2921
|
+
return;
|
|
2922
|
+
} else
|
|
2923
|
+
referenceMap2.set(value, { offset: position2 - start });
|
|
2924
|
+
}
|
|
2925
|
+
let constructor = value.constructor;
|
|
2926
|
+
if (constructor === Object) {
|
|
2927
|
+
writeObject(value);
|
|
2928
|
+
} else if (constructor === Array) {
|
|
2929
|
+
packArray(value);
|
|
2930
|
+
} else if (constructor === Map) {
|
|
2931
|
+
if (this.mapAsEmptyObject)
|
|
2932
|
+
target[position2++] = 128;
|
|
2933
|
+
else {
|
|
2934
|
+
length = value.size;
|
|
2935
|
+
if (length < 16) {
|
|
2936
|
+
target[position2++] = 128 | length;
|
|
2937
|
+
} else if (length < 65536) {
|
|
2938
|
+
target[position2++] = 222;
|
|
2939
|
+
target[position2++] = length >> 8;
|
|
2940
|
+
target[position2++] = length & 255;
|
|
2941
|
+
} else {
|
|
2942
|
+
target[position2++] = 223;
|
|
2943
|
+
targetView.setUint32(position2, length);
|
|
2944
|
+
position2 += 4;
|
|
2945
|
+
}
|
|
2946
|
+
for (let [key, entryValue] of value) {
|
|
2947
|
+
pack(key);
|
|
2948
|
+
pack(entryValue);
|
|
2949
|
+
}
|
|
2950
|
+
}
|
|
2951
|
+
} else {
|
|
2952
|
+
for (let i = 0, l = extensions.length;i < l; i++) {
|
|
2953
|
+
let extensionClass = extensionClasses[i];
|
|
2954
|
+
if (value instanceof extensionClass) {
|
|
2955
|
+
let extension = extensions[i];
|
|
2956
|
+
if (extension.write) {
|
|
2957
|
+
if (extension.type) {
|
|
2958
|
+
target[position2++] = 212;
|
|
2959
|
+
target[position2++] = extension.type;
|
|
2960
|
+
target[position2++] = 0;
|
|
2961
|
+
}
|
|
2962
|
+
let writeResult = extension.write.call(this, value);
|
|
2963
|
+
if (writeResult === value) {
|
|
2964
|
+
if (Array.isArray(value)) {
|
|
2965
|
+
packArray(value);
|
|
2966
|
+
} else {
|
|
2967
|
+
writeObject(value);
|
|
2968
|
+
}
|
|
2969
|
+
} else {
|
|
2970
|
+
pack(writeResult);
|
|
2971
|
+
}
|
|
2972
|
+
return;
|
|
2973
|
+
}
|
|
2974
|
+
let currentTarget = target;
|
|
2975
|
+
let currentTargetView = targetView;
|
|
2976
|
+
let currentPosition = position2;
|
|
2977
|
+
target = null;
|
|
2978
|
+
let result;
|
|
2979
|
+
try {
|
|
2980
|
+
result = extension.pack.call(this, value, (size) => {
|
|
2981
|
+
target = currentTarget;
|
|
2982
|
+
currentTarget = null;
|
|
2983
|
+
position2 += size;
|
|
2984
|
+
if (position2 > safeEnd)
|
|
2985
|
+
makeRoom(position2);
|
|
2986
|
+
return {
|
|
2987
|
+
target,
|
|
2988
|
+
targetView,
|
|
2989
|
+
position: position2 - size
|
|
2990
|
+
};
|
|
2991
|
+
}, pack);
|
|
2992
|
+
} finally {
|
|
2993
|
+
if (currentTarget) {
|
|
2994
|
+
target = currentTarget;
|
|
2995
|
+
targetView = currentTargetView;
|
|
2996
|
+
position2 = currentPosition;
|
|
2997
|
+
safeEnd = target.length - 10;
|
|
2998
|
+
}
|
|
2999
|
+
}
|
|
3000
|
+
if (result) {
|
|
3001
|
+
if (result.length + position2 > safeEnd)
|
|
3002
|
+
makeRoom(result.length + position2);
|
|
3003
|
+
position2 = writeExtensionData(result, target, position2, extension.type);
|
|
3004
|
+
}
|
|
3005
|
+
return;
|
|
3006
|
+
}
|
|
3007
|
+
}
|
|
3008
|
+
if (Array.isArray(value)) {
|
|
3009
|
+
packArray(value);
|
|
3010
|
+
} else {
|
|
3011
|
+
if (value.toJSON) {
|
|
3012
|
+
const json = value.toJSON();
|
|
3013
|
+
if (json !== value)
|
|
3014
|
+
return pack(json);
|
|
3015
|
+
}
|
|
3016
|
+
if (type === "function")
|
|
3017
|
+
return pack(this.writeFunction && this.writeFunction(value));
|
|
3018
|
+
writeObject(value);
|
|
3019
|
+
}
|
|
3020
|
+
}
|
|
3021
|
+
}
|
|
3022
|
+
} else if (type === "boolean") {
|
|
3023
|
+
target[position2++] = value ? 195 : 194;
|
|
3024
|
+
} else if (type === "bigint") {
|
|
3025
|
+
if (value < 9223372036854776000 && value >= -9223372036854776000) {
|
|
3026
|
+
target[position2++] = 211;
|
|
3027
|
+
targetView.setBigInt64(position2, value);
|
|
3028
|
+
} else if (value < 18446744073709552000 && value > 0) {
|
|
3029
|
+
target[position2++] = 207;
|
|
3030
|
+
targetView.setBigUint64(position2, value);
|
|
3031
|
+
} else {
|
|
3032
|
+
if (this.largeBigIntToFloat) {
|
|
3033
|
+
target[position2++] = 203;
|
|
3034
|
+
targetView.setFloat64(position2, Number(value));
|
|
3035
|
+
} else if (this.largeBigIntToString) {
|
|
3036
|
+
return pack(value.toString());
|
|
3037
|
+
} else if (this.useBigIntExtension || this.moreTypes) {
|
|
3038
|
+
let empty = value < 0 ? BigInt(-1) : BigInt(0);
|
|
3039
|
+
let array;
|
|
3040
|
+
if (value >> BigInt(65536) === empty) {
|
|
3041
|
+
let mask = BigInt(18446744073709552000) - BigInt(1);
|
|
3042
|
+
let chunks = [];
|
|
3043
|
+
while (true) {
|
|
3044
|
+
chunks.push(value & mask);
|
|
3045
|
+
if (value >> BigInt(63) === empty)
|
|
3046
|
+
break;
|
|
3047
|
+
value >>= BigInt(64);
|
|
3048
|
+
}
|
|
3049
|
+
array = new Uint8Array(new BigUint64Array(chunks).buffer);
|
|
3050
|
+
array.reverse();
|
|
3051
|
+
} else {
|
|
3052
|
+
let invert = value < 0;
|
|
3053
|
+
let string = (invert ? ~value : value).toString(16);
|
|
3054
|
+
if (string.length % 2) {
|
|
3055
|
+
string = "0" + string;
|
|
3056
|
+
} else if (parseInt(string.charAt(0), 16) >= 8) {
|
|
3057
|
+
string = "00" + string;
|
|
3058
|
+
}
|
|
3059
|
+
if (hasNodeBuffer) {
|
|
3060
|
+
array = Buffer.from(string, "hex");
|
|
3061
|
+
} else {
|
|
3062
|
+
array = new Uint8Array(string.length / 2);
|
|
3063
|
+
for (let i = 0;i < array.length; i++) {
|
|
3064
|
+
array[i] = parseInt(string.slice(i * 2, i * 2 + 2), 16);
|
|
3065
|
+
}
|
|
3066
|
+
}
|
|
3067
|
+
if (invert) {
|
|
3068
|
+
for (let i = 0;i < array.length; i++)
|
|
3069
|
+
array[i] = ~array[i];
|
|
3070
|
+
}
|
|
3071
|
+
}
|
|
3072
|
+
if (array.length + position2 > safeEnd)
|
|
3073
|
+
makeRoom(array.length + position2);
|
|
3074
|
+
position2 = writeExtensionData(array, target, position2, 66);
|
|
3075
|
+
return;
|
|
3076
|
+
} else {
|
|
3077
|
+
throw new RangeError(value + " was too large to fit in MessagePack 64-bit integer format, use" + " useBigIntExtension, or set largeBigIntToFloat to convert to float-64, or set" + " largeBigIntToString to convert to string");
|
|
3078
|
+
}
|
|
3079
|
+
}
|
|
3080
|
+
position2 += 8;
|
|
3081
|
+
} else if (type === "undefined") {
|
|
3082
|
+
if (this.encodeUndefinedAsNil)
|
|
3083
|
+
target[position2++] = 192;
|
|
3084
|
+
else {
|
|
3085
|
+
target[position2++] = 212;
|
|
3086
|
+
target[position2++] = 0;
|
|
3087
|
+
target[position2++] = 0;
|
|
3088
|
+
}
|
|
3089
|
+
} else {
|
|
3090
|
+
throw new Error("Unknown type: " + type);
|
|
3091
|
+
}
|
|
3092
|
+
};
|
|
3093
|
+
const writePlainObject = this.variableMapSize || this.coercibleKeyAsNumber || this.skipValues ? (object) => {
|
|
3094
|
+
let keys;
|
|
3095
|
+
if (this.skipValues) {
|
|
3096
|
+
keys = [];
|
|
3097
|
+
for (let key2 in object) {
|
|
3098
|
+
if ((typeof object.hasOwnProperty !== "function" || object.hasOwnProperty(key2)) && !this.skipValues.includes(object[key2]))
|
|
3099
|
+
keys.push(key2);
|
|
3100
|
+
}
|
|
3101
|
+
} else {
|
|
3102
|
+
keys = Object.keys(object);
|
|
3103
|
+
}
|
|
3104
|
+
let length = keys.length;
|
|
3105
|
+
if (length < 16) {
|
|
3106
|
+
target[position2++] = 128 | length;
|
|
3107
|
+
} else if (length < 65536) {
|
|
3108
|
+
target[position2++] = 222;
|
|
3109
|
+
target[position2++] = length >> 8;
|
|
3110
|
+
target[position2++] = length & 255;
|
|
3111
|
+
} else {
|
|
3112
|
+
target[position2++] = 223;
|
|
3113
|
+
targetView.setUint32(position2, length);
|
|
3114
|
+
position2 += 4;
|
|
3115
|
+
}
|
|
3116
|
+
let key;
|
|
3117
|
+
if (this.coercibleKeyAsNumber) {
|
|
3118
|
+
for (let i = 0;i < length; i++) {
|
|
3119
|
+
key = keys[i];
|
|
3120
|
+
let num = Number(key);
|
|
3121
|
+
pack(isNaN(num) ? key : num);
|
|
3122
|
+
pack(object[key]);
|
|
3123
|
+
}
|
|
3124
|
+
} else {
|
|
3125
|
+
for (let i = 0;i < length; i++) {
|
|
3126
|
+
pack(key = keys[i]);
|
|
3127
|
+
pack(object[key]);
|
|
3128
|
+
}
|
|
3129
|
+
}
|
|
3130
|
+
} : (object) => {
|
|
3131
|
+
target[position2++] = 222;
|
|
3132
|
+
let objectOffset = position2 - start;
|
|
3133
|
+
position2 += 2;
|
|
3134
|
+
let size = 0;
|
|
3135
|
+
for (let key in object) {
|
|
3136
|
+
if (typeof object.hasOwnProperty !== "function" || object.hasOwnProperty(key)) {
|
|
3137
|
+
pack(key);
|
|
3138
|
+
pack(object[key]);
|
|
3139
|
+
size++;
|
|
3140
|
+
}
|
|
3141
|
+
}
|
|
3142
|
+
if (size > 65535) {
|
|
3143
|
+
throw new Error("Object is too large to serialize with fast 16-bit map size," + ' use the "variableMapSize" option to serialize this object');
|
|
3144
|
+
}
|
|
3145
|
+
target[objectOffset++ + start] = size >> 8;
|
|
3146
|
+
target[objectOffset + start] = size & 255;
|
|
3147
|
+
};
|
|
3148
|
+
const writeRecord = this.useRecords === false ? writePlainObject : options.progressiveRecords && !useTwoByteRecords ? (object) => {
|
|
3149
|
+
let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
|
|
3150
|
+
let objectOffset = position2++ - start;
|
|
3151
|
+
let wroteKeys;
|
|
3152
|
+
for (let key in object) {
|
|
3153
|
+
if (typeof object.hasOwnProperty !== "function" || object.hasOwnProperty(key)) {
|
|
3154
|
+
nextTransition = transition[key];
|
|
3155
|
+
if (nextTransition)
|
|
3156
|
+
transition = nextTransition;
|
|
3157
|
+
else {
|
|
3158
|
+
let keys = Object.keys(object);
|
|
3159
|
+
let lastTransition = transition;
|
|
3160
|
+
transition = structures.transitions;
|
|
3161
|
+
let newTransitions = 0;
|
|
3162
|
+
for (let i = 0, l = keys.length;i < l; i++) {
|
|
3163
|
+
let key2 = keys[i];
|
|
3164
|
+
nextTransition = transition[key2];
|
|
3165
|
+
if (!nextTransition) {
|
|
3166
|
+
nextTransition = transition[key2] = Object.create(null);
|
|
3167
|
+
newTransitions++;
|
|
3168
|
+
}
|
|
3169
|
+
transition = nextTransition;
|
|
3170
|
+
}
|
|
3171
|
+
if (objectOffset + start + 1 == position2) {
|
|
3172
|
+
position2--;
|
|
3173
|
+
newRecord(transition, keys, newTransitions);
|
|
3174
|
+
} else
|
|
3175
|
+
insertNewRecord(transition, keys, objectOffset, newTransitions);
|
|
3176
|
+
wroteKeys = true;
|
|
3177
|
+
transition = lastTransition[key];
|
|
3178
|
+
}
|
|
3179
|
+
pack(object[key]);
|
|
3180
|
+
}
|
|
3181
|
+
}
|
|
3182
|
+
if (!wroteKeys) {
|
|
3183
|
+
let recordId = transition[RECORD_SYMBOL];
|
|
3184
|
+
if (recordId)
|
|
3185
|
+
target[objectOffset + start] = recordId;
|
|
3186
|
+
else
|
|
3187
|
+
insertNewRecord(transition, Object.keys(object), objectOffset, 0);
|
|
3188
|
+
}
|
|
3189
|
+
} : (object) => {
|
|
3190
|
+
let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
|
|
3191
|
+
let newTransitions = 0;
|
|
3192
|
+
for (let key in object)
|
|
3193
|
+
if (typeof object.hasOwnProperty !== "function" || object.hasOwnProperty(key)) {
|
|
3194
|
+
nextTransition = transition[key];
|
|
3195
|
+
if (!nextTransition) {
|
|
3196
|
+
nextTransition = transition[key] = Object.create(null);
|
|
3197
|
+
newTransitions++;
|
|
3198
|
+
}
|
|
3199
|
+
transition = nextTransition;
|
|
3200
|
+
}
|
|
3201
|
+
let recordId = transition[RECORD_SYMBOL];
|
|
3202
|
+
if (recordId) {
|
|
3203
|
+
if (recordId >= 96 && useTwoByteRecords) {
|
|
3204
|
+
target[position2++] = ((recordId -= 96) & 31) + 96;
|
|
3205
|
+
target[position2++] = recordId >> 5;
|
|
3206
|
+
} else
|
|
3207
|
+
target[position2++] = recordId;
|
|
3208
|
+
} else {
|
|
3209
|
+
newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions);
|
|
3210
|
+
}
|
|
3211
|
+
for (let key in object)
|
|
3212
|
+
if (typeof object.hasOwnProperty !== "function" || object.hasOwnProperty(key)) {
|
|
3213
|
+
pack(object[key]);
|
|
3214
|
+
}
|
|
3215
|
+
};
|
|
3216
|
+
const checkUseRecords = typeof this.useRecords == "function" && this.useRecords;
|
|
3217
|
+
const writeObject = checkUseRecords ? (object) => {
|
|
3218
|
+
checkUseRecords(object) ? writeRecord(object) : writePlainObject(object);
|
|
3219
|
+
} : writeRecord;
|
|
3220
|
+
const makeRoom = (end) => {
|
|
3221
|
+
let newSize;
|
|
3222
|
+
if (end > 16777216) {
|
|
3223
|
+
if (end - start > MAX_BUFFER_SIZE)
|
|
3224
|
+
throw new Error("Packed buffer would be larger than maximum buffer size");
|
|
3225
|
+
newSize = Math.min(MAX_BUFFER_SIZE, Math.round(Math.max((end - start) * (end > 67108864 ? 1.25 : 2), 4194304) / 4096) * 4096);
|
|
3226
|
+
} else
|
|
3227
|
+
newSize = (Math.max(end - start << 2, target.length - 1) >> 12) + 1 << 12;
|
|
3228
|
+
let newBuffer = new ByteArrayAllocate(newSize);
|
|
3229
|
+
targetView = newBuffer.dataView || (newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize));
|
|
3230
|
+
end = Math.min(end, target.length);
|
|
3231
|
+
if (target.copy)
|
|
3232
|
+
target.copy(newBuffer, 0, start, end);
|
|
3233
|
+
else
|
|
3234
|
+
newBuffer.set(target.slice(start, end));
|
|
3235
|
+
position2 -= start;
|
|
3236
|
+
start = 0;
|
|
3237
|
+
safeEnd = newBuffer.length - 10;
|
|
3238
|
+
return target = newBuffer;
|
|
3239
|
+
};
|
|
3240
|
+
const newRecord = (transition, keys, newTransitions) => {
|
|
3241
|
+
let recordId = structures.nextId;
|
|
3242
|
+
if (!recordId)
|
|
3243
|
+
recordId = 64;
|
|
3244
|
+
if (recordId < sharedLimitId && this.shouldShareStructure && !this.shouldShareStructure(keys)) {
|
|
3245
|
+
recordId = structures.nextOwnId;
|
|
3246
|
+
if (!(recordId < maxStructureId))
|
|
3247
|
+
recordId = sharedLimitId;
|
|
3248
|
+
structures.nextOwnId = recordId + 1;
|
|
3249
|
+
} else {
|
|
3250
|
+
if (recordId >= maxStructureId)
|
|
3251
|
+
recordId = sharedLimitId;
|
|
3252
|
+
structures.nextId = recordId + 1;
|
|
3253
|
+
}
|
|
3254
|
+
let highByte = keys.highByte = recordId >= 96 && useTwoByteRecords ? recordId - 96 >> 5 : -1;
|
|
3255
|
+
transition[RECORD_SYMBOL] = recordId;
|
|
3256
|
+
transition.__keys__ = keys;
|
|
3257
|
+
structures[recordId - 64] = keys;
|
|
3258
|
+
if (recordId < sharedLimitId) {
|
|
3259
|
+
keys.isShared = true;
|
|
3260
|
+
structures.sharedLength = recordId - 63;
|
|
3261
|
+
hasSharedUpdate = true;
|
|
3262
|
+
if (highByte >= 0) {
|
|
3263
|
+
target[position2++] = (recordId & 31) + 96;
|
|
3264
|
+
target[position2++] = highByte;
|
|
3265
|
+
} else {
|
|
3266
|
+
target[position2++] = recordId;
|
|
3267
|
+
}
|
|
3268
|
+
} else {
|
|
3269
|
+
if (highByte >= 0) {
|
|
3270
|
+
target[position2++] = 213;
|
|
3271
|
+
target[position2++] = 114;
|
|
3272
|
+
target[position2++] = (recordId & 31) + 96;
|
|
3273
|
+
target[position2++] = highByte;
|
|
3274
|
+
} else {
|
|
3275
|
+
target[position2++] = 212;
|
|
3276
|
+
target[position2++] = 114;
|
|
3277
|
+
target[position2++] = recordId;
|
|
3278
|
+
}
|
|
3279
|
+
if (newTransitions)
|
|
3280
|
+
transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
|
|
3281
|
+
if (recordIdsToRemove.length >= maxOwnStructures)
|
|
3282
|
+
recordIdsToRemove.shift()[RECORD_SYMBOL] = 0;
|
|
3283
|
+
recordIdsToRemove.push(transition);
|
|
3284
|
+
pack(keys);
|
|
3285
|
+
}
|
|
3286
|
+
};
|
|
3287
|
+
const insertNewRecord = (transition, keys, insertionOffset, newTransitions) => {
|
|
3288
|
+
let mainTarget = target;
|
|
3289
|
+
let mainPosition = position2;
|
|
3290
|
+
let mainSafeEnd = safeEnd;
|
|
3291
|
+
let mainStart = start;
|
|
3292
|
+
target = keysTarget;
|
|
3293
|
+
position2 = 0;
|
|
3294
|
+
start = 0;
|
|
3295
|
+
if (!target)
|
|
3296
|
+
keysTarget = target = new ByteArrayAllocate(8192);
|
|
3297
|
+
safeEnd = target.length - 10;
|
|
3298
|
+
newRecord(transition, keys, newTransitions);
|
|
3299
|
+
keysTarget = target;
|
|
3300
|
+
let keysPosition = position2;
|
|
3301
|
+
target = mainTarget;
|
|
3302
|
+
position2 = mainPosition;
|
|
3303
|
+
safeEnd = mainSafeEnd;
|
|
3304
|
+
start = mainStart;
|
|
3305
|
+
if (keysPosition > 1) {
|
|
3306
|
+
let newEnd = position2 + keysPosition - 1;
|
|
3307
|
+
if (newEnd > safeEnd)
|
|
3308
|
+
makeRoom(newEnd);
|
|
3309
|
+
let insertionPosition = insertionOffset + start;
|
|
3310
|
+
target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position2);
|
|
3311
|
+
target.set(keysTarget.slice(0, keysPosition), insertionPosition);
|
|
3312
|
+
position2 = newEnd;
|
|
3313
|
+
} else {
|
|
3314
|
+
target[insertionOffset + start] = keysTarget[0];
|
|
3315
|
+
}
|
|
3316
|
+
};
|
|
3317
|
+
const writeStruct = (object) => {
|
|
3318
|
+
let newPosition = writeStructSlots(object, target, start, position2, structures, makeRoom, (value, newPosition2, notifySharedUpdate) => {
|
|
3319
|
+
if (notifySharedUpdate)
|
|
3320
|
+
return hasSharedUpdate = true;
|
|
3321
|
+
position2 = newPosition2;
|
|
3322
|
+
let startTarget = target;
|
|
3323
|
+
pack(value);
|
|
3324
|
+
resetStructures();
|
|
3325
|
+
if (startTarget !== target) {
|
|
3326
|
+
return { position: position2, targetView, target };
|
|
3327
|
+
}
|
|
3328
|
+
return position2;
|
|
3329
|
+
}, this);
|
|
3330
|
+
if (newPosition === 0)
|
|
3331
|
+
return writeObject(object);
|
|
3332
|
+
position2 = newPosition;
|
|
3333
|
+
};
|
|
3334
|
+
}
|
|
3335
|
+
useBuffer(buffer) {
|
|
3336
|
+
target = buffer;
|
|
3337
|
+
target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength));
|
|
3338
|
+
targetView = target.dataView;
|
|
3339
|
+
position2 = 0;
|
|
3340
|
+
}
|
|
3341
|
+
set position(value) {
|
|
3342
|
+
position2 = value;
|
|
3343
|
+
}
|
|
3344
|
+
get position() {
|
|
3345
|
+
return position2;
|
|
3346
|
+
}
|
|
3347
|
+
clearSharedData() {
|
|
3348
|
+
if (this.structures)
|
|
3349
|
+
this.structures = [];
|
|
3350
|
+
if (this.typedStructs)
|
|
3351
|
+
this.typedStructs = [];
|
|
3352
|
+
}
|
|
3353
|
+
}
|
|
3354
|
+
extensionClasses = [Date, Set, Error, RegExp, ArrayBuffer, Object.getPrototypeOf(Uint8Array.prototype).constructor, DataView, C1Type];
|
|
3355
|
+
extensions = [{
|
|
3356
|
+
pack(date, allocateForWrite, pack) {
|
|
3357
|
+
let seconds = date.getTime() / 1000;
|
|
3358
|
+
if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 4294967296) {
|
|
3359
|
+
let { target: target2, targetView: targetView2, position: position3 } = allocateForWrite(6);
|
|
3360
|
+
target2[position3++] = 214;
|
|
3361
|
+
target2[position3++] = 255;
|
|
3362
|
+
targetView2.setUint32(position3, seconds);
|
|
3363
|
+
} else if (seconds > 0 && seconds < 4294967296) {
|
|
3364
|
+
let { target: target2, targetView: targetView2, position: position3 } = allocateForWrite(10);
|
|
3365
|
+
target2[position3++] = 215;
|
|
3366
|
+
target2[position3++] = 255;
|
|
3367
|
+
targetView2.setUint32(position3, date.getMilliseconds() * 4000000 + (seconds / 1000 / 4294967296 >> 0));
|
|
3368
|
+
targetView2.setUint32(position3 + 4, seconds);
|
|
3369
|
+
} else if (isNaN(seconds)) {
|
|
3370
|
+
if (this.onInvalidDate) {
|
|
3371
|
+
allocateForWrite(0);
|
|
3372
|
+
return pack(this.onInvalidDate());
|
|
3373
|
+
}
|
|
3374
|
+
let { target: target2, targetView: targetView2, position: position3 } = allocateForWrite(3);
|
|
3375
|
+
target2[position3++] = 212;
|
|
3376
|
+
target2[position3++] = 255;
|
|
3377
|
+
target2[position3++] = 255;
|
|
3378
|
+
} else {
|
|
3379
|
+
let { target: target2, targetView: targetView2, position: position3 } = allocateForWrite(15);
|
|
3380
|
+
target2[position3++] = 199;
|
|
3381
|
+
target2[position3++] = 12;
|
|
3382
|
+
target2[position3++] = 255;
|
|
3383
|
+
targetView2.setUint32(position3, date.getMilliseconds() * 1e6);
|
|
3384
|
+
targetView2.setBigInt64(position3 + 4, BigInt(Math.floor(seconds)));
|
|
3385
|
+
}
|
|
3386
|
+
}
|
|
3387
|
+
}, {
|
|
3388
|
+
pack(set, allocateForWrite, pack) {
|
|
3389
|
+
if (this.setAsEmptyObject) {
|
|
3390
|
+
allocateForWrite(0);
|
|
3391
|
+
return pack({});
|
|
3392
|
+
}
|
|
3393
|
+
let array = Array.from(set);
|
|
3394
|
+
let { target: target2, position: position3 } = allocateForWrite(this.moreTypes ? 3 : 0);
|
|
3395
|
+
if (this.moreTypes) {
|
|
3396
|
+
target2[position3++] = 212;
|
|
3397
|
+
target2[position3++] = 115;
|
|
3398
|
+
target2[position3++] = 0;
|
|
3399
|
+
}
|
|
3400
|
+
pack(array);
|
|
3401
|
+
}
|
|
3402
|
+
}, {
|
|
3403
|
+
pack(error, allocateForWrite, pack) {
|
|
3404
|
+
let { target: target2, position: position3 } = allocateForWrite(this.moreTypes ? 3 : 0);
|
|
3405
|
+
if (this.moreTypes) {
|
|
3406
|
+
target2[position3++] = 212;
|
|
3407
|
+
target2[position3++] = 101;
|
|
3408
|
+
target2[position3++] = 0;
|
|
3409
|
+
}
|
|
3410
|
+
pack([error.name, error.message, error.cause]);
|
|
3411
|
+
}
|
|
3412
|
+
}, {
|
|
3413
|
+
pack(regex, allocateForWrite, pack) {
|
|
3414
|
+
let { target: target2, position: position3 } = allocateForWrite(this.moreTypes ? 3 : 0);
|
|
3415
|
+
if (this.moreTypes) {
|
|
3416
|
+
target2[position3++] = 212;
|
|
3417
|
+
target2[position3++] = 120;
|
|
3418
|
+
target2[position3++] = 0;
|
|
3419
|
+
}
|
|
3420
|
+
pack([regex.source, regex.flags]);
|
|
3421
|
+
}
|
|
3422
|
+
}, {
|
|
3423
|
+
pack(arrayBuffer, allocateForWrite) {
|
|
3424
|
+
if (this.moreTypes)
|
|
3425
|
+
writeExtBuffer(arrayBuffer, 16, allocateForWrite);
|
|
3426
|
+
else
|
|
3427
|
+
writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
|
|
3428
|
+
}
|
|
3429
|
+
}, {
|
|
3430
|
+
pack(typedArray, allocateForWrite) {
|
|
3431
|
+
let constructor = typedArray.constructor;
|
|
3432
|
+
if (constructor !== ByteArray && this.moreTypes)
|
|
3433
|
+
writeExtBuffer(typedArray, typedArrays.indexOf(constructor.name), allocateForWrite);
|
|
3434
|
+
else
|
|
3435
|
+
writeBuffer(typedArray, allocateForWrite);
|
|
3436
|
+
}
|
|
3437
|
+
}, {
|
|
3438
|
+
pack(arrayBuffer, allocateForWrite) {
|
|
3439
|
+
if (this.moreTypes)
|
|
3440
|
+
writeExtBuffer(arrayBuffer, 17, allocateForWrite);
|
|
3441
|
+
else
|
|
3442
|
+
writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
|
|
3443
|
+
}
|
|
3444
|
+
}, {
|
|
3445
|
+
pack(c1, allocateForWrite) {
|
|
3446
|
+
let { target: target2, position: position3 } = allocateForWrite(1);
|
|
3447
|
+
target2[position3] = 193;
|
|
3448
|
+
}
|
|
3449
|
+
}];
|
|
3450
|
+
function writeExtBuffer(typedArray, type, allocateForWrite, encode) {
|
|
3451
|
+
let length = typedArray.byteLength;
|
|
3452
|
+
if (length + 1 < 256) {
|
|
3453
|
+
var { target: target2, position: position3 } = allocateForWrite(4 + length);
|
|
3454
|
+
target2[position3++] = 199;
|
|
3455
|
+
target2[position3++] = length + 1;
|
|
3456
|
+
} else if (length + 1 < 65536) {
|
|
3457
|
+
var { target: target2, position: position3 } = allocateForWrite(5 + length);
|
|
3458
|
+
target2[position3++] = 200;
|
|
3459
|
+
target2[position3++] = length + 1 >> 8;
|
|
3460
|
+
target2[position3++] = length + 1 & 255;
|
|
3461
|
+
} else {
|
|
3462
|
+
var { target: target2, position: position3, targetView: targetView2 } = allocateForWrite(7 + length);
|
|
3463
|
+
target2[position3++] = 201;
|
|
3464
|
+
targetView2.setUint32(position3, length + 1);
|
|
3465
|
+
position3 += 4;
|
|
3466
|
+
}
|
|
3467
|
+
target2[position3++] = 116;
|
|
3468
|
+
target2[position3++] = type;
|
|
3469
|
+
if (!typedArray.buffer)
|
|
3470
|
+
typedArray = new Uint8Array(typedArray);
|
|
3471
|
+
target2.set(new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength), position3);
|
|
3472
|
+
}
|
|
3473
|
+
function writeBuffer(buffer, allocateForWrite) {
|
|
3474
|
+
let length = buffer.byteLength;
|
|
3475
|
+
var target2, position3;
|
|
3476
|
+
if (length < 256) {
|
|
3477
|
+
var { target: target2, position: position3 } = allocateForWrite(length + 2);
|
|
3478
|
+
target2[position3++] = 196;
|
|
3479
|
+
target2[position3++] = length;
|
|
3480
|
+
} else if (length < 65536) {
|
|
3481
|
+
var { target: target2, position: position3 } = allocateForWrite(length + 3);
|
|
3482
|
+
target2[position3++] = 197;
|
|
3483
|
+
target2[position3++] = length >> 8;
|
|
3484
|
+
target2[position3++] = length & 255;
|
|
3485
|
+
} else {
|
|
3486
|
+
var { target: target2, position: position3, targetView: targetView2 } = allocateForWrite(length + 5);
|
|
3487
|
+
target2[position3++] = 198;
|
|
3488
|
+
targetView2.setUint32(position3, length);
|
|
3489
|
+
position3 += 4;
|
|
3490
|
+
}
|
|
3491
|
+
target2.set(buffer, position3);
|
|
3492
|
+
}
|
|
3493
|
+
function writeExtensionData(result, target2, position3, type) {
|
|
3494
|
+
let length = result.length;
|
|
3495
|
+
switch (length) {
|
|
3496
|
+
case 1:
|
|
3497
|
+
target2[position3++] = 212;
|
|
3498
|
+
break;
|
|
3499
|
+
case 2:
|
|
3500
|
+
target2[position3++] = 213;
|
|
3501
|
+
break;
|
|
3502
|
+
case 4:
|
|
3503
|
+
target2[position3++] = 214;
|
|
3504
|
+
break;
|
|
3505
|
+
case 8:
|
|
3506
|
+
target2[position3++] = 215;
|
|
3507
|
+
break;
|
|
3508
|
+
case 16:
|
|
3509
|
+
target2[position3++] = 216;
|
|
3510
|
+
break;
|
|
3511
|
+
default:
|
|
3512
|
+
if (length < 256) {
|
|
3513
|
+
target2[position3++] = 199;
|
|
3514
|
+
target2[position3++] = length;
|
|
3515
|
+
} else if (length < 65536) {
|
|
3516
|
+
target2[position3++] = 200;
|
|
3517
|
+
target2[position3++] = length >> 8;
|
|
3518
|
+
target2[position3++] = length & 255;
|
|
3519
|
+
} else {
|
|
3520
|
+
target2[position3++] = 201;
|
|
3521
|
+
target2[position3++] = length >> 24;
|
|
3522
|
+
target2[position3++] = length >> 16 & 255;
|
|
3523
|
+
target2[position3++] = length >> 8 & 255;
|
|
3524
|
+
target2[position3++] = length & 255;
|
|
3525
|
+
}
|
|
3526
|
+
}
|
|
3527
|
+
target2[position3++] = type;
|
|
3528
|
+
target2.set(result, position3);
|
|
3529
|
+
position3 += length;
|
|
3530
|
+
return position3;
|
|
3531
|
+
}
|
|
3532
|
+
function insertIds(serialized, idsToInsert) {
|
|
3533
|
+
let nextId;
|
|
3534
|
+
let distanceToMove = idsToInsert.length * 6;
|
|
3535
|
+
let lastEnd = serialized.length - distanceToMove;
|
|
3536
|
+
while (nextId = idsToInsert.pop()) {
|
|
3537
|
+
let offset = nextId.offset;
|
|
3538
|
+
let id = nextId.id;
|
|
3539
|
+
serialized.copyWithin(offset + distanceToMove, offset, lastEnd);
|
|
3540
|
+
distanceToMove -= 6;
|
|
3541
|
+
let position3 = offset + distanceToMove;
|
|
3542
|
+
serialized[position3++] = 214;
|
|
3543
|
+
serialized[position3++] = 105;
|
|
3544
|
+
serialized[position3++] = id >> 24;
|
|
3545
|
+
serialized[position3++] = id >> 16 & 255;
|
|
3546
|
+
serialized[position3++] = id >> 8 & 255;
|
|
3547
|
+
serialized[position3++] = id & 255;
|
|
3548
|
+
lastEnd = offset;
|
|
3549
|
+
}
|
|
3550
|
+
return serialized;
|
|
3551
|
+
}
|
|
3552
|
+
function writeBundles(start, pack, incrementPosition) {
|
|
3553
|
+
if (bundledStrings2.length > 0) {
|
|
3554
|
+
targetView.setUint32(bundledStrings2.position + start, position2 + incrementPosition - bundledStrings2.position - start);
|
|
3555
|
+
bundledStrings2.stringsPosition = position2 - start;
|
|
3556
|
+
let writeStrings = bundledStrings2;
|
|
3557
|
+
bundledStrings2 = null;
|
|
3558
|
+
pack(writeStrings[0]);
|
|
3559
|
+
pack(writeStrings[1]);
|
|
3560
|
+
}
|
|
3561
|
+
}
|
|
3562
|
+
function prepareStructures(structures, packr) {
|
|
3563
|
+
structures.isCompatible = (existingStructures) => {
|
|
3564
|
+
let compatible = !existingStructures || (packr.lastNamedStructuresLength || 0) === existingStructures.length;
|
|
3565
|
+
if (!compatible)
|
|
3566
|
+
packr._mergeStructures(existingStructures);
|
|
3567
|
+
return compatible;
|
|
3568
|
+
};
|
|
3569
|
+
return structures;
|
|
3570
|
+
}
|
|
3571
|
+
var defaultPackr = new Packr({ useRecords: false });
|
|
3572
|
+
var pack = defaultPackr.pack;
|
|
3573
|
+
var encode = defaultPackr.pack;
|
|
3574
|
+
var REUSE_BUFFER_MODE = 512;
|
|
3575
|
+
var RESET_BUFFER_MODE = 1024;
|
|
3576
|
+
var RESERVE_START_SPACE = 2048;
|
|
3577
|
+
// src/db/RestProxy.ts
|
|
3578
|
+
var isObjectId = (v) => !!(v && typeof v === "object" && v._bsontype === "ObjectId" && typeof v.toString === "function");
|
|
3579
|
+
var isTimestamp = (v) => !!(v && typeof v === "object" && v._bsontype === "Timestamp" && ("high" in v) && ("low" in v));
|
|
3580
|
+
function preprocessForPack(data) {
|
|
3581
|
+
if (data === null || data === undefined)
|
|
3582
|
+
return data;
|
|
3583
|
+
if (isObjectId(data))
|
|
3584
|
+
return data.toString();
|
|
3585
|
+
if (isTimestamp(data))
|
|
3586
|
+
return { t: data.high, i: data.low };
|
|
3587
|
+
if (typeof data !== "object")
|
|
3588
|
+
return data;
|
|
3589
|
+
if (data instanceof Date)
|
|
3590
|
+
return data;
|
|
3591
|
+
if (data instanceof Map || data instanceof Set || data instanceof RegExp)
|
|
3592
|
+
return data;
|
|
3593
|
+
if (Array.isArray(data))
|
|
3594
|
+
return data.map(preprocessForPack);
|
|
3595
|
+
const result = {};
|
|
3596
|
+
for (const key of Object.keys(data)) {
|
|
3597
|
+
result[key] = preprocessForPack(data[key]);
|
|
3598
|
+
}
|
|
3599
|
+
return result;
|
|
3600
|
+
}
|
|
3601
|
+
var packr = new Packr({ structuredClone: true });
|
|
3602
|
+
var unpackr = new Unpackr({ structuredClone: true });
|
|
3603
|
+
var pack2 = (x) => packr.pack(preprocessForPack(x));
|
|
3604
|
+
var unpack2 = (x) => unpackr.unpack(x);
|
|
1321
3605
|
var DEFAULT_TIMEOUT = 5000;
|
|
1322
3606
|
var DEFAULT_PROGRESS_CHUNK_SIZE = 16384;
|
|
1323
3607
|
|
|
@@ -1385,7 +3669,7 @@ class RestProxy {
|
|
|
1385
3669
|
naprava: this.audit.device
|
|
1386
3670
|
}
|
|
1387
3671
|
};
|
|
1388
|
-
const body =
|
|
3672
|
+
const body = pack2(data);
|
|
1389
3673
|
const totalBytes = body.byteLength;
|
|
1390
3674
|
const requestUrl = this.apiKey ? `${this.endpoint}?apikey=${this.apiKey}` : this.endpoint;
|
|
1391
3675
|
if (onProgress) {
|
|
@@ -1416,7 +3700,7 @@ class RestProxy {
|
|
|
1416
3700
|
throw new Error(`REST call failed: ${response.status} - ${errorText}`);
|
|
1417
3701
|
}
|
|
1418
3702
|
const buffer = await response.arrayBuffer();
|
|
1419
|
-
const result =
|
|
3703
|
+
const result = unpack2(new Uint8Array(buffer));
|
|
1420
3704
|
if (this.timeRequests) {
|
|
1421
3705
|
const elapsed = performance.now() - startTime;
|
|
1422
3706
|
this._lastRequestMs = elapsed;
|
|
@@ -1479,7 +3763,7 @@ class RestProxy {
|
|
|
1479
3763
|
}
|
|
1480
3764
|
async updateCollections(collectionsBatches) {
|
|
1481
3765
|
return await this.restCall("updateCollections", {
|
|
1482
|
-
|
|
3766
|
+
collectionsBatches
|
|
1483
3767
|
});
|
|
1484
3768
|
}
|
|
1485
3769
|
}
|