-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (37 loc) · 1.44 KB
/
index.js
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
'use strict';
/**
* Serves a security.txt file
*
* @param {Object} [options]
* @return {Function}
* @api public
*/
module.exports = function(options) {
options = options || {};
if (!options.paths) {
options.paths = ['/security.txt', '/.well-known/security.txt'];
}
if (!options.languages) {
options.languages = ['en'];
}
return (ctx, next) => {
if (!options.paths.includes(ctx.path)) {
return next();
}
if ('GET' !== ctx.method && 'HEAD' !== ctx.method) {
ctx.status = 'OPTIONS' == ctx.method ? 200 : 405;
ctx.set('Allow', 'GET, HEAD, OPTIONS');
} else {
ctx.type = 'text/plain';
let securitytxtBody = '';
if (options.contact) securitytxtBody = securitytxtBody + `Contact: mailto:${options.contact}\n`;
if (options.encryption) securitytxtBody = securitytxtBody + `Encryption: ${options.encryption}\n`;
if (options.acknowledgments) securitytxtBody = securitytxtBody + `Acknowledgments: ${options.acknowledgments}\n`;
if (options.canonical) securitytxtBody = securitytxtBody + `Canonical: ${options.canonical}\n`;
if (options.languages) securitytxtBody = securitytxtBody + `Preferred-Languages: ${options.languages.toString()}\n`;
if (options.policy) securitytxtBody = securitytxtBody + `Policy: ${options.policy}\n`;
if (options.hiring) securitytxtBody = securitytxtBody + `Hiring: ${options.hiring}\n`;
ctx.body = securitytxtBody;
}
};
};