@lemoncloud/chatic-apps-sdk 0.0.0-stage → 0.1.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.
- package/README.md +288 -2
- package/chatic-apps-sdk.global.js +1 -0
- package/index.cjs +1 -0
- package/index.d.cts +192 -0
- package/index.d.ts +192 -0
- package/index.js +288 -0
- package/mock.cjs +1 -0
- package/mock.d.cts +87 -0
- package/mock.d.ts +87 -0
- package/mock.js +196 -0
- package/package.json +45 -5
- package/storage-BuvWwLx2.js +104 -0
- package/storage-yeS4HWKZ.cjs +1 -0
package/mock.js
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { r as A } from "./storage-BuvWwLx2.js";
|
|
2
|
+
const G = [
|
|
3
|
+
"WebAppReady",
|
|
4
|
+
"Ping",
|
|
5
|
+
"FinishSubWebView",
|
|
6
|
+
"SetSubWebViewTitle",
|
|
7
|
+
"TriggerHaptic",
|
|
8
|
+
"SetCanGoBack",
|
|
9
|
+
"SetSubWebViewCloseGuard",
|
|
10
|
+
"ReportSubWebViewError",
|
|
11
|
+
"GetSubWebViewStorage",
|
|
12
|
+
"SetSubWebViewStorage",
|
|
13
|
+
"RemoveSubWebViewStorage",
|
|
14
|
+
"ClearSubWebViewStorage",
|
|
15
|
+
"GetSubWebViewStorageInfo"
|
|
16
|
+
], D = {
|
|
17
|
+
clipboard: "CopyToClipboard",
|
|
18
|
+
share: "OpenShareSheet",
|
|
19
|
+
openUrl: "OpenURL",
|
|
20
|
+
userKey: "GetSubWebViewUserKey"
|
|
21
|
+
}, _ = ["selection", "impact", "success", "warning", "error"], x = 64 * 1024, M = 16 * 1024, U = 3e3, K = (s) => {
|
|
22
|
+
if (/^(mailto|tel|sms|itms-apps|market):/i.test(s)) return !0;
|
|
23
|
+
const o = /^https?:\/\/([^/?#:@]+)/i.exec(s)?.[1]?.toLowerCase();
|
|
24
|
+
return o ? !/\.(apps|apps-dev)\.chatic\.io$/.test(o) && !/^app(-dev)?\.chatic\.io$/.test(o) : !1;
|
|
25
|
+
}, B = (s, o) => {
|
|
26
|
+
if (s === void 0) return !0;
|
|
27
|
+
try {
|
|
28
|
+
const l = JSON.stringify(s);
|
|
29
|
+
return l !== void 0 && l.length <= o;
|
|
30
|
+
} catch {
|
|
31
|
+
return !1;
|
|
32
|
+
}
|
|
33
|
+
}, k = (s, o) => s == null || typeof s == "string" && s.length <= o, F = (s) => {
|
|
34
|
+
let o = "";
|
|
35
|
+
for (let l = 0; o.length < 64; l++) {
|
|
36
|
+
let p = 2166136261 ^ l;
|
|
37
|
+
for (let u = 0; u < s.length; u++) p = Math.imul(p ^ s.charCodeAt(u), 16777619);
|
|
38
|
+
o += (p >>> 0).toString(16).padStart(8, "0");
|
|
39
|
+
}
|
|
40
|
+
return o.slice(0, 64);
|
|
41
|
+
}, $ = (s) => {
|
|
42
|
+
const o = /^https:\/\/([a-z0-9-]+)\.(apps|apps-dev)\.chatic\.io$/.exec(s);
|
|
43
|
+
return o ? `https://${o[2] === "apps" ? "app" : "app-dev"}.chatic.io/apps/${o[1]}` : s;
|
|
44
|
+
}, H = (s = {}) => {
|
|
45
|
+
const o = /* @__PURE__ */ new Set([...G, ...(s.permissions ?? []).map((r) => D[r])]), l = window.location.origin, p = {
|
|
46
|
+
protocolVersion: 1,
|
|
47
|
+
viewId: "mock-view",
|
|
48
|
+
platform: "ios",
|
|
49
|
+
appVersion: "0.0.0-mock",
|
|
50
|
+
locale: "ko",
|
|
51
|
+
theme: "light",
|
|
52
|
+
params: {},
|
|
53
|
+
appLink: $(l),
|
|
54
|
+
...s.context
|
|
55
|
+
}, u = s.log === !1 ? () => {
|
|
56
|
+
} : (...r) => console.info("[chatic-apps mock]", ...r);
|
|
57
|
+
let O = !1;
|
|
58
|
+
const C = [];
|
|
59
|
+
let T, b = null;
|
|
60
|
+
const m = { theme: p.theme, locale: p.locale };
|
|
61
|
+
let I = !1, v = !1, V = null, N = 0;
|
|
62
|
+
const d = /* @__PURE__ */ new Map(), g = A({
|
|
63
|
+
read: (r) => d.get(r),
|
|
64
|
+
write: (r, n) => {
|
|
65
|
+
d.set(r, n);
|
|
66
|
+
},
|
|
67
|
+
delete: (r) => {
|
|
68
|
+
d.delete(r);
|
|
69
|
+
},
|
|
70
|
+
keys: () => [...d.keys()]
|
|
71
|
+
});
|
|
72
|
+
if (s.storage && !g.set(s.storage).ok)
|
|
73
|
+
throw new Error("[chatic-apps mock] options.storage must be JSON entries within the storage limits.");
|
|
74
|
+
const W = (r) => {
|
|
75
|
+
const n = JSON.stringify(r);
|
|
76
|
+
if (!O) {
|
|
77
|
+
C.push(n);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
setTimeout(() => window.dispatchEvent(new MessageEvent("message", { data: n })), 0);
|
|
81
|
+
}, y = (r, n) => W({ type: r, success: !0, data: n, refId: `mock-push-${Date.now().toString(36)}` }), c = (r, n, i = {}) => W({ type: r, success: !0, data: i, refId: n }), a = (r, n, i, e) => W({ type: r, success: !1, error: { code: i, message: e }, refId: n }), E = (r) => {
|
|
82
|
+
let n;
|
|
83
|
+
try {
|
|
84
|
+
n = JSON.parse(r);
|
|
85
|
+
} catch {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const { type: i, refId: e } = n;
|
|
89
|
+
if (typeof i != "string") return;
|
|
90
|
+
const t = n.data ?? {};
|
|
91
|
+
if (O || (O = !0, C.splice(0).forEach(
|
|
92
|
+
(S) => setTimeout(() => window.dispatchEvent(new MessageEvent("message", { data: S })), 0)
|
|
93
|
+
)), u("←", i, t), b)
|
|
94
|
+
return i === "FinishSubWebView" ? a("OnFinishSubWebView", e, "NOT_OPEN", "The view has closed.") : u("view closed; no answer to", i);
|
|
95
|
+
if (r.length > x)
|
|
96
|
+
return a("ERROR", e, "TOO_LARGE", "The message is over 65,536 characters.");
|
|
97
|
+
if (!o.has(i)) return a("ERROR", e, "NOT_FOUND", `${i} is not answered for this page.`);
|
|
98
|
+
switch (i) {
|
|
99
|
+
case "WebAppReady":
|
|
100
|
+
return c("OnWebAppReady", e, {
|
|
101
|
+
appVersion: "2.19.0",
|
|
102
|
+
protocolVersion: "2.19.0",
|
|
103
|
+
supportedWebMessages: [...o],
|
|
104
|
+
supportedAppMessages: [],
|
|
105
|
+
subWebView: { ...p, ...m }
|
|
106
|
+
});
|
|
107
|
+
case "Ping":
|
|
108
|
+
return c("Pong", e, { payload: t.payload ?? "" });
|
|
109
|
+
case "FinishSubWebView":
|
|
110
|
+
return B(t.result, M) ? (b = { by: "page", result: t.result }, s.onClose?.(b), u("view finished", t.result), c("OnFinishSubWebView", e)) : a("OnFinishSubWebView", e, "INVALID", "result must be JSON of at most 16 KiB.");
|
|
111
|
+
case "SetSubWebViewTitle": {
|
|
112
|
+
const S = typeof t.title == "string" ? t.title.trim() : "";
|
|
113
|
+
return !S || S.length > 100 ? a("OnSetSubWebViewTitle", e, "INVALID", "title must be 1 to 100 characters.") : (T = S, c("OnSetSubWebViewTitle", e));
|
|
114
|
+
}
|
|
115
|
+
case "TriggerHaptic":
|
|
116
|
+
return _.includes(t.kind) ? c("OnTriggerHaptic", e) : a("OnTriggerHaptic", e, "INVALID_KIND", `Unknown haptic kind: ${String(t.kind)}`);
|
|
117
|
+
case "SetCanGoBack":
|
|
118
|
+
return typeof t.canGoBack != "boolean" ? a("OnSetCanGoBack", e, "INVALID", "canGoBack must be a boolean.") : (I = t.canGoBack, c("OnSetCanGoBack", e));
|
|
119
|
+
case "SetSubWebViewCloseGuard":
|
|
120
|
+
return typeof t.enabled != "boolean" ? a("OnSetSubWebViewCloseGuard", e, "INVALID", "enabled must be a boolean.") : (v = t.enabled, c("OnSetSubWebViewCloseGuard", e));
|
|
121
|
+
case "ReportSubWebViewError":
|
|
122
|
+
return typeof t.message != "string" || !t.message.trim() ? a("OnReportSubWebViewError", e, "INVALID", "message must be a non-empty string.") : N >= 20 ? c("OnReportSubWebViewError", e, { recorded: !1 }) : (N += 1, console.warn("[chatic-apps mock] page error", t.message, t.stack ?? ""), c("OnReportSubWebViewError", e, { recorded: !0 }));
|
|
123
|
+
case "CopyToClipboard":
|
|
124
|
+
return typeof t.text != "string" ? a("OnCopyToClipboard", e, "INVALID", "text must be a string.") : (navigator.clipboard?.writeText(t.text).catch(() => {
|
|
125
|
+
}), c("OnCopyToClipboard", e, { copied: !0 }));
|
|
126
|
+
case "OpenURL":
|
|
127
|
+
return typeof t.url != "string" || !K(t.url) ? a("OnOpenURL", e, "LINK_NOT_ALLOWED", "This link cannot be opened from a page.") : (window.open(t.url, "_blank", "noopener,noreferrer"), c("OnOpenURL", e));
|
|
128
|
+
case "OpenShareSheet": {
|
|
129
|
+
const { title: S, message: h, url: w } = t, L = !(typeof h == "string" && h.trim()) && w == null;
|
|
130
|
+
return !k(S, 100) || !k(h, 4e3) || !k(w, 2048) || w === "" || L ? a("OnOpenShareSheet", e, "INVALID", "Share a message or a url within their limits.") : typeof w == "string" && !/^https?:\/\//i.test(w) ? a(
|
|
131
|
+
"OnOpenShareSheet",
|
|
132
|
+
e,
|
|
133
|
+
"LINK_NOT_ALLOWED",
|
|
134
|
+
"Only an http or https url can be shared."
|
|
135
|
+
) : (u("share sheet", t), c("OnOpenShareSheet", e, { action: "sharedAction", activityType: null }));
|
|
136
|
+
}
|
|
137
|
+
case "GetSubWebViewStorage":
|
|
138
|
+
case "SetSubWebViewStorage":
|
|
139
|
+
case "RemoveSubWebViewStorage":
|
|
140
|
+
case "ClearSubWebViewStorage":
|
|
141
|
+
case "GetSubWebViewStorageInfo": {
|
|
142
|
+
const h = {
|
|
143
|
+
GetSubWebViewStorage: () => g.get(t.keys),
|
|
144
|
+
SetSubWebViewStorage: () => g.set(t.entries),
|
|
145
|
+
RemoveSubWebViewStorage: () => g.remove(t.keys),
|
|
146
|
+
ClearSubWebViewStorage: () => g.clear(),
|
|
147
|
+
GetSubWebViewStorageInfo: () => g.info()
|
|
148
|
+
}[i]();
|
|
149
|
+
return h.ok ? c(`On${i}`, e, h.value) : a(
|
|
150
|
+
`On${i}`,
|
|
151
|
+
e,
|
|
152
|
+
h.code,
|
|
153
|
+
h.code === "QUOTA_EXCEEDED" ? "This would take the page's storage past 1,048,576 characters." : "Keys are 1 to 200 characters; values must be JSON."
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
case "GetSubWebViewUserKey":
|
|
157
|
+
return c("OnGetSubWebViewUserKey", e, { key: s.userKey ?? F(l) });
|
|
158
|
+
default:
|
|
159
|
+
return a("ERROR", e, "NOT_FOUND", `${i} is not answered for this page.`);
|
|
160
|
+
}
|
|
161
|
+
}, f = window;
|
|
162
|
+
f.ReactNativeWebView = { postMessage: E }, f.ChaticSubWebView = p;
|
|
163
|
+
const R = {
|
|
164
|
+
get title() {
|
|
165
|
+
return T;
|
|
166
|
+
},
|
|
167
|
+
get closed() {
|
|
168
|
+
return b;
|
|
169
|
+
},
|
|
170
|
+
get storage() {
|
|
171
|
+
return Object.fromEntries([...d].map(([r, n]) => [r, JSON.parse(n)]));
|
|
172
|
+
},
|
|
173
|
+
pressBack: () => {
|
|
174
|
+
b || (I ? y("OnBackPressed", {}) : history.length > 1 ? history.back() : R.requestClose("back"));
|
|
175
|
+
},
|
|
176
|
+
requestClose: (r = "control") => {
|
|
177
|
+
if (b) return;
|
|
178
|
+
const n = Date.now();
|
|
179
|
+
if (v && (V === null || n - V > U)) {
|
|
180
|
+
V = n, y("OnSubWebViewCloseRequested", { trigger: r });
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
b = { by: "user" }, s.onClose?.(b), u("view closed by the user");
|
|
184
|
+
},
|
|
185
|
+
changeContext: (r) => {
|
|
186
|
+
Object.assign(m, r), y("OnSubWebViewContextChanged", { ...m });
|
|
187
|
+
},
|
|
188
|
+
uninstall: () => {
|
|
189
|
+
delete f.ReactNativeWebView, delete f.ChaticSubWebView;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
return R;
|
|
193
|
+
};
|
|
194
|
+
export {
|
|
195
|
+
H as installChaticAppsMock
|
|
196
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@lemoncloud/chatic-apps-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Build pages that run inside the DoU app",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/lemoncloud-io/dou-app.git",
|
|
9
|
+
"directory": "libs/chatic-apps-sdk"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"main": "./index.cjs",
|
|
14
|
+
"module": "./index.js",
|
|
15
|
+
"types": "./index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": {
|
|
19
|
+
"types": "./index.d.ts",
|
|
20
|
+
"default": "./index.js"
|
|
21
|
+
},
|
|
22
|
+
"require": {
|
|
23
|
+
"types": "./index.d.cts",
|
|
24
|
+
"default": "./index.cjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"./mock": {
|
|
28
|
+
"import": {
|
|
29
|
+
"types": "./mock.d.ts",
|
|
30
|
+
"default": "./mock.js"
|
|
31
|
+
},
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./mock.d.cts",
|
|
34
|
+
"default": "./mock.cjs"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
39
|
+
"unpkg": "./chatic-apps-sdk.global.js",
|
|
40
|
+
"jsdelivr": "./chatic-apps-sdk.global.js",
|
|
41
|
+
"files": ["*.js", "*.cjs", "*.d.ts", "*.d.cts", "README.md"],
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {}
|
|
46
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
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;
|