Enhances font caching with process spawning

Improves the font caching mechanism by using `child_process.spawn` instead of `child_process.exec` for enhanced security and reliability. This change also adds error handling for process spawning failures and includes the ability to set environment variables such as PATH.
This commit is contained in:
AlexKucera committed 2025-09-03 22:31:35 +02:00
1 parent 5fed127ed0
commit fcb62d5025
1 file changed
+65 -34
+65 -34
View File
@@ -80,46 +80,77 @@ export class obsidianTypstPDFExport extends Plugin {
* Cache available fonts from typst to a file for quick access * Cache available fonts from typst to a file for quick access
*/ */
private async cacheAvailableFonts(): Promise<void> { private async cacheAvailableFonts(): Promise<void> {
try { try {
const { exec } = require('child_process'); const { spawn } = require('child_process');
const { promisify } = require('util');
const execAsync = promisify(exec); const typstPath = this.settings.typstPath || 'typst';
// Use spawn instead of exec for security - arguments passed separately
const stdout = await new Promise<string>((resolve, reject) => {
const typstProcess = spawn(typstPath, ['fonts'], {
stdio: ['pipe', 'pipe', 'pipe'],
env: {
...process.env,
PATH: `/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:${process.env.PATH || ''}`
}
});
const typstPath = this.settings.typstPath || 'typst'; let output = '';
const { stdout } = await execAsync(`${typstPath} fonts`); let error = '';
const fonts = stdout typstProcess.stdout?.on('data', (data) => {
.split('\n') output += data.toString();
.map((line: string) => line.trim()) });
.filter((line: string) => line.length > 0)
.sort();
// Write fonts to cache file in plugin data directory typstProcess.stderr?.on('data', (data) => {
const cacheData = { error += data.toString();
fonts: fonts, });
timestamp: Date.now(),
typstPath: typstPath
};
await this.app.vault.adapter.write('.obsidian/plugins/obsidian-typst-pdf-export/fonts-cache.json', typstProcess.on('close', (code) => {
JSON.stringify(cacheData, null, 2)); if (code === 0) {
console.log('Cached', fonts.length, 'fonts from typst'); resolve(output);
} catch (error) { } else {
console.error('Failed to cache fonts from typst:', error); reject(new Error(`typst fonts command failed with code ${code}: ${error}`));
// Create fallback cache file }
const fallbackFonts = FALLBACK_FONTS; });
const cacheData = { typstProcess.on('error', (err) => {
fonts: fallbackFonts, reject(new Error(`Failed to spawn typst process: ${err.message}`));
timestamp: Date.now(), });
typstPath: 'fallback', });
error: error.message
}; const fonts = stdout
.split('\n')
await this.app.vault.adapter.write('.obsidian/plugins/obsidian-typst-pdf-export/fonts-cache.json', .map((line: string) => line.trim())
JSON.stringify(cacheData, null, 2)); .filter((line: string) => line.length > 0)
} .sort();
// Write fonts to cache file in plugin data directory
const cacheData = {
fonts: fonts,
timestamp: Date.now(),
typstPath: typstPath
};
await this.app.vault.adapter.write('.obsidian/plugins/obsidian-typst-pdf-export/fonts-cache.json',
JSON.stringify(cacheData, null, 2));
console.log('Cached', fonts.length, 'fonts from typst');
} catch (error) {
console.error('Failed to cache fonts from typst:', error);
// Create fallback cache file
const fallbackFonts = FALLBACK_FONTS;
const cacheData = {
fonts: fallbackFonts,
timestamp: Date.now(),
typstPath: 'fallback',
error: error.message
};
await this.app.vault.adapter.write('.obsidian/plugins/obsidian-typst-pdf-export/fonts-cache.json',
JSON.stringify(cacheData, null, 2));
} }
}
/** /**
* Get cached fonts list * Get cached fonts list