@open-workspace/create 0.4.1 → 0.5.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/README.md +9 -5
- package/dist/backup-schema.ts +110 -0
- package/dist/workspace.ts +162 -0
- package/import/backup-snapshot.ts +254 -0
- package/import/backup-source.ts +270 -0
- package/import/cli.ts +134 -90
- package/import/concurrency.ts +34 -37
- package/import/csv.ts +1 -1
- package/import/directory/command.ts +293 -72
- package/import/directory/google.ts +158 -120
- package/import/directory/invitation-file.ts +108 -0
- package/import/directory/plan.ts +314 -38
- package/import/directory/read.ts +55 -0
- package/import/gmail.ts +9 -12
- package/import/headers.ts +1 -1
- package/import/idp/apps.ts +244 -0
- package/import/idp/command.ts +161 -0
- package/import/idp/csv.ts +70 -0
- package/import/idp/entra.ts +297 -0
- package/import/idp/keycloak.ts +250 -0
- package/import/idp/model.ts +108 -0
- package/import/idp/okta.ts +159 -0
- package/import/idp/pages.ts +65 -0
- package/import/imap-command.ts +42 -18
- package/import/imap-source.ts +245 -220
- package/import/invariant.ts +1 -1
- package/import/loopback.ts +136 -0
- package/import/mbox-source.ts +106 -55
- package/import/mbox.ts +1 -1
- package/import/oauth.ts +234 -138
- package/import/preview.ts +48 -0
- package/import/uploader.ts +169 -115
- package/import/workspace.ts +78 -15
- package/index.ts +3 -0
- package/module-templates.ts +9 -12
- package/module.ts +1 -5
- package/package.json +13 -2
- package/setup/alchemy.ts +2 -2
- package/setup/args.ts +16 -17
- package/setup/cloudflare-api.ts +75 -19
- package/setup/domains.ts +18 -6
- package/setup/flow.ts +217 -58
- package/setup/migration-guide.ts +255 -0
- package/setup/mx-recovery.ts +115 -0
- package/setup/ui.ts +4 -1
- package/templates.ts +26 -18
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
|
|
3
3
|
import { forEachConcurrent } from "../concurrency.ts";
|
|
4
|
+
import { loopbackSignIn } from "../loopback.ts";
|
|
4
5
|
import type { Fetch } from "../oauth.ts";
|
|
5
6
|
import type { Directory, DirectoryGroup, DirectoryUser } from "./plan.ts";
|
|
7
|
+
import { readDirectoryPage } from "./read.ts";
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
10
|
* Reading a Google Workspace directory with the Admin SDK Directory API,
|
|
@@ -27,6 +29,9 @@ const AUTH_URL = "https://accounts.google.com/o/oauth2/v2/auth";
|
|
|
27
29
|
const DIRECTORY = "https://admin.googleapis.com/admin/directory/v1";
|
|
28
30
|
const GROUP_SETTINGS = "https://www.googleapis.com/groups/v1/groups";
|
|
29
31
|
|
|
32
|
+
/** What Google names itself in an authorization response (RFC 9207). */
|
|
33
|
+
const GOOGLE_ISSUER = "https://accounts.google.com";
|
|
34
|
+
|
|
30
35
|
const serviceAccountKeySchema = z.object({
|
|
31
36
|
type: z.literal("service_account"),
|
|
32
37
|
client_email: z.string(),
|
|
@@ -139,90 +144,39 @@ async function installedClientToken(
|
|
|
139
144
|
openBrowser: (url: string) => void | Promise<void>,
|
|
140
145
|
log: (message: string) => void
|
|
141
146
|
) {
|
|
142
|
-
const verifier =
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
const server = Bun.serve({
|
|
158
|
-
hostname: "127.0.0.1",
|
|
159
|
-
port: 0,
|
|
160
|
-
fetch(request) {
|
|
161
|
-
const url = new URL(request.url);
|
|
162
|
-
|
|
163
|
-
if (url.pathname !== "/") {
|
|
164
|
-
return new Response("Not found", { status: 404 });
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
receive(url.searchParams);
|
|
168
|
-
|
|
169
|
-
return new Response("Signed in to Google. You can close this tab.", {
|
|
170
|
-
headers: { "content-type": "text/plain" },
|
|
171
|
-
});
|
|
147
|
+
const { code, redirectUri, verifier } = await loopbackSignIn({
|
|
148
|
+
what: "Google sign-in",
|
|
149
|
+
path: "/",
|
|
150
|
+
issuer: GOOGLE_ISSUER,
|
|
151
|
+
prompt: "Sign in to Google as an administrator:",
|
|
152
|
+
openBrowser,
|
|
153
|
+
log,
|
|
154
|
+
authorizeUrl: () => {
|
|
155
|
+
const url = new URL(AUTH_URL);
|
|
156
|
+
url.searchParams.set("client_id", client.client_id);
|
|
157
|
+
url.searchParams.set("scope", scopes.join(" "));
|
|
158
|
+
|
|
159
|
+
return Promise.resolve(url);
|
|
172
160
|
},
|
|
173
161
|
});
|
|
174
162
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
response_type: "code",
|
|
183
|
-
scope: scopes.join(" "),
|
|
184
|
-
code_challenge: challenge,
|
|
185
|
-
code_challenge_method: "S256",
|
|
186
|
-
state,
|
|
187
|
-
})) {
|
|
188
|
-
url.searchParams.set(name, value);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
log(`Sign in to Google as an administrator:\n ${url}`);
|
|
192
|
-
await openBrowser(url.toString());
|
|
193
|
-
const params = await callback;
|
|
163
|
+
const form = new URLSearchParams({
|
|
164
|
+
grant_type: "authorization_code",
|
|
165
|
+
code,
|
|
166
|
+
redirect_uri: redirectUri,
|
|
167
|
+
client_id: client.client_id,
|
|
168
|
+
code_verifier: verifier,
|
|
169
|
+
});
|
|
194
170
|
|
|
195
|
-
|
|
171
|
+
if (client.client_secret) form.set("client_secret", client.client_secret);
|
|
196
172
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
173
|
+
const response = await fetchImpl(TOKEN_URL, {
|
|
174
|
+
method: "POST",
|
|
175
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
176
|
+
body: form,
|
|
177
|
+
});
|
|
202
178
|
|
|
203
|
-
|
|
204
|
-
grant_type: "authorization_code",
|
|
205
|
-
code,
|
|
206
|
-
redirect_uri: redirectUri,
|
|
207
|
-
client_id: client.client_id,
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
if (client.client_secret) form.set("client_secret", client.client_secret);
|
|
211
|
-
form.set("code_verifier", verifier);
|
|
212
|
-
|
|
213
|
-
const response = await fetchImpl(TOKEN_URL, {
|
|
214
|
-
method: "POST",
|
|
215
|
-
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
216
|
-
body: form,
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
return accessToken(
|
|
220
|
-
response,
|
|
221
|
-
(reason) => `Google sign-in failed: ${reason}`
|
|
222
|
-
);
|
|
223
|
-
} finally {
|
|
224
|
-
server.stop(true);
|
|
225
|
-
}
|
|
179
|
+
return accessToken(response, (reason) => `Google sign-in failed: ${reason}`);
|
|
226
180
|
}
|
|
227
181
|
|
|
228
182
|
export type GoogleCredentialsOptions = {
|
|
@@ -236,9 +190,7 @@ export type GoogleCredentialsOptions = {
|
|
|
236
190
|
log: (message: string) => void;
|
|
237
191
|
};
|
|
238
192
|
|
|
239
|
-
export function googleAccessToken(
|
|
240
|
-
options: GoogleCredentialsOptions
|
|
241
|
-
): Promise<string> {
|
|
193
|
+
export function googleAccessToken(options: GoogleCredentialsOptions) {
|
|
242
194
|
const scopes = [
|
|
243
195
|
...DIRECTORY_READ_SCOPES,
|
|
244
196
|
...(options.groupSettings ? [GROUP_SETTINGS_SCOPE] : []),
|
|
@@ -282,6 +234,39 @@ export function googleAccessToken(
|
|
|
282
234
|
);
|
|
283
235
|
}
|
|
284
236
|
|
|
237
|
+
const quotaErrorSchema = z.object({
|
|
238
|
+
error: z.object({ errors: z.array(z.object({ reason: z.string() })) }),
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
async function googleQuotaExceeded(response: Response) {
|
|
242
|
+
// Keep the original body for the caller when Google actually denied access.
|
|
243
|
+
const body = await response
|
|
244
|
+
.clone()
|
|
245
|
+
.json()
|
|
246
|
+
.catch(() => null);
|
|
247
|
+
|
|
248
|
+
const parsed = quotaErrorSchema.safeParse(body);
|
|
249
|
+
|
|
250
|
+
return (
|
|
251
|
+
parsed.success &&
|
|
252
|
+
parsed.data.error.errors.some(
|
|
253
|
+
({ reason }) =>
|
|
254
|
+
reason === "userRateLimitExceeded" || reason === "quotaExceeded"
|
|
255
|
+
)
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function readGooglePage(fetchImpl: Fetch, url: string, token: string) {
|
|
260
|
+
return readDirectoryPage(
|
|
261
|
+
fetchImpl,
|
|
262
|
+
url,
|
|
263
|
+
{
|
|
264
|
+
headers: { authorization: `Bearer ${token}` },
|
|
265
|
+
},
|
|
266
|
+
googleQuotaExceeded
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
285
270
|
/** Every page of a Directory API list: `schema` reads one page, `itemsOf` its items. */
|
|
286
271
|
async function pages<Schema extends z.ZodType, Item>(
|
|
287
272
|
token: string,
|
|
@@ -290,12 +275,10 @@ async function pages<Schema extends z.ZodType, Item>(
|
|
|
290
275
|
itemsOf: (page: z.output<Schema>) => Item[] | undefined,
|
|
291
276
|
fetchImpl: Fetch,
|
|
292
277
|
pageToken?: string
|
|
293
|
-
)
|
|
278
|
+
) {
|
|
294
279
|
if (pageToken) url.searchParams.set("pageToken", pageToken);
|
|
295
280
|
|
|
296
|
-
const response = await fetchImpl
|
|
297
|
-
headers: { authorization: `Bearer ${token}` },
|
|
298
|
-
});
|
|
281
|
+
const response = await readGooglePage(fetchImpl, url.toString(), token);
|
|
299
282
|
|
|
300
283
|
const json = await response.json();
|
|
301
284
|
|
|
@@ -321,10 +304,16 @@ async function pages<Schema extends z.ZodType, Item>(
|
|
|
321
304
|
if (!nextPageToken) return items;
|
|
322
305
|
|
|
323
306
|
// Each page names the next one, so they are read one after another.
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
307
|
+
const rest: Item[] = await pages(
|
|
308
|
+
token,
|
|
309
|
+
url,
|
|
310
|
+
schema,
|
|
311
|
+
itemsOf,
|
|
312
|
+
fetchImpl,
|
|
313
|
+
nextPageToken
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
return [...items, ...rest];
|
|
328
317
|
}
|
|
329
318
|
|
|
330
319
|
function unexpectedAnswer(url: URL, error: z.ZodError) {
|
|
@@ -339,10 +328,28 @@ const directoryErrorSchema = z.object({
|
|
|
339
328
|
|
|
340
329
|
const nextPageSchema = z.object({ nextPageToken: z.string().optional() });
|
|
341
330
|
|
|
331
|
+
const aliasFields = {
|
|
332
|
+
aliases: z.array(z.string()).optional(),
|
|
333
|
+
nonEditableAliases: z.array(z.string()).optional(),
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
function googleAliases(row: {
|
|
337
|
+
aliases?: string[];
|
|
338
|
+
nonEditableAliases?: string[];
|
|
339
|
+
}) {
|
|
340
|
+
return [
|
|
341
|
+
...new Set(
|
|
342
|
+
[...(row.aliases ?? []), ...(row.nonEditableAliases ?? [])].map(
|
|
343
|
+
(address) => address.toLowerCase()
|
|
344
|
+
)
|
|
345
|
+
),
|
|
346
|
+
];
|
|
347
|
+
}
|
|
348
|
+
|
|
342
349
|
const googleUserSchema = z.object({
|
|
343
350
|
primaryEmail: z.string(),
|
|
344
351
|
name: z.object({ fullName: z.string().optional() }).optional(),
|
|
345
|
-
|
|
352
|
+
...aliasFields,
|
|
346
353
|
suspended: z.boolean().optional(),
|
|
347
354
|
archived: z.boolean().optional(),
|
|
348
355
|
isAdmin: z.boolean().optional(),
|
|
@@ -353,10 +360,11 @@ const usersPageSchema = z.object({
|
|
|
353
360
|
});
|
|
354
361
|
|
|
355
362
|
const googleGroupSchema = z.object({
|
|
363
|
+
id: z.string().optional(),
|
|
356
364
|
email: z.string(),
|
|
357
365
|
name: z.string().optional(),
|
|
358
366
|
description: z.string().optional(),
|
|
359
|
-
|
|
367
|
+
...aliasFields,
|
|
360
368
|
});
|
|
361
369
|
|
|
362
370
|
const groupsPageSchema = z.object({
|
|
@@ -375,19 +383,26 @@ const membersPageSchema = z.object({
|
|
|
375
383
|
|
|
376
384
|
/** The Groups Settings API's answer, the parts read here (its booleans are strings). */
|
|
377
385
|
const groupSettingsSchema = z.object({
|
|
378
|
-
whoCanPostMessage: z.string()
|
|
379
|
-
|
|
386
|
+
whoCanPostMessage: z.string(),
|
|
387
|
+
messageModerationLevel: z.string().min(1),
|
|
388
|
+
membersCanPostAsTheGroup: z.enum(["true", "false"]),
|
|
389
|
+
enableCollaborativeInbox: z.enum(["true", "false"]),
|
|
380
390
|
});
|
|
381
391
|
|
|
382
|
-
type GroupSettings =
|
|
392
|
+
type GroupSettings = Pick<
|
|
393
|
+
DirectoryGroup,
|
|
394
|
+
| "sharedInbox"
|
|
395
|
+
| "posting"
|
|
396
|
+
| "membersCanSend"
|
|
397
|
+
| "mailboxSkipReason"
|
|
398
|
+
| "mailboxReadError"
|
|
399
|
+
>;
|
|
383
400
|
|
|
384
401
|
/** Directory API requests in flight while reading groups; well under Google's per-user quota. */
|
|
385
402
|
const GROUP_CONCURRENCY = 4;
|
|
386
403
|
|
|
387
404
|
/** What a group's whoCanPostMessage becomes. */
|
|
388
|
-
export function postingPolicy(
|
|
389
|
-
whoCanPostMessage: string | undefined
|
|
390
|
-
): DirectoryGroup["posting"] {
|
|
405
|
+
export function postingPolicy(whoCanPostMessage: string | undefined) {
|
|
391
406
|
switch (whoCanPostMessage) {
|
|
392
407
|
case "ANYONE_CAN_POST": {
|
|
393
408
|
return "anyone";
|
|
@@ -397,10 +412,7 @@ export function postingPolicy(
|
|
|
397
412
|
return "workspace";
|
|
398
413
|
}
|
|
399
414
|
|
|
400
|
-
case "ALL_MEMBERS_CAN_POST":
|
|
401
|
-
case "ALL_MANAGERS_CAN_POST":
|
|
402
|
-
case "ALL_OWNERS_CAN_POST":
|
|
403
|
-
case "NONE_CAN_POST": {
|
|
415
|
+
case "ALL_MEMBERS_CAN_POST": {
|
|
404
416
|
return "members";
|
|
405
417
|
}
|
|
406
418
|
|
|
@@ -414,14 +426,14 @@ export function postingPolicy(
|
|
|
414
426
|
export async function readGoogleDirectory(
|
|
415
427
|
token: string,
|
|
416
428
|
options: { customer?: string; groupSettings: boolean; fetch: Fetch }
|
|
417
|
-
)
|
|
429
|
+
) {
|
|
418
430
|
const customer = options.customer ?? "my_customer";
|
|
419
431
|
const usersUrl = new URL(`${DIRECTORY}/users`);
|
|
420
432
|
usersUrl.searchParams.set("customer", customer);
|
|
421
433
|
usersUrl.searchParams.set("maxResults", "500");
|
|
422
434
|
usersUrl.searchParams.set("projection", "basic");
|
|
423
435
|
|
|
424
|
-
const users = (
|
|
436
|
+
const users: DirectoryUser[] = (
|
|
425
437
|
await pages(
|
|
426
438
|
token,
|
|
427
439
|
usersUrl,
|
|
@@ -429,10 +441,10 @@ export async function readGoogleDirectory(
|
|
|
429
441
|
(page) => page.users,
|
|
430
442
|
options.fetch
|
|
431
443
|
)
|
|
432
|
-
).map((user)
|
|
444
|
+
).map((user) => ({
|
|
433
445
|
email: user.primaryEmail,
|
|
434
446
|
name: user.name?.fullName ?? user.primaryEmail,
|
|
435
|
-
aliases: user
|
|
447
|
+
aliases: googleAliases(user),
|
|
436
448
|
suspended: Boolean(user.suspended || user.archived),
|
|
437
449
|
isAdmin: Boolean(user.isAdmin),
|
|
438
450
|
}));
|
|
@@ -449,20 +461,46 @@ export async function readGoogleDirectory(
|
|
|
449
461
|
options.fetch
|
|
450
462
|
);
|
|
451
463
|
|
|
452
|
-
const
|
|
453
|
-
|
|
464
|
+
const defaults: GroupSettings = {
|
|
465
|
+
sharedInbox: false,
|
|
466
|
+
posting: null,
|
|
467
|
+
membersCanSend: null,
|
|
468
|
+
};
|
|
454
469
|
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
470
|
+
const groupSettings = async (email: string) => {
|
|
471
|
+
if (!options.groupSettings) return defaults;
|
|
472
|
+
|
|
473
|
+
try {
|
|
474
|
+
const response = await readGooglePage(
|
|
475
|
+
options.fetch,
|
|
476
|
+
`${GROUP_SETTINGS}/${encodeURIComponent(email)}?alt=json`,
|
|
477
|
+
token
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
481
|
+
const settings = groupSettingsSchema.parse(await response.json());
|
|
482
|
+
const posting = postingPolicy(settings.whoCanPostMessage);
|
|
461
483
|
|
|
462
|
-
|
|
463
|
-
|
|
484
|
+
let mailboxSkipReason: string | undefined;
|
|
485
|
+
|
|
486
|
+
if (!posting) {
|
|
487
|
+
mailboxSkipReason = `unsupported Google posting policy: ${settings.whoCanPostMessage}`;
|
|
488
|
+
} else if (settings.messageModerationLevel !== "MODERATE_NONE") {
|
|
489
|
+
mailboxSkipReason = `Google requires message moderation (${settings.messageModerationLevel})`;
|
|
490
|
+
}
|
|
464
491
|
|
|
465
|
-
|
|
492
|
+
return {
|
|
493
|
+
sharedInbox: settings.enableCollaborativeInbox === "true",
|
|
494
|
+
posting,
|
|
495
|
+
membersCanSend: settings.membersCanPostAsTheGroup === "true",
|
|
496
|
+
mailboxSkipReason,
|
|
497
|
+
} satisfies GroupSettings;
|
|
498
|
+
} catch (error) {
|
|
499
|
+
return {
|
|
500
|
+
...defaults,
|
|
501
|
+
mailboxReadError: `could not read Google group settings: ${error instanceof Error ? error.message : String(error)}`,
|
|
502
|
+
};
|
|
503
|
+
}
|
|
466
504
|
};
|
|
467
505
|
|
|
468
506
|
// Groups are independent of each other: read a few at a time, kept in Google's order.
|
|
@@ -490,21 +528,21 @@ export async function readGoogleDirectory(
|
|
|
490
528
|
]);
|
|
491
529
|
|
|
492
530
|
groups[index] = {
|
|
531
|
+
sourceId: group.id ? `google:${group.id}` : undefined,
|
|
493
532
|
email: group.email,
|
|
494
533
|
name: group.name ?? group.email,
|
|
495
534
|
description: group.description ?? null,
|
|
496
|
-
aliases: group
|
|
535
|
+
aliases: googleAliases(group),
|
|
497
536
|
members: members.flatMap((member) => {
|
|
498
537
|
if (member.status === "SUSPENDED") return [];
|
|
499
538
|
const address = member.type === "CUSTOMER" ? "*" : member.email;
|
|
500
539
|
|
|
501
540
|
return address ? [address] : [];
|
|
502
541
|
}),
|
|
503
|
-
|
|
504
|
-
posting: postingPolicy(settings.whoCanPostMessage),
|
|
542
|
+
...settings,
|
|
505
543
|
};
|
|
506
544
|
}
|
|
507
545
|
);
|
|
508
546
|
|
|
509
|
-
return { users, groups };
|
|
547
|
+
return { users, groups } satisfies Directory;
|
|
510
548
|
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import {
|
|
2
|
+
appendFileSync,
|
|
3
|
+
chmodSync,
|
|
4
|
+
existsSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
statSync,
|
|
7
|
+
unlinkSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
} from "node:fs";
|
|
10
|
+
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
|
|
13
|
+
import { parseCsv, toCsv } from "../csv.ts";
|
|
14
|
+
|
|
15
|
+
const header = "name,email,invitation_url,expires_at\n";
|
|
16
|
+
|
|
17
|
+
const pendingSchema = z.object({
|
|
18
|
+
origin: z.string(),
|
|
19
|
+
name: z.string(),
|
|
20
|
+
email: z.string(),
|
|
21
|
+
pid: z.number().int().positive(),
|
|
22
|
+
offset: z.number().int().nonnegative(),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/** Keep the one in-flight invitation identifiable if the CLI stops after the server creates it. */
|
|
26
|
+
export function invitationFile(path: string, origin: string) {
|
|
27
|
+
const marker = `${path}.pending`;
|
|
28
|
+
|
|
29
|
+
if (!existsSync(path)) {
|
|
30
|
+
writeFileSync(path, header, { flag: "wx", mode: 0o600 });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const contents = readFileSync(path, "utf-8");
|
|
34
|
+
|
|
35
|
+
if (!contents.startsWith(header) || !contents.endsWith("\n")) {
|
|
36
|
+
throw new Error(`Invalid invitation CSV: ${path}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
chmodSync(path, 0o600);
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
pending() {
|
|
43
|
+
if (!existsSync(marker)) return undefined;
|
|
44
|
+
|
|
45
|
+
const pending = pendingSchema.parse(
|
|
46
|
+
JSON.parse(readFileSync(marker, "utf-8"))
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
if (pending.pid !== process.pid) {
|
|
50
|
+
try {
|
|
51
|
+
// Signal 0 only checks that the CLI recording this invitation is still running.
|
|
52
|
+
process.kill(pending.pid, 0);
|
|
53
|
+
throw new Error(`Another import is writing ${path}.`);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
if (
|
|
56
|
+
!(
|
|
57
|
+
error instanceof Error &&
|
|
58
|
+
"code" in error &&
|
|
59
|
+
error.code === "ESRCH"
|
|
60
|
+
)
|
|
61
|
+
) {
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (pending.origin !== origin) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Finish the pending import for ${pending.origin} or choose a different --invites file.`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// The process can also stop after appending the row but before clearing the marker.
|
|
74
|
+
const saved = parseCsv(
|
|
75
|
+
Buffer.from(contents).subarray(pending.offset).toString()
|
|
76
|
+
).some((row) => row[1] === pending.email && row[2]);
|
|
77
|
+
|
|
78
|
+
if (!saved) return pending;
|
|
79
|
+
unlinkSync(marker);
|
|
80
|
+
|
|
81
|
+
return undefined;
|
|
82
|
+
},
|
|
83
|
+
begin(name: string, email: string) {
|
|
84
|
+
writeFileSync(
|
|
85
|
+
marker,
|
|
86
|
+
JSON.stringify({
|
|
87
|
+
origin,
|
|
88
|
+
name,
|
|
89
|
+
email,
|
|
90
|
+
pid: process.pid,
|
|
91
|
+
offset: statSync(path).size,
|
|
92
|
+
}),
|
|
93
|
+
{ flag: "wx", mode: 0o600 }
|
|
94
|
+
);
|
|
95
|
+
},
|
|
96
|
+
finish(
|
|
97
|
+
name: string,
|
|
98
|
+
email: string,
|
|
99
|
+
link: { url: string; expiresAt: string }
|
|
100
|
+
) {
|
|
101
|
+
appendFileSync(path, toCsv([[name, email, link.url, link.expiresAt]]));
|
|
102
|
+
unlinkSync(marker);
|
|
103
|
+
},
|
|
104
|
+
clear() {
|
|
105
|
+
unlinkSync(marker);
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|