@aklinker1/aframe 0.2.4 → 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/README.md +6 -0
- package/package.json +1 -1
- package/src/client/server.ts +55 -38
package/README.md
CHANGED
|
@@ -13,6 +13,7 @@ Simple wrapper around Vite for creating pre-rendered, client-side web apps with
|
|
|
13
13
|
📁 public/
|
|
14
14
|
📄 favicon.ico
|
|
15
15
|
📁 server/
|
|
16
|
+
📄 env.d.ts
|
|
16
17
|
📄 main.ts
|
|
17
18
|
📄 .env
|
|
18
19
|
📄 aframe.config.ts
|
|
@@ -34,6 +35,11 @@ export default defineConfig({
|
|
|
34
35
|
});
|
|
35
36
|
```
|
|
36
37
|
|
|
38
|
+
```ts
|
|
39
|
+
// server/env.d.ts
|
|
40
|
+
/// <reference types="@aklinker1/aframe/env" />
|
|
41
|
+
```
|
|
42
|
+
|
|
37
43
|
```ts
|
|
38
44
|
// server/main.ts
|
|
39
45
|
import { Elysia } from "elysia";
|
package/package.json
CHANGED
package/src/client/server.ts
CHANGED
|
@@ -8,53 +8,70 @@ export interface AframeServer {
|
|
|
8
8
|
listen(port: number): void | never;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
const cache: Record<string, BunFile> = {};
|
|
12
|
-
|
|
13
11
|
/**
|
|
14
12
|
* Fetches a file from the `public` directory.
|
|
15
13
|
*/
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
export function fetchStatic(options?: {
|
|
15
|
+
/** Override the fetch behavior of a file. */
|
|
16
|
+
onFetch?: (
|
|
17
|
+
path: string,
|
|
18
|
+
file: BunFile,
|
|
19
|
+
) => Promise<Response | undefined> | Response | undefined;
|
|
20
|
+
}): (request: Request) => Promise<Response> {
|
|
21
|
+
return async (request) => {
|
|
22
|
+
const path = new URL(request.url).pathname.replace(/\/+$/, "");
|
|
19
23
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
`${import.meta.publicDir}${path}/index.html`,
|
|
25
|
-
];
|
|
24
|
+
const paths = [
|
|
25
|
+
`${import.meta.publicDir}${path}`,
|
|
26
|
+
`${import.meta.publicDir}${path}/index.html`,
|
|
27
|
+
];
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
)
|
|
33
|
-
|
|
29
|
+
// Only fallback on the root HTML file when building application
|
|
30
|
+
if (import.meta.command === "build") {
|
|
31
|
+
paths.push(`${import.meta.publicDir}/index.html`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
for (const path of paths) {
|
|
35
|
+
const isHtml = path.includes(".html");
|
|
36
|
+
const gzFile = Bun.file(path + ".gz");
|
|
37
|
+
const file = Bun.file(path);
|
|
34
38
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
if (await isFile(gzFile)) {
|
|
40
|
+
const customResponse = await options?.onFetch?.(path, file);
|
|
41
|
+
if (customResponse) return customResponse;
|
|
42
|
+
return new Response(gzFile.stream(), {
|
|
43
|
+
headers: {
|
|
44
|
+
...(isHtml ? {} : headers),
|
|
45
|
+
"content-type": file.type,
|
|
46
|
+
"content-encoding": "gzip",
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (await isFile(file)) {
|
|
52
|
+
const customResponse = await options?.onFetch?.(path, file);
|
|
53
|
+
if (customResponse) return customResponse;
|
|
54
|
+
|
|
55
|
+
return new Response(file.stream(), { headers });
|
|
56
|
+
}
|
|
40
57
|
}
|
|
41
|
-
}
|
|
42
58
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
59
|
+
return new Response(
|
|
60
|
+
`<html>
|
|
61
|
+
<body>
|
|
62
|
+
This is a placeholder for your root <code>index.html</code> file during development.
|
|
63
|
+
<br/>
|
|
64
|
+
In production (or via the app's dev server), this path will fallback on the root <code>index.html</code>.
|
|
65
|
+
</body>
|
|
66
|
+
</html>`,
|
|
67
|
+
{
|
|
68
|
+
headers: {
|
|
69
|
+
"Content-Type": "text/html",
|
|
70
|
+
...headers,
|
|
71
|
+
},
|
|
55
72
|
},
|
|
56
|
-
|
|
57
|
-
|
|
73
|
+
);
|
|
74
|
+
};
|
|
58
75
|
}
|
|
59
76
|
|
|
60
77
|
async function isFile(file: BunFile): Promise<boolean> {
|