cbor-x 0.9.4 → 1.0.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/README.md +56 -21
- package/decode.d.ts +2 -0
- package/decode.js +306 -109
- package/dist/index.js +536 -258
- package/dist/index.min.js +99 -70
- package/dist/node.cjs +538 -253
- package/dist/test.js +51 -7
- package/encode.d.ts +1 -1
- package/encode.js +232 -86
- package/index.js +0 -2
- package/package.json +13 -4
- package/.vscode/launch.json +0 -22
- package/dist/index.js.map +0 -1
- package/tests/benchmark-stream.cjs +0 -282
- package/tests/benchmark.cjs +0 -245
- package/tests/example.json +0 -52
- package/tests/example2.json +0 -26
- package/tests/example3.json +0 -22
- package/tests/example4.json +0 -1
- package/tests/example5.json +0 -12
- package/tests/floats.json +0 -1
- package/tests/index.html +0 -28
- package/tests/sample-large.json +0 -231
- package/tests/strings2.json +0 -1
- package/tests/test-compatibility.cjs +0 -36
- package/tests/test-incomplete.js +0 -41
- package/tests/test-node-iterators.js +0 -72
- package/tests/test-node-stream.js +0 -60
- package/tests/test.js +0 -596
package/dist/index.min.js
CHANGED
|
@@ -1,59 +1,84 @@
|
|
|
1
|
-
(function(
|
|
2
|
-
|
|
3
|
-
let
|
|
4
|
-
return
|
|
5
|
-
let
|
|
6
|
-
switch(
|
|
1
|
+
(function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):(e=e||self,t(e.CBOR={}))})(this,function(e){'use strict';var t=Math.log,s=Math.exp;function n(){try{let e=a();if(T&&(// bundled strings to skip past
|
|
2
|
+
x=T.postBundlePosition),x==j)D=null,_=null,w&&(w=null);else if(x>j){// over read
|
|
3
|
+
let e=new Error("Unexpected end of CBOR data");throw e.incomplete=!0,e}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
|
|
4
|
+
return e}catch(e){throw m(),(e instanceof RangeError||e.message.startsWith("Unexpected end of buffer"))&&(e.incomplete=!0),e}}function a(){let e=_[x++],t=e>>5;if(e&=31,23<e)switch(e){case 24:e=_[x++];break;case 25:if(7==t)return c();e=z.getUint16(x),x+=2;break;case 26:if(7==t){let e=z.getFloat32(x);if(2<Y.useFloat32){// this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
5
|
+
let t=ue[(127&_[x])<<1|_[x+1]>>7];return x+=4,(t*e+(0<e?.5:-.5)>>0)/t}return x+=4,e}e=z.getUint32(x),x+=4;break;case 27:if(7==t){let e=z.getFloat64(x);return x+=8,e}Y.int64AsNumber?(e=4294967296*z.getUint32(x),e+=z.getUint32(x+4)):e=z.getBigUint64(x),x+=8;break;case 31:// indefinite length
|
|
6
|
+
switch(t){case 2:// byte string
|
|
7
7
|
case 3:// text string
|
|
8
|
-
case 4:// array
|
|
9
|
-
let
|
|
10
|
-
let
|
|
11
|
-
return
|
|
12
|
-
return~
|
|
13
|
-
return
|
|
14
|
-
if(
|
|
15
|
-
let
|
|
16
|
-
let
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if(
|
|
23
|
-
if(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
let
|
|
34
|
-
|
|
8
|
+
throw new Error("Indefinite length not supported for byte or text strings");case 4:// array
|
|
9
|
+
let e,s=[],n=0;for(;(e=a())!=F;)s[n++]=e;return 4==t?s:3==t?s.join(""):Buffer.concat(s);case 5:// map
|
|
10
|
+
let r;if(Y.mapsAsObjects){let e={};if(Y.keyMap)for(;(r=a())!=F;)e[Y.decodeKey(r)]=a();else for(;(r=a())!=F;)e[r]=a();return e}else{W&&(Y.mapsAsObjects=!0,W=!1);let e=new Map;if(Y.keyMap)for(;(r=a())!=F;)e.set(Y.decodeKey(r),a());else for(;(r=a())!=F;)e.set(r,a());return e}case 7:return F;default:throw new Error("Invalid major type for indefinite length "+t);}default:throw new Error("Unknown token "+e);}switch(t){case 0:// positive int
|
|
11
|
+
return e;case 1:// negative int
|
|
12
|
+
return~e;case 2:// buffer
|
|
13
|
+
return u(e);case 3:// string
|
|
14
|
+
if(G>=x)return K.slice(x-q,(x+=e)-q);if(0==G&&140>j&&32>e){// for small blocks, avoiding the overhead of the extract call is helpful
|
|
15
|
+
let t=16>e?d(e):o(e);if(null!=t)return t}return ee(e);case 4:// array
|
|
16
|
+
let s=Array(e);//if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
|
|
17
|
+
//else
|
|
18
|
+
for(let t=0;t<e;t++)s[t]=a();return s;case 5:// map
|
|
19
|
+
if(Y.mapsAsObjects){let t={};if(Y.keyMap)for(let s=0;s<e;s++)t[Y.decodeKey(a())]=a();else for(let s=0;s<e;s++)t[a()]=a();return t}else{W&&(Y.mapsAsObjects=!0,W=!1);let t=new Map;if(Y.keyMap)for(let s=0;s<e;s++)t.set(Y.decodeKey(a()),a());else for(let s=0;s<e;s++)t.set(a(),a());return t}case 6:// extension
|
|
20
|
+
if(e>=L){let t=D[8191&e];// check record structures first
|
|
21
|
+
// At some point we may provide an option for dynamic tag assignment with a range like token >= 8 && (token < 16 || (token > 0x80 && token < 0xc0) || (token > 0x130 && token < 0x4000))
|
|
22
|
+
if(t)return t.read||(t.read=r(t)),t.read();if(65536>e){if(e==P)// we do a special check for this so that we can keep the currentExtensions as densely stored array (v8 stores arrays densely under about 3000 elements)
|
|
23
|
+
return re(a());if(e==C){let e=h(),t=a();for(let s=2;s<e;s++)re([t++,a()]);return a()}if(e==L)return g();if(Y.getShared&&(y(),t=D[8191&e],t))return t.read||(t.read=r(t)),t.read()}}let n=$[e];if(n)return n.handlesRead?n(a):n(a());else{let t=a();for(let s,n=0;n<H.length;n++)if(s=H[n](e,t),void 0!==s)return s;return new ne(t,e)}case 7:// fixed value
|
|
24
|
+
switch(e){case 20:return!1;case 21:return!0;case 22:return null;case 23:return;// undefined
|
|
25
|
+
case 31:default:let t=(N||p())[e];if(void 0!==t)return t;throw new Error("Unknown token "+e);}default:// negative int
|
|
26
|
+
if(isNaN(e)){let e=new Error("Unexpected end of CBOR data");throw e.incomplete=!0,e}throw new Error("Unknown CBOR token "+e);}}function r(e){function t(){// get the array size from the header
|
|
27
|
+
let e=_[x++];//let majorType = token >> 5
|
|
28
|
+
if(e&=31,23<e)switch(e){case 24:e=_[x++];break;case 25:e=z.getUint16(x),x+=2;break;case 26:e=z.getUint32(x),x+=4;break;default:throw new Error("Expected array header, but got "+_[x-1]);}// This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
|
|
29
|
+
if(this.objectLiteralSize===e)// we have a fast object literal reader, use it (assuming it is the right length)
|
|
30
|
+
return this.objectLiteral(a);if(3==this.count++)return this.objectLiteralSize=e,this.objectLiteral=Y.keyMap?new Function("r","return {"+this.map(e=>Y.decodeKey(e)).map(e=>Z.test(e)?e+":r()":"["+JSON.stringify(e)+"]:r()").join(",")+"}"):new Function("r","return {"+this.map(e=>Z.test(e)?e+":r()":"["+JSON.stringify(e)+"]:r()").join(",")+"}"),this.objectLiteral(a);let t={};if(Y.keyMap)for(let s=0;s<e;s++)t[Y.decodeKey(this[s])]=a();else for(let s=0;s<e;s++)t[this[s]]=a();return t}return e.count=0,t}function i(e){let t;if(16>e&&(t=d(e)))return t;if(64<e&&E)return E.decode(_.subarray(x,x+=e));const s=x+e,n=[];for(t="";x<s;){const e=_[x++];if(0==(128&e))n.push(e);else if(192==(224&e)){// 2 bytes
|
|
31
|
+
const t=63&_[x++];n.push((31&e)<<6|t)}else if(224==(240&e)){// 3 bytes
|
|
32
|
+
const t=63&_[x++],s=63&_[x++];n.push((31&e)<<12|t<<6|s)}else if(240==(248&e)){// 4 bytes
|
|
33
|
+
const t=63&_[x++],s=63&_[x++],a=63&_[x++];let r=(7&e)<<18|t<<12|s<<6|a;65535<r&&(r-=65536,n.push(55296|1023&r>>>10),r=56320|1023&r),n.push(r)}else n.push(e);4096<=n.length&&(t+=te.apply(String,n),n.length=0)}return 0<n.length&&(t+=te.apply(String,n)),t}function o(e){let t=x,s=Array(e);for(let n=0;n<e;n++){const e=_[x++];if(0<(128&e))return void(x=t);s[n]=e}return te.apply(String,s)}function d(t){if(4>t){if(!(2>t)){let e=_[x++],s=_[x++];if(0<(128&e)||0<(128&s))return void(x-=2);if(3>t)return te(e,s);let n=_[x++];return 0<(128&n)?void(x-=3):te(e,s,n)}if(0===t)return"";else{let e=_[x++];return 1<(128&e)?void(x-=1):te(e)}}else{let s=_[x++],a=_[x++],r=_[x++],u=_[x++];if(0<(128&s)||0<(128&a)||0<(128&r)||0<(128&u))return void(x-=4);if(6>t){if(4===t)return te(s,a,r,u);else{let t=_[x++];return 0<(128&t)?void(x-=5):te(s,a,r,u,t)}}else if(8>t){let n=_[x++],e=_[x++];if(0<(128&n)||0<(128&e))return void(x-=6);if(7>t)return te(s,a,r,u,n,e);let i=_[x++];return 0<(128&i)?void(x-=7):te(s,a,r,u,n,e,i)}else{let d=_[x++],e=_[x++],c=_[x++],p=_[x++];if(0<(128&d)||0<(128&e)||0<(128&c)||0<(128&p))return void(x-=8);if(10>t){if(8===t)return te(s,a,r,u,d,e,c,p);else{let t=_[x++];return 0<(128&t)?void(x-=9):te(s,a,r,u,d,e,c,p,t)}}else if(12>t){let n=_[x++],i=_[x++];if(0<(128&n)||0<(128&i))return void(x-=10);if(11>t)return te(s,a,r,u,d,e,c,p,n,i);let o=_[x++];return 0<(128&o)?void(x-=11):te(s,a,r,u,d,e,c,p,n,i,o)}else{let f=_[x++],i=_[x++],g=_[x++],h=_[x++];if(0<(128&f)||0<(128&i)||0<(128&g)||0<(128&h))return void(x-=12);if(!(14>t)){let l=_[x++],y=_[x++];if(0<(128&l)||0<(128&y))return void(x-=14);if(15>t)return te(s,a,r,u,d,e,c,p,f,i,g,h,l,y);let n=_[x++];return 0<(128&n)?void(x-=15):te(s,a,r,u,d,e,c,p,f,i,g,h,l,y,n)}if(12===t)return te(s,a,r,u,d,e,c,p,f,i,g,h);else{let t=_[x++];return 0<(128&t)?void(x-=13):te(s,a,r,u,d,e,c,p,f,i,g,h,t)}}}}}function u(e){return Y.copyBuffers?// specifically use the copying slice (not the node one)
|
|
34
|
+
Uint8Array.prototype.slice.call(_,x,x+=e):_.subarray(x,x+=e)}function c(){let e,t=_[x++],n=_[x++],a=(t<<8)+n,r=31&a>>10,i=1023&a;return e=0==r?s(i,-24):31==r?0==i?1/0:NaN:s(i+1024,r-25),32768&a?-e:e}function l(e,t){return"string"==typeof e?e+t:e instanceof Array?e.concat(t):Object.assign({},e,t)}function p(){if(!N)if(Y.getShared)y();else throw new Error("No packed values available");return N}function f(e,t){$[t]=t=>{if(!e)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
|
|
35
|
+
return new ae[e](Uint8Array.prototype.slice.call(t,0).buffer)}}function g(){let e=h(),t=x+a();for(let t,s=2;s<e;s++)// skip past bundles that were already read
|
|
36
|
+
// this will increment position, so must add to position afterwards
|
|
37
|
+
t=h(),x+=t;let s=x;return x=t,T=[i(h()),i(h())],T.position0=0,T.position1=0,T.postBundlePosition=x,x=s,a()}function h(){let e=31&_[x++];return 23<e&&(24===e?e=_[x++]:25===e?(e=z.getUint16(x),x+=2):26===e?(e=z.getUint32(x),x+=4):void 0),e}function y(){if(Y.getShared){let e=b(()=>(_=null,Y.getShared()))||{},t=e.structures||[];Y.sharedVersion=e.version,N=Y.sharedValues=e.packedValues,!0===D?Y.structures=D=t:D.splice.apply(D,[0,t.length].concat(t))}}function b(e){let t=j,s=x,n=q,a=G,r=K,i=w,o=T,d=new Uint8Array(_.slice(0,j)),u=D,c=Y,l=Q,p=e();return j=t,x=s,q=n,G=a,K=r,w=i,T=o,_=d,Q=l,D=u,Y=c,z=new DataView(_.buffer,_.byteOffset,_.byteLength),p}function m(){_=null,w=null,D=null}function k(e){$[e.tag]=e.decode}function M(e){24>e?ve[Se++]=128|e:256>e?(ve[Se++]=152,ve[Se++]=e):65536>e?(ve[Se++]=153,ve[Se++]=e>>8,ve[Se++]=255&e):(ve[Se++]=154,Ae.setUint32(Se,e),Se+=4)}function I(e,t){switch(typeof e){case"string":if(3<e.length){if(-1<t.objectMap[e]||t.values.length>=t.maxValues)return;let s=t.get(e);if(s)2==++s.count&&t.values.push(e);else if(t.set(e,{count:1}),t.samplingPackedValues){let s=t.samplingPackedValues.get(e);s?s.count++:t.samplingPackedValues.set(e,{count:1})}}break;case"object":if(e)if(e instanceof Array)for(let s=0,n=e.length;s<n;s++)I(e[s],t);else{let n=!t.encoder.useRecords;for(var s in e)e.hasOwnProperty(s)&&(n&&I(s,t),I(e[s],t))}break;case"function":console.log(e);}}function O(e){return{tag:e,encode:function(e,t){let s=e.byteLength,n=e.byteOffset||0,a=e.buffer||e;t(ke?Buffer.from(a,n,s):new Uint8Array(a,n,s))}}}function U(e,t){let s=e.byteLength;24>s?ve[Se++]=64+s:256>s?(ve[Se++]=88,ve[Se++]=s):65536>s?(ve[Se++]=89,ve[Se++]=s>>8,ve[Se++]=255&s):(ve[Se++]=90,Ae.setUint32(Se,s),Se+=4),Se+s>=ve.length&&t(Se+s),ve.set(e,Se),Se+=s}function v(e,t){// insert the ids that need to be referenced for structured clones
|
|
38
|
+
let s,n=2*t.length,a=e.length-n;t.sort((e,t)=>e.offset>t.offset?1:-1);for(let s,n=0;n<t.length;n++){s=t[n],s.id=n;for(let t of s.references)e[t++]=n>>8,e[t]=255&n}for(;s=t.pop();){let t=s.offset;e.copyWithin(t+n,t,a),n-=2;let r=t+n;// http://cbor.schmorp.de/value-sharing
|
|
39
|
+
e[r++]=216,e[r++]=28,a=t}return e}function A(e,t){Ae.setUint32(Ve.position+e,Se-Ve.position-e+1);// the offset to bundle
|
|
40
|
+
let s=Ve;Ve=null,t(s[0]),t(s[1])}function R(e){if(e.Class){if(!e.encode)throw new Error("Extension has no encode function");me.unshift(e.Class),be.unshift(e)}k(e)}function*S(e,t){const s=new _e(t);for(const n of e)yield s.encode(n)}async function*V(e,t){const s=new _e(t);for await(const n of e)yield s.encode(n)}/**
|
|
35
41
|
* Given an Iterable/Iterator input which yields buffers, returns an IterableIterator which yields sync decoded objects
|
|
36
42
|
* Or, given an Async Iterable/Iterator which yields promises resolving in buffers, returns an AsyncIterableIterator.
|
|
37
43
|
* @param {Iterable|Iterator|AsyncIterable|AsyncIterableIterator} bufferIterator
|
|
38
44
|
* @param {object} [options] - Decoder options
|
|
39
45
|
* @returns {IterableIterator|Promise.<AsyncIterableIterator}
|
|
40
|
-
*/let
|
|
41
|
-
|
|
46
|
+
*/let E;try{E=new TextDecoder}catch(e){}let _,j,x=0;const C=57342,P=57343,L=57337,B=6,F={};let D,K,T,w,N,z,W,Y={},q=0,G=0,$=[],H=[],J={useRecords:!1,mapsAsObjects:!0},Q=!1;class X{constructor(e){if(e&&((e.keyMap||e._keyMap)&&!e.useRecords&&(e.useRecords=!1,e.mapsAsObjects=!0),!1===e.useRecords&&void 0===e.mapsAsObjects&&(e.mapsAsObjects=!0),e.getStructures&&(e.getShared=e.getStructures),e.getShared&&!e.structures&&((e.structures=[]).uninitialized=!0),e.keyMap))// this is what we use to denote an uninitialized structures
|
|
47
|
+
{this.mapKey=new Map;for(let[t,s]of Object.entries(e.keyMap))this.mapKey.set(s,t)}Object.assign(this,e)}/*
|
|
48
|
+
decodeKey(key) {
|
|
49
|
+
return this.keyMap
|
|
50
|
+
? Object.keys(this.keyMap)[Object.values(this.keyMap).indexOf(key)] || key
|
|
51
|
+
: key
|
|
52
|
+
}
|
|
53
|
+
*/decodeKey(e){return this.keyMap?this.mapKey.get(e)||e:e}encodeKey(e){return this.keyMap&&this.keyMap.hasOwnProperty(e)?this.keyMap[e]:e}encodeKeys(e){if(!this._keyMap)return e;let t=new Map;for(let[s,n]of Object.entries(e))t.set(this._keyMap.hasOwnProperty(s)?this._keyMap[s]:s,n);return t}decodeKeys(e){if(!this._keyMap||"Map"!=e.constructor.name)return e;if(!this._mapKey){this._mapKey=new Map;for(let[e,t]of Object.entries(this._keyMap))this._mapKey.set(t,e)}let t={};//map.forEach((v,k) => res[Object.keys(this._keyMap)[Object.values(this._keyMap).indexOf(k)] || k] = v)
|
|
54
|
+
return e.forEach((e,s)=>t[this._mapKey.has(s)?this._mapKey.get(s):s]=e),t}mapDecode(e){let t=this.decode(e);if(this._keyMap)//Experiemntal support for Optimised KeyMap decoding
|
|
55
|
+
switch(t.constructor.name){case"Array":return t.map(e=>this.decodeKeys(e));//case 'Map': return this.decodeKeys(res)
|
|
56
|
+
}return t}decode(e,t){if(_)// re-entrant execution, save the state and restore it after we do this decode
|
|
57
|
+
return b(()=>(m(),this?this.decode(e,t):X.prototype.decode.call(J,e,t)));j=-1<t?t:e.length,x=0,G=0,K=null,T=null,_=e;// this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
|
|
42
58
|
// technique for getting data from a database where it can be copied into an existing buffer instead of creating
|
|
43
59
|
// new ones
|
|
44
|
-
try{
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
60
|
+
try{z=e.dataView||(e.dataView=new DataView(e.buffer,e.byteOffset,e.byteLength))}catch(t){if(_=null,e instanceof Uint8Array)throw t;throw new Error("Source must be a Uint8Array or Buffer but was a "+(e&&"object"==typeof e?e.constructor.name:typeof e))}if(this instanceof X){if(Y=this,N=this.sharedValues&&(this.pack?Array(this.maxPrivatePackedValues||16).concat(this.sharedValues):this.sharedValues),this.structures)return D=this.structures,n();(!D||0<D.length)&&(D=[])}else Y=J,(!D||0<D.length)&&(D=[]),N=null;return n()}decodeMultiple(e,t){let s,a=0;try{let r=e.length;Q=!0;let i=this?this.decode(e,r):ce.decode(e,r);if(t){if(!1===t(i))return;for(;x<r;)if(a=x,!1===t(n()))return}else{for(s=[i];x<r;)a=x,s.push(n());return s}}catch(e){throw e.lastPosition=a,e.values=s,e}finally{Q=!1,m()}}}const Z=/^[a-zA-Z_$][a-zA-Z\d_$]*$/;let ee=i,te=String.fromCharCode,se=Array(4096);class ne{constructor(e,t){this.value=e,this.tag=t}}let ae="object"==typeof self?self:global;$[0]=e=>new Date(e),$[1]=e=>new Date(1e3*e),$[2]=e=>{// bigint extension
|
|
61
|
+
let t=BigInt(0);for(let s=0,n=e.byteLength;s<n;s++)t=BigInt(e[s])+t<<BigInt(8);return t},$[3]=e=>BigInt(-1)-$[2](e),$[4]=e=>+(e[1]+"e"+e[0]),$[5]=e=>e[1]*s(e[0]*t(2));// the registration of the record definition extension (tag 105)
|
|
62
|
+
const re=e=>{let t=e[0]-57344,s=e[1];D[t]=s,s.read=r(s);let n={};if(Y.keyMap)for(let t,a=2,r=e.length;a<r;a++)t=Y.decodeKey(s[a-2]),n[t]=e[a];else for(let t,a=2,r=e.length;a<r;a++)t=s[a-2],n[t]=e[a];return n};$[105]=re,$[14]=e=>T?T[0].slice(T.position0,T.position0+=e):new ne(e,14),$[15]=e=>T?T[1].slice(T.position1,T.position1+=e):new ne(e,15),$[27]=e=>(ae[e[0]]||Error)(e[1],e[2]);const ie=e=>{if(132!=_[x++])throw new Error("Packed values structure must be followed by a 4 element array");let t=e();// packed values
|
|
63
|
+
return N=N?t.concat(N.slice(t.length)):t,N.prefixes=e(),N.suffixes=e(),e();// read the rump
|
|
64
|
+
};ie.handlesRead=!0,$[51]=ie,$[B]=e=>{// packed reference
|
|
65
|
+
if(!N)if(Y.getShared)y();else return new ne(e,B);if("number"==typeof e)return N[16+(0<=e?2*e:-2*e-1)];throw new Error("No support for non-integer packed references yet")},$[25]=e=>stringRefs[e],$[256]=e=>{stringRefs=[];try{return e()}finally{stringRefs=null}},$[256].handlesRead=!0,$[28]=e=>{w||(w=new Map,w.id=0);let t,s=w.id++,n=_[x];t=4==n>>5?[]:{};let a={target:t};// a placeholder object
|
|
66
|
+
w.set(s,a);let r=e();// read the next value as the target object to id
|
|
67
|
+
return a.used?Object.assign(t,r):(a.target=r,r);// no cycle, can just use the returned read object
|
|
68
|
+
},$[28].handlesRead=!0,$[29]=e=>{// sharedref http://cbor.schmorp.de/value-sharing (for structured clones)
|
|
69
|
+
let t=w.get(e);return t.used=!0,t.target},$[258]=e=>new Set(e),($[259]=e=>(Y.mapsAsObjects&&(Y.mapsAsObjects=!1,W=!0),e())).handlesRead=!0;H.push((e,t)=>225<=e&&255>=e?l(p().prefixes[e-224],t):28704<=e&&32767>=e?l(p().prefixes[e-28672],t):1879052288<=e&&2147483647>=e?l(p().prefixes[e-1879048192],t):216<=e&&223>=e?l(t,p().suffixes[e-216]):27647<=e&&28671>=e?l(t,p().suffixes[e-27639]):1811940352<=e&&1879048191>=e?l(t,p().suffixes[e-1811939328]):e==1399353956?{packedValues:N,structures:D.slice(0),version:t}:void 0);const oe=["Uint8","Uint8Clamped","Uint16","Uint32","BigUint64","Int8","Int16","Int32","BigInt64","Float32","Float64"].map(e=>e+"Array"),de=[64,68,69,70,71,72,77,78,79,81,82];for(let t=0;t<oe.length;t++)f(oe[t],de[t]);const ue=Array(147);// this is a table matching binary exponents to the multiplier to determine significant digit rounding
|
|
70
|
+
for(let t=0;256>t;t++)ue[t]=+("1e"+Math.floor(45.15-.30103*t));let ce=new X({useRecords:!1});const le=ce.decode,pe=ce.decodeMultiple,fe={NEVER:0,ALWAYS:1,DECIMAL_ROUND:3,DECIMAL_FIT:4};let ge,he=new Float32Array(1),ye=new Uint8Array(he.buffer,0,4);try{ge=new TextEncoder}catch(e){}let be,me;const ke="undefined"!=typeof Buffer,Me=ke?Buffer.allocUnsafeSlow:Uint8Array,Ie=ke?Buffer:Uint8Array,Oe=256,Ue=ke?4294967296:2144337920;let ve,Ae,Re,Se=0,Ve=null;const Ee=Symbol("record-id");class _e extends X{constructor(e){super(e),this.offset=0;let t,s,n,a,r;e=e||{};let i=Ie.prototype.utf8Write?function(e,t,s){return ve.utf8Write(e,t,s)}:!!(ge&&ge.encodeInto)&&function(e,t){return ge.encodeInto(e,ve.subarray(t)).written},o=this,d=e.maxSharedStructures||128,u=e.sequential;u&&(d=0,this.structures=[]),this.saveStructures&&(this.saveShared=this.saveStructures);let c,p,f,g=e.sharedValues;if(g){f=Object.create(null);for(let e=0,t=g.length;e<t;e++)f[g[e]]=e}let h=[],y=0,b=0;this.mapEncode=function(e,t){// Experimental support for premapping keys using _keyMap instad of keyMap - not optiimised yet)
|
|
71
|
+
if(this._keyMap&&!this._mapped)//console.log('encoding ', value)
|
|
72
|
+
switch(e.constructor.name){case"Array":e=e.map(e=>this.encodeKeys(e));//case 'Map':
|
|
73
|
+
// value = this.encodeKeys(value)
|
|
74
|
+
// break
|
|
75
|
+
}//this._mapped = true
|
|
76
|
+
return this.encode(e,t)},this.encode=function(i,l){if(ve||(ve=new Me(8192),Ae=new DataView(ve.buffer,0,8192),Se=0),Re=ve.length-10,2048>Re-Se?(ve=new Me(ve.length),Ae=new DataView(ve.buffer,0,ve.length),Re=ve.length-10,Se=0):l===De&&(Se=2147483640&Se+7),t=Se,r=o.structuredClone?new Map:null,o.bundleStrings&&"string"!=typeof i?(Ve=[],Ve.size=1/0):Ve=null,s=o.structures,s){if(s.uninitialized){let e=o.getShared()||{};o.structures=s=e.structures||[],o.sharedVersion=e.version;let t=o.sharedValues=e.packedValues;if(t){f={};for(let e=0,s=t.length;e<s;e++)f[t[e]]=e}}let e=s.length;if(e>d&&!u&&(e=d),!s.transitions){s.transitions=Object.create(null);for(let t,n=0;n<e;n++){//console.log('shared struct keys:', keys)
|
|
77
|
+
if(t=s[n],!t)continue;let e,a=s.transitions;for(let s,n=0,r=t.length;n<r;n++)s=t[n],e=a[s],e||(e=a[s]=Object.create(null)),a=e;a[Ee]=n}}u||(s.nextId=e)}if(n&&(n=!1),a=s||[],p=f,e.pack){let t=new Map;if(t.values=[],t.encoder=o,t.maxValues=e.maxPrivatePackedValues||(f?16:1/0),t.objectMap=f||!1,t.samplingPackedValues=c,I(i,t),0<t.values.length){ve[Se++]=216,ve[Se++]=51,M(4);let e=t.values;m(e),M(0),M(0),p=Object.create(f||null);for(let t=0,s=e.length;t<s;t++)p[e[t]]=t}}try{// update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
|
|
78
|
+
if(m(i),Ve&&A(t,m),o.offset=Se,r&&r.idsToInsert){Se+=2*r.idsToInsert.length,Se>Re&&O(Se),o.offset=Se;let e=v(ve.subarray(t,Se),r.idsToInsert);return r=null,e}return l&De?(ve.start=t,ve.end=Se,ve):ve.subarray(t,Se);// position can change if we call pack again in saveShared, so we get the buffer now
|
|
79
|
+
}finally{if(s)if(10>b&&b++,1e4<y)s.transitions=null,b=0,y=0,0<h.length&&(h=[]);else if(0<h.length&&!u){for(let e=0,t=h.length;e<t;e++)h[e][Ee]=void 0;h=[]}if(n&&o.saveShared){o.structures.length>d&&(o.structures=o.structures.slice(0,d));// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
|
|
80
|
+
let e=ve.subarray(t,Se);return!1===o.updateSharedData()?o.encode(i):e;// re-encode if it fails
|
|
81
|
+
}l&Ke&&(Se=t)}},this.findCommonStringsToPack=()=>(c=new Map,f||(f=Object.create(null)),e=>{let t=e&&e.threshold||4,s=this.pack?e.maxPrivatePackedValues||16:0;g||(g=this.sharedValues=[]);for(let[a,r]of c)r.count>t&&(f[a]=s++,g.push(a),n=!0);for(;this.saveShared&&!1===this.updateSharedData(););c=null});const m=s=>{Se>Re&&(ve=O(Se));var n,a=typeof s;if("string"==a){if(p){let t=p[s];if(0<=t)return void(16>t?ve[Se++]=t+224:(ve[Se++]=198,1&t?m(15-t>>1):m(t-16>>1)));/* } else if (packedStatus.serializationId != serializationId) {
|
|
57
82
|
packedStatus.serializationId = serializationId
|
|
58
83
|
packedStatus.count = 1
|
|
59
84
|
if (options.sharedPack) {
|
|
@@ -66,15 +91,18 @@ let a=qa.subarray(b,ta),c=i.structures||[];return(o&&(c=c.concat(o)),!1===i.save
|
|
|
66
91
|
|
|
67
92
|
}
|
|
68
93
|
}
|
|
69
|
-
} // else any in-doc incrementation?*/if(
|
|
70
|
-
(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
94
|
+
} // else any in-doc incrementation?*/if(c&&!e.pack){let e=c.get(s);e?e.count++:c.set(s,{count:1})}}let a=s.length;if(Ve&&4<=a&&1024>a){if((Ve.size+=a)>61440){let e,s=(Ve[0]?3*Ve[0].length+Ve[1].length:0)+10;Se+s>Re&&(ve=O(Se+s)),ve[Se++]=217,ve[Se++]=223,ve[Se++]=249,ve[Se++]=Ve.position?132:130,ve[Se++]=26,e=Se-t,Se+=4,Ve.position&&A(t,m),Ve=["",""],Ve.size=0,Ve.position=e}let e=/[\u0080-\uFFFF]/.test(s);return Ve[e?0:1]+=s,ve[Se++]=e?206:207,void m(a)}let r=32>a?1:256>a?2:65536>a?3:5;// first we estimate the header size, so we can write to the correct location
|
|
95
|
+
let o=3*a;if(Se+o>Re&&(ve=O(Se+o)),64>a||!i){let e,t,i,o=Se+r;for(e=0;e<a;e++)t=s.charCodeAt(e),128>t?ve[o++]=t:2048>t?(ve[o++]=192|t>>6,ve[o++]=128|63&t):55296==(64512&t)&&56320==(64512&(i=s.charCodeAt(e+1)))?(t=65536+((1023&t)<<10)+(1023&i),e++,ve[o++]=240|t>>18,ve[o++]=128|63&t>>12,ve[o++]=128|63&t>>6,ve[o++]=128|63&t):(ve[o++]=224|t>>12,ve[o++]=128|63&t>>6,ve[o++]=128|63&t);n=o-Se-r}else n=i(s,Se+r,o);24>n?ve[Se++]=96|n:256>n?(2>r&&ve.copyWithin(Se+2,Se+1,Se+1+n),ve[Se++]=120,ve[Se++]=n):65536>n?(3>r&&ve.copyWithin(Se+3,Se+2,Se+2+n),ve[Se++]=121,ve[Se++]=n>>8,ve[Se++]=255&n):(5>r&&ve.copyWithin(Se+5,Se+3,Se+3+n),ve[Se++]=122,Ae.setUint32(Se,n),Se+=4),Se+=n}else if("number"===a){if(s>>>0===s)24>s?ve[Se++]=s:256>s?(ve[Se++]=24,ve[Se++]=s):65536>s?(ve[Se++]=25,ve[Se++]=s>>8,ve[Se++]=255&s):(ve[Se++]=26,Ae.setUint32(Se,s),Se+=4);else if(s>>0===s)-24<=s?ve[Se++]=31-s:-256<=s?(ve[Se++]=56,ve[Se++]=~s):-65536<=s?(ve[Se++]=57,Ae.setUint16(Se,~s),Se+=2):(ve[Se++]=58,Ae.setUint32(Se,~s),Se+=4);else{let e;if(0<(e=this.useFloat32)&&4294967296>s&&-2147483648<=s){ve[Se++]=250,Ae.setFloat32(Se,s);let t;if(4>e||// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
96
|
+
(t=s*ue[(127&ve[Se])<<1|ve[Se+1]>>7])>>0===t)return void(Se+=4);// move back into position for writing a double
|
|
97
|
+
Se--}ve[Se++]=251,Ae.setFloat64(Se,s),Se+=8}}else if("object"===a){if(!s)ve[Se++]=246;else{if(r){let e=r.get(s);if(e){// 16-bit uint
|
|
98
|
+
if(ve[Se++]=216,ve[Se++]=29,ve[Se++]=25,!e.references){let t=r.idsToInsert||(r.idsToInsert=[]);e.references=[],t.push(e)}// TODO: also support 32-bit
|
|
99
|
+
return e.references.push(Se-t),void(Se+=2)}r.set(s,{offset:Se-t})}let e=s.constructor;if(e===Object)k(s,!0);else if(e===Array){n=s.length,24>n?ve[Se++]=128|n:M(n);for(let e=0;e<n;e++)m(s[e])}else if(e!==Map){for(let e,t=0,n=be.length;t<n;t++)if(e=me[t],s instanceof e){let e=be[t],n=e.tag||e.getTag&&e.getTag(s);return 24>n?ve[Se++]=192|n:256>n?(ve[Se++]=216,ve[Se++]=n):65536>n?(ve[Se++]=217,ve[Se++]=n>>8,ve[Se++]=255&n):-1<n&&(ve[Se++]=218,Ae.setUint32(Se,n),Se+=4),void e.encode.call(this,s,m,O)}if(s[Symbol.iterator]){ve[Se++]=159;// indefinite length array
|
|
100
|
+
for(let e of s)m(e);// stop-code
|
|
101
|
+
return void(ve[Se++]=255)}// no extension found, write as object
|
|
102
|
+
k(s,!s.hasOwnProperty)}else if((this.mapsAsObjects?!1!==this.useTag259ForMaps:this.useTag259ForMaps)&&(ve[Se++]=217,ve[Se++]=1,ve[Se++]=3),n=s.size,24>n?ve[Se++]=160|n:256>n?(ve[Se++]=184,ve[Se++]=n):65536>n?(ve[Se++]=185,ve[Se++]=n>>8,ve[Se++]=255&n):(ve[Se++]=186,Ae.setUint32(Se,n),Se+=4),o.keyMap)for(let[e,t]of s)m(o.encodeKey(e)),m(t);else for(let[e,t]of s)m(e),m(t)}}else if("boolean"===a)ve[Se++]=s?245:244;else if("bigint"===a){if(s<BigInt(1)<<BigInt(64)&&0<=s)ve[Se++]=27,Ae.setBigUint64(Se,s);else if(s>-(BigInt(1)<<BigInt(64))&&0>s)ve[Se++]=59,Ae.setBigUint64(Se,-s-BigInt(1));else// overflow
|
|
103
|
+
if(this.largeBigIntToFloat)ve[Se++]=251,Ae.setFloat64(Se,+s);else throw new RangeError(s+" was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64");Se+=8}else if("undefined"===a)ve[Se++]=247;else throw new Error("Unknown type: "+a)},k=!1===this.useRecords?this.variableMapSize?e=>{// this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
|
|
104
|
+
let t=Object.keys(e),s=Object.values(e),n=t.length;if(24>n?ve[Se++]=160|n:256>n?(ve[Se++]=184,ve[Se++]=n):65536>n?(ve[Se++]=185,ve[Se++]=n>>8,ve[Se++]=255&n):(ve[Se++]=186,Ae.setUint32(Se,n),Se+=4),o.keyMap)for(let e=0;e<n;e++)m(encodeKey(t[e])),m(s[e]);else for(let e=0;e<n;e++)m(t[e]),m(s[e])}:(e,s)=>{ve[Se++]=185;// always use map 16, so we can preallocate and set the length afterwards
|
|
105
|
+
let n=Se-t;Se+=2;let a=0;if(o.keyMap)for(let t in e)(s||e.hasOwnProperty(t))&&(m(o.encodeKey(t)),m(e[t]),a++);else for(let t in e)(s||e.hasOwnProperty(t))&&(m(t),m(e[t]),a++);ve[n++ +t]=a>>8,ve[n+t]=255&a}:/* sharedStructures ? // For highly stable structures, using for-in can a little bit faster
|
|
78
106
|
(object, safePrototype) => {
|
|
79
107
|
let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null))
|
|
80
108
|
let objectOffset = position++ - start
|
|
@@ -113,21 +141,22 @@ let d=ta-b;ta+=2;let e=0;for(let b in a)(c||a.hasOwnProperty(b))&&(v(b),v(a[b]),
|
|
|
113
141
|
sharedStructures.onUpdate(id, transition.__keys__)
|
|
114
142
|
}
|
|
115
143
|
target[objectOffset + start] = id
|
|
116
|
-
}*/
|
|
117
|
-
|
|
118
|
-
//
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
encode(
|
|
125
|
-
encode(
|
|
126
|
-
encode(
|
|
127
|
-
|
|
144
|
+
}*/e=>{let t=Object.keys(e),r=Object.values(e);this.keyMap&&(t=t.map(e=>this.encodeKey(e)));let o,u=a.transitions||(a.transitions=Object.create(null)),c=0,l=t.length;//let parentRecordId
|
|
145
|
+
for(let s,n=0;n<l;n++)//if (!parentRecordId)
|
|
146
|
+
// parentRecordId = transition[RECORD_SYMBOL]
|
|
147
|
+
s=t[n],o=u[s],o||(o=u[s]=Object.create(null),c++),u=o;let p=u[Ee];if(void 0!==p)ve[Se++]=217,ve[Se++]=224|p>>8,ve[Se++]=255&p;else if(p=a.nextId++,p||(p=0,a.nextId=1),p>=Oe&&(a.nextId=(p=d)+1),u[Ee]=p,a[p]=t,s&&s.length<=d)ve[Se++]=217,ve[Se++]=224|p>>8,ve[Se++]=255&p,n=!0;else{Ae.setUint32(Se,3655335680),Se+=3,c&&(y+=b*c),h.length>=Oe-d&&(h.shift()[Ee]=void 0),h.push(u),M(l+2),m(57344+p),m(t);for(let t of Object.values(e))m(t);return}24>l?ve[Se++]=128|l:M(l);for(let t=0;t<l;t++)m(r[t])},O=e=>{var s=Math.min,n=Math.round,a=Math.max;let r;if(16777216<e){// special handling for really large buffers
|
|
148
|
+
if(e-t>Ue)throw new Error("Encoded buffer would be larger than maximum buffer size");r=s(Ue,4096*n(a((e-t)*(67108864<e?1.25:2),4194304)/4096))}else// faster handling for smaller buffers
|
|
149
|
+
r=(a(e-t<<2,ve.length-1)>>12)+1<<12;let i=new Me(r);return Ae=new DataView(i.buffer,0,r),ve.copy?ve.copy(i,0,t,e):i.set(ve.slice(t,e)),Se-=t,t=0,Re=i.length-10,ve=i}}useBuffer(e){// this means we are finished using our own buffer and we can write over it safely
|
|
150
|
+
ve=e,Ae=new DataView(ve.buffer,ve.byteOffset,ve.byteLength),Se=0}clearSharedData(){this.structures&&(this.structures=[]),this.sharedValues&&(this.sharedValues=void 0)}updateSharedData(){let e=this.sharedVersion||0;this.sharedVersion=e+1;let t=this.saveShared(new je(this.structures,this.sharedValues,this.sharedVersion),t=>(t&&t.version||0)==e);if(!1===t){// get updated structures and try again if the update failed
|
|
151
|
+
let e=this.getShared()||{};this.structures=e.structures||[],this.sharedValues=e.packedValues,this.sharedVersion=e.version}return t}}class je{constructor(e,t,s){this.structures=e,this.packedValues=t,this.version=s}}me=[Date,Set,Error,RegExp,ne,ArrayBuffer,Ie,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array,"undefined"==typeof BigUint64Array?function(){}:BigUint64Array,Int8Array,Int16Array,Int32Array,"undefined"==typeof BigInt64Array?function(){}:BigInt64Array,Float32Array,Float64Array,je],be=[{tag:1,encode(e){let t=e.getTime()/1e3;(this.useTimestamp32||0===e.getMilliseconds())&&0<=t&&4294967296>t?(ve[Se++]=26,Ae.setUint32(Se,t),Se+=4):(ve[Se++]=251,Ae.setFloat64(Se,t),Se+=8)}},{tag:258,// https://github.com/input-output-hk/cbor-sets-spec/blob/master/CBOR_SETS.md
|
|
152
|
+
encode(e,t){let s=Array.from(e);t(s)}},{tag:27,// http://cbor.schmorp.de/generic-object
|
|
153
|
+
encode(e,t){t([e.name,e.message])}},{tag:27,// http://cbor.schmorp.de/generic-object
|
|
154
|
+
encode(e,t){t(["RegExp",e.source,e.flags])}},{getTag(e){return e.tag},encode(e,t){t(e.value)}},{encode(e,t,s){U(e,s)}},{encode(e,t,s){U(e,s)}},O(64),O(68),O(69),O(70),O(71),O(72),O(77),O(78),O(79),O(81),O(82),{encode(e,t){// write SharedData
|
|
155
|
+
let s=e.packedValues||[],n=e.structures||[];if(0<s.values.length){ve[Se++]=216,ve[Se++]=51,M(4);let e=s.values;t(e),M(0),M(0),packedObjectMap=Object.create(sharedPackedObjectMap||null);for(let t=0,s=e.length;t<s;t++)packedObjectMap[e[t]]=t}if(n){Ae.setUint32(Se,3655335424),Se+=3;let s=n.slice(0);s.unshift(57344),s.push(new ne(e.version,1399353956)),t(s)}else t(new ne(e.version,1399353956))}}];let xe=new _e({useRecords:!1});const Ce=xe.encode,{NEVER:Pe,ALWAYS:Le,DECIMAL_ROUND:Be,DECIMAL_FIT:Fe}=fe,De=512,Ke=1024;e.ALWAYS=Le,e.DECIMAL_FIT=Fe,e.DECIMAL_ROUND=Be,e.Decoder=X,e.Encoder=_e,e.FLOAT32_OPTIONS=fe,e.NEVER=Pe,e.REUSE_BUFFER_MODE=De,e.Tag=ne,e.addExtension=R,e.clearSource=m,e.decode=le,e.decodeIter=function(e,t={}){if(!e||"object"!=typeof e)throw new Error("first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise");const s=new X(t);let n;const a=e=>{let t;// if there's incomplete data from previous chunk, concatinate and try again
|
|
156
|
+
n&&(e=Buffer.concat([n,e]),n=void 0);try{t=s.decodeMultiple(e)}catch(s){if(s.incomplete)n=e.slice(s.lastPosition),t=s.values;else throw s}return t};if("function"==typeof e[Symbol.iterator])return function*(){for(const t of e)yield*a(t)}();return"function"==typeof e[Symbol.asyncIterator]?async function*(){for await(const t of e)yield*a(t)}():void 0},e.decodeMultiple=pe,e.encode=Ce,e.encodeIter=/**
|
|
128
157
|
* Given an Iterable first argument, returns an Iterable where each value is encoded as a Buffer
|
|
129
158
|
* If the argument is only Async Iterable, the return value will be an Async Iterable.
|
|
130
159
|
* @param {Iterable|Iterator|AsyncIterable|AsyncIterator} objectIterator - iterable source, like a Readable object stream, an array, Set, or custom object
|
|
131
160
|
* @param {options} [options] - cbor-x Encoder options
|
|
132
161
|
* @returns {IterableIterator|Promise.<AsyncIterableIterator>}
|
|
133
|
-
*/function(
|
|
162
|
+
*/function(e,t={}){if(!e||"object"!=typeof e)throw new Error("first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable");else{if("function"==typeof e[Symbol.iterator])return S(e,t);if("function"==typeof e.then||"function"==typeof e[Symbol.asyncIterator])return V(e,t);throw new Error("first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise")}},e.isNativeAccelerationEnabled=!1,e.roundFloat32=function(e){he[0]=e;let t=ue[(127&ye[3])<<1|ye[2]>>7];return(t*e+(0<e?.5:-.5)>>0)/t},Object.defineProperty(e,"__esModule",{value:!0})});
|