cbor-x 1.3.3 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +347 -346
- package/SECURITY.md +11 -11
- package/benchmark.md +73 -73
- package/browser.js +11 -11
- package/decode.d.ts +2 -46
- package/decode.js +47 -29
- package/dist/index.js +1160 -1134
- package/dist/index.min.js +100 -85
- package/dist/node.cjs +1228 -1202
- package/dist/test.js +26 -1
- package/encode.d.ts +1 -6
- package/encode.js +1041 -1033
- package/index.d.ts +61 -12
- package/index.js +3 -3
- package/iterators.js +85 -85
- package/node-index.js +24 -24
- package/package.json +6 -1
- package/rollup.config.js +45 -45
- package/stream.js +57 -57
- package/webpack.config.js +19 -19
- package/dist/index.js.map +0 -1
- package/dist/node-index.js +0 -93
package/dist/node.cjs
CHANGED
|
@@ -89,7 +89,7 @@ class Decoder {
|
|
|
89
89
|
}
|
|
90
90
|
let res = {};
|
|
91
91
|
//map.forEach((v,k) => res[Object.keys(this._keyMap)[Object.values(this._keyMap).indexOf(k)] || k] = v)
|
|
92
|
-
map.forEach((v,k) => res[this._mapKey.has(k) ? this._mapKey.get(k) : k] = v);
|
|
92
|
+
map.forEach((v,k) => res[safeKey(this._mapKey.has(k) ? this._mapKey.get(k) : k)] = v);
|
|
93
93
|
return res
|
|
94
94
|
}
|
|
95
95
|
|
|
@@ -291,8 +291,8 @@ function read() {
|
|
|
291
291
|
let key;
|
|
292
292
|
if (currentDecoder.mapsAsObjects) {
|
|
293
293
|
let object = {};
|
|
294
|
-
if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) object[currentDecoder.decodeKey(key)] = read();
|
|
295
|
-
else while ((key = read()) != STOP_CODE) object[key] = read();
|
|
294
|
+
if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) object[safeKey(currentDecoder.decodeKey(key))] = read();
|
|
295
|
+
else while ((key = read()) != STOP_CODE) object[safeKey(key)] = read();
|
|
296
296
|
return object
|
|
297
297
|
} else {
|
|
298
298
|
if (restoreMapsAsObject) {
|
|
@@ -340,8 +340,8 @@ function read() {
|
|
|
340
340
|
case 5: // map
|
|
341
341
|
if (currentDecoder.mapsAsObjects) {
|
|
342
342
|
let object = {};
|
|
343
|
-
if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[currentDecoder.decodeKey(read())] = read();
|
|
344
|
-
else for (let i = 0; i < token; i++) object[read()] = read();
|
|
343
|
+
if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
|
|
344
|
+
else for (let i = 0; i < token; i++) object[safeKey(read())] = read();
|
|
345
345
|
return object
|
|
346
346
|
} else {
|
|
347
347
|
if (restoreMapsAsObject) {
|
|
@@ -457,8 +457,8 @@ function createStructureReader(structure) {
|
|
|
457
457
|
if (this.slowReads++ >= 3) { // create a fast compiled reader
|
|
458
458
|
let array = this.length == length ? this : this.slice(0, length);
|
|
459
459
|
compiledReader = currentDecoder.keyMap
|
|
460
|
-
? new Function('r', 'return {' + array.map(k => currentDecoder.decodeKey(k)).map(k => validName.test(k) ? k + ':r()' : ('[' + JSON.stringify(k) + ']:r()')).join(',') + '}')
|
|
461
|
-
: new Function('r', 'return {' + array.map(key => validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}');
|
|
460
|
+
? new Function('r', 'return {' + array.map(k => currentDecoder.decodeKey(k)).map(k => validName.test(k) ? safeKey(k) + ':r()' : ('[' + JSON.stringify(k) + ']:r()')).join(',') + '}')
|
|
461
|
+
: new Function('r', 'return {' + array.map(key => validName.test(key) ? safeKey(key) + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}');
|
|
462
462
|
if (this.compiledReader)
|
|
463
463
|
compiledReader.next = this.compiledReader; // if there is an existing one, we store multiple readers as a linked list because it is usually pretty rare to have multiple readers (of different length) for the same structure
|
|
464
464
|
compiledReader.propertyCount = length;
|
|
@@ -466,14 +466,20 @@ function createStructureReader(structure) {
|
|
|
466
466
|
return compiledReader(read)
|
|
467
467
|
}
|
|
468
468
|
let object = {};
|
|
469
|
-
if (currentDecoder.keyMap) for (let i = 0; i < length; i++) object[currentDecoder.decodeKey(this[i])] = read();
|
|
470
|
-
else for (let i = 0; i < length; i++)
|
|
469
|
+
if (currentDecoder.keyMap) for (let i = 0; i < length; i++) object[safeKey(currentDecoder.decodeKey(this[i]))] = read();
|
|
470
|
+
else for (let i = 0; i < length; i++) {
|
|
471
|
+
object[safeKey(this[i])] = read();
|
|
472
|
+
}
|
|
471
473
|
return object
|
|
472
474
|
}
|
|
473
475
|
structure.slowReads = 0;
|
|
474
476
|
return readObject
|
|
475
477
|
}
|
|
476
478
|
|
|
479
|
+
function safeKey(key) {
|
|
480
|
+
return key === '__proto__' ? '__proto_' : key
|
|
481
|
+
}
|
|
482
|
+
|
|
477
483
|
let readFixedString = readStringJS;
|
|
478
484
|
|
|
479
485
|
exports.isNativeAccelerationEnabled = false;
|
|
@@ -725,18 +731,30 @@ function readBin(length) {
|
|
|
725
731
|
Uint8Array.prototype.slice.call(src, position, position += length) :
|
|
726
732
|
src.subarray(position, position += length)
|
|
727
733
|
}
|
|
728
|
-
|
|
734
|
+
let f32Array = new Float32Array(1);
|
|
735
|
+
let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
|
|
729
736
|
function getFloat16() {
|
|
730
737
|
let byte0 = src[position++];
|
|
731
738
|
let byte1 = src[position++];
|
|
732
|
-
let
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
739
|
+
let exponent = (byte0 & 0x7f) >> 2;
|
|
740
|
+
if (exponent === 0x1f) { // specials
|
|
741
|
+
if (byte1 || (byte0 & 3))
|
|
742
|
+
return NaN;
|
|
743
|
+
return (byte0 & 0x80) ? -Infinity : Infinity;
|
|
744
|
+
}
|
|
745
|
+
if (exponent === 0) { // sub-normals
|
|
746
|
+
// significand with 10 fractional bits and divided by 2^14
|
|
747
|
+
let abs = (((byte0 & 3) << 8) | byte1) / (1 << 24);
|
|
748
|
+
return (byte0 & 0x80) ? -abs : abs
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
u8Array[3] = (byte0 & 0x80) | // sign bit
|
|
752
|
+
((exponent >> 1) + 56); // 4 of 5 of the exponent bits, re-offset-ed
|
|
753
|
+
u8Array[2] = ((byte0 & 7) << 5) | // last exponent bit and first two mantissa bits
|
|
754
|
+
(byte1 >> 3); // next 5 bits of mantissa
|
|
755
|
+
u8Array[1] = byte1 << 5; // last three bits of mantissa
|
|
756
|
+
u8Array[0] = 0;
|
|
757
|
+
return f32Array[0];
|
|
740
758
|
}
|
|
741
759
|
|
|
742
760
|
let keyCache = new Array(4096);
|
|
@@ -748,8 +766,6 @@ class Tag {
|
|
|
748
766
|
}
|
|
749
767
|
}
|
|
750
768
|
|
|
751
|
-
let glbl = typeof self == 'object' ? self : global;
|
|
752
|
-
|
|
753
769
|
currentExtensions[0] = (dateString) => {
|
|
754
770
|
// string date extension
|
|
755
771
|
return new Date(dateString)
|
|
@@ -797,11 +813,11 @@ const recordDefinition = (definition) => {
|
|
|
797
813
|
let object = {};
|
|
798
814
|
if (currentDecoder.keyMap) for (let i = 2,l = definition.length; i < l; i++) {
|
|
799
815
|
let key = currentDecoder.decodeKey(structure[i - 2]);
|
|
800
|
-
object[key] = definition[i];
|
|
816
|
+
object[safeKey(key)] = definition[i];
|
|
801
817
|
}
|
|
802
818
|
else for (let i = 2,l = definition.length; i < l; i++) {
|
|
803
819
|
let key = structure[i - 2];
|
|
804
|
-
object[key] = definition[i];
|
|
820
|
+
object[safeKey(key)] = definition[i];
|
|
805
821
|
}
|
|
806
822
|
return object
|
|
807
823
|
};
|
|
@@ -816,7 +832,7 @@ currentExtensions[15] = (value) => {
|
|
|
816
832
|
return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
|
|
817
833
|
return new Tag(value, 15)
|
|
818
834
|
};
|
|
819
|
-
|
|
835
|
+
let glbl = { Error, RegExp };
|
|
820
836
|
currentExtensions[27] = (data) => { // http://cbor.schmorp.de/generic-object
|
|
821
837
|
return (glbl[data[0]] || Error)(data[1], data[2])
|
|
822
838
|
};
|
|
@@ -941,13 +957,17 @@ currentExtensionRanges.push((tag, input) => {
|
|
|
941
957
|
});
|
|
942
958
|
|
|
943
959
|
const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
|
|
944
|
-
const typedArrays = [
|
|
960
|
+
const typedArrays = [Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
|
|
961
|
+
typeof BigUint64Array == 'undefined' ? { name:'BigUint64Array' } : BigUint64Array, Int8Array, Int16Array, Int32Array,
|
|
962
|
+
typeof BigInt64Array == 'undefined' ? { name:'BigInt64Array' } : BigInt64Array, Float32Array, Float64Array];
|
|
945
963
|
const typedArrayTags = [64, 68, 69, 70, 71, 72, 77, 78, 79, 85, 86];
|
|
946
964
|
for (let i = 0; i < typedArrays.length; i++) {
|
|
947
965
|
registerTypedArray(typedArrays[i], typedArrayTags[i]);
|
|
948
966
|
}
|
|
949
|
-
function registerTypedArray(
|
|
950
|
-
let
|
|
967
|
+
function registerTypedArray(TypedArray, tag) {
|
|
968
|
+
let dvMethod = 'get' + TypedArray.name.slice(0, -5);
|
|
969
|
+
if (typeof TypedArray !== 'function')
|
|
970
|
+
TypedArray = null;
|
|
951
971
|
let bytesPerElement = TypedArray.BYTES_PER_ELEMENT;
|
|
952
972
|
for (let littleEndian = 0; littleEndian < 2; littleEndian++) {
|
|
953
973
|
if (!littleEndian && bytesPerElement == 1)
|
|
@@ -964,7 +984,7 @@ function registerTypedArray(typedArrayId, tag, littleEndian) {
|
|
|
964
984
|
let dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
965
985
|
let elements = buffer.length >> sizeShift;
|
|
966
986
|
let ta = new TypedArray(elements);
|
|
967
|
-
let method = dv[
|
|
987
|
+
let method = dv[dvMethod];
|
|
968
988
|
for (let i = 0; i < elements; i++) {
|
|
969
989
|
ta[i] = method.call(dv, i << sizeShift, littleEndian);
|
|
970
990
|
}
|
|
@@ -1084,1193 +1104,1199 @@ const FLOAT32_OPTIONS = {
|
|
|
1084
1104
|
DECIMAL_ROUND: 3,
|
|
1085
1105
|
DECIMAL_FIT: 4
|
|
1086
1106
|
};
|
|
1087
|
-
let f32Array = new Float32Array(1);
|
|
1088
|
-
let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
|
|
1089
1107
|
function roundFloat32(float32Number) {
|
|
1090
1108
|
f32Array[0] = float32Number;
|
|
1091
1109
|
let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
|
|
1092
1110
|
return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
|
|
1093
1111
|
}
|
|
1094
1112
|
|
|
1095
|
-
let textEncoder;
|
|
1096
|
-
try {
|
|
1097
|
-
textEncoder = new TextEncoder();
|
|
1098
|
-
} catch (error) {}
|
|
1099
|
-
let extensions, extensionClasses;
|
|
1100
|
-
const hasNodeBuffer = typeof Buffer !== 'undefined';
|
|
1101
|
-
const ByteArrayAllocate = hasNodeBuffer ? Buffer.allocUnsafeSlow : Uint8Array;
|
|
1102
|
-
const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
|
|
1103
|
-
const MAX_STRUCTURES = 0x100;
|
|
1104
|
-
const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
|
|
1105
|
-
let target;
|
|
1106
|
-
let targetView;
|
|
1107
|
-
let position$1 = 0;
|
|
1108
|
-
let safeEnd;
|
|
1109
|
-
let bundledStrings$1 = null;
|
|
1110
|
-
const MAX_BUNDLE_SIZE = 0xf000;
|
|
1111
|
-
const hasNonLatin = /[\u0080-\uFFFF]/;
|
|
1112
|
-
const RECORD_SYMBOL = Symbol('record-id');
|
|
1113
|
-
class Encoder extends Decoder {
|
|
1114
|
-
constructor(options) {
|
|
1115
|
-
super(options);
|
|
1116
|
-
this.offset = 0;
|
|
1117
|
-
let start;
|
|
1118
|
-
let sharedStructures;
|
|
1119
|
-
let hasSharedUpdate;
|
|
1120
|
-
let structures;
|
|
1121
|
-
let referenceMap;
|
|
1122
|
-
options = options || {};
|
|
1123
|
-
let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position, maxBytes) {
|
|
1124
|
-
return target.utf8Write(string, position, maxBytes)
|
|
1125
|
-
} : (textEncoder && textEncoder.encodeInto) ?
|
|
1126
|
-
function(string, position) {
|
|
1127
|
-
return textEncoder.encodeInto(string, target.subarray(position)).written
|
|
1128
|
-
} : false;
|
|
1129
|
-
|
|
1130
|
-
let encoder = this;
|
|
1131
|
-
let hasSharedStructures = options.structures || options.saveStructures;
|
|
1132
|
-
let maxSharedStructures = options.maxSharedStructures;
|
|
1133
|
-
if (maxSharedStructures == null)
|
|
1134
|
-
maxSharedStructures = hasSharedStructures ? 128 : 0;
|
|
1135
|
-
if (maxSharedStructures > 8190)
|
|
1136
|
-
throw new Error('Maximum maxSharedStructure is 8190')
|
|
1137
|
-
let isSequential = options.sequential;
|
|
1138
|
-
if (isSequential) {
|
|
1139
|
-
maxSharedStructures = 0;
|
|
1140
|
-
}
|
|
1141
|
-
if (!this.structures)
|
|
1142
|
-
this.structures = [];
|
|
1143
|
-
if (this.saveStructures)
|
|
1144
|
-
this.saveShared = this.saveStructures;
|
|
1145
|
-
let samplingPackedValues, packedObjectMap, sharedValues = options.sharedValues;
|
|
1146
|
-
let sharedPackedObjectMap;
|
|
1147
|
-
if (sharedValues) {
|
|
1148
|
-
sharedPackedObjectMap = Object.create(null);
|
|
1149
|
-
for (let i = 0, l = sharedValues.length; i < l; i++) {
|
|
1150
|
-
sharedPackedObjectMap[sharedValues[i]] = i;
|
|
1151
|
-
}
|
|
1152
|
-
}
|
|
1153
|
-
let recordIdsToRemove = [];
|
|
1154
|
-
let transitionsCount = 0;
|
|
1155
|
-
let serializationsSinceTransitionRebuild = 0;
|
|
1156
|
-
|
|
1157
|
-
this.mapEncode = function(value, encodeOptions) {
|
|
1158
|
-
// Experimental support for premapping keys using _keyMap instad of keyMap - not optiimised yet)
|
|
1159
|
-
if (this._keyMap && !this._mapped) {
|
|
1160
|
-
//console.log('encoding ', value)
|
|
1161
|
-
switch (value.constructor.name) {
|
|
1162
|
-
case 'Array':
|
|
1163
|
-
value = value.map(r => this.encodeKeys(r));
|
|
1164
|
-
break
|
|
1165
|
-
//case 'Map':
|
|
1166
|
-
// value = this.encodeKeys(value)
|
|
1167
|
-
// break
|
|
1168
|
-
}
|
|
1169
|
-
//this._mapped = true
|
|
1170
|
-
}
|
|
1171
|
-
return this.encode(value, encodeOptions)
|
|
1172
|
-
};
|
|
1173
|
-
|
|
1174
|
-
this.encode = function(value, encodeOptions) {
|
|
1175
|
-
if (!target) {
|
|
1176
|
-
target = new ByteArrayAllocate(8192);
|
|
1177
|
-
targetView = new DataView(target.buffer, 0, 8192);
|
|
1178
|
-
position$1 = 0;
|
|
1179
|
-
}
|
|
1180
|
-
safeEnd = target.length - 10;
|
|
1181
|
-
if (safeEnd - position$1 < 0x800) {
|
|
1182
|
-
// don't start too close to the end,
|
|
1183
|
-
target = new ByteArrayAllocate(target.length);
|
|
1184
|
-
targetView = new DataView(target.buffer, 0, target.length);
|
|
1185
|
-
safeEnd = target.length - 10;
|
|
1186
|
-
position$1 = 0;
|
|
1187
|
-
} else if (encodeOptions === REUSE_BUFFER_MODE)
|
|
1188
|
-
position$1 = (position$1 + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
|
|
1189
|
-
start = position$1;
|
|
1190
|
-
if (encoder.useSelfDescribedHeader) {
|
|
1191
|
-
targetView.setUint32(position$1, 0xd9d9f700); // tag two byte, then self-descriptive tag
|
|
1192
|
-
position$1 += 3;
|
|
1193
|
-
}
|
|
1194
|
-
referenceMap = encoder.structuredClone ? new Map() : null;
|
|
1195
|
-
if (encoder.bundleStrings && typeof value !== 'string') {
|
|
1196
|
-
bundledStrings$1 = [];
|
|
1197
|
-
bundledStrings$1.size = Infinity; // force a new bundle start on first string
|
|
1198
|
-
} else
|
|
1199
|
-
bundledStrings$1 = null;
|
|
1200
|
-
|
|
1201
|
-
sharedStructures = encoder.structures;
|
|
1202
|
-
if (sharedStructures) {
|
|
1203
|
-
if (sharedStructures.uninitialized) {
|
|
1204
|
-
let sharedData = encoder.getShared() || {};
|
|
1205
|
-
encoder.structures = sharedStructures = sharedData.structures || [];
|
|
1206
|
-
encoder.sharedVersion = sharedData.version;
|
|
1207
|
-
let sharedValues = encoder.sharedValues = sharedData.packedValues;
|
|
1208
|
-
if (sharedValues) {
|
|
1209
|
-
sharedPackedObjectMap = {};
|
|
1210
|
-
for (let i = 0, l = sharedValues.length; i < l; i++)
|
|
1211
|
-
sharedPackedObjectMap[sharedValues[i]] = i;
|
|
1212
|
-
}
|
|
1213
|
-
}
|
|
1214
|
-
let sharedStructuresLength = sharedStructures.length;
|
|
1215
|
-
if (sharedStructuresLength > maxSharedStructures && !isSequential)
|
|
1216
|
-
sharedStructuresLength = maxSharedStructures;
|
|
1217
|
-
if (!sharedStructures.transitions) {
|
|
1218
|
-
// rebuild our structure transitions
|
|
1219
|
-
sharedStructures.transitions = Object.create(null);
|
|
1220
|
-
for (let i = 0; i < sharedStructuresLength; i++) {
|
|
1221
|
-
let keys = sharedStructures[i];
|
|
1222
|
-
//console.log('shared struct keys:', keys)
|
|
1223
|
-
if (!keys)
|
|
1224
|
-
continue
|
|
1225
|
-
let nextTransition, transition = sharedStructures.transitions;
|
|
1226
|
-
for (let j = 0, l = keys.length; j < l; j++) {
|
|
1227
|
-
if (transition[RECORD_SYMBOL] === undefined)
|
|
1228
|
-
transition[RECORD_SYMBOL] = i;
|
|
1229
|
-
let key = keys[j];
|
|
1230
|
-
nextTransition = transition[key];
|
|
1231
|
-
if (!nextTransition) {
|
|
1232
|
-
nextTransition = transition[key] = Object.create(null);
|
|
1233
|
-
}
|
|
1234
|
-
transition = nextTransition;
|
|
1235
|
-
}
|
|
1236
|
-
transition[RECORD_SYMBOL] = i | 0x100000;
|
|
1237
|
-
}
|
|
1238
|
-
}
|
|
1239
|
-
if (!isSequential)
|
|
1240
|
-
sharedStructures.nextId = sharedStructuresLength;
|
|
1241
|
-
}
|
|
1242
|
-
if (hasSharedUpdate)
|
|
1243
|
-
hasSharedUpdate = false;
|
|
1244
|
-
structures = sharedStructures || [];
|
|
1245
|
-
packedObjectMap = sharedPackedObjectMap;
|
|
1246
|
-
if (options.pack) {
|
|
1247
|
-
let packedValues = new Map();
|
|
1248
|
-
packedValues.values = [];
|
|
1249
|
-
packedValues.encoder = encoder;
|
|
1250
|
-
packedValues.maxValues = options.maxPrivatePackedValues || (sharedPackedObjectMap ? 16 : Infinity);
|
|
1251
|
-
packedValues.objectMap = sharedPackedObjectMap || false;
|
|
1252
|
-
packedValues.samplingPackedValues = samplingPackedValues;
|
|
1253
|
-
findRepetitiveStrings(value, packedValues);
|
|
1254
|
-
if (packedValues.values.length > 0) {
|
|
1255
|
-
target[position$1++] = 0xd8; // one-byte tag
|
|
1256
|
-
target[position$1++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1257
|
-
writeArrayHeader(4);
|
|
1258
|
-
let valuesArray = packedValues.values;
|
|
1259
|
-
encode(valuesArray);
|
|
1260
|
-
writeArrayHeader(0); // prefixes
|
|
1261
|
-
writeArrayHeader(0); // suffixes
|
|
1262
|
-
packedObjectMap = Object.create(sharedPackedObjectMap || null);
|
|
1263
|
-
for (let i = 0, l = valuesArray.length; i < l; i++) {
|
|
1264
|
-
packedObjectMap[valuesArray[i]] = i;
|
|
1265
|
-
}
|
|
1266
|
-
}
|
|
1267
|
-
}
|
|
1268
|
-
try {
|
|
1269
|
-
encode(value);
|
|
1270
|
-
if (bundledStrings$1) {
|
|
1271
|
-
writeBundles(start, encode);
|
|
1272
|
-
}
|
|
1273
|
-
encoder.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
|
|
1274
|
-
if (referenceMap && referenceMap.idsToInsert) {
|
|
1275
|
-
position$1 += referenceMap.idsToInsert.length * 2;
|
|
1276
|
-
if (position$1 > safeEnd)
|
|
1277
|
-
makeRoom(position$1);
|
|
1278
|
-
encoder.offset = position$1;
|
|
1279
|
-
let serialized = insertIds(target.subarray(start, position$1), referenceMap.idsToInsert);
|
|
1280
|
-
referenceMap = null;
|
|
1281
|
-
return serialized
|
|
1282
|
-
}
|
|
1283
|
-
if (encodeOptions & REUSE_BUFFER_MODE) {
|
|
1284
|
-
target.start = start;
|
|
1285
|
-
target.end = position$1;
|
|
1286
|
-
return target
|
|
1287
|
-
}
|
|
1288
|
-
return target.subarray(start, position$1) // position can change if we call encode again in saveShared, so we get the buffer now
|
|
1289
|
-
} finally {
|
|
1290
|
-
if (sharedStructures) {
|
|
1291
|
-
if (serializationsSinceTransitionRebuild < 10)
|
|
1292
|
-
serializationsSinceTransitionRebuild++;
|
|
1293
|
-
if (sharedStructures.length > maxSharedStructures)
|
|
1294
|
-
sharedStructures.length = maxSharedStructures;
|
|
1295
|
-
if (transitionsCount > 10000) {
|
|
1296
|
-
// force a rebuild occasionally after a lot of transitions so it can get cleaned up
|
|
1297
|
-
sharedStructures.transitions = null;
|
|
1298
|
-
serializationsSinceTransitionRebuild = 0;
|
|
1299
|
-
transitionsCount = 0;
|
|
1300
|
-
if (recordIdsToRemove.length > 0)
|
|
1301
|
-
recordIdsToRemove = [];
|
|
1302
|
-
} else if (recordIdsToRemove.length > 0 && !isSequential) {
|
|
1303
|
-
for (let i = 0, l = recordIdsToRemove.length; i < l; i++) {
|
|
1304
|
-
recordIdsToRemove[i][RECORD_SYMBOL] = undefined;
|
|
1305
|
-
}
|
|
1306
|
-
recordIdsToRemove = [];
|
|
1307
|
-
//sharedStructures.nextId = maxSharedStructures
|
|
1308
|
-
}
|
|
1309
|
-
}
|
|
1310
|
-
if (hasSharedUpdate && encoder.saveShared) {
|
|
1311
|
-
if (encoder.structures.length > maxSharedStructures) {
|
|
1312
|
-
encoder.structures = encoder.structures.slice(0, maxSharedStructures);
|
|
1313
|
-
}
|
|
1314
|
-
// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
|
|
1315
|
-
let returnBuffer = target.subarray(start, position$1);
|
|
1316
|
-
if (encoder.updateSharedData() === false)
|
|
1317
|
-
return encoder.encode(value) // re-encode if it fails
|
|
1318
|
-
return returnBuffer
|
|
1319
|
-
}
|
|
1320
|
-
if (encodeOptions & RESET_BUFFER_MODE)
|
|
1321
|
-
position$1 = start;
|
|
1322
|
-
}
|
|
1323
|
-
};
|
|
1324
|
-
this.findCommonStringsToPack = () => {
|
|
1325
|
-
samplingPackedValues = new Map();
|
|
1326
|
-
if (!sharedPackedObjectMap)
|
|
1327
|
-
sharedPackedObjectMap = Object.create(null);
|
|
1328
|
-
return (options) => {
|
|
1329
|
-
let threshold = options && options.threshold || 4;
|
|
1330
|
-
let position = this.pack ? options.maxPrivatePackedValues || 16 : 0;
|
|
1331
|
-
if (!sharedValues)
|
|
1332
|
-
sharedValues = this.sharedValues = [];
|
|
1333
|
-
for (let [ key, status ] of samplingPackedValues) {
|
|
1334
|
-
if (status.count > threshold) {
|
|
1335
|
-
sharedPackedObjectMap[key] = position++;
|
|
1336
|
-
sharedValues.push(key);
|
|
1337
|
-
hasSharedUpdate = true;
|
|
1338
|
-
}
|
|
1339
|
-
}
|
|
1340
|
-
while (this.saveShared && this.updateSharedData() === false) {}
|
|
1341
|
-
samplingPackedValues = null;
|
|
1342
|
-
}
|
|
1343
|
-
};
|
|
1344
|
-
const encode = (value) => {
|
|
1345
|
-
if (position$1 > safeEnd)
|
|
1346
|
-
target = makeRoom(position$1);
|
|
1347
|
-
|
|
1348
|
-
var type = typeof value;
|
|
1349
|
-
var length;
|
|
1350
|
-
if (type === 'string') {
|
|
1351
|
-
if (packedObjectMap) {
|
|
1352
|
-
let packedPosition = packedObjectMap[value];
|
|
1353
|
-
if (packedPosition >= 0) {
|
|
1354
|
-
if (packedPosition < 16)
|
|
1355
|
-
target[position$1++] = packedPosition + 0xe0; // simple values, defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1356
|
-
else {
|
|
1357
|
-
target[position$1++] = 0xc6; // tag 6 defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1358
|
-
if (packedPosition & 1)
|
|
1359
|
-
encode((15 - packedPosition) >> 1);
|
|
1360
|
-
else
|
|
1361
|
-
encode((packedPosition - 16) >> 1);
|
|
1362
|
-
}
|
|
1363
|
-
return
|
|
1364
|
-
/* } else if (packedStatus.serializationId != serializationId) {
|
|
1365
|
-
packedStatus.serializationId = serializationId
|
|
1366
|
-
packedStatus.count = 1
|
|
1367
|
-
if (options.sharedPack) {
|
|
1368
|
-
let sharedCount = packedStatus.sharedCount = (packedStatus.sharedCount || 0) + 1
|
|
1369
|
-
if (shareCount > (options.sharedPack.threshold || 5)) {
|
|
1370
|
-
let sharedPosition = packedStatus.position = packedStatus.nextSharedPosition
|
|
1371
|
-
hasSharedUpdate = true
|
|
1372
|
-
if (sharedPosition < 16)
|
|
1373
|
-
target[position++] = sharedPosition + 0xc0
|
|
1374
|
-
|
|
1375
|
-
}
|
|
1376
|
-
}
|
|
1377
|
-
} // else any in-doc incrementation?*/
|
|
1378
|
-
} else if (samplingPackedValues && !options.pack) {
|
|
1379
|
-
let status = samplingPackedValues.get(value);
|
|
1380
|
-
if (status)
|
|
1381
|
-
status.count++;
|
|
1382
|
-
else
|
|
1383
|
-
samplingPackedValues.set(value, {
|
|
1384
|
-
count: 1,
|
|
1385
|
-
});
|
|
1386
|
-
}
|
|
1387
|
-
}
|
|
1388
|
-
let strLength = value.length;
|
|
1389
|
-
if (bundledStrings$1 && strLength >= 4 && strLength < 0x400) {
|
|
1390
|
-
if ((bundledStrings$1.size += strLength) > MAX_BUNDLE_SIZE) {
|
|
1391
|
-
let extStart;
|
|
1392
|
-
let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
|
|
1393
|
-
if (position$1 + maxBytes > safeEnd)
|
|
1394
|
-
target = makeRoom(position$1 + maxBytes);
|
|
1395
|
-
target[position$1++] = 0xd9; // tag 16-bit
|
|
1396
|
-
target[position$1++] = 0xdf; // tag 0xdff9
|
|
1397
|
-
target[position$1++] = 0xf9;
|
|
1398
|
-
// TODO: If we only have one bundle with any string data, only write one string bundle
|
|
1399
|
-
target[position$1++] = bundledStrings$1.position ? 0x84 : 0x82; // array of 4 or 2 elements depending on if we write bundles
|
|
1400
|
-
target[position$1++] = 0x1a; // 32-bit unsigned int
|
|
1401
|
-
extStart = position$1 - start;
|
|
1402
|
-
position$1 += 4; // reserve for writing bundle reference
|
|
1403
|
-
if (bundledStrings$1.position) {
|
|
1404
|
-
writeBundles(start, encode); // write the last bundles
|
|
1405
|
-
}
|
|
1406
|
-
bundledStrings$1 = ['', '']; // create new ones
|
|
1407
|
-
bundledStrings$1.size = 0;
|
|
1408
|
-
bundledStrings$1.position = extStart;
|
|
1409
|
-
}
|
|
1410
|
-
let twoByte = hasNonLatin.test(value);
|
|
1411
|
-
bundledStrings$1[twoByte ? 0 : 1] += value;
|
|
1412
|
-
target[position$1++] = twoByte ? 0xce : 0xcf;
|
|
1413
|
-
encode(strLength);
|
|
1414
|
-
return
|
|
1415
|
-
}
|
|
1416
|
-
let headerSize;
|
|
1417
|
-
// first we estimate the header size, so we can write to the correct location
|
|
1418
|
-
if (strLength < 0x20) {
|
|
1419
|
-
headerSize = 1;
|
|
1420
|
-
} else if (strLength < 0x100) {
|
|
1421
|
-
headerSize = 2;
|
|
1422
|
-
} else if (strLength < 0x10000) {
|
|
1423
|
-
headerSize = 3;
|
|
1424
|
-
} else {
|
|
1425
|
-
headerSize = 5;
|
|
1426
|
-
}
|
|
1427
|
-
let maxBytes = strLength * 3;
|
|
1428
|
-
if (position$1 + maxBytes > safeEnd)
|
|
1429
|
-
target = makeRoom(position$1 + maxBytes);
|
|
1430
|
-
|
|
1431
|
-
if (strLength < 0x40 || !encodeUtf8) {
|
|
1432
|
-
let i, c1, c2, strPosition = position$1 + headerSize;
|
|
1433
|
-
for (i = 0; i < strLength; i++) {
|
|
1434
|
-
c1 = value.charCodeAt(i);
|
|
1435
|
-
if (c1 < 0x80) {
|
|
1436
|
-
target[strPosition++] = c1;
|
|
1437
|
-
} else if (c1 < 0x800) {
|
|
1438
|
-
target[strPosition++] = c1 >> 6 | 0xc0;
|
|
1439
|
-
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1440
|
-
} else if (
|
|
1441
|
-
(c1 & 0xfc00) === 0xd800 &&
|
|
1442
|
-
((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
|
|
1443
|
-
) {
|
|
1444
|
-
c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
|
|
1445
|
-
i++;
|
|
1446
|
-
target[strPosition++] = c1 >> 18 | 0xf0;
|
|
1447
|
-
target[strPosition++] = c1 >> 12 & 0x3f | 0x80;
|
|
1448
|
-
target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
|
|
1449
|
-
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1450
|
-
} else {
|
|
1451
|
-
target[strPosition++] = c1 >> 12 | 0xe0;
|
|
1452
|
-
target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
|
|
1453
|
-
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1454
|
-
}
|
|
1455
|
-
}
|
|
1456
|
-
length = strPosition - position$1 - headerSize;
|
|
1457
|
-
} else {
|
|
1458
|
-
length = encodeUtf8(value, position$1 + headerSize, maxBytes);
|
|
1459
|
-
}
|
|
1460
|
-
|
|
1461
|
-
if (length < 0x18) {
|
|
1462
|
-
target[position$1++] = 0x60 | length;
|
|
1463
|
-
} else if (length < 0x100) {
|
|
1464
|
-
if (headerSize < 2) {
|
|
1465
|
-
target.copyWithin(position$1 + 2, position$1 + 1, position$1 + 1 + length);
|
|
1466
|
-
}
|
|
1467
|
-
target[position$1++] = 0x78;
|
|
1468
|
-
target[position$1++] = length;
|
|
1469
|
-
} else if (length < 0x10000) {
|
|
1470
|
-
if (headerSize < 3) {
|
|
1471
|
-
target.copyWithin(position$1 + 3, position$1 + 2, position$1 + 2 + length);
|
|
1472
|
-
}
|
|
1473
|
-
target[position$1++] = 0x79;
|
|
1474
|
-
target[position$1++] = length >> 8;
|
|
1475
|
-
target[position$1++] = length & 0xff;
|
|
1476
|
-
} else {
|
|
1477
|
-
if (headerSize < 5) {
|
|
1478
|
-
target.copyWithin(position$1 + 5, position$1 + 3, position$1 + 3 + length);
|
|
1479
|
-
}
|
|
1480
|
-
target[position$1++] = 0x7a;
|
|
1481
|
-
targetView.setUint32(position$1, length);
|
|
1482
|
-
position$1 += 4;
|
|
1483
|
-
}
|
|
1484
|
-
position$1 += length;
|
|
1485
|
-
} else if (type === 'number') {
|
|
1486
|
-
if (value >>> 0 === value) {// positive integer, 32-bit or less
|
|
1487
|
-
// positive uint
|
|
1488
|
-
if (value < 0x18) {
|
|
1489
|
-
target[position$1++] = value;
|
|
1490
|
-
} else if (value < 0x100) {
|
|
1491
|
-
target[position$1++] = 0x18;
|
|
1492
|
-
target[position$1++] = value;
|
|
1493
|
-
} else if (value < 0x10000) {
|
|
1494
|
-
target[position$1++] = 0x19;
|
|
1495
|
-
target[position$1++] = value >> 8;
|
|
1496
|
-
target[position$1++] = value & 0xff;
|
|
1497
|
-
} else {
|
|
1498
|
-
target[position$1++] = 0x1a;
|
|
1499
|
-
targetView.setUint32(position$1, value);
|
|
1500
|
-
position$1 += 4;
|
|
1501
|
-
}
|
|
1502
|
-
} else if (value >> 0 === value) { // negative integer
|
|
1503
|
-
if (value >= -0x18) {
|
|
1504
|
-
target[position$1++] = 0x1f - value;
|
|
1505
|
-
} else if (value >= -0x100) {
|
|
1506
|
-
target[position$1++] = 0x38;
|
|
1507
|
-
target[position$1++] = ~value;
|
|
1508
|
-
} else if (value >= -0x10000) {
|
|
1509
|
-
target[position$1++] = 0x39;
|
|
1510
|
-
targetView.setUint16(position$1, ~value);
|
|
1511
|
-
position$1 += 2;
|
|
1512
|
-
} else {
|
|
1513
|
-
target[position$1++] = 0x3a;
|
|
1514
|
-
targetView.setUint32(position$1, ~value);
|
|
1515
|
-
position$1 += 4;
|
|
1516
|
-
}
|
|
1517
|
-
} else {
|
|
1518
|
-
let useFloat32;
|
|
1519
|
-
if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
|
|
1520
|
-
target[position$1++] = 0xfa;
|
|
1521
|
-
targetView.setFloat32(position$1, value);
|
|
1522
|
-
let xShifted;
|
|
1523
|
-
if (useFloat32 < 4 ||
|
|
1524
|
-
// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
1525
|
-
((xShifted = value * mult10[((target[position$1] & 0x7f) << 1) | (target[position$1 + 1] >> 7)]) >> 0) === xShifted) {
|
|
1526
|
-
position$1 += 4;
|
|
1527
|
-
return
|
|
1528
|
-
} else
|
|
1529
|
-
position$1--; // move back into position for writing a double
|
|
1530
|
-
}
|
|
1531
|
-
target[position$1++] = 0xfb;
|
|
1532
|
-
targetView.setFloat64(position$1, value);
|
|
1533
|
-
position$1 += 8;
|
|
1534
|
-
}
|
|
1535
|
-
} else if (type === 'object') {
|
|
1536
|
-
if (!value)
|
|
1537
|
-
target[position$1++] = 0xf6;
|
|
1538
|
-
else {
|
|
1539
|
-
if (referenceMap) {
|
|
1540
|
-
let referee = referenceMap.get(value);
|
|
1541
|
-
if (referee) {
|
|
1542
|
-
target[position$1++] = 0xd8;
|
|
1543
|
-
target[position$1++] = 29; // http://cbor.schmorp.de/value-sharing
|
|
1544
|
-
target[position$1++] = 0x19; // 16-bit uint
|
|
1545
|
-
if (!referee.references) {
|
|
1546
|
-
let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
|
|
1547
|
-
referee.references = [];
|
|
1548
|
-
idsToInsert.push(referee);
|
|
1549
|
-
}
|
|
1550
|
-
referee.references.push(position$1 - start);
|
|
1551
|
-
position$1 += 2; // TODO: also support 32-bit
|
|
1552
|
-
return
|
|
1553
|
-
} else
|
|
1554
|
-
referenceMap.set(value, { offset: position$1 - start });
|
|
1555
|
-
}
|
|
1556
|
-
let constructor = value.constructor;
|
|
1557
|
-
if (constructor === Object) {
|
|
1558
|
-
writeObject(value, true);
|
|
1559
|
-
} else if (constructor === Array) {
|
|
1560
|
-
length = value.length;
|
|
1561
|
-
if (length < 0x18) {
|
|
1562
|
-
target[position$1++] = 0x80 | length;
|
|
1563
|
-
} else {
|
|
1564
|
-
writeArrayHeader(length);
|
|
1565
|
-
}
|
|
1566
|
-
for (let i = 0; i < length; i++) {
|
|
1567
|
-
encode(value[i]);
|
|
1568
|
-
}
|
|
1569
|
-
} else if (constructor === Map) {
|
|
1570
|
-
if (this.mapsAsObjects ? this.useTag259ForMaps !== false : this.useTag259ForMaps) {
|
|
1571
|
-
// use Tag 259 (https://github.com/shanewholloway/js-cbor-codec/blob/master/docs/CBOR-259-spec--explicit-maps.md) for maps if the user wants it that way
|
|
1572
|
-
target[position$1++] = 0xd9;
|
|
1573
|
-
target[position$1++] = 1;
|
|
1574
|
-
target[position$1++] = 3;
|
|
1575
|
-
}
|
|
1576
|
-
length = value.size;
|
|
1577
|
-
if (length < 0x18) {
|
|
1578
|
-
target[position$1++] = 0xa0 | length;
|
|
1579
|
-
} else if (length < 0x100) {
|
|
1580
|
-
target[position$1++] = 0xb8;
|
|
1581
|
-
target[position$1++] = length;
|
|
1582
|
-
} else if (length < 0x10000) {
|
|
1583
|
-
target[position$1++] = 0xb9;
|
|
1584
|
-
target[position$1++] = length >> 8;
|
|
1585
|
-
target[position$1++] = length & 0xff;
|
|
1586
|
-
} else {
|
|
1587
|
-
target[position$1++] = 0xba;
|
|
1588
|
-
targetView.setUint32(position$1, length);
|
|
1589
|
-
position$1 += 4;
|
|
1590
|
-
}
|
|
1591
|
-
if (encoder.keyMap) {
|
|
1592
|
-
for (let [ key, entryValue ] of value) {
|
|
1593
|
-
encode(encoder.encodeKey(key));
|
|
1594
|
-
encode(entryValue);
|
|
1595
|
-
}
|
|
1596
|
-
} else {
|
|
1597
|
-
for (let [ key, entryValue ] of value) {
|
|
1598
|
-
encode(key);
|
|
1599
|
-
encode(entryValue);
|
|
1600
|
-
}
|
|
1601
|
-
}
|
|
1602
|
-
} else {
|
|
1603
|
-
for (let i = 0, l = extensions.length; i < l; i++) {
|
|
1604
|
-
let extensionClass = extensionClasses[i];
|
|
1605
|
-
if (value instanceof extensionClass) {
|
|
1606
|
-
let extension = extensions[i];
|
|
1607
|
-
let tag = extension.tag;
|
|
1608
|
-
if (tag == undefined)
|
|
1609
|
-
tag = extension.getTag && extension.getTag(value);
|
|
1610
|
-
if (tag < 0x18) {
|
|
1611
|
-
target[position$1++] = 0xc0 | tag;
|
|
1612
|
-
} else if (tag < 0x100) {
|
|
1613
|
-
target[position$1++] = 0xd8;
|
|
1614
|
-
target[position$1++] = tag;
|
|
1615
|
-
} else if (tag < 0x10000) {
|
|
1616
|
-
target[position$1++] = 0xd9;
|
|
1617
|
-
target[position$1++] = tag >> 8;
|
|
1618
|
-
target[position$1++] = tag & 0xff;
|
|
1619
|
-
} else if (tag > -1) {
|
|
1620
|
-
target[position$1++] = 0xda;
|
|
1621
|
-
targetView.setUint32(position$1, tag);
|
|
1622
|
-
position$1 += 4;
|
|
1623
|
-
} // else undefined, don't write tag
|
|
1624
|
-
extension.encode.call(this, value, encode, makeRoom);
|
|
1625
|
-
return
|
|
1626
|
-
}
|
|
1627
|
-
}
|
|
1628
|
-
if (value[Symbol.iterator]) {
|
|
1629
|
-
target[position$1++] = 0x9f; // indefinite length array
|
|
1630
|
-
for (let entry of value) {
|
|
1631
|
-
encode(entry);
|
|
1632
|
-
}
|
|
1633
|
-
target[position$1++] = 0xff; // stop-code
|
|
1634
|
-
return
|
|
1635
|
-
}
|
|
1636
|
-
// no extension found, write as object
|
|
1637
|
-
writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
|
|
1638
|
-
}
|
|
1639
|
-
}
|
|
1640
|
-
} else if (type === 'boolean') {
|
|
1641
|
-
target[position$1++] = value ? 0xf5 : 0xf4;
|
|
1642
|
-
} else if (type === 'bigint') {
|
|
1643
|
-
if (value < (BigInt(1)<<BigInt(64)) && value >= 0) {
|
|
1644
|
-
// use an unsigned int as long as it fits
|
|
1645
|
-
target[position$1++] = 0x1b;
|
|
1646
|
-
targetView.setBigUint64(position$1, value);
|
|
1647
|
-
} else if (value > -(BigInt(1)<<BigInt(64)) && value < 0) {
|
|
1648
|
-
// if we can fit an unsigned int, use that
|
|
1649
|
-
target[position$1++] = 0x3b;
|
|
1650
|
-
targetView.setBigUint64(position$1, -value - BigInt(1));
|
|
1651
|
-
} else {
|
|
1652
|
-
// overflow
|
|
1653
|
-
if (this.largeBigIntToFloat) {
|
|
1654
|
-
target[position$1++] = 0xfb;
|
|
1655
|
-
targetView.setFloat64(position$1, Number(value));
|
|
1656
|
-
} else {
|
|
1657
|
-
throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
|
|
1658
|
-
}
|
|
1659
|
-
}
|
|
1660
|
-
position$1 += 8;
|
|
1661
|
-
} else if (type === 'undefined') {
|
|
1662
|
-
target[position$1++] = 0xf7;
|
|
1663
|
-
} else {
|
|
1664
|
-
throw new Error('Unknown type: ' + type)
|
|
1665
|
-
}
|
|
1666
|
-
};
|
|
1667
|
-
|
|
1668
|
-
const writeObject = this.useRecords === false ? this.variableMapSize ? (object) => {
|
|
1669
|
-
// this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
|
|
1670
|
-
let keys = Object.keys(object);
|
|
1671
|
-
let vals = Object.values(object);
|
|
1672
|
-
let length = keys.length;
|
|
1673
|
-
if (length < 0x18) {
|
|
1674
|
-
target[position$1++] = 0xa0 | length;
|
|
1675
|
-
} else if (length < 0x100) {
|
|
1676
|
-
target[position$1++] = 0xb8;
|
|
1677
|
-
target[position$1++] = length;
|
|
1678
|
-
} else if (length < 0x10000) {
|
|
1679
|
-
target[position$1++] = 0xb9;
|
|
1680
|
-
target[position$1++] = length >> 8;
|
|
1681
|
-
target[position$1++] = length & 0xff;
|
|
1682
|
-
} else {
|
|
1683
|
-
target[position$1++] = 0xba;
|
|
1684
|
-
targetView.setUint32(position$1, length);
|
|
1685
|
-
position$1 += 4;
|
|
1686
|
-
}
|
|
1687
|
-
if (encoder.keyMap) {
|
|
1688
|
-
for (let i = 0; i < length; i++) {
|
|
1689
|
-
encode(encodeKey(keys[i]));
|
|
1690
|
-
encode(vals[i]);
|
|
1691
|
-
}
|
|
1692
|
-
} else {
|
|
1693
|
-
for (let i = 0; i < length; i++) {
|
|
1694
|
-
encode(keys[i]);
|
|
1695
|
-
encode(vals[i]);
|
|
1696
|
-
}
|
|
1697
|
-
}
|
|
1698
|
-
} :
|
|
1699
|
-
(object, safePrototype) => {
|
|
1700
|
-
target[position$1++] = 0xb9; // always use map 16, so we can preallocate and set the length afterwards
|
|
1701
|
-
let objectOffset = position$1 - start;
|
|
1702
|
-
position$1 += 2;
|
|
1703
|
-
let size = 0;
|
|
1704
|
-
if (encoder.keyMap) {
|
|
1705
|
-
for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
|
|
1706
|
-
encode(encoder.encodeKey(key));
|
|
1707
|
-
encode(object[key]);
|
|
1708
|
-
size++;
|
|
1709
|
-
}
|
|
1710
|
-
} else {
|
|
1711
|
-
for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
|
|
1712
|
-
encode(key);
|
|
1713
|
-
encode(object[key]);
|
|
1714
|
-
size++;
|
|
1715
|
-
}
|
|
1716
|
-
}
|
|
1717
|
-
target[objectOffset++ + start] = size >> 8;
|
|
1718
|
-
target[objectOffset + start] = size & 0xff;
|
|
1719
|
-
} :
|
|
1720
|
-
(object, safePrototype) => {
|
|
1721
|
-
let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
|
|
1722
|
-
let newTransitions = 0;
|
|
1723
|
-
let length = 0;
|
|
1724
|
-
let parentRecordId;
|
|
1725
|
-
let keys;
|
|
1726
|
-
if (this.keyMap) {
|
|
1727
|
-
keys = Object.keys(object).map(k => this.encodeKey(k));
|
|
1728
|
-
length = keys.length;
|
|
1729
|
-
for (let i = 0; i < length; i++) {
|
|
1730
|
-
let key = keys[i];
|
|
1731
|
-
nextTransition = transition[key];
|
|
1732
|
-
if (!nextTransition) {
|
|
1733
|
-
nextTransition = transition[key] = Object.create(null);
|
|
1734
|
-
newTransitions++;
|
|
1735
|
-
}
|
|
1736
|
-
transition = nextTransition;
|
|
1737
|
-
}
|
|
1738
|
-
} else {
|
|
1739
|
-
for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
|
|
1740
|
-
nextTransition = transition[key];
|
|
1741
|
-
if (!nextTransition) {
|
|
1742
|
-
if (transition[RECORD_SYMBOL] & 0x100000) {// this indicates it is a brancheable/extendable terminal node, so we will use this record id and extend it
|
|
1743
|
-
parentRecordId = transition[RECORD_SYMBOL] & 0xffff;
|
|
1744
|
-
}
|
|
1745
|
-
nextTransition = transition[key] = Object.create(null);
|
|
1746
|
-
newTransitions++;
|
|
1747
|
-
}
|
|
1748
|
-
transition = nextTransition;
|
|
1749
|
-
length++;
|
|
1750
|
-
}
|
|
1751
|
-
}
|
|
1752
|
-
let recordId = transition[RECORD_SYMBOL];
|
|
1753
|
-
if (recordId !== undefined) {
|
|
1754
|
-
recordId &= 0xffff;
|
|
1755
|
-
target[position$1++] = 0xd9;
|
|
1756
|
-
target[position$1++] = (recordId >> 8) | 0xe0;
|
|
1757
|
-
target[position$1++] = recordId & 0xff;
|
|
1758
|
-
} else {
|
|
1759
|
-
if (!keys)
|
|
1760
|
-
keys = transition.__keys__ || (transition.__keys__ = Object.keys(object));
|
|
1761
|
-
if (parentRecordId === undefined) {
|
|
1762
|
-
recordId = structures.nextId++;
|
|
1763
|
-
if (!recordId) {
|
|
1764
|
-
recordId = 0;
|
|
1765
|
-
structures.nextId = 1;
|
|
1766
|
-
}
|
|
1767
|
-
if (recordId >= MAX_STRUCTURES) {// cycle back around
|
|
1768
|
-
structures.nextId = (recordId = maxSharedStructures) + 1;
|
|
1769
|
-
}
|
|
1770
|
-
} else {
|
|
1771
|
-
recordId = parentRecordId;
|
|
1772
|
-
}
|
|
1773
|
-
structures[recordId] = keys;
|
|
1774
|
-
if (recordId < maxSharedStructures) {
|
|
1775
|
-
target[position$1++] = 0xd9;
|
|
1776
|
-
target[position$1++] = (recordId >> 8) | 0xe0;
|
|
1777
|
-
target[position$1++] = recordId & 0xff;
|
|
1778
|
-
transition = structures.transitions;
|
|
1779
|
-
for (let i = 0; i < length; i++) {
|
|
1780
|
-
if (transition[RECORD_SYMBOL] === undefined || (transition[RECORD_SYMBOL] & 0x100000))
|
|
1781
|
-
transition[RECORD_SYMBOL] = recordId;
|
|
1782
|
-
transition = transition[keys[i]];
|
|
1783
|
-
}
|
|
1784
|
-
transition[RECORD_SYMBOL] = recordId | 0x100000; // indicates it is a extendable terminal
|
|
1785
|
-
hasSharedUpdate = true;
|
|
1786
|
-
} else {
|
|
1787
|
-
transition[RECORD_SYMBOL] = recordId;
|
|
1788
|
-
targetView.setUint32(position$1, 0xd9dfff00); // tag two byte, then record definition id
|
|
1789
|
-
position$1 += 3;
|
|
1790
|
-
if (newTransitions)
|
|
1791
|
-
transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
|
|
1792
|
-
// record the removal of the id, we can maintain our shared structure
|
|
1793
|
-
if (recordIdsToRemove.length >= MAX_STRUCTURES - maxSharedStructures)
|
|
1794
|
-
recordIdsToRemove.shift()[RECORD_SYMBOL] = undefined; // we are cycling back through, and have to remove old ones
|
|
1795
|
-
recordIdsToRemove.push(transition);
|
|
1796
|
-
writeArrayHeader(length + 2);
|
|
1797
|
-
encode(0xe000 + recordId);
|
|
1798
|
-
encode(keys);
|
|
1799
|
-
for (let v of Object.values(object)) encode(v);
|
|
1800
|
-
return
|
|
1801
|
-
}
|
|
1802
|
-
}
|
|
1803
|
-
if (length < 0x18) { // write the array header
|
|
1804
|
-
target[position$1++] = 0x80 | length;
|
|
1805
|
-
} else {
|
|
1806
|
-
writeArrayHeader(length);
|
|
1807
|
-
}
|
|
1808
|
-
for (let key in object)
|
|
1809
|
-
if (safePrototype || object.hasOwnProperty(key))
|
|
1810
|
-
encode(object[key]);
|
|
1811
|
-
};
|
|
1812
|
-
const makeRoom = (end) => {
|
|
1813
|
-
let newSize;
|
|
1814
|
-
if (end > 0x1000000) {
|
|
1815
|
-
// special handling for really large buffers
|
|
1816
|
-
if ((end - start) > MAX_BUFFER_SIZE)
|
|
1817
|
-
throw new Error('Encoded buffer would be larger than maximum buffer size')
|
|
1818
|
-
newSize = Math.min(MAX_BUFFER_SIZE,
|
|
1819
|
-
Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000);
|
|
1820
|
-
} else // faster handling for smaller buffers
|
|
1821
|
-
newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
|
|
1822
|
-
let newBuffer = new ByteArrayAllocate(newSize);
|
|
1823
|
-
targetView = new DataView(newBuffer.buffer, 0, newSize);
|
|
1824
|
-
if (target.copy)
|
|
1825
|
-
target.copy(newBuffer, 0, start, end);
|
|
1826
|
-
else
|
|
1827
|
-
newBuffer.set(target.slice(start, end));
|
|
1828
|
-
position$1 -= start;
|
|
1829
|
-
start = 0;
|
|
1830
|
-
safeEnd = newBuffer.length - 10;
|
|
1831
|
-
return target = newBuffer
|
|
1832
|
-
};
|
|
1833
|
-
}
|
|
1834
|
-
useBuffer(buffer) {
|
|
1835
|
-
// this means we are finished using our own buffer and we can write over it safely
|
|
1836
|
-
target = buffer;
|
|
1837
|
-
targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
|
|
1838
|
-
position$1 = 0;
|
|
1839
|
-
}
|
|
1840
|
-
clearSharedData() {
|
|
1841
|
-
if (this.structures)
|
|
1842
|
-
this.structures = [];
|
|
1843
|
-
if (this.sharedValues)
|
|
1844
|
-
this.sharedValues = undefined;
|
|
1845
|
-
}
|
|
1846
|
-
updateSharedData() {
|
|
1847
|
-
let lastVersion = this.sharedVersion || 0;
|
|
1848
|
-
this.sharedVersion = lastVersion + 1;
|
|
1849
|
-
let structuresCopy = this.structures.slice(0);
|
|
1850
|
-
let sharedData = new SharedData(structuresCopy, this.sharedValues, this.sharedVersion);
|
|
1851
|
-
let saveResults = this.saveShared(sharedData,
|
|
1852
|
-
existingShared => (existingShared && existingShared.version || 0) == lastVersion);
|
|
1853
|
-
if (saveResults === false) {
|
|
1854
|
-
// get updated structures and try again if the update failed
|
|
1855
|
-
sharedData = this.getShared() || {};
|
|
1856
|
-
this.structures = sharedData.structures || [];
|
|
1857
|
-
this.sharedValues = sharedData.packedValues;
|
|
1858
|
-
this.sharedVersion = sharedData.version;
|
|
1859
|
-
this.structures.nextId = this.structures.length;
|
|
1860
|
-
} else {
|
|
1861
|
-
// restore structures
|
|
1862
|
-
structuresCopy.forEach((structure, i) => this.structures[i] = structure);
|
|
1863
|
-
}
|
|
1864
|
-
// saveShared may fail to write and reload, or may have reloaded to check compatibility and overwrite saved data, either way load the correct shared data
|
|
1865
|
-
return saveResults
|
|
1866
|
-
}
|
|
1867
|
-
}
|
|
1868
|
-
class SharedData {
|
|
1869
|
-
constructor(structures, values, version) {
|
|
1870
|
-
this.structures = structures;
|
|
1871
|
-
this.packedValues = values;
|
|
1872
|
-
this.version = version;
|
|
1873
|
-
}
|
|
1874
|
-
}
|
|
1875
|
-
|
|
1876
|
-
function writeArrayHeader(length) {
|
|
1877
|
-
if (length < 0x18)
|
|
1878
|
-
target[position$1++] = 0x80 | length;
|
|
1879
|
-
else if (length < 0x100) {
|
|
1880
|
-
target[position$1++] = 0x98;
|
|
1881
|
-
target[position$1++] = length;
|
|
1882
|
-
} else if (length < 0x10000) {
|
|
1883
|
-
target[position$1++] = 0x99;
|
|
1884
|
-
target[position$1++] = length >> 8;
|
|
1885
|
-
target[position$1++] = length & 0xff;
|
|
1886
|
-
} else {
|
|
1887
|
-
target[position$1++] = 0x9a;
|
|
1888
|
-
targetView.setUint32(position$1, length);
|
|
1889
|
-
position$1 += 4;
|
|
1890
|
-
}
|
|
1891
|
-
}
|
|
1892
|
-
|
|
1893
|
-
function findRepetitiveStrings(value, packedValues) {
|
|
1894
|
-
switch(typeof value) {
|
|
1895
|
-
case 'string':
|
|
1896
|
-
if (value.length > 3) {
|
|
1897
|
-
if (packedValues.objectMap[value] > -1 || packedValues.values.length >= packedValues.maxValues)
|
|
1898
|
-
return
|
|
1899
|
-
let packedStatus = packedValues.get(value);
|
|
1900
|
-
if (packedStatus) {
|
|
1901
|
-
if (++packedStatus.count == 2) {
|
|
1902
|
-
packedValues.values.push(value);
|
|
1903
|
-
}
|
|
1904
|
-
} else {
|
|
1905
|
-
packedValues.set(value, {
|
|
1906
|
-
count: 1,
|
|
1907
|
-
});
|
|
1908
|
-
if (packedValues.samplingPackedValues) {
|
|
1909
|
-
let status = packedValues.samplingPackedValues.get(value);
|
|
1910
|
-
if (status)
|
|
1911
|
-
status.count++;
|
|
1912
|
-
else
|
|
1913
|
-
packedValues.samplingPackedValues.set(value, {
|
|
1914
|
-
count: 1,
|
|
1915
|
-
});
|
|
1916
|
-
}
|
|
1917
|
-
}
|
|
1918
|
-
}
|
|
1919
|
-
break
|
|
1920
|
-
case 'object':
|
|
1921
|
-
if (value) {
|
|
1922
|
-
if (value instanceof Array) {
|
|
1923
|
-
for (let i = 0, l = value.length; i < l; i++) {
|
|
1924
|
-
findRepetitiveStrings(value[i], packedValues);
|
|
1925
|
-
}
|
|
1926
|
-
|
|
1927
|
-
} else {
|
|
1928
|
-
let includeKeys = !packedValues.encoder.useRecords;
|
|
1929
|
-
for (var key in value) {
|
|
1930
|
-
if (value.hasOwnProperty(key)) {
|
|
1931
|
-
if (includeKeys)
|
|
1932
|
-
findRepetitiveStrings(key, packedValues);
|
|
1933
|
-
findRepetitiveStrings(value[key], packedValues);
|
|
1934
|
-
}
|
|
1935
|
-
}
|
|
1936
|
-
}
|
|
1937
|
-
}
|
|
1938
|
-
break
|
|
1939
|
-
case 'function': console.log(value);
|
|
1940
|
-
}
|
|
1941
|
-
}
|
|
1942
|
-
const isLittleEndianMachine$1 = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
|
|
1943
|
-
extensionClasses = [ Date, Set, Error, RegExp, Tag, ArrayBuffer,
|
|
1944
|
-
Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
|
|
1945
|
-
typeof BigUint64Array == 'undefined' ? function() {} : BigUint64Array, Int8Array, Int16Array, Int32Array,
|
|
1946
|
-
typeof BigInt64Array == 'undefined' ? function() {} : BigInt64Array,
|
|
1947
|
-
Float32Array, Float64Array, SharedData];
|
|
1948
|
-
|
|
1949
|
-
//Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/
|
|
1950
|
-
extensions = [{
|
|
1951
|
-
tag: 1,
|
|
1952
|
-
encode(date, encode) {
|
|
1953
|
-
let seconds = date.getTime() / 1000;
|
|
1954
|
-
if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
|
|
1955
|
-
// Timestamp 32
|
|
1956
|
-
target[position$1++] = 0x1a;
|
|
1957
|
-
targetView.setUint32(position$1, seconds);
|
|
1958
|
-
position$1 += 4;
|
|
1959
|
-
} else {
|
|
1960
|
-
// Timestamp float64
|
|
1961
|
-
target[position$1++] = 0xfb;
|
|
1962
|
-
targetView.setFloat64(position$1, seconds);
|
|
1963
|
-
position$1 += 8;
|
|
1964
|
-
}
|
|
1965
|
-
}
|
|
1966
|
-
}, {
|
|
1967
|
-
tag: 258, // https://github.com/input-output-hk/cbor-sets-spec/blob/master/CBOR_SETS.md
|
|
1968
|
-
encode(set, encode) {
|
|
1969
|
-
let array = Array.from(set);
|
|
1970
|
-
encode(array);
|
|
1971
|
-
}
|
|
1972
|
-
}, {
|
|
1973
|
-
tag: 27, // http://cbor.schmorp.de/generic-object
|
|
1974
|
-
encode(error, encode) {
|
|
1975
|
-
encode([ error.name, error.message ]);
|
|
1976
|
-
}
|
|
1977
|
-
}, {
|
|
1978
|
-
tag: 27, // http://cbor.schmorp.de/generic-object
|
|
1979
|
-
encode(regex, encode) {
|
|
1980
|
-
encode([ 'RegExp', regex.source, regex.flags ]);
|
|
1981
|
-
}
|
|
1982
|
-
}, {
|
|
1983
|
-
getTag(tag) {
|
|
1984
|
-
return tag.tag
|
|
1985
|
-
},
|
|
1986
|
-
encode(tag, encode) {
|
|
1987
|
-
encode(tag.value);
|
|
1988
|
-
}
|
|
1989
|
-
}, {
|
|
1990
|
-
encode(arrayBuffer, encode, makeRoom) {
|
|
1991
|
-
writeBuffer(arrayBuffer, makeRoom);
|
|
1992
|
-
}
|
|
1993
|
-
}, {
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
typedArrayEncoder(
|
|
2005
|
-
typedArrayEncoder(
|
|
2006
|
-
typedArrayEncoder(
|
|
2007
|
-
typedArrayEncoder(
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
}
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
target[position$1++] =
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
target[position$1++] =
|
|
2063
|
-
|
|
2064
|
-
position$1
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
}
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
let
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
function
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
1113
|
+
let textEncoder;
|
|
1114
|
+
try {
|
|
1115
|
+
textEncoder = new TextEncoder();
|
|
1116
|
+
} catch (error) {}
|
|
1117
|
+
let extensions, extensionClasses;
|
|
1118
|
+
const hasNodeBuffer = typeof Buffer !== 'undefined';
|
|
1119
|
+
const ByteArrayAllocate = hasNodeBuffer ? Buffer.allocUnsafeSlow : Uint8Array;
|
|
1120
|
+
const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
|
|
1121
|
+
const MAX_STRUCTURES = 0x100;
|
|
1122
|
+
const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
|
|
1123
|
+
let target;
|
|
1124
|
+
let targetView;
|
|
1125
|
+
let position$1 = 0;
|
|
1126
|
+
let safeEnd;
|
|
1127
|
+
let bundledStrings$1 = null;
|
|
1128
|
+
const MAX_BUNDLE_SIZE = 0xf000;
|
|
1129
|
+
const hasNonLatin = /[\u0080-\uFFFF]/;
|
|
1130
|
+
const RECORD_SYMBOL = Symbol('record-id');
|
|
1131
|
+
class Encoder extends Decoder {
|
|
1132
|
+
constructor(options) {
|
|
1133
|
+
super(options);
|
|
1134
|
+
this.offset = 0;
|
|
1135
|
+
let start;
|
|
1136
|
+
let sharedStructures;
|
|
1137
|
+
let hasSharedUpdate;
|
|
1138
|
+
let structures;
|
|
1139
|
+
let referenceMap;
|
|
1140
|
+
options = options || {};
|
|
1141
|
+
let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position, maxBytes) {
|
|
1142
|
+
return target.utf8Write(string, position, maxBytes)
|
|
1143
|
+
} : (textEncoder && textEncoder.encodeInto) ?
|
|
1144
|
+
function(string, position) {
|
|
1145
|
+
return textEncoder.encodeInto(string, target.subarray(position)).written
|
|
1146
|
+
} : false;
|
|
1147
|
+
|
|
1148
|
+
let encoder = this;
|
|
1149
|
+
let hasSharedStructures = options.structures || options.saveStructures;
|
|
1150
|
+
let maxSharedStructures = options.maxSharedStructures;
|
|
1151
|
+
if (maxSharedStructures == null)
|
|
1152
|
+
maxSharedStructures = hasSharedStructures ? 128 : 0;
|
|
1153
|
+
if (maxSharedStructures > 8190)
|
|
1154
|
+
throw new Error('Maximum maxSharedStructure is 8190')
|
|
1155
|
+
let isSequential = options.sequential;
|
|
1156
|
+
if (isSequential) {
|
|
1157
|
+
maxSharedStructures = 0;
|
|
1158
|
+
}
|
|
1159
|
+
if (!this.structures)
|
|
1160
|
+
this.structures = [];
|
|
1161
|
+
if (this.saveStructures)
|
|
1162
|
+
this.saveShared = this.saveStructures;
|
|
1163
|
+
let samplingPackedValues, packedObjectMap, sharedValues = options.sharedValues;
|
|
1164
|
+
let sharedPackedObjectMap;
|
|
1165
|
+
if (sharedValues) {
|
|
1166
|
+
sharedPackedObjectMap = Object.create(null);
|
|
1167
|
+
for (let i = 0, l = sharedValues.length; i < l; i++) {
|
|
1168
|
+
sharedPackedObjectMap[sharedValues[i]] = i;
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
let recordIdsToRemove = [];
|
|
1172
|
+
let transitionsCount = 0;
|
|
1173
|
+
let serializationsSinceTransitionRebuild = 0;
|
|
1174
|
+
|
|
1175
|
+
this.mapEncode = function(value, encodeOptions) {
|
|
1176
|
+
// Experimental support for premapping keys using _keyMap instad of keyMap - not optiimised yet)
|
|
1177
|
+
if (this._keyMap && !this._mapped) {
|
|
1178
|
+
//console.log('encoding ', value)
|
|
1179
|
+
switch (value.constructor.name) {
|
|
1180
|
+
case 'Array':
|
|
1181
|
+
value = value.map(r => this.encodeKeys(r));
|
|
1182
|
+
break
|
|
1183
|
+
//case 'Map':
|
|
1184
|
+
// value = this.encodeKeys(value)
|
|
1185
|
+
// break
|
|
1186
|
+
}
|
|
1187
|
+
//this._mapped = true
|
|
1188
|
+
}
|
|
1189
|
+
return this.encode(value, encodeOptions)
|
|
1190
|
+
};
|
|
1191
|
+
|
|
1192
|
+
this.encode = function(value, encodeOptions) {
|
|
1193
|
+
if (!target) {
|
|
1194
|
+
target = new ByteArrayAllocate(8192);
|
|
1195
|
+
targetView = new DataView(target.buffer, 0, 8192);
|
|
1196
|
+
position$1 = 0;
|
|
1197
|
+
}
|
|
1198
|
+
safeEnd = target.length - 10;
|
|
1199
|
+
if (safeEnd - position$1 < 0x800) {
|
|
1200
|
+
// don't start too close to the end,
|
|
1201
|
+
target = new ByteArrayAllocate(target.length);
|
|
1202
|
+
targetView = new DataView(target.buffer, 0, target.length);
|
|
1203
|
+
safeEnd = target.length - 10;
|
|
1204
|
+
position$1 = 0;
|
|
1205
|
+
} else if (encodeOptions === REUSE_BUFFER_MODE)
|
|
1206
|
+
position$1 = (position$1 + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
|
|
1207
|
+
start = position$1;
|
|
1208
|
+
if (encoder.useSelfDescribedHeader) {
|
|
1209
|
+
targetView.setUint32(position$1, 0xd9d9f700); // tag two byte, then self-descriptive tag
|
|
1210
|
+
position$1 += 3;
|
|
1211
|
+
}
|
|
1212
|
+
referenceMap = encoder.structuredClone ? new Map() : null;
|
|
1213
|
+
if (encoder.bundleStrings && typeof value !== 'string') {
|
|
1214
|
+
bundledStrings$1 = [];
|
|
1215
|
+
bundledStrings$1.size = Infinity; // force a new bundle start on first string
|
|
1216
|
+
} else
|
|
1217
|
+
bundledStrings$1 = null;
|
|
1218
|
+
|
|
1219
|
+
sharedStructures = encoder.structures;
|
|
1220
|
+
if (sharedStructures) {
|
|
1221
|
+
if (sharedStructures.uninitialized) {
|
|
1222
|
+
let sharedData = encoder.getShared() || {};
|
|
1223
|
+
encoder.structures = sharedStructures = sharedData.structures || [];
|
|
1224
|
+
encoder.sharedVersion = sharedData.version;
|
|
1225
|
+
let sharedValues = encoder.sharedValues = sharedData.packedValues;
|
|
1226
|
+
if (sharedValues) {
|
|
1227
|
+
sharedPackedObjectMap = {};
|
|
1228
|
+
for (let i = 0, l = sharedValues.length; i < l; i++)
|
|
1229
|
+
sharedPackedObjectMap[sharedValues[i]] = i;
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
let sharedStructuresLength = sharedStructures.length;
|
|
1233
|
+
if (sharedStructuresLength > maxSharedStructures && !isSequential)
|
|
1234
|
+
sharedStructuresLength = maxSharedStructures;
|
|
1235
|
+
if (!sharedStructures.transitions) {
|
|
1236
|
+
// rebuild our structure transitions
|
|
1237
|
+
sharedStructures.transitions = Object.create(null);
|
|
1238
|
+
for (let i = 0; i < sharedStructuresLength; i++) {
|
|
1239
|
+
let keys = sharedStructures[i];
|
|
1240
|
+
//console.log('shared struct keys:', keys)
|
|
1241
|
+
if (!keys)
|
|
1242
|
+
continue
|
|
1243
|
+
let nextTransition, transition = sharedStructures.transitions;
|
|
1244
|
+
for (let j = 0, l = keys.length; j < l; j++) {
|
|
1245
|
+
if (transition[RECORD_SYMBOL] === undefined)
|
|
1246
|
+
transition[RECORD_SYMBOL] = i;
|
|
1247
|
+
let key = keys[j];
|
|
1248
|
+
nextTransition = transition[key];
|
|
1249
|
+
if (!nextTransition) {
|
|
1250
|
+
nextTransition = transition[key] = Object.create(null);
|
|
1251
|
+
}
|
|
1252
|
+
transition = nextTransition;
|
|
1253
|
+
}
|
|
1254
|
+
transition[RECORD_SYMBOL] = i | 0x100000;
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
if (!isSequential)
|
|
1258
|
+
sharedStructures.nextId = sharedStructuresLength;
|
|
1259
|
+
}
|
|
1260
|
+
if (hasSharedUpdate)
|
|
1261
|
+
hasSharedUpdate = false;
|
|
1262
|
+
structures = sharedStructures || [];
|
|
1263
|
+
packedObjectMap = sharedPackedObjectMap;
|
|
1264
|
+
if (options.pack) {
|
|
1265
|
+
let packedValues = new Map();
|
|
1266
|
+
packedValues.values = [];
|
|
1267
|
+
packedValues.encoder = encoder;
|
|
1268
|
+
packedValues.maxValues = options.maxPrivatePackedValues || (sharedPackedObjectMap ? 16 : Infinity);
|
|
1269
|
+
packedValues.objectMap = sharedPackedObjectMap || false;
|
|
1270
|
+
packedValues.samplingPackedValues = samplingPackedValues;
|
|
1271
|
+
findRepetitiveStrings(value, packedValues);
|
|
1272
|
+
if (packedValues.values.length > 0) {
|
|
1273
|
+
target[position$1++] = 0xd8; // one-byte tag
|
|
1274
|
+
target[position$1++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1275
|
+
writeArrayHeader(4);
|
|
1276
|
+
let valuesArray = packedValues.values;
|
|
1277
|
+
encode(valuesArray);
|
|
1278
|
+
writeArrayHeader(0); // prefixes
|
|
1279
|
+
writeArrayHeader(0); // suffixes
|
|
1280
|
+
packedObjectMap = Object.create(sharedPackedObjectMap || null);
|
|
1281
|
+
for (let i = 0, l = valuesArray.length; i < l; i++) {
|
|
1282
|
+
packedObjectMap[valuesArray[i]] = i;
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
try {
|
|
1287
|
+
encode(value);
|
|
1288
|
+
if (bundledStrings$1) {
|
|
1289
|
+
writeBundles(start, encode);
|
|
1290
|
+
}
|
|
1291
|
+
encoder.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
|
|
1292
|
+
if (referenceMap && referenceMap.idsToInsert) {
|
|
1293
|
+
position$1 += referenceMap.idsToInsert.length * 2;
|
|
1294
|
+
if (position$1 > safeEnd)
|
|
1295
|
+
makeRoom(position$1);
|
|
1296
|
+
encoder.offset = position$1;
|
|
1297
|
+
let serialized = insertIds(target.subarray(start, position$1), referenceMap.idsToInsert);
|
|
1298
|
+
referenceMap = null;
|
|
1299
|
+
return serialized
|
|
1300
|
+
}
|
|
1301
|
+
if (encodeOptions & REUSE_BUFFER_MODE) {
|
|
1302
|
+
target.start = start;
|
|
1303
|
+
target.end = position$1;
|
|
1304
|
+
return target
|
|
1305
|
+
}
|
|
1306
|
+
return target.subarray(start, position$1) // position can change if we call encode again in saveShared, so we get the buffer now
|
|
1307
|
+
} finally {
|
|
1308
|
+
if (sharedStructures) {
|
|
1309
|
+
if (serializationsSinceTransitionRebuild < 10)
|
|
1310
|
+
serializationsSinceTransitionRebuild++;
|
|
1311
|
+
if (sharedStructures.length > maxSharedStructures)
|
|
1312
|
+
sharedStructures.length = maxSharedStructures;
|
|
1313
|
+
if (transitionsCount > 10000) {
|
|
1314
|
+
// force a rebuild occasionally after a lot of transitions so it can get cleaned up
|
|
1315
|
+
sharedStructures.transitions = null;
|
|
1316
|
+
serializationsSinceTransitionRebuild = 0;
|
|
1317
|
+
transitionsCount = 0;
|
|
1318
|
+
if (recordIdsToRemove.length > 0)
|
|
1319
|
+
recordIdsToRemove = [];
|
|
1320
|
+
} else if (recordIdsToRemove.length > 0 && !isSequential) {
|
|
1321
|
+
for (let i = 0, l = recordIdsToRemove.length; i < l; i++) {
|
|
1322
|
+
recordIdsToRemove[i][RECORD_SYMBOL] = undefined;
|
|
1323
|
+
}
|
|
1324
|
+
recordIdsToRemove = [];
|
|
1325
|
+
//sharedStructures.nextId = maxSharedStructures
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
if (hasSharedUpdate && encoder.saveShared) {
|
|
1329
|
+
if (encoder.structures.length > maxSharedStructures) {
|
|
1330
|
+
encoder.structures = encoder.structures.slice(0, maxSharedStructures);
|
|
1331
|
+
}
|
|
1332
|
+
// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
|
|
1333
|
+
let returnBuffer = target.subarray(start, position$1);
|
|
1334
|
+
if (encoder.updateSharedData() === false)
|
|
1335
|
+
return encoder.encode(value) // re-encode if it fails
|
|
1336
|
+
return returnBuffer
|
|
1337
|
+
}
|
|
1338
|
+
if (encodeOptions & RESET_BUFFER_MODE)
|
|
1339
|
+
position$1 = start;
|
|
1340
|
+
}
|
|
1341
|
+
};
|
|
1342
|
+
this.findCommonStringsToPack = () => {
|
|
1343
|
+
samplingPackedValues = new Map();
|
|
1344
|
+
if (!sharedPackedObjectMap)
|
|
1345
|
+
sharedPackedObjectMap = Object.create(null);
|
|
1346
|
+
return (options) => {
|
|
1347
|
+
let threshold = options && options.threshold || 4;
|
|
1348
|
+
let position = this.pack ? options.maxPrivatePackedValues || 16 : 0;
|
|
1349
|
+
if (!sharedValues)
|
|
1350
|
+
sharedValues = this.sharedValues = [];
|
|
1351
|
+
for (let [ key, status ] of samplingPackedValues) {
|
|
1352
|
+
if (status.count > threshold) {
|
|
1353
|
+
sharedPackedObjectMap[key] = position++;
|
|
1354
|
+
sharedValues.push(key);
|
|
1355
|
+
hasSharedUpdate = true;
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
while (this.saveShared && this.updateSharedData() === false) {}
|
|
1359
|
+
samplingPackedValues = null;
|
|
1360
|
+
}
|
|
1361
|
+
};
|
|
1362
|
+
const encode = (value) => {
|
|
1363
|
+
if (position$1 > safeEnd)
|
|
1364
|
+
target = makeRoom(position$1);
|
|
1365
|
+
|
|
1366
|
+
var type = typeof value;
|
|
1367
|
+
var length;
|
|
1368
|
+
if (type === 'string') {
|
|
1369
|
+
if (packedObjectMap) {
|
|
1370
|
+
let packedPosition = packedObjectMap[value];
|
|
1371
|
+
if (packedPosition >= 0) {
|
|
1372
|
+
if (packedPosition < 16)
|
|
1373
|
+
target[position$1++] = packedPosition + 0xe0; // simple values, defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1374
|
+
else {
|
|
1375
|
+
target[position$1++] = 0xc6; // tag 6 defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1376
|
+
if (packedPosition & 1)
|
|
1377
|
+
encode((15 - packedPosition) >> 1);
|
|
1378
|
+
else
|
|
1379
|
+
encode((packedPosition - 16) >> 1);
|
|
1380
|
+
}
|
|
1381
|
+
return
|
|
1382
|
+
/* } else if (packedStatus.serializationId != serializationId) {
|
|
1383
|
+
packedStatus.serializationId = serializationId
|
|
1384
|
+
packedStatus.count = 1
|
|
1385
|
+
if (options.sharedPack) {
|
|
1386
|
+
let sharedCount = packedStatus.sharedCount = (packedStatus.sharedCount || 0) + 1
|
|
1387
|
+
if (shareCount > (options.sharedPack.threshold || 5)) {
|
|
1388
|
+
let sharedPosition = packedStatus.position = packedStatus.nextSharedPosition
|
|
1389
|
+
hasSharedUpdate = true
|
|
1390
|
+
if (sharedPosition < 16)
|
|
1391
|
+
target[position++] = sharedPosition + 0xc0
|
|
1392
|
+
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
} // else any in-doc incrementation?*/
|
|
1396
|
+
} else if (samplingPackedValues && !options.pack) {
|
|
1397
|
+
let status = samplingPackedValues.get(value);
|
|
1398
|
+
if (status)
|
|
1399
|
+
status.count++;
|
|
1400
|
+
else
|
|
1401
|
+
samplingPackedValues.set(value, {
|
|
1402
|
+
count: 1,
|
|
1403
|
+
});
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
let strLength = value.length;
|
|
1407
|
+
if (bundledStrings$1 && strLength >= 4 && strLength < 0x400) {
|
|
1408
|
+
if ((bundledStrings$1.size += strLength) > MAX_BUNDLE_SIZE) {
|
|
1409
|
+
let extStart;
|
|
1410
|
+
let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
|
|
1411
|
+
if (position$1 + maxBytes > safeEnd)
|
|
1412
|
+
target = makeRoom(position$1 + maxBytes);
|
|
1413
|
+
target[position$1++] = 0xd9; // tag 16-bit
|
|
1414
|
+
target[position$1++] = 0xdf; // tag 0xdff9
|
|
1415
|
+
target[position$1++] = 0xf9;
|
|
1416
|
+
// TODO: If we only have one bundle with any string data, only write one string bundle
|
|
1417
|
+
target[position$1++] = bundledStrings$1.position ? 0x84 : 0x82; // array of 4 or 2 elements depending on if we write bundles
|
|
1418
|
+
target[position$1++] = 0x1a; // 32-bit unsigned int
|
|
1419
|
+
extStart = position$1 - start;
|
|
1420
|
+
position$1 += 4; // reserve for writing bundle reference
|
|
1421
|
+
if (bundledStrings$1.position) {
|
|
1422
|
+
writeBundles(start, encode); // write the last bundles
|
|
1423
|
+
}
|
|
1424
|
+
bundledStrings$1 = ['', '']; // create new ones
|
|
1425
|
+
bundledStrings$1.size = 0;
|
|
1426
|
+
bundledStrings$1.position = extStart;
|
|
1427
|
+
}
|
|
1428
|
+
let twoByte = hasNonLatin.test(value);
|
|
1429
|
+
bundledStrings$1[twoByte ? 0 : 1] += value;
|
|
1430
|
+
target[position$1++] = twoByte ? 0xce : 0xcf;
|
|
1431
|
+
encode(strLength);
|
|
1432
|
+
return
|
|
1433
|
+
}
|
|
1434
|
+
let headerSize;
|
|
1435
|
+
// first we estimate the header size, so we can write to the correct location
|
|
1436
|
+
if (strLength < 0x20) {
|
|
1437
|
+
headerSize = 1;
|
|
1438
|
+
} else if (strLength < 0x100) {
|
|
1439
|
+
headerSize = 2;
|
|
1440
|
+
} else if (strLength < 0x10000) {
|
|
1441
|
+
headerSize = 3;
|
|
1442
|
+
} else {
|
|
1443
|
+
headerSize = 5;
|
|
1444
|
+
}
|
|
1445
|
+
let maxBytes = strLength * 3;
|
|
1446
|
+
if (position$1 + maxBytes > safeEnd)
|
|
1447
|
+
target = makeRoom(position$1 + maxBytes);
|
|
1448
|
+
|
|
1449
|
+
if (strLength < 0x40 || !encodeUtf8) {
|
|
1450
|
+
let i, c1, c2, strPosition = position$1 + headerSize;
|
|
1451
|
+
for (i = 0; i < strLength; i++) {
|
|
1452
|
+
c1 = value.charCodeAt(i);
|
|
1453
|
+
if (c1 < 0x80) {
|
|
1454
|
+
target[strPosition++] = c1;
|
|
1455
|
+
} else if (c1 < 0x800) {
|
|
1456
|
+
target[strPosition++] = c1 >> 6 | 0xc0;
|
|
1457
|
+
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1458
|
+
} else if (
|
|
1459
|
+
(c1 & 0xfc00) === 0xd800 &&
|
|
1460
|
+
((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
|
|
1461
|
+
) {
|
|
1462
|
+
c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
|
|
1463
|
+
i++;
|
|
1464
|
+
target[strPosition++] = c1 >> 18 | 0xf0;
|
|
1465
|
+
target[strPosition++] = c1 >> 12 & 0x3f | 0x80;
|
|
1466
|
+
target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
|
|
1467
|
+
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1468
|
+
} else {
|
|
1469
|
+
target[strPosition++] = c1 >> 12 | 0xe0;
|
|
1470
|
+
target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
|
|
1471
|
+
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
length = strPosition - position$1 - headerSize;
|
|
1475
|
+
} else {
|
|
1476
|
+
length = encodeUtf8(value, position$1 + headerSize, maxBytes);
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
if (length < 0x18) {
|
|
1480
|
+
target[position$1++] = 0x60 | length;
|
|
1481
|
+
} else if (length < 0x100) {
|
|
1482
|
+
if (headerSize < 2) {
|
|
1483
|
+
target.copyWithin(position$1 + 2, position$1 + 1, position$1 + 1 + length);
|
|
1484
|
+
}
|
|
1485
|
+
target[position$1++] = 0x78;
|
|
1486
|
+
target[position$1++] = length;
|
|
1487
|
+
} else if (length < 0x10000) {
|
|
1488
|
+
if (headerSize < 3) {
|
|
1489
|
+
target.copyWithin(position$1 + 3, position$1 + 2, position$1 + 2 + length);
|
|
1490
|
+
}
|
|
1491
|
+
target[position$1++] = 0x79;
|
|
1492
|
+
target[position$1++] = length >> 8;
|
|
1493
|
+
target[position$1++] = length & 0xff;
|
|
1494
|
+
} else {
|
|
1495
|
+
if (headerSize < 5) {
|
|
1496
|
+
target.copyWithin(position$1 + 5, position$1 + 3, position$1 + 3 + length);
|
|
1497
|
+
}
|
|
1498
|
+
target[position$1++] = 0x7a;
|
|
1499
|
+
targetView.setUint32(position$1, length);
|
|
1500
|
+
position$1 += 4;
|
|
1501
|
+
}
|
|
1502
|
+
position$1 += length;
|
|
1503
|
+
} else if (type === 'number') {
|
|
1504
|
+
if (value >>> 0 === value) {// positive integer, 32-bit or less
|
|
1505
|
+
// positive uint
|
|
1506
|
+
if (value < 0x18) {
|
|
1507
|
+
target[position$1++] = value;
|
|
1508
|
+
} else if (value < 0x100) {
|
|
1509
|
+
target[position$1++] = 0x18;
|
|
1510
|
+
target[position$1++] = value;
|
|
1511
|
+
} else if (value < 0x10000) {
|
|
1512
|
+
target[position$1++] = 0x19;
|
|
1513
|
+
target[position$1++] = value >> 8;
|
|
1514
|
+
target[position$1++] = value & 0xff;
|
|
1515
|
+
} else {
|
|
1516
|
+
target[position$1++] = 0x1a;
|
|
1517
|
+
targetView.setUint32(position$1, value);
|
|
1518
|
+
position$1 += 4;
|
|
1519
|
+
}
|
|
1520
|
+
} else if (value >> 0 === value) { // negative integer
|
|
1521
|
+
if (value >= -0x18) {
|
|
1522
|
+
target[position$1++] = 0x1f - value;
|
|
1523
|
+
} else if (value >= -0x100) {
|
|
1524
|
+
target[position$1++] = 0x38;
|
|
1525
|
+
target[position$1++] = ~value;
|
|
1526
|
+
} else if (value >= -0x10000) {
|
|
1527
|
+
target[position$1++] = 0x39;
|
|
1528
|
+
targetView.setUint16(position$1, ~value);
|
|
1529
|
+
position$1 += 2;
|
|
1530
|
+
} else {
|
|
1531
|
+
target[position$1++] = 0x3a;
|
|
1532
|
+
targetView.setUint32(position$1, ~value);
|
|
1533
|
+
position$1 += 4;
|
|
1534
|
+
}
|
|
1535
|
+
} else {
|
|
1536
|
+
let useFloat32;
|
|
1537
|
+
if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
|
|
1538
|
+
target[position$1++] = 0xfa;
|
|
1539
|
+
targetView.setFloat32(position$1, value);
|
|
1540
|
+
let xShifted;
|
|
1541
|
+
if (useFloat32 < 4 ||
|
|
1542
|
+
// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
1543
|
+
((xShifted = value * mult10[((target[position$1] & 0x7f) << 1) | (target[position$1 + 1] >> 7)]) >> 0) === xShifted) {
|
|
1544
|
+
position$1 += 4;
|
|
1545
|
+
return
|
|
1546
|
+
} else
|
|
1547
|
+
position$1--; // move back into position for writing a double
|
|
1548
|
+
}
|
|
1549
|
+
target[position$1++] = 0xfb;
|
|
1550
|
+
targetView.setFloat64(position$1, value);
|
|
1551
|
+
position$1 += 8;
|
|
1552
|
+
}
|
|
1553
|
+
} else if (type === 'object') {
|
|
1554
|
+
if (!value)
|
|
1555
|
+
target[position$1++] = 0xf6;
|
|
1556
|
+
else {
|
|
1557
|
+
if (referenceMap) {
|
|
1558
|
+
let referee = referenceMap.get(value);
|
|
1559
|
+
if (referee) {
|
|
1560
|
+
target[position$1++] = 0xd8;
|
|
1561
|
+
target[position$1++] = 29; // http://cbor.schmorp.de/value-sharing
|
|
1562
|
+
target[position$1++] = 0x19; // 16-bit uint
|
|
1563
|
+
if (!referee.references) {
|
|
1564
|
+
let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
|
|
1565
|
+
referee.references = [];
|
|
1566
|
+
idsToInsert.push(referee);
|
|
1567
|
+
}
|
|
1568
|
+
referee.references.push(position$1 - start);
|
|
1569
|
+
position$1 += 2; // TODO: also support 32-bit
|
|
1570
|
+
return
|
|
1571
|
+
} else
|
|
1572
|
+
referenceMap.set(value, { offset: position$1 - start });
|
|
1573
|
+
}
|
|
1574
|
+
let constructor = value.constructor;
|
|
1575
|
+
if (constructor === Object) {
|
|
1576
|
+
writeObject(value, true);
|
|
1577
|
+
} else if (constructor === Array) {
|
|
1578
|
+
length = value.length;
|
|
1579
|
+
if (length < 0x18) {
|
|
1580
|
+
target[position$1++] = 0x80 | length;
|
|
1581
|
+
} else {
|
|
1582
|
+
writeArrayHeader(length);
|
|
1583
|
+
}
|
|
1584
|
+
for (let i = 0; i < length; i++) {
|
|
1585
|
+
encode(value[i]);
|
|
1586
|
+
}
|
|
1587
|
+
} else if (constructor === Map) {
|
|
1588
|
+
if (this.mapsAsObjects ? this.useTag259ForMaps !== false : this.useTag259ForMaps) {
|
|
1589
|
+
// use Tag 259 (https://github.com/shanewholloway/js-cbor-codec/blob/master/docs/CBOR-259-spec--explicit-maps.md) for maps if the user wants it that way
|
|
1590
|
+
target[position$1++] = 0xd9;
|
|
1591
|
+
target[position$1++] = 1;
|
|
1592
|
+
target[position$1++] = 3;
|
|
1593
|
+
}
|
|
1594
|
+
length = value.size;
|
|
1595
|
+
if (length < 0x18) {
|
|
1596
|
+
target[position$1++] = 0xa0 | length;
|
|
1597
|
+
} else if (length < 0x100) {
|
|
1598
|
+
target[position$1++] = 0xb8;
|
|
1599
|
+
target[position$1++] = length;
|
|
1600
|
+
} else if (length < 0x10000) {
|
|
1601
|
+
target[position$1++] = 0xb9;
|
|
1602
|
+
target[position$1++] = length >> 8;
|
|
1603
|
+
target[position$1++] = length & 0xff;
|
|
1604
|
+
} else {
|
|
1605
|
+
target[position$1++] = 0xba;
|
|
1606
|
+
targetView.setUint32(position$1, length);
|
|
1607
|
+
position$1 += 4;
|
|
1608
|
+
}
|
|
1609
|
+
if (encoder.keyMap) {
|
|
1610
|
+
for (let [ key, entryValue ] of value) {
|
|
1611
|
+
encode(encoder.encodeKey(key));
|
|
1612
|
+
encode(entryValue);
|
|
1613
|
+
}
|
|
1614
|
+
} else {
|
|
1615
|
+
for (let [ key, entryValue ] of value) {
|
|
1616
|
+
encode(key);
|
|
1617
|
+
encode(entryValue);
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
} else {
|
|
1621
|
+
for (let i = 0, l = extensions.length; i < l; i++) {
|
|
1622
|
+
let extensionClass = extensionClasses[i];
|
|
1623
|
+
if (value instanceof extensionClass) {
|
|
1624
|
+
let extension = extensions[i];
|
|
1625
|
+
let tag = extension.tag;
|
|
1626
|
+
if (tag == undefined)
|
|
1627
|
+
tag = extension.getTag && extension.getTag.call(this, value);
|
|
1628
|
+
if (tag < 0x18) {
|
|
1629
|
+
target[position$1++] = 0xc0 | tag;
|
|
1630
|
+
} else if (tag < 0x100) {
|
|
1631
|
+
target[position$1++] = 0xd8;
|
|
1632
|
+
target[position$1++] = tag;
|
|
1633
|
+
} else if (tag < 0x10000) {
|
|
1634
|
+
target[position$1++] = 0xd9;
|
|
1635
|
+
target[position$1++] = tag >> 8;
|
|
1636
|
+
target[position$1++] = tag & 0xff;
|
|
1637
|
+
} else if (tag > -1) {
|
|
1638
|
+
target[position$1++] = 0xda;
|
|
1639
|
+
targetView.setUint32(position$1, tag);
|
|
1640
|
+
position$1 += 4;
|
|
1641
|
+
} // else undefined, don't write tag
|
|
1642
|
+
extension.encode.call(this, value, encode, makeRoom);
|
|
1643
|
+
return
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
if (value[Symbol.iterator]) {
|
|
1647
|
+
target[position$1++] = 0x9f; // indefinite length array
|
|
1648
|
+
for (let entry of value) {
|
|
1649
|
+
encode(entry);
|
|
1650
|
+
}
|
|
1651
|
+
target[position$1++] = 0xff; // stop-code
|
|
1652
|
+
return
|
|
1653
|
+
}
|
|
1654
|
+
// no extension found, write as object
|
|
1655
|
+
writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
} else if (type === 'boolean') {
|
|
1659
|
+
target[position$1++] = value ? 0xf5 : 0xf4;
|
|
1660
|
+
} else if (type === 'bigint') {
|
|
1661
|
+
if (value < (BigInt(1)<<BigInt(64)) && value >= 0) {
|
|
1662
|
+
// use an unsigned int as long as it fits
|
|
1663
|
+
target[position$1++] = 0x1b;
|
|
1664
|
+
targetView.setBigUint64(position$1, value);
|
|
1665
|
+
} else if (value > -(BigInt(1)<<BigInt(64)) && value < 0) {
|
|
1666
|
+
// if we can fit an unsigned int, use that
|
|
1667
|
+
target[position$1++] = 0x3b;
|
|
1668
|
+
targetView.setBigUint64(position$1, -value - BigInt(1));
|
|
1669
|
+
} else {
|
|
1670
|
+
// overflow
|
|
1671
|
+
if (this.largeBigIntToFloat) {
|
|
1672
|
+
target[position$1++] = 0xfb;
|
|
1673
|
+
targetView.setFloat64(position$1, Number(value));
|
|
1674
|
+
} else {
|
|
1675
|
+
throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
position$1 += 8;
|
|
1679
|
+
} else if (type === 'undefined') {
|
|
1680
|
+
target[position$1++] = 0xf7;
|
|
1681
|
+
} else {
|
|
1682
|
+
throw new Error('Unknown type: ' + type)
|
|
1683
|
+
}
|
|
1684
|
+
};
|
|
1685
|
+
|
|
1686
|
+
const writeObject = this.useRecords === false ? this.variableMapSize ? (object) => {
|
|
1687
|
+
// this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
|
|
1688
|
+
let keys = Object.keys(object);
|
|
1689
|
+
let vals = Object.values(object);
|
|
1690
|
+
let length = keys.length;
|
|
1691
|
+
if (length < 0x18) {
|
|
1692
|
+
target[position$1++] = 0xa0 | length;
|
|
1693
|
+
} else if (length < 0x100) {
|
|
1694
|
+
target[position$1++] = 0xb8;
|
|
1695
|
+
target[position$1++] = length;
|
|
1696
|
+
} else if (length < 0x10000) {
|
|
1697
|
+
target[position$1++] = 0xb9;
|
|
1698
|
+
target[position$1++] = length >> 8;
|
|
1699
|
+
target[position$1++] = length & 0xff;
|
|
1700
|
+
} else {
|
|
1701
|
+
target[position$1++] = 0xba;
|
|
1702
|
+
targetView.setUint32(position$1, length);
|
|
1703
|
+
position$1 += 4;
|
|
1704
|
+
}
|
|
1705
|
+
if (encoder.keyMap) {
|
|
1706
|
+
for (let i = 0; i < length; i++) {
|
|
1707
|
+
encode(encodeKey(keys[i]));
|
|
1708
|
+
encode(vals[i]);
|
|
1709
|
+
}
|
|
1710
|
+
} else {
|
|
1711
|
+
for (let i = 0; i < length; i++) {
|
|
1712
|
+
encode(keys[i]);
|
|
1713
|
+
encode(vals[i]);
|
|
1714
|
+
}
|
|
1715
|
+
}
|
|
1716
|
+
} :
|
|
1717
|
+
(object, safePrototype) => {
|
|
1718
|
+
target[position$1++] = 0xb9; // always use map 16, so we can preallocate and set the length afterwards
|
|
1719
|
+
let objectOffset = position$1 - start;
|
|
1720
|
+
position$1 += 2;
|
|
1721
|
+
let size = 0;
|
|
1722
|
+
if (encoder.keyMap) {
|
|
1723
|
+
for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
|
|
1724
|
+
encode(encoder.encodeKey(key));
|
|
1725
|
+
encode(object[key]);
|
|
1726
|
+
size++;
|
|
1727
|
+
}
|
|
1728
|
+
} else {
|
|
1729
|
+
for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
|
|
1730
|
+
encode(key);
|
|
1731
|
+
encode(object[key]);
|
|
1732
|
+
size++;
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
target[objectOffset++ + start] = size >> 8;
|
|
1736
|
+
target[objectOffset + start] = size & 0xff;
|
|
1737
|
+
} :
|
|
1738
|
+
(object, safePrototype) => {
|
|
1739
|
+
let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
|
|
1740
|
+
let newTransitions = 0;
|
|
1741
|
+
let length = 0;
|
|
1742
|
+
let parentRecordId;
|
|
1743
|
+
let keys;
|
|
1744
|
+
if (this.keyMap) {
|
|
1745
|
+
keys = Object.keys(object).map(k => this.encodeKey(k));
|
|
1746
|
+
length = keys.length;
|
|
1747
|
+
for (let i = 0; i < length; i++) {
|
|
1748
|
+
let key = keys[i];
|
|
1749
|
+
nextTransition = transition[key];
|
|
1750
|
+
if (!nextTransition) {
|
|
1751
|
+
nextTransition = transition[key] = Object.create(null);
|
|
1752
|
+
newTransitions++;
|
|
1753
|
+
}
|
|
1754
|
+
transition = nextTransition;
|
|
1755
|
+
}
|
|
1756
|
+
} else {
|
|
1757
|
+
for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
|
|
1758
|
+
nextTransition = transition[key];
|
|
1759
|
+
if (!nextTransition) {
|
|
1760
|
+
if (transition[RECORD_SYMBOL] & 0x100000) {// this indicates it is a brancheable/extendable terminal node, so we will use this record id and extend it
|
|
1761
|
+
parentRecordId = transition[RECORD_SYMBOL] & 0xffff;
|
|
1762
|
+
}
|
|
1763
|
+
nextTransition = transition[key] = Object.create(null);
|
|
1764
|
+
newTransitions++;
|
|
1765
|
+
}
|
|
1766
|
+
transition = nextTransition;
|
|
1767
|
+
length++;
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
let recordId = transition[RECORD_SYMBOL];
|
|
1771
|
+
if (recordId !== undefined) {
|
|
1772
|
+
recordId &= 0xffff;
|
|
1773
|
+
target[position$1++] = 0xd9;
|
|
1774
|
+
target[position$1++] = (recordId >> 8) | 0xe0;
|
|
1775
|
+
target[position$1++] = recordId & 0xff;
|
|
1776
|
+
} else {
|
|
1777
|
+
if (!keys)
|
|
1778
|
+
keys = transition.__keys__ || (transition.__keys__ = Object.keys(object));
|
|
1779
|
+
if (parentRecordId === undefined) {
|
|
1780
|
+
recordId = structures.nextId++;
|
|
1781
|
+
if (!recordId) {
|
|
1782
|
+
recordId = 0;
|
|
1783
|
+
structures.nextId = 1;
|
|
1784
|
+
}
|
|
1785
|
+
if (recordId >= MAX_STRUCTURES) {// cycle back around
|
|
1786
|
+
structures.nextId = (recordId = maxSharedStructures) + 1;
|
|
1787
|
+
}
|
|
1788
|
+
} else {
|
|
1789
|
+
recordId = parentRecordId;
|
|
1790
|
+
}
|
|
1791
|
+
structures[recordId] = keys;
|
|
1792
|
+
if (recordId < maxSharedStructures) {
|
|
1793
|
+
target[position$1++] = 0xd9;
|
|
1794
|
+
target[position$1++] = (recordId >> 8) | 0xe0;
|
|
1795
|
+
target[position$1++] = recordId & 0xff;
|
|
1796
|
+
transition = structures.transitions;
|
|
1797
|
+
for (let i = 0; i < length; i++) {
|
|
1798
|
+
if (transition[RECORD_SYMBOL] === undefined || (transition[RECORD_SYMBOL] & 0x100000))
|
|
1799
|
+
transition[RECORD_SYMBOL] = recordId;
|
|
1800
|
+
transition = transition[keys[i]];
|
|
1801
|
+
}
|
|
1802
|
+
transition[RECORD_SYMBOL] = recordId | 0x100000; // indicates it is a extendable terminal
|
|
1803
|
+
hasSharedUpdate = true;
|
|
1804
|
+
} else {
|
|
1805
|
+
transition[RECORD_SYMBOL] = recordId;
|
|
1806
|
+
targetView.setUint32(position$1, 0xd9dfff00); // tag two byte, then record definition id
|
|
1807
|
+
position$1 += 3;
|
|
1808
|
+
if (newTransitions)
|
|
1809
|
+
transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
|
|
1810
|
+
// record the removal of the id, we can maintain our shared structure
|
|
1811
|
+
if (recordIdsToRemove.length >= MAX_STRUCTURES - maxSharedStructures)
|
|
1812
|
+
recordIdsToRemove.shift()[RECORD_SYMBOL] = undefined; // we are cycling back through, and have to remove old ones
|
|
1813
|
+
recordIdsToRemove.push(transition);
|
|
1814
|
+
writeArrayHeader(length + 2);
|
|
1815
|
+
encode(0xe000 + recordId);
|
|
1816
|
+
encode(keys);
|
|
1817
|
+
for (let v of Object.values(object)) encode(v);
|
|
1818
|
+
return
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
if (length < 0x18) { // write the array header
|
|
1822
|
+
target[position$1++] = 0x80 | length;
|
|
1823
|
+
} else {
|
|
1824
|
+
writeArrayHeader(length);
|
|
1825
|
+
}
|
|
1826
|
+
for (let key in object)
|
|
1827
|
+
if (safePrototype || object.hasOwnProperty(key))
|
|
1828
|
+
encode(object[key]);
|
|
1829
|
+
};
|
|
1830
|
+
const makeRoom = (end) => {
|
|
1831
|
+
let newSize;
|
|
1832
|
+
if (end > 0x1000000) {
|
|
1833
|
+
// special handling for really large buffers
|
|
1834
|
+
if ((end - start) > MAX_BUFFER_SIZE)
|
|
1835
|
+
throw new Error('Encoded buffer would be larger than maximum buffer size')
|
|
1836
|
+
newSize = Math.min(MAX_BUFFER_SIZE,
|
|
1837
|
+
Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000);
|
|
1838
|
+
} else // faster handling for smaller buffers
|
|
1839
|
+
newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
|
|
1840
|
+
let newBuffer = new ByteArrayAllocate(newSize);
|
|
1841
|
+
targetView = new DataView(newBuffer.buffer, 0, newSize);
|
|
1842
|
+
if (target.copy)
|
|
1843
|
+
target.copy(newBuffer, 0, start, end);
|
|
1844
|
+
else
|
|
1845
|
+
newBuffer.set(target.slice(start, end));
|
|
1846
|
+
position$1 -= start;
|
|
1847
|
+
start = 0;
|
|
1848
|
+
safeEnd = newBuffer.length - 10;
|
|
1849
|
+
return target = newBuffer
|
|
1850
|
+
};
|
|
1851
|
+
}
|
|
1852
|
+
useBuffer(buffer) {
|
|
1853
|
+
// this means we are finished using our own buffer and we can write over it safely
|
|
1854
|
+
target = buffer;
|
|
1855
|
+
targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
|
|
1856
|
+
position$1 = 0;
|
|
1857
|
+
}
|
|
1858
|
+
clearSharedData() {
|
|
1859
|
+
if (this.structures)
|
|
1860
|
+
this.structures = [];
|
|
1861
|
+
if (this.sharedValues)
|
|
1862
|
+
this.sharedValues = undefined;
|
|
1863
|
+
}
|
|
1864
|
+
updateSharedData() {
|
|
1865
|
+
let lastVersion = this.sharedVersion || 0;
|
|
1866
|
+
this.sharedVersion = lastVersion + 1;
|
|
1867
|
+
let structuresCopy = this.structures.slice(0);
|
|
1868
|
+
let sharedData = new SharedData(structuresCopy, this.sharedValues, this.sharedVersion);
|
|
1869
|
+
let saveResults = this.saveShared(sharedData,
|
|
1870
|
+
existingShared => (existingShared && existingShared.version || 0) == lastVersion);
|
|
1871
|
+
if (saveResults === false) {
|
|
1872
|
+
// get updated structures and try again if the update failed
|
|
1873
|
+
sharedData = this.getShared() || {};
|
|
1874
|
+
this.structures = sharedData.structures || [];
|
|
1875
|
+
this.sharedValues = sharedData.packedValues;
|
|
1876
|
+
this.sharedVersion = sharedData.version;
|
|
1877
|
+
this.structures.nextId = this.structures.length;
|
|
1878
|
+
} else {
|
|
1879
|
+
// restore structures
|
|
1880
|
+
structuresCopy.forEach((structure, i) => this.structures[i] = structure);
|
|
1881
|
+
}
|
|
1882
|
+
// saveShared may fail to write and reload, or may have reloaded to check compatibility and overwrite saved data, either way load the correct shared data
|
|
1883
|
+
return saveResults
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
class SharedData {
|
|
1887
|
+
constructor(structures, values, version) {
|
|
1888
|
+
this.structures = structures;
|
|
1889
|
+
this.packedValues = values;
|
|
1890
|
+
this.version = version;
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
function writeArrayHeader(length) {
|
|
1895
|
+
if (length < 0x18)
|
|
1896
|
+
target[position$1++] = 0x80 | length;
|
|
1897
|
+
else if (length < 0x100) {
|
|
1898
|
+
target[position$1++] = 0x98;
|
|
1899
|
+
target[position$1++] = length;
|
|
1900
|
+
} else if (length < 0x10000) {
|
|
1901
|
+
target[position$1++] = 0x99;
|
|
1902
|
+
target[position$1++] = length >> 8;
|
|
1903
|
+
target[position$1++] = length & 0xff;
|
|
1904
|
+
} else {
|
|
1905
|
+
target[position$1++] = 0x9a;
|
|
1906
|
+
targetView.setUint32(position$1, length);
|
|
1907
|
+
position$1 += 4;
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1911
|
+
function findRepetitiveStrings(value, packedValues) {
|
|
1912
|
+
switch(typeof value) {
|
|
1913
|
+
case 'string':
|
|
1914
|
+
if (value.length > 3) {
|
|
1915
|
+
if (packedValues.objectMap[value] > -1 || packedValues.values.length >= packedValues.maxValues)
|
|
1916
|
+
return
|
|
1917
|
+
let packedStatus = packedValues.get(value);
|
|
1918
|
+
if (packedStatus) {
|
|
1919
|
+
if (++packedStatus.count == 2) {
|
|
1920
|
+
packedValues.values.push(value);
|
|
1921
|
+
}
|
|
1922
|
+
} else {
|
|
1923
|
+
packedValues.set(value, {
|
|
1924
|
+
count: 1,
|
|
1925
|
+
});
|
|
1926
|
+
if (packedValues.samplingPackedValues) {
|
|
1927
|
+
let status = packedValues.samplingPackedValues.get(value);
|
|
1928
|
+
if (status)
|
|
1929
|
+
status.count++;
|
|
1930
|
+
else
|
|
1931
|
+
packedValues.samplingPackedValues.set(value, {
|
|
1932
|
+
count: 1,
|
|
1933
|
+
});
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
break
|
|
1938
|
+
case 'object':
|
|
1939
|
+
if (value) {
|
|
1940
|
+
if (value instanceof Array) {
|
|
1941
|
+
for (let i = 0, l = value.length; i < l; i++) {
|
|
1942
|
+
findRepetitiveStrings(value[i], packedValues);
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
} else {
|
|
1946
|
+
let includeKeys = !packedValues.encoder.useRecords;
|
|
1947
|
+
for (var key in value) {
|
|
1948
|
+
if (value.hasOwnProperty(key)) {
|
|
1949
|
+
if (includeKeys)
|
|
1950
|
+
findRepetitiveStrings(key, packedValues);
|
|
1951
|
+
findRepetitiveStrings(value[key], packedValues);
|
|
1952
|
+
}
|
|
1953
|
+
}
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
break
|
|
1957
|
+
case 'function': console.log(value);
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1960
|
+
const isLittleEndianMachine$1 = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
|
|
1961
|
+
extensionClasses = [ Date, Set, Error, RegExp, Tag, ArrayBuffer,
|
|
1962
|
+
Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
|
|
1963
|
+
typeof BigUint64Array == 'undefined' ? function() {} : BigUint64Array, Int8Array, Int16Array, Int32Array,
|
|
1964
|
+
typeof BigInt64Array == 'undefined' ? function() {} : BigInt64Array,
|
|
1965
|
+
Float32Array, Float64Array, SharedData ];
|
|
1966
|
+
|
|
1967
|
+
//Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/
|
|
1968
|
+
extensions = [{ // Date
|
|
1969
|
+
tag: 1,
|
|
1970
|
+
encode(date, encode) {
|
|
1971
|
+
let seconds = date.getTime() / 1000;
|
|
1972
|
+
if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
|
|
1973
|
+
// Timestamp 32
|
|
1974
|
+
target[position$1++] = 0x1a;
|
|
1975
|
+
targetView.setUint32(position$1, seconds);
|
|
1976
|
+
position$1 += 4;
|
|
1977
|
+
} else {
|
|
1978
|
+
// Timestamp float64
|
|
1979
|
+
target[position$1++] = 0xfb;
|
|
1980
|
+
targetView.setFloat64(position$1, seconds);
|
|
1981
|
+
position$1 += 8;
|
|
1982
|
+
}
|
|
1983
|
+
}
|
|
1984
|
+
}, { // Set
|
|
1985
|
+
tag: 258, // https://github.com/input-output-hk/cbor-sets-spec/blob/master/CBOR_SETS.md
|
|
1986
|
+
encode(set, encode) {
|
|
1987
|
+
let array = Array.from(set);
|
|
1988
|
+
encode(array);
|
|
1989
|
+
}
|
|
1990
|
+
}, { // Error
|
|
1991
|
+
tag: 27, // http://cbor.schmorp.de/generic-object
|
|
1992
|
+
encode(error, encode) {
|
|
1993
|
+
encode([ error.name, error.message ]);
|
|
1994
|
+
}
|
|
1995
|
+
}, { // RegExp
|
|
1996
|
+
tag: 27, // http://cbor.schmorp.de/generic-object
|
|
1997
|
+
encode(regex, encode) {
|
|
1998
|
+
encode([ 'RegExp', regex.source, regex.flags ]);
|
|
1999
|
+
}
|
|
2000
|
+
}, { // Tag
|
|
2001
|
+
getTag(tag) {
|
|
2002
|
+
return tag.tag
|
|
2003
|
+
},
|
|
2004
|
+
encode(tag, encode) {
|
|
2005
|
+
encode(tag.value);
|
|
2006
|
+
}
|
|
2007
|
+
}, { // ArrayBuffer
|
|
2008
|
+
encode(arrayBuffer, encode, makeRoom) {
|
|
2009
|
+
writeBuffer(arrayBuffer, makeRoom);
|
|
2010
|
+
}
|
|
2011
|
+
}, { // Uint8Array
|
|
2012
|
+
getTag(typedArray) {
|
|
2013
|
+
if (typedArray.constructor === Uint8Array) {
|
|
2014
|
+
if (this.tagUint8Array || hasNodeBuffer && this.tagUint8Array !== false)
|
|
2015
|
+
return 64;
|
|
2016
|
+
} // else no tag
|
|
2017
|
+
},
|
|
2018
|
+
encode(typedArray, encode, makeRoom) {
|
|
2019
|
+
writeBuffer(typedArray, makeRoom);
|
|
2020
|
+
}
|
|
2021
|
+
},
|
|
2022
|
+
typedArrayEncoder(68, 1),
|
|
2023
|
+
typedArrayEncoder(69, 2),
|
|
2024
|
+
typedArrayEncoder(70, 4),
|
|
2025
|
+
typedArrayEncoder(71, 8),
|
|
2026
|
+
typedArrayEncoder(72, 1),
|
|
2027
|
+
typedArrayEncoder(77, 2),
|
|
2028
|
+
typedArrayEncoder(78, 4),
|
|
2029
|
+
typedArrayEncoder(79, 8),
|
|
2030
|
+
typedArrayEncoder(85, 4),
|
|
2031
|
+
typedArrayEncoder(86, 8),
|
|
2032
|
+
{
|
|
2033
|
+
encode(sharedData, encode) { // write SharedData
|
|
2034
|
+
let packedValues = sharedData.packedValues || [];
|
|
2035
|
+
let sharedStructures = sharedData.structures || [];
|
|
2036
|
+
if (packedValues.values.length > 0) {
|
|
2037
|
+
target[position$1++] = 0xd8; // one-byte tag
|
|
2038
|
+
target[position$1++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
2039
|
+
writeArrayHeader(4);
|
|
2040
|
+
let valuesArray = packedValues.values;
|
|
2041
|
+
encode(valuesArray);
|
|
2042
|
+
writeArrayHeader(0); // prefixes
|
|
2043
|
+
writeArrayHeader(0); // suffixes
|
|
2044
|
+
packedObjectMap = Object.create(sharedPackedObjectMap || null);
|
|
2045
|
+
for (let i = 0, l = valuesArray.length; i < l; i++) {
|
|
2046
|
+
packedObjectMap[valuesArray[i]] = i;
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
if (sharedStructures) {
|
|
2050
|
+
targetView.setUint32(position$1, 0xd9dffe00);
|
|
2051
|
+
position$1 += 3;
|
|
2052
|
+
let definitions = sharedStructures.slice(0);
|
|
2053
|
+
definitions.unshift(0xe000);
|
|
2054
|
+
definitions.push(new Tag(sharedData.version, 0x53687264));
|
|
2055
|
+
encode(definitions);
|
|
2056
|
+
} else
|
|
2057
|
+
encode(new Tag(sharedData.version, 0x53687264));
|
|
2058
|
+
}
|
|
2059
|
+
}];
|
|
2060
|
+
function typedArrayEncoder(tag, size) {
|
|
2061
|
+
if (!isLittleEndianMachine$1 && size > 1)
|
|
2062
|
+
tag -= 4; // the big endian equivalents are 4 less
|
|
2063
|
+
return {
|
|
2064
|
+
tag: tag,
|
|
2065
|
+
encode: function writeExtBuffer(typedArray, encode) {
|
|
2066
|
+
let length = typedArray.byteLength;
|
|
2067
|
+
let offset = typedArray.byteOffset || 0;
|
|
2068
|
+
let buffer = typedArray.buffer || typedArray;
|
|
2069
|
+
encode(hasNodeBuffer ? Buffer.from(buffer, offset, length) :
|
|
2070
|
+
new Uint8Array(buffer, offset, length));
|
|
2071
|
+
}
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
function writeBuffer(buffer, makeRoom) {
|
|
2075
|
+
let length = buffer.byteLength;
|
|
2076
|
+
if (length < 0x18) {
|
|
2077
|
+
target[position$1++] = 0x40 + length;
|
|
2078
|
+
} else if (length < 0x100) {
|
|
2079
|
+
target[position$1++] = 0x58;
|
|
2080
|
+
target[position$1++] = length;
|
|
2081
|
+
} else if (length < 0x10000) {
|
|
2082
|
+
target[position$1++] = 0x59;
|
|
2083
|
+
target[position$1++] = length >> 8;
|
|
2084
|
+
target[position$1++] = length & 0xff;
|
|
2085
|
+
} else {
|
|
2086
|
+
target[position$1++] = 0x5a;
|
|
2087
|
+
targetView.setUint32(position$1, length);
|
|
2088
|
+
position$1 += 4;
|
|
2089
|
+
}
|
|
2090
|
+
if (position$1 + length >= target.length) {
|
|
2091
|
+
makeRoom(position$1 + length);
|
|
2092
|
+
}
|
|
2093
|
+
// if it is already a typed array (has an ArrayBuffer), use that, but if it is an ArrayBuffer itself,
|
|
2094
|
+
// must wrap it to set it.
|
|
2095
|
+
target.set(buffer.buffer ? buffer : new Uint8Array(buffer), position$1);
|
|
2096
|
+
position$1 += length;
|
|
2097
|
+
}
|
|
2098
|
+
|
|
2099
|
+
function insertIds(serialized, idsToInsert) {
|
|
2100
|
+
// insert the ids that need to be referenced for structured clones
|
|
2101
|
+
let nextId;
|
|
2102
|
+
let distanceToMove = idsToInsert.length * 2;
|
|
2103
|
+
let lastEnd = serialized.length - distanceToMove;
|
|
2104
|
+
idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
|
|
2105
|
+
for (let id = 0; id < idsToInsert.length; id++) {
|
|
2106
|
+
let referee = idsToInsert[id];
|
|
2107
|
+
referee.id = id;
|
|
2108
|
+
for (let position of referee.references) {
|
|
2109
|
+
serialized[position++] = id >> 8;
|
|
2110
|
+
serialized[position] = id & 0xff;
|
|
2111
|
+
}
|
|
2112
|
+
}
|
|
2113
|
+
while (nextId = idsToInsert.pop()) {
|
|
2114
|
+
let offset = nextId.offset;
|
|
2115
|
+
serialized.copyWithin(offset + distanceToMove, offset, lastEnd);
|
|
2116
|
+
distanceToMove -= 2;
|
|
2117
|
+
let position = offset + distanceToMove;
|
|
2118
|
+
serialized[position++] = 0xd8;
|
|
2119
|
+
serialized[position++] = 28; // http://cbor.schmorp.de/value-sharing
|
|
2120
|
+
lastEnd = offset;
|
|
2121
|
+
}
|
|
2122
|
+
return serialized
|
|
2123
|
+
}
|
|
2124
|
+
function writeBundles(start, encode) {
|
|
2125
|
+
targetView.setUint32(bundledStrings$1.position + start, position$1 - bundledStrings$1.position - start + 1); // the offset to bundle
|
|
2126
|
+
let writeStrings = bundledStrings$1;
|
|
2127
|
+
bundledStrings$1 = null;
|
|
2128
|
+
encode(writeStrings[0]);
|
|
2129
|
+
encode(writeStrings[1]);
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
function addExtension$1(extension) {
|
|
2133
|
+
if (extension.Class) {
|
|
2134
|
+
if (!extension.encode)
|
|
2135
|
+
throw new Error('Extension has no encode function')
|
|
2136
|
+
extensionClasses.unshift(extension.Class);
|
|
2137
|
+
extensions.unshift(extension);
|
|
2138
|
+
}
|
|
2139
|
+
addExtension(extension);
|
|
2140
|
+
}
|
|
2141
|
+
let defaultEncoder = new Encoder({ useRecords: false });
|
|
2142
|
+
const encode = defaultEncoder.encode;
|
|
2143
|
+
const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
|
|
2144
|
+
const REUSE_BUFFER_MODE = 512;
|
|
2119
2145
|
const RESET_BUFFER_MODE = 1024;
|
|
2120
2146
|
|
|
2121
|
-
class EncoderStream extends stream.Transform {
|
|
2122
|
-
constructor(options) {
|
|
2123
|
-
if (!options)
|
|
2124
|
-
options = {};
|
|
2125
|
-
options.writableObjectMode = true;
|
|
2126
|
-
super(options);
|
|
2127
|
-
options.sequential = true;
|
|
2128
|
-
this.encoder = options.encoder || new Encoder(options);
|
|
2129
|
-
}
|
|
2130
|
-
_transform(value, encoding, callback) {
|
|
2131
|
-
this.push(this.encoder.encode(value));
|
|
2132
|
-
callback();
|
|
2133
|
-
}
|
|
2134
|
-
}
|
|
2135
|
-
|
|
2136
|
-
class DecoderStream extends stream.Transform {
|
|
2137
|
-
constructor(options) {
|
|
2138
|
-
if (!options)
|
|
2139
|
-
options = {};
|
|
2140
|
-
options.objectMode = true;
|
|
2141
|
-
super(options);
|
|
2142
|
-
options.structures = [];
|
|
2143
|
-
this.decoder = options.decoder || new Decoder(options);
|
|
2144
|
-
}
|
|
2145
|
-
_transform(chunk, encoding, callback) {
|
|
2146
|
-
if (this.incompleteBuffer) {
|
|
2147
|
-
chunk = Buffer.concat([this.incompleteBuffer, chunk]);
|
|
2148
|
-
this.incompleteBuffer = null;
|
|
2149
|
-
}
|
|
2150
|
-
let values;
|
|
2151
|
-
try {
|
|
2152
|
-
values = this.decoder.decodeMultiple(chunk);
|
|
2153
|
-
} catch(error) {
|
|
2154
|
-
if (error.incomplete) {
|
|
2155
|
-
this.incompleteBuffer = chunk.slice(error.lastPosition);
|
|
2156
|
-
values = error.values;
|
|
2157
|
-
}
|
|
2158
|
-
else
|
|
2159
|
-
throw error
|
|
2160
|
-
} finally {
|
|
2161
|
-
for (let value of values || []) {
|
|
2162
|
-
if (value === null)
|
|
2163
|
-
value = this.getNullValue();
|
|
2164
|
-
this.push(value);
|
|
2165
|
-
}
|
|
2166
|
-
}
|
|
2167
|
-
if (callback) callback();
|
|
2168
|
-
}
|
|
2169
|
-
getNullValue() {
|
|
2170
|
-
return Symbol.for(null)
|
|
2171
|
-
}
|
|
2147
|
+
class EncoderStream extends stream.Transform {
|
|
2148
|
+
constructor(options) {
|
|
2149
|
+
if (!options)
|
|
2150
|
+
options = {};
|
|
2151
|
+
options.writableObjectMode = true;
|
|
2152
|
+
super(options);
|
|
2153
|
+
options.sequential = true;
|
|
2154
|
+
this.encoder = options.encoder || new Encoder(options);
|
|
2155
|
+
}
|
|
2156
|
+
_transform(value, encoding, callback) {
|
|
2157
|
+
this.push(this.encoder.encode(value));
|
|
2158
|
+
callback();
|
|
2159
|
+
}
|
|
2172
2160
|
}
|
|
2173
2161
|
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
}
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
}
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
* @param {Iterable|Iterator|AsyncIterable|AsyncIterableIterator} bufferIterator
|
|
2211
|
-
* @param {object} [options] - Decoder options
|
|
2212
|
-
* @returns {IterableIterator|Promise.<AsyncIterableIterator}
|
|
2213
|
-
*/
|
|
2214
|
-
function decodeIter (bufferIterator, options = {}) {
|
|
2215
|
-
if (!bufferIterator || typeof bufferIterator !== 'object') {
|
|
2216
|
-
throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise')
|
|
2217
|
-
}
|
|
2218
|
-
|
|
2219
|
-
const decoder = new Decoder(options);
|
|
2220
|
-
let incomplete;
|
|
2221
|
-
const parser = (chunk) => {
|
|
2222
|
-
let yields;
|
|
2223
|
-
// if there's incomplete data from previous chunk, concatinate and try again
|
|
2224
|
-
if (incomplete) {
|
|
2225
|
-
chunk = Buffer.concat([incomplete, chunk]);
|
|
2226
|
-
incomplete = undefined;
|
|
2227
|
-
}
|
|
2228
|
-
|
|
2229
|
-
try {
|
|
2230
|
-
yields = decoder.decodeMultiple(chunk);
|
|
2231
|
-
} catch (err) {
|
|
2232
|
-
if (err.incomplete) {
|
|
2233
|
-
incomplete = chunk.slice(err.lastPosition);
|
|
2234
|
-
yields = err.values;
|
|
2235
|
-
} else {
|
|
2236
|
-
throw err
|
|
2237
|
-
}
|
|
2238
|
-
}
|
|
2239
|
-
return yields
|
|
2240
|
-
};
|
|
2241
|
-
|
|
2242
|
-
if (typeof bufferIterator[Symbol.iterator] === 'function') {
|
|
2243
|
-
return (function * iter () {
|
|
2244
|
-
for (const value of bufferIterator) {
|
|
2245
|
-
yield * parser(value);
|
|
2246
|
-
}
|
|
2247
|
-
})()
|
|
2248
|
-
} else if (typeof bufferIterator[Symbol.asyncIterator] === 'function') {
|
|
2249
|
-
return (async function * iter () {
|
|
2250
|
-
for await (const value of bufferIterator) {
|
|
2251
|
-
yield * parser(value);
|
|
2252
|
-
}
|
|
2253
|
-
})()
|
|
2254
|
-
}
|
|
2162
|
+
class DecoderStream extends stream.Transform {
|
|
2163
|
+
constructor(options) {
|
|
2164
|
+
if (!options)
|
|
2165
|
+
options = {};
|
|
2166
|
+
options.objectMode = true;
|
|
2167
|
+
super(options);
|
|
2168
|
+
options.structures = [];
|
|
2169
|
+
this.decoder = options.decoder || new Decoder(options);
|
|
2170
|
+
}
|
|
2171
|
+
_transform(chunk, encoding, callback) {
|
|
2172
|
+
if (this.incompleteBuffer) {
|
|
2173
|
+
chunk = Buffer.concat([this.incompleteBuffer, chunk]);
|
|
2174
|
+
this.incompleteBuffer = null;
|
|
2175
|
+
}
|
|
2176
|
+
let values;
|
|
2177
|
+
try {
|
|
2178
|
+
values = this.decoder.decodeMultiple(chunk);
|
|
2179
|
+
} catch(error) {
|
|
2180
|
+
if (error.incomplete) {
|
|
2181
|
+
this.incompleteBuffer = chunk.slice(error.lastPosition);
|
|
2182
|
+
values = error.values;
|
|
2183
|
+
}
|
|
2184
|
+
else
|
|
2185
|
+
throw error
|
|
2186
|
+
} finally {
|
|
2187
|
+
for (let value of values || []) {
|
|
2188
|
+
if (value === null)
|
|
2189
|
+
value = this.getNullValue();
|
|
2190
|
+
this.push(value);
|
|
2191
|
+
}
|
|
2192
|
+
}
|
|
2193
|
+
if (callback) callback();
|
|
2194
|
+
}
|
|
2195
|
+
getNullValue() {
|
|
2196
|
+
return Symbol.for(null)
|
|
2197
|
+
}
|
|
2255
2198
|
}
|
|
2256
2199
|
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2200
|
+
/**
|
|
2201
|
+
* Given an Iterable first argument, returns an Iterable where each value is encoded as a Buffer
|
|
2202
|
+
* If the argument is only Async Iterable, the return value will be an Async Iterable.
|
|
2203
|
+
* @param {Iterable|Iterator|AsyncIterable|AsyncIterator} objectIterator - iterable source, like a Readable object stream, an array, Set, or custom object
|
|
2204
|
+
* @param {options} [options] - cbor-x Encoder options
|
|
2205
|
+
* @returns {IterableIterator|Promise.<AsyncIterableIterator>}
|
|
2206
|
+
*/
|
|
2207
|
+
function encodeIter (objectIterator, options = {}) {
|
|
2208
|
+
if (!objectIterator || typeof objectIterator !== 'object') {
|
|
2209
|
+
throw new Error('first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable')
|
|
2210
|
+
} else if (typeof objectIterator[Symbol.iterator] === 'function') {
|
|
2211
|
+
return encodeIterSync(objectIterator, options)
|
|
2212
|
+
} else if (typeof objectIterator.then === 'function' || typeof objectIterator[Symbol.asyncIterator] === 'function') {
|
|
2213
|
+
return encodeIterAsync(objectIterator, options)
|
|
2214
|
+
} else {
|
|
2215
|
+
throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise')
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
function * encodeIterSync (objectIterator, options) {
|
|
2220
|
+
const encoder = new Encoder(options);
|
|
2221
|
+
for (const value of objectIterator) {
|
|
2222
|
+
yield encoder.encode(value);
|
|
2223
|
+
}
|
|
2224
|
+
}
|
|
2225
|
+
|
|
2226
|
+
async function * encodeIterAsync (objectIterator, options) {
|
|
2227
|
+
const encoder = new Encoder(options);
|
|
2228
|
+
for await (const value of objectIterator) {
|
|
2229
|
+
yield encoder.encode(value);
|
|
2230
|
+
}
|
|
2231
|
+
}
|
|
2232
|
+
|
|
2233
|
+
/**
|
|
2234
|
+
* Given an Iterable/Iterator input which yields buffers, returns an IterableIterator which yields sync decoded objects
|
|
2235
|
+
* Or, given an Async Iterable/Iterator which yields promises resolving in buffers, returns an AsyncIterableIterator.
|
|
2236
|
+
* @param {Iterable|Iterator|AsyncIterable|AsyncIterableIterator} bufferIterator
|
|
2237
|
+
* @param {object} [options] - Decoder options
|
|
2238
|
+
* @returns {IterableIterator|Promise.<AsyncIterableIterator}
|
|
2239
|
+
*/
|
|
2240
|
+
function decodeIter (bufferIterator, options = {}) {
|
|
2241
|
+
if (!bufferIterator || typeof bufferIterator !== 'object') {
|
|
2242
|
+
throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise')
|
|
2243
|
+
}
|
|
2244
|
+
|
|
2245
|
+
const decoder = new Decoder(options);
|
|
2246
|
+
let incomplete;
|
|
2247
|
+
const parser = (chunk) => {
|
|
2248
|
+
let yields;
|
|
2249
|
+
// if there's incomplete data from previous chunk, concatinate and try again
|
|
2250
|
+
if (incomplete) {
|
|
2251
|
+
chunk = Buffer.concat([incomplete, chunk]);
|
|
2252
|
+
incomplete = undefined;
|
|
2253
|
+
}
|
|
2254
|
+
|
|
2255
|
+
try {
|
|
2256
|
+
yields = decoder.decodeMultiple(chunk);
|
|
2257
|
+
} catch (err) {
|
|
2258
|
+
if (err.incomplete) {
|
|
2259
|
+
incomplete = chunk.slice(err.lastPosition);
|
|
2260
|
+
yields = err.values;
|
|
2261
|
+
} else {
|
|
2262
|
+
throw err
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
return yields
|
|
2266
|
+
};
|
|
2267
|
+
|
|
2268
|
+
if (typeof bufferIterator[Symbol.iterator] === 'function') {
|
|
2269
|
+
return (function * iter () {
|
|
2270
|
+
for (const value of bufferIterator) {
|
|
2271
|
+
yield * parser(value);
|
|
2272
|
+
}
|
|
2273
|
+
})()
|
|
2274
|
+
} else if (typeof bufferIterator[Symbol.asyncIterator] === 'function') {
|
|
2275
|
+
return (async function * iter () {
|
|
2276
|
+
for await (const value of bufferIterator) {
|
|
2277
|
+
yield * parser(value);
|
|
2278
|
+
}
|
|
2279
|
+
})()
|
|
2280
|
+
}
|
|
2281
|
+
}
|
|
2282
|
+
|
|
2283
|
+
const useRecords = false;
|
|
2284
|
+
const mapsAsObjects = true;
|
|
2285
|
+
|
|
2286
|
+
const nativeAccelerationDisabled = process.env.CBOR_NATIVE_ACCELERATION_DISABLED !== undefined && process.env.CBOR_NATIVE_ACCELERATION_DISABLED.toLowerCase() === 'true';
|
|
2287
|
+
|
|
2288
|
+
if (!nativeAccelerationDisabled) {
|
|
2289
|
+
let extractor;
|
|
2290
|
+
try {
|
|
2291
|
+
if (typeof require == 'function')
|
|
2292
|
+
extractor = require('cbor-extract');
|
|
2293
|
+
else
|
|
2294
|
+
extractor = module$1.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('node.cjs', document.baseURI).href)))('cbor-extract');
|
|
2295
|
+
if (extractor)
|
|
2296
|
+
setExtractor(extractor.extractStrings);
|
|
2297
|
+
} catch (error) {
|
|
2298
|
+
// native module is optional
|
|
2299
|
+
}
|
|
2274
2300
|
}
|
|
2275
2301
|
|
|
2276
2302
|
exports.ALWAYS = ALWAYS;
|