v1.3.5: own API key on the phone no longer hangs (secure-storage plugin handed to a promise as a thenable)
Every key-store call on Android stalled before it started: the Capacitor plugin proxy answers `then` with a native method, so a promise resolving to the plugin itself never settled. Save greyed out, key "not saved", Coach "thinking" forever (#42, #58). The plugin now travels inside a plain object; a test drives the store through a Capacitor-style proxy. Verified on an emulator.
This commit is contained in:
1 parent
95654b4f55
commit
684b0054ad
13 files changed
+94
-22
No files matched your search
@@ -10,12 +10,19 @@
|
||||
const KEY = 'coach.apiKey'
|
||||
const memory = new Map()
|
||||
|
||||
// The loaded plugin travels inside a plain object, never as a promise's own value (no `await`
|
||||
// or `.then` may ever hand the proxy back bare — the wrapper below is the whole point). Capacitor's
|
||||
// registerPlugin() hands out a Proxy that answers EVERY property name with a native-method
|
||||
// wrapper — `then` included — so a promise that resolves to the proxy itself takes it for a
|
||||
// thenable, calls SecureStorage.then() on the native side ("not implemented on android"), and
|
||||
// never settles. That one await hung the whole feature on the phone: "Save and use the Coach"
|
||||
// greyed out forever, the key "not saved", the Coach "thinking" without end (issues #42, #58).
|
||||
let pluginPromise = null
|
||||
function plugin() {
|
||||
if (!pluginPromise) {
|
||||
pluginPromise = import('@aparajita/capacitor-secure-storage')
|
||||
.then(m => (m && m.SecureStorage && typeof m.SecureStorage.get === 'function' ? m.SecureStorage : null))
|
||||
.catch(() => null)
|
||||
.then(m => ({ store: m && m.SecureStorage && typeof m.SecureStorage.get === 'function' ? m.SecureStorage : null }))
|
||||
.catch(() => ({ store: null }))
|
||||
}
|
||||
return pluginPromise
|
||||
}
|
||||
@@ -30,7 +37,7 @@ export const withTimeout = (promise, ms = NATIVE_TIMEOUT_MS) => new Promise((res
|
||||
})
|
||||
|
||||
export async function getApiKey() {
|
||||
const p = await plugin()
|
||||
const { store: p } = await plugin()
|
||||
if (p) {
|
||||
try { const v = await withTimeout(p.get(KEY)); return typeof v === 'string' && v ? v : null } catch { /* fall through */ }
|
||||
}
|
||||
@@ -39,14 +46,14 @@ export async function getApiKey() {
|
||||
export async function setApiKey(value) {
|
||||
const v = String(value || '').trim()
|
||||
if (!v) return clearApiKey()
|
||||
const p = await plugin()
|
||||
const { store: p } = await plugin()
|
||||
if (p) {
|
||||
try { await withTimeout(p.set(KEY, v)); memory.delete(KEY); return } catch { /* fall through */ }
|
||||
}
|
||||
memory.set(KEY, v)
|
||||
}
|
||||
export async function clearApiKey() {
|
||||
const p = await plugin()
|
||||
const { store: p } = await plugin()
|
||||
if (p) { try { await withTimeout(p.remove(KEY)) } catch { /* nothing to clear */ } }
|
||||
memory.delete(KEY)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// @vitest-environment node
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
// A stand-in for what @capacitor/core's registerPlugin() really returns: a Proxy that turns ANY
|
||||
// property — `then` included — into a method wrapper whose native call fails asynchronously
|
||||
// without ever invoking the callbacks it was handed. Resolving a promise with that object makes
|
||||
// the promise wait for `then` to call back, which it never does (issues #42 / #58).
|
||||
const calls = []
|
||||
const backing = new Map()
|
||||
const impl = {
|
||||
get: async k => (backing.has(k) ? backing.get(k) : null),
|
||||
set: async (k, v) => { backing.set(k, v) },
|
||||
remove: async k => backing.delete(k)
|
||||
}
|
||||
const SecureStorage = new Proxy({}, {
|
||||
get(_, prop) {
|
||||
if (prop === '$$typeof') return undefined
|
||||
if (prop === 'toJSON') return () => ({})
|
||||
return (...args) => {
|
||||
calls.push(String(prop))
|
||||
if (impl[prop]) return impl[prop](...args)
|
||||
return Promise.reject(new Error(`"SecureStorage.${String(prop)}()" is not implemented on android`))
|
||||
}
|
||||
}
|
||||
})
|
||||
vi.mock('@aparajita/capacitor-secure-storage', () => ({ SecureStorage }))
|
||||
|
||||
const secrets = await import('./coach-secrets.js')
|
||||
|
||||
const within = (p, ms) => Promise.race([p, new Promise((_, rej) => setTimeout(() => rej(new Error(`hung for ${ms} ms`)), ms))])
|
||||
|
||||
describe('coach-secrets against a Capacitor-style plugin proxy', () => {
|
||||
beforeEach(() => { calls.length = 0; backing.clear() })
|
||||
|
||||
it('a round trip settles instead of waiting on the proxy\'s never-answering then()', async () => {
|
||||
await within(secrets.setApiKey('sk-live-1'), 1000)
|
||||
expect(await within(secrets.getApiKey(), 1000)).toBe('sk-live-1')
|
||||
await within(secrets.clearApiKey(), 1000)
|
||||
expect(await within(secrets.getApiKey(), 1000)).toBe(null)
|
||||
expect(calls).not.toContain('then')
|
||||
})
|
||||
|
||||
it('goes through the platform store, not the in-memory fallback', async () => {
|
||||
await within(secrets.setApiKey('sk-live-2'), 1000)
|
||||
expect([...backing.keys()]).toEqual(['coach.apiKey'])
|
||||
expect(calls).toEqual(['set'])
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user