generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhome.js
More file actions
40 lines (36 loc) · 1.14 KB
/
home.js
File metadata and controls
40 lines (36 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { readFile } from 'node:fs/promises';
import { Importmap, imports, scopes } from '@shgysk8zer0/importmap';
const importmap = new Importmap({ imports, scopes });
await importmap.importLocalPackage();
const integrity = await importmap.getIntegrity();
const NO_MAP = ['controller', 'socket', 'reject', 'signal', 'resolve'];
export default async (req, context) => {
const doc = await readFile(process.cwd() + '/index.html', { encoding: 'utf8' });
const request = JSON.stringify({
url: req.url,
method: req.method,
destination: req.destination,
mode: req.mode,
cache: req.cache,
referrer: req.referrer,
headers: Object.fromEntries(req.headers),
}, null, 4);
return new Response(
doc
.replaceAll('{{ IMPORTMAP }}', importmap)
.replaceAll('{{ INTEGRITY }}', integrity)
.replaceAll('{{ POLYFILLS }}', imports['@shgysk8zer0/polyfills'])
.replaceAll('{{ REQUEST }}', request)
.replaceAll('{{ CONTEXT }}', JSON.stringify(
Object.fromEntries(Object.keys(context)
.filter(key => ! NO_MAP.includes(key))
.map(key => [key, context[key]])
),
null,
4
)),
{
headers: { 'Content-Type': 'text/html' }
}
);
};