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:
1 file changed
+36
-5
@@ -81,12 +81,43 @@ export class obsidianTypstPDFExport extends Plugin {
|
||||
*/
|
||||
private async cacheAvailableFonts(): Promise<void> {
|
||||
try {
|
||||
const { exec } = require('child_process');
|
||||
const { promisify } = require('util');
|
||||
const execAsync = promisify(exec);
|
||||
const { spawn } = require('child_process');
|
||||
|
||||
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
|
||||
.split('\n')
|
||||
@@ -119,7 +150,7 @@ export class obsidianTypstPDFExport extends Plugin {
|
||||
await this.app.vault.adapter.write('.obsidian/plugins/obsidian-typst-pdf-export/fonts-cache.json',
|
||||
JSON.stringify(cacheData, null, 2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached fonts list
|
||||
|
||||
Reference in new issue
Block a user