dbxlite-ui 0.3.6 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +0,0 @@
1
- function R(n){return Number.isNaN(n)||!Number.isFinite(n)?null:n}function P(n,u=!0){if(n.length===0)return 0n;let t=0n;for(let g=n.length-1;g>=0;g--)t=t<<32n|BigInt(n[g]>>>0);if(u){const g=n.length*32,c=1n<<BigInt(g-1);if(t>=c){const f=1n<<BigInt(g);t=t-f}}return t>BigInt(Number.MAX_SAFE_INTEGER)||t<BigInt(Number.MIN_SAFE_INTEGER)?t.toString():t}function C(n){return n>BigInt(Number.MAX_SAFE_INTEGER)||n<BigInt(Number.MIN_SAFE_INTEGER)?n.toString():Number(n)}function G(n){return n>BigInt(Number.MAX_SAFE_INTEGER)?n.toString():Number(n)}let _=null,w=null,b=null,q=[],F=null;const W=2,S=new Map;function L(){const n=globalThis;typeof n.gc=="function"&&n.gc()}setInterval(()=>{L()},15e3);function x(n,u,t,g,c){if(n==null)return n;if(typeof n=="object"&&n!==null&&!Array.isArray(n)&&t){let e=!1;for(const o of t.values())if(o.toLowerCase().includes("dictionary")){e=!0;break}if(e)try{const o=JSON.stringify(n);return JSON.parse(o)}catch{}}if(typeof n=="bigint")return n>Number.MAX_SAFE_INTEGER||n<Number.MIN_SAFE_INTEGER?n.toString():Number(n);if(n instanceof Date)return n.toISOString();if(typeof n!="object")return n;if(n.constructor?.name?.includes("Decimal")){const e=n.toString();try{const o=BigInt(e);if(o>BigInt(Number.MAX_SAFE_INTEGER)||o<BigInt(Number.MIN_SAFE_INTEGER))return e}catch{}return Number(e)}const m=Array.isArray(n),N=m?[]:{},d=[];if(m)for(let e=n.length-1;e>=0;e--)d.push([n[e],N,e,void 0]);else{const e=Object.keys(n);for(let o=e.length-1;o>=0;o--){const a=e[o];d.push([n[a],N,a,a])}}for(;d.length>0;){const[e,o,a,p]=d.pop();if(e==null){o[a]=e;continue}if(typeof e=="bigint"){e>Number.MAX_SAFE_INTEGER||e<Number.MIN_SAFE_INTEGER?o[a]=e.toString():o[a]=Number(e);continue}if(e instanceof Date){o[a]=e.toISOString();continue}if(typeof e!="object"){o[a]=e;continue}if(p&&t?.has(p)&&t.get(p).toLowerCase().includes("dictionary")){const s=String(e);if(s&&!s.startsWith("[object ")){o[a]=s;continue}}const A=e.constructor?.name;if(A?.includes("Decimal")){const r=e.toString();if(p&&u?.has(p)){const s=u.get(p);if(s>0){const y=Number(r),h=Math.pow(10,s);o[a]=y/h;continue}}try{const s=BigInt(r);s>BigInt(Number.MAX_SAFE_INTEGER)||s<BigInt(Number.MIN_SAFE_INTEGER)?o[a]=r:o[a]=Number(r)}catch{o[a]=Number(r)}continue}if(e instanceof Uint8Array){const r=Array.from(e).map(s=>s.toString(16).padStart(2,"0")).join("");o[a]=`0x${r}`;continue}if(e instanceof Uint32Array){const r=P(e);typeof r=="bigint"?o[a]=Number(r):o[a]=r;continue}if(e instanceof BigInt64Array){e.length===1?o[a]=C(e[0]):o[a]=Array.from(e).map(r=>C(r));continue}if(e instanceof BigUint64Array){e.length===1?o[a]=G(e[0]):o[a]=Array.from(e).map(r=>G(r));continue}if(e instanceof Float32Array){if(e.length===1){const r=R(e[0]);r===null&&(e[0],void 0),o[a]=r}else o[a]=Array.from(e).map(r=>R(r));continue}if(e instanceof Float64Array){if(e.length===1){const r=R(e[0]);r===null&&(e[0],void 0),o[a]=r}else o[a]=Array.from(e).map(r=>R(r));continue}if(p&&t?.get(p)?.toLowerCase().includes("interval")){let r=0,s=0,y=0;const h=g?.get(p);if(h&&c!==void 0){const l=c*4;if(l+3<h.length){r=h[l],s=h[l+1];const E=h[l+2]>>>0,M=h[l+3];y=E+M*4294967296}}else if(A==="Int32Array"){const l=e;l.length>=2&&(r=l[0]*12+l[1])}const i=[];if(r!==0){const l=Math.floor(Math.abs(r)/12),E=Math.abs(r)%12,M=r<0?"-":"";l!==0&&i.push(`${M}${l}y`),E!==0&&i.push(`${r<0&&l===0?"-":""}${E}mo`)}if(s!==0){const l=s<0?"-":"";i.push(`${l}${Math.abs(s)}d`)}if(y!==0){const l=Math.abs(y),E=y<0?"-":"",M=Math.floor(l/36e11),O=Math.floor(l%36e11/6e10),v=l%6e10,B=Math.floor(v/1e9),I=v%1e9;if(M!==0&&i.push(`${E}${M}h`),O!==0&&i.push(`${O}m`),B!==0||I!==0)if(I===0)i.push(`${B}s`);else if(I%1e6===0){const $=I/1e6;i.push(`${B}.${$.toString().padStart(3,"0")}s`)}else if(I%1e3===0){const $=I/1e3;i.push(`${B}.${$.toString().padStart(6,"0")}s`)}else i.push(`${B}.${I.toString().padStart(9,"0")}s`)}o[a]=i.length>0?i.join(" "):"0";continue}if(Array.isArray(e)){const r=[];o[a]=r;for(let s=e.length-1;s>=0;s--)d.push([e[s],r,s,p]);continue}if(typeof e.toJSON=="function"){const r=e.toJSON();d.push([r,o,a,p]);continue}if(e instanceof Map){const r={};o[a]=r;const s=Array.from(e.entries());for(let y=s.length-1;y>=0;y--){const[h,i]=s[y];d.push([i,r,String(h),String(h)])}continue}const T={};o[a]=T;const k=Object.keys(e);for(let r=k.length-1;r>=0;r--){const s=k[r];d.push([e[s],T,s,s])}}return N}async function U(){try{b&&await b.close(),w&&await w.terminate()}catch{}w=null,b=null}function X(n){return{mvp:{mainModule:`${n}/duckdb-mvp.wasm`,mainWorker:`${n}/duckdb-browser-mvp.worker.js`},eh:{mainModule:`${n}/duckdb-eh.wasm`,mainWorker:`${n}/duckdb-browser-eh.worker.js`},coi:{mainModule:`${n}/duckdb-coi.wasm`,mainWorker:`${n}/duckdb-browser-coi.worker.js`,pthreadWorker:`${n}/duckdb-browser-coi.pthread.worker.js`}}}async function z(n){for(const u of n)try{const t=u.eh?.mainModule||u.mvp?.mainModule;if(t&&(await fetch(t,{method:"HEAD"})).ok)return u}catch{}return null}self.addEventListener("message",async n=>{const u=n.data;try{if(u.type==="init"){const t=u.baseUrls||[];if(q=t,!_){const e=await import("./duckdb-browser-BX0LtQQW.js");_=e,F=e.DuckDBDataProtocol}const g=new _.ConsoleLogger,c=t.map(e=>X(e));let f=await z(c);f||(f=_.getJsDelivrBundles());const m=f.eh||await _.selectBundle(f),N=new Worker(m.mainWorker);w=new _.AsyncDuckDB(g,N),await w.instantiate(m.mainModule,m.pthreadWorker);let d=!1;await w.open({path:":memory:",accessMode:_.DuckDBAccessMode.READ_WRITE}),b=await w.connect();try{await b.query("SET memory_limit=-1"),await b.query("SET threads=1"),await b.query("SET preserve_insertion_order=false");const e=Intl.DateTimeFormat().resolvedOptions().timeZone;e&&await b.query(`SET TimeZone='${e}'`)}catch(e){console.warn("[DuckDB] Config failed (non-critical):",e)}self.postMessage({type:"inited",opfsPersistence:d})}else if(u.type==="run"){const{id:t,sql:g}=u;S.set(t,0);try{if(b&&typeof b.queryArrowIPC=="function"){const c=await b.queryArrowIPC(g);for await(const f of c){for(;(S.get(t)||0)>=W;)await new Promise(m=>setTimeout(m,10));try{self.postMessage({type:"arrow",id:t,buffer:f},[f])}catch{self.postMessage({type:"arrow",id:t,buffer:f})}S.set(t,(S.get(t)||0)+1)}}else{const c=await b.query(g),f=new Map;if(c.schema?.fields){for(const m of c.schema.fields)if(m.type?.toString()?.includes("Interval")){const N=c.getChild(m.name);N?.data?.[0]?.values instanceof Int32Array&&f.set(m.name,N.data[0].values)}}if(c.toArrow){const m=c.toArrow();self.postMessage({type:"arrow",id:t,buffer:m},[m])}else{let m,N;if(c.schema&&c.schema.fields){m=new Map,N=new Map;const s=c.schema.fields.map(i=>{let l="UNKNOWN";return i.type&&typeof i.type.toString=="function"&&(l=i.type.toString()),N.set(i.name,l),i.type&&i.type.constructor?.name?.includes("Decimal")&&i.type.scale!==void 0&&m.set(i.name,i.type.scale),{name:i.name,type:l,nullable:i.nullable}}),y=JSON.stringify(s),h=new TextEncoder().encode(y).buffer;self.postMessage({type:"json-schema",id:t,buffer:h},[h])}const d=c.toArray?c.toArray():c,e=5*1024*1024,o=500,a=5e5;let p=[],A=0,D=0,T=0,k=0,r=0;for(let s=0;s<d.length;s++)try{const y=x(d[s],m,N,f,s),i=JSON.stringify(y).length;if(D+=i,i>k&&(k=i),i>a&&T++,p.length>0&&(A+i>e||p.length>=o)){const l=JSON.stringify(p),E=new TextEncoder().encode(l).buffer;for(self.postMessage({type:"json",id:t,buffer:E},[E]),r++,S.set(t,(S.get(t)||0)+1);(S.get(t)||0)>=W;)await new Promise(M=>setTimeout(M,10));p=[],A=0}p.push(y),A+=i}catch(y){const h=d[s],i=h&&typeof h=="object"?Object.keys(h):[];console.error(`[DuckDB Worker] Row ${s} failed to serialize:`,{error:String(y),errorName:y.name,rowKeys:i.slice(0,10)}),p.push({__error:`Row ${s} failed: ${y.name}`}),A+=100}if(p.length>0){const s=JSON.stringify(p),y=new TextEncoder().encode(s).buffer;self.postMessage({type:"json",id:t,buffer:y},[y]),r++,S.set(t,(S.get(t)||0)+1)}self.postMessage({type:"query-stats",id:t,stats:{totalRows:d.length,totalBytes:D,largeRowCount:T,maxRowSize:k,chunkCount:r,avgRowSize:d.length>0?Math.round(D/d.length):0}})}}}catch(c){const f=String(c);f.includes("database has been invalidated")||f.includes("FATAL Error")?(await U(),self.postMessage({type:"error",id:t,error:"Database encountered a fatal error and has been reset. Please reload the page to reinitialize."})):f.includes("Out of Memory")||f.includes("Allocation failure")?self.postMessage({type:"error",id:t,error:`Out of Memory: The query or file is too large to process in the browser. Try:
2
- 1. Querying smaller subsets using LIMIT or WHERE clauses
3
- 2. Using SELECT with specific columns instead of SELECT *
4
- 3. Processing smaller files
5
- 4. Using COUNT(*) with LIMIT for large tables`}):self.postMessage({type:"error",id:t,error:f})}finally{self.postMessage({type:"done",id:t}),S.delete(t),L()}}else if(u.type==="ack"){const{id:t}=u,g=S.get(t)||0;S.set(t,Math.max(0,g-1))}else if(u.type==="cancel"){const{id:t}=u;self.postMessage({type:"cancelled",id:t})}else if(u.type==="register_file"){const{id:t,fileName:g,fileBuffer:c}=u;try{if(!w)throw new Error("Database not initialized");await w.registerFileBuffer(g,new Uint8Array(c)),self.postMessage({type:"file_registered",id:t})}catch(f){self.postMessage({type:"error",id:t,error:String(f)})}}else if(u.type==="copy_file_to_buffer"){const{id:t,fileName:g}=u;try{if(!w)throw new Error("Database not initialized");const c=await w.copyFileToBuffer(g);self.postMessage({type:"file_buffer",id:t,buffer:c},[c.buffer])}catch(c){self.postMessage({type:"error",id:t,error:String(c)})}}else if(u.type==="register_file_handle"){const{id:t,fileName:g,file:c}=u;try{if(!w)throw new Error("Database not initialized");if(!F)throw new Error("DuckDBDataProtocol not loaded");await w.registerFileHandle(g,c,F.BROWSER_FILEREADER,!0),self.postMessage({type:"file_registered",id:t})}catch(f){self.postMessage({type:"error",id:t,error:String(f)})}}}catch(t){self.postMessage({type:"error",error:String(t)})}});