@narumitw/pi-sync-daemon 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +89 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +128 -0
- package/dist/client.d.ts +55 -0
- package/dist/client.js +484 -0
- package/dist/consent.d.ts +24 -0
- package/dist/consent.js +205 -0
- package/dist/document.d.ts +10 -0
- package/dist/document.js +6 -0
- package/dist/endpoint.d.ts +8 -0
- package/dist/endpoint.js +91 -0
- package/dist/engine/backends/backend-factory.d.ts +6 -0
- package/dist/engine/backends/backend-factory.js +16 -0
- package/dist/engine/backends/backend-identity.d.ts +3 -0
- package/dist/engine/backends/backend-identity.js +38 -0
- package/dist/engine/backends/backend-types.d.ts +70 -0
- package/dist/engine/backends/backend-types.js +1 -0
- package/dist/engine/backends/git/git-backend.d.ts +55 -0
- package/dist/engine/backends/git/git-backend.js +700 -0
- package/dist/engine/backends/git/git-config.d.ts +5 -0
- package/dist/engine/backends/git/git-config.js +118 -0
- package/dist/engine/backends/git/git-runner.d.ts +25 -0
- package/dist/engine/backends/git/git-runner.js +232 -0
- package/dist/engine/backends/git/git-storage.d.ts +39 -0
- package/dist/engine/backends/git/git-storage.js +226 -0
- package/dist/engine/backends/s3/s3-backend.d.ts +32 -0
- package/dist/engine/backends/s3/s3-backend.js +436 -0
- package/dist/engine/backends/s3/s3-client.d.ts +36 -0
- package/dist/engine/backends/s3/s3-client.js +352 -0
- package/dist/engine/backends/s3/s3-conditions.d.ts +4 -0
- package/dist/engine/backends/s3/s3-conditions.js +66 -0
- package/dist/engine/backends/snapshot-pointer.d.ts +6 -0
- package/dist/engine/backends/snapshot-pointer.js +22 -0
- package/dist/engine/backends/sync-backend.d.ts +71 -0
- package/dist/engine/backends/sync-backend.js +23 -0
- package/dist/engine/backends/webdav/webdav-backend.d.ts +34 -0
- package/dist/engine/backends/webdav/webdav-backend.js +534 -0
- package/dist/engine/backends/webdav/webdav-client.d.ts +36 -0
- package/dist/engine/backends/webdav/webdav-client.js +326 -0
- package/dist/engine/backends/webdav/webdav-config.d.ts +5 -0
- package/dist/engine/backends/webdav/webdav-config.js +73 -0
- package/dist/engine/commands/command-types.d.ts +31 -0
- package/dist/engine/commands/command-types.js +1 -0
- package/dist/engine/host.d.ts +70 -0
- package/dist/engine/host.js +16 -0
- package/dist/engine/paths.d.ts +9 -0
- package/dist/engine/paths.js +71 -0
- package/dist/engine/settings/config-errors.d.ts +1 -0
- package/dist/engine/settings/config-errors.js +4 -0
- package/dist/engine/settings/config-file.d.ts +33 -0
- package/dist/engine/settings/config-file.js +630 -0
- package/dist/engine/settings/config.d.ts +24 -0
- package/dist/engine/settings/config.js +200 -0
- package/dist/engine/settings/settings-management.d.ts +24 -0
- package/dist/engine/settings/settings-management.js +191 -0
- package/dist/engine/settings/settings-store.d.ts +8 -0
- package/dist/engine/settings/settings-store.js +41 -0
- package/dist/engine/settings/settings-types.d.ts +134 -0
- package/dist/engine/settings/settings-types.js +1 -0
- package/dist/engine/settings/settings-validation.d.ts +15 -0
- package/dist/engine/settings/settings-validation.js +356 -0
- package/dist/engine/snapshot/session-paths.d.ts +12 -0
- package/dist/engine/snapshot/session-paths.js +91 -0
- package/dist/engine/snapshot/snapshot-apply.d.ts +22 -0
- package/dist/engine/snapshot/snapshot-apply.js +262 -0
- package/dist/engine/snapshot/snapshot-case-replacement.d.ts +15 -0
- package/dist/engine/snapshot/snapshot-case-replacement.js +82 -0
- package/dist/engine/snapshot/snapshot-codec.d.ts +6 -0
- package/dist/engine/snapshot/snapshot-codec.js +53 -0
- package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +7 -0
- package/dist/engine/snapshot/snapshot-exclude-safety.js +47 -0
- package/dist/engine/snapshot/snapshot-mutation-queues.d.ts +9 -0
- package/dist/engine/snapshot/snapshot-mutation-queues.js +133 -0
- package/dist/engine/snapshot/snapshot-paths.d.ts +1 -0
- package/dist/engine/snapshot/snapshot-paths.js +11 -0
- package/dist/engine/snapshot/snapshot-root-transition.d.ts +25 -0
- package/dist/engine/snapshot/snapshot-root-transition.js +122 -0
- package/dist/engine/snapshot/snapshot-transaction-directory.d.ts +2 -0
- package/dist/engine/snapshot/snapshot-transaction-directory.js +16 -0
- package/dist/engine/snapshot/snapshot-transaction-plan.d.ts +12 -0
- package/dist/engine/snapshot/snapshot-transaction-plan.js +68 -0
- package/dist/engine/snapshot/snapshot-transaction.d.ts +15 -0
- package/dist/engine/snapshot/snapshot-transaction.js +787 -0
- package/dist/engine/snapshot/snapshot-types.d.ts +48 -0
- package/dist/engine/snapshot/snapshot-types.js +1 -0
- package/dist/engine/snapshot/snapshot.d.ts +30 -0
- package/dist/engine/snapshot/snapshot.js +364 -0
- package/dist/engine/state/json-file.d.ts +6 -0
- package/dist/engine/state/json-file.js +49 -0
- package/dist/engine/state/lock-policy.d.ts +2 -0
- package/dist/engine/state/lock-policy.js +2 -0
- package/dist/engine/state/lock.d.ts +20 -0
- package/dist/engine/state/lock.js +259 -0
- package/dist/engine/state/lockfile-fs.d.ts +13 -0
- package/dist/engine/state/lockfile-fs.js +13 -0
- package/dist/engine/state/merge-baseline-store.d.ts +11 -0
- package/dist/engine/state/merge-baseline-store.js +187 -0
- package/dist/engine/state/mutation-directory-sync.d.ts +5 -0
- package/dist/engine/state/mutation-directory-sync.js +34 -0
- package/dist/engine/state/operation-availability.d.ts +49 -0
- package/dist/engine/state/operation-availability.js +40 -0
- package/dist/engine/state/state-directory.d.ts +14 -0
- package/dist/engine/state/state-directory.js +225 -0
- package/dist/engine/state/state-types.d.ts +27 -0
- package/dist/engine/state/state-types.js +1 -0
- package/dist/engine/state/sync-state-store.d.ts +11 -0
- package/dist/engine/state/sync-state-store.js +54 -0
- package/dist/engine/sync/agent-candidate.d.ts +44 -0
- package/dist/engine/sync/agent-candidate.js +207 -0
- package/dist/engine/sync/agent-plan.d.ts +8 -0
- package/dist/engine/sync/agent-plan.js +84 -0
- package/dist/engine/sync/agent-policy.d.ts +9 -0
- package/dist/engine/sync/agent-policy.js +10 -0
- package/dist/engine/sync/agent-refusal-retry.d.ts +4 -0
- package/dist/engine/sync/agent-refusal-retry.js +39 -0
- package/dist/engine/sync/agent-request.d.ts +14 -0
- package/dist/engine/sync/agent-request.js +13 -0
- package/dist/engine/sync/agent-runner.d.ts +6 -0
- package/dist/engine/sync/agent-runner.js +183 -0
- package/dist/engine/sync/agent-store.d.ts +7 -0
- package/dist/engine/sync/agent-store.js +177 -0
- package/dist/engine/sync/agent-sync.d.ts +4 -0
- package/dist/engine/sync/agent-sync.js +132 -0
- package/dist/engine/sync/apply-review.d.ts +8 -0
- package/dist/engine/sync/apply-review.js +51 -0
- package/dist/engine/sync/conflict-artifact-ownership.d.ts +4 -0
- package/dist/engine/sync/conflict-artifact-ownership.js +53 -0
- package/dist/engine/sync/conflict-artifacts.d.ts +32 -0
- package/dist/engine/sync/conflict-artifacts.js +158 -0
- package/dist/engine/sync/conflict-groups.d.ts +9 -0
- package/dist/engine/sync/conflict-groups.js +76 -0
- package/dist/engine/sync/conflict-preview.d.ts +4 -0
- package/dist/engine/sync/conflict-preview.js +53 -0
- package/dist/engine/sync/conflict-resolution.d.ts +13 -0
- package/dist/engine/sync/conflict-resolution.js +53 -0
- package/dist/engine/sync/conflict-retention.d.ts +5 -0
- package/dist/engine/sync/conflict-retention.js +61 -0
- package/dist/engine/sync/conflict-review.d.ts +4 -0
- package/dist/engine/sync/conflict-review.js +73 -0
- package/dist/engine/sync/content-conflicts.d.ts +10 -0
- package/dist/engine/sync/content-conflicts.js +70 -0
- package/dist/engine/sync/continuous-errors.d.ts +14 -0
- package/dist/engine/sync/continuous-errors.js +66 -0
- package/dist/engine/sync/continuous-lease.d.ts +9 -0
- package/dist/engine/sync/continuous-lease.js +206 -0
- package/dist/engine/sync/continuous-record.d.ts +31 -0
- package/dist/engine/sync/continuous-record.js +199 -0
- package/dist/engine/sync/continuous-storage.d.ts +14 -0
- package/dist/engine/sync/continuous-storage.js +59 -0
- package/dist/engine/sync/continuous-sync.d.ts +15 -0
- package/dist/engine/sync/continuous-sync.js +496 -0
- package/dist/engine/sync/continuous-watch.d.ts +3 -0
- package/dist/engine/sync/continuous-watch.js +65 -0
- package/dist/engine/sync/exclude-policy.d.ts +5 -0
- package/dist/engine/sync/exclude-policy.js +79 -0
- package/dist/engine/sync/field-migration.d.ts +5 -0
- package/dist/engine/sync/field-migration.js +20 -0
- package/dist/engine/sync/file-merge-planner.d.ts +36 -0
- package/dist/engine/sync/file-merge-planner.js +123 -0
- package/dist/engine/sync/initialization.d.ts +12 -0
- package/dist/engine/sync/initialization.js +75 -0
- package/dist/engine/sync/json-document.d.ts +29 -0
- package/dist/engine/sync/json-document.js +172 -0
- package/dist/engine/sync/local-field-policy.d.ts +2 -0
- package/dist/engine/sync/local-field-policy.js +26 -0
- package/dist/engine/sync/local-fields.d.ts +9 -0
- package/dist/engine/sync/local-fields.js +116 -0
- package/dist/engine/sync/merge-apply.d.ts +5 -0
- package/dist/engine/sync/merge-apply.js +386 -0
- package/dist/engine/sync/merge-journal.d.ts +31 -0
- package/dist/engine/sync/merge-journal.js +179 -0
- package/dist/engine/sync/merge-transfer-error.d.ts +3 -0
- package/dist/engine/sync/merge-transfer-error.js +64 -0
- package/dist/engine/sync/merged-sync.d.ts +6 -0
- package/dist/engine/sync/merged-sync.js +618 -0
- package/dist/engine/sync/mutation-owner.d.ts +2 -0
- package/dist/engine/sync/mutation-owner.js +18 -0
- package/dist/engine/sync/package-merge.d.ts +3 -0
- package/dist/engine/sync/package-merge.js +82 -0
- package/dist/engine/sync/partial-progress.d.ts +15 -0
- package/dist/engine/sync/partial-progress.js +39 -0
- package/dist/engine/sync/remote-snapshot.d.ts +28 -0
- package/dist/engine/sync/remote-snapshot.js +62 -0
- package/dist/engine/sync/resolver-stream.d.ts +4 -0
- package/dist/engine/sync/resolver-stream.js +34 -0
- package/dist/engine/sync/session-merge.d.ts +3 -0
- package/dist/engine/sync/session-merge.js +178 -0
- package/dist/engine/sync/session-push.d.ts +5 -0
- package/dist/engine/sync/session-push.js +32 -0
- package/dist/engine/sync/settings-conflicts.d.ts +8 -0
- package/dist/engine/sync/settings-conflicts.js +56 -0
- package/dist/engine/sync/settings-merge.d.ts +13 -0
- package/dist/engine/sync/settings-merge.js +93 -0
- package/dist/engine/sync/signals.d.ts +2 -0
- package/dist/engine/sync/signals.js +8 -0
- package/dist/engine/sync/startup-timings.d.ts +11 -0
- package/dist/engine/sync/startup-timings.js +44 -0
- package/dist/engine/sync/sync-decision.d.ts +35 -0
- package/dist/engine/sync/sync-decision.js +66 -0
- package/dist/engine/sync/sync-error-guidance.d.ts +2 -0
- package/dist/engine/sync/sync-error-guidance.js +55 -0
- package/dist/engine/sync/sync-errors.d.ts +9 -0
- package/dist/engine/sync/sync-errors.js +18 -0
- package/dist/engine/sync/sync-inspection.d.ts +26 -0
- package/dist/engine/sync/sync-inspection.js +40 -0
- package/dist/engine/sync/sync-loaders.d.ts +7 -0
- package/dist/engine/sync/sync-loaders.js +8 -0
- package/dist/engine/sync/sync-local.d.ts +5 -0
- package/dist/engine/sync/sync-local.js +40 -0
- package/dist/engine/sync/sync-mutations.d.ts +30 -0
- package/dist/engine/sync/sync-mutations.js +589 -0
- package/dist/engine/sync/sync-operations.d.ts +2 -0
- package/dist/engine/sync/sync-operations.js +3 -0
- package/dist/engine/sync/sync-policy.d.ts +85 -0
- package/dist/engine/sync/sync-policy.js +356 -0
- package/dist/engine/sync/sync-queries.d.ts +7 -0
- package/dist/engine/sync/sync-queries.js +195 -0
- package/dist/engine/sync/sync-state.d.ts +31 -0
- package/dist/engine/sync/sync-state.js +92 -0
- package/dist/engine/sync/text-merge.d.ts +19 -0
- package/dist/engine/sync/text-merge.js +124 -0
- package/dist/engine/types.d.ts +5 -0
- package/dist/engine/types.js +2 -0
- package/dist/engine/ui/merge-review.d.ts +2 -0
- package/dist/engine/ui/merge-review.js +7 -0
- package/dist/engine/ui/merged-sync-summary.d.ts +25 -0
- package/dist/engine/ui/merged-sync-summary.js +86 -0
- package/dist/engine/ui/sync-format.d.ts +13 -0
- package/dist/engine/ui/sync-format.js +150 -0
- package/dist/engine/ui/sync-status.d.ts +9 -0
- package/dist/engine/ui/sync-status.js +30 -0
- package/dist/engine/ui/terminal-text.d.ts +3 -0
- package/dist/engine/ui/terminal-text.js +9 -0
- package/dist/engine/ui/transfer-changes.d.ts +12 -0
- package/dist/engine/ui/transfer-changes.js +24 -0
- package/dist/engine/ui/transfer-review.d.ts +3 -0
- package/dist/engine/ui/transfer-review.js +19 -0
- package/dist/engine-service.d.ts +11 -0
- package/dist/engine-service.js +315 -0
- package/dist/identity.d.ts +20 -0
- package/dist/identity.js +133 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +4 -0
- package/dist/lifecycle-log.d.ts +7 -0
- package/dist/lifecycle-log.js +53 -0
- package/dist/lifecycle.d.ts +2 -0
- package/dist/lifecycle.js +21 -0
- package/dist/model-signature.d.ts +2 -0
- package/dist/model-signature.js +7 -0
- package/dist/monitor.d.ts +11 -0
- package/dist/monitor.js +245 -0
- package/dist/participant-admission.d.ts +5 -0
- package/dist/participant-admission.js +30 -0
- package/dist/protocol.d.ts +97 -0
- package/dist/protocol.js +90 -0
- package/dist/resolver-host.d.ts +11 -0
- package/dist/resolver-host.js +78 -0
- package/dist/server.d.ts +18 -0
- package/dist/server.js +724 -0
- package/dist/service-host.d.ts +7 -0
- package/dist/service-host.js +174 -0
- package/docs/architecture.md +133 -0
- package/package.json +54 -4
|
@@ -0,0 +1,630 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdir, mkdirSync, realpath, realpathSync, rmdir, rmdirSync, stat, statSync, utimes, utimesSync, } from "node:fs";
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import lockfile from "proper-lockfile";
|
|
6
|
+
import { engineHost, getEngineRoot as getAgentDir } from "../host.js";
|
|
7
|
+
export const CONFIG_FILE_NAME = "pi-sync.json";
|
|
8
|
+
const LEGACY_CONFIG_FILE_NAME = "pi-sync.local.json";
|
|
9
|
+
const configMigrationNotices = new Map();
|
|
10
|
+
const legacyPresenceNoticed = new Set();
|
|
11
|
+
const CONFIG_LOCK_STALE_MS = 30_000;
|
|
12
|
+
const CONFIG_LOCK_UPDATE_MS = 10_000;
|
|
13
|
+
const LOCKFILE_FS_ADAPTER = {
|
|
14
|
+
mkdir,
|
|
15
|
+
mkdirSync,
|
|
16
|
+
realpath,
|
|
17
|
+
realpathSync,
|
|
18
|
+
rmdir,
|
|
19
|
+
rmdirSync,
|
|
20
|
+
stat,
|
|
21
|
+
statSync,
|
|
22
|
+
utimes,
|
|
23
|
+
utimesSync,
|
|
24
|
+
};
|
|
25
|
+
let configUpdateQueue = Promise.resolve();
|
|
26
|
+
let publishConfigFile = publishFileWithGuard;
|
|
27
|
+
let beforeConfigPublicationHook = async () => undefined;
|
|
28
|
+
let afterMissingConfigReadProbeHook = async () => undefined;
|
|
29
|
+
let afterReplacementInstalledHook = async () => undefined;
|
|
30
|
+
let afterConfigQuarantinedHook = async () => undefined;
|
|
31
|
+
export async function withConfigFilePublicationForTest(publish, run) {
|
|
32
|
+
const previous = publishConfigFile;
|
|
33
|
+
publishConfigFile = publish;
|
|
34
|
+
try {
|
|
35
|
+
return await run();
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
publishConfigFile = previous;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export async function withConfigPublicationReadyHookForTest(hook, run) {
|
|
42
|
+
const previous = beforeConfigPublicationHook;
|
|
43
|
+
beforeConfigPublicationHook = hook;
|
|
44
|
+
try {
|
|
45
|
+
return await run();
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
beforeConfigPublicationHook = previous;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export async function withMissingConfigReadProbeHookForTest(hook, run) {
|
|
52
|
+
const previous = afterMissingConfigReadProbeHook;
|
|
53
|
+
afterMissingConfigReadProbeHook = hook;
|
|
54
|
+
try {
|
|
55
|
+
return await run();
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
afterMissingConfigReadProbeHook = previous;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export async function withConfigReplacementInstalledHookForTest(hook, run) {
|
|
62
|
+
const previous = afterReplacementInstalledHook;
|
|
63
|
+
afterReplacementInstalledHook = hook;
|
|
64
|
+
try {
|
|
65
|
+
return await run();
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
afterReplacementInstalledHook = previous;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
export async function withConfigQuarantinedHookForTest(hook, run) {
|
|
72
|
+
const previous = afterConfigQuarantinedHook;
|
|
73
|
+
afterConfigQuarantinedHook = hook;
|
|
74
|
+
try {
|
|
75
|
+
return await run();
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
afterConfigQuarantinedHook = previous;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
export function localConfigPath() {
|
|
82
|
+
return path.join(getAgentDir(), CONFIG_FILE_NAME);
|
|
83
|
+
}
|
|
84
|
+
export function legacyLocalConfigPath() {
|
|
85
|
+
return path.join(getAgentDir(), LEGACY_CONFIG_FILE_NAME);
|
|
86
|
+
}
|
|
87
|
+
export async function activeLocalConfigPath() {
|
|
88
|
+
const authority = engineHost().config;
|
|
89
|
+
if (authority)
|
|
90
|
+
return (await authority.read(true))?.path ?? localConfigPath();
|
|
91
|
+
const canonicalPath = localConfigPath();
|
|
92
|
+
const legacyPath = legacyLocalConfigPath();
|
|
93
|
+
return queueConfigOperation(() => withLocalConfigReadLockIfNeeded(async () => {
|
|
94
|
+
if (await pathExists(canonicalPath))
|
|
95
|
+
return canonicalPath;
|
|
96
|
+
return (await pathExists(legacyPath)) ? legacyPath : canonicalPath;
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
export function consumeLocalConfigMigrationNotice() {
|
|
100
|
+
const configPath = localConfigPath();
|
|
101
|
+
const notice = configMigrationNotices.get(configPath);
|
|
102
|
+
configMigrationNotices.delete(configPath);
|
|
103
|
+
return notice;
|
|
104
|
+
}
|
|
105
|
+
export async function readActiveLocalConfigDocumentForRepair() {
|
|
106
|
+
const authority = engineHost().config;
|
|
107
|
+
if (authority)
|
|
108
|
+
return authority.read(true);
|
|
109
|
+
return peekLocalConfigDocument();
|
|
110
|
+
}
|
|
111
|
+
/** Read-only bootstrap: no authority recursion, migration, or document creation. */
|
|
112
|
+
export async function peekLocalConfigDocument() {
|
|
113
|
+
const canonicalPath = localConfigPath();
|
|
114
|
+
const legacyPath = legacyLocalConfigPath();
|
|
115
|
+
return queueConfigOperation(() => withLocalConfigReadLockIfNeeded(async () => {
|
|
116
|
+
const filePath = (await pathExists(canonicalPath)) ? canonicalPath : legacyPath;
|
|
117
|
+
const snapshot = await readConfigSnapshotIfExists(filePath);
|
|
118
|
+
return snapshot ? { path: filePath, ...snapshot } : undefined;
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
export async function readMigratingLocalConfigDocument(validateForMigration) {
|
|
122
|
+
const authority = engineHost().config;
|
|
123
|
+
if (authority) {
|
|
124
|
+
const document = await authority.read();
|
|
125
|
+
if (document)
|
|
126
|
+
validateForMigration(document.parsed);
|
|
127
|
+
return document;
|
|
128
|
+
}
|
|
129
|
+
if (engineHost().allowConfigMigration?.() === false) {
|
|
130
|
+
const document = await peekLocalConfigDocument();
|
|
131
|
+
if (document)
|
|
132
|
+
validateForMigration(document.parsed);
|
|
133
|
+
return document;
|
|
134
|
+
}
|
|
135
|
+
return queueConfigOperation(() => withLocalConfigReadLockIfNeeded(async () => {
|
|
136
|
+
const configPath = await prepareLocalConfigPath(validateForMigration);
|
|
137
|
+
const snapshot = await readConfigSnapshotIfExists(configPath);
|
|
138
|
+
return snapshot ? { path: configPath, ...snapshot } : undefined;
|
|
139
|
+
}));
|
|
140
|
+
}
|
|
141
|
+
async function withLocalConfigReadLockIfNeeded(read) {
|
|
142
|
+
if ((await pathExists(localConfigPath())) || (await pathExists(legacyLocalConfigPath()))) {
|
|
143
|
+
return withLocalConfigFileLock(read);
|
|
144
|
+
}
|
|
145
|
+
await afterMissingConfigReadProbeHook();
|
|
146
|
+
if (await pathExists(configMutationLockPath()))
|
|
147
|
+
return withLocalConfigFileLock(read);
|
|
148
|
+
await assertNoInterruptedConfigReplacement();
|
|
149
|
+
return read();
|
|
150
|
+
}
|
|
151
|
+
function queueConfigOperation(run) {
|
|
152
|
+
// Mutation callbacks use the unlocked helpers below, never a queued public read.
|
|
153
|
+
const operation = configUpdateQueue.then(run);
|
|
154
|
+
configUpdateQueue = operation.then(() => undefined, () => undefined);
|
|
155
|
+
return operation;
|
|
156
|
+
}
|
|
157
|
+
export function updateLocalConfigDocument(defaultValue, update, validate, signal, validateSource) {
|
|
158
|
+
const authority = engineHost().config;
|
|
159
|
+
if (authority) {
|
|
160
|
+
if (validateSource)
|
|
161
|
+
throw new Error("Source validators must run in the authoritative settings writer.");
|
|
162
|
+
return authority.update(defaultValue, update, validate, signal);
|
|
163
|
+
}
|
|
164
|
+
return queueConfigOperation(() => {
|
|
165
|
+
signal?.throwIfAborted();
|
|
166
|
+
return withLocalConfigFileLock(async () => {
|
|
167
|
+
signal?.throwIfAborted();
|
|
168
|
+
const configPath = await prepareLocalConfigPath(validate);
|
|
169
|
+
signal?.throwIfAborted();
|
|
170
|
+
const snapshot = await readConfigSnapshotIfExists(configPath);
|
|
171
|
+
signal?.throwIfAborted();
|
|
172
|
+
const document = snapshot ? { path: configPath, ...snapshot } : undefined;
|
|
173
|
+
validateSource?.(document);
|
|
174
|
+
const current = document ? structuredClone(document.parsed) : structuredClone(defaultValue);
|
|
175
|
+
const next = update(current);
|
|
176
|
+
validate(next);
|
|
177
|
+
signal?.throwIfAborted();
|
|
178
|
+
if (document && JSON.stringify(document.parsed) === JSON.stringify(next))
|
|
179
|
+
return next;
|
|
180
|
+
if (document)
|
|
181
|
+
await replaceLocalConfigDocumentUnlocked(document, next, signal);
|
|
182
|
+
else
|
|
183
|
+
await installPrivateConfigExclusively(localConfigPath(), serializedConfig(next), signal);
|
|
184
|
+
return next;
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
export function createLocalConfigDocument(value, signal) {
|
|
189
|
+
const authority = engineHost().config;
|
|
190
|
+
if (authority)
|
|
191
|
+
return authority.create(value, signal);
|
|
192
|
+
return queueConfigOperation(() => {
|
|
193
|
+
signal?.throwIfAborted();
|
|
194
|
+
return withLocalConfigFileLock(async () => {
|
|
195
|
+
signal?.throwIfAborted();
|
|
196
|
+
const bytes = serializedConfig(value);
|
|
197
|
+
try {
|
|
198
|
+
await installPrivateConfigExclusively(localConfigPath(), bytes, signal);
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
if (error.code === "EEXIST") {
|
|
202
|
+
throw new Error("Pi-sync settings were created concurrently; reopen settings and retry.");
|
|
203
|
+
}
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
export function replaceLocalConfigDocument(document, value) {
|
|
210
|
+
const authority = engineHost().config;
|
|
211
|
+
if (authority)
|
|
212
|
+
return authority.replace(document, value);
|
|
213
|
+
return queueConfigOperation(() => withLocalConfigFileLock(() => replaceLocalConfigDocumentUnlocked(document, value)));
|
|
214
|
+
}
|
|
215
|
+
async function replaceLocalConfigDocumentUnlocked(document, value, signal) {
|
|
216
|
+
const nextBytes = serializedConfig(value);
|
|
217
|
+
const canonicalPath = localConfigPath();
|
|
218
|
+
if (document.path !== canonicalPath) {
|
|
219
|
+
if (!(await configDocumentStillMatches(document)))
|
|
220
|
+
throw settingsChangedError();
|
|
221
|
+
let installed;
|
|
222
|
+
try {
|
|
223
|
+
installed = await installPrivateConfigExclusively(canonicalPath, nextBytes, signal);
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
if (error.code === "EEXIST") {
|
|
227
|
+
throw new Error("Canonical settings were created concurrently; no settings were replaced.");
|
|
228
|
+
}
|
|
229
|
+
throw error;
|
|
230
|
+
}
|
|
231
|
+
if (!(await configDocumentStillMatches(document))) {
|
|
232
|
+
await quarantineAndRemoveConfigIfMatchesUnlocked(canonicalPath, installed, nextBytes);
|
|
233
|
+
throw settingsChangedError();
|
|
234
|
+
}
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const recoveryPath = path.join(path.dirname(canonicalPath), `.${path.basename(canonicalPath)}.${randomUUID()}.schema-migration-source`);
|
|
238
|
+
const previousHandle = await fs.open(canonicalPath, "r");
|
|
239
|
+
try {
|
|
240
|
+
const previous = await previousHandle.stat();
|
|
241
|
+
if (previous.dev !== document.identity.dev || previous.ino !== document.identity.ino) {
|
|
242
|
+
throw settingsChangedError();
|
|
243
|
+
}
|
|
244
|
+
await writePrivateConfigBytes(recoveryPath, document.bytes, "wx");
|
|
245
|
+
await syncParentDirectory(canonicalPath);
|
|
246
|
+
await withPrivateConfigTemporary(canonicalPath, nextBytes, async (temporaryPath, installed) => {
|
|
247
|
+
let published = false;
|
|
248
|
+
let previousBytesRead;
|
|
249
|
+
const readPreviousBytes = () => (previousBytesRead ??= previousHandle.readFile());
|
|
250
|
+
try {
|
|
251
|
+
signal?.throwIfAborted();
|
|
252
|
+
await publishConfigFile(temporaryPath, canonicalPath, document, signal);
|
|
253
|
+
published = true;
|
|
254
|
+
await syncParentDirectory(canonicalPath);
|
|
255
|
+
await afterReplacementInstalledHook();
|
|
256
|
+
if (!(await readPreviousBytes()).equals(document.bytes))
|
|
257
|
+
throw settingsChangedError();
|
|
258
|
+
if (!(await fileIdentityAndContentsMatch(canonicalPath, installed, nextBytes))) {
|
|
259
|
+
throw settingsChangedError();
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
if (published || (await fileIdentityAndContentsMatch(canonicalPath, installed, nextBytes))) {
|
|
264
|
+
const previousBytes = await readPreviousBytes().catch(() => document.bytes);
|
|
265
|
+
if (!previousBytes.equals(document.bytes)) {
|
|
266
|
+
await writePrivateConfigBytes(recoveryPath, previousBytes, "w");
|
|
267
|
+
}
|
|
268
|
+
// Rollback also replaces atomically; never remove the active settings first.
|
|
269
|
+
if (await fileIdentityAndContentsMatch(canonicalPath, installed, nextBytes)) {
|
|
270
|
+
await fs.rename(recoveryPath, canonicalPath);
|
|
271
|
+
await syncParentDirectory(canonicalPath);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
else if (await configDocumentStillMatches(document)) {
|
|
275
|
+
await fs.rm(recoveryPath);
|
|
276
|
+
}
|
|
277
|
+
if (error.code === "EEXIST") {
|
|
278
|
+
throw new Error("Canonical settings changed concurrently; no settings were replaced.");
|
|
279
|
+
}
|
|
280
|
+
throw error;
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
await fs.rm(recoveryPath).catch(() => undefined);
|
|
284
|
+
await syncParentDirectory(canonicalPath).catch(() => undefined);
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
if (await configDocumentStillMatches(document)) {
|
|
288
|
+
await fs.rm(recoveryPath, { force: true }).catch(() => undefined);
|
|
289
|
+
}
|
|
290
|
+
throw error;
|
|
291
|
+
}
|
|
292
|
+
finally {
|
|
293
|
+
await previousHandle.close().catch(() => undefined);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
async function prepareLocalConfigPath(validateForMigration) {
|
|
297
|
+
const canonicalPath = localConfigPath();
|
|
298
|
+
const legacyPath = legacyLocalConfigPath();
|
|
299
|
+
if (await pathExists(canonicalPath)) {
|
|
300
|
+
const legacyStatus = await secureIgnoredLegacyIfPresent(legacyPath);
|
|
301
|
+
if (legacyStatus !== "missing" && !legacyPresenceNoticed.has(canonicalPath)) {
|
|
302
|
+
legacyPresenceNoticed.add(canonicalPath);
|
|
303
|
+
recordConfigMigrationNotice(canonicalPath, legacyStatus === "private"
|
|
304
|
+
? `${LEGACY_CONFIG_FILE_NAME} legacy settings were ignored because ${CONFIG_FILE_NAME} takes precedence. Delete ${LEGACY_CONFIG_FILE_NAME} after confirming your settings.`
|
|
305
|
+
: `${LEGACY_CONFIG_FILE_NAME} legacy settings were ignored because ${CONFIG_FILE_NAME} takes precedence, but pi-sync could not verify them as a private regular file. Secure or delete the legacy path after confirming your settings.`);
|
|
306
|
+
}
|
|
307
|
+
return canonicalPath;
|
|
308
|
+
}
|
|
309
|
+
const legacy = await readConfigSnapshotIfExists(legacyPath);
|
|
310
|
+
if (!legacy)
|
|
311
|
+
return canonicalPath;
|
|
312
|
+
validateForMigration(legacy.parsed);
|
|
313
|
+
let installedIdentity;
|
|
314
|
+
try {
|
|
315
|
+
installedIdentity = await installPrivateConfigExclusively(canonicalPath, legacy.bytes);
|
|
316
|
+
}
|
|
317
|
+
catch (error) {
|
|
318
|
+
if (error.code === "EEXIST") {
|
|
319
|
+
recordConfigMigrationNotice(canonicalPath, `${LEGACY_CONFIG_FILE_NAME} legacy settings were ignored because ${CONFIG_FILE_NAME} was created concurrently and takes precedence.`);
|
|
320
|
+
return canonicalPath;
|
|
321
|
+
}
|
|
322
|
+
recordConfigMigrationNotice(canonicalPath, `Could not migrate ${LEGACY_CONFIG_FILE_NAME} to ${CONFIG_FILE_NAME}; the legacy settings were used for this session and were not changed.`);
|
|
323
|
+
return legacyPath;
|
|
324
|
+
}
|
|
325
|
+
if (!(await configSnapshotStillMatches(legacyPath, legacy))) {
|
|
326
|
+
const removed = await quarantineAndRemoveConfigIfMatchesUnlocked(canonicalPath, installedIdentity, legacy.bytes);
|
|
327
|
+
recordConfigMigrationNotice(canonicalPath, removed
|
|
328
|
+
? `${LEGACY_CONFIG_FILE_NAME} changed during migration; the stale ${CONFIG_FILE_NAME} copy was removed and the legacy settings were used for this session.`
|
|
329
|
+
: `${LEGACY_CONFIG_FILE_NAME} changed during migration, but ${CONFIG_FILE_NAME} was replaced concurrently and takes precedence.`);
|
|
330
|
+
return removed ? legacyPath : canonicalPath;
|
|
331
|
+
}
|
|
332
|
+
legacyPresenceNoticed.add(canonicalPath);
|
|
333
|
+
recordConfigMigrationNotice(canonicalPath, `pi-sync settings migrated from ${LEGACY_CONFIG_FILE_NAME} to ${CONFIG_FILE_NAME}; the private legacy file was retained as a recovery copy and can be deleted after verification.`);
|
|
334
|
+
return canonicalPath;
|
|
335
|
+
}
|
|
336
|
+
async function secureIgnoredLegacyIfPresent(filePath) {
|
|
337
|
+
let pathStat;
|
|
338
|
+
try {
|
|
339
|
+
pathStat = await fs.lstat(filePath);
|
|
340
|
+
}
|
|
341
|
+
catch (error) {
|
|
342
|
+
if (error.code === "ENOENT")
|
|
343
|
+
return "missing";
|
|
344
|
+
return "unsafe";
|
|
345
|
+
}
|
|
346
|
+
if (pathStat.isSymbolicLink() || !pathStat.isFile())
|
|
347
|
+
return "unsafe";
|
|
348
|
+
let handle;
|
|
349
|
+
try {
|
|
350
|
+
handle = await fs.open(filePath, "r");
|
|
351
|
+
const openedStat = await handle.stat();
|
|
352
|
+
if (openedStat.dev !== pathStat.dev || openedStat.ino !== pathStat.ino) {
|
|
353
|
+
return "unsafe";
|
|
354
|
+
}
|
|
355
|
+
if (process.platform !== "win32" && (openedStat.mode & 0o777) !== 0o600) {
|
|
356
|
+
await handle.chmod(0o600);
|
|
357
|
+
}
|
|
358
|
+
return "private";
|
|
359
|
+
}
|
|
360
|
+
catch {
|
|
361
|
+
return "unsafe";
|
|
362
|
+
}
|
|
363
|
+
finally {
|
|
364
|
+
await handle?.close().catch(() => undefined);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
async function readConfigSnapshotIfExists(filePath) {
|
|
368
|
+
let pathStat;
|
|
369
|
+
try {
|
|
370
|
+
pathStat = await fs.lstat(filePath);
|
|
371
|
+
}
|
|
372
|
+
catch (error) {
|
|
373
|
+
if (error.code === "ENOENT")
|
|
374
|
+
return undefined;
|
|
375
|
+
throw error;
|
|
376
|
+
}
|
|
377
|
+
if (pathStat.isSymbolicLink()) {
|
|
378
|
+
throw new Error(`Refusing to read symlinked pi-sync config: ${filePath}`);
|
|
379
|
+
}
|
|
380
|
+
if (!pathStat.isFile())
|
|
381
|
+
throw new Error(`pi-sync config is not a regular file: ${filePath}`);
|
|
382
|
+
const handle = await fs.open(filePath, "r");
|
|
383
|
+
try {
|
|
384
|
+
const openedStat = await handle.stat();
|
|
385
|
+
if (openedStat.dev !== pathStat.dev || openedStat.ino !== pathStat.ino) {
|
|
386
|
+
throw new Error(`pi-sync config changed while opening: ${filePath}`);
|
|
387
|
+
}
|
|
388
|
+
if (process.platform !== "win32" && (openedStat.mode & 0o777) !== 0o600) {
|
|
389
|
+
await handle.chmod(0o600);
|
|
390
|
+
}
|
|
391
|
+
const bytes = await handle.readFile();
|
|
392
|
+
return {
|
|
393
|
+
bytes,
|
|
394
|
+
identity: { dev: openedStat.dev, ino: openedStat.ino },
|
|
395
|
+
parsed: parseConfigObject(bytes, filePath),
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
finally {
|
|
399
|
+
await handle.close();
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
function serializedConfig(value) {
|
|
403
|
+
return Buffer.from(`${JSON.stringify(value, null, "\t")}\n`, "utf8");
|
|
404
|
+
}
|
|
405
|
+
function parseConfigObject(bytes, filePath) {
|
|
406
|
+
let parsed;
|
|
407
|
+
try {
|
|
408
|
+
parsed = JSON.parse(bytes.toString("utf8"));
|
|
409
|
+
}
|
|
410
|
+
catch {
|
|
411
|
+
throw new SyntaxError(`Invalid JSON in pi-sync config: ${filePath}`);
|
|
412
|
+
}
|
|
413
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
414
|
+
throw new Error(`pi-sync config must contain a JSON object: ${filePath}`);
|
|
415
|
+
}
|
|
416
|
+
return parsed;
|
|
417
|
+
}
|
|
418
|
+
async function installPrivateConfigExclusively(filePath, bytes, signal) {
|
|
419
|
+
return withPrivateConfigTemporary(filePath, bytes, async (temporaryPath, installed) => {
|
|
420
|
+
signal?.throwIfAborted();
|
|
421
|
+
await publishConfigFile(temporaryPath, filePath, undefined, signal);
|
|
422
|
+
try {
|
|
423
|
+
if (!(await fileIdentityAndContentsMatch(filePath, installed, bytes))) {
|
|
424
|
+
throw new Error(`Published pi-sync settings changed concurrently: ${filePath}`);
|
|
425
|
+
}
|
|
426
|
+
await syncParentDirectory(filePath);
|
|
427
|
+
}
|
|
428
|
+
catch (error) {
|
|
429
|
+
await quarantineAndRemoveConfigIfMatchesUnlocked(filePath, installed, bytes);
|
|
430
|
+
throw error;
|
|
431
|
+
}
|
|
432
|
+
return installed;
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
async function withPrivateConfigTemporary(filePath, bytes, publish) {
|
|
436
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
437
|
+
const temporaryPath = path.join(path.dirname(filePath), `.${path.basename(filePath)}.${process.pid}.${randomUUID()}.migrate`);
|
|
438
|
+
try {
|
|
439
|
+
const identity = await writePrivateConfigBytes(temporaryPath, bytes, "wx");
|
|
440
|
+
return await publish(temporaryPath, identity);
|
|
441
|
+
}
|
|
442
|
+
finally {
|
|
443
|
+
await fs.rm(temporaryPath, { force: true }).catch(() => undefined);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
async function writePrivateConfigBytes(filePath, bytes, flags) {
|
|
447
|
+
const handle = await fs.open(filePath, flags, 0o600);
|
|
448
|
+
try {
|
|
449
|
+
await handle.writeFile(bytes);
|
|
450
|
+
if (process.platform !== "win32")
|
|
451
|
+
await handle.chmod(0o600);
|
|
452
|
+
await handle.sync();
|
|
453
|
+
const installed = await handle.stat();
|
|
454
|
+
return { dev: installed.dev, ino: installed.ino };
|
|
455
|
+
}
|
|
456
|
+
finally {
|
|
457
|
+
await handle.close();
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
async function configDocumentStillMatches(document) {
|
|
461
|
+
return fileIdentityAndContentsMatch(document.path, document.identity, document.bytes);
|
|
462
|
+
}
|
|
463
|
+
function settingsChangedError() {
|
|
464
|
+
return new Error("pi-sync settings changed during migration; no settings were replaced.");
|
|
465
|
+
}
|
|
466
|
+
async function configSnapshotStillMatches(filePath, snapshot) {
|
|
467
|
+
return fileIdentityAndContentsMatch(filePath, snapshot.identity, snapshot.bytes);
|
|
468
|
+
}
|
|
469
|
+
export function quarantineAndRemoveConfigIfMatches(filePath, identity, expectedBytes) {
|
|
470
|
+
return queueConfigOperation(() => withLocalConfigFileLock(() => quarantineAndRemoveConfigIfMatchesUnlocked(filePath, identity, expectedBytes)));
|
|
471
|
+
}
|
|
472
|
+
async function quarantineAndRemoveConfigIfMatchesUnlocked(filePath, identity, expectedBytes) {
|
|
473
|
+
const quarantinePath = path.join(path.dirname(filePath), `.${path.basename(filePath)}.${randomUUID()}.migration-retired`);
|
|
474
|
+
try {
|
|
475
|
+
await fs.rename(filePath, quarantinePath);
|
|
476
|
+
}
|
|
477
|
+
catch {
|
|
478
|
+
return false;
|
|
479
|
+
}
|
|
480
|
+
await afterConfigQuarantinedHook();
|
|
481
|
+
try {
|
|
482
|
+
await syncParentDirectory(filePath);
|
|
483
|
+
}
|
|
484
|
+
catch {
|
|
485
|
+
await restoreQuarantinedConfig(filePath, quarantinePath);
|
|
486
|
+
return false;
|
|
487
|
+
}
|
|
488
|
+
const matches = await fileIdentityAndContentsMatch(quarantinePath, identity, expectedBytes);
|
|
489
|
+
if (!matches) {
|
|
490
|
+
await restoreQuarantinedConfig(filePath, quarantinePath);
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
if (await pathExists(filePath)) {
|
|
494
|
+
await fs.rm(quarantinePath, { force: true });
|
|
495
|
+
await syncParentDirectory(filePath);
|
|
496
|
+
return false;
|
|
497
|
+
}
|
|
498
|
+
await fs.rm(quarantinePath);
|
|
499
|
+
await syncParentDirectory(filePath);
|
|
500
|
+
return true;
|
|
501
|
+
}
|
|
502
|
+
async function fileIdentityAndContentsMatch(filePath, identity, expectedBytes) {
|
|
503
|
+
try {
|
|
504
|
+
const current = await fs.lstat(filePath);
|
|
505
|
+
if (current.isSymbolicLink())
|
|
506
|
+
return false;
|
|
507
|
+
if (current.dev !== identity.dev || current.ino !== identity.ino)
|
|
508
|
+
return false;
|
|
509
|
+
return (await fs.readFile(filePath)).equals(expectedBytes);
|
|
510
|
+
}
|
|
511
|
+
catch {
|
|
512
|
+
return false;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
async function restoreQuarantinedConfig(filePath, quarantinePath) {
|
|
516
|
+
try {
|
|
517
|
+
await renameFileWithoutReplacement(quarantinePath, filePath);
|
|
518
|
+
if (process.platform !== "win32")
|
|
519
|
+
await fs.chmod(filePath, 0o600);
|
|
520
|
+
await syncParentDirectory(filePath);
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
catch (error) {
|
|
524
|
+
if (error.code !== "EEXIST")
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
try {
|
|
528
|
+
if (process.platform !== "win32")
|
|
529
|
+
await fs.chmod(quarantinePath, 0o600);
|
|
530
|
+
await syncParentDirectory(filePath);
|
|
531
|
+
}
|
|
532
|
+
catch {
|
|
533
|
+
// Preserve the quarantine rather than risk deleting settings that changed concurrently.
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
function configMutationLockPath() {
|
|
537
|
+
return `${localConfigPath()}.mutation-lock`;
|
|
538
|
+
}
|
|
539
|
+
export async function withLocalConfigFileLock(run) {
|
|
540
|
+
const configPath = localConfigPath();
|
|
541
|
+
await fs.mkdir(path.dirname(configPath), { recursive: true });
|
|
542
|
+
let compromisedError;
|
|
543
|
+
const release = await lockfile.lock(configPath, {
|
|
544
|
+
fs: LOCKFILE_FS_ADAPTER,
|
|
545
|
+
lockfilePath: configMutationLockPath(),
|
|
546
|
+
realpath: false,
|
|
547
|
+
stale: CONFIG_LOCK_STALE_MS,
|
|
548
|
+
update: CONFIG_LOCK_UPDATE_MS,
|
|
549
|
+
retries: { retries: 100, factor: 1.2, minTimeout: 10, maxTimeout: 100 },
|
|
550
|
+
onCompromised: (error) => {
|
|
551
|
+
compromisedError = error;
|
|
552
|
+
},
|
|
553
|
+
});
|
|
554
|
+
try {
|
|
555
|
+
await assertNoInterruptedConfigReplacement();
|
|
556
|
+
const result = await run();
|
|
557
|
+
if (compromisedError)
|
|
558
|
+
throw compromisedError;
|
|
559
|
+
return result;
|
|
560
|
+
}
|
|
561
|
+
finally {
|
|
562
|
+
await release();
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
async function syncParentDirectory(filePath) {
|
|
566
|
+
if (process.platform === "win32")
|
|
567
|
+
return;
|
|
568
|
+
let handle;
|
|
569
|
+
try {
|
|
570
|
+
handle = await fs.open(path.dirname(filePath), "r");
|
|
571
|
+
await handle.sync();
|
|
572
|
+
}
|
|
573
|
+
finally {
|
|
574
|
+
await handle?.close().catch(() => undefined);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
async function publishFileWithGuard(source, destination, expected, signal) {
|
|
578
|
+
await beforeConfigPublicationHook();
|
|
579
|
+
signal?.throwIfAborted();
|
|
580
|
+
if (expected) {
|
|
581
|
+
// The shared lock orders Pi Sync writers; rename has no CAS for noncooperating writers.
|
|
582
|
+
if (!(await configDocumentStillMatches(expected)))
|
|
583
|
+
throw settingsChangedError();
|
|
584
|
+
signal?.throwIfAborted();
|
|
585
|
+
await fs.rename(source, destination);
|
|
586
|
+
}
|
|
587
|
+
else {
|
|
588
|
+
await renameFileWithoutReplacement(source, destination, signal);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
async function renameFileWithoutReplacement(source, destination, signal) {
|
|
592
|
+
if (await pathExists(destination)) {
|
|
593
|
+
throw Object.assign(new Error(`Settings already exist: ${destination}`), { code: "EEXIST" });
|
|
594
|
+
}
|
|
595
|
+
signal?.throwIfAborted();
|
|
596
|
+
await fs.rename(source, destination);
|
|
597
|
+
}
|
|
598
|
+
async function assertNoInterruptedConfigReplacement() {
|
|
599
|
+
const configPath = localConfigPath();
|
|
600
|
+
if (await pathExists(configPath))
|
|
601
|
+
return;
|
|
602
|
+
let names;
|
|
603
|
+
try {
|
|
604
|
+
names = await fs.readdir(path.dirname(configPath));
|
|
605
|
+
}
|
|
606
|
+
catch (error) {
|
|
607
|
+
if (error.code === "ENOENT")
|
|
608
|
+
return;
|
|
609
|
+
throw error;
|
|
610
|
+
}
|
|
611
|
+
const prefix = `.${path.basename(configPath)}.`;
|
|
612
|
+
if (names.some((name) => name.startsWith(prefix) && (name.endsWith(".schema-migration-source") || name.endsWith(".migration-retired")))) {
|
|
613
|
+
throw new Error("Interrupted pi-sync settings replacement: restore the retained recovery file to pi-sync.json before retrying; legacy settings were not loaded.");
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
async function pathExists(filePath) {
|
|
617
|
+
try {
|
|
618
|
+
await fs.lstat(filePath);
|
|
619
|
+
return true;
|
|
620
|
+
}
|
|
621
|
+
catch (error) {
|
|
622
|
+
if (error.code === "ENOENT")
|
|
623
|
+
return false;
|
|
624
|
+
throw error;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
function recordConfigMigrationNotice(configPath, notice) {
|
|
628
|
+
if (!configMigrationNotices.has(configPath))
|
|
629
|
+
configMigrationNotices.set(configPath, notice);
|
|
630
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { AnySyncConfig, PartialConfig, PiSyncSettingsV3, StorageConnectionSettings, SyncSetupSettings } from "./settings-types.js";
|
|
2
|
+
export declare function loadConfig(setupName?: string): Promise<AnySyncConfig>;
|
|
3
|
+
/** A background freshness read must never trigger legacy-file publication. */
|
|
4
|
+
export declare function loadConfigForCheck(): Promise<AnySyncConfig>;
|
|
5
|
+
export declare function configFromSettings(settings: PiSyncSettingsV3, setupName?: string): AnySyncConfig;
|
|
6
|
+
export type SyncSetupStorageReview = Pick<PartialConfig, "connectionName" | "storageKind" | "storagePath" | "bucket" | "branch">;
|
|
7
|
+
/** A validated setup-facing projection used by manager and settings UI. */
|
|
8
|
+
export declare function loadPartialConfig(setupName?: string): Promise<PartialConfig>;
|
|
9
|
+
export declare function syncSetupStorageReview(setupName: string, setup: SyncSetupSettings, connectionName: string, connection: StorageConnectionSettings): SyncSetupStorageReview;
|
|
10
|
+
export declare function syncSetupReviewIdentity(setupName: string, setup: SyncSetupSettings, connectionName: string, connection: StorageConnectionSettings): string;
|
|
11
|
+
export declare function syncConfigReviewIdentity(config: AnySyncConfig): string;
|
|
12
|
+
export declare function syncConfigReviewFingerprint(config: AnySyncConfig): string;
|
|
13
|
+
/** Internal freshness token, including credentials; never display or persist it. */
|
|
14
|
+
export declare function syncCheckConfigFingerprint(config: AnySyncConfig): string;
|
|
15
|
+
export declare function sessionTokenWarnings(config: {
|
|
16
|
+
endpoint?: string;
|
|
17
|
+
sessionToken?: string;
|
|
18
|
+
}): string[];
|
|
19
|
+
export declare function syncSessionsWarnings(config: {
|
|
20
|
+
include: readonly string[];
|
|
21
|
+
exclude?: readonly string[];
|
|
22
|
+
}): string[];
|
|
23
|
+
export declare function isEnabled(value: boolean | string | undefined, defaultValue: boolean): boolean;
|
|
24
|
+
export declare function isExplicitlyEnabled(value: boolean | string | undefined): boolean;
|