@open-workspace/create 0.4.2 → 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.
Files changed (46) hide show
  1. package/README.md +9 -5
  2. package/dist/backup-schema.ts +110 -0
  3. package/dist/workspace.ts +162 -0
  4. package/import/backup-snapshot.ts +254 -0
  5. package/import/backup-source.ts +270 -0
  6. package/import/cli.ts +134 -90
  7. package/import/concurrency.ts +34 -37
  8. package/import/csv.ts +1 -1
  9. package/import/directory/command.ts +293 -72
  10. package/import/directory/google.ts +158 -120
  11. package/import/directory/invitation-file.ts +108 -0
  12. package/import/directory/plan.ts +314 -38
  13. package/import/directory/read.ts +55 -0
  14. package/import/gmail.ts +9 -12
  15. package/import/headers.ts +1 -1
  16. package/import/idp/apps.ts +244 -0
  17. package/import/idp/command.ts +161 -0
  18. package/import/idp/csv.ts +70 -0
  19. package/import/idp/entra.ts +297 -0
  20. package/import/idp/keycloak.ts +250 -0
  21. package/import/idp/model.ts +108 -0
  22. package/import/idp/okta.ts +159 -0
  23. package/import/idp/pages.ts +65 -0
  24. package/import/imap-command.ts +42 -18
  25. package/import/imap-source.ts +245 -220
  26. package/import/invariant.ts +1 -1
  27. package/import/loopback.ts +136 -0
  28. package/import/mbox-source.ts +106 -55
  29. package/import/mbox.ts +1 -1
  30. package/import/oauth.ts +234 -138
  31. package/import/preview.ts +48 -0
  32. package/import/uploader.ts +169 -115
  33. package/import/workspace.ts +78 -15
  34. package/index.ts +3 -0
  35. package/module-templates.ts +9 -12
  36. package/module.ts +1 -5
  37. package/package.json +13 -2
  38. package/setup/alchemy.ts +2 -2
  39. package/setup/args.ts +16 -17
  40. package/setup/cloudflare-api.ts +75 -19
  41. package/setup/domains.ts +18 -6
  42. package/setup/flow.ts +217 -58
  43. package/setup/migration-guide.ts +255 -0
  44. package/setup/mx-recovery.ts +115 -0
  45. package/setup/ui.ts +4 -1
  46. package/templates.ts +26 -18
@@ -1,5 +1,5 @@
1
1
  import { readdirSync, statSync } from "node:fs";
2
- import { join } from "node:path";
2
+ import { basename, dirname, join } from "node:path";
3
3
 
4
4
  import {
5
5
  folderName,
@@ -12,6 +12,7 @@ import { header, headerText, parseHeaders, rootThreadKey } from "./headers.ts";
12
12
  import type { HeaderFields } from "./headers.ts";
13
13
  import { fromLineDate, readMbox } from "./mbox.ts";
14
14
  import type { MboxMessage } from "./mbox.ts";
15
+ import type { ImportPreview } from "./preview.ts";
15
16
  import { MAX_IMPORT_MESSAGE_BYTES } from "./protocol.ts";
16
17
  import type { ImportItem, Placement, SkippedItem } from "./protocol.ts";
17
18
 
@@ -31,14 +32,19 @@ export type MboxSourceOptions = GmailOptions & {
31
32
  folder: string | null;
32
33
  };
33
34
 
34
- /** The mbox files at these paths: files as given, directories searched for *.mbox (a Takeout folder). */
35
+ /** Files as given, *.mbox files in directories, and the mbox payload of Apple Mail packages. */
35
36
  export function mboxFiles(paths: string[]) {
36
37
  const files: string[] = [];
37
38
 
38
39
  const walk = (path: string) => {
39
40
  if (statSync(path).isDirectory()) {
40
41
  for (const entry of readdirSync(path).sort()) walk(join(path, entry));
41
- } else if (path.toLowerCase().endsWith(".mbox") || paths.includes(path)) {
42
+ } else if (
43
+ path.toLowerCase().endsWith(".mbox") ||
44
+ (basename(path) === "mbox" &&
45
+ dirname(path).toLowerCase().endsWith(".mbox")) ||
46
+ paths.includes(path)
47
+ ) {
42
48
  files.push(path);
43
49
  }
44
50
  };
@@ -54,9 +60,22 @@ export function mboxFiles(paths: string[]) {
54
60
  */
55
61
  async function* messages(files: string[]) {
56
62
  for (const file of files) {
57
- yield* readMbox(Bun.file(file).stream(), {
63
+ const source = Bun.file(file);
64
+ let found = false;
65
+
66
+ // oxlint-disable-next-line eslint/no-await-in-loop -- finish each file before validating it
67
+ for await (const message of readMbox(source.stream(), {
58
68
  maxBytes: MAX_IMPORT_MESSAGE_BYTES,
59
- });
69
+ })) {
70
+ found = true;
71
+ yield message;
72
+ }
73
+
74
+ if (!found && source.size > 0) {
75
+ throw new Error(
76
+ `No mbox messages found in ${file}. Pass an extracted mbox file.`
77
+ );
78
+ }
60
79
  }
61
80
  }
62
81
 
@@ -66,30 +85,45 @@ function threadOf(fields: HeaderFields) {
66
85
  return gmailThread ? `gmail:${gmailThread}` : rootThreadKey(fields);
67
86
  }
68
87
 
69
- /** First pass: how many messages there are, and which labels each Gmail thread carries. */
70
- export async function scanMbox(files: string[], options: MboxSourceOptions) {
88
+ /** Scan before uploading so a Gmail thread can take its folder from a later file. */
89
+ export async function scanMbox(
90
+ files: string[],
91
+ options: MboxSourceOptions,
92
+ preview?: ImportPreview
93
+ ) {
71
94
  const threads = new ThreadFolders(options.folderPriority);
95
+ const inboxByThread = new Map<string, number>();
72
96
  let count = 0;
73
- let bytes = 0;
74
97
 
75
98
  for await (const message of messages(files)) {
76
99
  count += 1;
77
- bytes += message.size;
100
+ const item = messagePlan(message, options, threads);
78
101
 
79
- if (message.tooLarge) continue;
80
- const fields = parseHeaders(headerText(message.raw));
81
- const labels = header(fields, "x-gmail-labels");
82
- const thread = threadOf(fields);
102
+ if (!preview) continue;
103
+ preview.add(item);
83
104
 
84
- if (labels !== undefined && thread) {
85
- threads.add(
86
- thread,
87
- gmailMessage(parseGmailLabelsHeader(labels), options).userLabels
88
- );
105
+ if (item.skipped === undefined && item.folderThread) {
106
+ // Only the folder count has to wait for the rest of this thread's labels.
107
+ const thread = item.folderThread;
108
+ inboxByThread.set(thread, (inboxByThread.get(thread) ?? 0) + 1);
89
109
  }
90
110
  }
91
111
 
92
- return { count, bytes, threads };
112
+ for (const [thread, n] of inboxByThread) {
113
+ preview?.addFolder(threads.folder(thread), n);
114
+ }
115
+
116
+ return { count, threads };
117
+ }
118
+
119
+ /** Thunderbird stores expunged messages until compaction; bit 0 is read, bit 3 is expunged. */
120
+ function thunderbirdStatus(fields: HeaderFields) {
121
+ const status = header(fields, "x-mozilla-status");
122
+
123
+ if (!status || !/^[0-9a-f]{4}$/iu.test(status)) return null;
124
+ const flags = Number.parseInt(status, 16);
125
+
126
+ return { seen: (flags & 1) !== 0, deleted: (flags & 8) !== 0 };
93
127
  }
94
128
 
95
129
  /** Read state from a Status header (R is read); mail without one is old, so read. */
@@ -99,68 +133,85 @@ function statusSeen(fields: HeaderFields) {
99
133
  return status === undefined ? true : status.includes("R");
100
134
  }
101
135
 
102
- /** What to import for one mbox message, or why it is skipped. */
103
- export function mboxItem(
136
+ type PlannedMail = Omit<ImportItem, "raw" | "sourceId" | "receivedAt"> & {
137
+ folderThread?: string;
138
+ };
139
+
140
+ function messagePlan(
104
141
  message: MboxMessage,
105
142
  options: MboxSourceOptions,
106
- threads: ThreadFolders
107
- ): ImportItem | SkippedItem {
108
- if (message.tooLarge) {
109
- return {
110
- sourceId: `mbox-large:${message.fromLine}:${message.size}`,
111
- skipped: "too large",
112
- };
113
- }
143
+ collectLabels?: ThreadFolders
144
+ ) {
145
+ if (message.tooLarge) return { skipped: "too large" };
114
146
 
115
- const sourceId = `mbox:${new Bun.CryptoHasher("sha256").update(message.raw).digest("hex")}`;
116
147
  const fields = parseHeaders(headerText(message.raw));
148
+ const local = thunderbirdStatus(fields);
149
+
150
+ if (local?.deleted) return { skipped: "deleted" };
151
+
117
152
  const thread = threadOf(fields);
118
- const receivedAt = fromLineDate(message.fromLine);
119
153
  const labels = header(fields, "x-gmail-labels");
120
154
 
121
155
  if (labels === undefined) {
122
156
  if (options.skip.has(options.placement)) {
123
- return { sourceId, skipped: options.placement };
157
+ return { skipped: options.placement };
124
158
  }
125
159
 
126
160
  return {
127
- sourceId,
128
- raw: message.raw,
129
161
  placement: options.placement,
130
162
  folder: options.placement === "inbox" ? options.folder : null,
131
- seen: statusSeen(fields),
163
+ seen: local?.seen ?? statusSeen(fields),
132
164
  thread,
133
- receivedAt,
134
- };
165
+ } satisfies PlannedMail;
135
166
  }
136
167
 
137
168
  const gmail = gmailMessage(parseGmailLabelsHeader(labels), options);
138
169
 
139
- if (gmail.skip) return { sourceId, skipped: gmail.skip };
170
+ if (thread) collectLabels?.add(thread, gmail.userLabels);
140
171
 
141
- if (options.skip.has(gmail.placement)) {
142
- return { sourceId, skipped: gmail.placement };
143
- }
172
+ if (gmail.skip) return { skipped: gmail.skip };
144
173
 
145
- const folder =
146
- gmail.placement === "inbox" && thread
147
- ? threads.folder(thread, gmail.userLabels)
148
- : gmail.placement === "inbox" && gmail.userLabels[0]
149
- ? folderName(gmail.userLabels[0])
150
- : null;
174
+ if (options.skip.has(gmail.placement)) return { skipped: gmail.placement };
175
+
176
+ const inbox = gmail.placement === "inbox";
151
177
 
152
178
  return {
153
- sourceId,
154
- raw: message.raw,
155
179
  placement: gmail.placement,
156
- folder,
157
- // Takeout says Unread or Opened; neither is old mail, read.
158
- seen: gmail.seen ?? true,
180
+ folder:
181
+ inbox && !thread && gmail.userLabels[0]
182
+ ? folderName(gmail.userLabels[0])
183
+ : null,
184
+ folderThread: inbox ? thread : undefined,
185
+ seen: local?.seen ?? gmail.seen ?? true,
159
186
  thread,
160
- receivedAt,
161
- // Gmail's Sent label says it, not the From address (which may be an alias).
162
187
  direction: gmail.sent ? "outgoing" : "incoming",
163
- };
188
+ } satisfies PlannedMail;
189
+ }
190
+
191
+ export function mboxItem(
192
+ message: MboxMessage,
193
+ options: MboxSourceOptions,
194
+ threads: ThreadFolders
195
+ ) {
196
+ const item = messagePlan(message, options);
197
+
198
+ const sourceId = message.tooLarge
199
+ ? `mbox-large:${message.fromLine}:${message.size}`
200
+ : `mbox:${new Bun.CryptoHasher("sha256").update(message.raw).digest("hex")}`;
201
+
202
+ if (item.skipped !== undefined) {
203
+ return { sourceId, skipped: item.skipped } satisfies SkippedItem;
204
+ }
205
+
206
+ const { folderThread, ...mail } = item;
207
+
208
+ return {
209
+ ...mail,
210
+ folder: folderThread ? threads.folder(folderThread) : mail.folder,
211
+ raw: message.raw,
212
+ receivedAt: fromLineDate(message.fromLine),
213
+ sourceId,
214
+ } satisfies ImportItem;
164
215
  }
165
216
 
166
217
  export async function* mboxItems(
package/import/mbox.ts CHANGED
@@ -50,7 +50,7 @@ function concat(parts: Uint8Array[], size: number) {
50
50
  export async function* readMbox(
51
51
  stream: AsyncIterable<Uint8Array>,
52
52
  options: { maxBytes: number }
53
- ): AsyncGenerator<MboxMessage> {
53
+ ) {
54
54
  let pending: Uint8Array = new Uint8Array(0);
55
55
  let lines: Uint8Array[] = [];
56
56
  let size = 0;