@preserves/core
Advanced tools
Comparing version 0.995.101 to 0.995.200
@@ -1,1 +0,1 @@ | ||
var t;!function(t){t[t.False=128]="False",t[t.True=129]="True",t[t.End=132]="End",t[t.Annotation=133]="Annotation",t[t.Embedded=134]="Embedded",t[t.Ieee754=135]="Ieee754",t[t.SignedInteger=176]="SignedInteger",t[t.String=177]="String",t[t.ByteString=178]="ByteString",t[t.Symbol=179]="Symbol",t[t.Record=180]="Record",t[t.Sequence=181]="Sequence",t[t.Set=182]="Set",t[t.Dictionary=183]="Dictionary"}(t||(t={}));var e=Object.freeze({__proto__:null,get Tag(){return t}});const n=Symbol.for("IsPreservesAnnotated");function r(t){return!!(null==t?void 0:t[n])}function s(t,e){if(r(t)&&(t=t.item),r(e)&&(e=e.item),Object.is(t,e))return!0;if(typeof t!=typeof e)return("number"==typeof t&&"bigint"==typeof e||"bigint"==typeof t&&"number"==typeof e)&&t==e;if("object"==typeof t){if(null===t||null===e)return!1;if("equals"in t&&"function"==typeof t.equals)return t.equals(e,s);if(Array.isArray(t)&&Array.isArray(e)){const n="label"in t;if(n!=="label"in e)return!1;if(n&&!s(t.label,e.label))return!1;if(t.length!==e.length)return!1;for(let n=0;n<t.length;n++)if(!s(t[n],e[n]))return!1;return!0}}return!1}function i(t){return{line:1,column:0,pos:0,name:t}}function o(t,e){if(t.pos++,void 0===t.line)return!1;{let n=!1;switch(e){case"\t":t.column=t.column+8&-8;break;case"\n":t.column=0,t.line++,n=!0;break;case"\r":t.column=0;break;default:t.column++}return n}}function a(t){var e,n,r;return null===t?"<unknown>":"string"==typeof t?t:`${null!==(e=t.name)&&void 0!==e?e:""}:${null!==(n=t.line)&&void 0!==n?n:""}:${null!==(r=t.column)&&void 0!==r?r:""}:${t.pos}`}class c{constructor(t,e){this.annotations=[],this.pos=null!=e?e:null,this.item=t}__as_preserve__(){return this}static __from_preserve__(t){return r(t)?t:void 0}__preserve_on__(e){if(e.includeAnnotations)for(const n of this.annotations)e.state.emitbyte(t.Annotation),e.push(n);e.push(this.item)}__preserve_text_on__(t){if(t.includeAnnotations){const e=this.annotations.length<=1;for(const n of this.annotations)t.state.pieces.push("@"),t.push(n),e?t.state.pieces.push(" "):t.state.writeIndentSpace()}t.push(this.item)}equals(t){return s(this.item,c.isAnnotated(t)?t.item:t)}get[n](){return!0}static isAnnotated(t){return r(t)}}function u(t,...e){const n=c.isAnnotated(t)?t:new c(t);return e.forEach((t=>n.annotations.push(t))),n}function h(t){return c.isAnnotated(t)?t.annotations:[]}function d(t){return c.isAnnotated(t)?t.pos:null}const l={};function f(t){const e=new Uint8Array(Math.floor(3*t.length/4));let n=0,r=0;for(;n<t.length;){const s=l[t[n++]],i=l[t[n++]],o=l[t[n++]],a=l[t[n++]],c=s<<18|i<<12|o<<6|a;if(e[r++]=c>>16&255,void 0===o)break;if(e[r++]=c>>8&255,void 0===a)break;e[r++]=255&c}return e.subarray(0,r)}[..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"].forEach(((t,e)=>l[t]=e)),l["+"]=l["-"]=62,l["/"]=l._=63;const p="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function y(t){let e="",n=0,r=0;for(let s of t)for(n=(63&n)<<8|s,r+=8;r>=6;){r-=6;e+=p[n>>r&63]}if(r>0){e+=p[n<<6-r&63]}return e}const m=new TextEncoder,b=new TextDecoder("utf-8",{fatal:!0}),w=Symbol.for("IsPreservesBytes");class g{constructor(t=new Uint8Array){if(g.isBytes(t))this._view=t._view;else if(ArrayBuffer.isView(t))this._view=new Uint8Array(t.buffer,t.byteOffset,t.byteLength);else if(t instanceof ArrayBuffer)this._view=new Uint8Array(t);else if("string"==typeof t)this._view=m.encode(t);else if("number"==typeof t)this._view=new Uint8Array(t);else{if("number"!=typeof t.length)throw new TypeError("Attempt to initialize Bytes from unsupported value: "+t);this._view=Uint8Array.from(t)}}dataview(){return new DataView(this._view.buffer,this._view.byteOffset,this._view.byteLength)}get length(){return this._view.length}static from(t){return new g(t)}static of(...t){return new g(Uint8Array.of(...t))}static fromLatin1(t){const e=new g(t.length);for(let n=0;n<t.length;n++){const r=t.charCodeAt(n);if(r>=256)throw new Error("Codepoint out of range for 'latin1' byte encoding");e._view[n]=r}return e}static fromBase64(t){return new g(f(t))}static fromHex(t){if(1&t.length)throw new Error("Cannot decode odd-length hexadecimal string");const e=new g(t.length>>1);return g._raw_fromHexInto(t,e._view),e}static _raw_fromHexInto(t,e){const n=t.length>>1;for(let r=0;r<n;r++)e[r]=_(t.charCodeAt(r<<1))<<4|_(t.charCodeAt(1+(r<<1)))}static fromIO(t){return"string"==typeof t||g.isBytes(t)?t:new g(t)}static toIO(t){return"string"==typeof t?t:g.isBytes(t)?t._view:t}get(t){return this._view[t]}equals(t){if(!g.isBytes(t))return!1;if(t.length!==this.length)return!1;const e=this._view,n=t._view;for(let t=0;t<e.length;t++)if(e[t]!==n[t])return!1;return!0}hashCode(){const t=this._view;let e=0;for(let n=0;n<t.length;n++)e=31*e+t[n]|0;return e}static compare(t,e){return t<e?-1:e<t?1:0}static decodeUtf8(t){return b.decode(x(t))}fromUtf8(){return b.decode(this._view)}__as_preserve__(){return this}static __from_preserve__(t){return g.isBytes(t)?t:void 0}toLatin1(){return String.fromCharCode.apply(null,this._view)}toBase64(){return y(this._view)}toHex(t=v){var e=[];for(let n=0;n<this.length;n++)e.push(t(this._view[n]>>4)),e.push(t(15&this._view[n]));return e.join("")}valueOf(){return this.toHex()}__preserve_on__(e){e.state.emitbyte(t.ByteString),e.state.varint(this.length),e.state.emitbytes(this._view)}__preserve_text_on__(t){t.state.writeBytes(this._view)}get[w](){return!0}static isBytes(t){return!!(null==t?void 0:t[w])}}function v(t){return"0123456789abcdef"[t]}function _(t,e=Error){if(t>=48&&t<=57)return t-48;if(t>=97&&t<=102)return t-97+10;if(t>=65&&t<=70)return t-65+10;throw new e("Invalid hex digit: "+String.fromCharCode(t))}function x(t){return t instanceof Uint8Array?t:t._view}function S(t){return t instanceof DataView?t:t.dataview()}g.concat=function(t){let e=0;for(let n=0;n<t.length;n++)e+=x(t[n]).length;const n=new g(e);let r=0;for(let e=0;e<t.length;e++){const s=x(t[e]);n._view.set(s,r),r+=s.length}return n},function(){for(const t of"entries every find findIndex forEach includes indexOf join\nkeys lastIndexOf reduce reduceRight some toLocaleString values".split(/\s+/))g.prototype[t]=function(...e){return this._view[t](...e)};for(const t of"filter map slice subarray".split(/\s+/))g.prototype[t]=function(...e){return new g(this._view[t](...e))};for(const t of"reverse sort".split(/\s+/))g.prototype[t]=function(...e){return new g(this._view.slice()[t](...e))};g.prototype[Symbol.iterator]=function(){return this._view[Symbol.iterator]()}}();const A=Symbol.for("ErrorType");class k{static isCodecError(t,e){return(null==t?void 0:t[A])===e}}class E extends Error{get[A](){return"DecodeError"}constructor(t,e){super(t),this.pos=e}static isDecodeError(t){return k.isCodecError(t,"DecodeError")}}class B extends Error{get[A](){return"EncodeError"}static isEncodeError(t){return k.isCodecError(t,"EncodeError")}constructor(t,e){super(t),this.irritant=e}}class I extends E{get[A](){return"ShortPacket"}static isShortPacket(t){return k.isCodecError(t,"ShortPacket")}}function D(t,e){return e.label=t,e}function C(t){return"object"==typeof t&&null!==t&&"__preserve_on__"in t&&"function"==typeof t.__preserve_on__}function j(t){return String.fromCharCode.apply(null,t)}!function(t){t.isRecord=function(t){return Array.isArray(t)&&"label"in t},t.constructorInfo=function(t){return{label:t.label,arity:t.length}},t.isClassOf=function(e,n){return t.isRecord(n)&&s(e.label,n.label)&&e.arity===n.length},t.makeConstructor=function(){return(e,n)=>{const r=(...n)=>t(e,n),s={label:e,arity:n.length};return r.constructorInfo=s,r.isClassOf=e=>t.isClassOf(s,e),r.fieldNumbers={},r._={},n.forEach(((t,e)=>{r._[t]=t=>t[e],r.fieldNumbers[t]=e})),r}}}(D||(D={}));let O=0;const q=new WeakMap;function M(t){let e=q.get(t);return void 0===e&&(e=O++,q.set(t,e)),e}const U={encode(t,e){new N(t,this).push(M(e))}};class F{constructor(t){this.chunks=[],this.view=new DataView(new ArrayBuffer(256)),this.index=0,this.options=t}get canonical(){var t;return null===(t=this.options.canonical)||void 0===t||t}get includeAnnotations(){var t;return null!==(t=this.options.includeAnnotations)&&void 0!==t?t:!this.canonical}contents(){if(0===this.chunks.length){const t=this.index;return this.index=0,new g(this.view.buffer.slice(0,t))}{this.rotatebuffer(4096);const t=this.chunks;return this.chunks=[],g.concat(t)}}contentsString(){if(0===this.chunks.length){const t=j(new Uint8Array(this.view.buffer,0,this.index));return this.index=0,t}{this.rotatebuffer(4096);const t=this.chunks;return this.chunks=[],t.map(j).join("")}}rotatebuffer(t){this.chunks.push(new Uint8Array(this.view.buffer,0,this.index)),this.view=new DataView(new ArrayBuffer(t)),this.index=0}makeroom(t){this.index+t>this.view.byteLength&&this.rotatebuffer(t+4096)}emitbyte(t){this.makeroom(1),this.view.setUint8(this.index++,t)}emitbytes(t){this.makeroom(t.length),new Uint8Array(this.view.buffer).set(t,this.index),this.index+=t.length}claimbytes(t){this.makeroom(t);const e=new Uint8Array(this.view.buffer,this.index,t);return this.index+=t,e}varint(t){for(;t>=128;)this.emitbyte(t%128+128),t=Math.floor(t/128);this.emitbyte(t)}encodeint(e){if("bigint"==typeof e)return this.encodebigint(e);if(this.emitbyte(t.SignedInteger),0===e)return void this.emitbyte(0);const n=(-1===e?0:Math.floor(Math.log2(e>0?e:-(1+e)))+1)+1+7>>3;this.varint(n);const r=(t,e)=>{t>0&&(r(t-1,Math.floor(e/256)),this.emitbyte(255&e))};r(n,e)}encodebigint(e){let n;if(this.emitbyte(t.SignedInteger),e>0)n=e.toString(16),1&n.length?n="0"+n:_(n.charCodeAt(0))>=8&&(n="00"+n);else{if(!(e<0))return void this.emitbyte(0);{const t=(~e).toString(16);n="";for(let e=0;e<t.length;e++)n+="fedcba9876543210"[_(t.charCodeAt(e))];1&n.length?n="f"+n:_(n.charCodeAt(0))<8&&(n="ff"+n)}}this.varint(n.length>>1),g._raw_fromHexInto(n,this.claimbytes(n.length>>1))}encodebytes(t,e){this.emitbyte(t),this.varint(e.length),this.emitbytes(e)}}class N{constructor(t={},e){var n;t instanceof F?(this.state=t,this.embeddedEncode=null!=e?e:U):(this.state=new F(t),this.embeddedEncode=null!==(n=t.embeddedEncode)&&void 0!==n?n:U)}withEmbeddedEncode(t,e){return e(new N(this.state,t)),this}get canonical(){return this.state.canonical}get includeAnnotations(){return this.state.includeAnnotations}contents(){return this.state.contents()}contentsString(){return this.state.contentsString()}grouped(e,n){this.state.emitbyte(e),n(),this.state.emitbyte(t.End)}push(e){if(C(e))e.__preserve_on__(this);else if(C(e))e.__preserve_on__(this);else if("boolean"==typeof e)this.state.emitbyte(e?t.True:t.False);else if("number"==typeof e||"bigint"==typeof e)this.state.encodeint(e);else if("string"==typeof e)this.state.encodebytes(t.String,new g(e)._view);else if("symbol"==typeof e){const n=Symbol.keyFor(e);if(void 0===n)throw new B("Cannot preserve non-global Symbol",e);this.state.encodebytes(t.Symbol,new g(n)._view)}else if(ArrayBuffer.isView(e))if(e instanceof Uint8Array)this.state.encodebytes(t.ByteString,e);else{const n=new Uint8Array(e.buffer,e.byteOffset,e.byteLength);this.state.encodebytes(t.ByteString,n)}else if(D.isRecord(e)){this.state.emitbyte(t.Record),this.push(e.label);for(let t of e)this.push(t);this.state.emitbyte(t.End)}else!function(t){return"object"==typeof t&&null!==t&&"function"==typeof t[Symbol.iterator]}(e)?(e=>{this.state.emitbyte(t.Embedded),this.embeddedEncode.encode(this.state,e)})(e):this.grouped(t.Sequence,(()=>{for(let t of e)this.push(t)}));return this}}function T(t,e={}){return new N(e).push(t).contents()}const R=new N({canonical:!0});let V=!1;function P(t,e){if(void 0!==e||V)return T(t,Object.assign(Object.assign({},e),{canonical:!0}));{V=!0;const e=R.push(t).contents();return V=!1,e}}function z(t){if(V)return new N({canonical:!0}).push(t).contentsString();{V=!0;const e=R.push(t).contentsString();return V=!1,e}}function H(t,e={}){return T(t,Object.assign(Object.assign({},e),{includeAnnotations:!0}))}const W=Map,L=Set,$=Symbol.for("IsMap"),K=Symbol.for("IsSet");function G(t,e){const n=t=>t.done?{done:!0,value:null}:{done:!1,value:e(t.value)};return{next:e=>n(t.next(e)),return:e=>{var r,s;return n(null!==(s=null===(r=t.return)||void 0===r?void 0:r.call(t,e))&&void 0!==s?s:{done:!0,value:null})},throw:e=>{var r,s;return n(null!==(s=null===(r=t.throw)||void 0===r?void 0:r.call(t,e))&&void 0!==s?s:{done:!0,value:null})},[Symbol.iterator](){return this}}}$ in Map.prototype||(Object.defineProperty(Map.prototype,$,{get:()=>!0}),Map.isMap=t=>!!(null==t?void 0:t[$])),K in Set.prototype||(Object.defineProperty(Set.prototype,K,{get:()=>!0}),Set.isSet=t=>!!(null==t?void 0:t[K]));class J{constructor(t,e){this.canonicalizer=t,this.items=void 0===e?new Map:new Map(G(e[Symbol.iterator](),(([t,e])=>[this._key(t),[t,e]])))}_key(t){return this.canonicalizer(t)}get(t,e){const n=this.items.get(this._key(t));return void 0===n?e:n[1]}getOrSet(t,e){const n=this._key(t);let r=this.items.get(n);return void 0===r&&(r=[t,e()],this.items.set(n,r)),r[1]}set(t,e){return this.items.set(this._key(t),[t,e]),this}forEach(t,e){this.items.forEach((([n,r])=>t.call(e,r,n,this)))}entries(){return this.items.values()}keys(){return G(this.items.values(),(([t,e])=>t))}values(){return G(this.items.values(),(([t,e])=>e))}delete(t){return this.items.delete(this._key(t))}getAndDelete(t,e){const n=this._key(t),r=this.items.get(n);return void 0===r?e:(this.items.delete(n),r[1])}clear(){this.items.clear()}has(t){return this.items.has(this._key(t))}get size(){return this.items.size}[Symbol.iterator](){return this.items.values()}get[Symbol.toStringTag](){return"FlexMap"}equals(t,e=((t,e)=>t===e)){if(!("size"in t&&"has"in t&&"get"in t))return!1;if(this.size!==t.size)return!1;for(let[n,r]of this.items.values()){if(!t.has(n))return!1;if(!e(r,t.get(n)))return!1}return!0}update(t,e,n,r=((t,e)=>t===e)){const s=this._key(t);if(this.items.has(s)){const n=this.items.get(s)[1],i=e(n);return void 0===i?(this.items.delete(s),-1):(r(i,n)||this.items.set(s,[t,i]),0)}{const r=e(n);return void 0===r?0:(this.items.set(s,[t,r]),1)}}canonicalKeys(){return this.items.keys()}get[$](){return!0}}class X{constructor(t,e){this.canonicalizer=t,this.items=void 0===e?new Map:new Map(G(e[Symbol.iterator](),(t=>[this._key(t),t])))}_key(t){return this.canonicalizer(t)}has(t){return this.items.has(this._key(t))}get(t){const e=this._key(t);return this.items.has(e)?{item:this.items.get(e)}:null}add(t){return this.items.set(this._key(t),t),this}forEach(t,e){this.items.forEach((n=>t.call(e,n,n,this)))}entries(){return G(this.items.values(),(t=>[t,t]))}keys(){return this.items.values()}values(){return this.items.values()}delete(t){return this.items.delete(this._key(t))}clear(){this.items.clear()}get size(){return this.items.size}[Symbol.iterator](){return this.items.values()}get[Symbol.toStringTag](){return"FlexSet"}equals(t){if(!("size"in t)||!("has"in t))return!1;if(this.size!==t.size)return!1;for(let e of this.items.values())if(!t.has(e))return!1;return!0}canonicalValues(){return this.items.keys()}union(t){const e=new X(this.canonicalizer,this);for(let n of t)e.add(n);return e}intersect(t){const e=new X(this.canonicalizer);for(let n of this)t.has(n)&&e.add(n);return e}subtract(t){const e=new X(this.canonicalizer);for(let n of this)t.has(n)||e.add(n);return e}get[K](){return!0}}const Z=Symbol.for("DictionaryType");class Q extends J{constructor(t,e,n){super((e=>z(t(e))),n),this.encodeK=t,this.encodeV=e}__preserve_on__(t){et(this,t,((t,e)=>e.push(this.encodeK(t))),((t,e)=>e.push(this.encodeV(t))))}__preserve_text_on__(t){nt(this,t,((t,e)=>e.push(this.encodeK(t))),((t,e)=>e.push(this.encodeV(t))))}}class Y extends Q{get[Z](){return"Dictionary"}static isKeyedDictionary(t){return"Dictionary"===(null==t?void 0:t[Z])}constructor(t){super((t=>t),(t=>t),t)}mapEntries(t){const e=new Y;for(let n of this.entries()){const r=t(n);e.set(r[0],r[1])}return e}clone(){return new Y(this)}get[Symbol.toStringTag](){return"Dictionary"}}class tt extends Y{static isDictionary(t){return"Dictionary"===(null==t?void 0:t[Z])}static __from_preserve__(t){return tt.isDictionary(t)?t:void 0}}function et(e,n,r,s){if(n.canonical){const i=Array.from(e),o=new N({canonical:!0,embeddedEncode:n.embeddedEncode}),a=i.map((([t,e],n)=>(r(t,o),[o.contents(),n])));a.sort(((t,e)=>g.compare(t[0],e[0]))),n.grouped(t.Dictionary,(()=>a.forEach((([t,e])=>{const[o,a]=i[e];r(o,n),s(a,n)}))))}else n.grouped(t.Dictionary,(()=>e.forEach(((t,e)=>{r(e,n),s(t,n)}))))}function nt(t,e,n,r){e.state.writeSeq("{","}",t.entries(),(([t,s])=>{n(t,e),c.isAnnotated(s)&&h(s).length>1&&e.state.isIndenting?(e.state.pieces.push(":"),e.state.indentCount++,e.state.writeIndent(),r(s,e),e.state.indentCount--):(e.state.pieces.push(": "),r(s,e))}))}class rt extends X{constructor(t,e){super((e=>z(t(e))),e),this.encodeV=t}__preserve_on__(t){ot(this,t,((t,e)=>e.push(this.encodeV(t))))}__preserve_text_on__(t){at(this,t,((t,e)=>e.push(this.encodeV(t))))}}class st extends rt{get[Z](){return"Set"}static isKeyedSet(t){return"Set"===(null==t?void 0:t[Z])}constructor(t){super((t=>t),t)}map(t){return new st(G(this[Symbol.iterator](),t))}filter(t){const e=new st;for(let n of this)t(n)&&e.add(n);return e}clone(){return new st(this)}get[Symbol.toStringTag](){return"Set"}}let it=class t extends st{static isSet(t){return"Set"===(null==t?void 0:t[Z])}static __from_preserve__(e){return t.isSet(e)?e:void 0}};function ot(e,n,r){if(n.canonical){const s=new N({canonical:!0,embeddedEncode:n.embeddedEncode}),i=Array.from(e).map((t=>(r(t,s),[s.contents(),t])));i.sort(((t,e)=>g.compare(t[0],e[0]))),n.grouped(t.Set,(()=>i.forEach((([t,e])=>r(e,n)))))}else n.grouped(t.Set,(()=>e.forEach((t=>r(t,n)))))}function at(t,e,n){e.state.writeSeq("#{","}",t,(t=>n(t,e)))}function ct(t){return Array.isArray(t)||it.isSet(t)||tt.isDictionary(t)}function ut(t){return Array.isArray(t)&&!("label"in t)}const ht=Symbol.for("FloatType");class dt{constructor(t){this.value="number"==typeof t?t:t.value}__preserve_text_on__(t){t.state.pieces.push(this.toString())}equals(t){return!!Object.is(t.constructor,this.constructor)&&(Number.isNaN(this.value)&&Number.isNaN(t.value)?t.toBytes().equals(this.toBytes()):Object.is(t.value,this.value))}hashCode(){return 0|this.value}}function lt(t){return"number"==typeof t?t:dt.isFloat(t)?t.value:NaN}function ft(t){if(Object.is(t,-0))return"-0.0";const e=""+t;return e.includes(".")||e.includes("e")||e.includes("E")?e:e+".0"}dt.isFloat=t=>void 0!==(null==t?void 0:t[ht]),dt.isDouble=t=>"Double"===(null==t?void 0:t[ht]);class pt extends dt{__as_preserve__(){return this}static fromBytes(t){return new pt(S(t).getFloat64(0,!1))}static __from_preserve__(t){return dt.isDouble(t)?t:void 0}__preserve_on__(e){e.state.emitbyte(t.Ieee754),e.state.emitbyte(8),e.state.makeroom(8),e.state.view.setFloat64(e.state.index,this.value,!1),e.state.index+=8}toBytes(){const t=new g(8);return t.dataview().setFloat64(0,this.value,!1),t}toString(){return Number.isFinite(this.value)?ft(this.value):'#xd"'+this.toBytes().toHex()+'"'}get[ht](){return"Double"}}function yt(t){return new pt(t)}function mt(t,e){return s(t,e)?e:void 0}class bt{constructor(t,e){this.index=0,this.packet=x(t),this.options=e}get includeAnnotations(){var t;return null!==(t=this.options.includeAnnotations)&&void 0!==t&&t}write(t){this.index===this.packet.length?this.packet=x(t):this.packet=g.concat([this.packet.slice(this.index),t])._view,this.index=0}atEnd(){return this.index>=this.packet.length}mark(){return this.index}restoreMark(t){this.index=t}shortGuard(t,e){if(this.atEnd())return e();const n=this.mark();try{return t()}catch(t){if(I.isShortPacket(t))return this.restoreMark(n),e();throw t}}nextbyte(){if(this.atEnd())throw new I("Short packet");return this.packet[this.index++]}nextbytes(t){const e=this.index;if(this.index+=t,this.index>this.packet.length)throw new I("Short packet");return new DataView(this.packet.buffer,this.packet.byteOffset+e,t)}varint(){const t=this.nextbyte();return t<128?t:(this.varint()<<7)+(t-128)}peekend(){return this.nextbyte()===t.End||(this.index--,!1)}nextint(t){const e=this.index;if(0===t)return 0;if(t>7)return this.nextbigint(t);if(7===t){const e=this.packet[this.index];if(e>=32&&e<224)return this.nextbigint(t)}let n=this.nextbyte();128&n&&(n-=256);for(let e=1;e<t;e++)n=256*n+this.nextbyte();return Number.isSafeInteger(n)?n:(this.index=e,this.nextbigint(t))}nextbigint(t){if(0===t)return BigInt(0);const e=g.from(this.nextbytes(t));if(e.get(0)>=128){const t=e.toHex((t=>v(15-t)));return~BigInt("0x"+t)}{const t=e.toHex();return BigInt("0x"+t)}}wrap(t){return this.includeAnnotations?new c(t):t}unshiftAnnotation(t,e){return this.includeAnnotations&&e.annotations.unshift(t),e}}const wt={decode(t){throw new Error("Embeddeds not permitted at this point in Preserves document")},fromValue(t,e){throw new Error("Embeddeds not permitted at this point in Preserves document")}};class gt{constructor(t=new Uint8Array(0),e){var n,r,s;if(t instanceof bt)this.state=t,this.embeddedDecode=null!==(n=e)&&void 0!==n?n:wt;else{const n=null!==(r=e)&&void 0!==r?r:{};this.state=new bt(t,n),this.embeddedDecode=null!==(s=n.embeddedDecode)&&void 0!==s?s:wt}}write(t){this.state.write(t)}nextvalues(){const t=[];for(;!this.state.peekend();)t.push(this.next());return t}static dictionaryFromArray(t){const e=new tt;if(t.length%2)throw new E("Missing dictionary value");for(let n=0;n<t.length;n+=2)e.set(t[n],t[n+1]);return e}next(){const e=this.state.nextbyte();switch(e){case t.False:return this.state.wrap(!1);case t.True:return this.state.wrap(!0);case t.End:throw new E("Unexpected Compound end marker");case t.Annotation:{const t=this.next(),e=this.next();return this.state.unshiftAnnotation(t,e)}case t.Embedded:return this.state.wrap(this.embeddedDecode.decode(this.state));case t.Ieee754:if(8===this.state.varint())return this.state.wrap(pt.fromBytes(this.state.nextbytes(8)));throw new E("Invalid IEEE754 size");case t.SignedInteger:return this.state.wrap(this.state.nextint(this.state.varint()));case t.String:return this.state.wrap(g.from(this.state.nextbytes(this.state.varint())).fromUtf8());case t.ByteString:return this.state.wrap(g.from(this.state.nextbytes(this.state.varint())));case t.Symbol:return this.state.wrap(Symbol.for(g.from(this.state.nextbytes(this.state.varint())).fromUtf8()));case t.Record:{const t=this.nextvalues();if(0===t.length)throw new E("Too few elements in encoded record");return this.state.wrap(D(t[0],t.slice(1)))}case t.Sequence:return this.state.wrap(this.nextvalues());case t.Set:return this.state.wrap(new it(this.nextvalues()));case t.Dictionary:return this.state.wrap(gt.dictionaryFromArray(this.nextvalues()));default:throw new E("Unsupported Preserves tag: "+e)}}try_next(){return this.state.shortGuard((()=>this.next()),(()=>{}))}atEnd(){return this.state.atEnd()}mark(){return this.state.mark()}restoreMark(t){return this.state.restoreMark(t)}skip(){this.next()}withEmbeddedDecode(t,e){return e(new gt(this.state,t))}skipAnnotations(e){const n=this.mark();for(;!this.state.atEnd()&&this.state.packet[this.state.index]===t.Annotation;)this.state.index++,this.skip();return e((()=>this.restoreMark(n)))}nextBoolean(){return this.skipAnnotations((e=>{switch(this.state.nextbyte()){case t.False:return!1;case t.True:return!0;default:return e()}}))}nextDouble(){return this.skipAnnotations((e=>this.state.nextbyte()!==t.Ieee754||8!==this.state.nextbyte()?e():pt.fromBytes(this.state.nextbytes(8))))}nextEmbedded(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Embedded?this.embeddedDecode.decode(this.state):e()))}nextSignedInteger(){return this.skipAnnotations((e=>this.state.nextbyte()===t.SignedInteger?this.state.nextint(this.state.varint()):e()))}nextString(){return this.skipAnnotations((e=>this.state.nextbyte()===t.String?g.from(this.state.nextbytes(this.state.varint())).fromUtf8():e()))}nextByteString(){return this.skipAnnotations((e=>this.state.nextbyte()===t.ByteString?g.from(this.state.nextbytes(this.state.varint())):e()))}nextSymbol(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Symbol?Symbol.for(g.from(this.state.nextbytes(this.state.varint())).fromUtf8()):e()))}openRecord(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Record||(e(),!1)))}openSequence(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Sequence||(e(),!1)))}openSet(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Set||(e(),!1)))}openDictionary(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Dictionary||(e(),!1)))}closeCompound(){return this.state.peekend()}}function vt(t,e={}){return new gt(t,e).next()}function _t(t,e={}){return vt(t,Object.assign(Object.assign({},e),{includeAnnotations:!0}))}function xt(t){return!("object"!=typeof t||null===t||Array.isArray(t)||g.isBytes(t)||dt.isFloat(t)||it.isSet(t)||tt.isDictionary(t)||c.isAnnotated(t))}class St{constructor(t){this.value=t}}class At{constructor(t){this.generic=t}equals(t,e){return"object"==typeof t&&"generic"in t&&e(this.generic,t.generic)}toString(){return this.generic.toString()}}function kt(t){return c.isAnnotated(t)?t.item:t}function Et(t){return Bt(t,1)}function Bt(t,e=1/0){return function t(e,n){if(0===n)return e;if(!c.isAnnotated(e))return e;const r=n-1;function s(e){return t(e,r)}if(D.isRecord(e.item))return D(t(e.item.label,n),e.item.map(s));if(c.isAnnotated(e.item))throw new Error("Improper annotation structure");return 0===r?e.item:Array.isArray(e.item)||it.isSet(e.item)?e.item.map(s):tt.isDictionary(e.item)?e.item.mapEntries((t=>[s(t[0]),s(t[1])])):e.item}(t,e)}function It(t){return"object"==typeof t&&null!==t&&"__preserve_text_on__"in t&&"function"==typeof t.__preserve_text_on__}const Dt={encode(t,e){new N(t,this).push(e.generic)},toValue:t=>t.generic},Ct={encode(t,e){throw new Error("Embeddeds not permitted encoding Preserves document")},toValue(t){throw new Error("Embeddeds not permitted writing Preserves document")}};class jt{constructor(t){var e;this.pieces=[],this.indentCount=0,this.options=t,this.indentDelta=" ".repeat(null!==(e=t.indent)&&void 0!==e?e:0)}get isIndenting(){return this.indentDelta.length>0}get includeAnnotations(){var t;return null===(t=this.options.includeAnnotations)||void 0===t||t}writeIndent(){if(this.isIndenting){this.pieces.push("\n");for(let t=0;t<this.indentCount;t++)this.pieces.push(this.indentDelta)}}writeIndentSpace(){this.isIndenting?this.writeIndent():this.pieces.push(" ")}escapeStringlikeChar(t,e=(t=>t)){switch(t){case"\\":return"\\\\";case"\b":return"\\b";case"\f":return"\\f";case"\n":return"\\n";case"\r":return"\\r";case"\t":return"\\t";default:return e(t)}}escapeStringlike(t,e){let n=e;for(let r of t)n+=r===e?"\\"+e:this.escapeStringlikeChar(r);return n+e}writeSeq(t,e,n,r){let s=n[Symbol.iterator]();this.pieces.push(t);const i=s.next();if(!0!==i.done){const t=i.value,e=s.next();if(!0===e.done)r(t);else{let n;for(this.indentCount++,this.writeIndent(),r(t),this.writeIndentSpace(),r(e.value);!0!==(n=s.next()).done;)this.writeIndentSpace(),r(n.value);this.indentCount--,this.writeIndent()}}this.pieces.push(e)}writeBytes(t){var e,n;const r=null!==(e=this.options.maxBinaryAsciiLength)&&void 0!==e?e:1024,s=null!==(n=this.options.maxBinaryAsciiProportion)&&void 0!==n?n:.75;if(t.length>=r)this.writeBase64(t);else{let e=0,n=Math.min(t.length,r);for(let r=0;r<n;r++){const n=t[r];switch(n){case 9:case 10:case 13:e++;break;default:n>=32&&n<=126&&e++}}0===n||e/n>=s?this.writeBinaryStringlike(t):this.writeBase64(t)}}writeBase64(t){this.pieces.push("#[",y(t),"]")}writeBinaryStringlike(t){let e='#"';for(let n of t)e+=34===n?'\\"':this.escapeStringlikeChar(String.fromCharCode(n),(t=>n>=32&&n<=126&&92!==n?t:"\\x"+("0"+n.toString(16)).slice(-2)));this.pieces.push(e+'"')}couldBeFlat(t){let e=!1;for(let n of t){if(Array.isArray(n)||Set.isSet(n)||Map.isMap(n)){if(e)return!1;e=!0}if(this.includeAnnotations&&r(n)&&n.annotations.length>1)return!1}return!0}}class Ot{constructor(t={},e){var n;t instanceof jt?(this.state=t,this.embeddedWrite=e):(this.state=new jt(t),this.embeddedWrite=null!==(n=t.embeddedWrite)&&void 0!==n?n:Ct)}static stringify(t,e){const n=new Ot(e);return n.push(t),n.contents()}contents(){return this.state.pieces.join("")}get includeAnnotations(){return this.state.includeAnnotations}push(t){switch(typeof t){case"boolean":this.state.pieces.push(t?"#t":"#f");break;case"string":this.state.pieces.push(this.state.escapeStringlike(t,'"'));break;case"symbol":{const e=t.description;/^[-a-zA-Z0-9~!$%^&*?_=+/.]+$/.exec(e)&&!Pt.exec(e)?this.state.pieces.push(e):this.state.pieces.push(this.state.escapeStringlike(e,"|"));break}case"bigint":case"number":this.state.pieces.push(""+t);break;case"object":if(null===t)throw new Error("Cannot encode null in Preserves Writer.push");if(It(t))t.__preserve_text_on__(this);else if(It(t))t.__preserve_text_on__(this);else if(ArrayBuffer.isView(t))if(t instanceof Uint8Array)this.state.writeBytes(t);else{const e=new Uint8Array(t.buffer,t.byteOffset,t.byteLength);this.state.writeBytes(e)}else if(D.isRecord(t)){const e=this.state.couldBeFlat(t);this.state.pieces.push("<"),this.push(t.label),e||this.state.indentCount++;for(let n of t)e?this.state.pieces.push(" "):this.state.writeIndentSpace(),this.push(n);e||this.state.indentCount--,this.state.pieces.push(">")}else!function(t){return"object"==typeof t&&null!==t&&"function"==typeof t[Symbol.iterator]}(t)?(t=>{this.state.pieces.push("#:"),"write"in this.embeddedWrite?this.embeddedWrite.write(this.state,t):new Ot(this.state,Dt).push(this.embeddedWrite.toValue(t))})(t):this.state.writeSeq("[","]",t,(t=>this.push(t)));break;default:(e=>{throw new Error(`Internal error: unhandled in Preserves Writer.push for ${t}`)})()}return this}}function qt(t){switch(typeof t){case"number":if(!Number.isInteger(t))throw new TypeError("Refusing to autoconvert non-integer number to Double");case"bigint":case"string":case"symbol":case"boolean":return t;case"undefined":case"function":break;case"object":if(null===t)break;if("function"==typeof t.__as_preserve__)return t.__as_preserve__();if(D.isRecord(t))return t;if(Array.isArray(t))return t.map(qt);if(ArrayBuffer.isView(t)||t instanceof ArrayBuffer)return g.from(t);if(Map.isMap(t)){const e=new tt;return t.forEach(((t,n)=>e.set(qt(n),qt(t)))),e}if(it.isSet(t)){const e=new it;return t.forEach((t=>e.add(qt(t)))),e}return t}throw new TypeError("Cannot represent JavaScript value as Preserves: "+t)}function Mt(t,e){return new Wt(t,e).next()}function Ut(t,e){return new Wt(t,e).readToEnd()}tt.fromJS=function(t){if(tt.isDictionary(t))return t;const e=new tt;return Object.entries(t).forEach((([t,n])=>e.set(t,qt(n)))),e};const Ft={write(t,e){if(e instanceof At)new Ot(t,this).push(e.generic);else{try{const n=qt(e);if(!xt(n))return void new Ot(t,this).push(n)}catch(t){}try{return void t.pieces.push(JSON.stringify(e))}catch(t){}t.pieces.push("⌜"+e+"⌝")}}};function Nt(t,e){var n;return(e=Object.assign({},null!=e?e:{})).embeddedWrite=null!==(n=e.embeddedWrite)&&void 0!==n?n:Ft,Ot.stringify(qt(t),e)}function Tt(t,...e){const n=[t[0]];return e.forEach(((e,r)=>{n.push(Nt(e)),n.push(t[r+1])})),n.join("")}[c,g,Y,st].forEach((t=>{t.prototype.toString=function(){return Nt(this)}}));const Rt=BigInt(Number.MAX_SAFE_INTEGER),Vt=BigInt(Number.MIN_SAFE_INTEGER),Pt=/^([-+]?\d+)((\.\d+([eE][-+]?\d+)?)|([eE][-+]?\d+))?$/;class zt{constructor(t,e){switch(this.discarded=0,this.buffer=t,typeof e.name){case"undefined":this.pos=i();break;case"string":this.pos=i(e.name);break;case"object":this.pos=Object.assign({},e.name)}this.index=0,this.options=e}error(t,e){throw new E(t,Object.assign({},e))}get includeAnnotations(){var t;return null!==(t=this.options.includeAnnotations)&&void 0!==t&&t}copyPos(){return Object.assign({},this.pos)}write(t){this.atEnd()?this.buffer=t:this.buffer=this.buffer.substring(this.index)+t,this.discarded+=this.index,this.index=0}atEnd(){return this.index>=this.buffer.length}peek(){if(this.atEnd())throw new I("Short term",this.pos);return this.buffer[this.index]}advance(){const t=this.index++;return o(this.pos,this.buffer[t]),t}nextchar(){if(this.atEnd())throw new I("Short term",this.pos);return this.buffer[this.advance()]}nextcharcode(){if(this.atEnd())throw new I("Short term",this.pos);return this.buffer.charCodeAt(this.advance())}skipws(t=!1){for(;!this.atEnd();){const e=this.peek();if(!(Lt(e)||t&&","===e))break;this.advance()}}readHex2(){return _(this.nextcharcode(),E)<<4|_(this.nextcharcode(),E)}readHex4(){return _(this.nextcharcode(),E)<<12|_(this.nextcharcode(),E)<<8|_(this.nextcharcode(),E)<<4|_(this.nextcharcode(),E)}readHexBinary(){const t=[];for(;;){if(this.skipws(),'"'===this.peek())return this.advance(),g.from(t);t.push(this.readHex2())}}readHexFloat(){const t=this.copyPos();'"'!==this.nextchar()&&this.error("Missing open-double-quote in hex-encoded floating-point number",t);const e=this.readHexBinary();return 8!==e.length&&this.error("Incorrect number of bytes in hex-encoded Double",t),pt.fromBytes(e)}readBase64Binary(){let t="";for(;;){this.skipws();const e=this.nextchar();if("]"===e)break;t+=e}return g.fromBase64(t)}requireDelimiter(t){this.delimiterFollows()||this.error(`Delimiter must follow ${t}`,this.pos)}delimiterFollows(){if(this.atEnd())return!0;const t=this.peek();return-1!=='(){}[]<>";,@#:|'.indexOf(t)||Lt(t)}readRawSymbolOrNumber(t){for(;!this.delimiterFollows();)t+=this.nextchar();const e=Pt.exec(t);if(e){if(void 0===e[2]){let t=BigInt(e[1]);return t<=Vt||t>=Rt?t:Number(t)}return yt(parseFloat(t))}return Symbol.for(t)}readStringlike(t,e,n,r,s){let i=[];for(;;){const o=this.nextchar();switch(o){case n:return e(i);case"\\":{const e=this.nextchar();switch(e){case r:i.push(s());break;case n:case"\\":case"/":i.push(t(e));break;case"b":i.push(t("\b"));break;case"f":i.push(t("\f"));break;case"n":i.push(t("\n"));break;case"r":i.push(t("\r"));break;case"t":i.push(t("\t"));break;default:this.error(`Invalid escape code \\${e}`,this.pos)}break}default:i.push(t(o))}}}readString(t){return this.readStringlike((t=>t),(t=>t.join("")),t,"u",(()=>{const t=this.readHex4();if(t>=55296&&t<=57343){if("\\"===this.nextchar()&&"u"===this.nextchar()){const e=this.readHex4();if(e>=56320&&e<=57343&&t<=56319)return String.fromCharCode(t,e)}this.error("Invalid surrogate pair",this.pos)}return String.fromCharCode(t)}))}readLiteralBinary(){return this.readStringlike((t=>{const e=t.charCodeAt(0);return e>=256&&this.error(`Invalid code point ${e} in literal binary`,this.pos),e}),g.from,'"',"x",(()=>this.readHex2()))}}const Ht={decode(t){return new At(new gt(t,this).next())},fromValue:(t,e)=>new At(e.includeAnnotations?t:Bt(t))};class Wt{constructor(t="",e){var n,r;if(t instanceof zt)this.state=t,this.embeddedType=e;else{const s=null!==(n=e)&&void 0!==n?n:{};this.state=new zt(t,s),this.embeddedType=null!==(r=s.embeddedDecode)&&void 0!==r?r:wt}}write(t){this.state.write(t)}readCommentLine(){const t=this.state.copyPos();let e="";for(;;){const n=this.state.nextchar();if("\n"===n||"\r"===n)return this.wrap(e,t);e+=n}}wrap(t,e){return this.state.includeAnnotations&&!c.isAnnotated(t)&&(t=new c(t,e)),t}annotateNextWith(t){if(this.state.skipws(),this.state.atEnd())throw new E("Trailing annotations and comments are not permitted",this.state.pos);const e=this.next();return this.state.includeAnnotations&&e.annotations.unshift(t),e}readToEnd(){const t=[];for(;;){if(this.state.skipws(),this.state.atEnd())return t;t.push(this.next())}}next(){this.state.skipws();const t=this.state.copyPos(),e=(()=>{const e=this.state.nextchar();switch(e){case'"':return this.state.readString('"');case"|":return Symbol.for(this.state.readString("|"));case";":this.state.error("Semicolon is reserved syntax",t);case"@":return this.annotateNextWith(this.next());case":":this.state.error("Unexpected key/value separator between items",t);case"#":{const e=this.state.nextchar();switch(e){case" ":case"\t":return this.annotateNextWith(this.readCommentLine());case"\n":case"\r":return this.annotateNextWith("");case"!":return this.annotateNextWith(D(Symbol.for("interpreter"),[this.readCommentLine()]));case"f":return this.state.requireDelimiter("#f"),!1;case"t":return this.state.requireDelimiter("#t"),!0;case"{":return this.readSet();case'"':return this.state.readLiteralBinary();case"x":switch(this.state.nextchar()){case'"':return this.state.readHexBinary();case"d":return this.state.readHexFloat();default:this.state.error("Invalid #x syntax",t)}case"[":return this.state.readBase64Binary();case":":return this.embeddedType.fromValue(new Wt(this.state,Ht).next(),this.state.options);default:this.state.error(`Invalid # syntax: ${e}`,t)}}case"<":return D(this.next(),this.readSequence(">",!1));case"[":return this.readSequence("]",!0);case"{":return this.readDictionary();case">":this.state.error("Unexpected >",t);case"]":this.state.error("Unexpected ]",t);case"}":this.state.error("Unexpected }",t);case",":this.state.error("Unexpected ,",t);default:return this.state.readRawSymbolOrNumber(e)}})();return this.wrap(e,t)}seq(t,e,n,r){for(;;){if(this.state.skipws(t),this.state.peek()===r)return this.state.advance(),e;n(this.next(),e)}}readSequence(t,e){return this.seq(e,[],((t,e)=>e.push(t)),t)}readDictionary(){return this.seq(!0,new tt,((t,e)=>{if(this.state.skipws(),":"===this.state.peek())e.has(t)&&this.state.error(`Duplicate key: ${Nt(t)}`,this.state.pos),this.state.advance(),e.set(t,this.next());else this.state.error("Missing key/value separator",this.state.pos)}),"}")}readSet(){return this.seq(!0,new it,((t,e)=>{e.has(t)&&this.state.error(`Duplicate value in set: ${Nt(t)}`,this.state.pos),e.add(t)}),"}")}}function Lt(t){return-1!==" \t\n\r".indexOf(t)}const $t=Object.assign({},Ht,Dt),Kt=Object.assign({},wt,Ct),Gt={decode(t){throw new Error("Cannot decode identityEmbeddedType")},fromValue(t,e){throw new Error("Cannot decode identityEmbeddedType")}},Jt=Object.assign({},Gt,U);var Xt;!function(t){t[t.Boolean=0]="Boolean",t[t.Double=1]="Double",t[t.SignedInteger=2]="SignedInteger",t[t.String=3]="String",t[t.ByteString=4]="ByteString",t[t.Symbol=5]="Symbol",t[t.Record=6]="Record",t[t.Sequence=7]="Sequence",t[t.Set=8]="Set",t[t.Dictionary=9]="Dictionary",t[t.Embedded=10]="Embedded",t[t.Annotated=11]="Annotated"}(Xt||(Xt={}));class Zt{boolean(t){}double(t){}integer(t){}string(t){}bytes(t){}symbol(t){}record(t,e){e(t.label),t.forEach(e)}array(t,e){t.forEach(e)}set(t,e){t.forEach(e)}dictionary(t,e){t.forEach(((t,n)=>{e(n),e(t)}))}annotated(t,e){e(t.item),t.annotations.forEach(e)}embedded(t,e){}}class Qt extends Zt{constructor(t){super(),this.f=t}embedded(t,e){this.f(t,e)}}class Yt{boolean(t){return t}double(t){return yt(t)}integer(t){return t}string(t){return t}bytes(t){return t}symbol(t){return t}record(t,e){return D(e(t.label),t.map(e))}array(t,e){return t.map(e)}set(t,e){return t.map(e)}dictionary(t,e){return t.mapEntries((([t,n])=>[e(t),e(n)]))}annotated(t,e){return u(e(t.item),...t.annotations.map(e))}}class te extends Yt{embedded(t,e){return t}}class ee extends Yt{constructor(t){super(),this.f=t}embedded(t,e){return this.f(t)}}function ne(t){switch(typeof t){case"boolean":return Xt.Boolean;case"number":if(Number.isInteger(t))return Xt.SignedInteger;throw new Error("Non-integer number in Preserves valueClass; missing Float wrapper?");case"bigint":return Xt.SignedInteger;case"string":return Xt.String;case"symbol":return Xt.Symbol;case"object":return D.isRecord(t)?Xt.Record:Array.isArray(t)?Xt.Sequence:it.isSet(t)?Xt.Set:tt.isDictionary(t)?Xt.Dictionary:c.isAnnotated(t)?Xt.Annotated:g.isBytes(t)?Xt.ByteString:dt.isDouble(t)?Xt.Double:Xt.Embedded;default:(t=>{throw new Error("Internal error")})()}}const re=new te;function se(t,e){const n=t=>{switch(typeof t){case"boolean":return e.boolean(t);case"number":return Number.isInteger(t)?e.integer(t):e.double(t);case"bigint":return e.integer(t);case"string":return e.string(t);case"symbol":return e.symbol(t);case"object":return D.isRecord(t)?e.record(t,n):Array.isArray(t)?e.array(t,n):it.isSet(t)?e.set(t,n):tt.isDictionary(t)?e.dictionary(t,n):c.isAnnotated(t)?e.annotated(t,n):g.isBytes(t)?e.bytes(t):dt.isDouble(t)?e.double(t.value):e.embedded(t,n);default:(t=>{throw new Error("Internal error")})()}};return n(t)}function ie(t,e){return se(t,new ee(e))}function oe(t,e){return se(t,new Qt(e))}function ae(t,e,...n){function r(){throw new Error("Cannot merge items")}function i(e,n){return e===n?e:ct(e)||ct(n)||!s(e,n)?se(e,{boolean:r,double:t=>s(e,n)?e:r(),integer:r,string:r,bytes:t=>s(e,n)?e:r(),symbol:r,record:t=>(D.isRecord(n)||r(),D(i(t.label,n.label),o(t,n))),array:t=>(Array.isArray(n)&&!D.isRecord(n)||r(),o(t,n)),set(t){r()},dictionary(t){tt.isDictionary(n)||r();const e=new tt;return t.forEach(((t,r)=>{const s=n.get(r);e.set(r,void 0===s?t:i(t,s))})),n.forEach(((n,r)=>{t.has(r)||e.set(r,n)})),e},annotated:t=>i(t,kt(n)),embedded(e){xt(n)||r();const s=t(e,n);return void 0===s&&r(),s}}):e}function o(t,e){return t.length<=e.length?e.map(((e,n)=>n<t.length?i(t[n],e):e)):t.map(((t,n)=>n<e.length?i(t,e[n]):t))}return n.reduce(i,e)}function ce(t){switch(r(t)&&(t=t.item),typeof t){case"boolean":return 0;case"number":case"bigint":return 3;case"string":return 4;case"symbol":return 6;case"object":if(dt.isFloat(t))return 2;if(g.isBytes(t))return 5;if(Array.isArray(t))return"label"in t?7:8;if(it.isSet(t))return 9;if(tt.isDictionary(t))return 10;if(xt(t))return 11;default:throw new Error("Invalid Value<T> in typeCode")}}function ue(t,e,n=((t,e)=>s(t,e)?0:t<e?-1:1)){return function t(e,s){r(e)&&(e=e.item),r(s)&&(s=s.item);const i=ce(e),o=ce(s);if(i<o)return-1;if(i>o)return 1;switch(i){case 0:case 3:case 4:return e<s?-1:e>s?1:0;case 2:{const t=e.value,n=s.value;return t<n?-1:t>n?1:0}case 5:return g.compare(e,s);case 6:{const t=e.description,n=s.description;return t<n?-1:t>n?1:0}case 7:{const n=t(e.label,s.label);if(0!==n)return n}case 8:{const n=e,r=s,i=Math.min(n.length,r.length);for(let e=0;e<i;e++){const s=t(n[e],r[e]);if(0!==s)return s}return n.length<r.length?-1:n.length>r.length?1:0}case 9:return t(Array.from(e).sort(t),Array.from(s).sort(t));case 10:return t(Array.from(e).sort(t),Array.from(s).sort(t));case 11:return n(e,s);default:throw new Error("Invalid typeCode: "+i)}}(t,e)}Array.__from_preserve__=t=>Array.isArray(t)?t:void 0;const he=Array,de=Symbol;export{c as Annotated,he as Array,g as Bytes,e as Constants,E as DecodeError,gt as Decoder,bt as DecoderState,tt as Dictionary,Z as DictionaryType,yt as Double,pt as DoubleFloat,St as Embedded,Q as EncodableDictionary,rt as EncodableSet,B as EncodeError,N as Encoder,F as EncoderState,A as ErrorType,J as FlexMap,X as FlexSet,dt as Float,ht as FloatType,Qt as ForEachEmbedded,At as GenericEmbedded,re as IDENTITY_FOLD,te as IdentityFold,W as IdentityMap,L as IdentitySet,$ as IsMap,n as IsPreservesAnnotated,w as IsPreservesBytes,K as IsSet,Y as KeyedDictionary,st as KeyedSet,ee as MapFold,Pt as NUMBER_RE,k as PreservesCodecError,Wt as Reader,zt as ReaderState,D as Record,it as Set,I as ShortPacket,de as Symbol,Xt as ValueClass,Yt as ValueFold,Zt as VoidFold,Ot as Writer,jt as WriterState,G as _iterMap,u as annotate,h as annotations,mt as asLiteral,P as canonicalEncode,z as canonicalString,ue as compare,S as dataview,vt as decode,f as decodeBase64,_t as decodeWithAnnotations,M as embeddedId,T as encode,y as encodeBase64,et as encodeDictionaryOn,ot as encodeSetOn,H as encodeWithAnnotations,lt as floatValue,ft as floatlikeString,se as fold,oe as forEachEmbedded,a as formatPosition,qt as fromJS,$t as genericEmbeddedType,Ht as genericEmbeddedTypeDecode,Dt as genericEmbeddedTypeEncode,v as hexDigit,Jt as identityEmbeddedType,Gt as identityEmbeddedTypeDecode,U as identityEmbeddedTypeEncode,s as is,r as isAnnotated,ct as isCompound,xt as isEmbedded,C as isPreservable,It as isPreserveWritable,ut as isSequence,ie as mapEmbeddeds,ae as merge,Kt as neverEmbeddedType,wt as neverEmbeddedTypeDecode,Ct as neverEmbeddedTypeEncode,i as newPosition,Mt as parse,Ut as parseAll,Et as peel,d as position,Tt as preserves,Nt as stringify,Ft as stringifyEmbeddedWrite,Bt as strip,ce as typeCode,kt as unannotate,x as underlying,_ as unhexDigit,o as updatePosition,ne as valueClass,nt as writeDictionaryOn,at as writeSetOn}; | ||
var t;!function(t){t[t.False=128]="False",t[t.True=129]="True",t[t.End=132]="End",t[t.Annotation=133]="Annotation",t[t.Embedded=134]="Embedded",t[t.Ieee754=135]="Ieee754",t[t.SignedInteger=176]="SignedInteger",t[t.String=177]="String",t[t.ByteString=178]="ByteString",t[t.Symbol=179]="Symbol",t[t.Record=180]="Record",t[t.Sequence=181]="Sequence",t[t.Set=182]="Set",t[t.Dictionary=183]="Dictionary"}(t||(t={}));var e=Object.freeze({__proto__:null,get Tag(){return t}});const n={};function r(t){const e=new Uint8Array(Math.floor(3*t.length/4));let r=0,s=0;for(;r<t.length;){const i=n[t[r++]],o=n[t[r++]],a=n[t[r++]],u=n[t[r++]],c=i<<18|o<<12|a<<6|u;if(e[s++]=c>>16&255,void 0===a)break;if(e[s++]=c>>8&255,void 0===u)break;e[s++]=255&c}return e.subarray(0,s)}[..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"].forEach(((t,e)=>n[t]=e)),n["+"]=n["-"]=62,n["/"]=n._=63;const s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function i(t){let e="",n=0,r=0;for(let i of t)for(n=(63&n)<<8|i,r+=8;r>=6;){r-=6;e+=s[n>>r&63]}if(r>0){e+=s[n<<6-r&63]}return e}const o=new TextEncoder,a=new TextDecoder("utf-8",{fatal:!0}),u=Symbol.for("IsPreservesBytes");class c{constructor(t=new Uint8Array){if(c.isBytes(t))this._view=t._view;else if(ArrayBuffer.isView(t))this._view=new Uint8Array(t.buffer,t.byteOffset,t.byteLength);else if(t instanceof ArrayBuffer)this._view=new Uint8Array(t);else if("string"==typeof t)this._view=o.encode(t);else if("number"==typeof t)this._view=new Uint8Array(t);else{if("number"!=typeof t.length)throw new TypeError("Attempt to initialize Bytes from unsupported value: "+t);this._view=Uint8Array.from(t)}}dataview(){return new DataView(this._view.buffer,this._view.byteOffset,this._view.byteLength)}get length(){return this._view.length}static from(t){return new c(t)}static of(...t){return new c(Uint8Array.of(...t))}static fromLatin1(t){const e=new c(t.length);for(let n=0;n<t.length;n++){const r=t.charCodeAt(n);if(r>=256)throw new Error("Codepoint out of range for 'latin1' byte encoding");e._view[n]=r}return e}static fromBase64(t){return new c(r(t))}static fromHex(t){if(1&t.length)throw new Error("Cannot decode odd-length hexadecimal string");const e=new c(t.length>>1);return c._raw_fromHexInto(t,e._view),e}static _raw_fromHexInto(t,e){const n=t.length>>1;for(let r=0;r<n;r++)e[r]=l(t.charCodeAt(r<<1))<<4|l(t.charCodeAt(1+(r<<1)))}static fromIO(t){return"string"==typeof t||c.isBytes(t)?t:new c(t)}static toIO(t){return"string"==typeof t?t:c.isBytes(t)?t._view:t}get(t){return this._view[t]}equals(t){if(!c.isBytes(t))return!1;if(t.length!==this.length)return!1;const e=this._view,n=t._view;for(let t=0;t<e.length;t++)if(e[t]!==n[t])return!1;return!0}hashCode(){const t=this._view;let e=0;for(let n=0;n<t.length;n++)e=31*e+t[n]|0;return e}static compare(t,e){return t<e?-1:e<t?1:0}static decodeUtf8(t){return a.decode(d(t))}fromUtf8(){return a.decode(this._view)}__as_preserve__(){return this}static __from_preserve__(t){return c.isBytes(t)?t:void 0}toLatin1(){return String.fromCharCode.apply(null,this._view)}toBase64(){return i(this._view)}toHex(t=h){var e=[];for(let n=0;n<this.length;n++)e.push(t(this._view[n]>>4)),e.push(t(15&this._view[n]));return e.join("")}valueOf(){return this.toHex()}__preserve_on__(e){e.state.emitbyte(t.ByteString),e.state.varint(this.length),e.state.emitbytes(this._view)}__preserve_text_on__(t){t.state.writeBytes(this._view)}get[u](){return!0}static isBytes(t){return!!(null==t?void 0:t[u])}}function h(t){return"0123456789abcdef"[t]}function l(t,e=Error){if(t>=48&&t<=57)return t-48;if(t>=97&&t<=102)return t-97+10;if(t>=65&&t<=70)return t-65+10;throw new e("Invalid hex digit: "+String.fromCharCode(t))}function d(t){return t instanceof Uint8Array?t:t._view}function f(t){return t instanceof DataView?t:t.dataview()}c.concat=function(t){let e=0;for(let n=0;n<t.length;n++)e+=d(t[n]).length;const n=new c(e);let r=0;for(let e=0;e<t.length;e++){const s=d(t[e]);n._view.set(s,r),r+=s.length}return n},function(){for(const t of"entries every find findIndex forEach includes indexOf join\nkeys lastIndexOf reduce reduceRight some toLocaleString values".split(/\s+/))c.prototype[t]=function(...e){return this._view[t](...e)};for(const t of"filter map slice subarray".split(/\s+/))c.prototype[t]=function(...e){return new c(this._view[t](...e))};for(const t of"reverse sort".split(/\s+/))c.prototype[t]=function(...e){return new c(this._view.slice()[t](...e))};c.prototype[Symbol.iterator]=function(){return this._view[Symbol.iterator]()}}();const p=Symbol.for("ErrorType");class y{static isCodecError(t,e){return(null==t?void 0:t[p])===e}}class m extends Error{get[p](){return"DecodeError"}constructor(t,e){super(t),this.pos=e}static isDecodeError(t){return y.isCodecError(t,"DecodeError")}}class b extends Error{get[p](){return"EncodeError"}static isEncodeError(t){return y.isCodecError(t,"EncodeError")}constructor(t,e){super(t),this.irritant=e}}class w extends m{get[p](){return"ShortPacket"}static isShortPacket(t){return y.isCodecError(t,"ShortPacket")}}function g(t,e){return e.label=t,e}!function(t){t.isRecord=function(t){return Array.isArray(t)&&"label"in t},t.constructorInfo=function(t){return{label:t.label,arity:t.length}},t.isClassOf=function(e,n){return t.isRecord(n)&&ct(e.label,n.label)&&e.arity===n.length},t.makeConstructor=function(){return(e,n)=>{const r=(...n)=>t(e,n),s={label:e,arity:n.length};return r.constructorInfo=s,r.isClassOf=e=>t.isClassOf(s,e),r.fieldNumbers={},r._={},n.forEach(((t,e)=>{r._[t]=t=>t[e],r.fieldNumbers[t]=e})),r}}}(g||(g={}));const v=Symbol.for("IsEmbedded");function _(t){return!!(null==t?void 0:t[v])}class x{get[v](){return!0}constructor(t){this.value=t}equals(t){return"object"==typeof t&&"value"in t&&Object.is(this.value,t.value)}}class S{get[v](){return!0}constructor(t){this.generic=t}equals(t,e){return"object"==typeof t&&"generic"in t&&e(this.generic,t.generic)}toString(){return this.generic.toString()}}function k(t){return"object"==typeof t&&null!==t&&"__preserve_on__"in t&&"function"==typeof t.__preserve_on__}function A(t){return String.fromCharCode.apply(null,t)}let E=0;const j=new WeakMap;function B(t){let e=j.get(t);return void 0===e&&(e=E++,j.set(t,e)),e}const I={encode(t,e){new C(t,this).push(B(e))}};class D{constructor(t){this.chunks=[],this.view=new DataView(new ArrayBuffer(256)),this.index=0,this.options=t}get canonical(){var t;return null===(t=this.options.canonical)||void 0===t||t}get includeAnnotations(){var t;return null!==(t=this.options.includeAnnotations)&&void 0!==t?t:!this.canonical}contents(){if(0===this.chunks.length){const t=this.index;return this.index=0,new c(this.view.buffer.slice(0,t))}{this.rotatebuffer(4096);const t=this.chunks;return this.chunks=[],c.concat(t)}}contentsString(){if(0===this.chunks.length){const t=A(new Uint8Array(this.view.buffer,0,this.index));return this.index=0,t}{this.rotatebuffer(4096);const t=this.chunks;return this.chunks=[],t.map(A).join("")}}rotatebuffer(t){this.chunks.push(new Uint8Array(this.view.buffer,0,this.index)),this.view=new DataView(new ArrayBuffer(t)),this.index=0}makeroom(t){this.index+t>this.view.byteLength&&this.rotatebuffer(t+4096)}emitbyte(t){this.makeroom(1),this.view.setUint8(this.index++,t)}emitbytes(t){this.makeroom(t.length),new Uint8Array(this.view.buffer).set(t,this.index),this.index+=t.length}claimbytes(t){this.makeroom(t);const e=new Uint8Array(this.view.buffer,this.index,t);return this.index+=t,e}varint(t){for(;t>=128;)this.emitbyte(t%128+128),t=Math.floor(t/128);this.emitbyte(t)}encodeint(e){if("bigint"==typeof e)return this.encodebigint(e);if(this.emitbyte(t.SignedInteger),0===e)return void this.emitbyte(0);const n=(-1===e?0:Math.floor(Math.log2(e>0?e:-(1+e)))+1)+1+7>>3;this.varint(n);const r=(t,e)=>{t>0&&(r(t-1,Math.floor(e/256)),this.emitbyte(255&e))};r(n,e)}encodebigint(e){let n;if(this.emitbyte(t.SignedInteger),e>0)n=e.toString(16),1&n.length?n="0"+n:l(n.charCodeAt(0))>=8&&(n="00"+n);else{if(!(e<0))return void this.emitbyte(0);{const t=(~e).toString(16);n="";for(let e=0;e<t.length;e++)n+="fedcba9876543210"[l(t.charCodeAt(e))];1&n.length?n="f"+n:l(n.charCodeAt(0))<8&&(n="ff"+n)}}this.varint(n.length>>1),c._raw_fromHexInto(n,this.claimbytes(n.length>>1))}encodebytes(t,e){this.emitbyte(t),this.varint(e.length),this.emitbytes(e)}}class C{constructor(t={},e){var n;t instanceof D?(this.state=t,this.embeddedEncode=null!=e?e:I):(this.state=new D(t),this.embeddedEncode=null!==(n=t.embeddedEncode)&&void 0!==n?n:I)}withEmbeddedEncode(t,e){return e(new C(this.state,t)),this}get canonical(){return this.state.canonical}get includeAnnotations(){return this.state.includeAnnotations}contents(){return this.state.contents()}contentsString(){return this.state.contentsString()}grouped(e,n){this.state.emitbyte(e),n(),this.state.emitbyte(t.End)}push(e){if(k(e))e.__preserve_on__(this);else if(k(e))e.__preserve_on__(this);else if("boolean"==typeof e)this.state.emitbyte(e?t.True:t.False);else if("number"==typeof e||"bigint"==typeof e)this.state.encodeint(e);else if("string"==typeof e)this.state.encodebytes(t.String,new c(e)._view);else if("symbol"==typeof e){const n=Symbol.keyFor(e);if(void 0===n)throw new b("Cannot preserve non-global Symbol",e);this.state.encodebytes(t.Symbol,new c(n)._view)}else if(ArrayBuffer.isView(e))if(e instanceof Uint8Array)this.state.encodebytes(t.ByteString,e);else{const n=new Uint8Array(e.buffer,e.byteOffset,e.byteLength);this.state.encodebytes(t.ByteString,n)}else if(g.isRecord(e)){this.state.emitbyte(t.Record),this.push(e.label);for(let t of e)this.push(t);this.state.emitbyte(t.End)}else!function(t){return"object"==typeof t&&null!==t&&"function"==typeof t[Symbol.iterator]}(e)?_(e)?(this.state.emitbyte(t.Embedded),this.embeddedEncode.encode(this.state,e)):tt(new Q(e),this,((t,e)=>e.push(t)),((t,e)=>e.push(t))):this.grouped(t.Sequence,(()=>{for(let t of e)this.push(t)}));return this}}function O(t,e={}){return new C(e).push(t).contents()}const M=new C({canonical:!0});let q=!1;function T(t,e){if(void 0!==e||q)return O(t,Object.assign(Object.assign({},e),{canonical:!0}));{q=!0;const e=M.push(t).contents();return q=!1,e}}function U(t){if(q)return new C({canonical:!0}).push(t).contentsString();{q=!0;const e=M.push(t).contentsString();return q=!1,e}}function V(t,e={}){return O(t,Object.assign(Object.assign({},e),{includeAnnotations:!0}))}const N=Map,F=Set,z=Symbol.for("IsMap"),P=Symbol.for("IsSet");function R(t,e){const n=t=>t.done?{done:!0,value:null}:{done:!1,value:e(t.value)};return{next:e=>n(t.next(e)),return:e=>{var r,s;return n(null!==(s=null===(r=t.return)||void 0===r?void 0:r.call(t,e))&&void 0!==s?s:{done:!0,value:null})},throw:e=>{var r,s;return n(null!==(s=null===(r=t.throw)||void 0===r?void 0:r.call(t,e))&&void 0!==s?s:{done:!0,value:null})},[Symbol.iterator](){return this}}}z in Map.prototype||(Object.defineProperty(Map.prototype,z,{get:()=>!0}),Map.isMap=t=>!!(null==t?void 0:t[z])),P in Set.prototype||(Object.defineProperty(Set.prototype,P,{get:()=>!0}),Set.isSet=t=>!!(null==t?void 0:t[P]));class H{constructor(t,e){this.canonicalizer=t,this.items=void 0===e?new Map:new Map(R(e[Symbol.iterator](),(([t,e])=>[this._key(t),[t,e]])))}_key(t){return this.canonicalizer(t)}get(t,e){const n=this.items.get(this._key(t));return void 0===n?e:n[1]}getOrSet(t,e){const n=this._key(t);let r=this.items.get(n);return void 0===r&&(r=[t,e()],this.items.set(n,r)),r[1]}set(t,e){return this.items.set(this._key(t),[t,e]),this}forEach(t,e){this.items.forEach((([n,r])=>t.call(e,r,n,this)))}entries(){return this.items.values()}keys(){return R(this.items.values(),(([t,e])=>t))}values(){return R(this.items.values(),(([t,e])=>e))}delete(t){return this.items.delete(this._key(t))}getAndDelete(t,e){const n=this._key(t),r=this.items.get(n);return void 0===r?e:(this.items.delete(n),r[1])}clear(){this.items.clear()}has(t){return this.items.has(this._key(t))}get size(){return this.items.size}[Symbol.iterator](){return this.items.values()}get[Symbol.toStringTag](){return"FlexMap"}equals(t,e=((t,e)=>t===e)){if(!("size"in t&&"has"in t&&"get"in t))return!1;if(this.size!==t.size)return!1;for(let[n,r]of this.items.values()){if(!t.has(n))return!1;if(!e(r,t.get(n)))return!1}return!0}update(t,e,n,r=((t,e)=>t===e)){const s=this._key(t);if(this.items.has(s)){const n=this.items.get(s)[1],i=e(n);return void 0===i?(this.items.delete(s),-1):(r(i,n)||this.items.set(s,[t,i]),0)}{const r=e(n);return void 0===r?0:(this.items.set(s,[t,r]),1)}}canonicalKeys(){return this.items.keys()}get[z](){return!0}}class W{constructor(t,e){this.canonicalizer=t,this.items=void 0===e?new Map:new Map(R(e[Symbol.iterator](),(t=>[this._key(t),t])))}_key(t){return this.canonicalizer(t)}has(t){return this.items.has(this._key(t))}get(t){const e=this._key(t);return this.items.has(e)?{item:this.items.get(e)}:null}add(t){return this.items.set(this._key(t),t),this}forEach(t,e){this.items.forEach((n=>t.call(e,n,n,this)))}entries(){return R(this.items.values(),(t=>[t,t]))}keys(){return this.items.values()}values(){return this.items.values()}delete(t){return this.items.delete(this._key(t))}clear(){this.items.clear()}get size(){return this.items.size}[Symbol.iterator](){return this.items.values()}get[Symbol.toStringTag](){return"FlexSet"}equals(t){if(!("size"in t)||!("has"in t))return!1;if(this.size!==t.size)return!1;for(let e of this.items.values())if(!t.has(e))return!1;return!0}canonicalValues(){return this.items.keys()}union(t){const e=new W(this.canonicalizer,this);for(let n of t)e.add(n);return e}intersect(t){const e=new W(this.canonicalizer);for(let n of this)t.has(n)&&e.add(n);return e}subtract(t){const e=new W(this.canonicalizer);for(let n of this)t.has(n)||e.add(n);return e}get[P](){return!0}}var L;function $(t){return ft.isAnnotated(t)?t.item:t}function K(t){return J(t,1)}function J(t,e=1/0){return function t(e,n){if(0===n)return e;if(!ft.isAnnotated(e))return e;const r=n-1;function s(e){return t(e,r)}if(g.isRecord(e.item))return g(t(e.item.label,n),e.item.map(s));if(ft.isAnnotated(e.item))throw new Error("Improper annotation structure");if(0===r)return e.item;if(Array.isArray(e.item))return e.item.map(s);if(st.isSet(e.item))return e.item.map(s);if(Y.isDictionary(e.item)){const t=new Q;return new Q(e.item).forEach(((e,n)=>t.set(s(n),s(e)))),t.simplifiedValue()}return e.item}(t,e)}!function(t){function e(t,e){return t[e.description]}function n(t,e){return Object.hasOwnProperty.call(t,e.description)}function r(t){return Object.keys(t).length}function s(t){return R(Object.entries(t).values(),(([t,e])=>[Symbol.for(t),e]))}t.isJsDictionary=function(t){return"object"==typeof t&&null!==t&&null===Object.getPrototypeOf(Object.getPrototypeOf(t))&&"function"!=typeof t.__as_preserve__},t.from=function(t){const e={};for(const[n,r]of t)e[n.description]=r;return e},t.clear=function(t){for(const e in t)delete t[e]},t.remove=function(t,e){const r=n(t,e);return delete t[e.description],r},t.forEach=function(t,e){Object.entries(t).forEach((([t,n])=>e(n,Symbol.for(t))))},t.get=e,t.has=n,t.set=function(t,e,n){return t[e.description]=n,t},t.size=r,t.entries=s,t.keys=function(t){return R(Object.keys(t).values(),(t=>Symbol.for(t)))},t.values=function(t){return Object.values(t).values()},t.clone=function(t){const e={};return Object.keys(t).forEach((n=>e[n]=t[n])),e},t.equals=function(t,i,o=((t,e)=>t===e)){if(r(t)!==r(i))return!1;for(let[r,a]of s(t)){if(!n(i,r))return!1;if(!o(a,e(i,r)))return!1}return!0}}(L||(L={}));const G=Symbol.for("DictionaryType");class X extends H{constructor(t,e,n){super((e=>U(t(e))),n),this.encodeK=t,this.encodeV=e}__preserve_on__(t){tt(this,t,((t,e)=>e.push(this.encodeK(t))),((t,e)=>e.push(this.encodeV(t))))}__preserve_text_on__(t){et(this,t,((t,e)=>e.push(this.encodeK(t))),((t,e)=>e.push(this.encodeV(t))))}}class Z extends X{get[G](){return"Dictionary"}static isKeyedDictionary(t){return"Dictionary"===(null==t?void 0:t[G])}constructor(t){super((t=>t),(t=>t),t)}clone(){return new Z(this)}get[Symbol.toStringTag](){return"Dictionary"}equals(t,e=((t,e)=>t===e)){const n=Y.asMap(t);return!!n&&super.equals(n,e)}}class Q{get[z](){return!0}constructor(t){void 0===t?(this.j={},this.k=void 0):G in t?(this.j=void 0,this.k=t):(this.j=t,this.k=void 0)}static from(t){const e=new Q;for(const[n,r]of t)e.set(n,r);return e}clear(){this.j?L.clear(this.j):this.k.clear()}delete(t){return this.j?"symbol"==typeof(t=$(t))&&L.remove(this.j,t):this.k.delete(t)}forEach(t,e){this.j?L.forEach(this.j,((n,r)=>t.call(e,n,r,this))):this.k.forEach(t,e)}get(t){if(this.j){if("symbol"!=typeof(t=$(t)))return;return L.get(this.j,t)}return this.k.get(t)}has(t){return this.j?"symbol"==typeof(t=$(t))&&L.has(this.j,t):this.k.has(t)}set(t,e){if(this.j){if("symbol"==typeof t)return L.set(this.j,t,e),this;this.k=new Z(L.entries(this.j)),this.j=void 0}return this.k.set(t,e),this}get size(){return this.j?L.size(this.j):this.k.size}entries(){return this.j?L.entries(this.j):this.k.entries()}keys(){return this.j?L.keys(this.j):this.k.keys()}values(){return this.j?L.values(this.j):this.k.values()}[Symbol.iterator](){return this.entries()}get[Symbol.toStringTag](){return"DictionaryMap"}clone(){return new Q(this.j?L.clone(this.j):this.k.clone())}get value(){var t;return null!==(t=this.j)&&void 0!==t?t:this.k}simplify(){if(!this.j){const t={};for(const[e,n]of this.k.entries()){if("symbol"!=typeof e)return;t[e.description]=n}this.j=t,this.k=void 0}}simplifiedValue(){return this.simplify(),this.value}asJsDictionary(){if(this.simplify(),!this.j)throw new Error("Cannot represent general dictionary as JsDictionary");return this.j}asKeyedDictionary(){var t;return null!==(t=this.k)&&void 0!==t?t:new Z(L.entries(this.j))}}var Y;function tt(e,n,r,s){if(n.canonical){const i=Array.from(e),o=new C({canonical:!0,embeddedEncode:n.embeddedEncode}),a=i.map((([t,e],n)=>(r(t,o),[o.contents(),n])));a.sort(((t,e)=>c.compare(t[0],e[0]))),n.grouped(t.Dictionary,(()=>a.forEach((([t,e])=>{const[o,a]=i[e];r(o,n),s(a,n)}))))}else n.grouped(t.Dictionary,(()=>e.forEach(((t,e)=>{r(e,n),s(t,n)}))))}function et(t,e,n,r){e.state.writeSeq("{","}",t.entries(),(([t,s])=>{n(t,e),ft.isAnnotated(s)&&yt(s).length>1&&e.state.isIndenting?(e.state.pieces.push(":"),e.state.indentCount++,e.state.writeIndent(),r(s,e),e.state.indentCount--):(e.state.pieces.push(": "),r(s,e))}))}!function(t){function e(t){if("object"!=typeof t||null===t)return!1;switch(t[G]){case"Dictionary":return!0;case void 0:return L.isJsDictionary(t);default:return!1}}t.isDictionary=e,t.asMap=function(t){return e(t)?new Q(t):void 0},t.from=function(t){return Q.from(t).simplifiedValue()},t.__from_preserve__=function(e){return t.isDictionary(e)?e:void 0}}(Y||(Y={}));class nt extends W{constructor(t,e){super((e=>U(t(e))),e),this.encodeV=t}__preserve_on__(t){it(this,t,((t,e)=>e.push(this.encodeV(t))))}__preserve_text_on__(t){ot(this,t,((t,e)=>e.push(this.encodeV(t))))}}class rt extends nt{get[G](){return"Set"}static isKeyedSet(t){return"Set"===(null==t?void 0:t[G])}constructor(t){super((t=>t),t)}map(t){return new rt(R(this[Symbol.iterator](),t))}filter(t){const e=new rt;for(let n of this)t(n)&&e.add(n);return e}clone(){return new rt(this)}get[Symbol.toStringTag](){return"Set"}}let st=class t extends rt{static isSet(t){return"Set"===(null==t?void 0:t[G])}static __from_preserve__(e){return t.isSet(e)?e:void 0}};function it(e,n,r){if(n.canonical){const s=new C({canonical:!0,embeddedEncode:n.embeddedEncode}),i=Array.from(e).map((t=>(r(t,s),[s.contents(),t])));i.sort(((t,e)=>c.compare(t[0],e[0]))),n.grouped(t.Set,(()=>i.forEach((([t,e])=>r(e,n)))))}else n.grouped(t.Set,(()=>e.forEach((t=>r(t,n)))))}function ot(t,e,n){e.state.writeSeq("#{","}",t,(t=>n(t,e)))}const at=Symbol.for("IsPreservesAnnotated");function ut(t){return!!(null==t?void 0:t[at])}function ct(t,e){if(ut(t)&&(t=t.item),ut(e)&&(e=e.item),Object.is(t,e))return!0;if(typeof t!=typeof e)return("number"==typeof t&&"bigint"==typeof e||"bigint"==typeof t&&"number"==typeof e)&&t==e;if("object"==typeof t){if(null===t||null===e)return!1;if("equals"in t&&"function"==typeof t.equals)return t.equals(e,ct);if(Array.isArray(t)&&Array.isArray(e)){const n="label"in t;if(n!=="label"in e)return!1;if(n&&!ct(t.label,e.label))return!1;if(t.length!==e.length)return!1;for(let n=0;n<t.length;n++)if(!ct(t[n],e[n]))return!1;return!0}{const n=Y.asMap(t),r=Y.asMap(e);if(!n||!r)return!1;if(n.size!==r.size)return!1;for(const t of n.keys()){if(!r.has(t))return!1;if(!ct(n.get(t),r.get(t)))return!1}return!0}}return!1}function ht(t){return{line:1,column:0,pos:0,name:t}}function lt(t,e){if(t.pos++,void 0===t.line)return!1;{let n=!1;switch(e){case"\t":t.column=t.column+8&-8;break;case"\n":t.column=0,t.line++,n=!0;break;case"\r":t.column=0;break;default:t.column++}return n}}function dt(t){var e,n,r;return null===t?"<unknown>":"string"==typeof t?t:`${null!==(e=t.name)&&void 0!==e?e:""}:${null!==(n=t.line)&&void 0!==n?n:""}:${null!==(r=t.column)&&void 0!==r?r:""}:${t.pos}`}class ft{constructor(t,e){this.annotations=[],this.pos=null!=e?e:null,this.item=t}__as_preserve__(){return this}static __from_preserve__(t){return ut(t)?t:void 0}__preserve_on__(e){if(e.includeAnnotations)for(const n of this.annotations)e.state.emitbyte(t.Annotation),e.push(n);e.push(this.item)}__preserve_text_on__(t){if(t.includeAnnotations){const e=this.annotations.length<=1;for(const n of this.annotations)t.state.pieces.push("@"),t.push(n),e?t.state.pieces.push(" "):t.state.writeIndentSpace()}t.push(this.item)}equals(t){return ct(this.item,ft.isAnnotated(t)?t.item:t)}get[at](){return!0}static isAnnotated(t){return ut(t)}}function pt(t,...e){const n=ft.isAnnotated(t)?t:new ft(t);return e.forEach((t=>n.annotations.push(t))),n}function yt(t){return ft.isAnnotated(t)?t.annotations:[]}function mt(t){return ft.isAnnotated(t)?t.pos:null}function bt(t){return Array.isArray(t)||st.isSet(t)||Y.isDictionary(t)}function wt(t){return Array.isArray(t)&&!("label"in t)}const gt=Symbol.for("FloatType");class vt{constructor(t){this.value="number"==typeof t?t:t.value}__preserve_text_on__(t){t.state.pieces.push(this.toString())}equals(t){return!!Object.is(t.constructor,this.constructor)&&(Number.isNaN(this.value)&&Number.isNaN(t.value)?t.toBytes().equals(this.toBytes()):Object.is(t.value,this.value))}hashCode(){return 0|this.value}}function _t(t){return"number"==typeof t?t:vt.isFloat(t)?t.value:NaN}function xt(t){if(Object.is(t,-0))return"-0.0";const e=""+t;return e.includes(".")||e.includes("e")||e.includes("E")?e:e+".0"}vt.isFloat=t=>void 0!==(null==t?void 0:t[gt]),vt.isDouble=t=>"Double"===(null==t?void 0:t[gt]);class St extends vt{__as_preserve__(){return this}static fromBytes(t){return new St(f(t).getFloat64(0,!1))}static __from_preserve__(t){return vt.isDouble(t)?t:void 0}__preserve_on__(e){e.state.emitbyte(t.Ieee754),e.state.emitbyte(8),e.state.makeroom(8),e.state.view.setFloat64(e.state.index,this.value,!1),e.state.index+=8}toBytes(){const t=new c(8);return t.dataview().setFloat64(0,this.value,!1),t}toString(){return Number.isFinite(this.value)?xt(this.value):'#xd"'+this.toBytes().toHex()+'"'}get[gt](){return"Double"}}function kt(t){return new St(t)}const At=BigInt(Number.MAX_SAFE_INTEGER),Et=BigInt(Number.MIN_SAFE_INTEGER),jt=/^([-+]?\d+)((\.\d+([eE][-+]?\d+)?)|([eE][-+]?\d+))?$/;class Bt{constructor(t,e){switch(this.discarded=0,this.buffer=t,typeof e.name){case"undefined":this.pos=ht();break;case"string":this.pos=ht(e.name);break;case"object":this.pos=Object.assign({},e.name)}this.index=0,this.options=e}error(t,e){throw new m(t,Object.assign({},e))}get includeAnnotations(){var t;return null!==(t=this.options.includeAnnotations)&&void 0!==t&&t}copyPos(){return Object.assign({},this.pos)}write(t){this.atEnd()?this.buffer=t:this.buffer=this.buffer.substring(this.index)+t,this.discarded+=this.index,this.index=0}atEnd(){return this.index>=this.buffer.length}peek(){if(this.atEnd())throw new w("Short term",this.pos);return this.buffer[this.index]}advance(){const t=this.index++;return lt(this.pos,this.buffer[t]),t}nextchar(){if(this.atEnd())throw new w("Short term",this.pos);return this.buffer[this.advance()]}nextcharcode(){if(this.atEnd())throw new w("Short term",this.pos);return this.buffer.charCodeAt(this.advance())}skipws(t=!1){for(;!this.atEnd();){const e=this.peek();if(!(Ct(e)||t&&","===e))break;this.advance()}}readHex2(){return l(this.nextcharcode(),m)<<4|l(this.nextcharcode(),m)}readHex4(){return l(this.nextcharcode(),m)<<12|l(this.nextcharcode(),m)<<8|l(this.nextcharcode(),m)<<4|l(this.nextcharcode(),m)}readHexBinary(){const t=[];for(;;){if(this.skipws(),'"'===this.peek())return this.advance(),c.from(t);t.push(this.readHex2())}}readHexFloat(){const t=this.copyPos();'"'!==this.nextchar()&&this.error("Missing open-double-quote in hex-encoded floating-point number",t);const e=this.readHexBinary();return 8!==e.length&&this.error("Incorrect number of bytes in hex-encoded Double",t),St.fromBytes(e)}readBase64Binary(){let t="";for(;;){this.skipws();const e=this.nextchar();if("]"===e)break;t+=e}return c.fromBase64(t)}requireDelimiter(t){this.delimiterFollows()||this.error(`Delimiter must follow ${t}`,this.pos)}delimiterFollows(){if(this.atEnd())return!0;const t=this.peek();return-1!=='(){}[]<>";,@#:|'.indexOf(t)||Ct(t)}readRawSymbolOrNumber(t){for(;!this.delimiterFollows();)t+=this.nextchar();const e=jt.exec(t);if(e){if(void 0===e[2]){let t=BigInt(e[1]);return t<=Et||t>=At?t:Number(t)}return kt(parseFloat(t))}return Symbol.for(t)}readStringlike(t,e,n,r,s){let i=[];for(;;){const o=this.nextchar();switch(o){case n:return e(i);case"\\":{const e=this.nextchar();switch(e){case r:i.push(s());break;case n:case"\\":case"/":i.push(t(e));break;case"b":i.push(t("\b"));break;case"f":i.push(t("\f"));break;case"n":i.push(t("\n"));break;case"r":i.push(t("\r"));break;case"t":i.push(t("\t"));break;default:this.error(`Invalid escape code \\${e}`,this.pos)}break}default:i.push(t(o))}}}readString(t){return this.readStringlike((t=>t),(t=>t.join("")),t,"u",(()=>{const t=this.readHex4();if(t>=55296&&t<=57343){if("\\"===this.nextchar()&&"u"===this.nextchar()){const e=this.readHex4();if(e>=56320&&e<=57343&&t<=56319)return String.fromCharCode(t,e)}this.error("Invalid surrogate pair",this.pos)}return String.fromCharCode(t)}))}readLiteralBinary(){return this.readStringlike((t=>{const e=t.charCodeAt(0);return e>=256&&this.error(`Invalid code point ${e} in literal binary`,this.pos),e}),c.from,'"',"x",(()=>this.readHex2()))}}const It={decode(t){return new S(new $t(t,this).next())},fromValue:(t,e)=>new S(e.includeAnnotations?t:J(t))};class Dt{constructor(t="",e){var n,r;if(t instanceof Bt)this.state=t,this.embeddedType=e;else{const s=null!==(n=e)&&void 0!==n?n:{};this.state=new Bt(t,s),this.embeddedType=null!==(r=s.embeddedDecode)&&void 0!==r?r:Lt}}write(t){this.state.write(t)}readCommentLine(){const t=this.state.copyPos();let e="";for(;;){const n=this.state.nextchar();if("\n"===n||"\r"===n)return this.wrap(e,t);e+=n}}wrap(t,e){return this.state.includeAnnotations&&!ft.isAnnotated(t)&&(t=new ft(t,e)),t}annotateNextWith(t){if(this.state.skipws(),this.state.atEnd())throw new m("Trailing annotations and comments are not permitted",this.state.pos);const e=this.next();return this.state.includeAnnotations&&e.annotations.unshift(t),e}readToEnd(){const t=[];for(;;){if(this.state.skipws(),this.state.atEnd())return t;t.push(this.next())}}next(){this.state.skipws();const t=this.state.copyPos(),e=(()=>{const e=this.state.nextchar();switch(e){case'"':return this.state.readString('"');case"|":return Symbol.for(this.state.readString("|"));case";":this.state.error("Semicolon is reserved syntax",t);case"@":return this.annotateNextWith(this.next());case":":this.state.error("Unexpected key/value separator between items",t);case"#":{const e=this.state.nextchar();switch(e){case" ":case"\t":return this.annotateNextWith(this.readCommentLine());case"\n":case"\r":return this.annotateNextWith("");case"!":return this.annotateNextWith(g(Symbol.for("interpreter"),[this.readCommentLine()]));case"f":return this.state.requireDelimiter("#f"),!1;case"t":return this.state.requireDelimiter("#t"),!0;case"{":return this.readSet();case'"':return this.state.readLiteralBinary();case"x":switch(this.state.nextchar()){case'"':return this.state.readHexBinary();case"d":return this.state.readHexFloat();default:this.state.error("Invalid #x syntax",t)}case"[":return this.state.readBase64Binary();case":":return this.embeddedType.fromValue(new Dt(this.state,It).next(),this.state.options);default:this.state.error(`Invalid # syntax: ${e}`,t)}}case"<":return g(this.next(),this.readSequence(">",!1));case"[":return this.readSequence("]",!0);case"{":return this.readDictionary();case">":this.state.error("Unexpected >",t);case"]":this.state.error("Unexpected ]",t);case"}":this.state.error("Unexpected }",t);case",":this.state.error("Unexpected ,",t);default:return this.state.readRawSymbolOrNumber(e)}})();return this.wrap(e,t)}seq(t,e,n,r){for(;;){if(this.state.skipws(t),this.state.peek()===r)return this.state.advance(),e;n(this.next(),e)}}readSequence(t,e){return this.seq(e,[],((t,e)=>e.push(t)),t)}readDictionary(){return this.seq(!0,new Q,((t,e)=>{if(this.state.skipws(),":"===this.state.peek())this.state.advance(),e.has(t)&&this.state.error(`Duplicate key: ${Pt(t)}`,this.state.pos),e.set(t,this.next());else this.state.error("Missing key/value separator",this.state.pos)}),"}").simplifiedValue()}readSet(){return this.seq(!0,new st,((t,e)=>{e.has(t)&&this.state.error(`Duplicate value in set: ${Pt(t)}`,this.state.pos),e.add(t)}),"}")}}function Ct(t){return-1!==" \t\n\r".indexOf(t)}function Ot(t){return"object"==typeof t&&null!==t&&"__preserve_text_on__"in t&&"function"==typeof t.__preserve_text_on__}const Mt={encode(t,e){new C(t,this).push(e.generic)},toValue:t=>t.generic},qt={encode(t,e){throw new Error("Embeddeds not permitted encoding Preserves document")},toValue(t){throw new Error("Embeddeds not permitted writing Preserves document")}};class Tt{constructor(t){var e;this.pieces=[],this.indentCount=0,this.options=t,this.indentDelta=" ".repeat(null!==(e=t.indent)&&void 0!==e?e:0)}get isIndenting(){return this.indentDelta.length>0}get includeAnnotations(){var t;return null===(t=this.options.includeAnnotations)||void 0===t||t}writeIndent(){if(this.isIndenting){this.pieces.push("\n");for(let t=0;t<this.indentCount;t++)this.pieces.push(this.indentDelta)}}writeIndentSpace(){this.isIndenting?this.writeIndent():this.pieces.push(" ")}escapeStringlikeChar(t,e=(t=>t)){switch(t){case"\\":return"\\\\";case"\b":return"\\b";case"\f":return"\\f";case"\n":return"\\n";case"\r":return"\\r";case"\t":return"\\t";default:return e(t)}}escapeStringlike(t,e){let n=e;for(let r of t)n+=r===e?"\\"+e:this.escapeStringlikeChar(r);return n+e}writeSeq(t,e,n,r){let s=n[Symbol.iterator]();this.pieces.push(t);const i=s.next();if(!0!==i.done){const t=i.value,e=s.next();if(!0===e.done)r(t);else{let n;for(this.indentCount++,this.writeIndent(),r(t),this.writeIndentSpace(),r(e.value);!0!==(n=s.next()).done;)this.writeIndentSpace(),r(n.value);this.indentCount--,this.writeIndent()}}this.pieces.push(e)}writeBytes(t){var e,n;const r=null!==(e=this.options.maxBinaryAsciiLength)&&void 0!==e?e:1024,s=null!==(n=this.options.maxBinaryAsciiProportion)&&void 0!==n?n:.75;if(t.length>=r)this.writeBase64(t);else{let e=0,n=Math.min(t.length,r);for(let r=0;r<n;r++){const n=t[r];switch(n){case 9:case 10:case 13:e++;break;default:n>=32&&n<=126&&e++}}0===n||e/n>=s?this.writeBinaryStringlike(t):this.writeBase64(t)}}writeBase64(t){this.pieces.push("#[",i(t),"]")}writeBinaryStringlike(t){let e='#"';for(let n of t)e+=34===n?'\\"':this.escapeStringlikeChar(String.fromCharCode(n),(t=>n>=32&&n<=126&&92!==n?t:"\\x"+("0"+n.toString(16)).slice(-2)));this.pieces.push(e+'"')}couldBeFlat(t){let e=!1;for(let n of t){if(Array.isArray(n)||Set.isSet(n)||Map.isMap(n)){if(e)return!1;e=!0}if(this.includeAnnotations&&ut(n)&&n.annotations.length>1)return!1}return!0}}class Ut{constructor(t={},e){var n;t instanceof Tt?(this.state=t,this.embeddedWrite=e):(this.state=new Tt(t),this.embeddedWrite=null!==(n=t.embeddedWrite)&&void 0!==n?n:qt)}static stringify(t,e){const n=new Ut(e);return n.push(t),n.contents()}contents(){return this.state.pieces.join("")}get includeAnnotations(){return this.state.includeAnnotations}push(t){switch(typeof t){case"boolean":this.state.pieces.push(t?"#t":"#f");break;case"string":this.state.pieces.push(this.state.escapeStringlike(t,'"'));break;case"symbol":{const e=t.description;/^[-a-zA-Z0-9~!$%^&*?_=+/.]+$/.exec(e)&&!jt.exec(e)?this.state.pieces.push(e):this.state.pieces.push(this.state.escapeStringlike(e,"|"));break}case"bigint":case"number":this.state.pieces.push(""+t);break;case"object":if(null===t)throw new Error("Cannot encode null in Preserves Writer.push");if(Ot(t))t.__preserve_text_on__(this);else if(Ot(t))t.__preserve_text_on__(this);else if(ArrayBuffer.isView(t))if(t instanceof Uint8Array)this.state.writeBytes(t);else{const e=new Uint8Array(t.buffer,t.byteOffset,t.byteLength);this.state.writeBytes(e)}else if(g.isRecord(t)){const e=this.state.couldBeFlat(t);this.state.pieces.push("<"),this.push(t.label),e||this.state.indentCount++;for(let n of t)e?this.state.pieces.push(" "):this.state.writeIndentSpace(),this.push(n);e||this.state.indentCount--,this.state.pieces.push(">")}else!function(t){return"object"==typeof t&&null!==t&&"function"==typeof t[Symbol.iterator]}(t)?_(t)?(this.state.pieces.push("#:"),"write"in this.embeddedWrite?this.embeddedWrite.write(this.state,t):new Ut(this.state,Mt).push(this.embeddedWrite.toValue(t))):et(new Q(t),this,((t,e)=>e.push(t)),((t,e)=>e.push(t))):this.state.writeSeq("[","]",t,(t=>this.push(t)));break;default:(e=>{throw new Error(`Internal error: unhandled in Preserves Writer.push for ${t}`)})()}return this}}function Vt(t){switch(typeof t){case"number":if(!Number.isInteger(t))throw new TypeError("Refusing to autoconvert non-integer number to Double");case"bigint":case"string":case"symbol":case"boolean":return t;case"undefined":case"function":break;case"object":if(null===t)break;if("function"==typeof t.__as_preserve__)return t.__as_preserve__();if(g.isRecord(t))return t;if(Array.isArray(t))return t.map(Vt);if(ArrayBuffer.isView(t)||t instanceof ArrayBuffer)return c.from(t);if(Map.isMap(t)){const e=new Z;return t.forEach(((t,n)=>e.set(Vt(n),Vt(t)))),e}if(st.isSet(t)){const e=new st;return t.forEach((t=>e.add(Vt(t)))),e}if(_(t))return t;{const e={};return Object.entries(t).forEach((([t,n])=>e[t]=Vt(n))),e}}throw new TypeError("Cannot represent JavaScript value as Preserves: "+t)}function Nt(t,e){return new Dt(t,e).next()}function Ft(t,e){return new Dt(t,e).readToEnd()}Y.stringMap=function(t){const e=new Z;return Object.entries(t).forEach((([t,n])=>e.set(t,Vt(n)))),e};const zt={write(t,e){if(e instanceof S)new Ut(t,this).push(e.generic);else{try{const n=Vt(e);if(!_(n))return void new Ut(t,this).push(n)}catch(t){}try{return void t.pieces.push(JSON.stringify(e))}catch(t){}t.pieces.push("⌜"+e+"⌝")}}};function Pt(t,e){var n;return(e=Object.assign({},null!=e?e:{})).embeddedWrite=null!==(n=e.embeddedWrite)&&void 0!==n?n:zt,Ut.stringify(Vt(t),e)}function Rt(t,...e){const n=[t[0]];return e.forEach(((e,r)=>{n.push(Pt(e)),n.push(t[r+1])})),n.join("")}function Ht(t,e){return ct(t,e)?e:void 0}[ft,c,Z,rt].forEach((t=>{t.prototype.toString=function(){return Pt(this)}}));class Wt{constructor(t,e){this.index=0,this.packet=d(t),this.options=e}get includeAnnotations(){var t;return null!==(t=this.options.includeAnnotations)&&void 0!==t&&t}write(t){this.index===this.packet.length?this.packet=d(t):this.packet=c.concat([this.packet.slice(this.index),t])._view,this.index=0}atEnd(){return this.index>=this.packet.length}mark(){return this.index}restoreMark(t){this.index=t}shortGuard(t,e){if(this.atEnd())return e();const n=this.mark();try{return t()}catch(t){if(w.isShortPacket(t))return this.restoreMark(n),e();throw t}}nextbyte(){if(this.atEnd())throw new w("Short packet");return this.packet[this.index++]}nextbytes(t){const e=this.index;if(this.index+=t,this.index>this.packet.length)throw new w("Short packet");return new DataView(this.packet.buffer,this.packet.byteOffset+e,t)}varint(){const t=this.nextbyte();return t<128?t:(this.varint()<<7)+(t-128)}peekend(){return this.nextbyte()===t.End||(this.index--,!1)}nextint(t){const e=this.index;if(0===t)return 0;if(t>7)return this.nextbigint(t);if(7===t){const e=this.packet[this.index];if(e>=32&&e<224)return this.nextbigint(t)}let n=this.nextbyte();128&n&&(n-=256);for(let e=1;e<t;e++)n=256*n+this.nextbyte();return Number.isSafeInteger(n)?n:(this.index=e,this.nextbigint(t))}nextbigint(t){if(0===t)return BigInt(0);const e=c.from(this.nextbytes(t));if(e.get(0)>=128){const t=e.toHex((t=>h(15-t)));return~BigInt("0x"+t)}{const t=e.toHex();return BigInt("0x"+t)}}wrap(t){return this.includeAnnotations?new ft(t):t}unshiftAnnotation(t,e){return this.includeAnnotations&&e.annotations.unshift(t),e}}const Lt={decode(t){throw new Error("Embeddeds not permitted at this point in Preserves document")},fromValue(t,e){throw new Error("Embeddeds not permitted at this point in Preserves document")}};class $t{constructor(t=new Uint8Array(0),e){var n,r,s;if(t instanceof Wt)this.state=t,this.embeddedDecode=null!==(n=e)&&void 0!==n?n:Lt;else{const n=null!==(r=e)&&void 0!==r?r:{};this.state=new Wt(t,n),this.embeddedDecode=null!==(s=n.embeddedDecode)&&void 0!==s?s:Lt}}write(t){this.state.write(t)}nextvalues(){const t=[];for(;!this.state.peekend();)t.push(this.next());return t}static dictionaryFromArray(t){const e=new Q;if(t.length%2)throw new m("Missing dictionary value");for(let n=0;n<t.length;n+=2){if(e.has(t[n]))throw new m(`Duplicate key: ${Pt(t[n])}`);e.set(t[n],t[n+1])}return e.simplifiedValue()}next(){const e=this.state.nextbyte();switch(e){case t.False:return this.state.wrap(!1);case t.True:return this.state.wrap(!0);case t.End:throw new m("Unexpected Compound end marker");case t.Annotation:{const t=this.next(),e=this.next();return this.state.unshiftAnnotation(t,e)}case t.Embedded:return this.state.wrap(this.embeddedDecode.decode(this.state));case t.Ieee754:if(8===this.state.varint())return this.state.wrap(St.fromBytes(this.state.nextbytes(8)));throw new m("Invalid IEEE754 size");case t.SignedInteger:return this.state.wrap(this.state.nextint(this.state.varint()));case t.String:return this.state.wrap(c.from(this.state.nextbytes(this.state.varint())).fromUtf8());case t.ByteString:return this.state.wrap(c.from(this.state.nextbytes(this.state.varint())));case t.Symbol:return this.state.wrap(Symbol.for(c.from(this.state.nextbytes(this.state.varint())).fromUtf8()));case t.Record:{const t=this.nextvalues();if(0===t.length)throw new m("Too few elements in encoded record");return this.state.wrap(g(t[0],t.slice(1)))}case t.Sequence:return this.state.wrap(this.nextvalues());case t.Set:{const t=new st;for(const e of this.nextvalues()){if(t.has(e))throw new m(`Duplicate value: ${Pt(e)}`);t.add(e)}return this.state.wrap(t)}case t.Dictionary:return this.state.wrap($t.dictionaryFromArray(this.nextvalues()));default:throw new m("Unsupported Preserves tag: "+e)}}try_next(){return this.state.shortGuard((()=>this.next()),(()=>{}))}atEnd(){return this.state.atEnd()}mark(){return this.state.mark()}restoreMark(t){return this.state.restoreMark(t)}skip(){this.next()}withEmbeddedDecode(t,e){return e(new $t(this.state,t))}skipAnnotations(e){const n=this.mark();for(;!this.state.atEnd()&&this.state.packet[this.state.index]===t.Annotation;)this.state.index++,this.skip();return e((()=>this.restoreMark(n)))}nextBoolean(){return this.skipAnnotations((e=>{switch(this.state.nextbyte()){case t.False:return!1;case t.True:return!0;default:return e()}}))}nextDouble(){return this.skipAnnotations((e=>this.state.nextbyte()!==t.Ieee754||8!==this.state.nextbyte()?e():St.fromBytes(this.state.nextbytes(8))))}nextEmbedded(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Embedded?this.embeddedDecode.decode(this.state):e()))}nextSignedInteger(){return this.skipAnnotations((e=>this.state.nextbyte()===t.SignedInteger?this.state.nextint(this.state.varint()):e()))}nextString(){return this.skipAnnotations((e=>this.state.nextbyte()===t.String?c.from(this.state.nextbytes(this.state.varint())).fromUtf8():e()))}nextByteString(){return this.skipAnnotations((e=>this.state.nextbyte()===t.ByteString?c.from(this.state.nextbytes(this.state.varint())):e()))}nextSymbol(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Symbol?Symbol.for(c.from(this.state.nextbytes(this.state.varint())).fromUtf8()):e()))}openRecord(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Record||(e(),!1)))}openSequence(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Sequence||(e(),!1)))}openSet(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Set||(e(),!1)))}openDictionary(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Dictionary||(e(),!1)))}closeCompound(){return this.state.peekend()}}function Kt(t,e={}){return new $t(t,e).next()}function Jt(t,e={}){return Kt(t,Object.assign(Object.assign({},e),{includeAnnotations:!0}))}const Gt=Object.assign({},It,Mt),Xt=Object.assign({},Lt,qt),Zt={decode(t){throw new Error("Cannot decode identityEmbeddedType")},fromValue(t,e){throw new Error("Cannot decode identityEmbeddedType")}},Qt=Object.assign({},Zt,I);var Yt;!function(t){t[t.Boolean=0]="Boolean",t[t.Double=1]="Double",t[t.SignedInteger=2]="SignedInteger",t[t.String=3]="String",t[t.ByteString=4]="ByteString",t[t.Symbol=5]="Symbol",t[t.Record=6]="Record",t[t.Sequence=7]="Sequence",t[t.Set=8]="Set",t[t.Dictionary=9]="Dictionary",t[t.Embedded=10]="Embedded",t[t.Annotated=11]="Annotated"}(Yt||(Yt={}));class te{boolean(t){}double(t){}integer(t){}string(t){}bytes(t){}symbol(t){}record(t,e){e(t.label),t.forEach(e)}array(t,e){t.forEach(e)}set(t,e){t.forEach(e)}dictionary(t,e){t.forEach(((t,n)=>{e(n),e(t)}))}annotated(t,e){e(t.item),t.annotations.forEach(e)}embedded(t,e){}}class ee extends te{constructor(t){super(),this.f=t}embedded(t,e){this.f(t,e)}}class ne{boolean(t){return t}double(t){return kt(t)}integer(t){return t}string(t){return t}bytes(t){return t}symbol(t){return t}record(t,e){return g(e(t.label),t.map(e))}array(t,e){return t.map(e)}set(t,e){return t.map(e)}dictionary(t,e){const n=new Q;return t.forEach(((t,r)=>n.set(e(r),e(t)))),n.simplifiedValue()}annotated(t,e){return pt(e(t.item),...t.annotations.map(e))}}class re extends ne{embedded(t,e){return t}}class se extends ne{constructor(t){super(),this.f=t}embedded(t,e){return this.f(t)}}function ie(t){switch(typeof t){case"boolean":return Yt.Boolean;case"number":if(Number.isInteger(t))return Yt.SignedInteger;throw new Error("Non-integer number in Preserves valueClass; missing Float wrapper?");case"bigint":return Yt.SignedInteger;case"string":return Yt.String;case"symbol":return Yt.Symbol;case"object":return g.isRecord(t)?Yt.Record:Array.isArray(t)?Yt.Sequence:st.isSet(t)?Yt.Set:Y.isDictionary(t)?Yt.Dictionary:ft.isAnnotated(t)?Yt.Annotated:c.isBytes(t)?Yt.ByteString:vt.isDouble(t)?Yt.Double:Yt.Embedded;default:(t=>{throw new Error("Internal error")})()}}const oe=new re;function ae(t,e){const n=t=>{switch(typeof t){case"boolean":return e.boolean(t);case"number":return Number.isInteger(t)?e.integer(t):e.double(t);case"bigint":return e.integer(t);case"string":return e.string(t);case"symbol":return e.symbol(t);case"object":if(g.isRecord(t))return e.record(t,n);if(Array.isArray(t))return e.array(t,n);if(st.isSet(t))return e.set(t,n);if(_(t))return e.embedded(t,n);if(ft.isAnnotated(t))return e.annotated(t,n);if(c.isBytes(t))return e.bytes(t);if(vt.isDouble(t))return e.double(t.value);if(Y.isDictionary(t))return e.dictionary(new Q(t),n);default:(t=>{throw new Error("Internal error")})()}};return n(t)}function ue(t,e){return ae(t,new se(e))}function ce(t,e){return ae(t,new ee(e))}function he(t,e,...n){function r(){throw new Error("Cannot merge items")}function s(e,n){return e===n?e:bt(e)||bt(n)||!ct(e,n)?ae(e,{boolean:r,double:t=>ct(e,n)?e:r(),integer:r,string:r,bytes:t=>ct(e,n)?e:r(),symbol:r,record:t=>(g.isRecord(n)||r(),g(s(t.label,n.label),i(t,n))),array:t=>(Array.isArray(n)&&!g.isRecord(n)||r(),i(t,n)),set(t){r()},dictionary(t){const e=Y.asMap(n);void 0===e&&r();const i=new Q;return t.forEach(((t,n)=>{const r=e.get(n);i.set(n,void 0===r?t:s(t,r))})),e.forEach(((e,n)=>{t.has(n)||i.set(n,e)})),i.simplifiedValue()},annotated:t=>s(t,$(n)),embedded(e){_(n)||r();const s=t(e,n);return void 0===s&&r(),s}}):e}function i(t,e){return t.length<=e.length?e.map(((e,n)=>n<t.length?s(t[n],e):e)):t.map(((t,n)=>n<e.length?s(t,e[n]):t))}return n.reduce(s,e)}function le(t){switch(ut(t)&&(t=t.item),typeof t){case"boolean":return 0;case"number":case"bigint":return 3;case"string":return 4;case"symbol":return 6;case"object":if(vt.isFloat(t))return 2;if(c.isBytes(t))return 5;if(Array.isArray(t))return"label"in t?7:8;if(st.isSet(t))return 9;if(Y.isDictionary(t))return 10;if(_(t))return 11;default:throw new Error("Invalid Value<T> in typeCode")}}function de(t,e,n=((t,e)=>ct(t,e)?0:t<e?-1:1)){return function t(e,r){ut(e)&&(e=e.item),ut(r)&&(r=r.item);const s=le(e),i=le(r);if(s<i)return-1;if(s>i)return 1;switch(s){case 0:case 3:case 4:return e<r?-1:e>r?1:0;case 2:{const t=e.value,n=r.value;return t<n?-1:t>n?1:0}case 5:return c.compare(e,r);case 6:{const t=e.description,n=r.description;return t<n?-1:t>n?1:0}case 7:{const n=t(e.label,r.label);if(0!==n)return n}case 8:{const n=e,s=r,i=Math.min(n.length,s.length);for(let e=0;e<i;e++){const r=t(n[e],s[e]);if(0!==r)return r}return n.length<s.length?-1:n.length>s.length?1:0}case 9:return t(Array.from(e).sort(t),Array.from(r).sort(t));case 10:return t(Array.from(Y.asMap(e).entries()).sort(t),Array.from(Y.asMap(r).entries()).sort(t));case 11:return n(e,r);default:throw new Error("Invalid typeCode: "+s)}}(t,e)}Array.__from_preserve__=t=>Array.isArray(t)?t:void 0;const fe=Array,pe=Symbol;export{ft as Annotated,fe as Array,c as Bytes,e as Constants,m as DecodeError,$t as Decoder,Wt as DecoderState,Y as Dictionary,Q as DictionaryMap,G as DictionaryType,kt as Double,St as DoubleFloat,x as Embedded,X as EncodableDictionary,nt as EncodableSet,b as EncodeError,C as Encoder,D as EncoderState,p as ErrorType,H as FlexMap,W as FlexSet,vt as Float,gt as FloatType,ee as ForEachEmbedded,S as GenericEmbedded,oe as IDENTITY_FOLD,re as IdentityFold,N as IdentityMap,F as IdentitySet,v as IsEmbedded,z as IsMap,at as IsPreservesAnnotated,u as IsPreservesBytes,P as IsSet,L as JsDictionary,Z as KeyedDictionary,rt as KeyedSet,se as MapFold,jt as NUMBER_RE,y as PreservesCodecError,Dt as Reader,Bt as ReaderState,g as Record,st as Set,w as ShortPacket,pe as Symbol,Yt as ValueClass,ne as ValueFold,te as VoidFold,Ut as Writer,Tt as WriterState,R as _iterMap,pt as annotate,yt as annotations,Ht as asLiteral,T as canonicalEncode,U as canonicalString,de as compare,f as dataview,Kt as decode,r as decodeBase64,Jt as decodeWithAnnotations,B as embeddedId,O as encode,i as encodeBase64,tt as encodeDictionaryOn,it as encodeSetOn,V as encodeWithAnnotations,_t as floatValue,xt as floatlikeString,ae as fold,ce as forEachEmbedded,dt as formatPosition,Vt as fromJS,Gt as genericEmbeddedType,It as genericEmbeddedTypeDecode,Mt as genericEmbeddedTypeEncode,h as hexDigit,Qt as identityEmbeddedType,Zt as identityEmbeddedTypeDecode,I as identityEmbeddedTypeEncode,ct as is,ut as isAnnotated,bt as isCompound,_ as isEmbedded,k as isPreservable,Ot as isPreserveWritable,wt as isSequence,ue as mapEmbeddeds,he as merge,Xt as neverEmbeddedType,Lt as neverEmbeddedTypeDecode,qt as neverEmbeddedTypeEncode,ht as newPosition,Nt as parse,Ft as parseAll,K as peel,mt as position,Rt as preserves,Pt as stringify,zt as stringifyEmbeddedWrite,J as strip,le as typeCode,$ as unannotate,d as underlying,l as unhexDigit,lt as updatePosition,ie as valueClass,et as writeDictionaryOn,ot as writeSetOn}; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Preserves={})}(this,(function(e){"use strict";var t;!function(e){e[e.False=128]="False",e[e.True=129]="True",e[e.End=132]="End",e[e.Annotation=133]="Annotation",e[e.Embedded=134]="Embedded",e[e.Ieee754=135]="Ieee754",e[e.SignedInteger=176]="SignedInteger",e[e.String=177]="String",e[e.ByteString=178]="ByteString",e[e.Symbol=179]="Symbol",e[e.Record=180]="Record",e[e.Sequence=181]="Sequence",e[e.Set=182]="Set",e[e.Dictionary=183]="Dictionary"}(t||(t={}));var n=Object.freeze({__proto__:null,get Tag(){return t}});const r=Symbol.for("IsPreservesAnnotated");function s(e){return!!(null==e?void 0:e[r])}function i(e,t){if(s(e)&&(e=e.item),s(t)&&(t=t.item),Object.is(e,t))return!0;if(typeof e!=typeof t)return("number"==typeof e&&"bigint"==typeof t||"bigint"==typeof e&&"number"==typeof t)&&e==t;if("object"==typeof e){if(null===e||null===t)return!1;if("equals"in e&&"function"==typeof e.equals)return e.equals(t,i);if(Array.isArray(e)&&Array.isArray(t)){const n="label"in e;if(n!=="label"in t)return!1;if(n&&!i(e.label,t.label))return!1;if(e.length!==t.length)return!1;for(let n=0;n<e.length;n++)if(!i(e[n],t[n]))return!1;return!0}}return!1}function o(e){return{line:1,column:0,pos:0,name:e}}function a(e,t){if(e.pos++,void 0===e.line)return!1;{let n=!1;switch(t){case"\t":e.column=e.column+8&-8;break;case"\n":e.column=0,e.line++,n=!0;break;case"\r":e.column=0;break;default:e.column++}return n}}class c{constructor(e,t){this.annotations=[],this.pos=null!=t?t:null,this.item=e}__as_preserve__(){return this}static __from_preserve__(e){return s(e)?e:void 0}__preserve_on__(e){if(e.includeAnnotations)for(const n of this.annotations)e.state.emitbyte(t.Annotation),e.push(n);e.push(this.item)}__preserve_text_on__(e){if(e.includeAnnotations){const t=this.annotations.length<=1;for(const n of this.annotations)e.state.pieces.push("@"),e.push(n),t?e.state.pieces.push(" "):e.state.writeIndentSpace()}e.push(this.item)}equals(e){return i(this.item,c.isAnnotated(e)?e.item:e)}get[r](){return!0}static isAnnotated(e){return s(e)}}function u(e,...t){const n=c.isAnnotated(e)?e:new c(e);return t.forEach((e=>n.annotations.push(e))),n}function h(e){return c.isAnnotated(e)?e.annotations:[]}const d={};function l(e){const t=new Uint8Array(Math.floor(3*e.length/4));let n=0,r=0;for(;n<e.length;){const s=d[e[n++]],i=d[e[n++]],o=d[e[n++]],a=d[e[n++]],c=s<<18|i<<12|o<<6|a;if(t[r++]=c>>16&255,void 0===o)break;if(t[r++]=c>>8&255,void 0===a)break;t[r++]=255&c}return t.subarray(0,r)}[..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"].forEach(((e,t)=>d[e]=t)),d["+"]=d["-"]=62,d["/"]=d._=63;const f="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function p(e){let t="",n=0,r=0;for(let s of e)for(n=(63&n)<<8|s,r+=8;r>=6;){r-=6;t+=f[n>>r&63]}if(r>0){t+=f[n<<6-r&63]}return t}const y=new TextEncoder,m=new TextDecoder("utf-8",{fatal:!0}),b=Symbol.for("IsPreservesBytes");class g{constructor(e=new Uint8Array){if(g.isBytes(e))this._view=e._view;else if(ArrayBuffer.isView(e))this._view=new Uint8Array(e.buffer,e.byteOffset,e.byteLength);else if(e instanceof ArrayBuffer)this._view=new Uint8Array(e);else if("string"==typeof e)this._view=y.encode(e);else if("number"==typeof e)this._view=new Uint8Array(e);else{if("number"!=typeof e.length)throw new TypeError("Attempt to initialize Bytes from unsupported value: "+e);this._view=Uint8Array.from(e)}}dataview(){return new DataView(this._view.buffer,this._view.byteOffset,this._view.byteLength)}get length(){return this._view.length}static from(e){return new g(e)}static of(...e){return new g(Uint8Array.of(...e))}static fromLatin1(e){const t=new g(e.length);for(let n=0;n<e.length;n++){const r=e.charCodeAt(n);if(r>=256)throw new Error("Codepoint out of range for 'latin1' byte encoding");t._view[n]=r}return t}static fromBase64(e){return new g(l(e))}static fromHex(e){if(1&e.length)throw new Error("Cannot decode odd-length hexadecimal string");const t=new g(e.length>>1);return g._raw_fromHexInto(e,t._view),t}static _raw_fromHexInto(e,t){const n=e.length>>1;for(let r=0;r<n;r++)t[r]=v(e.charCodeAt(r<<1))<<4|v(e.charCodeAt(1+(r<<1)))}static fromIO(e){return"string"==typeof e||g.isBytes(e)?e:new g(e)}static toIO(e){return"string"==typeof e?e:g.isBytes(e)?e._view:e}get(e){return this._view[e]}equals(e){if(!g.isBytes(e))return!1;if(e.length!==this.length)return!1;const t=this._view,n=e._view;for(let e=0;e<t.length;e++)if(t[e]!==n[e])return!1;return!0}hashCode(){const e=this._view;let t=0;for(let n=0;n<e.length;n++)t=31*t+e[n]|0;return t}static compare(e,t){return e<t?-1:t<e?1:0}static decodeUtf8(e){return m.decode(_(e))}fromUtf8(){return m.decode(this._view)}__as_preserve__(){return this}static __from_preserve__(e){return g.isBytes(e)?e:void 0}toLatin1(){return String.fromCharCode.apply(null,this._view)}toBase64(){return p(this._view)}toHex(e=w){var t=[];for(let n=0;n<this.length;n++)t.push(e(this._view[n]>>4)),t.push(e(15&this._view[n]));return t.join("")}valueOf(){return this.toHex()}__preserve_on__(e){e.state.emitbyte(t.ByteString),e.state.varint(this.length),e.state.emitbytes(this._view)}__preserve_text_on__(e){e.state.writeBytes(this._view)}get[b](){return!0}static isBytes(e){return!!(null==e?void 0:e[b])}}function w(e){return"0123456789abcdef"[e]}function v(e,t=Error){if(e>=48&&e<=57)return e-48;if(e>=97&&e<=102)return e-97+10;if(e>=65&&e<=70)return e-65+10;throw new t("Invalid hex digit: "+String.fromCharCode(e))}function _(e){return e instanceof Uint8Array?e:e._view}function x(e){return e instanceof DataView?e:e.dataview()}g.concat=function(e){let t=0;for(let n=0;n<e.length;n++)t+=_(e[n]).length;const n=new g(t);let r=0;for(let t=0;t<e.length;t++){const s=_(e[t]);n._view.set(s,r),r+=s.length}return n},function(){for(const e of"entries every find findIndex forEach includes indexOf join\nkeys lastIndexOf reduce reduceRight some toLocaleString values".split(/\s+/))g.prototype[e]=function(...t){return this._view[e](...t)};for(const e of"filter map slice subarray".split(/\s+/))g.prototype[e]=function(...t){return new g(this._view[e](...t))};for(const e of"reverse sort".split(/\s+/))g.prototype[e]=function(...t){return new g(this._view.slice()[e](...t))};g.prototype[Symbol.iterator]=function(){return this._view[Symbol.iterator]()}}();const S=Symbol.for("ErrorType");class E{static isCodecError(e,t){return(null==e?void 0:e[S])===t}}class A extends Error{get[S](){return"DecodeError"}constructor(e,t){super(e),this.pos=t}static isDecodeError(e){return E.isCodecError(e,"DecodeError")}}class k extends Error{get[S](){return"EncodeError"}static isEncodeError(e){return E.isCodecError(e,"EncodeError")}constructor(e,t){super(e),this.irritant=t}}class D extends A{get[S](){return"ShortPacket"}static isShortPacket(e){return E.isCodecError(e,"ShortPacket")}}function I(e,t){return t.label=e,t}function B(e){return"object"==typeof e&&null!==e&&"__preserve_on__"in e&&"function"==typeof e.__preserve_on__}function C(e){return String.fromCharCode.apply(null,e)}!function(e){e.isRecord=function(e){return Array.isArray(e)&&"label"in e},e.constructorInfo=function(e){return{label:e.label,arity:e.length}},e.isClassOf=function(t,n){return e.isRecord(n)&&i(t.label,n.label)&&t.arity===n.length},e.makeConstructor=function(){return(t,n)=>{const r=(...n)=>e(t,n),s={label:t,arity:n.length};return r.constructorInfo=s,r.isClassOf=t=>e.isClassOf(s,t),r.fieldNumbers={},r._={},n.forEach(((e,t)=>{r._[e]=e=>e[t],r.fieldNumbers[e]=t})),r}}}(I||(I={}));let T=0;const O=new WeakMap;function V(e){let t=O.get(e);return void 0===t&&(t=T++,O.set(e,t)),t}const j={encode(e,t){new M(e,this).push(V(t))}};class F{constructor(e){this.chunks=[],this.view=new DataView(new ArrayBuffer(256)),this.index=0,this.options=e}get canonical(){var e;return null===(e=this.options.canonical)||void 0===e||e}get includeAnnotations(){var e;return null!==(e=this.options.includeAnnotations)&&void 0!==e?e:!this.canonical}contents(){if(0===this.chunks.length){const e=this.index;return this.index=0,new g(this.view.buffer.slice(0,e))}{this.rotatebuffer(4096);const e=this.chunks;return this.chunks=[],g.concat(e)}}contentsString(){if(0===this.chunks.length){const e=C(new Uint8Array(this.view.buffer,0,this.index));return this.index=0,e}{this.rotatebuffer(4096);const e=this.chunks;return this.chunks=[],e.map(C).join("")}}rotatebuffer(e){this.chunks.push(new Uint8Array(this.view.buffer,0,this.index)),this.view=new DataView(new ArrayBuffer(e)),this.index=0}makeroom(e){this.index+e>this.view.byteLength&&this.rotatebuffer(e+4096)}emitbyte(e){this.makeroom(1),this.view.setUint8(this.index++,e)}emitbytes(e){this.makeroom(e.length),new Uint8Array(this.view.buffer).set(e,this.index),this.index+=e.length}claimbytes(e){this.makeroom(e);const t=new Uint8Array(this.view.buffer,this.index,e);return this.index+=e,t}varint(e){for(;e>=128;)this.emitbyte(e%128+128),e=Math.floor(e/128);this.emitbyte(e)}encodeint(e){if("bigint"==typeof e)return this.encodebigint(e);if(this.emitbyte(t.SignedInteger),0===e)return void this.emitbyte(0);const n=(-1===e?0:Math.floor(Math.log2(e>0?e:-(1+e)))+1)+1+7>>3;this.varint(n);const r=(e,t)=>{e>0&&(r(e-1,Math.floor(t/256)),this.emitbyte(255&t))};r(n,e)}encodebigint(e){let n;if(this.emitbyte(t.SignedInteger),e>0)n=e.toString(16),1&n.length?n="0"+n:v(n.charCodeAt(0))>=8&&(n="00"+n);else{if(!(e<0))return void this.emitbyte(0);{const t=(~e).toString(16);n="";for(let e=0;e<t.length;e++)n+="fedcba9876543210"[v(t.charCodeAt(e))];1&n.length?n="f"+n:v(n.charCodeAt(0))<8&&(n="ff"+n)}}this.varint(n.length>>1),g._raw_fromHexInto(n,this.claimbytes(n.length>>1))}encodebytes(e,t){this.emitbyte(e),this.varint(t.length),this.emitbytes(t)}}class M{constructor(e={},t){var n;e instanceof F?(this.state=e,this.embeddedEncode=null!=t?t:j):(this.state=new F(e),this.embeddedEncode=null!==(n=e.embeddedEncode)&&void 0!==n?n:j)}withEmbeddedEncode(e,t){return t(new M(this.state,e)),this}get canonical(){return this.state.canonical}get includeAnnotations(){return this.state.includeAnnotations}contents(){return this.state.contents()}contentsString(){return this.state.contentsString()}grouped(e,n){this.state.emitbyte(e),n(),this.state.emitbyte(t.End)}push(e){if(B(e))e.__preserve_on__(this);else if(B(e))e.__preserve_on__(this);else if("boolean"==typeof e)this.state.emitbyte(e?t.True:t.False);else if("number"==typeof e||"bigint"==typeof e)this.state.encodeint(e);else if("string"==typeof e)this.state.encodebytes(t.String,new g(e)._view);else if("symbol"==typeof e){const n=Symbol.keyFor(e);if(void 0===n)throw new k("Cannot preserve non-global Symbol",e);this.state.encodebytes(t.Symbol,new g(n)._view)}else if(ArrayBuffer.isView(e))if(e instanceof Uint8Array)this.state.encodebytes(t.ByteString,e);else{const n=new Uint8Array(e.buffer,e.byteOffset,e.byteLength);this.state.encodebytes(t.ByteString,n)}else if(I.isRecord(e)){this.state.emitbyte(t.Record),this.push(e.label);for(let t of e)this.push(t);this.state.emitbyte(t.End)}else!function(e){return"object"==typeof e&&null!==e&&"function"==typeof e[Symbol.iterator]}(e)?(e=>{this.state.emitbyte(t.Embedded),this.embeddedEncode.encode(this.state,e)})(e):this.grouped(t.Sequence,(()=>{for(let t of e)this.push(t)}));return this}}function q(e,t={}){return new M(t).push(e).contents()}const N=new M({canonical:!0});let P=!1;function U(e){if(P)return new M({canonical:!0}).push(e).contentsString();{P=!0;const t=N.push(e).contentsString();return P=!1,t}}const R=Map,W=Set,z=Symbol.for("IsMap"),H=Symbol.for("IsSet");function L(e,t){const n=e=>e.done?{done:!0,value:null}:{done:!1,value:t(e.value)};return{next:t=>n(e.next(t)),return:t=>{var r,s;return n(null!==(s=null===(r=e.return)||void 0===r?void 0:r.call(e,t))&&void 0!==s?s:{done:!0,value:null})},throw:t=>{var r,s;return n(null!==(s=null===(r=e.throw)||void 0===r?void 0:r.call(e,t))&&void 0!==s?s:{done:!0,value:null})},[Symbol.iterator](){return this}}}z in Map.prototype||(Object.defineProperty(Map.prototype,z,{get:()=>!0}),Map.isMap=e=>!!(null==e?void 0:e[z])),H in Set.prototype||(Object.defineProperty(Set.prototype,H,{get:()=>!0}),Set.isSet=e=>!!(null==e?void 0:e[H]));class ${constructor(e,t){this.canonicalizer=e,this.items=void 0===t?new Map:new Map(L(t[Symbol.iterator](),(([e,t])=>[this._key(e),[e,t]])))}_key(e){return this.canonicalizer(e)}get(e,t){const n=this.items.get(this._key(e));return void 0===n?t:n[1]}getOrSet(e,t){const n=this._key(e);let r=this.items.get(n);return void 0===r&&(r=[e,t()],this.items.set(n,r)),r[1]}set(e,t){return this.items.set(this._key(e),[e,t]),this}forEach(e,t){this.items.forEach((([n,r])=>e.call(t,r,n,this)))}entries(){return this.items.values()}keys(){return L(this.items.values(),(([e,t])=>e))}values(){return L(this.items.values(),(([e,t])=>t))}delete(e){return this.items.delete(this._key(e))}getAndDelete(e,t){const n=this._key(e),r=this.items.get(n);return void 0===r?t:(this.items.delete(n),r[1])}clear(){this.items.clear()}has(e){return this.items.has(this._key(e))}get size(){return this.items.size}[Symbol.iterator](){return this.items.values()}get[Symbol.toStringTag](){return"FlexMap"}equals(e,t=((e,t)=>e===t)){if(!("size"in e&&"has"in e&&"get"in e))return!1;if(this.size!==e.size)return!1;for(let[n,r]of this.items.values()){if(!e.has(n))return!1;if(!t(r,e.get(n)))return!1}return!0}update(e,t,n,r=((e,t)=>e===t)){const s=this._key(e);if(this.items.has(s)){const n=this.items.get(s)[1],i=t(n);return void 0===i?(this.items.delete(s),-1):(r(i,n)||this.items.set(s,[e,i]),0)}{const r=t(n);return void 0===r?0:(this.items.set(s,[e,r]),1)}}canonicalKeys(){return this.items.keys()}get[z](){return!0}}class K{constructor(e,t){this.canonicalizer=e,this.items=void 0===t?new Map:new Map(L(t[Symbol.iterator](),(e=>[this._key(e),e])))}_key(e){return this.canonicalizer(e)}has(e){return this.items.has(this._key(e))}get(e){const t=this._key(e);return this.items.has(t)?{item:this.items.get(t)}:null}add(e){return this.items.set(this._key(e),e),this}forEach(e,t){this.items.forEach((n=>e.call(t,n,n,this)))}entries(){return L(this.items.values(),(e=>[e,e]))}keys(){return this.items.values()}values(){return this.items.values()}delete(e){return this.items.delete(this._key(e))}clear(){this.items.clear()}get size(){return this.items.size}[Symbol.iterator](){return this.items.values()}get[Symbol.toStringTag](){return"FlexSet"}equals(e){if(!("size"in e)||!("has"in e))return!1;if(this.size!==e.size)return!1;for(let t of this.items.values())if(!e.has(t))return!1;return!0}canonicalValues(){return this.items.keys()}union(e){const t=new K(this.canonicalizer,this);for(let n of e)t.add(n);return t}intersect(e){const t=new K(this.canonicalizer);for(let n of this)e.has(n)&&t.add(n);return t}subtract(e){const t=new K(this.canonicalizer);for(let n of this)e.has(n)||t.add(n);return t}get[H](){return!0}}const G=Symbol.for("DictionaryType");class J extends ${constructor(e,t,n){super((t=>U(e(t))),n),this.encodeK=e,this.encodeV=t}__preserve_on__(e){Z(this,e,((e,t)=>t.push(this.encodeK(e))),((e,t)=>t.push(this.encodeV(e))))}__preserve_text_on__(e){Q(this,e,((e,t)=>t.push(this.encodeK(e))),((e,t)=>t.push(this.encodeV(e))))}}class X extends J{get[G](){return"Dictionary"}static isKeyedDictionary(e){return"Dictionary"===(null==e?void 0:e[G])}constructor(e){super((e=>e),(e=>e),e)}mapEntries(e){const t=new X;for(let n of this.entries()){const r=e(n);t.set(r[0],r[1])}return t}clone(){return new X(this)}get[Symbol.toStringTag](){return"Dictionary"}}class Y extends X{static isDictionary(e){return"Dictionary"===(null==e?void 0:e[G])}static __from_preserve__(e){return Y.isDictionary(e)?e:void 0}}function Z(e,n,r,s){if(n.canonical){const i=Array.from(e),o=new M({canonical:!0,embeddedEncode:n.embeddedEncode}),a=i.map((([e,t],n)=>(r(e,o),[o.contents(),n])));a.sort(((e,t)=>g.compare(e[0],t[0]))),n.grouped(t.Dictionary,(()=>a.forEach((([e,t])=>{const[o,a]=i[t];r(o,n),s(a,n)}))))}else n.grouped(t.Dictionary,(()=>e.forEach(((e,t)=>{r(t,n),s(e,n)}))))}function Q(e,t,n,r){t.state.writeSeq("{","}",e.entries(),(([e,s])=>{n(e,t),c.isAnnotated(s)&&h(s).length>1&&t.state.isIndenting?(t.state.pieces.push(":"),t.state.indentCount++,t.state.writeIndent(),r(s,t),t.state.indentCount--):(t.state.pieces.push(": "),r(s,t))}))}class ee extends K{constructor(e,t){super((t=>U(e(t))),t),this.encodeV=e}__preserve_on__(e){re(this,e,((e,t)=>t.push(this.encodeV(e))))}__preserve_text_on__(e){se(this,e,((e,t)=>t.push(this.encodeV(e))))}}class te extends ee{get[G](){return"Set"}static isKeyedSet(e){return"Set"===(null==e?void 0:e[G])}constructor(e){super((e=>e),e)}map(e){return new te(L(this[Symbol.iterator](),e))}filter(e){const t=new te;for(let n of this)e(n)&&t.add(n);return t}clone(){return new te(this)}get[Symbol.toStringTag](){return"Set"}}let ne=class e extends te{static isSet(e){return"Set"===(null==e?void 0:e[G])}static __from_preserve__(t){return e.isSet(t)?t:void 0}};function re(e,n,r){if(n.canonical){const s=new M({canonical:!0,embeddedEncode:n.embeddedEncode}),i=Array.from(e).map((e=>(r(e,s),[s.contents(),e])));i.sort(((e,t)=>g.compare(e[0],t[0]))),n.grouped(t.Set,(()=>i.forEach((([e,t])=>r(t,n)))))}else n.grouped(t.Set,(()=>e.forEach((e=>r(e,n)))))}function se(e,t,n){t.state.writeSeq("#{","}",e,(e=>n(e,t)))}function ie(e){return Array.isArray(e)||ne.isSet(e)||Y.isDictionary(e)}const oe=Symbol.for("FloatType");class ae{constructor(e){this.value="number"==typeof e?e:e.value}__preserve_text_on__(e){e.state.pieces.push(this.toString())}equals(e){return!!Object.is(e.constructor,this.constructor)&&(Number.isNaN(this.value)&&Number.isNaN(e.value)?e.toBytes().equals(this.toBytes()):Object.is(e.value,this.value))}hashCode(){return 0|this.value}}function ce(e){if(Object.is(e,-0))return"-0.0";const t=""+e;return t.includes(".")||t.includes("e")||t.includes("E")?t:t+".0"}ae.isFloat=e=>void 0!==(null==e?void 0:e[oe]),ae.isDouble=e=>"Double"===(null==e?void 0:e[oe]);class ue extends ae{__as_preserve__(){return this}static fromBytes(e){return new ue(x(e).getFloat64(0,!1))}static __from_preserve__(e){return ae.isDouble(e)?e:void 0}__preserve_on__(e){e.state.emitbyte(t.Ieee754),e.state.emitbyte(8),e.state.makeroom(8),e.state.view.setFloat64(e.state.index,this.value,!1),e.state.index+=8}toBytes(){const e=new g(8);return e.dataview().setFloat64(0,this.value,!1),e}toString(){return Number.isFinite(this.value)?ce(this.value):'#xd"'+this.toBytes().toHex()+'"'}get[oe](){return"Double"}}function he(e){return new ue(e)}class de{constructor(e,t){this.index=0,this.packet=_(e),this.options=t}get includeAnnotations(){var e;return null!==(e=this.options.includeAnnotations)&&void 0!==e&&e}write(e){this.index===this.packet.length?this.packet=_(e):this.packet=g.concat([this.packet.slice(this.index),e])._view,this.index=0}atEnd(){return this.index>=this.packet.length}mark(){return this.index}restoreMark(e){this.index=e}shortGuard(e,t){if(this.atEnd())return t();const n=this.mark();try{return e()}catch(e){if(D.isShortPacket(e))return this.restoreMark(n),t();throw e}}nextbyte(){if(this.atEnd())throw new D("Short packet");return this.packet[this.index++]}nextbytes(e){const t=this.index;if(this.index+=e,this.index>this.packet.length)throw new D("Short packet");return new DataView(this.packet.buffer,this.packet.byteOffset+t,e)}varint(){const e=this.nextbyte();return e<128?e:(this.varint()<<7)+(e-128)}peekend(){return this.nextbyte()===t.End||(this.index--,!1)}nextint(e){const t=this.index;if(0===e)return 0;if(e>7)return this.nextbigint(e);if(7===e){const t=this.packet[this.index];if(t>=32&&t<224)return this.nextbigint(e)}let n=this.nextbyte();128&n&&(n-=256);for(let t=1;t<e;t++)n=256*n+this.nextbyte();return Number.isSafeInteger(n)?n:(this.index=t,this.nextbigint(e))}nextbigint(e){if(0===e)return BigInt(0);const t=g.from(this.nextbytes(e));if(t.get(0)>=128){const e=t.toHex((e=>w(15-e)));return~BigInt("0x"+e)}{const e=t.toHex();return BigInt("0x"+e)}}wrap(e){return this.includeAnnotations?new c(e):e}unshiftAnnotation(e,t){return this.includeAnnotations&&t.annotations.unshift(e),t}}const le={decode(e){throw new Error("Embeddeds not permitted at this point in Preserves document")},fromValue(e,t){throw new Error("Embeddeds not permitted at this point in Preserves document")}};class fe{constructor(e=new Uint8Array(0),t){var n,r,s;if(e instanceof de)this.state=e,this.embeddedDecode=null!==(n=t)&&void 0!==n?n:le;else{const n=null!==(r=t)&&void 0!==r?r:{};this.state=new de(e,n),this.embeddedDecode=null!==(s=n.embeddedDecode)&&void 0!==s?s:le}}write(e){this.state.write(e)}nextvalues(){const e=[];for(;!this.state.peekend();)e.push(this.next());return e}static dictionaryFromArray(e){const t=new Y;if(e.length%2)throw new A("Missing dictionary value");for(let n=0;n<e.length;n+=2)t.set(e[n],e[n+1]);return t}next(){const e=this.state.nextbyte();switch(e){case t.False:return this.state.wrap(!1);case t.True:return this.state.wrap(!0);case t.End:throw new A("Unexpected Compound end marker");case t.Annotation:{const e=this.next(),t=this.next();return this.state.unshiftAnnotation(e,t)}case t.Embedded:return this.state.wrap(this.embeddedDecode.decode(this.state));case t.Ieee754:if(8===this.state.varint())return this.state.wrap(ue.fromBytes(this.state.nextbytes(8)));throw new A("Invalid IEEE754 size");case t.SignedInteger:return this.state.wrap(this.state.nextint(this.state.varint()));case t.String:return this.state.wrap(g.from(this.state.nextbytes(this.state.varint())).fromUtf8());case t.ByteString:return this.state.wrap(g.from(this.state.nextbytes(this.state.varint())));case t.Symbol:return this.state.wrap(Symbol.for(g.from(this.state.nextbytes(this.state.varint())).fromUtf8()));case t.Record:{const e=this.nextvalues();if(0===e.length)throw new A("Too few elements in encoded record");return this.state.wrap(I(e[0],e.slice(1)))}case t.Sequence:return this.state.wrap(this.nextvalues());case t.Set:return this.state.wrap(new ne(this.nextvalues()));case t.Dictionary:return this.state.wrap(fe.dictionaryFromArray(this.nextvalues()));default:throw new A("Unsupported Preserves tag: "+e)}}try_next(){return this.state.shortGuard((()=>this.next()),(()=>{}))}atEnd(){return this.state.atEnd()}mark(){return this.state.mark()}restoreMark(e){return this.state.restoreMark(e)}skip(){this.next()}withEmbeddedDecode(e,t){return t(new fe(this.state,e))}skipAnnotations(e){const n=this.mark();for(;!this.state.atEnd()&&this.state.packet[this.state.index]===t.Annotation;)this.state.index++,this.skip();return e((()=>this.restoreMark(n)))}nextBoolean(){return this.skipAnnotations((e=>{switch(this.state.nextbyte()){case t.False:return!1;case t.True:return!0;default:return e()}}))}nextDouble(){return this.skipAnnotations((e=>this.state.nextbyte()!==t.Ieee754||8!==this.state.nextbyte()?e():ue.fromBytes(this.state.nextbytes(8))))}nextEmbedded(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Embedded?this.embeddedDecode.decode(this.state):e()))}nextSignedInteger(){return this.skipAnnotations((e=>this.state.nextbyte()===t.SignedInteger?this.state.nextint(this.state.varint()):e()))}nextString(){return this.skipAnnotations((e=>this.state.nextbyte()===t.String?g.from(this.state.nextbytes(this.state.varint())).fromUtf8():e()))}nextByteString(){return this.skipAnnotations((e=>this.state.nextbyte()===t.ByteString?g.from(this.state.nextbytes(this.state.varint())):e()))}nextSymbol(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Symbol?Symbol.for(g.from(this.state.nextbytes(this.state.varint())).fromUtf8()):e()))}openRecord(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Record||(e(),!1)))}openSequence(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Sequence||(e(),!1)))}openSet(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Set||(e(),!1)))}openDictionary(){return this.skipAnnotations((e=>this.state.nextbyte()===t.Dictionary||(e(),!1)))}closeCompound(){return this.state.peekend()}}function pe(e,t={}){return new fe(e,t).next()}function ye(e){return!("object"!=typeof e||null===e||Array.isArray(e)||g.isBytes(e)||ae.isFloat(e)||ne.isSet(e)||Y.isDictionary(e)||c.isAnnotated(e))}class me{constructor(e){this.generic=e}equals(e,t){return"object"==typeof e&&"generic"in e&&t(this.generic,e.generic)}toString(){return this.generic.toString()}}function be(e){return c.isAnnotated(e)?e.item:e}function ge(e,t=1/0){return function e(t,n){if(0===n)return t;if(!c.isAnnotated(t))return t;const r=n-1;function s(t){return e(t,r)}if(I.isRecord(t.item))return I(e(t.item.label,n),t.item.map(s));if(c.isAnnotated(t.item))throw new Error("Improper annotation structure");return 0===r?t.item:Array.isArray(t.item)||ne.isSet(t.item)?t.item.map(s):Y.isDictionary(t.item)?t.item.mapEntries((e=>[s(e[0]),s(e[1])])):t.item}(e,t)}function we(e){return"object"==typeof e&&null!==e&&"__preserve_text_on__"in e&&"function"==typeof e.__preserve_text_on__}const ve={encode(e,t){new M(e,this).push(t.generic)},toValue:e=>e.generic},_e={encode(e,t){throw new Error("Embeddeds not permitted encoding Preserves document")},toValue(e){throw new Error("Embeddeds not permitted writing Preserves document")}};class xe{constructor(e){var t;this.pieces=[],this.indentCount=0,this.options=e,this.indentDelta=" ".repeat(null!==(t=e.indent)&&void 0!==t?t:0)}get isIndenting(){return this.indentDelta.length>0}get includeAnnotations(){var e;return null===(e=this.options.includeAnnotations)||void 0===e||e}writeIndent(){if(this.isIndenting){this.pieces.push("\n");for(let e=0;e<this.indentCount;e++)this.pieces.push(this.indentDelta)}}writeIndentSpace(){this.isIndenting?this.writeIndent():this.pieces.push(" ")}escapeStringlikeChar(e,t=(e=>e)){switch(e){case"\\":return"\\\\";case"\b":return"\\b";case"\f":return"\\f";case"\n":return"\\n";case"\r":return"\\r";case"\t":return"\\t";default:return t(e)}}escapeStringlike(e,t){let n=t;for(let r of e)n+=r===t?"\\"+t:this.escapeStringlikeChar(r);return n+t}writeSeq(e,t,n,r){let s=n[Symbol.iterator]();this.pieces.push(e);const i=s.next();if(!0!==i.done){const e=i.value,t=s.next();if(!0===t.done)r(e);else{let n;for(this.indentCount++,this.writeIndent(),r(e),this.writeIndentSpace(),r(t.value);!0!==(n=s.next()).done;)this.writeIndentSpace(),r(n.value);this.indentCount--,this.writeIndent()}}this.pieces.push(t)}writeBytes(e){var t,n;const r=null!==(t=this.options.maxBinaryAsciiLength)&&void 0!==t?t:1024,s=null!==(n=this.options.maxBinaryAsciiProportion)&&void 0!==n?n:.75;if(e.length>=r)this.writeBase64(e);else{let t=0,n=Math.min(e.length,r);for(let r=0;r<n;r++){const n=e[r];switch(n){case 9:case 10:case 13:t++;break;default:n>=32&&n<=126&&t++}}0===n||t/n>=s?this.writeBinaryStringlike(e):this.writeBase64(e)}}writeBase64(e){this.pieces.push("#[",p(e),"]")}writeBinaryStringlike(e){let t='#"';for(let n of e)t+=34===n?'\\"':this.escapeStringlikeChar(String.fromCharCode(n),(e=>n>=32&&n<=126&&92!==n?e:"\\x"+("0"+n.toString(16)).slice(-2)));this.pieces.push(t+'"')}couldBeFlat(e){let t=!1;for(let n of e){if(Array.isArray(n)||Set.isSet(n)||Map.isMap(n)){if(t)return!1;t=!0}if(this.includeAnnotations&&s(n)&&n.annotations.length>1)return!1}return!0}}class Se{constructor(e={},t){var n;e instanceof xe?(this.state=e,this.embeddedWrite=t):(this.state=new xe(e),this.embeddedWrite=null!==(n=e.embeddedWrite)&&void 0!==n?n:_e)}static stringify(e,t){const n=new Se(t);return n.push(e),n.contents()}contents(){return this.state.pieces.join("")}get includeAnnotations(){return this.state.includeAnnotations}push(e){switch(typeof e){case"boolean":this.state.pieces.push(e?"#t":"#f");break;case"string":this.state.pieces.push(this.state.escapeStringlike(e,'"'));break;case"symbol":{const t=e.description;/^[-a-zA-Z0-9~!$%^&*?_=+/.]+$/.exec(t)&&!Be.exec(t)?this.state.pieces.push(t):this.state.pieces.push(this.state.escapeStringlike(t,"|"));break}case"bigint":case"number":this.state.pieces.push(""+e);break;case"object":if(null===e)throw new Error("Cannot encode null in Preserves Writer.push");if(we(e))e.__preserve_text_on__(this);else if(we(e))e.__preserve_text_on__(this);else if(ArrayBuffer.isView(e))if(e instanceof Uint8Array)this.state.writeBytes(e);else{const t=new Uint8Array(e.buffer,e.byteOffset,e.byteLength);this.state.writeBytes(t)}else if(I.isRecord(e)){const t=this.state.couldBeFlat(e);this.state.pieces.push("<"),this.push(e.label),t||this.state.indentCount++;for(let n of e)t?this.state.pieces.push(" "):this.state.writeIndentSpace(),this.push(n);t||this.state.indentCount--,this.state.pieces.push(">")}else!function(e){return"object"==typeof e&&null!==e&&"function"==typeof e[Symbol.iterator]}(e)?(e=>{this.state.pieces.push("#:"),"write"in this.embeddedWrite?this.embeddedWrite.write(this.state,e):new Se(this.state,ve).push(this.embeddedWrite.toValue(e))})(e):this.state.writeSeq("[","]",e,(e=>this.push(e)));break;default:(t=>{throw new Error(`Internal error: unhandled in Preserves Writer.push for ${e}`)})()}return this}}function Ee(e){switch(typeof e){case"number":if(!Number.isInteger(e))throw new TypeError("Refusing to autoconvert non-integer number to Double");case"bigint":case"string":case"symbol":case"boolean":return e;case"undefined":case"function":break;case"object":if(null===e)break;if("function"==typeof e.__as_preserve__)return e.__as_preserve__();if(I.isRecord(e))return e;if(Array.isArray(e))return e.map(Ee);if(ArrayBuffer.isView(e)||e instanceof ArrayBuffer)return g.from(e);if(Map.isMap(e)){const t=new Y;return e.forEach(((e,n)=>t.set(Ee(n),Ee(e)))),t}if(ne.isSet(e)){const t=new ne;return e.forEach((e=>t.add(Ee(e)))),t}return e}throw new TypeError("Cannot represent JavaScript value as Preserves: "+e)}Y.fromJS=function(e){if(Y.isDictionary(e))return e;const t=new Y;return Object.entries(e).forEach((([e,n])=>t.set(e,Ee(n)))),t};const Ae={write(e,t){if(t instanceof me)new Se(e,this).push(t.generic);else{try{const n=Ee(t);if(!ye(n))return void new Se(e,this).push(n)}catch(e){}try{return void e.pieces.push(JSON.stringify(t))}catch(e){}e.pieces.push("⌜"+t+"⌝")}}};function ke(e,t){var n;return(t=Object.assign({},null!=t?t:{})).embeddedWrite=null!==(n=t.embeddedWrite)&&void 0!==n?n:Ae,Se.stringify(Ee(e),t)}[c,g,X,te].forEach((e=>{e.prototype.toString=function(){return ke(this)}}));const De=BigInt(Number.MAX_SAFE_INTEGER),Ie=BigInt(Number.MIN_SAFE_INTEGER),Be=/^([-+]?\d+)((\.\d+([eE][-+]?\d+)?)|([eE][-+]?\d+))?$/;class Ce{constructor(e,t){switch(this.discarded=0,this.buffer=e,typeof t.name){case"undefined":this.pos=o();break;case"string":this.pos=o(t.name);break;case"object":this.pos=Object.assign({},t.name)}this.index=0,this.options=t}error(e,t){throw new A(e,Object.assign({},t))}get includeAnnotations(){var e;return null!==(e=this.options.includeAnnotations)&&void 0!==e&&e}copyPos(){return Object.assign({},this.pos)}write(e){this.atEnd()?this.buffer=e:this.buffer=this.buffer.substring(this.index)+e,this.discarded+=this.index,this.index=0}atEnd(){return this.index>=this.buffer.length}peek(){if(this.atEnd())throw new D("Short term",this.pos);return this.buffer[this.index]}advance(){const e=this.index++;return a(this.pos,this.buffer[e]),e}nextchar(){if(this.atEnd())throw new D("Short term",this.pos);return this.buffer[this.advance()]}nextcharcode(){if(this.atEnd())throw new D("Short term",this.pos);return this.buffer.charCodeAt(this.advance())}skipws(e=!1){for(;!this.atEnd();){const t=this.peek();if(!(Ve(t)||e&&","===t))break;this.advance()}}readHex2(){return v(this.nextcharcode(),A)<<4|v(this.nextcharcode(),A)}readHex4(){return v(this.nextcharcode(),A)<<12|v(this.nextcharcode(),A)<<8|v(this.nextcharcode(),A)<<4|v(this.nextcharcode(),A)}readHexBinary(){const e=[];for(;;){if(this.skipws(),'"'===this.peek())return this.advance(),g.from(e);e.push(this.readHex2())}}readHexFloat(){const e=this.copyPos();'"'!==this.nextchar()&&this.error("Missing open-double-quote in hex-encoded floating-point number",e);const t=this.readHexBinary();return 8!==t.length&&this.error("Incorrect number of bytes in hex-encoded Double",e),ue.fromBytes(t)}readBase64Binary(){let e="";for(;;){this.skipws();const t=this.nextchar();if("]"===t)break;e+=t}return g.fromBase64(e)}requireDelimiter(e){this.delimiterFollows()||this.error(`Delimiter must follow ${e}`,this.pos)}delimiterFollows(){if(this.atEnd())return!0;const e=this.peek();return-1!=='(){}[]<>";,@#:|'.indexOf(e)||Ve(e)}readRawSymbolOrNumber(e){for(;!this.delimiterFollows();)e+=this.nextchar();const t=Be.exec(e);if(t){if(void 0===t[2]){let e=BigInt(t[1]);return e<=Ie||e>=De?e:Number(e)}return he(parseFloat(e))}return Symbol.for(e)}readStringlike(e,t,n,r,s){let i=[];for(;;){const o=this.nextchar();switch(o){case n:return t(i);case"\\":{const t=this.nextchar();switch(t){case r:i.push(s());break;case n:case"\\":case"/":i.push(e(t));break;case"b":i.push(e("\b"));break;case"f":i.push(e("\f"));break;case"n":i.push(e("\n"));break;case"r":i.push(e("\r"));break;case"t":i.push(e("\t"));break;default:this.error(`Invalid escape code \\${t}`,this.pos)}break}default:i.push(e(o))}}}readString(e){return this.readStringlike((e=>e),(e=>e.join("")),e,"u",(()=>{const e=this.readHex4();if(e>=55296&&e<=57343){if("\\"===this.nextchar()&&"u"===this.nextchar()){const t=this.readHex4();if(t>=56320&&t<=57343&&e<=56319)return String.fromCharCode(e,t)}this.error("Invalid surrogate pair",this.pos)}return String.fromCharCode(e)}))}readLiteralBinary(){return this.readStringlike((e=>{const t=e.charCodeAt(0);return t>=256&&this.error(`Invalid code point ${t} in literal binary`,this.pos),t}),g.from,'"',"x",(()=>this.readHex2()))}}const Te={decode(e){return new me(new fe(e,this).next())},fromValue:(e,t)=>new me(t.includeAnnotations?e:ge(e))};class Oe{constructor(e="",t){var n,r;if(e instanceof Ce)this.state=e,this.embeddedType=t;else{const s=null!==(n=t)&&void 0!==n?n:{};this.state=new Ce(e,s),this.embeddedType=null!==(r=s.embeddedDecode)&&void 0!==r?r:le}}write(e){this.state.write(e)}readCommentLine(){const e=this.state.copyPos();let t="";for(;;){const n=this.state.nextchar();if("\n"===n||"\r"===n)return this.wrap(t,e);t+=n}}wrap(e,t){return this.state.includeAnnotations&&!c.isAnnotated(e)&&(e=new c(e,t)),e}annotateNextWith(e){if(this.state.skipws(),this.state.atEnd())throw new A("Trailing annotations and comments are not permitted",this.state.pos);const t=this.next();return this.state.includeAnnotations&&t.annotations.unshift(e),t}readToEnd(){const e=[];for(;;){if(this.state.skipws(),this.state.atEnd())return e;e.push(this.next())}}next(){this.state.skipws();const e=this.state.copyPos(),t=(()=>{const t=this.state.nextchar();switch(t){case'"':return this.state.readString('"');case"|":return Symbol.for(this.state.readString("|"));case";":this.state.error("Semicolon is reserved syntax",e);case"@":return this.annotateNextWith(this.next());case":":this.state.error("Unexpected key/value separator between items",e);case"#":{const t=this.state.nextchar();switch(t){case" ":case"\t":return this.annotateNextWith(this.readCommentLine());case"\n":case"\r":return this.annotateNextWith("");case"!":return this.annotateNextWith(I(Symbol.for("interpreter"),[this.readCommentLine()]));case"f":return this.state.requireDelimiter("#f"),!1;case"t":return this.state.requireDelimiter("#t"),!0;case"{":return this.readSet();case'"':return this.state.readLiteralBinary();case"x":switch(this.state.nextchar()){case'"':return this.state.readHexBinary();case"d":return this.state.readHexFloat();default:this.state.error("Invalid #x syntax",e)}case"[":return this.state.readBase64Binary();case":":return this.embeddedType.fromValue(new Oe(this.state,Te).next(),this.state.options);default:this.state.error(`Invalid # syntax: ${t}`,e)}}case"<":return I(this.next(),this.readSequence(">",!1));case"[":return this.readSequence("]",!0);case"{":return this.readDictionary();case">":this.state.error("Unexpected >",e);case"]":this.state.error("Unexpected ]",e);case"}":this.state.error("Unexpected }",e);case",":this.state.error("Unexpected ,",e);default:return this.state.readRawSymbolOrNumber(t)}})();return this.wrap(t,e)}seq(e,t,n,r){for(;;){if(this.state.skipws(e),this.state.peek()===r)return this.state.advance(),t;n(this.next(),t)}}readSequence(e,t){return this.seq(t,[],((e,t)=>t.push(e)),e)}readDictionary(){return this.seq(!0,new Y,((e,t)=>{if(this.state.skipws(),":"===this.state.peek())t.has(e)&&this.state.error(`Duplicate key: ${ke(e)}`,this.state.pos),this.state.advance(),t.set(e,this.next());else this.state.error("Missing key/value separator",this.state.pos)}),"}")}readSet(){return this.seq(!0,new ne,((e,t)=>{t.has(e)&&this.state.error(`Duplicate value in set: ${ke(e)}`,this.state.pos),t.add(e)}),"}")}}function Ve(e){return-1!==" \t\n\r".indexOf(e)}const je=Object.assign({},Te,ve),Fe=Object.assign({},le,_e),Me={decode(e){throw new Error("Cannot decode identityEmbeddedType")},fromValue(e,t){throw new Error("Cannot decode identityEmbeddedType")}},qe=Object.assign({},Me,j);var Ne;e.ValueClass=void 0,(Ne=e.ValueClass||(e.ValueClass={}))[Ne.Boolean=0]="Boolean",Ne[Ne.Double=1]="Double",Ne[Ne.SignedInteger=2]="SignedInteger",Ne[Ne.String=3]="String",Ne[Ne.ByteString=4]="ByteString",Ne[Ne.Symbol=5]="Symbol",Ne[Ne.Record=6]="Record",Ne[Ne.Sequence=7]="Sequence",Ne[Ne.Set=8]="Set",Ne[Ne.Dictionary=9]="Dictionary",Ne[Ne.Embedded=10]="Embedded",Ne[Ne.Annotated=11]="Annotated";class Pe{boolean(e){}double(e){}integer(e){}string(e){}bytes(e){}symbol(e){}record(e,t){t(e.label),e.forEach(t)}array(e,t){e.forEach(t)}set(e,t){e.forEach(t)}dictionary(e,t){e.forEach(((e,n)=>{t(n),t(e)}))}annotated(e,t){t(e.item),e.annotations.forEach(t)}embedded(e,t){}}class Ue extends Pe{constructor(e){super(),this.f=e}embedded(e,t){this.f(e,t)}}class Re{boolean(e){return e}double(e){return he(e)}integer(e){return e}string(e){return e}bytes(e){return e}symbol(e){return e}record(e,t){return I(t(e.label),e.map(t))}array(e,t){return e.map(t)}set(e,t){return e.map(t)}dictionary(e,t){return e.mapEntries((([e,n])=>[t(e),t(n)]))}annotated(e,t){return u(t(e.item),...e.annotations.map(t))}}class We extends Re{embedded(e,t){return e}}class ze extends Re{constructor(e){super(),this.f=e}embedded(e,t){return this.f(e)}}const He=new We;function Le(e,t){const n=e=>{switch(typeof e){case"boolean":return t.boolean(e);case"number":return Number.isInteger(e)?t.integer(e):t.double(e);case"bigint":return t.integer(e);case"string":return t.string(e);case"symbol":return t.symbol(e);case"object":return I.isRecord(e)?t.record(e,n):Array.isArray(e)?t.array(e,n):ne.isSet(e)?t.set(e,n):Y.isDictionary(e)?t.dictionary(e,n):c.isAnnotated(e)?t.annotated(e,n):g.isBytes(e)?t.bytes(e):ae.isDouble(e)?t.double(e.value):t.embedded(e,n);default:(e=>{throw new Error("Internal error")})()}};return n(e)}function $e(e){switch(s(e)&&(e=e.item),typeof e){case"boolean":return 0;case"number":case"bigint":return 3;case"string":return 4;case"symbol":return 6;case"object":if(ae.isFloat(e))return 2;if(g.isBytes(e))return 5;if(Array.isArray(e))return"label"in e?7:8;if(ne.isSet(e))return 9;if(Y.isDictionary(e))return 10;if(ye(e))return 11;default:throw new Error("Invalid Value<T> in typeCode")}}Array.__from_preserve__=e=>Array.isArray(e)?e:void 0;const Ke=Array,Ge=Symbol;e.Annotated=c,e.Array=Ke,e.Bytes=g,e.Constants=n,e.DecodeError=A,e.Decoder=fe,e.DecoderState=de,e.Dictionary=Y,e.DictionaryType=G,e.Double=he,e.DoubleFloat=ue,e.Embedded=class{constructor(e){this.value=e}},e.EncodableDictionary=J,e.EncodableSet=ee,e.EncodeError=k,e.Encoder=M,e.EncoderState=F,e.ErrorType=S,e.FlexMap=$,e.FlexSet=K,e.Float=ae,e.FloatType=oe,e.ForEachEmbedded=Ue,e.GenericEmbedded=me,e.IDENTITY_FOLD=He,e.IdentityFold=We,e.IdentityMap=R,e.IdentitySet=W,e.IsMap=z,e.IsPreservesAnnotated=r,e.IsPreservesBytes=b,e.IsSet=H,e.KeyedDictionary=X,e.KeyedSet=te,e.MapFold=ze,e.NUMBER_RE=Be,e.PreservesCodecError=E,e.Reader=Oe,e.ReaderState=Ce,e.Record=I,e.Set=ne,e.ShortPacket=D,e.Symbol=Ge,e.ValueFold=Re,e.VoidFold=Pe,e.Writer=Se,e.WriterState=xe,e._iterMap=L,e.annotate=u,e.annotations=h,e.asLiteral=function(e,t){return i(e,t)?t:void 0},e.canonicalEncode=function(e,t){if(void 0!==t||P)return q(e,Object.assign(Object.assign({},t),{canonical:!0}));{P=!0;const t=N.push(e).contents();return P=!1,t}},e.canonicalString=U,e.compare=function(e,t,n=((e,t)=>i(e,t)?0:e<t?-1:1)){return function e(t,r){s(t)&&(t=t.item),s(r)&&(r=r.item);const i=$e(t),o=$e(r);if(i<o)return-1;if(i>o)return 1;switch(i){case 0:case 3:case 4:return t<r?-1:t>r?1:0;case 2:{const e=t.value,n=r.value;return e<n?-1:e>n?1:0}case 5:return g.compare(t,r);case 6:{const e=t.description,n=r.description;return e<n?-1:e>n?1:0}case 7:{const n=e(t.label,r.label);if(0!==n)return n}case 8:{const n=t,s=r,i=Math.min(n.length,s.length);for(let t=0;t<i;t++){const r=e(n[t],s[t]);if(0!==r)return r}return n.length<s.length?-1:n.length>s.length?1:0}case 9:return e(Array.from(t).sort(e),Array.from(r).sort(e));case 10:return e(Array.from(t).sort(e),Array.from(r).sort(e));case 11:return n(t,r);default:throw new Error("Invalid typeCode: "+i)}}(e,t)},e.dataview=x,e.decode=pe,e.decodeBase64=l,e.decodeWithAnnotations=function(e,t={}){return pe(e,Object.assign(Object.assign({},t),{includeAnnotations:!0}))},e.embeddedId=V,e.encode=q,e.encodeBase64=p,e.encodeDictionaryOn=Z,e.encodeSetOn=re,e.encodeWithAnnotations=function(e,t={}){return q(e,Object.assign(Object.assign({},t),{includeAnnotations:!0}))},e.floatValue=function(e){return"number"==typeof e?e:ae.isFloat(e)?e.value:NaN},e.floatlikeString=ce,e.fold=Le,e.forEachEmbedded=function(e,t){return Le(e,new Ue(t))},e.formatPosition=function(e){var t,n,r;return null===e?"<unknown>":"string"==typeof e?e:`${null!==(t=e.name)&&void 0!==t?t:""}:${null!==(n=e.line)&&void 0!==n?n:""}:${null!==(r=e.column)&&void 0!==r?r:""}:${e.pos}`},e.fromJS=Ee,e.genericEmbeddedType=je,e.genericEmbeddedTypeDecode=Te,e.genericEmbeddedTypeEncode=ve,e.hexDigit=w,e.identityEmbeddedType=qe,e.identityEmbeddedTypeDecode=Me,e.identityEmbeddedTypeEncode=j,e.is=i,e.isAnnotated=s,e.isCompound=ie,e.isEmbedded=ye,e.isPreservable=B,e.isPreserveWritable=we,e.isSequence=function(e){return Array.isArray(e)&&!("label"in e)},e.mapEmbeddeds=function(e,t){return Le(e,new ze(t))},e.merge=function(e,t,...n){function r(){throw new Error("Cannot merge items")}function s(t,n){return t===n?t:ie(t)||ie(n)||!i(t,n)?Le(t,{boolean:r,double:e=>i(t,n)?t:r(),integer:r,string:r,bytes:e=>i(t,n)?t:r(),symbol:r,record:e=>(I.isRecord(n)||r(),I(s(e.label,n.label),o(e,n))),array:e=>(Array.isArray(n)&&!I.isRecord(n)||r(),o(e,n)),set(e){r()},dictionary(e){Y.isDictionary(n)||r();const t=new Y;return e.forEach(((e,r)=>{const i=n.get(r);t.set(r,void 0===i?e:s(e,i))})),n.forEach(((n,r)=>{e.has(r)||t.set(r,n)})),t},annotated:e=>s(e,be(n)),embedded(t){ye(n)||r();const s=e(t,n);return void 0===s&&r(),s}}):t}function o(e,t){return e.length<=t.length?t.map(((t,n)=>n<e.length?s(e[n],t):t)):e.map(((e,n)=>n<t.length?s(e,t[n]):e))}return n.reduce(s,t)},e.neverEmbeddedType=Fe,e.neverEmbeddedTypeDecode=le,e.neverEmbeddedTypeEncode=_e,e.newPosition=o,e.parse=function(e,t){return new Oe(e,t).next()},e.parseAll=function(e,t){return new Oe(e,t).readToEnd()},e.peel=function(e){return ge(e,1)},e.position=function(e){return c.isAnnotated(e)?e.pos:null},e.preserves=function(e,...t){const n=[e[0]];return t.forEach(((t,r)=>{n.push(ke(t)),n.push(e[r+1])})),n.join("")},e.stringify=ke,e.stringifyEmbeddedWrite=Ae,e.strip=ge,e.typeCode=$e,e.unannotate=be,e.underlying=_,e.unhexDigit=v,e.updatePosition=a,e.valueClass=function(t){switch(typeof t){case"boolean":return e.ValueClass.Boolean;case"number":if(Number.isInteger(t))return e.ValueClass.SignedInteger;throw new Error("Non-integer number in Preserves valueClass; missing Float wrapper?");case"bigint":return e.ValueClass.SignedInteger;case"string":return e.ValueClass.String;case"symbol":return e.ValueClass.Symbol;case"object":return I.isRecord(t)?e.ValueClass.Record:Array.isArray(t)?e.ValueClass.Sequence:ne.isSet(t)?e.ValueClass.Set:Y.isDictionary(t)?e.ValueClass.Dictionary:c.isAnnotated(t)?e.ValueClass.Annotated:g.isBytes(t)?e.ValueClass.ByteString:ae.isDouble(t)?e.ValueClass.Double:e.ValueClass.Embedded;default:(e=>{throw new Error("Internal error")})()}},e.writeDictionaryOn=Q,e.writeSetOn=se})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).Preserves={})}(this,(function(t){"use strict";var e;!function(t){t[t.False=128]="False",t[t.True=129]="True",t[t.End=132]="End",t[t.Annotation=133]="Annotation",t[t.Embedded=134]="Embedded",t[t.Ieee754=135]="Ieee754",t[t.SignedInteger=176]="SignedInteger",t[t.String=177]="String",t[t.ByteString=178]="ByteString",t[t.Symbol=179]="Symbol",t[t.Record=180]="Record",t[t.Sequence=181]="Sequence",t[t.Set=182]="Set",t[t.Dictionary=183]="Dictionary"}(e||(e={}));var n=Object.freeze({__proto__:null,get Tag(){return e}});const r={};function i(t){const e=new Uint8Array(Math.floor(3*t.length/4));let n=0,i=0;for(;n<t.length;){const s=r[t[n++]],o=r[t[n++]],a=r[t[n++]],c=r[t[n++]],u=s<<18|o<<12|a<<6|c;if(e[i++]=u>>16&255,void 0===a)break;if(e[i++]=u>>8&255,void 0===c)break;e[i++]=255&u}return e.subarray(0,i)}[..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"].forEach(((t,e)=>r[t]=e)),r["+"]=r["-"]=62,r["/"]=r._=63;const s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function o(t){let e="",n=0,r=0;for(let i of t)for(n=(63&n)<<8|i,r+=8;r>=6;){r-=6;e+=s[n>>r&63]}if(r>0){e+=s[n<<6-r&63]}return e}const a=new TextEncoder,c=new TextDecoder("utf-8",{fatal:!0}),u=Symbol.for("IsPreservesBytes");class h{constructor(t=new Uint8Array){if(h.isBytes(t))this._view=t._view;else if(ArrayBuffer.isView(t))this._view=new Uint8Array(t.buffer,t.byteOffset,t.byteLength);else if(t instanceof ArrayBuffer)this._view=new Uint8Array(t);else if("string"==typeof t)this._view=a.encode(t);else if("number"==typeof t)this._view=new Uint8Array(t);else{if("number"!=typeof t.length)throw new TypeError("Attempt to initialize Bytes from unsupported value: "+t);this._view=Uint8Array.from(t)}}dataview(){return new DataView(this._view.buffer,this._view.byteOffset,this._view.byteLength)}get length(){return this._view.length}static from(t){return new h(t)}static of(...t){return new h(Uint8Array.of(...t))}static fromLatin1(t){const e=new h(t.length);for(let n=0;n<t.length;n++){const r=t.charCodeAt(n);if(r>=256)throw new Error("Codepoint out of range for 'latin1' byte encoding");e._view[n]=r}return e}static fromBase64(t){return new h(i(t))}static fromHex(t){if(1&t.length)throw new Error("Cannot decode odd-length hexadecimal string");const e=new h(t.length>>1);return h._raw_fromHexInto(t,e._view),e}static _raw_fromHexInto(t,e){const n=t.length>>1;for(let r=0;r<n;r++)e[r]=l(t.charCodeAt(r<<1))<<4|l(t.charCodeAt(1+(r<<1)))}static fromIO(t){return"string"==typeof t||h.isBytes(t)?t:new h(t)}static toIO(t){return"string"==typeof t?t:h.isBytes(t)?t._view:t}get(t){return this._view[t]}equals(t){if(!h.isBytes(t))return!1;if(t.length!==this.length)return!1;const e=this._view,n=t._view;for(let t=0;t<e.length;t++)if(e[t]!==n[t])return!1;return!0}hashCode(){const t=this._view;let e=0;for(let n=0;n<t.length;n++)e=31*e+t[n]|0;return e}static compare(t,e){return t<e?-1:e<t?1:0}static decodeUtf8(t){return c.decode(f(t))}fromUtf8(){return c.decode(this._view)}__as_preserve__(){return this}static __from_preserve__(t){return h.isBytes(t)?t:void 0}toLatin1(){return String.fromCharCode.apply(null,this._view)}toBase64(){return o(this._view)}toHex(t=d){var e=[];for(let n=0;n<this.length;n++)e.push(t(this._view[n]>>4)),e.push(t(15&this._view[n]));return e.join("")}valueOf(){return this.toHex()}__preserve_on__(t){t.state.emitbyte(e.ByteString),t.state.varint(this.length),t.state.emitbytes(this._view)}__preserve_text_on__(t){t.state.writeBytes(this._view)}get[u](){return!0}static isBytes(t){return!!(null==t?void 0:t[u])}}function d(t){return"0123456789abcdef"[t]}function l(t,e=Error){if(t>=48&&t<=57)return t-48;if(t>=97&&t<=102)return t-97+10;if(t>=65&&t<=70)return t-65+10;throw new e("Invalid hex digit: "+String.fromCharCode(t))}function f(t){return t instanceof Uint8Array?t:t._view}function p(t){return t instanceof DataView?t:t.dataview()}h.concat=function(t){let e=0;for(let n=0;n<t.length;n++)e+=f(t[n]).length;const n=new h(e);let r=0;for(let e=0;e<t.length;e++){const i=f(t[e]);n._view.set(i,r),r+=i.length}return n},function(){for(const t of"entries every find findIndex forEach includes indexOf join\nkeys lastIndexOf reduce reduceRight some toLocaleString values".split(/\s+/))h.prototype[t]=function(...e){return this._view[t](...e)};for(const t of"filter map slice subarray".split(/\s+/))h.prototype[t]=function(...e){return new h(this._view[t](...e))};for(const t of"reverse sort".split(/\s+/))h.prototype[t]=function(...e){return new h(this._view.slice()[t](...e))};h.prototype[Symbol.iterator]=function(){return this._view[Symbol.iterator]()}}();const y=Symbol.for("ErrorType");class m{static isCodecError(t,e){return(null==t?void 0:t[y])===e}}class b extends Error{get[y](){return"DecodeError"}constructor(t,e){super(t),this.pos=e}static isDecodeError(t){return m.isCodecError(t,"DecodeError")}}class g extends Error{get[y](){return"EncodeError"}static isEncodeError(t){return m.isCodecError(t,"EncodeError")}constructor(t,e){super(t),this.irritant=e}}class w extends b{get[y](){return"ShortPacket"}static isShortPacket(t){return m.isCodecError(t,"ShortPacket")}}function v(t,e){return e.label=t,e}!function(t){t.isRecord=function(t){return Array.isArray(t)&&"label"in t},t.constructorInfo=function(t){return{label:t.label,arity:t.length}},t.isClassOf=function(e,n){return t.isRecord(n)&&it(e.label,n.label)&&e.arity===n.length},t.makeConstructor=function(){return(e,n)=>{const r=(...n)=>t(e,n),i={label:e,arity:n.length};return r.constructorInfo=i,r.isClassOf=e=>t.isClassOf(i,e),r.fieldNumbers={},r._={},n.forEach(((t,e)=>{r._[t]=t=>t[e],r.fieldNumbers[t]=e})),r}}}(v||(v={}));const _=Symbol.for("IsEmbedded");function x(t){return!!(null==t?void 0:t[_])}class S{get[_](){return!0}constructor(t){this.generic=t}equals(t,e){return"object"==typeof t&&"generic"in t&&e(this.generic,t.generic)}toString(){return this.generic.toString()}}function E(t){return"object"==typeof t&&null!==t&&"__preserve_on__"in t&&"function"==typeof t.__preserve_on__}function k(t){return String.fromCharCode.apply(null,t)}let A=0;const D=new WeakMap;function j(t){let e=D.get(t);return void 0===e&&(e=A++,D.set(t,e)),e}const I={encode(t,e){new B(t,this).push(j(e))}};class C{constructor(t){this.chunks=[],this.view=new DataView(new ArrayBuffer(256)),this.index=0,this.options=t}get canonical(){var t;return null===(t=this.options.canonical)||void 0===t||t}get includeAnnotations(){var t;return null!==(t=this.options.includeAnnotations)&&void 0!==t?t:!this.canonical}contents(){if(0===this.chunks.length){const t=this.index;return this.index=0,new h(this.view.buffer.slice(0,t))}{this.rotatebuffer(4096);const t=this.chunks;return this.chunks=[],h.concat(t)}}contentsString(){if(0===this.chunks.length){const t=k(new Uint8Array(this.view.buffer,0,this.index));return this.index=0,t}{this.rotatebuffer(4096);const t=this.chunks;return this.chunks=[],t.map(k).join("")}}rotatebuffer(t){this.chunks.push(new Uint8Array(this.view.buffer,0,this.index)),this.view=new DataView(new ArrayBuffer(t)),this.index=0}makeroom(t){this.index+t>this.view.byteLength&&this.rotatebuffer(t+4096)}emitbyte(t){this.makeroom(1),this.view.setUint8(this.index++,t)}emitbytes(t){this.makeroom(t.length),new Uint8Array(this.view.buffer).set(t,this.index),this.index+=t.length}claimbytes(t){this.makeroom(t);const e=new Uint8Array(this.view.buffer,this.index,t);return this.index+=t,e}varint(t){for(;t>=128;)this.emitbyte(t%128+128),t=Math.floor(t/128);this.emitbyte(t)}encodeint(t){if("bigint"==typeof t)return this.encodebigint(t);if(this.emitbyte(e.SignedInteger),0===t)return void this.emitbyte(0);const n=(-1===t?0:Math.floor(Math.log2(t>0?t:-(1+t)))+1)+1+7>>3;this.varint(n);const r=(t,e)=>{t>0&&(r(t-1,Math.floor(e/256)),this.emitbyte(255&e))};r(n,t)}encodebigint(t){let n;if(this.emitbyte(e.SignedInteger),t>0)n=t.toString(16),1&n.length?n="0"+n:l(n.charCodeAt(0))>=8&&(n="00"+n);else{if(!(t<0))return void this.emitbyte(0);{const e=(~t).toString(16);n="";for(let t=0;t<e.length;t++)n+="fedcba9876543210"[l(e.charCodeAt(t))];1&n.length?n="f"+n:l(n.charCodeAt(0))<8&&(n="ff"+n)}}this.varint(n.length>>1),h._raw_fromHexInto(n,this.claimbytes(n.length>>1))}encodebytes(t,e){this.emitbyte(t),this.varint(e.length),this.emitbytes(e)}}class B{constructor(t={},e){var n;t instanceof C?(this.state=t,this.embeddedEncode=null!=e?e:I):(this.state=new C(t),this.embeddedEncode=null!==(n=t.embeddedEncode)&&void 0!==n?n:I)}withEmbeddedEncode(t,e){return e(new B(this.state,t)),this}get canonical(){return this.state.canonical}get includeAnnotations(){return this.state.includeAnnotations}contents(){return this.state.contents()}contentsString(){return this.state.contentsString()}grouped(t,n){this.state.emitbyte(t),n(),this.state.emitbyte(e.End)}push(t){if(E(t))t.__preserve_on__(this);else if(E(t))t.__preserve_on__(this);else if("boolean"==typeof t)this.state.emitbyte(t?e.True:e.False);else if("number"==typeof t||"bigint"==typeof t)this.state.encodeint(t);else if("string"==typeof t)this.state.encodebytes(e.String,new h(t)._view);else if("symbol"==typeof t){const n=Symbol.keyFor(t);if(void 0===n)throw new g("Cannot preserve non-global Symbol",t);this.state.encodebytes(e.Symbol,new h(n)._view)}else if(ArrayBuffer.isView(t))if(t instanceof Uint8Array)this.state.encodebytes(e.ByteString,t);else{const n=new Uint8Array(t.buffer,t.byteOffset,t.byteLength);this.state.encodebytes(e.ByteString,n)}else if(v.isRecord(t)){this.state.emitbyte(e.Record),this.push(t.label);for(let e of t)this.push(e);this.state.emitbyte(e.End)}else!function(t){return"object"==typeof t&&null!==t&&"function"==typeof t[Symbol.iterator]}(t)?x(t)?(this.state.emitbyte(e.Embedded),this.embeddedEncode.encode(this.state,t)):G(new K(t),this,((t,e)=>e.push(t)),((t,e)=>e.push(t))):this.grouped(e.Sequence,(()=>{for(let e of t)this.push(e)}));return this}}function O(t,e={}){return new B(e).push(t).contents()}const V=new B({canonical:!0});let M=!1;function T(t){if(M)return new B({canonical:!0}).push(t).contentsString();{M=!0;const e=V.push(t).contentsString();return M=!1,e}}const F=Map,q=Set,P=Symbol.for("IsMap"),N=Symbol.for("IsSet");function U(t,e){const n=t=>t.done?{done:!0,value:null}:{done:!1,value:e(t.value)};return{next:e=>n(t.next(e)),return:e=>{var r,i;return n(null!==(i=null===(r=t.return)||void 0===r?void 0:r.call(t,e))&&void 0!==i?i:{done:!0,value:null})},throw:e=>{var r,i;return n(null!==(i=null===(r=t.throw)||void 0===r?void 0:r.call(t,e))&&void 0!==i?i:{done:!0,value:null})},[Symbol.iterator](){return this}}}P in Map.prototype||(Object.defineProperty(Map.prototype,P,{get:()=>!0}),Map.isMap=t=>!!(null==t?void 0:t[P])),N in Set.prototype||(Object.defineProperty(Set.prototype,N,{get:()=>!0}),Set.isSet=t=>!!(null==t?void 0:t[N]));class R{constructor(t,e){this.canonicalizer=t,this.items=void 0===e?new Map:new Map(U(e[Symbol.iterator](),(([t,e])=>[this._key(t),[t,e]])))}_key(t){return this.canonicalizer(t)}get(t,e){const n=this.items.get(this._key(t));return void 0===n?e:n[1]}getOrSet(t,e){const n=this._key(t);let r=this.items.get(n);return void 0===r&&(r=[t,e()],this.items.set(n,r)),r[1]}set(t,e){return this.items.set(this._key(t),[t,e]),this}forEach(t,e){this.items.forEach((([n,r])=>t.call(e,r,n,this)))}entries(){return this.items.values()}keys(){return U(this.items.values(),(([t,e])=>t))}values(){return U(this.items.values(),(([t,e])=>e))}delete(t){return this.items.delete(this._key(t))}getAndDelete(t,e){const n=this._key(t),r=this.items.get(n);return void 0===r?e:(this.items.delete(n),r[1])}clear(){this.items.clear()}has(t){return this.items.has(this._key(t))}get size(){return this.items.size}[Symbol.iterator](){return this.items.values()}get[Symbol.toStringTag](){return"FlexMap"}equals(t,e=((t,e)=>t===e)){if(!("size"in t&&"has"in t&&"get"in t))return!1;if(this.size!==t.size)return!1;for(let[n,r]of this.items.values()){if(!t.has(n))return!1;if(!e(r,t.get(n)))return!1}return!0}update(t,e,n,r=((t,e)=>t===e)){const i=this._key(t);if(this.items.has(i)){const n=this.items.get(i)[1],s=e(n);return void 0===s?(this.items.delete(i),-1):(r(s,n)||this.items.set(i,[t,s]),0)}{const r=e(n);return void 0===r?0:(this.items.set(i,[t,r]),1)}}canonicalKeys(){return this.items.keys()}get[P](){return!0}}class z{constructor(t,e){this.canonicalizer=t,this.items=void 0===e?new Map:new Map(U(e[Symbol.iterator](),(t=>[this._key(t),t])))}_key(t){return this.canonicalizer(t)}has(t){return this.items.has(this._key(t))}get(t){const e=this._key(t);return this.items.has(e)?{item:this.items.get(e)}:null}add(t){return this.items.set(this._key(t),t),this}forEach(t,e){this.items.forEach((n=>t.call(e,n,n,this)))}entries(){return U(this.items.values(),(t=>[t,t]))}keys(){return this.items.values()}values(){return this.items.values()}delete(t){return this.items.delete(this._key(t))}clear(){this.items.clear()}get size(){return this.items.size}[Symbol.iterator](){return this.items.values()}get[Symbol.toStringTag](){return"FlexSet"}equals(t){if(!("size"in t)||!("has"in t))return!1;if(this.size!==t.size)return!1;for(let e of this.items.values())if(!t.has(e))return!1;return!0}canonicalValues(){return this.items.keys()}union(t){const e=new z(this.canonicalizer,this);for(let n of t)e.add(n);return e}intersect(t){const e=new z(this.canonicalizer);for(let n of this)t.has(n)&&e.add(n);return e}subtract(t){const e=new z(this.canonicalizer);for(let n of this)t.has(n)||e.add(n);return e}get[N](){return!0}}function J(t){return at.isAnnotated(t)?t.item:t}function W(e,n=1/0){return function e(n,r){if(0===r)return n;if(!at.isAnnotated(n))return n;const i=r-1;function s(t){return e(t,i)}if(v.isRecord(n.item))return v(e(n.item.label,r),n.item.map(s));if(at.isAnnotated(n.item))throw new Error("Improper annotation structure");if(0===i)return n.item;if(Array.isArray(n.item))return n.item.map(s);if(Q.isSet(n.item))return n.item.map(s);if(t.Dictionary.isDictionary(n.item)){const t=new K;return new K(n.item).forEach(((e,n)=>t.set(s(n),s(e)))),t.simplifiedValue()}return n.item}(e,n)}t.JsDictionary=void 0,function(t){function e(t,e){return t[e.description]}function n(t,e){return Object.hasOwnProperty.call(t,e.description)}function r(t){return Object.keys(t).length}function i(t){return U(Object.entries(t).values(),(([t,e])=>[Symbol.for(t),e]))}t.isJsDictionary=function(t){return"object"==typeof t&&null!==t&&null===Object.getPrototypeOf(Object.getPrototypeOf(t))&&"function"!=typeof t.__as_preserve__},t.from=function(t){const e={};for(const[n,r]of t)e[n.description]=r;return e},t.clear=function(t){for(const e in t)delete t[e]},t.remove=function(t,e){const r=n(t,e);return delete t[e.description],r},t.forEach=function(t,e){Object.entries(t).forEach((([t,n])=>e(n,Symbol.for(t))))},t.get=e,t.has=n,t.set=function(t,e,n){return t[e.description]=n,t},t.size=r,t.entries=i,t.keys=function(t){return U(Object.keys(t).values(),(t=>Symbol.for(t)))},t.values=function(t){return Object.values(t).values()},t.clone=function(t){const e={};return Object.keys(t).forEach((n=>e[n]=t[n])),e},t.equals=function(t,s,o=((t,e)=>t===e)){if(r(t)!==r(s))return!1;for(let[r,a]of i(t)){if(!n(s,r))return!1;if(!o(a,e(s,r)))return!1}return!0}}(t.JsDictionary||(t.JsDictionary={}));const H=Symbol.for("DictionaryType");class L extends R{constructor(t,e,n){super((e=>T(t(e))),n),this.encodeK=t,this.encodeV=e}__preserve_on__(t){G(this,t,((t,e)=>e.push(this.encodeK(t))),((t,e)=>e.push(this.encodeV(t))))}__preserve_text_on__(t){X(this,t,((t,e)=>e.push(this.encodeK(t))),((t,e)=>e.push(this.encodeV(t))))}}class $ extends L{get[H](){return"Dictionary"}static isKeyedDictionary(t){return"Dictionary"===(null==t?void 0:t[H])}constructor(t){super((t=>t),(t=>t),t)}clone(){return new $(this)}get[Symbol.toStringTag](){return"Dictionary"}equals(e,n=((t,e)=>t===e)){const r=t.Dictionary.asMap(e);return!!r&&super.equals(r,n)}}class K{get[P](){return!0}constructor(t){void 0===t?(this.j={},this.k=void 0):H in t?(this.j=void 0,this.k=t):(this.j=t,this.k=void 0)}static from(t){const e=new K;for(const[n,r]of t)e.set(n,r);return e}clear(){this.j?t.JsDictionary.clear(this.j):this.k.clear()}delete(e){return this.j?"symbol"==typeof(e=J(e))&&t.JsDictionary.remove(this.j,e):this.k.delete(e)}forEach(e,n){this.j?t.JsDictionary.forEach(this.j,((t,r)=>e.call(n,t,r,this))):this.k.forEach(e,n)}get(e){if(this.j){if("symbol"!=typeof(e=J(e)))return;return t.JsDictionary.get(this.j,e)}return this.k.get(e)}has(e){return this.j?"symbol"==typeof(e=J(e))&&t.JsDictionary.has(this.j,e):this.k.has(e)}set(e,n){if(this.j){if("symbol"==typeof e)return t.JsDictionary.set(this.j,e,n),this;this.k=new $(t.JsDictionary.entries(this.j)),this.j=void 0}return this.k.set(e,n),this}get size(){return this.j?t.JsDictionary.size(this.j):this.k.size}entries(){return this.j?t.JsDictionary.entries(this.j):this.k.entries()}keys(){return this.j?t.JsDictionary.keys(this.j):this.k.keys()}values(){return this.j?t.JsDictionary.values(this.j):this.k.values()}[Symbol.iterator](){return this.entries()}get[Symbol.toStringTag](){return"DictionaryMap"}clone(){return new K(this.j?t.JsDictionary.clone(this.j):this.k.clone())}get value(){var t;return null!==(t=this.j)&&void 0!==t?t:this.k}simplify(){if(!this.j){const t={};for(const[e,n]of this.k.entries()){if("symbol"!=typeof e)return;t[e.description]=n}this.j=t,this.k=void 0}}simplifiedValue(){return this.simplify(),this.value}asJsDictionary(){if(this.simplify(),!this.j)throw new Error("Cannot represent general dictionary as JsDictionary");return this.j}asKeyedDictionary(){var e;return null!==(e=this.k)&&void 0!==e?e:new $(t.JsDictionary.entries(this.j))}}function G(t,n,r,i){if(n.canonical){const s=Array.from(t),o=new B({canonical:!0,embeddedEncode:n.embeddedEncode}),a=s.map((([t,e],n)=>(r(t,o),[o.contents(),n])));a.sort(((t,e)=>h.compare(t[0],e[0]))),n.grouped(e.Dictionary,(()=>a.forEach((([t,e])=>{const[o,a]=s[e];r(o,n),i(a,n)}))))}else n.grouped(e.Dictionary,(()=>t.forEach(((t,e)=>{r(e,n),i(t,n)}))))}function X(t,e,n,r){e.state.writeSeq("{","}",t.entries(),(([t,i])=>{n(t,e),at.isAnnotated(i)&&ut(i).length>1&&e.state.isIndenting?(e.state.pieces.push(":"),e.state.indentCount++,e.state.writeIndent(),r(i,e),e.state.indentCount--):(e.state.pieces.push(": "),r(i,e))}))}t.Dictionary=void 0,function(e){function n(e){if("object"!=typeof e||null===e)return!1;switch(e[H]){case"Dictionary":return!0;case void 0:return t.JsDictionary.isJsDictionary(e);default:return!1}}e.isDictionary=n,e.asMap=function(t){return n(t)?new K(t):void 0},e.from=function(t){return K.from(t).simplifiedValue()},e.__from_preserve__=function(t){return e.isDictionary(t)?t:void 0}}(t.Dictionary||(t.Dictionary={}));class Y extends z{constructor(t,e){super((e=>T(t(e))),e),this.encodeV=t}__preserve_on__(t){tt(this,t,((t,e)=>e.push(this.encodeV(t))))}__preserve_text_on__(t){et(this,t,((t,e)=>e.push(this.encodeV(t))))}}class Z extends Y{get[H](){return"Set"}static isKeyedSet(t){return"Set"===(null==t?void 0:t[H])}constructor(t){super((t=>t),t)}map(t){return new Z(U(this[Symbol.iterator](),t))}filter(t){const e=new Z;for(let n of this)t(n)&&e.add(n);return e}clone(){return new Z(this)}get[Symbol.toStringTag](){return"Set"}}let Q=class t extends Z{static isSet(t){return"Set"===(null==t?void 0:t[H])}static __from_preserve__(e){return t.isSet(e)?e:void 0}};function tt(t,n,r){if(n.canonical){const i=new B({canonical:!0,embeddedEncode:n.embeddedEncode}),s=Array.from(t).map((t=>(r(t,i),[i.contents(),t])));s.sort(((t,e)=>h.compare(t[0],e[0]))),n.grouped(e.Set,(()=>s.forEach((([t,e])=>r(e,n)))))}else n.grouped(e.Set,(()=>t.forEach((t=>r(t,n)))))}function et(t,e,n){e.state.writeSeq("#{","}",t,(t=>n(t,e)))}const nt=Symbol.for("IsPreservesAnnotated");function rt(t){return!!(null==t?void 0:t[nt])}function it(e,n){if(rt(e)&&(e=e.item),rt(n)&&(n=n.item),Object.is(e,n))return!0;if(typeof e!=typeof n)return("number"==typeof e&&"bigint"==typeof n||"bigint"==typeof e&&"number"==typeof n)&&e==n;if("object"==typeof e){if(null===e||null===n)return!1;if("equals"in e&&"function"==typeof e.equals)return e.equals(n,it);if(Array.isArray(e)&&Array.isArray(n)){const t="label"in e;if(t!=="label"in n)return!1;if(t&&!it(e.label,n.label))return!1;if(e.length!==n.length)return!1;for(let t=0;t<e.length;t++)if(!it(e[t],n[t]))return!1;return!0}{const r=t.Dictionary.asMap(e),i=t.Dictionary.asMap(n);if(!r||!i)return!1;if(r.size!==i.size)return!1;for(const t of r.keys()){if(!i.has(t))return!1;if(!it(r.get(t),i.get(t)))return!1}return!0}}return!1}function st(t){return{line:1,column:0,pos:0,name:t}}function ot(t,e){if(t.pos++,void 0===t.line)return!1;{let n=!1;switch(e){case"\t":t.column=t.column+8&-8;break;case"\n":t.column=0,t.line++,n=!0;break;case"\r":t.column=0;break;default:t.column++}return n}}class at{constructor(t,e){this.annotations=[],this.pos=null!=e?e:null,this.item=t}__as_preserve__(){return this}static __from_preserve__(t){return rt(t)?t:void 0}__preserve_on__(t){if(t.includeAnnotations)for(const n of this.annotations)t.state.emitbyte(e.Annotation),t.push(n);t.push(this.item)}__preserve_text_on__(t){if(t.includeAnnotations){const e=this.annotations.length<=1;for(const n of this.annotations)t.state.pieces.push("@"),t.push(n),e?t.state.pieces.push(" "):t.state.writeIndentSpace()}t.push(this.item)}equals(t){return it(this.item,at.isAnnotated(t)?t.item:t)}get[nt](){return!0}static isAnnotated(t){return rt(t)}}function ct(t,...e){const n=at.isAnnotated(t)?t:new at(t);return e.forEach((t=>n.annotations.push(t))),n}function ut(t){return at.isAnnotated(t)?t.annotations:[]}function ht(e){return Array.isArray(e)||Q.isSet(e)||t.Dictionary.isDictionary(e)}const dt=Symbol.for("FloatType");class lt{constructor(t){this.value="number"==typeof t?t:t.value}__preserve_text_on__(t){t.state.pieces.push(this.toString())}equals(t){return!!Object.is(t.constructor,this.constructor)&&(Number.isNaN(this.value)&&Number.isNaN(t.value)?t.toBytes().equals(this.toBytes()):Object.is(t.value,this.value))}hashCode(){return 0|this.value}}function ft(t){if(Object.is(t,-0))return"-0.0";const e=""+t;return e.includes(".")||e.includes("e")||e.includes("E")?e:e+".0"}lt.isFloat=t=>void 0!==(null==t?void 0:t[dt]),lt.isDouble=t=>"Double"===(null==t?void 0:t[dt]);class pt extends lt{__as_preserve__(){return this}static fromBytes(t){return new pt(p(t).getFloat64(0,!1))}static __from_preserve__(t){return lt.isDouble(t)?t:void 0}__preserve_on__(t){t.state.emitbyte(e.Ieee754),t.state.emitbyte(8),t.state.makeroom(8),t.state.view.setFloat64(t.state.index,this.value,!1),t.state.index+=8}toBytes(){const t=new h(8);return t.dataview().setFloat64(0,this.value,!1),t}toString(){return Number.isFinite(this.value)?ft(this.value):'#xd"'+this.toBytes().toHex()+'"'}get[dt](){return"Double"}}function yt(t){return new pt(t)}const mt=BigInt(Number.MAX_SAFE_INTEGER),bt=BigInt(Number.MIN_SAFE_INTEGER),gt=/^([-+]?\d+)((\.\d+([eE][-+]?\d+)?)|([eE][-+]?\d+))?$/;class wt{constructor(t,e){switch(this.discarded=0,this.buffer=t,typeof e.name){case"undefined":this.pos=st();break;case"string":this.pos=st(e.name);break;case"object":this.pos=Object.assign({},e.name)}this.index=0,this.options=e}error(t,e){throw new b(t,Object.assign({},e))}get includeAnnotations(){var t;return null!==(t=this.options.includeAnnotations)&&void 0!==t&&t}copyPos(){return Object.assign({},this.pos)}write(t){this.atEnd()?this.buffer=t:this.buffer=this.buffer.substring(this.index)+t,this.discarded+=this.index,this.index=0}atEnd(){return this.index>=this.buffer.length}peek(){if(this.atEnd())throw new w("Short term",this.pos);return this.buffer[this.index]}advance(){const t=this.index++;return ot(this.pos,this.buffer[t]),t}nextchar(){if(this.atEnd())throw new w("Short term",this.pos);return this.buffer[this.advance()]}nextcharcode(){if(this.atEnd())throw new w("Short term",this.pos);return this.buffer.charCodeAt(this.advance())}skipws(t=!1){for(;!this.atEnd();){const e=this.peek();if(!(xt(e)||t&&","===e))break;this.advance()}}readHex2(){return l(this.nextcharcode(),b)<<4|l(this.nextcharcode(),b)}readHex4(){return l(this.nextcharcode(),b)<<12|l(this.nextcharcode(),b)<<8|l(this.nextcharcode(),b)<<4|l(this.nextcharcode(),b)}readHexBinary(){const t=[];for(;;){if(this.skipws(),'"'===this.peek())return this.advance(),h.from(t);t.push(this.readHex2())}}readHexFloat(){const t=this.copyPos();'"'!==this.nextchar()&&this.error("Missing open-double-quote in hex-encoded floating-point number",t);const e=this.readHexBinary();return 8!==e.length&&this.error("Incorrect number of bytes in hex-encoded Double",t),pt.fromBytes(e)}readBase64Binary(){let t="";for(;;){this.skipws();const e=this.nextchar();if("]"===e)break;t+=e}return h.fromBase64(t)}requireDelimiter(t){this.delimiterFollows()||this.error(`Delimiter must follow ${t}`,this.pos)}delimiterFollows(){if(this.atEnd())return!0;const t=this.peek();return-1!=='(){}[]<>";,@#:|'.indexOf(t)||xt(t)}readRawSymbolOrNumber(t){for(;!this.delimiterFollows();)t+=this.nextchar();const e=gt.exec(t);if(e){if(void 0===e[2]){let t=BigInt(e[1]);return t<=bt||t>=mt?t:Number(t)}return yt(parseFloat(t))}return Symbol.for(t)}readStringlike(t,e,n,r,i){let s=[];for(;;){const o=this.nextchar();switch(o){case n:return e(s);case"\\":{const e=this.nextchar();switch(e){case r:s.push(i());break;case n:case"\\":case"/":s.push(t(e));break;case"b":s.push(t("\b"));break;case"f":s.push(t("\f"));break;case"n":s.push(t("\n"));break;case"r":s.push(t("\r"));break;case"t":s.push(t("\t"));break;default:this.error(`Invalid escape code \\${e}`,this.pos)}break}default:s.push(t(o))}}}readString(t){return this.readStringlike((t=>t),(t=>t.join("")),t,"u",(()=>{const t=this.readHex4();if(t>=55296&&t<=57343){if("\\"===this.nextchar()&&"u"===this.nextchar()){const e=this.readHex4();if(e>=56320&&e<=57343&&t<=56319)return String.fromCharCode(t,e)}this.error("Invalid surrogate pair",this.pos)}return String.fromCharCode(t)}))}readLiteralBinary(){return this.readStringlike((t=>{const e=t.charCodeAt(0);return e>=256&&this.error(`Invalid code point ${e} in literal binary`,this.pos),e}),h.from,'"',"x",(()=>this.readHex2()))}}const vt={decode(t){return new S(new Vt(t,this).next())},fromValue:(t,e)=>new S(e.includeAnnotations?t:W(t))};class _t{constructor(t="",e){var n,r;if(t instanceof wt)this.state=t,this.embeddedType=e;else{const i=null!==(n=e)&&void 0!==n?n:{};this.state=new wt(t,i),this.embeddedType=null!==(r=i.embeddedDecode)&&void 0!==r?r:Ot}}write(t){this.state.write(t)}readCommentLine(){const t=this.state.copyPos();let e="";for(;;){const n=this.state.nextchar();if("\n"===n||"\r"===n)return this.wrap(e,t);e+=n}}wrap(t,e){return this.state.includeAnnotations&&!at.isAnnotated(t)&&(t=new at(t,e)),t}annotateNextWith(t){if(this.state.skipws(),this.state.atEnd())throw new b("Trailing annotations and comments are not permitted",this.state.pos);const e=this.next();return this.state.includeAnnotations&&e.annotations.unshift(t),e}readToEnd(){const t=[];for(;;){if(this.state.skipws(),this.state.atEnd())return t;t.push(this.next())}}next(){this.state.skipws();const t=this.state.copyPos(),e=(()=>{const e=this.state.nextchar();switch(e){case'"':return this.state.readString('"');case"|":return Symbol.for(this.state.readString("|"));case";":this.state.error("Semicolon is reserved syntax",t);case"@":return this.annotateNextWith(this.next());case":":this.state.error("Unexpected key/value separator between items",t);case"#":{const e=this.state.nextchar();switch(e){case" ":case"\t":return this.annotateNextWith(this.readCommentLine());case"\n":case"\r":return this.annotateNextWith("");case"!":return this.annotateNextWith(v(Symbol.for("interpreter"),[this.readCommentLine()]));case"f":return this.state.requireDelimiter("#f"),!1;case"t":return this.state.requireDelimiter("#t"),!0;case"{":return this.readSet();case'"':return this.state.readLiteralBinary();case"x":switch(this.state.nextchar()){case'"':return this.state.readHexBinary();case"d":return this.state.readHexFloat();default:this.state.error("Invalid #x syntax",t)}case"[":return this.state.readBase64Binary();case":":return this.embeddedType.fromValue(new _t(this.state,vt).next(),this.state.options);default:this.state.error(`Invalid # syntax: ${e}`,t)}}case"<":return v(this.next(),this.readSequence(">",!1));case"[":return this.readSequence("]",!0);case"{":return this.readDictionary();case">":this.state.error("Unexpected >",t);case"]":this.state.error("Unexpected ]",t);case"}":this.state.error("Unexpected }",t);case",":this.state.error("Unexpected ,",t);default:return this.state.readRawSymbolOrNumber(e)}})();return this.wrap(e,t)}seq(t,e,n,r){for(;;){if(this.state.skipws(t),this.state.peek()===r)return this.state.advance(),e;n(this.next(),e)}}readSequence(t,e){return this.seq(e,[],((t,e)=>e.push(t)),t)}readDictionary(){return this.seq(!0,new K,((t,e)=>{if(this.state.skipws(),":"===this.state.peek())this.state.advance(),e.has(t)&&this.state.error(`Duplicate key: ${Ct(t)}`,this.state.pos),e.set(t,this.next());else this.state.error("Missing key/value separator",this.state.pos)}),"}").simplifiedValue()}readSet(){return this.seq(!0,new Q,((t,e)=>{e.has(t)&&this.state.error(`Duplicate value in set: ${Ct(t)}`,this.state.pos),e.add(t)}),"}")}}function xt(t){return-1!==" \t\n\r".indexOf(t)}function St(t){return"object"==typeof t&&null!==t&&"__preserve_text_on__"in t&&"function"==typeof t.__preserve_text_on__}const Et={encode(t,e){new B(t,this).push(e.generic)},toValue:t=>t.generic},kt={encode(t,e){throw new Error("Embeddeds not permitted encoding Preserves document")},toValue(t){throw new Error("Embeddeds not permitted writing Preserves document")}};class At{constructor(t){var e;this.pieces=[],this.indentCount=0,this.options=t,this.indentDelta=" ".repeat(null!==(e=t.indent)&&void 0!==e?e:0)}get isIndenting(){return this.indentDelta.length>0}get includeAnnotations(){var t;return null===(t=this.options.includeAnnotations)||void 0===t||t}writeIndent(){if(this.isIndenting){this.pieces.push("\n");for(let t=0;t<this.indentCount;t++)this.pieces.push(this.indentDelta)}}writeIndentSpace(){this.isIndenting?this.writeIndent():this.pieces.push(" ")}escapeStringlikeChar(t,e=(t=>t)){switch(t){case"\\":return"\\\\";case"\b":return"\\b";case"\f":return"\\f";case"\n":return"\\n";case"\r":return"\\r";case"\t":return"\\t";default:return e(t)}}escapeStringlike(t,e){let n=e;for(let r of t)n+=r===e?"\\"+e:this.escapeStringlikeChar(r);return n+e}writeSeq(t,e,n,r){let i=n[Symbol.iterator]();this.pieces.push(t);const s=i.next();if(!0!==s.done){const t=s.value,e=i.next();if(!0===e.done)r(t);else{let n;for(this.indentCount++,this.writeIndent(),r(t),this.writeIndentSpace(),r(e.value);!0!==(n=i.next()).done;)this.writeIndentSpace(),r(n.value);this.indentCount--,this.writeIndent()}}this.pieces.push(e)}writeBytes(t){var e,n;const r=null!==(e=this.options.maxBinaryAsciiLength)&&void 0!==e?e:1024,i=null!==(n=this.options.maxBinaryAsciiProportion)&&void 0!==n?n:.75;if(t.length>=r)this.writeBase64(t);else{let e=0,n=Math.min(t.length,r);for(let r=0;r<n;r++){const n=t[r];switch(n){case 9:case 10:case 13:e++;break;default:n>=32&&n<=126&&e++}}0===n||e/n>=i?this.writeBinaryStringlike(t):this.writeBase64(t)}}writeBase64(t){this.pieces.push("#[",o(t),"]")}writeBinaryStringlike(t){let e='#"';for(let n of t)e+=34===n?'\\"':this.escapeStringlikeChar(String.fromCharCode(n),(t=>n>=32&&n<=126&&92!==n?t:"\\x"+("0"+n.toString(16)).slice(-2)));this.pieces.push(e+'"')}couldBeFlat(t){let e=!1;for(let n of t){if(Array.isArray(n)||Set.isSet(n)||Map.isMap(n)){if(e)return!1;e=!0}if(this.includeAnnotations&&rt(n)&&n.annotations.length>1)return!1}return!0}}class Dt{constructor(t={},e){var n;t instanceof At?(this.state=t,this.embeddedWrite=e):(this.state=new At(t),this.embeddedWrite=null!==(n=t.embeddedWrite)&&void 0!==n?n:kt)}static stringify(t,e){const n=new Dt(e);return n.push(t),n.contents()}contents(){return this.state.pieces.join("")}get includeAnnotations(){return this.state.includeAnnotations}push(t){switch(typeof t){case"boolean":this.state.pieces.push(t?"#t":"#f");break;case"string":this.state.pieces.push(this.state.escapeStringlike(t,'"'));break;case"symbol":{const e=t.description;/^[-a-zA-Z0-9~!$%^&*?_=+/.]+$/.exec(e)&&!gt.exec(e)?this.state.pieces.push(e):this.state.pieces.push(this.state.escapeStringlike(e,"|"));break}case"bigint":case"number":this.state.pieces.push(""+t);break;case"object":if(null===t)throw new Error("Cannot encode null in Preserves Writer.push");if(St(t))t.__preserve_text_on__(this);else if(St(t))t.__preserve_text_on__(this);else if(ArrayBuffer.isView(t))if(t instanceof Uint8Array)this.state.writeBytes(t);else{const e=new Uint8Array(t.buffer,t.byteOffset,t.byteLength);this.state.writeBytes(e)}else if(v.isRecord(t)){const e=this.state.couldBeFlat(t);this.state.pieces.push("<"),this.push(t.label),e||this.state.indentCount++;for(let n of t)e?this.state.pieces.push(" "):this.state.writeIndentSpace(),this.push(n);e||this.state.indentCount--,this.state.pieces.push(">")}else!function(t){return"object"==typeof t&&null!==t&&"function"==typeof t[Symbol.iterator]}(t)?x(t)?(this.state.pieces.push("#:"),"write"in this.embeddedWrite?this.embeddedWrite.write(this.state,t):new Dt(this.state,Et).push(this.embeddedWrite.toValue(t))):X(new K(t),this,((t,e)=>e.push(t)),((t,e)=>e.push(t))):this.state.writeSeq("[","]",t,(t=>this.push(t)));break;default:(e=>{throw new Error(`Internal error: unhandled in Preserves Writer.push for ${t}`)})()}return this}}function jt(t){switch(typeof t){case"number":if(!Number.isInteger(t))throw new TypeError("Refusing to autoconvert non-integer number to Double");case"bigint":case"string":case"symbol":case"boolean":return t;case"undefined":case"function":break;case"object":if(null===t)break;if("function"==typeof t.__as_preserve__)return t.__as_preserve__();if(v.isRecord(t))return t;if(Array.isArray(t))return t.map(jt);if(ArrayBuffer.isView(t)||t instanceof ArrayBuffer)return h.from(t);if(Map.isMap(t)){const e=new $;return t.forEach(((t,n)=>e.set(jt(n),jt(t)))),e}if(Q.isSet(t)){const e=new Q;return t.forEach((t=>e.add(jt(t)))),e}if(x(t))return t;{const e={};return Object.entries(t).forEach((([t,n])=>e[t]=jt(n))),e}}throw new TypeError("Cannot represent JavaScript value as Preserves: "+t)}t.Dictionary.stringMap=function(t){const e=new $;return Object.entries(t).forEach((([t,n])=>e.set(t,jt(n)))),e};const It={write(t,e){if(e instanceof S)new Dt(t,this).push(e.generic);else{try{const n=jt(e);if(!x(n))return void new Dt(t,this).push(n)}catch(t){}try{return void t.pieces.push(JSON.stringify(e))}catch(t){}t.pieces.push("⌜"+e+"⌝")}}};function Ct(t,e){var n;return(e=Object.assign({},null!=e?e:{})).embeddedWrite=null!==(n=e.embeddedWrite)&&void 0!==n?n:It,Dt.stringify(jt(t),e)}[at,h,$,Z].forEach((t=>{t.prototype.toString=function(){return Ct(this)}}));class Bt{constructor(t,e){this.index=0,this.packet=f(t),this.options=e}get includeAnnotations(){var t;return null!==(t=this.options.includeAnnotations)&&void 0!==t&&t}write(t){this.index===this.packet.length?this.packet=f(t):this.packet=h.concat([this.packet.slice(this.index),t])._view,this.index=0}atEnd(){return this.index>=this.packet.length}mark(){return this.index}restoreMark(t){this.index=t}shortGuard(t,e){if(this.atEnd())return e();const n=this.mark();try{return t()}catch(t){if(w.isShortPacket(t))return this.restoreMark(n),e();throw t}}nextbyte(){if(this.atEnd())throw new w("Short packet");return this.packet[this.index++]}nextbytes(t){const e=this.index;if(this.index+=t,this.index>this.packet.length)throw new w("Short packet");return new DataView(this.packet.buffer,this.packet.byteOffset+e,t)}varint(){const t=this.nextbyte();return t<128?t:(this.varint()<<7)+(t-128)}peekend(){return this.nextbyte()===e.End||(this.index--,!1)}nextint(t){const e=this.index;if(0===t)return 0;if(t>7)return this.nextbigint(t);if(7===t){const e=this.packet[this.index];if(e>=32&&e<224)return this.nextbigint(t)}let n=this.nextbyte();128&n&&(n-=256);for(let e=1;e<t;e++)n=256*n+this.nextbyte();return Number.isSafeInteger(n)?n:(this.index=e,this.nextbigint(t))}nextbigint(t){if(0===t)return BigInt(0);const e=h.from(this.nextbytes(t));if(e.get(0)>=128){const t=e.toHex((t=>d(15-t)));return~BigInt("0x"+t)}{const t=e.toHex();return BigInt("0x"+t)}}wrap(t){return this.includeAnnotations?new at(t):t}unshiftAnnotation(t,e){return this.includeAnnotations&&e.annotations.unshift(t),e}}const Ot={decode(t){throw new Error("Embeddeds not permitted at this point in Preserves document")},fromValue(t,e){throw new Error("Embeddeds not permitted at this point in Preserves document")}};class Vt{constructor(t=new Uint8Array(0),e){var n,r,i;if(t instanceof Bt)this.state=t,this.embeddedDecode=null!==(n=e)&&void 0!==n?n:Ot;else{const n=null!==(r=e)&&void 0!==r?r:{};this.state=new Bt(t,n),this.embeddedDecode=null!==(i=n.embeddedDecode)&&void 0!==i?i:Ot}}write(t){this.state.write(t)}nextvalues(){const t=[];for(;!this.state.peekend();)t.push(this.next());return t}static dictionaryFromArray(t){const e=new K;if(t.length%2)throw new b("Missing dictionary value");for(let n=0;n<t.length;n+=2){if(e.has(t[n]))throw new b(`Duplicate key: ${Ct(t[n])}`);e.set(t[n],t[n+1])}return e.simplifiedValue()}next(){const t=this.state.nextbyte();switch(t){case e.False:return this.state.wrap(!1);case e.True:return this.state.wrap(!0);case e.End:throw new b("Unexpected Compound end marker");case e.Annotation:{const t=this.next(),e=this.next();return this.state.unshiftAnnotation(t,e)}case e.Embedded:return this.state.wrap(this.embeddedDecode.decode(this.state));case e.Ieee754:if(8===this.state.varint())return this.state.wrap(pt.fromBytes(this.state.nextbytes(8)));throw new b("Invalid IEEE754 size");case e.SignedInteger:return this.state.wrap(this.state.nextint(this.state.varint()));case e.String:return this.state.wrap(h.from(this.state.nextbytes(this.state.varint())).fromUtf8());case e.ByteString:return this.state.wrap(h.from(this.state.nextbytes(this.state.varint())));case e.Symbol:return this.state.wrap(Symbol.for(h.from(this.state.nextbytes(this.state.varint())).fromUtf8()));case e.Record:{const t=this.nextvalues();if(0===t.length)throw new b("Too few elements in encoded record");return this.state.wrap(v(t[0],t.slice(1)))}case e.Sequence:return this.state.wrap(this.nextvalues());case e.Set:{const t=new Q;for(const e of this.nextvalues()){if(t.has(e))throw new b(`Duplicate value: ${Ct(e)}`);t.add(e)}return this.state.wrap(t)}case e.Dictionary:return this.state.wrap(Vt.dictionaryFromArray(this.nextvalues()));default:throw new b("Unsupported Preserves tag: "+t)}}try_next(){return this.state.shortGuard((()=>this.next()),(()=>{}))}atEnd(){return this.state.atEnd()}mark(){return this.state.mark()}restoreMark(t){return this.state.restoreMark(t)}skip(){this.next()}withEmbeddedDecode(t,e){return e(new Vt(this.state,t))}skipAnnotations(t){const n=this.mark();for(;!this.state.atEnd()&&this.state.packet[this.state.index]===e.Annotation;)this.state.index++,this.skip();return t((()=>this.restoreMark(n)))}nextBoolean(){return this.skipAnnotations((t=>{switch(this.state.nextbyte()){case e.False:return!1;case e.True:return!0;default:return t()}}))}nextDouble(){return this.skipAnnotations((t=>this.state.nextbyte()!==e.Ieee754||8!==this.state.nextbyte()?t():pt.fromBytes(this.state.nextbytes(8))))}nextEmbedded(){return this.skipAnnotations((t=>this.state.nextbyte()===e.Embedded?this.embeddedDecode.decode(this.state):t()))}nextSignedInteger(){return this.skipAnnotations((t=>this.state.nextbyte()===e.SignedInteger?this.state.nextint(this.state.varint()):t()))}nextString(){return this.skipAnnotations((t=>this.state.nextbyte()===e.String?h.from(this.state.nextbytes(this.state.varint())).fromUtf8():t()))}nextByteString(){return this.skipAnnotations((t=>this.state.nextbyte()===e.ByteString?h.from(this.state.nextbytes(this.state.varint())):t()))}nextSymbol(){return this.skipAnnotations((t=>this.state.nextbyte()===e.Symbol?Symbol.for(h.from(this.state.nextbytes(this.state.varint())).fromUtf8()):t()))}openRecord(){return this.skipAnnotations((t=>this.state.nextbyte()===e.Record||(t(),!1)))}openSequence(){return this.skipAnnotations((t=>this.state.nextbyte()===e.Sequence||(t(),!1)))}openSet(){return this.skipAnnotations((t=>this.state.nextbyte()===e.Set||(t(),!1)))}openDictionary(){return this.skipAnnotations((t=>this.state.nextbyte()===e.Dictionary||(t(),!1)))}closeCompound(){return this.state.peekend()}}function Mt(t,e={}){return new Vt(t,e).next()}const Tt=Object.assign({},vt,Et),Ft=Object.assign({},Ot,kt),qt={decode(t){throw new Error("Cannot decode identityEmbeddedType")},fromValue(t,e){throw new Error("Cannot decode identityEmbeddedType")}},Pt=Object.assign({},qt,I);var Nt;t.ValueClass=void 0,(Nt=t.ValueClass||(t.ValueClass={}))[Nt.Boolean=0]="Boolean",Nt[Nt.Double=1]="Double",Nt[Nt.SignedInteger=2]="SignedInteger",Nt[Nt.String=3]="String",Nt[Nt.ByteString=4]="ByteString",Nt[Nt.Symbol=5]="Symbol",Nt[Nt.Record=6]="Record",Nt[Nt.Sequence=7]="Sequence",Nt[Nt.Set=8]="Set",Nt[Nt.Dictionary=9]="Dictionary",Nt[Nt.Embedded=10]="Embedded",Nt[Nt.Annotated=11]="Annotated";class Ut{boolean(t){}double(t){}integer(t){}string(t){}bytes(t){}symbol(t){}record(t,e){e(t.label),t.forEach(e)}array(t,e){t.forEach(e)}set(t,e){t.forEach(e)}dictionary(t,e){t.forEach(((t,n)=>{e(n),e(t)}))}annotated(t,e){e(t.item),t.annotations.forEach(e)}embedded(t,e){}}class Rt extends Ut{constructor(t){super(),this.f=t}embedded(t,e){this.f(t,e)}}class zt{boolean(t){return t}double(t){return yt(t)}integer(t){return t}string(t){return t}bytes(t){return t}symbol(t){return t}record(t,e){return v(e(t.label),t.map(e))}array(t,e){return t.map(e)}set(t,e){return t.map(e)}dictionary(t,e){const n=new K;return t.forEach(((t,r)=>n.set(e(r),e(t)))),n.simplifiedValue()}annotated(t,e){return ct(e(t.item),...t.annotations.map(e))}}class Jt extends zt{embedded(t,e){return t}}class Wt extends zt{constructor(t){super(),this.f=t}embedded(t,e){return this.f(t)}}const Ht=new Jt;function Lt(e,n){const r=e=>{switch(typeof e){case"boolean":return n.boolean(e);case"number":return Number.isInteger(e)?n.integer(e):n.double(e);case"bigint":return n.integer(e);case"string":return n.string(e);case"symbol":return n.symbol(e);case"object":if(v.isRecord(e))return n.record(e,r);if(Array.isArray(e))return n.array(e,r);if(Q.isSet(e))return n.set(e,r);if(x(e))return n.embedded(e,r);if(at.isAnnotated(e))return n.annotated(e,r);if(h.isBytes(e))return n.bytes(e);if(lt.isDouble(e))return n.double(e.value);if(t.Dictionary.isDictionary(e))return n.dictionary(new K(e),r);default:(t=>{throw new Error("Internal error")})()}};return r(e)}function $t(e){switch(rt(e)&&(e=e.item),typeof e){case"boolean":return 0;case"number":case"bigint":return 3;case"string":return 4;case"symbol":return 6;case"object":if(lt.isFloat(e))return 2;if(h.isBytes(e))return 5;if(Array.isArray(e))return"label"in e?7:8;if(Q.isSet(e))return 9;if(t.Dictionary.isDictionary(e))return 10;if(x(e))return 11;default:throw new Error("Invalid Value<T> in typeCode")}}Array.__from_preserve__=t=>Array.isArray(t)?t:void 0;const Kt=Array,Gt=Symbol;t.Annotated=at,t.Array=Kt,t.Bytes=h,t.Constants=n,t.DecodeError=b,t.Decoder=Vt,t.DecoderState=Bt,t.DictionaryMap=K,t.DictionaryType=H,t.Double=yt,t.DoubleFloat=pt,t.Embedded=class{get[_](){return!0}constructor(t){this.value=t}equals(t){return"object"==typeof t&&"value"in t&&Object.is(this.value,t.value)}},t.EncodableDictionary=L,t.EncodableSet=Y,t.EncodeError=g,t.Encoder=B,t.EncoderState=C,t.ErrorType=y,t.FlexMap=R,t.FlexSet=z,t.Float=lt,t.FloatType=dt,t.ForEachEmbedded=Rt,t.GenericEmbedded=S,t.IDENTITY_FOLD=Ht,t.IdentityFold=Jt,t.IdentityMap=F,t.IdentitySet=q,t.IsEmbedded=_,t.IsMap=P,t.IsPreservesAnnotated=nt,t.IsPreservesBytes=u,t.IsSet=N,t.KeyedDictionary=$,t.KeyedSet=Z,t.MapFold=Wt,t.NUMBER_RE=gt,t.PreservesCodecError=m,t.Reader=_t,t.ReaderState=wt,t.Record=v,t.Set=Q,t.ShortPacket=w,t.Symbol=Gt,t.ValueFold=zt,t.VoidFold=Ut,t.Writer=Dt,t.WriterState=At,t._iterMap=U,t.annotate=ct,t.annotations=ut,t.asLiteral=function(t,e){return it(t,e)?e:void 0},t.canonicalEncode=function(t,e){if(void 0!==e||M)return O(t,Object.assign(Object.assign({},e),{canonical:!0}));{M=!0;const e=V.push(t).contents();return M=!1,e}},t.canonicalString=T,t.compare=function(e,n,r=((t,e)=>it(t,e)?0:t<e?-1:1)){return function e(n,i){rt(n)&&(n=n.item),rt(i)&&(i=i.item);const s=$t(n),o=$t(i);if(s<o)return-1;if(s>o)return 1;switch(s){case 0:case 3:case 4:return n<i?-1:n>i?1:0;case 2:{const t=n.value,e=i.value;return t<e?-1:t>e?1:0}case 5:return h.compare(n,i);case 6:{const t=n.description,e=i.description;return t<e?-1:t>e?1:0}case 7:{const t=e(n.label,i.label);if(0!==t)return t}case 8:{const t=n,r=i,s=Math.min(t.length,r.length);for(let n=0;n<s;n++){const i=e(t[n],r[n]);if(0!==i)return i}return t.length<r.length?-1:t.length>r.length?1:0}case 9:return e(Array.from(n).sort(e),Array.from(i).sort(e));case 10:return e(Array.from(t.Dictionary.asMap(n).entries()).sort(e),Array.from(t.Dictionary.asMap(i).entries()).sort(e));case 11:return r(n,i);default:throw new Error("Invalid typeCode: "+s)}}(e,n)},t.dataview=p,t.decode=Mt,t.decodeBase64=i,t.decodeWithAnnotations=function(t,e={}){return Mt(t,Object.assign(Object.assign({},e),{includeAnnotations:!0}))},t.embeddedId=j,t.encode=O,t.encodeBase64=o,t.encodeDictionaryOn=G,t.encodeSetOn=tt,t.encodeWithAnnotations=function(t,e={}){return O(t,Object.assign(Object.assign({},e),{includeAnnotations:!0}))},t.floatValue=function(t){return"number"==typeof t?t:lt.isFloat(t)?t.value:NaN},t.floatlikeString=ft,t.fold=Lt,t.forEachEmbedded=function(t,e){return Lt(t,new Rt(e))},t.formatPosition=function(t){var e,n,r;return null===t?"<unknown>":"string"==typeof t?t:`${null!==(e=t.name)&&void 0!==e?e:""}:${null!==(n=t.line)&&void 0!==n?n:""}:${null!==(r=t.column)&&void 0!==r?r:""}:${t.pos}`},t.fromJS=jt,t.genericEmbeddedType=Tt,t.genericEmbeddedTypeDecode=vt,t.genericEmbeddedTypeEncode=Et,t.hexDigit=d,t.identityEmbeddedType=Pt,t.identityEmbeddedTypeDecode=qt,t.identityEmbeddedTypeEncode=I,t.is=it,t.isAnnotated=rt,t.isCompound=ht,t.isEmbedded=x,t.isPreservable=E,t.isPreserveWritable=St,t.isSequence=function(t){return Array.isArray(t)&&!("label"in t)},t.mapEmbeddeds=function(t,e){return Lt(t,new Wt(e))},t.merge=function(e,n,...r){function i(){throw new Error("Cannot merge items")}function s(n,r){return n===r?n:ht(n)||ht(r)||!it(n,r)?Lt(n,{boolean:i,double:t=>it(n,r)?n:i(),integer:i,string:i,bytes:t=>it(n,r)?n:i(),symbol:i,record:t=>(v.isRecord(r)||i(),v(s(t.label,r.label),o(t,r))),array:t=>(Array.isArray(r)&&!v.isRecord(r)||i(),o(t,r)),set(t){i()},dictionary(e){const n=t.Dictionary.asMap(r);void 0===n&&i();const o=new K;return e.forEach(((t,e)=>{const r=n.get(e);o.set(e,void 0===r?t:s(t,r))})),n.forEach(((t,n)=>{e.has(n)||o.set(n,t)})),o.simplifiedValue()},annotated:t=>s(t,J(r)),embedded(t){x(r)||i();const n=e(t,r);return void 0===n&&i(),n}}):n}function o(t,e){return t.length<=e.length?e.map(((e,n)=>n<t.length?s(t[n],e):e)):t.map(((t,n)=>n<e.length?s(t,e[n]):t))}return r.reduce(s,n)},t.neverEmbeddedType=Ft,t.neverEmbeddedTypeDecode=Ot,t.neverEmbeddedTypeEncode=kt,t.newPosition=st,t.parse=function(t,e){return new _t(t,e).next()},t.parseAll=function(t,e){return new _t(t,e).readToEnd()},t.peel=function(t){return W(t,1)},t.position=function(t){return at.isAnnotated(t)?t.pos:null},t.preserves=function(t,...e){const n=[t[0]];return e.forEach(((e,r)=>{n.push(Ct(e)),n.push(t[r+1])})),n.join("")},t.stringify=Ct,t.stringifyEmbeddedWrite=It,t.strip=W,t.typeCode=$t,t.unannotate=J,t.underlying=f,t.unhexDigit=l,t.updatePosition=ot,t.valueClass=function(e){switch(typeof e){case"boolean":return t.ValueClass.Boolean;case"number":if(Number.isInteger(e))return t.ValueClass.SignedInteger;throw new Error("Non-integer number in Preserves valueClass; missing Float wrapper?");case"bigint":return t.ValueClass.SignedInteger;case"string":return t.ValueClass.String;case"symbol":return t.ValueClass.Symbol;case"object":return v.isRecord(e)?t.ValueClass.Record:Array.isArray(e)?t.ValueClass.Sequence:Q.isSet(e)?t.ValueClass.Set:t.Dictionary.isDictionary(e)?t.ValueClass.Dictionary:at.isAnnotated(e)?t.ValueClass.Annotated:h.isBytes(e)?t.ValueClass.ByteString:lt.isDouble(e)?t.ValueClass.Double:t.ValueClass.Embedded;default:(t=>{throw new Error("Internal error")})()}},t.writeDictionaryOn=X,t.writeSetOn=et})); |
import { Annotated } from "./annotated"; | ||
import { DecodeError, ShortPacket } from "./codec"; | ||
import { Tag } from "./constants"; | ||
import { Set, Dictionary } from "./dictionary"; | ||
import { Set, DictionaryMap } from "./dictionary"; | ||
import { DoubleFloat } from "./float"; | ||
@@ -9,2 +9,3 @@ import { Record } from "./record"; | ||
import { is } from "./is"; | ||
import { stringify } from "./text"; | ||
export function asLiteral(actual, expected) { | ||
@@ -162,9 +163,11 @@ return is(actual, expected) ? expected : void 0; | ||
static dictionaryFromArray(vs) { | ||
const d = new Dictionary(); | ||
const d = new DictionaryMap(); | ||
if (vs.length % 2) | ||
throw new DecodeError("Missing dictionary value"); | ||
for (let i = 0; i < vs.length; i += 2) { | ||
if (d.has(vs[i])) | ||
throw new DecodeError(`Duplicate key: ${stringify(vs[i])}`); | ||
d.set(vs[i], vs[i + 1]); | ||
} | ||
return d; | ||
return d.simplifiedValue(); | ||
} | ||
@@ -199,3 +202,11 @@ next() { | ||
case Tag.Sequence: return this.state.wrap(this.nextvalues()); | ||
case Tag.Set: return this.state.wrap(new Set(this.nextvalues())); | ||
case Tag.Set: { | ||
const s = new Set(); | ||
for (const v of this.nextvalues()) { | ||
if (s.has(v)) | ||
throw new DecodeError(`Duplicate value: ${stringify(v)}`); | ||
s.add(v); | ||
} | ||
return this.state.wrap(s); | ||
} | ||
case Tag.Dictionary: return this.state.wrap(Decoder.dictionaryFromArray(this.nextvalues())); | ||
@@ -202,0 +213,0 @@ default: throw new DecodeError("Unsupported Preserves tag: " + tag); |
import { Encoder } from "./encoder"; | ||
import { FlexMap, FlexSet, IdentitySet } from "./flex"; | ||
import { FlexMap, FlexSet, IdentitySet, Equivalence, IsMap } from "./flex"; | ||
import { Value } from "./values"; | ||
import type { Embeddable, GenericEmbedded } from "./embedded"; | ||
import { Embeddable, GenericEmbedded } from "./embedded"; | ||
import type { Preservable } from "./encoder"; | ||
import type { Writer, PreserveWritable } from "./writer"; | ||
import { JsDictionary } from "./jsdictionary"; | ||
export type DictionaryType = 'Dictionary' | 'Set'; | ||
export declare const DictionaryType: unique symbol; | ||
export type CompoundKey<T extends Embeddable> = Value<T> | (Preservable<T> & PreserveWritable<T>); | ||
export declare class EncodableDictionary<K, V, T extends Embeddable = GenericEmbedded> extends FlexMap<K, V> implements Preservable<T>, PreserveWritable<T> { | ||
export declare class EncodableDictionary<T extends Embeddable, K, V> extends FlexMap<K, V> implements Preservable<T>, PreserveWritable<T> { | ||
readonly encodeK: (k: K) => CompoundKey<T>; | ||
@@ -17,17 +18,46 @@ readonly encodeV: (v: V) => CompoundKey<T>; | ||
} | ||
export declare class KeyedDictionary<K extends CompoundKey<T>, V, T extends Embeddable = GenericEmbedded> extends EncodableDictionary<K, V, T> { | ||
export declare class KeyedDictionary<T extends Embeddable = GenericEmbedded, K extends CompoundKey<T> = Value<T>, V = Value<T>> extends EncodableDictionary<T, K, V> { | ||
get [DictionaryType](): DictionaryType; | ||
static isKeyedDictionary<K extends CompoundKey<T>, V, T extends Embeddable = GenericEmbedded>(x: any): x is KeyedDictionary<K, V, T>; | ||
static isKeyedDictionary<T extends Embeddable = GenericEmbedded, K extends CompoundKey<T> = Value<T>, V = Value<T>>(x: any): x is KeyedDictionary<T, K, V>; | ||
constructor(items?: readonly [K, V][] | Iterable<readonly [K, V]>); | ||
mapEntries<W, S extends Value<R>, R extends Embeddable = GenericEmbedded>(f: (entry: [K, V]) => [S, W]): KeyedDictionary<S, W, R>; | ||
clone(): KeyedDictionary<K, V, T>; | ||
clone(): KeyedDictionary<T, K, V>; | ||
get [Symbol.toStringTag](): string; | ||
equals(otherAny: any, eqv?: Equivalence<V>): boolean; | ||
} | ||
export declare class Dictionary<T extends Embeddable = GenericEmbedded, V = Value<T>> extends KeyedDictionary<Value<T>, V, T> { | ||
static isDictionary<T extends Embeddable = GenericEmbedded, V = Value<T>>(x: any): x is Dictionary<T, V>; | ||
static __from_preserve__<T extends Embeddable>(v: Value<T>): undefined | Dictionary<T>; | ||
export type Dictionary<T extends Embeddable = GenericEmbedded, V = Value<T>> = JsDictionary<V> | KeyedDictionary<T, Value<T>, V>; | ||
export declare class DictionaryMap<T extends Embeddable = GenericEmbedded, V = Value<T>> implements Map<Value<T>, V> { | ||
get [IsMap](): boolean; | ||
j: JsDictionary<V> | undefined; | ||
k: KeyedDictionary<T, Value<T>, V> | undefined; | ||
constructor(input?: Dictionary<T, V>); | ||
static from<T extends Embeddable = GenericEmbedded, V = Value<T>>(entries: Iterable<[Value<T>, V]>): DictionaryMap<T, V>; | ||
clear(): void; | ||
delete(key: Value<T>): boolean; | ||
forEach(callbackfn: (value: V, key: Value<T>, map: Map<Value<T>, V>) => void, thisArg?: any): void; | ||
get(key: Value<T>): V | undefined; | ||
has(key: Value<T>): boolean; | ||
set(key: Value<T>, value: V): this; | ||
get size(): number; | ||
entries(): IterableIterator<[Value<T>, V]>; | ||
keys(): IterableIterator<Value<T>>; | ||
values(): IterableIterator<V>; | ||
[Symbol.iterator](): IterableIterator<[Value<T>, V]>; | ||
get [Symbol.toStringTag](): string; | ||
clone(): DictionaryMap<T, V>; | ||
get value(): Dictionary<T, V>; | ||
simplify(): void; | ||
simplifiedValue(): Dictionary<T, V>; | ||
asJsDictionary(): JsDictionary<V>; | ||
asKeyedDictionary(): KeyedDictionary<T, Value<T>, V>; | ||
} | ||
export declare function encodeDictionaryOn<K, V, T extends Embeddable>(dict: Map<K, V>, encoder: Encoder<T>, encodeK: (k: K, encoder: Encoder<T>) => void, encodeV: (v: V, encoder: Encoder<T>) => void): void; | ||
export declare function writeDictionaryOn<K, V, T extends Embeddable>(dict: Map<K, V>, w: Writer<T>, writeK: (k: K, w: Writer<T>) => void, writeV: (v: V, w: Writer<T>) => void): void; | ||
export declare class EncodableSet<V, T extends Embeddable = GenericEmbedded> extends FlexSet<V> implements Preservable<T>, PreserveWritable<T> { | ||
export declare namespace Dictionary { | ||
function isDictionary<T extends Embeddable = GenericEmbedded, V = Value<T>>(x: any): x is Dictionary<T, V>; | ||
function asMap<T extends Embeddable = GenericEmbedded, V = Value<T>>(x: Dictionary<T, V>): DictionaryMap<T, V>; | ||
function asMap<T extends Embeddable = GenericEmbedded, V = Value<T>>(x: any): DictionaryMap<T, V> | undefined; | ||
function from<T extends Embeddable = GenericEmbedded, V = Value<T>>(entries: Iterable<[Value<T>, V]>): Dictionary<T, V>; | ||
function __from_preserve__<T extends Embeddable>(v: Value<T>): undefined | Dictionary<T>; | ||
} | ||
export declare function encodeDictionaryOn<T extends Embeddable, K, V>(dict: Map<K, V>, encoder: Encoder<T>, encodeK: (k: K, encoder: Encoder<T>) => void, encodeV: (v: V, encoder: Encoder<T>) => void): void; | ||
export declare function writeDictionaryOn<T extends Embeddable, K, V>(dict: Map<K, V>, w: Writer<T>, writeK: (k: K, w: Writer<T>) => void, writeV: (v: V, w: Writer<T>) => void): void; | ||
export declare class EncodableSet<T extends Embeddable, V> extends FlexSet<V> implements Preservable<T>, PreserveWritable<T> { | ||
readonly encodeV: (v: V) => CompoundKey<T>; | ||
@@ -38,16 +68,16 @@ constructor(encodeV: (v: V) => CompoundKey<T>, items?: Iterable<V>); | ||
} | ||
export declare class KeyedSet<K extends CompoundKey<T>, T extends Embeddable = GenericEmbedded> extends EncodableSet<K, T> { | ||
export declare class KeyedSet<T extends Embeddable = GenericEmbedded, K extends CompoundKey<T> = Value<T>> extends EncodableSet<T, K> { | ||
get [DictionaryType](): DictionaryType; | ||
static isKeyedSet<K extends CompoundKey<T>, T extends Embeddable = GenericEmbedded>(x: any): x is KeyedSet<K, T>; | ||
static isKeyedSet<T extends Embeddable = GenericEmbedded, K extends CompoundKey<T> = Value<T>>(x: any): x is KeyedSet<T, K>; | ||
constructor(items?: Iterable<K>); | ||
map<S extends Value<R>, R extends Embeddable = GenericEmbedded>(f: (value: K) => S): KeyedSet<S, R>; | ||
filter(f: (value: K) => boolean): KeyedSet<K, T>; | ||
clone(): KeyedSet<K, T>; | ||
map<R extends Embeddable = GenericEmbedded, S extends Value<R> = Value<R>>(f: (value: K) => S): KeyedSet<R, S>; | ||
filter(f: (value: K) => boolean): KeyedSet<T, K>; | ||
clone(): KeyedSet<T, K>; | ||
get [Symbol.toStringTag](): string; | ||
} | ||
export declare class Set<T extends Embeddable = GenericEmbedded> extends KeyedSet<Value<T>, T> { | ||
export declare class Set<T extends Embeddable = GenericEmbedded> extends KeyedSet<T> { | ||
static isSet<T extends Embeddable = GenericEmbedded>(x: any): x is Set<T>; | ||
static __from_preserve__<T extends Embeddable>(v: Value<T>): undefined | Set<T>; | ||
} | ||
export declare function encodeSetOn<V, T extends Embeddable>(s: IdentitySet<V>, encoder: Encoder<T>, encodeV: (v: V, encoder: Encoder<T>) => void): void; | ||
export declare function writeSetOn<V, T extends Embeddable>(s: IdentitySet<V>, w: Writer<T>, writeV: (v: V, w: Writer<T>) => void): void; | ||
export declare function encodeSetOn<T extends Embeddable, V>(s: IdentitySet<V>, encoder: Encoder<T>, encodeV: (v: V, encoder: Encoder<T>) => void): void; | ||
export declare function writeSetOn<T extends Embeddable, V>(s: IdentitySet<V>, w: Writer<T>, writeV: (v: V, w: Writer<T>) => void): void; |
import { Encoder, canonicalString } from "./encoder"; | ||
import { Tag } from "./constants"; | ||
import { FlexMap, FlexSet, _iterMap } from "./flex"; | ||
import { FlexMap, FlexSet, _iterMap, IsMap } from "./flex"; | ||
import { Bytes } from './bytes'; | ||
import { annotations, Annotated } from "./annotated"; | ||
import { JsDictionary } from "./jsdictionary"; | ||
import { unannotate } from "./strip"; | ||
export const DictionaryType = Symbol.for('DictionaryType'); | ||
@@ -32,10 +34,2 @@ export class EncodableDictionary extends FlexMap { | ||
} | ||
mapEntries(f) { | ||
const result = new KeyedDictionary(); | ||
for (let oldEntry of this.entries()) { | ||
const newEntry = f(oldEntry); | ||
result.set(newEntry[0], newEntry[1]); | ||
} | ||
return result; | ||
} | ||
clone() { | ||
@@ -45,11 +39,169 @@ return new KeyedDictionary(this); | ||
get [Symbol.toStringTag]() { return 'Dictionary'; } | ||
equals(otherAny, eqv = (v1, v2) => v1 === v2) { | ||
const otherMap = Dictionary.asMap(otherAny); | ||
if (!otherMap) | ||
return false; | ||
return super.equals(otherMap, eqv); | ||
} | ||
} | ||
export class Dictionary extends KeyedDictionary { | ||
static isDictionary(x) { | ||
return (x === null || x === void 0 ? void 0 : x[DictionaryType]) === 'Dictionary'; | ||
export class DictionaryMap { | ||
get [IsMap]() { return true; } | ||
constructor(input) { | ||
if (input === void 0) { | ||
this.j = {}; | ||
this.k = void 0; | ||
} | ||
else if (DictionaryType in input) { | ||
this.j = void 0; | ||
this.k = input; | ||
} | ||
else { | ||
this.j = input; | ||
this.k = void 0; | ||
} | ||
} | ||
static __from_preserve__(v) { | ||
static from(entries) { | ||
const r = new DictionaryMap(); | ||
for (const [key, value] of entries) | ||
r.set(key, value); | ||
return r; | ||
} | ||
clear() { | ||
if (this.j) { | ||
JsDictionary.clear(this.j); | ||
} | ||
else { | ||
this.k.clear(); | ||
} | ||
} | ||
delete(key) { | ||
if (this.j) { | ||
key = unannotate(key); | ||
if (typeof key !== 'symbol') | ||
return false; | ||
return JsDictionary.remove(this.j, key); | ||
} | ||
else { | ||
return this.k.delete(key); | ||
} | ||
} | ||
forEach(callbackfn, thisArg) { | ||
if (this.j) { | ||
JsDictionary.forEach(this.j, (v, k) => callbackfn.call(thisArg, v, k, this)); | ||
} | ||
else { | ||
this.k.forEach(callbackfn, thisArg); | ||
} | ||
} | ||
get(key) { | ||
if (this.j) { | ||
key = unannotate(key); | ||
if (typeof key !== 'symbol') | ||
return void 0; | ||
return JsDictionary.get(this.j, key); | ||
} | ||
else { | ||
return this.k.get(key); | ||
} | ||
} | ||
has(key) { | ||
if (this.j) { | ||
key = unannotate(key); | ||
if (typeof key !== 'symbol') | ||
return false; | ||
return JsDictionary.has(this.j, key); | ||
} | ||
else { | ||
return this.k.has(key); | ||
} | ||
} | ||
set(key, value) { | ||
if (this.j) { | ||
if (typeof key === 'symbol') { | ||
JsDictionary.set(this.j, key, value); | ||
return this; | ||
} | ||
this.k = new KeyedDictionary(JsDictionary.entries(this.j)); | ||
this.j = void 0; | ||
} | ||
this.k.set(key, value); | ||
return this; | ||
} | ||
get size() { | ||
return this.j ? JsDictionary.size(this.j) : this.k.size; | ||
} | ||
entries() { | ||
return this.j ? JsDictionary.entries(this.j) : this.k.entries(); | ||
} | ||
keys() { | ||
return this.j ? JsDictionary.keys(this.j) : this.k.keys(); | ||
} | ||
values() { | ||
return this.j ? JsDictionary.values(this.j) : this.k.values(); | ||
} | ||
[Symbol.iterator]() { | ||
return this.entries(); | ||
} | ||
get [Symbol.toStringTag]() { | ||
return 'DictionaryMap'; | ||
} | ||
clone() { | ||
return new DictionaryMap(this.j ? JsDictionary.clone(this.j) : this.k.clone()); | ||
} | ||
get value() { | ||
var _a; | ||
return (_a = this.j) !== null && _a !== void 0 ? _a : this.k; | ||
} | ||
simplify() { | ||
if (!this.j) { | ||
const r = {}; | ||
for (const [key, value] of this.k.entries()) { | ||
if (typeof key !== 'symbol') | ||
return; | ||
r[key.description] = value; | ||
} | ||
this.j = r; | ||
this.k = void 0; | ||
} | ||
} | ||
simplifiedValue() { | ||
this.simplify(); | ||
return this.value; | ||
} | ||
asJsDictionary() { | ||
this.simplify(); | ||
if (!this.j) | ||
throw new Error("Cannot represent general dictionary as JsDictionary"); | ||
return this.j; | ||
} | ||
asKeyedDictionary() { | ||
var _a; | ||
return (_a = this.k) !== null && _a !== void 0 ? _a : new KeyedDictionary(JsDictionary.entries(this.j)); | ||
} | ||
} | ||
export var Dictionary; | ||
(function (Dictionary) { | ||
function isDictionary(x) { | ||
if (typeof x !== 'object' || x === null) | ||
return false; | ||
switch (x[DictionaryType]) { | ||
case 'Dictionary': return true; | ||
case void 0: return JsDictionary.isJsDictionary(x); | ||
default: return false; | ||
} | ||
} | ||
Dictionary.isDictionary = isDictionary; | ||
function asMap(x) { | ||
return isDictionary(x) ? new DictionaryMap(x) : void 0; | ||
} | ||
Dictionary.asMap = asMap; | ||
function from(entries) { | ||
return DictionaryMap.from(entries).simplifiedValue(); | ||
} | ||
Dictionary.from = from; | ||
function __from_preserve__(v) { | ||
return Dictionary.isDictionary(v) ? v : void 0; | ||
} | ||
} | ||
Dictionary.__from_preserve__ = __from_preserve__; | ||
})(Dictionary || (Dictionary = {})); | ||
export function encodeDictionaryOn(dict, encoder, encodeK, encodeV) { | ||
@@ -56,0 +208,0 @@ if (encoder.canonical) { |
@@ -5,3 +5,7 @@ import type { DecoderState } from "./decoder"; | ||
import { ReaderStateOptions } from "./reader"; | ||
export type Embeddable = object; | ||
export declare const IsEmbedded: unique symbol; | ||
export interface Embeddable { | ||
readonly [IsEmbedded]: true; | ||
} | ||
export declare function isEmbedded<T extends Embeddable>(v: any): v is T; | ||
export type EmbeddedTypeEncode<T extends Embeddable> = { | ||
@@ -15,9 +19,11 @@ encode(s: EncoderState, v: T): void; | ||
export type EmbeddedType<T extends Embeddable> = EmbeddedTypeEncode<T> & EmbeddedTypeDecode<T>; | ||
export declare function isEmbedded<T extends Embeddable>(v: Value<T>): v is T; | ||
export declare class Embedded<T> { | ||
readonly value: T; | ||
get [IsEmbedded](): true; | ||
constructor(value: T); | ||
equals(other: any): boolean; | ||
} | ||
export declare class GenericEmbedded { | ||
generic: Value; | ||
readonly generic: Value; | ||
get [IsEmbedded](): true; | ||
constructor(generic: Value); | ||
@@ -24,0 +30,0 @@ equals(other: any, is: (a: any, b: any) => boolean): boolean; |
@@ -1,22 +0,16 @@ | ||
import { Annotated } from "./annotated"; | ||
import { Bytes } from "./bytes"; | ||
import { Float } from "./float"; | ||
import { Set, Dictionary } from "./dictionary"; | ||
export const IsEmbedded = Symbol.for('IsEmbedded'); | ||
export function isEmbedded(v) { | ||
return (typeof (v) === 'object') | ||
&& (v !== null) | ||
&& !Array.isArray(v) | ||
&& !Bytes.isBytes(v) | ||
&& !Float.isFloat(v) | ||
&& !Set.isSet(v) | ||
&& !Dictionary.isDictionary(v) | ||
&& !Annotated.isAnnotated(v) | ||
&& ((_v) => true)(v); | ||
return !!(v === null || v === void 0 ? void 0 : v[IsEmbedded]); | ||
} | ||
export class Embedded { | ||
get [IsEmbedded]() { return true; } | ||
constructor(value) { | ||
this.value = value; | ||
} | ||
equals(other) { | ||
return typeof other === 'object' && 'value' in other && Object.is(this.value, other.value); | ||
} | ||
} | ||
export class GenericEmbedded { | ||
get [IsEmbedded]() { return true; } | ||
constructor(generic) { | ||
@@ -23,0 +17,0 @@ this.generic = generic; |
@@ -5,2 +5,4 @@ import { Tag } from "./constants"; | ||
import { Record } from "./record"; | ||
import { isEmbedded } from "./embedded"; | ||
import { DictionaryMap, encodeDictionaryOn } from "./dictionary"; | ||
export function isPreservable(v) { | ||
@@ -240,7 +242,8 @@ return typeof v === 'object' && v !== null && '__preserve_on__' in v && typeof v.__preserve_on__ === 'function'; | ||
} | ||
else if (isEmbedded(v)) { | ||
this.state.emitbyte(Tag.Embedded); | ||
this.embeddedEncode.encode(this.state, v); | ||
} | ||
else { | ||
((v) => { | ||
this.state.emitbyte(Tag.Embedded); | ||
this.embeddedEncode.encode(this.state, v); | ||
})(v); | ||
encodeDictionaryOn(new DictionaryMap(v), this, (k, e) => e.push(k), (v, e) => e.push(v)); | ||
} | ||
@@ -247,0 +250,0 @@ return this; // for chaining |
import { Record, Tuple } from "./record"; | ||
import { Bytes } from "./bytes"; | ||
import { Value } from "./values"; | ||
import { Set, Dictionary } from "./dictionary"; | ||
import { Set, DictionaryMap } from "./dictionary"; | ||
import { Annotated } from "./annotated"; | ||
@@ -32,3 +32,3 @@ import { Embeddable } from "./embedded"; | ||
set(s: Set<T>, k: Fold<T, R>): R; | ||
dictionary(d: Dictionary<T>, k: Fold<T, R>): R; | ||
dictionary(d: DictionaryMap<T>, k: Fold<T, R>): R; | ||
annotated(a: Annotated<T>, k: Fold<T, R>): R; | ||
@@ -47,3 +47,3 @@ embedded(t: T, k: Fold<T, R>): R; | ||
set(s: Set<T>, k: Fold<T, void>): void; | ||
dictionary(d: Dictionary<T>, k: Fold<T, void>): void; | ||
dictionary(d: DictionaryMap<T>, k: Fold<T, void>): void; | ||
annotated(a: Annotated<T>, k: Fold<T, void>): void; | ||
@@ -67,3 +67,3 @@ embedded(_t: T, _k: Fold<T, void>): void; | ||
set(s: Set<T>, k: Fold<T, Value<R>>): Value<R>; | ||
dictionary(d: Dictionary<T>, k: Fold<T, Value<R>>): Value<R>; | ||
dictionary(d: DictionaryMap<T>, k: Fold<T, Value<R>>): Value<R>; | ||
annotated(a: Annotated<T>, k: Fold<T, Value<R>>): Value<R>; | ||
@@ -70,0 +70,0 @@ abstract embedded(t: T, k: Fold<T, Value<R>>): Value<R>; |
import { Record } from "./record"; | ||
import { Bytes } from "./bytes"; | ||
import { Set, Dictionary } from "./dictionary"; | ||
import { Set, Dictionary, DictionaryMap } from "./dictionary"; | ||
import { annotate, Annotated } from "./annotated"; | ||
import { Double, Float } from "./float"; | ||
import { isEmbedded } from "./embedded"; | ||
export var ValueClass; | ||
@@ -76,3 +77,5 @@ (function (ValueClass) { | ||
dictionary(d, k) { | ||
return d.mapEntries(([key, value]) => [k(key), k(value)]); | ||
const result = new DictionaryMap(); | ||
d.forEach((value, key) => result.set(k(key), k(value))); | ||
return result.simplifiedValue(); | ||
} | ||
@@ -173,4 +176,4 @@ annotated(a, k) { | ||
} | ||
else if (Dictionary.isDictionary(v)) { | ||
return o.dictionary(v, walk); | ||
else if (isEmbedded(v)) { | ||
return o.embedded(v, walk); | ||
} | ||
@@ -186,4 +189,4 @@ else if (Annotated.isAnnotated(v)) { | ||
} | ||
else { | ||
return ((_v) => o.embedded(v, walk))(v); | ||
else if (Dictionary.isDictionary(v)) { | ||
return o.dictionary(new DictionaryMap(v), walk); | ||
} | ||
@@ -190,0 +193,0 @@ default: |
@@ -6,4 +6,4 @@ import { Embeddable, GenericEmbedded } from "./embedded"; | ||
namespace Dictionary { | ||
function fromJS<T extends Embeddable = GenericEmbedded, V extends Embeddable = GenericEmbedded>(x: object): Dictionary<T, Value<V>>; | ||
function stringMap<T extends Embeddable = GenericEmbedded>(x: object): KeyedDictionary<T, string, Value<T>>; | ||
} | ||
} |
@@ -0,4 +1,5 @@ | ||
import { isEmbedded } from "./embedded"; | ||
import { Bytes } from "./bytes"; | ||
import { Record } from "./record"; | ||
import { Dictionary, Set } from "./dictionary"; | ||
import { Dictionary, KeyedDictionary, Set } from "./dictionary"; | ||
export function fromJS(x) { | ||
@@ -37,3 +38,3 @@ switch (typeof x) { | ||
if (Map.isMap(x)) { | ||
const d = new Dictionary(); | ||
const d = new KeyedDictionary(); | ||
x.forEach((v, k) => d.set(fromJS(k), fromJS(v))); | ||
@@ -47,4 +48,11 @@ return d; | ||
} | ||
// Just... assume it's a T. | ||
return x; | ||
if (isEmbedded(x)) { | ||
return x; | ||
} | ||
// Handle plain JS objects to build a JsDictionary | ||
{ | ||
const r = {}; | ||
Object.entries(x).forEach(([k, v]) => r[k] = fromJS(v)); | ||
return r; | ||
} | ||
default: | ||
@@ -55,9 +63,7 @@ break; | ||
} | ||
Dictionary.fromJS = function (x) { | ||
if (Dictionary.isDictionary(x)) | ||
return x; | ||
const d = new Dictionary(); | ||
Object.entries(x).forEach(([key, value]) => d.set(key, fromJS(value))); | ||
return d; | ||
Dictionary.stringMap = function (x) { | ||
const r = new KeyedDictionary(); | ||
Object.entries(x).forEach(([key, value]) => r.set(key, fromJS(value))); | ||
return r; | ||
}; | ||
//# sourceMappingURL=fromjs.js.map |
@@ -0,1 +1,2 @@ | ||
import { Dictionary } from "./dictionary"; | ||
export const IsPreservesAnnotated = Symbol.for('IsPreservesAnnotated'); | ||
@@ -37,2 +38,17 @@ export function isAnnotated(x) { | ||
} | ||
{ | ||
const aMap = Dictionary.asMap(a); | ||
const bMap = Dictionary.asMap(b); | ||
if (!aMap || !bMap) | ||
return false; | ||
if (aMap.size !== bMap.size) | ||
return false; | ||
for (const k of aMap.keys()) { | ||
if (!bMap.has(k)) | ||
return false; | ||
if (!is(aMap.get(k), bMap.get(k))) | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
@@ -39,0 +55,0 @@ return false; |
import { Record } from "./record"; | ||
import { fold } from "./fold"; | ||
import { is } from "./is"; | ||
import { Dictionary } from "./dictionary"; | ||
import { Dictionary, DictionaryMap } from "./dictionary"; | ||
import { unannotate } from "./strip"; | ||
@@ -42,15 +42,16 @@ import { isEmbedded } from "./embedded"; | ||
set(_s) { die(); }, | ||
dictionary(d) { | ||
if (!Dictionary.isDictionary(b)) | ||
dictionary(aMap) { | ||
const bMap = Dictionary.asMap(b); | ||
if (bMap === void 0) | ||
die(); | ||
const r = new Dictionary(); | ||
d.forEach((av, ak) => { | ||
const bv = b.get(ak); | ||
const r = new DictionaryMap(); | ||
aMap.forEach((av, ak) => { | ||
const bv = bMap.get(ak); | ||
r.set(ak, bv === void 0 ? av : walk(av, bv)); | ||
}); | ||
b.forEach((bv, bk) => { | ||
if (!d.has(bk)) | ||
bMap.forEach((bv, bk) => { | ||
if (!aMap.has(bk)) | ||
r.set(bk, bv); | ||
}); | ||
return r; | ||
return r.simplifiedValue(); | ||
}, | ||
@@ -57,0 +58,0 @@ annotated(a) { |
// Patching to support node.js extensions. | ||
import { Annotated } from './annotated'; | ||
import { Bytes } from './bytes'; | ||
import { Set, Dictionary } from './dictionary'; | ||
import { Set, EncodableDictionary } from './dictionary'; | ||
import { stringify } from './text'; | ||
import * as util from 'util'; | ||
[Bytes, Annotated, Set, Dictionary].forEach((C) => { | ||
[Bytes, Annotated, Set, EncodableDictionary].forEach((C) => { | ||
C.prototype[util.inspect.custom] = | ||
@@ -9,0 +9,0 @@ function (_depth, _options) { |
@@ -94,4 +94,4 @@ import { is, isAnnotated } from './is'; | ||
case 10: { | ||
const va = Array.from(a).sort(cmp); | ||
const vb = Array.from(b).sort(cmp); | ||
const va = Array.from(Dictionary.asMap(a).entries()).sort(cmp); | ||
const vb = Array.from(Dictionary.asMap(b).entries()).sort(cmp); | ||
return cmp(va, vb); | ||
@@ -98,0 +98,0 @@ } |
// Text syntax reader. | ||
import { DecodeError, ShortPacket } from './codec'; | ||
import { Dictionary, Set } from './dictionary'; | ||
import { DictionaryMap, Set } from './dictionary'; | ||
import { strip } from './strip'; | ||
@@ -369,9 +369,9 @@ import { Bytes, unhexDigit } from './bytes'; | ||
readDictionary() { | ||
return this.seq(true, new Dictionary(), (k, acc) => { | ||
return this.seq(true, new DictionaryMap(), (k, acc) => { | ||
this.state.skipws(); | ||
switch (this.state.peek()) { | ||
case ':': | ||
this.state.advance(); | ||
if (acc.has(k)) | ||
this.state.error(`Duplicate key: ${stringify(k)}`, this.state.pos); | ||
this.state.advance(); | ||
acc.set(k, this.next()); | ||
@@ -382,3 +382,3 @@ break; | ||
} | ||
}, '}'); | ||
}, '}').simplifiedValue(); | ||
} | ||
@@ -385,0 +385,0 @@ readSet() { |
@@ -16,2 +16,3 @@ export * from './annotated'; | ||
export * from './is'; | ||
export * from './jsdictionary'; | ||
export * from './merge'; | ||
@@ -18,0 +19,0 @@ export * from './order'; |
@@ -16,2 +16,3 @@ export * from './annotated'; | ||
export * from './is'; | ||
export * from './jsdictionary'; | ||
export * from './merge'; | ||
@@ -18,0 +19,0 @@ export * from './order'; |
import { Annotated } from "./annotated"; | ||
import { Record } from "./record"; | ||
import { Set, Dictionary } from "./dictionary"; | ||
import { Set, Dictionary, DictionaryMap } from "./dictionary"; | ||
export function unannotate(v) { | ||
@@ -34,3 +34,5 @@ return Annotated.isAnnotated(v) ? v.item : v; | ||
else if (Dictionary.isDictionary(v.item)) { | ||
return v.item.mapEntries((e) => [walk(e[0]), walk(e[1])]); | ||
const result = new DictionaryMap(); | ||
new DictionaryMap(v.item).forEach((val, key) => result.set(walk(key), walk(val))); | ||
return result.simplifiedValue(); | ||
} | ||
@@ -37,0 +39,0 @@ else { |
import type { Bytes } from './bytes'; | ||
import type { DoubleFloat } from './float'; | ||
import type { Annotated } from './annotated'; | ||
import type { Set, Dictionary } from './dictionary'; | ||
import type { JsDictionary } from './jsdictionary'; | ||
import { Set, KeyedDictionary } from './dictionary'; | ||
import type { Embeddable, GenericEmbedded } from './embedded'; | ||
@@ -10,2 +11,2 @@ export type Value<T extends Embeddable = GenericEmbedded> = Atom | Compound<T> | T | Annotated<T>; | ||
label: Value<T>; | ||
} | Array<Value<T>> | Set<T> | Dictionary<T>; | ||
} | Array<Value<T>> | Set<T> | JsDictionary<Value<T>> | KeyedDictionary<T>; |
@@ -1,2 +0,2 @@ | ||
import type { Embeddable, GenericEmbedded, EmbeddedTypeEncode } from "./embedded"; | ||
import { Embeddable, GenericEmbedded, EmbeddedTypeEncode } from "./embedded"; | ||
import type { Value } from "./values"; | ||
@@ -3,0 +3,0 @@ export type Writable<T extends Embeddable> = Value<T> | PreserveWritable<T> | Iterable<Value<T>> | ArrayBufferView; |
import { isAnnotated } from './is'; | ||
import { Record } from "./record"; | ||
import { isEmbedded } from "./embedded"; | ||
import { Encoder } from "./encoder"; | ||
import { NUMBER_RE } from './reader'; | ||
import { encodeBase64 } from './base64'; | ||
import { DictionaryMap, writeDictionaryOn } from './dictionary'; | ||
export function isPreserveWritable(v) { | ||
@@ -261,13 +263,14 @@ return typeof v === 'object' && v !== null && '__preserve_text_on__' in v && typeof v.__preserve_text_on__ === 'function'; | ||
} | ||
else if (isEmbedded(v)) { | ||
this.state.pieces.push('#:'); | ||
if ('write' in this.embeddedWrite) { | ||
this.embeddedWrite.write(this.state, v); | ||
} | ||
else { | ||
new Writer(this.state, genericEmbeddedTypeEncode) | ||
.push(this.embeddedWrite.toValue(v)); | ||
} | ||
} | ||
else { | ||
((v) => { | ||
this.state.pieces.push('#:'); | ||
if ('write' in this.embeddedWrite) { | ||
this.embeddedWrite.write(this.state, v); | ||
} | ||
else { | ||
new Writer(this.state, genericEmbeddedTypeEncode) | ||
.push(this.embeddedWrite.toValue(v)); | ||
} | ||
})(v); | ||
writeDictionaryOn(new DictionaryMap(v), this, (k, w) => w.push(k), (v, w) => w.push(v)); | ||
} | ||
@@ -274,0 +277,0 @@ break; |
{ | ||
"name": "@preserves/core", | ||
"version": "0.995.101", | ||
"version": "0.995.200", | ||
"description": "Preserves data serialization format", | ||
@@ -26,3 +26,3 @@ "homepage": "https://gitlab.com/preserves/preserves", | ||
}, | ||
"gitHead": "f0815ce4ebac9c67fceefd13a3f3fec758c178fb" | ||
"gitHead": "3e67c754275431964a06b72176573e7741f385eb" | ||
} |
import { Annotated } from "./annotated"; | ||
import { DecodeError, ShortPacket } from "./codec"; | ||
import { Tag } from "./constants"; | ||
import { Set, Dictionary } from "./dictionary"; | ||
import { Set, Dictionary, DictionaryMap } from "./dictionary"; | ||
import { DoubleFloat } from "./float"; | ||
@@ -11,3 +11,4 @@ import { Record } from "./record"; | ||
import { GenericEmbedded, Embeddable, EmbeddedTypeDecode } from "./embedded"; | ||
import { ReaderStateOptions } from "reader"; | ||
import { ReaderStateOptions } from "./reader"; | ||
import { stringify } from "./text"; | ||
@@ -222,8 +223,9 @@ export interface DecoderOptions { | ||
static dictionaryFromArray<T extends Embeddable>(vs: Value<T>[]): Dictionary<T> { | ||
const d = new Dictionary<T>(); | ||
const d = new DictionaryMap<T>(); | ||
if (vs.length % 2) throw new DecodeError("Missing dictionary value"); | ||
for (let i = 0; i < vs.length; i += 2) { | ||
if (d.has(vs[i])) throw new DecodeError(`Duplicate key: ${stringify(vs[i])}`); | ||
d.set(vs[i], vs[i+1]); | ||
} | ||
return d; | ||
return d.simplifiedValue(); | ||
} | ||
@@ -258,3 +260,10 @@ | ||
case Tag.Sequence: return this.state.wrap<T>(this.nextvalues()); | ||
case Tag.Set: return this.state.wrap<T>(new Set(this.nextvalues())); | ||
case Tag.Set: { | ||
const s = new Set<T>(); | ||
for (const v of this.nextvalues()) { | ||
if (s.has(v)) throw new DecodeError(`Duplicate value: ${stringify(v)}`); | ||
s.add(v); | ||
} | ||
return this.state.wrap<T>(s); | ||
} | ||
case Tag.Dictionary: return this.state.wrap<T>(Decoder.dictionaryFromArray(this.nextvalues())); | ||
@@ -261,0 +270,0 @@ default: throw new DecodeError("Unsupported Preserves tag: " + tag); |
import { Encoder, canonicalString } from "./encoder"; | ||
import { Tag } from "./constants"; | ||
import { FlexMap, FlexSet, _iterMap, IdentitySet } from "./flex"; | ||
import { FlexMap, FlexSet, _iterMap, IdentitySet, Equivalence, IsMap } from "./flex"; | ||
import { Value } from "./values"; | ||
import { Bytes } from './bytes'; | ||
import type { Embeddable, GenericEmbedded } from "./embedded"; | ||
import { Embeddable, GenericEmbedded, isEmbedded } from "./embedded"; | ||
import type { Preservable } from "./encoder"; | ||
import type { Writer, PreserveWritable } from "./writer"; | ||
import { annotations, Annotated } from "./annotated"; | ||
import { Float } from "./float"; | ||
import { JsDictionary } from "./jsdictionary"; | ||
import { unannotate } from "./strip"; | ||
@@ -16,3 +19,3 @@ export type DictionaryType = 'Dictionary' | 'Set'; | ||
export class EncodableDictionary<K, V, T extends Embeddable = GenericEmbedded> extends FlexMap<K, V> | ||
export class EncodableDictionary<T extends Embeddable, K, V> extends FlexMap<K, V> | ||
implements Preservable<T>, PreserveWritable<T> | ||
@@ -43,4 +46,4 @@ { | ||
export class KeyedDictionary<K extends CompoundKey<T>, V, T extends Embeddable = GenericEmbedded> | ||
extends EncodableDictionary<K, V, T> | ||
export class KeyedDictionary<T extends Embeddable = GenericEmbedded, K extends CompoundKey<T> = Value<T>, V = Value<T>> | ||
extends EncodableDictionary<T, K, V> | ||
{ | ||
@@ -51,5 +54,5 @@ get [DictionaryType](): DictionaryType { | ||
static isKeyedDictionary<K extends CompoundKey<T>, V, T extends Embeddable = GenericEmbedded>( | ||
static isKeyedDictionary<T extends Embeddable = GenericEmbedded, K extends CompoundKey<T> = Value<T>, V = Value<T>>( | ||
x: any, | ||
): x is KeyedDictionary<K, V, T> { | ||
): x is KeyedDictionary<T, K, V> { | ||
return x?.[DictionaryType] === 'Dictionary'; | ||
@@ -64,26 +67,195 @@ } | ||
mapEntries<W, S extends Value<R>, R extends Embeddable = GenericEmbedded>( | ||
f: (entry: [K, V]) => [S, W], | ||
): KeyedDictionary<S, W, R> { | ||
const result = new KeyedDictionary<S, W, R>(); | ||
for (let oldEntry of this.entries()) { | ||
const newEntry = f(oldEntry); | ||
result.set(newEntry[0], newEntry[1]) | ||
clone(): KeyedDictionary<T, K, V> { | ||
return new KeyedDictionary(this); | ||
} | ||
get [Symbol.toStringTag]() { return 'Dictionary'; } | ||
equals(otherAny: any, eqv: Equivalence<V> = (v1, v2) => v1 === v2): boolean { | ||
const otherMap = Dictionary.asMap(otherAny); | ||
if (!otherMap) return false; | ||
return super.equals(otherMap, eqv); | ||
} | ||
} | ||
export type Dictionary<T extends Embeddable = GenericEmbedded, V = Value<T>> = | ||
JsDictionary<V> | KeyedDictionary<T, Value<T>, V>; | ||
export class DictionaryMap<T extends Embeddable = GenericEmbedded, V = Value<T>> implements Map<Value<T>, V> { | ||
get [IsMap](): boolean { return true; } | ||
j: JsDictionary<V> | undefined; | ||
k: KeyedDictionary<T, Value<T>, V> | undefined; | ||
constructor(input?: Dictionary<T, V>) { | ||
if (input === void 0) { | ||
this.j = {}; | ||
this.k = void 0; | ||
} else if (DictionaryType in input) { | ||
this.j = void 0; | ||
this.k = input; | ||
} else { | ||
this.j = input; | ||
this.k = void 0; | ||
} | ||
return result; | ||
} | ||
clone(): KeyedDictionary<K, V, T> { | ||
return new KeyedDictionary(this); | ||
static from<T extends Embeddable = GenericEmbedded, V = Value<T>>( | ||
entries: Iterable<[Value<T>, V]>, | ||
): DictionaryMap<T, V> { | ||
const r = new DictionaryMap<T, V>(); | ||
for (const [key, value] of entries) r.set(key, value); | ||
return r; | ||
} | ||
get [Symbol.toStringTag]() { return 'Dictionary'; } | ||
clear(): void { | ||
if (this.j) { | ||
JsDictionary.clear(this.j); | ||
} else { | ||
this.k!.clear(); | ||
} | ||
} | ||
delete(key: Value<T>): boolean { | ||
if (this.j) { | ||
key = unannotate(key); | ||
if (typeof key !== 'symbol') return false; | ||
return JsDictionary.remove(this.j, key); | ||
} else { | ||
return this.k!.delete(key); | ||
} | ||
} | ||
forEach(callbackfn: (value: V, key: Value<T>, map: Map<Value<T>, V>) => void, thisArg?: any): void { | ||
if (this.j) { | ||
JsDictionary.forEach(this.j, (v, k) => callbackfn.call(thisArg, v, k, this)); | ||
} else { | ||
this.k!.forEach(callbackfn, thisArg); | ||
} | ||
} | ||
get(key: Value<T>): V | undefined { | ||
if (this.j) { | ||
key = unannotate(key); | ||
if (typeof key !== 'symbol') return void 0; | ||
return JsDictionary.get(this.j, key); | ||
} else { | ||
return this.k!.get(key); | ||
} | ||
} | ||
has(key: Value<T>): boolean { | ||
if (this.j) { | ||
key = unannotate(key); | ||
if (typeof key !== 'symbol') return false; | ||
return JsDictionary.has(this.j, key); | ||
} else { | ||
return this.k!.has(key); | ||
} | ||
} | ||
set(key: Value<T>, value: V): this { | ||
if (this.j) { | ||
if (typeof key === 'symbol') { | ||
JsDictionary.set(this.j, key, value); | ||
return this; | ||
} | ||
this.k = new KeyedDictionary<T, Value<T>, V>(JsDictionary.entries(this.j)); | ||
this.j = void 0; | ||
} | ||
this.k!.set(key, value); | ||
return this; | ||
} | ||
get size(): number { | ||
return this.j ? JsDictionary.size(this.j) : this.k!.size; | ||
} | ||
entries(): IterableIterator<[Value<T>, V]> { | ||
return this.j ? JsDictionary.entries(this.j) : this.k!.entries(); | ||
} | ||
keys(): IterableIterator<Value<T>> { | ||
return this.j ? JsDictionary.keys(this.j) : this.k!.keys(); | ||
} | ||
values(): IterableIterator<V> { | ||
return this.j ? JsDictionary.values(this.j) : this.k!.values(); | ||
} | ||
[Symbol.iterator](): IterableIterator<[Value<T>, V]> { | ||
return this.entries(); | ||
} | ||
get [Symbol.toStringTag](): string { | ||
return 'DictionaryMap'; | ||
} | ||
clone(): DictionaryMap<T, V> { | ||
return new DictionaryMap<T, V>(this.j ? JsDictionary.clone(this.j) : this.k!.clone()); | ||
} | ||
get value(): Dictionary<T, V> { | ||
return this.j ?? this.k!; | ||
} | ||
simplify(): void { | ||
if (!this.j) { | ||
const r: JsDictionary<V> = {}; | ||
for (const [key, value] of this.k!.entries()) { | ||
if (typeof key !== 'symbol') return; | ||
r[key.description!] = value; | ||
} | ||
this.j = r; | ||
this.k = void 0; | ||
} | ||
} | ||
simplifiedValue(): Dictionary<T, V> { | ||
this.simplify(); | ||
return this.value; | ||
} | ||
asJsDictionary(): JsDictionary<V> { | ||
this.simplify(); | ||
if (!this.j) throw new Error("Cannot represent general dictionary as JsDictionary"); | ||
return this.j; | ||
} | ||
asKeyedDictionary(): KeyedDictionary<T, Value<T>, V> { | ||
return this.k ?? new KeyedDictionary<T, Value<T>, V>(JsDictionary.entries(this.j!)); | ||
} | ||
} | ||
export class Dictionary<T extends Embeddable = GenericEmbedded, V = Value<T>> extends KeyedDictionary<Value<T>, V, T> { | ||
static isDictionary<T extends Embeddable = GenericEmbedded, V = Value<T>>(x: any): x is Dictionary<T, V> { | ||
return x?.[DictionaryType] === 'Dictionary'; | ||
export namespace Dictionary { | ||
export function isDictionary<T extends Embeddable = GenericEmbedded, V = Value<T>>( | ||
x: any | ||
): x is Dictionary<T, V> { | ||
if (typeof x !== 'object' || x === null) return false; | ||
switch (x[DictionaryType]) { | ||
case 'Dictionary': return true; | ||
case void 0: return JsDictionary.isJsDictionary(x); | ||
default: return false; | ||
} | ||
} | ||
static __from_preserve__<T extends Embeddable>(v: Value<T>): undefined | Dictionary<T> { | ||
export function asMap<T extends Embeddable = GenericEmbedded, V = Value<T>>( | ||
x: Dictionary<T, V> | ||
): DictionaryMap<T, V>; | ||
export function asMap<T extends Embeddable = GenericEmbedded, V = Value<T>>( | ||
x: any | ||
): DictionaryMap<T, V> | undefined; | ||
export function asMap<T extends Embeddable = GenericEmbedded, V = Value<T>>( | ||
x: any | ||
): DictionaryMap<T, V> | undefined { | ||
return isDictionary<T, V>(x) ? new DictionaryMap(x) : void 0; | ||
} | ||
export function from<T extends Embeddable = GenericEmbedded, V = Value<T>>( | ||
entries: Iterable<[Value<T>, V]>, | ||
): Dictionary<T, V> { | ||
return DictionaryMap.from(entries).simplifiedValue(); | ||
} | ||
export function __from_preserve__<T extends Embeddable>(v: Value<T>): undefined | Dictionary<T> { | ||
return Dictionary.isDictionary<T>(v) ? v : void 0; | ||
@@ -93,3 +265,3 @@ } | ||
export function encodeDictionaryOn<K, V, T extends Embeddable>( | ||
export function encodeDictionaryOn<T extends Embeddable, K, V>( | ||
dict: Map<K, V>, | ||
@@ -124,3 +296,3 @@ encoder: Encoder<T>, | ||
export function writeDictionaryOn<K, V, T extends Embeddable>( | ||
export function writeDictionaryOn<T extends Embeddable, K, V>( | ||
dict: Map<K, V>, | ||
@@ -146,3 +318,3 @@ w: Writer<T>, | ||
export class EncodableSet<V, T extends Embeddable = GenericEmbedded> extends FlexSet<V> | ||
export class EncodableSet<T extends Embeddable, V> extends FlexSet<V> | ||
implements Preservable<T>, PreserveWritable<T> | ||
@@ -166,4 +338,4 @@ { | ||
export class KeyedSet<K extends CompoundKey<T>, T extends Embeddable = GenericEmbedded> | ||
extends EncodableSet<K, T> | ||
export class KeyedSet<T extends Embeddable = GenericEmbedded, K extends CompoundKey<T> = Value<T>> | ||
extends EncodableSet<T, K> | ||
{ | ||
@@ -174,5 +346,5 @@ get [DictionaryType](): DictionaryType { | ||
static isKeyedSet<K extends CompoundKey<T>, T extends Embeddable = GenericEmbedded>( | ||
static isKeyedSet<T extends Embeddable = GenericEmbedded, K extends CompoundKey<T> = Value<T>>( | ||
x: any, | ||
): x is KeyedSet<K, T> { | ||
): x is KeyedSet<T, K> { | ||
return x?.[DictionaryType] === 'Set'; | ||
@@ -185,10 +357,10 @@ } | ||
map<S extends Value<R>, R extends Embeddable = GenericEmbedded>( | ||
map<R extends Embeddable = GenericEmbedded, S extends Value<R> = Value<R>>( | ||
f: (value: K) => S, | ||
): KeyedSet<S, R> { | ||
): KeyedSet<R, S> { | ||
return new KeyedSet(_iterMap(this[Symbol.iterator](), f)); | ||
} | ||
filter(f: (value: K) => boolean): KeyedSet<K, T> { | ||
const result = new KeyedSet<K, T>(); | ||
filter(f: (value: K) => boolean): KeyedSet<T, K> { | ||
const result = new KeyedSet<T, K>(); | ||
for (let k of this) if (f(k)) result.add(k); | ||
@@ -198,3 +370,3 @@ return result; | ||
clone(): KeyedSet<K, T> { | ||
clone(): KeyedSet<T, K> { | ||
return new KeyedSet(this); | ||
@@ -206,3 +378,3 @@ } | ||
export class Set<T extends Embeddable = GenericEmbedded> extends KeyedSet<Value<T>, T> { | ||
export class Set<T extends Embeddable = GenericEmbedded> extends KeyedSet<T> { | ||
static isSet<T extends Embeddable = GenericEmbedded>(x: any): x is Set<T> { | ||
@@ -217,3 +389,3 @@ return x?.[DictionaryType] === 'Set'; | ||
export function encodeSetOn<V, T extends Embeddable>( | ||
export function encodeSetOn<T extends Embeddable, V>( | ||
s: IdentitySet<V>, | ||
@@ -239,3 +411,3 @@ encoder: Encoder<T>, | ||
export function writeSetOn<V, T extends Embeddable>( | ||
export function writeSetOn<T extends Embeddable, V>( | ||
s: IdentitySet<V>, | ||
@@ -242,0 +414,0 @@ w: Writer<T>, |
import type { DecoderState } from "./decoder"; | ||
import type { EncoderState } from "./encoder"; | ||
import type { Value } from "./values"; | ||
import { Annotated } from "./annotated"; | ||
import { Bytes } from "./bytes"; | ||
import { Float } from "./float"; | ||
import { Set, Dictionary } from "./dictionary"; | ||
import { ReaderStateOptions } from "./reader"; | ||
export type Embeddable = object; | ||
export const IsEmbedded = Symbol.for('IsEmbedded'); | ||
export interface Embeddable { | ||
readonly [IsEmbedded]: true; | ||
} | ||
export function isEmbedded<T extends Embeddable>(v: any): v is T { | ||
return !!v?.[IsEmbedded]; | ||
} | ||
export type EmbeddedTypeEncode<T extends Embeddable> = { | ||
@@ -23,24 +27,16 @@ encode(s: EncoderState, v: T): void; | ||
export function isEmbedded<T extends Embeddable>(v: Value<T>): v is T { | ||
return (typeof(v) === 'object') | ||
&& (v !== null) | ||
&& !Array.isArray(v) | ||
&& !Bytes.isBytes(v) | ||
&& !Float.isFloat(v) | ||
&& !Set.isSet<T>(v) | ||
&& !Dictionary.isDictionary<T>(v) | ||
&& !Annotated.isAnnotated<T>(v) | ||
&& ((_v: T) => true)(v); | ||
} | ||
export class Embedded<T> { | ||
get [IsEmbedded](): true { return true; } | ||
export class Embedded<T> { | ||
constructor(public readonly value: T) {} | ||
equals(other: any): boolean { | ||
return typeof other === 'object' && 'value' in other && Object.is(this.value, other.value); | ||
} | ||
} | ||
export class GenericEmbedded { | ||
generic: Value; | ||
get [IsEmbedded](): true { return true; } | ||
constructor(generic: Value) { | ||
this.generic = generic; | ||
} | ||
constructor(public readonly generic: Value) {} | ||
@@ -47,0 +43,0 @@ equals(other: any, is: (a: any, b: any) => boolean) { |
@@ -6,4 +6,5 @@ import { Tag } from "./constants"; | ||
import { Record, Tuple } from "./record"; | ||
import { EmbeddedTypeEncode } from "./embedded"; | ||
import { EmbeddedTypeEncode, isEmbedded } from "./embedded"; | ||
import type { Embeddable } from "./embedded"; | ||
import { DictionaryMap, encodeDictionaryOn } from "./dictionary"; | ||
@@ -292,7 +293,11 @@ export type Encodable<T extends Embeddable> = | ||
} | ||
else if (isEmbedded<T>(v)) { | ||
this.state.emitbyte(Tag.Embedded); | ||
this.embeddedEncode.encode(this.state, v); | ||
} | ||
else { | ||
((v: T) => { | ||
this.state.emitbyte(Tag.Embedded); | ||
this.embeddedEncode.encode(this.state, v); | ||
})(v); | ||
encodeDictionaryOn(new DictionaryMap<T>(v), | ||
this, | ||
(k, e) => e.push(k), | ||
(v, e) => e.push(v)); | ||
} | ||
@@ -299,0 +304,0 @@ return this; // for chaining |
import { Record, Tuple } from "./record"; | ||
import { Bytes } from "./bytes"; | ||
import { Value } from "./values"; | ||
import { Set, Dictionary } from "./dictionary"; | ||
import { Set, KeyedDictionary, Dictionary, DictionaryMap } from "./dictionary"; | ||
import { annotate, Annotated } from "./annotated"; | ||
import { Double, Float } from "./float"; | ||
import { Embeddable } from "./embedded"; | ||
import { Embeddable, isEmbedded } from "./embedded"; | ||
@@ -37,3 +37,3 @@ export enum ValueClass { | ||
set(s: Set<T>, k: Fold<T, R>): R; | ||
dictionary(d: Dictionary<T>, k: Fold<T, R>): R; | ||
dictionary(d: DictionaryMap<T>, k: Fold<T, R>): R; | ||
@@ -58,3 +58,3 @@ annotated(a: Annotated<T>, k: Fold<T, R>): R; | ||
set(s: Set<T>, k: Fold<T, void>): void { s.forEach(k); } | ||
dictionary(d: Dictionary<T>, k: Fold<T, void>): void { | ||
dictionary(d: DictionaryMap<T>, k: Fold<T, void>): void { | ||
d.forEach((value, key) => { k(key); k(value); }); | ||
@@ -99,4 +99,6 @@ } | ||
} | ||
dictionary(d: Dictionary<T>, k: Fold<T, Value<R>>): Value<R> { | ||
return d.mapEntries(([key, value]) => [k(key), k(value)]); | ||
dictionary(d: DictionaryMap<T>, k: Fold<T, Value<R>>): Value<R> { | ||
const result = new DictionaryMap<R>(); | ||
d.forEach((value, key) => result.set(k(key), k(value))); | ||
return result.simplifiedValue(); | ||
} | ||
@@ -194,4 +196,4 @@ annotated(a: Annotated<T>, k: Fold<T, Value<R>>): Value<R> { | ||
return o.set(v, walk); | ||
} else if (Dictionary.isDictionary<T>(v)) { | ||
return o.dictionary(v, walk); | ||
} else if (isEmbedded(v)) { | ||
return o.embedded(v, walk); | ||
} else if (Annotated.isAnnotated<T>(v)) { | ||
@@ -203,4 +205,4 @@ return o.annotated(v, walk); | ||
return o.double(v.value); | ||
} else { | ||
return ((_v: T) => o.embedded(v, walk))(v); | ||
} else if (Dictionary.isDictionary<T>(v)) { | ||
return o.dictionary(new DictionaryMap(v), walk); | ||
} | ||
@@ -207,0 +209,0 @@ default: |
@@ -1,6 +0,7 @@ | ||
import { Embeddable, GenericEmbedded } from "./embedded"; | ||
import { Embeddable, GenericEmbedded, isEmbedded } from "./embedded"; | ||
import { Bytes } from "./bytes"; | ||
import { Record, Tuple } from "./record"; | ||
import { Value } from "./values"; | ||
import { Dictionary, Set } from "./dictionary"; | ||
import { Dictionary, KeyedDictionary, Set } from "./dictionary"; | ||
import { JsDictionary } from "./jsdictionary"; | ||
@@ -42,3 +43,3 @@ export function fromJS<T extends Embeddable = GenericEmbedded>(x: any): Value<T> { | ||
if (Map.isMap(x)) { | ||
const d = new Dictionary<T>(); | ||
const d = new KeyedDictionary<T>(); | ||
x.forEach((v, k) => d.set(fromJS(k), fromJS(v))); | ||
@@ -52,4 +53,11 @@ return d; | ||
} | ||
// Just... assume it's a T. | ||
return x as T; | ||
if (isEmbedded<T>(x)) { | ||
return x; | ||
} | ||
// Handle plain JS objects to build a JsDictionary | ||
{ | ||
const r: JsDictionary<Value<T>> = {}; | ||
Object.entries(x).forEach(([k, v]) => r[k] = fromJS(v)); | ||
return r; | ||
} | ||
@@ -65,15 +73,14 @@ default: | ||
namespace Dictionary { | ||
export function fromJS<T extends Embeddable = GenericEmbedded, V extends Embeddable = GenericEmbedded>( | ||
export function stringMap<T extends Embeddable = GenericEmbedded>( | ||
x: object | ||
): Dictionary<T, Value<V>>; | ||
): KeyedDictionary<T, string, Value<T>>; | ||
} | ||
} | ||
Dictionary.fromJS = function <T extends Embeddable = GenericEmbedded, V extends Embeddable = GenericEmbedded>( | ||
Dictionary.stringMap = function <T extends Embeddable = GenericEmbedded>( | ||
x: object | ||
): Dictionary<T, Value<V>> { | ||
if (Dictionary.isDictionary<T, Value<V>>(x)) return x; | ||
const d = new Dictionary<T, Value<V>>(); | ||
Object.entries(x).forEach(([key, value]) => d.set(key, fromJS(value))); | ||
return d; | ||
): KeyedDictionary<T, string, Value<T>> { | ||
const r = new KeyedDictionary<T, string, Value<T>>(); | ||
Object.entries(x).forEach(([key, value]) => r.set(key, fromJS(value))); | ||
return r; | ||
}; |
import type { Embeddable, GenericEmbedded } from "./embedded"; | ||
import type { Annotated } from "./annotated"; | ||
import { Dictionary } from "./dictionary"; | ||
@@ -33,4 +34,15 @@ export const IsPreservesAnnotated = Symbol.for('IsPreservesAnnotated'); | ||
} | ||
{ | ||
const aMap = Dictionary.asMap(a); | ||
const bMap = Dictionary.asMap(b); | ||
if (!aMap || !bMap) return false; | ||
if (aMap.size !== bMap.size) return false; | ||
for (const k of aMap.keys()) { | ||
if (!bMap.has(k)) return false; | ||
if (!is(aMap.get(k), bMap.get(k))) return false; | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
} |
@@ -6,3 +6,3 @@ import { Record, Tuple } from "./record"; | ||
import { Value } from "./values"; | ||
import { Set, Dictionary } from "./dictionary"; | ||
import { Set, Dictionary, KeyedDictionary, DictionaryMap } from "./dictionary"; | ||
import { Annotated } from "./annotated"; | ||
@@ -13,2 +13,3 @@ import { unannotate } from "./strip"; | ||
import type { Embeddable } from "./embedded"; | ||
import { JsDictionary } from "./jsdictionary"; | ||
@@ -48,2 +49,3 @@ export function merge<T extends Embeddable>( | ||
}, | ||
array(a: Array<Value<T>>) { | ||
@@ -53,14 +55,18 @@ if (!Array.isArray(b) || Record.isRecord(b)) die(); | ||
}, | ||
set(_s: Set<T>) { die(); }, | ||
dictionary(d: Dictionary<T>) { | ||
if (!Dictionary.isDictionary<T>(b)) die(); | ||
const r = new Dictionary<T>(); | ||
d.forEach((av,ak) => { | ||
const bv = b.get(ak); | ||
dictionary(aMap: DictionaryMap<T>) { | ||
const bMap = Dictionary.asMap<T>(b); | ||
if (bMap === void 0) die(); | ||
const r = new DictionaryMap<T>(); | ||
aMap.forEach((av,ak) => { | ||
const bv = bMap.get(ak); | ||
r.set(ak, bv === void 0 ? av : walk(av, bv)); | ||
}); | ||
b.forEach((bv, bk) => { | ||
if (!d.has(bk)) r.set(bk, bv); | ||
bMap.forEach((bv, bk) => { | ||
if (!aMap.has(bk)) r.set(bk, bv); | ||
}); | ||
return r; | ||
return r.simplifiedValue(); | ||
}, | ||
@@ -67,0 +73,0 @@ |
@@ -5,3 +5,3 @@ // Patching to support node.js extensions. | ||
import { Bytes } from './bytes'; | ||
import { Set, Dictionary } from './dictionary'; | ||
import { Set, EncodableDictionary } from './dictionary'; | ||
import { stringify } from './text'; | ||
@@ -11,3 +11,3 @@ | ||
[Bytes, Annotated, Set, Dictionary].forEach((C) => { | ||
[Bytes, Annotated, Set, EncodableDictionary].forEach((C) => { | ||
(C as any).prototype[util.inspect.custom] = | ||
@@ -14,0 +14,0 @@ function (_depth: any, _options: any) { |
@@ -92,4 +92,4 @@ import { is, isAnnotated } from './is'; | ||
case 10: { | ||
const va = Array.from(a as Dictionary<T>).sort(cmp); | ||
const vb = Array.from(b as Dictionary<T>).sort(cmp); | ||
const va = Array.from(Dictionary.asMap<T>(a)!.entries()).sort(cmp); | ||
const vb = Array.from(Dictionary.asMap<T>(b)!.entries()).sort(cmp); | ||
return cmp(va, vb); | ||
@@ -96,0 +96,0 @@ } |
@@ -5,3 +5,3 @@ // Text syntax reader. | ||
import { DecodeError, ShortPacket } from './codec'; | ||
import { Dictionary, Set } from './dictionary'; | ||
import { Dictionary, DictionaryMap, Set } from './dictionary'; | ||
import { strip } from './strip'; | ||
@@ -402,18 +402,14 @@ import { Bytes, unhexDigit } from './bytes'; | ||
readDictionary(): Dictionary<T> { | ||
return this.seq(true, | ||
new Dictionary<T>(), | ||
(k, acc) => { | ||
this.state.skipws(); | ||
switch (this.state.peek()) { | ||
case ':': | ||
if (acc.has(k)) this.state.error( | ||
`Duplicate key: ${stringify(k)}`, this.state.pos); | ||
this.state.advance(); | ||
acc.set(k, this.next()); | ||
break; | ||
default: | ||
this.state.error('Missing key/value separator', this.state.pos); | ||
} | ||
}, | ||
'}'); | ||
return this.seq(true, new DictionaryMap<T>(), (k, acc) => { | ||
this.state.skipws(); | ||
switch (this.state.peek()) { | ||
case ':': | ||
this.state.advance(); | ||
if (acc.has(k)) this.state.error(`Duplicate key: ${stringify(k)}`, this.state.pos); | ||
acc.set(k, this.next()); | ||
break; | ||
default: | ||
this.state.error('Missing key/value separator', this.state.pos); | ||
} | ||
}, '}').simplifiedValue(); | ||
} | ||
@@ -420,0 +416,0 @@ |
@@ -16,2 +16,3 @@ export * from './annotated'; | ||
export * from './is'; | ||
export * from './jsdictionary'; | ||
export * from './merge'; | ||
@@ -18,0 +19,0 @@ export * from './order'; |
import { Value } from "./values"; | ||
import { Annotated } from "./annotated"; | ||
import { Record, Tuple } from "./record"; | ||
import { Set, Dictionary } from "./dictionary"; | ||
import { Set, Dictionary, DictionaryMap } from "./dictionary"; | ||
import type { Embeddable, GenericEmbedded } from "./embedded"; | ||
@@ -37,3 +37,5 @@ | ||
} else if (Dictionary.isDictionary<T>(v.item)) { | ||
return v.item.mapEntries((e) => [walk(e[0]), walk(e[1])]); | ||
const result = new DictionaryMap<T>(); | ||
new DictionaryMap<T>(v.item).forEach((val, key) => result.set(walk(key), walk(val))); | ||
return result.simplifiedValue(); | ||
} else { | ||
@@ -40,0 +42,0 @@ return v.item; |
@@ -6,3 +6,4 @@ // Preserves Values. | ||
import type { Annotated } from './annotated'; | ||
import type { Set, Dictionary } from './dictionary'; | ||
import type { JsDictionary } from './jsdictionary'; | ||
import { Set, KeyedDictionary } from './dictionary'; | ||
import type { Embeddable, GenericEmbedded } from './embedded'; | ||
@@ -31,2 +32,5 @@ | ||
| Set<T> | ||
| Dictionary<T>; | ||
// v Expanded from definition of Dictionary<> in dictionary.ts, | ||
// because of circular-use-of-Value<T> issues. | ||
| JsDictionary<Value<T>> | ||
| KeyedDictionary<T>; |
import { isAnnotated } from './is'; | ||
import { Record, Tuple } from "./record"; | ||
import type { Embeddable, GenericEmbedded, EmbeddedTypeEncode } from "./embedded"; | ||
import { Embeddable, GenericEmbedded, EmbeddedTypeEncode, isEmbedded } from "./embedded"; | ||
import { Encoder, EncoderState } from "./encoder"; | ||
@@ -8,2 +8,3 @@ import type { Value } from "./values"; | ||
import { encodeBase64 } from './base64'; | ||
import { DictionaryMap, writeDictionaryOn } from './dictionary'; | ||
@@ -302,12 +303,15 @@ export type Writable<T extends Embeddable> = | ||
} | ||
else { | ||
((v: T) => { | ||
this.state.pieces.push('#:'); | ||
if ('write' in this.embeddedWrite) { | ||
this.embeddedWrite.write(this.state, v); | ||
} else { | ||
new Writer(this.state, genericEmbeddedTypeEncode) | ||
.push(this.embeddedWrite.toValue(v)); | ||
} | ||
})(v); | ||
else if (isEmbedded(v)) { | ||
this.state.pieces.push('#:'); | ||
if ('write' in this.embeddedWrite) { | ||
this.embeddedWrite.write(this.state, v); | ||
} else { | ||
new Writer(this.state, genericEmbeddedTypeEncode) | ||
.push(this.embeddedWrite.toValue(v)); | ||
} | ||
} else { | ||
writeDictionaryOn(new DictionaryMap<T>(v), | ||
this, | ||
(k, w) => w.push(k), | ||
(v, w) => w.push(v)); | ||
} | ||
@@ -314,0 +318,0 @@ break; |
@@ -21,2 +21,4 @@ import { | ||
parse, | ||
Embedded, | ||
KeyedDictionary, | ||
} from '../src/index'; | ||
@@ -79,3 +81,3 @@ const { Tag } = Constants; | ||
it('should be done safely, even with nested dictionaries', () => { | ||
expect(canonicalEncode(fromJS(['aaa', Dictionary.fromJS({a: 1}), 'zzz'])).toHex()).is( | ||
expect(canonicalEncode(fromJS(['aaa', Dictionary.stringMap({a: 1}), 'zzz'])).toHex()).is( | ||
`b5 | ||
@@ -92,24 +94,20 @@ b103616161 | ||
describe('encoding and decoding embeddeds', () => { | ||
class LookasideEmbeddedType implements EmbeddedType<object> { | ||
readonly objects: object[]; | ||
class LookasideEmbeddedType implements EmbeddedType<Embedded<object>> { | ||
readonly objects: Embedded<object>[]; | ||
constructor(objects: object[]) { | ||
constructor(objects: Embedded<object>[]) { | ||
this.objects = objects; | ||
} | ||
decode(d: DecoderState): object { | ||
decode(d: DecoderState): Embedded<object> { | ||
return this.fromValue(new Decoder<GenericEmbedded>(d).next()); | ||
} | ||
encode(e: EncoderState, v: object): void { | ||
encode(e: EncoderState, v: Embedded<object>): void { | ||
new Encoder(e).push(this.toValue(v)); | ||
} | ||
equals(a: object, b: object): boolean { | ||
return Object.is(a, b); | ||
} | ||
fromValue(v: Value<GenericEmbedded>): object { | ||
fromValue(v: Value<GenericEmbedded>): Embedded<object> { | ||
if (typeof v !== 'number' || v < 0 || v >= this.objects.length) { | ||
throw new Error("Unknown embedded target"); | ||
throw new Error(`Unknown embedded target: ${stringify(v)}`); | ||
} | ||
@@ -119,3 +117,3 @@ return this.objects[v]; | ||
toValue(v: object): number { | ||
toValue(v: Embedded<object>): number { | ||
let i = this.objects.indexOf(v); | ||
@@ -129,4 +127,4 @@ if (i !== -1) return i; | ||
it('should encode using embeddedId when no function has been supplied', () => { | ||
const A1 = {a: 1}; | ||
const A2 = {a: 1}; | ||
const A1 = new Embedded({a: 1}); | ||
const A2 = new Embedded({a: 1}); | ||
const bs1 = canonicalEncode(A1); | ||
@@ -148,6 +146,6 @@ const bs2 = canonicalEncode(A2); | ||
it('should encode properly', () => { | ||
const objects: object[] = []; | ||
const objects: Embedded<object>[] = []; | ||
const pt = new LookasideEmbeddedType(objects); | ||
const A = {a: 1}; | ||
const B = {b: 2}; | ||
const A = new Embedded({a: 1}); | ||
const B = new Embedded({b: 2}); | ||
expect(encode([A, B], { embeddedEncode: pt })).is( | ||
@@ -161,6 +159,6 @@ Bytes.from([Tag.Sequence, | ||
it('should decode properly', () => { | ||
const objects: object[] = []; | ||
const objects: Embedded<object>[] = []; | ||
const pt = new LookasideEmbeddedType(objects); | ||
const X = {x: 123}; | ||
const Y = {y: 456}; | ||
const X = new Embedded({x: 123}); | ||
const Y = new Embedded({y: 456}); | ||
objects.push(X); | ||
@@ -176,6 +174,6 @@ objects.push(Y); | ||
it('should store embeddeds embedded in map keys correctly', () => { | ||
const A1a = {a: 1}; | ||
const A1a = new Embedded({a: 1}); | ||
const A1 = A1a; | ||
const A2 = {a: 1}; | ||
const m = new Dictionary<object, number>(); | ||
const A2 = new Embedded({a: 1}); | ||
const m = new KeyedDictionary<Embedded<object>, Value<Embedded<object>>, number>(); | ||
m.set([A1], 1); | ||
@@ -187,3 +185,3 @@ m.set([A2], 2); | ||
expect(m.get([{a: 1}])).toBeUndefined(); | ||
A1a.a = 3; | ||
A1a.value.a = 3; | ||
expect(m.get([A1])).toBe(1); | ||
@@ -318,3 +316,3 @@ }); | ||
Dictionary<GenericEmbedded>); | ||
tests.forEach((t0: Value<GenericEmbedded>, tName0: Value<GenericEmbedded>) => { | ||
Dictionary.asMap(tests).forEach((t0, tName0) => { | ||
const tName = Symbol.keyFor(strip(tName0) as symbol)!; | ||
@@ -321,0 +319,0 @@ const t = peel(t0) as Record<symbol, any, GenericEmbedded>; |
@@ -1,2 +0,2 @@ | ||
import { Bytes, Decoder, genericEmbeddedType, encode, Reader, Double } from '../src/index'; | ||
import { Bytes, Decoder, genericEmbeddedType, encode, Reader, Double, KeyedDictionary, Value } from '../src/index'; | ||
import './test-utils'; | ||
@@ -39,2 +39,21 @@ | ||
}); | ||
it('should produce a sensible JS object for symbol-keyed dictionaries', () => { | ||
expect(new Reader('{a: 1, b: 2}').next()).toEqual({a: 1, b: 2}); | ||
}); | ||
it('should produce a sensible dictionary for mixed-keyed dictionaries', () => { | ||
expect(new Reader('{a: 1, "b": 2}').next()).is( | ||
new KeyedDictionary([[Symbol.for('a'), 1], ["b", 2]] as [Value, Value][])); | ||
}); | ||
it('should produce a sensible dictionary for string-keyed dictionaries', () => { | ||
expect(new Reader('{"a": 1, "b": 2}').next()).is( | ||
new KeyedDictionary([["a", 1], ["b", 2]] as [Value, Value][])); | ||
}); | ||
it('should produce a sensible dictionary for integer-keyed dictionaries', () => { | ||
expect(new Reader('{9: 1, 8: 2}').next()).is( | ||
new KeyedDictionary([[9, 1], [8, 2]] as [Value, Value][])); | ||
}); | ||
}); |
@@ -1,2 +0,2 @@ | ||
import { Double, fromJS, Dictionary, IDENTITY_FOLD, fold, mapEmbeddeds, Value, preserves } from '../src/index'; | ||
import { Double, fromJS, IDENTITY_FOLD, fold, mapEmbeddeds, Value, preserves, KeyedDictionary, Embeddable, Embedded } from '../src/index'; | ||
import './test-utils'; | ||
@@ -11,9 +11,9 @@ | ||
describe('fold', () => { | ||
function mkv<T extends object>(t: T): Value<T> { | ||
function mkv<T extends Embeddable>(t: T): Value<T> { | ||
return fromJS<T>([ | ||
1, | ||
2, | ||
new Dictionary([[[3, 4], fromJS([5, 6])], | ||
['a', 1], | ||
['b', true]]), | ||
new KeyedDictionary<T>([[[3, 4], fromJS([5, 6])], | ||
['a', 1], | ||
['b', true]]), | ||
Double(3.4), | ||
@@ -26,8 +26,8 @@ t, | ||
const w = new Date(); | ||
const v = mkv(w); | ||
const v = mkv(new Embedded(w)); | ||
expect(fold(v, IDENTITY_FOLD)).is(v); | ||
const w1 = new Date(); | ||
const v1 = mkv(w1); | ||
const v1 = mkv(new Embedded(w1)); | ||
expect(fold(v, IDENTITY_FOLD)).not.is(v1); | ||
expect(mapEmbeddeds(v, _t => w1)).is(v1); | ||
expect(mapEmbeddeds(v, _t => new Embedded(w1))).is(v1); | ||
}); | ||
@@ -74,2 +74,8 @@ }); | ||
}); | ||
it('should compare equivalent JsDictionary and KeyedDictionary values sensibly', () => { | ||
const a = {a: 1, b: 2}; | ||
const b = new KeyedDictionary( | ||
[[Symbol.for('a'), 1], [Symbol.for('b'), 2]] as [Value, Value][]); | ||
expect(a).is(b); | ||
}); | ||
}); | ||
@@ -88,2 +94,16 @@ | ||
}); | ||
it('should format regular JS objects', () => { | ||
expect(preserves`>${({a: 1, b: 2})}<`) | ||
.toBe('>{a: 1 b: 2}<'); | ||
}); | ||
it('should format dictionaries with string keys', () => { | ||
const v = new KeyedDictionary([["a", 1], ["b", 2]]); | ||
expect(preserves`>${v}<`) | ||
.toBe('>{"a": 1 "b": 2}<'); | ||
}); | ||
it('should format dictionaries with symbol keys', () => { | ||
const v = new KeyedDictionary([[Symbol.for("a"), 1], [Symbol.for("b"), 2]]); | ||
expect(preserves`>${v}<`) | ||
.toBe('>{a: 1 b: 2}<'); | ||
}); | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
726060
130
14658