@studyllm/resume-preview 0.0.0-stage → 3.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/LICENSE +21 -0
- package/README.en.md +318 -0
- package/README.md +313 -2
- package/build/constants.js +217 -0
- package/build/diagram-service.js +143 -0
- package/build/file-utils.js +350 -0
- package/build/handlers.js +172 -0
- package/build/html-utils.js +20 -0
- package/build/i18n.js +198 -0
- package/build/index.js +171 -0
- package/build/live-server.js +488 -0
- package/build/logger.js +92 -0
- package/build/mermaid-cli.js +48 -0
- package/build/page-renderer.js +152 -0
- package/build/preview/favicon.svg +23 -0
- package/build/preview/gallery.css +217 -0
- package/build/preview/gallery.html +59 -0
- package/build/preview/gallery.js +259 -0
- package/build/preview/i18n/en.json +57 -0
- package/build/preview/i18n/zh-CN.json +57 -0
- package/build/preview/i18n.js +125 -0
- package/build/preview/resume-template.html +13 -0
- package/build/preview/resume.css +214 -0
- package/build/preview/resume.js +393 -0
- package/build/preview/shared.css +529 -0
- package/build/resume-renderer.js +486 -0
- package/build/resume-service.js +117 -0
- package/build/routes.js +335 -0
- package/build/serve.js +46 -0
- package/build/types.js +5 -0
- package/package.json +63 -4
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { readFile, writeFile, mkdir, rm, stat, access, rename, cp } from "fs/promises";
|
|
2
|
+
import { join, resolve, extname } from "path";
|
|
3
|
+
import { tmpdir } from "os";
|
|
4
|
+
import { createHash } from "crypto";
|
|
5
|
+
import { APP_NAME, LEGACY_APP_NAME, PREVIEW_ID_REGEX, UNIX_SYSTEM_PATHS, WINDOWS_SYSTEM_PATHS, FILE_NAMES, DIR_NAMES, RESUME_TEMPLATES, RESUME_THEMES, RESUME_FORMATS, SUPPORTED_LOCALES, DEFAULT_RESUME_LOCALE, RESUME_CONTENT_VERSION, RESUME_MAX_CONTENT_BYTES, RESUME_SLUG_HASH_LENGTH, } from "./constants.js";
|
|
6
|
+
export function getConfigDir() {
|
|
7
|
+
const xdg = process.env.XDG_CONFIG_HOME;
|
|
8
|
+
if (xdg && xdg.trim())
|
|
9
|
+
return xdg;
|
|
10
|
+
const homeDir = process.env.HOME || process.env.USERPROFILE || tmpdir();
|
|
11
|
+
return join(homeDir, ".config");
|
|
12
|
+
}
|
|
13
|
+
export function getAppDir() {
|
|
14
|
+
return join(getConfigDir(), APP_NAME);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Moves the pre-rename data directory into the current one, once.
|
|
18
|
+
*
|
|
19
|
+
* The project was renamed from `claude-mermaid` to `resume-preview`, and the
|
|
20
|
+
* data directory is named after the app. Without this step an existing user's
|
|
21
|
+
* resumes and logs would stay in the old directory and become
|
|
22
|
+
* invisible.
|
|
23
|
+
*
|
|
24
|
+
* Rules:
|
|
25
|
+
* - legacy directory missing → fresh install, nothing to do
|
|
26
|
+
* - current directory missing → the legacy one is renamed to it (fast path)
|
|
27
|
+
* - current directory already exists → the legacy contents are **merged** into
|
|
28
|
+
* it, so neither side is orphaned; files already present in the current
|
|
29
|
+
* directory win
|
|
30
|
+
*
|
|
31
|
+
* A rename can fail on Windows while another process still holds a handle on a
|
|
32
|
+
* watched file inside the legacy directory (the pre-rename server does exactly
|
|
33
|
+
* that). When that happens the migration falls back to copying, which works
|
|
34
|
+
* with open read handles, and the stale directory is removed on a best-effort
|
|
35
|
+
* basis.
|
|
36
|
+
*
|
|
37
|
+
* Never throws: a failed migration is reported and the server keeps running.
|
|
38
|
+
*/
|
|
39
|
+
export async function migrateLegacyAppDir() {
|
|
40
|
+
const current = getAppDir();
|
|
41
|
+
const legacy = join(getConfigDir(), LEGACY_APP_NAME);
|
|
42
|
+
if (current === legacy)
|
|
43
|
+
return;
|
|
44
|
+
try {
|
|
45
|
+
await access(legacy);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// Fresh install — nothing to migrate.
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
let currentExists = true;
|
|
52
|
+
try {
|
|
53
|
+
await access(current);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
currentExists = false;
|
|
57
|
+
}
|
|
58
|
+
if (!currentExists) {
|
|
59
|
+
try {
|
|
60
|
+
await rename(legacy, current);
|
|
61
|
+
console.error(`Migrated data directory: ${legacy} -> ${current}`);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
// Fall through to the copy-based merge below.
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
await cp(legacy, current, { recursive: true, force: false, errorOnExist: false });
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
console.error(`Could not migrate data directory ${legacy} -> ${current}:`, error instanceof Error ? error.message : String(error));
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
// Best effort cleanup: the legacy directory may still be held open by a
|
|
76
|
+
// pre-rename server, in which case it stays behind as a stale copy.
|
|
77
|
+
try {
|
|
78
|
+
await rm(legacy, { recursive: true, force: true });
|
|
79
|
+
console.error(`Migrated data directory: ${legacy} -> ${current}`);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
console.error(`Merged data directory ${legacy} -> ${current}. ` +
|
|
83
|
+
`The old directory could not be removed and can be deleted manually.`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Root directory holding every resume workspace.
|
|
88
|
+
*/
|
|
89
|
+
export function getResumesDir() {
|
|
90
|
+
return join(getAppDir(), DIR_NAMES.RESUMES);
|
|
91
|
+
}
|
|
92
|
+
export function getLogsDir() {
|
|
93
|
+
return join(getAppDir(), DIR_NAMES.LOGS);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Validates that previewId is safe to use in file paths
|
|
97
|
+
* Only allows alphanumeric characters, hyphens, and underscores
|
|
98
|
+
*/
|
|
99
|
+
export function validatePreviewId(previewId) {
|
|
100
|
+
if (!previewId || !PREVIEW_ID_REGEX.test(previewId)) {
|
|
101
|
+
throw new Error("Invalid preview ID format. Only alphanumeric characters, hyphens, and underscores are allowed.");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function validateAllowed(label, value, allowed) {
|
|
105
|
+
if (!allowed.includes(value)) {
|
|
106
|
+
throw new Error(`Invalid ${label}: "${value}". Allowed values: ${allowed.join(", ")}.`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// ===== Resume helpers =====
|
|
110
|
+
export function validateResumeId(resumeId) {
|
|
111
|
+
if (!resumeId || typeof resumeId !== "string") {
|
|
112
|
+
throw new Error("resume_id parameter is required");
|
|
113
|
+
}
|
|
114
|
+
if (resumeId.length > 200) {
|
|
115
|
+
throw new Error("Invalid resume ID: must be at most 200 characters");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
export function validateResumeTemplate(template) {
|
|
119
|
+
validateAllowed("template", template, RESUME_TEMPLATES);
|
|
120
|
+
}
|
|
121
|
+
export function validateResumeTheme(theme) {
|
|
122
|
+
validateAllowed("theme", theme, RESUME_THEMES);
|
|
123
|
+
}
|
|
124
|
+
export function validateResumeFormat(format) {
|
|
125
|
+
validateAllowed("format", format, RESUME_FORMATS);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Checks a locale against the whitelist and returns it narrowed to the
|
|
129
|
+
* supported set. The cast is safe: validateAllowed rejects everything else.
|
|
130
|
+
*/
|
|
131
|
+
export function validateResumeLocale(locale) {
|
|
132
|
+
validateAllowed("locale", locale, SUPPORTED_LOCALES);
|
|
133
|
+
return locale;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Narrows a stored locale to a supported one. Options files written by an
|
|
137
|
+
* older version have no locale at all, and a hand-edited one may carry
|
|
138
|
+
* anything — both fall back to the default instead of failing the load.
|
|
139
|
+
*/
|
|
140
|
+
function toStoredLocale(value) {
|
|
141
|
+
if (typeof value !== "string")
|
|
142
|
+
return DEFAULT_RESUME_LOCALE;
|
|
143
|
+
return SUPPORTED_LOCALES.includes(value)
|
|
144
|
+
? value
|
|
145
|
+
: DEFAULT_RESUME_LOCALE;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Turns a resume id into a name that is safe to use as a directory name and
|
|
149
|
+
* as a URL path segment.
|
|
150
|
+
*
|
|
151
|
+
* Ids that are already safe are returned untouched so the common case stays
|
|
152
|
+
* readable ("frontend-senior" stays "frontend-senior"). Anything else — a
|
|
153
|
+
* Chinese id, for instance — is reduced to its ASCII skeleton plus a short
|
|
154
|
+
* hash of the original, which keeps distinct ids from colliding. The result
|
|
155
|
+
* always satisfies PREVIEW_ID_REGEX, so the path traversal guard holds.
|
|
156
|
+
*/
|
|
157
|
+
export function slugifyResumeId(resumeId) {
|
|
158
|
+
if (PREVIEW_ID_REGEX.test(resumeId)) {
|
|
159
|
+
return resumeId;
|
|
160
|
+
}
|
|
161
|
+
const skeleton = resumeId
|
|
162
|
+
.replace(/[^a-zA-Z0-9_-]+/g, "-")
|
|
163
|
+
.replace(/-{2,}/g, "-")
|
|
164
|
+
.replace(/^-+|-+$/g, "")
|
|
165
|
+
.slice(0, 32);
|
|
166
|
+
const hash = createHash("sha256")
|
|
167
|
+
.update(resumeId, "utf-8")
|
|
168
|
+
.digest("hex")
|
|
169
|
+
.slice(0, RESUME_SLUG_HASH_LENGTH);
|
|
170
|
+
return `${skeleton || "r"}-${hash}`;
|
|
171
|
+
}
|
|
172
|
+
export function getResumeDir(resumeId) {
|
|
173
|
+
const slug = slugifyResumeId(resumeId);
|
|
174
|
+
// The slug is what actually reaches the filesystem, so it must pass the
|
|
175
|
+
// same guard that protects the resume workspace.
|
|
176
|
+
validatePreviewId(slug);
|
|
177
|
+
return join(getResumesDir(), slug);
|
|
178
|
+
}
|
|
179
|
+
const RESUME_FILE_NAMES = {
|
|
180
|
+
content: FILE_NAMES.RESUME_CONTENT,
|
|
181
|
+
options: FILE_NAMES.RESUME_OPTIONS,
|
|
182
|
+
html: FILE_NAMES.RESUME_HTML,
|
|
183
|
+
exportHtml: FILE_NAMES.RESUME_EXPORT_HTML,
|
|
184
|
+
pdf: FILE_NAMES.RESUME_PDF,
|
|
185
|
+
};
|
|
186
|
+
export function getResumeFilePath(resumeId, kind) {
|
|
187
|
+
return join(getResumeDir(resumeId), RESUME_FILE_NAMES[kind]);
|
|
188
|
+
}
|
|
189
|
+
export async function saveResumeContent(resumeId, content) {
|
|
190
|
+
const dir = getResumeDir(resumeId);
|
|
191
|
+
await mkdir(dir, { recursive: true });
|
|
192
|
+
const payload = { ...content, version: content.version ?? RESUME_CONTENT_VERSION };
|
|
193
|
+
await writeFile(getResumeFilePath(resumeId, "content"), JSON.stringify(payload, null, 2), "utf-8");
|
|
194
|
+
}
|
|
195
|
+
export async function loadResumeContent(resumeId) {
|
|
196
|
+
const content = await readFile(getResumeFilePath(resumeId, "content"), "utf-8");
|
|
197
|
+
return JSON.parse(content);
|
|
198
|
+
}
|
|
199
|
+
export async function saveResumeOptions(resumeId, options) {
|
|
200
|
+
const dir = getResumeDir(resumeId);
|
|
201
|
+
await mkdir(dir, { recursive: true });
|
|
202
|
+
await writeFile(getResumeFilePath(resumeId, "options"), JSON.stringify(options, null, 2), "utf-8");
|
|
203
|
+
}
|
|
204
|
+
export async function loadResumeOptions(resumeId) {
|
|
205
|
+
try {
|
|
206
|
+
const content = await readFile(getResumeFilePath(resumeId, "options"), "utf-8");
|
|
207
|
+
const parsed = JSON.parse(content);
|
|
208
|
+
return {
|
|
209
|
+
template: parsed.template ?? RESUME_TEMPLATES[0],
|
|
210
|
+
theme: parsed.theme ?? RESUME_THEMES[0],
|
|
211
|
+
locale: toStoredLocale(parsed.locale),
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
return {
|
|
216
|
+
template: RESUME_TEMPLATES[0],
|
|
217
|
+
theme: RESUME_THEMES[0],
|
|
218
|
+
locale: DEFAULT_RESUME_LOCALE,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Accepts the resume content either as an object or as a JSON string and
|
|
224
|
+
* returns the normalized content model. Only structural problems are
|
|
225
|
+
* rejected — unknown fields are ignored and missing sections are simply
|
|
226
|
+
* omitted by the renderer.
|
|
227
|
+
*/
|
|
228
|
+
export function normalizeResumeContent(input) {
|
|
229
|
+
if (typeof input === "string") {
|
|
230
|
+
const trimmed = input.trim();
|
|
231
|
+
if (!trimmed) {
|
|
232
|
+
throw new Error("content parameter is required");
|
|
233
|
+
}
|
|
234
|
+
if (Buffer.byteLength(trimmed, "utf-8") > RESUME_MAX_CONTENT_BYTES) {
|
|
235
|
+
throw new Error(`Invalid content: Resume content exceeds ${RESUME_MAX_CONTENT_BYTES / 1024}KB limit`);
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
return normalizeResumeContent(JSON.parse(trimmed));
|
|
239
|
+
}
|
|
240
|
+
catch (error) {
|
|
241
|
+
if (error instanceof SyntaxError) {
|
|
242
|
+
throw new Error(`Invalid content: ${error.message}`);
|
|
243
|
+
}
|
|
244
|
+
throw error;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (input === null || typeof input !== "object" || Array.isArray(input)) {
|
|
248
|
+
const actual = input === null ? "null" : Array.isArray(input) ? "array" : typeof input;
|
|
249
|
+
throw new Error(`Invalid content: Expected a resume object, got ${actual}`);
|
|
250
|
+
}
|
|
251
|
+
const serialized = JSON.stringify(input);
|
|
252
|
+
if (Buffer.byteLength(serialized, "utf-8") > RESUME_MAX_CONTENT_BYTES) {
|
|
253
|
+
throw new Error(`Invalid content: Resume content exceeds ${RESUME_MAX_CONTENT_BYTES / 1024}KB limit`);
|
|
254
|
+
}
|
|
255
|
+
return input;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Removes the cached export artifacts of a resume.
|
|
259
|
+
*
|
|
260
|
+
* The export tool caches its rendered artifact inside the workspace and
|
|
261
|
+
* reuses it on the next call, so a cached PDF would keep the old content
|
|
262
|
+
* after the resume has been edited. Every content change invalidates the
|
|
263
|
+
* cache; the artifacts are rendered again on the next export.
|
|
264
|
+
*/
|
|
265
|
+
export async function invalidateResumeArtifacts(resumeId) {
|
|
266
|
+
for (const kind of ["exportHtml", "pdf"]) {
|
|
267
|
+
await rm(getResumeFilePath(resumeId, kind), { force: true });
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Tells whether a cached export artifact is older than the content it was
|
|
272
|
+
* rendered from.
|
|
273
|
+
*
|
|
274
|
+
* Comparing the two modification times catches every way the content can
|
|
275
|
+
* change — a new `resume_preview`, or an edit made outside the MCP tools —
|
|
276
|
+
* without the export path having to trust any bookkeeping elsewhere.
|
|
277
|
+
*
|
|
278
|
+
* An artifact is only reusable when it is *strictly* newer than the content.
|
|
279
|
+
* On a filesystem with a coarse timestamp resolution the two can land in the
|
|
280
|
+
* same tick, and re-rendering is always safe while serving stale content
|
|
281
|
+
* is not.
|
|
282
|
+
*/
|
|
283
|
+
export async function isResumeArtifactStale(resumeId, kind) {
|
|
284
|
+
try {
|
|
285
|
+
const [artifactStat, contentStat] = await Promise.all([
|
|
286
|
+
stat(getResumeFilePath(resumeId, kind)),
|
|
287
|
+
stat(getResumeFilePath(resumeId, "content")),
|
|
288
|
+
]);
|
|
289
|
+
return artifactStat.mtimeMs <= contentStat.mtimeMs;
|
|
290
|
+
}
|
|
291
|
+
catch {
|
|
292
|
+
// No artifact, or no content yet — nothing cached to reuse.
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Removes a resume workspace and everything in it.
|
|
298
|
+
*
|
|
299
|
+
* Uses rm({recursive:true}) rather than an unlink/rmdir dance: rmdir fails on
|
|
300
|
+
* subdirectories and is deprecated in Node.
|
|
301
|
+
*/
|
|
302
|
+
export async function deleteResumeDirectory(resumeId) {
|
|
303
|
+
const dir = getResumeDir(resumeId);
|
|
304
|
+
await rm(dir, { recursive: true, force: true });
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Validates a save path to prevent path traversal and writing to sensitive locations
|
|
308
|
+
* @param savePath - The path where the user wants to save the file
|
|
309
|
+
* @param expectedFormat - When given, the file extension must match this format
|
|
310
|
+
* @throws Error if the path is invalid or dangerous
|
|
311
|
+
*/
|
|
312
|
+
export function validateSavePath(savePath, expectedFormat) {
|
|
313
|
+
// Check for null bytes (potential security issue)
|
|
314
|
+
if (savePath.includes("\0")) {
|
|
315
|
+
throw new Error("Path contains null bytes");
|
|
316
|
+
}
|
|
317
|
+
// Resolve to absolute path
|
|
318
|
+
const absolutePath = resolve(savePath);
|
|
319
|
+
// Prevent writing to sensitive system directories (Unix/Linux/macOS)
|
|
320
|
+
if (process.platform !== "win32" &&
|
|
321
|
+
UNIX_SYSTEM_PATHS.some((danger) => absolutePath.startsWith(danger))) {
|
|
322
|
+
throw new Error("Cannot write to system directories");
|
|
323
|
+
}
|
|
324
|
+
// Prevent writing to Windows system directories
|
|
325
|
+
if (process.platform === "win32") {
|
|
326
|
+
const normalizedPath = absolutePath.replace(/\//g, "\\");
|
|
327
|
+
if (WINDOWS_SYSTEM_PATHS.some((danger) => normalizedPath.startsWith(danger))) {
|
|
328
|
+
throw new Error("Cannot write to system directories");
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
// A resume that lands in a .pdf file holding HTML content is unusable, so
|
|
332
|
+
// the extension has to agree with the requested format.
|
|
333
|
+
if (expectedFormat) {
|
|
334
|
+
const extension = extname(absolutePath).toLowerCase();
|
|
335
|
+
if (extension !== `.${expectedFormat.toLowerCase()}`) {
|
|
336
|
+
throw new Error(`Extension "${extension || ""}" does not match format "${expectedFormat}"`);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
export function getOpenCommand(url) {
|
|
341
|
+
switch (process.platform) {
|
|
342
|
+
case "darwin":
|
|
343
|
+
return { command: "open", args: [url] };
|
|
344
|
+
case "win32":
|
|
345
|
+
return { command: "cmd.exe", args: ["/c", "start", "", url] };
|
|
346
|
+
default:
|
|
347
|
+
return { command: "xdg-open", args: [url] };
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
//# sourceMappingURL=file-utils.js.map
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { spawn } from "child_process";
|
|
2
|
+
import { writeFile, mkdir, copyFile } from "fs/promises";
|
|
3
|
+
import { dirname } from "path";
|
|
4
|
+
import { ensureLiveServer, addLiveSession, hasActiveConnections } from "./live-server.js";
|
|
5
|
+
import { validateSavePath, getOpenCommand, getResumeFilePath, saveResumeContent, saveResumeOptions, loadResumeContent, loadResumeOptions, normalizeResumeContent, validateResumeId, validateResumeTemplate, validateResumeTheme, validateResumeFormat, validateResumeLocale, slugifyResumeId, invalidateResumeArtifacts, isResumeArtifactStale, } from "./file-utils.js";
|
|
6
|
+
import { mcpLogger } from "./logger.js";
|
|
7
|
+
import { renderResumeHtml, renderResumeExportHtml, renderResumePdf } from "./resume-renderer.js";
|
|
8
|
+
import { DEFAULT_RESUME_TEMPLATE, DEFAULT_RESUME_THEME, DEFAULT_RESUME_FORMAT, DEFAULT_RESUME_LOCALE, SESSION_NAMESPACES, } from "./constants.js";
|
|
9
|
+
function errorMessage(error) {
|
|
10
|
+
return error instanceof Error ? error.message : String(error);
|
|
11
|
+
}
|
|
12
|
+
function createErrorResponse(text) {
|
|
13
|
+
return { content: [{ type: "text", text }], isError: true };
|
|
14
|
+
}
|
|
15
|
+
// ===== Resume tools =====
|
|
16
|
+
/**
|
|
17
|
+
* Registers the resume's live preview session and opens the browser the
|
|
18
|
+
* first time it is rendered.
|
|
19
|
+
*/
|
|
20
|
+
async function setupResumeLivePreview(resumeId, slug, htmlPath) {
|
|
21
|
+
const port = await ensureLiveServer();
|
|
22
|
+
const hasConnections = hasActiveConnections(slug, SESSION_NAMESPACES.RESUME);
|
|
23
|
+
await addLiveSession(slug, htmlPath, SESSION_NAMESPACES.RESUME);
|
|
24
|
+
const serverUrl = `http://localhost:${port}/resume/${slug}`;
|
|
25
|
+
if (!hasConnections) {
|
|
26
|
+
mcpLogger.info(`Opening browser for new resume: ${slug}`, { serverUrl });
|
|
27
|
+
const { command, args } = getOpenCommand(serverUrl);
|
|
28
|
+
try {
|
|
29
|
+
const child = spawn(command, args, { detached: true, stdio: "ignore" });
|
|
30
|
+
child.on("error", (error) => {
|
|
31
|
+
mcpLogger.warn("Failed to open browser", { error: error.message, serverUrl });
|
|
32
|
+
});
|
|
33
|
+
child.unref();
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
// On Windows, spawn throws synchronously when the command exists but is
|
|
37
|
+
// not a valid executable — the async "error" event never fires. The
|
|
38
|
+
// resume is already rendered at this point, so failing to open a browser
|
|
39
|
+
// must not fail the call: the client still needs the live URL.
|
|
40
|
+
mcpLogger.warn("Failed to open browser", { error: errorMessage(error), serverUrl });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
mcpLogger.info(`Reusing existing browser tab for resume: ${slug}`);
|
|
45
|
+
}
|
|
46
|
+
return { serverUrl, hasConnections };
|
|
47
|
+
}
|
|
48
|
+
function createResumePreviewResponse(workingFile, serverUrl, hasConnections) {
|
|
49
|
+
const actionMessage = hasConnections
|
|
50
|
+
? "Resume updated successfully."
|
|
51
|
+
: "Resume rendered successfully and opened in browser.";
|
|
52
|
+
const liveMessage = hasConnections
|
|
53
|
+
? "\nResume updated. Browser will refresh automatically."
|
|
54
|
+
: `\nLive reload URL: ${serverUrl}\nThe resume will auto-refresh when you update it.`;
|
|
55
|
+
return {
|
|
56
|
+
content: [
|
|
57
|
+
{
|
|
58
|
+
type: "text",
|
|
59
|
+
text: `${actionMessage}\nWorking file: ${workingFile}\n${liveMessage}`,
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export async function handleResumePreview(args) {
|
|
65
|
+
const resumeId = args.resume_id;
|
|
66
|
+
const providedTemplate = args.template;
|
|
67
|
+
const providedTheme = args.theme;
|
|
68
|
+
const providedLocale = args.locale;
|
|
69
|
+
if (!resumeId) {
|
|
70
|
+
return createErrorResponse("resume_id parameter is required");
|
|
71
|
+
}
|
|
72
|
+
if (args.content === undefined || args.content === null) {
|
|
73
|
+
return createErrorResponse("content parameter is required");
|
|
74
|
+
}
|
|
75
|
+
let content;
|
|
76
|
+
let validatedLocale;
|
|
77
|
+
try {
|
|
78
|
+
validateResumeId(resumeId);
|
|
79
|
+
if (providedTemplate !== undefined)
|
|
80
|
+
validateResumeTemplate(providedTemplate);
|
|
81
|
+
if (providedTheme !== undefined)
|
|
82
|
+
validateResumeTheme(providedTheme);
|
|
83
|
+
if (providedLocale !== undefined)
|
|
84
|
+
validatedLocale = validateResumeLocale(providedLocale);
|
|
85
|
+
content = normalizeResumeContent(args.content);
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
return createErrorResponse(errorMessage(error));
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
const storedOptions = await loadResumeOptions(resumeId);
|
|
92
|
+
const template = providedTemplate ?? storedOptions.template ?? DEFAULT_RESUME_TEMPLATE;
|
|
93
|
+
const theme = providedTheme ?? storedOptions.theme ?? DEFAULT_RESUME_THEME;
|
|
94
|
+
// An omitted locale keeps the language the resume was written in.
|
|
95
|
+
const locale = validatedLocale ?? storedOptions.locale ?? DEFAULT_RESUME_LOCALE;
|
|
96
|
+
await saveResumeContent(resumeId, content);
|
|
97
|
+
await saveResumeOptions(resumeId, { template, theme, locale });
|
|
98
|
+
// The content changed, so any cached export artifact is stale. Dropping
|
|
99
|
+
// it here keeps the next export in sync with what the browser shows.
|
|
100
|
+
await invalidateResumeArtifacts(resumeId);
|
|
101
|
+
const htmlPath = getResumeFilePath(resumeId, "html");
|
|
102
|
+
await writeFile(htmlPath, await renderResumeHtml(content, locale), "utf-8");
|
|
103
|
+
const slug = slugifyResumeId(resumeId);
|
|
104
|
+
const { serverUrl, hasConnections } = await setupResumeLivePreview(resumeId, slug, htmlPath);
|
|
105
|
+
mcpLogger.info(`Resume rendered: ${slug}`, { template, theme, locale });
|
|
106
|
+
return createResumePreviewResponse(getResumeFilePath(resumeId, "content"), serverUrl, hasConnections);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
return createErrorResponse(`Error rendering resume: ${errorMessage(error)}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
export async function handleResumeExport(args) {
|
|
113
|
+
const resumeId = args.resume_id;
|
|
114
|
+
const savePath = args.save_path;
|
|
115
|
+
const format = args.format ?? DEFAULT_RESUME_FORMAT;
|
|
116
|
+
const refresh = args.refresh === true;
|
|
117
|
+
if (!savePath) {
|
|
118
|
+
return createErrorResponse("save_path parameter is required");
|
|
119
|
+
}
|
|
120
|
+
if (!resumeId) {
|
|
121
|
+
return createErrorResponse("resume_id parameter is required");
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
validateResumeId(resumeId);
|
|
125
|
+
validateResumeFormat(format);
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
return createErrorResponse(errorMessage(error));
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
validateSavePath(savePath, format);
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
return createErrorResponse(`Invalid save path: ${errorMessage(error)}`);
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const content = await loadResumeContent(resumeId);
|
|
138
|
+
// The exported document follows the resume's own language, never the
|
|
139
|
+
// language of whoever triggered the export.
|
|
140
|
+
const options = await loadResumeOptions(resumeId);
|
|
141
|
+
const artifactKind = format === "pdf" ? "pdf" : "exportHtml";
|
|
142
|
+
const target = getResumeFilePath(resumeId, artifactKind);
|
|
143
|
+
// The cached artifact is reused only when it is at least as new as the
|
|
144
|
+
// content it was rendered from. Otherwise the export would ship the
|
|
145
|
+
// previous version of the resume.
|
|
146
|
+
const stale = await isResumeArtifactStale(resumeId, artifactKind);
|
|
147
|
+
if (refresh || stale) {
|
|
148
|
+
await invalidateResumeArtifacts(resumeId);
|
|
149
|
+
if (format === "pdf") {
|
|
150
|
+
await writeFile(target, await renderResumePdf(content, options.locale));
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
await writeFile(target, await renderResumeExportHtml(content, options.locale), "utf-8");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const saveDir = dirname(savePath);
|
|
157
|
+
await mkdir(saveDir, { recursive: true });
|
|
158
|
+
await copyFile(target, savePath);
|
|
159
|
+
return {
|
|
160
|
+
content: [
|
|
161
|
+
{
|
|
162
|
+
type: "text",
|
|
163
|
+
text: `Resume saved to: ${savePath} (${format.toUpperCase()})`,
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
return createErrorResponse(`Error saving resume: ${errorMessage(error)}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=handlers.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML Utilities
|
|
3
|
+
* Single Responsibility: HTML escaping shared across the renderer layers.
|
|
4
|
+
*
|
|
5
|
+
* The escaping used to be implemented twice — once in page-renderer.ts and
|
|
6
|
+
* once in live-server.ts. It now lives here so the resume renderer can reuse
|
|
7
|
+
* it instead of adding a third copy.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Escapes HTML special characters to prevent XSS attacks
|
|
11
|
+
*/
|
|
12
|
+
export function escapeHtml(unsafe) {
|
|
13
|
+
return unsafe
|
|
14
|
+
.replace(/&/g, "&")
|
|
15
|
+
.replace(/</g, "<")
|
|
16
|
+
.replace(/>/g, ">")
|
|
17
|
+
.replace(/"/g, """)
|
|
18
|
+
.replace(/'/g, "'");
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=html-utils.js.map
|