@just-be/wildcard 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/handlers/static.ts +32 -7
package/package.json
CHANGED
package/src/handlers/static.ts
CHANGED
|
@@ -17,30 +17,55 @@ export const handleStatic: Handler<StaticConfig, { fileLoader: FileLoader }> = a
|
|
|
17
17
|
const basePath = subdomain;
|
|
18
18
|
|
|
19
19
|
return spa
|
|
20
|
-
? handleSpaMode(fileLoader, basePath)
|
|
20
|
+
? handleSpaMode(fileLoader, basePath, url.pathname)
|
|
21
21
|
: handleStaticMode(fileLoader, basePath, url.pathname, fallback);
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
-
async function handleSpaMode(
|
|
25
|
-
|
|
24
|
+
async function handleSpaMode(
|
|
25
|
+
fileLoader: FileLoader,
|
|
26
|
+
basePath: string,
|
|
27
|
+
pathname: string
|
|
28
|
+
): Promise<Response> {
|
|
29
|
+
// Sanitize paths to prevent directory traversal
|
|
26
30
|
const sanitizedBase = sanitizePath(basePath);
|
|
31
|
+
const sanitizedPathname = sanitizePath(pathname);
|
|
32
|
+
|
|
27
33
|
if (!sanitizedBase) {
|
|
28
34
|
return new Response("Invalid path", { status: 400 });
|
|
29
35
|
}
|
|
30
36
|
|
|
31
|
-
|
|
37
|
+
// First, try to serve the requested file if it exists
|
|
38
|
+
// Only check files that likely exist (have extensions or end with /)
|
|
39
|
+
if (pathname !== "/" && (pathname.includes(".") || pathname.endsWith("/"))) {
|
|
40
|
+
const fullPath = `${sanitizedBase}/${sanitizedPathname}`;
|
|
41
|
+
let key = fullPath.replace(/\/+/g, "/"); // Normalize double slashes
|
|
42
|
+
|
|
43
|
+
// Append index.html for directories
|
|
44
|
+
if (key.endsWith("/")) {
|
|
45
|
+
key = `${key}index.html`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const fileObject = await fileLoader.loadFile(key);
|
|
49
|
+
|
|
50
|
+
if (fileObject) {
|
|
51
|
+
return buildFileResponse(fileObject, key);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Fall back to index.html for SPA routing
|
|
56
|
+
const indexKey = sanitizedBase.endsWith("index.html")
|
|
32
57
|
? sanitizedBase
|
|
33
58
|
: sanitizedBase.endsWith("/")
|
|
34
59
|
? `${sanitizedBase}index.html`
|
|
35
60
|
: `${sanitizedBase}/index.html`;
|
|
36
61
|
|
|
37
|
-
const
|
|
62
|
+
const indexFile = await fileLoader.loadFile(indexKey);
|
|
38
63
|
|
|
39
|
-
if (!
|
|
64
|
+
if (!indexFile) {
|
|
40
65
|
return new Response("File not found", { status: 404 });
|
|
41
66
|
}
|
|
42
67
|
|
|
43
|
-
return buildFileResponse(
|
|
68
|
+
return buildFileResponse(indexFile, indexKey);
|
|
44
69
|
}
|
|
45
70
|
|
|
46
71
|
async function handleStaticMode(
|