fix: windows static file path handling (#3188)

This commit is contained in:
Marvin Hagemeister
2025-08-05 14:56:35 +02:00
committed by GitHub
parent 71b7d922ea
commit ad76097581
2 changed files with 81 additions and 1 deletions
+79
View File
@@ -9,6 +9,7 @@ import {
getStdOutput,
withChildProcessServer,
} from "../../tests/test_utils.tsx";
import { staticFiles } from "../middlewares/static_files.ts";
Deno.test({
name: "Builder - chain onTransformStaticFile",
@@ -388,6 +389,84 @@ Deno.test({
sanitizeResources: false,
});
Deno.test({
name: "Builder - serves static files in subdir",
fn: async () => {
const root = path.join(import.meta.dirname!, "..", "..");
await using _tmp = await withTmpDir({ dir: root, prefix: "tmp_builder_" });
const tmp = _tmp.dir;
const app = new App()
.use(staticFiles())
.get("/", () => new Response("no"));
await writeFiles(tmp, {
"static/foo.txt": "ok",
"static/test/foo.txt": "ok",
});
const builder = new Builder({
root: tmp,
});
const controller = new AbortController();
const waiter = Promise.withResolvers<void>();
await builder.listen(() => Promise.resolve<App<unknown>>(app), {
signal: controller.signal,
async onListen(addr) {
try {
let res = await fetch(`http://localhost:${addr.port}/foo.txt`);
expect(await res.text()).toEqual("ok");
res = await fetch(`http://localhost:${addr.port}/test/foo.txt`);
expect(await res.text()).toEqual("ok");
controller.abort();
waiter.resolve();
} catch (err) {
waiter.reject(err);
}
},
});
await waiter.promise;
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "Builder - serves static files in subdir in prod",
fn: async () => {
const root = path.join(import.meta.dirname!, "..", "..");
await using _tmp = await withTmpDir({ dir: root, prefix: "tmp_builder_" });
const tmp = _tmp.dir;
await writeFiles(tmp, {
"main.ts": `import { App, staticFiles } from "fresh";
export const app = new App()
.use(staticFiles());`,
"static/foo.txt": "ok",
"static/test/foo.txt": "ok",
});
await new Builder({ root: tmp }).build();
await withChildProcessServer(
{ cwd: tmp, args: ["serve", "-A", "_fresh/server.js"] },
async (address) => {
let res = await fetch(`${address}/foo.txt`);
expect(await res.text()).toEqual("ok");
res = await fetch(`${address}/test/foo.txt`);
expect(await res.text()).toEqual("ok");
},
);
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "Builder - dev server error_overlay ignores _app and _layout",
fn: async () => {
+2 -1
View File
@@ -133,7 +133,7 @@ export class MemoryBuildCache<State> implements DevBuildCache<State> {
`Processed file resolved outside of static dir ${file.path}`,
);
}
const pathname = `/${relative}`;
const pathname = new URL(relative, "http://localhost").pathname;
this.addProcessedFile(pathname, file.content, null);
}
@@ -145,6 +145,7 @@ export class MemoryBuildCache<State> implements DevBuildCache<State> {
const filePath = path.join(this.#config.staticDir, pathname);
const relative = path.relative(this.#config.staticDir, filePath);
if (!relative.startsWith(".") && (await Deno.stat(filePath)).isFile) {
const pathname = new URL(relative, "http://localhost").pathname;
this.addUnprocessedFile(pathname, this.#config.staticDir);
return this.readFile(pathname);
}