some styling cleanup

This commit is contained in:
Nystik
2026-03-17 12:38:30 +01:00
parent c70b9e9d0f
commit 0738c47ac5
27 changed files with 479 additions and 105 deletions

View File

@@ -2,7 +2,10 @@ export function createFsPromises(metadataCache, contentCache, transport) {
return {
async stat(path) {
const cached = metadataCache.toStat(path);
if (cached) return cached;
if (cached) {
return cached;
}
const meta = await transport.stat(path);
metadataCache.set(path, meta);
@@ -16,13 +19,16 @@ export function createFsPromises(metadataCache, contentCache, transport) {
async readdir(path) {
const meta = metadataCache.get(path);
if (meta && meta.type === "file") {
return [];
}
if (!meta && path && path !== "/" && path !== ".") {
const e = new Error(
`ENOENT: no such file or directory, scandir '${path}'`,
);
e.code = "ENOENT";
throw e;
}
@@ -31,7 +37,10 @@ export function createFsPromises(metadataCache, contentCache, transport) {
},
async readFile(path, encoding) {
if (typeof encoding === "object") encoding = encoding?.encoding;
if (typeof encoding === "object") {
encoding = encoding?.encoding;
}
const wantText = encoding === "utf8" || encoding === "utf-8";
const meta = metadataCache.get(path);
@@ -40,6 +49,7 @@ export function createFsPromises(metadataCache, contentCache, transport) {
e.code = "EISDIR";
throw e;
}
if (!meta && path) {
const e = new Error(
`ENOENT: no such file or directory, open '${path}'`,
@@ -49,16 +59,19 @@ export function createFsPromises(metadataCache, contentCache, transport) {
}
const cached = contentCache.get(path);
if (cached !== null) {
if (wantText) {
return typeof cached === "string"
? cached
: new TextDecoder().decode(cached);
}
// binary. ensure we return a proper Uint8Array with .buffer
if (typeof cached === "string") {
return new TextEncoder().encode(cached);
}
return cached;
}
@@ -68,11 +81,15 @@ export function createFsPromises(metadataCache, contentCache, transport) {
},
async writeFile(path, data, encoding) {
if (typeof encoding === "object") encoding = encoding?.encoding;
if (typeof encoding === "object") {
encoding = encoding?.encoding;
}
contentCache.set(path, data);
const size =
typeof data === "string" ? data.length : data.byteLength || 0;
metadataCache.set(path, {
type: "file",
size,
@@ -81,6 +98,7 @@ export function createFsPromises(metadataCache, contentCache, transport) {
});
const result = await transport.writeFile(path, data, encoding);
if (result.mtime) {
metadataCache.set(path, {
type: "file",
@@ -93,6 +111,7 @@ export function createFsPromises(metadataCache, contentCache, transport) {
async appendFile(path, data, encoding) {
contentCache.invalidate(path);
await transport.appendFile(path, data);
const meta = await transport.stat(path);
@@ -102,15 +121,18 @@ export function createFsPromises(metadataCache, contentCache, transport) {
async unlink(path) {
contentCache.delete(path);
metadataCache.delete(path);
await transport.unlink(path);
},
async rename(oldPath, newPath) {
const content = contentCache.get(oldPath);
if (content !== null) {
contentCache.set(newPath, content);
contentCache.delete(oldPath);
}
metadataCache.rename(oldPath, newPath);
await transport.rename(oldPath, newPath);
@@ -119,7 +141,9 @@ export function createFsPromises(metadataCache, contentCache, transport) {
async mkdir(path, options) {
const recursive =
typeof options === "object" ? !!options.recursive : !!options;
metadataCache.set(path, { type: "directory" });
await transport.mkdir(path, recursive);
},
@@ -131,19 +155,25 @@ export function createFsPromises(metadataCache, contentCache, transport) {
async rm(path, options) {
const recursive =
typeof options === "object" ? !!options.recursive : false;
metadataCache.delete(path);
contentCache.delete(path);
await transport.rm(path, recursive);
},
async copyFile(src, dest) {
await transport.copyFile(src, dest);
const meta = await transport.stat(dest);
metadataCache.set(dest, meta);
},
async access(path) {
if (metadataCache.has(path)) return;
if (metadataCache.has(path)) {
return;
}
const e = new Error(
`ENOENT: no such file or directory, access '${path}'`,
);
@@ -152,7 +182,10 @@ export function createFsPromises(metadataCache, contentCache, transport) {
},
async realpath(path) {
if (!path || path === "/" || path === ".") return "/";
if (!path || path === "/" || path === ".") {
return "/";
}
return transport.realpath(path);
},