mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 07:32:54 +00:00
* feat(plugins): add sync providers and encrypted sidecar sync
Implement PR 8 of the plugin platform (#2269): namespaced sync Providers
with provider.sync, encrypted-object storage operations, WebDAV through the
shared storage surface, dynamic cloud provider IDs, and non-cascade sidecars
for sync:true plugin settings plus account/CRDT baselines.
Related to #2269
* fix(plugins): wire sync providers and sidecars into production paths
Close PR 8 integration gaps: dynamic plugin provider registry and
getConnectedAdapter createPluginStorage, WebDAV through EncryptedObjectStorage,
cloud payload collect/apply for pluginSidecars, and IPC/preload bridges.
Related to #2269
* fix(plugins): preserve WebDAV auth reuse and resourceId through EOS wrap
encryptedObjectStorageAsCloudAdapter now reports initiallyAuthenticated when
config exists (matching raw WebDAVAdapter) so getConnectedAdapter reuses the
cached instance, and preserves/refreshes resourceId from the backing adapter
instead of always forcing the default object key.
Related to #2269
* fix(plugins): address Codex PR 8 review findings
Carry pluginSidecars through merge/convergent uploads, lazy-connect before
object I/O with revision-aware writes, fail closed on sidecar collect errors,
expose connectPluginProvider, preserve remote timestamps, and drop deleted
installed-plugin settings from collection.
Related to #2269
* fix(plugins): close remaining Codex P1/P2 sync issues
Lazy stream cancel for inline sync reads, must-not-exist conditional writes,
three-way sidecar merge for local resets, validated apply path for settings,
and merge remote sidecars into convergent uploads.
Related to #2269
* fix(plugins): harden sidecar collect/apply and write verification
Preserve last-known sidecars when the host is offline, apply remote
deletions for installed plugins, force convergent upload on sidecar
divergence, verify plugin object writes, and pick plugin providers in
auto-sync startup checks.
Related to #2269
* fix(plugins): close remaining Codex sidecar/sync path gaps
Include sidecars in auto-sync payload and hash, keep explicit empty bundles,
fail apply when host is offline, require getCapabilities, and carry merged
sidecars into convergent local apply/upload decisions.
Related to #2269
* fix(plugins): close Codex P1 sidecar collect/apply/merge gaps
Treat null host responses as unavailable, persist collected settings into
the non-cascade table, retain decoded convergent payloads, use three-way
sidecar merge for deletions, and fail apply only on operational errors.
Also treat non-empty plugin sidecars as meaningful sync data and re-run
auto-sync when plugin contributions change.
Related to #2269
* fix(plugins): close remaining Codex P1 sidecar and provider gaps
Replay offline-cached sidecars before collect, keep explicit empty
sidecar fields after three-way merge, thread sidecars through convergent
conflict/downgrade materialization, keep gated-off plugin providers from
joining sync as connected, and rebind plugin EOS sessions after runtime
replacement.
Related to #2269
* fix(plugins): separate pending remote sidecars from last-known cache
Replay only host-offline remote applies into the DB, keep last-known as
upload fallback, use contribution reset for deleted settings, probe real
plugin host readiness, rebind storage events for plugin providers, clear
plugin bases on reset, and swallow disconnect rejections.
Related to #2269
* fix(plugins): hydrate retained sidecars and gate missing providers
Materialize installed-plugin settings from retained sidecars before
collect pruning, clear sidecars on setting reset, fail closed when
pending remote replay fails, and only restore plugin providers that are
contribution-available. Also initialize dynamic provider counters on
connect.
Related to #2269
* fix(plugins): surface plugin sync providers and preserve conflict sidecars
Collect live sidecars before convergent conflict apply/upload, include
dynamic providers in status and disconnect paths, and list plugin sync
providers in Cloud Sync settings for connect/disconnect.
Related to #2269
* fix(plugins): per-provider sidecar baselines and safer collect/UI
Merge convergent sidecars against each provider's own baseline, keep
retained settings in sidecars without unvalidated plugin_settings writes,
guard isConnectDisabled for never-connected plugins, and fall back plugin
provider labels/icons in the status popup.
Related to #2269
* fix(plugins): correct plugin provider discovery and sidecar reverify
Read nested contribution metadata for sync provider cards, gate empty-config
connects, enforce single-provider disconnect for plugins, and re-verify after
sidecar-only convergent uploads so baselines track the post-write payload.
Related to #2269
* fix(plugins): refresh sync provider availability from live contributions
Replace the contribution-available provider ID set when plugins change so
disabled or uninstalled sync providers leave the auto-sync ready set and
drop to disconnected with config retained.
Related to #2269
* fix(plugins): preserve sidecars in backups, pending queue, and migration
Attach last-known plugin sidecars to protective local vault backups, fail
operationally when pending/last-known storage writes are rejected, carry
sidecars through convergent migration publish, and reject trailing garbage
on encrypted object parse.
Related to #2269
* fix(plugins): abort conflict collect failures and merge migration sidecars
Rethrow operational sidecar collection errors during convergent conflict
resolution, LWW-union all local/provider sidecars during convergent
migration, re-collect last-known after apply, and prefer live host collect
for protective local vault backups.
Related to #2269
* fix(plugins): restore pluginBridge tests, fix import path, union downgrade sidecars
Correct SettingsSyncTab dynamic import path so production builds resolve,
add ipcMain.on to the plugin bridge test double, union all provider
sidecars on convergent downgrade, keep empty last-known resets, and skip
force-delete when contribution reset validation rejects.
Related to #2269
* fix(plugins): abort backup collect failures and honor empty remote wipes
Only fall back to last-known sidecars when the host is unavailable during
protective backups, treat explicit empty remote sidecar bundles as
authoritative wipes, remove lint-blocking anys from harness tests, and
use a stable equal-timestamp sidecar tie-break.
Related to #2269
* fix(plugins): include sidecars in equality, preserve merge base on reconnect
Compare pluginSidecars in cloudSyncPayloadsEqual for migration freshness,
clear plugin merge state only when account/resource changes on reconnect,
cancel accepted sync read streams on failure, and stop re-connecting the
plugin EOS adapter on every I/O.
Related to #2269
* fix(plugins): drop cached adapters on contribution refresh
Invalidate plugin sync adapters when live contributions change so a
restarted runtime gets a fresh connect, take offline any non-disconnected
missing providers, and latch version-change backups only after cancellation
checks pass.
Related to #2269
* fix(plugins): preserve omitted sidecars and hydrate on plugin enable
Treat missing pluginSidecars as legacy/unsupported in three-way merge
(only explicit empty is a wipe), hydrate retained settings when a plugin
enables, and re-enable retained provider configs when contributions return.
Related to #2269
* fix(plugins): keep newer retained sidecars over older stored settings
When collection merges existing sidecars with plugin_settings rows, prefer
the newer updatedAt so a schema-rejected remote value retained only in
the sidecar table is not overwritten by a stale local setting.
Related to #2269
* fix(plugins): seal plugin provider config and clear base on config change
Encrypt opaque plugin provider configuration before persistence, clear
merge state when reconnect configuration changes, and stream sync objects
below a base64-safe inline threshold so control-plane JSON stays in budget.
Related to #2269
* fix(plugins): seal plugin configs by provider id and hydrate before start
Encrypt plugin provider configuration only for non-builtin provider IDs
so field-name collisions cannot skip sealing, hydrate retained sidecars
before plugin enable starts the runtime, and stop tearing down live
adapters on no-op availability refreshes.
Related to #2269
* fix(plugins): seal scalar configs, safe inline reads, stable config fingerprint
Encrypt any non-builtin plugin configuration JSON shape, enforce the
base64-safe inline read cutoff, and compare reconnect configurations with
order-independent fingerprints so merge bases are not cleared spuriously.
Related to #2269
* fix(plugins): thread sync credentials and harden availability checks
Pass SyncConnectPayload.credential through the object-storage and IPC host
path, treat only null/undefined config as missing, and avoid wiping the
plugin provider availability catalog on transient discovery failures.
Related to #2269
* fix(plugins): unblock schema-required sync connect and scalar configs
Add a JSON config dialog for plugin sync providers that declare a
required configurationSchema, reuse retained configs on reconnect, and
treat falsy scalar configs as present in readiness and adapter lookup.
Related to #2269
* fix(plugins): rebind sync adapters on contribution refresh
Drop cached plugin sync adapters when live contributions still include
the provider so replaced runtimes get a fresh connect, and seal falsy
scalar plugin configs with nullish presence checks end-to-end.
Related to #2269
* fix(plugins): apply sidecars under current declared setting scopes
When a plugin update changes a syncable setting scope, write under the
live declaration and drop the obsolete scope row so collection does not
republish duplicates.
Related to #2269
* fix(plugins): seal config envelopes and stabilize provider counters
Use an unambiguous host-owned sealed-config envelope, initialize
dynamic-provider sequence counters before cross-window decrypt, keep
three-way sidecar deletions during convergent migration, and create the
sidecar table for existing schema-1 plugin databases.
Related to #2269
* fix(plugins): avoid mid-sync adapter drops and sidecar resurrection
Keep plugin adapters across setting-only contribution refreshes and
rebind sessions on ensureConnected; stop preserving local-only missing
plugin sidecars when remote already mentions the plugin; compare configs
with nullish semantics across windows; swallow quick-connect failures
after toasting.
Related to #2269
* fix(plugins): restore remoteKeys for installed sidecar resets
Reintroduce the remote key set used when dropping installed-plugin
settings the remote no longer carries so applyFromSync cannot throw
after replacing the sidecar table.
Related to #2269
* fix(plugins): honor conflict policy and re-key scoped sidecars
Pass preferCloud/preferLocal into three-way sidecar merge, use
per-provider baselines on convergent downgrade so local resets are not
resurrected, and re-key applied settings under the current declared
scope coordinates.
Related to #2269
* fix(plugins): keep strategy on final sidecar merge and block dual legacy
Pass preferCloud/preferLocal through the final verified sidecar merge,
avoid auto-reactivating a retained plugin while another legacy provider
is already connected, cache plugin adapters only after initializeSync,
and preserve explicit null configuration values.
Related to #2269
* fix(plugins): strip plugin avatars and protect last-known sidecars
Do not load plugin-supplied avatar URLs in the main renderer, and avoid
overwriting last-known sidecars with an unmerged remote bundle when the
post-apply collect fails or is non-authoritative.
Related to #2269
* fix(plugins): strip top-bar plugin avatars and stop always-rebind
Hide plugin avatar URLs in the sync status popover, drop always-on
rebindSession for plugin adapters, and read convergent mode from the
local config module when deciding whether to auto-reactivate providers.
Related to #2269
* fix(plugins): use window.netcatty for sync IPC and allow null configs
Read plugin sync and sidecar APIs from the production preload global,
and treat an explicit config property (including JSON null) as present
for readiness and adapter lookup.
Related to #2269
* fix(plugins): close residual Codex P2 sync edge cases
Three-way merge sidecars during v2 convergent migration so local
resets are not resurrected, preserve explicit null plugin configs on
adapter rebuild, rebind EOS sessions after I/O failure for replaced
runtimes, and accept the public inlineObjectBytes limit on readObject.
Related to #2269
* fix(plugins): keep inline read cutoff under control-plane budget
Restore the base64-safe inline cutoff so advertised-size reads cannot
overflow the 128 KiB provider JSON envelope; providers near the public
inlineObjectBytes limit must stream.
Related to #2269
* fix(plugins): harden sidecar empty-vault, hydrate LWW, and credential seals
Close remaining Codex/review gaps for PR8: keep empty sidecar shells from
bypassing upload guards, skip startup hydrate when local settings are newer,
seal/validate durable plugin credential refs, and cover schema 1→2 migration.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): rebind plugin sync sessions and keep null configs
Address Codex P2 on 8ae52c54: re-issue connect after runtime replacement,
and treat stored config:null as present in the dashboard reconnect path.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): defer empty sidecar last-known until sync succeeds
Keep prior non-empty last-known across an authoritative empty collect so the
empty-vault guard can still recognize a plugin-only reset, then commit the
empty cache after a successful upload.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): avoid restricted localStorage global in sidecar test
Bind the mock to a local binding so eslint no-restricted-globals stays clean.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): commit applied sidecar cache and reset orphan settings
Prefer merged payload sidecars when updating last-known after sync, and clear
syncable plugin_settings that lack sidecar rows on authoritative remote deletes.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): reseal marker-shaped plugin configs instead of skipping
Exact __netcatty_plugin_config_v1 collisions no longer bypass encryption; trusted
host envelopes are unwrapped then resealed, and false collisions are sealed whole.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): keep merged sidecars in last-known after apply collect fails
Return applyFromSync entries from IPC and use them when follow-up collect fails,
so preserved missing-plugin rows are not dropped from the offline upload cache.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): delete stale-scope settings at stored coordinates on reset
After resetSetting against the current declared scope, also remove the row at
the sidecar's stored scope so scope migrations cannot republish remote deletes.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): close sync-provider credential, stream, and sidecar gaps
Make SecretRef usable end-to-end via lease-bound network.request, keep
renderer/main transfers abortable and bounded, and stop sidecar
last-known/pending from corrupting backups and conflict apply.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): harden single-provider, secret schema, and conditional conflicts
Keep legacy restart from dual-connecting providers, extract writeOnly schema
secrets into OS storage, and surface conditional-write precondition failures
as conflict UI instead of a generic sync error.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): dedupe sync type imports for lint
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix: address Codex review on PR #2713
* fix(plugins): wait for sidecar host before version backup latch
Defer the one-shot upgrade snapshot until the plugin sidecar host is ready (or a short grace), and catch getDataHash failures so pending/baseline paths do not reject unhandled.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): require sidecar grace before version backup
Always wait for contributions/grace tick before the upgrade snapshot, and only latch after a non-cancelled attempt so cleanup cannot suppress retries.
Co-authored-by: Cursor <cursoragent@cursor.com>
---------
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com>
953 lines
37 KiB
TypeScript
953 lines
37 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
import {
|
|
BUILTIN_CLOUD_PROVIDERS,
|
|
SYNC_CONSTANTS,
|
|
SYNC_STORAGE_KEYS,
|
|
cleanOneDriveErrorMessage,
|
|
generateDeviceId,
|
|
getDefaultDeviceName,
|
|
isBuiltinCloudProvider,
|
|
isOneDriveReauthRequiredMessage,
|
|
providerConnectionStorageKey,
|
|
type CloudProvider,
|
|
type MasterKeyConfig,
|
|
type ProviderConnection,
|
|
type SecurityState,
|
|
type SyncHistoryEntry,
|
|
} from '../../../domain/sync';
|
|
import { isPluginCloudProviderId } from '../../../domain/cloudProviderIds';
|
|
import { createPluginSyncObjectStorage } from '../adapters/pluginSyncObjectStorage';
|
|
import {
|
|
createPluginSyncIpcHost,
|
|
isPluginSyncIpcAvailable,
|
|
} from '../adapters/pluginSyncIpcHost';
|
|
import type { EncryptedObjectStorage } from '../../../domain/encryptedObjectStorage';
|
|
import {
|
|
DEFAULT_CLOUD_SYNC_STRATEGY,
|
|
normalizeCloudSyncStrategy,
|
|
} from '../../../domain/syncStrategy';
|
|
import { EncryptionService } from '../EncryptionService';
|
|
import { createAdapter } from '../adapters';
|
|
import { localStorageAdapter } from '../../persistence/localStorageAdapter';
|
|
import {
|
|
decryptProviderSecrets,
|
|
encryptProviderSecrets,
|
|
} from '../../persistence/secureFieldAdapter';
|
|
import type { CloudAdapter } from '../adapters';
|
|
import type { SyncManagerState } from '../CloudSyncManager';
|
|
import { getConvergentSyncLocalConfig } from '../convergentSyncConfig';
|
|
|
|
const SYNC_HISTORY_STORAGE_KEY = 'netcatty_sync_history_v1';
|
|
|
|
/** Ensure per-provider sequence counters exist (dynamic plugins arrive late). */
|
|
function ensureProviderSeqCounters(manager: any, provider: CloudProvider): void {
|
|
// Lightweight test harnesses may omit the maps entirely; initialize before indexing.
|
|
if (manager.providerDecryptSeq == null || typeof manager.providerDecryptSeq !== 'object') {
|
|
manager.providerDecryptSeq = {};
|
|
}
|
|
if (manager.providerWriteSeq == null || typeof manager.providerWriteSeq !== 'object') {
|
|
manager.providerWriteSeq = {};
|
|
}
|
|
if (manager.providerDecrypted == null || typeof manager.providerDecrypted !== 'object') {
|
|
manager.providerDecrypted = {};
|
|
}
|
|
if (manager.providerDecryptSeq[provider] == null || Number.isNaN(manager.providerDecryptSeq[provider])) {
|
|
manager.providerDecryptSeq[provider] = 0;
|
|
}
|
|
if (manager.providerWriteSeq[provider] == null || Number.isNaN(manager.providerWriteSeq[provider])) {
|
|
manager.providerWriteSeq[provider] = 0;
|
|
}
|
|
if (manager.providerDecrypted[provider] == null) {
|
|
manager.providerDecrypted[provider] = false;
|
|
}
|
|
}
|
|
|
|
export function loadInitialStateImpl(this: any): SyncManagerState {
|
|
// Load persisted configuration
|
|
const masterKeyConfig = this.loadFromStorage<MasterKeyConfig>(
|
|
SYNC_STORAGE_KEYS.MASTER_KEY_CONFIG
|
|
);
|
|
|
|
const deviceId = this.loadFromStorage<string>(SYNC_STORAGE_KEYS.DEVICE_ID)
|
|
|| generateDeviceId();
|
|
|
|
const deviceName = this.loadFromStorage<string>(SYNC_STORAGE_KEYS.DEVICE_NAME)
|
|
|| getDefaultDeviceName();
|
|
|
|
const syncConfig = this.loadFromStorage<{
|
|
autoSync: boolean;
|
|
interval: number;
|
|
localVersion: number;
|
|
localUpdatedAt: number;
|
|
remoteVersion: number;
|
|
remoteUpdatedAt: number;
|
|
syncStrategy?: unknown;
|
|
}>(SYNC_STORAGE_KEYS.SYNC_CONFIG);
|
|
|
|
// Load sync history
|
|
const syncHistory = this.loadFromStorage<SyncHistoryEntry[]>(SYNC_HISTORY_STORAGE_KEY) || [];
|
|
|
|
// Determine initial security state
|
|
const securityState: SecurityState = masterKeyConfig ? 'LOCKED' : 'NO_KEY';
|
|
|
|
// Load provider connections (built-ins + registered plugin provider IDs)
|
|
const providers: Record<CloudProvider, ProviderConnection> = {
|
|
github: this.loadProviderConnection('github'),
|
|
google: this.loadProviderConnection('google'),
|
|
onedrive: this.loadProviderConnection('onedrive'),
|
|
webdav: this.loadProviderConnection('webdav'),
|
|
s3: this.loadProviderConnection('s3'),
|
|
};
|
|
for (const pluginProviderId of listRegisteredPluginProviderIdsImpl.call(this)) {
|
|
providers[pluginProviderId] = this.loadProviderConnection(pluginProviderId);
|
|
if (this.providerWriteSeq[pluginProviderId] == null) this.providerWriteSeq[pluginProviderId] = 0;
|
|
if (this.providerDecryptSeq[pluginProviderId] == null) this.providerDecryptSeq[pluginProviderId] = 0;
|
|
if (this.providerDecrypted[pluginProviderId] == null) this.providerDecrypted[pluginProviderId] = false;
|
|
if (this.providerAuthAttemptSeq[pluginProviderId] == null) this.providerAuthAttemptSeq[pluginProviderId] = 0;
|
|
if (this.providerAuthRestoreState[pluginProviderId] === undefined) {
|
|
this.providerAuthRestoreState[pluginProviderId] = null;
|
|
}
|
|
}
|
|
enforceLegacySingleProviderConnected(providers);
|
|
|
|
// Save device ID if new
|
|
this.saveToStorage(SYNC_STORAGE_KEYS.DEVICE_ID, deviceId);
|
|
this.saveToStorage(SYNC_STORAGE_KEYS.DEVICE_NAME, deviceName);
|
|
|
|
return {
|
|
securityState,
|
|
syncState: 'IDLE',
|
|
masterKeyConfig,
|
|
unlockedKey: null,
|
|
providers,
|
|
deviceId,
|
|
deviceName,
|
|
localVersion: syncConfig?.localVersion || 0,
|
|
localUpdatedAt: syncConfig?.localUpdatedAt || 0,
|
|
remoteVersion: syncConfig?.remoteVersion || 0,
|
|
remoteUpdatedAt: syncConfig?.remoteUpdatedAt || 0,
|
|
currentConflict: null,
|
|
lastError: null,
|
|
autoSyncEnabled: syncConfig?.autoSync || false,
|
|
autoSyncInterval: syncConfig?.interval || SYNC_CONSTANTS.DEFAULT_AUTO_SYNC_INTERVAL,
|
|
syncStrategy: normalizeCloudSyncStrategy(syncConfig?.syncStrategy ?? DEFAULT_CLOUD_SYNC_STRATEGY),
|
|
syncHistory,
|
|
pendingLocalSync: false,
|
|
convergentConflicts: [],
|
|
};
|
|
}
|
|
|
|
export function loadProviderConnectionImpl(this: any,provider: CloudProvider): ProviderConnection {
|
|
const key = providerConnectionStorageKey(provider);
|
|
const stored = this.loadFromStorage<Partial<ProviderConnection>>(key);
|
|
|
|
// Config may be a valid scalar including JSON null (schema type: "null").
|
|
// Presence is property existence; only a missing property means absent.
|
|
const hasCreds = stored?.tokens != null
|
|
|| (stored != null && Object.prototype.hasOwnProperty.call(stored, 'config'));
|
|
// Determine the correct status: if tokens or config exist, should be 'connected'
|
|
// Never restore 'syncing' or 'error' status - those are transient.
|
|
// Dynamic plugin providers only rejoin sync when the host is ready AND the
|
|
// provider was recorded as contribution-available (survives restart).
|
|
// Host-off or uninstalled providers stay disconnected with config retained.
|
|
let status: ProviderConnection['status'] = hasCreds ? 'connected' : 'disconnected';
|
|
if (isPluginCloudProviderId(provider) && hasCreds) {
|
|
const hostReady = isPluginSyncIpcAvailable();
|
|
const available = listAvailablePluginSyncProviderIdsImpl.call(this);
|
|
status = hostReady && available.includes(provider) ? 'connected' : 'disconnected';
|
|
}
|
|
|
|
return {
|
|
provider,
|
|
...stored,
|
|
status, // Must be last to override any stored 'syncing' or 'error' status
|
|
} as ProviderConnection;
|
|
}
|
|
|
|
/**
|
|
* Legacy (non-convergent) mode allows only one ready provider. After restart,
|
|
* retained plugin configs can race with a builtin the user switched to — keep
|
|
* config but force extras offline (same policy as setAvailablePluginSyncProviderIds).
|
|
*/
|
|
export function enforceLegacySingleProviderConnected(
|
|
providers: Record<string, ProviderConnection>,
|
|
): void {
|
|
let convergentActive = false;
|
|
try {
|
|
convergentActive = getConvergentSyncLocalConfig().initialized === true;
|
|
} catch {
|
|
convergentActive = false;
|
|
}
|
|
if (convergentActive) return;
|
|
|
|
const readyIds = Object.entries(providers)
|
|
.filter(([, conn]) => conn != null && (conn.status === 'connected' || conn.status === 'syncing'))
|
|
.map(([id]) => id);
|
|
if (readyIds.length <= 1) return;
|
|
|
|
// Prefer a builtin (stable UI order), else first ready id.
|
|
const preferred = BUILTIN_CLOUD_PROVIDERS.find((id) => readyIds.includes(id))
|
|
?? readyIds[0]!;
|
|
for (const id of readyIds) {
|
|
if (id === preferred) continue;
|
|
const conn = providers[id];
|
|
if (!conn) continue;
|
|
providers[id] = {
|
|
...conn,
|
|
status: 'disconnected',
|
|
};
|
|
}
|
|
}
|
|
|
|
export function listRegisteredPluginProviderIdsImpl(this: any): string[] {
|
|
const raw = this.loadFromStorage<unknown>(SYNC_STORAGE_KEYS.PLUGIN_CLOUD_PROVIDERS);
|
|
if (!Array.isArray(raw)) return [];
|
|
return raw
|
|
.filter((id: unknown): id is string => typeof id === 'string' && isPluginCloudProviderId(id))
|
|
.sort();
|
|
}
|
|
|
|
export function listAvailablePluginSyncProviderIdsImpl(this: any): string[] {
|
|
if (typeof this.loadFromStorage !== 'function') return [];
|
|
const raw = this.loadFromStorage(SYNC_STORAGE_KEYS.AVAILABLE_PLUGIN_SYNC_PROVIDERS) as unknown;
|
|
if (!Array.isArray(raw)) return [];
|
|
return raw
|
|
.filter((id: unknown): id is string => typeof id === 'string' && isPluginCloudProviderId(id))
|
|
.sort();
|
|
}
|
|
|
|
export function markPluginSyncProviderAvailableImpl(this: any, provider: CloudProvider): void {
|
|
if (!isPluginCloudProviderId(provider)) return;
|
|
const next = new Set(listAvailablePluginSyncProviderIdsImpl.call(this));
|
|
next.add(provider);
|
|
this.saveToStorage(SYNC_STORAGE_KEYS.AVAILABLE_PLUGIN_SYNC_PROVIDERS, [...next].sort());
|
|
}
|
|
|
|
export function markPluginSyncProviderUnavailableImpl(this: any, provider: CloudProvider): void {
|
|
if (!isPluginCloudProviderId(provider)) return;
|
|
const next = listAvailablePluginSyncProviderIdsImpl.call(this).filter((id) => id !== provider);
|
|
if (next.length === 0) {
|
|
this.removeFromStorage(SYNC_STORAGE_KEYS.AVAILABLE_PLUGIN_SYNC_PROVIDERS);
|
|
} else {
|
|
this.saveToStorage(SYNC_STORAGE_KEYS.AVAILABLE_PLUGIN_SYNC_PROVIDERS, next);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Replace the live contribution-available set. Called when the renderer
|
|
* refreshes plugin sync provider contributions so disabled/uninstalled
|
|
* providers stop rejoining sync cycles after restart.
|
|
*/
|
|
export function setAvailablePluginSyncProviderIdsImpl(
|
|
this: any,
|
|
providerIds: readonly string[],
|
|
): void {
|
|
const previous = listAvailablePluginSyncProviderIdsImpl.call(this);
|
|
const previousSet = new Set(previous);
|
|
const next = [...new Set(
|
|
providerIds.filter((id): id is string => typeof id === 'string' && isPluginCloudProviderId(id)),
|
|
)].sort();
|
|
if (next.length === 0) {
|
|
this.removeFromStorage(SYNC_STORAGE_KEYS.AVAILABLE_PLUGIN_SYNC_PROVIDERS);
|
|
} else {
|
|
this.saveToStorage(SYNC_STORAGE_KEYS.AVAILABLE_PLUGIN_SYNC_PROVIDERS, next);
|
|
}
|
|
if (!this.state?.providers) return;
|
|
let changed = false;
|
|
for (const id of Object.keys(this.state.providers)) {
|
|
if (!isPluginCloudProviderId(id)) continue;
|
|
const conn = this.state.providers[id];
|
|
if (!conn) continue;
|
|
if (!next.includes(id)) {
|
|
// Disabled/uninstalled: leave config, force offline ready-state.
|
|
if (conn.status !== 'disconnected') {
|
|
this.state.providers[id] = {
|
|
...conn,
|
|
status: 'disconnected',
|
|
error: 'Plugin sync provider is no longer installed or enabled',
|
|
};
|
|
changed = true;
|
|
}
|
|
const adapter = this.adapters?.get?.(id);
|
|
if (adapter) {
|
|
try { adapter.signOut(); } catch { /* ignore */ }
|
|
this.adapters.delete(id);
|
|
}
|
|
continue;
|
|
}
|
|
// Provider is still contributed. Only drop the cached adapter when the
|
|
// provider newly re-entered the available set (install/enable/restart of
|
|
// the package). Setting-only contribution noise keeps membership stable
|
|
// and must not signOut mid-sync — plugin adapters rebind via rebindSession.
|
|
if (!previousSet.has(id) && this.adapters?.has?.(id)) {
|
|
const adapter = this.adapters.get(id);
|
|
try { adapter?.signOut?.(); } catch { /* ignore */ }
|
|
this.adapters.delete(id);
|
|
}
|
|
// Re-enable retained configs whenever the contribution reappears, but only
|
|
// when doing so cannot create a second active provider in legacy
|
|
// single-provider mode (convergent multi-provider allows it).
|
|
// Config may be a valid falsy scalar (false, 0, "") — only null/undefined means absent.
|
|
const hasRetainedConfig = conn.tokens != null
|
|
|| Object.prototype.hasOwnProperty.call(conn, 'config');
|
|
if (hasRetainedConfig && conn.status === 'disconnected') {
|
|
let convergentActive = false;
|
|
try {
|
|
convergentActive = getConvergentSyncLocalConfig().initialized === true;
|
|
} catch {
|
|
convergentActive = false;
|
|
}
|
|
const anotherReady = Object.entries(this.state.providers as Record<string, ProviderConnection>)
|
|
.some(([otherId, other]) => (
|
|
otherId !== id
|
|
&& other != null
|
|
&& (other.status === 'connected' || other.status === 'syncing')
|
|
));
|
|
if (!convergentActive && anotherReady) {
|
|
// Keep disconnected with retained config until the user reconnects
|
|
// explicitly; auto-reactivation would mirror two legacy providers.
|
|
continue;
|
|
}
|
|
this.state.providers[id] = {
|
|
...conn,
|
|
status: 'connected',
|
|
error: undefined,
|
|
};
|
|
changed = true;
|
|
}
|
|
}
|
|
if (changed && typeof this.notifyStateChange === 'function') {
|
|
this.notifyStateChange();
|
|
}
|
|
}
|
|
|
|
export function registerPluginProviderIdImpl(this: any, provider: CloudProvider): void {
|
|
if (!isPluginCloudProviderId(provider)) return;
|
|
const next = new Set(listRegisteredPluginProviderIdsImpl.call(this));
|
|
next.add(provider);
|
|
this.saveToStorage(SYNC_STORAGE_KEYS.PLUGIN_CLOUD_PROVIDERS, [...next].sort());
|
|
markPluginSyncProviderAvailableImpl.call(this, provider);
|
|
}
|
|
|
|
export function unregisterPluginProviderIdImpl(this: any, provider: CloudProvider): void {
|
|
if (!isPluginCloudProviderId(provider)) return;
|
|
const next = listRegisteredPluginProviderIdsImpl.call(this).filter((id) => id !== provider);
|
|
if (next.length === 0) {
|
|
this.removeFromStorage(SYNC_STORAGE_KEYS.PLUGIN_CLOUD_PROVIDERS);
|
|
} else {
|
|
this.saveToStorage(SYNC_STORAGE_KEYS.PLUGIN_CLOUD_PROVIDERS, next);
|
|
}
|
|
markPluginSyncProviderUnavailableImpl.call(this, provider);
|
|
}
|
|
|
|
export async function initProviderDecryptionImpl(this: any): Promise<void> {
|
|
const providers: CloudProvider[] = [
|
|
...BUILTIN_CLOUD_PROVIDERS,
|
|
...listRegisteredPluginProviderIdsImpl.call(this),
|
|
];
|
|
for (const p of providers) {
|
|
try {
|
|
const conn = this.state.providers[p];
|
|
// Config may be a valid scalar including JSON null — presence is property existence.
|
|
if (conn.tokens != null || Object.prototype.hasOwnProperty.call(conn, 'config')) {
|
|
const seq = ++this.providerDecryptSeq[p];
|
|
const decrypted = await decryptProviderSecrets(conn);
|
|
// Only apply if no newer update has occurred during the async gap
|
|
if (seq === this.providerDecryptSeq[p]) {
|
|
this.state.providers[p] = decrypted;
|
|
this.providerDecrypted[p] = true;
|
|
}
|
|
} else {
|
|
// No secrets to decrypt — mark as done
|
|
this.providerDecrypted[p] = true;
|
|
}
|
|
} catch {
|
|
// Decryption failed — likely the Electron IPC handler is not yet
|
|
// registered. getConnectedAdapter() will retry for this provider.
|
|
}
|
|
}
|
|
this.notifyStateChange();
|
|
}
|
|
|
|
export async function saveProviderConnectionImpl(this: any,
|
|
provider: CloudProvider,
|
|
connection: ProviderConnection,
|
|
authAttemptId?: number
|
|
): Promise<void> {
|
|
const key = providerConnectionStorageKey(provider);
|
|
// Use write-specific counter so status-only updates cannot discard
|
|
// an in-flight encrypted write that must be persisted.
|
|
ensureProviderSeqCounters(this, provider);
|
|
const seq = ++this.providerWriteSeq[provider];
|
|
const encrypted = await encryptProviderSecrets(connection);
|
|
// Only persist if no newer save has started during the async gap
|
|
if (
|
|
seq === this.providerWriteSeq[provider] &&
|
|
(authAttemptId == null || this.isActiveAuthAttempt(provider, authAttemptId))
|
|
) {
|
|
this.saveToStorage(key, encrypted);
|
|
// Keep dynamic plugin providers in the restart registry while connected
|
|
// (or while credentials/config remain so a missing plugin cannot drop them).
|
|
if (isPluginCloudProviderId(provider)) {
|
|
// Config may be a valid scalar including JSON null — presence is property existence.
|
|
const hasData = encrypted.tokens != null
|
|
|| Object.prototype.hasOwnProperty.call(encrypted, 'config');
|
|
if (hasData || encrypted.status === 'connected' || encrypted.status === 'syncing') {
|
|
registerPluginProviderIdImpl.call(this, provider);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export function loadFromStorageImpl<T>(this: any,key: string): T | null {
|
|
return localStorageAdapter.read<T>(key);
|
|
}
|
|
|
|
export function saveToStorageImpl(this: any,key: string, value: unknown): boolean {
|
|
return localStorageAdapter.write(key, value);
|
|
}
|
|
|
|
export function removeFromStorageImpl(this: any,key: string): void {
|
|
localStorageAdapter.remove(key);
|
|
}
|
|
|
|
export function setupCrossWindowSyncImpl(this: any): void {
|
|
if (this.hasStorageListener) return;
|
|
if (typeof window === 'undefined') return;
|
|
|
|
window.addEventListener('storage', this.handleStorageEvent);
|
|
this.hasStorageListener = true;
|
|
}
|
|
|
|
export function safeJsonParseImpl<T>(this: any,value: string | null): T | null {
|
|
if (!value) return null;
|
|
try {
|
|
return JSON.parse(value) as T;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function handleStorageEventImpl(this: any, event: StorageEvent): void {
|
|
if (event.storageArea !== window.localStorage) return;
|
|
const key = event.key;
|
|
if (!key) return;
|
|
|
|
// Handle master key config changes (e.g., when set up in settings window)
|
|
if (key === SYNC_STORAGE_KEYS.MASTER_KEY_CONFIG) {
|
|
const nextConfig = this.safeJsonParse<MasterKeyConfig>(event.newValue);
|
|
|
|
if (nextConfig) {
|
|
const currentConfig = this.state.masterKeyConfig as MasterKeyConfig | null;
|
|
const configChanged = !currentConfig
|
|
|| currentConfig.verificationHash !== nextConfig.verificationHash
|
|
|| currentConfig.salt !== nextConfig.salt
|
|
|| currentConfig.kdf !== nextConfig.kdf
|
|
|| currentConfig.kdfIterations !== nextConfig.kdfIterations;
|
|
|
|
if (!configChanged) return;
|
|
|
|
// Master key was set up or changed in another window. Lock this
|
|
// window so it cannot keep syncing with the stale in-memory password.
|
|
this.bumpSyncSecurityGeneration?.();
|
|
this.state.masterKeyConfig = nextConfig;
|
|
this.state.securityState = 'LOCKED';
|
|
this.state.unlockedKey = null;
|
|
this.masterPassword = null;
|
|
this.stopAutoSync();
|
|
this.notifyStateChange();
|
|
} else if (this.state.masterKeyConfig) {
|
|
// Master key was removed in another window
|
|
this.bumpSyncSecurityGeneration?.();
|
|
this.state.masterKeyConfig = null;
|
|
this.state.securityState = 'NO_KEY';
|
|
this.state.unlockedKey = null;
|
|
this.masterPassword = null;
|
|
this.notifyStateChange();
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Sync versions + auto-sync settings
|
|
if (key === SYNC_STORAGE_KEYS.SYNC_CONFIG) {
|
|
const next = this.safeJsonParse<{
|
|
autoSync?: boolean;
|
|
interval?: number;
|
|
localVersion?: number;
|
|
localUpdatedAt?: number;
|
|
remoteVersion?: number;
|
|
remoteUpdatedAt?: number;
|
|
syncStrategy?: unknown;
|
|
}>(event.newValue) || {
|
|
autoSync: false,
|
|
interval: SYNC_CONSTANTS.DEFAULT_AUTO_SYNC_INTERVAL,
|
|
localVersion: 0,
|
|
localUpdatedAt: 0,
|
|
remoteVersion: 0,
|
|
remoteUpdatedAt: 0,
|
|
syncStrategy: DEFAULT_CLOUD_SYNC_STRATEGY,
|
|
};
|
|
|
|
this.state.autoSyncEnabled = Boolean(next.autoSync);
|
|
this.state.autoSyncInterval = Math.max(
|
|
SYNC_CONSTANTS.MIN_SYNC_INTERVAL,
|
|
Math.min(
|
|
SYNC_CONSTANTS.MAX_SYNC_INTERVAL,
|
|
Number(next.interval ?? SYNC_CONSTANTS.DEFAULT_AUTO_SYNC_INTERVAL)
|
|
)
|
|
);
|
|
this.state.localVersion = Number(next.localVersion ?? 0);
|
|
this.state.localUpdatedAt = Number(next.localUpdatedAt ?? 0);
|
|
this.state.remoteVersion = Number(next.remoteVersion ?? 0);
|
|
this.state.remoteUpdatedAt = Number(next.remoteUpdatedAt ?? 0);
|
|
this.state.syncStrategy = normalizeCloudSyncStrategy(next.syncStrategy);
|
|
|
|
this.notifyStateChange();
|
|
return;
|
|
}
|
|
|
|
// Sync history list
|
|
if (key === SYNC_HISTORY_STORAGE_KEY) {
|
|
const nextHistory = this.safeJsonParse<SyncHistoryEntry[]>(event.newValue) || [];
|
|
this.state.syncHistory = Array.isArray(nextHistory) ? nextHistory : [];
|
|
this.notifyStateChange();
|
|
return;
|
|
}
|
|
|
|
// Plugin provider registry changes (connect/disconnect of dynamic IDs).
|
|
if (key === SYNC_STORAGE_KEYS.PLUGIN_CLOUD_PROVIDERS) {
|
|
const registered = listRegisteredPluginProviderIdsImpl.call(this);
|
|
const nextProviders = { ...this.state.providers } as Record<CloudProvider, ProviderConnection>;
|
|
for (const id of registered) {
|
|
if (!nextProviders[id]) {
|
|
nextProviders[id] = this.loadProviderConnection(id);
|
|
if (this.providerWriteSeq[id] == null) this.providerWriteSeq[id] = 0;
|
|
if (this.providerDecryptSeq[id] == null) this.providerDecryptSeq[id] = 0;
|
|
if (this.providerDecrypted[id] == null) this.providerDecrypted[id] = false;
|
|
if (this.providerAuthAttemptSeq[id] == null) this.providerAuthAttemptSeq[id] = 0;
|
|
}
|
|
}
|
|
for (const id of Object.keys(nextProviders)) {
|
|
if (isBuiltinCloudProvider(id)) continue;
|
|
if (!registered.includes(id)) {
|
|
const adapter = this.adapters.get(id);
|
|
if (adapter) {
|
|
adapter.signOut();
|
|
this.adapters.delete(id);
|
|
}
|
|
delete nextProviders[id];
|
|
}
|
|
}
|
|
this.state.providers = nextProviders;
|
|
this.notifyStateChange();
|
|
return;
|
|
}
|
|
|
|
// Sync provider connections (connect/disconnect, account, tokens, last sync)
|
|
const providerByKey: Partial<Record<string, CloudProvider>> = {
|
|
[SYNC_STORAGE_KEYS.PROVIDER_GITHUB]: 'github',
|
|
[SYNC_STORAGE_KEYS.PROVIDER_GOOGLE]: 'google',
|
|
[SYNC_STORAGE_KEYS.PROVIDER_ONEDRIVE]: 'onedrive',
|
|
[SYNC_STORAGE_KEYS.PROVIDER_WEBDAV]: 'webdav',
|
|
[SYNC_STORAGE_KEYS.PROVIDER_S3]: 's3',
|
|
};
|
|
let provider = providerByKey[key] as CloudProvider | undefined;
|
|
if (!provider && key.startsWith('netcatty_provider_plugin_v1:')) {
|
|
provider = key.slice('netcatty_provider_plugin_v1:'.length) as CloudProvider;
|
|
}
|
|
if (provider) {
|
|
// Dynamic plugin providers may receive their first storage event before
|
|
// the registry handler initializes counters; undefined++ becomes NaN and
|
|
// every subsequent decrypt result is discarded (NaN !== NaN).
|
|
ensureProviderSeqCounters(this, provider);
|
|
const rawNext = this.loadProviderConnection(provider);
|
|
const seq = ++this.providerDecryptSeq[provider];
|
|
// Also bump write seq so any in-flight save from this window for the
|
|
// same provider is discarded — the cross-window data is newer.
|
|
++this.providerWriteSeq[provider];
|
|
|
|
// Decrypt secrets asynchronously, then update state.
|
|
// Use sequence counter to discard stale results when multiple events
|
|
// for the same provider arrive in quick succession.
|
|
decryptProviderSecrets(rawNext).then((next) => {
|
|
if (seq !== this.providerDecryptSeq[provider]) return; // stale — discard
|
|
|
|
const prev = this.state.providers[provider] ?? {
|
|
provider,
|
|
status: 'disconnected' as const,
|
|
};
|
|
const preserveTransientStatus =
|
|
prev.status === 'connecting' || prev.status === 'syncing';
|
|
|
|
this.state.providers[provider] = {
|
|
...next,
|
|
status: preserveTransientStatus ? prev.status : next.status,
|
|
error: preserveTransientStatus ? prev.error : next.error,
|
|
};
|
|
|
|
const nextTokens = next.tokens;
|
|
const nextConfig = next.config;
|
|
const adapter = this.adapters.get(provider);
|
|
// Config may be a valid falsy scalar including JSON null — presence is
|
|
// property existence (matches hasProviderConnectionData).
|
|
const hasConfigProperty = Object.prototype.hasOwnProperty.call(next, 'config');
|
|
if (nextTokens == null && !hasConfigProperty && next.credential == null) {
|
|
if (adapter) {
|
|
adapter.signOut();
|
|
this.adapters.delete(provider);
|
|
}
|
|
this.notifyStateChange();
|
|
return;
|
|
}
|
|
|
|
const tokenChanged =
|
|
(prev.tokens?.accessToken ?? null) !== (nextTokens?.accessToken ?? null) ||
|
|
(prev.tokens?.refreshToken ?? null) !== (nextTokens?.refreshToken ?? null) ||
|
|
(prev.tokens?.expiresAt ?? null) !== (nextTokens?.expiresAt ?? null) ||
|
|
(prev.tokens?.tokenType ?? null) !== (nextTokens?.tokenType ?? null) ||
|
|
(prev.tokens?.scope ?? null) !== (nextTokens?.scope ?? null);
|
|
|
|
// Config may be a valid falsy scalar (false, 0, "") — use nullish, not ||.
|
|
const configChanged =
|
|
JSON.stringify(prev.config ?? null) !== JSON.stringify(nextConfig ?? null);
|
|
|
|
const prevCredential = prev.credential;
|
|
const nextCredential = next.credential;
|
|
const credentialChanged =
|
|
(prevCredential?.kind ?? null) !== (nextCredential?.kind ?? null)
|
|
|| (prevCredential?.id ?? null) !== (nextCredential?.id ?? null)
|
|
|| (prevCredential?.key ?? null) !== (nextCredential?.key ?? null);
|
|
|
|
const resourceChanged = (adapter?.resourceId || null) !== (next.resourceId || null);
|
|
|
|
if (adapter && (tokenChanged || configChanged || credentialChanged || resourceChanged)) {
|
|
adapter.signOut();
|
|
this.adapters.delete(provider);
|
|
}
|
|
// Credential identity changes invalidate merge anchors the same way
|
|
// config/resource changes do on the connect path (authMethods).
|
|
if (credentialChanged) {
|
|
try {
|
|
this.removeFromStorage(this.syncBaseKey(provider));
|
|
this.removeFromStorage(this.convergentProviderBaselineKey(provider));
|
|
this.clearSyncAnchor(provider);
|
|
} catch {
|
|
// Best-effort; adapter eviction above already forces reconnect.
|
|
}
|
|
}
|
|
|
|
this.notifyStateChange();
|
|
}).catch(() => {
|
|
// Decryption failure in cross-window handler is non-fatal
|
|
});
|
|
}
|
|
}
|
|
|
|
export async function getConnectedAdapterImpl(this: any,provider: CloudProvider): Promise<CloudAdapter> {
|
|
// Ensure startup decryption has finished before reading tokens
|
|
await this.decryptionReady;
|
|
ensureProviderSeqCounters(this, provider);
|
|
|
|
// If this provider's secrets were not successfully decrypted at
|
|
// startup (IPC handler not registered yet), retry now.
|
|
if (!this.providerDecrypted[provider]) {
|
|
const conn = this.state.providers[provider];
|
|
// Config may be a valid scalar including JSON null — presence is property existence.
|
|
if (conn.tokens != null || Object.prototype.hasOwnProperty.call(conn, 'config')) {
|
|
try {
|
|
const seq = ++this.providerDecryptSeq[provider];
|
|
const decrypted = await decryptProviderSecrets(conn);
|
|
if (seq === this.providerDecryptSeq[provider]) {
|
|
this.state.providers[provider] = decrypted;
|
|
this.providerDecrypted[provider] = true;
|
|
// Evict any adapter cached with the old (encrypted) tokens
|
|
// so a fresh one is built from the decrypted credentials below.
|
|
const stale = this.adapters.get(provider);
|
|
if (stale) {
|
|
stale.signOut();
|
|
this.adapters.delete(provider);
|
|
}
|
|
this.notifyStateChange();
|
|
}
|
|
} catch {
|
|
// Still failing — will surface when adapter tries to use tokens
|
|
}
|
|
}
|
|
}
|
|
|
|
const connection = this.state.providers[provider];
|
|
const tokens = connection?.tokens;
|
|
const config = connection?.config;
|
|
// Config may be a valid scalar including JSON null — presence is property existence.
|
|
const hasConfigProperty = connection != null
|
|
&& Object.prototype.hasOwnProperty.call(connection, 'config');
|
|
if (tokens == null && !hasConfigProperty && connection?.credential == null) {
|
|
throw new Error('Provider not connected');
|
|
}
|
|
|
|
const existing = this.adapters.get(provider);
|
|
if (existing?.isAuthenticated) {
|
|
attachTokenRefreshPersistence.call(this, provider, existing);
|
|
return existing;
|
|
}
|
|
|
|
const createPluginStorage = async (providerId: string): Promise<EncryptedObjectStorage> => {
|
|
if (typeof this.createPluginStorage === 'function') {
|
|
return this.createPluginStorage(providerId, connection);
|
|
}
|
|
if (!isPluginSyncIpcAvailable()) {
|
|
throw new Error(
|
|
`Plugin sync provider ${providerId} is unavailable (plugin host not enabled)`,
|
|
);
|
|
}
|
|
const host = createPluginSyncIpcHost();
|
|
// Preserve explicit null configuration (schema type: "null"); only
|
|
// default to {} when the config property is truly absent.
|
|
const configuration = connection != null
|
|
&& Object.prototype.hasOwnProperty.call(connection, 'config')
|
|
? connection.config
|
|
: {};
|
|
return createPluginSyncObjectStorage({
|
|
providerId,
|
|
host,
|
|
configuration,
|
|
credential: connection?.credential,
|
|
});
|
|
};
|
|
|
|
const adapter = await createAdapter(
|
|
provider,
|
|
tokens,
|
|
connection.resourceId,
|
|
config,
|
|
isBuiltinCloudProvider(provider) ? undefined : { createPluginStorage },
|
|
);
|
|
attachTokenRefreshPersistence.call(this, provider, adapter);
|
|
this.adapters.set(provider, adapter);
|
|
return adapter;
|
|
}
|
|
|
|
/**
|
|
* Wire an OAuth adapter's token-refresh callback so silently refreshed tokens
|
|
* are persisted. Without this, an adapter that refreshes its access token only
|
|
* updates memory and the next launch loads a stale token and is forced to
|
|
* reconnect — OneDrive's rotating refresh tokens go stale after the first
|
|
* in-session refresh (#1189), and Google's refreshed access token is likewise
|
|
* lost on restart. OneDrive and Google expose setOnTokensRefreshed; adapters
|
|
* without it (GitHub, WebDAV, S3) are no-ops.
|
|
*/
|
|
export function attachTokenRefreshPersistence(
|
|
this: any,
|
|
provider: CloudProvider,
|
|
adapter: CloudAdapter,
|
|
): void {
|
|
const setCallback = (adapter as {
|
|
setOnTokensRefreshed?: (cb: (tokens: import('../../../domain/sync').OAuthTokens) => void) => void;
|
|
}).setOnTokensRefreshed;
|
|
if (typeof setCallback !== 'function') return;
|
|
setCallback.call(adapter, (tokens) => {
|
|
persistRefreshedProviderTokensImpl.call(this, provider, tokens);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Persist tokens that an adapter refreshed mid-session into provider state and
|
|
* encrypted storage. Bumps the decrypt sequence so a concurrent stale decrypt
|
|
* (startup / cross-window) can't clobber the rotated tokens; preserves the live
|
|
* status/account/resource fields. saveProviderConnection manages its own write
|
|
* sequence to serialize the encrypted persist.
|
|
*/
|
|
export function persistRefreshedProviderTokensImpl(
|
|
this: any,
|
|
provider: CloudProvider,
|
|
tokens: import('../../../domain/sync').OAuthTokens,
|
|
): void {
|
|
const existing = this.state.providers[provider];
|
|
// Provider may have been disconnected during the async refresh — don't
|
|
// resurrect a connection that no longer has credentials.
|
|
if (!existing?.tokens) return;
|
|
|
|
// Invalidate any in-flight decrypt (startup / cross-window) so it cannot
|
|
// overwrite the rotated tokens we are about to commit.
|
|
ensureProviderSeqCounters(this, provider);
|
|
++this.providerDecryptSeq[provider];
|
|
this.state.providers[provider] = {
|
|
...existing,
|
|
tokens,
|
|
};
|
|
void this.saveProviderConnection(provider, this.state.providers[provider]);
|
|
this.notifyStateChange();
|
|
}
|
|
|
|
/**
|
|
* Handle a sync error that means OneDrive's refresh token is dead. Clears the
|
|
* now-useless tokens and tears down the adapter so the provider drops to a real
|
|
* "reconnect" (disconnected) state instead of staying `error`-with-tokens —
|
|
* which `isProviderReadyForSync` keeps treating as ready, so auto-sync would
|
|
* otherwise retry the dead token forever and never surface a reconnect prompt.
|
|
*
|
|
* Returns true when it handled a reauth-required OneDrive error so the caller
|
|
* can preserve a clean status message. No-op (returns false) for any other
|
|
* provider or error.
|
|
*/
|
|
export function handleProviderReauthRequiredImpl(
|
|
this: any,
|
|
provider: CloudProvider,
|
|
error: unknown,
|
|
): boolean {
|
|
if (provider !== 'onedrive') return false;
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
if (!isOneDriveReauthRequiredMessage(message)) return false;
|
|
|
|
// Idempotent: this error can surface on multiple paths in one sync (preflight
|
|
// inspection + the operation's own catch). Once the credentials are already
|
|
// cleared there is nothing more to do, but still report handled so callers
|
|
// skip the generic error status that would re-add the raw marker message.
|
|
const current = this.state.providers[provider];
|
|
if (!current?.tokens && !current?.config) return true;
|
|
|
|
const adapter = this.adapters.get(provider);
|
|
if (adapter) {
|
|
adapter.signOut();
|
|
this.adapters.delete(provider);
|
|
}
|
|
|
|
// Bump decrypt seq so a stale in-flight decrypt cannot resurrect the tokens.
|
|
++this.providerDecryptSeq[provider];
|
|
this.state.providers[provider] = {
|
|
provider,
|
|
status: 'error',
|
|
account: current?.account,
|
|
error: cleanOneDriveErrorMessage(message),
|
|
};
|
|
void this.saveProviderConnection(provider, this.state.providers[provider]);
|
|
this.notifyStateChange();
|
|
return true;
|
|
}
|
|
|
|
export async function setupMasterKeyImpl(this: any,password: string): Promise<void> {
|
|
if (this.state.masterKeyConfig) {
|
|
throw new Error('Master key already exists. Use changeMasterKey instead.');
|
|
}
|
|
|
|
const config = await EncryptionService.createMasterKeyConfig(password);
|
|
|
|
this.bumpSyncSecurityGeneration?.();
|
|
this.state.masterKeyConfig = config;
|
|
this.state.securityState = 'LOCKED';
|
|
|
|
this.saveToStorage(SYNC_STORAGE_KEYS.MASTER_KEY_CONFIG, config);
|
|
this.emit({ type: 'SECURITY_STATE_CHANGED', state: 'LOCKED' });
|
|
|
|
// Auto-unlock after setup
|
|
await this.unlock(password);
|
|
}
|
|
|
|
export async function unlockImpl(this: any,password: string): Promise<boolean> {
|
|
if (!this.state.masterKeyConfig) {
|
|
throw new Error('No master key configured');
|
|
}
|
|
|
|
if (this.state.securityState === 'UNLOCKED') {
|
|
return true;
|
|
}
|
|
|
|
const unlockedKey = await EncryptionService.unlockMasterKey(
|
|
password,
|
|
this.state.masterKeyConfig
|
|
);
|
|
|
|
if (!unlockedKey) {
|
|
return false;
|
|
}
|
|
|
|
this.state.unlockedKey = unlockedKey;
|
|
this.state.securityState = 'UNLOCKED';
|
|
this.masterPassword = password;
|
|
|
|
this.emit({ type: 'SECURITY_STATE_CHANGED', state: 'UNLOCKED' });
|
|
|
|
// Start auto-sync if enabled
|
|
if (this.state.autoSyncEnabled) {
|
|
this.startAutoSync();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
export function lockImpl(this: any): void {
|
|
if (this.state.securityState !== 'UNLOCKED') {
|
|
return;
|
|
}
|
|
|
|
// Clear sensitive data from memory
|
|
this.bumpSyncSecurityGeneration?.();
|
|
this.state.unlockedKey = null;
|
|
this.masterPassword = null;
|
|
this.state.securityState = 'LOCKED';
|
|
|
|
// Stop auto-sync
|
|
this.stopAutoSync();
|
|
|
|
this.emit({ type: 'SECURITY_STATE_CHANGED', state: 'LOCKED' });
|
|
}
|
|
|
|
export async function changeMasterKeyImpl(this: any,oldPassword: string, newPassword: string): Promise<boolean> {
|
|
if (!this.state.masterKeyConfig) {
|
|
throw new Error('No master key configured');
|
|
}
|
|
|
|
const newConfig = await EncryptionService.changeMasterPassword(
|
|
oldPassword,
|
|
newPassword,
|
|
this.state.masterKeyConfig
|
|
);
|
|
|
|
if (!newConfig) {
|
|
return false;
|
|
}
|
|
|
|
const oldUnlockedKey = await EncryptionService.unlockMasterKey(
|
|
oldPassword,
|
|
this.state.masterKeyConfig,
|
|
);
|
|
const newUnlockedKey = await EncryptionService.unlockMasterKey(
|
|
newPassword,
|
|
newConfig,
|
|
);
|
|
if (!oldUnlockedKey || !newUnlockedKey) {
|
|
throw new Error('Failed to derive keys for master key rotation');
|
|
}
|
|
|
|
// Local provider baselines, snapshots and the canonical CRDT replica use
|
|
// the master-derived key. Re-encrypt them before publishing the new master
|
|
// config so a failed rotation cannot strand records under mismatched keys.
|
|
await this.reencryptSyncStorage(
|
|
oldUnlockedKey.derivedKey,
|
|
newUnlockedKey.derivedKey,
|
|
newConfig,
|
|
);
|
|
|
|
this.bumpSyncSecurityGeneration?.();
|
|
this.state.masterKeyConfig = newConfig;
|
|
this.state.securityState = 'UNLOCKED';
|
|
this.masterPassword = newPassword;
|
|
|
|
this.state.unlockedKey = newUnlockedKey;
|
|
|
|
// Notify UI and restart auto-sync (actual re-upload requires a payload from app state)
|
|
this.emit({ type: 'SECURITY_STATE_CHANGED', state: 'UNLOCKED' });
|
|
if (this.state.autoSyncEnabled) {
|
|
this.startAutoSync();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
export async function verifyPasswordImpl(this: any,password: string): Promise<boolean> {
|
|
if (!this.state.masterKeyConfig) {
|
|
return false;
|
|
}
|
|
return EncryptionService.verifyPassword(password, this.state.masterKeyConfig);
|
|
}
|