@lunora/d1 1.0.0-alpha.101 → 1.0.0-alpha.103

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.
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{exportGlobalRows as o,importGlobalRows as t,selectGlobalTables as a}from"./packem_shared/exportGlobalRows-BvGvq9Jy.mjs";import{D1Client as i,D1Session as n}from"./packem_shared/D1Client-CzS0Qfa6.mjs";import{backfillD1SearchIndexes as D,createD1CtxDb as b,readD1CdcChanges as p,runD1AggregateMigrations as x,runD1CdcMigration as c,runD1GlobalTableMigrations as g,runD1RankMigrations as f,runD1SearchMigrations as m,sweepD1CdcRetention as C}from"./packem_shared/backfillD1SearchIndexes-V-i_kEvG.mjs";import{facetGlobalColumn as d,listGlobalTables as G,readGlobalTablePage as M}from"./packem_shared/facetGlobalColumn-FEtvbxMP.mjs";import{MigrationRunner as T}from"./packem_shared/MigrationRunner-SadkIbU5.mjs";import{D1TimeoutError as w,isTransientD1Error as S,retryingExec as y,withD1Retry as E}from"./packem_shared/D1TimeoutError-D4YUqZPk.mjs";import{default as q}from"./packem_shared/sqliteDialect-J6_WTDHg.mjs";import{applyCdcChanges as I}from"@lunora/shard-engine";import{createSqlCtxDb as j}from"@lunora/sql-store";export{i as D1Client,n as D1Session,w as D1TimeoutError,T as MigrationRunner,I as applyCdcChanges,D as backfillD1SearchIndexes,b as createD1CtxDb,j as createSqlCtxDb,o as exportGlobalRows,d as facetGlobalColumn,t as importGlobalRows,S as isTransientD1Error,G as listGlobalTables,p as readD1CdcChanges,M as readGlobalTablePage,y as retryingExec,x as runD1AggregateMigrations,c as runD1CdcMigration,g as runD1GlobalTableMigrations,f as runD1RankMigrations,m as runD1SearchMigrations,a as selectGlobalTables,q as sqliteDialect,C as sweepD1CdcRetention,E as withD1Retry};
1
+ import{exportGlobalRows as o,importGlobalRows as t,selectGlobalTables as a}from"./packem_shared/exportGlobalRows-BjvA5Ow_.mjs";import{D1Client as i,D1Session as n}from"./packem_shared/D1Client-CzS0Qfa6.mjs";import{backfillD1SearchIndexes as D,createD1CtxDb as b,readD1CdcChanges as p,runD1AggregateMigrations as x,runD1CdcMigration as c,runD1GlobalTableMigrations as g,runD1RankMigrations as f,runD1SearchMigrations as m,sweepD1CdcRetention as C}from"./packem_shared/backfillD1SearchIndexes-V-i_kEvG.mjs";import{facetGlobalColumn as d,listGlobalTables as G,readGlobalTablePage as M}from"./packem_shared/facetGlobalColumn-DC1jYCrB.mjs";import{MigrationRunner as T}from"./packem_shared/MigrationRunner-SadkIbU5.mjs";import{D1TimeoutError as w,isTransientD1Error as S,retryingExec as y,withD1Retry as E}from"./packem_shared/D1TimeoutError-D4YUqZPk.mjs";import{default as q}from"./packem_shared/sqliteDialect-J6_WTDHg.mjs";import{applyCdcChanges as I}from"@lunora/shard-engine";import{createSqlCtxDb as j}from"@lunora/sql-store";export{i as D1Client,n as D1Session,w as D1TimeoutError,T as MigrationRunner,I as applyCdcChanges,D as backfillD1SearchIndexes,b as createD1CtxDb,j as createSqlCtxDb,o as exportGlobalRows,d as facetGlobalColumn,t as importGlobalRows,S as isTransientD1Error,G as listGlobalTables,p as readD1CdcChanges,M as readGlobalTablePage,y as retryingExec,x as runD1AggregateMigrations,c as runD1CdcMigration,g as runD1GlobalTableMigrations,f as runD1RankMigrations,m as runD1SearchMigrations,a as selectGlobalTables,q as sqliteDialect,C as sweepD1CdcRetention,E as withD1Retry};
@@ -0,0 +1 @@
1
+ import{toErrorBody as p}from"@lunora/errors";import{n as E,e as m,d as g}from"./wire-codec-C2Avu56N.mjs";import{runD1GlobalTableMigrations as w}from"./backfillD1SearchIndexes-V-i_kEvG.mjs";import{quoteIdentifier as u}from"./quoteIdentifier-CObIFRhb.mjs";import{decodeGlobalRow as R}from"@lunora/sql-store";const O=200,k=(t,e)=>{const o=r=>t.tables[r]?.shardMode?.kind==="global";return e&&e.length>0?e.filter(r=>o(r)):Object.keys(t.tables).filter(r=>o(r))},h=(t,e,o)=>{const r=t.tables[e];return r?R(r,o):{_creationTime:o._creationTime,_id:o.id}},y=t=>E(t)?m(t):t,F=async function*(t,e,o){const r=k(e,o.tables),i=o.batchSize??O;await w(t,e);for(const s of r){const n=u(s);let a,l=!0;for(;l;){const c=a===void 0?await t.all(`SELECT * FROM ${n} ORDER BY "id" LIMIT ?`,[i]):await t.all(`SELECT * FROM ${n} WHERE "id" > ? ORDER BY "id" LIMIT ?`,[a,i]);for(const f of c)yield{doc:y(h(e,s,f)),table:s};const d=c.at(-1);d!==void 0&&(a=String(d.id)),l=c.length===i}}},b=t=>t?._meta?.column?.notNull===!1,I=(t,e)=>{const o=new Set;for(const[r,i]of Object.entries(t.shape)){if(e[r]!==null||i.kind!=="optional"||b(i))continue;const s=i._meta?.inner;b(s)||o.add(r)}return o.size===0?e:Object.fromEntries(Object.entries(e).filter(([r])=>!o.has(r)))},T=new Set(["_creationTime","_id"]),_=(t,e)=>Object.keys(e).find(o=>!T.has(o)&&!Object.hasOwn(t.shape,o)),S=(t,e,o)=>{const r=t.tables[e];if(!r)return`unknown table: ${e}`;const i=_(r,o);if(i!==void 0)return`unexpected field "${i}": not declared in table "${e}"`;for(const[s,n]of Object.entries(r.shape)){const a=o[s],l=n.kind==="optional";if(a===void 0&&l)continue;const c=n.parse;if(typeof c=="function")try{c(a)}catch(d){const f=d instanceof Error?d.message:String(d);return`field "${s}": ${f}`}}},v=async(t,e,o,r)=>{try{return e?(await e.all(`SELECT 1 AS hit FROM ${u(o)} WHERE "id" = ? LIMIT 1`,[r])).length>0:await t.get(r)!==null}catch{return!1}},A=async(t,e,o,r,i)=>{const{doc:s,table:n}=r;if(typeof n!="string"||n.length===0)return{error:{code:"BAD_ROW",line:i,message:"row is missing `table`",table:n},kind:"error"};if(e.tables[n]?.shardMode?.kind!=="global")return{kind:"skip"};if(!s||typeof s!="object"||Array.isArray(s))return{error:{code:"BAD_ROW",line:i,message:"row is missing or malformed `doc`",table:n},kind:"error"};let a;try{a=g(s)}catch(d){return{error:{code:"BAD_ROW",line:i,message:d instanceof Error?d.message:String(d),table:n},kind:"error"}}a=I(e.tables[n],a);const l=S(e,n,a);if(l!==void 0)return{error:{code:"VALIDATION_ERROR",line:i,message:l,table:n},kind:"error"};const c=typeof a._id=="string"?a._id:void 0;if(c!==void 0&&await v(t,o.exec,n,c))return{kind:"conflict"};try{return await t.insert(n,a,{allowExplicitId:!0}),{inserted:n,kind:"inserted"}}catch(d){const{body:f}=p(d,{fallbackCode:"INSERT_FAILED"});return{error:{code:f.code,line:i,message:f.message,table:n},kind:"error"}}},$=async(t,e,o)=>{const r=[],i={};let s=0,n=(o.startLine??1)-1;for(const a of o.rows){n+=1;const l=a.line??n,c=await A(t,e,o,a,l);switch(c.kind){case"conflict":{s+=1;break}case"error":{r.push(c.error);break}case"inserted":{i[c.inserted]=(i[c.inserted]??0)+1;break}}}return{conflicts:s,errors:r,inserted:i}};export{F as exportGlobalRows,$ as importGlobalRows,k as selectGlobalTables};
@@ -0,0 +1 @@
1
+ import{LunoraError as E}from"@lunora/errors";import{sqliteEncode as I,decodeGlobalRow as $}from"@lunora/sql-store";import{n as M,e as v}from"./wire-codec-C2Avu56N.mjs";import{runD1GlobalTableMigrations as S}from"./backfillD1SearchIndexes-V-i_kEvG.mjs";import{quoteIdentifier as i}from"./quoteIdentifier-CObIFRhb.mjs";const C=50,g=500,y=30,U=200,h=(t,o,n)=>Math.min(Math.max(t,o),n),F=/^sqlite_|^_cf_|^d1_|^__cdc|^__lunora_|__agg_|__rank_|__fts_/u,k=t=>F.test(t),N=/password|secret|token|hash|salt|credential/iu,p=async t=>(await t.all("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name",[])).map(n=>String(n.name)).filter(n=>!k(n)),A=async(t,o,n)=>{if(await S(t,o),!(await p(t)).includes(n))throw new E("UNKNOWN_TABLE",`unknown table: ${n}`,{status:404})},T=async(t,o,n="",a=[])=>{const e=await t.all(`SELECT COUNT(*) AS c FROM ${o}${n}`,a);return Number(e[0]?.c??0)},L=(t,o,n)=>t.tables[o]!==void 0&&n==="_id"?"id":n,O=(t,o,n,a)=>{const e=[],r=[];for(const s of a??[]){if(!n.includes(s.column))throw new E("UNKNOWN_COLUMN",`unknown column: ${s.column}`,{status:404});if(t.tables[o]===void 0&&N.test(s.column))throw new E("FORBIDDEN",`cannot filter on a redacted column: ${s.column}`,{status:403});const l=i(L(t,o,s.column));s.value===null||s.value===void 0?e.push(`${l} IS NULL`):(e.push(`${l} = ?`),r.push(I(s.value)))}return{params:r,where:e.length>0?` WHERE ${e.join(" AND ")}`:""}},q=(t,o,n)=>{const a=t.tables[o];if(a)return $(a,n);const e={};for(const[r,s]of Object.entries(n))e[r]=s!=null&&N.test(r)?"•••":s;return e},R=async(t,o,n)=>{const a=o.tables[n];return a?["_id","_creationTime",...Object.keys(a.shape)]:(await t.all(`PRAGMA table_info(${i(n)})`,[])).map(r=>String(r.name))},G=async(t,o,n)=>{if(o.tables[n])return;const a=await t.all(`PRAGMA foreign_key_list(${i(n)})`,[]);if(a.length===0)return;const e={};for(const r of a){const s=String(r.from),l=String(r.table);e[s]??=l}return e},K=async(t,o)=>{await S(t,o);const n=await p(t);return Promise.all(n.map(async a=>({name:a,rowCount:await T(t,i(a))})))},Y=async(t,o,n)=>{const{table:a}=n;await A(t,o,a);const e=h(Math.trunc(n.limit??C),1,g),r=Math.max(0,Math.trunc(n.offset??0)),s=i(a),l=await R(t,o,a),{params:w,where:u}=O(o,a,l,n.filters),f=await T(t,s,u,w),m=(await t.all(`SELECT * FROM ${s}${u} LIMIT ? OFFSET ?`,[...w,e,r])).map(d=>{const c=q(o,a,d);return M(c)?v(c):c}),b=await G(t,o,a);return b===void 0?{columns:l,rows:m,total:f}:{columns:l,refs:b,rows:m,total:f}},H=async(t,o,n)=>{const{column:a,table:e}=n;await A(t,o,e);const r=await R(t,o,e);if(!r.includes(a))throw new E("UNKNOWN_COLUMN",`unknown column: ${a}`,{status:404});const s=i(e),{params:l,where:w}=O(o,e,r,n.filters);if(o.tables[e]===void 0&&N.test(a)){const c=await T(t,s,w,l);return{truncated:!1,values:c===0?[]:[{count:c,value:"•••"}]}}const u=h(Math.trunc(n.limit??y),1,U),f=i(L(o,e,a)),_=await t.all(`SELECT ${f} AS value, COUNT(*) AS count FROM ${s}${w} GROUP BY ${f} ORDER BY count DESC LIMIT ?`,[...l,u+1]),m=_.length>u,d=(m?_.slice(0,u):_).map(c=>({count:Number(c.count),value:c.value}));return{truncated:m,values:M(d)?v(d):d}};export{H as facetGlobalColumn,K as listGlobalTables,Y as readGlobalTablePage};
@@ -0,0 +1 @@
1
+ const g=r=>{let t="";for(let o=0;o<r.length;o+=32768)t+=String.fromCharCode(...r.subarray(o,o+32768));return btoa(t)},l=r=>{const t=atob(r),s=new Uint8Array(t.length);for(let o=0;o<t.length;o+=1)s[o]=t.codePointAt(o)??0;return s},i="$lunora.wire$";const m="__proto__",A={BigInt64Array,BigUint64Array,Float32Array,Float64Array,Int8Array,Int16Array,Int32Array,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array},w={Error,EvalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError},E=r=>{if(r===null||typeof r!="object")return!1;const t=Object.getPrototypeOf(r);return t===null||t===Object.prototype},p=(r,t=0)=>{if(t>64)return!0;if(r===null)return!1;const s=typeof r;if(s==="string"||s==="boolean")return!1;if(s==="number")return!Number.isFinite(r);if(s!=="object")return!0;if(Array.isArray(r))return r.length>0&&r[0]===i?!0:r.some(o=>o===void 0||p(o,t+1));if(!E(r)||Object.hasOwn(r,m))return!0;for(const o of Object.keys(r)){const c=r[o];if(c!==void 0&&p(c,t+1))return!0}return!1},u=(r,t=0)=>{if(t>64)throw new RangeError("wire-codec: value nesting exceeds the 64-level limit");if(r===void 0)return[i,"undefined"];if(r===null)return null;const s=typeof r;if(s==="bigint")return[i,"bigint",r.toString()];if(s==="number"){const e=r;return Number.isNaN(e)?[i,"nan"]:e===1/0?[i,"inf"]:e===-1/0?[i,"-inf"]:e}if(s!=="object")return r;if(r instanceof Date)return[i,"date",u(r.getTime(),t+1)];if(r instanceof Error){const e=r,n={};for(const a of Object.keys(e))e[a]!==void 0&&(n[a]=u(e[a],t+1));const f=[i,"error",e.name,e.message,n];return e.cause!==void 0&&f.push(u(e.cause,t+1)),f}if(r instanceof URL)return[i,"url",r.href];if(r instanceof Map)return[i,"map",[...r.entries()].map(([e,n])=>[u(e,t+1),u(n,t+1)])];if(r instanceof Set)return[i,"set",[...r].map(e=>u(e,t+1))];if(r instanceof ArrayBuffer)return[i,"bytes",g(new Uint8Array(r)),"ArrayBuffer"];if(ArrayBuffer.isView(r)){const e=r,n=e.constructor.name,f=new Uint8Array(e.buffer,e.byteOffset,e.byteLength);return n==="Uint8Array"?[i,"bytes",g(f)]:[i,"bytes",g(f),n]}if(Array.isArray(r)){const e=r.map(n=>u(n,t+1));return e.length>0&&e[0]===i?[i,"arr",e]:e}if(!E(r)){const e=r.constructor?.name??"value";throw new TypeError(`wire-codec: cannot encode a ${e} over the Lunora wire — only plain objects, arrays, and the supported built-ins (Date, Error, URL, Map, Set, ArrayBuffer/typed arrays, bigint) round-trip`)}const o=r,c={};for(const e of Object.keys(o)){const n=o[e];if(n===void 0)continue;const f=u(n,t+1);e===m?Object.defineProperty(c,e,{configurable:!0,enumerable:!0,value:f,writable:!0}):c[e]=f}return c},y=(r,t=0)=>{if(t>64)throw new RangeError("wire-codec: value nesting exceeds the 64-level limit");if(r===null||typeof r!="object")return r;if(Array.isArray(r)){if(r[0]===i)switch(r[1]){case"-inf":return-1/0;case"arr":return r[2].map(e=>y(e,t+1));case"bigint":{const e=r[2];if(typeof e!="string"||e.length>1024||!/^-?\d+$/.test(e))throw new RangeError("wire-codec: invalid or over-long bigint (max 1024 digits)");return BigInt(e)}case"date":{if(r.length<3)throw new TypeError("wire-codec: malformed date — missing payload");return new Date(y(r[2],t+1))}case"map":{const e=r[2];return new Map(e.map(n=>{if(!Array.isArray(n)||n.length!==2)throw new TypeError("wire-codec: malformed map entry — expected a [key, value] pair");return[y(n[0],t+1),y(n[1],t+1)]}))}case"set":return new Set(r[2].map(e=>y(e,t+1)));case"url":return new URL(r[2]);case"error":{const e=r[2],n=r[3],f=(Object.hasOwn(w,e)?w[e]:void 0)??Error,a=new f(n);a.name!==e&&Object.defineProperty(a,"name",{configurable:!0,value:e,writable:!0});const b=y(r[4],t+1);if(b===null||typeof b!="object"||Array.isArray(b))throw new TypeError("wire-codec: malformed error — props must be an object");for(const d of Object.keys(b))d===m?Object.defineProperty(a,d,{configurable:!0,enumerable:!0,value:b[d],writable:!0}):a[d]=b[d];return r.length>5&&Object.defineProperty(a,"cause",{configurable:!0,value:y(r[5],t+1),writable:!0}),a}case"bytes":{const e=l(r[2]),n=r[3]??"Uint8Array";if(n==="ArrayBuffer")return e.buffer.byteLength===e.byteLength?e.buffer:e.slice().buffer;const f=Object.hasOwn(A,n)?A[n]:void 0;return f?new f(e.slice().buffer):e}case"inf":return 1/0;case"nan":return Number.NaN;case"undefined":return;default:return r.map(e=>y(e,t+1))}return r.map(c=>y(c,t+1))}const s=r,o={};for(const c of Object.keys(s)){const e=y(s[c],t+1);c===m?Object.defineProperty(o,c,{configurable:!0,enumerable:!0,value:e,writable:!0}):o[c]=e}return o};export{y as d,u as e,p as n};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/d1",
3
- "version": "1.0.0-alpha.101",
3
+ "version": "1.0.0-alpha.103",
4
4
  "description": "D1 adapter for Lunora .global() tables, wrapping the Sessions API for read-your-writes",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -50,10 +50,10 @@
50
50
  "access": "public"
51
51
  },
52
52
  "dependencies": {
53
- "@lunora/errors": "1.0.0-alpha.27",
54
- "@lunora/platform": "1.0.0-alpha.23",
55
- "@lunora/shard-engine": "1.0.0-alpha.50",
56
- "@lunora/sql-store": "1.0.0-alpha.103",
53
+ "@lunora/errors": "1.0.0-alpha.29",
54
+ "@lunora/platform": "1.0.0-alpha.24",
55
+ "@lunora/shard-engine": "1.0.0-alpha.52",
56
+ "@lunora/sql-store": "1.0.0-alpha.105",
57
57
  "drizzle-orm": "^0.45.2"
58
58
  },
59
59
  "engines": {
@@ -1 +0,0 @@
1
- import{toErrorBody as u}from"@lunora/errors";import{n as p,e as g,d as m}from"./wire-codec-CZoaQB26.mjs";import{runD1GlobalTableMigrations as E}from"./backfillD1SearchIndexes-V-i_kEvG.mjs";import{quoteIdentifier as b}from"./quoteIdentifier-CObIFRhb.mjs";import{decodeGlobalRow as R}from"@lunora/sql-store";const w=200,k=(e,o)=>{const i=r=>e.tables[r]?.shardMode?.kind==="global";return o&&o.length>0?o.filter(r=>i(r)):Object.keys(e.tables).filter(r=>i(r))},y=(e,o,i)=>{const r=e.tables[o];return r?R(r,i):{_creationTime:i._creationTime,_id:i.id}},I=e=>p(e)?g(e):e,M=async function*(e,o,i){const r=k(o,i.tables),n=i.batchSize??w;await E(e,o);for(const s of r){const t=b(s);let c,d=!0;for(;d;){const a=c===void 0?await e.all(`SELECT * FROM ${t} ORDER BY "id" LIMIT ?`,[n]):await e.all(`SELECT * FROM ${t} WHERE "id" > ? ORDER BY "id" LIMIT ?`,[c,n]);for(const f of a)yield{doc:I(y(o,s,f)),table:s};const l=a.at(-1);l!==void 0&&(c=String(l.id)),d=a.length===n}}},h=(e,o,i)=>{const r=e.tables[o];if(!r)return`unknown table: ${o}`;for(const[n,s]of Object.entries(r.shape)){const t=i[n],c=s.kind==="optional";if(t===void 0&&c)continue;const d=s.parse;if(typeof d=="function")try{d(t)}catch(a){const l=a instanceof Error?a.message:String(a);return`field "${n}": ${l}`}}},T=async(e,o,i,r)=>{try{return o?(await o.all(`SELECT 1 AS hit FROM ${b(i)} WHERE "id" = ? LIMIT 1`,[r])).length>0:await e.get(r)!==null}catch{return!1}},O=async(e,o,i,r,n)=>{const{doc:s,table:t}=r;if(typeof t!="string"||t.length===0)return{error:{code:"BAD_ROW",line:n,message:"row is missing `table`",table:t},kind:"error"};if(o.tables[t]?.shardMode?.kind!=="global")return{kind:"skip"};if(!s||typeof s!="object"||Array.isArray(s))return{error:{code:"BAD_ROW",line:n,message:"row is missing or malformed `doc`",table:t},kind:"error"};let c;try{c=m(s)}catch(l){return{error:{code:"BAD_ROW",line:n,message:l instanceof Error?l.message:String(l),table:t},kind:"error"}}const d=h(o,t,c);if(d!==void 0)return{error:{code:"VALIDATION_ERROR",line:n,message:d,table:t},kind:"error"};const a=typeof c._id=="string"?c._id:void 0;if(a!==void 0&&await T(e,i.exec,t,a))return{kind:"conflict"};try{return await e.insert(t,c,{allowExplicitId:!0}),{inserted:t,kind:"inserted"}}catch(l){const{body:f}=u(l,{fallbackCode:"INSERT_FAILED"});return{error:{code:f.code,line:n,message:f.message,table:t},kind:"error"}}},_=async(e,o,i)=>{const r=[],n={};let s=0,t=(i.startLine??1)-1;for(const c of i.rows){t+=1;const d=c.line??t,a=await O(e,o,i,c,d);switch(a.kind){case"conflict":{s+=1;break}case"error":{r.push(a.error);break}case"inserted":{n[a.inserted]=(n[a.inserted]??0)+1;break}}}return{conflicts:s,errors:r,inserted:n}};export{M as exportGlobalRows,_ as importGlobalRows,k as selectGlobalTables};
@@ -1 +0,0 @@
1
- import{LunoraError as E}from"@lunora/errors";import{sqliteEncode as I,decodeGlobalRow as $}from"@lunora/sql-store";import{n as M,e as v}from"./wire-codec-CZoaQB26.mjs";import{runD1GlobalTableMigrations as S}from"./backfillD1SearchIndexes-V-i_kEvG.mjs";import{quoteIdentifier as i}from"./quoteIdentifier-CObIFRhb.mjs";const C=50,g=500,y=30,U=200,h=(t,o,n)=>Math.min(Math.max(t,o),n),F=/^sqlite_|^_cf_|^d1_|^__cdc|__agg_|__rank_|__fts_/u,k=t=>F.test(t),N=/password|secret|token|hash|salt|credential/iu,p=async t=>(await t.all("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name",[])).map(n=>String(n.name)).filter(n=>!k(n)),A=async(t,o,n)=>{if(await S(t,o),!(await p(t)).includes(n))throw new E("UNKNOWN_TABLE",`unknown table: ${n}`,{status:404})},T=async(t,o,n="",a=[])=>{const e=await t.all(`SELECT COUNT(*) AS c FROM ${o}${n}`,a);return Number(e[0]?.c??0)},L=(t,o,n)=>t.tables[o]!==void 0&&n==="_id"?"id":n,O=(t,o,n,a)=>{const e=[],r=[];for(const s of a??[]){if(!n.includes(s.column))throw new E("UNKNOWN_COLUMN",`unknown column: ${s.column}`,{status:404});if(t.tables[o]===void 0&&N.test(s.column))throw new E("FORBIDDEN",`cannot filter on a redacted column: ${s.column}`,{status:403});const l=i(L(t,o,s.column));s.value===null||s.value===void 0?e.push(`${l} IS NULL`):(e.push(`${l} = ?`),r.push(I(s.value)))}return{params:r,where:e.length>0?` WHERE ${e.join(" AND ")}`:""}},q=(t,o,n)=>{const a=t.tables[o];if(a)return $(a,n);const e={};for(const[r,s]of Object.entries(n))e[r]=s!=null&&N.test(r)?"•••":s;return e},R=async(t,o,n)=>{const a=o.tables[n];return a?["_id","_creationTime",...Object.keys(a.shape)]:(await t.all(`PRAGMA table_info(${i(n)})`,[])).map(r=>String(r.name))},G=async(t,o,n)=>{if(o.tables[n])return;const a=await t.all(`PRAGMA foreign_key_list(${i(n)})`,[]);if(a.length===0)return;const e={};for(const r of a){const s=String(r.from),l=String(r.table);e[s]??=l}return e},K=async(t,o)=>{await S(t,o);const n=await p(t);return Promise.all(n.map(async a=>({name:a,rowCount:await T(t,i(a))})))},Y=async(t,o,n)=>{const{table:a}=n;await A(t,o,a);const e=h(Math.trunc(n.limit??C),1,g),r=Math.max(0,Math.trunc(n.offset??0)),s=i(a),l=await R(t,o,a),{params:w,where:u}=O(o,a,l,n.filters),f=await T(t,s,u,w),m=(await t.all(`SELECT * FROM ${s}${u} LIMIT ? OFFSET ?`,[...w,e,r])).map(d=>{const c=q(o,a,d);return M(c)?v(c):c}),b=await G(t,o,a);return b===void 0?{columns:l,rows:m,total:f}:{columns:l,refs:b,rows:m,total:f}},H=async(t,o,n)=>{const{column:a,table:e}=n;await A(t,o,e);const r=await R(t,o,e);if(!r.includes(a))throw new E("UNKNOWN_COLUMN",`unknown column: ${a}`,{status:404});const s=i(e),{params:l,where:w}=O(o,e,r,n.filters);if(o.tables[e]===void 0&&N.test(a)){const c=await T(t,s,w,l);return{truncated:!1,values:c===0?[]:[{count:c,value:"•••"}]}}const u=h(Math.trunc(n.limit??y),1,U),f=i(L(o,e,a)),_=await t.all(`SELECT ${f} AS value, COUNT(*) AS count FROM ${s}${w} GROUP BY ${f} ORDER BY count DESC LIMIT ?`,[...l,u+1]),m=_.length>u,d=(m?_.slice(0,u):_).map(c=>({count:Number(c.count),value:c.value}));return{truncated:m,values:M(d)?v(d):d}};export{H as facetGlobalColumn,K as listGlobalTables,Y as readGlobalTablePage};
@@ -1 +0,0 @@
1
- const d=r=>{let n="";for(let o=0;o<r.length;o+=32768)n+=String.fromCharCode(...r.subarray(o,o+32768));return btoa(n)},O=r=>{const n=atob(r),s=new Uint8Array(n.length);for(let o=0;o<n.length;o+=1)s[o]=n.codePointAt(o)??0;return s},i="$lunora.wire$";const g="__proto__",m={BigInt64Array,BigUint64Array,Float32Array,Float64Array,Int8Array,Int16Array,Int32Array,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array},w={Error,EvalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError},p=r=>{if(r===null||typeof r!="object")return!1;const n=Object.getPrototypeOf(r);return n===null||n===Object.prototype},E=(r,n=0)=>{if(n>64)return!0;if(r===null)return!1;const s=typeof r;if(s==="string"||s==="boolean")return!1;if(s==="number")return!Number.isFinite(r);if(s!=="object")return!0;if(Array.isArray(r))return r.length>0&&r[0]===i?!0:r.some(o=>o===void 0||E(o,n+1));if(!p(r)||Object.hasOwn(r,g))return!0;for(const o of Object.keys(r)){const c=r[o];if(c!==void 0&&E(c,n+1))return!0}return!1},y=(r,n=0)=>{if(n>64)throw new RangeError("wire-codec: value nesting exceeds the 64-level limit");if(r===void 0)return[i,"undefined"];if(r===null)return null;const s=typeof r;if(s==="bigint")return[i,"bigint",r.toString()];if(s==="number"){const e=r;return Number.isNaN(e)?[i,"nan"]:e===1/0?[i,"inf"]:e===-1/0?[i,"-inf"]:e}if(s!=="object")return r;if(r instanceof Date)return[i,"date",y(r.getTime(),n+1)];if(r instanceof Error){const e=r,t={};for(const a of Object.keys(e))e[a]!==void 0&&(t[a]=y(e[a],n+1));const f=[i,"error",e.name,e.message,t];return e.cause!==void 0&&f.push(y(e.cause,n+1)),f}if(r instanceof URL)return[i,"url",r.href];if(r instanceof Map)return[i,"map",[...r.entries()].map(([e,t])=>[y(e,n+1),y(t,n+1)])];if(r instanceof Set)return[i,"set",[...r].map(e=>y(e,n+1))];if(r instanceof ArrayBuffer)return[i,"bytes",d(new Uint8Array(r)),"ArrayBuffer"];if(ArrayBuffer.isView(r)){const e=r,t=e.constructor.name,f=new Uint8Array(e.buffer,e.byteOffset,e.byteLength);return t==="Uint8Array"?[i,"bytes",d(f)]:[i,"bytes",d(f),t]}if(Array.isArray(r)){const e=r.map(t=>y(t,n+1));return e.length>0&&e[0]===i?[i,"arr",e]:e}if(!p(r)){const e=r.constructor?.name??"value";throw new TypeError(`wire-codec: cannot encode a ${e} over the Lunora wire — only plain objects, arrays, and the supported built-ins (Date, Error, URL, Map, Set, ArrayBuffer/typed arrays, bigint) round-trip`)}const o=r,c={};for(const e of Object.keys(o)){const t=o[e];if(t===void 0)continue;const f=y(t,n+1);e===g?Object.defineProperty(c,e,{configurable:!0,enumerable:!0,value:f,writable:!0}):c[e]=f}return c},u=(r,n=0)=>{if(n>64)throw new RangeError("wire-codec: value nesting exceeds the 64-level limit");if(r===null||typeof r!="object")return r;if(Array.isArray(r)){if(r[0]===i)switch(r[1]){case"-inf":return-1/0;case"arr":return r[2].map(e=>u(e,n+1));case"bigint":{const e=r[2];if(typeof e!="string"||e.length>1024||!/^-?\d+$/.test(e))throw new RangeError("wire-codec: invalid or over-long bigint (max 1024 digits)");return BigInt(e)}case"date":return new Date(u(r[2],n+1));case"map":{const e=r[2];return new Map(e.map(t=>{if(!Array.isArray(t)||t.length!==2)throw new TypeError("wire-codec: malformed map entry — expected a [key, value] pair");return[u(t[0],n+1),u(t[1],n+1)]}))}case"set":return new Set(r[2].map(e=>u(e,n+1)));case"url":return new URL(r[2]);case"error":{const e=r[2],t=r[3],f=(Object.hasOwn(w,e)?w[e]:void 0)??Error,a=new f(t);a.name!==e&&Object.defineProperty(a,"name",{configurable:!0,value:e,writable:!0});const A=u(r[4],n+1);for(const b of Object.keys(A))b===g?Object.defineProperty(a,b,{configurable:!0,enumerable:!0,value:A[b],writable:!0}):a[b]=A[b];return r.length>5&&Object.defineProperty(a,"cause",{configurable:!0,value:u(r[5],n+1),writable:!0}),a}case"bytes":{const e=O(r[2]),t=r[3]??"Uint8Array";if(t==="ArrayBuffer")return e.buffer.byteLength===e.byteLength?e.buffer:e.slice().buffer;const f=Object.hasOwn(m,t)?m[t]:void 0;return f?new f(e.slice().buffer):e}case"inf":return 1/0;case"nan":return Number.NaN;case"undefined":return;default:return r.map(e=>u(e,n+1))}return r.map(c=>u(c,n+1))}const s=r,o={};for(const c of Object.keys(s)){const e=u(s[c],n+1);c===g?Object.defineProperty(o,c,{configurable:!0,enumerable:!0,value:e,writable:!0}):o[c]=e}return o};export{u as d,y as e,E as n};