@lunora/replica 1.0.0-alpha.57 → 1.0.0-alpha.58

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{createBetterSqlite3Adapter as o}from"./adapters/better-sqlite3.mjs";import{createSqliteWasmAdapter as f}from"./adapters/sqlite-wasm.mjs";import{createSqlJsAdapter as i}from"./adapters/sqljs.mjs";import{applyDiff as m,applyDiffToSnapshot as n,applyDiffs as x}from"./packem_shared/applyDiff-Da160K7_.mjs";import{defineEvents as l}from"./packem_shared/defineEvents-DHo-VK7G.mjs";import{MaterializerRuntime as S,defineMaterializer as E}from"./packem_shared/MaterializerRuntime-D0HOAe24.mjs";import{applyDiffToDb as v,applyDiffsToDb as y}from"./packem_shared/applyDiffToDb-BEot1Jiz.mjs";import{EventEmitter as d}from"./packem_shared/EventEmitter-uo75adUL.mjs";import{EventLog as M}from"./packem_shared/EventLog-B1-yhArT.mjs";import{EventLogDO as u}from"./packem_shared/EventLogDO-CaZvpgJN.mjs";import{EventLogDOClient as T}from"./packem_shared/EventLogDOClient-DGDMj5L5.mjs";import{EventSource as C,UNHANDLED as h}from"./packem_shared/EventSource-BC0hKJSA.mjs";import{eventsContext as I}from"./packem_shared/eventsContext-Dxow9Y7S.mjs";import{LocalMirror as O}from"./packem_shared/LocalMirror-DMzMW0Mc.mjs";import{isClientSeq as G,isGlobalSeq as H,isInputEvent as J}from"./packem_shared/isClientSeq-D2Xm0_lj.mjs";import{InMemorySnapshotStore as U}from"./packem_shared/InMemorySnapshotStore-C4taIG5K.mjs";import{subscribeToMirror as j}from"./packem_shared/subscribeToMirror-CX10AaP3.mjs";import{SubscriptionManager as w}from"./packem_shared/SubscriptionManager-AhPw3lFc.mjs";import{EventsSync as K}from"./packem_shared/EventsSync-DWyGGZQZ.mjs";import{classifyChanges as Q,createTableDiff as V,diffSize as X,isDiffEmpty as Y,mergeDiffs as Z}from"./packem_shared/classifyChanges-ioMfjjbU.mjs";export{d as EventEmitter,M as EventLog,u as EventLogDO,T as EventLogDOClient,C as EventSource,K as EventsSync,U as InMemorySnapshotStore,O as LocalMirror,S as MaterializerRuntime,w as SubscriptionManager,h as UNHANDLED,m as applyDiff,v as applyDiffToDb,n as applyDiffToSnapshot,x as applyDiffs,y as applyDiffsToDb,Q as classifyChanges,o as createBetterSqlite3Adapter,i as createSqlJsAdapter,f as createSqliteWasmAdapter,V as createTableDiff,l as defineEvents,E as defineMaterializer,X as diffSize,I as eventsContext,G as isClientSeq,Y as isDiffEmpty,H as isGlobalSeq,J as isInputEvent,Z as mergeDiffs,j as subscribeToMirror};
1
+ import{createBetterSqlite3Adapter as o}from"./adapters/better-sqlite3.mjs";import{createSqliteWasmAdapter as f}from"./adapters/sqlite-wasm.mjs";import{createSqlJsAdapter as i}from"./adapters/sqljs.mjs";import{applyDiff as m,applyDiffToSnapshot as n,applyDiffs as x}from"./packem_shared/applyDiff-Da160K7_.mjs";import{defineEvents as l}from"./packem_shared/defineEvents-DHo-VK7G.mjs";import{MaterializerRuntime as S,defineMaterializer as E}from"./packem_shared/MaterializerRuntime-D0HOAe24.mjs";import{applyDiffToDb as v,applyDiffsToDb as y}from"./packem_shared/applyDiffToDb-C6ek5Elp.mjs";import{EventEmitter as d}from"./packem_shared/EventEmitter-uo75adUL.mjs";import{EventLog as M}from"./packem_shared/EventLog-B1-yhArT.mjs";import{EventLogDO as u}from"./packem_shared/EventLogDO-CaZvpgJN.mjs";import{EventLogDOClient as T}from"./packem_shared/EventLogDOClient-DGDMj5L5.mjs";import{EventSource as C,UNHANDLED as h}from"./packem_shared/EventSource-BC0hKJSA.mjs";import{eventsContext as I}from"./packem_shared/eventsContext-Dxow9Y7S.mjs";import{LocalMirror as O}from"./packem_shared/LocalMirror-BIURA-He.mjs";import{isClientSeq as G,isGlobalSeq as H,isInputEvent as J}from"./packem_shared/isClientSeq-D2Xm0_lj.mjs";import{InMemorySnapshotStore as U}from"./packem_shared/InMemorySnapshotStore-C4taIG5K.mjs";import{subscribeToMirror as j}from"./packem_shared/subscribeToMirror-CX10AaP3.mjs";import{SubscriptionManager as w}from"./packem_shared/SubscriptionManager-AhPw3lFc.mjs";import{EventsSync as K}from"./packem_shared/EventsSync-DWyGGZQZ.mjs";import{classifyChanges as Q,createTableDiff as V,diffSize as X,isDiffEmpty as Y,mergeDiffs as Z}from"./packem_shared/classifyChanges-ioMfjjbU.mjs";export{d as EventEmitter,M as EventLog,u as EventLogDO,T as EventLogDOClient,C as EventSource,K as EventsSync,U as InMemorySnapshotStore,O as LocalMirror,S as MaterializerRuntime,w as SubscriptionManager,h as UNHANDLED,m as applyDiff,v as applyDiffToDb,n as applyDiffToSnapshot,x as applyDiffs,y as applyDiffsToDb,Q as classifyChanges,o as createBetterSqlite3Adapter,i as createSqlJsAdapter,f as createSqliteWasmAdapter,V as createTableDiff,l as defineEvents,E as defineMaterializer,X as diffSize,I as eventsContext,G as isClientSeq,Y as isDiffEmpty,H as isGlobalSeq,J as isInputEvent,Z as mergeDiffs,j as subscribeToMirror};
@@ -0,0 +1,4 @@
1
+ import{createSqlJsAdapter as y}from"../adapters/sqljs.mjs";import{applyDiffToDb as m,escapeIdentifier as a}from"./applyDiffToDb-C6ek5Elp.mjs";import{EventLog as b}from"./EventLog-B1-yhArT.mjs";const h="__lunora_mirror_meta",g=o=>{o.exec(`CREATE TABLE IF NOT EXISTS ${h} (
2
+ key TEXT PRIMARY KEY NOT NULL,
3
+ value TEXT NOT NULL
4
+ )`)},T="schema_version",f=3,l=o=>typeof o=="bigint"||typeof o=="boolean"?"INTEGER":typeof o=="number"?Number.isInteger(o)?"INTEGER":"REAL":"TEXT";class E{#e;#t;#n;#s=new Set;#i=0;static create(e,t){const n=y(e);return new E({db:n,tables:t?.tables})}constructor(e){this.#e=e.db,this.#t={...e.tables},this.#n=new b({maxEntries:e.maxEventLogEntries}),g(this.#e),this.#o()}onChange(e){return this.#s.add(e),()=>{this.#s.delete(e)}}get eventLog(){return this.#n}get db(){return this.#e}get version(){return this.#i}applyDiff(e){if(e.changes.length===0)return;const t=this.#t[e.table]?.primaryKey??"id";this.#h(e),m(this.#e,e,t),this.#n.append("table-diff",e,[e]),this.#r()}query(e,t){return this.#e.query(e,t)}clearData(){const e=this.#a();this.#e.transaction(()=>{for(const{name:t}of e)this.#e.exec(`DELETE FROM ${a(t)}`)}),this.#r()}#r(){this.#i+=1;for(const e of this.#s)try{e()}catch{}}close(){this.#e.close(),this.#n.clear(),this.#s.clear()}registerTable(e,t){this.#t[e]=t}get mirroredTables(){return Object.keys(this.#t)}#a(){return this.#e.query(String.raw`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE '\_\_lunora\_%' ESCAPE '\' AND name NOT LIKE 'sqlite\_%' ESCAPE '\'`)}#o(){if(this.#e.query(`SELECT key, value FROM ${h}`).find(s=>s.key===T)?.value===String(f))return;const n=this.#a();this.#e.transaction(()=>{for(const{name:s}of n)this.#e.exec(`DROP TABLE IF EXISTS ${a(s)}`);this.#e.exec(`INSERT OR REPLACE INTO ${h} (key, value) VALUES (?, ?)`,[T,String(f)])})}static#c(e,t){const n=new Set;for(const s of e.changes)if(s.type!=="delete")for(const c of Object.keys(s.data))c!==t&&n.add(c);return n}static#E(e,t,n){const s=new Map;for(const c of e.changes)if(!(c.type==="delete"||s.size===n.size))for(const r of n){if(r===t||s.has(r))continue;const i=c.data[r];i!=null&&s.set(r,l(i))}return s}static#l(e,t){for(const n of e.changes){const s=n.type==="delete"?n.id:n.data[t];if(s!=null)return l(s);if(n.type==="update")return l(n.id)}return"TEXT"}#h(e){const t=this.#t[e.table]?.primaryKey??"id",n=E.#c(e,t),s=E.#E(e,t,n);if(this.#e.query("SELECT name FROM sqlite_master WHERE type='table' AND name=?",[e.table]).length===0){const r=E.#l(e,t);let i=`${a(t)} ${r==="INTEGER"?"INT":r} PRIMARY KEY NOT NULL`;for(const u of n)i+=`, ${a(u)} ${s.get(u)??"TEXT"}`;this.#e.exec(`CREATE TABLE IF NOT EXISTS ${a(e.table)} (${i})`)}else if(n.size>0){const r=new Set(this.#e.query(`PRAGMA table_info(${a(e.table)})`).map(i=>i.name));for(const i of n)r.has(i)||this.#e.exec(`ALTER TABLE ${a(e.table)} ADD COLUMN ${a(i)} ${s.get(i)??"TEXT"}`)}}}export{E as LocalMirror};
@@ -0,0 +1 @@
1
+ const r=t=>`\`${t.replaceAll("`","``")}\``,E=t=>t.map(n=>`${r(n)} = ?`).join(", "),$=t=>`(${t.map(n=>r(n)).join(", ")})`,b=t=>`(${Array.from({length:t}).fill("?").join(", ")})`,c=t=>t==null?null:typeof t=="number"||typeof t=="bigint"||typeof t=="string"?t:typeof t=="boolean"?t?1:0:t instanceof Uint8Array?t:typeof t=="object"?JSON.stringify(t):String(t),g=(t,n,o)=>{if(n.changes.length===0)return;const f=r(n.table),y=r(o);for(const s of n.changes)switch(s.type){case"delete":{t.exec(`DELETE FROM ${f} WHERE ${y} = ?`,[c(s.id)]);break}case"insert":{const{data:i}=s,e=Object.keys(i);if(e.length===0)continue;const a=`INSERT OR REPLACE INTO ${f} ${$(e)} VALUES ${b(e.length)}`,p=e.map(l=>c(i[l]));t.exec(a,p);break}case"update":{const{data:i}=s,e=Object.keys(i);if(e.length===0)continue;const a=`UPDATE ${f} SET ${E(e)} WHERE ${y} = ?`,p=[...e.map(l=>c(i[l])),c(s.id)];t.exec(a,p);break}}},D=(t,n,o)=>{t.transaction(()=>{g(t,n,o??"id")})},h=(t,n)=>{n.length!==0&&t.transaction(()=>{for(const o of n)g(t,o,"id")})};export{D as applyDiffToDb,h as applyDiffsToDb,r as escapeIdentifier,c as normalizeBindValue};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/replica",
3
- "version": "1.0.0-alpha.57",
3
+ "version": "1.0.0-alpha.58",
4
4
  "description": "Local-first replica runtime + local SQLite mirror for Lunora",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -1,4 +0,0 @@
1
- import{createSqlJsAdapter as u}from"../adapters/sqljs.mjs";import{applyDiffToDb as f,escapeIdentifier as o}from"./applyDiffToDb-BEot1Jiz.mjs";import{EventLog as m}from"./EventLog-B1-yhArT.mjs";const l="__lunora_mirror_meta",y=a=>{a.exec(`CREATE TABLE IF NOT EXISTS ${l} (
2
- key TEXT PRIMARY KEY NOT NULL,
3
- value TEXT NOT NULL
4
- )`)},h="schema_version",T=2,b=a=>typeof a=="bigint"||typeof a=="boolean"?"INTEGER":typeof a=="number"?Number.isInteger(a)?"INTEGER":"REAL":"TEXT";class E{#e;#t;#s;#n=new Set;#i=0;static create(e,t){const s=u(e);return new E({db:s,tables:t?.tables})}constructor(e){this.#e=e.db,this.#t={...e.tables},this.#s=new m({maxEntries:e.maxEventLogEntries}),y(this.#e),this.#a()}onChange(e){return this.#n.add(e),()=>{this.#n.delete(e)}}get eventLog(){return this.#s}get db(){return this.#e}get version(){return this.#i}applyDiff(e){if(e.changes.length===0)return;const t=this.#t[e.table]?.primaryKey??"id";this.#l(e),f(this.#e,e,t),this.#s.append("table-diff",e,[e]),this.#r()}query(e,t){return this.#e.query(e,t)}clearData(){const e=this.#o();this.#e.transaction(()=>{for(const{name:t}of e)this.#e.exec(`DELETE FROM ${o(t)}`)}),this.#r()}#r(){this.#i+=1;for(const e of this.#n)try{e()}catch{}}close(){this.#e.close(),this.#s.clear()}registerTable(e,t){this.#t[e]=t}get mirroredTables(){return Object.keys(this.#t)}#o(){return this.#e.query(String.raw`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE '\_\_lunora\_%' ESCAPE '\' AND name NOT LIKE 'sqlite\_%' ESCAPE '\'`)}#a(){if(this.#e.query(`SELECT key, value FROM ${l}`).find(n=>n.key===h)?.value===String(T))return;const s=this.#o();this.#e.transaction(()=>{for(const{name:n}of s)this.#e.exec(`DROP TABLE IF EXISTS ${o(n)}`);this.#e.exec(`INSERT OR REPLACE INTO ${l} (key, value) VALUES (?, ?)`,[h,String(T)])})}static#c(e,t){const s=new Set;for(const n of e.changes)if(n.type!=="delete")for(const c of Object.keys(n.data))c!==t&&s.add(c);return s}static#E(e,t,s){const n=new Map;for(const c of e.changes)if(!(c.type==="delete"||n.size===s.size))for(const r of s){if(r===t||n.has(r))continue;const i=c.data[r];i!=null&&n.set(r,b(i))}return n}#l(e){const t=this.#t[e.table]?.primaryKey??"id",s=E.#c(e,t),n=E.#E(e,t,s);if(this.#e.query("SELECT name FROM sqlite_master WHERE type='table' AND name=?",[e.table]).length===0){let r=`${o(t)} TEXT PRIMARY KEY NOT NULL`;for(const i of s)r+=`, ${o(i)} ${n.get(i)??"TEXT"}`;this.#e.exec(`CREATE TABLE IF NOT EXISTS ${o(e.table)} (${r})`)}else if(s.size>0){const r=new Set(this.#e.query(`PRAGMA table_info(${o(e.table)})`).map(i=>i.name));for(const i of s)r.has(i)||this.#e.exec(`ALTER TABLE ${o(e.table)} ADD COLUMN ${o(i)} ${n.get(i)??"TEXT"}`)}}}export{E as LocalMirror};
@@ -1 +0,0 @@
1
- const c=t=>`\`${t.replaceAll("`","``")}\``,E=t=>t.map(n=>`${c(n)} = ?`).join(", "),$=t=>`(${t.map(n=>c(n)).join(", ")})`,b=t=>`(${Array.from({length:t}).fill("?").join(", ")})`,y=t=>t==null?null:typeof t=="number"||typeof t=="bigint"||typeof t=="string"?t:typeof t=="boolean"?t?1:0:t instanceof Uint8Array?t:typeof t=="object"?JSON.stringify(t):String(t),g=(t,n,o)=>{if(n.changes.length===0)return;const r=c(n.table),l=c(o);for(const s of n.changes)switch(s.type){case"delete":{t.exec(`DELETE FROM ${r} WHERE ${l} = ?`,[s.id]);break}case"insert":{const{data:i}=s,e=Object.keys(i);if(e.length===0)continue;const f=`INSERT OR REPLACE INTO ${r} ${$(e)} VALUES ${b(e.length)}`,a=e.map(p=>y(i[p]));t.exec(f,a);break}case"update":{const{data:i}=s,e=Object.keys(i);if(e.length===0)continue;const f=`UPDATE ${r} SET ${E(e)} WHERE ${l} = ?`,a=[...e.map(p=>y(i[p])),s.id];t.exec(f,a);break}}},D=(t,n,o)=>{t.transaction(()=>{g(t,n,o??"id")})},h=(t,n)=>{n.length!==0&&t.transaction(()=>{for(const o of n)g(t,o,"id")})};export{D as applyDiffToDb,h as applyDiffsToDb,c as escapeIdentifier,y as normalizeBindValue};