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