fix: windows static file path handling (#3188)
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
|||||||
getStdOutput,
|
getStdOutput,
|
||||||
withChildProcessServer,
|
withChildProcessServer,
|
||||||
} from "../../tests/test_utils.tsx";
|
} from "../../tests/test_utils.tsx";
|
||||||
|
import { staticFiles } from "../middlewares/static_files.ts";
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "Builder - chain onTransformStaticFile",
|
name: "Builder - chain onTransformStaticFile",
|
||||||
@@ -388,6 +389,84 @@ Deno.test({
|
|||||||
sanitizeResources: false,
|
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({
|
Deno.test({
|
||||||
name: "Builder - dev server error_overlay ignores _app and _layout",
|
name: "Builder - dev server error_overlay ignores _app and _layout",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ export class MemoryBuildCache<State> implements DevBuildCache<State> {
|
|||||||
`Processed file resolved outside of static dir ${file.path}`,
|
`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);
|
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 filePath = path.join(this.#config.staticDir, pathname);
|
||||||
const relative = path.relative(this.#config.staticDir, filePath);
|
const relative = path.relative(this.#config.staticDir, filePath);
|
||||||
if (!relative.startsWith(".") && (await Deno.stat(filePath)).isFile) {
|
if (!relative.startsWith(".") && (await Deno.stat(filePath)).isFile) {
|
||||||
|
const pathname = new URL(relative, "http://localhost").pathname;
|
||||||
this.addUnprocessedFile(pathname, this.#config.staticDir);
|
this.addUnprocessedFile(pathname, this.#config.staticDir);
|
||||||
return this.readFile(pathname);
|
return this.readFile(pathname);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user