UNPKG

@dfinity/cbor

Version:

A small implementation of Concise Binary Object Representation (CBOR) in pure JavaScript.

3 lines (2 loc) 5.38 kB
(function(w,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(w=typeof globalThis<"u"?globalThis:w||self,f(w.Cbor={}))})(this,function(w){"use strict";class f extends Error{constructor(n){super(n),this.name="DecodingError"}}const x=55799,m=Symbol("CBOR_STOP_CODE");var r=(t=>(t[t.False=20]="False",t[t.True=21]="True",t[t.Null=22]="Null",t[t.Undefined=23]="Undefined",t[t.Break=31]="Break",t))(r||{}),c=(t=>(t[t.UnsignedInteger=0]="UnsignedInteger",t[t.NegativeInteger=1]="NegativeInteger",t[t.ByteString=2]="ByteString",t[t.TextString=3]="TextString",t[t.Array=4]="Array",t[t.Map=5]="Map",t[t.Tag=6]="Tag",t[t.Simple=7]="Simple",t))(c||{});const P=23,Y=255,G=65535,W=4294967295,H=BigInt("0xffffffffffffffff");var g=(t=>(t[t.Value=23]="Value",t[t.OneByte=24]="OneByte",t[t.TwoBytes=25]="TwoBytes",t[t.FourBytes=26]="FourBytes",t[t.EightBytes=27]="EightBytes",t[t.Indefinite=31]="Indefinite",t))(g||{});const E=!1;function K(t){return t==null}function T(t,n){const e=new Uint8Array(n);return e.set(t),e}const Z=new TextDecoder;function q(t){return(t&224)>>5}function J(t){return t&31}let U=new Uint8Array,S,a=0;function Q(t,n){U=t,a=0;const e=h(n);return(n==null?void 0:n(e))??e}function h(t){const[n,e]=N();switch(n){case c.UnsignedInteger:return y(e);case c.NegativeInteger:return p(e);case c.ByteString:return L(e);case c.TextString:return D(e);case c.Array:return b(e,t);case c.Map:return M(e,t);case c.Tag:return C(e,t);case c.Simple:return j(e)}throw new f(`Unsupported major type: ${n}`)}function N(){const t=U.at(a);if(K(t))throw new f("Provided CBOR data is empty");const n=q(t),e=J(t);return a++,[n,e]}function b(t,n){const e=y(t);if(e===1/0){const d=[];let o=h(n);for(;o!==m;)d.push((n==null?void 0:n(o))??o),o=h(n);return d}const u=new Array(e);for(let d=0;d<e;d++){const o=h(n);u[d]=(n==null?void 0:n(o))??o}return u}function j(t){switch(t){case r.False:return!1;case r.True:return!0;case r.Null:return null;case r.Undefined:return;case r.Break:return m}throw new f(`Unrecognized simple type: ${t.toString(2)}`)}function M(t,n){const e=y(t),u={};if(e===1/0){let[d,o]=N();for(;d!==c.Simple&&o!==r.Break;){const F=D(o),A=h(n);u[F]=(n==null?void 0:n(A,F))??A,[d,o]=N()}return u}for(let d=0;d<e;d++){const[o,F]=N();if(o!==c.TextString)throw new f("Map keys must be text strings");const A=D(F),z=h(n);u[A]=(n==null?void 0:n(z,A))??z}return u}function y(t){if(t<=g.Value)return t;switch(S=new DataView(U.buffer,U.byteOffset+a),t){case g.OneByte:return a++,S.getUint8(0);case g.TwoBytes:return a+=2,S.getUint16(0,E);case g.FourBytes:return a+=4,S.getUint32(0,E);case g.EightBytes:return a+=8,S.getBigUint64(0,E);case g.Indefinite:return 1/0;default:throw new f(`Unsupported integer info: ${t.toString(2)}`)}}function p(t){const n=y(t);return typeof n=="number"?-1-n:-1n-n}function L(t){const n=y(t);if(n>Number.MAX_SAFE_INTEGER)throw new f("Byte length is too large");const e=Number(n);return a+=e,U.slice(a-e,a)}function D(t){const n=L(t);return Z.decode(n)}function C(t,n){const e=y(t);if(e===x)return h(n);throw new f(`Unsupported tag: ${e}.`)}class _ extends Error{constructor(n){super(n),this.name="SerializationError"}}const v=2*1024,tt=100,nt=new TextEncoder;function l(t){return t<<5}let i=new Uint8Array(v),B=new DataView(i.buffer),s=0,V=[];function et(t,n){s=0;const e=(n==null?void 0:n(t))??t;return O(e,n),i.slice(0,s)}function st(t,n){s=0;const e=(n==null?void 0:n(t))??t;return gt(x,e,n),i.slice(0,s)}function O(t,n){if(s>i.length-tt&&(i=T(i,i.length*2),B=new DataView(i.buffer)),t===!1||t===!0||t===null||t===void 0){ot(t);return}if(typeof t=="number"||typeof t=="bigint"){rt(t);return}if(typeof t=="string"){k(t);return}if(t instanceof Uint8Array){X(t);return}if(t instanceof ArrayBuffer){X(new Uint8Array(t));return}if(Array.isArray(t)){ct(t,n);return}if(typeof t=="object"){it(t,n);return}throw new _(`Unsupported type: ${typeof t}`)}function ct(t,n){I(c.Array,t.length),t.forEach((e,u)=>{O((n==null?void 0:n(e,u.toString()))??e,n)})}function it(t,n){V=Object.entries(t),I(c.Map,V.length),V.forEach(([e,u])=>{k(e),O((n==null?void 0:n(u,e))??u,n)})}function I(t,n){if(n<=P){B.setUint8(s++,l(t)|Number(n));return}if(n<=Y){B.setUint8(s++,l(t)|g.OneByte),B.setUint8(s,Number(n)),s+=1;return}if(n<=G){B.setUint8(s++,l(t)|g.TwoBytes),B.setUint16(s,Number(n),E),s+=2;return}if(n<=W){B.setUint8(s++,l(t)|g.FourBytes),B.setUint32(s,Number(n),E),s+=4;return}if(n<=H){B.setUint8(s++,l(t)|g.EightBytes),B.setBigUint64(s,BigInt(n),E),s+=8;return}throw new _(`Value too large to encode: ${n}`)}function ot(t){I(c.Simple,ft(t))}function ft(t){if(t===!1)return r.False;if(t===!0)return r.True;if(t===null)return r.Null;if(t===void 0)return r.Undefined;throw new _(`Unrecognized simple value: ${t.toString()}`)}function R(t,n){I(t,n.length),s>i.length-n.length&&(i=T(i,i.length+n.length),B=new DataView(i.buffer)),i.set(n,s),s+=n.length}function $(t,n){I(t,n)}function ut(t){$(c.UnsignedInteger,t)}function dt(t){$(c.NegativeInteger,typeof t=="bigint"?-1n-t:-1-t)}function rt(t){t>=0?ut(t):dt(t)}function k(t){R(c.TextString,nt.encode(t))}function X(t){R(c.ByteString,t)}function gt(t,n,e){I(c.Tag,t),O(n,e)}w.DecodingError=f,w.EncodingError=_,w.decode=Q,w.encode=et,w.encodeWithSelfDescribedTag=st,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})}); //# sourceMappingURL=cbor.umd.js.map