texlite 0.15.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/DESIGN.md +98 -5
  2. package/OPERATIONS.md +27 -0
  3. package/README.md +6 -0
  4. package/README.zh-CN.md +6 -0
  5. package/dist/client/assets/AdminUsers-5cAOkdRO.js +1 -0
  6. package/dist/client/assets/CitationLibraryDialog-G2kKkA2f.js +1 -0
  7. package/dist/client/assets/Dashboard-BJDFbzQa.js +5 -0
  8. package/dist/client/assets/GitDialog-BEtLh2S_.js +3 -0
  9. package/dist/client/assets/GitHubAccountDialog-D2kdCzOs.js +1 -0
  10. package/dist/client/assets/{HistoryDialog-IurkSJuU.js → HistoryDialog-BwxDdj-j.js} +3 -3
  11. package/dist/client/assets/LatexEditor-sMjmvA6V.js +141 -0
  12. package/dist/client/assets/{PdfPreview-BTmvQDCV.js → PdfPreview-z5KnowpL.js} +1 -1
  13. package/dist/client/assets/ProjectIconPickerDialog-DNV7M7vH.js +1 -0
  14. package/dist/client/assets/ProjectSettings-Mf8zDGRu.js +1 -0
  15. package/dist/client/assets/ProjectWorkspace-BJFxjMCn.js +32 -0
  16. package/dist/client/assets/QuickOpenDialog-C758pouQ.js +1 -0
  17. package/dist/client/assets/{SelectionHistoryDialog-DiRKiMEt.js → SelectionHistoryDialog-p8L2n4RS.js} +2 -2
  18. package/dist/client/assets/SystemMetricsDialog-DDiSuglF.js +1 -0
  19. package/dist/client/assets/TagManagementDialog-BTVERmQS.js +1 -0
  20. package/dist/client/assets/WordCountDialog-B6RvAI6V.js +1 -0
  21. package/dist/client/assets/check-Hlb-tLSh.js +1 -0
  22. package/dist/client/assets/chevron-up-D0psXvTv.js +1 -0
  23. package/dist/client/assets/{chktex-CZIYuMLb.js → chktex-ZnIx425m.js} +1 -1
  24. package/dist/client/assets/{citationLibrary-ZeQPCv5O.js → citationLibrary-C9WnXB-1.js} +2 -2
  25. package/dist/client/assets/clock-3-C71NmJZI.js +1 -0
  26. package/dist/client/assets/{editor-CLY5CgkY.js → editor-_i6aPXT4.js} +1 -1
  27. package/dist/client/assets/eye-swovgNYS.js +1 -0
  28. package/dist/client/assets/github-y6i0qxSV.js +1 -0
  29. package/dist/client/assets/{hard-drive-B9h-a7KY.js → hard-drive-BqcHPdoY.js} +1 -1
  30. package/dist/client/assets/index-7WrjzKvC.js +5 -0
  31. package/dist/client/assets/index-C5T8_lAl.js +1 -0
  32. package/dist/client/assets/index-DF5cor0N.js +10 -0
  33. package/dist/client/assets/index-Do5FsFqg.js +1 -0
  34. package/dist/client/assets/index-Y-wXUwwe.css +1 -0
  35. package/dist/client/assets/{minimize-2-Crq_SyYM.js → minimize-2-CK7jUy7-.js} +1 -1
  36. package/dist/client/assets/{minus-Dn3USloe.js → minus-B2sswwWT.js} +1 -1
  37. package/dist/client/assets/{plus-BwKRQJHI.js → plus-Co7iyp5m.js} +1 -1
  38. package/dist/client/assets/{refresh-cw-C5yVKb1r.js → refresh-cw-BHVcjInn.js} +1 -1
  39. package/dist/client/assets/{search-CDyQWr9H.js → search-Tpi6WAga.js} +1 -1
  40. package/dist/client/assets/{sigma-9fiQHdlo.js → sigma-DoHJQYf7.js} +1 -1
  41. package/dist/client/assets/spellCheck-BSbFJY9f.js +1 -0
  42. package/dist/client/assets/{tags-C_3BvsxV.js → tags-BHDlIcSb.js} +1 -1
  43. package/dist/client/assets/{type-CnT0mBxu.js → type-C-6Jq_0r.js} +1 -1
  44. package/dist/client/index.html +16 -4
  45. package/dist/client/texlite-icon.svg +8 -0
  46. package/dist/server/app.js +6 -1
  47. package/dist/server/auth.js +0 -3
  48. package/dist/server/cgroups.js +35 -0
  49. package/dist/server/chktex.js +3 -4
  50. package/dist/server/compilerVersions.js +46 -0
  51. package/dist/server/config.js +8 -0
  52. package/dist/server/cpuMetrics.js +122 -0
  53. package/dist/server/db.js +13 -0
  54. package/dist/server/environment.js +11 -0
  55. package/dist/server/i18n.js +4 -2
  56. package/dist/server/latexCompletion.js +11 -88
  57. package/dist/server/projectOutline.js +4 -39
  58. package/dist/server/resourceMetrics.js +214 -0
  59. package/dist/server/routes/citations.js +22 -0
  60. package/dist/server/routes/projectFiles.js +1 -59
  61. package/dist/server/routes/system.js +32 -1
  62. package/dist/server/routes/users.js +21 -3
  63. package/dist/server/systemMemory.js +51 -0
  64. package/dist/server/updates.js +116 -0
  65. package/dist/shared/bibtexReferences.js +5 -1
  66. package/dist/shared/compilerVersions.js +11 -0
  67. package/dist/shared/latexCompletion.js +1 -0
  68. package/dist/shared/latexReferences.js +13 -3
  69. package/dist/shared/latexSymbols.js +77 -0
  70. package/dist/shared/resourceMetrics.js +1 -0
  71. package/dist/shared/updates.js +5 -0
  72. package/package.json +3 -1
  73. package/texlite.config.example.json +3 -0
  74. package/dist/client/assets/AdminUsers-Cau9nlv9.js +0 -1
  75. package/dist/client/assets/CitationLibraryDialog-Cf_bbSq3.js +0 -1
  76. package/dist/client/assets/Dashboard-JQ8pJe2d.js +0 -2
  77. package/dist/client/assets/GitDialog-BcqqWKr-.js +0 -3
  78. package/dist/client/assets/GitHubAccountDialog-Cqcezf6G.js +0 -1
  79. package/dist/client/assets/LatexEditor-msdbtTyG.js +0 -141
  80. package/dist/client/assets/ProjectIconPickerDialog-Cbm-0YQW.js +0 -1
  81. package/dist/client/assets/ProjectNavigationDialogs-CozcEa2v.js +0 -1
  82. package/dist/client/assets/ProjectSettings-BlthcpdA.js +0 -1
  83. package/dist/client/assets/ProjectWorkspace-DUPLJCQ3.js +0 -32
  84. package/dist/client/assets/SystemMetricsDialog-DJJZueSr.js +0 -1
  85. package/dist/client/assets/TagManagementDialog-3c-WAWQd.js +0 -1
  86. package/dist/client/assets/WordCountDialog-BqlX2s36.js +0 -1
  87. package/dist/client/assets/check-EZ2H-iDj.js +0 -1
  88. package/dist/client/assets/external-link-DGfNJFKy.js +0 -1
  89. package/dist/client/assets/github-DJezB5ef.js +0 -1
  90. package/dist/client/assets/index-BgeOKYIx.js +0 -10
  91. package/dist/client/assets/index-CMBalIBd.css +0 -1
  92. package/dist/client/assets/index-DmBKxZf9.js +0 -5
  93. package/dist/client/assets/spellCheck-BCpcq-VN.js +0 -1
  94. package/dist/client/assets/user-plus-pxz36w4w.js +0 -1
  95. package/dist/client/assets/x-DN0Ol4Ya.js +0 -1
  96. package/dist/server/projectSearch.js +0 -117
@@ -3,7 +3,7 @@ import { maskLatexComments } from "../shared/latexLiterals.js";
3
3
  import { findLatexSourceIncludes } from "../shared/latexDependencies.js";
4
4
  import { forEachLatexCommand, readLatexMandatoryArguments } from "../shared/latexScanner.js";
5
5
  import { resolveLatexInclude, toLatexIncludeDirective } from "./latexDocumentGraph.js";
6
- import { listProjectFiles, listProjectFilesAsync, resolveSourcePath, safeRelativePath } from "./files.js";
6
+ import { listProjectFilesAsync, resolveSourcePath, safeRelativePath } from "./files.js";
7
7
  const levels = new Map([
8
8
  ["part", 0],
9
9
  ["chapter", 0],
@@ -12,40 +12,6 @@ const levels = new Map([
12
12
  ["subsubsection", 3],
13
13
  ["paragraph", 4]
14
14
  ]);
15
- export function buildProjectOutline(config, projectId, mainFileInput) {
16
- const mainFile = safeRelativePath(mainFileInput);
17
- return buildProjectOutlineFromFiles(config, projectId, mainFile, sourceFilePaths(listProjectFiles(config, projectId)));
18
- }
19
- function buildProjectOutlineFromFiles(config, projectId, mainFile, availableFiles) {
20
- const result = [];
21
- const visited = new Set();
22
- const visit = (filePath, importBase) => {
23
- if (visited.has(filePath) || visited.size >= 200)
24
- return;
25
- visited.add(filePath);
26
- const absolute = resolveSourcePath(config, projectId, filePath);
27
- if (!fs.existsSync(absolute) || !fs.statSync(absolute).isFile() || fs.statSync(absolute).size > 3 * 1024 * 1024)
28
- return;
29
- const content = fs.readFileSync(absolute, "utf8");
30
- for (const event of scanOutlineSource(content)) {
31
- if (event.type === "heading") {
32
- result.push({ path: filePath, line: event.line, level: event.level, title: event.title });
33
- continue;
34
- }
35
- const included = resolveLatexInclude(event.directive, importBase, availableFiles);
36
- if (included)
37
- visit(included.path, included.importBase);
38
- }
39
- };
40
- visit(mainFile, "");
41
- return result;
42
- }
43
- /** Async outline builder used by the HTTP path so large projects do not block the event loop. */
44
- export async function buildProjectOutlineAsync(config, projectId, mainFileInput) {
45
- const mainFile = safeRelativePath(mainFileInput);
46
- const availableFiles = sourceFilePaths(await listProjectFilesAsync(config, projectId));
47
- return buildProjectOutlineAsyncFromFiles(config, projectId, mainFile, availableFiles);
48
- }
49
15
  async function buildProjectOutlineAsyncFromFiles(config, projectId, mainFile, availableFiles) {
50
16
  const result = [];
51
17
  const visited = new Set();
@@ -137,10 +103,9 @@ function sourceFilePaths(entries) {
137
103
  return new Set(entries.filter((entry) => entry.type === "file").map((entry) => entry.path));
138
104
  }
139
105
  /**
140
- * Read one source file into ordered outline events. The shared scanner keeps
141
- * offsets from the original source while skipping comments and literal TeX
142
- * forms, so the synchronous and asynchronous builders cannot drift in their
143
- * interpretation of headings and arguments.
106
+ * Read one source file into ordered heading and include events. The shared
107
+ * scanner preserves original source offsets while skipping comments and
108
+ * literal TeX forms.
144
109
  */
145
110
  function scanOutlineSource(source) {
146
111
  const lineStarts = sourceLineStarts(source);
@@ -0,0 +1,214 @@
1
+ import { execFile } from "node:child_process";
2
+ import { readdir, readFile } from "node:fs/promises";
3
+ import { promisify } from "node:util";
4
+ import { collectSystemMemory } from "./systemMemory.js";
5
+ import { collectCpuSnapshot, CpuMetrics } from "./cpuMetrics.js";
6
+ const exec = promisify(execFile);
7
+ const commandOptions = { timeout: 3_000, maxBuffer: 4 * 1024 * 1024, windowsHide: true, env: { ...process.env, LC_ALL: "C" } };
8
+ const texTools = new Set([
9
+ "tex", "latex", "pdftex", "pdflatex", "xetex", "xelatex", "luatex", "lualatex", "luahbtex", "luahblatex",
10
+ "pdfetex", "etex", "elatex", "euptex", "uptex", "uplatex", "ptex", "platex", "tectonic",
11
+ "latexmk", "bibtex", "bibtex8", "bibtexu", "biber", "makeindex", "xindy", "texindy", "makeglossaries",
12
+ "dvipdfm", "dvipdfmx", "xdvipdfmx", "dvips", "texcount", "chktex", "harper-cli"
13
+ ]);
14
+ const texScripts = new Set(["latexmk", "biber", "makeglossaries", "texcount", "texindy", "xindy"]);
15
+ const unavailable = () => ({ rssBytes: null, processCount: null });
16
+ const processAccess = {
17
+ read: (file) => readFile(file, "utf8"),
18
+ directories: () => readdir("/proc"),
19
+ command: async (file, args) => (await exec(file, args, commandOptions)).stdout
20
+ };
21
+ function executableName(value) {
22
+ return value.replaceAll("\\", "/").split("/").at(-1).toLowerCase().replace(/\.(?:exe|pl)$/i, "").replace(/^miktex-/, "");
23
+ }
24
+ function isPerl(name) {
25
+ return /^perl(?:\d+(?:\.\d+)*)?$/.test(executableName(name));
26
+ }
27
+ /** Includes Harper CLI writing checks; match tool names, not arbitrary arguments or every Perl process. */
28
+ export function isTexProcess(process) {
29
+ if (texTools.has(executableName(process.name)))
30
+ return true;
31
+ if (!isPerl(process.name) || !process.argv)
32
+ return false;
33
+ const args = process.argv.slice(1);
34
+ for (let index = 0; index < args.length; index++) {
35
+ const arg = args[index];
36
+ if (arg === "--")
37
+ return texScripts.has(executableName(args[index + 1] ?? ""));
38
+ if (arg === "-e" || arg === "-E" || /^-[eE]/.test(arg))
39
+ return false;
40
+ if (["-I", "-M", "-m"].includes(arg)) {
41
+ index++;
42
+ continue;
43
+ }
44
+ if (arg.startsWith("-"))
45
+ continue;
46
+ return texScripts.has(executableName(arg));
47
+ }
48
+ return false;
49
+ }
50
+ /** A failed RSS read must not look like a successfully measured zero. */
51
+ export function sumTexMemory(processes) {
52
+ const selected = new Map(processes.filter(isTexProcess).map((process) => [process.pid, process]));
53
+ let rssBytes = 0;
54
+ for (const process of selected.values()) {
55
+ if (process.rssBytes === null || !Number.isFinite(process.rssBytes) || process.rssBytes < 0)
56
+ return unavailable();
57
+ rssBytes += process.rssBytes;
58
+ }
59
+ return { rssBytes, processCount: selected.size };
60
+ }
61
+ // Handle quoted script paths when available; Linux uses lossless NUL-separated argv.
62
+ function commandArguments(command) {
63
+ return (command.match(/"[^"]*"|'[^']*'|\S+/g) ?? []).map((part) => /^(["']).*\1$/.test(part) ? part.slice(1, -1) : part);
64
+ }
65
+ export function parsePosixProcesses(output) {
66
+ return output.trim().split("\n").filter(Boolean).map((line) => {
67
+ const match = line.match(/^\s*(\d+)\s+(\d+)\s+(.+?)\s*$/);
68
+ if (!match)
69
+ throw new Error("Invalid ps memory sample");
70
+ const timing = match[3].match(/^(\S+)\s+((?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s+\w+\s+\d+\s+[\d:]+\s+\d{4})\s+(.+)$/);
71
+ return { pid: Number(match[1]), rssBytes: Number(match[2]) * 1024, name: timing?.[3] ?? match[3],
72
+ ...(timing ? { cpuTime: parseCpuTime(timing[1]), started: timing[2] } : {}) };
73
+ });
74
+ }
75
+ /** BSD ps CPU time: [[days-]hours:]minutes:seconds[.fraction]. */
76
+ export function parseCpuTime(value) {
77
+ const match = value.match(/^(?:(\d+)-)?(?:(\d+):)?(\d+):(\d+(?:\.\d+)?)$/);
78
+ if (!match)
79
+ throw new Error("Invalid process CPU time");
80
+ return ((Number(match[1] ?? 0) * 86_400) + Number(match[2] ?? 0) * 3_600 + Number(match[3]) * 60 + Number(match[4])) * 1_000_000;
81
+ }
82
+ export function parseLinuxProcessCpu(stat) {
83
+ // comm can contain spaces and parentheses. The final ')' ends that field.
84
+ const closing = stat.lastIndexOf(")");
85
+ if (closing < 0)
86
+ throw new Error("Invalid process stat");
87
+ const fields = stat.slice(closing + 1).trim().split(/\s+/);
88
+ if (![fields[11], fields[12], fields[19]].every((field) => /^\d+$/.test(field ?? "")))
89
+ throw new Error("Invalid process CPU counters");
90
+ return { cpuTime: Number(fields[11]) + Number(fields[12]), started: fields[19] };
91
+ }
92
+ export function parseWindowsProcesses(output) {
93
+ const decoded = JSON.parse(output.replace(/^\uFEFF/, ""));
94
+ if (decoded === null)
95
+ return [];
96
+ const entries = Array.isArray(decoded) ? decoded : [decoded];
97
+ return entries.map((value) => {
98
+ if (!value || typeof value !== "object")
99
+ throw new Error("Invalid CIM memory sample");
100
+ const entry = value;
101
+ if (typeof entry.Name !== "string" || !Number.isInteger(entry.ProcessId))
102
+ throw new Error("Invalid CIM process");
103
+ const rss = entry.WorkingSetSize === null || entry.WorkingSetSize === undefined ? null : Number(entry.WorkingSetSize);
104
+ return {
105
+ pid: entry.ProcessId, name: entry.Name,
106
+ rssBytes: rss, argv: typeof entry.CommandLine === "string" ? commandArguments(entry.CommandLine) : undefined,
107
+ ...(entry.UserModeTime !== null && entry.UserModeTime !== undefined && entry.KernelModeTime !== null && entry.KernelModeTime !== undefined && typeof entry.CreationDate === "string"
108
+ ? { cpuTime: (Number(entry.UserModeTime) + Number(entry.KernelModeTime)) / 10, started: entry.CreationDate } : {})
109
+ };
110
+ });
111
+ }
112
+ async function linuxProcesses(io) {
113
+ const ids = (await io.directories()).filter((name) => /^\d+$/.test(name));
114
+ const result = [];
115
+ let cursor = 0;
116
+ // Bound open file descriptors even on hosts running many unrelated processes.
117
+ await Promise.all(Array.from({ length: Math.min(16, ids.length) }, async () => {
118
+ while (cursor < ids.length) {
119
+ const pid = Number(ids[cursor++]);
120
+ try {
121
+ const status = await io.read(`/proc/${pid}/status`);
122
+ if (/^State:\s+Z/m.test(status))
123
+ continue;
124
+ const name = status.match(/^Name:\s+(.+)$/m)?.[1]?.trim();
125
+ if (!name || (!texTools.has(executableName(name)) && !isPerl(name)))
126
+ continue;
127
+ const rss = status.match(/^VmRSS:\s+(\d+)\s+kB$/m);
128
+ const argv = isPerl(name) ? (await io.read(`/proc/${pid}/cmdline`)).split("\0").filter(Boolean) : undefined;
129
+ // A missing CPU counter must not discard an otherwise valid memory reading.
130
+ const cpu = await io.read(`/proc/${pid}/stat`).then(parseLinuxProcessCpu).catch(() => ({}));
131
+ result.push({ pid, name, rssBytes: rss ? Number(rss[1]) * 1024 : null, argv, ...cpu });
132
+ }
133
+ catch (error) {
134
+ // Exiting processes and processes hidden by OS permissions are outside this visible sample.
135
+ if (!["ENOENT", "ESRCH", "EACCES", "EPERM"].includes(error.code ?? ""))
136
+ throw error;
137
+ }
138
+ }
139
+ }));
140
+ return result;
141
+ }
142
+ async function macProcesses(io) {
143
+ const stdout = await io.command("ps", ["-ww", "-axo", "pid=,rss=,time=,lstart=,comm="]);
144
+ const result = parsePosixProcesses(stdout);
145
+ const interpreters = result.filter((process) => isPerl(process.name));
146
+ if (interpreters.length) {
147
+ const commands = await io.command("ps", ["-ww", "-p", interpreters.map((process) => process.pid).join(","), "-o", "pid=,args="])
148
+ .catch((error) => {
149
+ // ps exits 1 if all requested processes disappeared between the two samples.
150
+ if (error.code === 1)
151
+ return "";
152
+ throw error;
153
+ });
154
+ const argsByPid = new Map(commands.trim().split("\n").flatMap((line) => {
155
+ const match = line.match(/^\s*(\d+)\s+(.+)$/);
156
+ return match ? [[Number(match[1]), commandArguments(match[2])]] : [];
157
+ }));
158
+ for (const process of interpreters)
159
+ process.argv = argsByPid.get(process.pid);
160
+ }
161
+ return result;
162
+ }
163
+ export async function collectTexResources(platform = process.platform, io = processAccess) {
164
+ try {
165
+ let processes;
166
+ if (platform === "linux")
167
+ processes = await linuxProcesses(io);
168
+ else if (platform === "darwin")
169
+ processes = await macProcesses(io);
170
+ else if (platform === "win32") {
171
+ const stdout = await io.command("powershell.exe", ["-NoLogo", "-NoProfile", "-NonInteractive", "-Command",
172
+ "Get-CimInstance Win32_Process -ErrorAction Stop | Select-Object ProcessId,Name,WorkingSetSize,CommandLine,UserModeTime,KernelModeTime,CreationDate | ConvertTo-Json -Compress"]);
173
+ processes = parseWindowsProcesses(stdout);
174
+ }
175
+ else
176
+ return { memory: unavailable(), cpu: null };
177
+ const selected = processes.filter(isTexProcess);
178
+ const validCpu = selected.every((process) => process.cpuTime !== undefined && Number.isFinite(process.cpuTime) && process.cpuTime >= 0 && process.started !== undefined);
179
+ return { memory: sumTexMemory(selected), cpu: validCpu ? selected.map((process) => ({ pid: process.pid, started: process.started, time: process.cpuTime })) : null };
180
+ }
181
+ catch { /* Optional monitoring must never interrupt application operations. */ }
182
+ return { memory: unavailable(), cpu: null };
183
+ }
184
+ export async function collectTexMemory(platform = process.platform, io = processAccess) {
185
+ return (await collectTexResources(platform, io)).memory;
186
+ }
187
+ async function collectResources(cpu) {
188
+ const [systemMemory, tex] = await Promise.all([collectSystemMemory(), collectTexResources()]);
189
+ const cpuSnapshot = await collectCpuSnapshot(systemMemory.scope, tex.cpu);
190
+ return { systemMemory, texMemory: tex.memory, cpu: cpu.update(cpuSnapshot, systemMemory.scope) };
191
+ }
192
+ /** On-demand only: no timers or process scans while nobody is viewing System status. */
193
+ export class ResourceMetrics {
194
+ now;
195
+ cached = null;
196
+ pending = null;
197
+ collect;
198
+ constructor(collect, now = Date.now) {
199
+ this.now = now;
200
+ const cpu = new CpuMetrics();
201
+ this.collect = collect ?? (() => collectResources(cpu));
202
+ }
203
+ sample() {
204
+ if (this.cached && this.now() - this.cached.at < 2_000)
205
+ return Promise.resolve(this.cached.value);
206
+ if (this.pending)
207
+ return this.pending;
208
+ this.pending = this.collect().then((value) => {
209
+ this.cached = { at: this.now(), value };
210
+ return value;
211
+ }).finally(() => { this.pending = null; });
212
+ return this.pending;
213
+ }
214
+ }
@@ -177,6 +177,28 @@ export function registerCitationRoutes(app, context) {
177
177
  .run(tag.id, user.id, tag.name, tag.color, now());
178
178
  return reply.code(201).send({ tag, created: true });
179
179
  });
180
+ app.patch("/api/citations/tags/:tagId", async (request, reply) => {
181
+ const user = requireUser(request, reply, db);
182
+ if (!user)
183
+ return;
184
+ const { tagId } = request.params;
185
+ const existing = db.prepare("SELECT id, name, color, user_id FROM citation_library_tags WHERE id = ? AND user_id = ?")
186
+ .get(tagId, user.id);
187
+ if (!existing)
188
+ return apiError(reply, 404, "CITATION_TAG_NOT_FOUND");
189
+ const body = request.body;
190
+ const name = body?.name === undefined ? existing.name : citationTagName(body.name);
191
+ const color = body?.color === undefined ? existing.color : body.color;
192
+ if (!tagColors.includes(color))
193
+ throw new ValidationError();
194
+ const duplicate = db.prepare("SELECT id FROM citation_library_tags WHERE user_id = ? AND name = ? COLLATE NOCASE AND id != ?")
195
+ .get(user.id, name, tagId);
196
+ if (duplicate)
197
+ return apiError(reply, 409, "CITATION_TAG_NAME_EXISTS");
198
+ db.prepare("UPDATE citation_library_tags SET name = ?, color = ? WHERE id = ? AND user_id = ?")
199
+ .run(name, color, tagId, user.id);
200
+ return { tag: { id: tagId, name, color, ownerId: user.id } };
201
+ });
180
202
  app.delete("/api/citations/tags/:tagId", async (request, reply) => {
181
203
  const user = requireUser(request, reply, db);
182
204
  if (!user)
@@ -11,10 +11,9 @@ import { compileMainFile } from "../compileArtifacts.js";
11
11
  import { assertNoSourceSymlinks, listProjectFilesAsync, resolveSourcePath, safePathSegment, safeRelativePath, sourceRoot } from "../files.js";
12
12
  import { apiError, contentDisposition, httpError } from "../http.js";
13
13
  import { accessibleProject, canEdit } from "../projects.js";
14
- import { replaceProject, searchProject } from "../projectSearch.js";
15
14
  import { mainDocumentCandidates } from "../latexRoot.js";
16
15
  import { commentsForFile, escapeGlobPattern, movedProjectPath, now, requireEditableProject, touchProject } from "./projectShared.js";
17
- /** Register project file-tree, editor content, search, and upload routes. */
16
+ /** Register project file-tree, editor content, and upload routes. */
18
17
  export function registerProjectFileRoutes(app, context) {
19
18
  const { config, db, collaboration, projectMutations, latexCompletions, projectOutlines, metrics, recordHistory } = context;
20
19
  app.get("/api/projects/:id/files", async (request, reply) => {
@@ -87,63 +86,6 @@ export function registerProjectFileRoutes(app, context) {
87
86
  metrics.record("outline.build", performance.now() - startedAt);
88
87
  }
89
88
  });
90
- app.get("/api/projects/:id/search", async (request, reply) => {
91
- const user = requireUser(request, reply, db);
92
- if (!user)
93
- return;
94
- const { id } = request.params;
95
- if (!accessibleProject(db, id, user))
96
- return apiError(reply, 404, "PROJECT_NOT_FOUND");
97
- const query = request.query;
98
- const startedAt = performance.now();
99
- try {
100
- return await projectMutations.runConsistentRead(id, () => searchProject(config, id, {
101
- query: query.q ?? "",
102
- caseSensitive: query.caseSensitive === "1",
103
- wholeWord: query.wholeWord === "1"
104
- }), {
105
- preflight: () => {
106
- if (!accessibleProject(db, id, user))
107
- throw httpError(404, "PROJECT_NOT_FOUND");
108
- }
109
- });
110
- }
111
- finally {
112
- metrics.record("search.project", performance.now() - startedAt);
113
- }
114
- });
115
- app.post("/api/projects/:id/search/replace", async (request, reply) => {
116
- const user = requireUser(request, reply, db);
117
- if (!user)
118
- return;
119
- const { id } = request.params;
120
- const project = accessibleProject(db, id, user);
121
- if (!project || !canEdit(project))
122
- return apiError(reply, 403, "PROJECT_EDIT_FORBIDDEN");
123
- const body = request.body;
124
- if (typeof body.query !== "string" || typeof body.replacement !== "string" || body.replacement.length > 100_000) {
125
- return apiError(reply, 400, "SEARCH_QUERY_INVALID");
126
- }
127
- return await projectMutations.runExclusive(id, "project-wide replace", async () => {
128
- const changed = await replaceProject(config, id, {
129
- query: body.query,
130
- caseSensitive: body.caseSensitive === true,
131
- wholeWord: body.wholeWord === true,
132
- maxFileBytes: maxCollaborativeFileBytes(config)
133
- }, body.replacement);
134
- let replacements = 0;
135
- for (const file of changed) {
136
- reanchorFileComments(db, id, file.path, file.previous, file.content);
137
- collaboration.updateFile(id, file.path, file.content, user.id);
138
- replacements += file.count;
139
- }
140
- if (changed.length) {
141
- touchProject(db, id, user.id);
142
- recordHistory(id, user.id, "file", changed.map((file) => file.path));
143
- }
144
- return { ok: true, replacements, files: changed.map((file) => file.path) };
145
- }, { preflight: () => { requireEditableProject(db, id, user); } });
146
- });
147
89
  app.get("/api/projects/:id/completions", async (request, reply) => {
148
90
  const user = requireUser(request, reply, db);
149
91
  if (!user)
@@ -1,10 +1,38 @@
1
- import { requireAdmin } from "../auth.js";
1
+ import { requireAdmin, requireUser } from "../auth.js";
2
2
  import { maxCollaborativeFileBytes } from "../collaboration.js";
3
3
  import { MAX_CITATION_BIBTEX_BYTES } from "../limits.js";
4
4
  import { MIN_PASSWORD_LENGTH } from "../security.js";
5
+ import { apiError } from "../http.js";
6
+ import { ResourceMetrics } from "../resourceMetrics.js";
7
+ import { CompilerVersionService } from "../compilerVersions.js";
8
+ import { inspectCompilerEnvironment } from "../environment.js";
5
9
  /** Register routes that describe the running server or its live connections. */
6
10
  export function registerSystemRoutes(app, context) {
7
11
  const { config, db, queue, collaboration, latexCompletions, projectOutlines, metrics, eventLoopDelay } = context;
12
+ const resources = new ResourceMetrics();
13
+ const compilerVersions = new CompilerVersionService(() => inspectCompilerEnvironment(config));
14
+ app.get("/api/compiler/versions", async (request, reply) => {
15
+ if (!requireUser(request, reply, db))
16
+ return;
17
+ reply.header("Cache-Control", "no-store");
18
+ return compilerVersions.getVersions();
19
+ });
20
+ app.get("/api/admin/updates", async (request, reply) => {
21
+ const user = requireAdmin(request, reply, db);
22
+ if (!user)
23
+ return;
24
+ reply.header("Cache-Control", "no-store");
25
+ await context.updates.check();
26
+ return { update: context.updates.notice(user.id) };
27
+ });
28
+ app.post("/api/admin/updates/dismiss", async (request, reply) => {
29
+ const user = requireAdmin(request, reply, db);
30
+ if (!user)
31
+ return;
32
+ if (!context.updates.dismiss(user.id, request.body?.version))
33
+ return apiError(reply, 400, "REQUEST_INVALID");
34
+ return { ok: true };
35
+ });
8
36
  app.get("/api/config", async () => ({
9
37
  siteName: config.siteName,
10
38
  adminEmail: config.adminEmail,
@@ -20,10 +48,13 @@ export function registerSystemRoutes(app, context) {
20
48
  app.get("/api/health/metrics", async (request, reply) => {
21
49
  if (!requireAdmin(request, reply, db))
22
50
  return;
51
+ reply.header("Cache-Control", "no-store");
52
+ const resourceMemory = await resources.sample();
23
53
  const memory = process.memoryUsage();
24
54
  return {
25
55
  uptimeSeconds: Math.round(process.uptime()),
26
56
  memory: { rssBytes: memory.rss, heapUsedBytes: memory.heapUsed, heapTotalBytes: memory.heapTotal },
57
+ ...resourceMemory,
27
58
  eventLoopDelay: {
28
59
  p50Ms: Math.round(eventLoopDelay.percentile(50) / 100_000) / 10,
29
60
  p95Ms: Math.round(eventLoopDelay.percentile(95) / 100_000) / 10,
@@ -17,12 +17,27 @@ export function registerUserManagementRoutes(app, context) {
17
17
  app.get("/api/admin/users", async (request, reply) => {
18
18
  if (!requireAdmin(request, reply, db))
19
19
  return;
20
+ const query = request.query;
21
+ const search = typeof query.q === "string" ? query.q.trim() : "";
22
+ const requestedPage = Number(query.page ?? "1");
23
+ const pageSize = 20;
24
+ const where = search ? "WHERE (u.username LIKE ? ESCAPE '\\' OR u.display_name LIKE ? ESCAPE '\\')" : "";
25
+ const pattern = `%${search.replace(/[\\%_]/g, "\\$&")}%`;
26
+ const params = search ? [pattern, pattern] : [];
27
+ const total = db.prepare(`SELECT COUNT(*) AS count FROM users u ${where}`).get(...params).count;
28
+ const totalPages = Math.ceil(total / pageSize);
29
+ const page = Math.min(Number.isSafeInteger(requestedPage) && requestedPage > 0 ? requestedPage : 1, Math.max(1, totalPages));
20
30
  const users = db.prepare(`
21
31
  SELECT u.*,
22
32
  (SELECT COUNT(*) FROM projects p WHERE p.owner_id = u.id) AS owned_projects
23
- FROM users u ORDER BY u.created_at
24
- `).all();
25
- return { users: users.map((user) => ({ ...publicUser(user), ownedProjects: user.owned_projects })) };
33
+ FROM users u ${where}
34
+ ORDER BY u.created_at, u.username COLLATE NOCASE
35
+ LIMIT ? OFFSET ?
36
+ `).all(...params, pageSize, (page - 1) * pageSize);
37
+ return {
38
+ users: users.map((user) => ({ ...publicUser(user), ownedProjects: user.owned_projects })),
39
+ pagination: { page, pageSize, total, totalPages }
40
+ };
26
41
  });
27
42
  app.post("/api/admin/users", async (request, reply) => {
28
43
  if (!requireAdmin(request, reply, db))
@@ -56,6 +71,9 @@ export function registerUserManagementRoutes(app, context) {
56
71
  const body = request.body;
57
72
  const role = body.role === "admin" ? "admin" : body.role === "user" ? "user" : target.role;
58
73
  const disabled = typeof body.disabled === "boolean" ? Number(body.disabled) : target.disabled;
74
+ if (target.id === admin.id && (disabled || role !== "admin")) {
75
+ return apiError(reply, 400, "SELF_RESTRICT_FORBIDDEN");
76
+ }
59
77
  const canCreateProjects = typeof body.canCreateProjects === "boolean"
60
78
  ? Number(body.canCreateProjects) : target.can_create_projects;
61
79
  if (target.role === "admin" && (!role || role !== "admin" || disabled) && activeAdminCount(db) <= 1) {
@@ -0,0 +1,51 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { cgroupPaths } from "./cgroups.js";
5
+ const readText = (file) => readFile(file, "utf8");
6
+ /** Linux containers expose their own cgroup, not the Docker daemon or host socket. */
7
+ export async function collectSystemMemory(platform = process.platform, read = readText) {
8
+ const total = os.totalmem();
9
+ const fallback = { scope: "host", totalBytes: total, usedBytes: Math.max(0, total - os.freemem()) };
10
+ if (platform !== "linux")
11
+ return fallback;
12
+ const optional = (file) => read(file).catch(() => null);
13
+ const [groups, mounts, docker, podman] = await Promise.all([
14
+ optional("/proc/self/cgroup"), optional("/proc/self/mountinfo"), optional("/.dockerenv"), optional("/run/.containerenv")
15
+ ]);
16
+ const container = docker !== null || podman !== null || /(?:docker|kubepods|libpod|containerd)/.test(groups ?? "");
17
+ if (!container) {
18
+ const info = await optional("/proc/meminfo");
19
+ const available = info?.match(/^MemAvailable:\s+(\d+)\s+kB$/m);
20
+ if (available)
21
+ fallback.usedBytes = Math.max(0, total - Number(available[1]) * 1024);
22
+ return fallback;
23
+ }
24
+ const candidates = cgroupPaths(groups ?? "", mounts ?? "", "memory");
25
+ for (const { directory, mountPoint, v2 } of candidates) {
26
+ const usage = await optional(`${directory}/${v2 ? "memory.current" : "memory.usage_in_bytes"}`);
27
+ const usedBytes = memoryNumber(usage);
28
+ if (usedBytes === null)
29
+ continue;
30
+ // A parent cgroup can impose a tighter limit than the container's own entry.
31
+ let totalBytes = null;
32
+ for (let current = directory;; current = path.posix.dirname(current)) {
33
+ const raw = await optional(`${current}/${v2 ? "memory.max" : "memory.limit_in_bytes"}`);
34
+ const limit = memoryNumber(raw);
35
+ // cgroup v1 uses a huge sentinel for an unlimited group; v2 uses "max".
36
+ if (limit !== null && limit > 0 && limit < 2 ** 60)
37
+ totalBytes = Math.min(totalBytes ?? total, limit, total);
38
+ if (current === mountPoint || current === "/")
39
+ break;
40
+ }
41
+ return { scope: "container", usedBytes, totalBytes };
42
+ }
43
+ // Never label host-wide usage as container usage when cgroup access is unavailable.
44
+ return { scope: "container", usedBytes: null, totalBytes: null };
45
+ }
46
+ function memoryNumber(value) {
47
+ if (value === null || !/^\d+$/.test(value.trim()))
48
+ return null;
49
+ const number = Number(value.trim());
50
+ return Number.isSafeInteger(number) ? number : null;
51
+ }
@@ -0,0 +1,116 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { gt, prerelease, valid } from "semver";
4
+ import { packageRootDirectory } from "./runtimePaths.js";
5
+ export const UPDATE_CHECK_INTERVAL_MS = 24 * 60 * 60_000;
6
+ export const UPDATE_RETRY_INTERVAL_MS = 60 * 60_000;
7
+ const REQUEST_TIMEOUT_MS = 5_000;
8
+ const REGISTRY_URL = "https://registry.npmjs.org/texlite/latest";
9
+ function packageVersion() {
10
+ const metadata = JSON.parse(fs.readFileSync(path.join(packageRootDirectory(), "package.json"), "utf8"));
11
+ return metadata.version;
12
+ }
13
+ function stableVersion(value) {
14
+ return typeof value === "string" && valid(value) === value && prerelease(value) === null;
15
+ }
16
+ /** One background checker per server; cached results and dismissals survive restarts. */
17
+ export class UpdateChecker {
18
+ db;
19
+ options;
20
+ fetch;
21
+ now;
22
+ currentVersion;
23
+ method;
24
+ pending = null;
25
+ controller = null;
26
+ timer = null;
27
+ disposed = false;
28
+ constructor(db, options) {
29
+ this.db = db;
30
+ this.options = options;
31
+ this.fetch = options.fetch ?? globalThis.fetch;
32
+ this.now = options.now ?? Date.now;
33
+ this.currentVersion = options.currentVersion ?? packageVersion();
34
+ this.method = options.method ?? (fs.existsSync("/.dockerenv") || fs.existsSync("/run/.containerenv") ? "docker" : "npm");
35
+ }
36
+ start() {
37
+ if (!this.options.enabled || this.disposed || this.timer)
38
+ return;
39
+ void this.check().catch(() => undefined);
40
+ this.timer = setInterval(() => { void this.check().catch(() => undefined); }, UPDATE_RETRY_INTERVAL_MS);
41
+ this.timer.unref();
42
+ }
43
+ cache() {
44
+ return this.db.prepare("SELECT latest_version, next_check_at FROM update_check_cache WHERE id = 1").get();
45
+ }
46
+ check() {
47
+ if (!this.options.enabled || this.disposed || !valid(this.currentVersion))
48
+ return Promise.resolve();
49
+ if (this.pending)
50
+ return this.pending;
51
+ if ((this.cache()?.next_check_at ?? 0) > this.now())
52
+ return Promise.resolve();
53
+ this.pending = this.checkRegistry().finally(() => { this.pending = null; });
54
+ return this.pending;
55
+ }
56
+ async checkRegistry() {
57
+ const controller = new AbortController();
58
+ this.controller = controller;
59
+ const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
60
+ timeout.unref();
61
+ let latest = null;
62
+ try {
63
+ const response = await this.fetch(REGISTRY_URL, {
64
+ headers: { Accept: "application/json" }, signal: controller.signal,
65
+ credentials: "omit", redirect: "error"
66
+ });
67
+ if (!response.ok)
68
+ throw new Error("Update registry unavailable");
69
+ const metadata = await response.json();
70
+ const version = metadata && typeof metadata === "object" && "version" in metadata ? metadata.version : null;
71
+ if (!stableVersion(version))
72
+ throw new Error("Invalid stable release version");
73
+ latest = version;
74
+ }
75
+ catch {
76
+ // Offline operation is normal. Keep the last known release and back off.
77
+ }
78
+ finally {
79
+ clearTimeout(timeout);
80
+ this.controller = null;
81
+ }
82
+ if (this.disposed)
83
+ return;
84
+ this.db.prepare(`INSERT INTO update_check_cache (id, latest_version, next_check_at) VALUES (1, ?, ?)
85
+ ON CONFLICT(id) DO UPDATE SET latest_version = COALESCE(excluded.latest_version, latest_version), next_check_at = excluded.next_check_at`)
86
+ .run(latest, this.now() + (latest ? UPDATE_CHECK_INTERVAL_MS : UPDATE_RETRY_INTERVAL_MS));
87
+ }
88
+ notice(userId) {
89
+ if (!this.options.enabled || !valid(this.currentVersion))
90
+ return null;
91
+ const latest = this.cache()?.latest_version;
92
+ if (!stableVersion(latest) || !gt(latest, this.currentVersion))
93
+ return null;
94
+ const dismissed = this.db.prepare("SELECT version FROM dismissed_updates WHERE user_id = ?").get(userId);
95
+ if (dismissed && valid(dismissed.version) && !gt(latest, dismissed.version))
96
+ return null;
97
+ return { currentVersion: this.currentVersion, latestVersion: latest, method: this.method };
98
+ }
99
+ dismiss(userId, version) {
100
+ if (!stableVersion(version) || this.cache()?.latest_version !== version)
101
+ return false;
102
+ const previous = this.db.prepare("SELECT version FROM dismissed_updates WHERE user_id = ?").get(userId);
103
+ if (previous && valid(previous.version) && gt(previous.version, version))
104
+ return true;
105
+ this.db.prepare(`INSERT INTO dismissed_updates (user_id, version) VALUES (?, ?)
106
+ ON CONFLICT(user_id) DO UPDATE SET version = excluded.version`).run(userId, version);
107
+ return true;
108
+ }
109
+ async dispose() {
110
+ this.disposed = true;
111
+ if (this.timer)
112
+ clearInterval(this.timer);
113
+ this.controller?.abort();
114
+ await this.pending;
115
+ }
116
+ }
@@ -36,7 +36,11 @@ function readBibtexBlock(source, start) {
36
36
  continue;
37
37
  }
38
38
  if (quoted) {
39
- if (character === "\"")
39
+ if (character === "{")
40
+ braceDepth += 1;
41
+ else if (character === "}" && braceDepth > 0)
42
+ braceDepth -= 1;
43
+ else if (character === "\"" && braceDepth === 0)
40
44
  quoted = false;
41
45
  continue;
42
46
  }