@lemoncloud/chatic-apps-sdk 0.2.0 → 0.3.0

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.
@@ -0,0 +1,175 @@
1
+ class u extends Error {
2
+ code;
3
+ /** What the app sent with the failure, if anything: `{ retryAfterMs }` with `RATE_LIMITED`. */
4
+ details;
5
+ constructor(o, e, n) {
6
+ super(e), this.name = "ChaticAppsError", this.code = o, n !== void 0 && (this.details = n);
7
+ }
8
+ }
9
+ const T = "2.33.0", A = 1e4, S = 2e3;
10
+ let O = 0;
11
+ const D = () => `cas-${Date.now().toString(36)}-${(++O).toString(36)}`, p = () => typeof document < "u" && document.visibilityState === "hidden", j = (r, o = A) => {
12
+ const e = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map();
13
+ let i = !1;
14
+ const l = (s, t) => {
15
+ t.remaining === null || p() || (t.startedAt = Date.now(), t.timer = setTimeout(() => {
16
+ e.delete(s), t.reject(new u("TIMEOUT", "The app did not answer in time."));
17
+ }, t.remaining));
18
+ }, a = () => {
19
+ for (const [s, t] of e)
20
+ t.remaining !== null && (p() ? (t.timer && clearTimeout(t.timer), t.timer = null, t.remaining = Math.max(0, t.remaining - (Date.now() - t.startedAt))) : t.timer || (t.remaining = Math.max(t.remaining, S), l(s, t)));
21
+ };
22
+ typeof document < "u" && document.addEventListener("visibilitychange", a);
23
+ const f = r.listen((s) => {
24
+ let t;
25
+ try {
26
+ t = JSON.parse(s);
27
+ } catch {
28
+ return;
29
+ }
30
+ if (!t || typeof t != "object" || typeof t.type != "string") return;
31
+ const c = typeof t.refId == "string" ? e.get(t.refId) : void 0;
32
+ if (c && typeof t.refId == "string") {
33
+ if (e.delete(t.refId), c.timer && clearTimeout(c.timer), t.success === !0)
34
+ c.resolve(t.data);
35
+ else {
36
+ const d = typeof t.error?.code == "string" ? t.error.code : "INTERNAL_ERROR", g = typeof t.error?.message == "string" ? t.error.message : d;
37
+ c.reject(new u(d, g, t.error?.details));
38
+ }
39
+ return;
40
+ }
41
+ n.get(t.type)?.forEach((d) => {
42
+ try {
43
+ d(t.data);
44
+ } catch {
45
+ }
46
+ });
47
+ });
48
+ return {
49
+ request: (s, t = {}, c = {}) => new Promise((d, g) => {
50
+ if (i) {
51
+ g(new u("DISCONNECTED", "The connection to the app was closed."));
52
+ return;
53
+ }
54
+ const h = D(), w = c.timeoutMs === void 0 ? o : c.timeoutMs, y = {
55
+ resolve: d,
56
+ reject: g,
57
+ remaining: w,
58
+ startedAt: Date.now(),
59
+ timer: null
60
+ };
61
+ e.set(h, y), l(h, y), r.send(JSON.stringify({ type: s, data: t, refId: h, version: T }));
62
+ }),
63
+ onPush: (s, t) => {
64
+ let c = n.get(s);
65
+ return c || n.set(s, c = /* @__PURE__ */ new Set()), c.add(t), () => c?.delete(t);
66
+ },
67
+ close: () => {
68
+ if (!i) {
69
+ i = !0, f(), typeof document < "u" && document.removeEventListener("visibilitychange", a);
70
+ for (const s of e.values())
71
+ s.timer && clearTimeout(s.timer), s.reject(new u("DISCONNECTED", "The connection to the app was closed."));
72
+ e.clear(), n.clear();
73
+ }
74
+ }
75
+ };
76
+ }, v = (r) => {
77
+ try {
78
+ return JSON.stringify(r) ?? null;
79
+ } catch {
80
+ return null;
81
+ }
82
+ }, N = (r) => {
83
+ const o = (e) => Object.values(e ?? {}).every((n) => v(n) !== null) ? r.set(e).then(() => {
84
+ }) : Promise.reject(
85
+ new u("INVALID", "Values must be JSON; remove a key rather than storing undefined.")
86
+ );
87
+ return {
88
+ get: (e) => r.get([e]).then((n) => n.entries[e]),
89
+ getMany: (e) => r.get(e).then((n) => n.entries),
90
+ set: (e, n) => o({ [e]: n }),
91
+ setMany: o,
92
+ remove: (e) => r.remove(typeof e == "string" ? [e] : e).then(() => {
93
+ }),
94
+ keys: () => r.info().then((e) => e.keys),
95
+ clear: () => r.clear().then(() => {
96
+ }),
97
+ usage: () => r.info().then(({ used: e, limit: n }) => ({ used: e, limit: n }))
98
+ };
99
+ }, I = 1024 * 1024, _ = 200, M = 64 * 1024, E = (r) => typeof r == "string" && r.length >= 1 && r.length <= _ && r !== "__proto__", L = (r) => {
100
+ const o = () => r.keys().reduce((n, i) => n + i.length + (r.read(i)?.length ?? 0), 0), e = () => ({ used: o(), limit: I });
101
+ return {
102
+ get(n) {
103
+ if (!Array.isArray(n) || !n.every(E)) return { ok: !1, code: "INVALID" };
104
+ const i = {};
105
+ for (const l of n) {
106
+ const a = r.read(l);
107
+ if (a !== void 0)
108
+ try {
109
+ i[l] = JSON.parse(a);
110
+ } catch {
111
+ }
112
+ }
113
+ return { ok: !0, value: { entries: i } };
114
+ },
115
+ set(n) {
116
+ if (!n || typeof n != "object" || Array.isArray(n))
117
+ return { ok: !1, code: "INVALID" };
118
+ const i = [];
119
+ let l = o();
120
+ for (const [a, f] of Object.entries(n)) {
121
+ const s = v(f);
122
+ if (!E(a) || s === null) return { ok: !1, code: "INVALID" };
123
+ const t = r.read(a);
124
+ l += s.length - (t === void 0 ? -a.length : t.length), i.push([a, s]);
125
+ }
126
+ return l > I ? { ok: !1, code: "QUOTA_EXCEEDED" } : (i.forEach(([a, f]) => r.write(a, f)), { ok: !0, value: e() });
127
+ },
128
+ remove(n) {
129
+ return !Array.isArray(n) || !n.every(E) ? { ok: !1, code: "INVALID" } : (n.forEach((i) => r.delete(i)), { ok: !0, value: e() });
130
+ },
131
+ clear() {
132
+ return r.keys().forEach((n) => r.delete(n)), { ok: !0, value: {} };
133
+ },
134
+ info() {
135
+ return { ok: !0, value: { keys: r.keys(), ...e() } };
136
+ }
137
+ };
138
+ }, m = "chatic-apps.storage:", R = (r) => ({
139
+ read: (o) => r.getItem(m + o) ?? void 0,
140
+ write: (o, e) => r.setItem(m + o, e),
141
+ delete: (o) => r.removeItem(m + o),
142
+ keys: () => {
143
+ const o = [];
144
+ for (let e = 0; e < r.length; e++) {
145
+ const n = r.key(e);
146
+ n?.startsWith(m) && o.push(n.slice(m.length));
147
+ }
148
+ return o;
149
+ }
150
+ }), C = () => {
151
+ const r = (o) => {
152
+ let e;
153
+ try {
154
+ e = o(L(R(window.localStorage)));
155
+ } catch (n) {
156
+ return n?.name === "QuotaExceededError" ? Promise.reject(new u("QUOTA_EXCEEDED", "The browser's own storage is full.")) : Promise.reject(new u("NOT_SUPPORTED", "This browser refuses local storage."));
157
+ }
158
+ return e.ok ? Promise.resolve(e.value) : Promise.reject(new u(e.code, `The storage refused the call: ${e.code}.`));
159
+ };
160
+ return N({
161
+ get: (o) => r((e) => e.get(o)),
162
+ set: (o) => (v({ entries: o })?.length ?? 0) > M ? Promise.reject(new u("TOO_LARGE", "One call carries at most 65,536 characters.")) : r((e) => e.set(o)),
163
+ remove: (o) => r((e) => e.remove(o)),
164
+ clear: () => r((o) => o.clear()),
165
+ info: () => r((o) => o.info())
166
+ });
167
+ };
168
+ export {
169
+ u as C,
170
+ T as S,
171
+ C as b,
172
+ j as c,
173
+ L as r,
174
+ N as s
175
+ };
@@ -0,0 +1 @@
1
+ "use strict";class u extends Error{code;details;constructor(n,e,o){super(e),this.name="ChaticAppsError",this.code=n,o!==void 0&&(this.details=o)}}const w="2.33.0",O=1e4,D=2e3;let _=0;const N=()=>`cas-${Date.now().toString(36)}-${(++_).toString(36)}`,p=()=>typeof document<"u"&&document.visibilityState==="hidden",M=(r,n=O)=>{const e=new Map,o=new Map;let i=!1;const l=(s,t)=>{t.remaining===null||p()||(t.startedAt=Date.now(),t.timer=setTimeout(()=>{e.delete(s),t.reject(new u("TIMEOUT","The app did not answer in time."))},t.remaining))},a=()=>{for(const[s,t]of e)t.remaining!==null&&(p()?(t.timer&&clearTimeout(t.timer),t.timer=null,t.remaining=Math.max(0,t.remaining-(Date.now()-t.startedAt))):t.timer||(t.remaining=Math.max(t.remaining,D),l(s,t)))};typeof document<"u"&&document.addEventListener("visibilitychange",a);const f=r.listen(s=>{let t;try{t=JSON.parse(s)}catch{return}if(!t||typeof t!="object"||typeof t.type!="string")return;const c=typeof t.refId=="string"?e.get(t.refId):void 0;if(c&&typeof t.refId=="string"){if(e.delete(t.refId),c.timer&&clearTimeout(c.timer),t.success===!0)c.resolve(t.data);else{const d=typeof t.error?.code=="string"?t.error.code:"INTERNAL_ERROR",g=typeof t.error?.message=="string"?t.error.message:d;c.reject(new u(d,g,t.error?.details))}return}o.get(t.type)?.forEach(d=>{try{d(t.data)}catch{}})});return{request:(s,t={},c={})=>new Promise((d,g)=>{if(i){g(new u("DISCONNECTED","The connection to the app was closed."));return}const h=N(),A=c.timeoutMs===void 0?n:c.timeoutMs,y={resolve:d,reject:g,remaining:A,startedAt:Date.now(),timer:null};e.set(h,y),l(h,y),r.send(JSON.stringify({type:s,data:t,refId:h,version:w}))}),onPush:(s,t)=>{let c=o.get(s);return c||o.set(s,c=new Set),c.add(t),()=>c?.delete(t)},close:()=>{if(!i){i=!0,f(),typeof document<"u"&&document.removeEventListener("visibilitychange",a);for(const s of e.values())s.timer&&clearTimeout(s.timer),s.reject(new u("DISCONNECTED","The connection to the app was closed."));e.clear(),o.clear()}}}},v=r=>{try{return JSON.stringify(r)??null}catch{return null}},S=r=>{const n=e=>Object.values(e??{}).every(o=>v(o)!==null)?r.set(e).then(()=>{}):Promise.reject(new u("INVALID","Values must be JSON; remove a key rather than storing undefined."));return{get:e=>r.get([e]).then(o=>o.entries[e]),getMany:e=>r.get(e).then(o=>o.entries),set:(e,o)=>n({[e]:o}),setMany:n,remove:e=>r.remove(typeof e=="string"?[e]:e).then(()=>{}),keys:()=>r.info().then(e=>e.keys),clear:()=>r.clear().then(()=>{}),usage:()=>r.info().then(({used:e,limit:o})=>({used:e,limit:o}))}},I=1024*1024,R=200,L=64*1024,E=r=>typeof r=="string"&&r.length>=1&&r.length<=R&&r!=="__proto__",T=r=>{const n=()=>r.keys().reduce((o,i)=>o+i.length+(r.read(i)?.length??0),0),e=()=>({used:n(),limit:I});return{get(o){if(!Array.isArray(o)||!o.every(E))return{ok:!1,code:"INVALID"};const i={};for(const l of o){const a=r.read(l);if(a!==void 0)try{i[l]=JSON.parse(a)}catch{}}return{ok:!0,value:{entries:i}}},set(o){if(!o||typeof o!="object"||Array.isArray(o))return{ok:!1,code:"INVALID"};const i=[];let l=n();for(const[a,f]of Object.entries(o)){const s=v(f);if(!E(a)||s===null)return{ok:!1,code:"INVALID"};const t=r.read(a);l+=s.length-(t===void 0?-a.length:t.length),i.push([a,s])}return l>I?{ok:!1,code:"QUOTA_EXCEEDED"}:(i.forEach(([a,f])=>r.write(a,f)),{ok:!0,value:e()})},remove(o){return!Array.isArray(o)||!o.every(E)?{ok:!1,code:"INVALID"}:(o.forEach(i=>r.delete(i)),{ok:!0,value:e()})},clear(){return r.keys().forEach(o=>r.delete(o)),{ok:!0,value:{}}},info(){return{ok:!0,value:{keys:r.keys(),...e()}}}}},m="chatic-apps.storage:",j=r=>({read:n=>r.getItem(m+n)??void 0,write:(n,e)=>r.setItem(m+n,e),delete:n=>r.removeItem(m+n),keys:()=>{const n=[];for(let e=0;e<r.length;e++){const o=r.key(e);o?.startsWith(m)&&n.push(o.slice(m.length))}return n}}),C=()=>{const r=n=>{let e;try{e=n(T(j(window.localStorage)))}catch(o){return o?.name==="QuotaExceededError"?Promise.reject(new u("QUOTA_EXCEEDED","The browser's own storage is full.")):Promise.reject(new u("NOT_SUPPORTED","This browser refuses local storage."))}return e.ok?Promise.resolve(e.value):Promise.reject(new u(e.code,`The storage refused the call: ${e.code}.`))};return S({get:n=>r(e=>e.get(n)),set:n=>(v({entries:n})?.length??0)>L?Promise.reject(new u("TOO_LARGE","One call carries at most 65,536 characters.")):r(e=>e.set(n)),remove:n=>r(e=>e.remove(n)),clear:()=>r(n=>n.clear()),info:()=>r(n=>n.info())})};exports.ChaticAppsError=u;exports.SDK_BRIDGE_VERSION=w;exports.browserStorage=C;exports.createBridgeClient=M;exports.ruledStore=T;exports.storageOver=S;
@@ -1,104 +0,0 @@
1
- class u extends Error {
2
- code;
3
- constructor(o, e) {
4
- super(e), this.name = "ChaticAppsError", this.code = o;
5
- }
6
- }
7
- const g = (r) => {
8
- try {
9
- return JSON.stringify(r) ?? null;
10
- } catch {
11
- return null;
12
- }
13
- }, v = (r) => {
14
- const o = (e) => Object.values(e ?? {}).every((t) => g(t) !== null) ? r.set(e).then(() => {
15
- }) : Promise.reject(
16
- new u("INVALID", "Values must be JSON; remove a key rather than storing undefined.")
17
- );
18
- return {
19
- get: (e) => r.get([e]).then((t) => t.entries[e]),
20
- getMany: (e) => r.get(e).then((t) => t.entries),
21
- set: (e, t) => o({ [e]: t }),
22
- setMany: o,
23
- remove: (e) => r.remove(typeof e == "string" ? [e] : e).then(() => {
24
- }),
25
- keys: () => r.info().then((e) => e.keys),
26
- clear: () => r.clear().then(() => {
27
- }),
28
- usage: () => r.info().then(({ used: e, limit: t }) => ({ used: e, limit: t }))
29
- };
30
- }, f = 1024 * 1024, E = 200, m = 64 * 1024, h = (r) => typeof r == "string" && r.length >= 1 && r.length <= E && r !== "__proto__", y = (r) => {
31
- const o = () => r.keys().reduce((t, s) => t + s.length + (r.read(s)?.length ?? 0), 0), e = () => ({ used: o(), limit: f });
32
- return {
33
- get(t) {
34
- if (!Array.isArray(t) || !t.every(h)) return { ok: !1, code: "INVALID" };
35
- const s = {};
36
- for (const a of t) {
37
- const n = r.read(a);
38
- if (n !== void 0)
39
- try {
40
- s[a] = JSON.parse(n);
41
- } catch {
42
- }
43
- }
44
- return { ok: !0, value: { entries: s } };
45
- },
46
- set(t) {
47
- if (!t || typeof t != "object" || Array.isArray(t))
48
- return { ok: !1, code: "INVALID" };
49
- const s = [];
50
- let a = o();
51
- for (const [n, l] of Object.entries(t)) {
52
- const i = g(l);
53
- if (!h(n) || i === null) return { ok: !1, code: "INVALID" };
54
- const d = r.read(n);
55
- a += i.length - (d === void 0 ? -n.length : d.length), s.push([n, i]);
56
- }
57
- return a > f ? { ok: !1, code: "QUOTA_EXCEEDED" } : (s.forEach(([n, l]) => r.write(n, l)), { ok: !0, value: e() });
58
- },
59
- remove(t) {
60
- return !Array.isArray(t) || !t.every(h) ? { ok: !1, code: "INVALID" } : (t.forEach((s) => r.delete(s)), { ok: !0, value: e() });
61
- },
62
- clear() {
63
- return r.keys().forEach((t) => r.delete(t)), { ok: !0, value: {} };
64
- },
65
- info() {
66
- return { ok: !0, value: { keys: r.keys(), ...e() } };
67
- }
68
- };
69
- }, c = "chatic-apps.storage:", I = (r) => ({
70
- read: (o) => r.getItem(c + o) ?? void 0,
71
- write: (o, e) => r.setItem(c + o, e),
72
- delete: (o) => r.removeItem(c + o),
73
- keys: () => {
74
- const o = [];
75
- for (let e = 0; e < r.length; e++) {
76
- const t = r.key(e);
77
- t?.startsWith(c) && o.push(t.slice(c.length));
78
- }
79
- return o;
80
- }
81
- }), A = () => {
82
- const r = (o) => {
83
- let e;
84
- try {
85
- e = o(y(I(window.localStorage)));
86
- } catch (t) {
87
- return t?.name === "QuotaExceededError" ? Promise.reject(new u("QUOTA_EXCEEDED", "The browser's own storage is full.")) : Promise.reject(new u("NOT_SUPPORTED", "This browser refuses local storage."));
88
- }
89
- return e.ok ? Promise.resolve(e.value) : Promise.reject(new u(e.code, `The storage refused the call: ${e.code}.`));
90
- };
91
- return v({
92
- get: (o) => r((e) => e.get(o)),
93
- set: (o) => (g({ entries: o })?.length ?? 0) > m ? Promise.reject(new u("TOO_LARGE", "One call carries at most 65,536 characters.")) : r((e) => e.set(o)),
94
- remove: (o) => r((e) => e.remove(o)),
95
- clear: () => r((o) => o.clear()),
96
- info: () => r((o) => o.info())
97
- });
98
- };
99
- export {
100
- u as C,
101
- A as b,
102
- y as r,
103
- v as s
104
- };
@@ -1 +0,0 @@
1
- "use strict";class c extends Error{code;constructor(o,e){super(e),this.name="ChaticAppsError",this.code=o}}const g=r=>{try{return JSON.stringify(r)??null}catch{return null}},f=r=>{const o=e=>Object.values(e??{}).every(t=>g(t)!==null)?r.set(e).then(()=>{}):Promise.reject(new c("INVALID","Values must be JSON; remove a key rather than storing undefined."));return{get:e=>r.get([e]).then(t=>t.entries[e]),getMany:e=>r.get(e).then(t=>t.entries),set:(e,t)=>o({[e]:t}),setMany:o,remove:e=>r.remove(typeof e=="string"?[e]:e).then(()=>{}),keys:()=>r.info().then(e=>e.keys),clear:()=>r.clear().then(()=>{}),usage:()=>r.info().then(({used:e,limit:t})=>({used:e,limit:t}))}},v=1024*1024,m=200,y=64*1024,h=r=>typeof r=="string"&&r.length>=1&&r.length<=m&&r!=="__proto__",E=r=>{const o=()=>r.keys().reduce((t,s)=>t+s.length+(r.read(s)?.length??0),0),e=()=>({used:o(),limit:v});return{get(t){if(!Array.isArray(t)||!t.every(h))return{ok:!1,code:"INVALID"};const s={};for(const a of t){const n=r.read(a);if(n!==void 0)try{s[a]=JSON.parse(n)}catch{}}return{ok:!0,value:{entries:s}}},set(t){if(!t||typeof t!="object"||Array.isArray(t))return{ok:!1,code:"INVALID"};const s=[];let a=o();for(const[n,i]of Object.entries(t)){const l=g(i);if(!h(n)||l===null)return{ok:!1,code:"INVALID"};const d=r.read(n);a+=l.length-(d===void 0?-n.length:d.length),s.push([n,l])}return a>v?{ok:!1,code:"QUOTA_EXCEEDED"}:(s.forEach(([n,i])=>r.write(n,i)),{ok:!0,value:e()})},remove(t){return!Array.isArray(t)||!t.every(h)?{ok:!1,code:"INVALID"}:(t.forEach(s=>r.delete(s)),{ok:!0,value:e()})},clear(){return r.keys().forEach(t=>r.delete(t)),{ok:!0,value:{}}},info(){return{ok:!0,value:{keys:r.keys(),...e()}}}}},u="chatic-apps.storage:",I=r=>({read:o=>r.getItem(u+o)??void 0,write:(o,e)=>r.setItem(u+o,e),delete:o=>r.removeItem(u+o),keys:()=>{const o=[];for(let e=0;e<r.length;e++){const t=r.key(e);t?.startsWith(u)&&o.push(t.slice(u.length))}return o}}),A=()=>{const r=o=>{let e;try{e=o(E(I(window.localStorage)))}catch(t){return t?.name==="QuotaExceededError"?Promise.reject(new c("QUOTA_EXCEEDED","The browser's own storage is full.")):Promise.reject(new c("NOT_SUPPORTED","This browser refuses local storage."))}return e.ok?Promise.resolve(e.value):Promise.reject(new c(e.code,`The storage refused the call: ${e.code}.`))};return f({get:o=>r(e=>e.get(o)),set:o=>(g({entries:o})?.length??0)>y?Promise.reject(new c("TOO_LARGE","One call carries at most 65,536 characters.")):r(e=>e.set(o)),remove:o=>r(e=>e.remove(o)),clear:()=>r(o=>o.clear()),info:()=>r(o=>o.info())})};exports.ChaticAppsError=c;exports.browserStorage=A;exports.ruledStore=E;exports.storageOver=f;