!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).jsonExt=e()}(this,(function(){"use strict";function t(t){return"function"==typeof t.pipe&&"function"==typeof t._read&&"object"==typeof t._readableState&&null!==t._readableState}var e={escapableCharCodeSubstitution:{8:"\\b",9:"\\t",10:"\\n",12:"\\f",13:"\\r",34:'\\"',92:"\\\\"},isLeadingSurrogate:function(t){return t>=55296&&t<=56319},isTrailingSurrogate:function(t){return t>=56320&&t<=57343},type:{PRIMITIVE:1,PROMISE:4,ARRAY:3,OBJECT:2,STRING_STREAM:5,OBJECT_STREAM:6},isReadableStream:t,replaceValue:function(t,e,s,n){switch(s&&"function"==typeof s.toJSON&&(s=s.toJSON()),null!==n&&(s=n.call(t,String(e),s)),typeof s){case"function":case"symbol":s=void 0;break;case"object":if(null!==s){const t=s.constructor;t!==String&&t!==Number&&t!==Boolean||(s=s.valueOf())}}return s},getTypeNative:function(t){return null===t||"object"!=typeof t?1:Array.isArray(t)?3:2},getTypeAsync:function(e){return null===e||"object"!=typeof e?1:"function"==typeof e.then?4:t(e)?e._readableState.objectMode?6:5:Array.isArray(e)?3:2},normalizeReplacer:function(t){if("function"==typeof t)return t;if(Array.isArray(t)){const e=new Set(t.map((t=>"string"==typeof t||"number"==typeof t?String(t):null)).filter((t=>"string"==typeof t)));return e.add(""),(t,s)=>e.has(t)?s:void 0}return null},normalizeSpace:function(t){return"number"==typeof t?!(!Number.isFinite(t)||t<1)&&" ".repeat(Math.min(t,10)):"string"==typeof t&&t.slice(0,10)||!1}};const{normalizeReplacer:s,normalizeSpace:n,replaceValue:i,getTypeNative:a,getTypeAsync:r,isLeadingSurrogate:l,isTrailingSurrogate:h,escapableCharCodeSubstitution:u,type:{PRIMITIVE:o,OBJECT:c,ARRAY:f,PROMISE:p,STRING_STREAM:g,OBJECT_STREAM:d}}=e,S=Array.from({length:2048}).map(((t,e)=>u.hasOwnProperty(e)?2:e<32?6:e<128?1:2));function y(t){let e=0,s=!1;for(let n=0;n"at position "+(Number(s)+e.jsonParseOffset)))),t}function D(t,e){const s=t.length;t.length+=e.length;for(let n=0;n0?(this.jsonParseOffset--,1===this.stack[this.flushDepth-1]?Object.assign(this.valueStack.value,JSON.parse("{"+n+"}")):D(this.valueStack.value,JSON.parse("["+n+"]"))):(this.value=JSON.parse(n),this.valueStack={value:this.value,prev:null});else if(this.flushDepth>this.lastFlushDepth){for(let t=this.flushDepth-1;t>=this.lastFlushDepth;t--)n+=1===this.stack[t]?"}":"]";0===this.lastFlushDepth?(this.value=JSON.parse(n),this.valueStack={value:this.value,prev:null}):(this.jsonParseOffset--,1===this.stack[this.lastFlushDepth-1]?Object.assign(this.valueStack.value,JSON.parse("{"+n+"}")):D(this.valueStack.value,JSON.parse("["+n+"]")));for(let t=this.lastFlushDepth||1;t=this.flushDepth;t--)this.jsonParseOffset--,n=(1===this.stack[t]?"{":"[")+n;1===this.stack[this.lastFlushDepth-1]?Object.assign(this.valueStack.value,JSON.parse(n)):D(this.valueStack.value,JSON.parse(n));for(let t=this.lastFlushDepth-1;t>=this.flushDepth;t--)this.valueStack=this.valueStack.prev}this.pos+=s-e,this.lastFlushDepth=this.flushDepth}push(t){if("string"!=typeof t){if(null!==this.pendingByteSeq){const e=t;(t=new Uint8Array(this.pendingByteSeq.length+e.length)).set(this.pendingByteSeq),t.set(e,this.pendingByteSeq.length),this.pendingByteSeq=null}if(t[t.length-1]>127)for(let e=0;e>6==3){e++,(4!==e&&s>>3==30||3!==e&&s>>4==14||2!==e&&s>>5==6)&&(this.pendingByteSeq=t.slice(t.length-e),t=t.slice(0,-e));break}}t=v.decode(t)}const e=t.length;let s=0,n=0;t:for(let i=0;is&&this.flush(t,s,n),n1&&(w+=s-1),S.delete(a),l>0&&s>0&&(w+=(1+(S.size+1)*l+1)*s,w+=1+S.size*l),u.set(a,w-e);break}case f:{if(u.has(a)){b.add(a),w+=u.get(a);break}const e=w;w+=2,S.add(a);for(let e=0;e1&&(w+=a.length-1),S.delete(a),l>0&&a.length>0&&(w+=(1+(S.size+1)*l)*a.length,w+=1+S.size*l),u.set(a,w-e);break}case p:case g:v.add(a);break;case d:w+=2,v.add(a)}}(O,"",t),{minLength:isNaN(w)?1/0:w,circular:[...k],duplicate:[...b],async:[...v]}},stringifyStream:()=>{throw new Error("Method is not supported")},parseChunked:function(t){let e=new w;if(m(t)&&k(t))return new Promise(((s,n)=>{t.on("data",(t=>{try{e.push(t)}catch(t){n(O(t,e)),e=null}})).on("error",(t=>{e=null,n(t)})).on("end",(()=>{try{s(e.finish())}catch(t){n(O(t,e))}finally{e=null}}))}));if("function"==typeof t){const s=t();if(m(s)&&(Symbol.iterator in s||Symbol.asyncIterator in s))return new Promise((async(t,n)=>{try{for await(const t of s)e.push(t);t(e.finish())}catch(t){n(O(t,e))}finally{e=null}}))}throw new Error("Chunk emitter should be readable stream, generator, async generator or function returning an iterable object")}}}));