-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.config.js
More file actions
56 lines (52 loc) · 2.3 KB
/
http.config.js
File metadata and controls
56 lines (52 loc) · 2.3 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import '@shgysk8zer0/polyfills';
import { Importmap } from '@shgysk8zer0/importmap';
const importmap = new Importmap();
await importmap.importLocalPackage();
export default {
open: true,
routes: {
'/': async (req) => {
if (req.destination === 'document') {
// Hack to make it not read-only
// const importmap = await import('@shgysk8zer0/importmap').then(mod => ({ ...mod.importmap }));
const { readFile } = await import('node:fs/promises');
// importmap.imports['@aegisjsproject/markdown'] = '/markdown.min.js';
// importmap.imports['@aegisjsproject/markdown/'] = '/';
const json = JSON.stringify(importmap);
const integrity = await importmap.getIntegrity();
const doc = await readFile('./test/index.html', { encoding: 'utf-8' })
.then(doc => doc.replace('{{ importmap }}', json).replace('{{ integrity }}', integrity));
const csp = `default-src 'none';
script-src 'self' https://unpkg.com/@shgysk8zer0/ https://unpkg.com/@aegisjsproject/ https://unpkg.com/@highlightjs/ ${importmap.resolve('marked')} ${importmap.resolve('marked-highlight')} '${integrity}';
style-src 'self' blob: https://unpkg.com/@highlightjs/;
img-src 'self' https://img.shields.io/ https://github.com/AegisJSProject/markdown/workflows/ https://github.com/AegisJSProject/markdown/actions/workflows/ https://i.imgur.com/;
connect-src 'self';
trusted-types empty#html empty#script aegis-sanitizer#html;
require-trusted-types-for 'script'`;
return new Response(doc, {
headers: {
'Content-Type': 'text/html',
'Content-Security-Policy': csp.replaceAll(/[\n\t]/g, ' '),
}
});
} else {
return new Response(req.destination, {
headers: { 'Content-Type': 'text/plain' },
});
}
}
},
responsePostprocessors: [
resp => {
const types = ['text/plain', 'text/css', 'application/javascript', 'application/json', 'text/css', 'image/svg+xml', 'text/markdown'];
if (types.includes(resp.headers.get('Content-Type'))) {
resp.headers.set('Content-Encoding', 'deflate');
return new CompressionStream('deflate');
} else if (resp.headers.get('Content-Type') === 'application/octet-stream') {
resp.headers.set('Content-Type', 'text/markdown');
resp.headers.set('Content-Encoding', 'deflate');
return new CompressionStream('deflate');
}
}
]
};