@open-workspace/create 0.5.2 → 0.6.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/import/backup-snapshot.ts +81 -34
- package/import/backup-source.ts +6 -12
- package/import/workspace.ts +32 -11
- package/package.json +1 -1
- package/templates.ts +1 -1
|
@@ -1,21 +1,40 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
closeSync,
|
|
3
|
+
existsSync,
|
|
4
|
+
openSync,
|
|
5
|
+
readdirSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
readSync,
|
|
8
|
+
statSync,
|
|
9
|
+
} from "node:fs";
|
|
2
10
|
import { dirname, join, relative, sep } from "node:path";
|
|
3
11
|
|
|
4
12
|
import { z } from "zod";
|
|
5
13
|
|
|
6
|
-
/**
|
|
7
|
-
|
|
14
|
+
/**
|
|
15
|
+
* The largest object a restore request carries: Cloudflare's limit on a
|
|
16
|
+
* request body on the Enterprise plan. A workspace on a plan that takes less
|
|
17
|
+
* (100 MB on Free and Pro, 200 MB on Business) refuses a larger request, and
|
|
18
|
+
* the line is reported as failed.
|
|
19
|
+
*/
|
|
20
|
+
const MAX_OBJECT_BYTES = 500 * 1000 * 1000;
|
|
8
21
|
|
|
9
22
|
const objectName = /^([0-9a-f]{64})\.[a-z0-9]+$/u;
|
|
10
23
|
|
|
11
24
|
type LinePlace = { sourceId: string; module: string; folder: string };
|
|
12
25
|
|
|
26
|
+
/** A request body read from disk as it is sent, so an object needn't fit in memory. */
|
|
27
|
+
export type RestoreBody = {
|
|
28
|
+
size: number;
|
|
29
|
+
stream: () => ReadableStream<Uint8Array>;
|
|
30
|
+
};
|
|
31
|
+
|
|
13
32
|
/** One line of a module's part, ready to send. */
|
|
14
33
|
export type BackupLine = LinePlace & {
|
|
15
34
|
ordered: boolean;
|
|
16
35
|
parts: string[];
|
|
17
36
|
bytes: number;
|
|
18
|
-
body:
|
|
37
|
+
body: RestoreBody;
|
|
19
38
|
};
|
|
20
39
|
|
|
21
40
|
/** A line, or why it can't be sent. */
|
|
@@ -107,43 +126,79 @@ function locateObject(dir: string, name: string) {
|
|
|
107
126
|
return { path, hash, size };
|
|
108
127
|
}
|
|
109
128
|
|
|
110
|
-
/** The
|
|
111
|
-
function
|
|
112
|
-
const
|
|
129
|
+
/** The file's SHA-256, read a MiB at a time. */
|
|
130
|
+
function fileSha256(path: string) {
|
|
131
|
+
const hasher = new Bun.CryptoHasher("sha256");
|
|
132
|
+
const buffer = new Uint8Array(1024 * 1024);
|
|
133
|
+
const fd = openSync(path, "r");
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
for (let read; (read = readSync(fd, buffer)) > 0;) {
|
|
137
|
+
hasher.update(buffer.subarray(0, read));
|
|
138
|
+
}
|
|
139
|
+
} finally {
|
|
140
|
+
closeSync(fd);
|
|
141
|
+
}
|
|
113
142
|
|
|
114
|
-
return
|
|
143
|
+
return hasher.digest("hex");
|
|
115
144
|
}
|
|
116
145
|
|
|
117
|
-
|
|
146
|
+
/**
|
|
147
|
+
* An object's file under `dir`, its bytes checked against its name, or why
|
|
148
|
+
* it can't be sent. The workspace checks them again as they arrive.
|
|
149
|
+
*/
|
|
150
|
+
function backupObject(dir: string, name: string) {
|
|
118
151
|
const object = locateObject(dir, name);
|
|
119
152
|
|
|
120
|
-
if (object.problem !== undefined)
|
|
121
|
-
|
|
153
|
+
if (object.problem !== undefined) return object;
|
|
154
|
+
|
|
155
|
+
if (fileSha256(object.path) !== object.hash) {
|
|
156
|
+
return { problem: "damaged" };
|
|
122
157
|
}
|
|
123
158
|
|
|
124
|
-
return
|
|
159
|
+
return object;
|
|
125
160
|
}
|
|
126
161
|
|
|
127
|
-
|
|
162
|
+
/** A line, a newline and its object's file, as the restore route reads them. */
|
|
163
|
+
export function restoreBody(text: string, object?: string) {
|
|
128
164
|
const line = new TextEncoder().encode(object ? `${text}\n` : text);
|
|
129
165
|
|
|
130
|
-
if (!object)
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
166
|
+
if (!object) {
|
|
167
|
+
return {
|
|
168
|
+
size: line.byteLength,
|
|
169
|
+
stream: () => new Blob([line]).stream(),
|
|
170
|
+
} satisfies RestoreBody;
|
|
171
|
+
}
|
|
134
172
|
|
|
135
|
-
|
|
173
|
+
const file = Bun.file(object);
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
size: line.byteLength + file.size,
|
|
177
|
+
stream: () => {
|
|
178
|
+
const reader = file.stream().getReader();
|
|
179
|
+
|
|
180
|
+
return new ReadableStream<Uint8Array>({
|
|
181
|
+
start: (controller) => controller.enqueue(line),
|
|
182
|
+
async pull(controller) {
|
|
183
|
+
const next = await reader.read();
|
|
184
|
+
|
|
185
|
+
if (next.done) controller.close();
|
|
186
|
+
else controller.enqueue(next.value);
|
|
187
|
+
},
|
|
188
|
+
cancel: (reason) => reader.cancel(reason),
|
|
189
|
+
});
|
|
190
|
+
},
|
|
191
|
+
} satisfies RestoreBody;
|
|
136
192
|
}
|
|
137
193
|
|
|
138
194
|
/**
|
|
139
|
-
* Each module's lines in a snapshot, in order, each with its object
|
|
140
|
-
*
|
|
195
|
+
* Each module's lines in a snapshot, in order, each with its object's file
|
|
196
|
+
* in `../objects/`, checked against its hash.
|
|
141
197
|
* @yields {BackupItem} Each line, or why it is skipped.
|
|
142
198
|
*/
|
|
143
199
|
export function* backupItems(
|
|
144
200
|
snapshot: string,
|
|
145
|
-
onInvalid?: ((message: string) => void) | null
|
|
146
|
-
verifyParts = true
|
|
201
|
+
onInvalid?: ((message: string) => void) | null
|
|
147
202
|
) {
|
|
148
203
|
const objects = join(snapshot, "..", "objects");
|
|
149
204
|
|
|
@@ -172,13 +227,9 @@ export function* backupItems(
|
|
|
172
227
|
let failedObject = refs.object;
|
|
173
228
|
|
|
174
229
|
for (const part of refs.parts) {
|
|
175
|
-
const partFile =
|
|
230
|
+
const partFile = backupObject(objects, part);
|
|
176
231
|
problem = partFile.problem;
|
|
177
232
|
|
|
178
|
-
if (partFile.path && verifyParts) {
|
|
179
|
-
problem = checkedBytes(partFile.path, partFile.hash).problem;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
233
|
if (!partFile.path || problem) {
|
|
183
234
|
failedObject = part;
|
|
184
235
|
break;
|
|
@@ -188,9 +239,7 @@ export function* backupItems(
|
|
|
188
239
|
parts.push(partFile.path);
|
|
189
240
|
}
|
|
190
241
|
|
|
191
|
-
const object = refs.object
|
|
192
|
-
? readBackupObject(objects, refs.object)
|
|
193
|
-
: undefined;
|
|
242
|
+
const object = refs.object ? backupObject(objects, refs.object) : undefined;
|
|
194
243
|
|
|
195
244
|
problem ??= object?.problem;
|
|
196
245
|
|
|
@@ -209,11 +258,9 @@ export function* backupItems(
|
|
|
209
258
|
bytes:
|
|
210
259
|
partBytes +
|
|
211
260
|
Buffer.byteLength(text) +
|
|
212
|
-
(object?.
|
|
261
|
+
(object?.path ? 1 + object.size : 0),
|
|
213
262
|
ordered: module !== "email" || refs.kind !== undefined,
|
|
214
|
-
|
|
215
|
-
return restoreBody(text, object?.bytes);
|
|
216
|
-
},
|
|
263
|
+
body: restoreBody(text, object?.path),
|
|
217
264
|
};
|
|
218
265
|
}
|
|
219
266
|
}
|
package/import/backup-source.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
-
import { basename,
|
|
2
|
+
import { basename, join } from "node:path";
|
|
3
3
|
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
|
|
@@ -9,7 +9,6 @@ import { parseCliArgs } from "./args.ts";
|
|
|
9
9
|
import {
|
|
10
10
|
backupItems,
|
|
11
11
|
inspectSnapshot,
|
|
12
|
-
readBackupObject,
|
|
13
12
|
restoreBody,
|
|
14
13
|
sha256,
|
|
15
14
|
} from "./backup-snapshot.ts";
|
|
@@ -75,17 +74,12 @@ async function restoreLine(workspace: Workspace, line: BackupLine) {
|
|
|
75
74
|
const path = `/api/admin/backups/restore/${encodeURIComponent(line.module)}?folder=${encodeURIComponent(line.folder)}`;
|
|
76
75
|
|
|
77
76
|
for (const part of line.parts) {
|
|
78
|
-
|
|
79
|
-
const data = readBackupObject(dirname(part), name);
|
|
80
|
-
|
|
81
|
-
if (!data.bytes) return { skipped: data.problem ?? "damaged" };
|
|
82
|
-
|
|
83
|
-
// oxlint-disable-next-line eslint/no-await-in-loop -- one file part in memory per concurrent restore
|
|
77
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- a line's parts go before it, in order
|
|
84
78
|
const answer = await workspace.json(path, restoreAnswer, {
|
|
85
79
|
method: "POST",
|
|
86
80
|
raw: restoreBody(
|
|
87
|
-
JSON.stringify({ kind: "backup-part", object:
|
|
88
|
-
|
|
81
|
+
JSON.stringify({ kind: "backup-part", object: basename(part) }),
|
|
82
|
+
part
|
|
89
83
|
),
|
|
90
84
|
});
|
|
91
85
|
|
|
@@ -246,8 +240,8 @@ export async function backupCommand(args: string[], deps: CliDeps) {
|
|
|
246
240
|
state,
|
|
247
241
|
run: (onProgress) =>
|
|
248
242
|
runResumable<BackupLine>({
|
|
249
|
-
//
|
|
250
|
-
items: backupItems(snapshot, values.partial ? null : undefined
|
|
243
|
+
// A damaged object is skipped here; the workspace checks what arrives too.
|
|
244
|
+
items: backupItems(snapshot, values.partial ? null : undefined),
|
|
251
245
|
state,
|
|
252
246
|
concurrency: concurrency(values),
|
|
253
247
|
exclusive: (line) => !("skipped" in line) && line.ordered,
|
package/import/workspace.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
|
|
3
|
+
import type { RestoreBody } from "./backup-snapshot.ts";
|
|
3
4
|
import { SignInError } from "./oauth.ts";
|
|
4
5
|
import type { Fetch, TokenSource } from "./oauth.ts";
|
|
5
6
|
import { importTargetSchema } from "./protocol.ts";
|
|
@@ -85,13 +86,24 @@ export class Workspace {
|
|
|
85
86
|
this.origin = new URL(workspace).origin;
|
|
86
87
|
}
|
|
87
88
|
|
|
88
|
-
/**
|
|
89
|
-
|
|
89
|
+
/**
|
|
90
|
+
* A request with the token; a refused token is refreshed once. A body
|
|
91
|
+
* that is a stream comes from `openBody`, opened again for that retry.
|
|
92
|
+
*/
|
|
93
|
+
async request(
|
|
94
|
+
path: string,
|
|
95
|
+
init: RequestInit = {},
|
|
96
|
+
openBody?: () => ReadableStream<Uint8Array>
|
|
97
|
+
) {
|
|
90
98
|
const send = (token: string) => {
|
|
91
99
|
const headers = new Headers(init.headers);
|
|
92
100
|
headers.set("authorization", `Bearer ${token}`);
|
|
93
101
|
|
|
94
|
-
return this.fetchImpl(`${this.origin}${path}`, {
|
|
102
|
+
return this.fetchImpl(`${this.origin}${path}`, {
|
|
103
|
+
...init,
|
|
104
|
+
headers,
|
|
105
|
+
body: openBody ? openBody() : init.body,
|
|
106
|
+
});
|
|
95
107
|
};
|
|
96
108
|
|
|
97
109
|
const token = await this.auth.token();
|
|
@@ -118,12 +130,21 @@ export class Workspace {
|
|
|
118
130
|
}
|
|
119
131
|
|
|
120
132
|
private send(path: string, init: JsonRequest) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
133
|
+
const { raw } = init;
|
|
134
|
+
|
|
135
|
+
if (raw) {
|
|
136
|
+
// Its length up front: the workspace reads an object's size from it.
|
|
137
|
+
return this.request(
|
|
138
|
+
path,
|
|
139
|
+
{
|
|
140
|
+
method: init.method ?? "POST",
|
|
141
|
+
headers: {
|
|
142
|
+
"content-type": "application/octet-stream",
|
|
143
|
+
"content-length": String(raw.size),
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
raw.stream
|
|
147
|
+
);
|
|
127
148
|
}
|
|
128
149
|
|
|
129
150
|
return this.request(path, {
|
|
@@ -219,8 +240,8 @@ export class Workspace {
|
|
|
219
240
|
type JsonRequest = {
|
|
220
241
|
method?: string;
|
|
221
242
|
body?: unknown;
|
|
222
|
-
/** Bytes sent as they are, instead of a JSON `body`. */
|
|
223
|
-
raw?:
|
|
243
|
+
/** Bytes sent as they are, read as they go, instead of a JSON `body`. */
|
|
244
|
+
raw?: RestoreBody;
|
|
224
245
|
};
|
|
225
246
|
|
|
226
247
|
const importResponseSchema = z.object({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-workspace/create",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Create an OpenWorkspace deployment (`bun create @open-workspace`), and import people, teams, SSO apps and mail.",
|
|
5
5
|
"homepage": "https://openworkspace.dev",
|
|
6
6
|
"license": "Apache-2.0",
|