tinybase 4.3.11 → 4.3.12
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/lib/cjs/persisters/persister-partykit-client.cjs +1 -1
- package/lib/cjs/persisters/persister-partykit-client.cjs.gz +0 -0
- package/lib/cjs/persisters/persister-partykit-server.cjs +1 -1
- package/lib/cjs/persisters/persister-partykit-server.cjs.gz +0 -0
- package/lib/cjs-es6/persisters/persister-partykit-client.cjs +1 -1
- package/lib/cjs-es6/persisters/persister-partykit-client.cjs.gz +0 -0
- package/lib/cjs-es6/persisters/persister-partykit-server.cjs +1 -1
- package/lib/cjs-es6/persisters/persister-partykit-server.cjs.gz +0 -0
- package/lib/debug/persisters/persister-partykit-client.js +29 -19
- package/lib/debug/persisters/persister-partykit-server.js +155 -64
- package/lib/es6/persisters/persister-partykit-client.js +1 -1
- package/lib/es6/persisters/persister-partykit-client.js.gz +0 -0
- package/lib/es6/persisters/persister-partykit-server.js +1 -1
- package/lib/es6/persisters/persister-partykit-server.js.gz +0 -0
- package/lib/persisters/persister-partykit-client.js +1 -1
- package/lib/persisters/persister-partykit-client.js.gz +0 -0
- package/lib/persisters/persister-partykit-server.js +1 -1
- package/lib/persisters/persister-partykit-server.js.gz +0 -0
- package/lib/types/persisters/persister-indexed-db.d.ts +4 -5
- package/lib/types/persisters/persister-partykit-client.d.ts +18 -7
- package/lib/types/persisters/persister-partykit-server.d.ts +335 -7
- package/lib/types/tools.d.ts +4 -6
- package/lib/types/ui-react.d.ts +4 -4
- package/lib/types/with-schemas/persisters/persister-indexed-db.d.ts +4 -5
- package/lib/types/with-schemas/persisters/persister-partykit-client.d.ts +18 -7
- package/lib/types/with-schemas/persisters/persister-partykit-server.d.ts +359 -7
- package/lib/types/with-schemas/tools.d.ts +4 -6
- package/lib/types/with-schemas/ui-react.d.ts +4 -4
- package/lib/umd/persisters/persister-partykit-client.js +1 -1
- package/lib/umd/persisters/persister-partykit-client.js.gz +0 -0
- package/lib/umd/persisters/persister-partykit-server.js +1 -1
- package/lib/umd/persisters/persister-partykit-server.js.gz +0 -0
- package/lib/umd-es6/persisters/persister-partykit-client.js +1 -1
- package/lib/umd-es6/persisters/persister-partykit-client.js.gz +0 -0
- package/lib/umd-es6/persisters/persister-partykit-server.js +1 -1
- package/lib/umd-es6/persisters/persister-partykit-server.js.gz +0 -0
- package/package.json +8 -8
- package/readme.md +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const t=t=>typeof t,a=
|
|
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)};
|
|
Binary file
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const t=t=>typeof t,
|
|
1
|
+
"use strict";const t=t=>typeof t,e="",a=t(e),s="t",n=(t,e)=>t.startsWith(e),r=Promise,i=t=>null==t,c=(t,e,a)=>i(t)?a?.():e(t),o=(t,e,a)=>t.slice(e,a),l=t=>t.length,u=(t,e)=>t.map(e),h=(t,...e)=>t.push(...e),f=Object,g=(t=[])=>f.fromEntries(t),w=(t,e)=>u(f.entries(t),(([t,a])=>e(a,t))),y=(t,e,a)=>(((t,e)=>!i(((t,e)=>c(t,(t=>t[e])))(t,e)))(t,e)||(t[e]=a()),t[e]),p=t=>JSON.stringify(t,((t,e)=>e instanceof Map?f.fromEntries([...e]):e)),S=JSON.parse,d=(e,s,n)=>e+s+(t(n)==a?n:p(n)),D=(t,e,a)=>{const s=l(t);return n(e,t)?[e[s],(a?S:String)(o(e,s+1))]:void 0},P=(t,e)=>((t,e)=>t?.forEach(e))(t,((t,a)=>e(a,t))),m="hasStore",R=g(u(["Origin","Methods","Headers"],(t=>["Access-Control-Allow-"+t,"*"]))),b=async t=>await t.party.storage.get((t.config.storagePrefix??e)+m),v=async(t,a,c,o)=>{const u=t.party.storage,f=t.config.storagePrefix??e,g=[u.put(f+m,1)],y=[];w(a[0],((e,a)=>i(e)?!c&&t.canDelTable(a,o)&&((t,...e)=>t.unshift(...e))(y,x(f,s,a)):t.canSetTable(a,c,o)&&w(e,((e,n)=>i(e)?!c&&t.canDelRow(a,n,o)&&h(y,x(f,s,a,n)):t.canSetRow(a,n,c,o)&&w(e,((e,r)=>i(e)?!c&&t.canDelCell(a,n,r,o)&&h(g,u.delete(x(f,s,a,n,r))):t.canSetCell(a,n,r,e,c,o)&&h(g,u.put(x(f,s,a,n,r),e)))))))),w(a[1],((e,a)=>i(e)?!c&&t.canDelValue(a,o)&&h(g,u.delete(f+"v"+a)):t.canSetValue(a,e,c,o)&&h(g,u.put(f+"v"+a,e)))),0!=l(y)&&P(await u.list(),(t=>y.every((e=>!n(t,e)||h(g,u.delete(t))&&0)))),await(async t=>r.all(t))(g)},x=(t,e,...a)=>d(t,e,o(p(a),1,-1)),T=async(t,e,a=null)=>new Response(a,{status:e,headers:t.config.responseHeaders??R});exports.TinyBasePartyKitServer=class{constructor(t){this.party=t,this.config={}}async onRequest(t){const a=this.config.storePath??"/store";if(new URL(t.url).pathname.endsWith(a)){const a=await b(this),n=await t.text();return"PUT"==t.method?a?T(this,205):(await v(this,S(n),!0,t),T(this,201)):T(this,200,a?p(await(async t=>{const a={},n={},r=t.config.storagePrefix??e;return P(await t.party.storage.list(),((t,e)=>c(D(r,t),(([t,r])=>{if(t==s){const[t,s,n]=S("["+r+"]");y(y(a,t,g),s,g)[n]=e}else"v"==t&&(n[r]=e)})))),[a,n]})(this)):e)}return T(this,404)}async onMessage(t,a){const s=this.config.messagePrefix??e;await c(D(s,t,1),(async([t,e])=>{"s"==t&&await b(this)&&(await v(this,e,!1,a),this.party.broadcast(d(s,"s",e)))}))}canSetTable(t,e,a){return!0}canDelTable(t,e){return!0}canSetRow(t,e,a,s){return!0}canDelRow(t,e,a){return!0}canSetCell(t,e,a,s,n,r){return!0}canDelCell(t,e,a,s){return!0}canSetValue(t,e,a,s){return!0}canDelValue(t,e){return!0}};
|
|
Binary file
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const t=t=>typeof t,e=
|
|
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)};
|
|
Binary file
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const e=e=>typeof e,t=e(
|
|
1
|
+
"use strict";const e=e=>typeof e,t="",n=e(t),r="t",l=(e,t)=>e.startsWith(t),s=Promise,i=e=>null==e,a=(e,t,n)=>i(e)?null==n?void 0:n():t(e),o=(e,t,n)=>e.slice(t,n),u=e=>e.length,c=(e,t)=>e.map(t),h=(e,...t)=>e.push(...t),d=Object,v=(e=[])=>d.fromEntries(e),f=(e,t)=>c(d.entries(e),(([e,n])=>t(n,e))),y=(e,t,n)=>(((e,t)=>!i(((e,t)=>a(e,(e=>e[t])))(e,t)))(e,t)||(e[t]=n()),e[t]),p=e=>JSON.stringify(e,((e,t)=>t instanceof Map?d.fromEntries([...t]):t)),g=JSON.parse,S=(t,r,l)=>t+r+(e(l)==n?l:p(l)),P=(e,t,n)=>{const r=u(e);return l(t,e)?[t[r],(n?g:String)(o(t,r+1))]:void 0},m=(e,t)=>((e,t)=>null==e?void 0:e.forEach(t))(e,((e,n)=>t(n,e)));var w=(e,t,n)=>new Promise(((r,l)=>{var s=e=>{try{a(n.next(e))}catch(e){l(e)}},i=e=>{try{a(n.throw(e))}catch(e){l(e)}},a=e=>e.done?r(e.value):Promise.resolve(e.value).then(s,i);a((n=n.apply(e,t)).next())}));const x="hasStore",D=v(c(["Origin","Methods","Headers"],(e=>["Access-Control-Allow-"+e,"*"]))),R=e=>w(void 0,null,(function*(){var n;return yield e.party.storage.get((null!=(n=e.config.storagePrefix)?n:t)+x)})),b=(e,n,a,o)=>w(void 0,null,(function*(){var c;const d=e.party.storage,v=null!=(c=e.config.storagePrefix)?c:t,y=[d.put(v+x,1)],p=[];f(n[0],((t,n)=>i(t)?!a&&e.canDelTable(n,o)&&((e,...t)=>e.unshift(...t))(p,T(v,r,n)):e.canSetTable(n,a,o)&&f(t,((t,l)=>i(t)?!a&&e.canDelRow(n,l,o)&&h(p,T(v,r,n,l)):e.canSetRow(n,l,a,o)&&f(t,((t,s)=>i(t)?!a&&e.canDelCell(n,l,s,o)&&h(y,d.delete(T(v,r,n,l,s))):e.canSetCell(n,l,s,t,a,o)&&h(y,d.put(T(v,r,n,l,s),t)))))))),f(n[1],((t,n)=>i(t)?!a&&e.canDelValue(n,o)&&h(y,d.delete(v+"v"+n)):e.canSetValue(n,t,a,o)&&h(y,d.put(v+"v"+n,t)))),0!=u(p)&&m(yield d.list(),(e=>p.every((t=>!l(e,t)||h(y,d.delete(e))&&0)))),yield(e=>{return t=function*(){return s.all(e)},new Promise(((e,n)=>{var r=e=>{try{s(t.next(e))}catch(e){n(e)}},l=e=>{try{s(t.throw(e))}catch(e){n(e)}},s=t=>t.done?e(t.value):Promise.resolve(t.value).then(r,l);s((t=t.apply(void 0,null)).next())}));var t})(y)})),T=(e,t,...n)=>S(e,t,o(p(n),1,-1)),C=(e,t,n=null)=>w(void 0,null,(function*(){var r;return new Response(n,{status:t,headers:null!=(r=e.config.responseHeaders)?r:D})}));exports.TinyBasePartyKitServer=class{constructor(e){this.party=e,this.config={}}onRequest(e){return w(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 R(this),l=yield e.text();return"PUT"==e.method?n?C(this,205):(yield b(this,g(l),!0,e),C(this,201)):C(this,200,n?p(yield(s=this,w(void 0,null,(function*(){var e;const n={},l={},i=null!=(e=s.config.storagePrefix)?e:t;return m(yield s.party.storage.list(),((e,t)=>a(P(i,e),(([e,s])=>{if(e==r){const[e,r,l]=g("["+s+"]");y(y(n,e,v),r,v)[l]=t}else"v"==e&&(l[s]=t)})))),[n,l]})))):t)}var s;return C(this,404)}))}onMessage(e,n){return w(this,null,(function*(){var r;const l=null!=(r=this.config.messagePrefix)?r:t;yield a(P(l,e,1),(e=>w(this,[e],(function*([e,t]){"s"==e&&(yield R(this))&&(yield b(this,t,!1,n),this.party.broadcast(S(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,s){return!0}canDelCell(e,t,n,r){return!0}canSetValue(e,t,n,r){return!0}canDelValue(e,t){return!0}};
|
|
Binary file
|
|
@@ -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
|
|
34
|
-
type + (isString(payload) ? payload : jsonString(payload));
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
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 {
|
|
206
|
-
|
|
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(
|
|
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
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
|
46
|
-
type + (isString(payload) ? payload : jsonString(payload));
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
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
|
|
65
|
-
await storage.get(
|
|
66
|
-
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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,182 @@ 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
|
|
85
|
-
|
|
91
|
+
const saveStore = async (
|
|
92
|
+
that,
|
|
93
|
+
transactionChanges,
|
|
94
|
+
initialSave,
|
|
95
|
+
requestOrConnection,
|
|
96
|
+
) => {
|
|
97
|
+
const storage = that.party.storage;
|
|
98
|
+
const storagePrefix = that.config.storagePrefix ?? EMPTY_STRING;
|
|
99
|
+
const promises = [storage.put(storagePrefix + HAS_STORE, 1)];
|
|
86
100
|
const keyPrefixesToDelete = [];
|
|
87
101
|
objMap(transactionChanges[0], (table, tableId) =>
|
|
88
102
|
isUndefined(table)
|
|
89
|
-
?
|
|
103
|
+
? !initialSave &&
|
|
104
|
+
that.canDelTable(tableId, requestOrConnection) &&
|
|
105
|
+
arrayUnshift(
|
|
90
106
|
keyPrefixesToDelete,
|
|
91
|
-
|
|
107
|
+
constructStorageKey(storagePrefix, T, tableId),
|
|
92
108
|
)
|
|
93
|
-
:
|
|
109
|
+
: that.canSetTable(tableId, initialSave, requestOrConnection) &&
|
|
110
|
+
objMap(table, (row, rowId) =>
|
|
94
111
|
isUndefined(row)
|
|
95
|
-
?
|
|
112
|
+
? !initialSave &&
|
|
113
|
+
that.canDelRow(tableId, rowId, requestOrConnection) &&
|
|
114
|
+
arrayPush(
|
|
96
115
|
keyPrefixesToDelete,
|
|
97
|
-
|
|
116
|
+
constructStorageKey(storagePrefix, T, tableId, rowId),
|
|
98
117
|
)
|
|
99
|
-
:
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
118
|
+
: that.canSetRow(
|
|
119
|
+
tableId,
|
|
120
|
+
rowId,
|
|
121
|
+
initialSave,
|
|
122
|
+
requestOrConnection,
|
|
123
|
+
) &&
|
|
124
|
+
objMap(row, (cell, cellId) =>
|
|
125
|
+
isUndefined(cell)
|
|
126
|
+
? !initialSave &&
|
|
127
|
+
that.canDelCell(
|
|
128
|
+
tableId,
|
|
129
|
+
rowId,
|
|
130
|
+
cellId,
|
|
131
|
+
requestOrConnection,
|
|
132
|
+
) &&
|
|
133
|
+
arrayPush(
|
|
134
|
+
promises,
|
|
135
|
+
storage.delete(
|
|
136
|
+
constructStorageKey(
|
|
137
|
+
storagePrefix,
|
|
138
|
+
T,
|
|
139
|
+
tableId,
|
|
140
|
+
rowId,
|
|
141
|
+
cellId,
|
|
142
|
+
),
|
|
143
|
+
),
|
|
144
|
+
)
|
|
145
|
+
: that.canSetCell(
|
|
146
|
+
tableId,
|
|
147
|
+
rowId,
|
|
148
|
+
cellId,
|
|
149
|
+
cell,
|
|
150
|
+
initialSave,
|
|
151
|
+
requestOrConnection,
|
|
152
|
+
) &&
|
|
153
|
+
arrayPush(
|
|
154
|
+
promises,
|
|
155
|
+
storage.put(
|
|
156
|
+
constructStorageKey(
|
|
157
|
+
storagePrefix,
|
|
158
|
+
T,
|
|
159
|
+
tableId,
|
|
160
|
+
rowId,
|
|
161
|
+
cellId,
|
|
162
|
+
),
|
|
163
|
+
cell,
|
|
164
|
+
),
|
|
165
|
+
),
|
|
106
166
|
),
|
|
107
167
|
),
|
|
108
168
|
);
|
|
109
169
|
objMap(transactionChanges[1], (value, valueId) =>
|
|
110
|
-
|
|
170
|
+
isUndefined(value)
|
|
171
|
+
? !initialSave &&
|
|
172
|
+
that.canDelValue(valueId, requestOrConnection) &&
|
|
173
|
+
arrayPush(promises, storage.delete(storagePrefix + V + valueId))
|
|
174
|
+
: that.canSetValue(valueId, value, initialSave, requestOrConnection) &&
|
|
175
|
+
arrayPush(promises, storage.put(storagePrefix + V + valueId, value)),
|
|
111
176
|
);
|
|
112
177
|
if (!arrayIsEmpty(keyPrefixesToDelete)) {
|
|
113
178
|
mapForEach(await storage.list(), (key) =>
|
|
114
179
|
arrayEvery(
|
|
115
180
|
keyPrefixesToDelete,
|
|
116
181
|
(keyPrefixToDelete) =>
|
|
117
|
-
!key
|
|
118
|
-
(
|
|
182
|
+
!strStartsWith(key, keyPrefixToDelete) ||
|
|
183
|
+
(arrayPush(promises, storage.delete(key)) && 0),
|
|
119
184
|
),
|
|
120
185
|
);
|
|
121
186
|
}
|
|
122
187
|
await promiseAll(promises);
|
|
123
188
|
};
|
|
124
|
-
const
|
|
125
|
-
type
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
);
|
|
189
|
+
const constructStorageKey = (storagePrefix, type, ...ids) =>
|
|
190
|
+
construct(storagePrefix, type, slice(jsonString(ids), 1, -1));
|
|
191
|
+
const createResponse = async (that, status, body = null) =>
|
|
192
|
+
new Response(body, {
|
|
193
|
+
status,
|
|
194
|
+
headers: that.config.responseHeaders ?? RESPONSE_HEADERS,
|
|
195
|
+
});
|
|
131
196
|
class TinyBasePartyKitServer {
|
|
132
197
|
constructor(party) {
|
|
133
198
|
this.party = party;
|
|
134
199
|
this.config = {};
|
|
135
|
-
this.createResponse = (status, body = null) =>
|
|
136
|
-
new Response(body, {
|
|
137
|
-
status,
|
|
138
|
-
headers: this.config.responseHeaders ?? RESPONSE_HEADERS,
|
|
139
|
-
});
|
|
140
200
|
}
|
|
141
201
|
async onRequest(request) {
|
|
142
|
-
const storage = this.party.storage;
|
|
143
|
-
const prefix = this.config.storagePrefix ?? EMPTY_STRING;
|
|
144
202
|
const storePath = this.config.storePath ?? STORE_PATH;
|
|
145
203
|
if (new URL(request.url).pathname.endsWith(storePath)) {
|
|
146
|
-
const
|
|
204
|
+
const hasExistingStore = await hasStore(this);
|
|
147
205
|
const text = await request.text();
|
|
148
206
|
if (request.method == PUT) {
|
|
149
|
-
if (
|
|
150
|
-
return
|
|
207
|
+
if (hasExistingStore) {
|
|
208
|
+
return createResponse(this, 205);
|
|
151
209
|
}
|
|
152
|
-
await
|
|
153
|
-
return
|
|
210
|
+
await saveStore(this, jsonParse(text), true, request);
|
|
211
|
+
return createResponse(this, 201);
|
|
154
212
|
}
|
|
155
|
-
return
|
|
213
|
+
return createResponse(
|
|
214
|
+
this,
|
|
156
215
|
200,
|
|
157
|
-
|
|
158
|
-
? jsonString(await loadStoreFromStorage(storage, prefix))
|
|
159
|
-
: EMPTY_STRING,
|
|
216
|
+
hasExistingStore ? jsonString(await loadStore(this)) : EMPTY_STRING,
|
|
160
217
|
);
|
|
161
218
|
}
|
|
162
|
-
return
|
|
219
|
+
return createResponse(this, 404);
|
|
163
220
|
}
|
|
164
|
-
async onMessage(message,
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
221
|
+
async onMessage(message, connection) {
|
|
222
|
+
const messagePrefix = this.config.messagePrefix ?? EMPTY_STRING;
|
|
223
|
+
await ifNotUndefined(
|
|
224
|
+
deconstruct(messagePrefix, message, 1),
|
|
225
|
+
async ([type, payload]) => {
|
|
226
|
+
if (type == SET_CHANGES && (await hasStore(this))) {
|
|
227
|
+
await saveStore(this, payload, false, connection);
|
|
228
|
+
this.party.broadcast(construct(messagePrefix, SET_CHANGES, payload));
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
canSetTable(_tableId, _initialSave, _requestOrConnection) {
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
canDelTable(_tableId, _connection) {
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
canSetRow(_tableId, _rowId, _initialSave, _requestOrConnection) {
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
canDelRow(_tableId, _rowId, _connection) {
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
canSetCell(
|
|
246
|
+
_tableId,
|
|
247
|
+
_rowId,
|
|
248
|
+
_cellId,
|
|
249
|
+
_cell,
|
|
250
|
+
_initialSave,
|
|
251
|
+
_requestOrConnection,
|
|
252
|
+
) {
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
canDelCell(_tableId, _rowId, _cellId, _connection) {
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
canSetValue(_valueId, _value, _initialSave, _requestOrConnection) {
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
canDelValue(_valueId, _connection) {
|
|
262
|
+
return true;
|
|
172
263
|
}
|
|
173
264
|
}
|
|
174
265
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const t=t=>typeof t,e=
|
|
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};
|
|
Binary file
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=e=>typeof e,t=e(
|
|
1
|
+
const e=e=>typeof e,t="",n=e(t),r="t",l=(e,t)=>e.startsWith(t),s=Promise,i=e=>null==e,a=(e,t,n)=>i(e)?null==n?void 0:n():t(e),o=(e,t,n)=>e.slice(t,n),u=e=>e.length,c=(e,t)=>e.map(t),h=(e,...t)=>e.push(...t),d=Object,v=(e=[])=>d.fromEntries(e),f=(e,t)=>c(d.entries(e),(([e,n])=>t(n,e))),y=(e,t,n)=>(((e,t)=>!i(((e,t)=>a(e,(e=>e[t])))(e,t)))(e,t)||(e[t]=n()),e[t]),p=e=>JSON.stringify(e,((e,t)=>t instanceof Map?d.fromEntries([...t]):t)),g=JSON.parse,S=(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?g:String)(o(t,r+1))]:void 0},w=(e,t)=>((e,t)=>null==e?void 0:e.forEach(t))(e,((e,n)=>t(n,e)));var P=(e,t,n)=>new Promise(((r,l)=>{var s=e=>{try{a(n.next(e))}catch(e){l(e)}},i=e=>{try{a(n.throw(e))}catch(e){l(e)}},a=e=>e.done?r(e.value):Promise.resolve(e.value).then(s,i);a((n=n.apply(e,t)).next())}));const x="hasStore",D=v(c(["Origin","Methods","Headers"],(e=>["Access-Control-Allow-"+e,"*"]))),R=e=>P(void 0,null,(function*(){var n;return yield e.party.storage.get((null!=(n=e.config.storagePrefix)?n:t)+x)})),b=(e,n,a,o)=>P(void 0,null,(function*(){var c;const d=e.party.storage,v=null!=(c=e.config.storagePrefix)?c:t,y=[d.put(v+x,1)],p=[];f(n[0],((t,n)=>i(t)?!a&&e.canDelTable(n,o)&&((e,...t)=>e.unshift(...t))(p,C(v,r,n)):e.canSetTable(n,a,o)&&f(t,((t,l)=>i(t)?!a&&e.canDelRow(n,l,o)&&h(p,C(v,r,n,l)):e.canSetRow(n,l,a,o)&&f(t,((t,s)=>i(t)?!a&&e.canDelCell(n,l,s,o)&&h(y,d.delete(C(v,r,n,l,s))):e.canSetCell(n,l,s,t,a,o)&&h(y,d.put(C(v,r,n,l,s),t)))))))),f(n[1],((t,n)=>i(t)?!a&&e.canDelValue(n,o)&&h(y,d.delete(v+"v"+n)):e.canSetValue(n,t,a,o)&&h(y,d.put(v+"v"+n,t)))),0!=u(p)&&w(yield d.list(),(e=>p.every((t=>!l(e,t)||h(y,d.delete(e))&&0)))),yield(e=>{return t=function*(){return s.all(e)},new Promise(((e,n)=>{var r=e=>{try{s(t.next(e))}catch(e){n(e)}},l=e=>{try{s(t.throw(e))}catch(e){n(e)}},s=t=>t.done?e(t.value):Promise.resolve(t.value).then(r,l);s((t=t.apply(void 0,null)).next())}));var t})(y)})),C=(e,t,...n)=>S(e,t,o(p(n),1,-1)),T=(e,t,n=null)=>P(void 0,null,(function*(){var r;return new Response(n,{status:t,headers:null!=(r=e.config.responseHeaders)?r:D})}));class O{constructor(e){this.party=e,this.config={}}onRequest(e){return P(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 R(this),l=yield e.text();return"PUT"==e.method?n?T(this,205):(yield b(this,g(l),!0,e),T(this,201)):T(this,200,n?p(yield(s=this,P(void 0,null,(function*(){var e;const n={},l={},i=null!=(e=s.config.storagePrefix)?e:t;return w(yield s.party.storage.list(),((e,t)=>a(m(i,e),(([e,s])=>{if(e==r){const[e,r,l]=g("["+s+"]");y(y(n,e,v),r,v)[l]=t}else"v"==e&&(l[s]=t)})))),[n,l]})))):t)}var s;return T(this,404)}))}onMessage(e,n){return P(this,null,(function*(){var r;const l=null!=(r=this.config.messagePrefix)?r:t;yield a(m(l,e,1),(e=>P(this,[e],(function*([e,t]){"s"==e&&(yield R(this))&&(yield b(this,t,!1,n),this.party.broadcast(S(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,s){return!0}canDelCell(e,t,n,r){return!0}canSetValue(e,t,n,r){return!0}canDelValue(e,t){return!0}}export{O as TinyBasePartyKitServer};
|
|
Binary file
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const t=t=>typeof t,a=
|
|
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};
|
|
Binary file
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const t=t=>typeof t,
|
|
1
|
+
const t=t=>typeof t,e="",a=t(e),s="t",n=(t,e)=>t.startsWith(e),r=Promise,i=t=>null==t,c=(t,e,a)=>i(t)?a?.():e(t),o=(t,e,a)=>t.slice(e,a),l=t=>t.length,u=(t,e)=>t.map(e),h=(t,...e)=>t.push(...e),f=Object,g=(t=[])=>f.fromEntries(t),w=(t,e)=>u(f.entries(t),(([t,a])=>e(a,t))),p=(t,e,a)=>(((t,e)=>!i(((t,e)=>c(t,(t=>t[e])))(t,e)))(t,e)||(t[e]=a()),t[e]),y=t=>JSON.stringify(t,((t,e)=>e instanceof Map?f.fromEntries([...e]):e)),S=JSON.parse,d=(e,s,n)=>e+s+(t(n)==a?n:y(n)),D=(t,e,a)=>{const s=l(t);return n(e,t)?[e[s],(a?S:String)(o(e,s+1))]:void 0},m=(t,e)=>((t,e)=>t?.forEach(e))(t,((t,a)=>e(a,t))),P="hasStore",R=g(u(["Origin","Methods","Headers"],(t=>["Access-Control-Allow-"+t,"*"]))),b=async t=>await t.party.storage.get((t.config.storagePrefix??e)+P),x=async(t,a,c,o)=>{const u=t.party.storage,f=t.config.storagePrefix??e,g=[u.put(f+P,1)],p=[];w(a[0],((e,a)=>i(e)?!c&&t.canDelTable(a,o)&&((t,...e)=>t.unshift(...e))(p,v(f,s,a)):t.canSetTable(a,c,o)&&w(e,((e,n)=>i(e)?!c&&t.canDelRow(a,n,o)&&h(p,v(f,s,a,n)):t.canSetRow(a,n,c,o)&&w(e,((e,r)=>i(e)?!c&&t.canDelCell(a,n,r,o)&&h(g,u.delete(v(f,s,a,n,r))):t.canSetCell(a,n,r,e,c,o)&&h(g,u.put(v(f,s,a,n,r),e)))))))),w(a[1],((e,a)=>i(e)?!c&&t.canDelValue(a,o)&&h(g,u.delete(f+"v"+a)):t.canSetValue(a,e,c,o)&&h(g,u.put(f+"v"+a,e)))),0!=l(p)&&m(await u.list(),(t=>p.every((e=>!n(t,e)||h(g,u.delete(t))&&0)))),await(async t=>r.all(t))(g)},v=(t,e,...a)=>d(t,e,o(y(a),1,-1)),C=async(t,e,a=null)=>new Response(a,{status:e,headers:t.config.responseHeaders??R});class T{constructor(t){this.party=t,this.config={}}async onRequest(t){const a=this.config.storePath??"/store";if(new URL(t.url).pathname.endsWith(a)){const a=await b(this),n=await t.text();return"PUT"==t.method?a?C(this,205):(await x(this,S(n),!0,t),C(this,201)):C(this,200,a?y(await(async t=>{const a={},n={},r=t.config.storagePrefix??e;return m(await t.party.storage.list(),((t,e)=>c(D(r,t),(([t,r])=>{if(t==s){const[t,s,n]=S("["+r+"]");p(p(a,t,g),s,g)[n]=e}else"v"==t&&(n[r]=e)})))),[a,n]})(this)):e)}return C(this,404)}async onMessage(t,a){const s=this.config.messagePrefix??e;await c(D(s,t,1),(async([t,e])=>{"s"==t&&await b(this)&&(await x(this,e,!1,a),this.party.broadcast(d(s,"s",e)))}))}canSetTable(t,e,a){return!0}canDelTable(t,e){return!0}canSetRow(t,e,a,s){return!0}canDelRow(t,e,a){return!0}canSetCell(t,e,a,s,n,r){return!0}canDelCell(t,e,a,s){return!0}canSetValue(t,e,a,s){return!0}canDelValue(t,e){return!0}}export{T as TinyBasePartyKitServer};
|
|
Binary file
|