@indigoai-us/hq-cloud 6.11.12 → 6.11.13
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/dist/bin/sync-runner-company.d.ts +35 -0
- package/dist/bin/sync-runner-company.d.ts.map +1 -0
- package/dist/bin/sync-runner-company.js +290 -0
- package/dist/bin/sync-runner-company.js.map +1 -0
- package/dist/bin/sync-runner-events.d.ts +12 -0
- package/dist/bin/sync-runner-events.d.ts.map +1 -0
- package/dist/bin/sync-runner-events.js +12 -0
- package/dist/bin/sync-runner-events.js.map +1 -0
- package/dist/bin/sync-runner-planning.d.ts +53 -0
- package/dist/bin/sync-runner-planning.d.ts.map +1 -0
- package/dist/bin/sync-runner-planning.js +59 -0
- package/dist/bin/sync-runner-planning.js.map +1 -0
- package/dist/bin/sync-runner-rollup.d.ts +24 -0
- package/dist/bin/sync-runner-rollup.d.ts.map +1 -0
- package/dist/bin/sync-runner-rollup.js +46 -0
- package/dist/bin/sync-runner-rollup.js.map +1 -0
- package/dist/bin/sync-runner-telemetry.d.ts +5 -0
- package/dist/bin/sync-runner-telemetry.d.ts.map +1 -0
- package/dist/bin/sync-runner-telemetry.js +5 -0
- package/dist/bin/sync-runner-telemetry.js.map +1 -0
- package/dist/bin/sync-runner-watch-loop.d.ts +17 -0
- package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -0
- package/dist/bin/sync-runner-watch-loop.js +372 -0
- package/dist/bin/sync-runner-watch-loop.js.map +1 -0
- package/dist/bin/sync-runner-watch-routes.d.ts +25 -0
- package/dist/bin/sync-runner-watch-routes.d.ts.map +1 -0
- package/dist/bin/sync-runner-watch-routes.js +74 -0
- package/dist/bin/sync-runner-watch-routes.js.map +1 -0
- package/dist/bin/sync-runner.d.ts +3 -54
- package/dist/bin/sync-runner.d.ts.map +1 -1
- package/dist/bin/sync-runner.js +73 -1154
- package/dist/bin/sync-runner.js.map +1 -1
- package/dist/cli/reindex.d.ts.map +1 -1
- package/dist/cli/reindex.js +34 -17
- package/dist/cli/reindex.js.map +1 -1
- package/dist/cli/reindex.test.js +39 -5
- package/dist/cli/reindex.test.js.map +1 -1
- package/dist/cli/rescue-classify-ordering.test.js +17 -0
- package/dist/cli/rescue-classify-ordering.test.js.map +1 -1
- package/dist/cli/rescue-core.d.ts +45 -0
- package/dist/cli/rescue-core.d.ts.map +1 -1
- package/dist/cli/rescue-core.js +197 -170
- package/dist/cli/rescue-core.js.map +1 -1
- package/dist/cli/share.d.ts.map +1 -1
- package/dist/cli/share.js +224 -676
- package/dist/cli/share.js.map +1 -1
- package/dist/cli/sync.d.ts.map +1 -1
- package/dist/cli/sync.js +399 -726
- package/dist/cli/sync.js.map +1 -1
- package/dist/cli/sync.test.js +20 -0
- package/dist/cli/sync.test.js.map +1 -1
- package/dist/daemon-worker.d.ts +2 -2
- package/dist/daemon-worker.js +3 -3
- package/dist/daemon-worker.js.map +1 -1
- package/dist/object-io.js +1 -1
- package/dist/object-io.js.map +1 -1
- package/dist/remote-pull.d.ts +2 -2
- package/dist/remote-pull.d.ts.map +1 -1
- package/dist/remote-pull.js +23 -3
- package/dist/remote-pull.js.map +1 -1
- package/dist/remote-pull.test.js +24 -2
- package/dist/remote-pull.test.js.map +1 -1
- package/dist/sync/push-receiver.d.ts +6 -0
- package/dist/sync/push-receiver.d.ts.map +1 -1
- package/dist/sync/push-receiver.js +32 -2
- package/dist/sync/push-receiver.js.map +1 -1
- package/dist/sync/push-receiver.test.js +31 -0
- package/dist/sync/push-receiver.test.js.map +1 -1
- package/dist/sync-core.d.ts +27 -0
- package/dist/sync-core.d.ts.map +1 -0
- package/dist/sync-core.js +54 -0
- package/dist/sync-core.js.map +1 -0
- package/dist/vault-client.d.ts.map +1 -1
- package/dist/vault-client.js +284 -36
- package/dist/vault-client.js.map +1 -1
- package/dist/vault-client.test.js +59 -0
- package/dist/vault-client.test.js.map +1 -1
- package/dist/watcher.d.ts +2 -20
- package/dist/watcher.d.ts.map +1 -1
- package/dist/watcher.js +3 -113
- package/dist/watcher.js.map +1 -1
- package/package.json +1 -1
- package/src/bin/sync-runner-company.ts +350 -0
- package/src/bin/sync-runner-events.ts +25 -0
- package/src/bin/sync-runner-planning.ts +121 -0
- package/src/bin/sync-runner-rollup.ts +72 -0
- package/src/bin/sync-runner-telemetry.ts +8 -0
- package/src/bin/sync-runner-watch-loop.ts +443 -0
- package/src/bin/sync-runner-watch-routes.ts +86 -0
- package/src/bin/sync-runner.ts +96 -1253
- package/src/cli/reindex.test.ts +41 -3
- package/src/cli/reindex.ts +35 -19
- package/src/cli/rescue-classify-ordering.test.ts +20 -0
- package/src/cli/rescue-core.ts +252 -176
- package/src/cli/share.ts +363 -705
- package/src/cli/sync.test.ts +25 -0
- package/src/cli/sync.ts +612 -802
- package/src/daemon-worker.ts +3 -3
- package/src/object-io.ts +1 -1
- package/src/remote-pull.test.ts +30 -1
- package/src/remote-pull.ts +29 -4
- package/src/sync/push-receiver.test.ts +35 -0
- package/src/sync/push-receiver.ts +41 -2
- package/src/sync-core.ts +58 -0
- package/src/vault-client.test.ts +74 -0
- package/src/vault-client.ts +395 -43
- package/src/watcher.ts +6 -141
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import * as path from "path";
|
|
2
|
+
import { computePersonalVaultPaths } from "../personal-vault.js";
|
|
3
|
+
import type { SyncOptions, SyncProgressEvent, SyncResult } from "../cli/sync.js";
|
|
4
|
+
import type { ShareOptions, ShareResult } from "../cli/share.js";
|
|
5
|
+
import type { ConflictStrategy } from "../cli/conflict.js";
|
|
6
|
+
import type { UploadAuthor } from "../s3.js";
|
|
7
|
+
import type { VaultServiceConfig } from "../index.js";
|
|
8
|
+
import { resolvePullScope, type PullScope } from "../sync/pull-scope.js";
|
|
9
|
+
import { describeError } from "../lib/describe-error.js";
|
|
10
|
+
import type {
|
|
11
|
+
DeletePropagationPolicy,
|
|
12
|
+
Direction,
|
|
13
|
+
RunnerEvent,
|
|
14
|
+
VaultClientSurface,
|
|
15
|
+
} from "./sync-runner.js";
|
|
16
|
+
import type { RunnerTarget } from "./sync-runner-planning.js";
|
|
17
|
+
import {
|
|
18
|
+
createCompanyState,
|
|
19
|
+
type CompanyState,
|
|
20
|
+
} from "./sync-runner-rollup.js";
|
|
21
|
+
|
|
22
|
+
export interface CompanyFanoutResult {
|
|
23
|
+
stateByCompany: Map<string, CompanyState>;
|
|
24
|
+
errors: Array<{ company: string; message: string }>;
|
|
25
|
+
allConflicts: Array<{
|
|
26
|
+
company: string;
|
|
27
|
+
path: string;
|
|
28
|
+
direction: "pull" | "push";
|
|
29
|
+
}>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function executeCompanyFanout(options: {
|
|
33
|
+
plan: RunnerTarget[];
|
|
34
|
+
direction: Direction;
|
|
35
|
+
hqRoot: string;
|
|
36
|
+
onConflict: ConflictStrategy;
|
|
37
|
+
client: VaultClientSurface;
|
|
38
|
+
vaultConfig: VaultServiceConfig;
|
|
39
|
+
uploadAuthor?: UploadAuthor;
|
|
40
|
+
operationLockAlreadyHeld?: boolean;
|
|
41
|
+
syncFn: (options: SyncOptions) => Promise<SyncResult>;
|
|
42
|
+
shareFn: (options: ShareOptions) => Promise<ShareResult>;
|
|
43
|
+
resolveDeletePolicy: () => DeletePropagationPolicy;
|
|
44
|
+
emit: (event: RunnerEvent) => void;
|
|
45
|
+
}): Promise<CompanyFanoutResult> {
|
|
46
|
+
const doPush =
|
|
47
|
+
options.direction === "push" || options.direction === "both";
|
|
48
|
+
const doPull =
|
|
49
|
+
options.direction === "pull" || options.direction === "both";
|
|
50
|
+
const errors: Array<{ company: string; message: string }> = [];
|
|
51
|
+
const allConflicts: Array<{
|
|
52
|
+
company: string;
|
|
53
|
+
path: string;
|
|
54
|
+
direction: "pull" | "push";
|
|
55
|
+
}> = [];
|
|
56
|
+
const stateByCompany = new Map<string, CompanyState>();
|
|
57
|
+
|
|
58
|
+
for (const target of options.plan) {
|
|
59
|
+
const companyLabel = target.slug;
|
|
60
|
+
const state = createCompanyState(companyLabel);
|
|
61
|
+
stateByCompany.set(companyLabel, state);
|
|
62
|
+
|
|
63
|
+
let activePhase: "pull" | "push" = doPush && !doPull ? "push" : "pull";
|
|
64
|
+
let companyHadTransferError = false;
|
|
65
|
+
const tagAndEmit = (event: SyncProgressEvent): void => {
|
|
66
|
+
if (event.type === "plan") {
|
|
67
|
+
options.emit({
|
|
68
|
+
type: "plan",
|
|
69
|
+
company: companyLabel,
|
|
70
|
+
filesToDownload: event.filesToDownload,
|
|
71
|
+
bytesToDownload: event.bytesToDownload,
|
|
72
|
+
filesToUpload: event.filesToUpload,
|
|
73
|
+
bytesToUpload: event.bytesToUpload,
|
|
74
|
+
filesToSkip: event.filesToSkip,
|
|
75
|
+
filesToConflict: event.filesToConflict,
|
|
76
|
+
filesToDelete: event.filesToDelete,
|
|
77
|
+
});
|
|
78
|
+
} else if (event.type === "progress") {
|
|
79
|
+
if (activePhase === "push") {
|
|
80
|
+
state.filesUploaded += 1;
|
|
81
|
+
state.bytesUploaded += event.bytes;
|
|
82
|
+
} else {
|
|
83
|
+
state.filesDownloaded += 1;
|
|
84
|
+
state.bytesDownloaded += event.bytes;
|
|
85
|
+
}
|
|
86
|
+
options.emit({
|
|
87
|
+
type: "progress",
|
|
88
|
+
company: companyLabel,
|
|
89
|
+
path: event.path,
|
|
90
|
+
bytes: event.bytes,
|
|
91
|
+
direction: activePhase === "push" ? "up" : "down",
|
|
92
|
+
...(event.message ? { message: event.message } : {}),
|
|
93
|
+
...(event.deleted ? { deleted: event.deleted } : {}),
|
|
94
|
+
});
|
|
95
|
+
} else if (event.type === "conflict") {
|
|
96
|
+
options.emit({
|
|
97
|
+
type: "conflict",
|
|
98
|
+
company: companyLabel,
|
|
99
|
+
path: event.path,
|
|
100
|
+
direction: event.direction,
|
|
101
|
+
resolution: event.resolution,
|
|
102
|
+
});
|
|
103
|
+
} else if (event.type === "error") {
|
|
104
|
+
companyHadTransferError = true;
|
|
105
|
+
errors.push({
|
|
106
|
+
company: companyLabel,
|
|
107
|
+
message: event.path
|
|
108
|
+
? `${event.path}: ${event.message}`
|
|
109
|
+
: event.message,
|
|
110
|
+
});
|
|
111
|
+
options.emit({
|
|
112
|
+
type: "error",
|
|
113
|
+
company: companyLabel,
|
|
114
|
+
path: event.path,
|
|
115
|
+
message: event.message,
|
|
116
|
+
});
|
|
117
|
+
} else if (event.type === "new-files") {
|
|
118
|
+
options.emit({
|
|
119
|
+
type: "new-files",
|
|
120
|
+
company: companyLabel,
|
|
121
|
+
files: event.files,
|
|
122
|
+
});
|
|
123
|
+
} else if (event.type === "scope-excluded") {
|
|
124
|
+
options.emit({
|
|
125
|
+
type: "scope-excluded",
|
|
126
|
+
company: companyLabel,
|
|
127
|
+
count: event.count,
|
|
128
|
+
samplePaths: event.samplePaths,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
let pushResult: ShareResult = {
|
|
135
|
+
filesUploaded: 0,
|
|
136
|
+
bytesUploaded: 0,
|
|
137
|
+
filesSkipped: 0,
|
|
138
|
+
filesDeleted: 0,
|
|
139
|
+
filesTombstoned: 0,
|
|
140
|
+
filesRefusedStale: 0,
|
|
141
|
+
filesRefusedStalePaths: [],
|
|
142
|
+
filesSuppressedByTombstone: 0,
|
|
143
|
+
filesExcludedByPolicy: 0,
|
|
144
|
+
filesExcludedByScope: 0,
|
|
145
|
+
conflictPaths: [],
|
|
146
|
+
aborted: false,
|
|
147
|
+
};
|
|
148
|
+
let pullResult: SyncResult = {
|
|
149
|
+
filesDownloaded: 0,
|
|
150
|
+
bytesDownloaded: 0,
|
|
151
|
+
filesSkipped: 0,
|
|
152
|
+
conflicts: 0,
|
|
153
|
+
conflictPaths: [],
|
|
154
|
+
aborted: false,
|
|
155
|
+
newFiles: [],
|
|
156
|
+
newFilesCount: 0,
|
|
157
|
+
filesExcludedByPolicy: 0,
|
|
158
|
+
filesTombstoned: 0,
|
|
159
|
+
filesOutOfScope: 0,
|
|
160
|
+
scopeOrphansRemoved: 0,
|
|
161
|
+
scopeOrphansBlocked: 0,
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const includeLocalCompanies =
|
|
165
|
+
process.env.HQ_SYNC_LOCAL_COMPANIES_TO_PERSONAL === "1";
|
|
166
|
+
const teamSyncedSlugs = new Set(
|
|
167
|
+
options.plan
|
|
168
|
+
.filter((p) => p.personalMode !== true)
|
|
169
|
+
.map((p) => p.slug),
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
const scope: PullScope =
|
|
173
|
+
target.personalMode === true
|
|
174
|
+
? { syncMode: "all" }
|
|
175
|
+
: await resolvePullScope(
|
|
176
|
+
options.client,
|
|
177
|
+
target.uid,
|
|
178
|
+
target.slug,
|
|
179
|
+
options.hqRoot,
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
if (doPush) {
|
|
183
|
+
activePhase = "push";
|
|
184
|
+
const pushPaths =
|
|
185
|
+
target.personalMode === true
|
|
186
|
+
? computePersonalVaultPaths(options.hqRoot, {
|
|
187
|
+
includeLocalCompanies,
|
|
188
|
+
teamSyncedSlugs,
|
|
189
|
+
})
|
|
190
|
+
: [path.join(options.hqRoot, "companies", target.slug)];
|
|
191
|
+
const decommissionPrefixes =
|
|
192
|
+
target.personalMode === true
|
|
193
|
+
? Array.from(teamSyncedSlugs).map((slug) => `companies/${slug}`)
|
|
194
|
+
: undefined;
|
|
195
|
+
pushResult = await options.shareFn({
|
|
196
|
+
paths: pushPaths,
|
|
197
|
+
company: target.uid,
|
|
198
|
+
vaultConfig: options.vaultConfig,
|
|
199
|
+
hqRoot: options.hqRoot,
|
|
200
|
+
onConflict: options.onConflict,
|
|
201
|
+
skipUnchanged: true,
|
|
202
|
+
propagateDeletes: true,
|
|
203
|
+
propagateDeletePolicy: options.resolveDeletePolicy(),
|
|
204
|
+
onEvent: tagAndEmit,
|
|
205
|
+
...(options.uploadAuthor ? { author: options.uploadAuthor } : {}),
|
|
206
|
+
...(target.personalMode !== undefined
|
|
207
|
+
? { personalMode: target.personalMode }
|
|
208
|
+
: {}),
|
|
209
|
+
...(target.journalSlug !== undefined
|
|
210
|
+
? { journalSlug: target.journalSlug }
|
|
211
|
+
: {}),
|
|
212
|
+
...(decommissionPrefixes && decommissionPrefixes.length > 0
|
|
213
|
+
? { decommissionPrefixes }
|
|
214
|
+
: {}),
|
|
215
|
+
...(scope.prefixSet !== undefined
|
|
216
|
+
? { prefixSet: scope.prefixSet }
|
|
217
|
+
: {}),
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (doPull && !pushResult.aborted) {
|
|
222
|
+
activePhase = "pull";
|
|
223
|
+
const pullScope: PullScope = scope;
|
|
224
|
+
pullResult = await options.syncFn({
|
|
225
|
+
company: target.uid,
|
|
226
|
+
vaultConfig: options.vaultConfig,
|
|
227
|
+
hqRoot: options.hqRoot,
|
|
228
|
+
onConflict: options.onConflict,
|
|
229
|
+
syncMode: pullScope.syncMode,
|
|
230
|
+
scopeShrinkPolicy: "auto-recover",
|
|
231
|
+
...(options.uploadAuthor?.userSub !== undefined
|
|
232
|
+
? { callerSub: options.uploadAuthor.userSub }
|
|
233
|
+
: {}),
|
|
234
|
+
...(pullScope.prefixSet !== undefined
|
|
235
|
+
? { prefixSet: pullScope.prefixSet }
|
|
236
|
+
: {}),
|
|
237
|
+
...(target.personalMode !== undefined
|
|
238
|
+
? { personalMode: target.personalMode }
|
|
239
|
+
: {}),
|
|
240
|
+
...(target.journalSlug !== undefined
|
|
241
|
+
? { journalSlug: target.journalSlug }
|
|
242
|
+
: {}),
|
|
243
|
+
...(target.personalMode === true
|
|
244
|
+
? {
|
|
245
|
+
includeLocalCompanies,
|
|
246
|
+
teamSyncedSlugs,
|
|
247
|
+
}
|
|
248
|
+
: {}),
|
|
249
|
+
...(options.operationLockAlreadyHeld
|
|
250
|
+
? { operationLockAlreadyHeld: true }
|
|
251
|
+
: {}),
|
|
252
|
+
onEvent: tagAndEmit,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const seenConflictPaths = new Set<string>();
|
|
257
|
+
const mergedConflictPaths: string[] = [];
|
|
258
|
+
for (const p of [
|
|
259
|
+
...pullResult.conflictPaths,
|
|
260
|
+
...pushResult.conflictPaths,
|
|
261
|
+
]) {
|
|
262
|
+
if (seenConflictPaths.has(p)) continue;
|
|
263
|
+
seenConflictPaths.add(p);
|
|
264
|
+
mergedConflictPaths.push(p);
|
|
265
|
+
}
|
|
266
|
+
const aborted = pullResult.aborted || pushResult.aborted;
|
|
267
|
+
|
|
268
|
+
state.filesDownloaded = pullResult.filesDownloaded;
|
|
269
|
+
state.bytesDownloaded = pullResult.bytesDownloaded;
|
|
270
|
+
state.filesUploaded = pushResult.filesUploaded;
|
|
271
|
+
state.bytesUploaded = pushResult.bytesUploaded;
|
|
272
|
+
state.status = companyHadTransferError
|
|
273
|
+
? "errored"
|
|
274
|
+
: aborted
|
|
275
|
+
? "aborted"
|
|
276
|
+
: "complete";
|
|
277
|
+
|
|
278
|
+
options.emit({
|
|
279
|
+
type: "complete",
|
|
280
|
+
company: companyLabel,
|
|
281
|
+
filesDownloaded: pullResult.filesDownloaded,
|
|
282
|
+
bytesDownloaded: pullResult.bytesDownloaded,
|
|
283
|
+
filesUploaded: pushResult.filesUploaded,
|
|
284
|
+
bytesUploaded: pushResult.bytesUploaded,
|
|
285
|
+
filesSkipped: pullResult.filesSkipped + pushResult.filesSkipped,
|
|
286
|
+
filesTombstoned:
|
|
287
|
+
pushResult.filesTombstoned + pullResult.filesTombstoned,
|
|
288
|
+
filesRefusedStale: pushResult.filesRefusedStale,
|
|
289
|
+
filesRefusedStalePaths: pushResult.filesRefusedStalePaths,
|
|
290
|
+
filesExcludedByPolicy:
|
|
291
|
+
pushResult.filesExcludedByPolicy +
|
|
292
|
+
pullResult.filesExcludedByPolicy,
|
|
293
|
+
conflicts: mergedConflictPaths.length,
|
|
294
|
+
conflictPaths: mergedConflictPaths,
|
|
295
|
+
aborted,
|
|
296
|
+
newFiles: pullResult.newFiles,
|
|
297
|
+
newFilesCount: pullResult.newFilesCount,
|
|
298
|
+
filesOutOfScope: pullResult.filesOutOfScope,
|
|
299
|
+
scopeOrphansRemoved: pullResult.scopeOrphansRemoved,
|
|
300
|
+
scopeOrphansBlocked: pullResult.scopeOrphansBlocked,
|
|
301
|
+
});
|
|
302
|
+
for (const p of pullResult.conflictPaths) {
|
|
303
|
+
allConflicts.push({
|
|
304
|
+
company: companyLabel,
|
|
305
|
+
path: p,
|
|
306
|
+
direction: "pull",
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
for (const p of pushResult.conflictPaths) {
|
|
310
|
+
allConflicts.push({
|
|
311
|
+
company: companyLabel,
|
|
312
|
+
path: p,
|
|
313
|
+
direction: "push",
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
} catch (err) {
|
|
317
|
+
const message = describeError(err);
|
|
318
|
+
errors.push({ company: companyLabel, message });
|
|
319
|
+
options.emit({
|
|
320
|
+
type: "complete",
|
|
321
|
+
company: companyLabel,
|
|
322
|
+
filesDownloaded: state.filesDownloaded,
|
|
323
|
+
bytesDownloaded: state.bytesDownloaded,
|
|
324
|
+
filesUploaded: state.filesUploaded,
|
|
325
|
+
bytesUploaded: state.bytesUploaded,
|
|
326
|
+
filesSkipped: 0,
|
|
327
|
+
filesTombstoned: 0,
|
|
328
|
+
filesRefusedStale: 0,
|
|
329
|
+
filesRefusedStalePaths: [],
|
|
330
|
+
filesExcludedByPolicy: 0,
|
|
331
|
+
conflicts: 0,
|
|
332
|
+
conflictPaths: [],
|
|
333
|
+
aborted: true,
|
|
334
|
+
newFiles: [],
|
|
335
|
+
newFilesCount: 0,
|
|
336
|
+
filesOutOfScope: 0,
|
|
337
|
+
scopeOrphansRemoved: 0,
|
|
338
|
+
scopeOrphansBlocked: 0,
|
|
339
|
+
});
|
|
340
|
+
options.emit({
|
|
341
|
+
type: "error",
|
|
342
|
+
company: companyLabel,
|
|
343
|
+
path: "(company)",
|
|
344
|
+
message,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return { stateByCompany, errors, allConflicts };
|
|
350
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { RunnerEvent } from "./sync-runner.js";
|
|
2
|
+
|
|
3
|
+
export interface RunnerEventStreams {
|
|
4
|
+
stdout: { write: (chunk: string) => boolean | void };
|
|
5
|
+
stderr: { write: (chunk: string) => boolean | void };
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const ERROR_TYPES: ReadonlySet<RunnerEvent["type"]> = new Set([
|
|
9
|
+
"error",
|
|
10
|
+
"auth-error",
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
export function routeEvents(
|
|
14
|
+
event: RunnerEvent,
|
|
15
|
+
streams: RunnerEventStreams,
|
|
16
|
+
): void {
|
|
17
|
+
const stream = ERROR_TYPES.has(event.type) ? streams.stderr : streams.stdout;
|
|
18
|
+
stream.write(`${JSON.stringify(event)}\n`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function createRunnerEmitter(
|
|
22
|
+
streams: RunnerEventStreams,
|
|
23
|
+
): (event: RunnerEvent) => void {
|
|
24
|
+
return (event) => routeEvents(event, streams);
|
|
25
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { Membership } from "../index.js";
|
|
2
|
+
import { pickCanonicalPersonEntity } from "../vault-client.js";
|
|
3
|
+
import { PERSONAL_VAULT_JOURNAL_SLUG } from "../journal.js";
|
|
4
|
+
import type { RunnerEvent, VaultClientSurface } from "./sync-runner.js";
|
|
5
|
+
|
|
6
|
+
interface IdentityClaims {
|
|
7
|
+
sub?: string;
|
|
8
|
+
email?: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
given_name?: string;
|
|
11
|
+
family_name?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface RunnerTarget {
|
|
15
|
+
uid: string;
|
|
16
|
+
slug: string;
|
|
17
|
+
name?: string;
|
|
18
|
+
bucketName?: string;
|
|
19
|
+
personalMode?: boolean;
|
|
20
|
+
journalSlug?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type MembershipResolution =
|
|
24
|
+
| { status: "setup-needed" }
|
|
25
|
+
| { status: "memberships"; memberships: Pick<Membership, "companyUid">[] };
|
|
26
|
+
|
|
27
|
+
export async function resolveMembershipsForRun(options: {
|
|
28
|
+
personal: boolean;
|
|
29
|
+
companies: boolean;
|
|
30
|
+
company?: string;
|
|
31
|
+
client: VaultClientSurface;
|
|
32
|
+
claims: IdentityClaims | null;
|
|
33
|
+
stderr: { write: (chunk: string) => boolean | void };
|
|
34
|
+
runClaimDance: (
|
|
35
|
+
client: VaultClientSurface,
|
|
36
|
+
claims: IdentityClaims,
|
|
37
|
+
stderr: { write: (chunk: string) => boolean | void },
|
|
38
|
+
) => Promise<void>;
|
|
39
|
+
listMemberships: (
|
|
40
|
+
client: VaultClientSurface,
|
|
41
|
+
) => Promise<Pick<Membership, "companyUid">[]>;
|
|
42
|
+
}): Promise<MembershipResolution> {
|
|
43
|
+
if (options.personal) {
|
|
44
|
+
return { status: "memberships", memberships: [] };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (options.companies) {
|
|
48
|
+
if (options.claims) {
|
|
49
|
+
await options.runClaimDance(
|
|
50
|
+
options.client,
|
|
51
|
+
options.claims,
|
|
52
|
+
options.stderr,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const memberships = await options.listMemberships(options.client);
|
|
57
|
+
if (memberships.length === 0) {
|
|
58
|
+
return { status: "setup-needed" };
|
|
59
|
+
}
|
|
60
|
+
return { status: "memberships", memberships };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
status: "memberships",
|
|
65
|
+
memberships: [{ companyUid: options.company! }],
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function buildFanoutPlan(options: {
|
|
70
|
+
memberships: Pick<Membership, "companyUid">[];
|
|
71
|
+
companies: boolean;
|
|
72
|
+
personal: boolean;
|
|
73
|
+
skipPersonal: boolean;
|
|
74
|
+
client: VaultClientSurface;
|
|
75
|
+
resolveSkipPersonal: (flag: boolean) => boolean;
|
|
76
|
+
}): Promise<
|
|
77
|
+
| { status: "setup-needed" }
|
|
78
|
+
| { status: "plan"; plan: RunnerTarget[] }
|
|
79
|
+
> {
|
|
80
|
+
const plan: RunnerTarget[] = [];
|
|
81
|
+
for (const m of options.memberships) {
|
|
82
|
+
let slug = m.companyUid;
|
|
83
|
+
let name: string | undefined;
|
|
84
|
+
try {
|
|
85
|
+
const info = await options.client.entity.get(m.companyUid);
|
|
86
|
+
slug = info.slug || m.companyUid;
|
|
87
|
+
name = info.name;
|
|
88
|
+
} catch {
|
|
89
|
+
// Best-effort — keep UID as the display identifier.
|
|
90
|
+
}
|
|
91
|
+
plan.push({ uid: m.companyUid, slug, ...(name ? { name } : {}) });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (
|
|
95
|
+
(options.companies || options.personal) &&
|
|
96
|
+
!options.resolveSkipPersonal(options.skipPersonal)
|
|
97
|
+
) {
|
|
98
|
+
const persons = await options.client.entity.listByType("person");
|
|
99
|
+
const pick = pickCanonicalPersonEntity(persons);
|
|
100
|
+
if (pick?.bucketName) {
|
|
101
|
+
plan.push({
|
|
102
|
+
slug: "personal",
|
|
103
|
+
uid: pick.uid,
|
|
104
|
+
bucketName: pick.bucketName,
|
|
105
|
+
personalMode: true,
|
|
106
|
+
journalSlug: PERSONAL_VAULT_JOURNAL_SLUG,
|
|
107
|
+
});
|
|
108
|
+
} else if (options.personal) {
|
|
109
|
+
return { status: "setup-needed" };
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return { status: "plan", plan };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function emitFanoutPlan(
|
|
117
|
+
emit: (event: RunnerEvent) => void,
|
|
118
|
+
plan: RunnerTarget[],
|
|
119
|
+
): void {
|
|
120
|
+
emit({ type: "fanout-plan", companies: plan });
|
|
121
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { RunnerEvent } from "./sync-runner.js";
|
|
2
|
+
import type { RunnerTarget } from "./sync-runner-planning.js";
|
|
3
|
+
|
|
4
|
+
export type CompanyStatus = "complete" | "aborted" | "errored";
|
|
5
|
+
|
|
6
|
+
export interface CompanyState {
|
|
7
|
+
company: string;
|
|
8
|
+
status: CompanyStatus;
|
|
9
|
+
filesDownloaded: number;
|
|
10
|
+
bytesDownloaded: number;
|
|
11
|
+
filesUploaded: number;
|
|
12
|
+
bytesUploaded: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface AllCompleteRollup {
|
|
16
|
+
totalDownloaded: number;
|
|
17
|
+
totalDownloadedBytes: number;
|
|
18
|
+
totalUploaded: number;
|
|
19
|
+
totalUploadedBytes: number;
|
|
20
|
+
partial: boolean;
|
|
21
|
+
companies: Extract<RunnerEvent, { type: "all-complete" }>["companies"];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createCompanyState(company: string): CompanyState {
|
|
25
|
+
return {
|
|
26
|
+
company,
|
|
27
|
+
status: "errored",
|
|
28
|
+
filesDownloaded: 0,
|
|
29
|
+
bytesDownloaded: 0,
|
|
30
|
+
filesUploaded: 0,
|
|
31
|
+
bytesUploaded: 0,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function rollupAllComplete(
|
|
36
|
+
plan: RunnerTarget[],
|
|
37
|
+
stateByCompany: Map<string, CompanyState>,
|
|
38
|
+
): AllCompleteRollup {
|
|
39
|
+
let totalDownloaded = 0;
|
|
40
|
+
let totalDownloadedBytes = 0;
|
|
41
|
+
let totalUploaded = 0;
|
|
42
|
+
let totalUploadedBytes = 0;
|
|
43
|
+
let partial = false;
|
|
44
|
+
const companies: AllCompleteRollup["companies"] = [];
|
|
45
|
+
|
|
46
|
+
for (const target of plan) {
|
|
47
|
+
const s = stateByCompany.get(target.slug);
|
|
48
|
+
if (!s) continue;
|
|
49
|
+
totalDownloaded += s.filesDownloaded;
|
|
50
|
+
totalDownloadedBytes += s.bytesDownloaded;
|
|
51
|
+
totalUploaded += s.filesUploaded;
|
|
52
|
+
totalUploadedBytes += s.bytesUploaded;
|
|
53
|
+
if (s.status !== "complete") partial = true;
|
|
54
|
+
companies.push({
|
|
55
|
+
company: s.company,
|
|
56
|
+
status: s.status,
|
|
57
|
+
filesDownloaded: s.filesDownloaded,
|
|
58
|
+
bytesDownloaded: s.bytesDownloaded,
|
|
59
|
+
filesUploaded: s.filesUploaded,
|
|
60
|
+
bytesUploaded: s.bytesUploaded,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
totalDownloaded,
|
|
66
|
+
totalDownloadedBytes,
|
|
67
|
+
totalUploaded,
|
|
68
|
+
totalUploadedBytes,
|
|
69
|
+
partial,
|
|
70
|
+
companies,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export async function emitTelemetry(options: {
|
|
2
|
+
collectTelemetry?: () => Promise<void>;
|
|
3
|
+
defaultCollectTelemetry: () => Promise<void>;
|
|
4
|
+
}): Promise<void> {
|
|
5
|
+
const telemetryFn =
|
|
6
|
+
options.collectTelemetry ?? options.defaultCollectTelemetry;
|
|
7
|
+
await telemetryFn().catch(() => undefined);
|
|
8
|
+
}
|