- Connections and settings stay with each app. Signed in, synced ones follow you.
+ Both apps share saved connections and their passwords. Settings stay with each app; signed in, synced ones follow you.
(switchTo = null)}>Cancel
diff --git a/src/lib/components/StudioShell.svelte b/src/lib/components/StudioShell.svelte
index 380bfe58..5585714b 100644
--- a/src/lib/components/StudioShell.svelte
+++ b/src/lib/components/StudioShell.svelte
@@ -310,6 +310,7 @@
setWasDisconnected,
upsertConnection,
engineFamily,
+ refreshConnectionsFromDisk,
} from '$lib/stores/connections.js'
import { workspaces, activeWorkspaceId, setActiveWorkspace, workspaceOf } from '$lib/stores/workspaces.js'
import { hasPro, FREE_CONNECTION_LIMIT } from '$lib/stores/license.js'
@@ -775,6 +776,21 @@
window.addEventListener('stroke:connections-synced', reload)
return () => window.removeEventListener('stroke:connections-synced', reload)
})
+ // Stroke and Stroke Nightly share the saved list: coming back to this window,
+ // show what the other one added, edited or deleted in the meantime.
+ $effect(() => {
+ if (!('__TAURI_INTERNALS__' in window)) return
+ let busy = false
+ const onFocus = () => {
+ if (busy) return
+ busy = true
+ void refreshConnectionsFromDisk()
+ .then((changed) => { if (changed) savedConnections = loadSavedConnections() })
+ .finally(() => { busy = false })
+ }
+ window.addEventListener('focus', onFocus)
+ return () => window.removeEventListener('focus', onFocus)
+ })
let showSettingsModal = $state(false)
/** Settings category to open on next (e.g. 'keybindings'); the dialog clears it. */
let settingsSection = $state('')
diff --git a/src/lib/stores/connections.js b/src/lib/stores/connections.js
index aea0b887..997c4daa 100644
--- a/src/lib/stores/connections.js
+++ b/src/lib/stores/connections.js
@@ -157,7 +157,7 @@ export function saveConnections(connections) {
return pub
})
_secretCache = next
- queueSecretsWrite()
+ queueSecretsWrite(connections.map((c) => c.id))
}
localStorage.setItem(STORAGE_KEY, JSON.stringify(stored))
mirrorToDisk()
@@ -230,11 +230,22 @@ export function splitSecrets(c) {
let _secretsChain = Promise.resolve()
-/** Persist the cache; on failure, put the secrets back inline so none is lost. */
-function queueSecretsWrite() {
- const json = JSON.stringify(Object.fromEntries(_secretCache))
+/**
+ * Merge this app's secrets into the keychain vault, which Stroke and Stroke
+ * Nightly share: every id of `ids` is written from the cache (an empty object
+ * clears it), deleted ids are removed, and ids this app doesn't have are left
+ * as the other app saved them. Writing the whole vault from this app's cache
+ * used to drop the other track's passwords. On failure the secrets go back
+ * inline so none is lost.
+ * @param {Iterable} ids
+ */
+function queueSecretsWrite(ids) {
+ /** @type {Record>} */
+ const set = {}
+ for (const id of ids) if (typeof id === 'string') set[id] = _secretCache.get(id) ?? {}
+ const payload = { set: JSON.stringify(set), removed: [...loadDeletedIds()] }
_secretsChain = _secretsChain
- .then(() => invoke('conn_secrets_write', { json }))
+ .then(() => invoke('conn_secrets_merge', payload))
.catch((err) => {
console.error('Keychain write failed; keeping connection secrets inline:', err)
_vaultReady = false
@@ -267,11 +278,18 @@ export async function hydrateConnectionSecrets() {
inline = true
}
}
- // Secrets of deleted connections don't come back.
- const ids = new Set(list.map((c) => c?.id))
- for (const id of [...cache.keys()]) if (!ids.has(id)) cache.delete(id)
- if (inline || cache.size !== Object.keys(stored ?? {}).length) {
- await invoke('conn_secrets_write', { json: JSON.stringify(Object.fromEntries(cache)) })
+ // Secrets of deleted connections don't come back. Only an id with a
+ // tombstone counts as deleted: one missing from this list may belong to
+ // the other track (Stroke and Stroke Nightly share the vault), and
+ // dropping those wiped its passwords every time the other app started.
+ const deleted = loadDeletedIds()
+ let pruned = false
+ for (const id of [...cache.keys()]) if (deleted.has(id)) { cache.delete(id); pruned = true }
+ if (inline || pruned) {
+ /** @type {Record>} */
+ const set = {}
+ for (const c of list) if (c && typeof c.id === 'string') set[c.id] = cache.get(c.id) ?? {}
+ await invoke('conn_secrets_merge', { set: JSON.stringify(set), removed: [...deleted] })
}
_secretCache = cache
_vaultReady = true
@@ -293,6 +311,9 @@ export async function hydrateConnectionSecrets() {
// file is loaded into localStorage before anything reads the list.
let _mirrorChain = Promise.resolve()
+/** Ids re-added after a delete this session. The backend merges each write
+ * with the file, where a tombstone otherwise wins over a copy of the row. */
+const _revived = new Set()
/** Queue a write of the current list and deleted ids. Writes run one at a time,
* so an older payload can never land after a newer one. */
@@ -303,6 +324,7 @@ function mirrorToDisk() {
version: 1,
connections: JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '[]'),
deleted: [...loadDeletedIds()],
+ revived: [..._revived],
})
} catch {
return
@@ -380,6 +402,54 @@ export async function hydrateConnectionsFromDisk({ waitForSecrets = true } = {})
}
}
+/** JSON with object keys sorted, for comparing two payloads by value. @param {unknown} v @returns {string} */
+function canonical(v) {
+ if (Array.isArray(v)) return `[${v.map(canonical).join(',')}]`
+ if (v && typeof v === 'object') {
+ return `{${Object.keys(v).sort().map((k) => `${JSON.stringify(k)}:${canonical(/** @type {Record} */ (v)[k])}`).join(',')}}`
+ }
+ return JSON.stringify(v) ?? 'null'
+}
+
+/**
+ * Pick up what the other track saved. Stroke and Stroke Nightly share
+ * connections.json and the keychain vault, and both can be open: call this
+ * when the window comes back to the front. Returns true when the list changed,
+ * after reading the passwords fresh so a connection added over there connects
+ * here without asking again.
+ * @returns {Promise}
+ */
+export async function refreshConnectionsFromDisk() {
+ try {
+ // Let this app's own pending writes land first, so the file includes them.
+ await _mirrorChain
+ await _secretsChain
+ const json = /** @type {string | null} */ (await invoke('connections_store_read'))
+ if (json == null) return false
+ const data = JSON.parse(json)
+ if (!Array.isArray(data?.connections)) return false
+ const list = JSON.stringify(data.connections)
+ const deleted = JSON.stringify(Array.isArray(data.deleted) ? data.deleted : [])
+ // The backend may write keys in another order: compare values, not text.
+ const same = (/** @type {string} */ a, /** @type {string | null} */ b) => {
+ try { return canonical(JSON.parse(a)) === canonical(JSON.parse(b ?? '[]')) } catch { return false }
+ }
+ if (same(list, localStorage.getItem(STORAGE_KEY)) && same(deleted, localStorage.getItem(DELETED_KEY))) return false
+ if (_vaultReady) {
+ try {
+ const sec = /** @type {string | null} */ (await invoke('conn_secrets_read_fresh'))
+ const parsed = sec ? JSON.parse(sec) : {}
+ _secretCache = new Map(Object.entries(parsed ?? {}))
+ } catch { /* keep the cache: a connection without its password asks for it */ }
+ }
+ localStorage.setItem(STORAGE_KEY, list)
+ localStorage.setItem(DELETED_KEY, deleted)
+ return true
+ } catch {
+ return false
+ }
+}
+
/**
* Where a connection is filed and how it looks, set by the person rather than
* by whatever dialled it. The one-click paths (a provider's database, a Docker
@@ -417,6 +487,7 @@ export function upsertConnection(conn, { revive = false, exact = false } = {}) {
}
deleted.delete(conn.id)
saveDeletedIds(deleted)
+ _revived.add(conn.id)
}
const list = loadSavedConnections()
const idx = list.findIndex((c) => c.id === conn.id)
@@ -482,6 +553,7 @@ export function removeConnection(id) {
closeParkedFor(id)
const deleted = loadDeletedIds()
deleted.add(id)
+ _revived.delete(id)
// Before saveConnections, so the disk copy it writes carries the id too.
saveDeletedIds(deleted)
// Sync carries the delete to other devices (a no-op while sync applies one).
diff --git a/src/lib/stores/connections.test.js b/src/lib/stores/connections.test.js
index 9c7d2bd3..f89a5d6d 100644
--- a/src/lib/stores/connections.test.js
+++ b/src/lib/stores/connections.test.js
@@ -15,7 +15,19 @@ vi.mock('@tauri-apps/api/core', () => ({
if (cmd === 'connections_store_read') return diskPayload
if (cmd === 'close_parked') { closedParked.push(args.connection); return null }
if (cmd === 'conn_secrets_read') { if (vaultBroken) throw new Error('no keychain'); return vault }
- if (cmd === 'conn_secrets_write') { if (vaultBroken) throw new Error('no keychain'); vault = args.json; return null }
+ if (cmd === 'conn_secrets_read_fresh') { if (vaultBroken) throw new Error('no keychain'); return vault }
+ if (cmd === 'conn_secrets_merge') {
+ // Same rules as merge_conn_secrets in secrets.rs.
+ if (vaultBroken) throw new Error('no keychain')
+ const map = vault ? JSON.parse(vault) : {}
+ for (const [id, sec] of Object.entries(JSON.parse(args.set))) {
+ if (sec && Object.keys(sec).length) map[id] = sec
+ else delete map[id]
+ }
+ for (const id of args.removed) delete map[id]
+ vault = Object.keys(map).length ? JSON.stringify(map) : null
+ return null
+ }
throw new Error(`unexpected command ${cmd}`)
}),
}))
@@ -165,6 +177,48 @@ describe('connection secrets in the keychain', () => {
expect(JSON.parse(vault ?? '{}').n).toBeUndefined()
})
+ it("leaves the other track's passwords in the shared keychain", async () => {
+ // Nightly saved "n" (with its password); this app (Stable) never had it.
+ vault = JSON.stringify({ n: { password: 'nightly' }, gone: { password: 'x' } })
+ diskPayload = JSON.stringify({ connections: [{ id: 's', type: 'postgres', name: 's', host: 'h' }], deleted: ['gone'] })
+ const store = await import('./connections.js')
+ await store.hydrateConnectionsFromDisk()
+ // Startup drops only a deleted id, not every id missing from this list.
+ expect(JSON.parse(vault ?? '{}')).toEqual({ n: { password: 'nightly' } })
+ // A save here merges, so "n" survives this app's writes too.
+ store.upsertConnection({ id: 's', type: 'postgres', name: 's', host: 'h', password: 'stable' })
+ await flush()
+ expect(JSON.parse(vault ?? '{}')).toEqual({ n: { password: 'nightly' }, s: { password: 'stable' } })
+ })
+
+ it('picks up a connection the other track added, with its password', async () => {
+ diskPayload = JSON.stringify({ connections: [{ id: 's', type: 'postgres', name: 's' }], deleted: [] })
+ const store = await import('./connections.js')
+ await store.hydrateConnectionsFromDisk()
+ expect(await store.refreshConnectionsFromDisk()).toBe(false)
+ // Meanwhile Nightly adds "n" to the shared file and keychain.
+ diskPayload = JSON.stringify({ connections: [{ id: 's', type: 'postgres', name: 's' }, { id: 'n', type: 'mysql', name: 'n' }], deleted: [] })
+ vault = JSON.stringify({ n: { password: 'nightly' } })
+ expect(await store.refreshConnectionsFromDisk()).toBe(true)
+ const n = store.loadSavedConnections().find((c) => c.id === 'n')
+ expect(n?.password).toBe('nightly')
+ // Same list with keys in another order is not a change.
+ diskPayload = JSON.stringify({ deleted: [], connections: [{ name: 's', type: 'postgres', id: 's' }, { name: 'n', id: 'n', type: 'mysql' }] })
+ expect(await store.refreshConnectionsFromDisk()).toBe(false)
+ })
+
+ it('tells the disk copy about a revived connection', async () => {
+ const store = await import('./connections.js')
+ await store.hydrateConnectionsFromDisk()
+ store.upsertConnection({ id: 'sample', type: 'sqlite', name: 'Sample', filePath: '/s.db' })
+ store.removeConnection('sample')
+ store.upsertConnection({ id: 'sample', type: 'sqlite', name: 'Sample', filePath: '/s.db' }, { revive: true })
+ await flush()
+ const last = JSON.parse(writes.at(-1) ?? '{}')
+ expect(last.revived).toEqual(['sample'])
+ expect(last.deleted).toEqual([])
+ })
+
it('keeps secrets inline when the keychain is unavailable', async () => {
vaultBroken = true
localStorage.setItem('stroke:connections', JSON.stringify(legacy))