@lemonriver/core 1.0.2 → 1.1.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/dist/{build-DYN-H65n.js → build-Dm7gVRhb.js} +1 -1
- package/dist/cli/bin.js +3 -3
- package/dist/{config-DW1wSoxa.js → config-CubOIzrs.js} +1950 -1429
- package/dist/{dev-_VDoRfAK.js → dev-DgLeXPqp.js} +1 -1
- package/dist/{preview-CYIGh3Z8.js → preview-CxXQpPk5.js} +1 -1
- package/dist/vite/index.js +1 -1
- package/package.json +1 -1
- package/src/app/components/command/slide-command-menu.tsx +0 -9
- package/src/app/lemonriver/backup/client.ts +25 -1
- package/src/app/lemonriver/backup/model.test.ts +19 -7
- package/src/app/lemonriver/backup/model.ts +63 -18
- package/src/app/lemonriver/board/board-page.tsx +2 -2
- package/src/app/lemonriver/help/model.test.ts +1 -1
- package/src/app/lemonriver/help/model.ts +1 -1
- package/src/app/lemonriver/home/drafts.test.ts +23 -0
- package/src/app/lemonriver/home/drafts.ts +57 -0
- package/src/app/lemonriver/home/home-page.tsx +94 -10
- package/src/app/lemonriver/home/organize-drafts.tsx +192 -0
- package/src/app/lemonriver/outline/outline-page.tsx +8 -1
- package/src/app/lemonriver/settings/backup-restore-dialog.tsx +15 -1
- package/src/app/lemonriver/settings/backup-section.tsx +146 -28
- package/src/app/lemonriver/settings/settings-page.tsx +3 -0
- package/src/app/lemonriver/settings/workspace-card.tsx +53 -0
- package/src/app/lemonriver/settings/workspace.ts +37 -0
- package/src/app/lemonriver/share/editor-share.tsx +55 -0
- package/src/app/lemonriver/share/import-dialog.tsx +14 -0
- package/src/app/lemonriver/share/naming.test.ts +11 -0
- package/src/app/lemonriver/share/naming.ts +6 -1
- package/src/app/lemonriver/slide-extensions.tsx +37 -53
- package/src/app/lemonriver/text/en.ts +46 -11
- package/src/app/lemonriver/text/es.ts +39 -6
- package/src/app/lemonriver/text/ja.ts +39 -6
- package/src/app/lemonriver/text/types.ts +45 -6
- package/src/app/lemonriver/text/zh-cn.ts +47 -11
- package/src/app/lemonriver/text/zh-tw.ts +47 -11
- package/src/app/lemonriver/update/beta-badge.tsx +69 -0
- package/src/app/lib/export-html.ts +5 -3
- package/src/app/lib/export-pptx.ts +5 -4
- package/src/app/routes/slide.tsx +6 -57
|
@@ -15,8 +15,8 @@ import react from "@vitejs/plugin-react";
|
|
|
15
15
|
import { loadConfigFromFile, normalizePath } from "vite";
|
|
16
16
|
import os from "node:os";
|
|
17
17
|
import fg from "fast-glob";
|
|
18
|
-
import { once } from "node:events";
|
|
19
18
|
import { Inflate, Zip, ZipDeflate, ZipPassThrough, strToU8, unzipSync, zipSync } from "fflate";
|
|
19
|
+
import { once } from "node:events";
|
|
20
20
|
import * as t from "@babel/types";
|
|
21
21
|
import { isJSXElement, isJSXFragment } from "@babel/types";
|
|
22
22
|
//#region src/app/lemonriver/templates/knobs.ts
|
|
@@ -177,7 +177,7 @@ const MIN_TYPE_FLOOR = {
|
|
|
177
177
|
min: 16,
|
|
178
178
|
max: 20
|
|
179
179
|
};
|
|
180
|
-
const isRecord$
|
|
180
|
+
const isRecord$5 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
181
181
|
const text$2 = (value) => typeof value === "string" && value.trim() !== "" ? value.trim() : null;
|
|
182
182
|
const KNOB_KEYS = [
|
|
183
183
|
"formal",
|
|
@@ -185,11 +185,11 @@ const KNOB_KEYS = [
|
|
|
185
185
|
"motion"
|
|
186
186
|
];
|
|
187
187
|
function readDesign(raw) {
|
|
188
|
-
if (!isRecord$
|
|
188
|
+
if (!isRecord$5(raw)) return null;
|
|
189
189
|
const palette = raw.palette;
|
|
190
190
|
const fonts = raw.fonts;
|
|
191
191
|
const typeScale = raw.typeScale;
|
|
192
|
-
if (!isRecord$
|
|
192
|
+
if (!isRecord$5(palette) || !isRecord$5(fonts) || !isRecord$5(typeScale)) return null;
|
|
193
193
|
const bg = text$2(palette.bg);
|
|
194
194
|
const ink = text$2(palette.text);
|
|
195
195
|
const accent = text$2(palette.accent);
|
|
@@ -220,9 +220,9 @@ function readDesign(raw) {
|
|
|
220
220
|
};
|
|
221
221
|
}
|
|
222
222
|
function readOverrides(raw) {
|
|
223
|
-
if (!isRecord$
|
|
223
|
+
if (!isRecord$5(raw)) return void 0;
|
|
224
224
|
const out = {};
|
|
225
|
-
if (isRecord$
|
|
225
|
+
if (isRecord$5(raw.palette)) {
|
|
226
226
|
const palette = {};
|
|
227
227
|
for (const key of [
|
|
228
228
|
"bg",
|
|
@@ -234,7 +234,7 @@ function readOverrides(raw) {
|
|
|
234
234
|
}
|
|
235
235
|
if (Object.keys(palette).length > 0) out.palette = palette;
|
|
236
236
|
}
|
|
237
|
-
if (isRecord$
|
|
237
|
+
if (isRecord$5(raw.fonts)) {
|
|
238
238
|
const fonts = {};
|
|
239
239
|
for (const key of ["display", "body"]) {
|
|
240
240
|
const value = text$2(raw.fonts[key]);
|
|
@@ -242,7 +242,7 @@ function readOverrides(raw) {
|
|
|
242
242
|
}
|
|
243
243
|
if (Object.keys(fonts).length > 0) out.fonts = fonts;
|
|
244
244
|
}
|
|
245
|
-
if (isRecord$
|
|
245
|
+
if (isRecord$5(raw.typeScale)) {
|
|
246
246
|
const typeScale = {};
|
|
247
247
|
for (const key of ["hero", "body"]) {
|
|
248
248
|
const value = raw.typeScale[key];
|
|
@@ -255,12 +255,12 @@ function readOverrides(raw) {
|
|
|
255
255
|
return Object.keys(out).length > 0 ? out : void 0;
|
|
256
256
|
}
|
|
257
257
|
function readKnobEnd(raw) {
|
|
258
|
-
if (!isRecord$
|
|
258
|
+
if (!isRecord$5(raw)) return null;
|
|
259
259
|
const label = text$2(raw.label);
|
|
260
260
|
if (!label) return null;
|
|
261
261
|
const design = readOverrides(raw.design);
|
|
262
262
|
const flags = {};
|
|
263
|
-
if (isRecord$
|
|
263
|
+
if (isRecord$5(raw.flags)) {
|
|
264
264
|
for (const [key, value] of Object.entries(raw.flags)) if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") flags[key] = value;
|
|
265
265
|
}
|
|
266
266
|
const end = { label };
|
|
@@ -269,11 +269,11 @@ function readKnobEnd(raw) {
|
|
|
269
269
|
return end;
|
|
270
270
|
}
|
|
271
271
|
function readKnobs(raw) {
|
|
272
|
-
if (!isRecord$
|
|
272
|
+
if (!isRecord$5(raw)) return null;
|
|
273
273
|
const knobs = {};
|
|
274
274
|
for (const key of KNOB_KEYS) {
|
|
275
275
|
const knob = raw[key];
|
|
276
|
-
if (!isRecord$
|
|
276
|
+
if (!isRecord$5(knob)) return null;
|
|
277
277
|
const left = readKnobEnd(knob.left);
|
|
278
278
|
const right = readKnobEnd(knob.right);
|
|
279
279
|
if (!left || !right) return null;
|
|
@@ -288,7 +288,7 @@ function readFonts(raw) {
|
|
|
288
288
|
if (!Array.isArray(raw)) return null;
|
|
289
289
|
const fonts = [];
|
|
290
290
|
for (const entry of raw) {
|
|
291
|
-
if (!isRecord$
|
|
291
|
+
if (!isRecord$5(entry)) return null;
|
|
292
292
|
const file = text$2(entry.file);
|
|
293
293
|
const license = text$2(entry.license);
|
|
294
294
|
if (!file || !license) return null;
|
|
@@ -305,7 +305,7 @@ function readFonts(raw) {
|
|
|
305
305
|
* than half-read.
|
|
306
306
|
*/
|
|
307
307
|
function sanitizeTemplate(raw, expectedId) {
|
|
308
|
-
if (!isRecord$
|
|
308
|
+
if (!isRecord$5(raw)) return {
|
|
309
309
|
ok: false,
|
|
310
310
|
reason: "not a JSON object"
|
|
311
311
|
};
|
|
@@ -419,7 +419,7 @@ function sanitizeTemplate(raw, expectedId) {
|
|
|
419
419
|
}
|
|
420
420
|
/** The board record written when a template is applied; anything else is ignored. */
|
|
421
421
|
function sanitizeAppliedTemplate(raw) {
|
|
422
|
-
if (!isRecord$
|
|
422
|
+
if (!isRecord$5(raw)) return null;
|
|
423
423
|
const id = text$2(raw.id);
|
|
424
424
|
const appliedAt = raw.appliedAt;
|
|
425
425
|
if (!id || typeof appliedAt !== "number" || !Number.isFinite(appliedAt)) return null;
|
|
@@ -430,7 +430,7 @@ function sanitizeAppliedTemplate(raw) {
|
|
|
430
430
|
};
|
|
431
431
|
}
|
|
432
432
|
function sanitizeKnobValues(raw) {
|
|
433
|
-
if (!isRecord$
|
|
433
|
+
if (!isRecord$5(raw)) return {};
|
|
434
434
|
const values = {};
|
|
435
435
|
for (const key of KNOB_KEYS) {
|
|
436
436
|
const value = raw[key];
|
|
@@ -546,7 +546,7 @@ async function loadTemplates(root, log = (line) => console.warn(line)) {
|
|
|
546
546
|
}
|
|
547
547
|
return entries;
|
|
548
548
|
}
|
|
549
|
-
function summarize(entry) {
|
|
549
|
+
function summarize$1(entry) {
|
|
550
550
|
const { id, name, occasions, audience, object, reason, accent, design, knobs, skeletons, previewPages } = entry.template;
|
|
551
551
|
return {
|
|
552
552
|
id,
|
|
@@ -570,7 +570,7 @@ async function readTemplateListing(root) {
|
|
|
570
570
|
return {
|
|
571
571
|
available: root !== null,
|
|
572
572
|
occasions: OCCASIONS,
|
|
573
|
-
templates: entries.map(summarize)
|
|
573
|
+
templates: entries.map(summarize$1)
|
|
574
574
|
};
|
|
575
575
|
}
|
|
576
576
|
/** Only an id the library already accepted can name a file; nothing is joined blindly. */
|
|
@@ -590,7 +590,7 @@ function resolved$2(id) {
|
|
|
590
590
|
return `\0${id}`;
|
|
591
591
|
}
|
|
592
592
|
function generateModule(templates) {
|
|
593
|
-
const meta = templates.map(summarize);
|
|
593
|
+
const meta = templates.map(summarize$1);
|
|
594
594
|
const casesFor = (file, ok) => templates.flatMap((entry) => {
|
|
595
595
|
if (!ok(entry)) return [];
|
|
596
596
|
const abs = path.join(entry.dir, file);
|
|
@@ -1097,12 +1097,15 @@ function registerAgentProviderRoutes(server, ctx) {
|
|
|
1097
1097
|
const BACKUP_FOLDER_NAME = "LemonRiver 备份";
|
|
1098
1098
|
const BACKUP_SETTINGS_FILE = "backup.json";
|
|
1099
1099
|
const BACKUP_PARTIAL_SUFFIX = ".partial";
|
|
1100
|
-
const
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1100
|
+
const VIDEO_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
1101
|
+
"mp4",
|
|
1102
|
+
"mov",
|
|
1103
|
+
"webm",
|
|
1104
|
+
"m4v",
|
|
1105
|
+
"avi",
|
|
1106
|
+
"mkv"
|
|
1107
|
+
]);
|
|
1108
|
+
const DAY_MS = 864e5;
|
|
1106
1109
|
const MIN_RETRY_MS = 36e5;
|
|
1107
1110
|
/** What a backup cannot carry stays under the 4GB a zip holds without ZIP64. */
|
|
1108
1111
|
const BACKUP_TOO_LARGE = "workspace-too-large";
|
|
@@ -1141,14 +1144,21 @@ function isCompressedFile(name) {
|
|
|
1141
1144
|
if (dot <= 0) return false;
|
|
1142
1145
|
return ALREADY_COMPRESSED.has(name.slice(dot + 1).toLowerCase());
|
|
1143
1146
|
}
|
|
1147
|
+
/** A video big enough that copying it into every backup would matter. */
|
|
1148
|
+
function isLargeVideo(name, size) {
|
|
1149
|
+
if (size <= 104857600) return false;
|
|
1150
|
+
const dot = name.lastIndexOf(".");
|
|
1151
|
+
if (dot <= 0) return false;
|
|
1152
|
+
return VIDEO_EXTENSIONS.has(name.slice(dot + 1).toLowerCase());
|
|
1153
|
+
}
|
|
1154
|
+
const SNAPSHOT_FORMAT = "lemonriver-backup";
|
|
1144
1155
|
const EMPTY_SETTINGS = {
|
|
1145
1156
|
folder: null,
|
|
1146
1157
|
auto: false,
|
|
1147
|
-
|
|
1148
|
-
last: null
|
|
1149
|
-
written: []
|
|
1158
|
+
skipLargeVideos: false,
|
|
1159
|
+
last: null
|
|
1150
1160
|
};
|
|
1151
|
-
function isRecord$
|
|
1161
|
+
function isRecord$4(value) {
|
|
1152
1162
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1153
1163
|
}
|
|
1154
1164
|
function isAbsolutePath(value) {
|
|
@@ -1158,29 +1168,42 @@ function readWarnings(raw) {
|
|
|
1158
1168
|
if (!Array.isArray(raw)) return [];
|
|
1159
1169
|
return raw.filter((item) => typeof item === "string" && item !== "");
|
|
1160
1170
|
}
|
|
1171
|
+
function readSkipped$1(raw) {
|
|
1172
|
+
if (!Array.isArray(raw)) return [];
|
|
1173
|
+
const out = [];
|
|
1174
|
+
for (const item of raw) {
|
|
1175
|
+
if (!isRecord$4(item) || typeof item.rel !== "string" || item.rel === "") continue;
|
|
1176
|
+
if (typeof item.size !== "number" || item.size < 0) continue;
|
|
1177
|
+
if (item.reason !== "large-video") continue;
|
|
1178
|
+
out.push({
|
|
1179
|
+
rel: item.rel,
|
|
1180
|
+
size: item.size,
|
|
1181
|
+
reason: item.reason
|
|
1182
|
+
});
|
|
1183
|
+
}
|
|
1184
|
+
return out;
|
|
1185
|
+
}
|
|
1161
1186
|
function readLast(raw) {
|
|
1162
|
-
if (!isRecord$
|
|
1187
|
+
if (!isRecord$4(raw)) return null;
|
|
1163
1188
|
if (typeof raw.at !== "number" || !Number.isFinite(raw.at)) return null;
|
|
1164
1189
|
return {
|
|
1165
1190
|
at: raw.at,
|
|
1166
1191
|
path: typeof raw.path === "string" ? raw.path : "",
|
|
1167
1192
|
size: typeof raw.size === "number" && raw.size >= 0 ? raw.size : 0,
|
|
1193
|
+
addedBytes: typeof raw.addedBytes === "number" && raw.addedBytes >= 0 ? raw.addedBytes : 0,
|
|
1168
1194
|
ok: raw.ok === true,
|
|
1169
1195
|
error: typeof raw.error === "string" && raw.error !== "" ? raw.error : null,
|
|
1170
|
-
warnings: readWarnings(raw.warnings)
|
|
1196
|
+
warnings: readWarnings(raw.warnings),
|
|
1197
|
+
skipped: readSkipped$1(raw.skipped)
|
|
1171
1198
|
};
|
|
1172
1199
|
}
|
|
1173
1200
|
function sanitizeSettings(raw) {
|
|
1174
|
-
if (!isRecord$
|
|
1175
|
-
const folder = typeof raw.folder === "string" && isAbsolutePath(raw.folder) ? raw.folder.replace(/\/+$/, "") : null;
|
|
1176
|
-
const keep = typeof raw.keep === "number" && KEEP_CHOICES.includes(raw.keep) ? raw.keep : 7;
|
|
1177
|
-
const written = Array.isArray(raw.written) ? raw.written.filter((name) => typeof name === "string" && name !== "" && !name.includes("/")) : [];
|
|
1201
|
+
if (!isRecord$4(raw)) return { ...EMPTY_SETTINGS };
|
|
1178
1202
|
return {
|
|
1179
|
-
folder,
|
|
1203
|
+
folder: typeof raw.folder === "string" && isAbsolutePath(raw.folder) ? raw.folder.replace(/\/+$/, "") : null,
|
|
1180
1204
|
auto: raw.auto === true,
|
|
1181
|
-
|
|
1182
|
-
last: readLast(raw.last)
|
|
1183
|
-
written: [...new Set(written)]
|
|
1205
|
+
skipLargeVideos: raw.skipLargeVideos === true,
|
|
1206
|
+
last: readLast(raw.last)
|
|
1184
1207
|
};
|
|
1185
1208
|
}
|
|
1186
1209
|
/**
|
|
@@ -1231,7 +1254,7 @@ const PREFS_KEYS = [
|
|
|
1231
1254
|
];
|
|
1232
1255
|
/** Keeps the whitelist's keys only: auto-approve and sessions never travel. */
|
|
1233
1256
|
function sanitizePrefs$1(raw) {
|
|
1234
|
-
if (!isRecord$
|
|
1257
|
+
if (!isRecord$4(raw)) return {};
|
|
1235
1258
|
const out = {};
|
|
1236
1259
|
for (const key of PREFS_KEYS) {
|
|
1237
1260
|
const value = raw[key];
|
|
@@ -1901,11 +1924,11 @@ const SHARE_ROOT = "LemonRiver-files";
|
|
|
1901
1924
|
const SHARE_MANIFEST = "manifest.json";
|
|
1902
1925
|
const SHARE_APP_NAME = "LemonRiver";
|
|
1903
1926
|
const KIND = ["share", "backup"];
|
|
1904
|
-
function isRecord$
|
|
1927
|
+
function isRecord$3(value) {
|
|
1905
1928
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1906
1929
|
}
|
|
1907
1930
|
function readDeck$1(raw) {
|
|
1908
|
-
if (!isRecord$
|
|
1931
|
+
if (!isRecord$3(raw)) return null;
|
|
1909
1932
|
const { slideId, title, shareId } = raw;
|
|
1910
1933
|
if (typeof slideId !== "string" || slideId === "") return null;
|
|
1911
1934
|
if (typeof title !== "string") return null;
|
|
@@ -1926,7 +1949,7 @@ function readDeck$1(raw) {
|
|
|
1926
1949
|
* answer, because the package may still be perfectly well formed.
|
|
1927
1950
|
*/
|
|
1928
1951
|
function sanitizeManifest(raw) {
|
|
1929
|
-
if (!isRecord$
|
|
1952
|
+
if (!isRecord$3(raw)) return {
|
|
1930
1953
|
ok: false,
|
|
1931
1954
|
code: "bad-manifest",
|
|
1932
1955
|
detail: "manifest is not an object"
|
|
@@ -1942,7 +1965,7 @@ function sanitizeManifest(raw) {
|
|
|
1942
1965
|
code: "bad-manifest",
|
|
1943
1966
|
detail: "manifest has no usable version"
|
|
1944
1967
|
};
|
|
1945
|
-
const app = isRecord$
|
|
1968
|
+
const app = isRecord$3(raw.app) ? raw.app : {};
|
|
1946
1969
|
const appVersion = typeof app.version === "string" ? app.version : "";
|
|
1947
1970
|
if (version > 1) return {
|
|
1948
1971
|
ok: false,
|
|
@@ -1971,7 +1994,7 @@ function sanitizeManifest(raw) {
|
|
|
1971
1994
|
};
|
|
1972
1995
|
decks.push(deck);
|
|
1973
1996
|
}
|
|
1974
|
-
if (!isRecord$
|
|
1997
|
+
if (!isRecord$3(raw.files)) return {
|
|
1975
1998
|
ok: false,
|
|
1976
1999
|
code: "bad-manifest",
|
|
1977
2000
|
detail: "manifest lists no files"
|
|
@@ -2381,1507 +2404,1890 @@ async function moveBoardRecord(userCwd, fromId, toId) {
|
|
|
2381
2404
|
} catch {}
|
|
2382
2405
|
}
|
|
2383
2406
|
//#endregion
|
|
2384
|
-
//#region src/lemonriver/backup/
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
const
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
const DEFLATE = 8;
|
|
2395
|
-
/** Streams entries into a zip on disk; nothing larger than a chunk is held in memory. */
|
|
2396
|
-
var ZipWriter = class {
|
|
2397
|
-
zip;
|
|
2398
|
-
out;
|
|
2399
|
-
blocked = null;
|
|
2400
|
-
failure = null;
|
|
2401
|
-
failureSignal;
|
|
2402
|
-
rejectFailure;
|
|
2403
|
-
done;
|
|
2404
|
-
written = 0;
|
|
2405
|
-
constructor(target) {
|
|
2406
|
-
this.out = createWriteStream(target);
|
|
2407
|
-
this.failureSignal = new Promise((_, reject) => {
|
|
2408
|
-
this.rejectFailure = reject;
|
|
2409
|
-
});
|
|
2410
|
-
this.failureSignal.catch(() => {});
|
|
2411
|
-
this.done = new Promise((resolve, reject) => {
|
|
2412
|
-
let finished = false;
|
|
2413
|
-
this.out.on("finish", () => {
|
|
2414
|
-
finished = true;
|
|
2415
|
-
resolve();
|
|
2416
|
-
});
|
|
2417
|
-
this.out.on("close", () => {
|
|
2418
|
-
if (!finished) reject(/* @__PURE__ */ new Error("the backup file closed before it was written"));
|
|
2419
|
-
});
|
|
2420
|
-
this.out.on("error", reject);
|
|
2421
|
-
});
|
|
2422
|
-
this.done.catch(() => {});
|
|
2423
|
-
this.zip = new Zip((err, chunk, final) => {
|
|
2424
|
-
if (err) {
|
|
2425
|
-
this.fail(err);
|
|
2426
|
-
this.out.destroy(err);
|
|
2427
|
-
return;
|
|
2428
|
-
}
|
|
2429
|
-
this.written += chunk.length;
|
|
2430
|
-
if (this.written > ZIP64_MARK) {
|
|
2431
|
-
this.fail(/* @__PURE__ */ new Error("the backup grew past 4GB while it was written"));
|
|
2432
|
-
this.out.destroy();
|
|
2433
|
-
return;
|
|
2434
|
-
}
|
|
2435
|
-
if (!this.out.write(Buffer.from(chunk))) this.blocked = once(this.out, "drain");
|
|
2436
|
-
if (final) this.out.end();
|
|
2437
|
-
});
|
|
2438
|
-
this.out.on("error", (err) => this.fail(err));
|
|
2439
|
-
}
|
|
2440
|
-
fail(err) {
|
|
2441
|
-
if (this.failure) return;
|
|
2442
|
-
this.failure = err;
|
|
2443
|
-
this.rejectFailure(err);
|
|
2444
|
-
}
|
|
2445
|
-
async ready() {
|
|
2446
|
-
if (this.blocked) {
|
|
2447
|
-
const wait = this.blocked;
|
|
2448
|
-
this.blocked = null;
|
|
2449
|
-
await Promise.race([wait, this.failureSignal]);
|
|
2450
|
-
}
|
|
2451
|
-
if (this.failure) throw this.failure;
|
|
2452
|
-
}
|
|
2453
|
-
entry(name) {
|
|
2454
|
-
return isCompressedFile(name) ? new ZipPassThrough(name) : new ZipDeflate(name, { level: 6 });
|
|
2455
|
-
}
|
|
2456
|
-
async addFile(name, file, limits = BACKUP_LIMITS) {
|
|
2457
|
-
const entry = this.entry(name);
|
|
2458
|
-
this.zip.add(entry);
|
|
2459
|
-
const hash = createHash("sha256");
|
|
2460
|
-
let size = 0;
|
|
2461
|
-
for await (const chunk of createReadStream(file, { highWaterMark: CHUNK })) {
|
|
2462
|
-
size += chunk.length;
|
|
2463
|
-
if (size > limits.fileBytes) throw new Error(BACKUP_TOO_LARGE);
|
|
2464
|
-
hash.update(chunk);
|
|
2465
|
-
entry.push(chunk, false);
|
|
2466
|
-
await this.ready();
|
|
2467
|
-
}
|
|
2468
|
-
entry.push(/* @__PURE__ */ new Uint8Array(0), true);
|
|
2469
|
-
await this.ready();
|
|
2470
|
-
return {
|
|
2471
|
-
sha: hash.digest("hex"),
|
|
2472
|
-
size
|
|
2473
|
-
};
|
|
2474
|
-
}
|
|
2475
|
-
async addBytes(name, bytes) {
|
|
2476
|
-
const entry = this.entry(name);
|
|
2477
|
-
this.zip.add(entry);
|
|
2478
|
-
entry.push(bytes, true);
|
|
2479
|
-
await this.ready();
|
|
2480
|
-
return {
|
|
2481
|
-
sha: createHash("sha256").update(bytes).digest("hex"),
|
|
2482
|
-
size: bytes.length
|
|
2483
|
-
};
|
|
2484
|
-
}
|
|
2485
|
-
async close() {
|
|
2486
|
-
this.zip.end();
|
|
2487
|
-
await this.done;
|
|
2488
|
-
return this.written;
|
|
2489
|
-
}
|
|
2490
|
-
abort() {
|
|
2491
|
-
this.fail(/* @__PURE__ */ new Error("the backup was abandoned"));
|
|
2492
|
-
this.out.destroy();
|
|
2493
|
-
}
|
|
2494
|
-
};
|
|
2495
|
-
function concat(chunks) {
|
|
2496
|
-
let length = 0;
|
|
2497
|
-
for (const chunk of chunks) length += chunk.length;
|
|
2498
|
-
const out = new Uint8Array(length);
|
|
2499
|
-
let offset = 0;
|
|
2500
|
-
for (const chunk of chunks) {
|
|
2501
|
-
out.set(chunk, offset);
|
|
2502
|
-
offset += chunk.length;
|
|
2503
|
-
}
|
|
2504
|
-
return out;
|
|
2407
|
+
//#region src/app/lemonriver/backup/guide.ts
|
|
2408
|
+
/**
|
|
2409
|
+
* The how-to-restore page inside a backup zip. Same rules as the share guide:
|
|
2410
|
+
* standalone, no script, every string escaped.
|
|
2411
|
+
*/
|
|
2412
|
+
const RELEASES_URL = "https://github.com/SegawaBeer/lemonriver-slide/releases/latest";
|
|
2413
|
+
function restoreGuideFileName(locale) {
|
|
2414
|
+
if (locale === "zh-CN") return "怎么恢复.html";
|
|
2415
|
+
if (locale === "zh-TW") return "怎麼還原.html";
|
|
2416
|
+
return "How to restore.html";
|
|
2505
2417
|
}
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
if (bytesRead === 0) break;
|
|
2512
|
-
read += bytesRead;
|
|
2513
|
-
}
|
|
2514
|
-
if (read !== length) throw new Error("the archive was cut short");
|
|
2515
|
-
return buffer;
|
|
2418
|
+
function formatSize(bytes) {
|
|
2419
|
+
if (bytes >= 1073741824) return `${(bytes / 1073741824).toFixed(1)} GB`;
|
|
2420
|
+
if (bytes >= 1048576) return `${Math.round(bytes / 1048576)} MB`;
|
|
2421
|
+
if (bytes >= 1024) return `${Math.round(bytes / 1024)} KB`;
|
|
2422
|
+
return `${bytes} B`;
|
|
2516
2423
|
}
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2424
|
+
const PHRASES = {
|
|
2425
|
+
"zh-CN": {
|
|
2426
|
+
lang: "zh-CN",
|
|
2427
|
+
heading: (date) => `LemonRiver 备份 · ${date}`,
|
|
2428
|
+
sub: (decks, size, version) => `${decks} 份演示 · ${size} · 用 LemonRiver ${version} 备份`,
|
|
2429
|
+
steps: (url) => [
|
|
2430
|
+
`打开 LemonRiver(换了电脑就先装好,下载地址 ${url} )。`,
|
|
2431
|
+
"打开「设置 → 备份」,点「从备份恢复…」,选这个压缩包。",
|
|
2432
|
+
"勾选要恢复的演示,点「恢复」。"
|
|
2433
|
+
],
|
|
2434
|
+
replaces: "你电脑上同名的演示会先移进回收站,30 天内能找回。",
|
|
2435
|
+
footer: "不要解开这个压缩包改里面的文件,改了会恢复失败。"
|
|
2436
|
+
},
|
|
2437
|
+
"zh-TW": {
|
|
2438
|
+
lang: "zh-TW",
|
|
2439
|
+
heading: (date) => `LemonRiver 備份 · ${date}`,
|
|
2440
|
+
sub: (decks, size, version) => `${decks} 份演示 · ${size} · 用 LemonRiver ${version} 備份`,
|
|
2441
|
+
steps: (url) => [
|
|
2442
|
+
`打開 LemonRiver(換了電腦就先裝好,下載位址 ${url} )。`,
|
|
2443
|
+
"打開「設定 → 備份」,點「從備份還原…」,選這個壓縮檔。",
|
|
2444
|
+
"勾選要還原的演示,點「還原」。"
|
|
2445
|
+
],
|
|
2446
|
+
replaces: "你電腦上同名的演示會先移進回收站,30 天內能找回。",
|
|
2447
|
+
footer: "不要解開這個壓縮檔改裡面的檔案,改了會還原失敗。"
|
|
2448
|
+
},
|
|
2449
|
+
en: {
|
|
2450
|
+
lang: "en",
|
|
2451
|
+
heading: (date) => `LemonRiver backup · ${date}`,
|
|
2452
|
+
sub: (decks, size, version) => `${decks} decks · ${size} · Made with LemonRiver ${version}`,
|
|
2453
|
+
steps: (url) => [
|
|
2454
|
+
`Open LemonRiver. On a new computer, install it first: ${url}.`,
|
|
2455
|
+
"Open Settings → Backup, click “Restore from backup…”, and pick this zip.",
|
|
2456
|
+
"Tick the decks you want back and click “Restore”."
|
|
2457
|
+
],
|
|
2458
|
+
replaces: "A deck with the same name on your computer goes to the recycle bin first and can be brought back for 30 days.",
|
|
2459
|
+
footer: "Do not unpack the zip and edit what is inside; the restore will fail."
|
|
2523
2460
|
}
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
return
|
|
2461
|
+
};
|
|
2462
|
+
function formatDateTime(createdAt) {
|
|
2463
|
+
const date = new Date(createdAt);
|
|
2464
|
+
const pad = (value) => String(value).padStart(2, "0");
|
|
2465
|
+
return `${`${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`} ${`${pad(date.getHours())}:${pad(date.getMinutes())}`}`;
|
|
2529
2466
|
}
|
|
2530
|
-
function
|
|
2531
|
-
|
|
2467
|
+
function buildRestoreGuide(input) {
|
|
2468
|
+
const p = PHRASES[input.locale];
|
|
2469
|
+
const heading = htmlEscape(p.heading(formatDateTime(input.createdAt)));
|
|
2470
|
+
const sub = htmlEscape(p.sub(input.decks, formatSize(input.sizeBytes), input.appVersion));
|
|
2471
|
+
const steps = p.steps(RELEASES_URL).map((step) => htmlEscape(step).replace(RELEASES_URL, `<a href="${RELEASES_URL}">${RELEASES_URL}</a>`));
|
|
2472
|
+
return `<!doctype html>
|
|
2473
|
+
<html lang="${p.lang}">
|
|
2474
|
+
<head>
|
|
2475
|
+
<meta charset="utf-8">
|
|
2476
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
2477
|
+
<title>${heading}</title>
|
|
2478
|
+
<style>
|
|
2479
|
+
:root { color-scheme: light dark; }
|
|
2480
|
+
body { margin: 0 auto; padding: 40px 24px 72px; max-width: 680px; font: 16px/1.7 -apple-system, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif; color: #1c1c1e; background: #fff; }
|
|
2481
|
+
h1 { font-size: 28px; line-height: 1.25; margin: 0 0 8px; }
|
|
2482
|
+
.sub { margin: 0 0 32px; color: #6b6b70; font-size: 14px; }
|
|
2483
|
+
ol { margin: 0; padding-left: 1.4em; }
|
|
2484
|
+
li { margin: 6px 0; }
|
|
2485
|
+
.note { margin: 16px 0 0; padding: 12px 14px; border-radius: 10px; background: #f2f2f7; }
|
|
2486
|
+
footer { margin-top: 40px; padding-top: 16px; border-top: 1px solid #e5e5ea; color: #6b6b70; font-size: 14px; }
|
|
2487
|
+
a { color: #0a58ca; }
|
|
2488
|
+
@media (prefers-color-scheme: dark) {
|
|
2489
|
+
body { color: #f2f2f7; background: #1c1c1e; }
|
|
2490
|
+
.sub, footer { color: #a1a1a6; }
|
|
2491
|
+
.note { background: #2c2c2e; }
|
|
2492
|
+
footer { border-top-color: #2c2c2e; }
|
|
2493
|
+
a { color: #6cb2ff; }
|
|
2532
2494
|
}
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
const mode = directory.readUInt32LE(at + 38) >>> 16;
|
|
2570
|
-
at = nameStart + nameLength + extraLength + commentLength;
|
|
2571
|
-
if (size0 === ZIP64_MARK || compressedSize === ZIP64_MARK || localAt === ZIP64_MARK) throw new Error(`${name} is too large to read as a backup`);
|
|
2572
|
-
if (method !== DEFLATE && method !== 0) throw unsupported(name);
|
|
2573
|
-
if (size0 > limits.fileBytes) throw new Error(`${name} is too large to read as a backup`);
|
|
2574
|
-
total += size0;
|
|
2575
|
-
if (total > limits.totalBytes) throw new Error("this archive is larger than a backup may be");
|
|
2576
|
-
const local = await readRange(handle, localAt, LOCAL_HEADER);
|
|
2577
|
-
if (local.readUInt32LE(0) !== SIG_LOCAL) throw new Error(`${name} is not in this archive`);
|
|
2578
|
-
const dataStart = localAt + LOCAL_HEADER + local.readUInt16LE(26) + local.readUInt16LE(28);
|
|
2579
|
-
if (dataStart + compressedSize > size) throw new Error(`${name} was cut short`);
|
|
2580
|
-
entries.push({
|
|
2581
|
-
name,
|
|
2582
|
-
method,
|
|
2583
|
-
size: size0,
|
|
2584
|
-
compressedSize,
|
|
2585
|
-
dataStart,
|
|
2586
|
-
executable: (mode & 73) !== 0
|
|
2587
|
-
});
|
|
2588
|
-
}
|
|
2589
|
-
return {
|
|
2590
|
-
file,
|
|
2591
|
-
size,
|
|
2592
|
-
entries
|
|
2593
|
-
};
|
|
2594
|
-
} finally {
|
|
2595
|
-
await handle.close();
|
|
2495
|
+
</style>
|
|
2496
|
+
</head>
|
|
2497
|
+
<body>
|
|
2498
|
+
<h1>${heading}</h1>
|
|
2499
|
+
<p class="sub">${sub}</p>
|
|
2500
|
+
<ol>
|
|
2501
|
+
${steps.map((step) => `<li>${step}</li>`).join("\n")}
|
|
2502
|
+
</ol>
|
|
2503
|
+
<p class="note">${htmlEscape(p.replaces)}</p>
|
|
2504
|
+
<footer>${htmlEscape(p.footer)}</footer>
|
|
2505
|
+
</body>
|
|
2506
|
+
</html>
|
|
2507
|
+
`;
|
|
2508
|
+
}
|
|
2509
|
+
//#endregion
|
|
2510
|
+
//#region src/app/lemonriver/share/naming.ts
|
|
2511
|
+
const RESERVED = /* @__PURE__ */ new Set([
|
|
2512
|
+
"/",
|
|
2513
|
+
"\\",
|
|
2514
|
+
":",
|
|
2515
|
+
":",
|
|
2516
|
+
"*",
|
|
2517
|
+
"?",
|
|
2518
|
+
"\"",
|
|
2519
|
+
"<",
|
|
2520
|
+
">",
|
|
2521
|
+
"|"
|
|
2522
|
+
]);
|
|
2523
|
+
const MAX_BASE = 80;
|
|
2524
|
+
function clean(raw) {
|
|
2525
|
+
let out = "";
|
|
2526
|
+
for (const character of raw) {
|
|
2527
|
+
const code = character.codePointAt(0) ?? 0;
|
|
2528
|
+
if (code < 32 || code === 127) continue;
|
|
2529
|
+
if (RESERVED.has(character)) continue;
|
|
2530
|
+
out += character;
|
|
2596
2531
|
}
|
|
2532
|
+
return out.replace(/\s+/g, " ").trim().slice(0, MAX_BASE).trim();
|
|
2597
2533
|
}
|
|
2598
|
-
/**
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2534
|
+
/** What the user can call a file without tripping over macOS, Windows or Finder. */
|
|
2535
|
+
function safeFileName(raw, fallback) {
|
|
2536
|
+
const name = clean(raw);
|
|
2537
|
+
if (name !== "") return name;
|
|
2538
|
+
const fallbackName = clean(fallback);
|
|
2539
|
+
return fallbackName !== "" ? fallbackName : "LemonRiver";
|
|
2540
|
+
}
|
|
2541
|
+
function two(n) {
|
|
2542
|
+
return String(n).padStart(2, "0");
|
|
2543
|
+
}
|
|
2544
|
+
function dayStamp(now) {
|
|
2545
|
+
const date = new Date(now);
|
|
2546
|
+
return `${date.getFullYear()}-${two(date.getMonth() + 1)}-${two(date.getDate())}`;
|
|
2547
|
+
}
|
|
2548
|
+
/** One deck is named after itself; a set of them is a backup with a date. */
|
|
2549
|
+
function packageBaseName(decks, now) {
|
|
2550
|
+
if (decks.length === 1) {
|
|
2551
|
+
const [deck] = decks;
|
|
2552
|
+
return safeFileName(deck.title, deck.slideId);
|
|
2606
2553
|
}
|
|
2607
|
-
|
|
2608
|
-
const stream = createReadStream(index.file, {
|
|
2609
|
-
start: entry.dataStart,
|
|
2610
|
-
end,
|
|
2611
|
-
highWaterMark: CHUNK
|
|
2612
|
-
});
|
|
2613
|
-
let written = 0;
|
|
2614
|
-
let failure = null;
|
|
2615
|
-
let ended = false;
|
|
2616
|
-
const fail = (err) => {
|
|
2617
|
-
if (failure) return;
|
|
2618
|
-
failure = err;
|
|
2619
|
-
stream.destroy();
|
|
2620
|
-
};
|
|
2621
|
-
const deliver = (chunk) => {
|
|
2622
|
-
if (failure) return;
|
|
2623
|
-
written += chunk.length;
|
|
2624
|
-
if (written > entry.size) {
|
|
2625
|
-
fail(/* @__PURE__ */ new Error(`${entry.name} holds more than the archive says`));
|
|
2626
|
-
return;
|
|
2627
|
-
}
|
|
2628
|
-
try {
|
|
2629
|
-
onChunk(chunk);
|
|
2630
|
-
} catch (err) {
|
|
2631
|
-
fail(err);
|
|
2632
|
-
}
|
|
2633
|
-
};
|
|
2634
|
-
const inflate = entry.method === DEFLATE ? new Inflate((chunk) => deliver(chunk)) : null;
|
|
2635
|
-
await new Promise((resolve, reject) => {
|
|
2636
|
-
stream.on("data", (chunk) => {
|
|
2637
|
-
if (failure) return;
|
|
2638
|
-
if (!inflate) {
|
|
2639
|
-
deliver(chunk);
|
|
2640
|
-
return;
|
|
2641
|
-
}
|
|
2642
|
-
try {
|
|
2643
|
-
inflate.push(chunk, false);
|
|
2644
|
-
} catch (err) {
|
|
2645
|
-
fail(err);
|
|
2646
|
-
}
|
|
2647
|
-
});
|
|
2648
|
-
stream.on("error", (err) => {
|
|
2649
|
-
failure ??= err;
|
|
2650
|
-
});
|
|
2651
|
-
stream.on("end", () => {
|
|
2652
|
-
if (!failure && inflate) try {
|
|
2653
|
-
inflate.push(/* @__PURE__ */ new Uint8Array(0), true);
|
|
2654
|
-
} catch (err) {
|
|
2655
|
-
failure = err;
|
|
2656
|
-
}
|
|
2657
|
-
if (!failure && written !== entry.size) failure = /* @__PURE__ */ new Error(`${entry.name} is shorter than the archive says`);
|
|
2658
|
-
ended = true;
|
|
2659
|
-
});
|
|
2660
|
-
stream.on("close", () => {
|
|
2661
|
-
if (failure) reject(failure);
|
|
2662
|
-
else if (!ended) reject(/* @__PURE__ */ new Error(`${entry.name} stopped before it was read`));
|
|
2663
|
-
else resolve();
|
|
2664
|
-
});
|
|
2665
|
-
});
|
|
2554
|
+
return safeFileName(`LemonRiver 备份 ${dayStamp(now)}`, `LemonRiver-${dayStamp(now)}`);
|
|
2666
2555
|
}
|
|
2667
|
-
/**
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
const kept = /* @__PURE__ */ new Map();
|
|
2675
|
-
let totalSize = 0;
|
|
2676
|
-
for (const entry of index.entries) {
|
|
2677
|
-
const keepBytes = keep(entry.name);
|
|
2678
|
-
const hash = createHash("sha256");
|
|
2679
|
-
const chunks = [];
|
|
2680
|
-
await readZipEntry(index, entry, (chunk) => {
|
|
2681
|
-
totalSize += chunk.length;
|
|
2682
|
-
hash.update(chunk);
|
|
2683
|
-
if (keepBytes) chunks.push(chunk);
|
|
2684
|
-
});
|
|
2685
|
-
entries.push({
|
|
2686
|
-
name: entry.name,
|
|
2687
|
-
sha: hash.digest("hex"),
|
|
2688
|
-
size: entry.size,
|
|
2689
|
-
executable: entry.executable
|
|
2690
|
-
});
|
|
2691
|
-
if (keepBytes) kept.set(entry.name, concat(chunks));
|
|
2556
|
+
/** Finder and Explorer both treat these as the same name. */
|
|
2557
|
+
function uniqueFileName(base, taken) {
|
|
2558
|
+
const used = new Set([...taken].map((name) => name.toLowerCase()));
|
|
2559
|
+
if (!used.has(base.toLowerCase())) return base;
|
|
2560
|
+
for (let n = 2;; n++) {
|
|
2561
|
+
const candidate = `${base} (${n})`;
|
|
2562
|
+
if (!used.has(candidate.toLowerCase())) return candidate;
|
|
2692
2563
|
}
|
|
2693
|
-
return {
|
|
2694
|
-
entries,
|
|
2695
|
-
kept,
|
|
2696
|
-
totalSize
|
|
2697
|
-
};
|
|
2698
2564
|
}
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
return
|
|
2565
|
+
/** A deck that arrives beside one with the same id gets its own. */
|
|
2566
|
+
function freeImportSlideId(slideId, taken) {
|
|
2567
|
+
const used = new Set([...taken].map((id) => id.toLowerCase()));
|
|
2568
|
+
if (!used.has(slideId.toLowerCase())) return slideId;
|
|
2569
|
+
for (let n = 1;; n++) {
|
|
2570
|
+
const candidate = n === 1 ? `${slideId}-imported` : `${slideId}-imported-${n}`;
|
|
2571
|
+
if (!used.has(candidate.toLowerCase())) return candidate;
|
|
2706
2572
|
}
|
|
2707
2573
|
}
|
|
2574
|
+
const EXTRA_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
2575
|
+
"pdf",
|
|
2576
|
+
"html",
|
|
2577
|
+
"txt"
|
|
2578
|
+
]);
|
|
2579
|
+
/** The PDF and the how-to-open page ride at the top of the zip. */
|
|
2580
|
+
function isValidExtraName(name) {
|
|
2581
|
+
if (name === "" || name.startsWith(".") || name.includes("/") || name.includes("\\")) return false;
|
|
2582
|
+
if (name.includes("\0")) return false;
|
|
2583
|
+
const dot = name.lastIndexOf(".");
|
|
2584
|
+
if (dot <= 0) return false;
|
|
2585
|
+
return EXTRA_EXTENSIONS.has(name.slice(dot + 1).toLowerCase());
|
|
2586
|
+
}
|
|
2708
2587
|
//#endregion
|
|
2709
|
-
//#region src/lemonriver/
|
|
2710
|
-
const
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
return {
|
|
2718
|
-
ok: false,
|
|
2719
|
-
code,
|
|
2720
|
-
detail,
|
|
2721
|
-
...appVersion ? { appVersion } : {}
|
|
2722
|
-
};
|
|
2723
|
-
}
|
|
2724
|
-
const EXECUTABLE_NAME = /\.(?:sh|command)$/i;
|
|
2725
|
-
/** A program, by its name, its packaged app folder, or its own mode bits. */
|
|
2726
|
-
function looksExecutable(rel) {
|
|
2727
|
-
if (EXECUTABLE_NAME.test(rel)) return true;
|
|
2728
|
-
return rel.split("/").slice(0, -1).some((segment) => segment.toLowerCase().endsWith(".app"));
|
|
2729
|
-
}
|
|
2730
|
-
function findPrefix$1(names) {
|
|
2731
|
-
let best = null;
|
|
2732
|
-
for (const name of names) {
|
|
2733
|
-
let prefix = null;
|
|
2734
|
-
if (name === "manifest.json") prefix = "";
|
|
2735
|
-
else if (name === `LemonRiver-files/manifest.json`) prefix = `${SHARE_ROOT}/`;
|
|
2736
|
-
else if (name.endsWith(`/LemonRiver-files/manifest.json`)) prefix = name.slice(0, name.length - 13);
|
|
2737
|
-
if (prefix === null) continue;
|
|
2738
|
-
if (best === null || prefix.length < best.length) best = prefix;
|
|
2739
|
-
}
|
|
2740
|
-
return best;
|
|
2741
|
-
}
|
|
2742
|
-
async function localTitle(userCwd, slideId) {
|
|
2743
|
-
const outline = (await readBoardFile(boardStructurePath(userCwd, slideId)).catch(() => ({}))).outline;
|
|
2744
|
-
if (typeof outline?.brief === "string" && outline.brief.trim() !== "") return outline.brief.trim();
|
|
2745
|
-
for (const name of ENTRY_NAMES$5) try {
|
|
2746
|
-
const source = await fs$1.readFile(path.join(userCwd, "slides", slideId, name), "utf8");
|
|
2747
|
-
const read = readMetaTitleInSource(source);
|
|
2748
|
-
if (read.kind === "found") return read.title;
|
|
2749
|
-
} catch {}
|
|
2750
|
-
return null;
|
|
2588
|
+
//#region src/lemonriver/share/archive.ts
|
|
2589
|
+
const DEFAULT_ZIP_LIMITS = {
|
|
2590
|
+
maxFiles: 5e3,
|
|
2591
|
+
maxFileBytes: 104857600,
|
|
2592
|
+
maxTotalBytes: 524288e3
|
|
2593
|
+
};
|
|
2594
|
+
function sha256(bytes) {
|
|
2595
|
+
return createHash("sha256").update(bytes).digest("hex");
|
|
2751
2596
|
}
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
if (!bytes) return empty;
|
|
2597
|
+
/**
|
|
2598
|
+
* Unpacks without ever trusting a size the archive declares after the fact:
|
|
2599
|
+
* the filter sees each entry's expanded size before fflate allocates it.
|
|
2600
|
+
*/
|
|
2601
|
+
function unpackZip(bytes, limits = DEFAULT_ZIP_LIMITS) {
|
|
2602
|
+
let count = 0;
|
|
2603
|
+
let total = 0;
|
|
2604
|
+
let problem = null;
|
|
2605
|
+
let files;
|
|
2762
2606
|
try {
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2607
|
+
files = unzipSync(bytes, { filter: (file) => {
|
|
2608
|
+
if (problem) return false;
|
|
2609
|
+
if (isJunkPath(file.name)) return false;
|
|
2610
|
+
count += 1;
|
|
2611
|
+
if (count > limits.maxFiles) {
|
|
2612
|
+
problem = {
|
|
2613
|
+
code: "too-many-files",
|
|
2614
|
+
detail: `the package holds more than ${limits.maxFiles} files`
|
|
2615
|
+
};
|
|
2616
|
+
return false;
|
|
2617
|
+
}
|
|
2618
|
+
if (file.originalSize > limits.maxFileBytes) {
|
|
2619
|
+
problem = {
|
|
2620
|
+
code: "too-large",
|
|
2621
|
+
detail: `${file.name} unpacks to more than ${limits.maxFileBytes} bytes`
|
|
2622
|
+
};
|
|
2623
|
+
return false;
|
|
2624
|
+
}
|
|
2625
|
+
total += file.originalSize;
|
|
2626
|
+
if (total > limits.maxTotalBytes) {
|
|
2627
|
+
problem = {
|
|
2628
|
+
code: "too-large",
|
|
2629
|
+
detail: `the package unpacks to more than ${limits.maxTotalBytes} bytes`
|
|
2630
|
+
};
|
|
2631
|
+
return false;
|
|
2632
|
+
}
|
|
2633
|
+
return true;
|
|
2634
|
+
} });
|
|
2635
|
+
} catch {
|
|
2766
2636
|
return {
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2637
|
+
ok: false,
|
|
2638
|
+
code: "not-a-package",
|
|
2639
|
+
detail: "this file is not a zip"
|
|
2770
2640
|
};
|
|
2771
|
-
} catch {
|
|
2772
|
-
return empty;
|
|
2773
|
-
}
|
|
2774
|
-
}
|
|
2775
|
-
/** Validates a backup on disk by reading it once; writes nothing. */
|
|
2776
|
-
async function inspectBackupFile(file, deps) {
|
|
2777
|
-
if (!path.isAbsolute(file) || path.extname(file).toLowerCase() !== ".zip") return failure$1("not-a-package", "a backup is a .zip file");
|
|
2778
|
-
const stat = await fs$1.stat(file).catch(() => null);
|
|
2779
|
-
if (!stat?.isFile()) return failure$1("not-a-package", `${file} is not a file`);
|
|
2780
|
-
let scan;
|
|
2781
|
-
try {
|
|
2782
|
-
scan = await scanZip(file, deps.limits, (name) => name.endsWith(`/manifest.json`) || name.endsWith("/workspace.json"));
|
|
2783
|
-
} catch (err) {
|
|
2784
|
-
return failure$1("not-a-package", String(err?.message ?? err));
|
|
2785
|
-
}
|
|
2786
|
-
const prefix = findPrefix$1(scan.entries.map((entry) => entry.name));
|
|
2787
|
-
if (prefix === null) return failure$1("not-a-package", `no ${SHARE_ROOT}/${SHARE_MANIFEST} in the zip`);
|
|
2788
|
-
const manifestBytes = scan.kept.get(`${prefix}${SHARE_MANIFEST}`);
|
|
2789
|
-
if (!manifestBytes) return failure$1("bad-manifest", `no ${SHARE_ROOT}/${SHARE_MANIFEST} in the zip`);
|
|
2790
|
-
let raw;
|
|
2791
|
-
try {
|
|
2792
|
-
raw = JSON.parse(new TextDecoder().decode(manifestBytes));
|
|
2793
|
-
} catch {
|
|
2794
|
-
return failure$1("bad-manifest", "manifest.json is not readable JSON");
|
|
2795
|
-
}
|
|
2796
|
-
const read = sanitizeManifest(raw);
|
|
2797
|
-
if (!read.ok) return failure$1(read.code, read.detail, read.appVersion);
|
|
2798
|
-
if (read.manifest.kind !== "backup") return failure$1("not-a-package", "this zip is a share package, not a backup");
|
|
2799
|
-
const manifest = read.manifest;
|
|
2800
|
-
const inside = /* @__PURE__ */ new Map();
|
|
2801
|
-
for (const entry of scan.entries) {
|
|
2802
|
-
if (!entry.name.startsWith(prefix)) continue;
|
|
2803
|
-
const rel = entry.name.slice(prefix.length);
|
|
2804
|
-
if (rel === "" || rel === "manifest.json") continue;
|
|
2805
|
-
inside.set(rel, {
|
|
2806
|
-
sha: entry.sha,
|
|
2807
|
-
size: entry.size,
|
|
2808
|
-
executable: entry.executable
|
|
2809
|
-
});
|
|
2810
|
-
}
|
|
2811
|
-
for (const rel of [...Object.keys(manifest.files), ...inside.keys()]) {
|
|
2812
|
-
const problem = packagePathProblem(rel, "backup");
|
|
2813
|
-
if (problem) return failure$1(problem.code, `${rel}: ${problem.detail}`);
|
|
2814
2641
|
}
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
});
|
|
2823
|
-
if (wrong.length > 0) return failure$1("checksum", `${wrong[0]} does not match its checksum`);
|
|
2824
|
-
const workspace = readWorkspace(scan.kept.get(`${prefix}workspace.json`));
|
|
2825
|
-
const assignments = typeof workspace.folders.assignments === "object" && workspace.folders.assignments !== null && !Array.isArray(workspace.folders.assignments) ? workspace.folders.assignments : {};
|
|
2826
|
-
const folders = Array.isArray(workspace.folders.folders) ? workspace.folders.folders.length : 0;
|
|
2827
|
-
const localIds = new Set((await fs$1.readdir(deps.slidesRoot).catch(() => [])).map((id) => id.toLowerCase()));
|
|
2828
|
-
const decks = [];
|
|
2829
|
-
for (const deck of manifest.decks) {
|
|
2830
|
-
const local = localIds.has(deck.slideId.toLowerCase());
|
|
2831
|
-
decks.push({
|
|
2832
|
-
slideId: deck.slideId,
|
|
2833
|
-
title: deck.title,
|
|
2834
|
-
pages: deck.pages,
|
|
2835
|
-
local,
|
|
2836
|
-
localTitle: local ? await localTitle(deps.userCwd, deck.slideId) : null,
|
|
2837
|
-
folderId: typeof assignments[deck.slideId] === "string" ? assignments[deck.slideId] : null
|
|
2838
|
-
});
|
|
2642
|
+
if (problem) {
|
|
2643
|
+
const failed = problem;
|
|
2644
|
+
return {
|
|
2645
|
+
ok: false,
|
|
2646
|
+
code: failed.code,
|
|
2647
|
+
detail: failed.detail
|
|
2648
|
+
};
|
|
2839
2649
|
}
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
for (const [rel, info] of inside) {
|
|
2845
|
-
if (rel.startsWith("assets/")) {
|
|
2846
|
-
assetsCount += 1;
|
|
2847
|
-
assetsSize += info.size;
|
|
2848
|
-
} else if (rel.startsWith("themes/")) themes += 1;
|
|
2849
|
-
if (info.executable || looksExecutable(rel)) executables.push(rel);
|
|
2650
|
+
const out = /* @__PURE__ */ new Map();
|
|
2651
|
+
for (const [name, data] of Object.entries(files)) {
|
|
2652
|
+
if (name.endsWith("/")) continue;
|
|
2653
|
+
out.set(name, data);
|
|
2850
2654
|
}
|
|
2851
2655
|
return {
|
|
2852
2656
|
ok: true,
|
|
2853
|
-
|
|
2854
|
-
workspace,
|
|
2855
|
-
prefix,
|
|
2856
|
-
size: stat.size,
|
|
2857
|
-
preview: {
|
|
2858
|
-
createdAt: manifest.createdAt || workspace.createdAt,
|
|
2859
|
-
app: manifest.app,
|
|
2860
|
-
decks,
|
|
2861
|
-
assets: {
|
|
2862
|
-
count: assetsCount,
|
|
2863
|
-
size: assetsSize
|
|
2864
|
-
},
|
|
2865
|
-
themes,
|
|
2866
|
-
folders,
|
|
2867
|
-
prefs: Object.keys(workspace.prefs).length > 0,
|
|
2868
|
-
size: stat.size,
|
|
2869
|
-
executables
|
|
2870
|
-
}
|
|
2657
|
+
files: out
|
|
2871
2658
|
};
|
|
2872
2659
|
}
|
|
2873
2660
|
//#endregion
|
|
2874
|
-
//#region src/lemonriver/
|
|
2875
|
-
const
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2661
|
+
//#region src/lemonriver/share/pack.ts
|
|
2662
|
+
const ENTRY_NAMES$5 = [
|
|
2663
|
+
"index.tsx",
|
|
2664
|
+
"index.jsx",
|
|
2665
|
+
"index.ts",
|
|
2666
|
+
"index.js"
|
|
2667
|
+
];
|
|
2668
|
+
const SOURCE_RE = /\.(?:tsx|jsx|ts|js|mjs)$/;
|
|
2669
|
+
const ASSET_REF = /@assets\/([^\s'"`\\]+)/g;
|
|
2670
|
+
/** Reads `meta.theme` out of a slide source without running it. */
|
|
2671
|
+
function readMetaThemeInSource(source) {
|
|
2672
|
+
let ast;
|
|
2673
|
+
try {
|
|
2674
|
+
ast = parse(source, {
|
|
2675
|
+
sourceType: "module",
|
|
2676
|
+
plugins: ["typescript", "jsx"],
|
|
2677
|
+
errorRecovery: true
|
|
2678
|
+
});
|
|
2679
|
+
} catch {
|
|
2680
|
+
return null;
|
|
2681
|
+
}
|
|
2682
|
+
const body = ast.program?.body ?? [];
|
|
2683
|
+
for (const statement of body) {
|
|
2684
|
+
if (statement.type !== "ExportNamedDeclaration") continue;
|
|
2685
|
+
const declaration = statement.declaration;
|
|
2686
|
+
if (declaration?.type !== "VariableDeclaration") continue;
|
|
2687
|
+
for (const item of declaration.declarations ?? []) {
|
|
2688
|
+
const id = item.id;
|
|
2689
|
+
if (id?.type !== "Identifier" || id.name !== "meta") continue;
|
|
2690
|
+
const init = item.init;
|
|
2691
|
+
if (init?.type !== "ObjectExpression") return null;
|
|
2692
|
+
for (const property of init.properties ?? []) {
|
|
2693
|
+
if (property.type !== "ObjectProperty" || property.computed) continue;
|
|
2694
|
+
const key = property.key;
|
|
2695
|
+
if ((key?.type === "Identifier" ? key.name : key?.value) !== "theme") continue;
|
|
2696
|
+
const value = property.value;
|
|
2697
|
+
return typeof value?.value === "string" ? value.value : null;
|
|
2698
|
+
}
|
|
2699
|
+
return null;
|
|
2887
2700
|
}
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2701
|
+
}
|
|
2702
|
+
return null;
|
|
2703
|
+
}
|
|
2704
|
+
async function exists(file) {
|
|
2705
|
+
try {
|
|
2706
|
+
await fs$1.access(file);
|
|
2707
|
+
return true;
|
|
2708
|
+
} catch {
|
|
2709
|
+
return false;
|
|
2710
|
+
}
|
|
2711
|
+
}
|
|
2712
|
+
async function walk$2(dir, prefix, out, work, inWork = false) {
|
|
2713
|
+
const entries = await fs$1.readdir(dir, { withFileTypes: true });
|
|
2714
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
2715
|
+
if (entry.name === "node_modules" || entry.name === ".DS_Store" || entry.name.startsWith(".")) continue;
|
|
2716
|
+
const full = path.join(dir, entry.name);
|
|
2717
|
+
const rel = prefix === "" ? entry.name : `${prefix}/${entry.name}`;
|
|
2718
|
+
if (entry.isDirectory()) {
|
|
2719
|
+
await walk$2(full, rel, out, work, inWork || prefix === "" && entry.name.startsWith("_"));
|
|
2720
|
+
continue;
|
|
2721
|
+
}
|
|
2722
|
+
if (!entry.isFile()) continue;
|
|
2723
|
+
out.set(rel, new Uint8Array(await fs$1.readFile(full)));
|
|
2724
|
+
if (inWork) work.add(rel);
|
|
2725
|
+
}
|
|
2726
|
+
}
|
|
2727
|
+
const IMPORT_FROM = /(?:import|export)\s[^'"]*?from\s*['"]([^'"]+)['"]/g;
|
|
2728
|
+
const IMPORT_BARE = /import\s*['"]([^'"]+)['"]/g;
|
|
2729
|
+
const IMPORT_CALL = /(?:import|require)\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
|
|
2730
|
+
const CSS_URL = /url\(\s*['"]?([^'")]+)['"]?\s*\)/g;
|
|
2731
|
+
const EXTENSION_TRIES = [
|
|
2732
|
+
"",
|
|
2733
|
+
".tsx",
|
|
2734
|
+
".ts",
|
|
2735
|
+
".jsx",
|
|
2736
|
+
".js",
|
|
2737
|
+
".mjs",
|
|
2738
|
+
".json"
|
|
2739
|
+
];
|
|
2740
|
+
const INDEX_TRIES = [
|
|
2741
|
+
"/index.tsx",
|
|
2742
|
+
"/index.ts",
|
|
2743
|
+
"/index.jsx",
|
|
2744
|
+
"/index.js"
|
|
2745
|
+
];
|
|
2746
|
+
const TEXT_FILE = /\.(?:tsx|jsx|ts|js|mjs|css|html|md|json)$/i;
|
|
2747
|
+
/** `./kit/shell` may name `kit/shell.tsx`; each suffix is tried before giving up. */
|
|
2748
|
+
function resolveSpecifier(fromFile, files, spec) {
|
|
2749
|
+
const clean = spec.split(/[?#]/)[0].trim();
|
|
2750
|
+
if (clean === "" || /^[a-z]+:/i.test(clean) || clean.startsWith("@") || clean.startsWith("/")) return null;
|
|
2751
|
+
if (!clean.startsWith(".")) return null;
|
|
2752
|
+
const base = path.posix.dirname(fromFile);
|
|
2753
|
+
const joined = path.posix.normalize(path.posix.join(base === "." ? "" : base, clean));
|
|
2754
|
+
if (joined.startsWith("..")) return null;
|
|
2755
|
+
for (const suffix of [...EXTENSION_TRIES, ...INDEX_TRIES]) if (files.has(joined + suffix)) return joined + suffix;
|
|
2756
|
+
return null;
|
|
2757
|
+
}
|
|
2758
|
+
/** The files the deck reaches from its entry, following references file by file. */
|
|
2759
|
+
function usedPaths(files, entry) {
|
|
2760
|
+
const used = /* @__PURE__ */ new Set();
|
|
2761
|
+
if (entry === null) return used;
|
|
2762
|
+
used.add(entry);
|
|
2763
|
+
const queue = [entry];
|
|
2764
|
+
while (queue.length > 0) {
|
|
2765
|
+
const rel = queue.pop();
|
|
2766
|
+
const bytes = files.get(rel);
|
|
2767
|
+
if (!bytes || !TEXT_FILE.test(rel)) continue;
|
|
2768
|
+
const text = new TextDecoder().decode(bytes);
|
|
2769
|
+
for (const pattern of [
|
|
2770
|
+
IMPORT_FROM,
|
|
2771
|
+
IMPORT_BARE,
|
|
2772
|
+
IMPORT_CALL,
|
|
2773
|
+
CSS_URL
|
|
2774
|
+
]) for (const match of text.matchAll(pattern)) {
|
|
2775
|
+
const resolved = resolveSpecifier(rel, files, match[1] ?? "");
|
|
2776
|
+
if (resolved !== null && !used.has(resolved)) {
|
|
2777
|
+
used.add(resolved);
|
|
2778
|
+
queue.push(resolved);
|
|
2908
2779
|
}
|
|
2909
|
-
}
|
|
2780
|
+
}
|
|
2781
|
+
}
|
|
2782
|
+
return used;
|
|
2783
|
+
}
|
|
2784
|
+
async function deckEntry$1(slideDir) {
|
|
2785
|
+
for (const name of ENTRY_NAMES$5) try {
|
|
2910
2786
|
return {
|
|
2911
|
-
|
|
2912
|
-
|
|
2787
|
+
name,
|
|
2788
|
+
source: await fs$1.readFile(path.join(slideDir, name), "utf8")
|
|
2913
2789
|
};
|
|
2790
|
+
} catch {}
|
|
2791
|
+
return null;
|
|
2792
|
+
}
|
|
2793
|
+
function relativeAssetRef(fromFile, target) {
|
|
2794
|
+
const from = path.posix.dirname(fromFile);
|
|
2795
|
+
const rel = path.posix.relative(from === "." ? "" : from, target);
|
|
2796
|
+
return rel.startsWith("..") ? rel : `./${rel}`;
|
|
2797
|
+
}
|
|
2798
|
+
/**
|
|
2799
|
+
* Rewrites `@assets/…` to a path inside the package, copying the global file
|
|
2800
|
+
* next to the deck. The workspace's own sources are never touched.
|
|
2801
|
+
*/
|
|
2802
|
+
async function localizeAssets(opts) {
|
|
2803
|
+
const names = /* @__PURE__ */ new Set();
|
|
2804
|
+
for (const match of opts.source.matchAll(ASSET_REF)) names.add(match[1]);
|
|
2805
|
+
if (names.size === 0) return opts.source;
|
|
2806
|
+
let source = opts.source;
|
|
2807
|
+
for (const name of names) {
|
|
2808
|
+
const globalFile = path.join(opts.globalAssetsRoot, name);
|
|
2809
|
+
let globalBytes;
|
|
2810
|
+
try {
|
|
2811
|
+
globalBytes = new Uint8Array(await fs$1.readFile(globalFile));
|
|
2812
|
+
} catch {
|
|
2813
|
+
opts.warnings.push(`global asset ${name} is missing; left the reference as it was`);
|
|
2814
|
+
continue;
|
|
2815
|
+
}
|
|
2816
|
+
const packaged = `slides/${opts.slideId}/assets/${name}`;
|
|
2817
|
+
const own = opts.packageFiles.get(packaged);
|
|
2818
|
+
let target = `assets/${name}`;
|
|
2819
|
+
if (own) {
|
|
2820
|
+
if (own.length === globalBytes.length && own.every((byte, i) => byte === globalBytes[i])) target = `assets/${name}`;
|
|
2821
|
+
else {
|
|
2822
|
+
const dot = name.lastIndexOf(".");
|
|
2823
|
+
target = `assets/${dot > 0 ? name.slice(0, dot) : name}-shared${dot > 0 ? name.slice(dot) : ""}`;
|
|
2824
|
+
opts.packageFiles.set(`slides/${opts.slideId}/${target}`, globalBytes);
|
|
2825
|
+
}
|
|
2826
|
+
} else opts.packageFiles.set(packaged, globalBytes);
|
|
2827
|
+
source = source.split(`@assets/${name}`).join(relativeAssetRef(opts.relFile, target));
|
|
2914
2828
|
}
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2829
|
+
return source;
|
|
2830
|
+
}
|
|
2831
|
+
async function packDecks(request, deps) {
|
|
2832
|
+
const warnings = [];
|
|
2833
|
+
const decks = [];
|
|
2834
|
+
const packageFiles = /* @__PURE__ */ new Map();
|
|
2835
|
+
for (const extra of request.extras) if (!isValidExtraName(extra.name)) return {
|
|
2836
|
+
ok: false,
|
|
2837
|
+
status: 400,
|
|
2838
|
+
error: `bad extra file name: ${extra.name}`
|
|
2839
|
+
};
|
|
2840
|
+
const badPaths = [];
|
|
2841
|
+
const skipped = [];
|
|
2842
|
+
let workFiles = 0;
|
|
2843
|
+
for (const slideId of request.slideIds) {
|
|
2844
|
+
if (!SHARE_SLIDE_ID_RE.test(slideId)) return {
|
|
2845
|
+
ok: false,
|
|
2846
|
+
status: 400,
|
|
2847
|
+
error: `bad slideId: ${slideId}`
|
|
2848
|
+
};
|
|
2849
|
+
const slideDir = path.join(deps.slidesRoot, slideId);
|
|
2850
|
+
if (!await exists(slideDir)) return {
|
|
2851
|
+
ok: false,
|
|
2852
|
+
status: 404,
|
|
2853
|
+
error: `slide ${slideId} not found`
|
|
2854
|
+
};
|
|
2855
|
+
const collected = /* @__PURE__ */ new Map();
|
|
2856
|
+
const work = /* @__PURE__ */ new Set();
|
|
2857
|
+
await walk$2(slideDir, "", collected, work);
|
|
2858
|
+
const used = usedPaths(collected, ENTRY_NAMES$5.find((name) => collected.has(name)) ?? null);
|
|
2859
|
+
for (const rel of work) if (!used.has(rel)) workFiles += 1;
|
|
2860
|
+
for (const [rel, bytes] of collected) {
|
|
2861
|
+
if (work.has(rel) && !used.has(rel)) continue;
|
|
2862
|
+
const packaged = `slides/${slideId}/${rel}`;
|
|
2863
|
+
const problem = packagePathProblem(packaged);
|
|
2864
|
+
if (problem) {
|
|
2865
|
+
if (used.has(rel)) badPaths.push(`${packaged}: ${problem.detail}`);
|
|
2866
|
+
else skipped.push(packaged);
|
|
2867
|
+
continue;
|
|
2868
|
+
}
|
|
2869
|
+
packageFiles.set(packaged, bytes);
|
|
2870
|
+
}
|
|
2871
|
+
if (badPaths.length > 0) continue;
|
|
2872
|
+
const entry = await deckEntry$1(slideDir);
|
|
2873
|
+
const titleRead = entry ? readMetaTitleInSource(entry.source) : { kind: "missing" };
|
|
2874
|
+
const title = titleRead.kind === "found" ? titleRead.title : safeFileName("", slideId);
|
|
2875
|
+
const theme = entry ? readMetaThemeInSource(entry.source) : null;
|
|
2876
|
+
for (const [rel, data] of [...packageFiles]) {
|
|
2877
|
+
if (!rel.startsWith(`slides/${slideId}/`)) continue;
|
|
2878
|
+
if (!SOURCE_RE.test(rel)) continue;
|
|
2879
|
+
const rewritten = await localizeAssets({
|
|
2880
|
+
source: new TextDecoder().decode(data),
|
|
2881
|
+
relFile: rel.slice(`slides/${slideId}/`.length),
|
|
2882
|
+
slideId,
|
|
2883
|
+
packageFiles,
|
|
2884
|
+
globalAssetsRoot: deps.globalAssetsRoot,
|
|
2885
|
+
warnings
|
|
2886
|
+
});
|
|
2887
|
+
if (rewritten !== new TextDecoder().decode(data)) packageFiles.set(rel, strToU8(rewritten));
|
|
2888
|
+
}
|
|
2889
|
+
if (theme) {
|
|
2890
|
+
for (const suffix of [".md", ".demo.tsx"]) {
|
|
2891
|
+
const file = path.join(deps.userCwd, "themes", `${theme}${suffix}`);
|
|
2892
|
+
const packaged = `themes/${theme}${suffix}`;
|
|
2893
|
+
if (packageFiles.has(packaged)) continue;
|
|
2894
|
+
try {
|
|
2895
|
+
packageFiles.set(packaged, new Uint8Array(await fs$1.readFile(file)));
|
|
2896
|
+
} catch {}
|
|
2897
|
+
}
|
|
2898
|
+
const assetsDir = path.join(deps.userCwd, "themes", `${theme}-assets`);
|
|
2899
|
+
const themeFiles = await fs$1.readdir(assetsDir, { withFileTypes: true }).catch(() => []);
|
|
2900
|
+
for (const entry of themeFiles.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
2901
|
+
if (!entry.isFile() || entry.name.startsWith(".")) continue;
|
|
2902
|
+
const packaged = `themes/${theme}-assets/${entry.name}`;
|
|
2903
|
+
if (packageFiles.has(packaged)) continue;
|
|
2904
|
+
if (packagePathProblem(packaged)) {
|
|
2905
|
+
skipped.push(packaged);
|
|
2906
|
+
continue;
|
|
2907
|
+
}
|
|
2908
|
+
packageFiles.set(packaged, new Uint8Array(await fs$1.readFile(path.join(assetsDir, entry.name))));
|
|
2909
|
+
}
|
|
2926
2910
|
}
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2911
|
+
const boardFile = boardStructurePath(deps.userCwd, slideId);
|
|
2912
|
+
const hasBoard = await exists(boardFile);
|
|
2913
|
+
const record = hasBoard ? await readBoardFile(boardFile) : {};
|
|
2914
|
+
const stored = typeof record.shareId === "string" ? record.shareId : "";
|
|
2915
|
+
const shareId = stored !== "" ? stored : deps.newShareId();
|
|
2916
|
+
if (stored === "") await updateBoardFile(boardFile, { shareId });
|
|
2917
|
+
if (hasBoard) packageFiles.set(`board/${slideId}.json`, strToU8(`${JSON.stringify(stripBoardRecord(record), null, 2)}\n`));
|
|
2918
|
+
const thumbs = request.thumbs[slideId] ?? [];
|
|
2919
|
+
thumbs.forEach((base64, index) => {
|
|
2920
|
+
const name = String(index + 1).padStart(2, "0");
|
|
2921
|
+
packageFiles.set(`thumbs/${slideId}/${name}.jpg`, Buffer.from(base64, "base64"));
|
|
2922
|
+
});
|
|
2923
|
+
decks.push({
|
|
2924
|
+
slideId,
|
|
2925
|
+
title,
|
|
2926
|
+
pages: typeof request.pages[slideId] === "number" ? request.pages[slideId] : null,
|
|
2927
|
+
shareId,
|
|
2928
|
+
theme,
|
|
2929
|
+
thumbs: thumbs.length
|
|
2946
2930
|
});
|
|
2947
2931
|
}
|
|
2948
|
-
|
|
2932
|
+
if (badPaths.length > 0) return {
|
|
2933
|
+
ok: false,
|
|
2934
|
+
status: 400,
|
|
2935
|
+
error: "the deck holds files a package may not carry",
|
|
2936
|
+
paths: badPaths
|
|
2937
|
+
};
|
|
2938
|
+
const files = {};
|
|
2939
|
+
for (const [rel, data] of packageFiles) files[rel] = sha256(data);
|
|
2940
|
+
const manifest = {
|
|
2941
|
+
format: SHARE_FORMAT,
|
|
2942
|
+
version: 1,
|
|
2943
|
+
kind: request.kind,
|
|
2944
|
+
app: {
|
|
2945
|
+
name: "LemonRiver",
|
|
2946
|
+
version: deps.coreVersion
|
|
2947
|
+
},
|
|
2948
|
+
createdAt: deps.now,
|
|
2949
|
+
decks,
|
|
2950
|
+
files
|
|
2951
|
+
};
|
|
2952
|
+
const entries = {};
|
|
2953
|
+
for (const [rel, data] of packageFiles) entries[`${SHARE_ROOT}/${rel}`] = data;
|
|
2954
|
+
entries[`${SHARE_ROOT}/${SHARE_MANIFEST}`] = strToU8(`${JSON.stringify(manifest, null, 2)}\n`);
|
|
2955
|
+
for (const extra of request.extras) entries[extra.name] = Buffer.from(extra.base64, "base64");
|
|
2956
|
+
const zipped = zipSync(entries, { level: 6 });
|
|
2957
|
+
await fs$1.mkdir(deps.sharedRoot, { recursive: true });
|
|
2958
|
+
const taken = await fs$1.readdir(deps.sharedRoot).catch(() => []);
|
|
2959
|
+
const fileName = uniqueFileName(packageBaseName(decks.map((deck) => ({
|
|
2960
|
+
title: deck.title,
|
|
2961
|
+
slideId: deck.slideId
|
|
2962
|
+
})), deps.now), taken.map((name) => name.replace(/\.zip$/i, "")));
|
|
2963
|
+
const target = path.join(deps.sharedRoot, `${fileName}.zip`);
|
|
2964
|
+
await fs$1.writeFile(target, zipped);
|
|
2965
|
+
return {
|
|
2966
|
+
ok: true,
|
|
2967
|
+
path: target,
|
|
2968
|
+
name: `${fileName}.zip`,
|
|
2969
|
+
size: zipped.length,
|
|
2970
|
+
warnings,
|
|
2971
|
+
skipped,
|
|
2972
|
+
workFiles
|
|
2973
|
+
};
|
|
2949
2974
|
}
|
|
2950
2975
|
//#endregion
|
|
2951
|
-
//#region src/
|
|
2952
|
-
|
|
2953
|
-
|
|
2954
|
-
* standalone, no script, every string escaped.
|
|
2955
|
-
*/
|
|
2956
|
-
const RELEASES_URL = "https://github.com/SegawaBeer/lemonriver-slide/releases/latest";
|
|
2957
|
-
function restoreGuideFileName(locale) {
|
|
2958
|
-
if (locale === "zh-CN") return "怎么恢复.html";
|
|
2959
|
-
if (locale === "zh-TW") return "怎麼還原.html";
|
|
2960
|
-
return "How to restore.html";
|
|
2976
|
+
//#region src/lemonriver/backup/settings.ts
|
|
2977
|
+
function backupSettingsFile(userCwd) {
|
|
2978
|
+
return path.join(userCwd, ".open-slide", BACKUP_SETTINGS_FILE);
|
|
2961
2979
|
}
|
|
2962
|
-
function
|
|
2963
|
-
|
|
2964
|
-
if (bytes >= 1048576) return `${Math.round(bytes / 1048576)} MB`;
|
|
2965
|
-
if (bytes >= 1024) return `${Math.round(bytes / 1024)} KB`;
|
|
2966
|
-
return `${bytes} B`;
|
|
2980
|
+
function prefsSnapshotFile(userCwd) {
|
|
2981
|
+
return path.join(userCwd, "node_modules", ".open-slide", "prefs-snapshot.json");
|
|
2967
2982
|
}
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
steps: (url) => [
|
|
2974
|
-
`打开 LemonRiver(换了电脑就先装好,下载地址 ${url} )。`,
|
|
2975
|
-
"打开「设置 → 备份」,点「从备份恢复…」,选这个压缩包。",
|
|
2976
|
-
"勾选要恢复的演示,点「恢复」。"
|
|
2977
|
-
],
|
|
2978
|
-
replaces: "你电脑上同名的演示会先移进回收站,30 天内能找回。",
|
|
2979
|
-
footer: "不要解开这个压缩包改里面的文件,改了会恢复失败。"
|
|
2980
|
-
},
|
|
2981
|
-
"zh-TW": {
|
|
2982
|
-
lang: "zh-TW",
|
|
2983
|
-
heading: (date) => `LemonRiver 備份 · ${date}`,
|
|
2984
|
-
sub: (decks, size, version) => `${decks} 份演示 · ${size} · 用 LemonRiver ${version} 備份`,
|
|
2985
|
-
steps: (url) => [
|
|
2986
|
-
`打開 LemonRiver(換了電腦就先裝好,下載位址 ${url} )。`,
|
|
2987
|
-
"打開「設定 → 備份」,點「從備份還原…」,選這個壓縮檔。",
|
|
2988
|
-
"勾選要還原的演示,點「還原」。"
|
|
2989
|
-
],
|
|
2990
|
-
replaces: "你電腦上同名的演示會先移進回收站,30 天內能找回。",
|
|
2991
|
-
footer: "不要解開這個壓縮檔改裡面的檔案,改了會還原失敗。"
|
|
2992
|
-
},
|
|
2993
|
-
en: {
|
|
2994
|
-
lang: "en",
|
|
2995
|
-
heading: (date) => `LemonRiver backup · ${date}`,
|
|
2996
|
-
sub: (decks, size, version) => `${decks} decks · ${size} · Made with LemonRiver ${version}`,
|
|
2997
|
-
steps: (url) => [
|
|
2998
|
-
`Open LemonRiver. On a new computer, install it first: ${url}.`,
|
|
2999
|
-
"Open Settings → Backup, click “Restore from backup…”, and pick this zip.",
|
|
3000
|
-
"Tick the decks you want back and click “Restore”."
|
|
3001
|
-
],
|
|
3002
|
-
replaces: "A deck with the same name on your computer goes to the recycle bin first and can be brought back for 30 days.",
|
|
3003
|
-
footer: "Do not unpack the zip and edit what is inside; the restore will fail."
|
|
2983
|
+
async function readBackupSettings(userCwd) {
|
|
2984
|
+
try {
|
|
2985
|
+
return sanitizeSettings(JSON.parse(await fs$1.readFile(backupSettingsFile(userCwd), "utf8")));
|
|
2986
|
+
} catch {
|
|
2987
|
+
return sanitizeSettings(null);
|
|
3004
2988
|
}
|
|
3005
|
-
};
|
|
3006
|
-
function formatDateTime(createdAt) {
|
|
3007
|
-
const date = new Date(createdAt);
|
|
3008
|
-
const pad = (value) => String(value).padStart(2, "0");
|
|
3009
|
-
return `${`${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`} ${`${pad(date.getHours())}:${pad(date.getMinutes())}`}`;
|
|
3010
2989
|
}
|
|
3011
|
-
function
|
|
3012
|
-
const
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
const steps = p.steps(RELEASES_URL).map((step) => htmlEscape(step).replace(RELEASES_URL, `<a href="${RELEASES_URL}">${RELEASES_URL}</a>`));
|
|
3016
|
-
return `<!doctype html>
|
|
3017
|
-
<html lang="${p.lang}">
|
|
3018
|
-
<head>
|
|
3019
|
-
<meta charset="utf-8">
|
|
3020
|
-
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
3021
|
-
<title>${heading}</title>
|
|
3022
|
-
<style>
|
|
3023
|
-
:root { color-scheme: light dark; }
|
|
3024
|
-
body { margin: 0 auto; padding: 40px 24px 72px; max-width: 680px; font: 16px/1.7 -apple-system, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif; color: #1c1c1e; background: #fff; }
|
|
3025
|
-
h1 { font-size: 28px; line-height: 1.25; margin: 0 0 8px; }
|
|
3026
|
-
.sub { margin: 0 0 32px; color: #6b6b70; font-size: 14px; }
|
|
3027
|
-
ol { margin: 0; padding-left: 1.4em; }
|
|
3028
|
-
li { margin: 6px 0; }
|
|
3029
|
-
.note { margin: 16px 0 0; padding: 12px 14px; border-radius: 10px; background: #f2f2f7; }
|
|
3030
|
-
footer { margin-top: 40px; padding-top: 16px; border-top: 1px solid #e5e5ea; color: #6b6b70; font-size: 14px; }
|
|
3031
|
-
a { color: #0a58ca; }
|
|
3032
|
-
@media (prefers-color-scheme: dark) {
|
|
3033
|
-
body { color: #f2f2f7; background: #1c1c1e; }
|
|
3034
|
-
.sub, footer { color: #a1a1a6; }
|
|
3035
|
-
.note { background: #2c2c2e; }
|
|
3036
|
-
footer { border-top-color: #2c2c2e; }
|
|
3037
|
-
a { color: #6cb2ff; }
|
|
2990
|
+
async function writeBackupSettings(userCwd, settings) {
|
|
2991
|
+
const file = backupSettingsFile(userCwd);
|
|
2992
|
+
await fs$1.mkdir(path.dirname(file), { recursive: true });
|
|
2993
|
+
await fs$1.writeFile(file, `${JSON.stringify(settings, null, 2)}\n`);
|
|
3038
2994
|
}
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
<h1>${heading}</h1>
|
|
3043
|
-
<p class="sub">${sub}</p>
|
|
3044
|
-
<ol>
|
|
3045
|
-
${steps.map((step) => `<li>${step}</li>`).join("\n")}
|
|
3046
|
-
</ol>
|
|
3047
|
-
<p class="note">${htmlEscape(p.replaces)}</p>
|
|
3048
|
-
<footer>${htmlEscape(p.footer)}</footer>
|
|
3049
|
-
</body>
|
|
3050
|
-
</html>
|
|
3051
|
-
`;
|
|
2995
|
+
/** The chosen folder itself when it already is "LemonRiver 备份", else below it. */
|
|
2996
|
+
function resolveBackupDir(folder) {
|
|
2997
|
+
return path.basename(folder) === "LemonRiver 备份" ? folder : path.join(folder, BACKUP_FOLDER_NAME);
|
|
3052
2998
|
}
|
|
3053
|
-
|
|
3054
|
-
|
|
3055
|
-
|
|
3056
|
-
|
|
3057
|
-
|
|
3058
|
-
":",
|
|
3059
|
-
"*",
|
|
3060
|
-
"?",
|
|
3061
|
-
"\"",
|
|
3062
|
-
"<",
|
|
3063
|
-
">",
|
|
3064
|
-
"|"
|
|
3065
|
-
]);
|
|
3066
|
-
const MAX_BASE = 80;
|
|
3067
|
-
function clean(raw) {
|
|
3068
|
-
let out = "";
|
|
3069
|
-
for (const character of raw) {
|
|
3070
|
-
const code = character.codePointAt(0) ?? 0;
|
|
3071
|
-
if (code < 32 || code === 127) continue;
|
|
3072
|
-
if (RESERVED.has(character)) continue;
|
|
3073
|
-
out += character;
|
|
2999
|
+
async function readPrefsSnapshot(userCwd) {
|
|
3000
|
+
try {
|
|
3001
|
+
return sanitizePrefs$1(JSON.parse(await fs$1.readFile(prefsSnapshotFile(userCwd), "utf8")));
|
|
3002
|
+
} catch {
|
|
3003
|
+
return {};
|
|
3074
3004
|
}
|
|
3075
|
-
return out.replace(/\s+/g, " ").trim().slice(0, MAX_BASE).trim();
|
|
3076
3005
|
}
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
const fallbackName = clean(fallback);
|
|
3082
|
-
return fallbackName !== "" ? fallbackName : "LemonRiver";
|
|
3006
|
+
async function writePrefsSnapshot(userCwd, prefs) {
|
|
3007
|
+
const file = prefsSnapshotFile(userCwd);
|
|
3008
|
+
await fs$1.mkdir(path.dirname(file), { recursive: true });
|
|
3009
|
+
await fs$1.writeFile(file, `${JSON.stringify(sanitizePrefs$1(prefs), null, 2)}\n`);
|
|
3083
3010
|
}
|
|
3084
|
-
|
|
3085
|
-
|
|
3011
|
+
/** Checks that the folder can hold backups before anything is saved. */
|
|
3012
|
+
async function checkWritableFolder(folder) {
|
|
3013
|
+
try {
|
|
3014
|
+
if (!path.isAbsolute(folder)) return "bad-folder";
|
|
3015
|
+
if (!(await fs$1.stat(folder)).isDirectory()) return "bad-folder";
|
|
3016
|
+
} catch {
|
|
3017
|
+
return "bad-folder";
|
|
3018
|
+
}
|
|
3019
|
+
const target = resolveBackupDir(folder);
|
|
3020
|
+
const probe = path.join(target, `.write-check-${process.pid}-${Date.now()}`);
|
|
3021
|
+
try {
|
|
3022
|
+
await fs$1.mkdir(target, { recursive: true });
|
|
3023
|
+
await fs$1.writeFile(probe, "ok");
|
|
3024
|
+
await fs$1.rm(probe, { force: true });
|
|
3025
|
+
return null;
|
|
3026
|
+
} catch (err) {
|
|
3027
|
+
const code = err.code;
|
|
3028
|
+
return code === "EPERM" || code === "EACCES" ? "no-permission" : "bad-folder";
|
|
3029
|
+
}
|
|
3086
3030
|
}
|
|
3087
|
-
|
|
3088
|
-
|
|
3089
|
-
|
|
3031
|
+
//#endregion
|
|
3032
|
+
//#region src/lemonriver/backup/pack.ts
|
|
3033
|
+
const ENTRY_NAMES$4 = [
|
|
3034
|
+
"index.tsx",
|
|
3035
|
+
"index.jsx",
|
|
3036
|
+
"index.ts",
|
|
3037
|
+
"index.js"
|
|
3038
|
+
];
|
|
3039
|
+
const SKIP_NAMES = /* @__PURE__ */ new Set([
|
|
3040
|
+
"node_modules",
|
|
3041
|
+
".DS_Store",
|
|
3042
|
+
".git"
|
|
3043
|
+
]);
|
|
3044
|
+
function isJunk(name) {
|
|
3045
|
+
return SKIP_NAMES.has(name);
|
|
3090
3046
|
}
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3047
|
+
async function walk$1(dir, prefix, label, skipDirs, limits, warnings, out) {
|
|
3048
|
+
let entries;
|
|
3049
|
+
try {
|
|
3050
|
+
entries = await fs$1.readdir(dir, { withFileTypes: true });
|
|
3051
|
+
} catch {
|
|
3052
|
+
return;
|
|
3096
3053
|
}
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
if (!
|
|
3054
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
3055
|
+
if (isJunk(entry.name)) continue;
|
|
3056
|
+
const full = path.join(dir, entry.name);
|
|
3057
|
+
if (entry.isDirectory()) {
|
|
3058
|
+
if (skipDirs.has(path.resolve(full))) continue;
|
|
3059
|
+
await walk$1(full, prefix === "" ? entry.name : `${prefix}/${entry.name}`, label, skipDirs, limits, warnings, out);
|
|
3060
|
+
continue;
|
|
3061
|
+
}
|
|
3062
|
+
if (!entry.isFile()) continue;
|
|
3063
|
+
const rel = prefix === "" ? entry.name : `${prefix}/${entry.name}`;
|
|
3064
|
+
const stat = await fs$1.stat(full).catch(() => null);
|
|
3065
|
+
if (!stat) continue;
|
|
3066
|
+
if (stat.size > limits.fileBytes) {
|
|
3067
|
+
warnings.push(`${label}${rel}`);
|
|
3068
|
+
continue;
|
|
3069
|
+
}
|
|
3070
|
+
out.push({
|
|
3071
|
+
rel,
|
|
3072
|
+
file: full,
|
|
3073
|
+
size: stat.size
|
|
3074
|
+
});
|
|
3106
3075
|
}
|
|
3107
3076
|
}
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3077
|
+
async function deckEntry(slideDir) {
|
|
3078
|
+
for (const name of ENTRY_NAMES$4) try {
|
|
3079
|
+
return {
|
|
3080
|
+
name,
|
|
3081
|
+
source: await fs$1.readFile(path.join(slideDir, name), "utf8")
|
|
3082
|
+
};
|
|
3083
|
+
} catch {}
|
|
3084
|
+
return null;
|
|
3116
3085
|
}
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3121
|
-
|
|
3122
|
-
|
|
3123
|
-
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3086
|
+
/** The length of `export default [...]`, or null when it is not a plain array. */
|
|
3087
|
+
function pageCountInSource(source) {
|
|
3088
|
+
let body;
|
|
3089
|
+
try {
|
|
3090
|
+
body = parse(source, {
|
|
3091
|
+
sourceType: "module",
|
|
3092
|
+
plugins: ["typescript", "jsx"],
|
|
3093
|
+
errorRecovery: true
|
|
3094
|
+
}).program.body ?? [];
|
|
3095
|
+
} catch {
|
|
3096
|
+
return null;
|
|
3097
|
+
}
|
|
3098
|
+
for (const raw of body) {
|
|
3099
|
+
const node = raw;
|
|
3100
|
+
if (node.type !== "ExportDefaultDeclaration") continue;
|
|
3101
|
+
let inner = node.declaration;
|
|
3102
|
+
while (inner && (inner.type === "TSAsExpression" || inner.type === "TSSatisfiesExpression")) inner = inner.expression;
|
|
3103
|
+
if (inner?.type !== "ArrayExpression") return null;
|
|
3104
|
+
const elements = inner.elements ?? [];
|
|
3105
|
+
return elements.some((element) => element === null) ? null : elements.length;
|
|
3106
|
+
}
|
|
3107
|
+
return null;
|
|
3129
3108
|
}
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
const
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
|
|
3137
|
-
|
|
3138
|
-
|
|
3109
|
+
async function readFoldersRaw(slidesRoot) {
|
|
3110
|
+
try {
|
|
3111
|
+
const raw = JSON.parse(await fs$1.readFile(path.join(slidesRoot, ".folders.json"), "utf8"));
|
|
3112
|
+
if (typeof raw === "object" && raw !== null && !Array.isArray(raw)) return raw;
|
|
3113
|
+
} catch {}
|
|
3114
|
+
return {
|
|
3115
|
+
folders: [],
|
|
3116
|
+
assignments: {}
|
|
3117
|
+
};
|
|
3139
3118
|
}
|
|
3140
|
-
/**
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
let
|
|
3119
|
+
/** Collects everything a backup carries, without reading file contents yet. */
|
|
3120
|
+
async function planBackup(settings, deps) {
|
|
3121
|
+
const warnings = [];
|
|
3122
|
+
const skipped = [];
|
|
3123
|
+
const entries = [];
|
|
3124
|
+
const decks = [];
|
|
3125
|
+
const targetDir = settings.folder ? path.resolve(resolveBackupDir(settings.folder)) : null;
|
|
3126
|
+
const skipDirs = new Set(targetDir ? [targetDir] : []);
|
|
3127
|
+
let slideIds = [];
|
|
3149
3128
|
try {
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
3160
|
-
|
|
3161
|
-
if (file.originalSize > limits.maxFileBytes) {
|
|
3162
|
-
problem = {
|
|
3163
|
-
code: "too-large",
|
|
3164
|
-
detail: `${file.name} unpacks to more than ${limits.maxFileBytes} bytes`
|
|
3165
|
-
};
|
|
3166
|
-
return false;
|
|
3129
|
+
slideIds = (await fs$1.readdir(deps.slidesRoot, { withFileTypes: true })).filter((entry) => entry.isDirectory() && !isJunk(entry.name)).map((entry) => entry.name).sort((a, b) => a.localeCompare(b));
|
|
3130
|
+
} catch {}
|
|
3131
|
+
for (const slideId of slideIds) {
|
|
3132
|
+
const slideDir = path.join(deps.slidesRoot, slideId);
|
|
3133
|
+
const files = [];
|
|
3134
|
+
await walk$1(slideDir, "", `slides/${slideId}/`, skipDirs, deps.limits, warnings, files);
|
|
3135
|
+
for (const { rel, file, size } of files) {
|
|
3136
|
+
const name = `slides/${slideId}/${rel}`;
|
|
3137
|
+
if (packagePathProblem(name, "backup")) {
|
|
3138
|
+
warnings.push(name);
|
|
3139
|
+
continue;
|
|
3167
3140
|
}
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
};
|
|
3174
|
-
|
|
3141
|
+
if (settings.skipLargeVideos && isLargeVideo(name, size)) {
|
|
3142
|
+
skipped.push({
|
|
3143
|
+
rel: name,
|
|
3144
|
+
size,
|
|
3145
|
+
reason: "large-video"
|
|
3146
|
+
});
|
|
3147
|
+
continue;
|
|
3175
3148
|
}
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
|
|
3179
|
-
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
};
|
|
3149
|
+
entries.push({
|
|
3150
|
+
name,
|
|
3151
|
+
file,
|
|
3152
|
+
size
|
|
3153
|
+
});
|
|
3154
|
+
}
|
|
3155
|
+
const source = await deckEntry(slideDir);
|
|
3156
|
+
const titleRead = source ? readMetaTitleInSource(source.source) : { kind: "missing" };
|
|
3157
|
+
const title = titleRead.kind === "found" ? titleRead.title : slideId;
|
|
3158
|
+
const theme = source ? readMetaThemeInSource(source.source) : null;
|
|
3159
|
+
const record = await readBoardFile(boardStructurePath(deps.userCwd, slideId)).catch(() => ({}));
|
|
3160
|
+
const stored = typeof record.shareId === "string" && record.shareId !== "" ? record.shareId : "";
|
|
3161
|
+
if (Object.keys(record).length > 0) {
|
|
3162
|
+
const bytes = strToU8(`${JSON.stringify(stripBoardRecord(record), null, 2)}\n`);
|
|
3163
|
+
entries.push({
|
|
3164
|
+
name: `board/${slideId}.json`,
|
|
3165
|
+
bytes
|
|
3166
|
+
});
|
|
3167
|
+
}
|
|
3168
|
+
decks.push({
|
|
3169
|
+
slideId,
|
|
3170
|
+
title,
|
|
3171
|
+
pages: source ? pageCountInSource(source.source) : null,
|
|
3172
|
+
shareId: stored !== "" ? stored : slideId,
|
|
3173
|
+
theme,
|
|
3174
|
+
thumbs: 0
|
|
3175
|
+
});
|
|
3184
3176
|
}
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
|
|
3177
|
+
const assets = [];
|
|
3178
|
+
await walk$1(deps.globalAssetsRoot, "", "assets/", skipDirs, deps.limits, warnings, assets);
|
|
3179
|
+
for (const { rel, file, size } of assets) {
|
|
3180
|
+
const name = `assets/${rel}`;
|
|
3181
|
+
if (packagePathProblem(name, "backup")) {
|
|
3182
|
+
warnings.push(name);
|
|
3183
|
+
continue;
|
|
3184
|
+
}
|
|
3185
|
+
if (settings.skipLargeVideos && isLargeVideo(name, size)) {
|
|
3186
|
+
skipped.push({
|
|
3187
|
+
rel: name,
|
|
3188
|
+
size,
|
|
3189
|
+
reason: "large-video"
|
|
3190
|
+
});
|
|
3191
|
+
continue;
|
|
3192
|
+
}
|
|
3193
|
+
entries.push({
|
|
3194
|
+
name,
|
|
3195
|
+
file,
|
|
3196
|
+
size
|
|
3197
|
+
});
|
|
3192
3198
|
}
|
|
3193
|
-
const
|
|
3194
|
-
|
|
3195
|
-
|
|
3196
|
-
|
|
3199
|
+
const themes = [];
|
|
3200
|
+
await walk$1(deps.themesRoot, "", "themes/", skipDirs, deps.limits, warnings, themes);
|
|
3201
|
+
for (const { rel, file, size } of themes) {
|
|
3202
|
+
const name = `themes/${rel}`;
|
|
3203
|
+
if (packagePathProblem(name, "backup")) {
|
|
3204
|
+
warnings.push(name);
|
|
3205
|
+
continue;
|
|
3206
|
+
}
|
|
3207
|
+
if (settings.skipLargeVideos && isLargeVideo(name, size)) {
|
|
3208
|
+
skipped.push({
|
|
3209
|
+
rel: name,
|
|
3210
|
+
size,
|
|
3211
|
+
reason: "large-video"
|
|
3212
|
+
});
|
|
3213
|
+
continue;
|
|
3214
|
+
}
|
|
3215
|
+
entries.push({
|
|
3216
|
+
name,
|
|
3217
|
+
file,
|
|
3218
|
+
size
|
|
3219
|
+
});
|
|
3197
3220
|
}
|
|
3221
|
+
const folders = await readFoldersRaw(deps.slidesRoot);
|
|
3222
|
+
const prefs = await readPrefsSnapshot(deps.userCwd);
|
|
3223
|
+
entries.push({
|
|
3224
|
+
name: "workspace.json",
|
|
3225
|
+
bytes: strToU8(`${JSON.stringify({
|
|
3226
|
+
folders,
|
|
3227
|
+
prefs
|
|
3228
|
+
}, null, 2)}\n`)
|
|
3229
|
+
});
|
|
3198
3230
|
return {
|
|
3199
|
-
|
|
3200
|
-
|
|
3231
|
+
entries,
|
|
3232
|
+
decks,
|
|
3233
|
+
folders,
|
|
3234
|
+
prefs,
|
|
3235
|
+
skipped,
|
|
3236
|
+
warnings
|
|
3201
3237
|
};
|
|
3202
3238
|
}
|
|
3239
|
+
/** `LemonRiver 备份 2026-11-01 2130.json`, taken from the same clock as the rest. */
|
|
3240
|
+
function snapshotFileName(at) {
|
|
3241
|
+
return `${backupFileStem(at)}.json`;
|
|
3242
|
+
}
|
|
3203
3243
|
//#endregion
|
|
3204
|
-
//#region src/lemonriver/
|
|
3205
|
-
const
|
|
3206
|
-
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
const
|
|
3212
|
-
const
|
|
3213
|
-
/**
|
|
3214
|
-
|
|
3215
|
-
|
|
3216
|
-
|
|
3217
|
-
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
|
|
3244
|
+
//#region src/lemonriver/backup/zip.ts
|
|
3245
|
+
const CHUNK$1 = 1048576;
|
|
3246
|
+
const SIG_LOCAL = 67324752;
|
|
3247
|
+
const SIG_CENTRAL = 33639248;
|
|
3248
|
+
const SIG_EOCD = 101010256;
|
|
3249
|
+
const LOCAL_HEADER = 30;
|
|
3250
|
+
const CENTRAL_HEADER = 46;
|
|
3251
|
+
const EOCD_SIZE = 22;
|
|
3252
|
+
const ZIP64_MARK = 4294967295;
|
|
3253
|
+
/** Deflate is method 8; anything else has to be stored (method 0). */
|
|
3254
|
+
const DEFLATE = 8;
|
|
3255
|
+
/** Streams entries into a zip on disk; nothing larger than a chunk is held in memory. */
|
|
3256
|
+
var ZipWriter = class {
|
|
3257
|
+
zip;
|
|
3258
|
+
out;
|
|
3259
|
+
blocked = null;
|
|
3260
|
+
failure = null;
|
|
3261
|
+
failureSignal;
|
|
3262
|
+
rejectFailure;
|
|
3263
|
+
done;
|
|
3264
|
+
written = 0;
|
|
3265
|
+
constructor(target) {
|
|
3266
|
+
this.out = createWriteStream(target);
|
|
3267
|
+
this.failureSignal = new Promise((_, reject) => {
|
|
3268
|
+
this.rejectFailure = reject;
|
|
3221
3269
|
});
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
|
|
3237
|
-
|
|
3238
|
-
|
|
3239
|
-
|
|
3240
|
-
return typeof value?.value === "string" ? value.value : null;
|
|
3270
|
+
this.failureSignal.catch(() => {});
|
|
3271
|
+
this.done = new Promise((resolve, reject) => {
|
|
3272
|
+
let finished = false;
|
|
3273
|
+
this.out.on("finish", () => {
|
|
3274
|
+
finished = true;
|
|
3275
|
+
resolve();
|
|
3276
|
+
});
|
|
3277
|
+
this.out.on("close", () => {
|
|
3278
|
+
if (!finished) reject(/* @__PURE__ */ new Error("the backup file closed before it was written"));
|
|
3279
|
+
});
|
|
3280
|
+
this.out.on("error", reject);
|
|
3281
|
+
});
|
|
3282
|
+
this.done.catch(() => {});
|
|
3283
|
+
this.zip = new Zip((err, chunk, final) => {
|
|
3284
|
+
if (err) {
|
|
3285
|
+
this.fail(err);
|
|
3286
|
+
this.out.destroy(err);
|
|
3287
|
+
return;
|
|
3241
3288
|
}
|
|
3242
|
-
|
|
3289
|
+
this.written += chunk.length;
|
|
3290
|
+
if (this.written > ZIP64_MARK) {
|
|
3291
|
+
this.fail(/* @__PURE__ */ new Error("the backup grew past 4GB while it was written"));
|
|
3292
|
+
this.out.destroy();
|
|
3293
|
+
return;
|
|
3294
|
+
}
|
|
3295
|
+
if (!this.out.write(Buffer.from(chunk))) this.blocked = once(this.out, "drain");
|
|
3296
|
+
if (final) this.out.end();
|
|
3297
|
+
});
|
|
3298
|
+
this.out.on("error", (err) => this.fail(err));
|
|
3299
|
+
}
|
|
3300
|
+
fail(err) {
|
|
3301
|
+
if (this.failure) return;
|
|
3302
|
+
this.failure = err;
|
|
3303
|
+
this.rejectFailure(err);
|
|
3304
|
+
}
|
|
3305
|
+
async ready() {
|
|
3306
|
+
if (this.blocked) {
|
|
3307
|
+
const wait = this.blocked;
|
|
3308
|
+
this.blocked = null;
|
|
3309
|
+
await Promise.race([wait, this.failureSignal]);
|
|
3243
3310
|
}
|
|
3311
|
+
if (this.failure) throw this.failure;
|
|
3244
3312
|
}
|
|
3245
|
-
|
|
3246
|
-
}
|
|
3247
|
-
async function exists(file) {
|
|
3248
|
-
try {
|
|
3249
|
-
await fs$1.access(file);
|
|
3250
|
-
return true;
|
|
3251
|
-
} catch {
|
|
3252
|
-
return false;
|
|
3313
|
+
entry(name) {
|
|
3314
|
+
return isCompressedFile(name) ? new ZipPassThrough(name) : new ZipDeflate(name, { level: 6 });
|
|
3253
3315
|
}
|
|
3254
|
-
|
|
3255
|
-
|
|
3256
|
-
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
const
|
|
3260
|
-
|
|
3261
|
-
|
|
3262
|
-
|
|
3263
|
-
|
|
3316
|
+
async addFile(name, file, limits = BACKUP_LIMITS) {
|
|
3317
|
+
const entry = this.entry(name);
|
|
3318
|
+
this.zip.add(entry);
|
|
3319
|
+
const hash = createHash("sha256");
|
|
3320
|
+
let size = 0;
|
|
3321
|
+
for await (const chunk of createReadStream(file, { highWaterMark: CHUNK$1 })) {
|
|
3322
|
+
size += chunk.length;
|
|
3323
|
+
if (size > limits.fileBytes) throw new Error(BACKUP_TOO_LARGE);
|
|
3324
|
+
hash.update(chunk);
|
|
3325
|
+
entry.push(chunk, false);
|
|
3326
|
+
await this.ready();
|
|
3264
3327
|
}
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3328
|
+
entry.push(/* @__PURE__ */ new Uint8Array(0), true);
|
|
3329
|
+
await this.ready();
|
|
3330
|
+
return {
|
|
3331
|
+
sha: hash.digest("hex"),
|
|
3332
|
+
size
|
|
3333
|
+
};
|
|
3334
|
+
}
|
|
3335
|
+
async addBytes(name, bytes) {
|
|
3336
|
+
const entry = this.entry(name);
|
|
3337
|
+
this.zip.add(entry);
|
|
3338
|
+
entry.push(bytes, true);
|
|
3339
|
+
await this.ready();
|
|
3340
|
+
return {
|
|
3341
|
+
sha: createHash("sha256").update(bytes).digest("hex"),
|
|
3342
|
+
size: bytes.length
|
|
3343
|
+
};
|
|
3344
|
+
}
|
|
3345
|
+
async close() {
|
|
3346
|
+
this.zip.end();
|
|
3347
|
+
await this.done;
|
|
3348
|
+
return this.written;
|
|
3349
|
+
}
|
|
3350
|
+
abort() {
|
|
3351
|
+
this.fail(/* @__PURE__ */ new Error("the backup was abandoned"));
|
|
3352
|
+
this.out.destroy();
|
|
3353
|
+
}
|
|
3354
|
+
};
|
|
3355
|
+
function concat(chunks) {
|
|
3356
|
+
let length = 0;
|
|
3357
|
+
for (const chunk of chunks) length += chunk.length;
|
|
3358
|
+
const out = new Uint8Array(length);
|
|
3359
|
+
let offset = 0;
|
|
3360
|
+
for (const chunk of chunks) {
|
|
3361
|
+
out.set(chunk, offset);
|
|
3362
|
+
offset += chunk.length;
|
|
3268
3363
|
}
|
|
3364
|
+
return out;
|
|
3269
3365
|
}
|
|
3270
|
-
|
|
3271
|
-
const
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
const
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
"
|
|
3279
|
-
|
|
3280
|
-
".mjs",
|
|
3281
|
-
".json"
|
|
3282
|
-
];
|
|
3283
|
-
const INDEX_TRIES = [
|
|
3284
|
-
"/index.tsx",
|
|
3285
|
-
"/index.ts",
|
|
3286
|
-
"/index.jsx",
|
|
3287
|
-
"/index.js"
|
|
3288
|
-
];
|
|
3289
|
-
const TEXT_FILE = /\.(?:tsx|jsx|ts|js|mjs|css|html|md|json)$/i;
|
|
3290
|
-
/** `./kit/shell` may name `kit/shell.tsx`; each suffix is tried before giving up. */
|
|
3291
|
-
function resolveSpecifier(fromFile, files, spec) {
|
|
3292
|
-
const clean = spec.split(/[?#]/)[0].trim();
|
|
3293
|
-
if (clean === "" || /^[a-z]+:/i.test(clean) || clean.startsWith("@") || clean.startsWith("/")) return null;
|
|
3294
|
-
if (!clean.startsWith(".")) return null;
|
|
3295
|
-
const base = path.posix.dirname(fromFile);
|
|
3296
|
-
const joined = path.posix.normalize(path.posix.join(base === "." ? "" : base, clean));
|
|
3297
|
-
if (joined.startsWith("..")) return null;
|
|
3298
|
-
for (const suffix of [...EXTENSION_TRIES, ...INDEX_TRIES]) if (files.has(joined + suffix)) return joined + suffix;
|
|
3299
|
-
return null;
|
|
3366
|
+
async function readRange(handle, start, length) {
|
|
3367
|
+
const buffer = Buffer.alloc(length);
|
|
3368
|
+
let read = 0;
|
|
3369
|
+
while (read < length) {
|
|
3370
|
+
const { bytesRead } = await handle.read(buffer, read, length - read, start + read);
|
|
3371
|
+
if (bytesRead === 0) break;
|
|
3372
|
+
read += bytesRead;
|
|
3373
|
+
}
|
|
3374
|
+
if (read !== length) throw new Error("the archive was cut short");
|
|
3375
|
+
return buffer;
|
|
3300
3376
|
}
|
|
3301
|
-
/** The
|
|
3302
|
-
function
|
|
3303
|
-
|
|
3304
|
-
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
while (queue.length > 0) {
|
|
3308
|
-
const rel = queue.pop();
|
|
3309
|
-
const bytes = files.get(rel);
|
|
3310
|
-
if (!bytes || !TEXT_FILE.test(rel)) continue;
|
|
3311
|
-
const text = new TextDecoder().decode(bytes);
|
|
3312
|
-
for (const pattern of [
|
|
3313
|
-
IMPORT_FROM,
|
|
3314
|
-
IMPORT_BARE,
|
|
3315
|
-
IMPORT_CALL,
|
|
3316
|
-
CSS_URL
|
|
3317
|
-
]) for (const match of text.matchAll(pattern)) {
|
|
3318
|
-
const resolved = resolveSpecifier(rel, files, match[1] ?? "");
|
|
3319
|
-
if (resolved !== null && !used.has(resolved)) {
|
|
3320
|
-
used.add(resolved);
|
|
3321
|
-
queue.push(resolved);
|
|
3322
|
-
}
|
|
3323
|
-
}
|
|
3377
|
+
/** The end-of-central-directory record sits in the last 64 KiB; its comment can hide it. */
|
|
3378
|
+
function findEocd(tail) {
|
|
3379
|
+
for (let at = tail.length - EOCD_SIZE; at >= 0; at--) {
|
|
3380
|
+
if (tail.readUInt32LE(at) !== SIG_EOCD) continue;
|
|
3381
|
+
const comment = tail.readUInt16LE(at + 20);
|
|
3382
|
+
if (at + EOCD_SIZE + comment <= tail.length) return at;
|
|
3324
3383
|
}
|
|
3325
|
-
return
|
|
3384
|
+
return -1;
|
|
3326
3385
|
}
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
|
|
3330
|
-
name,
|
|
3331
|
-
source: await fs$1.readFile(path.join(slideDir, name), "utf8")
|
|
3332
|
-
};
|
|
3333
|
-
} catch {}
|
|
3334
|
-
return null;
|
|
3386
|
+
/** Bit 11 of the flags says the name is UTF-8; older writers used Latin-1. */
|
|
3387
|
+
function decodeName(bytes, flags) {
|
|
3388
|
+
return bytes.toString((flags & 2048) !== 0 ? "utf8" : "latin1");
|
|
3335
3389
|
}
|
|
3336
|
-
function
|
|
3337
|
-
|
|
3338
|
-
const rel = path.posix.relative(from === "." ? "" : from, target);
|
|
3339
|
-
return rel.startsWith("..") ? rel : `./${rel}`;
|
|
3390
|
+
function unsupported(name) {
|
|
3391
|
+
return /* @__PURE__ */ new Error(`${name} is stored in a way backups do not use`);
|
|
3340
3392
|
}
|
|
3341
3393
|
/**
|
|
3342
|
-
*
|
|
3343
|
-
*
|
|
3394
|
+
* Reads the archive's ending directory, which names and locates every entry.
|
|
3395
|
+
* Nothing is read out of the file's contents to find where one entry stops.
|
|
3344
3396
|
*/
|
|
3345
|
-
async function
|
|
3346
|
-
const
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
const
|
|
3352
|
-
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3397
|
+
async function readZip(file, limits = BACKUP_LIMITS) {
|
|
3398
|
+
const handle = await fs$1.open(file, "r");
|
|
3399
|
+
try {
|
|
3400
|
+
const { size } = await handle.stat();
|
|
3401
|
+
if (size < EOCD_SIZE) throw new Error("this is not a zip file");
|
|
3402
|
+
const tailLength = Math.min(size, 65557);
|
|
3403
|
+
const tail = await readRange(handle, size - tailLength, tailLength);
|
|
3404
|
+
const eocdAt = findEocd(tail);
|
|
3405
|
+
if (eocdAt < 0) throw new Error("this is not a zip file, or it was cut short");
|
|
3406
|
+
const count = tail.readUInt16LE(eocdAt + 10);
|
|
3407
|
+
const directorySize = tail.readUInt32LE(eocdAt + 12);
|
|
3408
|
+
const directoryAt = tail.readUInt32LE(eocdAt + 16);
|
|
3409
|
+
if (count === 65535 || directorySize === ZIP64_MARK || directoryAt === ZIP64_MARK) throw new Error("this archive is too large to read as a backup");
|
|
3410
|
+
if (count > limits.files) throw new Error(`this archive holds more than ${limits.files} files`);
|
|
3411
|
+
if (directoryAt + directorySize > size) throw new Error("this archive was cut short");
|
|
3412
|
+
const directory = await readRange(handle, directoryAt, directorySize);
|
|
3413
|
+
const entries = [];
|
|
3414
|
+
let total = 0;
|
|
3415
|
+
let at = 0;
|
|
3416
|
+
for (let index = 0; index < count; index++) {
|
|
3417
|
+
if (at + CENTRAL_HEADER > directory.length || directory.readUInt32LE(at) !== SIG_CENTRAL) throw new Error("the archive has a damaged ending directory");
|
|
3418
|
+
const flags = directory.readUInt16LE(at + 8);
|
|
3419
|
+
const method = directory.readUInt16LE(at + 10);
|
|
3420
|
+
const compressedSize = directory.readUInt32LE(at + 20);
|
|
3421
|
+
const size0 = directory.readUInt32LE(at + 24);
|
|
3422
|
+
const nameLength = directory.readUInt16LE(at + 28);
|
|
3423
|
+
const extraLength = directory.readUInt16LE(at + 30);
|
|
3424
|
+
const commentLength = directory.readUInt16LE(at + 32);
|
|
3425
|
+
const localAt = directory.readUInt32LE(at + 42);
|
|
3426
|
+
const nameStart = at + CENTRAL_HEADER;
|
|
3427
|
+
if (nameStart + nameLength > directory.length) throw new Error("the archive has a damaged ending directory");
|
|
3428
|
+
const name = decodeName(directory.subarray(nameStart, nameStart + nameLength), flags);
|
|
3429
|
+
const mode = directory.readUInt32LE(at + 38) >>> 16;
|
|
3430
|
+
at = nameStart + nameLength + extraLength + commentLength;
|
|
3431
|
+
if (size0 === ZIP64_MARK || compressedSize === ZIP64_MARK || localAt === ZIP64_MARK) throw new Error(`${name} is too large to read as a backup`);
|
|
3432
|
+
if (method !== DEFLATE && method !== 0) throw unsupported(name);
|
|
3433
|
+
if (size0 > limits.fileBytes) throw new Error(`${name} is too large to read as a backup`);
|
|
3434
|
+
total += size0;
|
|
3435
|
+
if (total > limits.totalBytes) throw new Error("this archive is larger than a backup may be");
|
|
3436
|
+
const local = await readRange(handle, localAt, LOCAL_HEADER);
|
|
3437
|
+
if (local.readUInt32LE(0) !== SIG_LOCAL) throw new Error(`${name} is not in this archive`);
|
|
3438
|
+
const dataStart = localAt + LOCAL_HEADER + local.readUInt16LE(26) + local.readUInt16LE(28);
|
|
3439
|
+
if (dataStart + compressedSize > size) throw new Error(`${name} was cut short`);
|
|
3440
|
+
entries.push({
|
|
3441
|
+
name,
|
|
3442
|
+
method,
|
|
3443
|
+
size: size0,
|
|
3444
|
+
compressedSize,
|
|
3445
|
+
dataStart,
|
|
3446
|
+
executable: (mode & 73) !== 0
|
|
3447
|
+
});
|
|
3358
3448
|
}
|
|
3359
|
-
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
3363
|
-
|
|
3364
|
-
|
|
3365
|
-
|
|
3366
|
-
target = `assets/${dot > 0 ? name.slice(0, dot) : name}-shared${dot > 0 ? name.slice(dot) : ""}`;
|
|
3367
|
-
opts.packageFiles.set(`slides/${opts.slideId}/${target}`, globalBytes);
|
|
3368
|
-
}
|
|
3369
|
-
} else opts.packageFiles.set(packaged, globalBytes);
|
|
3370
|
-
source = source.split(`@assets/${name}`).join(relativeAssetRef(opts.relFile, target));
|
|
3449
|
+
return {
|
|
3450
|
+
file,
|
|
3451
|
+
size,
|
|
3452
|
+
entries
|
|
3453
|
+
};
|
|
3454
|
+
} finally {
|
|
3455
|
+
await handle.close();
|
|
3371
3456
|
}
|
|
3372
|
-
return source;
|
|
3373
3457
|
}
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3458
|
+
/**
|
|
3459
|
+
* Streams one entry's bytes. The stream is read to its own end only; a stream
|
|
3460
|
+
* that closes before that is a failure, not a short entry.
|
|
3461
|
+
*/
|
|
3462
|
+
async function readZipEntry(index, entry, onChunk) {
|
|
3463
|
+
if (entry.compressedSize === 0) {
|
|
3464
|
+
if (entry.size !== 0) throw new Error(`${entry.name} is empty but should hold data`);
|
|
3465
|
+
return;
|
|
3466
|
+
}
|
|
3467
|
+
const end = entry.dataStart + entry.compressedSize - 1;
|
|
3468
|
+
const stream = createReadStream(index.file, {
|
|
3469
|
+
start: entry.dataStart,
|
|
3470
|
+
end,
|
|
3471
|
+
highWaterMark: CHUNK$1
|
|
3472
|
+
});
|
|
3473
|
+
let written = 0;
|
|
3474
|
+
let failure = null;
|
|
3475
|
+
let ended = false;
|
|
3476
|
+
const fail = (err) => {
|
|
3477
|
+
if (failure) return;
|
|
3478
|
+
failure = err;
|
|
3479
|
+
stream.destroy();
|
|
3382
3480
|
};
|
|
3383
|
-
const
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
status: 400,
|
|
3390
|
-
error: `bad slideId: ${slideId}`
|
|
3391
|
-
};
|
|
3392
|
-
const slideDir = path.join(deps.slidesRoot, slideId);
|
|
3393
|
-
if (!await exists(slideDir)) return {
|
|
3394
|
-
ok: false,
|
|
3395
|
-
status: 404,
|
|
3396
|
-
error: `slide ${slideId} not found`
|
|
3397
|
-
};
|
|
3398
|
-
const collected = /* @__PURE__ */ new Map();
|
|
3399
|
-
const work = /* @__PURE__ */ new Set();
|
|
3400
|
-
await walk$2(slideDir, "", collected, work);
|
|
3401
|
-
const used = usedPaths(collected, ENTRY_NAMES$4.find((name) => collected.has(name)) ?? null);
|
|
3402
|
-
for (const rel of work) if (!used.has(rel)) workFiles += 1;
|
|
3403
|
-
for (const [rel, bytes] of collected) {
|
|
3404
|
-
if (work.has(rel) && !used.has(rel)) continue;
|
|
3405
|
-
const packaged = `slides/${slideId}/${rel}`;
|
|
3406
|
-
const problem = packagePathProblem(packaged);
|
|
3407
|
-
if (problem) {
|
|
3408
|
-
if (used.has(rel)) badPaths.push(`${packaged}: ${problem.detail}`);
|
|
3409
|
-
else skipped.push(packaged);
|
|
3410
|
-
continue;
|
|
3411
|
-
}
|
|
3412
|
-
packageFiles.set(packaged, bytes);
|
|
3481
|
+
const deliver = (chunk) => {
|
|
3482
|
+
if (failure) return;
|
|
3483
|
+
written += chunk.length;
|
|
3484
|
+
if (written > entry.size) {
|
|
3485
|
+
fail(/* @__PURE__ */ new Error(`${entry.name} holds more than the archive says`));
|
|
3486
|
+
return;
|
|
3413
3487
|
}
|
|
3414
|
-
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
const theme = entry ? readMetaThemeInSource(entry.source) : null;
|
|
3419
|
-
for (const [rel, data] of [...packageFiles]) {
|
|
3420
|
-
if (!rel.startsWith(`slides/${slideId}/`)) continue;
|
|
3421
|
-
if (!SOURCE_RE.test(rel)) continue;
|
|
3422
|
-
const rewritten = await localizeAssets({
|
|
3423
|
-
source: new TextDecoder().decode(data),
|
|
3424
|
-
relFile: rel.slice(`slides/${slideId}/`.length),
|
|
3425
|
-
slideId,
|
|
3426
|
-
packageFiles,
|
|
3427
|
-
globalAssetsRoot: deps.globalAssetsRoot,
|
|
3428
|
-
warnings
|
|
3429
|
-
});
|
|
3430
|
-
if (rewritten !== new TextDecoder().decode(data)) packageFiles.set(rel, strToU8(rewritten));
|
|
3488
|
+
try {
|
|
3489
|
+
onChunk(chunk);
|
|
3490
|
+
} catch (err) {
|
|
3491
|
+
fail(err);
|
|
3431
3492
|
}
|
|
3432
|
-
|
|
3433
|
-
|
|
3434
|
-
|
|
3435
|
-
|
|
3436
|
-
|
|
3437
|
-
|
|
3438
|
-
|
|
3439
|
-
|
|
3493
|
+
};
|
|
3494
|
+
const inflate = entry.method === DEFLATE ? new Inflate((chunk) => deliver(chunk)) : null;
|
|
3495
|
+
await new Promise((resolve, reject) => {
|
|
3496
|
+
stream.on("data", (chunk) => {
|
|
3497
|
+
if (failure) return;
|
|
3498
|
+
if (!inflate) {
|
|
3499
|
+
deliver(chunk);
|
|
3500
|
+
return;
|
|
3440
3501
|
}
|
|
3441
|
-
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
const packaged = `themes/${theme}-assets/${entry.name}`;
|
|
3446
|
-
if (packageFiles.has(packaged)) continue;
|
|
3447
|
-
if (packagePathProblem(packaged)) {
|
|
3448
|
-
skipped.push(packaged);
|
|
3449
|
-
continue;
|
|
3450
|
-
}
|
|
3451
|
-
packageFiles.set(packaged, new Uint8Array(await fs$1.readFile(path.join(assetsDir, entry.name))));
|
|
3502
|
+
try {
|
|
3503
|
+
inflate.push(chunk, false);
|
|
3504
|
+
} catch (err) {
|
|
3505
|
+
fail(err);
|
|
3452
3506
|
}
|
|
3453
|
-
}
|
|
3454
|
-
const boardFile = boardStructurePath(deps.userCwd, slideId);
|
|
3455
|
-
const hasBoard = await exists(boardFile);
|
|
3456
|
-
const record = hasBoard ? await readBoardFile(boardFile) : {};
|
|
3457
|
-
const stored = typeof record.shareId === "string" ? record.shareId : "";
|
|
3458
|
-
const shareId = stored !== "" ? stored : deps.newShareId();
|
|
3459
|
-
if (stored === "") await updateBoardFile(boardFile, { shareId });
|
|
3460
|
-
if (hasBoard) packageFiles.set(`board/${slideId}.json`, strToU8(`${JSON.stringify(stripBoardRecord(record), null, 2)}\n`));
|
|
3461
|
-
const thumbs = request.thumbs[slideId] ?? [];
|
|
3462
|
-
thumbs.forEach((base64, index) => {
|
|
3463
|
-
const name = String(index + 1).padStart(2, "0");
|
|
3464
|
-
packageFiles.set(`thumbs/${slideId}/${name}.jpg`, Buffer.from(base64, "base64"));
|
|
3465
3507
|
});
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3508
|
+
stream.on("error", (err) => {
|
|
3509
|
+
failure ??= err;
|
|
3510
|
+
});
|
|
3511
|
+
stream.on("end", () => {
|
|
3512
|
+
if (!failure && inflate) try {
|
|
3513
|
+
inflate.push(/* @__PURE__ */ new Uint8Array(0), true);
|
|
3514
|
+
} catch (err) {
|
|
3515
|
+
failure = err;
|
|
3516
|
+
}
|
|
3517
|
+
if (!failure && written !== entry.size) failure = /* @__PURE__ */ new Error(`${entry.name} is shorter than the archive says`);
|
|
3518
|
+
ended = true;
|
|
3519
|
+
});
|
|
3520
|
+
stream.on("close", () => {
|
|
3521
|
+
if (failure) reject(failure);
|
|
3522
|
+
else if (!ended) reject(/* @__PURE__ */ new Error(`${entry.name} stopped before it was read`));
|
|
3523
|
+
else resolve();
|
|
3524
|
+
});
|
|
3525
|
+
});
|
|
3526
|
+
}
|
|
3527
|
+
/**
|
|
3528
|
+
* Reads the whole archive once, hashing every entry. `keep` names entries whose
|
|
3529
|
+
* bytes are also wanted (the manifest and workspace file are small).
|
|
3530
|
+
*/
|
|
3531
|
+
async function scanZip(file, limits = BACKUP_LIMITS, keep = () => false) {
|
|
3532
|
+
const index = await readZip(file, limits);
|
|
3533
|
+
const entries = [];
|
|
3534
|
+
const kept = /* @__PURE__ */ new Map();
|
|
3535
|
+
let totalSize = 0;
|
|
3536
|
+
for (const entry of index.entries) {
|
|
3537
|
+
const keepBytes = keep(entry.name);
|
|
3538
|
+
const hash = createHash("sha256");
|
|
3539
|
+
const chunks = [];
|
|
3540
|
+
await readZipEntry(index, entry, (chunk) => {
|
|
3541
|
+
totalSize += chunk.length;
|
|
3542
|
+
hash.update(chunk);
|
|
3543
|
+
if (keepBytes) chunks.push(chunk);
|
|
3544
|
+
});
|
|
3545
|
+
entries.push({
|
|
3546
|
+
name: entry.name,
|
|
3547
|
+
sha: hash.digest("hex"),
|
|
3548
|
+
size: entry.size,
|
|
3549
|
+
executable: entry.executable
|
|
3473
3550
|
});
|
|
3551
|
+
if (keepBytes) kept.set(entry.name, concat(chunks));
|
|
3474
3552
|
}
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
paths: badPaths
|
|
3553
|
+
return {
|
|
3554
|
+
entries,
|
|
3555
|
+
kept,
|
|
3556
|
+
totalSize
|
|
3480
3557
|
};
|
|
3481
|
-
|
|
3482
|
-
|
|
3483
|
-
|
|
3484
|
-
|
|
3558
|
+
}
|
|
3559
|
+
async function sha256FileStream(file) {
|
|
3560
|
+
try {
|
|
3561
|
+
const hash = createHash("sha256");
|
|
3562
|
+
for await (const chunk of createReadStream(file, { highWaterMark: CHUNK$1 })) hash.update(chunk);
|
|
3563
|
+
return hash.digest("hex");
|
|
3564
|
+
} catch {
|
|
3565
|
+
return null;
|
|
3566
|
+
}
|
|
3567
|
+
}
|
|
3568
|
+
//#endregion
|
|
3569
|
+
//#region src/lemonriver/backup/snapshot.ts
|
|
3570
|
+
const CHUNK = 1048576;
|
|
3571
|
+
function snapshotParts(dir) {
|
|
3572
|
+
return {
|
|
3573
|
+
objectsRoot: path.join(dir, "objects"),
|
|
3574
|
+
snapshotsRoot: path.join(dir, "snapshots")
|
|
3575
|
+
};
|
|
3576
|
+
}
|
|
3577
|
+
function objectPath(objectsRoot, sha) {
|
|
3578
|
+
return path.join(objectsRoot, sha.slice(0, 2), sha);
|
|
3579
|
+
}
|
|
3580
|
+
function isRecord$2(value) {
|
|
3581
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3582
|
+
}
|
|
3583
|
+
function readFileMap(raw) {
|
|
3584
|
+
const out = {};
|
|
3585
|
+
if (!isRecord$2(raw)) return out;
|
|
3586
|
+
for (const [rel, value] of Object.entries(raw)) {
|
|
3587
|
+
if (!isRecord$2(value)) continue;
|
|
3588
|
+
if (typeof value.sha256 !== "string" || !/^[a-f0-9]{64}$/.test(value.sha256)) continue;
|
|
3589
|
+
if (typeof value.size !== "number" || value.size < 0) continue;
|
|
3590
|
+
out[rel] = {
|
|
3591
|
+
sha256: value.sha256,
|
|
3592
|
+
size: value.size
|
|
3593
|
+
};
|
|
3594
|
+
}
|
|
3595
|
+
return out;
|
|
3596
|
+
}
|
|
3597
|
+
function readSkipped(raw) {
|
|
3598
|
+
if (!Array.isArray(raw)) return [];
|
|
3599
|
+
const out = [];
|
|
3600
|
+
for (const item of raw) {
|
|
3601
|
+
if (!isRecord$2(item) || typeof item.rel !== "string" || item.rel === "") continue;
|
|
3602
|
+
if (typeof item.size !== "number" || item.size < 0) continue;
|
|
3603
|
+
if (item.reason !== "large-video") continue;
|
|
3604
|
+
out.push({
|
|
3605
|
+
rel: item.rel,
|
|
3606
|
+
size: item.size,
|
|
3607
|
+
reason: item.reason
|
|
3608
|
+
});
|
|
3609
|
+
}
|
|
3610
|
+
return out;
|
|
3611
|
+
}
|
|
3612
|
+
function readDecks(raw) {
|
|
3613
|
+
if (!Array.isArray(raw)) return [];
|
|
3614
|
+
const out = [];
|
|
3615
|
+
for (const item of raw) {
|
|
3616
|
+
if (!isRecord$2(item) || typeof item.slideId !== "string" || item.slideId === "") continue;
|
|
3617
|
+
out.push({
|
|
3618
|
+
slideId: item.slideId,
|
|
3619
|
+
title: typeof item.title === "string" ? item.title : item.slideId,
|
|
3620
|
+
pages: typeof item.pages === "number" ? item.pages : null,
|
|
3621
|
+
shareId: typeof item.shareId === "string" ? item.shareId : item.slideId,
|
|
3622
|
+
theme: typeof item.theme === "string" ? item.theme : null,
|
|
3623
|
+
thumbs: typeof item.thumbs === "number" ? item.thumbs : 0
|
|
3624
|
+
});
|
|
3625
|
+
}
|
|
3626
|
+
return out;
|
|
3627
|
+
}
|
|
3628
|
+
function sanitizeSnapshot(raw) {
|
|
3629
|
+
if (!isRecord$2(raw)) return null;
|
|
3630
|
+
if (raw.format !== "lemonriver-backup" || raw.version !== 1) return null;
|
|
3631
|
+
if (typeof raw.createdAt !== "number" || !Number.isFinite(raw.createdAt)) return null;
|
|
3632
|
+
const app = isRecord$2(raw.app) ? raw.app : {};
|
|
3633
|
+
return {
|
|
3634
|
+
format: SNAPSHOT_FORMAT,
|
|
3485
3635
|
version: 1,
|
|
3486
|
-
|
|
3636
|
+
createdAt: raw.createdAt,
|
|
3487
3637
|
app: {
|
|
3488
|
-
name: "LemonRiver",
|
|
3489
|
-
version:
|
|
3638
|
+
name: typeof app.name === "string" ? app.name : "LemonRiver",
|
|
3639
|
+
version: typeof app.version === "string" ? app.version : ""
|
|
3490
3640
|
},
|
|
3491
|
-
|
|
3492
|
-
decks,
|
|
3493
|
-
|
|
3494
|
-
|
|
3495
|
-
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
3499
|
-
const zipped = zipSync(entries, { level: 6 });
|
|
3500
|
-
await fs$1.mkdir(deps.sharedRoot, { recursive: true });
|
|
3501
|
-
const taken = await fs$1.readdir(deps.sharedRoot).catch(() => []);
|
|
3502
|
-
const fileName = uniqueFileName(packageBaseName(decks.map((deck) => ({
|
|
3503
|
-
title: deck.title,
|
|
3504
|
-
slideId: deck.slideId
|
|
3505
|
-
})), deps.now), taken.map((name) => name.replace(/\.zip$/i, "")));
|
|
3506
|
-
const target = path.join(deps.sharedRoot, `${fileName}.zip`);
|
|
3507
|
-
await fs$1.writeFile(target, zipped);
|
|
3508
|
-
return {
|
|
3509
|
-
ok: true,
|
|
3510
|
-
path: target,
|
|
3511
|
-
name: `${fileName}.zip`,
|
|
3512
|
-
size: zipped.length,
|
|
3513
|
-
warnings,
|
|
3514
|
-
skipped,
|
|
3515
|
-
workFiles
|
|
3641
|
+
files: readFileMap(raw.files),
|
|
3642
|
+
decks: readDecks(raw.decks),
|
|
3643
|
+
folders: isRecord$2(raw.folders) ? raw.folders : {
|
|
3644
|
+
folders: [],
|
|
3645
|
+
assignments: {}
|
|
3646
|
+
},
|
|
3647
|
+
prefs: sanitizePrefs$1(raw.prefs),
|
|
3648
|
+
skipped: readSkipped(raw.skipped)
|
|
3516
3649
|
};
|
|
3517
3650
|
}
|
|
3518
|
-
|
|
3519
|
-
|
|
3520
|
-
|
|
3521
|
-
|
|
3651
|
+
async function readSnapshot(file) {
|
|
3652
|
+
try {
|
|
3653
|
+
return sanitizeSnapshot(JSON.parse(await fs$1.readFile(file, "utf8")));
|
|
3654
|
+
} catch {
|
|
3655
|
+
return null;
|
|
3656
|
+
}
|
|
3522
3657
|
}
|
|
3523
|
-
function
|
|
3524
|
-
return
|
|
3658
|
+
async function hashBytes(bytes) {
|
|
3659
|
+
return createHash("sha256").update(bytes).digest("hex");
|
|
3525
3660
|
}
|
|
3526
|
-
async function
|
|
3661
|
+
async function writeVerified(target, expected, write) {
|
|
3662
|
+
const tmp = `${target}${BACKUP_PARTIAL_SUFFIX}-${randomUUID()}`;
|
|
3663
|
+
const handle = await fs$1.open(tmp, "w");
|
|
3527
3664
|
try {
|
|
3528
|
-
|
|
3529
|
-
|
|
3530
|
-
|
|
3665
|
+
const sha = await write(handle);
|
|
3666
|
+
await handle.sync();
|
|
3667
|
+
await handle.close();
|
|
3668
|
+
if (sha !== expected) throw new Error("the copy does not match its checksum");
|
|
3669
|
+
await fs$1.rename(tmp, target);
|
|
3670
|
+
} catch (err) {
|
|
3671
|
+
await handle.close().catch(() => {});
|
|
3672
|
+
await fs$1.rm(tmp, { force: true }).catch(() => {});
|
|
3673
|
+
throw err;
|
|
3531
3674
|
}
|
|
3532
3675
|
}
|
|
3533
|
-
async function
|
|
3534
|
-
const
|
|
3535
|
-
|
|
3536
|
-
await fs$1.
|
|
3676
|
+
async function storeBytes(objectsRoot, bytes) {
|
|
3677
|
+
const sha = await hashBytes(bytes);
|
|
3678
|
+
const target = objectPath(objectsRoot, sha);
|
|
3679
|
+
if (await fs$1.stat(target).catch(() => null)) return {
|
|
3680
|
+
sha256: sha,
|
|
3681
|
+
size: bytes.length,
|
|
3682
|
+
added: false
|
|
3683
|
+
};
|
|
3684
|
+
await fs$1.mkdir(path.dirname(target), { recursive: true });
|
|
3685
|
+
await writeVerified(target, sha, async (handle) => {
|
|
3686
|
+
await handle.write(bytes);
|
|
3687
|
+
return hashBytes(bytes);
|
|
3688
|
+
});
|
|
3689
|
+
return {
|
|
3690
|
+
sha256: sha,
|
|
3691
|
+
size: bytes.length,
|
|
3692
|
+
added: true
|
|
3693
|
+
};
|
|
3694
|
+
}
|
|
3695
|
+
async function storeFile(objectsRoot, file) {
|
|
3696
|
+
const sha = await sha256FileStream(file);
|
|
3697
|
+
if (sha === null) throw new Error(`cannot read ${path.basename(file)}`);
|
|
3698
|
+
const target = objectPath(objectsRoot, sha);
|
|
3699
|
+
if (await fs$1.stat(target).catch(() => null)) return {
|
|
3700
|
+
sha256: sha,
|
|
3701
|
+
size: (await fs$1.stat(file).catch(() => null))?.size ?? 0,
|
|
3702
|
+
added: false
|
|
3703
|
+
};
|
|
3704
|
+
await fs$1.mkdir(path.dirname(target), { recursive: true });
|
|
3705
|
+
await writeVerified(target, sha, async (handle) => {
|
|
3706
|
+
const hash = createHash("sha256");
|
|
3707
|
+
for await (const chunk of createReadStream(file, { highWaterMark: CHUNK })) {
|
|
3708
|
+
hash.update(chunk);
|
|
3709
|
+
await handle.write(chunk);
|
|
3710
|
+
}
|
|
3711
|
+
return hash.digest("hex");
|
|
3712
|
+
});
|
|
3713
|
+
return {
|
|
3714
|
+
sha256: sha,
|
|
3715
|
+
size: (await fs$1.stat(target)).size,
|
|
3716
|
+
added: true
|
|
3717
|
+
};
|
|
3537
3718
|
}
|
|
3538
|
-
|
|
3539
|
-
|
|
3540
|
-
|
|
3719
|
+
async function uniqueSnapshotName(snapshotsRoot, at) {
|
|
3720
|
+
const stem = snapshotFileName(at).replace(/\.json$/, "");
|
|
3721
|
+
const taken = new Set(await fs$1.readdir(snapshotsRoot).catch(() => []));
|
|
3722
|
+
let name = `${stem}.json`;
|
|
3723
|
+
for (let n = 2; taken.has(name); n++) name = `${stem} (${n}).json`;
|
|
3724
|
+
return name;
|
|
3541
3725
|
}
|
|
3542
|
-
|
|
3726
|
+
/**
|
|
3727
|
+
* Copies every file the workspace holds into the object store (one copy per
|
|
3728
|
+
* distinct content), then writes the snapshot that names them. The snapshot is
|
|
3729
|
+
* written last: a backup that did not finish leaves no day to restore from.
|
|
3730
|
+
*/
|
|
3731
|
+
async function createSnapshot(settings, deps) {
|
|
3732
|
+
if (!settings.folder) return {
|
|
3733
|
+
ok: false,
|
|
3734
|
+
error: "no backup location is set",
|
|
3735
|
+
warnings: []
|
|
3736
|
+
};
|
|
3737
|
+
const { objectsRoot, snapshotsRoot } = snapshotParts(resolveBackupDir(settings.folder));
|
|
3738
|
+
const plan = await planBackup(settings, deps);
|
|
3739
|
+
const warnings = plan.warnings;
|
|
3543
3740
|
try {
|
|
3544
|
-
|
|
3545
|
-
|
|
3546
|
-
|
|
3741
|
+
const total = plan.entries.length;
|
|
3742
|
+
if (total > deps.limits.files) throw new Error("too many files to back up");
|
|
3743
|
+
let sizeBytes = 0;
|
|
3744
|
+
for (const entry of plan.entries) sizeBytes += "bytes" in entry ? entry.bytes.length : entry.size;
|
|
3745
|
+
if (sizeBytes > deps.limits.totalBytes) throw new Error(BACKUP_TOO_LARGE);
|
|
3746
|
+
await fs$1.mkdir(objectsRoot, { recursive: true });
|
|
3747
|
+
await fs$1.mkdir(snapshotsRoot, { recursive: true });
|
|
3748
|
+
const files = {};
|
|
3749
|
+
let addedBytes = 0;
|
|
3750
|
+
let addedFiles = 0;
|
|
3751
|
+
let done = 0;
|
|
3752
|
+
deps.onProgress(0, total);
|
|
3753
|
+
for (const entry of plan.entries) {
|
|
3754
|
+
const stored = "bytes" in entry ? await storeBytes(objectsRoot, entry.bytes) : await storeFile(objectsRoot, entry.file);
|
|
3755
|
+
files[entry.name] = {
|
|
3756
|
+
sha256: stored.sha256,
|
|
3757
|
+
size: stored.size
|
|
3758
|
+
};
|
|
3759
|
+
if (stored.added) {
|
|
3760
|
+
addedBytes += stored.size;
|
|
3761
|
+
addedFiles += 1;
|
|
3762
|
+
}
|
|
3763
|
+
done += 1;
|
|
3764
|
+
deps.onProgress(done, total);
|
|
3765
|
+
}
|
|
3766
|
+
const snapshot = {
|
|
3767
|
+
format: SNAPSHOT_FORMAT,
|
|
3768
|
+
version: 1,
|
|
3769
|
+
createdAt: deps.now,
|
|
3770
|
+
app: {
|
|
3771
|
+
name: "LemonRiver",
|
|
3772
|
+
version: deps.coreVersion
|
|
3773
|
+
},
|
|
3774
|
+
files,
|
|
3775
|
+
decks: plan.decks,
|
|
3776
|
+
folders: plan.folders,
|
|
3777
|
+
prefs: plan.prefs,
|
|
3778
|
+
skipped: plan.skipped
|
|
3779
|
+
};
|
|
3780
|
+
const name = await uniqueSnapshotName(snapshotsRoot, deps.now);
|
|
3781
|
+
const target = path.join(snapshotsRoot, name);
|
|
3782
|
+
const tmp = `${target}${BACKUP_PARTIAL_SUFFIX}-${randomUUID()}`;
|
|
3783
|
+
await fs$1.writeFile(tmp, `${JSON.stringify(snapshot, null, 2)}\n`);
|
|
3784
|
+
await fs$1.rename(tmp, target);
|
|
3785
|
+
return {
|
|
3786
|
+
ok: true,
|
|
3787
|
+
path: target,
|
|
3788
|
+
name,
|
|
3789
|
+
size: sizeBytes,
|
|
3790
|
+
addedBytes,
|
|
3791
|
+
addedFiles,
|
|
3792
|
+
files: total,
|
|
3793
|
+
skipped: plan.skipped,
|
|
3794
|
+
warnings
|
|
3795
|
+
};
|
|
3796
|
+
} catch (err) {
|
|
3797
|
+
return {
|
|
3798
|
+
ok: false,
|
|
3799
|
+
error: String(err?.message ?? err),
|
|
3800
|
+
warnings
|
|
3801
|
+
};
|
|
3547
3802
|
}
|
|
3548
3803
|
}
|
|
3549
|
-
async function
|
|
3550
|
-
const file = prefsSnapshotFile(userCwd);
|
|
3551
|
-
await fs$1.mkdir(path.dirname(file), { recursive: true });
|
|
3552
|
-
await fs$1.writeFile(file, `${JSON.stringify(sanitizePrefs$1(prefs), null, 2)}\n`);
|
|
3553
|
-
}
|
|
3554
|
-
/** Checks that the folder can hold backups before anything is saved. */
|
|
3555
|
-
async function checkWritableFolder(folder) {
|
|
3804
|
+
async function readdirSafe(dir) {
|
|
3556
3805
|
try {
|
|
3557
|
-
|
|
3558
|
-
if (!(await fs$1.stat(folder)).isDirectory()) return "bad-folder";
|
|
3806
|
+
return await fs$1.readdir(dir);
|
|
3559
3807
|
} catch {
|
|
3560
|
-
return
|
|
3808
|
+
return [];
|
|
3561
3809
|
}
|
|
3562
|
-
|
|
3563
|
-
|
|
3810
|
+
}
|
|
3811
|
+
async function treeBytes(dir) {
|
|
3812
|
+
let total = 0;
|
|
3813
|
+
for (const name of await readdirSafe(dir)) {
|
|
3814
|
+
const full = path.join(dir, name);
|
|
3815
|
+
const stat = await fs$1.stat(full).catch(() => null);
|
|
3816
|
+
if (!stat) continue;
|
|
3817
|
+
if (stat.isDirectory()) total += await treeBytes(full);
|
|
3818
|
+
else total += stat.size;
|
|
3819
|
+
}
|
|
3820
|
+
return total;
|
|
3821
|
+
}
|
|
3822
|
+
/**
|
|
3823
|
+
* Deletes snapshots past the keep window, then objects no remaining snapshot
|
|
3824
|
+
* names. A snapshot this build cannot read may still be using content, and a
|
|
3825
|
+
* copy another machine is writing has no snapshot yet: either case means no
|
|
3826
|
+
* content is removed this run.
|
|
3827
|
+
*/
|
|
3828
|
+
async function pruneSnapshots(dir, now, keepDays = 30) {
|
|
3829
|
+
const { objectsRoot, snapshotsRoot } = snapshotParts(dir);
|
|
3830
|
+
const cutoff = now - keepDays * DAY_MS;
|
|
3831
|
+
const removed = [];
|
|
3832
|
+
const referenced = /* @__PURE__ */ new Set();
|
|
3833
|
+
let unreadable = false;
|
|
3834
|
+
for (const name of await readdirSafe(snapshotsRoot)) {
|
|
3835
|
+
if (!name.endsWith(".json")) continue;
|
|
3836
|
+
const file = path.join(snapshotsRoot, name);
|
|
3837
|
+
const snapshot = await readSnapshot(file);
|
|
3838
|
+
if (!snapshot) {
|
|
3839
|
+
unreadable = true;
|
|
3840
|
+
continue;
|
|
3841
|
+
}
|
|
3842
|
+
if (snapshot.createdAt < cutoff) {
|
|
3843
|
+
await fs$1.rm(file, { force: true }).catch(() => {});
|
|
3844
|
+
removed.push(name);
|
|
3845
|
+
continue;
|
|
3846
|
+
}
|
|
3847
|
+
for (const entry of Object.values(snapshot.files)) referenced.add(entry.sha256);
|
|
3848
|
+
}
|
|
3849
|
+
if (unreadable) return {
|
|
3850
|
+
removed,
|
|
3851
|
+
removedObjects: 0,
|
|
3852
|
+
freedBytes: 0
|
|
3853
|
+
};
|
|
3854
|
+
const freshCutoff = now - DAY_MS;
|
|
3855
|
+
let removedObjects = 0;
|
|
3856
|
+
let freedBytes = 0;
|
|
3857
|
+
for (const bucket of await readdirSafe(objectsRoot)) {
|
|
3858
|
+
const bucketDir = path.join(objectsRoot, bucket);
|
|
3859
|
+
if (!(await fs$1.stat(bucketDir).catch(() => null))?.isDirectory()) continue;
|
|
3860
|
+
for (const name of await readdirSafe(bucketDir)) {
|
|
3861
|
+
const full = path.join(bucketDir, name);
|
|
3862
|
+
if (referenced.has(name)) continue;
|
|
3863
|
+
const fileStat = await fs$1.stat(full).catch(() => null);
|
|
3864
|
+
if (!fileStat?.isFile()) continue;
|
|
3865
|
+
if (fileStat.mtimeMs > freshCutoff) continue;
|
|
3866
|
+
await fs$1.rm(full, { force: true }).catch(() => {});
|
|
3867
|
+
removedObjects += 1;
|
|
3868
|
+
freedBytes += fileStat.size;
|
|
3869
|
+
}
|
|
3870
|
+
}
|
|
3871
|
+
return {
|
|
3872
|
+
removed,
|
|
3873
|
+
removedObjects,
|
|
3874
|
+
freedBytes
|
|
3875
|
+
};
|
|
3876
|
+
}
|
|
3877
|
+
function deckFor(rel) {
|
|
3878
|
+
const match = /^slides\/([^/]+)\//.exec(rel);
|
|
3879
|
+
return match ? match[1] : null;
|
|
3880
|
+
}
|
|
3881
|
+
async function listSnapshots(dir) {
|
|
3882
|
+
const { snapshotsRoot } = snapshotParts(dir);
|
|
3883
|
+
const out = [];
|
|
3884
|
+
for (const name of await readdirSafe(snapshotsRoot)) {
|
|
3885
|
+
if (!name.endsWith(".json")) continue;
|
|
3886
|
+
const file = path.join(snapshotsRoot, name);
|
|
3887
|
+
const snapshot = await readSnapshot(file);
|
|
3888
|
+
if (!snapshot) continue;
|
|
3889
|
+
let size = 0;
|
|
3890
|
+
for (const entry of Object.values(snapshot.files)) size += entry.size;
|
|
3891
|
+
out.push({
|
|
3892
|
+
name: name.replace(/\.json$/, ""),
|
|
3893
|
+
path: file,
|
|
3894
|
+
at: snapshot.createdAt,
|
|
3895
|
+
size,
|
|
3896
|
+
decks: snapshot.decks.length
|
|
3897
|
+
});
|
|
3898
|
+
}
|
|
3899
|
+
return out.sort((a, b) => b.at - a.at);
|
|
3900
|
+
}
|
|
3901
|
+
async function summarize(dir, settings) {
|
|
3902
|
+
const { objectsRoot, snapshotsRoot } = snapshotParts(dir);
|
|
3903
|
+
const totalBytes = await treeBytes(objectsRoot) + await treeBytes(snapshotsRoot);
|
|
3904
|
+
const [latest] = await listSnapshots(dir);
|
|
3905
|
+
const biggest = [];
|
|
3906
|
+
if (latest) {
|
|
3907
|
+
const snapshot = await readSnapshot(latest.path);
|
|
3908
|
+
if (snapshot) {
|
|
3909
|
+
const titles = new Map(snapshot.decks.map((deck) => [deck.slideId, deck.title]));
|
|
3910
|
+
biggest.push(...Object.entries(snapshot.files).map(([rel, entry]) => {
|
|
3911
|
+
const slideId = deckFor(rel);
|
|
3912
|
+
return {
|
|
3913
|
+
rel,
|
|
3914
|
+
size: entry.size,
|
|
3915
|
+
deck: slideId ? titles.get(slideId) ?? slideId : null
|
|
3916
|
+
};
|
|
3917
|
+
}).sort((a, b) => b.size - a.size).slice(0, 5));
|
|
3918
|
+
}
|
|
3919
|
+
}
|
|
3920
|
+
return {
|
|
3921
|
+
totalBytes,
|
|
3922
|
+
last: settings.last,
|
|
3923
|
+
biggest,
|
|
3924
|
+
skipLargeVideos: settings.skipLargeVideos
|
|
3925
|
+
};
|
|
3926
|
+
}
|
|
3927
|
+
/** Writes one snapshot back into the ordinary zip layout readers already know. */
|
|
3928
|
+
async function writeSnapshotZip(dir, snapshot, target, opts) {
|
|
3929
|
+
const { objectsRoot } = snapshotParts(dir);
|
|
3930
|
+
const writer = new ZipWriter(target);
|
|
3564
3931
|
try {
|
|
3565
|
-
|
|
3566
|
-
|
|
3567
|
-
|
|
3568
|
-
|
|
3932
|
+
let sizeBytes = 0;
|
|
3933
|
+
for (const entry of Object.values(snapshot.files)) sizeBytes += entry.size;
|
|
3934
|
+
const guide = {
|
|
3935
|
+
createdAt: snapshot.createdAt,
|
|
3936
|
+
decks: snapshot.decks.length,
|
|
3937
|
+
sizeBytes,
|
|
3938
|
+
appVersion: opts.coreVersion,
|
|
3939
|
+
locale: guideLocaleFor(opts.locale)
|
|
3940
|
+
};
|
|
3941
|
+
await writer.addBytes(restoreGuideFileName(guide.locale), strToU8(buildRestoreGuide(guide)));
|
|
3942
|
+
const files = {};
|
|
3943
|
+
for (const [rel, entry] of Object.entries(snapshot.files)) {
|
|
3944
|
+
const source = objectPath(objectsRoot, entry.sha256);
|
|
3945
|
+
const result = await writer.addFile(`${SHARE_ROOT}/${rel}`, source, opts.limits).catch((err) => {
|
|
3946
|
+
throw new Error(`${rel}: ${err.message}`);
|
|
3947
|
+
});
|
|
3948
|
+
if (result.sha !== entry.sha256) throw new Error(`${rel} does not match the checksum this backup recorded`);
|
|
3949
|
+
files[rel] = result.sha;
|
|
3950
|
+
}
|
|
3951
|
+
const manifest = {
|
|
3952
|
+
format: SHARE_FORMAT,
|
|
3953
|
+
version: 1,
|
|
3954
|
+
kind: "backup",
|
|
3955
|
+
app: snapshot.app,
|
|
3956
|
+
createdAt: snapshot.createdAt,
|
|
3957
|
+
decks: snapshot.decks,
|
|
3958
|
+
files
|
|
3959
|
+
};
|
|
3960
|
+
await writer.addBytes(`${SHARE_ROOT}/${SHARE_MANIFEST}`, strToU8(`${JSON.stringify(manifest, null, 2)}\n`));
|
|
3961
|
+
return await writer.close();
|
|
3569
3962
|
} catch (err) {
|
|
3570
|
-
|
|
3571
|
-
|
|
3963
|
+
writer.abort();
|
|
3964
|
+
await fs$1.rm(target, { force: true }).catch(() => {});
|
|
3965
|
+
throw err;
|
|
3572
3966
|
}
|
|
3573
3967
|
}
|
|
3574
3968
|
//#endregion
|
|
3575
|
-
//#region src/lemonriver/backup/
|
|
3969
|
+
//#region src/lemonriver/backup/inspect.ts
|
|
3576
3970
|
const ENTRY_NAMES$3 = [
|
|
3577
3971
|
"index.tsx",
|
|
3578
3972
|
"index.jsx",
|
|
3579
3973
|
"index.ts",
|
|
3580
3974
|
"index.js"
|
|
3581
3975
|
];
|
|
3582
|
-
|
|
3583
|
-
|
|
3584
|
-
|
|
3585
|
-
|
|
3586
|
-
|
|
3587
|
-
|
|
3588
|
-
|
|
3976
|
+
function failure$1(code, detail, appVersion) {
|
|
3977
|
+
return {
|
|
3978
|
+
ok: false,
|
|
3979
|
+
code,
|
|
3980
|
+
detail,
|
|
3981
|
+
...appVersion ? { appVersion } : {}
|
|
3982
|
+
};
|
|
3589
3983
|
}
|
|
3590
|
-
|
|
3591
|
-
|
|
3592
|
-
|
|
3593
|
-
|
|
3594
|
-
|
|
3595
|
-
|
|
3596
|
-
|
|
3597
|
-
|
|
3598
|
-
|
|
3599
|
-
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
}
|
|
3605
|
-
if (
|
|
3606
|
-
|
|
3607
|
-
|
|
3608
|
-
if (!stat) continue;
|
|
3609
|
-
if (stat.size > limits.fileBytes) {
|
|
3610
|
-
warnings.push(`${label}${rel}`);
|
|
3611
|
-
continue;
|
|
3612
|
-
}
|
|
3613
|
-
out.push({
|
|
3614
|
-
rel,
|
|
3615
|
-
file: full
|
|
3616
|
-
});
|
|
3984
|
+
function isRecord$1(value) {
|
|
3985
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3986
|
+
}
|
|
3987
|
+
const EXECUTABLE_NAME = /\.(?:sh|command)$/i;
|
|
3988
|
+
/** A program, by its name, its packaged app folder, or its own mode bits. */
|
|
3989
|
+
function looksExecutable(rel) {
|
|
3990
|
+
if (EXECUTABLE_NAME.test(rel)) return true;
|
|
3991
|
+
return rel.split("/").slice(0, -1).some((segment) => segment.toLowerCase().endsWith(".app"));
|
|
3992
|
+
}
|
|
3993
|
+
function findPrefix$1(names) {
|
|
3994
|
+
let best = null;
|
|
3995
|
+
for (const name of names) {
|
|
3996
|
+
let prefix = null;
|
|
3997
|
+
if (name === "manifest.json") prefix = "";
|
|
3998
|
+
else if (name === `LemonRiver-files/manifest.json`) prefix = `${SHARE_ROOT}/`;
|
|
3999
|
+
else if (name.endsWith(`/LemonRiver-files/manifest.json`)) prefix = name.slice(0, name.length - 13);
|
|
4000
|
+
if (prefix === null) continue;
|
|
4001
|
+
if (best === null || prefix.length < best.length) best = prefix;
|
|
3617
4002
|
}
|
|
4003
|
+
return best;
|
|
3618
4004
|
}
|
|
3619
|
-
async function
|
|
4005
|
+
async function localTitle(userCwd, slideId) {
|
|
4006
|
+
const outline = (await readBoardFile(boardStructurePath(userCwd, slideId)).catch(() => ({}))).outline;
|
|
4007
|
+
if (typeof outline?.brief === "string" && outline.brief.trim() !== "") return outline.brief.trim();
|
|
3620
4008
|
for (const name of ENTRY_NAMES$3) try {
|
|
3621
|
-
|
|
3622
|
-
|
|
3623
|
-
|
|
3624
|
-
};
|
|
4009
|
+
const source = await fs$1.readFile(path.join(userCwd, "slides", slideId, name), "utf8");
|
|
4010
|
+
const read = readMetaTitleInSource(source);
|
|
4011
|
+
if (read.kind === "found") return read.title;
|
|
3625
4012
|
} catch {}
|
|
3626
4013
|
return null;
|
|
3627
4014
|
}
|
|
3628
|
-
|
|
3629
|
-
|
|
3630
|
-
|
|
4015
|
+
function readWorkspace(bytes) {
|
|
4016
|
+
const empty = {
|
|
4017
|
+
folders: {
|
|
4018
|
+
folders: [],
|
|
4019
|
+
assignments: {}
|
|
4020
|
+
},
|
|
4021
|
+
prefs: {},
|
|
4022
|
+
createdAt: 0
|
|
4023
|
+
};
|
|
4024
|
+
if (!bytes) return empty;
|
|
3631
4025
|
try {
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
4026
|
+
const raw = JSON.parse(new TextDecoder().decode(bytes));
|
|
4027
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return empty;
|
|
4028
|
+
const value = raw;
|
|
4029
|
+
return {
|
|
4030
|
+
folders: typeof value.folders === "object" && value.folders !== null && !Array.isArray(value.folders) ? value.folders : empty.folders,
|
|
4031
|
+
prefs: sanitizePrefs$1(value.prefs),
|
|
4032
|
+
createdAt: typeof value.createdAt === "number" ? value.createdAt : 0
|
|
4033
|
+
};
|
|
3637
4034
|
} catch {
|
|
3638
|
-
return
|
|
3639
|
-
}
|
|
3640
|
-
for (const raw of body) {
|
|
3641
|
-
const node = raw;
|
|
3642
|
-
if (node.type !== "ExportDefaultDeclaration") continue;
|
|
3643
|
-
let inner = node.declaration;
|
|
3644
|
-
while (inner && (inner.type === "TSAsExpression" || inner.type === "TSSatisfiesExpression")) inner = inner.expression;
|
|
3645
|
-
if (inner?.type !== "ArrayExpression") return null;
|
|
3646
|
-
const elements = inner.elements ?? [];
|
|
3647
|
-
return elements.some((element) => element === null) ? null : elements.length;
|
|
4035
|
+
return empty;
|
|
3648
4036
|
}
|
|
3649
|
-
return null;
|
|
3650
4037
|
}
|
|
3651
|
-
|
|
4038
|
+
/** Validates a backup on disk by reading it once; writes nothing. */
|
|
4039
|
+
async function inspectBackupFile(file, deps) {
|
|
4040
|
+
if (!path.isAbsolute(file) || path.extname(file).toLowerCase() !== ".zip") return failure$1("not-a-package", "a backup is a .zip file");
|
|
4041
|
+
const stat = await fs$1.stat(file).catch(() => null);
|
|
4042
|
+
if (!stat?.isFile()) return failure$1("not-a-package", `${file} is not a file`);
|
|
4043
|
+
let scan;
|
|
3652
4044
|
try {
|
|
3653
|
-
|
|
3654
|
-
|
|
3655
|
-
|
|
3656
|
-
|
|
3657
|
-
|
|
3658
|
-
|
|
3659
|
-
};
|
|
3660
|
-
}
|
|
3661
|
-
|
|
3662
|
-
async function planBackup(settings, deps) {
|
|
3663
|
-
const warnings = [];
|
|
3664
|
-
const entries = [];
|
|
3665
|
-
const decks = [];
|
|
3666
|
-
const targetDir = settings.folder ? path.resolve(resolveBackupDir(settings.folder)) : null;
|
|
3667
|
-
const skipDirs = new Set(targetDir ? [targetDir] : []);
|
|
3668
|
-
let slideIds = [];
|
|
4045
|
+
scan = await scanZip(file, deps.limits, (name) => name.endsWith(`/manifest.json`) || name.endsWith("/workspace.json"));
|
|
4046
|
+
} catch (err) {
|
|
4047
|
+
return failure$1("not-a-package", String(err?.message ?? err));
|
|
4048
|
+
}
|
|
4049
|
+
const prefix = findPrefix$1(scan.entries.map((entry) => entry.name));
|
|
4050
|
+
if (prefix === null) return failure$1("not-a-package", `no ${SHARE_ROOT}/${SHARE_MANIFEST} in the zip`);
|
|
4051
|
+
const manifestBytes = scan.kept.get(`${prefix}${SHARE_MANIFEST}`);
|
|
4052
|
+
if (!manifestBytes) return failure$1("bad-manifest", `no ${SHARE_ROOT}/${SHARE_MANIFEST} in the zip`);
|
|
4053
|
+
let raw;
|
|
3669
4054
|
try {
|
|
3670
|
-
|
|
3671
|
-
} catch {
|
|
3672
|
-
|
|
3673
|
-
const slideDir = path.join(deps.slidesRoot, slideId);
|
|
3674
|
-
const files = [];
|
|
3675
|
-
await walk$1(slideDir, "", `slides/${slideId}/`, skipDirs, deps.limits, warnings, files);
|
|
3676
|
-
for (const { rel, file } of files) {
|
|
3677
|
-
const name = `slides/${slideId}/${rel}`;
|
|
3678
|
-
if (packagePathProblem(name, "backup")) {
|
|
3679
|
-
warnings.push(name);
|
|
3680
|
-
continue;
|
|
3681
|
-
}
|
|
3682
|
-
entries.push({
|
|
3683
|
-
name,
|
|
3684
|
-
file
|
|
3685
|
-
});
|
|
3686
|
-
}
|
|
3687
|
-
const source = await deckEntry(slideDir);
|
|
3688
|
-
const titleRead = source ? readMetaTitleInSource(source.source) : { kind: "missing" };
|
|
3689
|
-
const title = titleRead.kind === "found" ? titleRead.title : slideId;
|
|
3690
|
-
const theme = source ? readMetaThemeInSource(source.source) : null;
|
|
3691
|
-
const record = await readBoardFile(boardStructurePath(deps.userCwd, slideId)).catch(() => ({}));
|
|
3692
|
-
const stored = typeof record.shareId === "string" && record.shareId !== "" ? record.shareId : "";
|
|
3693
|
-
if (Object.keys(record).length > 0) entries.push({
|
|
3694
|
-
name: `board/${slideId}.json`,
|
|
3695
|
-
bytes: strToU8(`${JSON.stringify(stripBoardRecord(record), null, 2)}\n`)
|
|
3696
|
-
});
|
|
3697
|
-
decks.push({
|
|
3698
|
-
slideId,
|
|
3699
|
-
title,
|
|
3700
|
-
pages: source ? pageCountInSource(source.source) : null,
|
|
3701
|
-
shareId: stored !== "" ? stored : slideId,
|
|
3702
|
-
theme,
|
|
3703
|
-
thumbs: 0
|
|
3704
|
-
});
|
|
4055
|
+
raw = JSON.parse(new TextDecoder().decode(manifestBytes));
|
|
4056
|
+
} catch {
|
|
4057
|
+
return failure$1("bad-manifest", "manifest.json is not readable JSON");
|
|
3705
4058
|
}
|
|
3706
|
-
const
|
|
3707
|
-
|
|
3708
|
-
|
|
3709
|
-
|
|
3710
|
-
|
|
3711
|
-
|
|
3712
|
-
|
|
3713
|
-
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
|
|
4059
|
+
const read = sanitizeManifest(raw);
|
|
4060
|
+
if (!read.ok) return failure$1(read.code, read.detail, read.appVersion);
|
|
4061
|
+
if (read.manifest.kind !== "backup") return failure$1("not-a-package", "this zip is a share package, not a backup");
|
|
4062
|
+
const manifest = read.manifest;
|
|
4063
|
+
const inside = /* @__PURE__ */ new Map();
|
|
4064
|
+
for (const entry of scan.entries) {
|
|
4065
|
+
if (!entry.name.startsWith(prefix)) continue;
|
|
4066
|
+
const rel = entry.name.slice(prefix.length);
|
|
4067
|
+
if (rel === "" || rel === "manifest.json") continue;
|
|
4068
|
+
inside.set(rel, {
|
|
4069
|
+
sha: entry.sha,
|
|
4070
|
+
size: entry.size,
|
|
4071
|
+
executable: entry.executable
|
|
3717
4072
|
});
|
|
3718
4073
|
}
|
|
3719
|
-
const
|
|
3720
|
-
|
|
3721
|
-
|
|
3722
|
-
const name = `themes/${rel}`;
|
|
3723
|
-
if (packagePathProblem(name, "backup")) {
|
|
3724
|
-
warnings.push(name);
|
|
3725
|
-
continue;
|
|
3726
|
-
}
|
|
3727
|
-
entries.push({
|
|
3728
|
-
name,
|
|
3729
|
-
file
|
|
3730
|
-
});
|
|
4074
|
+
for (const rel of [...Object.keys(manifest.files), ...inside.keys()]) {
|
|
4075
|
+
const problem = packagePathProblem(rel, "backup");
|
|
4076
|
+
if (problem) return failure$1(problem.code, `${rel}: ${problem.detail}`);
|
|
3731
4077
|
}
|
|
3732
|
-
const
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
createdAt: deps.now
|
|
3740
|
-
}, null, 2)}\n`)
|
|
4078
|
+
const missing = Object.keys(manifest.files).filter((rel) => !inside.has(rel));
|
|
4079
|
+
if (missing.length > 0) return failure$1("missing-file", `${missing[0]} is listed but not in the zip`);
|
|
4080
|
+
const extra = [...inside.keys()].filter((rel) => !(rel in manifest.files));
|
|
4081
|
+
if (extra.length > 0) return failure$1("extra-file", `${extra[0]} is in the zip but not in the manifest`);
|
|
4082
|
+
const wrong = Object.keys(manifest.files).filter((rel) => {
|
|
4083
|
+
const found = inside.get(rel);
|
|
4084
|
+
return found ? found.sha !== manifest.files[rel] : false;
|
|
3741
4085
|
});
|
|
4086
|
+
if (wrong.length > 0) return failure$1("checksum", `${wrong[0]} does not match its checksum`);
|
|
4087
|
+
const workspace = readWorkspace(scan.kept.get(`${prefix}workspace.json`));
|
|
4088
|
+
const assignments = typeof workspace.folders.assignments === "object" && workspace.folders.assignments !== null && !Array.isArray(workspace.folders.assignments) ? workspace.folders.assignments : {};
|
|
4089
|
+
const folders = Array.isArray(workspace.folders.folders) ? workspace.folders.folders.length : 0;
|
|
4090
|
+
const localIds = new Set((await fs$1.readdir(deps.slidesRoot).catch(() => [])).map((id) => id.toLowerCase()));
|
|
4091
|
+
const decks = [];
|
|
4092
|
+
for (const deck of manifest.decks) {
|
|
4093
|
+
const local = localIds.has(deck.slideId.toLowerCase());
|
|
4094
|
+
decks.push({
|
|
4095
|
+
slideId: deck.slideId,
|
|
4096
|
+
title: deck.title,
|
|
4097
|
+
pages: deck.pages,
|
|
4098
|
+
local,
|
|
4099
|
+
localTitle: local ? await localTitle(deps.userCwd, deck.slideId) : null,
|
|
4100
|
+
folderId: typeof assignments[deck.slideId] === "string" ? assignments[deck.slideId] : null
|
|
4101
|
+
});
|
|
4102
|
+
}
|
|
4103
|
+
let assetsCount = 0;
|
|
4104
|
+
let assetsSize = 0;
|
|
4105
|
+
let themes = 0;
|
|
4106
|
+
const executables = [];
|
|
4107
|
+
for (const [rel, info] of inside) {
|
|
4108
|
+
if (rel.startsWith("assets/")) {
|
|
4109
|
+
assetsCount += 1;
|
|
4110
|
+
assetsSize += info.size;
|
|
4111
|
+
} else if (rel.startsWith("themes/")) themes += 1;
|
|
4112
|
+
if (info.executable || looksExecutable(rel)) executables.push(rel);
|
|
4113
|
+
}
|
|
3742
4114
|
return {
|
|
3743
|
-
|
|
3744
|
-
|
|
3745
|
-
|
|
3746
|
-
|
|
3747
|
-
|
|
4115
|
+
ok: true,
|
|
4116
|
+
manifest,
|
|
4117
|
+
workspace,
|
|
4118
|
+
prefix,
|
|
4119
|
+
size: stat.size,
|
|
4120
|
+
preview: {
|
|
4121
|
+
createdAt: manifest.createdAt || workspace.createdAt,
|
|
4122
|
+
app: manifest.app,
|
|
4123
|
+
decks,
|
|
4124
|
+
assets: {
|
|
4125
|
+
count: assetsCount,
|
|
4126
|
+
size: assetsSize
|
|
4127
|
+
},
|
|
4128
|
+
themes,
|
|
4129
|
+
folders,
|
|
4130
|
+
prefs: Object.keys(workspace.prefs).length > 0,
|
|
4131
|
+
size: stat.size,
|
|
4132
|
+
executables
|
|
4133
|
+
}
|
|
3748
4134
|
};
|
|
3749
4135
|
}
|
|
3750
|
-
|
|
3751
|
-
|
|
3752
|
-
|
|
3753
|
-
|
|
3754
|
-
|
|
3755
|
-
|
|
3756
|
-
}
|
|
3757
|
-
try {
|
|
3758
|
-
total += (await fs$1.stat(entry.file)).size;
|
|
3759
|
-
} catch {}
|
|
4136
|
+
/** The same preview the zip path builds, read straight from a snapshot file. */
|
|
4137
|
+
async function inspectSnapshotFile(file, deps) {
|
|
4138
|
+
const snapshot = await readSnapshot(file);
|
|
4139
|
+
if (!snapshot) return failure$1("not-a-package", "this file is not a backup snapshot");
|
|
4140
|
+
for (const rel of Object.keys(snapshot.files)) {
|
|
4141
|
+
const problem = packagePathProblem(rel, "backup");
|
|
4142
|
+
if (problem) return failure$1(problem.code, `${rel}: ${problem.detail}`);
|
|
3760
4143
|
}
|
|
3761
|
-
|
|
3762
|
-
|
|
3763
|
-
|
|
3764
|
-
const
|
|
3765
|
-
|
|
3766
|
-
let
|
|
3767
|
-
|
|
3768
|
-
|
|
3769
|
-
|
|
3770
|
-
|
|
3771
|
-
|
|
3772
|
-
|
|
3773
|
-
|
|
3774
|
-
|
|
3775
|
-
if (rel
|
|
3776
|
-
|
|
3777
|
-
sha: entry.sha,
|
|
3778
|
-
size: entry.size
|
|
3779
|
-
});
|
|
4144
|
+
const assignments = isRecord$1(snapshot.folders.assignments) ? snapshot.folders.assignments : {};
|
|
4145
|
+
const folderCount = Array.isArray(snapshot.folders.folders) ? snapshot.folders.folders.length : 0;
|
|
4146
|
+
const localIds = new Set((await fs$1.readdir(deps.slidesRoot).catch(() => [])).map((id) => id.toLowerCase()));
|
|
4147
|
+
const decks = [];
|
|
4148
|
+
let size = 0;
|
|
4149
|
+
let assetsCount = 0;
|
|
4150
|
+
let assetsSize = 0;
|
|
4151
|
+
let themes = 0;
|
|
4152
|
+
const executables = [];
|
|
4153
|
+
for (const [rel, info] of Object.entries(snapshot.files)) {
|
|
4154
|
+
size += info.size;
|
|
4155
|
+
if (rel.startsWith("assets/")) {
|
|
4156
|
+
assetsCount += 1;
|
|
4157
|
+
assetsSize += info.size;
|
|
4158
|
+
} else if (rel.startsWith("themes/")) themes += 1;
|
|
4159
|
+
if (looksExecutable(rel)) executables.push(rel);
|
|
3780
4160
|
}
|
|
3781
|
-
for (const
|
|
3782
|
-
const
|
|
3783
|
-
|
|
3784
|
-
|
|
4161
|
+
for (const deck of snapshot.decks) {
|
|
4162
|
+
const local = localIds.has(deck.slideId.toLowerCase());
|
|
4163
|
+
decks.push({
|
|
4164
|
+
slideId: deck.slideId,
|
|
4165
|
+
title: deck.title,
|
|
4166
|
+
pages: deck.pages,
|
|
4167
|
+
local,
|
|
4168
|
+
localTitle: local ? await localTitle(deps.userCwd, deck.slideId) : null,
|
|
4169
|
+
folderId: typeof assignments[deck.slideId] === "string" ? assignments[deck.slideId] : null
|
|
4170
|
+
});
|
|
3785
4171
|
}
|
|
3786
|
-
|
|
3787
|
-
|
|
3788
|
-
|
|
3789
|
-
async function createBackup(settings, deps) {
|
|
3790
|
-
if (!settings.folder) return {
|
|
3791
|
-
ok: false,
|
|
3792
|
-
error: "no backup location is set",
|
|
3793
|
-
warnings: []
|
|
3794
|
-
};
|
|
3795
|
-
const dir = resolveBackupDir(settings.folder);
|
|
3796
|
-
const plan = await planBackup(settings, deps);
|
|
3797
|
-
const total = plan.entries.length;
|
|
3798
|
-
await fs$1.mkdir(dir, { recursive: true });
|
|
3799
|
-
const partial = path.join(dir, `${backupFileStem(deps.now)}${BACKUP_PARTIAL_SUFFIX}`);
|
|
3800
|
-
const writer = new ZipWriter(partial);
|
|
3801
|
-
const files = {};
|
|
3802
|
-
try {
|
|
3803
|
-
const sizeBytes = await sizes(plan.entries);
|
|
3804
|
-
if (plan.entries.length > deps.limits.files) throw new Error("too many files to back up");
|
|
3805
|
-
if (sizeBytes > deps.limits.totalBytes) throw new Error(BACKUP_TOO_LARGE);
|
|
3806
|
-
const guide = {
|
|
3807
|
-
createdAt: deps.now,
|
|
3808
|
-
decks: plan.decks.length,
|
|
3809
|
-
sizeBytes,
|
|
3810
|
-
appVersion: deps.coreVersion,
|
|
3811
|
-
locale: guideLocaleFor(deps.locale)
|
|
3812
|
-
};
|
|
3813
|
-
await writer.addBytes(restoreGuideFileName(guide.locale), strToU8(buildRestoreGuide(guide)));
|
|
3814
|
-
let done = 0;
|
|
3815
|
-
deps.onProgress(0, total);
|
|
3816
|
-
for (const entry of plan.entries) {
|
|
3817
|
-
const name = `${SHARE_ROOT}/${entry.name}`;
|
|
3818
|
-
const result = "bytes" in entry ? await writer.addBytes(name, entry.bytes) : await writer.addFile(name, entry.file, deps.limits);
|
|
3819
|
-
files[entry.name] = result.sha;
|
|
3820
|
-
done += 1;
|
|
3821
|
-
deps.onProgress(done, total);
|
|
3822
|
-
}
|
|
3823
|
-
const manifest = {
|
|
4172
|
+
return {
|
|
4173
|
+
ok: true,
|
|
4174
|
+
manifest: {
|
|
3824
4175
|
format: SHARE_FORMAT,
|
|
3825
4176
|
version: 1,
|
|
3826
4177
|
kind: "backup",
|
|
3827
|
-
app:
|
|
3828
|
-
|
|
3829
|
-
|
|
4178
|
+
app: snapshot.app,
|
|
4179
|
+
createdAt: snapshot.createdAt,
|
|
4180
|
+
decks: snapshot.decks,
|
|
4181
|
+
files: Object.fromEntries(Object.entries(snapshot.files).map(([rel, entry]) => [rel, entry.sha256]))
|
|
4182
|
+
},
|
|
4183
|
+
workspace: {
|
|
4184
|
+
folders: snapshot.folders,
|
|
4185
|
+
prefs: snapshot.prefs,
|
|
4186
|
+
createdAt: snapshot.createdAt
|
|
4187
|
+
},
|
|
4188
|
+
prefix: "",
|
|
4189
|
+
size,
|
|
4190
|
+
preview: {
|
|
4191
|
+
createdAt: snapshot.createdAt,
|
|
4192
|
+
app: snapshot.app,
|
|
4193
|
+
decks,
|
|
4194
|
+
assets: {
|
|
4195
|
+
count: assetsCount,
|
|
4196
|
+
size: assetsSize
|
|
3830
4197
|
},
|
|
3831
|
-
|
|
3832
|
-
|
|
3833
|
-
|
|
3834
|
-
};
|
|
3835
|
-
await writer.addBytes(`${SHARE_ROOT}/${SHARE_MANIFEST}`, strToU8(`${JSON.stringify(manifest, null, 2)}\n`));
|
|
3836
|
-
const size = await writer.close();
|
|
3837
|
-
const problem = verifyScan((await scanZip(partial, deps.limits)).entries, manifest);
|
|
3838
|
-
if (problem) throw new Error(problem);
|
|
3839
|
-
const name = await uniqueTarget(dir, deps.now);
|
|
3840
|
-
const target = path.join(dir, name);
|
|
3841
|
-
await fs$1.rename(partial, target);
|
|
3842
|
-
return {
|
|
3843
|
-
ok: true,
|
|
3844
|
-
path: target,
|
|
3845
|
-
name,
|
|
4198
|
+
themes,
|
|
4199
|
+
folders: folderCount,
|
|
4200
|
+
prefs: Object.keys(snapshot.prefs).length > 0,
|
|
3846
4201
|
size,
|
|
3847
|
-
|
|
3848
|
-
|
|
4202
|
+
executables,
|
|
4203
|
+
skipped: snapshot.skipped
|
|
4204
|
+
}
|
|
4205
|
+
};
|
|
4206
|
+
}
|
|
4207
|
+
//#endregion
|
|
4208
|
+
//#region src/lemonriver/backup/jobs.ts
|
|
4209
|
+
const KEEP_JOBS = 20;
|
|
4210
|
+
/** One backup and one restore may run at a time; the second caller gets the running job. */
|
|
4211
|
+
var JobRegistry = class {
|
|
4212
|
+
jobs = /* @__PURE__ */ new Map();
|
|
4213
|
+
active = /* @__PURE__ */ new Map();
|
|
4214
|
+
start(kind, run) {
|
|
4215
|
+
const currentId = this.active.get(kind);
|
|
4216
|
+
if (currentId) {
|
|
4217
|
+
if (this.jobs.get(currentId)?.state === "running") return {
|
|
4218
|
+
id: currentId,
|
|
4219
|
+
running: true
|
|
4220
|
+
};
|
|
4221
|
+
}
|
|
4222
|
+
const id = randomUUID();
|
|
4223
|
+
const job = {
|
|
4224
|
+
id,
|
|
4225
|
+
state: "running",
|
|
4226
|
+
done: 0,
|
|
4227
|
+
total: 0
|
|
3849
4228
|
};
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
|
|
4229
|
+
this.jobs.set(id, job);
|
|
4230
|
+
this.active.set(kind, id);
|
|
4231
|
+
(async () => {
|
|
4232
|
+
try {
|
|
4233
|
+
const patch = await run((update) => Object.assign(job, update));
|
|
4234
|
+
if (patch) Object.assign(job, patch);
|
|
4235
|
+
job.state = "done";
|
|
4236
|
+
} catch (err) {
|
|
4237
|
+
job.state = "failed";
|
|
4238
|
+
job.error = String(err?.message ?? err);
|
|
4239
|
+
} finally {
|
|
4240
|
+
if (this.active.get(kind) === id) this.active.delete(kind);
|
|
4241
|
+
this.prune();
|
|
4242
|
+
}
|
|
4243
|
+
})();
|
|
3853
4244
|
return {
|
|
3854
|
-
|
|
3855
|
-
|
|
3856
|
-
warnings: plan.warnings
|
|
4245
|
+
id,
|
|
4246
|
+
running: false
|
|
3857
4247
|
};
|
|
3858
4248
|
}
|
|
3859
|
-
|
|
3860
|
-
|
|
3861
|
-
async function pruneBackups(dir, keep, written) {
|
|
3862
|
-
const owned = new Set(written);
|
|
3863
|
-
const found = [];
|
|
3864
|
-
for (const name of await fs$1.readdir(dir).catch(() => [])) {
|
|
3865
|
-
if (!owned.has(name) || !looksLikeBackupName(name)) continue;
|
|
3866
|
-
try {
|
|
3867
|
-
const stat = await fs$1.stat(path.join(dir, name));
|
|
3868
|
-
found.push({
|
|
3869
|
-
name,
|
|
3870
|
-
at: stat.mtimeMs
|
|
3871
|
-
});
|
|
3872
|
-
} catch {}
|
|
4249
|
+
get(id) {
|
|
4250
|
+
return this.jobs.get(id) ?? null;
|
|
3873
4251
|
}
|
|
3874
|
-
|
|
3875
|
-
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
if (
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3882
|
-
}
|
|
4252
|
+
activeId(kind) {
|
|
4253
|
+
return this.active.get(kind) ?? null;
|
|
4254
|
+
}
|
|
4255
|
+
prune() {
|
|
4256
|
+
if (this.jobs.size <= KEEP_JOBS) return;
|
|
4257
|
+
for (const [id, job] of this.jobs) {
|
|
4258
|
+
if (this.jobs.size <= KEEP_JOBS) break;
|
|
4259
|
+
if (job.state !== "running") this.jobs.delete(id);
|
|
4260
|
+
}
|
|
4261
|
+
}
|
|
4262
|
+
};
|
|
4263
|
+
//#endregion
|
|
4264
|
+
//#region src/lemonriver/backup/list.ts
|
|
4265
|
+
/** Every day this folder can restore from, newest first: snapshots and old zips. */
|
|
4266
|
+
async function listBackups(dir) {
|
|
4267
|
+
const out = [];
|
|
4268
|
+
for (const snapshot of await listSnapshots(dir)) out.push({
|
|
4269
|
+
name: snapshot.name,
|
|
4270
|
+
path: snapshot.path,
|
|
4271
|
+
size: snapshot.size,
|
|
4272
|
+
at: snapshot.at,
|
|
4273
|
+
kind: "snapshot",
|
|
4274
|
+
decks: snapshot.decks
|
|
4275
|
+
});
|
|
4276
|
+
for (const name of await fs$1.readdir(dir).catch(() => [])) {
|
|
4277
|
+
if (!looksLikeBackupName(name)) continue;
|
|
4278
|
+
const file = path.join(dir, name);
|
|
4279
|
+
const stat = await fs$1.stat(file).catch(() => null);
|
|
4280
|
+
if (!stat?.isFile()) continue;
|
|
4281
|
+
out.push({
|
|
4282
|
+
name,
|
|
4283
|
+
path: file,
|
|
4284
|
+
size: stat.size,
|
|
4285
|
+
at: stat.mtimeMs,
|
|
4286
|
+
kind: "zip",
|
|
4287
|
+
decks: null
|
|
4288
|
+
});
|
|
3883
4289
|
}
|
|
3884
|
-
return
|
|
4290
|
+
return out.sort((a, b) => b.at - a.at);
|
|
3885
4291
|
}
|
|
3886
4292
|
//#endregion
|
|
3887
4293
|
//#region src/app/lemonriver/backup/places.ts
|
|
@@ -4199,6 +4605,12 @@ function exec(file, args) {
|
|
|
4199
4605
|
});
|
|
4200
4606
|
});
|
|
4201
4607
|
}
|
|
4608
|
+
async function uniqueTarget(dir, stem) {
|
|
4609
|
+
const taken = new Set(await fs$1.readdir(dir).catch(() => []));
|
|
4610
|
+
let name = `${stem}.zip`;
|
|
4611
|
+
for (let n = 2; taken.has(name); n++) name = `${stem} (${n}).zip`;
|
|
4612
|
+
return path.join(dir, name);
|
|
4613
|
+
}
|
|
4202
4614
|
async function existsAsZip(file) {
|
|
4203
4615
|
if (!path.isAbsolute(file) || path.extname(file).toLowerCase() !== ".zip") return false;
|
|
4204
4616
|
return (await fs$1.stat(file).catch(() => null))?.isFile() === true;
|
|
@@ -4210,12 +4622,21 @@ function registerBackupRoutes(server, ctx) {
|
|
|
4210
4622
|
const localeFromPrefs = async (fallback) => {
|
|
4211
4623
|
return (await readPrefsSnapshot(ctx.userCwd))["open-slide:locale"] ?? fallback;
|
|
4212
4624
|
};
|
|
4625
|
+
const snapshotsRootFor = (settings) => settings.folder ? path.join(resolveBackupDir(settings.folder), "snapshots") : null;
|
|
4626
|
+
/** A snapshot may only be read out of this workspace's own backup folder. */
|
|
4627
|
+
const snapshotAt = async (settings, file) => {
|
|
4628
|
+
if (!path.isAbsolute(file) || path.extname(file).toLowerCase() !== ".json") return null;
|
|
4629
|
+
const root = snapshotsRootFor(settings);
|
|
4630
|
+
if (!root) return null;
|
|
4631
|
+
if (path.resolve(path.dirname(file)) !== path.resolve(root)) return null;
|
|
4632
|
+
return (await fs$1.stat(file).catch(() => null))?.isFile() ? file : null;
|
|
4633
|
+
};
|
|
4213
4634
|
const startBackupJob = (locale) => {
|
|
4214
4635
|
const { id, running } = jobs.start("backup", async (report) => {
|
|
4215
4636
|
const settings = await readBackupSettings(ctx.userCwd);
|
|
4216
4637
|
if (!settings.folder) throw new Error("no backup location is set");
|
|
4217
4638
|
const now = Date.now();
|
|
4218
|
-
const outcome = await
|
|
4639
|
+
const outcome = await createSnapshot(settings, {
|
|
4219
4640
|
userCwd: ctx.userCwd,
|
|
4220
4641
|
slidesRoot: ctx.slidesRoot,
|
|
4221
4642
|
globalAssetsRoot: ctx.globalAssetsRoot,
|
|
@@ -4236,35 +4657,36 @@ function registerBackupRoutes(server, ctx) {
|
|
|
4236
4657
|
at: now,
|
|
4237
4658
|
path: "",
|
|
4238
4659
|
size: 0,
|
|
4660
|
+
addedBytes: 0,
|
|
4239
4661
|
ok: false,
|
|
4240
4662
|
error: outcome.error,
|
|
4241
|
-
warnings: outcome.warnings
|
|
4663
|
+
warnings: outcome.warnings,
|
|
4664
|
+
skipped: []
|
|
4242
4665
|
}
|
|
4243
4666
|
});
|
|
4244
4667
|
report({ warnings: outcome.warnings });
|
|
4245
4668
|
throw new Error(outcome.error);
|
|
4246
4669
|
}
|
|
4247
|
-
|
|
4248
|
-
const written = [.../* @__PURE__ */ new Set([...settings.written, outcome.name])];
|
|
4249
|
-
const removed = await pruneBackups(dir, settings.keep, written);
|
|
4250
|
-
const kept = written.filter((name) => !removed.includes(name));
|
|
4670
|
+
await pruneSnapshots(resolveBackupDir(settings.folder), now);
|
|
4251
4671
|
await writeBackupSettings(ctx.userCwd, {
|
|
4252
4672
|
...settings,
|
|
4253
|
-
written: kept,
|
|
4254
4673
|
last: {
|
|
4255
4674
|
at: now,
|
|
4256
4675
|
path: outcome.path,
|
|
4257
4676
|
size: outcome.size,
|
|
4677
|
+
addedBytes: outcome.addedBytes,
|
|
4258
4678
|
ok: true,
|
|
4259
4679
|
error: null,
|
|
4260
|
-
warnings: outcome.warnings
|
|
4680
|
+
warnings: outcome.warnings,
|
|
4681
|
+
skipped: outcome.skipped
|
|
4261
4682
|
}
|
|
4262
4683
|
});
|
|
4263
4684
|
produced.add(outcome.path);
|
|
4264
4685
|
return {
|
|
4265
4686
|
path: outcome.path,
|
|
4266
4687
|
size: outcome.size,
|
|
4267
|
-
warnings: outcome.warnings
|
|
4688
|
+
warnings: outcome.warnings,
|
|
4689
|
+
skipped: outcome.skipped
|
|
4268
4690
|
};
|
|
4269
4691
|
});
|
|
4270
4692
|
return {
|
|
@@ -4274,27 +4696,47 @@ function registerBackupRoutes(server, ctx) {
|
|
|
4274
4696
|
};
|
|
4275
4697
|
const startRestoreJob = (file, decks, prefs) => {
|
|
4276
4698
|
const { id, running } = jobs.start("restore", async (report) => {
|
|
4277
|
-
const
|
|
4278
|
-
|
|
4279
|
-
|
|
4280
|
-
|
|
4281
|
-
|
|
4282
|
-
|
|
4283
|
-
|
|
4284
|
-
|
|
4285
|
-
|
|
4286
|
-
|
|
4287
|
-
|
|
4288
|
-
|
|
4289
|
-
|
|
4290
|
-
|
|
4291
|
-
|
|
4292
|
-
|
|
4293
|
-
|
|
4294
|
-
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
|
|
4699
|
+
const settings = await readBackupSettings(ctx.userCwd);
|
|
4700
|
+
const asSnapshot = await snapshotAt(settings, file);
|
|
4701
|
+
let toRestore = file;
|
|
4702
|
+
let temp = null;
|
|
4703
|
+
try {
|
|
4704
|
+
if (asSnapshot) {
|
|
4705
|
+
const snapshot = await readSnapshot(asSnapshot);
|
|
4706
|
+
if (!snapshot) throw new Error("this backup snapshot cannot be read");
|
|
4707
|
+
temp = path.join(ctx.userCwd, "node_modules", ".open-slide", "backup-staging", `restoring-${randomUUID()}.zip`);
|
|
4708
|
+
await fs$1.mkdir(path.dirname(temp), { recursive: true });
|
|
4709
|
+
await writeSnapshotZip(resolveBackupDir(settings.folder), snapshot, temp, {
|
|
4710
|
+
locale: await localeFromPrefs("en"),
|
|
4711
|
+
coreVersion: ctx.coreVersion,
|
|
4712
|
+
limits: BACKUP_LIMITS
|
|
4713
|
+
});
|
|
4714
|
+
toRestore = temp;
|
|
4715
|
+
}
|
|
4716
|
+
const outcome = await restoreBackupFile(toRestore, {
|
|
4717
|
+
decks,
|
|
4718
|
+
prefs
|
|
4719
|
+
}, {
|
|
4720
|
+
userCwd: ctx.userCwd,
|
|
4721
|
+
slidesRoot: ctx.slidesRoot,
|
|
4722
|
+
manifestPath: ctx.manifestPath,
|
|
4723
|
+
limits: BACKUP_LIMITS,
|
|
4724
|
+
onProgress: (done, total) => report({
|
|
4725
|
+
done,
|
|
4726
|
+
total
|
|
4727
|
+
})
|
|
4728
|
+
});
|
|
4729
|
+
if (!outcome.ok) throw new Error(outcome.error);
|
|
4730
|
+
refreshDecks(server, ctx.slidesRoot, outcome.restored);
|
|
4731
|
+
return {
|
|
4732
|
+
restored: outcome.restored,
|
|
4733
|
+
assetsKept: outcome.assetsKept,
|
|
4734
|
+
themesKept: outcome.themesKept,
|
|
4735
|
+
prefs: outcome.prefs
|
|
4736
|
+
};
|
|
4737
|
+
} finally {
|
|
4738
|
+
if (temp) await fs$1.rm(temp, { force: true }).catch(() => {});
|
|
4739
|
+
}
|
|
4298
4740
|
});
|
|
4299
4741
|
return {
|
|
4300
4742
|
jobId: id,
|
|
@@ -4320,6 +4762,17 @@ function registerBackupRoutes(server, ctx) {
|
|
|
4320
4762
|
dir
|
|
4321
4763
|
});
|
|
4322
4764
|
}
|
|
4765
|
+
if (method === "GET" && url.pathname === "/summary") {
|
|
4766
|
+
const settings = await readBackupSettings(ctx.userCwd);
|
|
4767
|
+
if (!settings.folder) return json(res, 200, { summary: {
|
|
4768
|
+
totalBytes: 0,
|
|
4769
|
+
last: settings.last,
|
|
4770
|
+
biggest: [],
|
|
4771
|
+
skipLargeVideos: settings.skipLargeVideos
|
|
4772
|
+
} });
|
|
4773
|
+
const dir = resolveBackupDir(settings.folder);
|
|
4774
|
+
return json(res, 200, { summary: await summarize(dir, settings) });
|
|
4775
|
+
}
|
|
4323
4776
|
if (method === "POST" && url.pathname === "/choose-folder") {
|
|
4324
4777
|
const check = validateMutationRequest(req, { requireJsonBody: true });
|
|
4325
4778
|
if (!check.ok) return json(res, check.status, { error: check.error });
|
|
@@ -4370,7 +4823,7 @@ function registerBackupRoutes(server, ctx) {
|
|
|
4370
4823
|
});
|
|
4371
4824
|
}
|
|
4372
4825
|
if (typeof body.auto === "boolean") next.auto = body.auto;
|
|
4373
|
-
if (typeof body.
|
|
4826
|
+
if (typeof body.skipLargeVideos === "boolean") next.skipLargeVideos = body.skipLargeVideos;
|
|
4374
4827
|
await writeBackupSettings(ctx.userCwd, next);
|
|
4375
4828
|
return json(res, 200, {
|
|
4376
4829
|
ok: true,
|
|
@@ -4417,19 +4870,24 @@ function registerBackupRoutes(server, ctx) {
|
|
|
4417
4870
|
const settings = await readBackupSettings(ctx.userCwd);
|
|
4418
4871
|
if (!settings.folder) return json(res, 200, { backups: [] });
|
|
4419
4872
|
const dir = resolveBackupDir(settings.folder);
|
|
4420
|
-
return json(res, 200, { backups: await listBackups(dir
|
|
4873
|
+
return json(res, 200, { backups: await listBackups(dir) });
|
|
4421
4874
|
}
|
|
4422
4875
|
if (method === "POST" && url.pathname === "/inspect") {
|
|
4423
4876
|
const check = validateMutationRequest(req, { requireJsonBody: true });
|
|
4424
4877
|
if (!check.ok) return json(res, check.status, { error: check.error });
|
|
4425
4878
|
const body = await readBody(req, BODY_LIMIT$1);
|
|
4426
4879
|
const file = typeof body?.path === "string" ? body.path : "";
|
|
4427
|
-
|
|
4428
|
-
const
|
|
4880
|
+
const settings = await readBackupSettings(ctx.userCwd);
|
|
4881
|
+
const snapshot = await snapshotAt(settings, file);
|
|
4882
|
+
const inspected = snapshot ? await inspectSnapshotFile(snapshot, {
|
|
4883
|
+
userCwd: ctx.userCwd,
|
|
4884
|
+
slidesRoot: ctx.slidesRoot
|
|
4885
|
+
}) : await existsAsZip(file) ? await inspectBackupFile(file, {
|
|
4429
4886
|
userCwd: ctx.userCwd,
|
|
4430
4887
|
slidesRoot: ctx.slidesRoot,
|
|
4431
4888
|
limits: BACKUP_LIMITS
|
|
4432
|
-
});
|
|
4889
|
+
}) : null;
|
|
4890
|
+
if (!inspected) return json(res, 400, { error: "pick a backup from this folder" });
|
|
4433
4891
|
if (!inspected.ok) return json(res, 400, {
|
|
4434
4892
|
ok: false,
|
|
4435
4893
|
code: inspected.code,
|
|
@@ -4447,10 +4905,49 @@ function registerBackupRoutes(server, ctx) {
|
|
|
4447
4905
|
if (!check.ok) return json(res, check.status, { error: check.error });
|
|
4448
4906
|
const body = await readBody(req, BODY_LIMIT$1);
|
|
4449
4907
|
const file = typeof body?.path === "string" ? body.path : "";
|
|
4450
|
-
|
|
4908
|
+
const settings = await readBackupSettings(ctx.userCwd);
|
|
4909
|
+
const known = await snapshotAt(settings, file) ?? (await existsAsZip(file) ? file : null);
|
|
4910
|
+
if (!known) return json(res, 400, { error: "pick a backup from this folder" });
|
|
4451
4911
|
const decks = Array.isArray(body?.decks) ? body.decks.filter((id) => typeof id === "string") : [];
|
|
4452
4912
|
if (decks.length === 0) return json(res, 400, { error: "no decks were chosen" });
|
|
4453
|
-
return json(res, 200, startRestoreJob(
|
|
4913
|
+
return json(res, 200, startRestoreJob(known, decks, body?.prefs === true));
|
|
4914
|
+
}
|
|
4915
|
+
if (method === "POST" && url.pathname === "/export") {
|
|
4916
|
+
const check = validateMutationRequest(req, { requireJsonBody: true });
|
|
4917
|
+
if (!check.ok) return json(res, check.status, { error: check.error });
|
|
4918
|
+
if (!isLoopbackRequest(req)) return json(res, 403, { error: "local requests only" });
|
|
4919
|
+
const body = await readBody(req, BODY_LIMIT$1);
|
|
4920
|
+
const settings = await readBackupSettings(ctx.userCwd);
|
|
4921
|
+
if (!settings.folder) return json(res, 400, { error: "no backup location is set" });
|
|
4922
|
+
const file = typeof body?.snapshot === "string" ? body.snapshot : "";
|
|
4923
|
+
const snapshotPath = await snapshotAt(settings, file);
|
|
4924
|
+
if (!snapshotPath) return json(res, 400, { error: "pick a backup from this folder" });
|
|
4925
|
+
const snapshot = await readSnapshot(snapshotPath);
|
|
4926
|
+
if (!snapshot) return json(res, 400, { error: "this backup snapshot cannot be read" });
|
|
4927
|
+
const locale = guideLocaleFor(typeof body?.locale === "string" ? body.locale : "en");
|
|
4928
|
+
const picked = await chooseFolder(locale, {
|
|
4929
|
+
platform: process.platform,
|
|
4930
|
+
exec
|
|
4931
|
+
});
|
|
4932
|
+
if ("unsupported" in picked) return json(res, 501, picked);
|
|
4933
|
+
if ("cancelled" in picked) return json(res, 200, { cancelled: true });
|
|
4934
|
+
const target = await uniqueTarget(picked.path, backupFileStem(snapshot.createdAt));
|
|
4935
|
+
const dir = resolveBackupDir(settings.folder);
|
|
4936
|
+
try {
|
|
4937
|
+
const size = await writeSnapshotZip(dir, snapshot, target, {
|
|
4938
|
+
locale,
|
|
4939
|
+
coreVersion: ctx.coreVersion,
|
|
4940
|
+
limits: BACKUP_LIMITS
|
|
4941
|
+
});
|
|
4942
|
+
produced.add(target);
|
|
4943
|
+
return json(res, 200, {
|
|
4944
|
+
ok: true,
|
|
4945
|
+
path: target,
|
|
4946
|
+
size
|
|
4947
|
+
});
|
|
4948
|
+
} catch (err) {
|
|
4949
|
+
return json(res, 500, { error: String(err?.message ?? err) });
|
|
4950
|
+
}
|
|
4454
4951
|
}
|
|
4455
4952
|
next();
|
|
4456
4953
|
} catch (err) {
|
|
@@ -8156,6 +8653,29 @@ function registerTemplateRoutes(server, ctx) {
|
|
|
8156
8653
|
}));
|
|
8157
8654
|
}
|
|
8158
8655
|
//#endregion
|
|
8656
|
+
//#region src/lemonriver/workspace/routes.ts
|
|
8657
|
+
function revealFolder(target) {
|
|
8658
|
+
const command = process.platform === "darwin" ? "open" : "xdg-open";
|
|
8659
|
+
execFile(command, [target], () => {});
|
|
8660
|
+
}
|
|
8661
|
+
function registerWorkspaceRoutes(server, ctx) {
|
|
8662
|
+
server.middlewares.use("/__lr/workspace", (req, res, next) => {
|
|
8663
|
+
if (!isLoopbackRequest(req)) return json(res, 403, { error: "local requests only" });
|
|
8664
|
+
const pathname = (req.url ?? "/").split("?")[0];
|
|
8665
|
+
const method = req.method ?? "GET";
|
|
8666
|
+
if (pathname === "/" || pathname === "") {
|
|
8667
|
+
if (method !== "GET") return next();
|
|
8668
|
+
return json(res, 200, { path: ctx.userCwd });
|
|
8669
|
+
}
|
|
8670
|
+
if (pathname === "/reveal") {
|
|
8671
|
+
if (method !== "POST") return next();
|
|
8672
|
+
revealFolder(ctx.userCwd);
|
|
8673
|
+
return json(res, 200, { ok: true });
|
|
8674
|
+
}
|
|
8675
|
+
return next();
|
|
8676
|
+
});
|
|
8677
|
+
}
|
|
8678
|
+
//#endregion
|
|
8159
8679
|
//#region src/lemonriver/routes.ts
|
|
8160
8680
|
function registerLemonRiverRoutes(server, ctx) {
|
|
8161
8681
|
registerBoardRoutes(server, ctx);
|
|
@@ -8168,6 +8688,7 @@ function registerLemonRiverRoutes(server, ctx) {
|
|
|
8168
8688
|
registerGuideRoutes(server, ctx);
|
|
8169
8689
|
registerAgentProviderRoutes(server, ctx);
|
|
8170
8690
|
registerMcpRoutes(server, ctx);
|
|
8691
|
+
registerWorkspaceRoutes(server, ctx);
|
|
8171
8692
|
}
|
|
8172
8693
|
//#endregion
|
|
8173
8694
|
//#region src/app/lemonriver/outline/rule.ts
|