Skip to content

Commit b579564

Browse files
authored
Support static paths with spaces (#14)
1 parent 18efd6f commit b579564

4 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { type DeployConfig } from "@deno/experimental-route-config";
12
import type { Adapter } from "@sveltejs/kit";
2-
import path from "node:path";
33
import fsp from "node:fs/promises";
4-
import { type DeployConfig } from "@deno/experimental-route-config";
4+
import path from "node:path";
55

66
const OUT_DIR = ".deno-deploy";
77

@@ -111,8 +111,9 @@ export default function denoAdapter(): Adapter {
111111
await walk(assetDir, assets);
112112
for (const asset of assets) {
113113
const rel = path.relative(assetDir, asset);
114+
const encodedRel = encodeAssetRelativePath(rel);
114115
staticFiles.push({
115-
source: `/${rel.replace(/\\+/, "/")}`,
116+
source: `/${encodedRel}`,
116117
destination: path.join(dirs.static, rel),
117118
});
118119
}
@@ -162,3 +163,11 @@ async function walk(dir: string, result: string[]): Promise<void> {
162163
}
163164
}
164165
}
166+
167+
export function encodeAssetRelativePath(rel: string): string {
168+
return rel
169+
.replace(/\\+/g, "/")
170+
.split("/")
171+
.map(encodeURIComponent)
172+
.join("/");
173+
}

test/adapter.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ Deno.test("Adapter - serve static files from root", async () => {
9393
});
9494
});
9595

96+
Deno.test("Adapter - serve static files with space in path (URL-encoded)", async () => {
97+
await withServer(async (origin) => {
98+
const res = await fetch(`${origin}/foo%20bar/baz.txt`);
99+
expect(res.status).toEqual(200);
100+
expect(await res.text()).toContain("quux");
101+
});
102+
});
103+
96104
Deno.test("Adapter - serve static files with cache headers", async () => {
97105
const immutableDir = path.join(
98106
cwd,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
quux

test/utils.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect } from "expect";
2+
import { encodeAssetRelativePath } from "../src/index.ts";
3+
4+
Deno.test("Utils - encodeAssetRelativePath encodes path segments", () => {
5+
expect(encodeAssetRelativePath("foo bar/baz qux.txt")).toEqual(
6+
"foo%20bar/baz%20qux.txt",
7+
);
8+
});
9+
10+
Deno.test("Utils - encodeAssetRelativePath normalizes Windows separators before URL-encoding", () => {
11+
expect(encodeAssetRelativePath("foo bar\\baz\\qux.txt")).toEqual(
12+
"foo%20bar/baz/qux.txt",
13+
);
14+
expect(encodeAssetRelativePath("a\\b\\c.txt")).toEqual("a/b/c.txt");
15+
});

0 commit comments

Comments
 (0)