-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheaders.ts
More file actions
75 lines (67 loc) · 1.83 KB
/
Copy pathheaders.ts
File metadata and controls
75 lines (67 loc) · 1.83 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Page } from 'lume/core/file.ts'
import type { Plugin } from 'lume/core/site.ts'
import { merge } from 'lume/core/utils/object.ts'
export interface Options {
/**
* If false, the generated rule will use `/well-known` instead of `/.well-known/`.
* @defaultValue `true`
*/
dotdir: boolean
/**
* Custom headers rule.
* @example
* ```ts
* site.use(headers({
* custom: {
* '/*': [
* 'X-Frame-Options: DENY',
* 'X-XSS-Protection: 1; mode=block',
* ],
* '/templates/index2.html': [
* 'X-Frame-Options: SAMEORIGIN',
* ],
* },
* }))
* ```
*/
custom: Record<string, string[]>
}
export const defaults: Options = {
dotdir: true,
custom: {},
}
export default (userOption: Partial<Options>): Plugin => (site: Lume.Site) => {
const { dotdir, custom } = merge(defaults, userOption)
const headers = new Map<string, string[]>()
// webfinger
headers.set(dotdir ? '/.well-known/webfinger' : '/well-known/webfinger', [
'Content-Type: application/jrd+json',
])
// nodeinfo
headers.set(dotdir ? '/.well-known/nodeinfo' : '/well-known/nodeinfo', [
'Content-Type: application/json',
])
// host-meta
// host-meta.json has a .json extension, so it is skipped.
headers.set(dotdir ? '/.well-known/host-meta' : '/well-known/host-meta', [
'Content-Type: application/xrd+xml',
])
// custom rules
if (custom) {
Object.entries(custom).forEach(([key, values]) => headers.set(key, values))
}
site.addEventListener('beforeRender', ({ pages }) =>
pages.push(
Page.create({
url: '/_headers',
content: Array.from(
headers,
([key, values]) =>
[
key,
...values.map((value) => ' ' + value),
].join('\n'),
).join('\n'),
}),
))
}