fix(gsd): avoid EISDIR crash in file loader
This commit is contained in:
parent
d121c8e3b2
commit
76e7aec0e8
2 changed files with 22 additions and 1 deletions
|
|
@ -590,7 +590,8 @@ export async function loadFile(path: string): Promise<string | null> {
|
|||
try {
|
||||
return await fs.readFile(path, 'utf-8');
|
||||
} catch (err: unknown) {
|
||||
if ((err as NodeJS.ErrnoException).code === 'ENOENT') return null;
|
||||
const code = (err as NodeJS.ErrnoException).code;
|
||||
if (code === 'ENOENT' || code === 'EISDIR') return null;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import fs from "node:fs";
|
||||
|
||||
import { loadFile } from "../files.ts";
|
||||
|
||||
test("loadFile returns null for directory paths instead of throwing EISDIR", async () => {
|
||||
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "gsd-loadfile-eisdir-"));
|
||||
const dirPath = path.join(tmp, "tasks");
|
||||
fs.mkdirSync(dirPath);
|
||||
|
||||
try {
|
||||
const result = await loadFile(dirPath);
|
||||
assert.equal(result, null);
|
||||
} finally {
|
||||
fs.rmSync(tmp, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue