@live-state/sync 0.0.3 → 0.0.4-beta.1
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/client.d.ts +1 -1
- package/dist/client.js +1 -1
- package/dist/fetch-client.d.ts +1 -1
- package/dist/{index-4Y_YcJTJ.d.ts → index-C1FzxdnB.d.ts} +9 -74
- package/dist/server.cjs +2 -2
- package/dist/server.d.cts +11 -68
- package/dist/server.d.ts +11 -68
- package/dist/server.js +2 -2
- package/package.json +2 -2
package/dist/client.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { f as Client, e as ClientEvents, C as ClientOptions, d as ConnectionStateChangeEvent, M as MessageReceivedEvent, c as SubscriptionProvider, g as createClient, u as useLiveQuery } from './index-
|
|
1
|
+
export { f as Client, e as ClientEvents, C as ClientOptions, d as ConnectionStateChangeEvent, M as MessageReceivedEvent, c as SubscriptionProvider, g as createClient, u as useLiveQuery } from './index-C1FzxdnB.js';
|
|
2
2
|
import 'react/jsx-runtime';
|
|
3
3
|
import 'zod';
|
package/dist/client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {c,a,f,d,e,b}from'./chunk-G5SVBG4B.js';import {useSyncExternalStore,useEffect}from'react';import {xxHash32}from'js-xxhash';import {jsx,Fragment}from'react/jsx-runtime';import {z}from'zod';import {stringify}from'qs';import ae from'fast-deep-equal';import {openDB}from'idb';var m=d=>xxHash32(JSON.stringify(d)).toString(32);var k=class{subscriptions=new Map;getOrStoreSubscription(e){let t=m(e);return this.subscriptions.has(t)?this.subscriptions.get(t).subscribe:(this.subscriptions.set(t,{subscribe:i=>{var n;(n=this.subscriptions.get(t))==null||n.callbacks.add(i);let s=e.subscribe(()=>{var r;(r=this.subscriptions.get(t))==null||r.callbacks.forEach(o=>{o();});});return ()=>{var r;(r=this.subscriptions.get(t))==null||r.callbacks.delete(i),setTimeout(()=>{var o;((o=this.subscriptions.get(t))==null?void 0:o.callbacks.size)===0&&(this.subscriptions.delete(t),s());},10);}},callbacks:new Set}),this.subscriptions.get(t).subscribe)}},F=new k,he=d=>useSyncExternalStore(F.getOrStoreSubscription(d),d.get),pe=({children:d,client:e})=>(useEffect(()=>{e.subscribe();},[e.subscribe]),jsx(Fragment,{children:d}));var N=z.object({resource:z.string(),where:z.record(z.any()).optional(),include:z.record(z.any()).optional(),lastSyncedAt:z.string().optional(),limit:z.number().optional(),sort:z.array(z.object({key:z.string(),direction:z.enum(["asc","desc"])})).optional()}),x=z.record(z.object({value:z.string().or(z.number()).or(z.boolean()).or(z.date()).nullable(),_meta:z.object({timestamp:z.string().optional().nullable()}).optional()})).superRefine((d,e)=>{d.id&&e.addIssue({code:z.ZodIssueCode.custom,message:"Payload cannot have an id"});}),H=z.object({id:z.string().optional(),type:z.literal("MUTATE"),resource:z.string()}),_=H.extend({procedure:z.string(),payload:z.any().optional()}),L=H.extend({resourceId:z.string(),payload:x});z.union([_,L]);var v=z.string(),ee=z.object({id:v,type:z.literal("SUBSCRIBE"),resource:z.string()}),te=N.extend({id:v,type:z.literal("QUERY")}),q=L.extend({id:v}),ie=_.extend({id:v}),se=z.union([ie,q]);z.union([ee,te,se]);var ne=z.object({id:v,type:z.literal("REJECT"),resource:z.string(),message:z.string().optional()}),re=z.object({id:v,type:z.literal("REPLY"),data:z.any()}),K=z.union([ne,re,q]),U=z.object({resource:z.string(),data:z.record(x)});var M=class d{_collection;_client;_where;_include;_limit;_single;_sort;constructor(e,t,i,s,n,r,o){this._collection=e,this._client=t,this._where=i??{},this._include=s??{},this._limit=n,this._single=r,this._sort=o,this.get=this.get.bind(this),this.subscribe=this.subscribe.bind(this);}where(e){return new d(this._collection,this._client,{...this._where,...e},this._include,this._limit,this._single,this._sort)}include(e){return new d(this._collection,this._client,this._where,{...this._include,...e},this._limit,this._single,this._sort)}get(){let e=this._client.get({resource:this._collection.name,where:this._where,include:this._include,limit:this._limit,sort:this._sort});return this._single?e[0]:e}subscribe(e){return this._client.subscribe({resource:this._collection.name,where:this._where,include:this._include,limit:this._limit,sort:this._sort},t=>{if(this._single)return e(t[0]);e(t);})}limit(e){return new d(this._collection,this._client,this._where,this._include,e,this._single,this._sort)}one(e){return this.first({id:e})}first(e){return new d(this._collection,this._client,e??this._where,this._include,1,true,this._sort)}orderBy(e,t="asc"){let i=[...this._sort??[],{key:e,direction:t}];return new d(this._collection,this._client,this._where,this._include,this._limit,this._single,i)}toJSON(){return {resource:this._collection.name,where:this._where,include:this._include,limit:this._limit,sort:this._sort}}static _init(e,t){return new d(e,t)}};var E=class{ws=null;url;autoConnect;autoReconnect;reconnectTimeout;reconnectLimit;reconnectAttempts=0;eventListeners=new Map;reconnectTimer=null;intentionallyDisconnected=false;credentials;constructor(e){this.url=e.url,this.autoConnect=e.autoConnect??false,this.autoReconnect=e.autoReconnect??false,this.reconnectTimeout=e.reconnectTimeout??5e3,this.reconnectLimit=e.reconnectLimit,this.credentials=e.credentials,this.autoConnect&&this.connect();}connected(){var e;return ((e=this.ws)==null?void 0:e.readyState)===WebSocket.OPEN}async connect(){if(this.ws&&(this.ws.readyState===WebSocket.OPEN||this.ws.readyState===WebSocket.CONNECTING))return;this.intentionallyDisconnected=false;let e=await b(this.credentials);this.ws=new WebSocket(this.url+(e?`?${stringify(e)}`:"")),this.ws.addEventListener("open",this.handleOpen.bind(this)),this.ws.addEventListener("close",this.handleClose.bind(this)),this.ws.addEventListener("error",this.handleError.bind(this)),this.ws.addEventListener("message",this.handleMessage.bind(this));}disconnect(){this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.intentionallyDisconnected=true,this.ws&&(this.ws.close(),this.ws=null);}addEventListener(e,t){var i;this.eventListeners.has(e)||this.eventListeners.set(e,new Set),(i=this.eventListeners.get(e))==null||i.add(t);}removeEventListener(e,t){var i;this.eventListeners.has(e)&&((i=this.eventListeners.get(e))==null||i.delete(t));}send(e){if(this.ws&&this.ws.readyState===WebSocket.OPEN)this.ws.send(e);else throw new Error("WebSocket is not open")}handleOpen(e){this.reconnectAttempts=0,this.dispatchEvent("open",e),this.dispatchEvent("connectionChange",{open:true});}handleClose(e){this.dispatchEvent("close",e),this.dispatchEvent("connectionChange",{open:false}),this.autoReconnect&&!this.intentionallyDisconnected&&this.scheduleReconnect();}handleError(e){var t,i;this.dispatchEvent("error",e),this.dispatchEvent("connectionChange",{open:false}),(i=(t=e.error)==null?void 0:t.message)!=null&&i.includes("non-101")&&(this.ws&&(this.ws.close(),this.ws=null),this.autoReconnect&&!this.intentionallyDisconnected&&this.scheduleReconnect());}handleMessage(e){this.dispatchEvent("message",e);}scheduleReconnect(){this.reconnectLimit&&this.reconnectAttempts>=this.reconnectLimit||(this.reconnectAttempts++,this.reconnectTimer=setTimeout(()=>{this.connect();},this.reconnectTimeout));}dispatchEvent(e,t){var i;(i=this.eventListeners.get(e))==null||i.forEach(s=>{s(t);});}};var T=class{nodes;constructor(){this.nodes=new Map;}createNode(e,t,i){if(this.nodes.has(e))throw new Error(`Node with id ${e} already exists`);let s={id:e,type:t,referencedBy:new Map(i.map(n=>[n,new Set])),references:new Map,subscriptions:new Set};return this.nodes.set(e,s),s}getNode(e){return this.nodes.get(e)}hasNode(e){return this.nodes.has(e)}createLink(e,t){let i=this.nodes.get(e),s=this.nodes.get(t);if(!i)throw new Error(`Source node with id ${e} does not exist`);if(!s)throw new Error(`Target node with id ${t} does not exist`);i.references.set(s.type,t);let n=s.referencedBy.get(i.type);n&&n instanceof Set?n.add(e):s.referencedBy.set(i.type,e),this.notifySubscribers(t);}removeLink(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);let s=i.references.get(t);if(!s)return;i.references.delete(t);let n=this.nodes.get(s);if(!n)return;let r=n.referencedBy.get(i.type);r&&(r instanceof Set?r.delete(e):n.referencedBy.delete(i.type),this.notifySubscribers(s)),this.notifySubscribers(e);}subscribe(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);return i.subscriptions.add(t),()=>{i.subscriptions.delete(t);}}removeNode(e){let t=this.nodes.get(e);t&&(Array.from(t.referencedBy.entries()).forEach(([i,s])=>{(s instanceof Set?Array.from(s.values()):[s]).forEach(r=>{let o=this.nodes.get(r);!o||!o.references.get(i)||(o.references.delete(i),this.notifySubscribers(r));});}),this.nodes.delete(e));}updateNode(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);t(i),this.notifySubscribers(e);}notifySubscribers(e){let t=this.nodes.get(e);t&&Array.from(t.subscriptions).forEach(i=>{try{i(e);}catch(s){console.error(`Error in node subscription for node ${e}:`,s);}});}getAllNodes(){return Array.from(this.nodes.values())}};var A="__meta",C="databases",R=class{db;async init(e,t){var y,h;if(typeof window>"u")return;let s=((y=(await window.indexedDB.databases()).find(l=>l.name===t))==null?void 0:y.version)??1,n=await m(e),r=Object.fromEntries(await Promise.all(Object.entries(e).map(async([l,u])=>[l,await m(u)]))),o=await openDB("live-state-databases",1,{upgrade(l){l.objectStoreNames.contains(C)||l.createObjectStore(C);}}),c=(h=await this.getAll(o,C))==null?void 0:h[t];(c==null?void 0:c.schemaHash)!==n&&s++,this.db=await openDB(t,s,{async upgrade(l){[...Object.keys(e),A].forEach(u=>{(c==null?void 0:c.objectHashes[u])!==r[u]&&l.objectStoreNames.contains(u)&&l.deleteObjectStore(u),l.objectStoreNames.contains(u)||l.createObjectStore(u);}),await o.put(C,{schemaHash:n,objectHashes:r},t);},blocking(){window.location.reload();},blocked(){window.location.reload();}});}async get(e){return await this.getAll(this.db,e)??{}}getOne(e,t){return this.db?this.db.get(e,t):new Promise(i=>i(void 0))}set(e,t,i){var s;return (s=this.db)==null?void 0:s.put(e,i,t)}delete(e,t){var i;return (i=this.db)==null?void 0:i.delete(e,t)}getMeta(e){return this.db?this.db.get(A,e):new Promise(t=>t(void 0))}setMeta(e,t){var i;return (i=this.db)==null?void 0:i.put(A,t,e)}async getAll(e,t){if(!e)return;if(e.getAllRecords)return e.getAllRecords(t);let[i,s]=await Promise.all([e.getAll(t),e.getAllKeys(t)]);return Object.fromEntries(i.map((n,r)=>[s[r],n]))}};var O=class{constructor(e,t,i){this.schema=e;this.kvStorage=new R,t!==false&&this.kvStorage.init(this.schema,t.name).then(()=>{this.kvStorage.getMeta("mutationStack").then(s=>{!s||Object.keys(s).length===0||(this.optimisticMutationStack=s,i==null||i(this.optimisticMutationStack));}).then(()=>{Object.entries(this.schema).forEach(([s,n])=>{this.kvStorage.get(s).then(r=>{!r||Object.keys(r).length===0||this.loadConsolidatedState(s,r);});});}).catch(s=>{console.error("Failed to load state from storage",s);});});}rawObjPool={};optimisticMutationStack={};optimisticObjGraph=new T;optimisticRawObjPool={};collectionSubscriptions=new Map;querySnapshots={};kvStorage;get(e$1,t,i=false){var r;let s=t??m(e$1);if(this.querySnapshots[s]&&!i){let o=this.querySnapshots[s];if(o)return o}let n=((r=e$1.where)!=null&&r.id?[e$1.where.id]:Object.keys(this.optimisticRawObjPool[e$1.resource]??{})).flatMap(o=>{let c=f(this.materializeOneWithInclude(o,e$1.include));return c?[c]:[]});if(e$1.sort&&e$1.sort.length>0){let o=(c,y)=>{for(let h of e$1.sort){let l=c[h.key],u=y[h.key];if(l<u)return h.direction==="asc"?-1:1;if(l>u)return h.direction==="asc"?1:-1}return 0};n.sort(o);}if(e$1.where||e$1.limit){let o=e$1.where?c=>d(c,e$1.where):()=>true;n=e(n,o,e$1.limit);}return i||(this.querySnapshots[s]=n),n}subscribe(e,t){var r;let i=m(e),s=this.collectionSubscriptions.get(i),n=this.schema[e.resource];return s||this.collectionSubscriptions.set(i,{callbacks:new Set,query:e,flatInclude:e.include?Object.keys(e.include).map(o=>n.relations[o].entity.name):void 0}),(r=this.collectionSubscriptions.get(i))==null||r.callbacks.add(t),()=>{var o,c;(o=this.collectionSubscriptions.get(i))==null||o.callbacks.delete(t),((c=this.collectionSubscriptions.get(i))==null?void 0:c.callbacks.size)===0&&(this.collectionSubscriptions.delete(i),delete this.querySnapshots[i]);}}addMutation(e,t,i=false){var c,y,h,l;let s=this.schema[e];if(console.log("Adding mutation",t),!s)throw new Error("Schema not found");let n=(c=this.optimisticRawObjPool[e])==null?void 0:c[t.resourceId];if(i)this.optimisticMutationStack[e]??=[],this.optimisticMutationStack[e].push(t);else {this.optimisticMutationStack[e]=((h=(y=this.optimisticMutationStack)==null?void 0:y[e])==null?void 0:h.filter(b=>b.id!==t.id))??[],this.rawObjPool[e]??={};let u={value:{...this.schema[e].mergeMutation("set",t.payload,this.rawObjPool[e][t.resourceId])[0].value,id:{value:t.resourceId}}};this.rawObjPool[e][t.resourceId]=u;let f=u.value;delete f.id,this.kvStorage.set(e,t.resourceId,f);}this.kvStorage.setMeta("mutationStack",this.optimisticMutationStack);let r=(l=this.rawObjPool[e])==null?void 0:l[t.resourceId],o=(this.optimisticMutationStack[e]??[]).reduce((u,f)=>f.resourceId!==t.resourceId?u:this.schema[e].mergeMutation("set",f.payload,u)[0],r);if(o&&(this.optimisticRawObjPool[e]??={},this.optimisticRawObjPool[e][t.resourceId]={value:{...o.value,id:{value:t.resourceId}}}),this.optimisticObjGraph.hasNode(t.resourceId)||this.optimisticObjGraph.createNode(t.resourceId,e,Object.values(s.relations).flatMap(u=>u.type==="many"?[u.entity.name]:[])),Object.keys(s.relations).length>0){let u=Object.fromEntries(Object.entries(s.relations).flatMap(([f,b])=>b.type==="one"?[[b.relationalColumn,f]]:[]));Object.entries(t.payload).forEach(([f,b])=>{let j=s.relations[u[f]];if(!b||!u[f])return;let S=n==null?void 0:n.value[f],[,w]=j.mergeMutation("set",b,S);if(w){if(!this.optimisticObjGraph.hasNode(w.value)){let W=j.entity.name;this.optimisticObjGraph.createNode(w.value,W,Object.values(this.schema[W].relations).flatMap(P=>P.type==="many"?[P.entity.name]:[]));}S!=null&&S.value&&this.optimisticObjGraph.removeLink(t.resourceId,j.entity.name),this.optimisticObjGraph.createLink(t.resourceId,w.value);}});}this.notifyCollectionSubscribers(e),this.optimisticObjGraph.notifySubscribers(t.resourceId);}loadConsolidatedState(e,t){Object.entries(t).forEach(([i,s])=>{this.addMutation(e,{id:i,type:"MUTATE",resource:e,resourceId:i,payload:s});});}materializeOneWithInclude(e,t={}){var c;if(!e)return;let i=this.optimisticObjGraph.getNode(e);if(!i)return;let s=i.type,n=(c=this.optimisticRawObjPool[s])==null?void 0:c[e];if(!n)return;let[r,o]=Object.keys(t).reduce((y,h)=>{let l=this.schema[s].relations[h];return l.type==="one"?y[0].push([h,l.entity.name]):l.type==="many"&&y[1].push([h,l.entity.name]),y},[[],[]]);return {value:{...n.value,...Object.fromEntries(r.map(([y,h])=>[y,this.materializeOneWithInclude(i.references.get(h))])),...Object.fromEntries(o.map(([y,h])=>{let l=i.referencedBy.get(h),u=l instanceof Set;return [y,u?{value:Array.from(l.values()).map(f=>this.materializeOneWithInclude(f))}:this.materializeOneWithInclude(l)]}))}}}notifyCollectionSubscribers(e){this.collectionSubscriptions.forEach(t=>{if(t.query.resource===e||t.flatInclude&&t.flatInclude.includes(e)){let i=m(t.query),s=this.querySnapshots[i],n=this.get(t.query,void 0,true);if(ae(n,s))return;this.querySnapshots[i]=n,t.callbacks.forEach(r=>{r(n);});}});}};var I=class{url;ws;store;remoteSubCounters={};eventListeners=new Set;replyHandlers={};constructor(e){this.url=e.url,this.store=new O(e.schema,e.storage,t=>{var i,s;(s=(i=Object.values(t))==null?void 0:i.flat())==null||s.forEach(n=>{this.sendWsMessage(n);});}),this.ws=new E({url:e.url,autoConnect:true,autoReconnect:true,reconnectTimeout:5e3,credentials:e.credentials}),this.ws.addEventListener("message",t=>{this.handleServerMessage(t.data);}),this.ws.addEventListener("connectionChange",t=>{this.emitEvent({type:"CONNECTION_STATE_CHANGE",open:t.open}),t.open&&(Object.keys(this.store.schema).forEach(i=>{this.sendWsMessage({id:a(),type:"QUERY",resource:i});}),Object.entries(this.remoteSubCounters).forEach(([i,s])=>{s>0&&this.sendWsMessage({id:a(),type:"SUBSCRIBE",resource:i});}),Object.values(this.store.optimisticMutationStack).forEach(i=>{i&&i.forEach(s=>{this.sendWsMessage(s);});}));});}get(e){return this.store.get(e)}handleServerMessage(e){try{console.log("Message received from the server:",e);let t=K.parse(JSON.parse(e));if(console.log("Parsed message:",t),this.emitEvent({type:"MESSAGE_RECEIVED",message:t}),t.type==="MUTATE"){let{resource:i}=t;try{this.store.addMutation(i,t);}catch(s){console.error("Error merging mutation from the server:",s);}}else if(t.type!=="REJECT"){if(t.type==="REPLY"){let{id:i,data:s}=t;if(this.replyHandlers[i]){clearTimeout(this.replyHandlers[i].timeoutHandle),this.replyHandlers[i].handler(s);return}let n=U.parse(s);this.store.loadConsolidatedState(n.resource,n.data);}}}catch(t){console.error("Error parsing message from the server:",t);}}subscribeToRemote(e){return this.remoteSubCounters[e]=(this.remoteSubCounters[e]??0)+1,this.sendWsMessage({id:a(),type:"SUBSCRIBE",resource:e}),()=>{this.remoteSubCounters[e]-=1,this.remoteSubCounters[e];}}subscribe(e,t){return this.store.subscribe(e,t)}mutate(e,t,i){var n;let s={id:a(),type:"MUTATE",resource:e,payload:this.store.schema[e].encodeMutation("set",i,new Date().toISOString()),resourceId:t};(n=this.store)==null||n.addMutation(e,s,true),this.sendWsMessage(s);}genericMutate(e,t,i){if(!this.ws||!this.ws.connected())throw new Error("WebSocket not connected");let s={id:a(),type:"MUTATE",resource:e,procedure:t,payload:i};return this.sendWsMessage(s),new Promise((n,r)=>{this.replyHandlers[s.id]={timeoutHandle:setTimeout(()=>{delete this.replyHandlers[s.id],r(new Error("Reply timeout"));},5e3),handler:o=>{delete this.replyHandlers[s.id],n(o);}};})}addEventListener(e){return this.eventListeners.add(e),()=>{this.eventListeners.delete(e);}}sendWsMessage(e){var t;(t=this.ws)!=null&&t.connected()&&this.ws.send(JSON.stringify(e));}emitEvent(e){this.eventListeners.forEach(t=>{t(e);});}},qe=d=>{let e=new I(d);return {client:{ws:e.ws,subscribe:t=>{let i=[];for(let s of t??Object.keys(e.store.schema))i.push(e.subscribeToRemote(s));return ()=>{console.log("Removing listeners",i),i.forEach(s=>{s();});}},addEventListener:t=>e.addEventListener(t)},store:{query:Object.entries(d.schema).reduce((t,[i,s])=>(t[i]=M._init(s,e),t),{}),mutate:c(()=>{},{apply:(t,i,s)=>{if(i.length<2)return;if(i.length>2)throw new Error("Trying to access an invalid path");let[n,r]=i;if(r==="insert"){let{id:o,...c}=s[0];return e.mutate(n,o,c)}if(r==="update"){let[o,c]=s;return e.mutate(n,o,c)}return e.genericMutate(n,r,s[0])}})}}};export{pe as SubscriptionProvider,qe as createClient,he as useLiveQuery};
|
|
1
|
+
import {c,a,f,d,e,b}from'./chunk-G5SVBG4B.js';import {useSyncExternalStore,useEffect}from'react';import {xxHash32}from'js-xxhash';import {jsx,Fragment}from'react/jsx-runtime';import {z}from'zod';import {stringify}from'qs';import ae from'fast-deep-equal';import {openDB}from'idb';var m=d=>xxHash32(JSON.stringify(d)).toString(32);var k=class{subscriptions=new Map;getOrStoreSubscription(e){let t=m(e);return this.subscriptions.has(t)?this.subscriptions.get(t).subscribe:(this.subscriptions.set(t,{subscribe:i=>{var n;(n=this.subscriptions.get(t))==null||n.callbacks.add(i);let s=e.subscribe(()=>{var r;(r=this.subscriptions.get(t))==null||r.callbacks.forEach(o=>{o();});});return ()=>{var r;(r=this.subscriptions.get(t))==null||r.callbacks.delete(i),setTimeout(()=>{var o;((o=this.subscriptions.get(t))==null?void 0:o.callbacks.size)===0&&(this.subscriptions.delete(t),s());},10);}},callbacks:new Set}),this.subscriptions.get(t).subscribe)}},F=new k,he=d=>useSyncExternalStore(F.getOrStoreSubscription(d),d.get),pe=({children:d,client:e})=>(useEffect(()=>{e.subscribe();},[e.subscribe]),jsx(Fragment,{children:d}));var N=z.object({resource:z.string(),where:z.record(z.string(),z.any()).optional(),include:z.record(z.string(),z.any()).optional(),lastSyncedAt:z.string().optional(),limit:z.number().optional(),sort:z.array(z.object({key:z.string(),direction:z.enum(["asc","desc"])})).optional()}),x=z.record(z.string(),z.object({value:z.string().or(z.number()).or(z.boolean()).or(z.date()).nullable(),_meta:z.object({timestamp:z.string().optional().nullable()}).optional()})).superRefine((d,e)=>{d.id&&e.addIssue({code:z.ZodIssueCode.custom,message:"Payload cannot have an id"});}),H=z.object({id:z.string().optional(),type:z.literal("MUTATE"),resource:z.string()}),_=H.extend({procedure:z.string(),payload:z.any().optional()}),L=H.extend({resourceId:z.string(),payload:x});z.union([_,L]);var v=z.string(),ee=z.object({id:v,type:z.literal("SUBSCRIBE"),resource:z.string()}),te=N.extend({id:v,type:z.literal("QUERY")}),q=L.extend({id:v}),ie=_.extend({id:v}),se=z.union([ie,q]);z.union([ee,te,se]);var ne=z.object({id:v,type:z.literal("REJECT"),resource:z.string(),message:z.string().optional()}),re=z.object({id:v,type:z.literal("REPLY"),data:z.any()}),K=z.union([ne,re,q]),U=z.object({resource:z.string(),data:z.record(z.string(),x)});var M=class d{_collection;_client;_where;_include;_limit;_single;_sort;constructor(e,t,i,s,n,r,o){this._collection=e,this._client=t,this._where=i??{},this._include=s??{},this._limit=n,this._single=r,this._sort=o,this.get=this.get.bind(this),this.subscribe=this.subscribe.bind(this);}where(e){return new d(this._collection,this._client,{...this._where,...e},this._include,this._limit,this._single,this._sort)}include(e){return new d(this._collection,this._client,this._where,{...this._include,...e},this._limit,this._single,this._sort)}get(){let e=this._client.get({resource:this._collection.name,where:this._where,include:this._include,limit:this._limit,sort:this._sort});return this._single?e[0]:e}subscribe(e){return this._client.subscribe({resource:this._collection.name,where:this._where,include:this._include,limit:this._limit,sort:this._sort},t=>{if(this._single)return e(t[0]);e(t);})}limit(e){return new d(this._collection,this._client,this._where,this._include,e,this._single,this._sort)}one(e){return this.first({id:e})}first(e){return new d(this._collection,this._client,e??this._where,this._include,1,true,this._sort)}orderBy(e,t="asc"){let i=[...this._sort??[],{key:e,direction:t}];return new d(this._collection,this._client,this._where,this._include,this._limit,this._single,i)}toJSON(){return {resource:this._collection.name,where:this._where,include:this._include,limit:this._limit,sort:this._sort}}static _init(e,t){return new d(e,t)}};var E=class{ws=null;url;autoConnect;autoReconnect;reconnectTimeout;reconnectLimit;reconnectAttempts=0;eventListeners=new Map;reconnectTimer=null;intentionallyDisconnected=false;credentials;constructor(e){this.url=e.url,this.autoConnect=e.autoConnect??false,this.autoReconnect=e.autoReconnect??false,this.reconnectTimeout=e.reconnectTimeout??5e3,this.reconnectLimit=e.reconnectLimit,this.credentials=e.credentials,this.autoConnect&&this.connect();}connected(){var e;return ((e=this.ws)==null?void 0:e.readyState)===WebSocket.OPEN}async connect(){if(this.ws&&(this.ws.readyState===WebSocket.OPEN||this.ws.readyState===WebSocket.CONNECTING))return;this.intentionallyDisconnected=false;let e=await b(this.credentials);this.ws=new WebSocket(this.url+(e?`?${stringify(e)}`:"")),this.ws.addEventListener("open",this.handleOpen.bind(this)),this.ws.addEventListener("close",this.handleClose.bind(this)),this.ws.addEventListener("error",this.handleError.bind(this)),this.ws.addEventListener("message",this.handleMessage.bind(this));}disconnect(){this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.intentionallyDisconnected=true,this.ws&&(this.ws.close(),this.ws=null);}addEventListener(e,t){var i;this.eventListeners.has(e)||this.eventListeners.set(e,new Set),(i=this.eventListeners.get(e))==null||i.add(t);}removeEventListener(e,t){var i;this.eventListeners.has(e)&&((i=this.eventListeners.get(e))==null||i.delete(t));}send(e){if(this.ws&&this.ws.readyState===WebSocket.OPEN)this.ws.send(e);else throw new Error("WebSocket is not open")}handleOpen(e){this.reconnectAttempts=0,this.dispatchEvent("open",e),this.dispatchEvent("connectionChange",{open:true});}handleClose(e){this.dispatchEvent("close",e),this.dispatchEvent("connectionChange",{open:false}),this.autoReconnect&&!this.intentionallyDisconnected&&this.scheduleReconnect();}handleError(e){var t,i;this.dispatchEvent("error",e),this.dispatchEvent("connectionChange",{open:false}),(i=(t=e.error)==null?void 0:t.message)!=null&&i.includes("non-101")&&(this.ws&&(this.ws.close(),this.ws=null),this.autoReconnect&&!this.intentionallyDisconnected&&this.scheduleReconnect());}handleMessage(e){this.dispatchEvent("message",e);}scheduleReconnect(){this.reconnectLimit&&this.reconnectAttempts>=this.reconnectLimit||(this.reconnectAttempts++,this.reconnectTimer=setTimeout(()=>{this.connect();},this.reconnectTimeout));}dispatchEvent(e,t){var i;(i=this.eventListeners.get(e))==null||i.forEach(s=>{s(t);});}};var T=class{nodes;constructor(){this.nodes=new Map;}createNode(e,t,i){if(this.nodes.has(e))throw new Error(`Node with id ${e} already exists`);let s={id:e,type:t,referencedBy:new Map(i.map(n=>[n,new Set])),references:new Map,subscriptions:new Set};return this.nodes.set(e,s),s}getNode(e){return this.nodes.get(e)}hasNode(e){return this.nodes.has(e)}createLink(e,t){let i=this.nodes.get(e),s=this.nodes.get(t);if(!i)throw new Error(`Source node with id ${e} does not exist`);if(!s)throw new Error(`Target node with id ${t} does not exist`);i.references.set(s.type,t);let n=s.referencedBy.get(i.type);n&&n instanceof Set?n.add(e):s.referencedBy.set(i.type,e),this.notifySubscribers(t);}removeLink(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);let s=i.references.get(t);if(!s)return;i.references.delete(t);let n=this.nodes.get(s);if(!n)return;let r=n.referencedBy.get(i.type);r&&(r instanceof Set?r.delete(e):n.referencedBy.delete(i.type),this.notifySubscribers(s)),this.notifySubscribers(e);}subscribe(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);return i.subscriptions.add(t),()=>{i.subscriptions.delete(t);}}removeNode(e){let t=this.nodes.get(e);t&&(Array.from(t.referencedBy.entries()).forEach(([i,s])=>{(s instanceof Set?Array.from(s.values()):[s]).forEach(r=>{let o=this.nodes.get(r);!o||!o.references.get(i)||(o.references.delete(i),this.notifySubscribers(r));});}),this.nodes.delete(e));}updateNode(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);t(i),this.notifySubscribers(e);}notifySubscribers(e){let t=this.nodes.get(e);t&&Array.from(t.subscriptions).forEach(i=>{try{i(e);}catch(s){console.error(`Error in node subscription for node ${e}:`,s);}});}getAllNodes(){return Array.from(this.nodes.values())}};var A="__meta",C="databases",R=class{db;async init(e,t){var y,p;if(typeof window>"u")return;let s=((y=(await window.indexedDB.databases()).find(l=>l.name===t))==null?void 0:y.version)??1,n=await m(e),r=Object.fromEntries(await Promise.all(Object.entries(e).map(async([l,u])=>[l,await m(u)]))),o=await openDB("live-state-databases",1,{upgrade(l){l.objectStoreNames.contains(C)||l.createObjectStore(C);}}),c=(p=await this.getAll(o,C))==null?void 0:p[t];(c==null?void 0:c.schemaHash)!==n&&s++,this.db=await openDB(t,s,{async upgrade(l){[...Object.keys(e),A].forEach(u=>{(c==null?void 0:c.objectHashes[u])!==r[u]&&l.objectStoreNames.contains(u)&&l.deleteObjectStore(u),l.objectStoreNames.contains(u)||l.createObjectStore(u);}),await o.put(C,{schemaHash:n,objectHashes:r},t);},blocking(){window.location.reload();},blocked(){window.location.reload();}});}async get(e){return await this.getAll(this.db,e)??{}}getOne(e,t){return this.db?this.db.get(e,t):new Promise(i=>i(void 0))}set(e,t,i){var s;return (s=this.db)==null?void 0:s.put(e,i,t)}delete(e,t){var i;return (i=this.db)==null?void 0:i.delete(e,t)}getMeta(e){return this.db?this.db.get(A,e):new Promise(t=>t(void 0))}setMeta(e,t){var i;return (i=this.db)==null?void 0:i.put(A,t,e)}async getAll(e,t){if(!e)return;if(e.getAllRecords)return e.getAllRecords(t);let[i,s]=await Promise.all([e.getAll(t),e.getAllKeys(t)]);return Object.fromEntries(i.map((n,r)=>[s[r],n]))}};var O=class{constructor(e,t,i){this.schema=e;this.kvStorage=new R,t!==false&&this.kvStorage.init(this.schema,t.name).then(()=>{this.kvStorage.getMeta("mutationStack").then(s=>{!s||Object.keys(s).length===0||(this.optimisticMutationStack=s,i==null||i(this.optimisticMutationStack));}).then(()=>{Object.entries(this.schema).forEach(([s,n])=>{this.kvStorage.get(s).then(r=>{!r||Object.keys(r).length===0||this.loadConsolidatedState(s,r);});});}).catch(s=>{console.error("Failed to load state from storage",s);});});}rawObjPool={};optimisticMutationStack={};optimisticObjGraph=new T;optimisticRawObjPool={};collectionSubscriptions=new Map;querySnapshots={};kvStorage;get(e$1,t,i=false){var r;let s=t??m(e$1);if(this.querySnapshots[s]&&!i){let o=this.querySnapshots[s];if(o)return o}let n=((r=e$1.where)!=null&&r.id?[e$1.where.id]:Object.keys(this.optimisticRawObjPool[e$1.resource]??{})).flatMap(o=>{let c=f(this.materializeOneWithInclude(o,e$1.include));return c?[c]:[]});if(e$1.sort&&e$1.sort.length>0){let o=(c,y)=>{for(let p of e$1.sort){let l=c[p.key],u=y[p.key];if(l<u)return p.direction==="asc"?-1:1;if(l>u)return p.direction==="asc"?1:-1}return 0};n.sort(o);}if(e$1.where||e$1.limit){let o=e$1.where?c=>d(c,e$1.where):()=>true;n=e(n,o,e$1.limit);}return i||(this.querySnapshots[s]=n),n}subscribe(e,t){var r;let i=m(e),s=this.collectionSubscriptions.get(i),n=this.schema[e.resource];return s||this.collectionSubscriptions.set(i,{callbacks:new Set,query:e,flatInclude:e.include?Object.keys(e.include).map(o=>n.relations[o].entity.name):void 0}),(r=this.collectionSubscriptions.get(i))==null||r.callbacks.add(t),()=>{var o,c;(o=this.collectionSubscriptions.get(i))==null||o.callbacks.delete(t),((c=this.collectionSubscriptions.get(i))==null?void 0:c.callbacks.size)===0&&(this.collectionSubscriptions.delete(i),delete this.querySnapshots[i]);}}addMutation(e,t,i=false){var c,y,p,l;let s=this.schema[e];if(console.log("Adding mutation",t),!s)throw new Error("Schema not found");let n=(c=this.optimisticRawObjPool[e])==null?void 0:c[t.resourceId];if(i)this.optimisticMutationStack[e]??=[],this.optimisticMutationStack[e].push(t);else {this.optimisticMutationStack[e]=((p=(y=this.optimisticMutationStack)==null?void 0:y[e])==null?void 0:p.filter(b=>b.id!==t.id))??[],this.rawObjPool[e]??={};let u={value:{...this.schema[e].mergeMutation("set",t.payload,this.rawObjPool[e][t.resourceId])[0].value,id:{value:t.resourceId}}};this.rawObjPool[e][t.resourceId]=u;let f=u.value;delete f.id,this.kvStorage.set(e,t.resourceId,f);}this.kvStorage.setMeta("mutationStack",this.optimisticMutationStack);let r=(l=this.rawObjPool[e])==null?void 0:l[t.resourceId],o=(this.optimisticMutationStack[e]??[]).reduce((u,f)=>f.resourceId!==t.resourceId?u:this.schema[e].mergeMutation("set",f.payload,u)[0],r);if(o&&(this.optimisticRawObjPool[e]??={},this.optimisticRawObjPool[e][t.resourceId]={value:{...o.value,id:{value:t.resourceId}}}),this.optimisticObjGraph.hasNode(t.resourceId)||this.optimisticObjGraph.createNode(t.resourceId,e,Object.values(s.relations).flatMap(u=>u.type==="many"?[u.entity.name]:[])),Object.keys(s.relations).length>0){let u=Object.fromEntries(Object.entries(s.relations).flatMap(([f,b])=>b.type==="one"?[[b.relationalColumn,f]]:[]));Object.entries(t.payload).forEach(([f,b])=>{let j=s.relations[u[f]];if(!b||!u[f])return;let S=n==null?void 0:n.value[f],[,w]=j.mergeMutation("set",b,S);if(w){if(!this.optimisticObjGraph.hasNode(w.value)){let W=j.entity.name;this.optimisticObjGraph.createNode(w.value,W,Object.values(this.schema[W].relations).flatMap(P=>P.type==="many"?[P.entity.name]:[]));}S!=null&&S.value&&this.optimisticObjGraph.removeLink(t.resourceId,j.entity.name),this.optimisticObjGraph.createLink(t.resourceId,w.value);}});}this.notifyCollectionSubscribers(e),this.optimisticObjGraph.notifySubscribers(t.resourceId);}loadConsolidatedState(e,t){Object.entries(t).forEach(([i,s])=>{this.addMutation(e,{id:i,type:"MUTATE",resource:e,resourceId:i,payload:s});});}materializeOneWithInclude(e,t={}){var c;if(!e)return;let i=this.optimisticObjGraph.getNode(e);if(!i)return;let s=i.type,n=(c=this.optimisticRawObjPool[s])==null?void 0:c[e];if(!n)return;let[r,o]=Object.keys(t).reduce((y,p)=>{let l=this.schema[s].relations[p];return l.type==="one"?y[0].push([p,l.entity.name]):l.type==="many"&&y[1].push([p,l.entity.name]),y},[[],[]]);return {value:{...n.value,...Object.fromEntries(r.map(([y,p])=>[y,this.materializeOneWithInclude(i.references.get(p))])),...Object.fromEntries(o.map(([y,p])=>{let l=i.referencedBy.get(p),u=l instanceof Set;return [y,u?{value:Array.from(l.values()).map(f=>this.materializeOneWithInclude(f))}:this.materializeOneWithInclude(l)]}))}}}notifyCollectionSubscribers(e){this.collectionSubscriptions.forEach(t=>{if(t.query.resource===e||t.flatInclude&&t.flatInclude.includes(e)){let i=m(t.query),s=this.querySnapshots[i],n=this.get(t.query,void 0,true);if(ae(n,s))return;this.querySnapshots[i]=n,t.callbacks.forEach(r=>{r(n);});}});}};var I=class{url;ws;store;remoteSubCounters={};eventListeners=new Set;replyHandlers={};constructor(e){this.url=e.url,this.store=new O(e.schema,e.storage,t=>{var i,s;(s=(i=Object.values(t))==null?void 0:i.flat())==null||s.forEach(n=>{this.sendWsMessage(n);});}),this.ws=new E({url:e.url,autoConnect:true,autoReconnect:true,reconnectTimeout:5e3,credentials:e.credentials}),this.ws.addEventListener("message",t=>{this.handleServerMessage(t.data);}),this.ws.addEventListener("connectionChange",t=>{this.emitEvent({type:"CONNECTION_STATE_CHANGE",open:t.open}),t.open&&(Object.keys(this.store.schema).forEach(i=>{this.sendWsMessage({id:a(),type:"QUERY",resource:i});}),Object.entries(this.remoteSubCounters).forEach(([i,s])=>{s>0&&this.sendWsMessage({id:a(),type:"SUBSCRIBE",resource:i});}),Object.values(this.store.optimisticMutationStack).forEach(i=>{i&&i.forEach(s=>{this.sendWsMessage(s);});}));});}get(e){return this.store.get(e)}handleServerMessage(e){try{console.log("Message received from the server:",e);let t=K.parse(JSON.parse(e));if(console.log("Parsed message:",t),this.emitEvent({type:"MESSAGE_RECEIVED",message:t}),t.type==="MUTATE"){let{resource:i}=t;try{this.store.addMutation(i,t);}catch(s){console.error("Error merging mutation from the server:",s);}}else if(t.type!=="REJECT"){if(t.type==="REPLY"){let{id:i,data:s}=t;if(this.replyHandlers[i]){clearTimeout(this.replyHandlers[i].timeoutHandle),this.replyHandlers[i].handler(s);return}let n=U.parse(s);this.store.loadConsolidatedState(n.resource,n.data);}}}catch(t){console.error("Error parsing message from the server:",t);}}subscribeToRemote(e){return this.remoteSubCounters[e]=(this.remoteSubCounters[e]??0)+1,this.sendWsMessage({id:a(),type:"SUBSCRIBE",resource:e}),()=>{this.remoteSubCounters[e]-=1,this.remoteSubCounters[e];}}subscribe(e,t){return this.store.subscribe(e,t)}mutate(e,t,i){var n;let s={id:a(),type:"MUTATE",resource:e,payload:this.store.schema[e].encodeMutation("set",i,new Date().toISOString()),resourceId:t};(n=this.store)==null||n.addMutation(e,s,true),this.sendWsMessage(s);}genericMutate(e,t,i){if(!this.ws||!this.ws.connected())throw new Error("WebSocket not connected");let s={id:a(),type:"MUTATE",resource:e,procedure:t,payload:i};return this.sendWsMessage(s),new Promise((n,r)=>{this.replyHandlers[s.id]={timeoutHandle:setTimeout(()=>{delete this.replyHandlers[s.id],r(new Error("Reply timeout"));},5e3),handler:o=>{delete this.replyHandlers[s.id],n(o);}};})}addEventListener(e){return this.eventListeners.add(e),()=>{this.eventListeners.delete(e);}}sendWsMessage(e){var t;(t=this.ws)!=null&&t.connected()&&this.ws.send(JSON.stringify(e));}emitEvent(e){this.eventListeners.forEach(t=>{t(e);});}},qe=d=>{let e=new I(d);return {client:{ws:e.ws,subscribe:t=>{let i=[];for(let s of t??Object.keys(e.store.schema))i.push(e.subscribeToRemote(s));return ()=>{console.log("Removing listeners",i),i.forEach(s=>{s();});}},addEventListener:t=>e.addEventListener(t)},store:{query:Object.entries(d.schema).reduce((t,[i,s])=>(t[i]=M._init(s,e),t),{}),mutate:c(()=>{},{apply:(t,i,s)=>{if(i.length<2)return;if(i.length>2)throw new Error("Trying to access an invalid path");let[n,r]=i;if(r==="insert"){let{id:o,...c}=s[0];return e.mutate(n,o,c)}if(r==="update"){let[o,c]=s;return e.mutate(n,o,c)}return e.genericMutate(n,r,s[0])}})}}};export{pe as SubscriptionProvider,qe as createClient,he as useLiveQuery};
|
package/dist/fetch-client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { A as AnyRouter, C as ClientOptions, L as LiveObjectAny, W as WhereClause, I as IncludeClause, S as Simplify, a as InferLiveObject, b as LiveObjectMutationInput } from './index-
|
|
1
|
+
import { A as AnyRouter, C as ClientOptions, L as LiveObjectAny, W as WhereClause, I as IncludeClause, S as Simplify, a as InferLiveObject, b as LiveObjectMutationInput } from './index-C1FzxdnB.js';
|
|
2
2
|
import 'react/jsx-runtime';
|
|
3
3
|
import 'zod';
|
|
4
4
|
|
|
@@ -261,6 +261,8 @@ type Simplify<T> = T extends Record<string, unknown> ? {
|
|
|
261
261
|
[K in keyof T]: Simplify<T[K]>;
|
|
262
262
|
} : T;
|
|
263
263
|
|
|
264
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
265
|
+
|
|
264
266
|
declare abstract class Storage {
|
|
265
267
|
abstract findOne<T extends LiveObjectAny>(resource: T, id: string, options?: {
|
|
266
268
|
include?: IncludeClause<T>;
|
|
@@ -301,94 +303,27 @@ declare const SubscriptionProvider: ({ children, client, }: {
|
|
|
301
303
|
client: Client<AnyRouter>["client"];
|
|
302
304
|
}) => react_jsx_runtime.JSX.Element;
|
|
303
305
|
|
|
304
|
-
declare const serverMessageSchema: z.ZodUnion<[z.ZodObject<{
|
|
306
|
+
declare const serverMessageSchema: z.ZodUnion<readonly [z.ZodObject<{
|
|
305
307
|
id: z.ZodString;
|
|
306
308
|
type: z.ZodLiteral<"REJECT">;
|
|
307
309
|
resource: z.ZodString;
|
|
308
310
|
message: z.ZodOptional<z.ZodString>;
|
|
309
|
-
},
|
|
310
|
-
type: "REJECT";
|
|
311
|
-
id: string;
|
|
312
|
-
resource: string;
|
|
313
|
-
message?: string | undefined;
|
|
314
|
-
}, {
|
|
315
|
-
type: "REJECT";
|
|
316
|
-
id: string;
|
|
317
|
-
resource: string;
|
|
318
|
-
message?: string | undefined;
|
|
319
|
-
}>, z.ZodObject<{
|
|
311
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
320
312
|
id: z.ZodString;
|
|
321
313
|
type: z.ZodLiteral<"REPLY">;
|
|
322
314
|
data: z.ZodAny;
|
|
323
|
-
},
|
|
324
|
-
type: "REPLY";
|
|
325
|
-
id: string;
|
|
326
|
-
data?: any;
|
|
327
|
-
}, {
|
|
328
|
-
type: "REPLY";
|
|
329
|
-
id: string;
|
|
330
|
-
data?: any;
|
|
331
|
-
}>, z.ZodObject<z.objectUtil.extendShape<z.objectUtil.extendShape<{
|
|
332
|
-
id: z.ZodOptional<z.ZodString>;
|
|
315
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
333
316
|
type: z.ZodLiteral<"MUTATE">;
|
|
334
317
|
resource: z.ZodString;
|
|
335
|
-
}, {
|
|
336
318
|
resourceId: z.ZodString;
|
|
337
|
-
payload: z.
|
|
319
|
+
payload: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
338
320
|
value: z.ZodNullable<z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodDate]>>;
|
|
339
321
|
_meta: z.ZodOptional<z.ZodObject<{
|
|
340
322
|
timestamp: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
341
|
-
},
|
|
342
|
-
|
|
343
|
-
}, {
|
|
344
|
-
timestamp?: string | null | undefined;
|
|
345
|
-
}>>;
|
|
346
|
-
}, "strip", z.ZodTypeAny, {
|
|
347
|
-
value: string | number | boolean | Date | null;
|
|
348
|
-
_meta?: {
|
|
349
|
-
timestamp?: string | null | undefined;
|
|
350
|
-
} | undefined;
|
|
351
|
-
}, {
|
|
352
|
-
value: string | number | boolean | Date | null;
|
|
353
|
-
_meta?: {
|
|
354
|
-
timestamp?: string | null | undefined;
|
|
355
|
-
} | undefined;
|
|
356
|
-
}>>, Record<string, {
|
|
357
|
-
value: string | number | boolean | Date | null;
|
|
358
|
-
_meta?: {
|
|
359
|
-
timestamp?: string | null | undefined;
|
|
360
|
-
} | undefined;
|
|
361
|
-
}>, Record<string, {
|
|
362
|
-
value: string | number | boolean | Date | null;
|
|
363
|
-
_meta?: {
|
|
364
|
-
timestamp?: string | null | undefined;
|
|
365
|
-
} | undefined;
|
|
366
|
-
}>>;
|
|
367
|
-
}>, {
|
|
323
|
+
}, z.core.$strip>>;
|
|
324
|
+
}, z.core.$strip>>;
|
|
368
325
|
id: z.ZodString;
|
|
369
|
-
}
|
|
370
|
-
type: "MUTATE";
|
|
371
|
-
id: string;
|
|
372
|
-
resource: string;
|
|
373
|
-
payload: Record<string, {
|
|
374
|
-
value: string | number | boolean | Date | null;
|
|
375
|
-
_meta?: {
|
|
376
|
-
timestamp?: string | null | undefined;
|
|
377
|
-
} | undefined;
|
|
378
|
-
}>;
|
|
379
|
-
resourceId: string;
|
|
380
|
-
}, {
|
|
381
|
-
type: "MUTATE";
|
|
382
|
-
id: string;
|
|
383
|
-
resource: string;
|
|
384
|
-
payload: Record<string, {
|
|
385
|
-
value: string | number | boolean | Date | null;
|
|
386
|
-
_meta?: {
|
|
387
|
-
timestamp?: string | null | undefined;
|
|
388
|
-
} | undefined;
|
|
389
|
-
}>;
|
|
390
|
-
resourceId: string;
|
|
391
|
-
}>]>;
|
|
326
|
+
}, z.core.$strip>]>;
|
|
392
327
|
type ServerMessage = z.infer<typeof serverMessageSchema>;
|
|
393
328
|
|
|
394
329
|
/** biome-ignore-all lint/complexity/noBannedTypes: <explanation> */
|
package/dist/server.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var je=require('qs'),zod=require('zod'),j=require('crypto'),kysely=require('kysely'),postgres=require('kysely/helpers/postgres');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var je__default=/*#__PURE__*/_interopDefault(je);var j__default=/*#__PURE__*/_interopDefault(j);var pe=Object.create;var Q=Object.defineProperty;var me=Object.getOwnPropertyDescriptor;var Te=Object.getOwnPropertyNames;var fe=Object.getPrototypeOf,he=Object.prototype.hasOwnProperty;var ge=(n,e)=>()=>(e||n((e={exports:{}}).exports,e),e.exports);var Re=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of Te(e))!he.call(n,a)&&a!==t&&Q(n,a,{get:()=>e[a],enumerable:!(r=me(e,a))||r.enumerable});return n};var B=(n,e,t)=>(t=n!=null?pe(fe(n)):{},Re(Q(t,"default",{value:n,enumerable:true}),n));var P=ge(E=>{Object.defineProperty(E,"__esModule",{value:true});E.parse=Ie;E.serialize=Le;var ve=/^[\u0021-\u003A\u003C\u003E-\u007E]+$/,xe=/^[\u0021-\u003A\u003C-\u007E]*$/,be=/^([.]?[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)([.][a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/i,we=/^[\u0020-\u003A\u003D-\u007E]*$/,Me=Object.prototype.toString,Se=(()=>{let n=function(){};return n.prototype=Object.create(null),n})();function Ie(n,e){let t=new Se,r=n.length;if(r<2)return t;let a=(e==null?void 0:e.decode)||Ae,i=0;do{let o=n.indexOf("=",i);if(o===-1)break;let c=n.indexOf(";",i),s=c===-1?r:c;if(o>s){i=n.lastIndexOf(";",o-1)+1;continue}let T=J(n,i,o),m=Y(n,o,T),d=n.slice(T,m);if(t[d]===void 0){let y=J(n,o+1,s),u=Y(n,s,y),l=a(n.slice(y,u));t[d]=l;}i=s+1;}while(i<r);return t}function J(n,e,t){do{let r=n.charCodeAt(e);if(r!==32&&r!==9)return e}while(++e<t);return t}function Y(n,e,t){for(;e>t;){let r=n.charCodeAt(--e);if(r!==32&&r!==9)return e+1}return t}function Le(n,e,t){let r=(t==null?void 0:t.encode)||encodeURIComponent;if(!ve.test(n))throw new TypeError(`argument name is invalid: ${n}`);let a=r(e);if(!xe.test(a))throw new TypeError(`argument val is invalid: ${e}`);let i=n+"="+a;if(!t)return i;if(t.maxAge!==void 0){if(!Number.isInteger(t.maxAge))throw new TypeError(`option maxAge is invalid: ${t.maxAge}`);i+="; Max-Age="+t.maxAge;}if(t.domain){if(!be.test(t.domain))throw new TypeError(`option domain is invalid: ${t.domain}`);i+="; Domain="+t.domain;}if(t.path){if(!we.test(t.path))throw new TypeError(`option path is invalid: ${t.path}`);i+="; Path="+t.path;}if(t.expires){if(!Ee(t.expires)||!Number.isFinite(t.expires.valueOf()))throw new TypeError(`option expires is invalid: ${t.expires}`);i+="; Expires="+t.expires.toUTCString();}if(t.httpOnly&&(i+="; HttpOnly"),t.secure&&(i+="; Secure"),t.partitioned&&(i+="; Partitioned"),t.priority)switch(typeof t.priority=="string"?t.priority.toLowerCase():void 0){case "low":i+="; Priority=Low";break;case "medium":i+="; Priority=Medium";break;case "high":i+="; Priority=High";break;default:throw new TypeError(`option priority is invalid: ${t.priority}`)}if(t.sameSite)switch(typeof t.sameSite=="string"?t.sameSite.toLowerCase():t.sameSite){case true:case "strict":i+="; SameSite=Strict";break;case "lax":i+="; SameSite=Lax";break;case "none":i+="; SameSite=None";break;default:throw new TypeError(`option sameSite is invalid: ${t.sameSite}`)}return i}function Ae(n){if(n.indexOf("%")===-1)return n;try{return decodeURIComponent(n)}catch{return n}}function Ee(n){return Me.call(n)==="[object Date]"}});var te=B(P());var O=zod.z.object({resource:zod.z.string(),where:zod.z.record(zod.z.any()).optional(),include:zod.z.record(zod.z.any()).optional(),lastSyncedAt:zod.z.string().optional(),limit:zod.z.number().optional(),sort:zod.z.array(zod.z.object({key:zod.z.string(),direction:zod.z.enum(["asc","desc"])})).optional()}),V=zod.z.record(zod.z.object({value:zod.z.string().or(zod.z.number()).or(zod.z.boolean()).or(zod.z.date()).nullable(),_meta:zod.z.object({timestamp:zod.z.string().optional().nullable()}).optional()})).superRefine((n,e)=>{n.id&&e.addIssue({code:zod.z.ZodIssueCode.custom,message:"Payload cannot have an id"});}),X=zod.z.object({id:zod.z.string().optional(),type:zod.z.literal("MUTATE"),resource:zod.z.string()}),S=X.extend({procedure:zod.z.string(),payload:zod.z.any().optional()}),I=X.extend({resourceId:zod.z.string(),payload:V});zod.z.union([S,I]);var ee=O.omit({resource:true}),N=S.omit({id:true,type:true,resource:true,procedure:true}),F=I.omit({id:true,type:true,resource:true});zod.z.union([F,N]);var ne=n=>async e=>{var t;try{let r=typeof e.headers.getSetCookie=="function"?Object.fromEntries(e.headers):e.headers,a={headers:r,cookies:r.cookie?te.default.parse(r.cookie):{}},i=new URL(e.url),o=i.pathname.split("/"),c=i.searchParams,s=je__default.default.parse(c.toString()),T=await((t=n.contextProvider)==null?void 0:t.call(n,{transport:"HTTP",headers:a.headers,cookies:a.cookies,query:s}))??{};if(e.method==="GET"){let m=o[o.length-1],{success:d,data:y,error:u}=ee.safeParse(s);if(!d)return Response.json({message:"Invalid query",code:"INVALID_QUERY",details:u},{status:400});let l=await n.handleRequest({req:{...a,type:"QUERY",resourceName:m,context:T,where:y.where,include:y.include,query:s}});return !l||!l.data?Response.json({message:"Invalid resource",code:"INVALID_RESOURCE"},{status:400}):Response.json(l.data)}if(e.method==="POST")try{let m=o[o.length-1],d=o[o.length-2],y=e.body?await e.json():{},u;if(m==="set"){let{success:w,data:M,error:_}=F.safeParse(y);if(!w)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:_},{status:400});u=M;}else {let{success:w,data:M,error:_}=N.safeParse(y);if(!w)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:_},{status:400});u=M;}let l=await n.handleRequest({req:{...a,type:"MUTATE",resourceName:d,input:u.payload,context:T,resourceId:u.resourceId,procedure:m!=="set"?m:void 0,query:{}}});return Response.json(l)}catch(m){return console.error("Error parsing mutation from the client:",m),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}return Response.json({message:"Not found",code:"NOT_FOUND"},{status:404})}catch(r){return console.error("Unexpected error:",r),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}};var ue=B(P());var x=zod.z.string(),Ce=zod.z.object({id:x,type:zod.z.literal("SUBSCRIBE"),resource:zod.z.string()}),_e=O.extend({id:x,type:zod.z.literal("QUERY")}),re=I.extend({id:x}),Pe=S.extend({id:x}),Ve=zod.z.union([Pe,re]),ae=zod.z.union([Ce,_e,Ve]),Ne=zod.z.object({id:x,type:zod.z.literal("REJECT"),resource:zod.z.string(),message:zod.z.string().optional()}),Fe=zod.z.object({id:x,type:zod.z.literal("REPLY"),data:zod.z.any()});zod.z.union([Ne,Fe,re]);zod.z.object({resource:zod.z.string(),data:zod.z.record(V)});var oe="0123456789ABCDEFGHJKMNPQRSTVWXYZ",L=32;var $e=16,se=10,ie=0xffffffffffff;var h;(function(n){n.Base32IncorrectEncoding="B32_ENC_INVALID",n.DecodeTimeInvalidCharacter="DEC_TIME_CHAR",n.DecodeTimeValueMalformed="DEC_TIME_MALFORMED",n.EncodeTimeNegative="ENC_TIME_NEG",n.EncodeTimeSizeExceeded="ENC_TIME_SIZE_EXCEED",n.EncodeTimeValueMalformed="ENC_TIME_MALFORMED",n.PRNGDetectFailure="PRNG_DETECT",n.ULIDInvalid="ULID_INVALID",n.Unexpected="UNEXPECTED",n.UUIDInvalid="UUID_INVALID";})(h||(h={}));var g=class extends Error{constructor(e,t){super(`${t} (${e})`),this.name="ULIDError",this.code=e;}};function Ke(n){let e=Math.floor(n()*L);return e===L&&(e=L-1),oe.charAt(e)}function De(n){var r;let e=qe(),t=e&&(e.crypto||e.msCrypto)||(typeof j__default.default<"u"?j__default.default:null);if(typeof(t==null?void 0:t.getRandomValues)=="function")return ()=>{let a=new Uint8Array(1);return t.getRandomValues(a),a[0]/255};if(typeof(t==null?void 0:t.randomBytes)=="function")return ()=>t.randomBytes(1).readUInt8()/255;if((r=j__default.default)!=null&&r.randomBytes)return ()=>j__default.default.randomBytes(1).readUInt8()/255;throw new g(h.PRNGDetectFailure,"Failed to find a reliable PRNG")}function qe(){return ze()?self:typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:null}function Ue(n,e){let t="";for(;n>0;n--)t=Ke(e)+t;return t}function ke(n,e=se){if(isNaN(n))throw new g(h.EncodeTimeValueMalformed,`Time must be a number: ${n}`);if(n>ie)throw new g(h.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${ie}: ${n}`);if(n<0)throw new g(h.EncodeTimeNegative,`Time must be positive: ${n}`);if(Number.isInteger(n)===false)throw new g(h.EncodeTimeValueMalformed,`Time must be an integer: ${n}`);let t,r="";for(let a=e;a>0;a--)t=n%L,r=oe.charAt(t)+r,n=(n-t)/L;return r}function ze(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function ce(n,e){let t=De(),r=Date.now();return ke(r,se)+Ue($e,t)}var $=()=>ce().toLowerCase();var le=n=>{let e={},t={};return n.subscribeToMutations(r=>{let a=r;!a.resourceId||!a.payload||(console.log("Mutation propagated:",a),Object.entries(t[a.resource]??{}).forEach(([i,o])=>{var c;(c=e[i])==null||c.send(JSON.stringify({...a,id:a.id??$()}));}));}),(r,a)=>{var m;let i=d=>{r.send(JSON.stringify(d));},o=$(),c={headers:a.headers,cookies:typeof a.headers.cookie=="string"?ue.default.parse(a.headers.cookie):{}},s=je.parse(a.url.split("?")[1]),T=(m=n.contextProvider)==null?void 0:m.call(n,{transport:"WEBSOCKET",headers:c.headers,cookies:c.cookies,query:s});e[o]=r,console.log("Client connected:",o),r.on("message",async d=>{try{console.log("Message received from the client:",d);let y=ae.parse(JSON.parse(d.toString()));if(y.type==="SUBSCRIBE"){let{resource:u}=y;t[u]||(t[u]={}),t[u][o]={};}else if(y.type==="QUERY"){let{resource:u}=y,l=await n.handleRequest({req:{...c,type:"QUERY",resourceName:u,context:await T??{},query:s}});if(!l||!l.data)throw new Error("Invalid resource");i({id:y.id,type:"REPLY",data:{resource:u,data:Object.fromEntries(Object.entries(l.data??{}).map(([w,M])=>[w,M.value]))}});}else if(y.type==="MUTATE"){let{resource:u}=y;console.log("Received mutation from client:",y);try{let l=await n.handleRequest({req:{...c,type:"MUTATE",resourceName:u,input:y.payload,context:{messageId:y.id,...await T??{}},resourceId:y.resourceId,procedure:y.procedure,query:s}});y.procedure&&i({id:y.id,type:"REPLY",data:l});}catch(l){i({id:y.id,type:"REJECT",resource:u,message:l.message}),console.error("Error parsing mutation from the client:",l);}}}catch(y){console.error("Error handling message from the client:",y);}}),r.on("close",()=>{console.log("Connection closed",o),delete e[o];for(let d of Object.values(t))delete d[o];});}};function de(n){let e=`${n.protocol}://${n.hostname}${n.url}`,t=new Headers;return Object.entries(n.headers).forEach(([r,a])=>{a&&t.set(r,Array.isArray(a)?a.join(","):a);}),new Request(e,{method:n.method,headers:t,body:n.body&&n.method!=="GET"?JSON.stringify(n.body):void 0})}var It=(n,e,t)=>{n.ws(`${(t==null?void 0:t.basePath)??""}/ws`,le(e)),n.use(`${(t==null?void 0:t.basePath)??""}/`,(r,a)=>{ne(e)(de(r)).then(o=>o.json().then(c=>a.status(o.status).send(c)));});};var K=class n{routes;constructor(e){this.routes=e.routes;}static create(e){return new n(e)}},Et=n=>K.create({...n}),He=n=>({handler:e=>({inputValidator:n??zod.z.undefined(),handler:e})}),D=class n{_resourceSchema;resourceName;middlewares;customMutations;constructor(e,t){this.resourceName=e,this.middlewares=new Set,this.customMutations=t??{};}handleFind=async({req:e,db:t})=>({data:await t.rawFind(e.resourceName,e.where,e.include),acceptedValues:null});handleSet=async({req:e,db:t,schema:r})=>{if(!e.input)throw new Error("Payload is required");if(!e.resourceId)throw new Error("ResourceId is required");let a=await t.rawFindById(e.resourceName,e.resourceId),[i,o]=r[this.resourceName].mergeMutation("set",e.input,a);if(!o)throw new Error("Mutation rejected");return {data:await t.rawUpsert(e.resourceName,e.resourceId,i),acceptedValues:o}};async handleRequest(e){let t=r=>(()=>{if(r.type==="QUERY")return this.handleFind({req:r,db:e.db,schema:e.schema});if(r.type==="MUTATE")if(r.procedure){if(this.customMutations[r.procedure]){let a=this.customMutations[r.procedure].inputValidator.parse(r.input);return r.input=a,this.customMutations[r.procedure].handler({req:r,db:e.db,schema:e.schema})}}else return this.handleSet({req:r,db:e.db,schema:e.schema});throw new Error("Invalid request")})();return await Array.from(this.middlewares.values()).reduceRight((r,a)=>i=>a({req:i,next:r}),async r=>t(r))(e.req)}use(...e){for(let t of e)this.middlewares.add(t);return this}withMutations(e){return new n(this.resourceName,e({mutation:He}))}},q=class n{middlewares;constructor(e=[]){this.middlewares=e;}createBasicRoute(e){return new D(e.name).use(...this.middlewares)}use(...e){return new n([...this.middlewares,...e])}static create(){return new n}},Ot=q.create;var v=n=>{if(n)return Array.isArray(n.value)?n.value.map(e=>v(e)):typeof n.value!="object"||n.value===null||n.value instanceof Date?n.value:Object.fromEntries(Object.entries(n.value).map(([e,t])=>[e,v(t)]))};var H=class{async insert(e,t){let r=new Date().toISOString();return v(await this.rawUpsert(e.name,t.id,{value:Object.fromEntries(Object.entries(t).map(([a,i])=>[a,{value:i,_meta:{timestamp:r}}]))}))}async update(e,t,r){let a=new Date().toISOString(),{id:i,...o}=r;return v(await this.rawUpsert(e.name,t,{value:Object.fromEntries(Object.entries(o).map(([c,s])=>[c,{value:s,_meta:{timestamp:a}}]))}))}},ye=class extends H{db;schema;constructor(e){super(),this.db=new kysely.Kysely({dialect:new kysely.PostgresDialect({pool:e})});}async updateSchema(e){this.schema=e;let t=await this.db.introspection.getTables();for(let[r,a]of Object.entries(e)){let i=t.find(s=>s.name===r);i||await this.db.schema.createTable(r).ifNotExists().execute();let o=`${r}_meta`,c=t.find(s=>s.name===o);c||await this.db.schema.createTable(o).ifNotExists().execute();for(let[s,T]of Object.entries(a.fields)){let m=i==null?void 0:i.columns.find(u=>u.name===s),d=T.getStorageFieldType();m?m.dataType!==d.type&&console.error("Column type mismatch:",s,"expected to have type:",d.type,"but has type:",m.dataType):(await this.db.schema.alterTable(r).addColumn(s,d.type,u=>{let l=u;return d.unique&&(l=l.unique()),d.nullable||(l=l.notNull()),d.references&&(l=l.references(d.references)),d.primary&&(l=l.primaryKey()),d.default!==void 0&&(l=l.defaultTo(d.default)),l}).execute().catch(u=>{throw console.error("Error adding column",s,u),u}),d.index&&await this.db.schema.createIndex(`${r}_${s}_index`).on(r).column(s).execute().catch(u=>{})),(c==null?void 0:c.columns.find(u=>u.name===s))||await this.db.schema.alterTable(o).addColumn(s,"varchar",u=>{let l=u;return d.primary&&(l=l.primaryKey().references(`${r}.${s}`)),l}).execute();}}}async rawFindById(e,t,r){let a=await this.db.selectFrom(e).where("id","=",t).selectAll(e).select(o=>postgres.jsonObjectFrom(o.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=this.applyInclude(e,a,r);let i=await a.executeTakeFirst();if(i)return this.convertToMaterializedLiveType(i)}async findOne(e,t,r){let a=await this.rawFindById(e.name,t,r==null?void 0:r.include);if(a)return v(a)}async rawFind(e,t,r){let a=this.db.selectFrom(e).selectAll(e).select(s=>postgres.jsonObjectFrom(s.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=this.applyWhere(e,a,t),a=this.applyInclude(e,a,r);let i=await a.execute(),o=Object.fromEntries(i.map(s=>{let{id:T,...m}=s;return [T,m]}));return Object.keys(o).length===0?{}:Object.entries(o).reduce((s,[T,m])=>(s[T]=this.convertToMaterializedLiveType(m),s),{})}async find(e,t){let r=await this.rawFind(e.name,t==null?void 0:t.where,t==null?void 0:t.include);return Object.fromEntries(Object.entries(r).map(([a,i])=>[a,v(i)]))}async rawUpsert(e,t,r){return await this.db.transaction().execute(async a=>{var s;let i=!!await a.selectFrom(e).select("id").where("id","=",t).executeTakeFirst(),o={},c={};for(let[T,m]of Object.entries(r.value)){let d=(s=m._meta)==null?void 0:s.timestamp;d&&(o[T]=m.value,c[T]=d);}i?await Promise.all([a.updateTable(e).set(o).where("id","=",t).execute(),a.updateTable(`${e}_meta`).set(c).where("id","=",t).execute()]):await Promise.all([a.insertInto(e).values({...o,id:t}).execute(),a.insertInto(`${e}_meta`).values({...c,id:t}).execute()]);}),r}convertToMaterializedLiveType(e){if(!e._meta)throw new Error("Missing _meta");return {value:Object.entries(e).reduce((t,[r,a])=>{var i,o,c;return r==="_meta"||(r==="id"?t[r]={value:a}:Array.isArray(a)?t[r]={value:a.map(s=>this.convertToMaterializedLiveType(s)),_meta:{timestamp:(i=e==null?void 0:e._meta)==null?void 0:i[r]}}:typeof a=="object"&&a!==null&&!(a instanceof Date)?t[r]={...this.convertToMaterializedLiveType(a),_meta:{timestamp:(o=e==null?void 0:e._meta)==null?void 0:o[r]}}:t[r]={value:a,_meta:{timestamp:(c=e==null?void 0:e._meta)==null?void 0:c[r]}}),t},{})}}applyWhere(e,t,r){if(!r)return t;if(!this.schema)throw new Error("Schema not initialized");let a=this.schema[e];if(!a)throw new Error("Resource not found");for(let[i,o]of Object.entries(r))if(a.fields[i])t=t.where(`${e}.${i}`,"=",o);else if(a.relations[i]){let c=a.relations[i],s=c.entity.name,T=c.type==="one"?"id":c.foreignColumn,m=c.type==="one"?c.relationalColumn:"id";t=t.leftJoin(s,`${s}.${T}`,`${e}.${m}`),t=this.applyWhere(s,t,o);}return t}applyInclude(e,t,r){if(!r)return t;if(!this.schema)throw new Error("Schema not initialized");let a=this.schema[e];if(!a)throw new Error(`Resource not found: ${e}`);for(let[i,o]of Object.entries(r)){if(!a.relations[i])throw new Error(`Relation ${i} not found in resource ${e}`);let c=a.relations[i],s=c.entity.name,T=c.type==="one"?"id":c.foreignColumn,m=c.type==="one"?c.relationalColumn:"id",d=c.type==="one"?postgres.jsonObjectFrom:postgres.jsonArrayFrom;t=t.select(y=>d(y.selectFrom(s).selectAll(s).whereRef(`${s}.${T}`,"=",`${e}.${m}`).select(u=>postgres.jsonObjectFrom(u.selectFrom(`${s}_meta`).selectAll(`${s}_meta`).whereRef(`${s}_meta.id`,"=",`${s}.id`)).as("_meta"))).as(i));}return t}};var G=class n{router;storage;schema;middlewares=new Set;contextProvider;mutationSubscriptions=new Set;constructor(e){var t;this.router=e.router,this.storage=e.storage,this.schema=e.schema,(t=e.middlewares)==null||t.forEach(r=>{this.middlewares.add(r);}),this.storage.updateSchema(this.schema),this.contextProvider=e.contextProvider;}static create(e){return new n(e)}subscribeToMutations(e){return this.mutationSubscriptions.add(e),()=>{this.mutationSubscriptions.delete(e);}}async handleRequest(e){if(!this.router.routes[e.req.resourceName])throw new Error("Invalid resource");let t=await Array.from(this.middlewares.values()).reduceRight((r,a)=>i=>a({req:i,next:r}),async r=>this.router.routes[e.req.resourceName].handleRequest({req:r,db:this.storage,schema:this.schema}))(e.req);return t&&e.req.type==="MUTATE"&&t.acceptedValues&&Object.keys(t.acceptedValues).length>0&&this.mutationSubscriptions.forEach(r=>{r({id:e.req.context.messageId,type:"MUTATE",resource:e.req.resourceName,payload:t.acceptedValues??{},resourceId:e.req.resourceId});}),t}use(e){return this.middlewares.add(e),this}context(e){return this.contextProvider=e,this}},Bt=G.create;
|
|
2
|
-
exports.Route=
|
|
1
|
+
'use strict';var _e=require('qs'),zod=require('zod'),$=require('crypto'),kysely=require('kysely'),postgres=require('kysely/helpers/postgres');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var _e__default=/*#__PURE__*/_interopDefault(_e);var $__default=/*#__PURE__*/_interopDefault($);var fe=Object.create;var Y=Object.defineProperty;var he=Object.getOwnPropertyDescriptor;var ge=Object.getOwnPropertyNames;var Re=Object.getPrototypeOf,xe=Object.prototype.hasOwnProperty;var be=(n,e)=>()=>(e||n((e={exports:{}}).exports,e),e.exports);var ve=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of ge(e))!xe.call(n,a)&&a!==t&&Y(n,a,{get:()=>e[a],enumerable:!(r=he(e,a))||r.enumerable});return n};var J=(n,e,t)=>(t=n!=null?fe(Re(n)):{},ve(Y(t,"default",{value:n,enumerable:true}),n));var _=be(O=>{Object.defineProperty(O,"__esModule",{value:true});O.parse=Ee;O.serialize=Oe;var we=/^[\u0021-\u003A\u003C\u003E-\u007E]+$/,Me=/^[\u0021-\u003A\u003C-\u007E]*$/,Se=/^([.]?[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)([.][a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/i,Ie=/^[\u0020-\u003A\u003D-\u007E]*$/,Le=Object.prototype.toString,Ae=(()=>{let n=function(){};return n.prototype=Object.create(null),n})();function Ee(n,e){let t=new Ae,r=n.length;if(r<2)return t;let a=(e==null?void 0:e.decode)||je,o=0;do{let s=n.indexOf("=",o);if(s===-1)break;let c=n.indexOf(";",o),i=c===-1?r:c;if(s>i){o=n.lastIndexOf(";",s-1)+1;continue}let T=X(n,o,s),m=ee(n,s,T),u=n.slice(T,m);if(t[u]===void 0){let p=X(n,s+1,i),y=ee(n,i,p),l=a(n.slice(p,y));t[u]=l;}o=i+1;}while(o<r);return t}function X(n,e,t){do{let r=n.charCodeAt(e);if(r!==32&&r!==9)return e}while(++e<t);return t}function ee(n,e,t){for(;e>t;){let r=n.charCodeAt(--e);if(r!==32&&r!==9)return e+1}return t}function Oe(n,e,t){let r=(t==null?void 0:t.encode)||encodeURIComponent;if(!we.test(n))throw new TypeError(`argument name is invalid: ${n}`);let a=r(e);if(!Me.test(a))throw new TypeError(`argument val is invalid: ${e}`);let o=n+"="+a;if(!t)return o;if(t.maxAge!==void 0){if(!Number.isInteger(t.maxAge))throw new TypeError(`option maxAge is invalid: ${t.maxAge}`);o+="; Max-Age="+t.maxAge;}if(t.domain){if(!Se.test(t.domain))throw new TypeError(`option domain is invalid: ${t.domain}`);o+="; Domain="+t.domain;}if(t.path){if(!Ie.test(t.path))throw new TypeError(`option path is invalid: ${t.path}`);o+="; Path="+t.path;}if(t.expires){if(!$e(t.expires)||!Number.isFinite(t.expires.valueOf()))throw new TypeError(`option expires is invalid: ${t.expires}`);o+="; Expires="+t.expires.toUTCString();}if(t.httpOnly&&(o+="; HttpOnly"),t.secure&&(o+="; Secure"),t.partitioned&&(o+="; Partitioned"),t.priority)switch(typeof t.priority=="string"?t.priority.toLowerCase():void 0){case "low":o+="; Priority=Low";break;case "medium":o+="; Priority=Medium";break;case "high":o+="; Priority=High";break;default:throw new TypeError(`option priority is invalid: ${t.priority}`)}if(t.sameSite)switch(typeof t.sameSite=="string"?t.sameSite.toLowerCase():t.sameSite){case true:case "strict":o+="; SameSite=Strict";break;case "lax":o+="; SameSite=Lax";break;case "none":o+="; SameSite=None";break;default:throw new TypeError(`option sameSite is invalid: ${t.sameSite}`)}return o}function je(n){if(n.indexOf("%")===-1)return n;try{return decodeURIComponent(n)}catch{return n}}function $e(n){return Le.call(n)==="[object Date]"}});var re=J(_());var j=zod.z.object({resource:zod.z.string(),where:zod.z.record(zod.z.string(),zod.z.any()).optional(),include:zod.z.record(zod.z.string(),zod.z.any()).optional(),lastSyncedAt:zod.z.string().optional(),limit:zod.z.number().optional(),sort:zod.z.array(zod.z.object({key:zod.z.string(),direction:zod.z.enum(["asc","desc"])})).optional()}),P=zod.z.record(zod.z.string(),zod.z.object({value:zod.z.string().or(zod.z.number()).or(zod.z.boolean()).or(zod.z.date()).nullable(),_meta:zod.z.object({timestamp:zod.z.string().optional().nullable()}).optional()})).superRefine((n,e)=>{n.id&&e.addIssue({code:zod.z.ZodIssueCode.custom,message:"Payload cannot have an id"});}),te=zod.z.object({id:zod.z.string().optional(),type:zod.z.literal("MUTATE"),resource:zod.z.string()}),S=te.extend({procedure:zod.z.string(),payload:zod.z.any().optional()}),I=te.extend({resourceId:zod.z.string(),payload:P});zod.z.union([S,I]);var ne=j.omit({resource:true}),V=S.omit({id:true,type:true,resource:true,procedure:true}),F=I.omit({id:true,type:true,resource:true});zod.z.union([F,V]);var ae=n=>async e=>{var t;try{let r=typeof e.headers.getSetCookie=="function"?Object.fromEntries(e.headers):e.headers,a={headers:r,cookies:r.cookie?re.default.parse(r.cookie):{}},o=new URL(e.url),s=o.pathname.split("/"),c=o.searchParams,i=_e__default.default.parse(c.toString()),T=await((t=n.contextProvider)==null?void 0:t.call(n,{transport:"HTTP",headers:a.headers,cookies:a.cookies,query:i}))??{};if(e.method==="GET"){let m=s[s.length-1],{success:u,data:p,error:y}=ne.safeParse(i);if(!u)return Response.json({message:"Invalid query",code:"INVALID_QUERY",details:y},{status:400});let l=await n.handleRequest({req:{...a,type:"QUERY",resourceName:m,context:T,where:p.where,include:p.include,query:i}});return !l||!l.data?Response.json({message:"Invalid resource",code:"INVALID_RESOURCE"},{status:400}):Response.json(l.data)}if(e.method==="POST")try{let m=s[s.length-1],u=s[s.length-2],p=e.body?await e.json():{},y;if(m==="set"){let{success:w,data:M,error:C}=F.safeParse(p);if(!w)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:C},{status:400});y=M;}else {let{success:w,data:M,error:C}=V.safeParse(p);if(!w)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:C},{status:400});y=M;}let l=await n.handleRequest({req:{...a,type:"MUTATE",resourceName:u,input:y.payload,context:T,resourceId:y.resourceId,procedure:m!=="set"?m:void 0,query:{}}});return Response.json(l)}catch(m){return console.error("Error parsing mutation from the client:",m),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}return Response.json({message:"Not found",code:"NOT_FOUND"},{status:404})}catch(r){return console.error("Unexpected error:",r),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}};var de=J(_());var b=zod.z.string(),Pe=zod.z.object({id:b,type:zod.z.literal("SUBSCRIBE"),resource:zod.z.string()}),Ve=j.extend({id:b,type:zod.z.literal("QUERY")}),ie=I.extend({id:b}),Fe=S.extend({id:b}),Ne=zod.z.union([Fe,ie]),oe=zod.z.union([Pe,Ve,Ne]),De=zod.z.object({id:b,type:zod.z.literal("REJECT"),resource:zod.z.string(),message:zod.z.string().optional()}),Ke=zod.z.object({id:b,type:zod.z.literal("REPLY"),data:zod.z.any()});zod.z.union([De,Ke,ie]);zod.z.object({resource:zod.z.string(),data:zod.z.record(zod.z.string(),P)});var ce="0123456789ABCDEFGHJKMNPQRSTVWXYZ",L=32;var Ue=16,ue=10,se=0xffffffffffff;var g;(function(n){n.Base32IncorrectEncoding="B32_ENC_INVALID",n.DecodeTimeInvalidCharacter="DEC_TIME_CHAR",n.DecodeTimeValueMalformed="DEC_TIME_MALFORMED",n.EncodeTimeNegative="ENC_TIME_NEG",n.EncodeTimeSizeExceeded="ENC_TIME_SIZE_EXCEED",n.EncodeTimeValueMalformed="ENC_TIME_MALFORMED",n.PRNGDetectFailure="PRNG_DETECT",n.ULIDInvalid="ULID_INVALID",n.Unexpected="UNEXPECTED",n.UUIDInvalid="UUID_INVALID";})(g||(g={}));var R=class extends Error{constructor(e,t){super(`${t} (${e})`),this.name="ULIDError",this.code=e;}};function ze(n){let e=Math.floor(n()*L);return e===L&&(e=L-1),ce.charAt(e)}function ke(n){var r;let e=qe(),t=e&&(e.crypto||e.msCrypto)||(typeof $__default.default<"u"?$__default.default:null);if(typeof(t==null?void 0:t.getRandomValues)=="function")return ()=>{let a=new Uint8Array(1);return t.getRandomValues(a),a[0]/255};if(typeof(t==null?void 0:t.randomBytes)=="function")return ()=>t.randomBytes(1).readUInt8()/255;if((r=$__default.default)!=null&&r.randomBytes)return ()=>$__default.default.randomBytes(1).readUInt8()/255;throw new R(g.PRNGDetectFailure,"Failed to find a reliable PRNG")}function qe(){return He()?self:typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:null}function Ze(n,e){let t="";for(;n>0;n--)t=ze(e)+t;return t}function We(n,e=ue){if(isNaN(n))throw new R(g.EncodeTimeValueMalformed,`Time must be a number: ${n}`);if(n>se)throw new R(g.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${se}: ${n}`);if(n<0)throw new R(g.EncodeTimeNegative,`Time must be positive: ${n}`);if(Number.isInteger(n)===false)throw new R(g.EncodeTimeValueMalformed,`Time must be an integer: ${n}`);let t,r="";for(let a=e;a>0;a--)t=n%L,r=ce.charAt(t)+r,n=(n-t)/L;return r}function He(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function le(n,e){let t=ke(),r=Date.now();return We(r,ue)+Ze(Ue,t)}var N=()=>le().toLowerCase();var pe=n=>{let e={},t={};return n.subscribeToMutations(r=>{let a=r;!a.resourceId||!a.payload||(console.log("Mutation propagated:",a),Object.entries(t[a.resource]??{}).forEach(([o,s])=>{var c;(c=e[o])==null||c.send(JSON.stringify({...a,id:a.id??N()}));}));}),(r,a)=>{var m;let o=u=>{r.send(JSON.stringify(u));},s=N(),c={headers:a.headers,cookies:typeof a.headers.cookie=="string"?de.default.parse(a.headers.cookie):{}},i=_e.parse(a.url.split("?")[1]),T=(m=n.contextProvider)==null?void 0:m.call(n,{transport:"WEBSOCKET",headers:c.headers,cookies:c.cookies,query:i});e[s]=r,console.log("Client connected:",s),r.on("message",async u=>{try{console.log("Message received from the client:",u);let p=oe.parse(JSON.parse(u.toString()));if(p.type==="SUBSCRIBE"){let{resource:y}=p;t[y]||(t[y]={}),t[y][s]={};}else if(p.type==="QUERY"){let{resource:y}=p,l=await n.handleRequest({req:{...c,type:"QUERY",resourceName:y,context:await T??{},query:i}});if(!l||!l.data)throw new Error("Invalid resource");o({id:p.id,type:"REPLY",data:{resource:y,data:Object.fromEntries(Object.entries(l.data??{}).map(([w,M])=>[w,M.value]))}});}else if(p.type==="MUTATE"){let{resource:y}=p;console.log("Received mutation from client:",p);try{let l=await n.handleRequest({req:{...c,type:"MUTATE",resourceName:y,input:p.payload,context:{messageId:p.id,...await T??{}},resourceId:p.resourceId,procedure:p.procedure,query:i}});p.procedure&&o({id:p.id,type:"REPLY",data:l});}catch(l){o({id:p.id,type:"REJECT",resource:y,message:l.message}),console.error("Error parsing mutation from the client:",l);}}}catch(p){console.error("Error handling message from the client:",p);}}),r.on("close",()=>{console.log("Connection closed",s),delete e[s];for(let u of Object.values(t))delete u[s];});}};function ye(n){let e=`${n.protocol}://${n.hostname}${n.url}`,t=new Headers;return Object.entries(n.headers).forEach(([r,a])=>{a&&t.set(r,Array.isArray(a)?a.join(","):a);}),new Request(e,{method:n.method,headers:t,body:n.body&&n.method!=="GET"?JSON.stringify(n.body):void 0})}var Et=(n,e,t)=>{n.ws(`${(t==null?void 0:t.basePath)??""}/ws`,pe(e)),n.use(`${(t==null?void 0:t.basePath)??""}/`,(r,a)=>{ae(e)(ye(r)).then(s=>s.json().then(c=>a.status(s.status).send(c)));});};var D=class n{routes;constructor(e){this.routes=e.routes;}static create(e){return new n(e)}},$t=n=>D.create({...n}),Qe=n=>({handler:e=>({inputValidator:n??zod.z.undefined(),handler:e})}),K=class n{_resourceSchema;resourceName;middlewares;customMutations;constructor(e,t){this.resourceName=e,this.middlewares=new Set,this.customMutations=t??{};}handleFind=async({req:e,db:t})=>({data:await t.rawFind(e.resourceName,e.where,e.include),acceptedValues:null});handleSet=async({req:e,db:t,schema:r})=>{if(!e.input)throw new Error("Payload is required");if(!e.resourceId)throw new Error("ResourceId is required");let a=await t.rawFindById(e.resourceName,e.resourceId),[o,s]=r[this.resourceName].mergeMutation("set",e.input,a);if(!s)throw new Error("Mutation rejected");return {data:await t.rawUpsert(e.resourceName,e.resourceId,o),acceptedValues:s}};async handleRequest(e){let t=r=>(()=>{if(r.type==="QUERY")return this.handleFind({req:r,db:e.db,schema:e.schema});if(r.type==="MUTATE")if(r.procedure){if(this.customMutations[r.procedure]){let a=this.customMutations[r.procedure].inputValidator.parse(r.input);return r.input=a,this.customMutations[r.procedure].handler({req:r,db:e.db,schema:e.schema})}}else return this.handleSet({req:r,db:e.db,schema:e.schema});throw new Error("Invalid request")})();return await Array.from(this.middlewares.values()).reduceRight((r,a)=>o=>a({req:o,next:r}),async r=>t(r))(e.req)}use(...e){for(let t of e)this.middlewares.add(t);return this}withMutations(e){return new n(this.resourceName,e({mutation:Qe}))}},U=class n{middlewares;constructor(e=[]){this.middlewares=e;}createBasicRoute(e){return new K(e.name).use(...this.middlewares)}use(...e){return new n([...this.middlewares,...e])}static create(){return new n}},Ct=U.create;var h=n=>{if(n)return Array.isArray(n.value)?n.value.map(e=>h(e)):typeof n.value!="object"||n.value===null||n.value instanceof Date?n.value:Object.fromEntries(Object.entries(n.value).map(([e,t])=>[e,h(t)]))};var E=class{async insert(e,t){let r=new Date().toISOString();return h(await this.rawUpsert(e.name,t.id,{value:Object.fromEntries(Object.entries(t).map(([a,o])=>[a,{value:o,_meta:{timestamp:r}}]))}))}async update(e,t,r){let a=new Date().toISOString(),{id:o,...s}=r;return h(await this.rawUpsert(e.name,t,{value:Object.fromEntries(Object.entries(s).map(([c,i])=>[c,{value:i,_meta:{timestamp:a}}]))}))}};function H(n,e,t,r){var o,s;if(!r)return t;if(!n)throw new Error("Schema not initialized");let a=n[e];if(!a)throw new Error("Resource not found");for(let[c,i]of Object.entries(r))if(a.fields[c])(i==null?void 0:i.$eq)!==void 0?t=t.where(`${e}.${c}`,i.$eq===null?"is":"=",i.$eq):(i==null?void 0:i.$in)!==void 0?t=t.where(`${e}.${c}`,"in",i.$in):(i==null?void 0:i.$not)!==void 0?((o=i==null?void 0:i.$not)==null?void 0:o.$in)!==void 0?t=t.where(`${e}.${c}`,"not in",i.$not.$in):((s=i==null?void 0:i.$not)==null?void 0:s.$eq)!==void 0?t=t.where(`${e}.${c}`,i.$not.$eq===null?"is not":"!=",i.$not.$eq):t=t.where(`${e}.${c}`,i.$not===null?"is not":"!=",i.$not):(i==null?void 0:i.$gt)!==void 0?t=t.where(`${e}.${c}`,">",i.$gt):(i==null?void 0:i.$gte)!==void 0?t=t.where(`${e}.${c}`,">=",i.$gte):(i==null?void 0:i.$lt)!==void 0?t=t.where(`${e}.${c}`,"<",i.$lt):(i==null?void 0:i.$lte)!==void 0?t=t.where(`${e}.${c}`,"<=",i.$lte):t=t.where(`${e}.${c}`,i===null?"is":"=",i);else if(a.relations[c]){let T=a.relations[c],m=T.entity.name,u=T.type==="one"?"id":T.foreignColumn,p=T.type==="one"?T.relationalColumn:"id";t=t.leftJoin(m,`${m}.${u}`,`${e}.${p}`),t=H(n,m,t,i);}return t}function B(n,e,t,r){if(!r)return t;if(!n)throw new Error("Schema not initialized");let a=n[e];if(!a)throw new Error(`Resource not found: ${e}`);for(let o of Object.keys(r)){if(!a.relations[o])throw new Error(`Relation ${o} not found in resource ${e}`);let s=a.relations[o],c=s.entity.name,i=s.type==="one"?"id":s.foreignColumn,T=s.type==="one"?s.relationalColumn:"id",m=s.type==="one"?postgres.jsonObjectFrom:postgres.jsonArrayFrom;t=t.select(u=>m(u.selectFrom(c).selectAll(c).whereRef(`${c}.${i}`,"=",`${e}.${T}`).select(p=>postgres.jsonObjectFrom(p.selectFrom(`${c}_meta`).selectAll(`${c}_meta`).whereRef(`${c}_meta.id`,"=",`${c}.id`)).as("_meta"))).as(o));}return t}var G=class extends E{db;schema;constructor(e){super(),this.db=new kysely.Kysely({dialect:new kysely.PostgresDialect({pool:e})});}async updateSchema(e){this.schema=e;let t=await this.db.introspection.getTables();for(let[r,a]of Object.entries(e)){let o=t.find(i=>i.name===r);o||await this.db.schema.createTable(r).ifNotExists().execute();let s=`${r}_meta`,c=t.find(i=>i.name===s);c||await this.db.schema.createTable(s).ifNotExists().execute();for(let[i,T]of Object.entries(a.fields)){let m=o==null?void 0:o.columns.find(y=>y.name===i),u=T.getStorageFieldType();m?m.dataType!==u.type&&console.error("Column type mismatch:",i,"expected to have type:",u.type,"but has type:",m.dataType):(await this.db.schema.alterTable(r).addColumn(i,u.type,y=>{let l=y;return u.unique&&(l=l.unique()),u.nullable||(l=l.notNull()),u.references&&(l=l.references(u.references)),u.primary&&(l=l.primaryKey()),u.default!==void 0&&(l=l.defaultTo(u.default)),l}).execute().catch(y=>{throw console.error("Error adding column",i,y),y}),u.index&&await this.db.schema.createIndex(`${r}_${i}_index`).on(r).column(i).execute().catch(y=>{})),(c==null?void 0:c.columns.find(y=>y.name===i))||await this.db.schema.alterTable(s).addColumn(i,"varchar",y=>{let l=y;return u.primary&&(l=l.primaryKey().references(`${r}.${i}`)),l}).execute();}}}async rawFindById(e,t,r){if(!this.schema)throw new Error("Schema not initialized");let a=await this.db.selectFrom(e).where("id","=",t).selectAll(e).select(s=>postgres.jsonObjectFrom(s.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=B(this.schema,e,a,r);let o=await a.executeTakeFirst();if(o)return this.convertToMaterializedLiveType(o)}async findOne(e,t,r){let a=await this.rawFindById(e.name,t,r==null?void 0:r.include);if(a)return h(a)}async rawFind(e,t,r){if(!this.schema)throw new Error("Schema not initialized");let a=this.db.selectFrom(e).selectAll(e).select(i=>postgres.jsonObjectFrom(i.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=H(this.schema,e,a,t),a=B(this.schema,e,a,r);let o=await a.execute(),s=Object.fromEntries(o.map(i=>{let{id:T,...m}=i;return [T,m]}));return Object.keys(s).length===0?{}:Object.entries(s).reduce((i,[T,m])=>(i[T]=this.convertToMaterializedLiveType(m),i),{})}async find(e,t){let r=await this.rawFind(e.name,t==null?void 0:t.where,t==null?void 0:t.include);return Object.fromEntries(Object.entries(r).map(([a,o])=>[a,h(o)]))}async rawUpsert(e,t,r){return await this.db.transaction().execute(async a=>{var i;let o=!!await a.selectFrom(e).select("id").where("id","=",t).executeTakeFirst(),s={},c={};for(let[T,m]of Object.entries(r.value)){let u=(i=m._meta)==null?void 0:i.timestamp;u&&(s[T]=m.value,c[T]=u);}o?await Promise.all([a.updateTable(e).set(s).where("id","=",t).execute(),a.updateTable(`${e}_meta`).set(c).where("id","=",t).execute()]):await Promise.all([a.insertInto(e).values({...s,id:t}).execute(),a.insertInto(`${e}_meta`).values({...c,id:t}).execute()]);}),r}convertToMaterializedLiveType(e){if(!e._meta)throw new Error("Missing _meta");return {value:Object.entries(e).reduce((t,[r,a])=>{var o,s,c;return r==="_meta"||(r==="id"?t[r]={value:a}:Array.isArray(a)?t[r]={value:a.map(i=>this.convertToMaterializedLiveType(i)),_meta:{timestamp:(o=e==null?void 0:e._meta)==null?void 0:o[r]}}:typeof a=="object"&&a!==null&&!(a instanceof Date)?t[r]={...this.convertToMaterializedLiveType(a),_meta:{timestamp:(s=e==null?void 0:e._meta)==null?void 0:s[r]}}:t[r]={value:a,_meta:{timestamp:(c=e==null?void 0:e._meta)==null?void 0:c[r]}}),t},{})}}};var Q=class n{router;storage;schema;middlewares=new Set;contextProvider;mutationSubscriptions=new Set;constructor(e){var t;this.router=e.router,this.storage=e.storage,this.schema=e.schema,(t=e.middlewares)==null||t.forEach(r=>{this.middlewares.add(r);}),this.storage.updateSchema(this.schema),this.contextProvider=e.contextProvider;}static create(e){return new n(e)}subscribeToMutations(e){return this.mutationSubscriptions.add(e),()=>{this.mutationSubscriptions.delete(e);}}async handleRequest(e){if(!this.router.routes[e.req.resourceName])throw new Error("Invalid resource");let t=await Array.from(this.middlewares.values()).reduceRight((r,a)=>o=>a({req:o,next:r}),async r=>this.router.routes[e.req.resourceName].handleRequest({req:r,db:this.storage,schema:this.schema}))(e.req);return t&&e.req.type==="MUTATE"&&t.acceptedValues&&Object.keys(t.acceptedValues).length>0&&this.mutationSubscriptions.forEach(r=>{r({id:e.req.context.messageId,type:"MUTATE",resource:e.req.resourceName,payload:t.acceptedValues??{},resourceId:e.req.resourceId});}),t}use(e){return this.middlewares.add(e),this}context(e){return this.contextProvider=e,this}},un=Q.create;
|
|
2
|
+
exports.Route=K;exports.RouteFactory=U;exports.Router=D;exports.SQLStorage=G;exports.Server=Q;exports.Storage=E;exports.expressAdapter=Et;exports.routeFactory=Ct;exports.router=$t;exports.server=un;
|
package/dist/server.d.cts
CHANGED
|
@@ -3,84 +3,24 @@ import { LiveObjectAny, Schema, MaterializedLiveType, IncludeClause, InferLiveOb
|
|
|
3
3
|
import { PostgresPool } from 'kysely';
|
|
4
4
|
import { Application } from 'express-ws';
|
|
5
5
|
|
|
6
|
-
declare const mutationSchema: z.ZodUnion<[z.ZodObject<
|
|
6
|
+
declare const mutationSchema: z.ZodUnion<readonly [z.ZodObject<{
|
|
7
7
|
id: z.ZodOptional<z.ZodString>;
|
|
8
8
|
type: z.ZodLiteral<"MUTATE">;
|
|
9
9
|
resource: z.ZodString;
|
|
10
|
-
}, {
|
|
11
10
|
procedure: z.ZodString;
|
|
12
11
|
payload: z.ZodOptional<z.ZodAny>;
|
|
13
|
-
}
|
|
14
|
-
procedure: string;
|
|
15
|
-
type: "MUTATE";
|
|
16
|
-
resource: string;
|
|
17
|
-
id?: string | undefined;
|
|
18
|
-
payload?: any;
|
|
19
|
-
}, {
|
|
20
|
-
procedure: string;
|
|
21
|
-
type: "MUTATE";
|
|
22
|
-
resource: string;
|
|
23
|
-
id?: string | undefined;
|
|
24
|
-
payload?: any;
|
|
25
|
-
}>, z.ZodObject<z.objectUtil.extendShape<{
|
|
12
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
26
13
|
id: z.ZodOptional<z.ZodString>;
|
|
27
14
|
type: z.ZodLiteral<"MUTATE">;
|
|
28
15
|
resource: z.ZodString;
|
|
29
|
-
}, {
|
|
30
16
|
resourceId: z.ZodString;
|
|
31
|
-
payload: z.
|
|
17
|
+
payload: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
32
18
|
value: z.ZodNullable<z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodDate]>>;
|
|
33
19
|
_meta: z.ZodOptional<z.ZodObject<{
|
|
34
20
|
timestamp: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
35
|
-
},
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
timestamp?: string | null | undefined;
|
|
39
|
-
}>>;
|
|
40
|
-
}, "strip", z.ZodTypeAny, {
|
|
41
|
-
value: string | number | boolean | Date | null;
|
|
42
|
-
_meta?: {
|
|
43
|
-
timestamp?: string | null | undefined;
|
|
44
|
-
} | undefined;
|
|
45
|
-
}, {
|
|
46
|
-
value: string | number | boolean | Date | null;
|
|
47
|
-
_meta?: {
|
|
48
|
-
timestamp?: string | null | undefined;
|
|
49
|
-
} | undefined;
|
|
50
|
-
}>>, Record<string, {
|
|
51
|
-
value: string | number | boolean | Date | null;
|
|
52
|
-
_meta?: {
|
|
53
|
-
timestamp?: string | null | undefined;
|
|
54
|
-
} | undefined;
|
|
55
|
-
}>, Record<string, {
|
|
56
|
-
value: string | number | boolean | Date | null;
|
|
57
|
-
_meta?: {
|
|
58
|
-
timestamp?: string | null | undefined;
|
|
59
|
-
} | undefined;
|
|
60
|
-
}>>;
|
|
61
|
-
}>, "strip", z.ZodTypeAny, {
|
|
62
|
-
type: "MUTATE";
|
|
63
|
-
resourceId: string;
|
|
64
|
-
resource: string;
|
|
65
|
-
payload: Record<string, {
|
|
66
|
-
value: string | number | boolean | Date | null;
|
|
67
|
-
_meta?: {
|
|
68
|
-
timestamp?: string | null | undefined;
|
|
69
|
-
} | undefined;
|
|
70
|
-
}>;
|
|
71
|
-
id?: string | undefined;
|
|
72
|
-
}, {
|
|
73
|
-
type: "MUTATE";
|
|
74
|
-
resourceId: string;
|
|
75
|
-
resource: string;
|
|
76
|
-
payload: Record<string, {
|
|
77
|
-
value: string | number | boolean | Date | null;
|
|
78
|
-
_meta?: {
|
|
79
|
-
timestamp?: string | null | undefined;
|
|
80
|
-
} | undefined;
|
|
81
|
-
}>;
|
|
82
|
-
id?: string | undefined;
|
|
83
|
-
}>]>;
|
|
21
|
+
}, z.core.$strip>>;
|
|
22
|
+
}, z.core.$strip>>;
|
|
23
|
+
}, z.core.$strip>]>;
|
|
84
24
|
type RawMutationRequest = z.infer<typeof mutationSchema>;
|
|
85
25
|
|
|
86
26
|
type RouteRecord = Record<string, AnyRoute>;
|
|
@@ -148,6 +88,8 @@ type Simplify<T> = T extends Record<string, unknown> ? {
|
|
|
148
88
|
[K in keyof T]: Simplify<T[K]>;
|
|
149
89
|
} : T;
|
|
150
90
|
|
|
91
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
92
|
+
|
|
151
93
|
declare abstract class Storage {
|
|
152
94
|
abstract findOne<T extends LiveObjectAny>(resource: T, id: string, options?: {
|
|
153
95
|
include?: IncludeClause<T>;
|
|
@@ -159,6 +101,9 @@ declare abstract class Storage {
|
|
|
159
101
|
insert<T extends LiveObjectAny>(resource: T, value: Simplify<LiveObjectMutationInput<T>>): Promise<InferLiveObject<T>>;
|
|
160
102
|
update<T extends LiveObjectAny>(resource: T, resourceId: string, value: LiveObjectMutationInput<T>): Promise<InferLiveObject<T>>;
|
|
161
103
|
}
|
|
104
|
+
|
|
105
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
106
|
+
|
|
162
107
|
declare class SQLStorage extends Storage {
|
|
163
108
|
private db;
|
|
164
109
|
private schema?;
|
|
@@ -171,8 +116,6 @@ declare class SQLStorage extends Storage {
|
|
|
171
116
|
include?: IncludeClause<T>;
|
|
172
117
|
}): Promise<Record<string, InferLiveObject<T>>>;
|
|
173
118
|
private convertToMaterializedLiveType;
|
|
174
|
-
private applyWhere;
|
|
175
|
-
private applyInclude;
|
|
176
119
|
}
|
|
177
120
|
|
|
178
121
|
declare const expressAdapter: (app: Application, server: Server<AnyRouter>, options?: {
|
package/dist/server.d.ts
CHANGED
|
@@ -3,84 +3,24 @@ import { LiveObjectAny, Schema, MaterializedLiveType, IncludeClause, InferLiveOb
|
|
|
3
3
|
import { PostgresPool } from 'kysely';
|
|
4
4
|
import { Application } from 'express-ws';
|
|
5
5
|
|
|
6
|
-
declare const mutationSchema: z.ZodUnion<[z.ZodObject<
|
|
6
|
+
declare const mutationSchema: z.ZodUnion<readonly [z.ZodObject<{
|
|
7
7
|
id: z.ZodOptional<z.ZodString>;
|
|
8
8
|
type: z.ZodLiteral<"MUTATE">;
|
|
9
9
|
resource: z.ZodString;
|
|
10
|
-
}, {
|
|
11
10
|
procedure: z.ZodString;
|
|
12
11
|
payload: z.ZodOptional<z.ZodAny>;
|
|
13
|
-
}
|
|
14
|
-
procedure: string;
|
|
15
|
-
type: "MUTATE";
|
|
16
|
-
resource: string;
|
|
17
|
-
id?: string | undefined;
|
|
18
|
-
payload?: any;
|
|
19
|
-
}, {
|
|
20
|
-
procedure: string;
|
|
21
|
-
type: "MUTATE";
|
|
22
|
-
resource: string;
|
|
23
|
-
id?: string | undefined;
|
|
24
|
-
payload?: any;
|
|
25
|
-
}>, z.ZodObject<z.objectUtil.extendShape<{
|
|
12
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
26
13
|
id: z.ZodOptional<z.ZodString>;
|
|
27
14
|
type: z.ZodLiteral<"MUTATE">;
|
|
28
15
|
resource: z.ZodString;
|
|
29
|
-
}, {
|
|
30
16
|
resourceId: z.ZodString;
|
|
31
|
-
payload: z.
|
|
17
|
+
payload: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
32
18
|
value: z.ZodNullable<z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodDate]>>;
|
|
33
19
|
_meta: z.ZodOptional<z.ZodObject<{
|
|
34
20
|
timestamp: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
35
|
-
},
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
timestamp?: string | null | undefined;
|
|
39
|
-
}>>;
|
|
40
|
-
}, "strip", z.ZodTypeAny, {
|
|
41
|
-
value: string | number | boolean | Date | null;
|
|
42
|
-
_meta?: {
|
|
43
|
-
timestamp?: string | null | undefined;
|
|
44
|
-
} | undefined;
|
|
45
|
-
}, {
|
|
46
|
-
value: string | number | boolean | Date | null;
|
|
47
|
-
_meta?: {
|
|
48
|
-
timestamp?: string | null | undefined;
|
|
49
|
-
} | undefined;
|
|
50
|
-
}>>, Record<string, {
|
|
51
|
-
value: string | number | boolean | Date | null;
|
|
52
|
-
_meta?: {
|
|
53
|
-
timestamp?: string | null | undefined;
|
|
54
|
-
} | undefined;
|
|
55
|
-
}>, Record<string, {
|
|
56
|
-
value: string | number | boolean | Date | null;
|
|
57
|
-
_meta?: {
|
|
58
|
-
timestamp?: string | null | undefined;
|
|
59
|
-
} | undefined;
|
|
60
|
-
}>>;
|
|
61
|
-
}>, "strip", z.ZodTypeAny, {
|
|
62
|
-
type: "MUTATE";
|
|
63
|
-
resourceId: string;
|
|
64
|
-
resource: string;
|
|
65
|
-
payload: Record<string, {
|
|
66
|
-
value: string | number | boolean | Date | null;
|
|
67
|
-
_meta?: {
|
|
68
|
-
timestamp?: string | null | undefined;
|
|
69
|
-
} | undefined;
|
|
70
|
-
}>;
|
|
71
|
-
id?: string | undefined;
|
|
72
|
-
}, {
|
|
73
|
-
type: "MUTATE";
|
|
74
|
-
resourceId: string;
|
|
75
|
-
resource: string;
|
|
76
|
-
payload: Record<string, {
|
|
77
|
-
value: string | number | boolean | Date | null;
|
|
78
|
-
_meta?: {
|
|
79
|
-
timestamp?: string | null | undefined;
|
|
80
|
-
} | undefined;
|
|
81
|
-
}>;
|
|
82
|
-
id?: string | undefined;
|
|
83
|
-
}>]>;
|
|
21
|
+
}, z.core.$strip>>;
|
|
22
|
+
}, z.core.$strip>>;
|
|
23
|
+
}, z.core.$strip>]>;
|
|
84
24
|
type RawMutationRequest = z.infer<typeof mutationSchema>;
|
|
85
25
|
|
|
86
26
|
type RouteRecord = Record<string, AnyRoute>;
|
|
@@ -148,6 +88,8 @@ type Simplify<T> = T extends Record<string, unknown> ? {
|
|
|
148
88
|
[K in keyof T]: Simplify<T[K]>;
|
|
149
89
|
} : T;
|
|
150
90
|
|
|
91
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
92
|
+
|
|
151
93
|
declare abstract class Storage {
|
|
152
94
|
abstract findOne<T extends LiveObjectAny>(resource: T, id: string, options?: {
|
|
153
95
|
include?: IncludeClause<T>;
|
|
@@ -159,6 +101,9 @@ declare abstract class Storage {
|
|
|
159
101
|
insert<T extends LiveObjectAny>(resource: T, value: Simplify<LiveObjectMutationInput<T>>): Promise<InferLiveObject<T>>;
|
|
160
102
|
update<T extends LiveObjectAny>(resource: T, resourceId: string, value: LiveObjectMutationInput<T>): Promise<InferLiveObject<T>>;
|
|
161
103
|
}
|
|
104
|
+
|
|
105
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
106
|
+
|
|
162
107
|
declare class SQLStorage extends Storage {
|
|
163
108
|
private db;
|
|
164
109
|
private schema?;
|
|
@@ -171,8 +116,6 @@ declare class SQLStorage extends Storage {
|
|
|
171
116
|
include?: IncludeClause<T>;
|
|
172
117
|
}): Promise<Record<string, InferLiveObject<T>>>;
|
|
173
118
|
private convertToMaterializedLiveType;
|
|
174
|
-
private applyWhere;
|
|
175
|
-
private applyInclude;
|
|
176
119
|
}
|
|
177
120
|
|
|
178
121
|
declare const expressAdapter: (app: Application, server: Server<AnyRouter>, options?: {
|
package/dist/server.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {a,b,r}from'./chunk-LLHCJUB6.js';import he,{parse}from'qs';import {z as z$1}from'zod';import A from'node:crypto';import {Kysely,PostgresDialect}from'kysely';import {jsonObjectFrom,jsonArrayFrom}from'kysely/helpers/postgres';var L=a(v=>{Object.defineProperty(v,"__esModule",{value:true});v.parse=le;v.serialize=me;var ie=/^[\u0021-\u003A\u003C\u003E-\u007E]+$/,oe=/^[\u0021-\u003A\u003C-\u007E]*$/,se=/^([.]?[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)([.][a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/i,ce=/^[\u0020-\u003A\u003D-\u007E]*$/,ue=Object.prototype.toString,de=(()=>{let r=function(){};return r.prototype=Object.create(null),r})();function le(r,e){let t=new de,n=r.length;if(n<2)return t;let a=(e==null?void 0:e.decode)||pe,i=0;do{let s=r.indexOf("=",i);if(s===-1)break;let c=r.indexOf(";",i),o=c===-1?n:c;if(s>o){i=r.lastIndexOf(";",s-1)+1;continue}let f=F(r,i,s),y=q(r,s,f),l=r.slice(f,y);if(t[l]===void 0){let m=F(r,s+1,o),u=q(r,o,m),d=a(r.slice(m,u));t[l]=d;}i=o+1;}while(i<n);return t}function F(r,e,t){do{let n=r.charCodeAt(e);if(n!==32&&n!==9)return e}while(++e<t);return t}function q(r,e,t){for(;e>t;){let n=r.charCodeAt(--e);if(n!==32&&n!==9)return e+1}return t}function me(r,e,t){let n=(t==null?void 0:t.encode)||encodeURIComponent;if(!ie.test(r))throw new TypeError(`argument name is invalid: ${r}`);let a=n(e);if(!oe.test(a))throw new TypeError(`argument val is invalid: ${e}`);let i=r+"="+a;if(!t)return i;if(t.maxAge!==void 0){if(!Number.isInteger(t.maxAge))throw new TypeError(`option maxAge is invalid: ${t.maxAge}`);i+="; Max-Age="+t.maxAge;}if(t.domain){if(!se.test(t.domain))throw new TypeError(`option domain is invalid: ${t.domain}`);i+="; Domain="+t.domain;}if(t.path){if(!ce.test(t.path))throw new TypeError(`option path is invalid: ${t.path}`);i+="; Path="+t.path;}if(t.expires){if(!ye(t.expires)||!Number.isFinite(t.expires.valueOf()))throw new TypeError(`option expires is invalid: ${t.expires}`);i+="; Expires="+t.expires.toUTCString();}if(t.httpOnly&&(i+="; HttpOnly"),t.secure&&(i+="; Secure"),t.partitioned&&(i+="; Partitioned"),t.priority)switch(typeof t.priority=="string"?t.priority.toLowerCase():void 0){case "low":i+="; Priority=Low";break;case "medium":i+="; Priority=Medium";break;case "high":i+="; Priority=High";break;default:throw new TypeError(`option priority is invalid: ${t.priority}`)}if(t.sameSite)switch(typeof t.sameSite=="string"?t.sameSite.toLowerCase():t.sameSite){case true:case "strict":i+="; SameSite=Strict";break;case "lax":i+="; SameSite=Lax";break;case "none":i+="; SameSite=None";break;default:throw new TypeError(`option sameSite is invalid: ${t.sameSite}`)}return i}function pe(r){if(r.indexOf("%")===-1)return r;try{return decodeURIComponent(r)}catch{return r}}function ye(r){return ue.call(r)==="[object Date]"}});var G=b(L(),1);var E=z$1.object({resource:z$1.string(),where:z$1.record(z$1.any()).optional(),include:z$1.record(z$1.any()).optional(),lastSyncedAt:z$1.string().optional(),limit:z$1.number().optional(),sort:z$1.array(z$1.object({key:z$1.string(),direction:z$1.enum(["asc","desc"])})).optional()}),P=z$1.record(z$1.object({value:z$1.string().or(z$1.number()).or(z$1.boolean()).or(z$1.date()).nullable(),_meta:z$1.object({timestamp:z$1.string().optional().nullable()}).optional()})).superRefine((r,e)=>{r.id&&e.addIssue({code:z$1.ZodIssueCode.custom,message:"Payload cannot have an id"});}),Z=z$1.object({id:z$1.string().optional(),type:z$1.literal("MUTATE"),resource:z$1.string()}),S=Z.extend({procedure:z$1.string(),payload:z$1.any().optional()}),M=Z.extend({resourceId:z$1.string(),payload:P});z$1.union([S,M]);var H=E.omit({resource:true}),C=S.omit({id:true,type:true,resource:true,procedure:true}),N=M.omit({id:true,type:true,resource:true});z$1.union([N,C]);var Q=r=>async e=>{var t;try{let n=typeof e.headers.getSetCookie=="function"?Object.fromEntries(e.headers):e.headers,a={headers:n,cookies:n.cookie?G.default.parse(n.cookie):{}},i=new URL(e.url),s=i.pathname.split("/"),c=i.searchParams,o=he.parse(c.toString()),f=await((t=r.contextProvider)==null?void 0:t.call(r,{transport:"HTTP",headers:a.headers,cookies:a.cookies,query:o}))??{};if(e.method==="GET"){let y=s[s.length-1],{success:l,data:m,error:u}=H.safeParse(o);if(!l)return Response.json({message:"Invalid query",code:"INVALID_QUERY",details:u},{status:400});let d=await r.handleRequest({req:{...a,type:"QUERY",resourceName:y,context:f,where:m.where,include:m.include,query:o}});return !d||!d.data?Response.json({message:"Invalid resource",code:"INVALID_RESOURCE"},{status:400}):Response.json(d.data)}if(e.method==="POST")try{let y=s[s.length-1],l=s[s.length-2],m=e.body?await e.json():{},u;if(y==="set"){let{success:b,data:w,error:O}=N.safeParse(m);if(!b)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:O},{status:400});u=w;}else {let{success:b,data:w,error:O}=C.safeParse(m);if(!b)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:O},{status:400});u=w;}let d=await r.handleRequest({req:{...a,type:"MUTATE",resourceName:l,input:u.payload,context:f,resourceId:u.resourceId,procedure:y!=="set"?y:void 0,query:{}}});return Response.json(d)}catch(y){return console.error("Error parsing mutation from the client:",y),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}return Response.json({message:"Not found",code:"NOT_FOUND"},{status:404})}catch(n){return console.error("Unexpected error:",n),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}};var ee=b(L(),1);var R=z$1.string(),Te=z$1.object({id:R,type:z$1.literal("SUBSCRIBE"),resource:z$1.string()}),ge=E.extend({id:R,type:z$1.literal("QUERY")}),B=M.extend({id:R}),Re=S.extend({id:R}),be=z$1.union([Re,B]),W=z$1.union([Te,ge,be]),we=z$1.object({id:R,type:z$1.literal("REJECT"),resource:z$1.string(),message:z$1.string().optional()}),xe=z$1.object({id:R,type:z$1.literal("REPLY"),data:z$1.any()});z$1.union([we,xe,B]);z$1.object({resource:z$1.string(),data:z$1.record(P)});var Y="0123456789ABCDEFGHJKMNPQRSTVWXYZ",I=32;var Se=16,J=10,K=0xffffffffffff;var T;(function(r){r.Base32IncorrectEncoding="B32_ENC_INVALID",r.DecodeTimeInvalidCharacter="DEC_TIME_CHAR",r.DecodeTimeValueMalformed="DEC_TIME_MALFORMED",r.EncodeTimeNegative="ENC_TIME_NEG",r.EncodeTimeSizeExceeded="ENC_TIME_SIZE_EXCEED",r.EncodeTimeValueMalformed="ENC_TIME_MALFORMED",r.PRNGDetectFailure="PRNG_DETECT",r.ULIDInvalid="ULID_INVALID",r.Unexpected="UNEXPECTED",r.UUIDInvalid="UUID_INVALID";})(T||(T={}));var g=class extends Error{constructor(e,t){super(`${t} (${e})`),this.name="ULIDError",this.code=e;}};function Me(r){let e=Math.floor(r()*I);return e===I&&(e=I-1),Y.charAt(e)}function Ie(r){var n;let e=ve(),t=e&&(e.crypto||e.msCrypto)||(typeof A<"u"?A:null);if(typeof(t==null?void 0:t.getRandomValues)=="function")return ()=>{let a=new Uint8Array(1);return t.getRandomValues(a),a[0]/255};if(typeof(t==null?void 0:t.randomBytes)=="function")return ()=>t.randomBytes(1).readUInt8()/255;if((n=A)!=null&&n.randomBytes)return ()=>A.randomBytes(1).readUInt8()/255;throw new g(T.PRNGDetectFailure,"Failed to find a reliable PRNG")}function ve(){return je()?self:typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:null}function Ee(r,e){let t="";for(;r>0;r--)t=Me(e)+t;return t}function Ae(r,e=J){if(isNaN(r))throw new g(T.EncodeTimeValueMalformed,`Time must be a number: ${r}`);if(r>K)throw new g(T.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${K}: ${r}`);if(r<0)throw new g(T.EncodeTimeNegative,`Time must be positive: ${r}`);if(Number.isInteger(r)===false)throw new g(T.EncodeTimeValueMalformed,`Time must be an integer: ${r}`);let t,n="";for(let a=e;a>0;a--)t=r%I,n=Y.charAt(t)+n,r=(r-t)/I;return n}function je(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function X(r,e){let t=Ie(),n=Date.now();return Ae(n,J)+Ee(Se,t)}var _=()=>X().toLowerCase();var te=r=>{let e={},t={};return r.subscribeToMutations(n=>{let a=n;!a.resourceId||!a.payload||(console.log("Mutation propagated:",a),Object.entries(t[a.resource]??{}).forEach(([i,s])=>{var c;(c=e[i])==null||c.send(JSON.stringify({...a,id:a.id??_()}));}));}),(n,a)=>{var y;let i=l=>{n.send(JSON.stringify(l));},s=_(),c={headers:a.headers,cookies:typeof a.headers.cookie=="string"?ee.default.parse(a.headers.cookie):{}},o=parse(a.url.split("?")[1]),f=(y=r.contextProvider)==null?void 0:y.call(r,{transport:"WEBSOCKET",headers:c.headers,cookies:c.cookies,query:o});e[s]=n,console.log("Client connected:",s),n.on("message",async l=>{try{console.log("Message received from the client:",l);let m=W.parse(JSON.parse(l.toString()));if(m.type==="SUBSCRIBE"){let{resource:u}=m;t[u]||(t[u]={}),t[u][s]={};}else if(m.type==="QUERY"){let{resource:u}=m,d=await r.handleRequest({req:{...c,type:"QUERY",resourceName:u,context:await f??{},query:o}});if(!d||!d.data)throw new Error("Invalid resource");i({id:m.id,type:"REPLY",data:{resource:u,data:Object.fromEntries(Object.entries(d.data??{}).map(([b,w])=>[b,w.value]))}});}else if(m.type==="MUTATE"){let{resource:u}=m;console.log("Received mutation from client:",m);try{let d=await r.handleRequest({req:{...c,type:"MUTATE",resourceName:u,input:m.payload,context:{messageId:m.id,...await f??{}},resourceId:m.resourceId,procedure:m.procedure,query:o}});m.procedure&&i({id:m.id,type:"REPLY",data:d});}catch(d){i({id:m.id,type:"REJECT",resource:u,message:d.message}),console.error("Error parsing mutation from the client:",d);}}}catch(m){console.error("Error handling message from the client:",m);}}),n.on("close",()=>{console.log("Connection closed",s),delete e[s];for(let l of Object.values(t))delete l[s];});}};function re(r){let e=`${r.protocol}://${r.hostname}${r.url}`,t=new Headers;return Object.entries(r.headers).forEach(([n,a])=>{a&&t.set(n,Array.isArray(a)?a.join(","):a);}),new Request(e,{method:r.method,headers:t,body:r.body&&r.method!=="GET"?JSON.stringify(r.body):void 0})}var dt=(r,e,t)=>{r.ws(`${(t==null?void 0:t.basePath)??""}/ws`,te(e)),r.use(`${(t==null?void 0:t.basePath)??""}/`,(n,a)=>{Q(e)(re(n)).then(s=>s.json().then(c=>a.status(s.status).send(c)));});};var $=class r{routes;constructor(e){this.routes=e.routes;}static create(e){return new r(e)}},pt=r=>$.create({...r}),Pe=r=>({handler:e=>({inputValidator:r??z$1.undefined(),handler:e})}),U=class r{_resourceSchema;resourceName;middlewares;customMutations;constructor(e,t){this.resourceName=e,this.middlewares=new Set,this.customMutations=t??{};}handleFind=async({req:e,db:t})=>({data:await t.rawFind(e.resourceName,e.where,e.include),acceptedValues:null});handleSet=async({req:e,db:t,schema:n})=>{if(!e.input)throw new Error("Payload is required");if(!e.resourceId)throw new Error("ResourceId is required");let a=await t.rawFindById(e.resourceName,e.resourceId),[i,s]=n[this.resourceName].mergeMutation("set",e.input,a);if(!s)throw new Error("Mutation rejected");return {data:await t.rawUpsert(e.resourceName,e.resourceId,i),acceptedValues:s}};async handleRequest(e){let t=n=>(()=>{if(n.type==="QUERY")return this.handleFind({req:n,db:e.db,schema:e.schema});if(n.type==="MUTATE")if(n.procedure){if(this.customMutations[n.procedure]){let a=this.customMutations[n.procedure].inputValidator.parse(n.input);return n.input=a,this.customMutations[n.procedure].handler({req:n,db:e.db,schema:e.schema})}}else return this.handleSet({req:n,db:e.db,schema:e.schema});throw new Error("Invalid request")})();return await Array.from(this.middlewares.values()).reduceRight((n,a)=>i=>a({req:i,next:n}),async n=>t(n))(e.req)}use(...e){for(let t of e)this.middlewares.add(t);return this}withMutations(e){return new r(this.resourceName,e({mutation:Pe}))}},D=class r{middlewares;constructor(e=[]){this.middlewares=e;}createBasicRoute(e){return new U(e.name).use(...this.middlewares)}use(...e){return new r([...this.middlewares,...e])}static create(){return new r}},yt=D.create;var z=class{async insert(e,t){let n=new Date().toISOString();return r(await this.rawUpsert(e.name,t.id,{value:Object.fromEntries(Object.entries(t).map(([a,i])=>[a,{value:i,_meta:{timestamp:n}}]))}))}async update(e,t,n){let a=new Date().toISOString(),{id:i,...s}=n;return r(await this.rawUpsert(e.name,t,{value:Object.fromEntries(Object.entries(s).map(([c,o])=>[c,{value:o,_meta:{timestamp:a}}]))}))}},ne=class extends z{db;schema;constructor(e){super(),this.db=new Kysely({dialect:new PostgresDialect({pool:e})});}async updateSchema(e){this.schema=e;let t=await this.db.introspection.getTables();for(let[n,a]of Object.entries(e)){let i=t.find(o=>o.name===n);i||await this.db.schema.createTable(n).ifNotExists().execute();let s=`${n}_meta`,c=t.find(o=>o.name===s);c||await this.db.schema.createTable(s).ifNotExists().execute();for(let[o,f]of Object.entries(a.fields)){let y=i==null?void 0:i.columns.find(u=>u.name===o),l=f.getStorageFieldType();y?y.dataType!==l.type&&console.error("Column type mismatch:",o,"expected to have type:",l.type,"but has type:",y.dataType):(await this.db.schema.alterTable(n).addColumn(o,l.type,u=>{let d=u;return l.unique&&(d=d.unique()),l.nullable||(d=d.notNull()),l.references&&(d=d.references(l.references)),l.primary&&(d=d.primaryKey()),l.default!==void 0&&(d=d.defaultTo(l.default)),d}).execute().catch(u=>{throw console.error("Error adding column",o,u),u}),l.index&&await this.db.schema.createIndex(`${n}_${o}_index`).on(n).column(o).execute().catch(u=>{})),(c==null?void 0:c.columns.find(u=>u.name===o))||await this.db.schema.alterTable(s).addColumn(o,"varchar",u=>{let d=u;return l.primary&&(d=d.primaryKey().references(`${n}.${o}`)),d}).execute();}}}async rawFindById(e,t,n){let a=await this.db.selectFrom(e).where("id","=",t).selectAll(e).select(s=>jsonObjectFrom(s.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=this.applyInclude(e,a,n);let i=await a.executeTakeFirst();if(i)return this.convertToMaterializedLiveType(i)}async findOne(e,t,n){let a=await this.rawFindById(e.name,t,n==null?void 0:n.include);if(a)return r(a)}async rawFind(e,t,n){let a=this.db.selectFrom(e).selectAll(e).select(o=>jsonObjectFrom(o.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=this.applyWhere(e,a,t),a=this.applyInclude(e,a,n);let i=await a.execute(),s=Object.fromEntries(i.map(o=>{let{id:f,...y}=o;return [f,y]}));return Object.keys(s).length===0?{}:Object.entries(s).reduce((o,[f,y])=>(o[f]=this.convertToMaterializedLiveType(y),o),{})}async find(e,t){let n=await this.rawFind(e.name,t==null?void 0:t.where,t==null?void 0:t.include);return Object.fromEntries(Object.entries(n).map(([a,i])=>[a,r(i)]))}async rawUpsert(e,t,n){return await this.db.transaction().execute(async a=>{var o;let i=!!await a.selectFrom(e).select("id").where("id","=",t).executeTakeFirst(),s={},c={};for(let[f,y]of Object.entries(n.value)){let l=(o=y._meta)==null?void 0:o.timestamp;l&&(s[f]=y.value,c[f]=l);}i?await Promise.all([a.updateTable(e).set(s).where("id","=",t).execute(),a.updateTable(`${e}_meta`).set(c).where("id","=",t).execute()]):await Promise.all([a.insertInto(e).values({...s,id:t}).execute(),a.insertInto(`${e}_meta`).values({...c,id:t}).execute()]);}),n}convertToMaterializedLiveType(e){if(!e._meta)throw new Error("Missing _meta");return {value:Object.entries(e).reduce((t,[n,a])=>{var i,s,c;return n==="_meta"||(n==="id"?t[n]={value:a}:Array.isArray(a)?t[n]={value:a.map(o=>this.convertToMaterializedLiveType(o)),_meta:{timestamp:(i=e==null?void 0:e._meta)==null?void 0:i[n]}}:typeof a=="object"&&a!==null&&!(a instanceof Date)?t[n]={...this.convertToMaterializedLiveType(a),_meta:{timestamp:(s=e==null?void 0:e._meta)==null?void 0:s[n]}}:t[n]={value:a,_meta:{timestamp:(c=e==null?void 0:e._meta)==null?void 0:c[n]}}),t},{})}}applyWhere(e,t,n){if(!n)return t;if(!this.schema)throw new Error("Schema not initialized");let a=this.schema[e];if(!a)throw new Error("Resource not found");for(let[i,s]of Object.entries(n))if(a.fields[i])t=t.where(`${e}.${i}`,"=",s);else if(a.relations[i]){let c=a.relations[i],o=c.entity.name,f=c.type==="one"?"id":c.foreignColumn,y=c.type==="one"?c.relationalColumn:"id";t=t.leftJoin(o,`${o}.${f}`,`${e}.${y}`),t=this.applyWhere(o,t,s);}return t}applyInclude(e,t,n){if(!n)return t;if(!this.schema)throw new Error("Schema not initialized");let a=this.schema[e];if(!a)throw new Error(`Resource not found: ${e}`);for(let[i,s]of Object.entries(n)){if(!a.relations[i])throw new Error(`Relation ${i} not found in resource ${e}`);let c=a.relations[i],o=c.entity.name,f=c.type==="one"?"id":c.foreignColumn,y=c.type==="one"?c.relationalColumn:"id",l=c.type==="one"?jsonObjectFrom:jsonArrayFrom;t=t.select(m=>l(m.selectFrom(o).selectAll(o).whereRef(`${o}.${f}`,"=",`${e}.${y}`).select(u=>jsonObjectFrom(u.selectFrom(`${o}_meta`).selectAll(`${o}_meta`).whereRef(`${o}_meta.id`,"=",`${o}.id`)).as("_meta"))).as(i));}return t}};var k=class r{router;storage;schema;middlewares=new Set;contextProvider;mutationSubscriptions=new Set;constructor(e){var t;this.router=e.router,this.storage=e.storage,this.schema=e.schema,(t=e.middlewares)==null||t.forEach(n=>{this.middlewares.add(n);}),this.storage.updateSchema(this.schema),this.contextProvider=e.contextProvider;}static create(e){return new r(e)}subscribeToMutations(e){return this.mutationSubscriptions.add(e),()=>{this.mutationSubscriptions.delete(e);}}async handleRequest(e){if(!this.router.routes[e.req.resourceName])throw new Error("Invalid resource");let t=await Array.from(this.middlewares.values()).reduceRight((n,a)=>i=>a({req:i,next:n}),async n=>this.router.routes[e.req.resourceName].handleRequest({req:n,db:this.storage,schema:this.schema}))(e.req);return t&&e.req.type==="MUTATE"&&t.acceptedValues&&Object.keys(t.acceptedValues).length>0&&this.mutationSubscriptions.forEach(n=>{n({id:e.req.context.messageId,type:"MUTATE",resource:e.req.resourceName,payload:t.acceptedValues??{},resourceId:e.req.resourceId});}),t}use(e){return this.middlewares.add(e),this}context(e){return this.contextProvider=e,this}},bt=k.create;
|
|
2
|
-
export{
|
|
1
|
+
import {a,b,r}from'./chunk-LLHCJUB6.js';import Re,{parse}from'qs';import {z as z$1}from'zod';import $ from'node:crypto';import {Kysely,PostgresDialect}from'kysely';import {jsonObjectFrom,jsonArrayFrom}from'kysely/helpers/postgres';var O=a(v=>{Object.defineProperty(v,"__esModule",{value:true});v.parse=fe;v.serialize=ye;var ce=/^[\u0021-\u003A\u003C\u003E-\u007E]+$/,de=/^[\u0021-\u003A\u003C-\u007E]*$/,ue=/^([.]?[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)([.][a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/i,le=/^[\u0020-\u003A\u003D-\u007E]*$/,me=Object.prototype.toString,pe=(()=>{let r=function(){};return r.prototype=Object.create(null),r})();function fe(r,e){let t=new pe,n=r.length;if(n<2)return t;let a=(e==null?void 0:e.decode)||he,o=0;do{let s=r.indexOf("=",o);if(s===-1)break;let c=r.indexOf(";",o),i=c===-1?n:c;if(s>i){o=r.lastIndexOf(";",s-1)+1;continue}let y=q(r,o,s),f=B(r,s,y),d=r.slice(y,f);if(t[d]===void 0){let m=q(r,s+1,i),p=B(r,i,m),u=a(r.slice(m,p));t[d]=u;}o=i+1;}while(o<n);return t}function q(r,e,t){do{let n=r.charCodeAt(e);if(n!==32&&n!==9)return e}while(++e<t);return t}function B(r,e,t){for(;e>t;){let n=r.charCodeAt(--e);if(n!==32&&n!==9)return e+1}return t}function ye(r,e,t){let n=(t==null?void 0:t.encode)||encodeURIComponent;if(!ce.test(r))throw new TypeError(`argument name is invalid: ${r}`);let a=n(e);if(!de.test(a))throw new TypeError(`argument val is invalid: ${e}`);let o=r+"="+a;if(!t)return o;if(t.maxAge!==void 0){if(!Number.isInteger(t.maxAge))throw new TypeError(`option maxAge is invalid: ${t.maxAge}`);o+="; Max-Age="+t.maxAge;}if(t.domain){if(!ue.test(t.domain))throw new TypeError(`option domain is invalid: ${t.domain}`);o+="; Domain="+t.domain;}if(t.path){if(!le.test(t.path))throw new TypeError(`option path is invalid: ${t.path}`);o+="; Path="+t.path;}if(t.expires){if(!Te(t.expires)||!Number.isFinite(t.expires.valueOf()))throw new TypeError(`option expires is invalid: ${t.expires}`);o+="; Expires="+t.expires.toUTCString();}if(t.httpOnly&&(o+="; HttpOnly"),t.secure&&(o+="; Secure"),t.partitioned&&(o+="; Partitioned"),t.priority)switch(typeof t.priority=="string"?t.priority.toLowerCase():void 0){case "low":o+="; Priority=Low";break;case "medium":o+="; Priority=Medium";break;case "high":o+="; Priority=High";break;default:throw new TypeError(`option priority is invalid: ${t.priority}`)}if(t.sameSite)switch(typeof t.sameSite=="string"?t.sameSite.toLowerCase():t.sameSite){case true:case "strict":o+="; SameSite=Strict";break;case "lax":o+="; SameSite=Lax";break;case "none":o+="; SameSite=None";break;default:throw new TypeError(`option sameSite is invalid: ${t.sameSite}`)}return o}function he(r){if(r.indexOf("%")===-1)return r;try{return decodeURIComponent(r)}catch{return r}}function Te(r){return me.call(r)==="[object Date]"}});var Q=b(O(),1);var A=z$1.object({resource:z$1.string(),where:z$1.record(z$1.string(),z$1.any()).optional(),include:z$1.record(z$1.string(),z$1.any()).optional(),lastSyncedAt:z$1.string().optional(),limit:z$1.number().optional(),sort:z$1.array(z$1.object({key:z$1.string(),direction:z$1.enum(["asc","desc"])})).optional()}),L=z$1.record(z$1.string(),z$1.object({value:z$1.string().or(z$1.number()).or(z$1.boolean()).or(z$1.date()).nullable(),_meta:z$1.object({timestamp:z$1.string().optional().nullable()}).optional()})).superRefine((r,e)=>{r.id&&e.addIssue({code:z$1.ZodIssueCode.custom,message:"Payload cannot have an id"});}),H=z$1.object({id:z$1.string().optional(),type:z$1.literal("MUTATE"),resource:z$1.string()}),S=H.extend({procedure:z$1.string(),payload:z$1.any().optional()}),M=H.extend({resourceId:z$1.string(),payload:L});z$1.union([S,M]);var G=A.omit({resource:true}),P=S.omit({id:true,type:true,resource:true,procedure:true}),C=M.omit({id:true,type:true,resource:true});z$1.union([C,P]);var W=r=>async e=>{var t;try{let n=typeof e.headers.getSetCookie=="function"?Object.fromEntries(e.headers):e.headers,a={headers:n,cookies:n.cookie?Q.default.parse(n.cookie):{}},o=new URL(e.url),s=o.pathname.split("/"),c=o.searchParams,i=Re.parse(c.toString()),y=await((t=r.contextProvider)==null?void 0:t.call(r,{transport:"HTTP",headers:a.headers,cookies:a.cookies,query:i}))??{};if(e.method==="GET"){let f=s[s.length-1],{success:d,data:m,error:p}=G.safeParse(i);if(!d)return Response.json({message:"Invalid query",code:"INVALID_QUERY",details:p},{status:400});let u=await r.handleRequest({req:{...a,type:"QUERY",resourceName:f,context:y,where:m.where,include:m.include,query:i}});return !u||!u.data?Response.json({message:"Invalid resource",code:"INVALID_RESOURCE"},{status:400}):Response.json(u.data)}if(e.method==="POST")try{let f=s[s.length-1],d=s[s.length-2],m=e.body?await e.json():{},p;if(f==="set"){let{success:b,data:x,error:j}=C.safeParse(m);if(!b)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:j},{status:400});p=x;}else {let{success:b,data:x,error:j}=P.safeParse(m);if(!b)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:j},{status:400});p=x;}let u=await r.handleRequest({req:{...a,type:"MUTATE",resourceName:d,input:p.payload,context:y,resourceId:p.resourceId,procedure:f!=="set"?f:void 0,query:{}}});return Response.json(u)}catch(f){return console.error("Error parsing mutation from the client:",f),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}return Response.json({message:"Not found",code:"NOT_FOUND"},{status:404})}catch(n){return console.error("Unexpected error:",n),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}};var re=b(O(),1);var w=z$1.string(),we=z$1.object({id:w,type:z$1.literal("SUBSCRIBE"),resource:z$1.string()}),be=A.extend({id:w,type:z$1.literal("QUERY")}),Y=M.extend({id:w}),xe=S.extend({id:w}),Se=z$1.union([xe,Y]),J=z$1.union([we,be,Se]),Me=z$1.object({id:w,type:z$1.literal("REJECT"),resource:z$1.string(),message:z$1.string().optional()}),Ie=z$1.object({id:w,type:z$1.literal("REPLY"),data:z$1.any()});z$1.union([Me,Ie,Y]);z$1.object({resource:z$1.string(),data:z$1.record(z$1.string(),L)});var X="0123456789ABCDEFGHJKMNPQRSTVWXYZ",I=32;var Ee=16,ee=10,K=0xffffffffffff;var T;(function(r){r.Base32IncorrectEncoding="B32_ENC_INVALID",r.DecodeTimeInvalidCharacter="DEC_TIME_CHAR",r.DecodeTimeValueMalformed="DEC_TIME_MALFORMED",r.EncodeTimeNegative="ENC_TIME_NEG",r.EncodeTimeSizeExceeded="ENC_TIME_SIZE_EXCEED",r.EncodeTimeValueMalformed="ENC_TIME_MALFORMED",r.PRNGDetectFailure="PRNG_DETECT",r.ULIDInvalid="ULID_INVALID",r.Unexpected="UNEXPECTED",r.UUIDInvalid="UUID_INVALID";})(T||(T={}));var g=class extends Error{constructor(e,t){super(`${t} (${e})`),this.name="ULIDError",this.code=e;}};function ve(r){let e=Math.floor(r()*I);return e===I&&(e=I-1),X.charAt(e)}function Ae(r){var n;let e=$e(),t=e&&(e.crypto||e.msCrypto)||(typeof $<"u"?$:null);if(typeof(t==null?void 0:t.getRandomValues)=="function")return ()=>{let a=new Uint8Array(1);return t.getRandomValues(a),a[0]/255};if(typeof(t==null?void 0:t.randomBytes)=="function")return ()=>t.randomBytes(1).readUInt8()/255;if((n=$)!=null&&n.randomBytes)return ()=>$.randomBytes(1).readUInt8()/255;throw new g(T.PRNGDetectFailure,"Failed to find a reliable PRNG")}function $e(){return Le()?self:typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:null}function je(r,e){let t="";for(;r>0;r--)t=ve(e)+t;return t}function Oe(r,e=ee){if(isNaN(r))throw new g(T.EncodeTimeValueMalformed,`Time must be a number: ${r}`);if(r>K)throw new g(T.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${K}: ${r}`);if(r<0)throw new g(T.EncodeTimeNegative,`Time must be positive: ${r}`);if(Number.isInteger(r)===false)throw new g(T.EncodeTimeValueMalformed,`Time must be an integer: ${r}`);let t,n="";for(let a=e;a>0;a--)t=r%I,n=X.charAt(t)+n,r=(r-t)/I;return n}function Le(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function te(r,e){let t=Ae(),n=Date.now();return Oe(n,ee)+je(Ee,t)}var _=()=>te().toLowerCase();var ne=r=>{let e={},t={};return r.subscribeToMutations(n=>{let a=n;!a.resourceId||!a.payload||(console.log("Mutation propagated:",a),Object.entries(t[a.resource]??{}).forEach(([o,s])=>{var c;(c=e[o])==null||c.send(JSON.stringify({...a,id:a.id??_()}));}));}),(n,a)=>{var f;let o=d=>{n.send(JSON.stringify(d));},s=_(),c={headers:a.headers,cookies:typeof a.headers.cookie=="string"?re.default.parse(a.headers.cookie):{}},i=parse(a.url.split("?")[1]),y=(f=r.contextProvider)==null?void 0:f.call(r,{transport:"WEBSOCKET",headers:c.headers,cookies:c.cookies,query:i});e[s]=n,console.log("Client connected:",s),n.on("message",async d=>{try{console.log("Message received from the client:",d);let m=J.parse(JSON.parse(d.toString()));if(m.type==="SUBSCRIBE"){let{resource:p}=m;t[p]||(t[p]={}),t[p][s]={};}else if(m.type==="QUERY"){let{resource:p}=m,u=await r.handleRequest({req:{...c,type:"QUERY",resourceName:p,context:await y??{},query:i}});if(!u||!u.data)throw new Error("Invalid resource");o({id:m.id,type:"REPLY",data:{resource:p,data:Object.fromEntries(Object.entries(u.data??{}).map(([b,x])=>[b,x.value]))}});}else if(m.type==="MUTATE"){let{resource:p}=m;console.log("Received mutation from client:",m);try{let u=await r.handleRequest({req:{...c,type:"MUTATE",resourceName:p,input:m.payload,context:{messageId:m.id,...await y??{}},resourceId:m.resourceId,procedure:m.procedure,query:i}});m.procedure&&o({id:m.id,type:"REPLY",data:u});}catch(u){o({id:m.id,type:"REJECT",resource:p,message:u.message}),console.error("Error parsing mutation from the client:",u);}}}catch(m){console.error("Error handling message from the client:",m);}}),n.on("close",()=>{console.log("Connection closed",s),delete e[s];for(let d of Object.values(t))delete d[s];});}};function ae(r){let e=`${r.protocol}://${r.hostname}${r.url}`,t=new Headers;return Object.entries(r.headers).forEach(([n,a])=>{a&&t.set(n,Array.isArray(a)?a.join(","):a);}),new Request(e,{method:r.method,headers:t,body:r.body&&r.method!=="GET"?JSON.stringify(r.body):void 0})}var pt=(r,e,t)=>{r.ws(`${(t==null?void 0:t.basePath)??""}/ws`,ne(e)),r.use(`${(t==null?void 0:t.basePath)??""}/`,(n,a)=>{W(e)(ae(n)).then(s=>s.json().then(c=>a.status(s.status).send(c)));});};var U=class r{routes;constructor(e){this.routes=e.routes;}static create(e){return new r(e)}},ht=r=>U.create({...r}),_e=r=>({handler:e=>({inputValidator:r??z$1.undefined(),handler:e})}),N=class r{_resourceSchema;resourceName;middlewares;customMutations;constructor(e,t){this.resourceName=e,this.middlewares=new Set,this.customMutations=t??{};}handleFind=async({req:e,db:t})=>({data:await t.rawFind(e.resourceName,e.where,e.include),acceptedValues:null});handleSet=async({req:e,db:t,schema:n})=>{if(!e.input)throw new Error("Payload is required");if(!e.resourceId)throw new Error("ResourceId is required");let a=await t.rawFindById(e.resourceName,e.resourceId),[o,s]=n[this.resourceName].mergeMutation("set",e.input,a);if(!s)throw new Error("Mutation rejected");return {data:await t.rawUpsert(e.resourceName,e.resourceId,o),acceptedValues:s}};async handleRequest(e){let t=n=>(()=>{if(n.type==="QUERY")return this.handleFind({req:n,db:e.db,schema:e.schema});if(n.type==="MUTATE")if(n.procedure){if(this.customMutations[n.procedure]){let a=this.customMutations[n.procedure].inputValidator.parse(n.input);return n.input=a,this.customMutations[n.procedure].handler({req:n,db:e.db,schema:e.schema})}}else return this.handleSet({req:n,db:e.db,schema:e.schema});throw new Error("Invalid request")})();return await Array.from(this.middlewares.values()).reduceRight((n,a)=>o=>a({req:o,next:n}),async n=>t(n))(e.req)}use(...e){for(let t of e)this.middlewares.add(t);return this}withMutations(e){return new r(this.resourceName,e({mutation:_e}))}},D=class r{middlewares;constructor(e=[]){this.middlewares=e;}createBasicRoute(e){return new N(e.name).use(...this.middlewares)}use(...e){return new r([...this.middlewares,...e])}static create(){return new r}},Tt=D.create;var E=class{async insert(e,t){let n=new Date().toISOString();return r(await this.rawUpsert(e.name,t.id,{value:Object.fromEntries(Object.entries(t).map(([a,o])=>[a,{value:o,_meta:{timestamp:n}}]))}))}async update(e,t,n){let a=new Date().toISOString(),{id:o,...s}=n;return r(await this.rawUpsert(e.name,t,{value:Object.fromEntries(Object.entries(s).map(([c,i])=>[c,{value:i,_meta:{timestamp:a}}]))}))}};function z(r,e,t,n){var o,s;if(!n)return t;if(!r)throw new Error("Schema not initialized");let a=r[e];if(!a)throw new Error("Resource not found");for(let[c,i]of Object.entries(n))if(a.fields[c])(i==null?void 0:i.$eq)!==void 0?t=t.where(`${e}.${c}`,i.$eq===null?"is":"=",i.$eq):(i==null?void 0:i.$in)!==void 0?t=t.where(`${e}.${c}`,"in",i.$in):(i==null?void 0:i.$not)!==void 0?((o=i==null?void 0:i.$not)==null?void 0:o.$in)!==void 0?t=t.where(`${e}.${c}`,"not in",i.$not.$in):((s=i==null?void 0:i.$not)==null?void 0:s.$eq)!==void 0?t=t.where(`${e}.${c}`,i.$not.$eq===null?"is not":"!=",i.$not.$eq):t=t.where(`${e}.${c}`,i.$not===null?"is not":"!=",i.$not):(i==null?void 0:i.$gt)!==void 0?t=t.where(`${e}.${c}`,">",i.$gt):(i==null?void 0:i.$gte)!==void 0?t=t.where(`${e}.${c}`,">=",i.$gte):(i==null?void 0:i.$lt)!==void 0?t=t.where(`${e}.${c}`,"<",i.$lt):(i==null?void 0:i.$lte)!==void 0?t=t.where(`${e}.${c}`,"<=",i.$lte):t=t.where(`${e}.${c}`,i===null?"is":"=",i);else if(a.relations[c]){let y=a.relations[c],f=y.entity.name,d=y.type==="one"?"id":y.foreignColumn,m=y.type==="one"?y.relationalColumn:"id";t=t.leftJoin(f,`${f}.${d}`,`${e}.${m}`),t=z(r,f,t,i);}return t}function V(r,e,t,n){if(!n)return t;if(!r)throw new Error("Schema not initialized");let a=r[e];if(!a)throw new Error(`Resource not found: ${e}`);for(let o of Object.keys(n)){if(!a.relations[o])throw new Error(`Relation ${o} not found in resource ${e}`);let s=a.relations[o],c=s.entity.name,i=s.type==="one"?"id":s.foreignColumn,y=s.type==="one"?s.relationalColumn:"id",f=s.type==="one"?jsonObjectFrom:jsonArrayFrom;t=t.select(d=>f(d.selectFrom(c).selectAll(c).whereRef(`${c}.${i}`,"=",`${e}.${y}`).select(m=>jsonObjectFrom(m.selectFrom(`${c}_meta`).selectAll(`${c}_meta`).whereRef(`${c}_meta.id`,"=",`${c}.id`)).as("_meta"))).as(o));}return t}var k=class extends E{db;schema;constructor(e){super(),this.db=new Kysely({dialect:new PostgresDialect({pool:e})});}async updateSchema(e){this.schema=e;let t=await this.db.introspection.getTables();for(let[n,a]of Object.entries(e)){let o=t.find(i=>i.name===n);o||await this.db.schema.createTable(n).ifNotExists().execute();let s=`${n}_meta`,c=t.find(i=>i.name===s);c||await this.db.schema.createTable(s).ifNotExists().execute();for(let[i,y]of Object.entries(a.fields)){let f=o==null?void 0:o.columns.find(p=>p.name===i),d=y.getStorageFieldType();f?f.dataType!==d.type&&console.error("Column type mismatch:",i,"expected to have type:",d.type,"but has type:",f.dataType):(await this.db.schema.alterTable(n).addColumn(i,d.type,p=>{let u=p;return d.unique&&(u=u.unique()),d.nullable||(u=u.notNull()),d.references&&(u=u.references(d.references)),d.primary&&(u=u.primaryKey()),d.default!==void 0&&(u=u.defaultTo(d.default)),u}).execute().catch(p=>{throw console.error("Error adding column",i,p),p}),d.index&&await this.db.schema.createIndex(`${n}_${i}_index`).on(n).column(i).execute().catch(p=>{})),(c==null?void 0:c.columns.find(p=>p.name===i))||await this.db.schema.alterTable(s).addColumn(i,"varchar",p=>{let u=p;return d.primary&&(u=u.primaryKey().references(`${n}.${i}`)),u}).execute();}}}async rawFindById(e,t,n){if(!this.schema)throw new Error("Schema not initialized");let a=await this.db.selectFrom(e).where("id","=",t).selectAll(e).select(s=>jsonObjectFrom(s.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=V(this.schema,e,a,n);let o=await a.executeTakeFirst();if(o)return this.convertToMaterializedLiveType(o)}async findOne(e,t,n){let a=await this.rawFindById(e.name,t,n==null?void 0:n.include);if(a)return r(a)}async rawFind(e,t,n){if(!this.schema)throw new Error("Schema not initialized");let a=this.db.selectFrom(e).selectAll(e).select(i=>jsonObjectFrom(i.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=z(this.schema,e,a,t),a=V(this.schema,e,a,n);let o=await a.execute(),s=Object.fromEntries(o.map(i=>{let{id:y,...f}=i;return [y,f]}));return Object.keys(s).length===0?{}:Object.entries(s).reduce((i,[y,f])=>(i[y]=this.convertToMaterializedLiveType(f),i),{})}async find(e,t){let n=await this.rawFind(e.name,t==null?void 0:t.where,t==null?void 0:t.include);return Object.fromEntries(Object.entries(n).map(([a,o])=>[a,r(o)]))}async rawUpsert(e,t,n){return await this.db.transaction().execute(async a=>{var i;let o=!!await a.selectFrom(e).select("id").where("id","=",t).executeTakeFirst(),s={},c={};for(let[y,f]of Object.entries(n.value)){let d=(i=f._meta)==null?void 0:i.timestamp;d&&(s[y]=f.value,c[y]=d);}o?await Promise.all([a.updateTable(e).set(s).where("id","=",t).execute(),a.updateTable(`${e}_meta`).set(c).where("id","=",t).execute()]):await Promise.all([a.insertInto(e).values({...s,id:t}).execute(),a.insertInto(`${e}_meta`).values({...c,id:t}).execute()]);}),n}convertToMaterializedLiveType(e){if(!e._meta)throw new Error("Missing _meta");return {value:Object.entries(e).reduce((t,[n,a])=>{var o,s,c;return n==="_meta"||(n==="id"?t[n]={value:a}:Array.isArray(a)?t[n]={value:a.map(i=>this.convertToMaterializedLiveType(i)),_meta:{timestamp:(o=e==null?void 0:e._meta)==null?void 0:o[n]}}:typeof a=="object"&&a!==null&&!(a instanceof Date)?t[n]={...this.convertToMaterializedLiveType(a),_meta:{timestamp:(s=e==null?void 0:e._meta)==null?void 0:s[n]}}:t[n]={value:a,_meta:{timestamp:(c=e==null?void 0:e._meta)==null?void 0:c[n]}}),t},{})}}};var F=class r{router;storage;schema;middlewares=new Set;contextProvider;mutationSubscriptions=new Set;constructor(e){var t;this.router=e.router,this.storage=e.storage,this.schema=e.schema,(t=e.middlewares)==null||t.forEach(n=>{this.middlewares.add(n);}),this.storage.updateSchema(this.schema),this.contextProvider=e.contextProvider;}static create(e){return new r(e)}subscribeToMutations(e){return this.mutationSubscriptions.add(e),()=>{this.mutationSubscriptions.delete(e);}}async handleRequest(e){if(!this.router.routes[e.req.resourceName])throw new Error("Invalid resource");let t=await Array.from(this.middlewares.values()).reduceRight((n,a)=>o=>a({req:o,next:n}),async n=>this.router.routes[e.req.resourceName].handleRequest({req:n,db:this.storage,schema:this.schema}))(e.req);return t&&e.req.type==="MUTATE"&&t.acceptedValues&&Object.keys(t.acceptedValues).length>0&&this.mutationSubscriptions.forEach(n=>{n({id:e.req.context.messageId,type:"MUTATE",resource:e.req.resourceName,payload:t.acceptedValues??{},resourceId:e.req.resourceId});}),t}use(e){return this.middlewares.add(e),this}context(e){return this.contextProvider=e,this}},Lt=F.create;
|
|
2
|
+
export{N as Route,D as RouteFactory,U as Router,k as SQLStorage,F as Server,E as Storage,pt as expressAdapter,Tt as routeFactory,ht as router,Lt as server};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-state/sync",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4-beta.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"kysely": "^0.28.2",
|
|
51
51
|
"qs": "^6.14.0",
|
|
52
52
|
"ws": "^8.18.0",
|
|
53
|
-
"zod": "^
|
|
53
|
+
"zod": "^4.1.9"
|
|
54
54
|
},
|
|
55
55
|
"exports": {
|
|
56
56
|
".": {
|