-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.js
92 lines (80 loc) · 1.93 KB
/
helpers.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const path = require('path');
const fs = require('fs');
const csp = require('helmet-csp');
const isDev = process.env.NODE_ENV !== 'production';
const directives = {
// blockAllMixedContent: true,
// upgradeInsecureRequests: true,
defaultSrc: ["'self'"],
styleSrc: [
"'self'",
'fonts.googleapis.com',
'a.disquscdn.com',
],
scriptSrc: [
"'self'",
"'unsafe-eval'",
"'unsafe-inline'",
'cdn.polyfill.io',
'cdn.bootcss.com',
'atalktome.disqus.com',
'a.disquscdn.com',
],
imgSrc: [
"'self'",
'referrer.disqus.com',
],
fontSrc: [
"'self'",
'fonts.gstatic.com',
],
connectSrc: [
"'self'",
'ws:',
'disqus.com/api',
'links.services.disqus.com/api',
],
frameSrc: [
"'self'",
'disqus.com',
],
};
if (isDev) {
directives.styleSrc.push("'unsafe-inline'");
directives.styleSrc.push('blob:');
}
exports.setCsp = csp({
directives,
});
exports.setSecurityHeaders = (req, res, next) => {
res.set({
'X-Content-Type-Options': 'nosniff',
'X-Frame-Options': 'DENY',
'X-XSS-Protection': '1; mode=block',
});
return next();
};
const getData = (filename) => {
const filePath = path.resolve('db', filename);
return JSON.parse(fs.readFileSync(filePath));
};
const talks = getData('talks.json');
const posts = getData('posts.json');
exports.getRandomTalk = () => {
const randomIndex = Math.ceil(Math.random() * talks.length);
return talks[randomIndex];
};
exports.getPosts = (tag, page) => {
const PER_PAGE = 5;
const startIndex = PER_PAGE * (page - 1);
const endIndex = startIndex + PER_PAGE;
const postsByTag = posts.filter(post => tag === 'all' || post.tags.includes(tag));
const postsByPage = postsByTag
.slice(startIndex, endIndex)
.map(({ name, title, date }) => ({ name, title, date }));
return ({
posts: postsByPage,
total: postsByTag.length,
});
};
exports.getPost = name => posts.find(post => post.name === name);