UNPKG

cbor2

Version:

Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC8949).

2 lines (1 loc) 1.45 kB
import{DecodeStream as a}from"./decodeStream.js";import{CBORcontainer as s}from"./container.js";import{SYMS as u}from"./constants.js";function c(i){const e={...s.defaultDecodeOptions};if(i.dcbor?Object.assign(e,s.dcborDecodeOptions):i.cde&&Object.assign(e,s.cdeDecodeOptions),Object.assign(e,i),Object.hasOwn(e,"rejectLongNumbers"))throw new TypeError("rejectLongNumbers has changed to requirePreferred");return e.boxed&&(e.saveOriginal=!0),e}class d{parent=void 0;ret=void 0;step(e,n,t){if(this.ret=s.create(e,this.parent,n,t),e[2]===u.BREAK)if(this.parent?.isStreaming)this.parent.left=0;else throw new Error("Unexpected BREAK");else this.parent&&this.parent.push(this.ret,t,e[3]);for(this.ret instanceof s&&(this.parent=this.ret);this.parent?.done;){this.ret=this.parent.convert(t);const r=this.parent.parent;r?.replaceLast(this.ret,this.parent,t),this.parent=r}}}function l(i,e={}){const n=c(e),t=new a(i,n),r=new d;for(const o of t)r.step(o,n,t);return r.ret}class O{#t;#e;constructor(e,n={}){const t=new a(e,c(n));this.#t=t.seq()}peek(){return this.#e||(this.#e=this.#n()),this.#e}read(){const e=this.#e??this.#n();return this.#e=void 0,e}*[Symbol.iterator](){for(;;){const e=this.read();if(!e)return;yield e}}#n(){const{value:e,done:n}=this.#t.next();if(!n)return e}}function*b(i,e={}){const n=c(e),t=new a(i,n),r=new d;for(const o of t.seq())r.step(o,n,t),r.parent||(yield r.ret)}export{O as SequenceEvents,l as decode,b as decodeSequence};