@open-workspace/create 0.4.2 → 0.5.1
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 +302 -72
- package/import/directory/google.ts +159 -121
- 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 +167 -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 +161 -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 +8 -8
- 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
package/import/imap-source.ts
CHANGED
|
@@ -44,8 +44,8 @@ const SKIPPED_USES = new Set(["\\Drafts", "\\All", "\\Flagged", "\\Important"]);
|
|
|
44
44
|
export function folderPlans(
|
|
45
45
|
folders: Pick<ListResponse, "path" | "delimiter" | "flags" | "specialUse">[],
|
|
46
46
|
options: GmailOptions
|
|
47
|
-
)
|
|
48
|
-
return folders.flatMap((folder)
|
|
47
|
+
) {
|
|
48
|
+
return folders.flatMap<FolderPlan>((folder) => {
|
|
49
49
|
if (folder.flags?.has("\\Noselect") || folder.flags?.has("\\NonExistent")) {
|
|
50
50
|
return [];
|
|
51
51
|
}
|
|
@@ -58,7 +58,15 @@ export function folderPlans(
|
|
|
58
58
|
|
|
59
59
|
switch (use) {
|
|
60
60
|
case "\\Inbox": {
|
|
61
|
-
|
|
61
|
+
// A self-addressed message also has a distinct outgoing copy in Sent.
|
|
62
|
+
return [
|
|
63
|
+
{
|
|
64
|
+
path: folder.path,
|
|
65
|
+
placement: "inbox",
|
|
66
|
+
folder: null,
|
|
67
|
+
direction: "incoming",
|
|
68
|
+
},
|
|
69
|
+
];
|
|
62
70
|
}
|
|
63
71
|
|
|
64
72
|
case "\\Sent": {
|
|
@@ -117,268 +125,248 @@ function labelsOf(message: FetchMessageObject) {
|
|
|
117
125
|
return [...(message.labels ?? [])];
|
|
118
126
|
}
|
|
119
127
|
|
|
120
|
-
|
|
121
|
-
gmail: boolean;
|
|
122
|
-
/** Messages to go through (after skipping folders), counted before any body is fetched. */
|
|
123
|
-
total: number;
|
|
124
|
-
items: AsyncIterable<ImportItem | SkippedItem>;
|
|
125
|
-
};
|
|
128
|
+
type PlannedMessage = Omit<ImportItem, "raw">;
|
|
126
129
|
|
|
127
130
|
type Work = {
|
|
128
131
|
uid: number;
|
|
129
|
-
sourceId: string;
|
|
130
132
|
size?: number;
|
|
131
|
-
|
|
133
|
+
item: PlannedMessage;
|
|
132
134
|
};
|
|
133
135
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
136
|
+
type MailboxPlan = {
|
|
137
|
+
pending: Work[];
|
|
138
|
+
skipped: SkippedItem[];
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/** Bound a FETCH by both UID count and advertised bytes. Reserve a whole
|
|
142
|
+
* batch for a message whose size the server omitted.
|
|
143
|
+
* @yields {Work[]} Messages for one FETCH command.
|
|
144
|
+
*/
|
|
145
|
+
function* bodyBatches(work: Work[], maxCount: number) {
|
|
146
|
+
let batch: Work[] = [];
|
|
147
|
+
let bytes = 0;
|
|
148
|
+
|
|
149
|
+
for (const entry of work) {
|
|
150
|
+
const size = entry.size ?? MAX_IMPORT_MESSAGE_BYTES;
|
|
151
|
+
|
|
152
|
+
if (
|
|
153
|
+
batch.length &&
|
|
154
|
+
(batch.length >= maxCount || bytes + size > MAX_IMPORT_MESSAGE_BYTES)
|
|
155
|
+
) {
|
|
156
|
+
yield batch;
|
|
157
|
+
batch = [];
|
|
158
|
+
bytes = 0;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
batch.push(entry);
|
|
162
|
+
bytes += size;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (batch.length) yield batch;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async function* fetchBodies(client: ImapFlow, work: Work[], batchSize: number) {
|
|
169
|
+
for (const batch of bodyBatches(work, batchSize)) {
|
|
146
170
|
const byUid = new Map(batch.map((entry) => [entry.uid, entry]));
|
|
147
|
-
const fetched: (ImportItem | SkippedItem)[] = [];
|
|
148
171
|
|
|
149
|
-
|
|
150
|
-
// oxlint-disable-next-line eslint/no-await-in-loop -- one IMAP connection runs one command at a time, and the next batch is fetched only once this one is consumed
|
|
151
|
-
const messages = await client.fetchAll(
|
|
172
|
+
const messages = client.fetch(
|
|
152
173
|
batch.map((entry) => entry.uid).join(","),
|
|
153
174
|
{ uid: true, source: true },
|
|
154
175
|
{ uid: true }
|
|
155
176
|
);
|
|
156
177
|
|
|
157
|
-
|
|
178
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- each FETCH must finish before the next command on this connection
|
|
179
|
+
for await (const message of messages) {
|
|
158
180
|
const entry = byUid.get(message.uid);
|
|
181
|
+
const { source } = message;
|
|
159
182
|
|
|
160
|
-
if (!entry || !
|
|
183
|
+
if (!entry || !source) continue;
|
|
161
184
|
byUid.delete(message.uid);
|
|
162
|
-
fetched.push(
|
|
163
|
-
toItem(entry.meta, entry.sourceId, new Uint8Array(message.source))
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
185
|
|
|
167
|
-
|
|
186
|
+
const raw =
|
|
187
|
+
source.buffer instanceof ArrayBuffer
|
|
188
|
+
? new Uint8Array(source.buffer, source.byteOffset, source.byteLength)
|
|
189
|
+
: new Uint8Array(source);
|
|
190
|
+
|
|
191
|
+
yield {
|
|
192
|
+
...entry.item,
|
|
193
|
+
raw,
|
|
194
|
+
thread:
|
|
195
|
+
entry.item.thread ?? rootThreadKey(parseHeaders(headerText(raw))),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
168
198
|
|
|
169
199
|
// Gone since it was listed (expunged meanwhile).
|
|
170
200
|
for (const entry of byUid.values()) {
|
|
171
|
-
yield { sourceId: entry.sourceId, skipped: "gone" };
|
|
201
|
+
yield { sourceId: entry.item.sourceId, skipped: "gone" };
|
|
172
202
|
}
|
|
173
203
|
}
|
|
174
204
|
}
|
|
175
205
|
|
|
176
|
-
function
|
|
177
|
-
|
|
178
|
-
const done: SkippedItem[] = [];
|
|
179
|
-
const large: SkippedItem[] = [];
|
|
206
|
+
function flaggedSkip(meta: FetchMessageObject) {
|
|
207
|
+
if (meta.flags?.has("\\Deleted")) return "deleted";
|
|
180
208
|
|
|
181
|
-
|
|
182
|
-
if (options.done(entry.sourceId)) {
|
|
183
|
-
done.push({ sourceId: entry.sourceId, skipped: "done" });
|
|
184
|
-
} else if ((entry.size ?? 0) > MAX_IMPORT_MESSAGE_BYTES) {
|
|
185
|
-
large.push({ sourceId: entry.sourceId, skipped: "too large" });
|
|
186
|
-
} else pending.push(entry);
|
|
187
|
-
}
|
|
209
|
+
if (meta.flags?.has("\\Draft")) return "draft";
|
|
188
210
|
|
|
189
|
-
return
|
|
211
|
+
return null;
|
|
190
212
|
}
|
|
191
213
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
options: ImapSourceOptions
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
214
|
+
function planMessage(
|
|
215
|
+
plan: MailboxPlan,
|
|
216
|
+
meta: FetchMessageObject,
|
|
217
|
+
item: PlannedMessage,
|
|
218
|
+
options: ImapSourceOptions,
|
|
219
|
+
labelSkip?: string | null
|
|
220
|
+
) {
|
|
221
|
+
const reason =
|
|
222
|
+
flaggedSkip(meta) ??
|
|
223
|
+
(options.done(item.sourceId) ? "done" : null) ??
|
|
224
|
+
((meta.size ?? 0) > MAX_IMPORT_MESSAGE_BYTES ? "too large" : null) ??
|
|
225
|
+
labelSkip ??
|
|
226
|
+
(options.skip.has(item.placement) ? item.placement : null);
|
|
227
|
+
|
|
228
|
+
if (reason) plan.skipped.push({ sourceId: item.sourceId, skipped: reason });
|
|
229
|
+
else plan.pending.push({ uid: meta.uid, size: meta.size, item });
|
|
230
|
+
}
|
|
203
231
|
|
|
204
|
-
|
|
205
|
-
|
|
232
|
+
// A thread can acquire its folder from a later message, including a skipped one.
|
|
233
|
+
async function gmailPlan(
|
|
234
|
+
client: ImapFlow,
|
|
235
|
+
options: ImapSourceOptions,
|
|
236
|
+
uidValidity: bigint
|
|
237
|
+
) {
|
|
238
|
+
const threads = new ThreadFolders(options.folderPriority);
|
|
239
|
+
const plan: MailboxPlan = { pending: [], skipped: [] };
|
|
240
|
+
|
|
241
|
+
for await (const meta of client.fetch(
|
|
242
|
+
"1:*",
|
|
243
|
+
{
|
|
244
|
+
uid: true,
|
|
245
|
+
flags: true,
|
|
246
|
+
labels: true,
|
|
247
|
+
threadId: true,
|
|
248
|
+
emailId: true,
|
|
249
|
+
internalDate: true,
|
|
250
|
+
size: true,
|
|
251
|
+
},
|
|
252
|
+
{ uid: true }
|
|
253
|
+
)) {
|
|
254
|
+
const thread = meta.threadId ? `gmail:${meta.threadId}` : undefined;
|
|
255
|
+
const gmail = gmailMessage(labelsOf(meta), options);
|
|
256
|
+
|
|
257
|
+
if (thread && !flaggedSkip(meta)) threads.add(thread, gmail.userLabels);
|
|
258
|
+
|
|
259
|
+
planMessage(
|
|
260
|
+
plan,
|
|
261
|
+
meta,
|
|
206
262
|
{
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
263
|
+
sourceId: `gmail:${meta.emailId ?? `${options.account}:${uidValidity}:${meta.uid}`}`,
|
|
264
|
+
thread,
|
|
265
|
+
placement: gmail.placement,
|
|
266
|
+
folder:
|
|
267
|
+
!thread && gmail.placement === "inbox"
|
|
268
|
+
? threads.folder("", gmail.userLabels)
|
|
269
|
+
: null,
|
|
270
|
+
direction: gmail.sent ? "outgoing" : "incoming",
|
|
271
|
+
seen: meta.flags?.has("\\Seen") ?? false,
|
|
272
|
+
receivedAt: receivedAt(meta),
|
|
214
273
|
},
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
if (thread) {
|
|
220
|
-
threads.add(thread, gmailMessage(labelsOf(meta), options).userLabels);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
work.push({
|
|
224
|
-
uid: meta.uid,
|
|
225
|
-
size: meta.size,
|
|
226
|
-
sourceId: `gmail:${meta.emailId ?? `${options.account}:${meta.uid}`}`,
|
|
227
|
-
meta,
|
|
228
|
-
});
|
|
229
|
-
}
|
|
274
|
+
options,
|
|
275
|
+
gmail.skip
|
|
276
|
+
);
|
|
277
|
+
}
|
|
230
278
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
async function* items() {
|
|
235
|
-
// Report what earlier runs did, and what cannot be imported, before downloading.
|
|
236
|
-
yield* done;
|
|
237
|
-
yield* large;
|
|
238
|
-
|
|
239
|
-
const bodiesLock = await client.getMailboxLock(allMail, {
|
|
240
|
-
readOnly: true,
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
try {
|
|
244
|
-
yield* fetchBodies(
|
|
245
|
-
client,
|
|
246
|
-
pending,
|
|
247
|
-
options.batchSize ?? 50,
|
|
248
|
-
(meta, sourceId, raw): ImportItem | SkippedItem => {
|
|
249
|
-
const gmail = gmailMessage(labelsOf(meta), options);
|
|
250
|
-
|
|
251
|
-
if (gmail.skip) return { sourceId, skipped: gmail.skip };
|
|
252
|
-
|
|
253
|
-
if (options.skip.has(gmail.placement)) {
|
|
254
|
-
return { sourceId, skipped: gmail.placement };
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
const thread = meta.threadId
|
|
258
|
-
? `gmail:${meta.threadId}`
|
|
259
|
-
: rootThreadKey(parseHeaders(headerText(raw)));
|
|
260
|
-
|
|
261
|
-
return {
|
|
262
|
-
sourceId,
|
|
263
|
-
raw,
|
|
264
|
-
placement: gmail.placement,
|
|
265
|
-
folder:
|
|
266
|
-
gmail.placement === "inbox" && thread
|
|
267
|
-
? threads.folder(thread, gmail.userLabels)
|
|
268
|
-
: null,
|
|
269
|
-
seen: meta.flags?.has("\\Seen") ?? false,
|
|
270
|
-
thread,
|
|
271
|
-
receivedAt: receivedAt(meta),
|
|
272
|
-
direction: gmail.sent ? "outgoing" : "incoming",
|
|
273
|
-
};
|
|
274
|
-
}
|
|
275
|
-
);
|
|
276
|
-
} finally {
|
|
277
|
-
bodiesLock.release();
|
|
278
|
-
}
|
|
279
|
+
for (const { item } of plan.pending) {
|
|
280
|
+
if (item.placement === "inbox" && item.thread) {
|
|
281
|
+
item.folder = threads.folder(item.thread);
|
|
279
282
|
}
|
|
280
|
-
|
|
281
|
-
return { gmail: true, total: work.length, items: items() };
|
|
282
|
-
} finally {
|
|
283
|
-
lock.release();
|
|
284
283
|
}
|
|
284
|
+
|
|
285
|
+
return plan;
|
|
285
286
|
}
|
|
286
287
|
|
|
287
|
-
|
|
288
|
-
async function folderWork(
|
|
288
|
+
async function folderPlan(
|
|
289
289
|
client: ImapFlow,
|
|
290
|
-
|
|
291
|
-
options: ImapSourceOptions
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
{ uid: true, flags: true, internalDate: true, size: true },
|
|
304
|
-
{ uid: true }
|
|
305
|
-
);
|
|
306
|
-
|
|
307
|
-
return messages.map((meta) => ({
|
|
308
|
-
uid: meta.uid,
|
|
309
|
-
size: meta.size,
|
|
310
|
-
sourceId: `imap:${options.account}/${plan.path}/${uidValidity}/${meta.uid}`,
|
|
290
|
+
folder: FolderPlan,
|
|
291
|
+
options: ImapSourceOptions,
|
|
292
|
+
uidValidity: bigint
|
|
293
|
+
) {
|
|
294
|
+
const plan: MailboxPlan = { pending: [], skipped: [] };
|
|
295
|
+
|
|
296
|
+
for await (const meta of client.fetch(
|
|
297
|
+
"1:*",
|
|
298
|
+
{ uid: true, flags: true, internalDate: true, size: true },
|
|
299
|
+
{ uid: true }
|
|
300
|
+
)) {
|
|
301
|
+
planMessage(
|
|
302
|
+
plan,
|
|
311
303
|
meta,
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
304
|
+
{
|
|
305
|
+
sourceId: `imap:${options.account}/${folder.path}/${uidValidity}/${meta.uid}`,
|
|
306
|
+
placement: folder.placement,
|
|
307
|
+
folder: folder.folder,
|
|
308
|
+
direction: folder.direction,
|
|
309
|
+
seen: meta.flags?.has("\\Seen") ?? false,
|
|
310
|
+
receivedAt: receivedAt(meta),
|
|
311
|
+
},
|
|
312
|
+
options
|
|
313
|
+
);
|
|
315
314
|
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/** Any other server: folder by folder. */
|
|
319
|
-
async function foldersSource(
|
|
320
|
-
client: ImapFlow,
|
|
321
|
-
options: ImapSourceOptions
|
|
322
|
-
): Promise<ImapSource> {
|
|
323
|
-
const plans = folderPlans(await client.list(), options).filter(
|
|
324
|
-
(plan) => !options.skip.has(plan.placement)
|
|
325
|
-
);
|
|
326
315
|
|
|
327
|
-
|
|
328
|
-
|
|
316
|
+
return plan;
|
|
317
|
+
}
|
|
329
318
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
perFolder.push({ plan, work });
|
|
335
|
-
}
|
|
319
|
+
type Selection = {
|
|
320
|
+
path: string;
|
|
321
|
+
plan: (validity: bigint) => Promise<MailboxPlan>;
|
|
322
|
+
};
|
|
336
323
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
receivedAt: receivedAt(meta),
|
|
361
|
-
};
|
|
362
|
-
|
|
363
|
-
if (plan.direction) item.direction = plan.direction;
|
|
364
|
-
|
|
365
|
-
return item;
|
|
366
|
-
}
|
|
367
|
-
);
|
|
368
|
-
} finally {
|
|
369
|
-
lock.release();
|
|
370
|
-
}
|
|
324
|
+
// Keep metadata and bodies in the same selection: a UID may name a different
|
|
325
|
+
// message after the mailbox is recreated.
|
|
326
|
+
async function* readMailboxes<T>(
|
|
327
|
+
client: ImapFlow,
|
|
328
|
+
selections: Selection[],
|
|
329
|
+
read: (pending: Work[]) => AsyncIterable<T> | Iterable<T>
|
|
330
|
+
) {
|
|
331
|
+
for (const selection of selections) {
|
|
332
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- one selected mailbox per IMAP connection
|
|
333
|
+
const lock = await client.getMailboxLock(selection.path, {
|
|
334
|
+
readOnly: true,
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
try {
|
|
338
|
+
const { mailbox } = client;
|
|
339
|
+
|
|
340
|
+
if (!mailbox || !mailbox.exists) continue;
|
|
341
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- UIDs belong to this selection
|
|
342
|
+
const plan = await selection.plan(mailbox.uidValidity);
|
|
343
|
+
yield* plan.skipped;
|
|
344
|
+
yield* read(plan.pending);
|
|
345
|
+
} finally {
|
|
346
|
+
lock.release();
|
|
371
347
|
}
|
|
372
348
|
}
|
|
373
|
-
|
|
374
|
-
return { gmail: false, total, items: items() };
|
|
375
349
|
}
|
|
376
350
|
|
|
377
351
|
/** The account's mail as import items, Gmail or not. */
|
|
378
|
-
export async function imapSource(
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
352
|
+
export async function imapSource(client: ImapFlow, options: ImapSourceOptions) {
|
|
353
|
+
const source = (
|
|
354
|
+
gmail: boolean,
|
|
355
|
+
total: number | null,
|
|
356
|
+
selections: Selection[]
|
|
357
|
+
) => ({
|
|
358
|
+
gmail,
|
|
359
|
+
total,
|
|
360
|
+
preview: readMailboxes(client, selections, (pending) =>
|
|
361
|
+
pending.map(({ item }) => item)
|
|
362
|
+
),
|
|
363
|
+
items: readMailboxes<ImportItem | SkippedItem>(
|
|
364
|
+
client,
|
|
365
|
+
selections,
|
|
366
|
+
(pending) => fetchBodies(client, pending, options.batchSize ?? 50)
|
|
367
|
+
),
|
|
368
|
+
});
|
|
369
|
+
|
|
382
370
|
if (client.capabilities.has("X-GM-EXT-1")) {
|
|
383
371
|
const allMail = (await client.list()).find(
|
|
384
372
|
(folder) => folder.specialUse === "\\All"
|
|
@@ -390,8 +378,45 @@ export async function imapSource(
|
|
|
390
378
|
);
|
|
391
379
|
}
|
|
392
380
|
|
|
393
|
-
|
|
381
|
+
const status = await client.status(allMail.path, { messages: true });
|
|
382
|
+
|
|
383
|
+
return source(true, status ? (status.messages ?? null) : null, [
|
|
384
|
+
{
|
|
385
|
+
path: allMail.path,
|
|
386
|
+
plan: (validity) => gmailPlan(client, options, validity),
|
|
387
|
+
},
|
|
388
|
+
]);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const folders = await client.list({ statusQuery: { messages: true } });
|
|
392
|
+
|
|
393
|
+
const plans = folderPlans(folders, options).filter(
|
|
394
|
+
(plan) => !options.skip.has(plan.placement)
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
const counts = new Map(
|
|
398
|
+
folders.map((folder) => [folder.path, folder.status?.messages])
|
|
399
|
+
);
|
|
400
|
+
|
|
401
|
+
let total: number | null = 0;
|
|
402
|
+
|
|
403
|
+
for (const plan of plans) {
|
|
404
|
+
const count = counts.get(plan.path);
|
|
405
|
+
|
|
406
|
+
if (count === undefined) {
|
|
407
|
+
total = null;
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
total += count;
|
|
394
412
|
}
|
|
395
413
|
|
|
396
|
-
return
|
|
414
|
+
return source(
|
|
415
|
+
false,
|
|
416
|
+
total,
|
|
417
|
+
plans.map((folder) => ({
|
|
418
|
+
path: folder.path,
|
|
419
|
+
plan: (validity) => folderPlan(client, folder, options, validity),
|
|
420
|
+
}))
|
|
421
|
+
);
|
|
397
422
|
}
|
package/import/invariant.ts
CHANGED
|
@@ -15,7 +15,7 @@ export function invariant(
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
/** `value`, or an InvariantError with `message` when it is null or undefined. */
|
|
18
|
-
export function required<T>(value: T, message: string)
|
|
18
|
+
export function required<T>(value: T, message: string) {
|
|
19
19
|
if (value == null) throw new InvariantError(message);
|
|
20
20
|
|
|
21
21
|
return value;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one browser sign-in the CLI makes, for the workspace and for Google
|
|
3
|
+
* alike: the authorization code flow with PKCE (RFC 7636) and a loopback
|
|
4
|
+
* redirect (RFC 8252), its answer checked (an error, the state, the issuer
|
|
5
|
+
* per RFC 9207), and given up after a while.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** How long the browser has to come back. */
|
|
9
|
+
const SIGN_IN_TIMEOUT_MS = 10 * 60 * 1000;
|
|
10
|
+
|
|
11
|
+
function base64Url(bytes: Uint8Array) {
|
|
12
|
+
return Buffer.from(bytes).toString("base64url");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type LoopbackSignInOptions = {
|
|
16
|
+
/** Names the sign-in in messages, e.g. "Sign-in" or "Google sign-in". */
|
|
17
|
+
what: string;
|
|
18
|
+
/** The redirect's path at 127.0.0.1, e.g. "/callback". */
|
|
19
|
+
path: string;
|
|
20
|
+
/**
|
|
21
|
+
* The authorization URL for this redirect URI, with the request's own
|
|
22
|
+
* parameters (client_id, scope…); the PKCE, state, redirect and
|
|
23
|
+
* response type are added here.
|
|
24
|
+
*/
|
|
25
|
+
authorizeUrl: (redirectUri: string) => Promise<URL>;
|
|
26
|
+
/** The issuer the answer must name, when it names one. */
|
|
27
|
+
issuer: string;
|
|
28
|
+
/** What to say before the browser opens; the URL follows. */
|
|
29
|
+
prompt: string;
|
|
30
|
+
openBrowser: (url: string) => void | Promise<void>;
|
|
31
|
+
log: (message: string) => void;
|
|
32
|
+
timeoutMs?: number;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** Signs in in the browser; the code to exchange, with its redirect URI and PKCE verifier. */
|
|
36
|
+
export async function loopbackSignIn(options: LoopbackSignInOptions) {
|
|
37
|
+
const verifier = base64Url(crypto.getRandomValues(new Uint8Array(32)));
|
|
38
|
+
|
|
39
|
+
const challenge = base64Url(
|
|
40
|
+
new Uint8Array(
|
|
41
|
+
await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier))
|
|
42
|
+
)
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const state = base64Url(crypto.getRandomValues(new Uint8Array(16)));
|
|
46
|
+
let receive!: (params: URLSearchParams) => void;
|
|
47
|
+
|
|
48
|
+
const callback = new Promise<URLSearchParams>((resolve) => {
|
|
49
|
+
receive = resolve;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const server = Bun.serve({
|
|
53
|
+
hostname: "127.0.0.1",
|
|
54
|
+
port: 0,
|
|
55
|
+
fetch(request) {
|
|
56
|
+
const url = new URL(request.url);
|
|
57
|
+
|
|
58
|
+
if (url.pathname !== options.path) {
|
|
59
|
+
return new Response("Not found", { status: 404 });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
receive(url.searchParams);
|
|
63
|
+
|
|
64
|
+
return new Response(
|
|
65
|
+
url.searchParams.has("error")
|
|
66
|
+
? `${options.what} was not completed. You can close this tab.`
|
|
67
|
+
: "Signed in. You can close this tab and return to the terminal.",
|
|
68
|
+
{ headers: { "content-type": "text/plain; charset=utf-8" } }
|
|
69
|
+
);
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
let timer: Timer | undefined;
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const redirectUri = `http://127.0.0.1:${server.port}${options.path}`;
|
|
77
|
+
const url = await options.authorizeUrl(redirectUri);
|
|
78
|
+
|
|
79
|
+
for (const [name, value] of Object.entries({
|
|
80
|
+
response_type: "code",
|
|
81
|
+
redirect_uri: redirectUri,
|
|
82
|
+
code_challenge: challenge,
|
|
83
|
+
code_challenge_method: "S256",
|
|
84
|
+
state,
|
|
85
|
+
})) {
|
|
86
|
+
url.searchParams.set(name, value);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
options.log(`${options.prompt}\n ${url}`);
|
|
90
|
+
await options.openBrowser(url.toString());
|
|
91
|
+
|
|
92
|
+
const timeout = new Promise<never>((_resolve, reject) => {
|
|
93
|
+
timer = setTimeout(
|
|
94
|
+
() =>
|
|
95
|
+
reject(
|
|
96
|
+
new Error(
|
|
97
|
+
`${options.what} timed out: the browser did not come back; run the command again`
|
|
98
|
+
)
|
|
99
|
+
),
|
|
100
|
+
options.timeoutMs ?? SIGN_IN_TIMEOUT_MS
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const params = await Promise.race([callback, timeout]);
|
|
105
|
+
const error = params.get("error");
|
|
106
|
+
|
|
107
|
+
if (error) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
`${options.what} failed: ${params.get("error_description") ?? error}`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (params.get("state") !== state) {
|
|
114
|
+
throw new Error(
|
|
115
|
+
`${options.what} failed: the response does not match this request`
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const iss = params.get("iss");
|
|
120
|
+
|
|
121
|
+
if (iss !== null && iss !== options.issuer) {
|
|
122
|
+
throw new Error(
|
|
123
|
+
`${options.what} failed: the response came from another issuer`
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const code = params.get("code");
|
|
128
|
+
|
|
129
|
+
if (!code) throw new Error(`${options.what} failed: no code came back`);
|
|
130
|
+
|
|
131
|
+
return { code, redirectUri, verifier };
|
|
132
|
+
} finally {
|
|
133
|
+
clearTimeout(timer);
|
|
134
|
+
server.stop(true);
|
|
135
|
+
}
|
|
136
|
+
}
|