Comparing version 0.9.0 to 0.9.1
@@ -1,2 +0,1 @@ | ||
"use strict" | ||
let decoder | ||
@@ -12,2 +11,4 @@ try { | ||
const RECORD_TAG_ID = 0x69 | ||
const PACKED_TABLE_TAG_ID = 51 | ||
const PACKED_REFERENCE_TAG_ID = 6 | ||
const STOP_CODE = {} | ||
@@ -23,4 +24,7 @@ let strings = EMPTY_ARRAY | ||
let currentExtensions = [] | ||
let currentExtensionRanges = [] | ||
let packedValues | ||
let dataView | ||
let restoreMapsAsObject | ||
let sharedValues | ||
let defaultOptions = { | ||
@@ -63,2 +67,5 @@ useRecords: false, | ||
currentDecoder = this | ||
packedValues = this.sharedValues && | ||
(this.pack ? new Array(this.maxPrivatePackedValues || 16).concat(this.sharedValues) : | ||
this.sharedValues) | ||
if (this.structures) { | ||
@@ -74,2 +81,3 @@ currentStructures = this.structures | ||
currentStructures = [] | ||
packedValues = null | ||
} | ||
@@ -85,3 +93,5 @@ return checkedRead() | ||
if (forEach) { | ||
forEach(value) | ||
if (forEach(value) === false) { | ||
return | ||
} | ||
while(position < size) { | ||
@@ -299,5 +309,11 @@ lastPosition = position | ||
return extension(read()) | ||
} else { | ||
let input = read() | ||
for (let i = 0; i < currentExtensionRanges.length; i++) { | ||
let value = currentExtensionRanges[i](token, input) | ||
if (value !== undefined) | ||
return value | ||
} | ||
return new Tag(input) | ||
} | ||
else | ||
return new Tag(read()) | ||
} | ||
@@ -312,2 +328,5 @@ case 7: // fixed value | ||
default: | ||
let packedValue = packedValues[token] | ||
if (packedValue !== undefined) | ||
return packedValue | ||
throw new Error('Unknown token ' + token) | ||
@@ -749,3 +768,20 @@ } | ||
} | ||
const packedTable = (read) => { | ||
if (src[position++] != 0x84) | ||
throw new Error('Packed values structure must be followed by 4 element array') | ||
let newPackedValues = read() // packed values | ||
packedValues = packedValues ? newPackedValues.concat(packedValues.slice(newPackedValues.length)) : newPackedValues | ||
packedValues.prefixes = read() | ||
packedValues.suffixes = read() | ||
return read() // read the rump | ||
} | ||
packedTable.handlesRead = true | ||
currentExtensions[51] = packedTable | ||
currentExtensions[PACKED_REFERENCE_TAG_ID] = (data) => { // packed reference | ||
if (typeof data == 'number') | ||
return packedValues[16 + (data >= 0 ? 2 * data : (-2 * data - 1))] | ||
throw new Error('No support for non-integer packed references yet') | ||
} | ||
currentExtensions[40009] = (id) => { | ||
@@ -790,4 +826,24 @@ // id extension (for structured clones) | ||
}).handlesRead = true | ||
function combine(a, b) { | ||
if (typeof a === 'string') | ||
return a + b | ||
if (a instanceof Array) | ||
return a.concat(b) | ||
return Object.assign({}, a, b) | ||
} | ||
currentExtensionRanges.push((tag, input) => { | ||
if (tag >= 225 && tag <= 255) | ||
return combine(packedTable.prefixes[tag - 224], input) | ||
if (tag >= 28704 && tag <= 32767) | ||
return combine(packedTable.prefixes[tag - 28672], input) | ||
if (tag >= 1879052288 && tag <= 2147483647) | ||
return combine(packedTable.prefixes[tag - 1879048192], input) | ||
if (tag >= 216 && tag <= 223) | ||
return combine(input, packedTable.suffixes[tag - 216]) | ||
if (tag >= 27647 && tag <= 28671) | ||
return combine(input, packedTable.suffixes[tag - 27639]) | ||
if (tag >= 1811940352 && tag <= 1879048191) | ||
return combine(input, packedTable.suffixes[tag - 1811939328]) | ||
}) | ||
export const typedArrays = ['Uint8', 'Uint8Clamped', 'Uint16', 'Uint32', 'BigUint64','Int8', 'Int16', 'Int32', 'BigInt64', 'Float32', 'Float64'].map(type => type + 'Array') | ||
@@ -794,0 +850,0 @@ const typedArrayTags = [64, 68, 69, 70, 71, 72, 77, 78, 79, 81, 82] |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = global || self, factory(global.CBOR = {})); | ||
}(this, (function (exports) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('fs')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'fs'], factory) : | ||
(global = global || self, factory(global.CBOR = {}, global.fs)); | ||
}(this, (function (exports, fs) { 'use strict'; | ||
@@ -15,2 +15,3 @@ let decoder; | ||
const RECORD_TAG_ID = 0x69; | ||
const PACKED_REFERENCE_TAG_ID = 6; | ||
const STOP_CODE = {}; | ||
@@ -24,2 +25,4 @@ let currentDecoder = {}; | ||
let currentExtensions = []; | ||
let currentExtensionRanges = []; | ||
let packedValues; | ||
let dataView; | ||
@@ -62,2 +65,5 @@ let restoreMapsAsObject; | ||
currentDecoder = this; | ||
packedValues = this.sharedValues && | ||
(this.pack ? new Array(this.maxPrivatePackedValues || 16).concat(this.sharedValues) : | ||
this.sharedValues); | ||
if (this.structures) { | ||
@@ -73,2 +79,3 @@ currentStructures = this.structures; | ||
currentStructures = []; | ||
packedValues = null; | ||
} | ||
@@ -84,3 +91,5 @@ return checkedRead() | ||
if (forEach) { | ||
forEach(value); | ||
if (forEach(value) === false) { | ||
return | ||
} | ||
while(position < size) { | ||
@@ -295,5 +304,11 @@ lastPosition = position; | ||
return extension(read()) | ||
} else { | ||
let input = read(); | ||
for (let i = 0; i < currentExtensionRanges.length; i++) { | ||
let value = currentExtensionRanges[i](token, input); | ||
if (value !== undefined) | ||
return value | ||
} | ||
return new Tag(input) | ||
} | ||
else | ||
return new Tag(read()) | ||
} | ||
@@ -308,2 +323,5 @@ case 7: // fixed value | ||
default: | ||
let packedValue = packedValues[token]; | ||
if (packedValue !== undefined) | ||
return packedValue | ||
throw new Error('Unknown token ' + token) | ||
@@ -698,3 +716,20 @@ } | ||
}; | ||
const packedTable = (read) => { | ||
if (src[position++] != 0x84) | ||
throw new Error('Packed values structure must be followed by 4 element array') | ||
let newPackedValues = read(); // packed values | ||
packedValues = packedValues ? newPackedValues.concat(packedValues.slice(newPackedValues.length)) : newPackedValues; | ||
packedValues.prefixes = read(); | ||
packedValues.suffixes = read(); | ||
return read() // read the rump | ||
}; | ||
packedTable.handlesRead = true; | ||
currentExtensions[51] = packedTable; | ||
currentExtensions[PACKED_REFERENCE_TAG_ID] = (data) => { // packed reference | ||
if (typeof data == 'number') | ||
return packedValues[16 + (data >= 0 ? 2 * data : (-2 * data - 1))] | ||
throw new Error('No support for non-integer packed references yet') | ||
}; | ||
currentExtensions[40009] = (id) => { | ||
@@ -739,4 +774,24 @@ // id extension (for structured clones) | ||
}).handlesRead = true; | ||
function combine(a, b) { | ||
if (typeof a === 'string') | ||
return a + b | ||
if (a instanceof Array) | ||
return a.concat(b) | ||
return Object.assign({}, a, b) | ||
} | ||
currentExtensionRanges.push((tag, input) => { | ||
if (tag >= 225 && tag <= 255) | ||
return combine(packedTable.prefixes[tag - 224], input) | ||
if (tag >= 28704 && tag <= 32767) | ||
return combine(packedTable.prefixes[tag - 28672], input) | ||
if (tag >= 1879052288 && tag <= 2147483647) | ||
return combine(packedTable.prefixes[tag - 1879048192], input) | ||
if (tag >= 216 && tag <= 223) | ||
return combine(input, packedTable.suffixes[tag - 216]) | ||
if (tag >= 27647 && tag <= 28671) | ||
return combine(input, packedTable.suffixes[tag - 27639]) | ||
if (tag >= 1811940352 && tag <= 1879048191) | ||
return combine(input, packedTable.suffixes[tag - 1811939328]) | ||
}); | ||
const typedArrays = ['Uint8', 'Uint8Clamped', 'Uint16', 'Uint32', 'BigUint64','Int8', 'Int16', 'Int32', 'BigInt64', 'Float32', 'Float64'].map(type => type + 'Array'); | ||
@@ -838,2 +893,3 @@ const typedArrayTags = [64, 68, 69, 70, 71, 72, 77, 78, 79, 81, 82]; | ||
let referenceMap; | ||
options = options || {}; | ||
let lastSharedStructuresLength = 0; | ||
@@ -849,3 +905,3 @@ let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position, maxBytes) { | ||
let maxSharedStructures = 64; | ||
let isSequential = options && options.sequential; | ||
let isSequential = options.sequential; | ||
if (isSequential) { | ||
@@ -855,2 +911,11 @@ maxSharedStructures = 0; | ||
} | ||
let samplingPackedValues, packedObjectMap, sharedValues = options.sharedValues; | ||
let sharedPackedObjectMap; | ||
if (sharedValues) { | ||
sharedPackedObjectMap = Object.create(null); | ||
for (let i = 0, l = sharedValues.length; i < l; i++) { | ||
sharedPackedObjectMap[sharedValues[i]] = i; | ||
} | ||
} | ||
let recordIdsToRemove = []; | ||
@@ -910,2 +975,25 @@ let transitionsCount = 0; | ||
structures = sharedStructures || []; | ||
packedObjectMap = sharedPackedObjectMap; | ||
if (options.pack) { | ||
let packedValues = new Map(); | ||
packedValues.values = []; | ||
packedValues.encoder = encoder; | ||
packedValues.maxValues = options.maxPrivatePackedValues || (sharedPackedObjectMap ? 16 : Infinity); | ||
packedValues.objectMap = sharedPackedObjectMap || false; | ||
packedValues.samplingPackedValues = samplingPackedValues; | ||
findRepetitiveStrings(value, packedValues); | ||
if (packedValues.values.length > 0) { | ||
target[position$1++] = 0xd8; // one-byte tag | ||
target[position$1++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt | ||
writeArrayHeader(4); | ||
let valuesArray = packedValues.values; | ||
encode(valuesArray); | ||
writeArrayHeader(0); // prefixes | ||
writeArrayHeader(0); // suffixes | ||
packedObjectMap = Object.create(sharedPackedObjectMap || null); | ||
for (let i = 0, l = valuesArray.length; i < l; i++) { | ||
packedObjectMap[valuesArray[i]] = i; | ||
} | ||
} | ||
} | ||
try { | ||
@@ -952,2 +1040,7 @@ encode(value); | ||
let returnBuffer = target.subarray(start, position$1); | ||
let shared = encoder.structures || []; | ||
if (sharedValues) { | ||
shared = shared.concat(sharedValues); | ||
} | ||
if (encoder.saveStructures(encoder.structures, lastSharedStructuresLength) === false) { | ||
@@ -958,3 +1051,3 @@ // get updated structures and try again if the update failed | ||
} | ||
lastSharedStructuresLength = encoder.structures.length; | ||
lastSharedStructuresLength = shared.length; | ||
return returnBuffer | ||
@@ -965,2 +1058,21 @@ } | ||
}; | ||
this.findCommonStringsToPack = () => { | ||
samplingPackedValues = new Map(); | ||
if (!sharedPackedObjectMap) | ||
sharedPackedObjectMap = Object.create(null); | ||
return ({ threshold }) => { | ||
threshold = threshold || 4; | ||
let position = this.pack ? options.maxPrivatePackedValues || 16 : 0; | ||
if (!sharedValues) | ||
sharedValues = this.sharedValues = []; | ||
for (let [ key, status ] of samplingPackedValues) { | ||
if (status.count > threshold) { | ||
sharedPackedObjectMap[key] = position++; | ||
sharedValues.push(key); | ||
hasSharedUpdate = true; | ||
} | ||
} | ||
samplingPackedValues = null; | ||
} | ||
}; | ||
const encode = (value) => { | ||
@@ -973,2 +1085,39 @@ if (position$1 > safeEnd) | ||
if (type === 'string') { | ||
if (packedObjectMap) { | ||
let packedPosition = packedObjectMap[value]; | ||
if (packedPosition >= 0) { | ||
if (packedPosition < 16) | ||
target[position$1++] = packedPosition + 0xe0; // simple values, defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt | ||
else { | ||
target[position$1++] = 0xc6; // tag 6 defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt | ||
if (packedPosition & 1) | ||
encode((15 - packedPosition) >> 1); | ||
else | ||
encode((packedPosition - 16) >> 1); | ||
} | ||
return | ||
/* } else if (packedStatus.serializationId != serializationId) { | ||
packedStatus.serializationId = serializationId | ||
packedStatus.count = 1 | ||
if (options.sharedPack) { | ||
let sharedCount = packedStatus.sharedCount = (packedStatus.sharedCount || 0) + 1 | ||
if (shareCount > (options.sharedPack.threshold || 5)) { | ||
let sharedPosition = packedStatus.position = packedStatus.nextSharedPosition | ||
hasSharedUpdate = true | ||
if (sharedPosition < 16) | ||
target[position++] = sharedPosition + 0xc0 | ||
} | ||
} | ||
} // else any in-doc incrementation?*/ | ||
} else if (samplingPackedValues && !options.pack) { | ||
let status = samplingPackedValues.get(value); | ||
if (status) | ||
status.count++; | ||
else | ||
samplingPackedValues.set(value, { | ||
count: 1, | ||
}); | ||
} | ||
} | ||
let strLength = value.length; | ||
@@ -1342,6 +1491,3 @@ let headerSize; | ||
recordIdsToRemove.push(transition); | ||
if (length < 0x16) | ||
target[position$1++] = 0x82 + length; // array header, length of values + 2 | ||
else | ||
writeArrayHeader(length + 2); | ||
writeArrayHeader(length + 2); | ||
encode(keys); | ||
@@ -1396,3 +1542,5 @@ target[position$1++] = 0x19; // uint16 | ||
function writeArrayHeader(length) { | ||
if (length < 0x100) { | ||
if (length < 0x18) | ||
target[position$1++] = 0x80 | length; | ||
else if (length < 0x100) { | ||
target[position$1++] = 0x98; | ||
@@ -1411,2 +1559,52 @@ target[position$1++] = length; | ||
function findRepetitiveStrings(value, packedValues) { | ||
switch(typeof value) { | ||
case 'string': | ||
if (value.length > 3) { | ||
if (packedValues.objectMap[value] > -1 || packedValues.values.length >= packedValues.maxValues) | ||
return | ||
let packedStatus = packedValues.get(value); | ||
if (packedStatus) { | ||
if (++packedStatus.count == 2) { | ||
packedValues.values.push(value); | ||
} | ||
} else { | ||
packedValues.set(value, { | ||
count: 1, | ||
}); | ||
if (packedValues.samplingPackedValues) { | ||
let status = packedValues.samplingPackedValues.get(value); | ||
if (status) | ||
status.count++; | ||
else | ||
packedValues.samplingPackedValues.set(value, { | ||
count: 1, | ||
}); | ||
} | ||
} | ||
} | ||
break | ||
case 'object': | ||
if (value) { | ||
if (value instanceof Array) { | ||
for (let i = 0, l = value.length; i < l; i++) { | ||
findRepetitiveStrings(value[i], packedValues); | ||
} | ||
} else { | ||
let includeKeys = !packedValues.encoder.useRecords; | ||
for (var key in value) { | ||
if (value.hasOwnProperty(key)) { | ||
if (includeKeys) | ||
findRepetitiveStrings(key, packedValues); | ||
findRepetitiveStrings(value[key], packedValues); | ||
} | ||
} | ||
} | ||
} | ||
break | ||
case 'function': console.log(value); | ||
} | ||
} | ||
extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, ByteArray, | ||
@@ -1413,0 +1611,0 @@ Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array, BigUint64Array, Int8Array, Int16Array, Int32Array, BigInt64Array, |
@@ -1,35 +0,35 @@ | ||
(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports):"function"==typeof define&&define.amd?define(["exports"],b):(a=a||self,b(a.CBOR={}))})(this,function(a){'use strict';function b(){try{let a=c();if(z==y)// finished reading this source, cleanup references | ||
C=null,x=null,E&&(E=null);else if(z>y){// over read | ||
let a=new Error("Unexpected end of CBOR data");throw a.incomplete=!0,a}else if(!M)throw new Error("Data read, but end of buffer not reached");// else more to read, but we are reading sequentially, so don't clear source yet | ||
return a}catch(a){throw n(),(a instanceof RangeError||a.message.startsWith("Unexpected end of buffer"))&&(a.incomplete=!0),a}}function c(){let a=x[z++],b=a>>5;if(a&=31,23<a)switch(a){case 24:a=x[z++];break;case 25:if(7==b)return j();a=F.getUint16(z),z+=2;break;case 26:if(7==b){let a=F.getFloat32(z);if(2<H.useFloat32){// this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved | ||
let b=X[(127&x[z])<<1|x[z+1]>>7];return z+=4,(b*a+(0<a?.5:-.5)>>0)/b}return z+=4,a}a=F.getUint32(z),z+=4;break;case 27:if(7==b){let a=F.getFloat64(z);return z+=8,a}if(H.uint64AsNumber)return 72057594037927940*x[z++]+281474976710656*x[z++]+1099511627776*x[z++]+4294967296*x[z++]+16777216*x[z++]+(x[z++]<<16)+(x[z++]<<8)+x[z++];a=F.getBigUint64(z),z+=8;break;case 31:// indefinite length | ||
(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports,require("fs")):"function"==typeof define&&define.amd?define(["exports","fs"],b):(a=a||self,b(a.CBOR={},a.fs))})(this,function(a){'use strict';function b(){try{let a=c();if(B==A)// finished reading this source, cleanup references | ||
E=null,z=null,G&&(G=null);else if(B>A){// over read | ||
let a=new Error("Unexpected end of CBOR data");throw a.incomplete=!0,a}else if(!Q)throw new Error("Data read, but end of buffer not reached");// else more to read, but we are reading sequentially, so don't clear source yet | ||
return a}catch(a){throw o(),(a instanceof RangeError||a.message.startsWith("Unexpected end of buffer"))&&(a.incomplete=!0),a}}function c(){let a=z[B++],b=a>>5;if(a&=31,23<a)switch(a){case 24:a=z[B++];break;case 25:if(7==b)return j();a=I.getUint16(B),B+=2;break;case 26:if(7==b){let a=I.getFloat32(B);if(2<K.useFloat32){// this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved | ||
let b=aa[(127&z[B])<<1|z[B+1]>>7];return B+=4,(b*a+(0<a?.5:-.5)>>0)/b}return B+=4,a}a=I.getUint32(B),B+=4;break;case 27:if(7==b){let a=I.getFloat64(B);return B+=8,a}if(K.uint64AsNumber)return 72057594037927940*z[B++]+281474976710656*z[B++]+1099511627776*z[B++]+4294967296*z[B++]+16777216*z[B++]+(z[B++]<<16)+(z[B++]<<8)+z[B++];a=I.getBigUint64(B),B+=8;break;case 31:// indefinite length | ||
switch(b){case 2:// byte string | ||
case 3:// text string | ||
case 4:// array | ||
let a,d=[],e=0;for(;(a=c())!=B;)d[e++]=a;return 4==b?d:3==b?d.join(""):Buffer.concat(d);case 5:// map | ||
let f;if(H.mapsAsObjects){let a={};for(;(f=k())!=B;)a[f]=c();return a}else{G&&(H.mapsAsObjects=!0,G=!1);let a=new Map;for(;(f=c())!=B;)a.set(f,c());return a}case 7:return B;default:throw new Error("Invalid major type for indefinite length "+b);}default:throw new Error("Unknown token "+a);}switch(b){case 0:// positive int | ||
let a,d=[],e=0;for(;(a=c())!=D;)d[e++]=a;return 4==b?d:3==b?d.join(""):Buffer.concat(d);case 5:// map | ||
let f;if(K.mapsAsObjects){let a={};for(;(f=k())!=D;)a[f]=c();return a}else{J&&(K.mapsAsObjects=!0,J=!1);let a=new Map;for(;(f=c())!=D;)a.set(f,c());return a}case 7:return D;default:throw new Error("Invalid major type for indefinite length "+b);}default:throw new Error("Unknown token "+a);}switch(b){case 0:// positive int | ||
return a;case 1:// negative int | ||
return~a;case 2:// buffer | ||
return i(a);case 3:// string | ||
if(J>=z)return D.slice(z-I,(z+=a)-I);if(0==J&&140>y&&32>a){// for small blocks, avoiding the overhead of the extract call is helpful | ||
let b=16>a?h(a):g(a);if(null!=b)return b}return P(a);case 4:// array | ||
if(M>=B)return F.slice(B-L,(B+=a)-L);if(0==M&&140>A&&32>a){// for small blocks, avoiding the overhead of the extract call is helpful | ||
let b=16>a?h(a):g(a);if(null!=b)return b}return T(a);case 4:// array | ||
let e=Array(a);for(let b=0;b<a;b++)e[b]=c();return e;case 5:// map | ||
if(H.mapsAsObjects){let b={};for(let d=0;d<a;d++)b[k()]=c();return b}else{G&&(H.mapsAsObjects=!0,G=!1);let b=new Map;for(let d=0;d<a;d++)b.set(c(),c());return b}case 6:// extension | ||
if(a>>8==A){// record structures | ||
let b=C[255&a];if(b)return b.read||(b.read=d(b)),b.read();if(H.getStructures){let c=m(()=>(x=null,H.getStructures()));return!0===C?H.structures=C=c:C.splice.apply(C,[0,c.length].concat(c)),b=C[255&a],b?(b.read||(b.read=d(b)),b.read()):a}return a}else{let b=K[a];return b?b.handlesRead?b(c):b(c()):new S(c())}case 7:// fixed value | ||
if(K.mapsAsObjects){let b={};for(let d=0;d<a;d++)b[k()]=c();return b}else{J&&(K.mapsAsObjects=!0,J=!1);let b=new Map;for(let d=0;d<a;d++)b.set(c(),c());return b}case 6:// extension | ||
if(a>>8==C){// record structures | ||
let b=E[255&a];if(b)return b.read||(b.read=d(b)),b.read();if(K.getStructures){let c=n(()=>(z=null,K.getStructures()));return!0===E?K.structures=E=c:E.splice.apply(E,[0,c.length].concat(c)),b=E[255&a],b?(b.read||(b.read=d(b)),b.read()):a}return a}else{let b=N[a];if(b)return b.handlesRead?b(c):b(c());else{let b=c();for(let c,d=0;d<O.length;d++)if(c=O[d](a,b),void 0!==c)return c;return new W(b)}}case 7:// fixed value | ||
switch(a){case 20:return!1;case 21:return!0;case 22:return null;case 23:return;// undefined | ||
case 31:default:throw new Error("Unknown token "+a);}default:// negative int | ||
case 31:default:let b=H[a];if(void 0!==b)return b;throw new Error("Unknown token "+a);}default:// negative int | ||
if(isNaN(a)){let a=new Error("Unexpected end of CBOR data");throw a.incomplete=!0,a}throw new Error("Unknown CBOR token "+a);}}function d(a){function b(){// This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function | ||
if(2<b.count++)return this.read=new Function("a","r","return function(){a();return {"+a.map(a=>O.test(a)?a+":r()":"["+JSON.stringify(a)+"]:r()").join(",")+"}}")(e,c),this.read();e();let f={};for(let b,e=0;e<d;e++)b=a[e],f[b]=c();return f}let d=a.length;return b.count=0,b}function e(){// consume the array header, TODO: check expected length | ||
let a=x[z++];//let majorType = token >> 5 | ||
a&=31;23<a&&(24===a?z++:25===a?z+=2:26===a?z+=4:void 0)}function f(a){let b;if(16>a&&(b=h(a)))return b;if(64<a&&w)return w.decode(x.subarray(z,z+=a));const c=z+a,d=[];for(b="";z<c;){const a=x[z++];if(0==(128&a))d.push(a);else if(192==(224&a)){// 2 bytes | ||
const b=63&x[z++];d.push((31&a)<<6|b)}else if(224==(240&a)){// 3 bytes | ||
const b=63&x[z++],c=63&x[z++];d.push((31&a)<<12|b<<6|c)}else if(240==(248&a)){// 4 bytes | ||
const b=63&x[z++],c=63&x[z++],e=63&x[z++];let f=(7&a)<<18|b<<12|c<<6|e;65535<f&&(f-=65536,d.push(55296|1023&f>>>10),f=56320|1023&f),d.push(f)}else d.push(a);4096<=d.length&&(b+=Q.apply(String,d),d.length=0)}return 0<d.length&&(b+=Q.apply(String,d)),b}function g(a){let b=z,c=Array(a);for(let d=0;d<a;d++){const a=x[z++];if(0<(128&a))return void(z=b);c[d]=a}return Q.apply(String,c)}function h(p){if(4>p){if(!(2>p)){let d=x[z++],a=x[z++];if(0<(128&d)||0<(128&a))return void(z-=2);if(3>p)return Q(d,a);let b=x[z++];return 0<(128&b)?void(z-=3):Q(d,a,b)}if(0===p)return"";else{let b=x[z++];return 1<(128&b)?void(z-=1):Q(b)}}else{let q=x[z++],a=x[z++],b=x[z++],c=x[z++];if(0<(128&q)||0<(128&a)||0<(128&b)||0<(128&c))return void(z-=4);if(6>p){if(4===p)return Q(q,a,b,c);else{let d=x[z++];return 0<(128&d)?void(z-=5):Q(q,a,b,c,d)}}else if(8>p){let d=x[z++],e=x[z++];if(0<(128&d)||0<(128&e))return void(z-=6);if(7>p)return Q(q,a,b,c,d,e);let f=x[z++];return 0<(128&f)?void(z-=7):Q(q,a,b,c,d,e,f)}else{let d=x[z++],e=x[z++],f=x[z++],g=x[z++];if(0<(128&d)||0<(128&e)||0<(128&f)||0<(128&g))return void(z-=8);if(10>p){if(8===p)return Q(q,a,b,c,d,e,f,g);else{let h=x[z++];return 0<(128&h)?void(z-=9):Q(q,a,b,c,d,e,f,g,h)}}else if(12>p){let h=x[z++],i=x[z++];if(0<(128&h)||0<(128&i))return void(z-=10);if(11>p)return Q(q,a,b,c,d,e,f,g,h,i);let j=x[z++];return 0<(128&j)?void(z-=11):Q(q,a,b,c,d,e,f,g,h,i,j)}else{let h=x[z++],i=x[z++],j=x[z++],k=x[z++];if(0<(128&h)||0<(128&i)||0<(128&j)||0<(128&k))return void(z-=12);if(!(14>p)){let l=x[z++],m=x[z++];if(0<(128&l)||0<(128&m))return void(z-=14);if(15>p)return Q(q,a,b,c,d,e,f,g,h,i,j,k,l,m);let n=x[z++];return 0<(128&n)?void(z-=15):Q(q,a,b,c,d,e,f,g,h,i,j,k,l,m,n)}if(12===p)return Q(q,a,b,c,d,e,f,g,h,i,j,k);else{let l=x[z++];return 0<(128&l)?void(z-=13):Q(q,a,b,c,d,e,f,g,h,i,j,k,l)}}}}}function i(a){return H.copyBuffers?// specifically use the copying slice (not the node one) | ||
Uint8Array.prototype.slice.call(x,z,z+=a):x.subarray(z,z+=a)}function j(){var a=Math.exp;let b,c=x[z++],d=x[z++],e=(c<<8)+d,f=31&e>>10,g=1023&e;return b=0==f?a(g,-24):31==f?0==g?1/0:NaN:a(g+1024,f-25),32768&e?-b:b}function k(){let a=x[z++];if(96<=a&&120>a){if(a-=96,J>=z)// if it has been extracted, must use it (and faster anyway) | ||
return D.slice(z-I,(z+=a)-I);if(!(0==J&&180>y))return P(a)}else return z--,c();let b,d=4095&(a<<5^(1<a?F.getUint16(z):0<a?x[z]:0)),e=R[d],f=z,j=z+a-3,k=0;if(e&&e.bytes==a){for(;f<j;){if(b=F.getUint32(f),b!=e[k++]){f=1879048192;break}f+=4}for(j+=3;f<j;)if(b=x[f++],b!=e[k++]){f=1879048192;break}if(f===j)return z=f,e.string;j-=3,f=z}for(e=[],R[d]=e,e.bytes=a;f<j;)b=F.getUint32(f),e.push(b),f+=4;for(j+=3;f<j;)b=x[f++],e.push(b);// for small blocks, avoiding the overhead of the extract call is helpful | ||
let l=16>a?h(a):g(a);return null==l?e.string=P(a):e.string=l}function l(a,b){K[b]=b=>{if(!a)throw new Error("Could not find typed array for code "+typeCode);// we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned | ||
return new T[a](Uint8Array.prototype.slice.call(b,0).buffer)}}function m(a){let b=y,c=z,d=I,e=J,f=D,g=E,h=new Uint8Array(x.slice(0,y)),i=C,j=H,k=M,l=a();return y=b,z=c,I=d,J=e,D=f,E=g,x=h,M=k,C=i,H=j,F=new DataView(x.buffer,x.byteOffset,x.byteLength),l}function n(){x=null,E=null,C=null}function o(a){K[a.tag]=a.decode}function p(a){256>a?(la[oa++]=152,la[oa++]=a):65536>a?(la[oa++]=153,la[oa++]=a>>8,la[oa++]=255&a):(la[oa++]=154,ma.setUint32(oa,a),oa+=4)}function q(a){return{tag:a,encode:function(a,b){let c=a.byteLength,d=a.byteOffset||0,e=a.buffer||a;b(fa?Buffer.from(e,d,c):new Uint8Array(e,d,c))}}}function r(a,b){let c=a.byteLength;24>c?la[oa++]=64+c:256>c?(la[oa++]=88,la[oa++]=c):65536>c?(la[oa++]=89,la[oa++]=c>>8,la[oa++]=255&c):(la[oa++]=90,ma.setUint32(oa,c),oa+=4),oa+c>=la.length&&b(oa+c),la.set(a,oa),oa+=c}function s(a,b){// insert the ids that need to be referenced for structured clones | ||
if(2<b.count++)return this.read=new Function("a","r","return function(){a();return {"+a.map(a=>S.test(a)?a+":r()":"["+JSON.stringify(a)+"]:r()").join(",")+"}}")(e,c),this.read();e();let f={};for(let b,e=0;e<d;e++)b=a[e],f[b]=c();return f}let d=a.length;return b.count=0,b}function e(){// consume the array header, TODO: check expected length | ||
let a=z[B++];//let majorType = token >> 5 | ||
a&=31;23<a&&(24===a?B++:25===a?B+=2:26===a?B+=4:void 0)}function f(a){let b;if(16>a&&(b=h(a)))return b;if(64<a&&y)return y.decode(z.subarray(B,B+=a));const c=B+a,d=[];for(b="";B<c;){const a=z[B++];if(0==(128&a))d.push(a);else if(192==(224&a)){// 2 bytes | ||
const b=63&z[B++];d.push((31&a)<<6|b)}else if(224==(240&a)){// 3 bytes | ||
const b=63&z[B++],c=63&z[B++];d.push((31&a)<<12|b<<6|c)}else if(240==(248&a)){// 4 bytes | ||
const b=63&z[B++],c=63&z[B++],e=63&z[B++];let f=(7&a)<<18|b<<12|c<<6|e;65535<f&&(f-=65536,d.push(55296|1023&f>>>10),f=56320|1023&f),d.push(f)}else d.push(a);4096<=d.length&&(b+=U.apply(String,d),d.length=0)}return 0<d.length&&(b+=U.apply(String,d)),b}function g(a){let b=B,c=Array(a);for(let d=0;d<a;d++){const a=z[B++];if(0<(128&a))return void(B=b);c[d]=a}return U.apply(String,c)}function h(p){if(4>p){if(!(2>p)){let d=z[B++],a=z[B++];if(0<(128&d)||0<(128&a))return void(B-=2);if(3>p)return U(d,a);let b=z[B++];return 0<(128&b)?void(B-=3):U(d,a,b)}if(0===p)return"";else{let b=z[B++];return 1<(128&b)?void(B-=1):U(b)}}else{let q=z[B++],a=z[B++],b=z[B++],c=z[B++];if(0<(128&q)||0<(128&a)||0<(128&b)||0<(128&c))return void(B-=4);if(6>p){if(4===p)return U(q,a,b,c);else{let d=z[B++];return 0<(128&d)?void(B-=5):U(q,a,b,c,d)}}else if(8>p){let d=z[B++],e=z[B++];if(0<(128&d)||0<(128&e))return void(B-=6);if(7>p)return U(q,a,b,c,d,e);let f=z[B++];return 0<(128&f)?void(B-=7):U(q,a,b,c,d,e,f)}else{let d=z[B++],e=z[B++],f=z[B++],g=z[B++];if(0<(128&d)||0<(128&e)||0<(128&f)||0<(128&g))return void(B-=8);if(10>p){if(8===p)return U(q,a,b,c,d,e,f,g);else{let h=z[B++];return 0<(128&h)?void(B-=9):U(q,a,b,c,d,e,f,g,h)}}else if(12>p){let h=z[B++],i=z[B++];if(0<(128&h)||0<(128&i))return void(B-=10);if(11>p)return U(q,a,b,c,d,e,f,g,h,i);let j=z[B++];return 0<(128&j)?void(B-=11):U(q,a,b,c,d,e,f,g,h,i,j)}else{let h=z[B++],i=z[B++],j=z[B++],k=z[B++];if(0<(128&h)||0<(128&i)||0<(128&j)||0<(128&k))return void(B-=12);if(!(14>p)){let l=z[B++],m=z[B++];if(0<(128&l)||0<(128&m))return void(B-=14);if(15>p)return U(q,a,b,c,d,e,f,g,h,i,j,k,l,m);let n=z[B++];return 0<(128&n)?void(B-=15):U(q,a,b,c,d,e,f,g,h,i,j,k,l,m,n)}if(12===p)return U(q,a,b,c,d,e,f,g,h,i,j,k);else{let l=z[B++];return 0<(128&l)?void(B-=13):U(q,a,b,c,d,e,f,g,h,i,j,k,l)}}}}}function i(a){return K.copyBuffers?// specifically use the copying slice (not the node one) | ||
Uint8Array.prototype.slice.call(z,B,B+=a):z.subarray(B,B+=a)}function j(){var a=Math.exp;let b,c=z[B++],d=z[B++],e=(c<<8)+d,f=31&e>>10,g=1023&e;return b=0==f?a(g,-24):31==f?0==g?1/0:NaN:a(g+1024,f-25),32768&e?-b:b}function k(){let a=z[B++];if(96<=a&&120>a){if(a-=96,M>=B)// if it has been extracted, must use it (and faster anyway) | ||
return F.slice(B-L,(B+=a)-L);if(!(0==M&&180>A))return T(a)}else return B--,c();let b,d=4095&(a<<5^(1<a?I.getUint16(B):0<a?z[B]:0)),e=V[d],f=B,j=B+a-3,k=0;if(e&&e.bytes==a){for(;f<j;){if(b=I.getUint32(f),b!=e[k++]){f=1879048192;break}f+=4}for(j+=3;f<j;)if(b=z[f++],b!=e[k++]){f=1879048192;break}if(f===j)return B=f,e.string;j-=3,f=B}for(e=[],V[d]=e,e.bytes=a;f<j;)b=I.getUint32(f),e.push(b),f+=4;for(j+=3;f<j;)b=z[f++],e.push(b);// for small blocks, avoiding the overhead of the extract call is helpful | ||
let l=16>a?h(a):g(a);return null==l?e.string=T(a):e.string=l}function l(c,a){return"string"==typeof c?c+a:c instanceof Array?c.concat(a):Object.assign({},c,a)}function m(a,b){N[b]=b=>{if(!a)throw new Error("Could not find typed array for code "+typeCode);// we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned | ||
return new X[a](Uint8Array.prototype.slice.call(b,0).buffer)}}function n(a){let b=A,c=B,d=L,e=M,f=F,g=G,h=new Uint8Array(z.slice(0,A)),i=E,j=K,k=Q,l=a();return A=b,B=c,L=d,M=e,F=f,G=g,z=h,Q=k,E=i,K=j,I=new DataView(z.buffer,z.byteOffset,z.byteLength),l}function o(){z=null,G=null,E=null}function p(a){N[a.tag]=a.decode}function q(a){24>a?qa[ta++]=128|a:256>a?(qa[ta++]=152,qa[ta++]=a):65536>a?(qa[ta++]=153,qa[ta++]=a>>8,qa[ta++]=255&a):(qa[ta++]=154,ra.setUint32(ta,a),ta+=4)}function r(a,b){switch(typeof a){case"string":if(3<a.length){if(-1<b.objectMap[a]||b.values.length>=b.maxValues)return;let c=b.get(a);if(c)2==++c.count&&b.values.push(a);else if(b.set(a,{count:1}),b.samplingPackedValues){let c=b.samplingPackedValues.get(a);c?c.count++:b.samplingPackedValues.set(a,{count:1})}}break;case"object":if(a)if(a instanceof Array)for(let c=0,d=a.length;c<d;c++)r(a[c],b);else{let d=!b.encoder.useRecords;for(var c in a)a.hasOwnProperty(c)&&(d&&r(c,b),r(a[c],b))}break;case"function":console.log(a);}}function s(a){return{tag:a,encode:function(a,b){let c=a.byteLength,d=a.byteOffset||0,e=a.buffer||a;b(ka?Buffer.from(e,d,c):new Uint8Array(e,d,c))}}}function t(a,b){let c=a.byteLength;24>c?qa[ta++]=64+c:256>c?(qa[ta++]=88,qa[ta++]=c):65536>c?(qa[ta++]=89,qa[ta++]=c>>8,qa[ta++]=255&c):(qa[ta++]=90,ra.setUint32(ta,c),ta+=4),ta+c>=qa.length&&b(ta+c),qa.set(a,ta),ta+=c}function u(a,b){// insert the ids that need to be referenced for structured clones | ||
let c,d=8*b.length,e=a.length-d;for(b.sort((c,a)=>c.offset>a.offset?1:-1);c=b.pop();){let b=c.offset,f=c.id;a.copyWithin(b+d,b,e),d-=8;let g=b+d;// uint32 | ||
a[g++]=217,a[g++]=156,a[g++]=73,a[g++]=26,a[g++]=f>>24,a[g++]=255&f>>16,a[g++]=255&f>>8,a[g++]=255&f,e=b}return a}function t(a){if(a.Class){if(!a.encode)throw new Error("Extension has no encode function");ea.unshift(a.Class),da.unshift(a)}o(a)}function*u(a,b){const c=new qa(b);for(const d of a)yield c.encode(d)}async function*v(a,b){const c=new qa(b);for await(const d of a)yield c.encode(d)}/** | ||
a[g++]=217,a[g++]=156,a[g++]=73,a[g++]=26,a[g++]=f>>24,a[g++]=255&f>>16,a[g++]=255&f>>8,a[g++]=255&f,e=b}return a}function v(a){if(a.Class){if(!a.encode)throw new Error("Extension has no encode function");ja.unshift(a.Class),ia.unshift(a)}p(a)}function*w(a,b){const c=new va(b);for(const d of a)yield c.encode(d)}async function*x(a,b){const c=new va(b);for await(const d of a)yield c.encode(d)}/** | ||
* Given an Iterable/Iterator input which yields buffers, returns an IterableIterator which yields sync decoded objects | ||
@@ -40,21 +40,37 @@ * Or, given an Async Iterable/Iterator which yields promises resolving in buffers, returns an AsyncIterableIterator. | ||
* @returns {IterableIterator|Promise.<AsyncIterableIterator} | ||
*/let w;try{w=new TextDecoder}catch(a){}let x,y,z=0;const A=105,B={};let C,D,E,F,G,H={},I=0,J=0,K=[],L={useRecords:!1,mapsAsObjects:!0},M=!1;class N{constructor(a){a&&(!1===a.useRecords&&a.mapsAsObjects===void 0&&(a.mapsAsObjects=!0),a.getStructures&&!a.structures&&((a.structures=[]).uninitialized=!0)),Object.assign(this,a)}decode(a,c){if(x)// re-entrant execution, save the state and restore it after we do this decode | ||
return m(()=>(n(),this?this.decode(a,c):N.prototype.decode.call(L,a,c)));if(y=-1<c?c:a.length,z=0,J=0,D=null,x=a,F=a.dataView||(a.dataView=new DataView(a.buffer,a.byteOffset,a.byteLength)),this){if(H=this,this.structures)return C=this.structures,b();(!C||0<C.length)&&(C=[])}else H=L,(!C||0<C.length)&&(C=[]);return b()}decodeMultiple(a,c){let d,e=0;try{let f=a.length;M=!0;let g=this?this.decode(a,f):Y.decode(a,f);if(c){for(c(g);z<f;)if(e=z,!1===c(b()))return;}else{for(d=[g];z<f;)e=z,d.push(b());return d}}catch(a){throw a.lastPosition=e,a.values=d,a}finally{M=!1,n()}}}const O=/^[a-zA-Z_$][a-zA-Z\d_$]*$/;let P=f,Q=String.fromCharCode,R=Array(4096);class S{constructor(a){this.value=a}}let T="object"==typeof window?window:global;K[0]=a=>new Date(a),K[1]=a=>new Date(1e3*a),K[2]=a=>new DataView(a.buffer,a.byteOffset,a.byteLength).getBigUint64(0),K[3]=a=>BigInt(-1)-new DataView(a.buffer,a.byteOffset,a.byteLength).getBigUint64(0);// the registration of the record definition extension (tag 105) | ||
const U=()=>{let a=c(),b=a[0],e=a[1];C[255&e]=b,b.read=d(b);let f={};for(let c,d=2,e=a.length;d<e;d++)c=b[d-2],f[c]=a[d];return f};U.handlesRead=!0,K[105]=U,K[27]=a=>(T[a[0]]||Error)(a[1],a[2]),K[40009]=a=>{E||(E=new Map);let b,d=x[z];b=4==d>>5?[]:{};let e={target:b};// a placeholder object | ||
E.set(a,e);let f=c();// read the next value as the target object to id | ||
*/let y;try{y=new TextDecoder}catch(a){}let z,A,B=0;const C=105,D={};let E,F,G,H,I,J,K={},L=0,M=0,N=[],O=[],P={useRecords:!1,mapsAsObjects:!0},Q=!1;class R{constructor(a){a&&(!1===a.useRecords&&a.mapsAsObjects===void 0&&(a.mapsAsObjects=!0),a.getStructures&&!a.structures&&((a.structures=[]).uninitialized=!0)),Object.assign(this,a)}decode(a,c){if(z)// re-entrant execution, save the state and restore it after we do this decode | ||
return n(()=>(o(),this?this.decode(a,c):R.prototype.decode.call(P,a,c)));if(A=-1<c?c:a.length,B=0,M=0,F=null,z=a,I=a.dataView||(a.dataView=new DataView(a.buffer,a.byteOffset,a.byteLength)),this){if(K=this,H=this.sharedValues&&(this.pack?Array(this.maxPrivatePackedValues||16).concat(this.sharedValues):this.sharedValues),this.structures)return E=this.structures,b();(!E||0<E.length)&&(E=[])}else K=P,(!E||0<E.length)&&(E=[]),H=null;return b()}decodeMultiple(a,c){let d,e=0;try{let f=a.length;Q=!0;let g=this?this.decode(a,f):ba.decode(a,f);if(c){if(!1===c(g))return;for(;B<f;)if(e=B,!1===c(b()))return}else{for(d=[g];B<f;)e=B,d.push(b());return d}}catch(a){throw a.lastPosition=e,a.values=d,a}finally{Q=!1,o()}}}const S=/^[a-zA-Z_$][a-zA-Z\d_$]*$/;let T=f,U=String.fromCharCode,V=Array(4096);class W{constructor(a){this.value=a}}let X="object"==typeof window?window:global;N[0]=a=>new Date(a),N[1]=a=>new Date(1e3*a),N[2]=a=>new DataView(a.buffer,a.byteOffset,a.byteLength).getBigUint64(0),N[3]=a=>BigInt(-1)-new DataView(a.buffer,a.byteOffset,a.byteLength).getBigUint64(0);// the registration of the record definition extension (tag 105) | ||
const Y=()=>{let a=c(),b=a[0],e=a[1];E[255&e]=b,b.read=d(b);let f={};for(let c,d=2,e=a.length;d<e;d++)c=b[d-2],f[c]=a[d];return f};Y.handlesRead=!0,N[105]=Y,N[27]=a=>(X[a[0]]||Error)(a[1],a[2]);const Z=a=>{if(132!=z[B++])throw new Error("Packed values structure must be followed by 4 element array");let b=a();// packed values | ||
return H=H?b.concat(H.slice(b.length)):b,H.prefixes=a(),H.suffixes=a(),a();// read the rump | ||
};Z.handlesRead=!0,N[51]=Z,N[6]=a=>{// packed reference | ||
if("number"==typeof a)return H[16+(0<=a?2*a:-2*a-1)];throw new Error("No support for non-integer packed references yet")},N[40009]=a=>{G||(G=new Map);let b,d=z[B];b=4==d>>5?[]:{};let e={target:b};// a placeholder object | ||
G.set(a,e);let f=c();// read the next value as the target object to id | ||
return e.used?Object.assign(b,f):(e.target=f,f);// no cycle, can just use the returned read object | ||
},K[40010]=a=>{// pointer extension (for structured clones) | ||
let b=E.get(a);return b.used=!0,b.target},K[258]=a=>new Set(a),(K[259]=a=>(H.mapsAsObjects&&(H.mapsAsObjects=!1,G=!0),a())).handlesRead=!0;const V=["Uint8","Uint8Clamped","Uint16","Uint32","BigUint64","Int8","Int16","Int32","BigInt64","Float32","Float64"].map(a=>a+"Array"),W=[64,68,69,70,71,72,77,78,79,81,82];for(let b=0;b<V.length;b++)l(V[b],W[b]);const X=Array(147);// this is a table matching binary exponents to the multiplier to determine significant digit rounding | ||
for(let b=0;256>b;b++)X[b]=+("1e"+Math.floor(45.15-.30103*b));let Y=new N({useRecords:!1});const Z=Y.decode,$=Y.decodeMultiple,_={NEVER:0,ALWAYS:1,DECIMAL_ROUND:3,DECIMAL_FIT:4};let aa,ba=new Float32Array(1),ca=new Uint8Array(ba.buffer,0,4);try{aa=new TextEncoder}catch(a){}let da,ea;const fa="undefined"!=typeof Buffer,ga=fa?Buffer.allocUnsafeSlow:Uint8Array,ha=fa?Buffer:Uint8Array,ia=105,ja=256,ka=fa?4294967296:2144337920;let la,ma,na,oa=0;const pa=Symbol("record-id");class qa extends N{constructor(a){super(a),this.offset=0;let b,c,d,e,f,g=0,h=ha.prototype.utf8Write?function(a,b,c){return la.utf8Write(a,b,c)}:!!(aa&&aa.encodeInto)&&function(a,b){return aa.encodeInto(a,la.subarray(b)).written},i=this,j=64,k=a&&a.sequential;k&&(j=0,this.structures=[]);let m=[],n=0,o=0;this.encode=function(a,h){if(la||(la=new ga(8192),ma=new DataView(la.buffer,0,8192),oa=0),na=la.length-10,2048>na-oa?(la=new ga(la.length),ma=new DataView(la.buffer,0,la.length),na=la.length-10,oa=0):h===xa&&(oa=2147483640&oa+7),b=oa,f=i.structuredClone?new Map:null,c=i.structures,c){c.uninitialized&&(i.structures=c=i.getStructures());let a=c.length;if(a>j&&!k&&(a=j),!c.transitions){c.transitions=Object.create(null);for(let b,d=0;d<a;d++){if(b=c[d],!b)continue;let a,e=c.transitions;for(let c,d=0,f=b.length;d<f;d++)c=b[d],a=e[c],a||(a=e[c]=Object.create(null)),e=a;e[pa]=d}g=c.length}k||(c.nextId=a)}d&&(d=!1),e=c||[];try{// update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially | ||
if(q(a),i.offset=oa,f&&f.idsToInsert){oa+=8*f.idsToInsert.length,oa>na&&t(oa),i.offset=oa;let a=s(la.subarray(b,oa),f.idsToInsert);return f=null,a}return h===xa?(la.start=b,la.end=oa,la):la.subarray(b,oa);// position can change if we call pack again in saveStructures, so we get the buffer now | ||
}finally{if(c){if(10>o&&o++,1e4<n)c.transitions=null,o=0,n=0,0<m.length&&(m=[]);else if(0<m.length&&!k){for(let a=0,b=m.length;a<b;a++)m[a][pa]=void 0;m=[]}if(d&&i.saveStructures){i.structures.length>j&&(i.structures=i.structures.slice(0,j));// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save | ||
let c=la.subarray(b,oa);return!1===i.saveStructures(i.structures,g)?(i.structures=i.getStructures()||[],i.encode(a)):(g=i.structures.length,c)}}}};const q=a=>{oa>na&&(la=t(oa));var c,d=typeof a;if("string"==d){let b,d=a.length;b=32>d?1:256>d?2:65536>d?3:5;let e=3*d;if(oa+e>na&&(la=t(oa+e)),64>d||!h){let e,f,g,h=oa+b;for(e=0;e<d;e++)f=a.charCodeAt(e),128>f?la[h++]=f:2048>f?(la[h++]=192|f>>6,la[h++]=128|63&f):55296==(64512&f)&&56320==(64512&(g=a.charCodeAt(e+1)))?(f=65536+((1023&f)<<10)+(1023&g),e++,la[h++]=240|f>>18,la[h++]=128|63&f>>12,la[h++]=128|63&f>>6,la[h++]=128|63&f):(la[h++]=224|f>>12,la[h++]=128|63&f>>6,la[h++]=128|63&f);c=h-oa-b}else c=h(a,oa+b,e);24>c?la[oa++]=96|c:256>c?(2>b&&la.copyWithin(oa+2,oa+1,oa+1+c),la[oa++]=120,la[oa++]=c):65536>c?(3>b&&la.copyWithin(oa+3,oa+2,oa+2+c),la[oa++]=121,la[oa++]=c>>8,la[oa++]=255&c):(5>b&&la.copyWithin(oa+5,oa+3,oa+3+c),la[oa++]=122,ma.setUint32(oa,c),oa+=4),oa+=c}else if("number"===d){if(a>>>0===a)24>a?la[oa++]=a:256>a?(la[oa++]=24,la[oa++]=a):65536>a?(la[oa++]=25,la[oa++]=a>>8,la[oa++]=255&a):(la[oa++]=26,ma.setUint32(oa,a),oa+=4);else if(a>>0===a)-24<=a?la[oa++]=31-a:-256<=a?(la[oa++]=56,la[oa++]=~a):-65536<=a?(la[oa++]=57,ma.setUint16(oa,~a),oa+=2):(la[oa++]=58,ma.setUint32(oa,~a),oa+=4);else{let b;if(0<(b=this.useFloat32)&&4294967296>a&&-2147483648<=a){la[oa++]=250,ma.setFloat32(oa,a);let c;if(4>b||// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved | ||
(c=a*X[(127&la[oa])<<1|la[oa+1]>>7])>>0===c)return void(oa+=4);// move back into position for writing a double | ||
oa--}la[oa++]=251,ma.setFloat64(oa,a),oa+=8}}else if("object"===d){if(!a)la[oa++]=246;else{if(f){let c=f.get(a);if(c){if(!c.id){let a=f.idsToInsert||(f.idsToInsert=[]);c.id=a.push(c)}return la[oa++]=217,la[oa++]=156,la[oa++]=74,la[oa++]=26,ma.setUint32(oa,c.id),void(oa+=4)}f.set(a,{offset:oa-b})}let d=a.constructor;if(d===Object)r(a,!0);else if(d===Array){c=a.length,24>c?la[oa++]=128|c:p(c);for(let b=0;b<c;b++)q(a[b])}else if(d===Map){(this.mapsAsObjects?!1!==this.useTag259ForMaps:this.useTag259ForMaps)&&(la[oa++]=217,la[oa++]=1,la[oa++]=3),c=a.size,24>c?la[oa++]=160|c:256>c?(la[oa++]=184,la[oa++]=c):65536>c?(la[oa++]=185,la[oa++]=c>>8,la[oa++]=255&c):(la[oa++]=186,ma.setUint32(oa,c),oa+=4);for(let[b,c]of a)q(b),q(c)}else{for(let b,c=0,d=da.length;c<d;c++)if(b=ea[c],a instanceof b){let b=da[c],d=b.tag;return 24>d?la[oa++]=192|d:256>d?(la[oa++]=216,la[oa++]=d):65536>d?(la[oa++]=217,la[oa++]=d>>8,la[oa++]=255&d):-1<d&&(la[oa++]=218,ma.setUint32(oa,d),oa+=4),void b.encode.call(this,a,q,t)}if(a[Symbol.iterator]){la[oa++]=159;// indefinite length array | ||
for(let b of a)q(b);// stop-code | ||
return void(la[oa++]=255)}// no extension found, write as object | ||
r(a,!a.hasOwnProperty)}}}else if("boolean"===d)la[oa++]=a?245:244;else if("bigint"===d){if(a<BigInt(1)<<BigInt(64)&&0<=a)la[oa++]=27,ma.setBigUint64(oa,a);else if(a>-(BigInt(1)<<BigInt(64))&&0>a)la[oa++]=59,ma.setBigUint64(oa,-a-BigInt(1));else// overflow | ||
if(this.largeBigIntToFloat)la[oa++]=251,ma.setFloat64(oa,+a);else throw new RangeError(a+" was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64");oa+=8}else if("undefined"===d)la[oa++]=247;else throw new Error("Unknown type: "+d)},r=!1===this.useRecords?this.variableMapSize?a=>{// this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects) | ||
let b=Object.keys(a),c=b.length;24>c?la[oa++]=160|c:256>c?(la[oa++]=184,la[oa++]=c):65536>c?(la[oa++]=185,la[oa++]=c>>8,la[oa++]=255&c):(la[oa++]=186,ma.setUint32(oa,c),oa+=4);let d;for(let e=0;e<c;e++)q(d=b[e]),q(a[d])}:(a,c)=>{la[oa++]=185;// always use map 16, so we can preallocate and set the length afterwards | ||
let d=oa-b;oa+=2;let e=0;for(let b in a)(c||a.hasOwnProperty(b))&&(q(b),q(a[b]),e++);la[d++ +b]=e>>8,la[d+b]=255&e}:/* sharedStructures ? // For highly stable structures, using for-in can a little bit faster | ||
},N[40010]=a=>{// pointer extension (for structured clones) | ||
let b=G.get(a);return b.used=!0,b.target},N[258]=a=>new Set(a),(N[259]=a=>(K.mapsAsObjects&&(K.mapsAsObjects=!1,J=!0),a())).handlesRead=!0,O.push((a,b)=>225<=a&&255>=a?l(Z.prefixes[a-224],b):28704<=a&&32767>=a?l(Z.prefixes[a-28672],b):1879052288<=a&&2147483647>=a?l(Z.prefixes[a-1879048192],b):216<=a&&223>=a?l(b,Z.suffixes[a-216]):27647<=a&&28671>=a?l(b,Z.suffixes[a-27639]):1811940352<=a&&1879048191>=a?l(b,Z.suffixes[a-1811939328]):void 0);const $=["Uint8","Uint8Clamped","Uint16","Uint32","BigUint64","Int8","Int16","Int32","BigInt64","Float32","Float64"].map(a=>a+"Array"),_=[64,68,69,70,71,72,77,78,79,81,82];for(let b=0;b<$.length;b++)m($[b],_[b]);const aa=Array(147);// this is a table matching binary exponents to the multiplier to determine significant digit rounding | ||
for(let b=0;256>b;b++)aa[b]=+("1e"+Math.floor(45.15-.30103*b));let ba=new R({useRecords:!1});const ca=ba.decode,da=ba.decodeMultiple,ea={NEVER:0,ALWAYS:1,DECIMAL_ROUND:3,DECIMAL_FIT:4};let fa,ga=new Float32Array(1),ha=new Uint8Array(ga.buffer,0,4);try{fa=new TextEncoder}catch(a){}let ia,ja;const ka="undefined"!=typeof Buffer,la=ka?Buffer.allocUnsafeSlow:Uint8Array,ma=ka?Buffer:Uint8Array,na=105,oa=256,pa=ka?4294967296:2144337920;let qa,ra,sa,ta=0;const ua=Symbol("record-id");class va extends R{constructor(a){super(a),this.offset=0;let b,c,d,e,f;a=a||{};let g=0,h=ma.prototype.utf8Write?function(a,b,c){return qa.utf8Write(a,b,c)}:!!(fa&&fa.encodeInto)&&function(a,b){return fa.encodeInto(a,qa.subarray(b)).written},i=this,j=64,k=a.sequential;k&&(j=0,this.structures=[]);let l,m,n,o=a.sharedValues;if(o){n=Object.create(null);for(let a=0,b=o.length;a<b;a++)n[o[a]]=a}let p=[],s=0,t=0;this.encode=function(h,w){if(qa||(qa=new la(8192),ra=new DataView(qa.buffer,0,8192),ta=0),sa=qa.length-10,2048>sa-ta?(qa=new la(qa.length),ra=new DataView(qa.buffer,0,qa.length),sa=qa.length-10,ta=0):w===Ca&&(ta=2147483640&ta+7),b=ta,f=i.structuredClone?new Map:null,c=i.structures,c){c.uninitialized&&(i.structures=c=i.getStructures());let a=c.length;if(a>j&&!k&&(a=j),!c.transitions){c.transitions=Object.create(null);for(let b,d=0;d<a;d++){if(b=c[d],!b)continue;let a,e=c.transitions;for(let c,d=0,f=b.length;d<f;d++)c=b[d],a=e[c],a||(a=e[c]=Object.create(null)),e=a;e[ua]=d}g=c.length}k||(c.nextId=a)}if(d&&(d=!1),e=c||[],m=n,a.pack){let b=new Map;if(b.values=[],b.encoder=i,b.maxValues=a.maxPrivatePackedValues||(n?16:1/0),b.objectMap=n||!1,b.samplingPackedValues=l,r(h,b),0<b.values.length){qa[ta++]=216,qa[ta++]=51,q(4);let a=b.values;v(a),q(0),q(0),m=Object.create(n||null);for(let b=0,c=a.length;b<c;b++)m[a[b]]=b}}try{// update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially | ||
if(v(h),i.offset=ta,f&&f.idsToInsert){ta+=8*f.idsToInsert.length,ta>sa&&x(ta),i.offset=ta;let a=u(qa.subarray(b,ta),f.idsToInsert);return f=null,a}return w===Ca?(qa.start=b,qa.end=ta,qa):qa.subarray(b,ta);// position can change if we call pack again in saveStructures, so we get the buffer now | ||
}finally{if(c){if(10>t&&t++,1e4<s)c.transitions=null,t=0,s=0,0<p.length&&(p=[]);else if(0<p.length&&!k){for(let a=0,b=p.length;a<b;a++)p[a][ua]=void 0;p=[]}if(d&&i.saveStructures){i.structures.length>j&&(i.structures=i.structures.slice(0,j));// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save | ||
let a=qa.subarray(b,ta),c=i.structures||[];return(o&&(c=c.concat(o)),!1===i.saveStructures(i.structures,g))?(i.structures=i.getStructures()||[],i.encode(h)):(g=c.length,a)}}}},this.findCommonStringsToPack=()=>(l=new Map,n||(n=Object.create(null)),({threshold:b})=>{b=b||4;let c=this.pack?a.maxPrivatePackedValues||16:0;o||(o=this.sharedValues=[]);for(let[a,e]of l)e.count>b&&(n[a]=c++,o.push(a),d=!0);l=null});const v=c=>{ta>sa&&(qa=x(ta));var d,e=typeof c;if("string"==e){if(m){let b=m[c];if(0<=b)return void(16>b?qa[ta++]=b+224:(qa[ta++]=198,1&b?v(15-b>>1):v(b-16>>1)));/* } else if (packedStatus.serializationId != serializationId) { | ||
packedStatus.serializationId = serializationId | ||
packedStatus.count = 1 | ||
if (options.sharedPack) { | ||
let sharedCount = packedStatus.sharedCount = (packedStatus.sharedCount || 0) + 1 | ||
if (shareCount > (options.sharedPack.threshold || 5)) { | ||
let sharedPosition = packedStatus.position = packedStatus.nextSharedPosition | ||
hasSharedUpdate = true | ||
if (sharedPosition < 16) | ||
target[position++] = sharedPosition + 0xc0 | ||
} | ||
} | ||
} // else any in-doc incrementation?*/if(l&&!a.pack){let a=l.get(c);a?a.count++:l.set(c,{count:1})}}let b,e=c.length;b=32>e?1:256>e?2:65536>e?3:5;let f=3*e;if(ta+f>sa&&(qa=x(ta+f)),64>e||!h){let a,f,g,h=ta+b;for(a=0;a<e;a++)f=c.charCodeAt(a),128>f?qa[h++]=f:2048>f?(qa[h++]=192|f>>6,qa[h++]=128|63&f):55296==(64512&f)&&56320==(64512&(g=c.charCodeAt(a+1)))?(f=65536+((1023&f)<<10)+(1023&g),a++,qa[h++]=240|f>>18,qa[h++]=128|63&f>>12,qa[h++]=128|63&f>>6,qa[h++]=128|63&f):(qa[h++]=224|f>>12,qa[h++]=128|63&f>>6,qa[h++]=128|63&f);d=h-ta-b}else d=h(c,ta+b,f);24>d?qa[ta++]=96|d:256>d?(2>b&&qa.copyWithin(ta+2,ta+1,ta+1+d),qa[ta++]=120,qa[ta++]=d):65536>d?(3>b&&qa.copyWithin(ta+3,ta+2,ta+2+d),qa[ta++]=121,qa[ta++]=d>>8,qa[ta++]=255&d):(5>b&&qa.copyWithin(ta+5,ta+3,ta+3+d),qa[ta++]=122,ra.setUint32(ta,d),ta+=4),ta+=d}else if("number"===e){if(c>>>0===c)24>c?qa[ta++]=c:256>c?(qa[ta++]=24,qa[ta++]=c):65536>c?(qa[ta++]=25,qa[ta++]=c>>8,qa[ta++]=255&c):(qa[ta++]=26,ra.setUint32(ta,c),ta+=4);else if(c>>0===c)-24<=c?qa[ta++]=31-c:-256<=c?(qa[ta++]=56,qa[ta++]=~c):-65536<=c?(qa[ta++]=57,ra.setUint16(ta,~c),ta+=2):(qa[ta++]=58,ra.setUint32(ta,~c),ta+=4);else{let a;if(0<(a=this.useFloat32)&&4294967296>c&&-2147483648<=c){qa[ta++]=250,ra.setFloat32(ta,c);let b;if(4>a||// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved | ||
(b=c*aa[(127&qa[ta])<<1|qa[ta+1]>>7])>>0===b)return void(ta+=4);// move back into position for writing a double | ||
ta--}qa[ta++]=251,ra.setFloat64(ta,c),ta+=8}}else if("object"===e){if(!c)qa[ta++]=246;else{if(f){let a=f.get(c);if(a){if(!a.id){let b=f.idsToInsert||(f.idsToInsert=[]);a.id=b.push(a)}return qa[ta++]=217,qa[ta++]=156,qa[ta++]=74,qa[ta++]=26,ra.setUint32(ta,a.id),void(ta+=4)}f.set(c,{offset:ta-b})}let a=c.constructor;if(a===Object)w(c,!0);else if(a===Array){d=c.length,24>d?qa[ta++]=128|d:q(d);for(let a=0;a<d;a++)v(c[a])}else if(a===Map){(this.mapsAsObjects?!1!==this.useTag259ForMaps:this.useTag259ForMaps)&&(qa[ta++]=217,qa[ta++]=1,qa[ta++]=3),d=c.size,24>d?qa[ta++]=160|d:256>d?(qa[ta++]=184,qa[ta++]=d):65536>d?(qa[ta++]=185,qa[ta++]=d>>8,qa[ta++]=255&d):(qa[ta++]=186,ra.setUint32(ta,d),ta+=4);for(let[a,b]of c)v(a),v(b)}else{for(let a,b=0,d=ia.length;b<d;b++)if(a=ja[b],c instanceof a){let a=ia[b],d=a.tag;return 24>d?qa[ta++]=192|d:256>d?(qa[ta++]=216,qa[ta++]=d):65536>d?(qa[ta++]=217,qa[ta++]=d>>8,qa[ta++]=255&d):-1<d&&(qa[ta++]=218,ra.setUint32(ta,d),ta+=4),void a.encode.call(this,c,v,x)}if(c[Symbol.iterator]){qa[ta++]=159;// indefinite length array | ||
for(let a of c)v(a);// stop-code | ||
return void(qa[ta++]=255)}// no extension found, write as object | ||
w(c,!c.hasOwnProperty)}}}else if("boolean"===e)qa[ta++]=c?245:244;else if("bigint"===e){if(c<BigInt(1)<<BigInt(64)&&0<=c)qa[ta++]=27,ra.setBigUint64(ta,c);else if(c>-(BigInt(1)<<BigInt(64))&&0>c)qa[ta++]=59,ra.setBigUint64(ta,-c-BigInt(1));else// overflow | ||
if(this.largeBigIntToFloat)qa[ta++]=251,ra.setFloat64(ta,+c);else throw new RangeError(c+" was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64");ta+=8}else if("undefined"===e)qa[ta++]=247;else throw new Error("Unknown type: "+e)},w=!1===this.useRecords?this.variableMapSize?a=>{// this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects) | ||
let b=Object.keys(a),c=b.length;24>c?qa[ta++]=160|c:256>c?(qa[ta++]=184,qa[ta++]=c):65536>c?(qa[ta++]=185,qa[ta++]=c>>8,qa[ta++]=255&c):(qa[ta++]=186,ra.setUint32(ta,c),ta+=4);let d;for(let e=0;e<c;e++)v(d=b[e]),v(a[d])}:(a,c)=>{qa[ta++]=185;// always use map 16, so we can preallocate and set the length afterwards | ||
let d=ta-b;ta+=2;let e=0;for(let b in a)(c||a.hasOwnProperty(b))&&(v(b),v(a[b]),e++);qa[d++ +b]=e>>8,qa[d+b]=255&e}:/* sharedStructures ? // For highly stable structures, using for-in can a little bit faster | ||
(object, safePrototype) => { | ||
@@ -98,14 +114,14 @@ let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null)) | ||
target[objectOffset + start] = id | ||
}*/a=>{let b,f=Object.keys(a),g=e.transitions||(e.transitions=Object.create(null)),h=0,k=f.length;for(let c,d=0;d<k;d++)c=f[d],b=g[c],b||(b=g[c]=Object.create(null),h++),g=b;let l=g[pa];if(void 0!==l)// tag two byte | ||
la[oa++]=217,la[oa++]=ia,la[oa++]=l;else if(l=e.nextId++,l||(l=0,e.nextId=1),l>=ja&&(e.nextId=(l=j)+1),g[pa]=l,e[l]=f,c&&c.length<=j)// tag two byte | ||
}*/a=>{let b,f=Object.keys(a),g=e.transitions||(e.transitions=Object.create(null)),h=0,k=f.length;for(let c,d=0;d<k;d++)c=f[d],b=g[c],b||(b=g[c]=Object.create(null),h++),g=b;let l=g[ua];if(void 0!==l)// tag two byte | ||
qa[ta++]=217,qa[ta++]=na,qa[ta++]=l;else if(l=e.nextId++,l||(l=0,e.nextId=1),l>=oa&&(e.nextId=(l=j)+1),g[ua]=l,e[l]=f,c&&c.length<=j)// tag two byte | ||
// tag number | ||
la[oa++]=217,la[oa++]=ia,la[oa++]=l,d=!0;else{la[oa++]=216,la[oa++]=ia,h&&(n+=o*h),m.length>=ja-j&&(m.shift()[pa]=void 0),m.push(g),22>k?la[oa++]=130+k:p(k+2),q(f),la[oa++]=25,la[oa++]=ia,la[oa++]=l;// now write the values | ||
for(let b=0;b<k;b++)q(a[f[b]]);return}24>k?la[oa++]=128|k:p(k);for(let b=0;b<k;b++)q(a[f[b]])},t=a=>{var c=Math.min,d=Math.round,e=Math.max;let f;if(16777216<a){// special handling for really large buffers | ||
if(a-b>ka)throw new Error("Encoded buffer would be larger than maximum buffer size");f=c(ka,4096*d(e((a-b)*(67108864<a?1.25:2),16777216)/4096))}else// faster handling for smaller buffers | ||
f=(e(a-b<<2,la.length-1)>>12)+1<<12;let g=new ga(f);return ma=new DataView(g.buffer,0,f),la.copy?la.copy(g,0,b,a):g.set(la.slice(b,a)),oa-=b,b=0,na=g.length-10,la=g}}useBuffer(a){// this means we are finished using our own buffer and we can write over it safely | ||
la=a,ma=new DataView(la.buffer,la.byteOffset,la.byteLength),oa=0}}ea=[Date,Set,Error,RegExp,ArrayBuffer,ha,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array,BigUint64Array,Int8Array,Int16Array,Int32Array,BigInt64Array,Float32Array,Float64Array],da=[{tag:1,encode(a){let b=a.getTime()/1e3;(this.useTimestamp32||0===a.getMilliseconds())&&0<=b&&4294967296>b?(la[oa++]=26,ma.setUint32(oa,b),oa+=4):(la[oa++]=251,ma.setFloat64(oa,b),oa+=8)}},{tag:258,// https://github.com/input-output-hk/cbor-sets-spec/blob/master/CBOR_SETS.md | ||
qa[ta++]=217,qa[ta++]=na,qa[ta++]=l,d=!0;else{qa[ta++]=216,qa[ta++]=na,h&&(s+=t*h),p.length>=oa-j&&(p.shift()[ua]=void 0),p.push(g),q(k+2),v(f),qa[ta++]=25,qa[ta++]=na,qa[ta++]=l;// now write the values | ||
for(let b=0;b<k;b++)v(a[f[b]]);return}24>k?qa[ta++]=128|k:q(k);for(let b=0;b<k;b++)v(a[f[b]])},x=a=>{var c=Math.min,d=Math.round,e=Math.max;let f;if(16777216<a){// special handling for really large buffers | ||
if(a-b>pa)throw new Error("Encoded buffer would be larger than maximum buffer size");f=c(pa,4096*d(e((a-b)*(67108864<a?1.25:2),16777216)/4096))}else// faster handling for smaller buffers | ||
f=(e(a-b<<2,qa.length-1)>>12)+1<<12;let g=new la(f);return ra=new DataView(g.buffer,0,f),qa.copy?qa.copy(g,0,b,a):g.set(qa.slice(b,a)),ta-=b,b=0,sa=g.length-10,qa=g}}useBuffer(a){// this means we are finished using our own buffer and we can write over it safely | ||
qa=a,ra=new DataView(qa.buffer,qa.byteOffset,qa.byteLength),ta=0}}ja=[Date,Set,Error,RegExp,ArrayBuffer,ma,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array,BigUint64Array,Int8Array,Int16Array,Int32Array,BigInt64Array,Float32Array,Float64Array],ia=[{tag:1,encode(a){let b=a.getTime()/1e3;(this.useTimestamp32||0===a.getMilliseconds())&&0<=b&&4294967296>b?(qa[ta++]=26,ra.setUint32(ta,b),ta+=4):(qa[ta++]=251,ra.setFloat64(ta,b),ta+=8)}},{tag:258,// https://github.com/input-output-hk/cbor-sets-spec/blob/master/CBOR_SETS.md | ||
encode(a,b){let c=Array.from(a);b(c)}},{tag:27,// http://cbor.schmorp.de/generic-object | ||
encode(a,b){b([a.name,a.message])}},{tag:27,// http://cbor.schmorp.de/generic-object | ||
encode(a,b){b(["RegExp",a.source,a.flags])}},{encode(a,b,c){r(a,c)}},{encode(a,b,c){r(a,c)}},q(64),q(68),q(69),q(70),q(71),q(72),q(77),q(78),q(79),q(81),q(82)];let ra=new qa({useRecords:!1});const sa=ra.encode,{NEVER:ta,ALWAYS:ua,DECIMAL_ROUND:va,DECIMAL_FIT:wa}=_,xa=1e3;a.ALWAYS=ua,a.DECIMAL_FIT=wa,a.DECIMAL_ROUND=va,a.Decoder=N,a.Encoder=qa,a.FLOAT32_OPTIONS=_,a.NEVER=ta,a.Tag=S,a.addExtension=t,a.clearSource=n,a.decode=Z,a.decodeIter=function(a,b={}){if(!a||"object"!=typeof a)throw new Error("first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise");const c=new N(b);let d;const e=a=>{let b;// if there's incomplete data from previous chunk, concatinate and try again | ||
d&&(a=Buffer.concat([d,a]),d=void 0);try{b=c.decodeMultiple(a)}catch(c){if(c.incomplete)d=a.slice(c.lastPosition),b=c.values;else throw c}return b};if("function"==typeof a[Symbol.iterator])return function*(){for(const b of a)yield*e(b)}();return"function"==typeof a[Symbol.asyncIterator]?async function*(){for await(const b of a)yield*e(b)}():void 0},a.decodeMultiple=$,a.encode=sa,a.encodeIter=/** | ||
encode(a,b){b(["RegExp",a.source,a.flags])}},{encode(a,b,c){t(a,c)}},{encode(a,b,c){t(a,c)}},s(64),s(68),s(69),s(70),s(71),s(72),s(77),s(78),s(79),s(81),s(82)];let wa=new va({useRecords:!1});const xa=wa.encode,{NEVER:ya,ALWAYS:za,DECIMAL_ROUND:Aa,DECIMAL_FIT:Ba}=ea,Ca=1e3;a.ALWAYS=za,a.DECIMAL_FIT=Ba,a.DECIMAL_ROUND=Aa,a.Decoder=R,a.Encoder=va,a.FLOAT32_OPTIONS=ea,a.NEVER=ya,a.Tag=W,a.addExtension=v,a.clearSource=o,a.decode=ca,a.decodeIter=function(a,b={}){if(!a||"object"!=typeof a)throw new Error("first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise");const c=new R(b);let d;const e=a=>{let b;// if there's incomplete data from previous chunk, concatinate and try again | ||
d&&(a=Buffer.concat([d,a]),d=void 0);try{b=c.decodeMultiple(a)}catch(c){if(c.incomplete)d=a.slice(c.lastPosition),b=c.values;else throw c}return b};if("function"==typeof a[Symbol.iterator])return function*(){for(const b of a)yield*e(b)}();return"function"==typeof a[Symbol.asyncIterator]?async function*(){for await(const b of a)yield*e(b)}():void 0},a.decodeMultiple=da,a.encode=xa,a.encodeIter=/** | ||
* Given an Iterable first argument, returns an Iterable where each value is encoded as a Buffer | ||
@@ -116,2 +132,2 @@ * If the argument is only Async Iterable, the return value will be an Async Iterable. | ||
* @returns {IterableIterator|Promise.<AsyncIterableIterator>} | ||
*/function(a,b={}){if(!a||"object"!=typeof a)throw new Error("first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable");else{if("function"==typeof a[Symbol.iterator])return u(a,b);if("function"==typeof a.then||"function"==typeof a[Symbol.asyncIterator])return v(a,b);throw new Error("first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise")}},a.mapsAsObjects=!0,a.roundFloat32=function(a){ba[0]=a;let b=X[(127&ca[3])<<1|ca[2]>>7];return(b*a+(0<a?.5:-.5)>>0)/b},a.useRecords=!1,Object.defineProperty(a,"__esModule",{value:!0})}); | ||
*/function(a,b={}){if(!a||"object"!=typeof a)throw new Error("first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable");else{if("function"==typeof a[Symbol.iterator])return w(a,b);if("function"==typeof a.then||"function"==typeof a[Symbol.asyncIterator])return x(a,b);throw new Error("first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise")}},a.mapsAsObjects=!0,a.roundFloat32=function(a){ga[0]=a;let b=aa[(127&ha[3])<<1|ha[2]>>7];return(b*a+(0<a?.5:-.5)>>0)/b},a.useRecords=!1,Object.defineProperty(a,"__esModule",{value:!0})}); |
@@ -686,2 +686,51 @@ (function (CBOR, chai) { | ||
}); | ||
test('encode/decode sample data with packing', function(){ | ||
var data = sampleData; | ||
let encoder = new Encoder({ pack: true, useRecords: false }); | ||
var serialized = encoder.encode(data); | ||
var deserialized = encoder.decode(serialized); | ||
assert.deepEqual(deserialized, data); | ||
}); | ||
test('encode/decode sample data with packing and records', function(){ | ||
var data = sampleData; | ||
let structures = []; | ||
let encoder = new Encoder({ structures, pack: true, useRecords: true }); | ||
var serialized = encoder.encode(data); | ||
var deserialized = encoder.decode(serialized); | ||
assert.deepEqual(deserialized, data); | ||
}); | ||
test('encode/decode sample data with shared packing and records', function(){ | ||
let encoder = new Encoder({ useRecords: true }); | ||
let finishPack = encoder.findCommonStringsToPack(); | ||
for (let i = 0; i < 20; i++) { | ||
let data = { | ||
shouldShare: 'same each time', | ||
shouldShare2: 'same each time 2', | ||
shouldntShare: 'different each time ' + i | ||
}; | ||
if (i == 10) | ||
finishPack({}); | ||
var serialized = encoder.encode(data); | ||
var deserialized = encoder.decode(serialized); | ||
assert.deepEqual(deserialized, data); | ||
} | ||
}); | ||
test('encode/decode sample data with individual packing, shared packing and records', function(){ | ||
let encoder = new Encoder({ pack: true, useRecords: true }); | ||
let finishPack = encoder.findCommonStringsToPack(); | ||
for (let i = 0; i < 20; i++) { | ||
let data = { | ||
shouldShare: 'same each time', | ||
shouldShare2: 'same each time', | ||
shouldntShare: 'different each time ' + i, | ||
shouldntShare2: 'different each time ' + i, | ||
noPack: 'no packing ' + i, | ||
}; | ||
if (i == 10) | ||
finishPack({ threshold: 5 }); | ||
var serialized = encoder.encode(data); | ||
var deserialized = encoder.decode(serialized); | ||
assert.deepEqual(deserialized, data); | ||
} | ||
}); | ||
if (typeof Buffer != 'undefined') | ||
@@ -688,0 +737,0 @@ test('replace data', function(){ |
159
encode.js
@@ -0,1 +1,2 @@ | ||
import { write } from 'fs' | ||
import { Decoder, mult10, Tag, typedArrays, addExtension as decodeAddExtension } from './decode.js' | ||
@@ -13,2 +14,3 @@ let textEncoder | ||
const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000 | ||
let serializationId = 1 | ||
let target | ||
@@ -29,2 +31,3 @@ let targetView | ||
let referenceMap | ||
options = options || {} | ||
let lastSharedStructuresLength = 0 | ||
@@ -40,3 +43,3 @@ let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position, maxBytes) { | ||
let maxSharedStructures = 64 | ||
let isSequential = options && options.sequential | ||
let isSequential = options.sequential | ||
if (isSequential) { | ||
@@ -46,2 +49,11 @@ maxSharedStructures = 0 | ||
} | ||
let samplingPackedValues, packedObjectMap, sharedValues = options.sharedValues | ||
let sharedPackedObjectMap | ||
if (sharedValues) { | ||
sharedPackedObjectMap = Object.create(null) | ||
for (let i = 0, l = sharedValues.length; i < l; i++) { | ||
sharedPackedObjectMap[sharedValues[i]] = i | ||
} | ||
} | ||
let recordIdsToRemove = [] | ||
@@ -101,2 +113,25 @@ let transitionsCount = 0 | ||
structures = sharedStructures || [] | ||
packedObjectMap = sharedPackedObjectMap | ||
if (options.pack) { | ||
let packedValues = new Map() | ||
packedValues.values = [] | ||
packedValues.encoder = encoder | ||
packedValues.maxValues = options.maxPrivatePackedValues || (sharedPackedObjectMap ? 16 : Infinity) | ||
packedValues.objectMap = sharedPackedObjectMap || false | ||
packedValues.samplingPackedValues = samplingPackedValues | ||
findRepetitiveStrings(value, packedValues) | ||
if (packedValues.values.length > 0) { | ||
target[position++] = 0xd8 // one-byte tag | ||
target[position++] = 51 // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt | ||
writeArrayHeader(4) | ||
let valuesArray = packedValues.values | ||
encode(valuesArray) | ||
writeArrayHeader(0) // prefixes | ||
writeArrayHeader(0) // suffixes | ||
packedObjectMap = Object.create(sharedPackedObjectMap || null) | ||
for (let i = 0, l = valuesArray.length; i < l; i++) { | ||
packedObjectMap[valuesArray[i]] = i | ||
} | ||
} | ||
} | ||
try { | ||
@@ -143,2 +178,7 @@ encode(value) | ||
let returnBuffer = target.subarray(start, position) | ||
let shared = encoder.structures || [] | ||
if (sharedValues) { | ||
shared = shared.concat(sharedValues) | ||
} | ||
if (encoder.saveStructures(encoder.structures, lastSharedStructuresLength) === false) { | ||
@@ -149,3 +189,3 @@ // get updated structures and try again if the update failed | ||
} | ||
lastSharedStructuresLength = encoder.structures.length | ||
lastSharedStructuresLength = shared.length | ||
return returnBuffer | ||
@@ -156,2 +196,21 @@ } | ||
} | ||
this.findCommonStringsToPack = () => { | ||
samplingPackedValues = new Map() | ||
if (!sharedPackedObjectMap) | ||
sharedPackedObjectMap = Object.create(null) | ||
return ({ threshold }) => { | ||
threshold = threshold || 4 | ||
let position = this.pack ? options.maxPrivatePackedValues || 16 : 0 | ||
if (!sharedValues) | ||
sharedValues = this.sharedValues = [] | ||
for (let [ key, status ] of samplingPackedValues) { | ||
if (status.count > threshold) { | ||
sharedPackedObjectMap[key] = position++ | ||
sharedValues.push(key) | ||
hasSharedUpdate = true | ||
} | ||
} | ||
samplingPackedValues = null | ||
} | ||
} | ||
const encode = (value) => { | ||
@@ -164,2 +223,39 @@ if (position > safeEnd) | ||
if (type === 'string') { | ||
if (packedObjectMap) { | ||
let packedPosition = packedObjectMap[value] | ||
if (packedPosition >= 0) { | ||
if (packedPosition < 16) | ||
target[position++] = packedPosition + 0xe0 // simple values, defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt | ||
else { | ||
target[position++] = 0xc6 // tag 6 defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt | ||
if (packedPosition & 1) | ||
encode((15 - packedPosition) >> 1) | ||
else | ||
encode((packedPosition - 16) >> 1) | ||
} | ||
return | ||
/* } else if (packedStatus.serializationId != serializationId) { | ||
packedStatus.serializationId = serializationId | ||
packedStatus.count = 1 | ||
if (options.sharedPack) { | ||
let sharedCount = packedStatus.sharedCount = (packedStatus.sharedCount || 0) + 1 | ||
if (shareCount > (options.sharedPack.threshold || 5)) { | ||
let sharedPosition = packedStatus.position = packedStatus.nextSharedPosition | ||
hasSharedUpdate = true | ||
if (sharedPosition < 16) | ||
target[position++] = sharedPosition + 0xc0 | ||
} | ||
} | ||
} // else any in-doc incrementation?*/ | ||
} else if (samplingPackedValues && !options.pack) { | ||
let status = samplingPackedValues.get(value) | ||
if (status) | ||
status.count++ | ||
else | ||
samplingPackedValues.set(value, { | ||
count: 1, | ||
}) | ||
} | ||
} | ||
let strLength = value.length | ||
@@ -533,6 +629,3 @@ let headerSize | ||
recordIdsToRemove.push(transition) | ||
if (length < 0x16) | ||
target[position++] = 0x82 + length // array header, length of values + 2 | ||
else | ||
writeArrayHeader(length + 2) | ||
writeArrayHeader(length + 2) | ||
encode(keys) | ||
@@ -593,3 +686,5 @@ target[position++] = 0x19 // uint16 | ||
function writeArrayHeader(length) { | ||
if (length < 0x100) { | ||
if (length < 0x18) | ||
target[position++] = 0x80 | length | ||
else if (length < 0x100) { | ||
target[position++] = 0x98 | ||
@@ -608,2 +703,52 @@ target[position++] = length | ||
function findRepetitiveStrings(value, packedValues) { | ||
switch(typeof value) { | ||
case 'string': | ||
if (value.length > 3) { | ||
if (packedValues.objectMap[value] > -1 || packedValues.values.length >= packedValues.maxValues) | ||
return | ||
let packedStatus = packedValues.get(value) | ||
if (packedStatus) { | ||
if (++packedStatus.count == 2) { | ||
packedValues.values.push(value) | ||
} | ||
} else { | ||
packedValues.set(value, { | ||
count: 1, | ||
}) | ||
if (packedValues.samplingPackedValues) { | ||
let status = packedValues.samplingPackedValues.get(value) | ||
if (status) | ||
status.count++ | ||
else | ||
packedValues.samplingPackedValues.set(value, { | ||
count: 1, | ||
}) | ||
} | ||
} | ||
} | ||
break | ||
case 'object': | ||
if (value) { | ||
if (value instanceof Array) { | ||
for (let i = 0, l = value.length; i < l; i++) { | ||
findRepetitiveStrings(value[i], packedValues) | ||
} | ||
} else { | ||
let includeKeys = !packedValues.encoder.useRecords | ||
for (var key in value) { | ||
if (value.hasOwnProperty(key)) { | ||
if (includeKeys) | ||
findRepetitiveStrings(key, packedValues) | ||
findRepetitiveStrings(value[key], packedValues) | ||
} | ||
} | ||
} | ||
} | ||
break | ||
case 'function': console.log(value) | ||
} | ||
} | ||
extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, ByteArray, | ||
@@ -610,0 +755,0 @@ Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array, BigUint64Array, Int8Array, Int16Array, Int32Array, BigInt64Array, |
{ | ||
"name": "cbor-x", | ||
"author": "Kris Zyp", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"description": "Ultra-fast CBOR implementation with tag extensions for records and structured cloning", | ||
@@ -67,4 +67,5 @@ "license": "MIT", | ||
"rollup": "^1.20.3", | ||
"@rollup/plugin-json": "^4.1.0", | ||
"rollup-plugin-babel-minify": "^9.0.0" | ||
} | ||
} |
@@ -127,3 +127,3 @@ # cbor-x | ||
### Reading Multiple Values | ||
If you have a buffer with multiple values sequentially encoded, you can choose to parse and read multiple values. This can be done using the `unpackMultiple` function/method, which can return an array of all the values it can sequentially parse within the provided buffer. For example: | ||
If you have a buffer with multiple values sequentially encoded, you can choose to parse and read multiple values. This can be done using the `decodeMultiple` function/method, which can return an array of all the values it can sequentially parse within the provided buffer. For example: | ||
```js | ||
@@ -142,2 +142,7 @@ let data = new Uint8Array([1, 2, 3]) // encodings of values 1, 2, and 3 | ||
CBOR Packing | ||
[Packed CBOR](https://datatracker.ietf.org/doc/html/draft-ietf-cbor-packed) is additional specification for CBOR which allows for compact encoding of data that has repeated values. Cbor-x supports decoding packed CBOR, no or flags/options needed. Cbor-x can also optionally generate packed CBOR (with the `pack` option), which will cause the encoder to look for repeated strings in a data structure that is being encoded, and store the strings in a packed table that can be referenced, to reduce encoding size. This involves extra overhead and reduces encoding performance, and generally does not yield as much compaction as standard compression tools. However, this is can be much faster than encoding plus compression, while still providing some level of reduction in encoding size. In addition to size reduction, packed CBOR is also usually faster to decode (assuming that some repetitive values could be found/packed). | ||
Cbor-x also has in-progress effort to support shared packed tables. | ||
## Options | ||
@@ -151,2 +156,3 @@ The following options properties can be provided to the Encoder or Decoder constructor: | ||
* `useFloat32` - This will enable cbor-x to encode non-integer numbers as 32-bit (4 byte) floating point numbers. See next section for possible values. | ||
* `pack` - This will enable [CBOR packing](https://datatracker.ietf.org/doc/html/draft-ietf-cbor-packed) for encoding, as described above. | ||
* `variableMapSize` - This will use varying map size definition (from single-byte to full 32-bit representation) based on the number of keys when encoding objects, which yields slightly more compact encodings (for small objects), but is typically 5-10% slower during encoding. This is only relevant when record extension is disabled. | ||
@@ -153,0 +159,0 @@ * `copyBuffers` - When decoding a CBOR message with binary data (Buffers are encoded as binary data), copy the buffer rather than providing a slice/view of the buffer. If you want your input data to be collected or modified while the decoded embedded buffer continues to live on, you can use this option (there is extra overhead to copying). |
@@ -7,3 +7,3 @@ # Security Policy | ||
| ------- | ------------------ | | ||
| 1.4.x | :white_check_mark: | | ||
| 0.9.x | :white_check_mark: | | ||
@@ -10,0 +10,0 @@ ## Reporting a Vulnerability |
@@ -111,2 +111,51 @@ import * as CBOR from '../index.js' | ||
}) | ||
test('encode/decode sample data with packing', function(){ | ||
var data = sampleData | ||
let encoder = new Encoder({ pack: true, useRecords: false }) | ||
var serialized = encoder.encode(data) | ||
var deserialized = encoder.decode(serialized) | ||
assert.deepEqual(deserialized, data) | ||
}) | ||
test('encode/decode sample data with packing and records', function(){ | ||
var data = sampleData | ||
let structures = [] | ||
let encoder = new Encoder({ structures, pack: true, useRecords: true }) | ||
var serialized = encoder.encode(data) | ||
var deserialized = encoder.decode(serialized) | ||
assert.deepEqual(deserialized, data) | ||
}) | ||
test('encode/decode sample data with shared packing and records', function(){ | ||
let encoder = new Encoder({ useRecords: true }) | ||
let finishPack = encoder.findCommonStringsToPack() | ||
for (let i = 0; i < 20; i++) { | ||
let data = { | ||
shouldShare: 'same each time', | ||
shouldShare2: 'same each time 2', | ||
shouldntShare: 'different each time ' + i | ||
} | ||
if (i == 10) | ||
finishPack({}) | ||
var serialized = encoder.encode(data) | ||
var deserialized = encoder.decode(serialized) | ||
assert.deepEqual(deserialized, data) | ||
} | ||
}) | ||
test('encode/decode sample data with individual packing, shared packing and records', function(){ | ||
let encoder = new Encoder({ pack: true, useRecords: true }) | ||
let finishPack = encoder.findCommonStringsToPack() | ||
for (let i = 0; i < 20; i++) { | ||
let data = { | ||
shouldShare: 'same each time', | ||
shouldShare2: 'same each time', | ||
shouldntShare: 'different each time ' + i, | ||
shouldntShare2: 'different each time ' + i, | ||
noPack: 'no packing ' + i, | ||
} | ||
if (i == 10) | ||
finishPack({ threshold: 5 }) | ||
var serialized = encoder.encode(data) | ||
var deserialized = encoder.decode(serialized) | ||
assert.deepEqual(deserialized, data) | ||
} | ||
}) | ||
if (typeof Buffer != 'undefined') | ||
@@ -113,0 +162,0 @@ test('replace data', function(){ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
429767
8556
302
10
3
13
3