cbor-x 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/launch.json +22 -0
- package/README.md +49 -45
- package/SECURITY.md +11 -0
- package/{unpack.d.ts → decode.d.ts} +12 -11
- package/decode.js +86 -33
- package/dist/index.js +194 -49
- package/dist/index.min.js +65 -51
- package/dist/node.cjs +228 -81
- package/dist/test.js +33 -1
- package/encode.d.ts +6 -0
- package/encode.js +31 -18
- package/index.d.ts +7 -6
- package/index.js +2 -2
- package/iterators.js +85 -0
- package/node-index.js +24 -0
- package/package.json +16 -17
- package/rollup.config.js +5 -5
- package/stream.js +20 -22
- package/tests/benchmark.cjs +13 -7
- package/tests/{test-compatibility.js → test-compatibility.cjs} +7 -12
- package/tests/test-incomplete.js +41 -0
- package/tests/test-node-iterators.js +72 -0
- package/tests/test-node-stream.js +17 -5
- package/tests/test.js +33 -2
- package/index.mjs +0 -35
- package/node.js +0 -25
- package/pack.d.ts +0 -9
- package/tests/test.mjs +0 -31
package/dist/index.min.js
CHANGED
|
@@ -1,53 +1,61 @@
|
|
|
1
|
-
(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports):"function"==typeof define&&define.amd?define(["exports"],b):(a=a||self,b(a.
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
(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
|
|
2
|
+
C=null,x=null,E&&(E=null);else if(z>y){// over read
|
|
3
|
+
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
|
|
4
|
+
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
|
|
5
|
+
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
|
|
6
|
+
switch(b){case 2:// byte string
|
|
4
7
|
case 3:// text string
|
|
5
8
|
case 4:// array
|
|
6
|
-
let a,
|
|
7
|
-
let f;if(
|
|
9
|
+
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
|
|
10
|
+
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
|
|
8
11
|
return a;case 1:// negative int
|
|
9
12
|
return~a;case 2:// buffer
|
|
10
|
-
return
|
|
11
|
-
if(
|
|
12
|
-
let b=16>a?
|
|
13
|
-
let e=Array(a);for(let
|
|
14
|
-
if(
|
|
15
|
-
if(a>>8==
|
|
16
|
-
let b=
|
|
13
|
+
return i(a);case 3:// string
|
|
14
|
+
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
|
|
15
|
+
let b=16>a?h(a):g(a);if(null!=b)return b}return P(a);case 4:// array
|
|
16
|
+
let e=Array(a);for(let b=0;b<a;b++)e[b]=c();return e;case 5:// map
|
|
17
|
+
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
|
|
18
|
+
if(a>>8==A){// record structures
|
|
19
|
+
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
|
|
17
20
|
switch(a){case 20:return!1;case 21:return!0;case 22:return null;case 23:return;// undefined
|
|
18
21
|
case 31:default:throw new Error("Unknown token "+a);}default:// negative int
|
|
19
|
-
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
|
|
20
|
-
if(2<
|
|
21
|
-
let a=
|
|
22
|
-
a&=31;23<a&&(24===a?
|
|
23
|
-
const b=63&
|
|
24
|
-
const b=63&
|
|
25
|
-
const b=63&
|
|
26
|
-
Uint8Array.prototype.slice.call(
|
|
27
|
-
return
|
|
28
|
-
let l=16>a?
|
|
29
|
-
return new
|
|
22
|
+
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
|
|
23
|
+
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
|
|
24
|
+
let a=x[z++];//let majorType = token >> 5
|
|
25
|
+
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
|
|
26
|
+
const b=63&x[z++];d.push((31&a)<<6|b)}else if(224==(240&a)){// 3 bytes
|
|
27
|
+
const b=63&x[z++],c=63&x[z++];d.push((31&a)<<12|b<<6|c)}else if(240==(248&a)){// 4 bytes
|
|
28
|
+
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)
|
|
29
|
+
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)
|
|
30
|
+
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
|
|
31
|
+
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
|
|
32
|
+
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
|
|
30
33
|
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
|
|
31
|
-
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
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
let
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
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)}/**
|
|
35
|
+
* Given an Iterable/Iterator input which yields buffers, returns an IterableIterator which yields sync decoded objects
|
|
36
|
+
* Or, given an Async Iterable/Iterator which yields promises resolving in buffers, returns an AsyncIterableIterator.
|
|
37
|
+
* @param {Iterable|Iterator|AsyncIterable|AsyncIterableIterator} bufferIterator
|
|
38
|
+
* @param {object} [options] - Decoder options
|
|
39
|
+
* @returns {IterableIterator|Promise.<AsyncIterableIterator}
|
|
40
|
+
*/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
|
|
41
|
+
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)
|
|
42
|
+
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
|
|
43
|
+
E.set(a,e);let f=c();// read the next value as the target object to id
|
|
44
|
+
return e.used?Object.assign(b,f):(e.target=f,f);// no cycle, can just use the returned read object
|
|
45
|
+
},K[40010]=a=>{// pointer extension (for structured clones)
|
|
46
|
+
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
|
|
47
|
+
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
|
|
48
|
+
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
|
|
49
|
+
}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
|
|
50
|
+
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
|
|
51
|
+
(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
|
|
52
|
+
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
|
|
44
53
|
for(let b of a)q(b);// stop-code
|
|
45
|
-
return void(
|
|
46
|
-
|
|
47
|
-
if(this.largeBigIntToFloat)
|
|
48
|
-
|
|
49
|
-
let
|
|
50
|
-
let d=ha-b;ha+=2;let e=0;for(let b in a)(c||a.hasOwnProperty(b))&&(q(b),q(a[b]),e++);ea[d++ +b]=e>>8,ea[d+b]=255&e}:/* sharedStructures ? // For highly stable structures, using for-in can a little bit faster
|
|
54
|
+
return void(la[oa++]=255)}// no extension found, write as object
|
|
55
|
+
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
|
|
56
|
+
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)
|
|
57
|
+
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
|
|
58
|
+
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
|
|
51
59
|
(object, safePrototype) => {
|
|
52
60
|
let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null))
|
|
53
61
|
let objectOffset = position++ - start
|
|
@@ -86,15 +94,21 @@ let d=ha-b;ha+=2;let e=0;for(let b in a)(c||a.hasOwnProperty(b))&&(q(b),q(a[b]),
|
|
|
86
94
|
sharedStructures.onUpdate(id, transition.__keys__)
|
|
87
95
|
}
|
|
88
96
|
target[objectOffset + start] = id
|
|
89
|
-
}*/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[
|
|
90
|
-
|
|
97
|
+
}*/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
|
|
98
|
+
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
|
|
91
99
|
// tag number
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
ea=a,fa=new DataView(ea.buffer,ea.byteOffset,ea.byteLength),ha=0}}Z=[Date,Set,Error,RegExp,ArrayBuffer,aa,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array,BigUint64Array,Int8Array,Int16Array,Int32Array,BigInt64Array,Float32Array,Float64Array],Y=[{tag:1,encode(a){let b=a.getTime()/1e3;(this.useTimestamp32||0===a.getMilliseconds())&&0<=b&&4294967296>b?(ea[ha++]=26,fa.setUint32(ha,b),ha+=4):(ea[ha++]=251,fa.setFloat64(ha,b),ha+=8)}},{tag:258,// https://github.com/input-output-hk/cbor-sets-spec/blob/master/CBOR_SETS.md
|
|
100
|
+
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
|
|
101
|
+
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
|
|
102
|
+
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
|
|
103
|
+
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
|
|
104
|
+
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
|
|
98
105
|
encode(a,b){let c=Array.from(a);b(c)}},{tag:27,// http://cbor.schmorp.de/generic-object
|
|
99
106
|
encode(a,b){b([a.name,a.message])}},{tag:27,// http://cbor.schmorp.de/generic-object
|
|
100
|
-
encode(a,b){b(["RegExp",a.source,a.flags])}},{encode(a,b,c){
|
|
107
|
+
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
|
|
108
|
+
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=/**
|
|
109
|
+
* Given an Iterable first argument, returns an Iterable where each value is encoded as a Buffer
|
|
110
|
+
* If the argument is only Async Iterable, the return value will be an Async Iterable.
|
|
111
|
+
* @param {Iterable|Iterator|AsyncIterable|AsyncIterator} objectIterator - iterable source, like a Readable object stream, an array, Set, or custom object
|
|
112
|
+
* @param {options} [options] - cbor-x Encoder options
|
|
113
|
+
* @returns {IterableIterator|Promise.<AsyncIterableIterator>}
|
|
114
|
+
*/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})});
|