@ghostlytools/cli 1.2.4 → 1.3.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 +25 -9
- package/dist/assets/api-Dq2QXQDk.js +4172 -0
- package/dist/assets/api-Dq2QXQDk.js.map +1 -0
- package/dist/assets/api-TiB5QSG9.js +3 -0
- package/dist/assets/apiKit-CA2x5GJF.js +200 -0
- package/dist/assets/apiKit-CA2x5GJF.js.map +1 -0
- package/dist/assets/apps-BxV9i_Y4.js +786 -0
- package/dist/assets/apps-BxV9i_Y4.js.map +1 -0
- package/dist/assets/apps-C-T8jeji.js +2 -0
- package/dist/assets/{arkade-CdnKGDbg.js → arkade-C_qDmHnm.js} +2 -2
- package/dist/assets/{arkade-CdnKGDbg.js.map → arkade-C_qDmHnm.js.map} +1 -1
- package/dist/assets/backup-Bugnn3Ue.js +2 -0
- package/dist/assets/backup-CnMN2oUK.js +731 -0
- package/dist/assets/backup-CnMN2oUK.js.map +1 -0
- package/dist/assets/bark_ffi_wasm_bg-EY2uc0Bx.js.map +1 -1
- package/dist/assets/bitcoind-CZchoh43.js +349 -0
- package/dist/assets/bitcoind-CZchoh43.js.map +1 -0
- package/dist/assets/bitcoindProvider-DGw3xBSa.js +97 -0
- package/dist/assets/bitcoindProvider-DGw3xBSa.js.map +1 -0
- package/dist/assets/bitcoindevkit_bg-B-iLWrmX.js.map +1 -1
- package/dist/assets/buttons-Bj7Vf7Pn.js +2 -0
- package/dist/assets/buttons-CvNBzh2S.js +496 -0
- package/dist/assets/buttons-CvNBzh2S.js.map +1 -0
- package/dist/assets/{endpoint-pgFkR2AS.js → endpoint-BOTFMpfS.js} +56 -18
- package/dist/assets/endpoint-BOTFMpfS.js.map +1 -0
- package/dist/assets/envelope-WjingYSc.js +90 -0
- package/dist/assets/envelope-WjingYSc.js.map +1 -0
- package/dist/assets/errors-DO5z6h_l.js +105 -0
- package/dist/assets/errors-DO5z6h_l.js.map +1 -0
- package/dist/assets/fedimint_client_wasm_bg-BtHHlFpp.js.map +1 -1
- package/dist/assets/{fileBytes-Dbifu5I5.js → fileBytes-CfSXjTFa.js} +2 -2
- package/dist/assets/{fileBytes-Dbifu5I5.js.map → fileBytes-CfSXjTFa.js.map} +1 -1
- package/dist/assets/fileBytes-CgbibpbV.js +2 -0
- package/dist/assets/{fileBytes-BcYZhNgF.js → fileBytes-Di7VZtmU.js} +6 -19
- package/dist/assets/fileBytes-Di7VZtmU.js.map +1 -0
- package/dist/assets/fileBytesIdb-CFP-DSkZ.js +2 -0
- package/dist/assets/{fileBytesIdb-DzSaqI0A.js → fileBytesIdb-D5u6H8X_.js} +4 -4
- package/dist/assets/{fileBytesIdb-DzSaqI0A.js.map → fileBytesIdb-D5u6H8X_.js.map} +1 -1
- package/dist/assets/{fileBytesOpfs-Dx8RZiV5.js → fileBytesOpfs-Dd1N4ERH.js} +3 -3
- package/dist/assets/{fileBytesOpfs-Dx8RZiV5.js.map → fileBytesOpfs-Dd1N4ERH.js.map} +1 -1
- package/dist/assets/groupFiles-IEUW3DQz.js +3202 -0
- package/dist/assets/groupFiles-IEUW3DQz.js.map +1 -0
- package/dist/assets/host-CWt-jtqz.js +4673 -0
- package/dist/assets/host-CWt-jtqz.js.map +1 -0
- package/dist/assets/hyperdhtRelay-BT_wRxab.js +21 -0
- package/dist/assets/hyperdhtRelay-BT_wRxab.js.map +1 -0
- package/dist/assets/{hyperdhtRelay-C1p0nkle.js → hyperdhtRelay-CPYwSLLS.js} +25 -10
- package/dist/assets/hyperdhtRelay-CPYwSLLS.js.map +1 -0
- package/dist/assets/{idb-CTF6LF5B.js → idb-B5L8wuOm.js} +7 -5
- package/dist/assets/idb-B5L8wuOm.js.map +1 -0
- package/dist/assets/irohWeb-4oChIOIp.js +544 -0
- package/dist/assets/irohWeb-4oChIOIp.js.map +1 -0
- package/dist/assets/{joinTrace-CO8mlhNO.js → joinTrace-CjRDFy85.js} +2 -7
- package/dist/assets/{joinTrace-CO8mlhNO.js.map → joinTrace-CjRDFy85.js.map} +1 -1
- package/dist/assets/joinTrace-D-fnt2p3.js +2 -0
- package/dist/assets/main-DbqcdShN.js +4409 -0
- package/dist/assets/main-DbqcdShN.js.map +1 -0
- package/dist/assets/{mainline-BH6s8HVi.js → mainline-ZkiOnevs.js} +11 -6
- package/dist/assets/mainline-ZkiOnevs.js.map +1 -0
- package/dist/assets/mints-BrlY773j.js +74 -0
- package/dist/assets/mints-BrlY773j.js.map +1 -0
- package/dist/assets/mints-CqTdA4Gk.js +2 -0
- package/dist/assets/networkReason-CetKkTPo.js +154 -0
- package/dist/assets/networkReason-CetKkTPo.js.map +1 -0
- package/dist/assets/nwc-CgkqyvsE.js +612 -0
- package/dist/assets/nwc-CgkqyvsE.js.map +1 -0
- package/dist/assets/nwcProvider-4eVz_8HP.js +77 -0
- package/dist/assets/nwcProvider-4eVz_8HP.js.map +1 -0
- package/dist/assets/privateFolder-Bk9C9oFv.js +38 -0
- package/dist/assets/privateFolder-Bk9C9oFv.js.map +1 -0
- package/dist/assets/profiles-BHc9pEYg.js +139 -0
- package/dist/assets/profiles-BHc9pEYg.js.map +1 -0
- package/dist/assets/registry-DlAKO0Fy.js +3793 -0
- package/dist/assets/registry-DlAKO0Fy.js.map +1 -0
- package/dist/assets/registry-OBJBnHVQ.js +2 -0
- package/dist/assets/resolve-CouHhCpJ.js +591 -0
- package/dist/assets/resolve-CouHhCpJ.js.map +1 -0
- package/dist/assets/{restoredRows-C_84q0Pd.js → restoredRows-Db3oUTko.js} +2 -2
- package/dist/assets/{restoredRows-C_84q0Pd.js.map → restoredRows-Db3oUTko.js.map} +1 -1
- package/dist/assets/{server-0zfNpABQ.js → server-Ss0gJ-nw.js} +1275 -1404
- package/dist/assets/server-Ss0gJ-nw.js.map +1 -0
- package/dist/assets/{src-CbLkHfz8.js → src-DDzSERyc.js} +882 -134
- package/dist/assets/src-DDzSERyc.js.map +1 -0
- package/dist/assets/turnRefs-B87dtrHM.js +323 -0
- package/dist/assets/turnRefs-B87dtrHM.js.map +1 -0
- package/dist/assets/types-C4_KafL_.js +122 -0
- package/dist/assets/types-C4_KafL_.js.map +1 -0
- package/dist/assets/{usdt-Qf4UuygS.js → usdt-Fj8CQO2z.js} +3 -3
- package/dist/assets/{usdt-Qf4UuygS.js.map → usdt-Fj8CQO2z.js.map} +1 -1
- package/dist/ghostly.mjs +4 -22077
- package/dist/ghostly.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/assets/endpoint-pgFkR2AS.js.map +0 -1
- package/dist/assets/fileBytes-BcYZhNgF.js.map +0 -1
- package/dist/assets/hyperdhtRelay-C1p0nkle.js.map +0 -1
- package/dist/assets/idb-CTF6LF5B.js.map +0 -1
- package/dist/assets/mainline-BH6s8HVi.js.map +0 -1
- package/dist/assets/rolldown-runtime-8H4AJuhK.js +0 -14
- package/dist/assets/server-0zfNpABQ.js.map +0 -1
- package/dist/assets/src-CbLkHfz8.js.map +0 -1
|
@@ -0,0 +1,731 @@
|
|
|
1
|
+
import { vl as toBase64Url } from "./src-DDzSERyc.js";
|
|
2
|
+
import { c as fileStore } from "./idb-B5L8wuOm.js";
|
|
3
|
+
import { i as fromBase64Url, r as open$1 } from "./envelope-WjingYSc.js";
|
|
4
|
+
import { t as CliError } from "./errors-DO5z6h_l.js";
|
|
5
|
+
import { _ as str, n as bool } from "./apiKit-CA2x5GJF.js";
|
|
6
|
+
import { d as profilePaths, n as RESTORED_MARK, u as profileExists } from "./profiles-BHc9pEYg.js";
|
|
7
|
+
import { existsSync } from "node:fs";
|
|
8
|
+
import { link, mkdir, open, readFile, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
9
|
+
import { basename, dirname, join, relative, resolve, sep } from "node:path";
|
|
10
|
+
//#region ../browser/src/backup/stream.ts
|
|
11
|
+
/**
|
|
12
|
+
* A backup written and read a piece at a time (WISP 05, envelope version 2), so a profile with years of files never
|
|
13
|
+
* has to fit in memory. The file is one line of clear JSON (the header), then frames:
|
|
14
|
+
*
|
|
15
|
+
* header line: {"format":"ghostly-backup","version":2,"protection":"passphrase"|"none",...}\n
|
|
16
|
+
* frame: kind (1 byte) | length (4 bytes, big endian) | body
|
|
17
|
+
*
|
|
18
|
+
* A frame carries one record: a piece of JSON (gzip or plain) or a piece of a file's bytes, at most `FRAME_BYTES`
|
|
19
|
+
* of them. The last frame is the final marker; a file that ends without it was cut short and is refused.
|
|
20
|
+
*
|
|
21
|
+
* - `passphrase`: the key comes from the passphrase as in version 1 (PBKDF2-SHA256, 600 000 rounds, 16 byte salt).
|
|
22
|
+
* Every frame is sealed alone with AES-256-GCM: its nonce is the header's 7 random bytes, the frame's number
|
|
23
|
+
* (4 bytes) and its kind (1 byte), and the SHA-256 of the header line is its associated data. A frame changed,
|
|
24
|
+
* moved, dropped or retyped, a changed header, or a missing final marker fails to open.
|
|
25
|
+
* - `none`: nothing is encrypted. A SHA-256 chain runs over the header line and every frame, and the final marker
|
|
26
|
+
* holds its last value, so a damaged or cut file is refused. It proves nothing about who made the file.
|
|
27
|
+
*/
|
|
28
|
+
var FRAME_BYTES = 1048576;
|
|
29
|
+
/** No frame is larger than this: a reader refuses one that says it is, before reading it. */
|
|
30
|
+
var MAX_FRAME_BYTES = 67108864;
|
|
31
|
+
/** One piece of JSON, decompressed: past it the backup is refused, not read. */
|
|
32
|
+
var MAX_JSON_BYTES = 268435456;
|
|
33
|
+
var MAX_HEADER_BYTES = 4096;
|
|
34
|
+
var ITERATIONS = 6e5;
|
|
35
|
+
var KIND = {
|
|
36
|
+
gzipJson: 1,
|
|
37
|
+
json: 2,
|
|
38
|
+
bytes: 3,
|
|
39
|
+
final: 255
|
|
40
|
+
};
|
|
41
|
+
/** Thrown when the work was cancelled (`AbortSignal`): nothing is wrong with the backup. */
|
|
42
|
+
var BackupCancelled = class extends Error {
|
|
43
|
+
constructor() {
|
|
44
|
+
super("Cancelled");
|
|
45
|
+
this.name = "AbortError";
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var check = (signal) => {
|
|
49
|
+
if (signal?.aborted) throw new BackupCancelled();
|
|
50
|
+
};
|
|
51
|
+
var canCompress = () => typeof CompressionStream !== "undefined";
|
|
52
|
+
async function through(bytes, stream, limit = Infinity) {
|
|
53
|
+
const reader = new Blob([bytes]).stream().pipeThrough(stream).getReader();
|
|
54
|
+
const chunks = [];
|
|
55
|
+
let size = 0;
|
|
56
|
+
for (let next = await reader.read(); !next.done; next = await reader.read()) {
|
|
57
|
+
size += next.value.length;
|
|
58
|
+
if (size > limit) {
|
|
59
|
+
await reader.cancel();
|
|
60
|
+
throw new Error("This backup is too large to restore");
|
|
61
|
+
}
|
|
62
|
+
chunks.push(next.value);
|
|
63
|
+
}
|
|
64
|
+
if (chunks.length === 1) return chunks[0];
|
|
65
|
+
const out = new Uint8Array(size);
|
|
66
|
+
let at = 0;
|
|
67
|
+
for (const chunk of chunks) {
|
|
68
|
+
out.set(chunk, at);
|
|
69
|
+
at += chunk.length;
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
async function keyFrom(passphrase, salt, iterations) {
|
|
74
|
+
const material = await crypto.subtle.importKey("raw", new TextEncoder().encode(passphrase), "PBKDF2", false, ["deriveKey"]);
|
|
75
|
+
return crypto.subtle.deriveKey({
|
|
76
|
+
name: "PBKDF2",
|
|
77
|
+
hash: "SHA-256",
|
|
78
|
+
salt,
|
|
79
|
+
iterations
|
|
80
|
+
}, material, {
|
|
81
|
+
name: "AES-GCM",
|
|
82
|
+
length: 256
|
|
83
|
+
}, false, ["encrypt", "decrypt"]);
|
|
84
|
+
}
|
|
85
|
+
var sha256 = async (...parts) => new Uint8Array(await crypto.subtle.digest("SHA-256", join$1(parts)));
|
|
86
|
+
function join$1(parts) {
|
|
87
|
+
if (parts.length === 1) return parts[0];
|
|
88
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
89
|
+
let at = 0;
|
|
90
|
+
for (const part of parts) {
|
|
91
|
+
out.set(part, at);
|
|
92
|
+
at += part.length;
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
function prefix(kind, length) {
|
|
97
|
+
const out = /* @__PURE__ */ new Uint8Array(5);
|
|
98
|
+
out[0] = kind;
|
|
99
|
+
new DataView(out.buffer).setUint32(1, length);
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
function nonce(base, frame, kind) {
|
|
103
|
+
const out = /* @__PURE__ */ new Uint8Array(12);
|
|
104
|
+
out.set(base, 0);
|
|
105
|
+
new DataView(out.buffer).setUint32(7, frame);
|
|
106
|
+
out[11] = kind;
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
/** Writes a backup: `json` and `bytes` in the order they are to be read, then `finish`. */
|
|
110
|
+
var BackupWriter = class BackupWriter {
|
|
111
|
+
sink;
|
|
112
|
+
key;
|
|
113
|
+
base;
|
|
114
|
+
chain;
|
|
115
|
+
signal;
|
|
116
|
+
frame = 0;
|
|
117
|
+
/** Bytes handed to the sink so far. */
|
|
118
|
+
written = 0;
|
|
119
|
+
finished = false;
|
|
120
|
+
/** Set once the sink refused a frame: the error it gave, thrown again at every later write. */
|
|
121
|
+
failed = null;
|
|
122
|
+
constructor(sink, key, base, chain, signal) {
|
|
123
|
+
this.sink = sink;
|
|
124
|
+
this.key = key;
|
|
125
|
+
this.base = base;
|
|
126
|
+
this.chain = chain;
|
|
127
|
+
this.signal = signal;
|
|
128
|
+
}
|
|
129
|
+
/** `passphrase` null: a backup anyone can read (the caller has made sure that is what the person chose). */
|
|
130
|
+
static async start(sink, passphrase, signal, version = 2) {
|
|
131
|
+
if (passphrase !== null && passphrase.length < 12) throw new Error("Use at least 12 characters for the backup passphrase");
|
|
132
|
+
check(signal);
|
|
133
|
+
const salt = crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16)), base = crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(7));
|
|
134
|
+
const header = passphrase === null ? {
|
|
135
|
+
format: "ghostly-backup",
|
|
136
|
+
version,
|
|
137
|
+
protection: "none",
|
|
138
|
+
check: { name: "SHA-256-chain" }
|
|
139
|
+
} : {
|
|
140
|
+
format: "ghostly-backup",
|
|
141
|
+
version,
|
|
142
|
+
protection: "passphrase",
|
|
143
|
+
kdf: {
|
|
144
|
+
name: "PBKDF2-SHA256",
|
|
145
|
+
iterations: ITERATIONS,
|
|
146
|
+
salt: toBase64Url(salt)
|
|
147
|
+
},
|
|
148
|
+
cipher: {
|
|
149
|
+
name: "AES-256-GCM",
|
|
150
|
+
nonce: toBase64Url(base)
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
const line = new TextEncoder().encode(`${JSON.stringify(header)}\n`);
|
|
154
|
+
const key = passphrase === null ? null : await keyFrom(passphrase, salt, ITERATIONS);
|
|
155
|
+
const writer = new BackupWriter(sink, key, base, await sha256(line), signal);
|
|
156
|
+
await sink.write(line);
|
|
157
|
+
writer.written = line.length;
|
|
158
|
+
return writer;
|
|
159
|
+
}
|
|
160
|
+
async put(kind, plain) {
|
|
161
|
+
check(this.signal);
|
|
162
|
+
if (this.failed) throw this.failed.error;
|
|
163
|
+
if (this.finished) throw new Error("This backup is already finished");
|
|
164
|
+
if (this.frame >= 4294967295) throw new Error("This backup is too large");
|
|
165
|
+
if (plain.length > MAX_FRAME_BYTES) throw new Error("A record of this profile is too large for a backup");
|
|
166
|
+
let out;
|
|
167
|
+
if (this.key) {
|
|
168
|
+
const sealed = new Uint8Array(await crypto.subtle.encrypt({
|
|
169
|
+
name: "AES-GCM",
|
|
170
|
+
iv: nonce(this.base, this.frame, kind),
|
|
171
|
+
additionalData: this.chain
|
|
172
|
+
}, this.key, plain));
|
|
173
|
+
out = join$1([prefix(kind, sealed.length), sealed]);
|
|
174
|
+
} else {
|
|
175
|
+
const head = prefix(kind, kind === KIND.final ? 32 : plain.length);
|
|
176
|
+
if (kind === KIND.final) out = join$1([head, this.chain]);
|
|
177
|
+
else {
|
|
178
|
+
out = join$1([head, plain]);
|
|
179
|
+
this.chain = await sha256(this.chain, out);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
this.frame += 1;
|
|
183
|
+
try {
|
|
184
|
+
await this.sink.write(out);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
this.failed = { error };
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
this.written += out.length;
|
|
190
|
+
}
|
|
191
|
+
/** One piece of JSON, compressed where the platform can. */
|
|
192
|
+
async json(text) {
|
|
193
|
+
const plain = new TextEncoder().encode(text);
|
|
194
|
+
if (plain.length > MAX_JSON_BYTES) throw new Error("A record of this profile is too large for a backup");
|
|
195
|
+
if (canCompress()) await this.put(KIND.gzipJson, await through(plain, new CompressionStream("gzip")));
|
|
196
|
+
else await this.put(KIND.json, plain);
|
|
197
|
+
}
|
|
198
|
+
/** Bytes of the file the last JSON announced, cut into frames. */
|
|
199
|
+
async bytes(chunk) {
|
|
200
|
+
for (let at = 0; at < chunk.length; at += FRAME_BYTES) await this.put(KIND.bytes, chunk.subarray(at, Math.min(chunk.length, at + FRAME_BYTES)));
|
|
201
|
+
}
|
|
202
|
+
/** The final marker: without it a reader refuses the backup. Resolves to the size of the whole backup. */
|
|
203
|
+
async finish() {
|
|
204
|
+
await this.put(KIND.final, /* @__PURE__ */ new Uint8Array());
|
|
205
|
+
this.finished = true;
|
|
206
|
+
return this.written;
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
/** The first line of a version 2 backup, or null when this is not one (version 1 is one JSON object with no line end). */
|
|
210
|
+
async function readBackupHeader(source) {
|
|
211
|
+
const start = await source.read(0, Math.min(MAX_HEADER_BYTES, source.size));
|
|
212
|
+
const end = start.indexOf(10);
|
|
213
|
+
if (end < 0) return null;
|
|
214
|
+
let header;
|
|
215
|
+
try {
|
|
216
|
+
header = JSON.parse(new TextDecoder().decode(start.subarray(0, end)));
|
|
217
|
+
} catch {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
if (header?.format !== "ghostly-backup" || typeof header.version !== "number" || header.version < 2) return null;
|
|
221
|
+
if (header.version !== 2 && header.version !== 3) throw new Error("This backup comes from a newer Ghostly; update to restore it");
|
|
222
|
+
if (header.protection === "none") {
|
|
223
|
+
if (header.check?.name !== "SHA-256-chain") throw new Error("Unsupported backup format");
|
|
224
|
+
} else if (header.protection === "passphrase") {
|
|
225
|
+
if (header.kdf?.name !== "PBKDF2-SHA256" || !(header.kdf.iterations >= ITERATIONS) || header.kdf.iterations > 1e7 || header.cipher?.name !== "AES-256-GCM") throw new Error("Unsupported backup encryption");
|
|
226
|
+
} else throw new Error("Unsupported backup format");
|
|
227
|
+
return {
|
|
228
|
+
header,
|
|
229
|
+
line: start.slice(0, end + 1)
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
var DAMAGED = "This backup is damaged: it was changed or cut short";
|
|
233
|
+
/** Reads a version 2 backup record by record. Nothing of a frame is handed out before it passed its check. */
|
|
234
|
+
var BackupReader = class BackupReader {
|
|
235
|
+
source;
|
|
236
|
+
key;
|
|
237
|
+
base;
|
|
238
|
+
chain;
|
|
239
|
+
signal;
|
|
240
|
+
frame = 0;
|
|
241
|
+
at;
|
|
242
|
+
block = /* @__PURE__ */ new Uint8Array();
|
|
243
|
+
blockAt = 0;
|
|
244
|
+
done = false;
|
|
245
|
+
constructor(source, key, base, chain, start, signal) {
|
|
246
|
+
this.source = source;
|
|
247
|
+
this.key = key;
|
|
248
|
+
this.base = base;
|
|
249
|
+
this.chain = chain;
|
|
250
|
+
this.signal = signal;
|
|
251
|
+
this.at = start;
|
|
252
|
+
}
|
|
253
|
+
get protection() {
|
|
254
|
+
return this.key ? "passphrase" : "none";
|
|
255
|
+
}
|
|
256
|
+
/** Null when `source` is not a version 2 backup. `passphrase` is needed, and used, only for a sealed one. */
|
|
257
|
+
static async open(source, passphrase, signal) {
|
|
258
|
+
const found = await readBackupHeader(source);
|
|
259
|
+
if (!found) return null;
|
|
260
|
+
check(signal);
|
|
261
|
+
const { header, line } = found;
|
|
262
|
+
if (header.protection === "none") return new BackupReader(source, null, /* @__PURE__ */ new Uint8Array(), await sha256(line), line.length, signal);
|
|
263
|
+
let key, base;
|
|
264
|
+
try {
|
|
265
|
+
base = fromBase64Url(header.cipher.nonce);
|
|
266
|
+
if (base.length !== 7) throw new Error("nonce");
|
|
267
|
+
key = await keyFrom(passphrase ?? "", fromBase64Url(header.kdf.salt), header.kdf.iterations);
|
|
268
|
+
} catch {
|
|
269
|
+
throw new Error("Unsupported backup encryption");
|
|
270
|
+
}
|
|
271
|
+
return new BackupReader(source, key, base, await sha256(line), line.length, signal);
|
|
272
|
+
}
|
|
273
|
+
/** Bytes of the source read so far (for progress). */
|
|
274
|
+
get position() {
|
|
275
|
+
return this.at;
|
|
276
|
+
}
|
|
277
|
+
/** `length` bytes at the reading point, through a read-ahead block so a source is asked a few times per megabyte, not per frame. */
|
|
278
|
+
async take(length) {
|
|
279
|
+
if (this.at + length > this.source.size) throw new Error(DAMAGED);
|
|
280
|
+
const from = this.at - this.blockAt;
|
|
281
|
+
if (from < 0 || from + length > this.block.length) {
|
|
282
|
+
this.block = await this.source.read(this.at, Math.min(this.source.size - this.at, Math.max(length, 4 * FRAME_BYTES)));
|
|
283
|
+
this.blockAt = this.at;
|
|
284
|
+
if (this.block.length < length) throw new Error(DAMAGED);
|
|
285
|
+
}
|
|
286
|
+
const out = this.block.subarray(this.at - this.blockAt, this.at - this.blockAt + length);
|
|
287
|
+
this.at += length;
|
|
288
|
+
return out;
|
|
289
|
+
}
|
|
290
|
+
/** The next record, or null at the end: only then is the backup known to be whole. */
|
|
291
|
+
async next() {
|
|
292
|
+
check(this.signal);
|
|
293
|
+
if (this.done) return null;
|
|
294
|
+
const head = await this.take(5);
|
|
295
|
+
const kind = head[0], length = new DataView(head.buffer, head.byteOffset, 5).getUint32(1);
|
|
296
|
+
if (length > 67108880 || !Object.values(KIND).includes(kind)) throw new Error(DAMAGED);
|
|
297
|
+
const headCopy = head.slice();
|
|
298
|
+
const body = await this.take(length);
|
|
299
|
+
let plain;
|
|
300
|
+
if (this.key) try {
|
|
301
|
+
plain = new Uint8Array(await crypto.subtle.decrypt({
|
|
302
|
+
name: "AES-GCM",
|
|
303
|
+
iv: nonce(this.base, this.frame, kind),
|
|
304
|
+
additionalData: this.chain
|
|
305
|
+
}, this.key, body));
|
|
306
|
+
} catch {
|
|
307
|
+
throw new Error(this.frame === 0 ? "Wrong passphrase, or the backup was changed" : DAMAGED);
|
|
308
|
+
}
|
|
309
|
+
else if (kind === KIND.final) {
|
|
310
|
+
if (body.length !== 32 || body.some((byte, i) => byte !== this.chain[i])) throw new Error(DAMAGED);
|
|
311
|
+
plain = /* @__PURE__ */ new Uint8Array();
|
|
312
|
+
} else {
|
|
313
|
+
this.chain = await sha256(this.chain, headCopy, body);
|
|
314
|
+
plain = body.slice();
|
|
315
|
+
}
|
|
316
|
+
this.frame += 1;
|
|
317
|
+
if (kind === KIND.final) {
|
|
318
|
+
if (plain.length || this.at !== this.source.size) throw new Error(DAMAGED);
|
|
319
|
+
this.done = true;
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
if (kind === KIND.bytes) return { bytes: plain };
|
|
323
|
+
const text = kind === KIND.gzipJson ? await through(plain, new DecompressionStream("gzip"), MAX_JSON_BYTES).catch((error) => {
|
|
324
|
+
throw error instanceof Error && error.message.startsWith("This backup is too large") ? error : /* @__PURE__ */ new Error(DAMAGED);
|
|
325
|
+
}) : plain;
|
|
326
|
+
return { json: new TextDecoder().decode(text) };
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
//#endregion
|
|
330
|
+
//#region ../browser/src/backup/light.ts
|
|
331
|
+
/**
|
|
332
|
+
* A light backup (WISP 05 § Light backups): everything of a profile but the bytes of its larger files. Their messages
|
|
333
|
+
* and records stay; after a restore they show as not in this backup.
|
|
334
|
+
*
|
|
335
|
+
* The cut: a file of at most 1 MiB keeps its bytes (a short clip, a sticker, a screenshot, a document), and so does a
|
|
336
|
+
* voice message of at most 4 MiB. Voice messages are part of the conversation, and the longest one the apps record
|
|
337
|
+
* (15 minutes at 32 kbit/s, `VOICE_LIMITS`) is about 3.6 MB, so every recorded one stays. Photos from a camera
|
|
338
|
+
* (usually 2 to 8 MB), videos and large files are what is left out.
|
|
339
|
+
*/
|
|
340
|
+
var LIGHT_FILE_BYTES = 1048576;
|
|
341
|
+
var LIGHT_VOICE_BYTES = 4194304;
|
|
342
|
+
/** Whether a file of this size keeps its bytes in a light backup. */
|
|
343
|
+
var keptInLight = (size, voice) => size <= (voice ? LIGHT_VOICE_BYTES : LIGHT_FILE_BYTES);
|
|
344
|
+
//#endregion
|
|
345
|
+
//#region src/backup.ts
|
|
346
|
+
/**
|
|
347
|
+
* A headless profile's backup: the WISP 05 envelope around the profile itself, its store and its files, written and
|
|
348
|
+
* read a piece at a time (envelope version 2), so a profile with large files never has to fit in memory. It restores
|
|
349
|
+
* into a new headless profile; the app's own backups are made from its page's storage and are not this format
|
|
350
|
+
* (WISP 1100 § Parity). A backup an older CLI made (version 1: everything in one sealed JSON document) still restores.
|
|
351
|
+
*/
|
|
352
|
+
var FORMAT = "ghostly-cli-profile/2";
|
|
353
|
+
var FORMAT_1 = "ghostly-cli-profile/1";
|
|
354
|
+
async function walk(root, dir = root, out = []) {
|
|
355
|
+
let names;
|
|
356
|
+
try {
|
|
357
|
+
names = await readdir(dir);
|
|
358
|
+
} catch {
|
|
359
|
+
return out;
|
|
360
|
+
}
|
|
361
|
+
for (const name of names) {
|
|
362
|
+
const path = join(dir, name);
|
|
363
|
+
if ((await stat(path)).isDirectory()) await walk(root, path, out);
|
|
364
|
+
else out.push(relative(root, path));
|
|
365
|
+
}
|
|
366
|
+
return out;
|
|
367
|
+
}
|
|
368
|
+
/** A file's first `size` bytes into the backup, a frame at a time. One that got shorter meanwhile stops the backup. */
|
|
369
|
+
async function copyIn(writer, path, size) {
|
|
370
|
+
const handle = await open(path, "r");
|
|
371
|
+
try {
|
|
372
|
+
const buffer = new Uint8Array(FRAME_BYTES);
|
|
373
|
+
for (let at = 0; at < size;) {
|
|
374
|
+
const { bytesRead } = await handle.read(buffer, 0, Math.min(FRAME_BYTES, size - at), at);
|
|
375
|
+
if (!bytesRead) throw new CliError("unavailable", `${path} changed while it was backed up: try again`);
|
|
376
|
+
await writer.bytes(buffer.subarray(0, bytesRead));
|
|
377
|
+
at += bytesRead;
|
|
378
|
+
}
|
|
379
|
+
} finally {
|
|
380
|
+
await handle.close();
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
var BACKUP_METHODS = {
|
|
384
|
+
/**
|
|
385
|
+
* Writes the backup to `path` (resolved by the command), sealed with `passphrase`, or not encrypted when
|
|
386
|
+
* `noPassphrase` is true: one of the two, never a guess. The store is written whole for it, every value inline. It is
|
|
387
|
+
* written beside `path` and only takes its name once it is complete: a backup that fails leaves no file. `light`:
|
|
388
|
+
* the files over the cut (`keptInLight`) are left out, their records kept.
|
|
389
|
+
*/
|
|
390
|
+
async "profile.backup"(ctx, params) {
|
|
391
|
+
const path = resolve(str(params, "path", true));
|
|
392
|
+
if (existsSync(path)) throw new CliError("refused", `${path} exists: choose another name`);
|
|
393
|
+
const passphrase = str(params, "passphrase"), unsealed = bool(params, "noPassphrase");
|
|
394
|
+
if (!passphrase && !unsealed) throw new CliError("bad_request", "passphrase is required (or noPassphrase: true for a backup anyone can read)");
|
|
395
|
+
if (passphrase && unsealed) throw new CliError("bad_request", "Give a passphrase or noPassphrase, not both");
|
|
396
|
+
const folder = dirname(path);
|
|
397
|
+
if (!(await stat(folder)).isDirectory()) throw new CliError("not_found", `No such file or folder: ${folder}`);
|
|
398
|
+
const paths = ctx.runtime.paths;
|
|
399
|
+
const store = join(paths.db, `backup-store-${process.pid}.bin`);
|
|
400
|
+
await ctx.runtime.store.exportSnapshot(store);
|
|
401
|
+
const light = bool(params, "light");
|
|
402
|
+
const all = await Promise.all((await walk(paths.files)).map(async (name) => ({
|
|
403
|
+
name,
|
|
404
|
+
size: (await stat(join(paths.files, name))).size
|
|
405
|
+
})));
|
|
406
|
+
const kept = async (file) => keptInLight(file.size, false) || file.size <= 4194304 && !!(await fileStore.get(basename(file.name)).catch(() => void 0))?.metadata?.voice;
|
|
407
|
+
const files = [], leftOut = [];
|
|
408
|
+
for (const file of all) (!light || await kept(file) ? files : leftOut).push(file);
|
|
409
|
+
const storeSize = (await stat(store)).size, bytes = files.reduce((sum, file) => sum + file.size, 0);
|
|
410
|
+
const leftOutBytes = leftOut.reduce((sum, file) => sum + file.size, 0);
|
|
411
|
+
const partial = `${path}.partial-${process.pid}`;
|
|
412
|
+
let out = null;
|
|
413
|
+
try {
|
|
414
|
+
out = await openFile(partial);
|
|
415
|
+
const handle = out;
|
|
416
|
+
let writer;
|
|
417
|
+
try {
|
|
418
|
+
writer = await BackupWriter.start({ write: async (chunk) => {
|
|
419
|
+
await handle.write(chunk);
|
|
420
|
+
} }, unsealed ? null : passphrase);
|
|
421
|
+
} catch (error) {
|
|
422
|
+
throw new CliError("bad_request", error instanceof Error ? error.message : String(error));
|
|
423
|
+
}
|
|
424
|
+
const put = (record) => writer.json(JSON.stringify(record));
|
|
425
|
+
await put({
|
|
426
|
+
t: "profile",
|
|
427
|
+
format: FORMAT,
|
|
428
|
+
createdAt: Date.now(),
|
|
429
|
+
files: files.length,
|
|
430
|
+
bytes,
|
|
431
|
+
...light && { light: {
|
|
432
|
+
maxFileBytes: 1048576,
|
|
433
|
+
maxVoiceBytes: 4194304,
|
|
434
|
+
files: leftOut.length,
|
|
435
|
+
bytes: leftOutBytes,
|
|
436
|
+
ids: leftOut.map((file) => basename(file.name))
|
|
437
|
+
} }
|
|
438
|
+
});
|
|
439
|
+
await put({
|
|
440
|
+
t: "file",
|
|
441
|
+
kind: "store",
|
|
442
|
+
size: storeSize
|
|
443
|
+
});
|
|
444
|
+
await copyIn(writer, store, storeSize);
|
|
445
|
+
for (const file of files) {
|
|
446
|
+
await put({
|
|
447
|
+
t: "file",
|
|
448
|
+
kind: "file",
|
|
449
|
+
path: file.name.split(sep).join("/"),
|
|
450
|
+
size: file.size
|
|
451
|
+
});
|
|
452
|
+
await copyIn(writer, join(paths.files, file.name), file.size);
|
|
453
|
+
}
|
|
454
|
+
await put({
|
|
455
|
+
t: "end",
|
|
456
|
+
files: files.length,
|
|
457
|
+
bytes
|
|
458
|
+
});
|
|
459
|
+
const written = await writer.finish();
|
|
460
|
+
await handle.sync();
|
|
461
|
+
await handle.close();
|
|
462
|
+
out = null;
|
|
463
|
+
try {
|
|
464
|
+
await link(partial, path);
|
|
465
|
+
} catch (error) {
|
|
466
|
+
throw error.code === "EEXIST" ? new CliError("refused", `${path} exists: choose another name`) : error;
|
|
467
|
+
}
|
|
468
|
+
return {
|
|
469
|
+
path,
|
|
470
|
+
bytes: written,
|
|
471
|
+
files: files.length,
|
|
472
|
+
protection: unsealed ? "none" : "passphrase",
|
|
473
|
+
...light && {
|
|
474
|
+
light: true,
|
|
475
|
+
leftOut: leftOut.length,
|
|
476
|
+
leftOutBytes
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
} finally {
|
|
480
|
+
await out?.close().catch(() => {});
|
|
481
|
+
await rm(partial, { force: true });
|
|
482
|
+
await rm(store, { force: true });
|
|
483
|
+
}
|
|
484
|
+
} };
|
|
485
|
+
async function openFile(path) {
|
|
486
|
+
try {
|
|
487
|
+
return await open(path, "wx", 384);
|
|
488
|
+
} catch (error) {
|
|
489
|
+
if (error.code === "EEXIST") {
|
|
490
|
+
await rm(path, { force: true });
|
|
491
|
+
return open(path, "wx", 384);
|
|
492
|
+
}
|
|
493
|
+
throw error;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
/** A file on disk as a backup's source: read in ranges, never whole. A folder opens, and is refused as one here. */
|
|
497
|
+
async function fileSource(path) {
|
|
498
|
+
const handle = await open(path, "r");
|
|
499
|
+
const found = await handle.stat();
|
|
500
|
+
if (found.isDirectory()) {
|
|
501
|
+
await handle.close();
|
|
502
|
+
throw new CliError("bad_request", `${path} is a folder: name a file`);
|
|
503
|
+
}
|
|
504
|
+
const { size } = found;
|
|
505
|
+
return {
|
|
506
|
+
size,
|
|
507
|
+
read: async (offset, length) => {
|
|
508
|
+
const buffer = new Uint8Array(Math.max(0, Math.min(length, size - offset)));
|
|
509
|
+
let got = 0;
|
|
510
|
+
while (got < buffer.length) {
|
|
511
|
+
const { bytesRead } = await handle.read(buffer, got, buffer.length - got, offset + got);
|
|
512
|
+
if (!bytesRead) break;
|
|
513
|
+
got += bytesRead;
|
|
514
|
+
}
|
|
515
|
+
return buffer.subarray(0, got);
|
|
516
|
+
},
|
|
517
|
+
close: () => handle.close()
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
/** A version 1 backup is one JSON object, and `seal` writes its format first. */
|
|
521
|
+
var VERSION_1_START = /^\s*\{\s*"format"\s*:\s*"ghostly-backup"/;
|
|
522
|
+
/**
|
|
523
|
+
* Whether the backup at `file` is sealed with a passphrase or was made without one, read from its clear header.
|
|
524
|
+
* A file that is neither version is refused here, before any passphrase is asked for: the path is what is wrong.
|
|
525
|
+
*/
|
|
526
|
+
async function backupFileProtection(file) {
|
|
527
|
+
const source = await fileSource(file);
|
|
528
|
+
try {
|
|
529
|
+
let found;
|
|
530
|
+
try {
|
|
531
|
+
found = await readBackupHeader(source);
|
|
532
|
+
} catch (error) {
|
|
533
|
+
throw new CliError("refused", error instanceof Error ? error.message : String(error));
|
|
534
|
+
}
|
|
535
|
+
if (found) return found.header.protection;
|
|
536
|
+
if (!VERSION_1_START.test(new TextDecoder().decode(await source.read(0, 256)))) throw new CliError("refused", "This is not a Ghostly backup");
|
|
537
|
+
return "passphrase";
|
|
538
|
+
} finally {
|
|
539
|
+
await source.close();
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
/** Where a path named by a backup lands inside the new profile's files folder; never outside it. */
|
|
543
|
+
function inside(root, path) {
|
|
544
|
+
const target = typeof path === "string" && path ? resolve(root, path) : "";
|
|
545
|
+
if (!target.startsWith(resolve(root) + sep)) throw new CliError("refused", "The backup names a file outside its folder");
|
|
546
|
+
return target;
|
|
547
|
+
}
|
|
548
|
+
/** While a restore writes it, the profile's folder is not a profile: no profile name starts with a dot (`listProfiles`). */
|
|
549
|
+
var RESTORING = /^\.restoring-(.+)-(\d+)$/;
|
|
550
|
+
/**
|
|
551
|
+
* The folder a restore writes into, beside the profiles: it takes the profile's name only once everything is in it,
|
|
552
|
+
* so a restore that is stopped at any moment (Ctrl-C, a kill, the power) leaves no profile, whole or half-made.
|
|
553
|
+
* What an earlier restore that was stopped left behind is removed here, once the process that made it is gone.
|
|
554
|
+
*/
|
|
555
|
+
async function restoringFolder(home, name) {
|
|
556
|
+
const root = join(home, "profiles");
|
|
557
|
+
await mkdir(root, {
|
|
558
|
+
recursive: true,
|
|
559
|
+
mode: 448
|
|
560
|
+
});
|
|
561
|
+
for (const entry of await readdir(root)) {
|
|
562
|
+
const pid = Number(RESTORING.exec(entry)?.[2]);
|
|
563
|
+
if (!pid) continue;
|
|
564
|
+
let alive = pid === process.pid;
|
|
565
|
+
if (!alive) try {
|
|
566
|
+
process.kill(pid, 0);
|
|
567
|
+
alive = true;
|
|
568
|
+
} catch (error) {
|
|
569
|
+
alive = error.code === "EPERM";
|
|
570
|
+
}
|
|
571
|
+
if (!alive) await rm(join(root, entry), {
|
|
572
|
+
recursive: true,
|
|
573
|
+
force: true
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
const dir = join(root, `.restoring-${name}-${process.pid}`);
|
|
577
|
+
await rm(dir, {
|
|
578
|
+
recursive: true,
|
|
579
|
+
force: true
|
|
580
|
+
});
|
|
581
|
+
await mkdir(dir, { mode: 448 });
|
|
582
|
+
return {
|
|
583
|
+
...profilePaths(home, name),
|
|
584
|
+
dir,
|
|
585
|
+
db: join(dir, "db"),
|
|
586
|
+
files: join(dir, "files")
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* The folder is whole: marked as a restored copy (`RESTORED_MARK`, which names the files a light backup left out),
|
|
591
|
+
* then given the profile's name. Never over a profile.
|
|
592
|
+
*/
|
|
593
|
+
async function finishRestore(home, name, work, leftOut = []) {
|
|
594
|
+
const paths = profilePaths(home, name);
|
|
595
|
+
await writeFile(join(work.dir, RESTORED_MARK), leftOut.length ? JSON.stringify({ leftOut }) : "", { mode: 384 });
|
|
596
|
+
if (existsSync(paths.dir)) throw new CliError("refused", `Profile ${name} exists: restore into a new name`);
|
|
597
|
+
await rename(work.dir, paths.dir);
|
|
598
|
+
return paths;
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Opens the backup at `file` into a new profile (never over an existing one), with its passphrase when it has one.
|
|
602
|
+
* Runs with no engine: nothing is open yet. A backup that is refused, damaged or cut short leaves no profile behind,
|
|
603
|
+
* and neither does a restore that is stopped while it runs (`restoringFolder`). The profile is marked as a restored
|
|
604
|
+
* copy: its engine, the first time it starts, treats the money in it as a restore does (`RESTORED_MARK`).
|
|
605
|
+
*/
|
|
606
|
+
async function restoreProfile(home, name, file, passphrase) {
|
|
607
|
+
if (profileExists(home, name)) throw new CliError("refused", `Profile ${name} exists: restore into a new name`);
|
|
608
|
+
const source = await fileSource(file);
|
|
609
|
+
let work = null;
|
|
610
|
+
try {
|
|
611
|
+
let reader;
|
|
612
|
+
try {
|
|
613
|
+
reader = await BackupReader.open(source, passphrase);
|
|
614
|
+
} catch (error) {
|
|
615
|
+
throw new CliError("refused", error instanceof Error ? error.message : String(error));
|
|
616
|
+
}
|
|
617
|
+
if (!reader) {
|
|
618
|
+
const payload = await readWhole(file, passphrase ?? "");
|
|
619
|
+
work = await restoringFolder(home, name);
|
|
620
|
+
await writeWhole(work, payload);
|
|
621
|
+
return await finishRestore(home, name, work);
|
|
622
|
+
}
|
|
623
|
+
const next = async () => {
|
|
624
|
+
try {
|
|
625
|
+
return await reader.next();
|
|
626
|
+
} catch (error) {
|
|
627
|
+
throw new CliError("refused", error instanceof Error ? error.message : String(error));
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
const parse = (json) => {
|
|
631
|
+
try {
|
|
632
|
+
return JSON.parse(json);
|
|
633
|
+
} catch {
|
|
634
|
+
throw new CliError("refused", "This backup is damaged: it was changed or cut short");
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
const first = await next();
|
|
638
|
+
const head = first?.json === void 0 ? null : parse(first.json);
|
|
639
|
+
if (head?.t !== "profile" || head.format !== FORMAT) throw new CliError("refused", "This backup is not a headless Ghostly profile");
|
|
640
|
+
const leftOut = Array.isArray(head.light?.ids) ? head.light.ids.filter((id) => typeof id === "string") : [];
|
|
641
|
+
work = await restoringFolder(home, name);
|
|
642
|
+
const paths = work;
|
|
643
|
+
await mkdir(paths.db, {
|
|
644
|
+
recursive: true,
|
|
645
|
+
mode: 448
|
|
646
|
+
});
|
|
647
|
+
let out = null, files = 0, bytes = 0, ended = false, stored = false;
|
|
648
|
+
try {
|
|
649
|
+
for (let record = await next(); record; record = await next()) {
|
|
650
|
+
if (ended) throw new CliError("refused", "This backup is damaged: it was changed or cut short");
|
|
651
|
+
if (record.bytes) {
|
|
652
|
+
if (!out || record.bytes.length > out.left) throw new CliError("refused", "This backup is damaged: it was changed or cut short");
|
|
653
|
+
await out.handle.write(record.bytes);
|
|
654
|
+
out.left -= record.bytes.length;
|
|
655
|
+
continue;
|
|
656
|
+
}
|
|
657
|
+
if (out) {
|
|
658
|
+
if (out.left) throw new CliError("refused", "This backup is damaged: it was changed or cut short");
|
|
659
|
+
await out.handle.close();
|
|
660
|
+
out = null;
|
|
661
|
+
}
|
|
662
|
+
const value = parse(record.json);
|
|
663
|
+
if (value.t === "end") {
|
|
664
|
+
ended = value.files === files && value.bytes === bytes;
|
|
665
|
+
if (!ended) throw new CliError("refused", "This backup is damaged: it was changed or cut short");
|
|
666
|
+
continue;
|
|
667
|
+
}
|
|
668
|
+
if (value.t !== "file" || !Number.isSafeInteger(value.size) || value.size < 0) continue;
|
|
669
|
+
let target;
|
|
670
|
+
if (value.kind === "store") {
|
|
671
|
+
target = join(paths.db, "snapshot.bin");
|
|
672
|
+
stored = true;
|
|
673
|
+
} else {
|
|
674
|
+
target = inside(paths.files, value.path);
|
|
675
|
+
await mkdir(dirname(target), {
|
|
676
|
+
recursive: true,
|
|
677
|
+
mode: 448
|
|
678
|
+
});
|
|
679
|
+
files += 1;
|
|
680
|
+
bytes += value.size;
|
|
681
|
+
}
|
|
682
|
+
out = {
|
|
683
|
+
handle: await open(target, "wx", 384),
|
|
684
|
+
left: value.size
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
if (out || !ended || !stored) throw new CliError("refused", "This backup is damaged: it was changed or cut short");
|
|
688
|
+
} finally {
|
|
689
|
+
await out?.handle.close().catch(() => {});
|
|
690
|
+
}
|
|
691
|
+
return await finishRestore(home, name, work, leftOut);
|
|
692
|
+
} catch (error) {
|
|
693
|
+
if (work) await rm(work.dir, {
|
|
694
|
+
recursive: true,
|
|
695
|
+
force: true
|
|
696
|
+
});
|
|
697
|
+
throw error;
|
|
698
|
+
} finally {
|
|
699
|
+
await source.close();
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
/** A version 1 backup: one sealed JSON document, read whole. */
|
|
703
|
+
async function readWhole(file, passphrase) {
|
|
704
|
+
let payload;
|
|
705
|
+
try {
|
|
706
|
+
payload = JSON.parse(await open$1(await readFile(file, "utf8"), passphrase));
|
|
707
|
+
} catch (error) {
|
|
708
|
+
throw new CliError("refused", error instanceof Error ? error.message : String(error));
|
|
709
|
+
}
|
|
710
|
+
if (payload?.format !== FORMAT_1 || typeof payload.store !== "string" || !Array.isArray(payload.files)) throw new CliError("refused", "This backup is not a headless Ghostly profile");
|
|
711
|
+
return payload;
|
|
712
|
+
}
|
|
713
|
+
async function writeWhole(paths, payload) {
|
|
714
|
+
await mkdir(paths.db, {
|
|
715
|
+
recursive: true,
|
|
716
|
+
mode: 448
|
|
717
|
+
});
|
|
718
|
+
await writeFile(join(paths.db, "snapshot.bin"), Buffer.from(payload.store, "base64"), { mode: 384 });
|
|
719
|
+
for (const entry of payload.files) {
|
|
720
|
+
const target = inside(paths.files, entry.path);
|
|
721
|
+
await mkdir(dirname(target), {
|
|
722
|
+
recursive: true,
|
|
723
|
+
mode: 448
|
|
724
|
+
});
|
|
725
|
+
await writeFile(target, Buffer.from(entry.data, "base64"), { mode: 384 });
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
//#endregion
|
|
729
|
+
export { backupFileProtection as n, restoreProfile as r, BACKUP_METHODS as t };
|
|
730
|
+
|
|
731
|
+
//# sourceMappingURL=backup-CnMN2oUK.js.map
|