14 lines
9.2 KiB
JavaScript
14 lines
9.2 KiB
JavaScript
(function (global, factory) {
|
|
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
typeof define === 'function' && define.amd ? define(factory) :
|
|
(global.jsonExt = factory());
|
|
}(typeof globalThis != 'undefined' ? globalThis : typeof window != 'undefined' ? window : typeof global != 'undefined' ? global : typeof self != 'undefined' ? self : this, (function () {
|
|
var exports=(()=>{var L=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var W=Object.prototype.hasOwnProperty;var K=(e,t)=>{for(var n in t)L(e,n,{get:t[n],enumerable:!0})},_=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of V(t))!W.call(e,i)&&i!==n&&L(e,i,{get:()=>t[i],enumerable:!(r=M(t,i))||r.enumerable});return e};var $=e=>_(L({},"__esModule",{value:!0}),e);var it={};K(it,{createStringifyWebStream:()=>z,parseChunked:()=>I,parseFromWebStream:()=>J,stringifyChunked:()=>m,stringifyInfo:()=>q});function O(e){return typeof e=="object"&&e!==null&&(typeof e[Symbol.iterator]=="function"||typeof e[Symbol.asyncIterator]=="function")}function F(e,t,n,r){switch(n&&typeof n.toJSON=="function"&&(n=n.toJSON()),r!==null&&(n=r.call(e,String(t),n)),typeof n){case"function":case"symbol":n=void 0;break;case"object":if(n!==null){let i=n.constructor;(i===String||i===Number||i===Boolean)&&(n=n.valueOf())}break}return n}function B(e){return typeof e=="function"?e:Array.isArray(e)?[...new Set(e.map(n=>{let r=n&&n.constructor;return r===String||r===Number?String(n):null}).filter(n=>typeof n=="string"))]:null}function N(e){return typeof e=="number"?!Number.isFinite(e)||e<1?!1:" ".repeat(Math.min(e,10)):typeof e=="string"&&e.slice(0,10)||!1}var C=1,U=2,Y=new TextDecoder;function G(e,t){return e.name==="SyntaxError"&&t.jsonParseOffset&&(e.message=e.message.replace(/at position (\d+)/,(n,r)=>"at position "+(Number(r)+t.jsonParseOffset))),e}function H(e,t){let n=e.length;e.length+=t.length;for(let r=0;r<t.length;r++)e[n+r]=t[r]}async function I(e){let t=typeof e=="function"?e():e;if(O(t)){let n=new P;try{for await(let r of t){if(typeof r!="string"&&!ArrayBuffer.isView(r))throw new TypeError("Invalid chunk: Expected string, TypedArray or Buffer");n.push(r)}return n.finish()}catch(r){throw G(r,n)}}throw new TypeError("Invalid chunk emitter: Expected an Iterable, AsyncIterable, generator, async generator, or a function returning an Iterable or AsyncIterable")}var P=class{constructor(){this.value=void 0,this.valueStack=null,this.stack=new Array(100),this.lastFlushDepth=0,this.flushDepth=0,this.stateString=!1,this.stateStringEscape=!1,this.pendingByteSeq=null,this.pendingChunk=null,this.chunkOffset=0,this.jsonParseOffset=0}parseAndAppend(t,n){this.stack[this.lastFlushDepth-1]===C?(n&&(this.jsonParseOffset--,t="{"+t+"}"),Object.assign(this.valueStack.value,JSON.parse(t))):(n&&(this.jsonParseOffset--,t="["+t+"]"),H(this.valueStack.value,JSON.parse(t)))}prepareAddition(t){let{value:n}=this.valueStack;if(Array.isArray(n)?n.length!==0:Object.keys(n).length!==0){if(t[0]===",")return this.jsonParseOffset++,t.slice(1);if(t[0]!=="}"&&t[0]!=="]")return this.jsonParseOffset-=3,"[[]"+t}return t}flush(t,n,r){let i=t.slice(n,r);if(this.jsonParseOffset=this.chunkOffset+n,this.pendingChunk!==null&&(i=this.pendingChunk+i,this.jsonParseOffset-=this.pendingChunk.length,this.pendingChunk=null),this.flushDepth===this.lastFlushDepth)this.flushDepth>0?this.parseAndAppend(this.prepareAddition(i),!0):(this.value=JSON.parse(i),this.valueStack={value:this.value,prev:null});else if(this.flushDepth>this.lastFlushDepth){for(let s=this.flushDepth-1;s>=this.lastFlushDepth;s--)i+=this.stack[s]===C?"}":"]";this.lastFlushDepth===0?(this.value=JSON.parse(i),this.valueStack={value:this.value,prev:null}):this.parseAndAppend(this.prepareAddition(i),!0);for(let s=this.lastFlushDepth||1;s<this.flushDepth;s++){let l=this.valueStack.value;if(this.stack[s-1]===C){let g;for(g in l);l=l[g]}else l=l[l.length-1];this.valueStack={value:l,prev:this.valueStack}}}else{i=this.prepareAddition(i);for(let s=this.lastFlushDepth-1;s>=this.flushDepth;s--)this.jsonParseOffset--,i=(this.stack[s]===C?"{":"[")+i;this.parseAndAppend(i,!1);for(let s=this.lastFlushDepth-1;s>=this.flushDepth;s--)this.valueStack=this.valueStack.prev}this.lastFlushDepth=this.flushDepth}push(t){if(typeof t!="string"){if(this.pendingByteSeq!==null){let s=t;t=new Uint8Array(this.pendingByteSeq.length+s.length),t.set(this.pendingByteSeq),t.set(s,this.pendingByteSeq.length),this.pendingByteSeq=null}if(t[t.length-1]>127)for(let s=0;s<t.length;s++){let l=t[t.length-1-s];if(l>>6===3){s++,(s!==4&&l>>3===30||s!==3&&l>>4===14||s!==2&&l>>5===6)&&(this.pendingByteSeq=t.slice(t.length-s),t=t.slice(0,-s));break}}t=Y.decode(t)}let n=t.length,r=0,i=0;t:for(let s=0;s<n;s++){if(this.stateString){for(;s<n;s++)if(this.stateStringEscape)this.stateStringEscape=!1;else switch(t.charCodeAt(s)){case 34:this.stateString=!1;continue t;case 92:this.stateStringEscape=!0}break}switch(t.charCodeAt(s)){case 34:this.stateString=!0,this.stateStringEscape=!1;break;case 44:i=s;break;case 123:i=s+1,this.stack[this.flushDepth++]=C;break;case 91:i=s+1,this.stack[this.flushDepth++]=U;break;case 93:case 125:i=s+1,this.flushDepth--,this.flushDepth<this.lastFlushDepth&&(this.flush(t,r,i),r=i);break;case 9:case 10:case 13:case 32:r===s&&r++,i===s&&i++;break}}i>r&&this.flush(t,r,i),i<n&&(this.pendingChunk!==null?this.pendingChunk+=t:this.pendingChunk=t.slice(i,n)),this.chunkOffset+=n}finish(){return this.pendingChunk!==null&&(this.flush("",0,0),this.pendingChunk=null),this.value}};function E(e){return/[^\x20\x21\x23-\x5B\x5D-\uD799]/.test(e)?JSON.stringify(e):'"'+e+'"'}function*m(e,t,n){(t===null||Array.isArray(t)||typeof t!="object")&&(t={replacer:t,space:n});let r=Number(t.highWaterMark)||16384,i=B(t.replacer);n=N(t.space);let s=new Map,l=[],g={"":e},u=null,x=()=>D("",e),y=g,d=!0,h=[""],c=0,a="",k=Object.keys;if(Array.isArray(i)){let f=i;k=()=>f,i=null}for(;x(),!((a.length>=r||u===null)&&(yield a,a="",u===null)););function w(){if(c===0&&(h=k(y),a+="{"),c===h.length){a+=n&&!d?`
|
|
${n.repeat(l.length-1)}}`:"}",A();return}let f=h[c++];D(f,y[f])}function o(){if(c===0&&(a+="["),c===y.length){a+=n&&!d?`
|
|
${n.repeat(l.length-1)}]`:"]",A();return}D(c,y[c++])}function j(f){if(d?d=!1:a+=",",n&&u!==null&&(a+=`
|
|
${n.repeat(l.length)}`),x===w){let p=s.get(f);p===void 0&&s.set(f,p=E(f)+(n?": ":":")),a+=p}}function D(f,p){if(p=F(y,f,p,i),p===null||typeof p!="object")(x!==w||p!==void 0)&&(j(f),b(p));else{if(l.includes(p))throw new TypeError("Converting circular structure to JSON");j(f),l.push(p),S(),x=Array.isArray(p)?o:w,y=p,d=!0,c=0}}function b(f){switch(typeof f){case"string":a+=E(f);break;case"number":a+=Number.isFinite(f)?String(f):"null";break;case"boolean":a+=f?"true":"false";break;case"undefined":case"object":a+="null";break;default:throw new TypeError(`Do not know how to serialize a ${f.constructor?.name||typeof f}`)}}function S(){u={keys:h,index:c,prev:u}}function A(){l.pop();let f=l.length>0?l[l.length-1]:g;x=Array.isArray(f)?o:w,y=f,d=!1,h=u.keys,c=u.index,u=u.prev}}var Q=typeof Object.hasOwn=="function"?Object.hasOwn:(e,t)=>Object.hasOwnProperty.call(e,t),X={8:"\\b",9:"\\t",10:"\\n",12:"\\f",13:"\\r",34:'\\"',92:"\\\\"},Z=Uint8Array.from({length:2048},(e,t)=>Q(X,t)?2:t<32?6:t<128?1:2);function R(e){return e>=55296&&e<=56319}function v(e){return e>=56320&&e<=57343}function T(e){if(!/[^\x20\x21\x23-\x5B\x5D-\x7F]/.test(e))return e.length+2;let t=0,n=!1;for(let r=0;r<e.length;r++){let i=e.charCodeAt(r);if(i<2048)t+=Z[i];else if(R(i)){t+=6,n=!0;continue}else v(i)?t=n?t-2:t+6:t+=3;n=!1}return t+2}function tt(e){let t=0;return e<0&&(t=1,e=-e),e>=1e9&&(t+=9,e=(e-e%1e9)/1e9),e>=1e4?e>=1e6?t+(e>=1e8?9:e>=1e7?8:7):t+(e>=1e5?6:5):t+(e>=100?e>=1e3?4:3:e>=10?2:1)}function et(e){switch(typeof e){case"string":return T(e);case"number":return Number.isFinite(e)?Number.isInteger(e)?tt(e):String(e).length:4;case"boolean":return e?4:5;case"undefined":case"object":return 4;default:return 0}}function nt(e){return e=N(e),typeof e=="string"?e.length:0}function q(e,t,n){(t===null||Array.isArray(t)||typeof t!="object")&&(t={replacer:t,space:n});let r=!!t.continueOnCircular,i=B(t.replacer),s=Object.keys;if(Array.isArray(i)){let k=i;s=()=>k,i=null}n=nt(n);let l=new Map,g=new Map,u=[],x=new Set,y={"":e},d=!1,h=0,c=0;return a(y,"",e),h===0&&(h+=9),{bytes:isNaN(h)?1/0:h,circular:[...x]};function a(k,w,o){if(!d)if(o=F(k,w,o,i),o===null||typeof o!="object")(o!==void 0||Array.isArray(k))&&(h+=et(o));else{if(u.includes(o)){x.add(o),h+=4,r||(d=!0);return}if(g.has(o)){h+=g.get(o);return}c++;let j=c,D=h,b=0;if(u.push(o),Array.isArray(o)){b=o.length;for(let S=0;S<b;S++)a(o,S,o[S])}else{let S=h;for(let A of s(o))if(a(o,A,o[A]),S!==h){let f=l.get(A);f===void 0&&l.set(A,f=T(A)+(n>0?2:1)),h+=f,b++,S=h}}h+=b===0?2:1+b,n>0&&b>0&&(h+=(1+u.length*n)*b+1+(u.length-1)*n),u.pop(),j!==c&&g.set(o,h-D)}}}function J(e){return I(O(e)?e:async function*(){let t=e.getReader();try{for(;;){let{value:n,done:r}=await t.read();if(r)break;yield n}}finally{t.releaseLock()}})}function z(e,t,n){return typeof ReadableStream.from=="function"?ReadableStream.from(m(e,t,n)):new ReadableStream({start(){this.generator=m(e,t,n)},pull(r){let{value:i,done:s}=this.generator.next();s?r.close():r.enqueue(i)},cancel(){this.generator=null}})}return $(it);})();
|
|
|
|
return exports;
|
|
})));
|
|
//# sourceMappingURL=json-ext.min.js.map
|