@h-ai/capacitor 0.1.0-alpha.67 → 0.1.0-alpha.69
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.en.md +10 -2
- package/README.ja.md +10 -2
- package/README.md +10 -2
- package/dist/index.d.ts +295 -48
- package/dist/index.js +264 -184
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -8,6 +8,7 @@ var en_US_default = {
|
|
|
8
8
|
capacitor_initFailed: "Capacitor initialization failed",
|
|
9
9
|
capacitor_initInProgress: "Capacitor initialization is already in progress",
|
|
10
10
|
capacitor_notAvailable: "Capacitor is not available in this environment",
|
|
11
|
+
capacitor_secureStorageClearFailed: "Could not fully clear sign-in credentials from secure storage; retry signing out",
|
|
11
12
|
capacitor_preferencesGetFailed: "Failed to read from Capacitor Preferences",
|
|
12
13
|
capacitor_preferencesSetFailed: "Failed to write to Capacitor Preferences",
|
|
13
14
|
capacitor_preferencesRemoveFailed: "Failed to remove from Capacitor Preferences",
|
|
@@ -17,7 +18,29 @@ var en_US_default = {
|
|
|
17
18
|
capacitor_pushListenFailed: "Failed to listen for push notifications",
|
|
18
19
|
capacitor_cameraFailed: "Failed to access camera",
|
|
19
20
|
capacitor_statusBarFailed: "Failed to configure status bar",
|
|
20
|
-
capacitor_notInitialized: "Capacitor module is not initialized"
|
|
21
|
+
capacitor_notInitialized: "Capacitor module is not initialized",
|
|
22
|
+
capacitor_pushCancelled: "The push operation was cancelled, timed out or the module was closed",
|
|
23
|
+
capacitor_pushCleanupFailed: "Push listeners could not be fully removed; retry cleanup or close the module"
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// messages/ja-JP.json
|
|
27
|
+
var ja_JP_default = {
|
|
28
|
+
capacitor_initFailed: "Capacitor\u306E\u521D\u671F\u5316\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
29
|
+
capacitor_initInProgress: "Capacitor\u306E\u521D\u671F\u5316\u306F\u65E2\u306B\u5B9F\u884C\u4E2D\u3067\u3059",
|
|
30
|
+
capacitor_notAvailable: "\u3053\u306E\u74B0\u5883\u3067\u306FCapacitor\u3092\u5229\u7528\u3067\u304D\u307E\u305B\u3093",
|
|
31
|
+
capacitor_secureStorageClearFailed: "\u5B89\u5168\u306A\u30B9\u30C8\u30EC\u30FC\u30B8\u306E\u30ED\u30B0\u30A4\u30F3\u8A8D\u8A3C\u60C5\u5831\u3092\u5B8C\u5168\u306B\u524A\u9664\u3067\u304D\u307E\u305B\u3093\u3067\u3057\u305F\u3002\u30ED\u30B0\u30A2\u30A6\u30C8\u3092\u518D\u8A66\u884C\u3057\u3066\u304F\u3060\u3055\u3044",
|
|
32
|
+
capacitor_preferencesGetFailed: "Capacitor Preferences\u304B\u3089\u306E\u8AAD\u307F\u53D6\u308A\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
33
|
+
capacitor_preferencesSetFailed: "Capacitor Preferences\u3078\u306E\u66F8\u304D\u8FBC\u307F\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
34
|
+
capacitor_preferencesRemoveFailed: "Capacitor Preferences\u304B\u3089\u306E\u524A\u9664\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
35
|
+
capacitor_deviceInfoFailed: "\u30C7\u30D0\u30A4\u30B9\u60C5\u5831\u306E\u53D6\u5F97\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
36
|
+
capacitor_appVersionFailed: "\u30A2\u30D7\u30EA\u306E\u30D0\u30FC\u30B8\u30E7\u30F3\u306E\u53D6\u5F97\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
37
|
+
capacitor_pushRegisterFailed: "\u30D7\u30C3\u30B7\u30E5\u901A\u77E5\u306E\u767B\u9332\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
38
|
+
capacitor_pushListenFailed: "\u30D7\u30C3\u30B7\u30E5\u901A\u77E5\u306E\u53D7\u4FE1\u8A2D\u5B9A\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
39
|
+
capacitor_cameraFailed: "\u30AB\u30E1\u30E9\u3078\u306E\u30A2\u30AF\u30BB\u30B9\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
40
|
+
capacitor_statusBarFailed: "\u30B9\u30C6\u30FC\u30BF\u30B9\u30D0\u30FC\u306E\u8A2D\u5B9A\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
41
|
+
capacitor_notInitialized: "Capacitor\u30E2\u30B8\u30E5\u30FC\u30EB\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093",
|
|
42
|
+
capacitor_pushCancelled: "\u30D7\u30C3\u30B7\u30E5\u64CD\u4F5C\u304C\u30AD\u30E3\u30F3\u30BB\u30EB\u3055\u308C\u305F\u304B\u3001\u30BF\u30A4\u30E0\u30A2\u30A6\u30C8\u3057\u305F\u304B\u3001\u30E2\u30B8\u30E5\u30FC\u30EB\u304C\u7D42\u4E86\u3057\u307E\u3057\u305F",
|
|
43
|
+
capacitor_pushCleanupFailed: "\u30D7\u30C3\u30B7\u30E5\u30EA\u30B9\u30CA\u30FC\u3092\u5B8C\u5168\u306B\u524A\u9664\u3067\u304D\u307E\u305B\u3093\u3067\u3057\u305F\u3002\u30AF\u30EA\u30FC\u30F3\u30A2\u30C3\u30D7\u3092\u518D\u8A66\u884C\u3059\u308B\u304B\u3001\u30E2\u30B8\u30E5\u30FC\u30EB\u3092\u7D42\u4E86\u3057\u3066\u304F\u3060\u3055\u3044"
|
|
21
44
|
};
|
|
22
45
|
|
|
23
46
|
// messages/zh-CN.json
|
|
@@ -25,6 +48,7 @@ var zh_CN_default = {
|
|
|
25
48
|
capacitor_initFailed: "Capacitor \u521D\u59CB\u5316\u5931\u8D25",
|
|
26
49
|
capacitor_initInProgress: "Capacitor \u6B63\u5728\u521D\u59CB\u5316\u4E2D",
|
|
27
50
|
capacitor_notAvailable: "\u5F53\u524D\u73AF\u5883\u4E0D\u652F\u6301 Capacitor",
|
|
51
|
+
capacitor_secureStorageClearFailed: "\u672A\u80FD\u5B8C\u5168\u6E05\u9664\u5B89\u5168\u5B58\u50A8\u4E2D\u7684\u767B\u5F55\u51ED\u636E\uFF0C\u8BF7\u91CD\u8BD5\u9000\u51FA",
|
|
28
52
|
capacitor_preferencesGetFailed: "\u8BFB\u53D6 Capacitor Preferences \u5931\u8D25",
|
|
29
53
|
capacitor_preferencesSetFailed: "\u5199\u5165 Capacitor Preferences \u5931\u8D25",
|
|
30
54
|
capacitor_preferencesRemoveFailed: "\u5220\u9664 Capacitor Preferences \u5931\u8D25",
|
|
@@ -34,13 +58,16 @@ var zh_CN_default = {
|
|
|
34
58
|
capacitor_pushListenFailed: "\u76D1\u542C\u63A8\u9001\u901A\u77E5\u5931\u8D25",
|
|
35
59
|
capacitor_cameraFailed: "\u8BBF\u95EE\u76F8\u673A\u5931\u8D25",
|
|
36
60
|
capacitor_statusBarFailed: "\u914D\u7F6E\u72B6\u6001\u680F\u5931\u8D25",
|
|
37
|
-
capacitor_notInitialized: "Capacitor \u6A21\u5757\u672A\u521D\u59CB\u5316"
|
|
61
|
+
capacitor_notInitialized: "Capacitor \u6A21\u5757\u672A\u521D\u59CB\u5316",
|
|
62
|
+
capacitor_pushCancelled: "\u63A8\u9001\u64CD\u4F5C\u5DF2\u53D6\u6D88\u3001\u8D85\u65F6\u6216\u6A21\u5757\u5DF2\u5173\u95ED",
|
|
63
|
+
capacitor_pushCleanupFailed: "\u63A8\u9001\u76D1\u542C\u5668\u672A\u5B8C\u5168\u79FB\u9664\uFF0C\u8BF7\u91CD\u8BD5\u6E05\u7406\u6216\u5173\u95ED\u6A21\u5757"
|
|
38
64
|
};
|
|
39
65
|
|
|
40
66
|
// src/capacitor-i18n.ts
|
|
41
67
|
var capacitorM = core.i18n.createMessageGetter({
|
|
42
68
|
"zh-CN": zh_CN_default,
|
|
43
|
-
"en-US": en_US_default
|
|
69
|
+
"en-US": en_US_default,
|
|
70
|
+
"ja-JP": ja_JP_default
|
|
44
71
|
});
|
|
45
72
|
var CapacitorErrorInfo = {
|
|
46
73
|
INIT_FAILED: "001:500",
|
|
@@ -54,6 +81,7 @@ var CapacitorErrorInfo = {
|
|
|
54
81
|
APP_VERSION_FAILED: "021:500",
|
|
55
82
|
PUSH_REGISTER_FAILED: "030:500",
|
|
56
83
|
PUSH_LISTEN_FAILED: "031:500",
|
|
84
|
+
PUSH_CLEANUP_FAILED: "032:500",
|
|
57
85
|
CAMERA_FAILED: "040:500",
|
|
58
86
|
STATUS_BAR_FAILED: "050:500"
|
|
59
87
|
};
|
|
@@ -138,113 +166,181 @@ async function getAppVersion() {
|
|
|
138
166
|
}
|
|
139
167
|
}
|
|
140
168
|
var logger = core.logger.child({ module: "capacitor", scope: "push" });
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
169
|
+
var activeScopes = /* @__PURE__ */ new Set();
|
|
170
|
+
function createPushScope(options) {
|
|
171
|
+
let scope;
|
|
172
|
+
const controller = new AbortController();
|
|
173
|
+
const handles = /* @__PURE__ */ new Set();
|
|
174
|
+
const removals = /* @__PURE__ */ new Map();
|
|
175
|
+
let pending = 0;
|
|
176
|
+
let closing;
|
|
177
|
+
let timer;
|
|
178
|
+
const cancelled = () => new Error(capacitorM("capacitor_pushCancelled"));
|
|
179
|
+
function releaseIfComplete() {
|
|
180
|
+
if (controller.signal.aborted && pending === 0 && handles.size === 0)
|
|
181
|
+
activeScopes.delete(scope);
|
|
182
|
+
}
|
|
183
|
+
function remove(handle) {
|
|
184
|
+
const current = removals.get(handle);
|
|
185
|
+
if (current)
|
|
186
|
+
return current;
|
|
187
|
+
const removal = Promise.resolve().then(() => handle.remove()).then(() => {
|
|
188
|
+
handles.delete(handle);
|
|
189
|
+
}).finally(() => {
|
|
190
|
+
removals.delete(handle);
|
|
191
|
+
releaseIfComplete();
|
|
192
|
+
});
|
|
193
|
+
removals.set(handle, removal);
|
|
194
|
+
return removal;
|
|
195
|
+
}
|
|
196
|
+
function finishSetup() {
|
|
197
|
+
if (timer !== void 0) {
|
|
198
|
+
clearTimeout(timer);
|
|
199
|
+
timer = void 0;
|
|
150
200
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
201
|
+
}
|
|
202
|
+
function dispose() {
|
|
203
|
+
controller.abort();
|
|
204
|
+
finishSetup();
|
|
205
|
+
options?.signal?.removeEventListener("abort", onAbort);
|
|
206
|
+
if (closing)
|
|
207
|
+
return closing;
|
|
208
|
+
closing = Promise.allSettled([...handles].map(remove)).then((results) => {
|
|
209
|
+
releaseIfComplete();
|
|
210
|
+
if (results.some((result) => result.status === "rejected"))
|
|
211
|
+
throw new Error(capacitorM("capacitor_pushCleanupFailed"));
|
|
212
|
+
}).finally(() => {
|
|
213
|
+
closing = void 0;
|
|
214
|
+
});
|
|
215
|
+
return closing;
|
|
216
|
+
}
|
|
217
|
+
function onAbort() {
|
|
218
|
+
void dispose().catch(() => logger.warn("Push listener cleanup failed; retry cleanup or capacitor.close"));
|
|
219
|
+
}
|
|
220
|
+
scope = {
|
|
221
|
+
signal: controller.signal,
|
|
222
|
+
assertOpen() {
|
|
223
|
+
if (controller.signal.aborted)
|
|
224
|
+
throw cancelled();
|
|
225
|
+
},
|
|
226
|
+
async attach(add) {
|
|
227
|
+
scope.assertOpen();
|
|
228
|
+
pending++;
|
|
229
|
+
try {
|
|
230
|
+
const handle = await add();
|
|
231
|
+
handles.add(handle);
|
|
232
|
+
if (controller.signal.aborted) {
|
|
233
|
+
await remove(handle);
|
|
234
|
+
throw cancelled();
|
|
164
235
|
}
|
|
236
|
+
} finally {
|
|
237
|
+
pending--;
|
|
238
|
+
releaseIfComplete();
|
|
165
239
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
});
|
|
240
|
+
},
|
|
241
|
+
async wait(operation) {
|
|
242
|
+
scope.assertOpen();
|
|
243
|
+
let interrupt;
|
|
244
|
+
const aborted = new Promise((_, reject) => {
|
|
245
|
+
interrupt = () => reject(cancelled());
|
|
246
|
+
controller.signal.addEventListener("abort", interrupt, { once: true });
|
|
247
|
+
});
|
|
248
|
+
try {
|
|
249
|
+
return await Promise.race([Promise.resolve().then(operation), aborted]);
|
|
250
|
+
} finally {
|
|
251
|
+
if (interrupt)
|
|
252
|
+
controller.signal.removeEventListener("abort", interrupt);
|
|
173
253
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
254
|
+
},
|
|
255
|
+
finishSetup,
|
|
256
|
+
dispose
|
|
257
|
+
};
|
|
258
|
+
activeScopes.add(scope);
|
|
259
|
+
timer = setTimeout(onAbort, 3e4);
|
|
260
|
+
options?.signal?.addEventListener("abort", onAbort, { once: true });
|
|
261
|
+
if (options?.signal?.aborted)
|
|
262
|
+
onAbort();
|
|
263
|
+
return scope;
|
|
264
|
+
}
|
|
265
|
+
async function loadPushPlugin() {
|
|
266
|
+
const { Capacitor } = await import('@capacitor/core');
|
|
267
|
+
if (!Capacitor.isNativePlatform())
|
|
268
|
+
throw new Error(capacitorM("capacitor_notAvailable"));
|
|
269
|
+
return (await import('@capacitor/push-notifications')).PushNotifications;
|
|
270
|
+
}
|
|
271
|
+
async function closePushListeners() {
|
|
272
|
+
const results = await Promise.allSettled([...activeScopes].map((scope) => scope.dispose()));
|
|
273
|
+
return results.some((result) => result.status === "rejected") ? err(HaiCapacitorError.PUSH_CLEANUP_FAILED, capacitorM("capacitor_pushCleanupFailed")) : ok(void 0);
|
|
274
|
+
}
|
|
275
|
+
async function registerPush(options) {
|
|
276
|
+
const scope = createPushScope(options);
|
|
277
|
+
try {
|
|
278
|
+
const token = await scope.wait(async () => {
|
|
279
|
+
const push = await loadPushPlugin();
|
|
280
|
+
scope.assertOpen();
|
|
281
|
+
const permission = await push.requestPermissions();
|
|
282
|
+
scope.assertOpen();
|
|
283
|
+
if (permission.receive !== "granted")
|
|
284
|
+
throw new Error(capacitorM("capacitor_pushRegisterFailed"));
|
|
285
|
+
let resolveToken;
|
|
286
|
+
let rejectToken;
|
|
287
|
+
const tokenResult = new Promise((resolve, reject) => {
|
|
288
|
+
resolveToken = resolve;
|
|
289
|
+
rejectToken = reject;
|
|
191
290
|
});
|
|
291
|
+
const setup = async () => {
|
|
292
|
+
await scope.attach(() => push.addListener("registration", (result) => {
|
|
293
|
+
if (!scope.signal.aborted)
|
|
294
|
+
resolveToken(result.value);
|
|
295
|
+
}));
|
|
296
|
+
await scope.attach(() => push.addListener("registrationError", () => {
|
|
297
|
+
if (!scope.signal.aborted)
|
|
298
|
+
rejectToken(new Error(capacitorM("capacitor_pushRegisterFailed")));
|
|
299
|
+
}));
|
|
300
|
+
scope.assertOpen();
|
|
301
|
+
await push.register();
|
|
302
|
+
};
|
|
303
|
+
return (await Promise.all([setup(), tokenResult]))[1];
|
|
192
304
|
});
|
|
305
|
+
await scope.dispose();
|
|
193
306
|
return ok({ token });
|
|
194
307
|
} catch (cause) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
capacitorM("capacitor_pushRegisterFailed"),
|
|
198
|
-
cause
|
|
199
|
-
);
|
|
308
|
+
await scope.dispose().catch(() => logger.warn("Push registration cleanup failed; retry capacitor.close"));
|
|
309
|
+
return err(HaiCapacitorError.PUSH_REGISTER_FAILED, capacitorM("capacitor_pushRegisterFailed"), cause);
|
|
200
310
|
}
|
|
201
311
|
}
|
|
202
|
-
async function listenPush(callbacks) {
|
|
312
|
+
async function listenPush(callbacks, options) {
|
|
313
|
+
const scope = createPushScope(options);
|
|
203
314
|
try {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
"pushNotificationReceived",
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
data: action.notification.data
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
);
|
|
232
|
-
listeners.push(listener);
|
|
233
|
-
}
|
|
234
|
-
const cleanup = async () => {
|
|
235
|
-
const removeResults = await Promise.allSettled(listeners.map((listener) => listener.remove()));
|
|
236
|
-
const rejectedCount = removeResults.filter((result) => result.status === "rejected").length;
|
|
237
|
-
if (rejectedCount > 0) {
|
|
238
|
-
logger.warn("Failed to remove push listeners", { rejectedCount });
|
|
315
|
+
await scope.wait(async () => {
|
|
316
|
+
const push = await loadPushPlugin();
|
|
317
|
+
scope.assertOpen();
|
|
318
|
+
if (callbacks.onReceived) {
|
|
319
|
+
await scope.attach(() => push.addListener("pushNotificationReceived", (notification) => {
|
|
320
|
+
if (!scope.signal.aborted)
|
|
321
|
+
callbacks.onReceived?.({ id: notification.id, title: notification.title, body: notification.body, data: notification.data });
|
|
322
|
+
}));
|
|
323
|
+
}
|
|
324
|
+
if (callbacks.onActionPerformed) {
|
|
325
|
+
await scope.attach(() => push.addListener("pushNotificationActionPerformed", (action) => {
|
|
326
|
+
if (!scope.signal.aborted)
|
|
327
|
+
callbacks.onActionPerformed?.({ id: action.notification.id, title: action.notification.title, body: action.notification.body, data: action.notification.data });
|
|
328
|
+
}));
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
scope.assertOpen();
|
|
332
|
+
scope.finishSetup();
|
|
333
|
+
return ok(async () => {
|
|
334
|
+
try {
|
|
335
|
+
await scope.dispose();
|
|
336
|
+
return ok(void 0);
|
|
337
|
+
} catch (cause) {
|
|
338
|
+
return err(HaiCapacitorError.PUSH_CLEANUP_FAILED, capacitorM("capacitor_pushCleanupFailed"), cause);
|
|
239
339
|
}
|
|
240
|
-
};
|
|
241
|
-
return ok(cleanup);
|
|
340
|
+
});
|
|
242
341
|
} catch (cause) {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
capacitorM("capacitor_pushListenFailed"),
|
|
246
|
-
cause
|
|
247
|
-
);
|
|
342
|
+
await scope.dispose().catch(() => logger.warn("Push setup cleanup failed; retry capacitor.close"));
|
|
343
|
+
return err(HaiCapacitorError.PUSH_LISTEN_FAILED, capacitorM("capacitor_pushListenFailed"), cause);
|
|
248
344
|
}
|
|
249
345
|
}
|
|
250
346
|
async function configureStatusBar(config) {
|
|
@@ -299,95 +395,74 @@ async function hideStatusBar() {
|
|
|
299
395
|
);
|
|
300
396
|
}
|
|
301
397
|
}
|
|
302
|
-
var logger2 = core.logger.child({ module: "capacitor", scope: "token-storage" });
|
|
303
398
|
var PREF_ACCESS_TOKEN = "hai_access_token";
|
|
304
399
|
var PREF_REFRESH_TOKEN = "hai_refresh_token";
|
|
305
|
-
var secureStoragePromise = null;
|
|
306
|
-
var secureStorageWarningLogged = false;
|
|
307
|
-
function logSecureStorageUnavailable(reason, error) {
|
|
308
|
-
if (secureStorageWarningLogged)
|
|
309
|
-
return;
|
|
310
|
-
secureStorageWarningLogged = true;
|
|
311
|
-
logger2.warn("Capacitor secure token storage unavailable; token persistence is disabled", {
|
|
312
|
-
reason,
|
|
313
|
-
error
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
400
|
async function loadSecureStoragePlugin() {
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
logSecureStorageUnavailable("non-native-platform");
|
|
323
|
-
return null;
|
|
324
|
-
}
|
|
325
|
-
const { SecureStorage } = await import('@aparajita/capacitor-secure-storage');
|
|
326
|
-
return SecureStorage;
|
|
327
|
-
} catch (error) {
|
|
328
|
-
logSecureStorageUnavailable("plugin-load-failed", error);
|
|
329
|
-
return null;
|
|
330
|
-
}
|
|
331
|
-
})();
|
|
332
|
-
}
|
|
333
|
-
return secureStoragePromise;
|
|
401
|
+
const { Capacitor } = await import('@capacitor/core');
|
|
402
|
+
if (!Capacitor.isNativePlatform())
|
|
403
|
+
throw new Error(capacitorM("capacitor_notAvailable"));
|
|
404
|
+
const { SecureStorage } = await import('@aparajita/capacitor-secure-storage');
|
|
405
|
+
return SecureStorage;
|
|
334
406
|
}
|
|
335
407
|
function createCapacitorTokenStorage() {
|
|
408
|
+
let credentialGeneration = 0;
|
|
409
|
+
let accessBlocked = false;
|
|
410
|
+
let refreshBlocked = false;
|
|
411
|
+
function blockCredentials() {
|
|
412
|
+
credentialGeneration++;
|
|
413
|
+
accessBlocked = true;
|
|
414
|
+
refreshBlocked = true;
|
|
415
|
+
}
|
|
416
|
+
async function writeToken(key, token) {
|
|
417
|
+
const writeGeneration = credentialGeneration;
|
|
418
|
+
try {
|
|
419
|
+
const storage = await loadSecureStoragePlugin();
|
|
420
|
+
await storage.setItem(key, token);
|
|
421
|
+
if (writeGeneration === credentialGeneration) {
|
|
422
|
+
if (key === PREF_ACCESS_TOKEN)
|
|
423
|
+
accessBlocked = false;
|
|
424
|
+
else
|
|
425
|
+
refreshBlocked = false;
|
|
426
|
+
}
|
|
427
|
+
} catch (error) {
|
|
428
|
+
if (writeGeneration === credentialGeneration)
|
|
429
|
+
blockCredentials();
|
|
430
|
+
throw error;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
336
433
|
return {
|
|
337
434
|
async getAccessToken() {
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
return null;
|
|
342
|
-
return await storage.getItem(PREF_ACCESS_TOKEN);
|
|
343
|
-
} catch (error) {
|
|
344
|
-
logger2.error("Failed to get access token from secure storage", { error });
|
|
435
|
+
const readGeneration = credentialGeneration;
|
|
436
|
+
const storage = await loadSecureStoragePlugin();
|
|
437
|
+
if (accessBlocked)
|
|
345
438
|
return null;
|
|
346
|
-
|
|
439
|
+
const token = await storage.getItem(PREF_ACCESS_TOKEN);
|
|
440
|
+
return accessBlocked || readGeneration !== credentialGeneration ? null : token;
|
|
347
441
|
},
|
|
348
442
|
async getRefreshToken() {
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
return null;
|
|
353
|
-
return await storage.getItem(PREF_REFRESH_TOKEN);
|
|
354
|
-
} catch (error) {
|
|
355
|
-
logger2.error("Failed to get refresh token from secure storage", { error });
|
|
443
|
+
const readGeneration = credentialGeneration;
|
|
444
|
+
const storage = await loadSecureStoragePlugin();
|
|
445
|
+
if (refreshBlocked)
|
|
356
446
|
return null;
|
|
357
|
-
|
|
447
|
+
const token = await storage.getItem(PREF_REFRESH_TOKEN);
|
|
448
|
+
return refreshBlocked || readGeneration !== credentialGeneration ? null : token;
|
|
358
449
|
},
|
|
359
450
|
async setAccessToken(token) {
|
|
360
|
-
|
|
361
|
-
const storage = await loadSecureStoragePlugin();
|
|
362
|
-
if (!storage)
|
|
363
|
-
return;
|
|
364
|
-
await storage.setItem(PREF_ACCESS_TOKEN, token);
|
|
365
|
-
} catch (error) {
|
|
366
|
-
logger2.error("Failed to set access token in secure storage", { error });
|
|
367
|
-
}
|
|
451
|
+
await writeToken(PREF_ACCESS_TOKEN, token);
|
|
368
452
|
},
|
|
369
453
|
async setRefreshToken(token) {
|
|
370
|
-
|
|
371
|
-
const storage = await loadSecureStoragePlugin();
|
|
372
|
-
if (!storage)
|
|
373
|
-
return;
|
|
374
|
-
await storage.setItem(PREF_REFRESH_TOKEN, token);
|
|
375
|
-
} catch (error) {
|
|
376
|
-
logger2.error("Failed to set refresh token in secure storage", { error });
|
|
377
|
-
}
|
|
454
|
+
await writeToken(PREF_REFRESH_TOKEN, token);
|
|
378
455
|
},
|
|
379
456
|
async clear() {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
logger2.error("Failed to clear tokens from secure storage", { error });
|
|
390
|
-
}
|
|
457
|
+
blockCredentials();
|
|
458
|
+
const storage = await loadSecureStoragePlugin();
|
|
459
|
+
const results = await Promise.allSettled([
|
|
460
|
+
storage.removeItem(PREF_ACCESS_TOKEN),
|
|
461
|
+
storage.removeItem(PREF_REFRESH_TOKEN)
|
|
462
|
+
]);
|
|
463
|
+
const errors = results.filter((result) => result.status === "rejected").map((result) => result.reason);
|
|
464
|
+
if (errors.length > 0)
|
|
465
|
+
throw new AggregateError(errors, capacitorM("capacitor_secureStorageClearFailed"));
|
|
391
466
|
}
|
|
392
467
|
};
|
|
393
468
|
}
|
|
@@ -429,7 +504,7 @@ async function safeRemovePreference(key) {
|
|
|
429
504
|
}
|
|
430
505
|
|
|
431
506
|
// src/capacitor-main.ts
|
|
432
|
-
var
|
|
507
|
+
var logger2 = core.logger.child({ module: "capacitor", scope: "main" });
|
|
433
508
|
var initialized = false;
|
|
434
509
|
var initInProgress = false;
|
|
435
510
|
var cachedPlatform = null;
|
|
@@ -473,7 +548,7 @@ function isCapacitorAvailable() {
|
|
|
473
548
|
var capacitor = {
|
|
474
549
|
async init() {
|
|
475
550
|
if (initInProgress) {
|
|
476
|
-
|
|
551
|
+
logger2.warn("Capacitor init already in progress, skipping concurrent call");
|
|
477
552
|
return err(
|
|
478
553
|
HaiCapacitorError.INIT_IN_PROGRESS,
|
|
479
554
|
capacitorM("capacitor_initInProgress")
|
|
@@ -482,12 +557,17 @@ var capacitor = {
|
|
|
482
557
|
initInProgress = true;
|
|
483
558
|
try {
|
|
484
559
|
if (initialized) {
|
|
485
|
-
|
|
486
|
-
await capacitor.close();
|
|
560
|
+
logger2.warn("Capacitor module is already initialized, reinitializing");
|
|
561
|
+
const closed = await capacitor.close();
|
|
562
|
+
if (!closed.success)
|
|
563
|
+
return closed;
|
|
487
564
|
}
|
|
488
|
-
|
|
565
|
+
const cleaned = await closePushListeners();
|
|
566
|
+
if (!cleaned.success)
|
|
567
|
+
return cleaned;
|
|
568
|
+
logger2.info("Initializing capacitor module");
|
|
489
569
|
if (!isCapacitorAvailable()) {
|
|
490
|
-
|
|
570
|
+
logger2.error("Capacitor is not available in current environment");
|
|
491
571
|
return err(
|
|
492
572
|
HaiCapacitorError.NOT_AVAILABLE,
|
|
493
573
|
capacitorM("capacitor_notAvailable")
|
|
@@ -496,7 +576,7 @@ var capacitor = {
|
|
|
496
576
|
const { Capacitor } = await import('@capacitor/core');
|
|
497
577
|
const platform = Capacitor.getPlatform();
|
|
498
578
|
if (!platform) {
|
|
499
|
-
|
|
579
|
+
logger2.error("Capacitor initialization failed, platform not detected");
|
|
500
580
|
return err(
|
|
501
581
|
HaiCapacitorError.INIT_FAILED,
|
|
502
582
|
capacitorM("capacitor_initFailed")
|
|
@@ -504,10 +584,10 @@ var capacitor = {
|
|
|
504
584
|
}
|
|
505
585
|
cachedPlatform = platform;
|
|
506
586
|
initialized = true;
|
|
507
|
-
|
|
587
|
+
logger2.info("Capacitor module initialized", { platform });
|
|
508
588
|
return ok(void 0);
|
|
509
589
|
} catch (cause) {
|
|
510
|
-
|
|
590
|
+
logger2.error("Capacitor module initialization failed", { error: cause });
|
|
511
591
|
return err(
|
|
512
592
|
HaiCapacitorError.INIT_FAILED,
|
|
513
593
|
capacitorM("capacitor_initFailed"),
|
|
@@ -518,14 +598,14 @@ var capacitor = {
|
|
|
518
598
|
}
|
|
519
599
|
},
|
|
520
600
|
async close() {
|
|
521
|
-
|
|
522
|
-
logger3.info("Capacitor module is already closed, skipping");
|
|
523
|
-
return;
|
|
524
|
-
}
|
|
525
|
-
logger3.info("Closing capacitor module");
|
|
601
|
+
logger2.info("Closing capacitor module");
|
|
526
602
|
initialized = false;
|
|
527
603
|
cachedPlatform = null;
|
|
528
|
-
|
|
604
|
+
const cleaned = await closePushListeners();
|
|
605
|
+
if (!cleaned.success)
|
|
606
|
+
return cleaned;
|
|
607
|
+
logger2.info("Capacitor module closed");
|
|
608
|
+
return ok(void 0);
|
|
529
609
|
},
|
|
530
610
|
getPlatform() {
|
|
531
611
|
if (cachedPlatform) {
|