nerdgram1337 1.4.6 → 1.4.8
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/chunk-54BVSMKR.js +87 -0
- package/dist/chunk-5C6VO4LC.js +90 -0
- package/dist/{chunk-KO565WWM.js → chunk-AFGGIQ22.js} +252 -80
- package/dist/{chunk-BOMYBBJ6.js → chunk-GQKUFF4P.js} +8 -102
- package/dist/chunk-I5EOFHBL.js +1494 -0
- package/dist/chunk-JUCAQ6EJ.js +1265 -0
- package/dist/chunk-TQHP2K7Q.js +492 -0
- package/dist/chunk-ULY5YEBR.js +490 -0
- package/dist/chunk-WSGEBKT4.js +943 -0
- package/dist/{chunk-EGA2UGTF.js → chunk-XJTF2GUE.js} +102 -104
- package/dist/cli.js +46 -3
- package/package.json +1 -1
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
// server/src/paths.ts
|
|
2
|
+
import fs2 from "node:fs";
|
|
3
|
+
import path2 from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
// server/src/instanceConfig.ts
|
|
7
|
+
import { randomUUID } from "node:crypto";
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import os from "node:os";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
12
|
+
var processStartDataDir = process.env.NERDGRAM_DATA_DIR?.trim() || null;
|
|
13
|
+
var externalForTests;
|
|
14
|
+
function instanceFilePath() {
|
|
15
|
+
const override = process.env.NERDGRAM_INSTANCE_FILE?.trim();
|
|
16
|
+
if (override) return path.resolve(override);
|
|
17
|
+
return path.join(os.homedir(), ".nerdgram", "instance.json");
|
|
18
|
+
}
|
|
19
|
+
function externalDataDir() {
|
|
20
|
+
if (externalForTests !== void 0) return externalForTests;
|
|
21
|
+
return processStartDataDir;
|
|
22
|
+
}
|
|
23
|
+
function readInstanceFileSync() {
|
|
24
|
+
try {
|
|
25
|
+
const raw = fs.readFileSync(instanceFilePath(), "utf8");
|
|
26
|
+
const parsed = JSON.parse(raw);
|
|
27
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null;
|
|
28
|
+
return parsed;
|
|
29
|
+
} catch (err) {
|
|
30
|
+
const code = err.code;
|
|
31
|
+
if (code === "ENOENT" || err instanceof SyntaxError) return null;
|
|
32
|
+
throw err;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function readStartupDataDir() {
|
|
36
|
+
const value = readInstanceFileSync()?.NERDGRAM_DATA_DIR;
|
|
37
|
+
if (typeof value !== "string") return null;
|
|
38
|
+
const trimmed = value.trim();
|
|
39
|
+
return trimmed || null;
|
|
40
|
+
}
|
|
41
|
+
var startupApplied = false;
|
|
42
|
+
function applyStartupDataDir() {
|
|
43
|
+
if (startupApplied) return;
|
|
44
|
+
startupApplied = true;
|
|
45
|
+
if (externalDataDir()) return;
|
|
46
|
+
if (process.env.NERDGRAM_DATA_DIR?.trim()) return;
|
|
47
|
+
const saved = readStartupDataDir();
|
|
48
|
+
if (saved) process.env.NERDGRAM_DATA_DIR = saved;
|
|
49
|
+
}
|
|
50
|
+
var identityCache = null;
|
|
51
|
+
var identityCachePath = "";
|
|
52
|
+
function machineHostname() {
|
|
53
|
+
const name = os.hostname().trim();
|
|
54
|
+
return name || "localhost";
|
|
55
|
+
}
|
|
56
|
+
function displayName(configured, hostname) {
|
|
57
|
+
const trimmed = configured?.trim() ?? "";
|
|
58
|
+
return trimmed || hostname;
|
|
59
|
+
}
|
|
60
|
+
async function writeInstanceFile(file) {
|
|
61
|
+
const target = instanceFilePath();
|
|
62
|
+
await fs.promises.mkdir(path.dirname(target), { recursive: true });
|
|
63
|
+
const tmp = `${target}.${process.pid}.tmp`;
|
|
64
|
+
await fs.promises.writeFile(tmp, `${JSON.stringify(file, null, 2)}
|
|
65
|
+
`, { mode: 384 });
|
|
66
|
+
await fs.promises.rename(tmp, target);
|
|
67
|
+
}
|
|
68
|
+
async function getInstanceIdentity() {
|
|
69
|
+
const filePath = instanceFilePath();
|
|
70
|
+
if (identityCache && identityCachePath === filePath) return identityCache;
|
|
71
|
+
const hostname = machineHostname();
|
|
72
|
+
const existing = readInstanceFileSync();
|
|
73
|
+
let instanceId = existing?.instanceId?.trim() ?? "";
|
|
74
|
+
if (!UUID_RE.test(instanceId)) instanceId = randomUUID();
|
|
75
|
+
const configured = typeof existing?.instanceName === "string" ? existing.instanceName.trim() : "";
|
|
76
|
+
const next = {
|
|
77
|
+
...existing ?? { instanceId },
|
|
78
|
+
instanceId,
|
|
79
|
+
hostname
|
|
80
|
+
};
|
|
81
|
+
if (configured) next.instanceName = configured;
|
|
82
|
+
else delete next.instanceName;
|
|
83
|
+
await writeInstanceFile(next);
|
|
84
|
+
identityCache = {
|
|
85
|
+
instanceId,
|
|
86
|
+
instanceName: displayName(configured, hostname),
|
|
87
|
+
hostname,
|
|
88
|
+
configuredName: configured || null
|
|
89
|
+
};
|
|
90
|
+
identityCachePath = filePath;
|
|
91
|
+
return identityCache;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// server/src/paths.ts
|
|
95
|
+
var __dirname = path2.dirname(fileURLToPath(import.meta.url));
|
|
96
|
+
var REPO_ROOT = path2.resolve(__dirname, "../..");
|
|
97
|
+
function defaultDataRoot() {
|
|
98
|
+
return path2.join(REPO_ROOT, "diagrams");
|
|
99
|
+
}
|
|
100
|
+
function getDataRoot() {
|
|
101
|
+
applyStartupDataDir();
|
|
102
|
+
const fromEnv = process.env.NERDGRAM_DATA_DIR;
|
|
103
|
+
if (fromEnv && fromEnv.trim()) {
|
|
104
|
+
return path2.resolve(fromEnv);
|
|
105
|
+
}
|
|
106
|
+
return defaultDataRoot();
|
|
107
|
+
}
|
|
108
|
+
function ensureDataRoot() {
|
|
109
|
+
const root = getDataRoot();
|
|
110
|
+
fs2.mkdirSync(root, { recursive: true });
|
|
111
|
+
return root;
|
|
112
|
+
}
|
|
113
|
+
var INVALID_PATH_CHARS = /[<>:"|?*\u0000-\u001f]/;
|
|
114
|
+
var INVALID_PATH_MESSAGE = 'Invalid path. A name cannot be only spaces, and it cannot include a drive letter or : \\ * ? " < > |.';
|
|
115
|
+
function resolveSafePath(relativePath) {
|
|
116
|
+
const root = ensureDataRoot();
|
|
117
|
+
const cleaned = (relativePath || ".").replace(/\\/g, "/").replace(/^\/+/, "");
|
|
118
|
+
if (cleaned.includes("\0")) {
|
|
119
|
+
throw new Error("Invalid path");
|
|
120
|
+
}
|
|
121
|
+
const hidden = cleaned.split("/").filter((part) => part.length > 0 && part !== "." && part !== "..");
|
|
122
|
+
for (const part of hidden) {
|
|
123
|
+
if (part.startsWith(".")) throw new Error("Invalid path");
|
|
124
|
+
if (INVALID_PATH_CHARS.test(part) || !part.trim()) throw new Error(INVALID_PATH_MESSAGE);
|
|
125
|
+
const stem = part.toLowerCase().endsWith(".mmd") ? part.slice(0, -4) : part;
|
|
126
|
+
if (!stem.trim()) throw new Error(INVALID_PATH_MESSAGE);
|
|
127
|
+
}
|
|
128
|
+
if (hidden.some((part) => Buffer.byteLength(part) > 255)) {
|
|
129
|
+
throw Object.assign(
|
|
130
|
+
new Error("That name is too long. Each folder or file name has to be 255 bytes or fewer."),
|
|
131
|
+
{ status: 400, code: "invalid" }
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
const absolute = path2.resolve(root, cleaned === "" ? "." : cleaned);
|
|
135
|
+
const relative = path2.relative(root, absolute);
|
|
136
|
+
if (relative.startsWith("..") || path2.isAbsolute(relative)) {
|
|
137
|
+
throw new Error("Path escapes data directory");
|
|
138
|
+
}
|
|
139
|
+
return absolute;
|
|
140
|
+
}
|
|
141
|
+
function toRelativePath(absolutePath) {
|
|
142
|
+
const root = ensureDataRoot();
|
|
143
|
+
const relative = path2.relative(root, absolutePath);
|
|
144
|
+
return relative.split(path2.sep).join("/");
|
|
145
|
+
}
|
|
146
|
+
var META_DIR_NAME = ".meta";
|
|
147
|
+
function getMetaPath() {
|
|
148
|
+
return path2.join(ensureDataRoot(), META_DIR_NAME);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// server/src/storage.ts
|
|
152
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
153
|
+
import fs3 from "node:fs/promises";
|
|
154
|
+
import path3 from "node:path";
|
|
155
|
+
|
|
156
|
+
// server/src/dataFiles.ts
|
|
157
|
+
var DATA_FILE_EXTENSIONS = [".network.json", ".csv", ".json", ".cdp", ".lldp"];
|
|
158
|
+
function dataFileExtension(name) {
|
|
159
|
+
const lower = name.toLowerCase();
|
|
160
|
+
return DATA_FILE_EXTENSIONS.find((ext) => lower.endsWith(ext)) ?? "";
|
|
161
|
+
}
|
|
162
|
+
function isDataFileName(name) {
|
|
163
|
+
return dataFileExtension(name) !== "";
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// server/src/storage.ts
|
|
167
|
+
var REVISION_RETENTION = 20;
|
|
168
|
+
var LOCK_STALE_MS = 9e4;
|
|
169
|
+
var MTIME_MATCH_TOLERANCE_MS = 1;
|
|
170
|
+
var VERSIONS_DIR_NAME = "versions";
|
|
171
|
+
var LOCKS_DIR_NAME = "locks";
|
|
172
|
+
function getVersionsRoot() {
|
|
173
|
+
return path3.join(getMetaPath(), VERSIONS_DIR_NAME);
|
|
174
|
+
}
|
|
175
|
+
function getLocksRoot() {
|
|
176
|
+
return path3.join(getMetaPath(), LOCKS_DIR_NAME);
|
|
177
|
+
}
|
|
178
|
+
function normalizeRelative(input) {
|
|
179
|
+
return input.replace(/\\/g, "/").replace(/^\/+|\/+$/g, "");
|
|
180
|
+
}
|
|
181
|
+
function hashContent(content) {
|
|
182
|
+
return createHash("sha256").update(content, "utf8").digest("hex");
|
|
183
|
+
}
|
|
184
|
+
function createId() {
|
|
185
|
+
return `${Date.now().toString(36)}-${randomBytes(6).toString("hex")}`;
|
|
186
|
+
}
|
|
187
|
+
function encodeStorageKey(relativePath) {
|
|
188
|
+
const normalized = normalizeRelative(relativePath);
|
|
189
|
+
if (!normalized || normalized.includes("\0")) {
|
|
190
|
+
throw httpFail(400, "Invalid path", "invalid");
|
|
191
|
+
}
|
|
192
|
+
return `p_${encodeURIComponent(normalized)}`;
|
|
193
|
+
}
|
|
194
|
+
function safeStorageKey(relativePath, suffix = "") {
|
|
195
|
+
const encoded = encodeStorageKey(relativePath);
|
|
196
|
+
if (Buffer.byteLength(`${encoded}${suffix}`) <= 255) return encoded;
|
|
197
|
+
const digest = createHash("sha256").update(normalizeRelative(relativePath), "utf8").digest("hex");
|
|
198
|
+
return `h_${digest}`;
|
|
199
|
+
}
|
|
200
|
+
function mtimeMatches(actual, expected) {
|
|
201
|
+
return Math.abs(actual - expected) <= MTIME_MATCH_TOLERANCE_MS;
|
|
202
|
+
}
|
|
203
|
+
function httpFail(status, message, code, details) {
|
|
204
|
+
return Object.assign(new Error(message), { status, code, details });
|
|
205
|
+
}
|
|
206
|
+
function evaluateWrite(disk, intent) {
|
|
207
|
+
const hasBaseline = typeof intent.expectedMtimeMs === "number" || typeof intent.expectedHash === "string" && intent.expectedHash.length > 0;
|
|
208
|
+
if (!disk.exists) {
|
|
209
|
+
if (hasBaseline && !intent.force) return { action: "reject", code: "missing" };
|
|
210
|
+
return { action: "create" };
|
|
211
|
+
}
|
|
212
|
+
if (hashContent(intent.content) === disk.hash) return { action: "unchanged" };
|
|
213
|
+
if (intent.force) return { action: "overwrite" };
|
|
214
|
+
if (!hasBaseline) return { action: "reject", code: "exists" };
|
|
215
|
+
if (typeof intent.expectedHash === "string" && intent.expectedHash.length > 0) {
|
|
216
|
+
if (intent.expectedHash !== disk.hash) return { action: "reject", code: "conflict" };
|
|
217
|
+
return { action: "overwrite" };
|
|
218
|
+
}
|
|
219
|
+
if (typeof intent.expectedMtimeMs === "number" && !mtimeMatches(disk.mtimeMs, intent.expectedMtimeMs)) {
|
|
220
|
+
return { action: "reject", code: "conflict" };
|
|
221
|
+
}
|
|
222
|
+
return { action: "overwrite" };
|
|
223
|
+
}
|
|
224
|
+
function insertSuffix(relativePath, suffix) {
|
|
225
|
+
const normalized = normalizeRelative(relativePath);
|
|
226
|
+
const slash = normalized.lastIndexOf("/");
|
|
227
|
+
const dir = slash >= 0 ? normalized.slice(0, slash) : "";
|
|
228
|
+
const base = slash >= 0 ? normalized.slice(slash + 1) : normalized;
|
|
229
|
+
const lower = base.toLowerCase();
|
|
230
|
+
const dataExt = dataFileExtension(base);
|
|
231
|
+
const renamed = lower.endsWith(".mmd") ? `${base.slice(0, -".mmd".length)}${suffix}.mmd` : dataExt ? `${base.slice(0, -dataExt.length)}${suffix}${base.slice(-dataExt.length)}` : `${base}${suffix}`;
|
|
232
|
+
return dir ? `${dir}/${renamed}` : renamed;
|
|
233
|
+
}
|
|
234
|
+
async function resolveAvailablePath(desired, isTaken, options) {
|
|
235
|
+
if (!await isTaken(desired)) return desired;
|
|
236
|
+
const word = options?.word;
|
|
237
|
+
const start = word ? 1 : 2;
|
|
238
|
+
for (let n = start; n < 1e3; n += 1) {
|
|
239
|
+
const suffix = word ? n === 1 ? ` (${word})` : ` (${word} ${n})` : ` ${n}`;
|
|
240
|
+
const candidate = insertSuffix(desired, suffix);
|
|
241
|
+
if (!await isTaken(candidate)) return candidate;
|
|
242
|
+
}
|
|
243
|
+
throw httpFail(409, "No free path is available", "collision", { path: desired });
|
|
244
|
+
}
|
|
245
|
+
function rewriteMovedPath(itemPath, fromPath, toPath, isDir) {
|
|
246
|
+
const item = normalizeRelative(itemPath);
|
|
247
|
+
const from = normalizeRelative(fromPath);
|
|
248
|
+
const to = normalizeRelative(toPath);
|
|
249
|
+
if (item === from) return to;
|
|
250
|
+
if (isDir && item.startsWith(`${from}/`)) return `${to}${item.slice(from.length)}`;
|
|
251
|
+
return item;
|
|
252
|
+
}
|
|
253
|
+
async function replaceFile(tmp, dest) {
|
|
254
|
+
try {
|
|
255
|
+
await fs3.rename(tmp, dest);
|
|
256
|
+
} catch (err) {
|
|
257
|
+
const code = err.code;
|
|
258
|
+
if (code !== "EEXIST" && code !== "EPERM") throw err;
|
|
259
|
+
await fs3.rm(dest, { force: true });
|
|
260
|
+
await fs3.rename(tmp, dest);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
async function atomicWriteText(absolutePath, content) {
|
|
264
|
+
const dir = path3.dirname(absolutePath);
|
|
265
|
+
await fs3.mkdir(dir, { recursive: true });
|
|
266
|
+
const tmp = path3.join(dir, `.ng-${process.pid}-${randomBytes(6).toString("hex")}.tmp`);
|
|
267
|
+
try {
|
|
268
|
+
await fs3.writeFile(tmp, content, "utf8");
|
|
269
|
+
await replaceFile(tmp, absolutePath);
|
|
270
|
+
} catch (err) {
|
|
271
|
+
await fs3.rm(tmp, { force: true }).catch(() => void 0);
|
|
272
|
+
throw err;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
async function atomicWriteJson(absolutePath, value) {
|
|
276
|
+
await atomicWriteText(absolutePath, `${JSON.stringify(value, null, 2)}
|
|
277
|
+
`);
|
|
278
|
+
}
|
|
279
|
+
async function readJsonFile(absolutePath) {
|
|
280
|
+
try {
|
|
281
|
+
const raw = await fs3.readFile(absolutePath, "utf8");
|
|
282
|
+
return JSON.parse(raw);
|
|
283
|
+
} catch (err) {
|
|
284
|
+
const code = err.code;
|
|
285
|
+
if (code === "ENOENT") return null;
|
|
286
|
+
throw err;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
async function pathExists(absolutePath) {
|
|
290
|
+
try {
|
|
291
|
+
await fs3.stat(absolutePath);
|
|
292
|
+
return true;
|
|
293
|
+
} catch (err) {
|
|
294
|
+
const code = err.code;
|
|
295
|
+
if (code === "ENOENT") return false;
|
|
296
|
+
throw err;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
function isProcessAlive(pid) {
|
|
300
|
+
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
301
|
+
if (pid === process.pid) return true;
|
|
302
|
+
try {
|
|
303
|
+
process.kill(pid, 0);
|
|
304
|
+
return true;
|
|
305
|
+
} catch (err) {
|
|
306
|
+
const code = err.code;
|
|
307
|
+
return code === "EPERM";
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// server/src/thumbnails.ts
|
|
312
|
+
import * as cheerio from "cheerio";
|
|
313
|
+
import fs5 from "node:fs/promises";
|
|
314
|
+
import path4 from "node:path";
|
|
315
|
+
|
|
316
|
+
// server/src/readError.ts
|
|
317
|
+
var READ_REASONS = {
|
|
318
|
+
EIO: "The file may still be syncing or the folder may be unavailable on this computer",
|
|
319
|
+
EACCES: "This computer does not have permission to read the file",
|
|
320
|
+
EPERM: "This computer does not have permission to read the file",
|
|
321
|
+
ENOENT: "The file was removed or renamed before it could be read",
|
|
322
|
+
EBUSY: "The file is busy and could not be read",
|
|
323
|
+
ETIMEDOUT: "Reading the file timed out. The folder may be slow or unavailable"
|
|
324
|
+
};
|
|
325
|
+
var KNOWN_READ_ERRNOS = new Set(Object.keys(READ_REASONS));
|
|
326
|
+
function isKnownReadErrno(code) {
|
|
327
|
+
return KNOWN_READ_ERRNOS.has(code);
|
|
328
|
+
}
|
|
329
|
+
function fileLabel(filePath) {
|
|
330
|
+
const absolute = filePath.startsWith("/") || /^[A-Za-z]:[\\/]/.test(filePath);
|
|
331
|
+
if (!absolute) return filePath || "diagram";
|
|
332
|
+
const slash = Math.max(filePath.lastIndexOf("/"), filePath.lastIndexOf("\\"));
|
|
333
|
+
const name = slash >= 0 ? filePath.slice(slash + 1) : filePath;
|
|
334
|
+
return name || filePath;
|
|
335
|
+
}
|
|
336
|
+
function unreadableMessage(filePath, code) {
|
|
337
|
+
const reason = READ_REASONS[code] ?? "The file could not be read";
|
|
338
|
+
const suffix = code ? ` (${code})` : "";
|
|
339
|
+
return `Couldn't read \`${fileLabel(filePath)}\`. ${reason}${suffix}.`;
|
|
340
|
+
}
|
|
341
|
+
function detailsErrno(err) {
|
|
342
|
+
if (!err || typeof err !== "object" || !("details" in err)) return "";
|
|
343
|
+
const details = err.details;
|
|
344
|
+
if (!details || typeof details !== "object" || !("errno" in details)) return "";
|
|
345
|
+
const errno = details.errno;
|
|
346
|
+
return typeof errno === "string" ? errno : "";
|
|
347
|
+
}
|
|
348
|
+
function errnoInText(err) {
|
|
349
|
+
const message = err instanceof Error ? err.message : "";
|
|
350
|
+
const match = message.match(/\b(EIO|EACCES|EPERM|ENOENT|EBUSY|ETIMEDOUT)\b/);
|
|
351
|
+
return match?.[1] ?? "";
|
|
352
|
+
}
|
|
353
|
+
function errnoFrom(err) {
|
|
354
|
+
const fromDetails = detailsErrno(err);
|
|
355
|
+
if (/^E[A-Z0-9]+$/.test(fromDetails)) return fromDetails;
|
|
356
|
+
if (err && typeof err === "object" && "code" in err) {
|
|
357
|
+
const code = String(err.code);
|
|
358
|
+
if (/^E[A-Z0-9]+$/.test(code)) return code;
|
|
359
|
+
}
|
|
360
|
+
return errnoInText(err);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// server/src/readFailure.ts
|
|
364
|
+
function rethrowKnownReadError(filePath, err) {
|
|
365
|
+
if (err && typeof err === "object" && "status" in err) return;
|
|
366
|
+
const code = errnoFrom(err);
|
|
367
|
+
if (!isKnownReadErrno(code)) return;
|
|
368
|
+
throw httpFail(500, unreadableMessage(filePath, code), "unreadable", { errno: code });
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// server/src/svgImage.ts
|
|
372
|
+
function svgForImage(svg) {
|
|
373
|
+
if (!/xlink:/i.test(svg) || /xmlns:xlink\s*=/i.test(svg)) return svg;
|
|
374
|
+
return svg.replace(/<svg\b/i, '<svg xmlns:xlink="http://www.w3.org/1999/xlink"');
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// server/src/textFile.ts
|
|
378
|
+
import fs4 from "node:fs/promises";
|
|
379
|
+
var textFiles = {
|
|
380
|
+
read(abs) {
|
|
381
|
+
return fs4.readFile(abs, "utf8");
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
// server/src/thumbnails.ts
|
|
386
|
+
var THUMBS_DIR_NAME = "thumbs";
|
|
387
|
+
function thumbsRoot() {
|
|
388
|
+
return path4.join(getMetaPath(), THUMBS_DIR_NAME);
|
|
389
|
+
}
|
|
390
|
+
function thumbFile(relativePath) {
|
|
391
|
+
return path4.join(thumbsRoot(), `${safeStorageKey(relativePath, ".json")}.json`);
|
|
392
|
+
}
|
|
393
|
+
var URL_ATTRS = /* @__PURE__ */ new Set(["href", "xlink:href", "src"]);
|
|
394
|
+
function decodeUrl(value) {
|
|
395
|
+
return value.replace(/&#x([0-9a-f]+);/gi, (_, hex) => {
|
|
396
|
+
const code = Number.parseInt(hex, 16);
|
|
397
|
+
return Number.isFinite(code) ? String.fromCodePoint(code) : "";
|
|
398
|
+
}).replace(/&#(\d+);/g, (_, num) => {
|
|
399
|
+
const code = Number.parseInt(num, 10);
|
|
400
|
+
return Number.isFinite(code) ? String.fromCodePoint(code) : "";
|
|
401
|
+
}).replace(/[\u0000-\u0020]+/g, "").trim().toLowerCase();
|
|
402
|
+
}
|
|
403
|
+
function isDangerousUrl(value) {
|
|
404
|
+
const decoded = decodeUrl(value);
|
|
405
|
+
return decoded.startsWith("javascript:") || decoded.startsWith("vbscript:") || decoded.startsWith("data:text/html") || decoded.startsWith("data:application/xhtml");
|
|
406
|
+
}
|
|
407
|
+
function sanitizeThumbnailSvg(svg) {
|
|
408
|
+
const trimmed = svg.trim();
|
|
409
|
+
if (!trimmed) return svg;
|
|
410
|
+
let changed = false;
|
|
411
|
+
const $ = cheerio.load(trimmed, { xml: true });
|
|
412
|
+
$("*").each((_, el) => {
|
|
413
|
+
if (el.type !== "tag" && el.type !== "script") return;
|
|
414
|
+
const name = el.name.toLowerCase();
|
|
415
|
+
if (name === "script" || name === "iframe" || name === "object" || name === "embed" || name === "link" || name === "meta" || name === "base") {
|
|
416
|
+
$(el).remove();
|
|
417
|
+
changed = true;
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
const attribs = el.attribs;
|
|
421
|
+
for (const [attr, raw] of Object.entries(attribs)) {
|
|
422
|
+
const lower = attr.toLowerCase();
|
|
423
|
+
const value = raw ?? "";
|
|
424
|
+
if (lower.startsWith("on")) {
|
|
425
|
+
$(el).removeAttr(attr);
|
|
426
|
+
changed = true;
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
if (URL_ATTRS.has(lower) || lower.endsWith(":href")) {
|
|
430
|
+
if (isDangerousUrl(value)) {
|
|
431
|
+
$(el).removeAttr(attr);
|
|
432
|
+
changed = true;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
if (lower === "style" && /javascript\s*:|expression\s*\(|behavior\s*:|-moz-binding|@import/i.test(value)) {
|
|
436
|
+
$(el).removeAttr(attr);
|
|
437
|
+
changed = true;
|
|
438
|
+
}
|
|
439
|
+
if ((name === "set" || name === "animate" || name === "animatetransform") && lower === "attributename" && (value.toLowerCase().startsWith("on") || value.toLowerCase() === "href" || value.toLowerCase().endsWith(":href"))) {
|
|
440
|
+
$(el).remove();
|
|
441
|
+
changed = true;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
const structured = changed ? $.xml().trim() : trimmed;
|
|
446
|
+
const swept = structured.replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi, "").replace(/<script\b[^>]*\/?>/gi, "").replace(/\s+on[a-z]+\s*=\s*("[^"]*"|'[^']*'|[^\s>]+)/gi, "").replace(
|
|
447
|
+
/(\s(?:href|xlink:href|src)\s*=\s*)(["'])\s*(?:javascript|vbscript):[^"']*\2/gi,
|
|
448
|
+
"$1$2#$2"
|
|
449
|
+
);
|
|
450
|
+
const cleaned = swept === svg || swept === trimmed ? svg : swept;
|
|
451
|
+
return svgForImage(cleaned);
|
|
452
|
+
}
|
|
453
|
+
async function dropThumbnail(relativePath) {
|
|
454
|
+
const normalized = normalizeRelative(relativePath);
|
|
455
|
+
if (!normalized) return;
|
|
456
|
+
await fs5.rm(thumbFile(normalized), { force: true });
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
export {
|
|
460
|
+
isDataFileName,
|
|
461
|
+
getInstanceIdentity,
|
|
462
|
+
REPO_ROOT,
|
|
463
|
+
getDataRoot,
|
|
464
|
+
ensureDataRoot,
|
|
465
|
+
resolveSafePath,
|
|
466
|
+
toRelativePath,
|
|
467
|
+
META_DIR_NAME,
|
|
468
|
+
getMetaPath,
|
|
469
|
+
REVISION_RETENTION,
|
|
470
|
+
LOCK_STALE_MS,
|
|
471
|
+
getVersionsRoot,
|
|
472
|
+
getLocksRoot,
|
|
473
|
+
normalizeRelative,
|
|
474
|
+
hashContent,
|
|
475
|
+
createId,
|
|
476
|
+
safeStorageKey,
|
|
477
|
+
httpFail,
|
|
478
|
+
evaluateWrite,
|
|
479
|
+
resolveAvailablePath,
|
|
480
|
+
rewriteMovedPath,
|
|
481
|
+
atomicWriteText,
|
|
482
|
+
atomicWriteJson,
|
|
483
|
+
readJsonFile,
|
|
484
|
+
pathExists,
|
|
485
|
+
isProcessAlive,
|
|
486
|
+
rethrowKnownReadError,
|
|
487
|
+
textFiles,
|
|
488
|
+
sanitizeThumbnailSvg,
|
|
489
|
+
dropThumbnail
|
|
490
|
+
};
|