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
+35 -4
+35 -4
View File
@@ -81,12 +81,43 @@ export class obsidianTypstPDFExport extends Plugin {
*/ */
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'; const typstPath = this.settings.typstPath || 'typst';
const { stdout } = await execAsync(`${typstPath} fonts`);
// 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 || ''}`
}
});
let output = '';
let error = '';
typstProcess.stdout?.on('data', (data) => {
output += data.toString();
});
typstProcess.stderr?.on('data', (data) => {
error += data.toString();
});
typstProcess.on('close', (code) => {
if (code === 0) {
resolve(output);
} else {
reject(new Error(`typst fonts command failed with code ${code}: ${error}`));
}
});
typstProcess.on('error', (err) => {
reject(new Error(`Failed to spawn typst process: ${err.message}`));
});
});
const fonts = stdout const fonts = stdout
.split('\n') .split('\n')