tinybase 4.3.11 → 4.3.13

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.
Files changed (38) hide show
  1. package/lib/cjs/persisters/persister-partykit-client.cjs +1 -1
  2. package/lib/cjs/persisters/persister-partykit-client.cjs.gz +0 -0
  3. package/lib/cjs/persisters/persister-partykit-server.cjs +1 -1
  4. package/lib/cjs/persisters/persister-partykit-server.cjs.gz +0 -0
  5. package/lib/cjs-es6/persisters/persister-partykit-client.cjs +1 -1
  6. package/lib/cjs-es6/persisters/persister-partykit-client.cjs.gz +0 -0
  7. package/lib/cjs-es6/persisters/persister-partykit-server.cjs +1 -1
  8. package/lib/cjs-es6/persisters/persister-partykit-server.cjs.gz +0 -0
  9. package/lib/debug/persisters/persister-partykit-client.js +29 -19
  10. package/lib/debug/persisters/persister-partykit-server.js +160 -78
  11. package/lib/es6/persisters/persister-partykit-client.js +1 -1
  12. package/lib/es6/persisters/persister-partykit-client.js.gz +0 -0
  13. package/lib/es6/persisters/persister-partykit-server.js +1 -1
  14. package/lib/es6/persisters/persister-partykit-server.js.gz +0 -0
  15. package/lib/persisters/persister-partykit-client.js +1 -1
  16. package/lib/persisters/persister-partykit-client.js.gz +0 -0
  17. package/lib/persisters/persister-partykit-server.js +1 -1
  18. package/lib/persisters/persister-partykit-server.js.gz +0 -0
  19. package/lib/types/persisters/persister-indexed-db.d.ts +4 -5
  20. package/lib/types/persisters/persister-partykit-client.d.ts +19 -8
  21. package/lib/types/persisters/persister-partykit-server.d.ts +350 -12
  22. package/lib/types/tools.d.ts +4 -6
  23. package/lib/types/ui-react.d.ts +4 -4
  24. package/lib/types/with-schemas/persisters/persister-indexed-db.d.ts +4 -5
  25. package/lib/types/with-schemas/persisters/persister-partykit-client.d.ts +19 -8
  26. package/lib/types/with-schemas/persisters/persister-partykit-server.d.ts +383 -12
  27. package/lib/types/with-schemas/tools.d.ts +4 -6
  28. package/lib/types/with-schemas/ui-react.d.ts +4 -4
  29. package/lib/umd/persisters/persister-partykit-client.js +1 -1
  30. package/lib/umd/persisters/persister-partykit-client.js.gz +0 -0
  31. package/lib/umd/persisters/persister-partykit-server.js +1 -1
  32. package/lib/umd/persisters/persister-partykit-server.js.gz +0 -0
  33. package/lib/umd-es6/persisters/persister-partykit-client.js +1 -1
  34. package/lib/umd-es6/persisters/persister-partykit-client.js.gz +0 -0
  35. package/lib/umd-es6/persisters/persister-partykit-server.js +1 -1
  36. package/lib/umd-es6/persisters/persister-partykit-server.js.gz +0 -0
  37. package/package.json +15 -15
  38. package/readme.md +2 -2
@@ -1 +1 @@
1
- "use strict";const t=t=>typeof t,a=t(""),e=(t,a)=>t instanceof a,s=t=>null==t,o=e=>t(e)==a,n=Object,r=n.keys,c=n.freeze,i=t=>(t=>e(t,n)&&t.constructor==n)(t)&&0==(t=>r(t).length)(t),y=t=>JSON.stringify(t,((t,a)=>e(a,Map)?n.fromEntries([...a]):a)),d=JSON.parse,u=t=>new Map(t),h=(t,a)=>t?.get(a),v=(t,a,e)=>{return s(e)?(o=t,n=a,o?.delete(n),t):t?.set(a,e);var o,n},w=(t,a,e)=>{var s,o;return s=t,o=a,s?.has(o)||v(t,a,e()),h(t,a)},l=u(),p=u(),f="message",S={storeProtocol:"https",storePath:"/store"};exports.createPartyKitPersister=(t,a,e,n)=>{const{host:r,room:u}=a.partySocketOptions,{storeProtocol:g,storePath:m}={...S,...o(e)?{storeProtocol:e}:e},A=g+"://"+r+"/parties/"+(a.name??"main")+"/"+u+m,L=async t=>await(await fetch(A,{...t?{method:"PUT",body:y(t)}:{},mode:"cors",cache:"no-store"})).json();return((t,a,e,o,n,r,y=[])=>{let d,u,f,S=0,g=0;w(l,y,(()=>0)),w(p,y,(()=>[]));const m=async t=>(2!=S&&(S=1,await A.schedule((async()=>{await t(),S=0}))),A),A={load:async(e,s)=>await m((async()=>{try{t.setContent(await a())}catch{t.setContent([e,s])}})),startAutoLoad:async(e={},s={})=>(A.stopAutoLoad(),await A.load(e,s),g=1,f=o((async(e,s)=>{if(s){const a=s();await m((async()=>t.setTransactionChanges(a)))}else await m((async()=>{try{t.setContent(e?.()??await a())}catch(t){r?.(t)}}))})),A),stopAutoLoad:()=>(g&&(n(f),f=void 0,g=0),A),save:async a=>(1!=S&&(S=2,await A.schedule((async()=>{try{await e(t.getContent,a)}catch(t){r?.(t)}S=0}))),A),startAutoSave:async()=>(await A.stopAutoSave().save(),d=t.addDidFinishTransactionListener(((t,a)=>{const[e,s]=a();i(e)&&i(s)||A.save((()=>[e,s]))})),A),stopAutoSave:()=>{var a,e;return a=d,e=t.delListener,s(a)||e(a),A},schedule:async(...t)=>(((t,...a)=>{t.push(...a)})(h(p,y),...t),await(async()=>{if(!h(l,y)){for(v(l,y,1);!s((t=h(p,y),u=t.shift()));)try{await u()}catch(t){r?.(t)}v(l,y,0)}var t})(),A),getStore:()=>t,destroy:()=>A.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return c(A)})(t,(async()=>await L()),(async(t,e)=>{var s;e?a.send((s=e(),"s"+(o(s)?s:y(s)))):await L(t())}),(t=>{const e=a=>{const[e,s]=[(c=a.data)[0],d((o=c,n=1,o.slice(n,r)))];var o,n,r,c;"s"==e&&t(void 0,(()=>s))};return a.addEventListener(f,e),e}),(t=>{a.removeEventListener(f,t)}),n)};
1
+ "use strict";const t=t=>typeof t,a="",e=t(a),s=(t,a)=>t instanceof a,o=t=>null==t,n=(t,a,e)=>o(t)?e?.():a(t),r=a=>t(a)==e,c=t=>t.length,i=Object,y=i.keys,d=i.freeze,u=t=>(t=>s(t,i)&&t.constructor==i)(t)&&0==(t=>c(y(t)))(t),h=t=>JSON.stringify(t,((t,a)=>s(a,Map)?i.fromEntries([...a]):a)),v=JSON.parse,w="/store",p=t=>new Map(t),l=(t,a)=>t?.get(a),f=(t,a,e)=>{return o(e)?(s=t,n=a,s?.delete(n),t):t?.set(a,e);var s,n},g=(t,a,e)=>{var s,o;return s=t,o=a,s?.has(o)||f(t,a,e()),l(t,a)},m=p(),S=p(),A="message";exports.createPartyKitPersister=(t,e,s,i)=>{const{host:y,room:p}=e.partySocketOptions,{storeProtocol:L="https",storePath:P=w,messagePrefix:C=a}={...r(s)?{storeProtocol:s}:s},O=L+"://"+y+"/parties/"+(e.name??"main")+"/"+p+P,E=async t=>await(await fetch(O,{...t?{method:"PUT",body:h(t)}:{},mode:"cors",cache:"no-store"})).json();return((t,a,e,s,r,c,i=[])=>{let y,h,v,w=0,p=0;g(m,i,(()=>0)),g(S,i,(()=>[]));const A=async t=>(2!=w&&(w=1,await L.schedule((async()=>{await t(),w=0}))),L),L={load:async(e,s)=>await A((async()=>{try{t.setContent(await a())}catch{t.setContent([e,s])}})),startAutoLoad:async(e={},o={})=>(L.stopAutoLoad(),await L.load(e,o),p=1,v=s((async(e,s)=>{if(s){const a=s();await A((async()=>t.setTransactionChanges(a)))}else await A((async()=>{try{t.setContent(e?.()??await a())}catch(t){c?.(t)}}))})),L),stopAutoLoad:()=>(p&&(r(v),v=void 0,p=0),L),save:async a=>(1!=w&&(w=2,await L.schedule((async()=>{try{await e(t.getContent,a)}catch(t){c?.(t)}w=0}))),L),startAutoSave:async()=>(await L.stopAutoSave().save(),y=t.addDidFinishTransactionListener(((t,a)=>{const[e,s]=a();u(e)&&u(s)||L.save((()=>[e,s]))})),L),stopAutoSave:()=>(n(y,t.delListener),L),schedule:async(...t)=>(((t,...a)=>{t.push(...a)})(l(S,i),...t),await(async()=>{if(!l(m,i)){for(f(m,i,1);!o((t=l(S,i),h=t.shift()));)try{await h()}catch(t){c?.(t)}f(m,i,0)}var t})(),L),getStore:()=>t,destroy:()=>L.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return d(L)})(t,(async()=>await E()),(async(t,a)=>{var s,o;a?e.send((s=C,o=a(),s+"s"+(r(o)?o:h(o)))):await E(t())}),(t=>{const a=a=>n(((t,a,e)=>{const s=c(t);return((t,a)=>t.startsWith(a))(a,t)?[a[s],v((o=a,n=s+1,o.slice(n,void 0)))]:void 0;var o,n})(C,a.data),(([a,e])=>{"s"==a&&t(void 0,(()=>e))}));return e.addEventListener(A,a),a}),(t=>{e.removeEventListener(A,t)}),i)};
@@ -1 +1 @@
1
- "use strict";const t=t=>typeof t,s=t(""),e="t",a=Promise,i=t=>null==t,r=(t,s,e)=>t.slice(s,e),n=t=>t.length,o=(t,s)=>t.map(s),c=(t,...s)=>t.push(...s),h=Object,p=(t=[])=>h.fromEntries(t),f=(t,s)=>o(h.entries(t),(([t,e])=>s(e,t))),l=(t,s,e)=>(((t,s)=>!i(((t,s)=>{return a=t=>t[s],i(e=t)?void 0:a(e);var e,a})(t,s)))(t,s)||(t[s]=e()),t[s]),u=t=>JSON.stringify(t,((t,s)=>s instanceof Map?h.fromEntries([...s]):s)),y=JSON.parse,g=(t,s)=>[t[0],s?y(r(t,1)):r(t,1)],w=(t,s)=>((t,s)=>t?.forEach(s))(t,((t,e)=>s(e,t))),d="hasStore",R=p(o(["Origin","Methods","Headers"],(t=>["Access-Control-Allow-"+t,"*"]))),m=async(t,s)=>await t.get(s+d),v=async(t,s,r)=>{const o=[t.put(s+d,1)],h=[];f(r[0],((a,r)=>i(a)?((t,...s)=>t.unshift(...s))(h,s+P(e,r)):f(a,((a,n)=>i(a)?c(h,s+P(e,r,n)):f(a,((a,i)=>x(o,t,s+P(e,r,n,i),a))))))),f(r[1],((e,a)=>x(o,t,s+"v"+a,e))),0!=n(h)&&w(await t.list(),(s=>h.every((e=>!s.startsWith(e)||x(o,t,s)&&!1)))),await(async t=>a.all(t))(o)},P=(t,...s)=>t+r(u(s),1,-1),x=(t,s,e,a)=>c(t,i(a)?s.delete(e):s.put(e,a));exports.TinyBasePartyKitServer=class{constructor(t){this.party=t,this.config={},this.createResponse=(t,s=null)=>new Response(s,{status:t,headers:this.config.responseHeaders??R})}async onRequest(t){const s=this.party.storage,a=this.config.storagePrefix??"",i=this.config.storePath??"/store";if(new URL(t.url).pathname.endsWith(i)){const i=await m(s,a),o=await t.text();return"PUT"==t.method?i?this.createResponse(205):(await v(s,a,y(o)),this.createResponse(201)):this.createResponse(200,i?u(await(async(t,s)=>{const a={},i={};return w(await t.list(),((t,o)=>{if(t.startsWith(s)){t=r(t,n(s));const[c,h]=g(t);if(c==e){const[t,s,e]=y("["+h+"]");l(l(a,t,p),s,p)[e]=o}else"v"==c&&(i[h]=o)}})),[a,i]})(s,a)):"")}return this.createResponse(404)}async onMessage(e,a){const i=this.party.storage,r=this.config.storagePrefix??"",[n,o]=g(e,1);"s"==n&&await m(i,r)&&(await v(i,r,o),this.party.broadcast(((e,a)=>"s"+(t(a)==s?a:u(a)))(0,o),[a.id]))}};
1
+ "use strict";const t=t=>typeof t,a="",e=t(a),s="t",n=(t,a)=>t.startsWith(a),r=Promise,i=t=>null==t,c=(t,a,e)=>i(t)?e?.():a(t),o=(t,a,e)=>t.slice(a,e),l=t=>t.length,h=async t=>r.all(t),u=(t,a)=>t.map(a),w=(t,...a)=>t.push(...a),g=Object,y=(t=[])=>g.fromEntries(t),f=(t,a)=>u(g.entries(t),(([t,e])=>a(e,t))),p=(t,a,e)=>(((t,a)=>!i(((t,a)=>c(t,(t=>t[a])))(t,a)))(t,a)||(t[a]=e()),t[a]),S=t=>JSON.stringify(t,((t,a)=>a instanceof Map?g.fromEntries([...a]):a)),d=JSON.parse,D=(a,s,n)=>a+s+(t(n)==e?n:S(n)),P=(t,a,e)=>{const s=l(t);return n(a,t)?[a[s],(e?d:String)(o(a,s+1))]:void 0},m=(t,a)=>((t,a)=>t?.forEach(a))(t,((t,e)=>a(e,t))),R="hasStore",b=y(u(["Origin","Methods","Headers"],(t=>["Access-Control-Allow-"+t,"*"]))),x=async t=>await t.party.storage.get((t.config.storagePrefix??a)+R),T=async(t,e,r,c)=>{const o=t.party.storage,u=t.config.storagePrefix??a,g={[u+R]:1},y=[],p=[];await h(f(e[0],(async(a,e)=>i(a)?!r&&t.canDelTable(e,c)&&((t,...a)=>t.unshift(...a))(p,v(u,s,e)):t.canSetTable(e,r,c)&&await h(f(a,(async(a,n)=>i(a)?!r&&t.canDelRow(e,n,c)&&w(p,v(u,s,e,n)):t.canSetRow(e,n,r,c)&&await h(f(a,(async(a,l)=>{const h=[e,n,l],f=v(u,s,...h);i(a)?!r&&t.canDelCell(...h,c)&&w(y,f):t.canSetCell(...h,a,r,c,await o.get(f))&&(g[f]=a)}))))))))),await h(f(e[1],(async(a,e)=>{const s=u+"v"+e;i(a)?!r&&t.canDelValue(e,c)&&w(y,s):t.canSetValue(e,a,r,c,await o.get(s))&&(g[s]=a)}))),0!=l(p)&&m(await o.list(),(t=>p.every((a=>!n(t,a)||w(y,t)&&0)))),await o.delete(y),await o.put(g)},v=(t,a,...e)=>D(t,a,o(S(e),1,-1)),C=async(t,a,e=null)=>new Response(e,{status:a,headers:t.config.responseHeaders??b});exports.TinyBasePartyKitServer=class{constructor(t){this.party=t,this.config={}}async onRequest(t){const e=this.config.storePath??"/store";if(new URL(t.url).pathname.endsWith(e)){const e=await x(this),n=await t.text();return"PUT"==t.method?e?C(this,205):(await T(this,d(n),!0,t),C(this,201)):C(this,200,e?S(await(async t=>{const e={},n={},r=t.config.storagePrefix??a;return m(await t.party.storage.list(),((t,a)=>c(P(r,t),(([t,r])=>{if(t==s){const[t,s,n]=d("["+r+"]");p(p(e,t,y),s,y)[n]=a}else"v"==t&&(n[r]=a)})))),[e,n]})(this)):a)}return C(this,404)}async onMessage(t,e){const s=this.config.messagePrefix??a;await c(P(s,t,1),(async([t,a])=>{"s"==t&&await x(this)&&(await T(this,a,!1,e),this.party.broadcast(D(s,"s",a)))}))}canSetTable(t,a,e){return!0}canDelTable(t,a){return!0}canSetRow(t,a,e,s){return!0}canDelRow(t,a,e){return!0}canSetCell(t,a,e,s,n,r,i){return!0}canDelCell(t,a,e,s){return!0}canSetValue(t,a,e,s,n){return!0}canDelValue(t,a){return!0}};
@@ -1 +1 @@
1
- "use strict";const t=t=>typeof t,e=t(""),n=(t,e)=>t instanceof e,o=t=>null==t,r=n=>t(n)==e,l=Object,i=l.keys,s=l.freeze,u=t=>(t=>n(t,l)&&t.constructor==l)(t)&&0==(t=>i(t).length)(t),a=t=>JSON.stringify(t,((t,e)=>n(e,Map)?l.fromEntries([...e]):e)),c=JSON.parse,d=t=>new Map(t),v=(t,e)=>null==t?void 0:t.get(e),y=(t,e,n)=>{return o(n)?(l=e,null==(r=t)||r.delete(l),t):null==t?void 0:t.set(e,n);var r,l},f=(t,e,n)=>{var o,r,l;return r=e,null!=(l=null==(o=t)?void 0:o.has(r))&&l||y(t,e,n()),v(t,e)};var p=(t,e,n)=>new Promise(((o,r)=>{var l=t=>{try{s(n.next(t))}catch(t){r(t)}},i=t=>{try{s(n.throw(t))}catch(t){r(t)}},s=t=>t.done?o(t.value):Promise.resolve(t.value).then(l,i);s((n=n.apply(t,e)).next())}));const h=d(),P=d();var m=Object.defineProperty,b=Object.defineProperties,O=Object.getOwnPropertyDescriptors,g=Object.getOwnPropertySymbols,S=Object.prototype.hasOwnProperty,w=Object.prototype.propertyIsEnumerable,j=(t,e,n)=>e in t?m(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,A=(t,e)=>{for(var n in e||(e={}))S.call(e,n)&&j(t,n,e[n]);if(g)for(var n of g(e))w.call(e,n)&&j(t,n,e[n]);return t},L=(t,e,n)=>new Promise(((o,r)=>{var l=t=>{try{s(n.next(t))}catch(t){r(t)}},i=t=>{try{s(n.throw(t))}catch(t){r(t)}},s=t=>t.done?o(t.value):Promise.resolve(t.value).then(l,i);s((n=n.apply(t,e)).next())}));const x="message",C={storeProtocol:"https",storePath:"/store"};exports.createPartyKitPersister=(t,e,n,l)=>{var i;const{host:d,room:m}=e.partySocketOptions,{storeProtocol:g,storePath:S}=A(A({},C),r(n)?{storeProtocol:n}:n),w=g+"://"+d+"/parties/"+(null!=(i=e.name)?i:"main")+"/"+m+S,j=t=>L(void 0,null,(function*(){return yield(yield fetch(w,(e=A({},t?{method:"PUT",body:a(t)}:{}),n={mode:"cors",cache:"no-store"},b(e,O(n))))).json();var e,n}));return((t,e,n,r,l,i,a=[])=>{let c,d,m,b=0,O=0;f(h,a,(()=>0)),f(P,a,(()=>[]));const g=t=>p(void 0,null,(function*(){return 2!=b&&(b=1,yield S.schedule((()=>p(void 0,null,(function*(){yield t(),b=0}))))),S})),S={load:(n,o)=>p(void 0,null,(function*(){return yield g((()=>p(void 0,null,(function*(){try{t.setContent(yield e())}catch(e){t.setContent([n,o])}}))))})),startAutoLoad:(...n)=>p(void 0,[...n],(function*(n={},o={}){return S.stopAutoLoad(),yield S.load(n,o),O=1,m=r(((n,o)=>p(void 0,null,(function*(){if(o){const e=o();yield g((()=>p(void 0,null,(function*(){return t.setTransactionChanges(e)}))))}else yield g((()=>p(void 0,null,(function*(){var o;try{t.setContent(null!=(o=null==n?void 0:n())?o:yield e())}catch(t){null==i||i(t)}}))))})))),S})),stopAutoLoad:()=>(O&&(l(m),m=void 0,O=0),S),save:e=>p(void 0,null,(function*(){return 1!=b&&(b=2,yield S.schedule((()=>p(void 0,null,(function*(){try{yield n(t.getContent,e)}catch(t){null==i||i(t)}b=0}))))),S})),startAutoSave:()=>p(void 0,null,(function*(){return yield S.stopAutoSave().save(),c=t.addDidFinishTransactionListener(((t,e)=>{const[n,o]=e();u(n)&&u(o)||S.save((()=>[n,o]))})),S})),stopAutoSave:()=>{var e,n;return e=c,n=t.delListener,!!o(e)||n(e),S},schedule:(...t)=>p(void 0,null,(function*(){return((t,...e)=>{t.push(...e)})(v(P,a),...t),yield p(void 0,null,(function*(){if(!v(h,a)){for(y(h,a,1);!o((t=v(P,a),d=t.shift()));)try{yield d()}catch(t){null==i||i(t)}y(h,a,0)}var t})),S})),getStore:()=>t,destroy:()=>S.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return s(S)})(t,(()=>L(void 0,null,(function*(){return yield j()}))),((t,n)=>L(void 0,null,(function*(){var o;n?e.send((o=n(),"s"+(r(o)?o:a(o)))):yield j(t())}))),(t=>{const n=e=>{const[n,o]=[(s=e.data)[0],c((r=s,l=1,r.slice(l,i)))];var r,l,i,s;"s"==n&&t(void 0,(()=>o))};return e.addEventListener(x,n),n}),(t=>{e.removeEventListener(x,t)}),l)};
1
+ "use strict";const t=t=>typeof t,e="",n=t(e),o=(t,e)=>t instanceof e,r=t=>null==t,l=(t,e,n)=>r(t)?null==n?void 0:n():e(t),i=e=>t(e)==n,s=t=>t.length,u=Object,a=u.keys,c=u.freeze,d=t=>(t=>o(t,u)&&t.constructor==u)(t)&&0==(t=>s(a(t)))(t),v=t=>JSON.stringify(t,((t,e)=>o(e,Map)?u.fromEntries([...e]):e)),y=JSON.parse,f="/store",p=t=>new Map(t),h=(t,e)=>null==t?void 0:t.get(e),m=(t,e,n)=>{return r(n)?(l=e,null==(o=t)||o.delete(l),t):null==t?void 0:t.set(e,n);var o,l},P=(t,e,n)=>{var o,r,l;return r=e,null!=(l=null==(o=t)?void 0:o.has(r))&&l||m(t,e,n()),h(t,e)};var b=(t,e,n)=>new Promise(((o,r)=>{var l=t=>{try{s(n.next(t))}catch(t){r(t)}},i=t=>{try{s(n.throw(t))}catch(t){r(t)}},s=t=>t.done?o(t.value):Promise.resolve(t.value).then(l,i);s((n=n.apply(t,e)).next())}));const O=p(),g=p();var S=Object.defineProperty,w=Object.defineProperties,j=Object.getOwnPropertyDescriptors,A=Object.getOwnPropertySymbols,L=Object.prototype.hasOwnProperty,x=Object.prototype.propertyIsEnumerable,C=(t,e,n)=>e in t?S(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,E=(t,e)=>{for(var n in e||(e={}))L.call(e,n)&&C(t,n,e[n]);if(A)for(var n of A(e))x.call(e,n)&&C(t,n,e[n]);return t},T=(t,e,n)=>new Promise(((o,r)=>{var l=t=>{try{s(n.next(t))}catch(t){r(t)}},i=t=>{try{s(n.throw(t))}catch(t){r(t)}},s=t=>t.done?o(t.value):Promise.resolve(t.value).then(l,i);s((n=n.apply(t,e)).next())}));const k="message";exports.createPartyKitPersister=(t,n,o,u)=>{var a;const{host:p,room:S}=n.partySocketOptions,{storeProtocol:A="https",storePath:L=f,messagePrefix:x=e}=E({},i(o)?{storeProtocol:o}:o),C=A+"://"+p+"/parties/"+(null!=(a=n.name)?a:"main")+"/"+S+L,D=t=>T(void 0,null,(function*(){return yield(yield fetch(C,(e=E({},t?{method:"PUT",body:v(t)}:{}),n={mode:"cors",cache:"no-store"},w(e,j(n))))).json();var e,n}));return((t,e,n,o,i,s,u=[])=>{let a,v,y,f=0,p=0;P(O,u,(()=>0)),P(g,u,(()=>[]));const S=t=>b(void 0,null,(function*(){return 2!=f&&(f=1,yield w.schedule((()=>b(void 0,null,(function*(){yield t(),f=0}))))),w})),w={load:(n,o)=>b(void 0,null,(function*(){return yield S((()=>b(void 0,null,(function*(){try{t.setContent(yield e())}catch(e){t.setContent([n,o])}}))))})),startAutoLoad:(...n)=>b(void 0,[...n],(function*(n={},r={}){return w.stopAutoLoad(),yield w.load(n,r),p=1,y=o(((n,o)=>b(void 0,null,(function*(){if(o){const e=o();yield S((()=>b(void 0,null,(function*(){return t.setTransactionChanges(e)}))))}else yield S((()=>b(void 0,null,(function*(){var o;try{t.setContent(null!=(o=null==n?void 0:n())?o:yield e())}catch(t){null==s||s(t)}}))))})))),w})),stopAutoLoad:()=>(p&&(i(y),y=void 0,p=0),w),save:e=>b(void 0,null,(function*(){return 1!=f&&(f=2,yield w.schedule((()=>b(void 0,null,(function*(){try{yield n(t.getContent,e)}catch(t){null==s||s(t)}f=0}))))),w})),startAutoSave:()=>b(void 0,null,(function*(){return yield w.stopAutoSave().save(),a=t.addDidFinishTransactionListener(((t,e)=>{const[n,o]=e();d(n)&&d(o)||w.save((()=>[n,o]))})),w})),stopAutoSave:()=>(l(a,t.delListener),w),schedule:(...t)=>b(void 0,null,(function*(){return((t,...e)=>{t.push(...e)})(h(g,u),...t),yield b(void 0,null,(function*(){if(!h(O,u)){for(m(O,u,1);!r((t=h(g,u),v=t.shift()));)try{yield v()}catch(t){null==s||s(t)}m(O,u,0)}var t})),w})),getStore:()=>t,destroy:()=>w.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return c(w)})(t,(()=>T(void 0,null,(function*(){return yield D()}))),((t,e)=>T(void 0,null,(function*(){var o,r;e?n.send((o=x,r=e(),o+"s"+(i(r)?r:v(r)))):yield D(t())}))),(t=>{const e=e=>l(((t,e,n)=>{const o=s(t);return((t,e)=>t.startsWith(e))(e,t)?[e[o],y((r=e,l=o+1,r.slice(l,void 0)))]:void 0;var r,l})(x,e.data),(([e,n])=>{"s"==e&&t(void 0,(()=>n))}));return n.addEventListener(k,e),e}),(t=>{n.removeEventListener(k,t)}),u)};
@@ -1 +1 @@
1
- "use strict";const e=e=>typeof e,t=e(""),s="t",r=Promise,n=e=>null==e,i=(e,t,s)=>e.slice(t,s),o=e=>e.length,l=(e,t)=>e.map(t),a=(e,...t)=>e.push(...t),c=Object,u=(e=[])=>c.fromEntries(e),h=(e,t)=>l(c.entries(e),(([e,s])=>t(s,e))),d=(e,t,s)=>(((e,t)=>!n(((e,t)=>{return r=e=>e[t],n(s=e)?void 0:r(s);var s,r})(e,t)))(e,t)||(e[t]=s()),e[t]),y=e=>JSON.stringify(e,((e,t)=>t instanceof Map?c.fromEntries([...t]):t)),p=JSON.parse,v=(e,t)=>[e[0],t?p(i(e,1)):i(e,1)],f=(e,t)=>((e,t)=>null==e?void 0:e.forEach(t))(e,((e,s)=>t(s,e)));var g=(e,t,s)=>new Promise(((r,n)=>{var i=e=>{try{l(s.next(e))}catch(e){n(e)}},o=e=>{try{l(s.throw(e))}catch(e){n(e)}},l=e=>e.done?r(e.value):Promise.resolve(e.value).then(i,o);l((s=s.apply(e,t)).next())}));const m="hasStore",P=u(l(["Origin","Methods","Headers"],(e=>["Access-Control-Allow-"+e,"*"]))),x=(e,t)=>g(void 0,null,(function*(){return yield e.get(t+m)})),R=(e,t,i)=>g(void 0,null,(function*(){const l=[e.put(t+m,1)],c=[];h(i[0],((r,i)=>n(r)?((e,...t)=>e.unshift(...t))(c,t+w(s,i)):h(r,((r,o)=>n(r)?a(c,t+w(s,i,o)):h(r,((r,n)=>O(l,e,t+w(s,i,o,n),r))))))),h(i[1],((s,r)=>O(l,e,t+"v"+r,s))),0!=o(c)&&f(yield e.list(),(t=>c.every((s=>!t.startsWith(s)||O(l,e,t)&&!1)))),yield(e=>{return t=function*(){return r.all(e)},new Promise(((e,s)=>{var r=e=>{try{i(t.next(e))}catch(e){s(e)}},n=e=>{try{i(t.throw(e))}catch(e){s(e)}},i=t=>t.done?e(t.value):Promise.resolve(t.value).then(r,n);i((t=t.apply(void 0,null)).next())}));var t})(l)})),w=(e,...t)=>e+i(y(t),1,-1),O=(e,t,s,r)=>a(e,n(r)?t.delete(s):t.put(s,r));exports.TinyBasePartyKitServer=class{constructor(e){this.party=e,this.config={},this.createResponse=(e,t=null)=>{var s;return new Response(t,{status:e,headers:null!=(s=this.config.responseHeaders)?s:P})}}onRequest(e){return g(this,null,(function*(){var t,r;const n=this.party.storage,l=null!=(t=this.config.storagePrefix)?t:"",a=null!=(r=this.config.storePath)?r:"/store";if(new URL(e.url).pathname.endsWith(a)){const t=yield x(n,l),r=yield e.text();return"PUT"==e.method?t?this.createResponse(205):(yield R(n,l,p(r)),this.createResponse(201)):this.createResponse(200,t?y(yield((e,t)=>g(void 0,null,(function*(){const r={},n={};return f(yield e.list(),((e,l)=>{if(e.startsWith(t)){e=i(e,o(t));const[a,c]=v(e);if(a==s){const[e,t,s]=p("["+c+"]");d(d(r,e,u),t,u)[s]=l}else"v"==a&&(n[c]=l)}})),[r,n]})))(n,l)):"")}return this.createResponse(404)}))}onMessage(s,r){return g(this,null,(function*(){var n;const i=this.party.storage,o=null!=(n=this.config.storagePrefix)?n:"",[l,a]=v(s,1);"s"==l&&(yield x(i,o))&&(yield R(i,o,a),this.party.broadcast(((s,r)=>"s"+(e(r)==t?r:y(r)))(0,a),[r.id]))}))}};
1
+ "use strict";const e=e=>typeof e,t="",n=e(t),r="t",l=(e,t)=>e.startsWith(t),i=Promise,s=e=>null==e,o=(e,t,n)=>s(e)?null==n?void 0:n():t(e),a=(e,t,n)=>e.slice(t,n),u=e=>e.length,c=e=>{return t=function*(){return i.all(e)},new Promise(((e,n)=>{var r=e=>{try{i(t.next(e))}catch(e){n(e)}},l=e=>{try{i(t.throw(e))}catch(e){n(e)}},i=t=>t.done?e(t.value):Promise.resolve(t.value).then(r,l);i((t=t.apply(void 0,null)).next())}));var t},d=(e,t)=>e.map(t),h=(e,...t)=>e.push(...t),y=Object,v=(e=[])=>y.fromEntries(e),f=(e,t)=>d(y.entries(e),(([e,n])=>t(n,e))),g=(e,t,n)=>(((e,t)=>!s(((e,t)=>o(e,(e=>e[t])))(e,t)))(e,t)||(e[t]=n()),e[t]),p=e=>JSON.stringify(e,((e,t)=>t instanceof Map?y.fromEntries([...t]):t)),S=JSON.parse,P=(t,r,l)=>t+r+(e(l)==n?l:p(l)),m=(e,t,n)=>{const r=u(e);return l(t,e)?[t[r],(n?S:String)(a(t,r+1))]:void 0},w=(e,t)=>((e,t)=>null==e?void 0:e.forEach(t))(e,((e,n)=>t(n,e)));var x=(e,t,n)=>new Promise(((r,l)=>{var i=e=>{try{o(n.next(e))}catch(e){l(e)}},s=e=>{try{o(n.throw(e))}catch(e){l(e)}},o=e=>e.done?r(e.value):Promise.resolve(e.value).then(i,s);o((n=n.apply(e,t)).next())}));const D="hasStore",R=v(d(["Origin","Methods","Headers"],(e=>["Access-Control-Allow-"+e,"*"]))),b=e=>x(void 0,null,(function*(){var n;return yield e.party.storage.get((null!=(n=e.config.storagePrefix)?n:t)+D)})),T=(e,n,i,o)=>x(void 0,null,(function*(){var a;const d=e.party.storage,y=null!=(a=e.config.storagePrefix)?a:t,v={[y+D]:1},g=[],p=[];yield c(f(n[0],((t,n)=>x(void 0,null,(function*(){return s(t)?!i&&e.canDelTable(n,o)&&((e,...t)=>e.unshift(...t))(p,C(y,r,n)):e.canSetTable(n,i,o)&&(yield c(f(t,((t,l)=>x(void 0,null,(function*(){return s(t)?!i&&e.canDelRow(n,l,o)&&h(p,C(y,r,n,l)):e.canSetRow(n,l,i,o)&&(yield c(f(t,((t,a)=>x(void 0,null,(function*(){const u=[n,l,a],c=C(y,r,...u);s(t)?!i&&e.canDelCell(...u,o)&&h(g,c):e.canSetCell(...u,t,i,o,yield d.get(c))&&(v[c]=t)}))))))}))))))}))))),yield c(f(n[1],((t,n)=>x(void 0,null,(function*(){const r=y+"v"+n;s(t)?!i&&e.canDelValue(n,o)&&h(g,r):e.canSetValue(n,t,i,o,yield d.get(r))&&(v[r]=t)}))))),0!=u(p)&&w(yield d.list(),(e=>p.every((t=>!l(e,t)||h(g,e)&&0)))),yield d.delete(g),yield d.put(v)})),C=(e,t,...n)=>P(e,t,a(p(n),1,-1)),O=(e,t,n=null)=>x(void 0,null,(function*(){var r;return new Response(n,{status:t,headers:null!=(r=e.config.responseHeaders)?r:R})}));exports.TinyBasePartyKitServer=class{constructor(e){this.party=e,this.config={}}onRequest(e){return x(this,null,(function*(){var n;const l=null!=(n=this.config.storePath)?n:"/store";if(new URL(e.url).pathname.endsWith(l)){const n=yield b(this),l=yield e.text();return"PUT"==e.method?n?O(this,205):(yield T(this,S(l),!0,e),O(this,201)):O(this,200,n?p(yield(i=this,x(void 0,null,(function*(){var e;const n={},l={},s=null!=(e=i.config.storagePrefix)?e:t;return w(yield i.party.storage.list(),((e,t)=>o(m(s,e),(([e,i])=>{if(e==r){const[e,r,l]=S("["+i+"]");g(g(n,e,v),r,v)[l]=t}else"v"==e&&(l[i]=t)})))),[n,l]})))):t)}var i;return O(this,404)}))}onMessage(e,n){return x(this,null,(function*(){var r;const l=null!=(r=this.config.messagePrefix)?r:t;yield o(m(l,e,1),(e=>x(this,[e],(function*([e,t]){"s"==e&&(yield b(this))&&(yield T(this,t,!1,n),this.party.broadcast(P(l,"s",t)))}))))}))}canSetTable(e,t,n){return!0}canDelTable(e,t){return!0}canSetRow(e,t,n,r){return!0}canDelRow(e,t,n){return!0}canSetCell(e,t,n,r,l,i,s){return!0}canDelCell(e,t,n,r){return!0}canSetValue(e,t,n,r,l){return!0}canDelValue(e,t){return!0}};
@@ -1,6 +1,7 @@
1
1
  const getTypeOf = (thing) => typeof thing;
2
2
  const EMPTY_STRING = '';
3
3
  const STRING = getTypeOf(EMPTY_STRING);
4
+ const strStartsWith = (str, prefix) => str.startsWith(prefix);
4
5
 
5
6
  const isInstanceOf = (thing, cls) => thing instanceof cls;
6
7
  const isUndefined = (thing) => thing == void 0;
@@ -30,12 +31,17 @@ const jsonParse = JSON.parse;
30
31
  const SET_CHANGES = 's';
31
32
  const STORE_PATH = '/store';
32
33
  const PUT = 'PUT';
33
- const constructMessage = (type, payload) =>
34
- type + (isString(payload) ? payload : jsonString(payload));
35
- const deconstructMessage = (message, stringified) => [
36
- message[0],
37
- stringified ? jsonParse(slice(message, 1)) : slice(message, 1),
38
- ];
34
+ const construct = (prefix, type, payload) =>
35
+ prefix + type + (isString(payload) ? payload : jsonString(payload));
36
+ const deconstruct = (prefix, message, stringified) => {
37
+ const prefixSize = size(prefix);
38
+ return strStartsWith(message, prefix)
39
+ ? [
40
+ message[prefixSize],
41
+ (stringified ? jsonParse : String)(slice(message, prefixSize + 1)),
42
+ ]
43
+ : void 0;
44
+ };
39
45
 
40
46
  const collHas = (coll, keyOrValue) => coll?.has(keyOrValue) ?? false;
41
47
  const collDel = (coll, keyOrValue) => coll?.delete(keyOrValue);
@@ -191,10 +197,6 @@ const createCustomPersister = (
191
197
  };
192
198
 
193
199
  const MESSAGE = 'message';
194
- const DEFAULT_CONFIG = {
195
- storeProtocol: 'https',
196
- storePath: STORE_PATH,
197
- };
198
200
  const createPartyKitPersister = (
199
201
  store,
200
202
  connection,
@@ -202,8 +204,11 @@ const createPartyKitPersister = (
202
204
  onIgnoredError,
203
205
  ) => {
204
206
  const {host, room} = connection.partySocketOptions;
205
- const {storeProtocol, storePath} = {
206
- ...DEFAULT_CONFIG,
207
+ const {
208
+ storeProtocol = 'https',
209
+ storePath = STORE_PATH,
210
+ messagePrefix = EMPTY_STRING,
211
+ } = {
207
212
  ...(isString(configOrStoreProtocol)
208
213
  ? {storeProtocol: configOrStoreProtocol}
209
214
  : configOrStoreProtocol),
@@ -228,18 +233,23 @@ const createPartyKitPersister = (
228
233
  const getPersisted = async () => await getOrSetStore();
229
234
  const setPersisted = async (getContent, getTransactionChanges) => {
230
235
  if (getTransactionChanges) {
231
- connection.send(constructMessage(SET_CHANGES, getTransactionChanges()));
236
+ connection.send(
237
+ construct(messagePrefix, SET_CHANGES, getTransactionChanges()),
238
+ );
232
239
  } else {
233
240
  await getOrSetStore(getContent());
234
241
  }
235
242
  };
236
243
  const addPersisterListener = (listener) => {
237
- const messageListener = (event) => {
238
- const [type, payload] = deconstructMessage(event.data, 1);
239
- if (type == SET_CHANGES) {
240
- listener(void 0, () => payload);
241
- }
242
- };
244
+ const messageListener = (event) =>
245
+ ifNotUndefined(
246
+ deconstruct(messagePrefix, event.data, 1),
247
+ ([type, payload]) => {
248
+ if (type == SET_CHANGES) {
249
+ listener(void 0, () => payload);
250
+ }
251
+ },
252
+ );
243
253
  connection.addEventListener(MESSAGE, messageListener);
244
254
  return messageListener;
245
255
  };
@@ -3,6 +3,7 @@ const EMPTY_STRING = '';
3
3
  const STRING = getTypeOf(EMPTY_STRING);
4
4
  const T = 't';
5
5
  const V = 'v';
6
+ const strStartsWith = (str, prefix) => str.startsWith(prefix);
6
7
 
7
8
  const promise = Promise;
8
9
  const isInstanceOf = (thing, cls) => thing instanceof cls;
@@ -42,12 +43,17 @@ const jsonParse = JSON.parse;
42
43
  const SET_CHANGES = 's';
43
44
  const STORE_PATH = '/store';
44
45
  const PUT = 'PUT';
45
- const constructMessage = (type, payload) =>
46
- type + (isString(payload) ? payload : jsonString(payload));
47
- const deconstructMessage = (message, stringified) => [
48
- message[0],
49
- stringified ? jsonParse(slice(message, 1)) : slice(message, 1),
50
- ];
46
+ const construct = (prefix, type, payload) =>
47
+ prefix + type + (isString(payload) ? payload : jsonString(payload));
48
+ const deconstruct = (prefix, message, stringified) => {
49
+ const prefixSize = size(prefix);
50
+ return strStartsWith(message, prefix)
51
+ ? [
52
+ message[prefixSize],
53
+ (stringified ? jsonParse : String)(slice(message, prefixSize + 1)),
54
+ ]
55
+ : void 0;
56
+ };
51
57
 
52
58
  const collForEach = (coll, cb) => coll?.forEach(cb);
53
59
 
@@ -61,15 +67,16 @@ const RESPONSE_HEADERS = objNew(
61
67
  '*',
62
68
  ]),
63
69
  );
64
- const hasStoreInStorage = async (storage, prefix) =>
65
- await storage.get(prefix + HAS_STORE);
66
- const loadStoreFromStorage = async (storage, prefix) => {
70
+ const hasStore = async (that) =>
71
+ await that.party.storage.get(
72
+ (that.config.storagePrefix ?? EMPTY_STRING) + HAS_STORE,
73
+ );
74
+ const loadStore = async (that) => {
67
75
  const tables = {};
68
76
  const values = {};
69
- mapForEach(await storage.list(), (key, cellOrValue) => {
70
- if (key.startsWith(prefix)) {
71
- key = slice(key, size(prefix));
72
- const [type, ids] = deconstructMessage(key);
77
+ const storagePrefix = that.config.storagePrefix ?? EMPTY_STRING;
78
+ mapForEach(await that.party.storage.list(), (key, cellOrValue) =>
79
+ ifNotUndefined(deconstruct(storagePrefix, key), ([type, ids]) => {
73
80
  if (type == T) {
74
81
  const [tableId, rowId, cellId] = jsonParse('[' + ids + ']');
75
82
  objEnsure(objEnsure(tables, tableId, objNew), rowId, objNew)[cellId] =
@@ -77,98 +84,173 @@ const loadStoreFromStorage = async (storage, prefix) => {
77
84
  } else if (type == V) {
78
85
  values[ids] = cellOrValue;
79
86
  }
80
- }
81
- });
87
+ }),
88
+ );
82
89
  return [tables, values];
83
90
  };
84
- const saveStoreToStorage = async (storage, prefix, transactionChanges) => {
85
- const promises = [storage.put(prefix + HAS_STORE, 1)];
86
- const keyPrefixesToDelete = [];
87
- objMap(transactionChanges[0], (table, tableId) =>
88
- isUndefined(table)
89
- ? arrayUnshift(
90
- keyPrefixesToDelete,
91
- prefix + getStoreStorageKey(T, tableId),
92
- )
93
- : objMap(table, (row, rowId) =>
94
- isUndefined(row)
95
- ? arrayPush(
96
- keyPrefixesToDelete,
97
- prefix + getStoreStorageKey(T, tableId, rowId),
98
- )
99
- : objMap(row, (cell, cellId) =>
100
- promiseToSetOrDelStorage(
101
- promises,
102
- storage,
103
- prefix + getStoreStorageKey(T, tableId, rowId, cellId),
104
- cell,
105
- ),
106
- ),
107
- ),
91
+ const saveStore = async (
92
+ that,
93
+ transactionChanges,
94
+ initialSave,
95
+ requestOrConnection,
96
+ ) => {
97
+ const storage = that.party.storage;
98
+ const prefix = that.config.storagePrefix ?? EMPTY_STRING;
99
+ const keysToSet = {
100
+ [prefix + HAS_STORE]: 1,
101
+ };
102
+ const keysToDel = [];
103
+ const keyPrefixesToDel = [];
104
+ await promiseAll(
105
+ objMap(transactionChanges[0], async (table, tableId) =>
106
+ isUndefined(table)
107
+ ? !initialSave &&
108
+ that.canDelTable(tableId, requestOrConnection) &&
109
+ arrayUnshift(
110
+ keyPrefixesToDel,
111
+ constructStorageKey(prefix, T, tableId),
112
+ )
113
+ : that.canSetTable(tableId, initialSave, requestOrConnection) &&
114
+ (await promiseAll(
115
+ objMap(table, async (row, rowId) =>
116
+ isUndefined(row)
117
+ ? !initialSave &&
118
+ that.canDelRow(tableId, rowId, requestOrConnection) &&
119
+ arrayPush(
120
+ keyPrefixesToDel,
121
+ constructStorageKey(prefix, T, tableId, rowId),
122
+ )
123
+ : that.canSetRow(
124
+ tableId,
125
+ rowId,
126
+ initialSave,
127
+ requestOrConnection,
128
+ ) &&
129
+ (await promiseAll(
130
+ objMap(row, async (cell, cellId) => {
131
+ const ids = [tableId, rowId, cellId];
132
+ const key = constructStorageKey(prefix, T, ...ids);
133
+ isUndefined(cell)
134
+ ? !initialSave &&
135
+ that.canDelCell(...ids, requestOrConnection) &&
136
+ arrayPush(keysToDel, key)
137
+ : that.canSetCell(
138
+ ...ids,
139
+ cell,
140
+ initialSave,
141
+ requestOrConnection,
142
+ await storage.get(key),
143
+ ) && (keysToSet[key] = cell);
144
+ }),
145
+ )),
146
+ ),
147
+ )),
148
+ ),
108
149
  );
109
- objMap(transactionChanges[1], (value, valueId) =>
110
- promiseToSetOrDelStorage(promises, storage, prefix + V + valueId, value),
150
+ await promiseAll(
151
+ objMap(transactionChanges[1], async (value, valueId) => {
152
+ const key = prefix + V + valueId;
153
+ isUndefined(value)
154
+ ? !initialSave &&
155
+ that.canDelValue(valueId, requestOrConnection) &&
156
+ arrayPush(keysToDel, key)
157
+ : that.canSetValue(
158
+ valueId,
159
+ value,
160
+ initialSave,
161
+ requestOrConnection,
162
+ await storage.get(key),
163
+ ) && (keysToSet[key] = value);
164
+ }),
111
165
  );
112
- if (!arrayIsEmpty(keyPrefixesToDelete)) {
166
+ if (!arrayIsEmpty(keyPrefixesToDel)) {
113
167
  mapForEach(await storage.list(), (key) =>
114
168
  arrayEvery(
115
- keyPrefixesToDelete,
169
+ keyPrefixesToDel,
116
170
  (keyPrefixToDelete) =>
117
- !key.startsWith(keyPrefixToDelete) ||
118
- (promiseToSetOrDelStorage(promises, storage, key) && false),
171
+ !strStartsWith(key, keyPrefixToDelete) ||
172
+ (arrayPush(keysToDel, key) && 0),
119
173
  ),
120
174
  );
121
175
  }
122
- await promiseAll(promises);
176
+ await storage.delete(keysToDel);
177
+ await storage.put(keysToSet);
123
178
  };
124
- const getStoreStorageKey = (type, ...ids) =>
125
- type + slice(jsonString(ids), 1, -1);
126
- const promiseToSetOrDelStorage = (promises, storage, key, value) =>
127
- arrayPush(
128
- promises,
129
- isUndefined(value) ? storage.delete(key) : storage.put(key, value),
130
- );
179
+ const constructStorageKey = (storagePrefix, type, ...ids) =>
180
+ construct(storagePrefix, type, slice(jsonString(ids), 1, -1));
181
+ const createResponse = async (that, status, body = null) =>
182
+ new Response(body, {
183
+ status,
184
+ headers: that.config.responseHeaders ?? RESPONSE_HEADERS,
185
+ });
131
186
  class TinyBasePartyKitServer {
132
187
  constructor(party) {
133
188
  this.party = party;
134
189
  this.config = {};
135
- this.createResponse = (status, body = null) =>
136
- new Response(body, {
137
- status,
138
- headers: this.config.responseHeaders ?? RESPONSE_HEADERS,
139
- });
140
190
  }
141
191
  async onRequest(request) {
142
- const storage = this.party.storage;
143
- const prefix = this.config.storagePrefix ?? EMPTY_STRING;
144
192
  const storePath = this.config.storePath ?? STORE_PATH;
145
193
  if (new URL(request.url).pathname.endsWith(storePath)) {
146
- const hasStore = await hasStoreInStorage(storage, prefix);
194
+ const hasExistingStore = await hasStore(this);
147
195
  const text = await request.text();
148
196
  if (request.method == PUT) {
149
- if (hasStore) {
150
- return this.createResponse(205);
197
+ if (hasExistingStore) {
198
+ return createResponse(this, 205);
151
199
  }
152
- await saveStoreToStorage(storage, prefix, jsonParse(text));
153
- return this.createResponse(201);
200
+ await saveStore(this, jsonParse(text), true, request);
201
+ return createResponse(this, 201);
154
202
  }
155
- return this.createResponse(
203
+ return createResponse(
204
+ this,
156
205
  200,
157
- hasStore
158
- ? jsonString(await loadStoreFromStorage(storage, prefix))
159
- : EMPTY_STRING,
206
+ hasExistingStore ? jsonString(await loadStore(this)) : EMPTY_STRING,
160
207
  );
161
208
  }
162
- return this.createResponse(404);
209
+ return createResponse(this, 404);
163
210
  }
164
- async onMessage(message, client) {
165
- const storage = this.party.storage;
166
- const prefix = this.config.storagePrefix ?? EMPTY_STRING;
167
- const [type, payload] = deconstructMessage(message, 1);
168
- if (type == SET_CHANGES && (await hasStoreInStorage(storage, prefix))) {
169
- await saveStoreToStorage(storage, prefix, payload);
170
- this.party.broadcast(constructMessage(SET_CHANGES, payload), [client.id]);
171
- }
211
+ async onMessage(message, connection) {
212
+ const messagePrefix = this.config.messagePrefix ?? EMPTY_STRING;
213
+ await ifNotUndefined(
214
+ deconstruct(messagePrefix, message, 1),
215
+ async ([type, payload]) => {
216
+ if (type == SET_CHANGES && (await hasStore(this))) {
217
+ await saveStore(this, payload, false, connection);
218
+ this.party.broadcast(construct(messagePrefix, SET_CHANGES, payload));
219
+ }
220
+ },
221
+ );
222
+ }
223
+ canSetTable(_tableId, _initialSave, _requestOrConnection) {
224
+ return true;
225
+ }
226
+ canDelTable(_tableId, _connection) {
227
+ return true;
228
+ }
229
+ canSetRow(_tableId, _rowId, _initialSave, _requestOrConnection) {
230
+ return true;
231
+ }
232
+ canDelRow(_tableId, _rowId, _connection) {
233
+ return true;
234
+ }
235
+ canSetCell(
236
+ _tableId,
237
+ _rowId,
238
+ _cellId,
239
+ _cell,
240
+ _initialSave,
241
+ _requestOrConnection,
242
+ _oldCell,
243
+ ) {
244
+ return true;
245
+ }
246
+ canDelCell(_tableId, _rowId, _cellId, _connection) {
247
+ return true;
248
+ }
249
+ canSetValue(_valueId, _value, _initialSave, _requestOrConnection, _oldValue) {
250
+ return true;
251
+ }
252
+ canDelValue(_valueId, _connection) {
253
+ return true;
172
254
  }
173
255
  }
174
256
 
@@ -1 +1 @@
1
- const t=t=>typeof t,e=t(""),n=(t,e)=>t instanceof e,o=t=>null==t,r=n=>t(n)==e,l=Object,i=l.keys,u=l.freeze,s=t=>(t=>n(t,l)&&t.constructor==l)(t)&&0==(t=>i(t).length)(t),a=t=>JSON.stringify(t,((t,e)=>n(e,Map)?l.fromEntries([...e]):e)),c=JSON.parse,d=t=>new Map(t),v=(t,e)=>null==t?void 0:t.get(e),y=(t,e,n)=>{return o(n)?(l=e,null==(r=t)||r.delete(l),t):null==t?void 0:t.set(e,n);var r,l},f=(t,e,n)=>{var o,r,l;return r=e,null!=(l=null==(o=t)?void 0:o.has(r))&&l||y(t,e,n()),v(t,e)};var p=(t,e,n)=>new Promise(((o,r)=>{var l=t=>{try{u(n.next(t))}catch(t){r(t)}},i=t=>{try{u(n.throw(t))}catch(t){r(t)}},u=t=>t.done?o(t.value):Promise.resolve(t.value).then(l,i);u((n=n.apply(t,e)).next())}));const h=d(),m=d();var P=Object.defineProperty,b=Object.defineProperties,O=Object.getOwnPropertyDescriptors,g=Object.getOwnPropertySymbols,S=Object.prototype.hasOwnProperty,w=Object.prototype.propertyIsEnumerable,j=(t,e,n)=>e in t?P(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,A=(t,e)=>{for(var n in e||(e={}))S.call(e,n)&&j(t,n,e[n]);if(g)for(var n of g(e))w.call(e,n)&&j(t,n,e[n]);return t},L=(t,e,n)=>new Promise(((o,r)=>{var l=t=>{try{u(n.next(t))}catch(t){r(t)}},i=t=>{try{u(n.throw(t))}catch(t){r(t)}},u=t=>t.done?o(t.value):Promise.resolve(t.value).then(l,i);u((n=n.apply(t,e)).next())}));const x="message",C={storeProtocol:"https",storePath:"/store"},E=(t,e,n,l)=>{var i;const{host:d,room:P}=e.partySocketOptions,{storeProtocol:g,storePath:S}=A(A({},C),r(n)?{storeProtocol:n}:n),w=g+"://"+d+"/parties/"+(null!=(i=e.name)?i:"main")+"/"+P+S,j=t=>L(void 0,null,(function*(){return yield(yield fetch(w,(e=A({},t?{method:"PUT",body:a(t)}:{}),n={mode:"cors",cache:"no-store"},b(e,O(n))))).json();var e,n}));return((t,e,n,r,l,i,a=[])=>{let c,d,P,b=0,O=0;f(h,a,(()=>0)),f(m,a,(()=>[]));const g=t=>p(void 0,null,(function*(){return 2!=b&&(b=1,yield S.schedule((()=>p(void 0,null,(function*(){yield t(),b=0}))))),S})),S={load:(n,o)=>p(void 0,null,(function*(){return yield g((()=>p(void 0,null,(function*(){try{t.setContent(yield e())}catch(e){t.setContent([n,o])}}))))})),startAutoLoad:(...n)=>p(void 0,[...n],(function*(n={},o={}){return S.stopAutoLoad(),yield S.load(n,o),O=1,P=r(((n,o)=>p(void 0,null,(function*(){if(o){const e=o();yield g((()=>p(void 0,null,(function*(){return t.setTransactionChanges(e)}))))}else yield g((()=>p(void 0,null,(function*(){var o;try{t.setContent(null!=(o=null==n?void 0:n())?o:yield e())}catch(t){null==i||i(t)}}))))})))),S})),stopAutoLoad:()=>(O&&(l(P),P=void 0,O=0),S),save:e=>p(void 0,null,(function*(){return 1!=b&&(b=2,yield S.schedule((()=>p(void 0,null,(function*(){try{yield n(t.getContent,e)}catch(t){null==i||i(t)}b=0}))))),S})),startAutoSave:()=>p(void 0,null,(function*(){return yield S.stopAutoSave().save(),c=t.addDidFinishTransactionListener(((t,e)=>{const[n,o]=e();s(n)&&s(o)||S.save((()=>[n,o]))})),S})),stopAutoSave:()=>{var e,n;return e=c,n=t.delListener,!!o(e)||n(e),S},schedule:(...t)=>p(void 0,null,(function*(){return((t,...e)=>{t.push(...e)})(v(m,a),...t),yield p(void 0,null,(function*(){if(!v(h,a)){for(y(h,a,1);!o((t=v(m,a),d=t.shift()));)try{yield d()}catch(t){null==i||i(t)}y(h,a,0)}var t})),S})),getStore:()=>t,destroy:()=>S.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return u(S)})(t,(()=>L(void 0,null,(function*(){return yield j()}))),((t,n)=>L(void 0,null,(function*(){var o;n?e.send((o=n(),"s"+(r(o)?o:a(o)))):yield j(t())}))),(t=>{const n=e=>{const[n,o]=[(u=e.data)[0],c((r=u,l=1,r.slice(l,i)))];var r,l,i,u;"s"==n&&t(void 0,(()=>o))};return e.addEventListener(x,n),n}),(t=>{e.removeEventListener(x,t)}),l)};export{E as createPartyKitPersister};
1
+ const t=t=>typeof t,e="",n=t(e),o=(t,e)=>t instanceof e,r=t=>null==t,l=(t,e,n)=>r(t)?null==n?void 0:n():e(t),i=e=>t(e)==n,s=t=>t.length,u=Object,a=u.keys,c=u.freeze,d=t=>(t=>o(t,u)&&t.constructor==u)(t)&&0==(t=>s(a(t)))(t),v=t=>JSON.stringify(t,((t,e)=>o(e,Map)?u.fromEntries([...e]):e)),y=JSON.parse,f="/store",p=t=>new Map(t),h=(t,e)=>null==t?void 0:t.get(e),m=(t,e,n)=>{return r(n)?(l=e,null==(o=t)||o.delete(l),t):null==t?void 0:t.set(e,n);var o,l},P=(t,e,n)=>{var o,r,l;return r=e,null!=(l=null==(o=t)?void 0:o.has(r))&&l||m(t,e,n()),h(t,e)};var b=(t,e,n)=>new Promise(((o,r)=>{var l=t=>{try{s(n.next(t))}catch(t){r(t)}},i=t=>{try{s(n.throw(t))}catch(t){r(t)}},s=t=>t.done?o(t.value):Promise.resolve(t.value).then(l,i);s((n=n.apply(t,e)).next())}));const O=p(),g=p();var S=Object.defineProperty,w=Object.defineProperties,j=Object.getOwnPropertyDescriptors,A=Object.getOwnPropertySymbols,L=Object.prototype.hasOwnProperty,x=Object.prototype.propertyIsEnumerable,C=(t,e,n)=>e in t?S(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,E=(t,e)=>{for(var n in e||(e={}))L.call(e,n)&&C(t,n,e[n]);if(A)for(var n of A(e))x.call(e,n)&&C(t,n,e[n]);return t},T=(t,e,n)=>new Promise(((o,r)=>{var l=t=>{try{s(n.next(t))}catch(t){r(t)}},i=t=>{try{s(n.throw(t))}catch(t){r(t)}},s=t=>t.done?o(t.value):Promise.resolve(t.value).then(l,i);s((n=n.apply(t,e)).next())}));const k="message",D=(t,n,o,u)=>{var a;const{host:p,room:S}=n.partySocketOptions,{storeProtocol:A="https",storePath:L=f,messagePrefix:x=e}=E({},i(o)?{storeProtocol:o}:o),C=A+"://"+p+"/parties/"+(null!=(a=n.name)?a:"main")+"/"+S+L,D=t=>T(void 0,null,(function*(){return yield(yield fetch(C,(e=E({},t?{method:"PUT",body:v(t)}:{}),n={mode:"cors",cache:"no-store"},w(e,j(n))))).json();var e,n}));return((t,e,n,o,i,s,u=[])=>{let a,v,y,f=0,p=0;P(O,u,(()=>0)),P(g,u,(()=>[]));const S=t=>b(void 0,null,(function*(){return 2!=f&&(f=1,yield w.schedule((()=>b(void 0,null,(function*(){yield t(),f=0}))))),w})),w={load:(n,o)=>b(void 0,null,(function*(){return yield S((()=>b(void 0,null,(function*(){try{t.setContent(yield e())}catch(e){t.setContent([n,o])}}))))})),startAutoLoad:(...n)=>b(void 0,[...n],(function*(n={},r={}){return w.stopAutoLoad(),yield w.load(n,r),p=1,y=o(((n,o)=>b(void 0,null,(function*(){if(o){const e=o();yield S((()=>b(void 0,null,(function*(){return t.setTransactionChanges(e)}))))}else yield S((()=>b(void 0,null,(function*(){var o;try{t.setContent(null!=(o=null==n?void 0:n())?o:yield e())}catch(t){null==s||s(t)}}))))})))),w})),stopAutoLoad:()=>(p&&(i(y),y=void 0,p=0),w),save:e=>b(void 0,null,(function*(){return 1!=f&&(f=2,yield w.schedule((()=>b(void 0,null,(function*(){try{yield n(t.getContent,e)}catch(t){null==s||s(t)}f=0}))))),w})),startAutoSave:()=>b(void 0,null,(function*(){return yield w.stopAutoSave().save(),a=t.addDidFinishTransactionListener(((t,e)=>{const[n,o]=e();d(n)&&d(o)||w.save((()=>[n,o]))})),w})),stopAutoSave:()=>(l(a,t.delListener),w),schedule:(...t)=>b(void 0,null,(function*(){return((t,...e)=>{t.push(...e)})(h(g,u),...t),yield b(void 0,null,(function*(){if(!h(O,u)){for(m(O,u,1);!r((t=h(g,u),v=t.shift()));)try{yield v()}catch(t){null==s||s(t)}m(O,u,0)}var t})),w})),getStore:()=>t,destroy:()=>w.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return c(w)})(t,(()=>T(void 0,null,(function*(){return yield D()}))),((t,e)=>T(void 0,null,(function*(){var o,r;e?n.send((o=x,r=e(),o+"s"+(i(r)?r:v(r)))):yield D(t())}))),(t=>{const e=e=>l(((t,e,n)=>{const o=s(t);return((t,e)=>t.startsWith(e))(e,t)?[e[o],y((r=e,l=o+1,r.slice(l,void 0)))]:void 0;var r,l})(x,e.data),(([e,n])=>{"s"==e&&t(void 0,(()=>n))}));return n.addEventListener(k,e),e}),(t=>{n.removeEventListener(k,t)}),u)};export{D as createPartyKitPersister};
@@ -1 +1 @@
1
- const e=e=>typeof e,t=e(""),s="t",n=Promise,r=e=>null==e,i=(e,t,s)=>e.slice(t,s),o=e=>e.length,l=(e,t)=>e.map(t),a=(e,...t)=>e.push(...t),c=Object,u=(e=[])=>c.fromEntries(e),h=(e,t)=>l(c.entries(e),(([e,s])=>t(s,e))),d=(e,t,s)=>(((e,t)=>!r(((e,t)=>{return n=e=>e[t],r(s=e)?void 0:n(s);var s,n})(e,t)))(e,t)||(e[t]=s()),e[t]),p=e=>JSON.stringify(e,((e,t)=>t instanceof Map?c.fromEntries([...t]):t)),f=JSON.parse,v=(e,t)=>[e[0],t?f(i(e,1)):i(e,1)],y=(e,t)=>((e,t)=>null==e?void 0:e.forEach(t))(e,((e,s)=>t(s,e)));var g=(e,t,s)=>new Promise(((n,r)=>{var i=e=>{try{l(s.next(e))}catch(e){r(e)}},o=e=>{try{l(s.throw(e))}catch(e){r(e)}},l=e=>e.done?n(e.value):Promise.resolve(e.value).then(i,o);l((s=s.apply(e,t)).next())}));const m="hasStore",P=u(l(["Origin","Methods","Headers"],(e=>["Access-Control-Allow-"+e,"*"]))),x=(e,t)=>g(void 0,null,(function*(){return yield e.get(t+m)})),R=(e,t,i)=>g(void 0,null,(function*(){const l=[e.put(t+m,1)],c=[];h(i[0],((n,i)=>r(n)?((e,...t)=>e.unshift(...t))(c,t+w(s,i)):h(n,((n,o)=>r(n)?a(c,t+w(s,i,o)):h(n,((n,r)=>O(l,e,t+w(s,i,o,r),n))))))),h(i[1],((s,n)=>O(l,e,t+"v"+n,s))),0!=o(c)&&y(yield e.list(),(t=>c.every((s=>!t.startsWith(s)||O(l,e,t)&&!1)))),yield(e=>{return t=function*(){return n.all(e)},new Promise(((e,s)=>{var n=e=>{try{i(t.next(e))}catch(e){s(e)}},r=e=>{try{i(t.throw(e))}catch(e){s(e)}},i=t=>t.done?e(t.value):Promise.resolve(t.value).then(n,r);i((t=t.apply(void 0,null)).next())}));var t})(l)})),w=(e,...t)=>e+i(p(t),1,-1),O=(e,t,s,n)=>a(e,r(n)?t.delete(s):t.put(s,n));class E{constructor(e){this.party=e,this.config={},this.createResponse=(e,t=null)=>{var s;return new Response(t,{status:e,headers:null!=(s=this.config.responseHeaders)?s:P})}}onRequest(e){return g(this,null,(function*(){var t,n;const r=this.party.storage,l=null!=(t=this.config.storagePrefix)?t:"",a=null!=(n=this.config.storePath)?n:"/store";if(new URL(e.url).pathname.endsWith(a)){const t=yield x(r,l),n=yield e.text();return"PUT"==e.method?t?this.createResponse(205):(yield R(r,l,f(n)),this.createResponse(201)):this.createResponse(200,t?p(yield((e,t)=>g(void 0,null,(function*(){const n={},r={};return y(yield e.list(),((e,l)=>{if(e.startsWith(t)){e=i(e,o(t));const[a,c]=v(e);if(a==s){const[e,t,s]=f("["+c+"]");d(d(n,e,u),t,u)[s]=l}else"v"==a&&(r[c]=l)}})),[n,r]})))(r,l)):"")}return this.createResponse(404)}))}onMessage(s,n){return g(this,null,(function*(){var r;const i=this.party.storage,o=null!=(r=this.config.storagePrefix)?r:"",[l,a]=v(s,1);"s"==l&&(yield x(i,o))&&(yield R(i,o,a),this.party.broadcast(((s,n)=>"s"+(e(n)==t?n:p(n)))(0,a),[n.id]))}))}}export{E as TinyBasePartyKitServer};
1
+ const e=e=>typeof e,t="",n=e(t),l="t",r=(e,t)=>e.startsWith(t),i=Promise,o=e=>null==e,s=(e,t,n)=>o(e)?null==n?void 0:n():t(e),a=(e,t,n)=>e.slice(t,n),u=e=>e.length,c=e=>{return t=function*(){return i.all(e)},new Promise(((e,n)=>{var l=e=>{try{i(t.next(e))}catch(e){n(e)}},r=e=>{try{i(t.throw(e))}catch(e){n(e)}},i=t=>t.done?e(t.value):Promise.resolve(t.value).then(l,r);i((t=t.apply(void 0,null)).next())}));var t},d=(e,t)=>e.map(t),h=(e,...t)=>e.push(...t),f=Object,v=(e=[])=>f.fromEntries(e),y=(e,t)=>d(f.entries(e),(([e,n])=>t(n,e))),g=(e,t,n)=>(((e,t)=>!o(((e,t)=>s(e,(e=>e[t])))(e,t)))(e,t)||(e[t]=n()),e[t]),p=e=>JSON.stringify(e,((e,t)=>t instanceof Map?f.fromEntries([...t]):t)),S=JSON.parse,m=(t,l,r)=>t+l+(e(r)==n?r:p(r)),w=(e,t,n)=>{const l=u(e);return r(t,e)?[t[l],(n?S:String)(a(t,l+1))]:void 0},P=(e,t)=>((e,t)=>null==e?void 0:e.forEach(t))(e,((e,n)=>t(n,e)));var x=(e,t,n)=>new Promise(((l,r)=>{var i=e=>{try{s(n.next(e))}catch(e){r(e)}},o=e=>{try{s(n.throw(e))}catch(e){r(e)}},s=e=>e.done?l(e.value):Promise.resolve(e.value).then(i,o);s((n=n.apply(e,t)).next())}));const D="hasStore",R=v(d(["Origin","Methods","Headers"],(e=>["Access-Control-Allow-"+e,"*"]))),b=e=>x(void 0,null,(function*(){var n;return yield e.party.storage.get((null!=(n=e.config.storagePrefix)?n:t)+D)})),C=(e,n,i,s)=>x(void 0,null,(function*(){var a;const d=e.party.storage,f=null!=(a=e.config.storagePrefix)?a:t,v={[f+D]:1},g=[],p=[];yield c(y(n[0],((t,n)=>x(void 0,null,(function*(){return o(t)?!i&&e.canDelTable(n,s)&&((e,...t)=>e.unshift(...t))(p,T(f,l,n)):e.canSetTable(n,i,s)&&(yield c(y(t,((t,r)=>x(void 0,null,(function*(){return o(t)?!i&&e.canDelRow(n,r,s)&&h(p,T(f,l,n,r)):e.canSetRow(n,r,i,s)&&(yield c(y(t,((t,a)=>x(void 0,null,(function*(){const u=[n,r,a],c=T(f,l,...u);o(t)?!i&&e.canDelCell(...u,s)&&h(g,c):e.canSetCell(...u,t,i,s,yield d.get(c))&&(v[c]=t)}))))))}))))))}))))),yield c(y(n[1],((t,n)=>x(void 0,null,(function*(){const l=f+"v"+n;o(t)?!i&&e.canDelValue(n,s)&&h(g,l):e.canSetValue(n,t,i,s,yield d.get(l))&&(v[l]=t)}))))),0!=u(p)&&P(yield d.list(),(e=>p.every((t=>!r(e,t)||h(g,e)&&0)))),yield d.delete(g),yield d.put(v)})),T=(e,t,...n)=>m(e,t,a(p(n),1,-1)),O=(e,t,n=null)=>x(void 0,null,(function*(){var l;return new Response(n,{status:t,headers:null!=(l=e.config.responseHeaders)?l:R})}));class V{constructor(e){this.party=e,this.config={}}onRequest(e){return x(this,null,(function*(){var n;const r=null!=(n=this.config.storePath)?n:"/store";if(new URL(e.url).pathname.endsWith(r)){const n=yield b(this),r=yield e.text();return"PUT"==e.method?n?O(this,205):(yield C(this,S(r),!0,e),O(this,201)):O(this,200,n?p(yield(i=this,x(void 0,null,(function*(){var e;const n={},r={},o=null!=(e=i.config.storagePrefix)?e:t;return P(yield i.party.storage.list(),((e,t)=>s(w(o,e),(([e,i])=>{if(e==l){const[e,l,r]=S("["+i+"]");g(g(n,e,v),l,v)[r]=t}else"v"==e&&(r[i]=t)})))),[n,r]})))):t)}var i;return O(this,404)}))}onMessage(e,n){return x(this,null,(function*(){var l;const r=null!=(l=this.config.messagePrefix)?l:t;yield s(w(r,e,1),(e=>x(this,[e],(function*([e,t]){"s"==e&&(yield b(this))&&(yield C(this,t,!1,n),this.party.broadcast(m(r,"s",t)))}))))}))}canSetTable(e,t,n){return!0}canDelTable(e,t){return!0}canSetRow(e,t,n,l){return!0}canDelRow(e,t,n){return!0}canSetCell(e,t,n,l,r,i,o){return!0}canDelCell(e,t,n,l){return!0}canSetValue(e,t,n,l,r){return!0}canDelValue(e,t){return!0}}export{V as TinyBasePartyKitServer};
@@ -1 +1 @@
1
- const t=t=>typeof t,a=t(""),e=(t,a)=>t instanceof a,s=t=>null==t,o=e=>t(e)==a,n=Object,r=n.keys,c=n.freeze,i=t=>(t=>e(t,n)&&t.constructor==n)(t)&&0==(t=>r(t).length)(t),y=t=>JSON.stringify(t,((t,a)=>e(a,Map)?n.fromEntries([...a]):a)),d=JSON.parse,h=t=>new Map(t),u=(t,a)=>t?.get(a),v=(t,a,e)=>{return s(e)?(o=t,n=a,o?.delete(n),t):t?.set(a,e);var o,n},w=(t,a,e)=>{var s,o;return s=t,o=a,s?.has(o)||v(t,a,e()),u(t,a)},l=h(),p=h(),f="message",S={storeProtocol:"https",storePath:"/store"},g=(t,a,e,n)=>{const{host:r,room:h}=a.partySocketOptions,{storeProtocol:g,storePath:m}={...S,...o(e)?{storeProtocol:e}:e},A=g+"://"+r+"/parties/"+(a.name??"main")+"/"+h+m,L=async t=>await(await fetch(A,{...t?{method:"PUT",body:y(t)}:{},mode:"cors",cache:"no-store"})).json();return((t,a,e,o,n,r,y=[])=>{let d,h,f,S=0,g=0;w(l,y,(()=>0)),w(p,y,(()=>[]));const m=async t=>(2!=S&&(S=1,await A.schedule((async()=>{await t(),S=0}))),A),A={load:async(e,s)=>await m((async()=>{try{t.setContent(await a())}catch{t.setContent([e,s])}})),startAutoLoad:async(e={},s={})=>(A.stopAutoLoad(),await A.load(e,s),g=1,f=o((async(e,s)=>{if(s){const a=s();await m((async()=>t.setTransactionChanges(a)))}else await m((async()=>{try{t.setContent(e?.()??await a())}catch(t){r?.(t)}}))})),A),stopAutoLoad:()=>(g&&(n(f),f=void 0,g=0),A),save:async a=>(1!=S&&(S=2,await A.schedule((async()=>{try{await e(t.getContent,a)}catch(t){r?.(t)}S=0}))),A),startAutoSave:async()=>(await A.stopAutoSave().save(),d=t.addDidFinishTransactionListener(((t,a)=>{const[e,s]=a();i(e)&&i(s)||A.save((()=>[e,s]))})),A),stopAutoSave:()=>{var a,e;return a=d,e=t.delListener,s(a)||e(a),A},schedule:async(...t)=>(((t,...a)=>{t.push(...a)})(u(p,y),...t),await(async()=>{if(!u(l,y)){for(v(l,y,1);!s((t=u(p,y),h=t.shift()));)try{await h()}catch(t){r?.(t)}v(l,y,0)}var t})(),A),getStore:()=>t,destroy:()=>A.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return c(A)})(t,(async()=>await L()),(async(t,e)=>{var s;e?a.send((s=e(),"s"+(o(s)?s:y(s)))):await L(t())}),(t=>{const e=a=>{const[e,s]=[(c=a.data)[0],d((o=c,n=1,o.slice(n,r)))];var o,n,r,c;"s"==e&&t(void 0,(()=>s))};return a.addEventListener(f,e),e}),(t=>{a.removeEventListener(f,t)}),n)};export{g as createPartyKitPersister};
1
+ const t=t=>typeof t,a="",e=t(a),s=(t,a)=>t instanceof a,o=t=>null==t,n=(t,a,e)=>o(t)?e?.():a(t),r=a=>t(a)==e,c=t=>t.length,i=Object,d=i.keys,y=i.freeze,h=t=>(t=>s(t,i)&&t.constructor==i)(t)&&0==(t=>c(d(t)))(t),u=t=>JSON.stringify(t,((t,a)=>s(a,Map)?i.fromEntries([...a]):a)),v=JSON.parse,w="/store",p=t=>new Map(t),l=(t,a)=>t?.get(a),f=(t,a,e)=>{return o(e)?(s=t,n=a,s?.delete(n),t):t?.set(a,e);var s,n},g=(t,a,e)=>{var s,o;return s=t,o=a,s?.has(o)||f(t,a,e()),l(t,a)},m=p(),S=p(),A="message",L=(t,e,s,i)=>{const{host:d,room:p}=e.partySocketOptions,{storeProtocol:L="https",storePath:C=w,messagePrefix:P=a}={...r(s)?{storeProtocol:s}:s},O=L+"://"+d+"/parties/"+(e.name??"main")+"/"+p+C,E=async t=>await(await fetch(O,{...t?{method:"PUT",body:u(t)}:{},mode:"cors",cache:"no-store"})).json();return((t,a,e,s,r,c,i=[])=>{let d,u,v,w=0,p=0;g(m,i,(()=>0)),g(S,i,(()=>[]));const A=async t=>(2!=w&&(w=1,await L.schedule((async()=>{await t(),w=0}))),L),L={load:async(e,s)=>await A((async()=>{try{t.setContent(await a())}catch{t.setContent([e,s])}})),startAutoLoad:async(e={},o={})=>(L.stopAutoLoad(),await L.load(e,o),p=1,v=s((async(e,s)=>{if(s){const a=s();await A((async()=>t.setTransactionChanges(a)))}else await A((async()=>{try{t.setContent(e?.()??await a())}catch(t){c?.(t)}}))})),L),stopAutoLoad:()=>(p&&(r(v),v=void 0,p=0),L),save:async a=>(1!=w&&(w=2,await L.schedule((async()=>{try{await e(t.getContent,a)}catch(t){c?.(t)}w=0}))),L),startAutoSave:async()=>(await L.stopAutoSave().save(),d=t.addDidFinishTransactionListener(((t,a)=>{const[e,s]=a();h(e)&&h(s)||L.save((()=>[e,s]))})),L),stopAutoSave:()=>(n(d,t.delListener),L),schedule:async(...t)=>(((t,...a)=>{t.push(...a)})(l(S,i),...t),await(async()=>{if(!l(m,i)){for(f(m,i,1);!o((t=l(S,i),u=t.shift()));)try{await u()}catch(t){c?.(t)}f(m,i,0)}var t})(),L),getStore:()=>t,destroy:()=>L.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return y(L)})(t,(async()=>await E()),(async(t,a)=>{var s,o;a?e.send((s=P,o=a(),s+"s"+(r(o)?o:u(o)))):await E(t())}),(t=>{const a=a=>n(((t,a,e)=>{const s=c(t);return((t,a)=>t.startsWith(a))(a,t)?[a[s],v((o=a,n=s+1,o.slice(n,void 0)))]:void 0;var o,n})(P,a.data),(([a,e])=>{"s"==a&&t(void 0,(()=>e))}));return e.addEventListener(A,a),a}),(t=>{e.removeEventListener(A,t)}),i)};export{L as createPartyKitPersister};
@@ -1 +1 @@
1
- const t=t=>typeof t,s=t(""),e="t",a=Promise,i=t=>null==t,r=(t,s,e)=>t.slice(s,e),n=t=>t.length,o=(t,s)=>t.map(s),c=(t,...s)=>t.push(...s),h=Object,p=(t=[])=>h.fromEntries(t),f=(t,s)=>o(h.entries(t),(([t,e])=>s(e,t))),l=(t,s,e)=>(((t,s)=>!i(((t,s)=>{return a=t=>t[s],i(e=t)?void 0:a(e);var e,a})(t,s)))(t,s)||(t[s]=e()),t[s]),g=t=>JSON.stringify(t,((t,s)=>s instanceof Map?h.fromEntries([...s]):s)),u=JSON.parse,w=(t,s)=>[t[0],s?u(r(t,1)):r(t,1)],y=(t,s)=>((t,s)=>t?.forEach(s))(t,((t,e)=>s(e,t))),d="hasStore",R=p(o(["Origin","Methods","Headers"],(t=>["Access-Control-Allow-"+t,"*"]))),m=async(t,s)=>await t.get(s+d),v=async(t,s,r)=>{const o=[t.put(s+d,1)],h=[];f(r[0],((a,r)=>i(a)?((t,...s)=>t.unshift(...s))(h,s+P(e,r)):f(a,((a,n)=>i(a)?c(h,s+P(e,r,n)):f(a,((a,i)=>x(o,t,s+P(e,r,n,i),a))))))),f(r[1],((e,a)=>x(o,t,s+"v"+a,e))),0!=n(h)&&y(await t.list(),(s=>h.every((e=>!s.startsWith(e)||x(o,t,s)&&!1)))),await(async t=>a.all(t))(o)},P=(t,...s)=>t+r(g(s),1,-1),x=(t,s,e,a)=>c(t,i(a)?s.delete(e):s.put(e,a));class O{constructor(t){this.party=t,this.config={},this.createResponse=(t,s=null)=>new Response(s,{status:t,headers:this.config.responseHeaders??R})}async onRequest(t){const s=this.party.storage,a=this.config.storagePrefix??"",i=this.config.storePath??"/store";if(new URL(t.url).pathname.endsWith(i)){const i=await m(s,a),o=await t.text();return"PUT"==t.method?i?this.createResponse(205):(await v(s,a,u(o)),this.createResponse(201)):this.createResponse(200,i?g(await(async(t,s)=>{const a={},i={};return y(await t.list(),((t,o)=>{if(t.startsWith(s)){t=r(t,n(s));const[c,h]=w(t);if(c==e){const[t,s,e]=u("["+h+"]");l(l(a,t,p),s,p)[e]=o}else"v"==c&&(i[h]=o)}})),[a,i]})(s,a)):"")}return this.createResponse(404)}async onMessage(e,a){const i=this.party.storage,r=this.config.storagePrefix??"",[n,o]=w(e,1);"s"==n&&await m(i,r)&&(await v(i,r,o),this.party.broadcast(((e,a)=>"s"+(t(a)==s?a:g(a)))(0,o),[a.id]))}}export{O as TinyBasePartyKitServer};
1
+ const t=t=>typeof t,a="",e=t(a),s="t",n=(t,a)=>t.startsWith(a),r=Promise,i=t=>null==t,c=(t,a,e)=>i(t)?e?.():a(t),o=(t,a,e)=>t.slice(a,e),l=t=>t.length,h=async t=>r.all(t),u=(t,a)=>t.map(a),w=(t,...a)=>t.push(...a),g=Object,f=(t=[])=>g.fromEntries(t),y=(t,a)=>u(g.entries(t),(([t,e])=>a(e,t))),p=(t,a,e)=>(((t,a)=>!i(((t,a)=>c(t,(t=>t[a])))(t,a)))(t,a)||(t[a]=e()),t[a]),S=t=>JSON.stringify(t,((t,a)=>a instanceof Map?g.fromEntries([...a]):a)),d=JSON.parse,D=(a,s,n)=>a+s+(t(n)==e?n:S(n)),m=(t,a,e)=>{const s=l(t);return n(a,t)?[a[s],(e?d:String)(o(a,s+1))]:void 0},P=(t,a)=>((t,a)=>t?.forEach(a))(t,((t,e)=>a(e,t))),R="hasStore",b=f(u(["Origin","Methods","Headers"],(t=>["Access-Control-Allow-"+t,"*"]))),x=async t=>await t.party.storage.get((t.config.storagePrefix??a)+R),C=async(t,e,r,c)=>{const o=t.party.storage,u=t.config.storagePrefix??a,g={[u+R]:1},f=[],p=[];await h(y(e[0],(async(a,e)=>i(a)?!r&&t.canDelTable(e,c)&&((t,...a)=>t.unshift(...a))(p,T(u,s,e)):t.canSetTable(e,r,c)&&await h(y(a,(async(a,n)=>i(a)?!r&&t.canDelRow(e,n,c)&&w(p,T(u,s,e,n)):t.canSetRow(e,n,r,c)&&await h(y(a,(async(a,l)=>{const h=[e,n,l],y=T(u,s,...h);i(a)?!r&&t.canDelCell(...h,c)&&w(f,y):t.canSetCell(...h,a,r,c,await o.get(y))&&(g[y]=a)}))))))))),await h(y(e[1],(async(a,e)=>{const s=u+"v"+e;i(a)?!r&&t.canDelValue(e,c)&&w(f,s):t.canSetValue(e,a,r,c,await o.get(s))&&(g[s]=a)}))),0!=l(p)&&P(await o.list(),(t=>p.every((a=>!n(t,a)||w(f,t)&&0)))),await o.delete(f),await o.put(g)},T=(t,a,...e)=>D(t,a,o(S(e),1,-1)),v=async(t,a,e=null)=>new Response(e,{status:a,headers:t.config.responseHeaders??b});class O{constructor(t){this.party=t,this.config={}}async onRequest(t){const e=this.config.storePath??"/store";if(new URL(t.url).pathname.endsWith(e)){const e=await x(this),n=await t.text();return"PUT"==t.method?e?v(this,205):(await C(this,d(n),!0,t),v(this,201)):v(this,200,e?S(await(async t=>{const e={},n={},r=t.config.storagePrefix??a;return P(await t.party.storage.list(),((t,a)=>c(m(r,t),(([t,r])=>{if(t==s){const[t,s,n]=d("["+r+"]");p(p(e,t,f),s,f)[n]=a}else"v"==t&&(n[r]=a)})))),[e,n]})(this)):a)}return v(this,404)}async onMessage(t,e){const s=this.config.messagePrefix??a;await c(m(s,t,1),(async([t,a])=>{"s"==t&&await x(this)&&(await C(this,a,!1,e),this.party.broadcast(D(s,"s",a)))}))}canSetTable(t,a,e){return!0}canDelTable(t,a){return!0}canSetRow(t,a,e,s){return!0}canDelRow(t,a,e){return!0}canSetCell(t,a,e,s,n,r,i){return!0}canDelCell(t,a,e,s){return!0}canSetValue(t,a,e,s,n){return!0}canDelValue(t,a){return!0}}export{O as TinyBasePartyKitServer};