+80
-15
@@ -1,41 +1,106 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
'plugins': ['jest'],
|
plugins: [ 'jest' ],
|
||||||
'env': {
|
env: {
|
||||||
'commonjs': true,
|
'commonjs': true,
|
||||||
'es2021': true,
|
'es2021': true,
|
||||||
'node': true,
|
'node': true,
|
||||||
'jest/globals': true
|
'jest/globals': true,
|
||||||
},
|
},
|
||||||
'extends': ['eslint:recommended', 'plugin:jest/recommended'],
|
extends: [
|
||||||
'parserOptions': {
|
'eslint:recommended',
|
||||||
'ecmaVersion': 12
|
'plugin:jest/recommended',
|
||||||
|
],
|
||||||
|
parserOptions: {
|
||||||
|
ecmaVersion: 12,
|
||||||
},
|
},
|
||||||
'rules': {
|
rules: {
|
||||||
'indent': [
|
'indent': [
|
||||||
'error',
|
'error',
|
||||||
4
|
4,
|
||||||
],
|
],
|
||||||
'linebreak-style': [
|
'linebreak-style': [
|
||||||
'error',
|
'error',
|
||||||
'unix'
|
'unix',
|
||||||
],
|
],
|
||||||
'quotes': [
|
'quotes': [
|
||||||
'error',
|
'error',
|
||||||
'single'
|
'single',
|
||||||
],
|
],
|
||||||
'semi': [
|
'semi': [
|
||||||
'error',
|
'error',
|
||||||
'always'
|
'always',
|
||||||
],
|
],
|
||||||
'curly': [
|
'curly': [
|
||||||
'error',
|
'error',
|
||||||
'all'
|
'all',
|
||||||
],
|
],
|
||||||
'brace-style': [
|
'brace-style': [
|
||||||
'error',
|
'error',
|
||||||
'1tbs'
|
'1tbs',
|
||||||
],
|
],
|
||||||
'jest/no-done-callback': 'off',
|
'jest/no-done-callback': 'off',
|
||||||
'jest/expect-expect': 'off'
|
'jest/expect-expect': 'off',
|
||||||
}
|
'comma-dangle': [
|
||||||
|
'error',
|
||||||
|
'always-multiline',
|
||||||
|
],
|
||||||
|
'complexity': 'error',
|
||||||
|
'consistent-return': 'error',
|
||||||
|
'dot-location': [
|
||||||
|
'error',
|
||||||
|
'property',
|
||||||
|
],
|
||||||
|
'eqeqeq': [
|
||||||
|
'error',
|
||||||
|
'always',
|
||||||
|
{ null: 'ignore' },
|
||||||
|
],
|
||||||
|
'no-empty-function': 'error',
|
||||||
|
'no-floating-decimal': 'error',
|
||||||
|
'no-multi-spaces': 'error',
|
||||||
|
'camelcase': [
|
||||||
|
'error',
|
||||||
|
{ properties: 'never' },
|
||||||
|
],
|
||||||
|
'comma-spacing': [
|
||||||
|
'error',
|
||||||
|
{ before: false, after: true },
|
||||||
|
],
|
||||||
|
'array-bracket-newline': [
|
||||||
|
'error',
|
||||||
|
{ multiline: true },
|
||||||
|
],
|
||||||
|
'array-element-newline': [
|
||||||
|
'error',
|
||||||
|
{ multiline: true, minItems: 2 },
|
||||||
|
],
|
||||||
|
'array-bracket-spacing': [
|
||||||
|
'error',
|
||||||
|
'always',
|
||||||
|
],
|
||||||
|
'object-curly-spacing': [
|
||||||
|
'error',
|
||||||
|
'always',
|
||||||
|
],
|
||||||
|
'comma-style': 'error',
|
||||||
|
'computed-property-spacing': 'error',
|
||||||
|
'eol-last': 'error',
|
||||||
|
'func-call-spacing': 'error',
|
||||||
|
'key-spacing': 'error',
|
||||||
|
'keyword-spacing': 'error',
|
||||||
|
'multiline-comment-style': 'error',
|
||||||
|
'newline-per-chained-call': 'error',
|
||||||
|
'no-lonely-if': 'error',
|
||||||
|
'no-multiple-empty-lines': 'error',
|
||||||
|
'no-trailing-spaces': 'error',
|
||||||
|
'no-unneeded-ternary': 'error',
|
||||||
|
'no-whitespace-before-property': 'error',
|
||||||
|
'operator-assignment': 'error',
|
||||||
|
'quote-props': [
|
||||||
|
'error',
|
||||||
|
'consistent-as-needed',
|
||||||
|
],
|
||||||
|
'space-before-blocks': 'error',
|
||||||
|
'space-infix-ops': 'error',
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -282,6 +282,8 @@ Any URL like `/year/month/day/anything/` will redirect to this article (and link
|
|||||||
activate PlantUML diagram rendering
|
activate PlantUML diagram rendering
|
||||||
* `fa-diagrams` (default: true)
|
* `fa-diagrams` (default: true)
|
||||||
activate fa-diagrams rendering
|
activate fa-diagrams rendering
|
||||||
|
* `hit_counter` (default: true)
|
||||||
|
activate /stats endpoints and visitor counting (need an active redis connection)
|
||||||
* `home`
|
* `home`
|
||||||
* `title` (default: GitBlog.md)
|
* `title` (default: GitBlog.md)
|
||||||
the title of your blog, **strongly advised to be changed**
|
the title of your blog, **strongly advised to be changed**
|
||||||
@@ -332,4 +334,11 @@ Any URL like `/year/month/day/anything/` will redirect to this article (and link
|
|||||||
specify the output format between svg, html or MathMl (mml)
|
specify the output format between svg, html or MathMl (mml)
|
||||||
* `speak_text`: (default: true)
|
* `speak_text`: (default: true)
|
||||||
activate the alternate text in equations
|
activate the alternate text in equations
|
||||||
|
* `hit_counter`
|
||||||
|
* `unique_visitor_timeout`: (default: 7200000 / 2h)
|
||||||
|
specify the time (in ms) before a visitor can be accounted again
|
||||||
|
* `redis`
|
||||||
|
Options to connect to redis (see [redis options](https://github.com/NodeRedis/node-redis#options-object-properties) for more info)
|
||||||
|
* `host`: (default: localhost)
|
||||||
|
* `port`: (default: 6379)
|
||||||
|
|
||||||
|
|||||||
Generated
+85
@@ -19,6 +19,7 @@
|
|||||||
"ncp": "^2.0.0",
|
"ncp": "^2.0.0",
|
||||||
"node-prismjs": "^0.1.0",
|
"node-prismjs": "^0.1.0",
|
||||||
"prismjs": "^1.23.0",
|
"prismjs": "^1.23.0",
|
||||||
|
"redis": "^3.0.2",
|
||||||
"rss": "^1.2.2",
|
"rss": "^1.2.2",
|
||||||
"showdown": "^1.9.1"
|
"showdown": "^1.9.1"
|
||||||
},
|
},
|
||||||
@@ -1897,6 +1898,14 @@
|
|||||||
"integrity": "sha512-IofjkYBZaZivn0V8nnsMJGBr4jVLxHDheKSW88PyxS5QC4Vo9ZbZVvhzlSxY87fVq3STR6r+4cGepyHkcWOQSw==",
|
"integrity": "sha512-IofjkYBZaZivn0V8nnsMJGBr4jVLxHDheKSW88PyxS5QC4Vo9ZbZVvhzlSxY87fVq3STR6r+4cGepyHkcWOQSw==",
|
||||||
"optional": true
|
"optional": true
|
||||||
},
|
},
|
||||||
|
"node_modules/denque": {
|
||||||
|
"version": "1.5.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/denque/-/denque-1.5.0.tgz",
|
||||||
|
"integrity": "sha512-CYiCSgIF1p6EUByQPlGkKnP1M9g0ZV3qMIrqMqZqdwazygIA/YP2vrbcyl1h/WppKJTdl1F85cXIle+394iDAQ==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.10"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/depd": {
|
"node_modules/depd": {
|
||||||
"version": "1.1.2",
|
"version": "1.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz",
|
||||||
@@ -6966,6 +6975,48 @@
|
|||||||
"node": ">=4"
|
"node": ">=4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/redis": {
|
||||||
|
"version": "3.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/redis/-/redis-3.0.2.tgz",
|
||||||
|
"integrity": "sha512-PNhLCrjU6vKVuMOyFu7oSP296mwBkcE6lrAjruBYG5LgdSqtRBoVQIylrMyVZD/lkF24RSNNatzvYag6HRBHjQ==",
|
||||||
|
"dependencies": {
|
||||||
|
"denque": "^1.4.1",
|
||||||
|
"redis-commands": "^1.5.0",
|
||||||
|
"redis-errors": "^1.2.0",
|
||||||
|
"redis-parser": "^3.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "opencollective",
|
||||||
|
"url": "https://opencollective.com/node-redis"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/redis-commands": {
|
||||||
|
"version": "1.7.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz",
|
||||||
|
"integrity": "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ=="
|
||||||
|
},
|
||||||
|
"node_modules/redis-errors": {
|
||||||
|
"version": "1.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz",
|
||||||
|
"integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/redis-parser": {
|
||||||
|
"version": "3.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz",
|
||||||
|
"integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=",
|
||||||
|
"dependencies": {
|
||||||
|
"redis-errors": "^1.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/regex-not": {
|
"node_modules/regex-not": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/regex-not/-/regex-not-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/regex-not/-/regex-not-1.0.2.tgz",
|
||||||
@@ -10365,6 +10416,11 @@
|
|||||||
"integrity": "sha512-IofjkYBZaZivn0V8nnsMJGBr4jVLxHDheKSW88PyxS5QC4Vo9ZbZVvhzlSxY87fVq3STR6r+4cGepyHkcWOQSw==",
|
"integrity": "sha512-IofjkYBZaZivn0V8nnsMJGBr4jVLxHDheKSW88PyxS5QC4Vo9ZbZVvhzlSxY87fVq3STR6r+4cGepyHkcWOQSw==",
|
||||||
"optional": true
|
"optional": true
|
||||||
},
|
},
|
||||||
|
"denque": {
|
||||||
|
"version": "1.5.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/denque/-/denque-1.5.0.tgz",
|
||||||
|
"integrity": "sha512-CYiCSgIF1p6EUByQPlGkKnP1M9g0ZV3qMIrqMqZqdwazygIA/YP2vrbcyl1h/WppKJTdl1F85cXIle+394iDAQ=="
|
||||||
|
},
|
||||||
"depd": {
|
"depd": {
|
||||||
"version": "1.1.2",
|
"version": "1.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz",
|
||||||
@@ -14305,6 +14361,35 @@
|
|||||||
"util.promisify": "^1.0.0"
|
"util.promisify": "^1.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"redis": {
|
||||||
|
"version": "3.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/redis/-/redis-3.0.2.tgz",
|
||||||
|
"integrity": "sha512-PNhLCrjU6vKVuMOyFu7oSP296mwBkcE6lrAjruBYG5LgdSqtRBoVQIylrMyVZD/lkF24RSNNatzvYag6HRBHjQ==",
|
||||||
|
"requires": {
|
||||||
|
"denque": "^1.4.1",
|
||||||
|
"redis-commands": "^1.5.0",
|
||||||
|
"redis-errors": "^1.2.0",
|
||||||
|
"redis-parser": "^3.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"redis-commands": {
|
||||||
|
"version": "1.7.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz",
|
||||||
|
"integrity": "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ=="
|
||||||
|
},
|
||||||
|
"redis-errors": {
|
||||||
|
"version": "1.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz",
|
||||||
|
"integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60="
|
||||||
|
},
|
||||||
|
"redis-parser": {
|
||||||
|
"version": "3.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz",
|
||||||
|
"integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=",
|
||||||
|
"requires": {
|
||||||
|
"redis-errors": "^1.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"regex-not": {
|
"regex-not": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/regex-not/-/regex-not-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/regex-not/-/regex-not-1.0.2.tgz",
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
"ncp": "^2.0.0",
|
"ncp": "^2.0.0",
|
||||||
"node-prismjs": "^0.1.0",
|
"node-prismjs": "^0.1.0",
|
||||||
"prismjs": "^1.23.0",
|
"prismjs": "^1.23.0",
|
||||||
|
"redis": "^3.0.2",
|
||||||
"rss": "^1.2.2",
|
"rss": "^1.2.2",
|
||||||
"showdown": "^1.9.1"
|
"showdown": "^1.9.1"
|
||||||
},
|
},
|
||||||
|
|||||||
+66
-21
@@ -51,6 +51,16 @@ module.exports = (config) => {
|
|||||||
let showError;
|
let showError;
|
||||||
const fw = require('./file_walker')(config);
|
const fw = require('./file_walker')(config);
|
||||||
const renderer = require('./renderer')(config);
|
const renderer = require('./renderer')(config);
|
||||||
|
const hc = require('./hit_counter')(config,
|
||||||
|
() => {
|
||||||
|
console.log(cons.ok, 'redis connected');
|
||||||
|
},
|
||||||
|
(err) => {
|
||||||
|
if (err.code !== 'ECONNREFUSED') {
|
||||||
|
console.log(cons.warn, 'redis error: ' + err);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// set view engine from configuration
|
// set view engine from configuration
|
||||||
app.set('view engine', config['view_engine']);
|
app.set('view engine', config['view_engine']);
|
||||||
@@ -65,8 +75,8 @@ module.exports = (config) => {
|
|||||||
fw.fetchArticles((err, dict) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(cons.error, 'error loading articles : ' + err);
|
console.error(cons.error, 'error loading articles : ' + err);
|
||||||
return error ? error() : null;
|
error();
|
||||||
}
|
} else {
|
||||||
Object.keys(articles).forEach((key) => delete articles[key]);
|
Object.keys(articles).forEach((key) => delete articles[key]);
|
||||||
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
|
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
|
||||||
const nb = Object.keys(articles).length;
|
const nb = Object.keys(articles).length;
|
||||||
@@ -80,6 +90,7 @@ module.exports = (config) => {
|
|||||||
lastRSS = '';
|
lastRSS = '';
|
||||||
|
|
||||||
success();
|
success();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
if (config['test']) {
|
if (config['test']) {
|
||||||
@@ -93,7 +104,7 @@ module.exports = (config) => {
|
|||||||
host: host,
|
host: host,
|
||||||
version: pjson.version,
|
version: pjson.version,
|
||||||
request: req,
|
request: req,
|
||||||
config: config
|
config: config,
|
||||||
};
|
};
|
||||||
res.render(vPath, data, (err, html) => {
|
res.render(vPath, data, (err, html) => {
|
||||||
if (err && vPath !== path.join(config['data_dir'], config['home']['error'])) {
|
if (err && vPath !== path.join(config['data_dir'], config['home']['error'])) {
|
||||||
@@ -114,7 +125,7 @@ module.exports = (config) => {
|
|||||||
if (err) {
|
if (err) {
|
||||||
res.sendStatus(code);
|
res.sendStatus(code);
|
||||||
} else {
|
} else {
|
||||||
render(req, res, errorPath, {error: code}, code);
|
render(req, res, errorPath, { error: code }, code);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -130,7 +141,7 @@ module.exports = (config) => {
|
|||||||
//rate limit for safer server
|
//rate limit for safer server
|
||||||
const limiter = rateLimit({
|
const limiter = rateLimit({
|
||||||
windowMs: 15 * 60 * 1000, // 15 minutes
|
windowMs: 15 * 60 * 1000, // 15 minutes
|
||||||
max: config['rate_limit']
|
max: config['rate_limit'],
|
||||||
});
|
});
|
||||||
app.use(limiter);
|
app.use(limiter);
|
||||||
|
|
||||||
@@ -141,7 +152,7 @@ module.exports = (config) => {
|
|||||||
res.end = (chunk, encoding) => {
|
res.end = (chunk, encoding) => {
|
||||||
fs.appendFile(config['access_log'],
|
fs.appendFile(config['access_log'],
|
||||||
`${res.statusCode} ${req.method} ${req.url} ${new Date().toUTCString()} ${req.ips.join(' ') || req.ip}\n`,
|
`${res.statusCode} ${req.method} ${req.url} ${new Date().toUTCString()} ${req.ips.join(' ') || req.ip}\n`,
|
||||||
{encoding: 'UTF-8'}, () => {
|
{ encoding: 'UTF-8' }, () => {
|
||||||
res.end = end;
|
res.end = end;
|
||||||
res.end(chunk, encoding);
|
res.end(chunk, encoding);
|
||||||
});
|
});
|
||||||
@@ -157,24 +168,39 @@ module.exports = (config) => {
|
|||||||
if (err) {
|
if (err) {
|
||||||
showError(req, res, 404);
|
showError(req, res, 404);
|
||||||
} else {
|
} else {
|
||||||
|
hc.count(req, '/', () => {
|
||||||
render(req, res, homePath,
|
render(req, res, homePath,
|
||||||
{
|
{
|
||||||
articles: Object.values(articles)
|
articles: Object.values(articles)
|
||||||
.filter(d => !d.draft).sort((a, b) => ('' + b.path).localeCompare(a.path))
|
.filter(d => !d.draft)
|
||||||
|
.sort((a, b) => ('' + b.path).localeCompare(a.path)),
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
app.get('/stats', (req, res) => {
|
||||||
|
if (config['modules']['hit_counter']) {
|
||||||
|
hc.read('/', (data) => {
|
||||||
|
res.json({
|
||||||
|
hits: data.hits,
|
||||||
|
visitors: data.visitors,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
showError(req, res, 404);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
//RSS endpoint
|
//RSS endpoint
|
||||||
app.get(config['rss']['endpoint'], (req, res) => {
|
app.get(config['rss']['endpoint'], (req, res) => {
|
||||||
if (config['modules']['rss']) {
|
if (config['modules']['rss']) {
|
||||||
if (!lastRSS) {
|
if (!lastRSS) {
|
||||||
const feed = new Rss({
|
const feed = new Rss({
|
||||||
'title': config['rss']['title'],
|
title: config['rss']['title'],
|
||||||
'description': config['rss']['description'],
|
description: config['rss']['description'],
|
||||||
'feed_url': host + req.url,
|
feed_url: host + req.url,
|
||||||
'site_url': host
|
site_url: host,
|
||||||
});
|
});
|
||||||
Object.values(articles)
|
Object.values(articles)
|
||||||
.slice(0, config['rss']['length'])
|
.slice(0, config['rss']['length'])
|
||||||
@@ -182,7 +208,7 @@ module.exports = (config) => {
|
|||||||
feed.item({
|
feed.item({
|
||||||
title: article.title,
|
title: article.title,
|
||||||
url: host + article.url,
|
url: host + article.url,
|
||||||
date: article.date
|
date: article.date,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
lastRSS = feed.xml();
|
lastRSS = feed.xml();
|
||||||
@@ -196,24 +222,29 @@ module.exports = (config) => {
|
|||||||
//webhook endpoint
|
//webhook endpoint
|
||||||
app.post(config['webhook']['endpoint'], (req, res) => {
|
app.post(config['webhook']['endpoint'], (req, res) => {
|
||||||
if (config['modules']['webhook']) {
|
if (config['modules']['webhook']) {
|
||||||
|
let valid = true;
|
||||||
if (config['webhook']['signature_header'] && config['webhook']['secret']) {
|
if (config['webhook']['signature_header'] && config['webhook']['secret']) {
|
||||||
const payload = JSON.stringify(req.body) || '';
|
const payload = JSON.stringify(req.body) || '';
|
||||||
const hmac = crypto.createHmac('sha1', config['webhook']['secret']);
|
const hmac = crypto.createHmac('sha1', config['webhook']['secret']);
|
||||||
const digest = 'sha1=' + hmac.update(payload).digest('hex');
|
const digest = 'sha1=' + hmac.update(payload).digest('hex');
|
||||||
const checksum = req.headers[config['webhook']['signature_header']];
|
const checksum = req.headers[config['webhook']['signature_header']];
|
||||||
if (!checksum || !digest || checksum !== digest) {
|
if (!checksum || !digest || checksum !== digest) {
|
||||||
return res.sendStatus(403);
|
res.sendStatus(403);
|
||||||
|
valid = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cp.exec(config['webhook']['pull_command'], {cwd: path.join(__dirname, '..', config['data_dir'])}, (err) => {
|
if (valid) {
|
||||||
|
cp.exec(config['webhook']['pull_command'], { cwd: path.join(__dirname, '..', config['data_dir']) }, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(cons.error, `command '${config['webhook']['pull_command']}' failed : ${err}`);
|
console.log(cons.error, `command '${config['webhook']['pull_command']}' failed : ${err}`);
|
||||||
return res.sendStatus(500);
|
res.sendStatus(500);
|
||||||
}
|
} else {
|
||||||
reload(() => {
|
reload(() => {
|
||||||
res.sendStatus(200);
|
res.sendStatus(200);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
res.sendStatus(400);
|
res.sendStatus(400);
|
||||||
}
|
}
|
||||||
@@ -229,17 +260,29 @@ module.exports = (config) => {
|
|||||||
|
|
||||||
// catch all article urls and render them
|
// catch all article urls and render them
|
||||||
app.get('*', (req, res, next) => {
|
app.get('*', (req, res, next) => {
|
||||||
if (/^\/\d{4}\/\d{2}\/\d{2}\/$/.test(req.path)) {
|
if (/^\/\d{4}\/\d{2}\/\d{2}\/(stats)?$/.test(req.path)) {
|
||||||
const articlePath = req.path.substr(1, 10);
|
const articlePath = req.path.substr(1, 10);
|
||||||
const article = articles[articlePath];
|
const article = articles[articlePath];
|
||||||
if (!article) {
|
if (!article) {
|
||||||
showError(req, res, 404);
|
showError(req, res, 404);
|
||||||
|
} else if (req.path.endsWith('stats')) {
|
||||||
|
if (config['modules']['hit_counter']) {
|
||||||
|
hc.read(articlePath, (data) => {
|
||||||
|
res.json({
|
||||||
|
hits: data.hits,
|
||||||
|
visitors: data.visitors,
|
||||||
|
});
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
|
showError(req, res, 404);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
hc.count(req, articlePath, () => {
|
||||||
renderer.render(article.realPath, (err, html) => {
|
renderer.render(article.realPath, (err, html) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
||||||
return showError(req, res, 500);
|
showError(req, res, 500);
|
||||||
}
|
} else {
|
||||||
article.content = html;
|
article.content = html;
|
||||||
const templatePath = path.join(config['data_dir'], config['article']['template']);
|
const templatePath = path.join(config['data_dir'], config['article']['template']);
|
||||||
fs.access(templatePath, fs.constants.R_OK, (err) => {
|
fs.access(templatePath, fs.constants.R_OK, (err) => {
|
||||||
@@ -247,7 +290,9 @@ module.exports = (config) => {
|
|||||||
console.log(cons.error, `no template found at ${templatePath}`);
|
console.log(cons.error, `no template found at ${templatePath}`);
|
||||||
showError(req, res, 500);
|
showError(req, res, 500);
|
||||||
} else {
|
} else {
|
||||||
render(req, res, templatePath, {article: article});
|
render(req, res, templatePath, { article: article });
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -284,7 +329,7 @@ module.exports = (config) => {
|
|||||||
}
|
}
|
||||||
fs.appendFile(config['error_log'],
|
fs.appendFile(config['error_log'],
|
||||||
`500 ${req.method} ${req.url} ${new Date().toUTCString()} ${req.ips.join(' ') || req.ip}\n${err.stack}\n`,
|
`500 ${req.method} ${req.url} ${new Date().toUTCString()} ${req.ips.join(' ') || req.ip}\n${err.stack}\n`,
|
||||||
{encoding: 'UTF-8'}, () => {
|
{ encoding: 'UTF-8' }, () => {
|
||||||
next(err);
|
next(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -12,7 +12,8 @@
|
|||||||
"prism": true,
|
"prism": true,
|
||||||
"mathjax": true,
|
"mathjax": true,
|
||||||
"plantuml": true,
|
"plantuml": true,
|
||||||
"fa-diagrams": true
|
"fa-diagrams": true,
|
||||||
|
"hit_counter": true
|
||||||
},
|
},
|
||||||
"home": {
|
"home": {
|
||||||
"title": "GitBlog.md",
|
"title": "GitBlog.md",
|
||||||
@@ -58,5 +59,12 @@
|
|||||||
},
|
},
|
||||||
"plantuml": {
|
"plantuml": {
|
||||||
"output_format": "svg"
|
"output_format": "svg"
|
||||||
|
},
|
||||||
|
"hit_counter": {
|
||||||
|
"unique_visitor_timeout": 7200000
|
||||||
|
},
|
||||||
|
"redis": {
|
||||||
|
"host": "localhost",
|
||||||
|
"port": 6379
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -25,7 +25,7 @@ const merge = (ref, src) => {
|
|||||||
|
|
||||||
module.exports = () => {
|
module.exports = () => {
|
||||||
try {
|
try {
|
||||||
let configData = fs.readFileSync('config.json', {encoding: 'UTF-8'});
|
let configData = fs.readFileSync('config.json', { encoding: 'UTF-8' });
|
||||||
let config = JSON.parse(configData);
|
let config = JSON.parse(configData);
|
||||||
return merge(refConfig, config);
|
return merge(refConfig, config);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
+22
-17
@@ -11,22 +11,23 @@ const joinUrl = (...paths) => path.join(...paths).replace(/\\/g, '/');
|
|||||||
const getFileTree = (dir, cb) => {
|
const getFileTree = (dir, cb) => {
|
||||||
let list = [];
|
let list = [];
|
||||||
let remaining = 0;
|
let remaining = 0;
|
||||||
fs.readdir(dir, {withFileTypes: true}, (err, items) => {
|
fs.readdir(dir, { withFileTypes: true }, (err, items) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
cb(err);
|
||||||
}
|
} else {
|
||||||
items.forEach((item) => {
|
items.forEach((item) => {
|
||||||
if (item.isDirectory()) {
|
if (item.isDirectory()) {
|
||||||
remaining++;
|
remaining++;
|
||||||
getFileTree(path.join(dir, item.name), (err, out) => {
|
getFileTree(path.join(dir, item.name), (err, out) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
cb(err);
|
||||||
}
|
} else {
|
||||||
list.push(...out);
|
list.push(...out);
|
||||||
remaining--;
|
remaining--;
|
||||||
if (remaining === 0) {
|
if (remaining === 0) {
|
||||||
cb(null, list);
|
cb(null, list);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
list.push(path.join(dir, item.name));
|
list.push(path.join(dir, item.name));
|
||||||
@@ -35,6 +36,7 @@ const getFileTree = (dir, cb) => {
|
|||||||
if (remaining === 0) {
|
if (remaining === 0) {
|
||||||
cb(null, list);
|
cb(null, list);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -45,11 +47,10 @@ const getFileTree = (dir, cb) => {
|
|||||||
* @param cb
|
* @param cb
|
||||||
*/
|
*/
|
||||||
const readIndexFile = (path, thumbnailTag, cb) => {
|
const readIndexFile = (path, thumbnailTag, cb) => {
|
||||||
fs.readFile(path, {encoding: 'UTF-8'}, (err, data) => {
|
fs.readFile(path, { encoding: 'UTF-8' }, (err, data) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
cb(err);
|
||||||
}
|
} else {
|
||||||
|
|
||||||
let info = {};
|
let info = {};
|
||||||
|
|
||||||
const regRes1 = data.match(/(^|[^#])#([^#\r\n]*)\r?\n?$/m);
|
const regRes1 = data.match(/(^|[^#])#([^#\r\n]*)\r?\n?$/m);
|
||||||
@@ -60,6 +61,7 @@ const readIndexFile = (path, thumbnailTag, cb) => {
|
|||||||
info.thumbnail = regRes2 ? regRes2[1].trim() : undefined;
|
info.thumbnail = regRes2 ? regRes2[1].trim() : undefined;
|
||||||
|
|
||||||
cb(null, info);
|
cb(null, info);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -74,8 +76,8 @@ module.exports = (config) => {
|
|||||||
fetchArticles: (cb) => {
|
fetchArticles: (cb) => {
|
||||||
getFileTree(config['data_dir'], (err, fileList) => {
|
getFileTree(config['data_dir'], (err, fileList) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
cb(err);
|
||||||
}
|
} else {
|
||||||
const paths = fileList
|
const paths = fileList
|
||||||
.map((p) => p.substr(config['data_dir'].length + 1).split(path.sep))
|
.map((p) => p.substr(config['data_dir'].length + 1).split(path.sep))
|
||||||
.filter((p) => p.length === 4 && (p[3] === config['article']['index'] || p[3] === config['article']['draft']) &&
|
.filter((p) => p.length === 4 && (p[3] === config['article']['index'] || p[3] === config['article']['draft']) &&
|
||||||
@@ -92,18 +94,20 @@ module.exports = (config) => {
|
|||||||
realPath: path.join(config['data_dir'], p[0], p[1], p[2], p[3]),
|
realPath: path.join(config['data_dir'], p[0], p[1], p[2], p[3]),
|
||||||
year: parseInt(p[0]),
|
year: parseInt(p[0]),
|
||||||
month: parseInt(p[1]),
|
month: parseInt(p[1]),
|
||||||
day: parseInt(p[2])
|
day: parseInt(p[2]),
|
||||||
};
|
};
|
||||||
article.date = new Date(article.year, article.month, article.day);
|
article.date = new Date(article.year, article.month, article.day);
|
||||||
article.date.setUTCHours(0);
|
article.date.setUTCHours(0);
|
||||||
remaining++;
|
remaining++;
|
||||||
readIndexFile(article.realPath, config['article']['thumbnail_tag'], (err, info) => {
|
readIndexFile(article.realPath, config['article']['thumbnail_tag'], (err, info) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
cb(err);
|
||||||
}
|
} else {
|
||||||
article.title = info.title || config['article']['default_title'];
|
article.title = info.title || config['article']['default_title'];
|
||||||
article.thumbnail = info.thumbnail ? joinUrl(article.path, info.thumbnail) : config['article']['default_thumbnail'];
|
article.thumbnail = info.thumbnail ? joinUrl(article.path, info.thumbnail) : config['article']['default_thumbnail'];
|
||||||
article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ').trim().replace(/ /gm, '_');
|
article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ')
|
||||||
|
.trim()
|
||||||
|
.replace(/ /gm, '_');
|
||||||
article.url = '/' + joinUrl(article.path, article.escapedTitle) + '/';
|
article.url = '/' + joinUrl(article.path, article.escapedTitle) + '/';
|
||||||
if (!articles[article.path] || !article.draft) {
|
if (!articles[article.path] || !article.draft) {
|
||||||
articles[article.path] = article;
|
articles[article.path] = article;
|
||||||
@@ -112,10 +116,11 @@ module.exports = (config) => {
|
|||||||
if (remaining === 0) {
|
if (remaining === 0) {
|
||||||
cb(null, articles);
|
cb(null, articles);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
const redis = require('redis');
|
||||||
|
|
||||||
|
module.exports = (config, onConnect, onError) => {
|
||||||
|
const client = config['modules']['hit_counter'] ? redis.createClient(config['redis']) : { connected: false, on: () => { /* ignore */ } };
|
||||||
|
|
||||||
|
client.on('connect', onConnect);
|
||||||
|
client.on('error', onError);
|
||||||
|
|
||||||
|
const visitors = {};
|
||||||
|
|
||||||
|
const count = (req, path, cb) => {
|
||||||
|
if (!client.connected) {
|
||||||
|
cb();
|
||||||
|
} else {
|
||||||
|
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
||||||
|
const key = path + ':' + ip;
|
||||||
|
const now = Date.now();
|
||||||
|
const isNewVisitor = (now - (visitors[key] || 0)) > config['hit_counter']['unique_visitor_timeout'];
|
||||||
|
visitors[key] = now;
|
||||||
|
client
|
||||||
|
.multi()
|
||||||
|
.hincrby(path, 'h', 1)
|
||||||
|
.hincrby(path, 'v', isNewVisitor ? 1 : 0)
|
||||||
|
.exec(cb);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const read = (path, cb) => {
|
||||||
|
if (!client.connected) {
|
||||||
|
cb({
|
||||||
|
hits: 0,
|
||||||
|
visitors: 0,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
client.hgetall(path, (_, value) => {
|
||||||
|
cb({
|
||||||
|
hits: value ? value.h || 0 : 0,
|
||||||
|
visitors: value ? value.v || 0 : 0,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
count: count,
|
||||||
|
read: read,
|
||||||
|
};
|
||||||
|
};
|
||||||
+1
-1
@@ -25,7 +25,7 @@ if (!fs.existsSync('data')) {
|
|||||||
const dir = path.join('data', datetime.getFullYear().toString(), pad0(datetime.getMonth() + 1), pad0(datetime.getDate()));
|
const dir = path.join('data', datetime.getFullYear().toString(), pad0(datetime.getMonth() + 1), pad0(datetime.getDate()));
|
||||||
|
|
||||||
if (!fs.existsSync(dir)) {
|
if (!fs.existsSync(dir)) {
|
||||||
fs.mkdirSync(dir, {recursive: true});
|
fs.mkdirSync(dir, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
copy(path.join('sample_data', 'article'), dir);
|
copy(path.join('sample_data', 'article'), dir);
|
||||||
|
|||||||
+41
-21
@@ -67,8 +67,8 @@ module.exports = (config) => {
|
|||||||
|
|
||||||
const renderPrism = (data, cb) => {
|
const renderPrism = (data, cb) => {
|
||||||
if (!config['modules']['prism']) {
|
if (!config['modules']['prism']) {
|
||||||
return cb(data);
|
cb(data);
|
||||||
}
|
} else {
|
||||||
const codeRegex = /```([\w-]+)\r?\n((?:(?!```)[\s\S])*)\r?\n```/m;
|
const codeRegex = /```([\w-]+)\r?\n((?:(?!```)[\s\S])*)\r?\n```/m;
|
||||||
let match;
|
let match;
|
||||||
while ((match = codeRegex.exec(data))) {
|
while ((match = codeRegex.exec(data))) {
|
||||||
@@ -78,6 +78,7 @@ module.exports = (config) => {
|
|||||||
data = data.slice(0, match.index) + `<pre><code class="${lang} language-${lang}">` + block + '</code></pre>' + data.slice(match.index + match[0].length);
|
data = data.slice(0, match.index) + `<pre><code class="${lang} language-${lang}">` + block + '</code></pre>' + data.slice(match.index + match[0].length);
|
||||||
}
|
}
|
||||||
cb(data);
|
cb(data);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (config['modules']['plantuml']) {
|
if (config['modules']['plantuml']) {
|
||||||
@@ -87,8 +88,8 @@ module.exports = (config) => {
|
|||||||
const renderPlantUML = (data, cb) => {
|
const renderPlantUML = (data, cb) => {
|
||||||
/* global encode64 */
|
/* global encode64 */
|
||||||
if (!config['modules']['plantuml']) {
|
if (!config['modules']['plantuml']) {
|
||||||
return cb(data);
|
cb(data);
|
||||||
}
|
} else {
|
||||||
const parts = getParts(data);
|
const parts = getParts(data);
|
||||||
const umlRegex = /@startuml\r?\n((?:(?!@enduml)[\s\S])*)\r?\n@enduml/m;
|
const umlRegex = /@startuml\r?\n((?:(?!@enduml)[\s\S])*)\r?\n@enduml/m;
|
||||||
let match;
|
let match;
|
||||||
@@ -103,6 +104,7 @@ module.exports = (config) => {
|
|||||||
data = data.slice(0, part.index) + part.text + data.slice(part.end);
|
data = data.slice(0, part.index) + part.text + data.slice(part.end);
|
||||||
});
|
});
|
||||||
cb(data);
|
cb(data);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mjAPI;
|
let mjAPI;
|
||||||
@@ -111,18 +113,27 @@ module.exports = (config) => {
|
|||||||
mjAPI.config({
|
mjAPI.config({
|
||||||
MathJax: {
|
MathJax: {
|
||||||
tex2jax: {
|
tex2jax: {
|
||||||
inlineMath: [['$', '$']],
|
inlineMath: [
|
||||||
displayMath: [['$$', '$$']]
|
[
|
||||||
}
|
'$',
|
||||||
}
|
'$',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
displayMath: [
|
||||||
|
[
|
||||||
|
'$$',
|
||||||
|
'$$',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const renderMathJax = (data, cb) => {
|
const renderMathJax = (data, cb) => {
|
||||||
if (!config['modules']['mathjax']) {
|
if (!config['modules']['mathjax']) {
|
||||||
return cb(data);
|
cb(data);
|
||||||
}
|
} else {
|
||||||
|
|
||||||
const parts = getParts(data);
|
const parts = getParts(data);
|
||||||
|
|
||||||
const doMJ = (match, format, i) => {
|
const doMJ = (match, format, i) => {
|
||||||
@@ -131,7 +142,7 @@ module.exports = (config) => {
|
|||||||
const mjConf = {
|
const mjConf = {
|
||||||
math: eq,
|
math: eq,
|
||||||
format: format,
|
format: format,
|
||||||
speakText: config['mathjax']['speak_text']
|
speakText: config['mathjax']['speak_text'],
|
||||||
};
|
};
|
||||||
mjConf[output] = true;
|
mjConf[output] = true;
|
||||||
mjAPI.typeset(mjConf, (res) => {
|
mjAPI.typeset(mjConf, (res) => {
|
||||||
@@ -145,15 +156,23 @@ module.exports = (config) => {
|
|||||||
const eqRegex = /\$\$((?:(?!\$\$)[\s\S])*)\$\$/m;
|
const eqRegex = /\$\$((?:(?!\$\$)[\s\S])*)\$\$/m;
|
||||||
const inlineEqRegex = /\$([^$\n]*)\$/;
|
const inlineEqRegex = /\$([^$\n]*)\$/;
|
||||||
|
|
||||||
|
let found = false;
|
||||||
for (let i = 0; i < parts.length; i++) {
|
for (let i = 0; i < parts.length; i++) {
|
||||||
let match;
|
let match;
|
||||||
if ((match = eqRegex.exec(parts[i].text))) {
|
if ((match = eqRegex.exec(parts[i].text))) {
|
||||||
return doMJ(match, 'TeX', i);
|
doMJ(match, 'TeX', i);
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
} else if ((match = inlineEqRegex.exec(parts[i].text))) {
|
} else if ((match = inlineEqRegex.exec(parts[i].text))) {
|
||||||
return doMJ(match, 'inline-TeX', i);
|
doMJ(match, 'inline-TeX', i);
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!found) {
|
||||||
cb(data);
|
cb(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let faDiagrams;
|
let faDiagrams;
|
||||||
@@ -165,8 +184,8 @@ module.exports = (config) => {
|
|||||||
|
|
||||||
const renderFaDiagrams = (data, cb) => {
|
const renderFaDiagrams = (data, cb) => {
|
||||||
if (!config['modules']['fa-diagrams']) {
|
if (!config['modules']['fa-diagrams']) {
|
||||||
return cb(data);
|
cb(data);
|
||||||
}
|
} else {
|
||||||
const parts = getParts(data);
|
const parts = getParts(data);
|
||||||
const diagramsRegex = /@startfad\r?\n((?:(?!@endfad)[\s\S])*)\r?\n@endfad/m;
|
const diagramsRegex = /@startfad\r?\n((?:(?!@endfad)[\s\S])*)\r?\n@endfad/m;
|
||||||
let match;
|
let match;
|
||||||
@@ -195,6 +214,7 @@ module.exports = (config) => {
|
|||||||
data = data.slice(0, part.index) + part.text + data.slice(part.end);
|
data = data.slice(0, part.index) + part.text + data.slice(part.end);
|
||||||
});
|
});
|
||||||
cb(data);
|
cb(data);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -205,11 +225,10 @@ module.exports = (config) => {
|
|||||||
renderMathJax: config['test'] ? renderMathJax : undefined,
|
renderMathJax: config['test'] ? renderMathJax : undefined,
|
||||||
renderFaDiagrams: config['test'] ? renderFaDiagrams : undefined,
|
renderFaDiagrams: config['test'] ? renderFaDiagrams : undefined,
|
||||||
render: (file, cb) => {
|
render: (file, cb) => {
|
||||||
fs.readFile(file, {encoding: 'UTF-8'}, (err, data) => {
|
fs.readFile(file, { encoding: 'UTF-8' }, (err, data) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
cb(err);
|
||||||
}
|
} else {
|
||||||
|
|
||||||
renderPlantUML(data, (data) => {
|
renderPlantUML(data, (data) => {
|
||||||
renderFaDiagrams(data, (data) => {
|
renderFaDiagrams(data, (data) => {
|
||||||
renderMathJax(data, (data) => {
|
renderMathJax(data, (data) => {
|
||||||
@@ -221,8 +240,9 @@ module.exports = (config) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,6 @@ const fs = require('fs');
|
|||||||
* @param scriptPath
|
* @param scriptPath
|
||||||
*/
|
*/
|
||||||
module.exports = (scriptPath) => {
|
module.exports = (scriptPath) => {
|
||||||
eval.call(global, fs.readFileSync(scriptPath, {encoding: 'UTF-8'}));
|
eval.call(global, fs.readFileSync(scriptPath, { encoding: 'UTF-8' }));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+162
-67
@@ -17,6 +17,7 @@ config['rss']['endpoint'] = '/rsstest';
|
|||||||
config['rss']['length'] = 2;
|
config['rss']['length'] = 2;
|
||||||
config['home']['error'] = testError;
|
config['home']['error'] = testError;
|
||||||
config['article']['template'] = testTemplate;
|
config['article']['template'] = testTemplate;
|
||||||
|
config['modules']['hit_counter'] = false;
|
||||||
|
|
||||||
const app = require('../src/app')(config);
|
const app = require('../src/app')(config);
|
||||||
|
|
||||||
@@ -28,6 +29,7 @@ beforeEach((done, fail) => {
|
|||||||
config['error_log'] = '';
|
config['error_log'] = '';
|
||||||
config['modules']['rss'] = true;
|
config['modules']['rss'] = true;
|
||||||
config['modules']['webhook'] = true;
|
config['modules']['webhook'] = true;
|
||||||
|
config['modules']['hit_counter'] = false;
|
||||||
|
|
||||||
utils.deleteFolderSync(dataDir);
|
utils.deleteFolderSync(dataDir);
|
||||||
fs.mkdirSync(dataDir);
|
fs.mkdirSync(dataDir);
|
||||||
@@ -49,15 +51,17 @@ describe('Test reload', () => {
|
|||||||
|
|
||||||
describe('Test request logging', () => {
|
describe('Test request logging', () => {
|
||||||
test('no log', (done) => {
|
test('no log', (done) => {
|
||||||
request(app).get('/rsstest').then(() => {
|
request(app).get('/rsstest')
|
||||||
|
.then(() => {
|
||||||
expect(fs.existsSync(path.join(dataDir, 'access.log'))).toBe(false);
|
expect(fs.existsSync(path.join(dataDir, 'access.log'))).toBe(false);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('get 200', (done) => {
|
test('get 200', (done) => {
|
||||||
config['access_log'] = path.join(dataDir, 'access.log');
|
config['access_log'] = path.join(dataDir, 'access.log');
|
||||||
request(app).get('/rsstest').then(() => {
|
request(app).get('/rsstest')
|
||||||
fs.readFile(path.join(dataDir, 'access.log'), {encoding: 'UTF-8'}, (err, data) => {
|
.then(() => {
|
||||||
|
fs.readFile(path.join(dataDir, 'access.log'), { encoding: 'UTF-8' }, (err, data) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(data).toBe('200 GET /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
|
expect(data).toBe('200 GET /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
|
||||||
done();
|
done();
|
||||||
@@ -66,8 +70,9 @@ describe('Test request logging', () => {
|
|||||||
});
|
});
|
||||||
test('post 400', (done) => {
|
test('post 400', (done) => {
|
||||||
config['access_log'] = path.join(dataDir, 'access.log');
|
config['access_log'] = path.join(dataDir, 'access.log');
|
||||||
request(app).post('/rsstest').then(() => {
|
request(app).post('/rsstest')
|
||||||
fs.readFile(path.join(dataDir, 'access.log'), {encoding: 'UTF-8'}, (err, data) => {
|
.then(() => {
|
||||||
|
fs.readFile(path.join(dataDir, 'access.log'), { encoding: 'UTF-8' }, (err, data) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(data).toBe('400 POST /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
|
expect(data).toBe('400 POST /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
|
||||||
done();
|
done();
|
||||||
@@ -76,9 +81,11 @@ describe('Test request logging', () => {
|
|||||||
});
|
});
|
||||||
test('2 requests', (done) => {
|
test('2 requests', (done) => {
|
||||||
config['access_log'] = path.join(dataDir, 'access.log');
|
config['access_log'] = path.join(dataDir, 'access.log');
|
||||||
request(app).get('/rss').then(() => {
|
request(app).get('/rss')
|
||||||
request(app).post('/rsstest').then(() => {
|
.then(() => {
|
||||||
fs.readFile(path.join(dataDir, 'access.log'), {encoding: 'UTF-8'}, (err, data) => {
|
request(app).post('/rsstest')
|
||||||
|
.then(() => {
|
||||||
|
fs.readFile(path.join(dataDir, 'access.log'), { encoding: 'UTF-8' }, (err, data) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(data).toBe('404 GET /rss ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n' +
|
expect(data).toBe('404 GET /rss ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n' +
|
||||||
'400 POST /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
|
'400 POST /rsstest ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\n');
|
||||||
@@ -92,7 +99,8 @@ describe('Test request logging', () => {
|
|||||||
describe('Test error logging', () => {
|
describe('Test error logging', () => {
|
||||||
test('no log', (done) => {
|
test('no log', (done) => {
|
||||||
config['home']['index'] = null;
|
config['home']['index'] = null;
|
||||||
request(app).get('/').then(() => {
|
request(app).get('/')
|
||||||
|
.then(() => {
|
||||||
expect(fs.existsSync(path.join(dataDir, 'error.log'))).toBe(false);
|
expect(fs.existsSync(path.join(dataDir, 'error.log'))).toBe(false);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -100,10 +108,12 @@ describe('Test error logging', () => {
|
|||||||
test('null error', (done) => {
|
test('null error', (done) => {
|
||||||
config['home']['index'] = null;
|
config['home']['index'] = null;
|
||||||
config['error_log'] = path.join(dataDir, 'error.log');
|
config['error_log'] = path.join(dataDir, 'error.log');
|
||||||
request(app).get('/').then(() => {
|
request(app).get('/')
|
||||||
fs.readFile(path.join(dataDir, 'error.log'), {encoding: 'UTF-8'}, (err, data) => {
|
.then(() => {
|
||||||
|
fs.readFile(path.join(dataDir, 'error.log'), { encoding: 'UTF-8' }, (err, data) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
const start = data.split('\n').slice(0, 2).join('\n');
|
const start = data.split('\n').slice(0, 2)
|
||||||
|
.join('\n');
|
||||||
const expected = '500 GET / ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\nTypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string.';
|
const expected = '500 GET / ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\nTypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string.';
|
||||||
expect(start.indexOf(expected)).toBe(0);
|
expect(start.indexOf(expected)).toBe(0);
|
||||||
done();
|
done();
|
||||||
@@ -114,14 +124,16 @@ describe('Test error logging', () => {
|
|||||||
|
|
||||||
describe('Test root path', () => {
|
describe('Test root path', () => {
|
||||||
test('404 no index no error', (done) => {
|
test('404 no index no error', (done) => {
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(404);
|
expect(response.statusCode).toBe(404);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('404 no index but error page', (done) => {
|
test('404 no index but error page', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(404);
|
expect(response.statusCode).toBe(404);
|
||||||
expect(response.text).toBe('error 404');
|
expect(response.text).toBe('error 404');
|
||||||
done();
|
done();
|
||||||
@@ -129,7 +141,8 @@ describe('Test root path', () => {
|
|||||||
});
|
});
|
||||||
test('500 render error', (done) => {
|
test('500 render error', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(500);
|
expect(response.statusCode).toBe(500);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -137,7 +150,8 @@ describe('Test root path', () => {
|
|||||||
test('500 render error with page', (done) => {
|
test('500 render error with page', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
|
||||||
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(500);
|
expect(response.statusCode).toBe(500);
|
||||||
expect(response.text).toBe('error 500');
|
expect(response.text).toBe('error 500');
|
||||||
done();
|
done();
|
||||||
@@ -146,14 +160,16 @@ describe('Test root path', () => {
|
|||||||
test('500 render error with failing page', (done) => {
|
test('500 render error with failing page', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
|
||||||
fs.writeFileSync(path.join(dataDir, testError), 'error <%= null.error %>');
|
fs.writeFileSync(path.join(dataDir, testError), 'error <%= null.error %>');
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(500);
|
expect(response.statusCode).toBe(500);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('200 no articles', (done) => {
|
test('200 no articles', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.text).toBe('articles 0');
|
expect(response.text).toBe('articles 0');
|
||||||
done();
|
done();
|
||||||
@@ -163,7 +179,7 @@ describe('Test root path', () => {
|
|||||||
utils.createEmptyDirs([
|
utils.createEmptyDirs([
|
||||||
path.join(dataDir, '2019', '05', '05'),
|
path.join(dataDir, '2019', '05', '05'),
|
||||||
path.join(dataDir, '2018', '05', '05'),
|
path.join(dataDir, '2018', '05', '05'),
|
||||||
path.join(dataDir, '2017', '05', '05')
|
path.join(dataDir, '2017', '05', '05'),
|
||||||
]);
|
]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, '2019', '05', '05', 'draft.md'),
|
path.join(dataDir, '2019', '05', '05', 'draft.md'),
|
||||||
@@ -173,25 +189,44 @@ describe('Test root path', () => {
|
|||||||
]);
|
]);
|
||||||
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.text).toBe('articles 2');
|
expect(response.text).toBe('articles 2');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
}, fail);
|
}, fail);
|
||||||
});
|
});
|
||||||
|
test('404 index no stats', (done) => {
|
||||||
|
request(app).get('/stats')
|
||||||
|
.then((response) => {
|
||||||
|
expect(response.statusCode).toBe(404);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('200 index stats', (done) => {
|
||||||
|
config['modules']['hit_counter'] = true;
|
||||||
|
request(app).get('/stats')
|
||||||
|
.then((response) => {
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.body).toEqual({ hits: 0, visitors: 0 });
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Test RSS feed', () => {
|
describe('Test RSS feed', () => {
|
||||||
test('404 rss deactivated', (done) => {
|
test('404 rss deactivated', (done) => {
|
||||||
config['modules']['rss'] = false;
|
config['modules']['rss'] = false;
|
||||||
request(app).get('/rsstest').then((response) => {
|
request(app).get('/rsstest')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(404);
|
expect(response.statusCode).toBe(404);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('200 empty rss', (done) => {
|
test('200 empty rss', (done) => {
|
||||||
request(app).get('/rsstest').then((response) => {
|
request(app).get('/rsstest')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.type).toBe('application/rss+xml');
|
expect(response.type).toBe('application/rss+xml');
|
||||||
expect(response.text.length).toBeGreaterThan(0);
|
expect(response.text.length).toBeGreaterThan(0);
|
||||||
@@ -200,15 +235,19 @@ describe('Test RSS feed', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('200 Mozilla fix', (done) => {
|
test('200 Mozilla fix', (done) => {
|
||||||
request(app).get('/rsstest').set('user-agent', 'Mozilla Firefox 64.0').then((response) => {
|
request(app).get('/rsstest')
|
||||||
|
.set('user-agent', 'Mozilla Firefox 64.0')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.type).toBe('text/xml');
|
expect(response.type).toBe('text/xml');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('200 rss cache', (done) => {
|
test('200 rss cache', (done) => {
|
||||||
request(app).get('/rsstest').then(() => {
|
request(app).get('/rsstest')
|
||||||
request(app).get('/rsstest').then((response) => {
|
.then(() => {
|
||||||
|
request(app).get('/rsstest')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.text.length).toBeGreaterThan(0);
|
expect(response.text.length).toBeGreaterThan(0);
|
||||||
expect(response.text.split('<item>').length).toBe(1);
|
expect(response.text.split('<item>').length).toBe(1);
|
||||||
@@ -219,14 +258,15 @@ describe('Test RSS feed', () => {
|
|||||||
test('200 2 rss items', (done, fail) => {
|
test('200 2 rss items', (done, fail) => {
|
||||||
utils.createEmptyDirs([
|
utils.createEmptyDirs([
|
||||||
path.join(dataDir, '2019', '05', '05'),
|
path.join(dataDir, '2019', '05', '05'),
|
||||||
path.join(dataDir, '2018', '05', '05')
|
path.join(dataDir, '2018', '05', '05'),
|
||||||
]);
|
]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
path.join(dataDir, '2018', '05', '05', 'index.md')
|
path.join(dataDir, '2018', '05', '05', 'index.md'),
|
||||||
]);
|
]);
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/rsstest').then((response) => {
|
request(app).get('/rsstest')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.text.length).toBeGreaterThan(0);
|
expect(response.text.length).toBeGreaterThan(0);
|
||||||
expect(response.text.split('<item>').length).toBe(3);
|
expect(response.text.split('<item>').length).toBe(3);
|
||||||
@@ -238,15 +278,16 @@ describe('Test RSS feed', () => {
|
|||||||
utils.createEmptyDirs([
|
utils.createEmptyDirs([
|
||||||
path.join(dataDir, '2019', '05', '05'),
|
path.join(dataDir, '2019', '05', '05'),
|
||||||
path.join(dataDir, '2018', '05', '05'),
|
path.join(dataDir, '2018', '05', '05'),
|
||||||
path.join(dataDir, '2017', '05', '05')
|
path.join(dataDir, '2017', '05', '05'),
|
||||||
]);
|
]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
path.join(dataDir, '2018', '05', '05', 'index.md'),
|
path.join(dataDir, '2018', '05', '05', 'index.md'),
|
||||||
path.join(dataDir, '2017', '05', '05', 'index.md')
|
path.join(dataDir, '2017', '05', '05', 'index.md'),
|
||||||
]);
|
]);
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/rsstest').then((response) => {
|
request(app).get('/rsstest')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.text.length).toBeGreaterThan(0);
|
expect(response.text.length).toBeGreaterThan(0);
|
||||||
expect(response.text.split('<item>').length).toBe(3);
|
expect(response.text.split('<item>').length).toBe(3);
|
||||||
@@ -259,34 +300,38 @@ describe('Test RSS feed', () => {
|
|||||||
describe('Test webhook', () => {
|
describe('Test webhook', () => {
|
||||||
test('400 webhook deactivated', (done) => {
|
test('400 webhook deactivated', (done) => {
|
||||||
config['modules']['webhook'] = false;
|
config['modules']['webhook'] = false;
|
||||||
request(app).post('/webhooktest').then((response) => {
|
request(app).post('/webhooktest')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(400);
|
expect(response.statusCode).toBe(400);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('200 no secret', (done) => {
|
test('200 no secret', (done) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
path.join(dataDir, testTemplate)
|
path.join(dataDir, testTemplate),
|
||||||
]);
|
]);
|
||||||
config['webhook']['pull_command'] = 'git --help';
|
config['webhook']['pull_command'] = 'git --help';
|
||||||
request(app).post('/webhooktest').then((response) => {
|
request(app).post('/webhooktest')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
request(app).get('/2019/05/05/').then((response) => {
|
request(app).get('/2019/05/05/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('500 command failed', (done) => {
|
test('500 command failed', (done) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
path.join(dataDir, testTemplate)
|
path.join(dataDir, testTemplate),
|
||||||
]);
|
]);
|
||||||
config['webhook']['pull_command'] = 'qzgfqgqz';
|
config['webhook']['pull_command'] = 'qzgfqgqz';
|
||||||
request(app).post('/webhooktest').then((response) => {
|
request(app).post('/webhooktest')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(500);
|
expect(response.statusCode).toBe(500);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -294,7 +339,9 @@ describe('Test webhook', () => {
|
|||||||
test('403 wrong secret', (done) => {
|
test('403 wrong secret', (done) => {
|
||||||
config['webhook']['signature_header'] = 'testheader';
|
config['webhook']['signature_header'] = 'testheader';
|
||||||
config['webhook']['secret'] = 'testvalue';
|
config['webhook']['secret'] = 'testvalue';
|
||||||
request(app).post('/webhooktest').set('testheader', 'sha1=invalid').then((response) => {
|
request(app).post('/webhooktest')
|
||||||
|
.set('testheader', 'sha1=invalid')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(403);
|
expect(response.statusCode).toBe(403);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -315,18 +362,20 @@ describe('Test webhook', () => {
|
|||||||
|
|
||||||
describe('Test articles rendering', () => {
|
describe('Test articles rendering', () => {
|
||||||
test('404 article not found', (done) => {
|
test('404 article not found', (done) => {
|
||||||
request(app).get('/2019/05/06/untitled/').then((response) => {
|
request(app).get('/2019/05/06/untitled/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(404);
|
expect(response.statusCode).toBe(404);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('500 fail to render', (done, fail) => {
|
test('500 fail to render', (done, fail) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
|
||||||
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- articl.content %><%- `<a href="${article.url}">reload</a>` %>');
|
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- articl.content %><%- `<a href="${article.url}">reload</a>` %>');
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/2019/05/05/hello/').then((response) => {
|
request(app).get('/2019/05/05/hello/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(500);
|
expect(response.statusCode).toBe(500);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -334,10 +383,11 @@ describe('Test articles rendering', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('500 no template', (done, fail) => {
|
test('500 no template', (done, fail) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/2019/05/05/hello/').then((response) => {
|
request(app).get('/2019/05/05/hello/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(500);
|
expect(response.statusCode).toBe(500);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -345,11 +395,12 @@ describe('Test articles rendering', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('200 rendered article', (done, fail) => {
|
test('200 rendered article', (done, fail) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
|
||||||
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
|
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/2019/05/05/hello/').then((response) => {
|
request(app).get('/2019/05/05/hello/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
|
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
|
||||||
done();
|
done();
|
||||||
@@ -358,11 +409,12 @@ describe('Test articles rendering', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('200 rendered draft', (done, fail) => {
|
test('200 rendered draft', (done, fail) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'draft.md'), '# Hello');
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'draft.md'), '# Hello');
|
||||||
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
|
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/2019/05/05/hello/').then((response) => {
|
request(app).get('/2019/05/05/hello/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
|
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
|
||||||
done();
|
done();
|
||||||
@@ -371,13 +423,14 @@ describe('Test articles rendering', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('200 other url', (done, fail) => {
|
test('200 other url', (done, fail) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
path.join(dataDir, testTemplate)
|
path.join(dataDir, testTemplate),
|
||||||
]);
|
]);
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/2019/05/05/anything/').then((response) => {
|
request(app).get('/2019/05/05/anything/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -385,55 +438,93 @@ describe('Test articles rendering', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('200 other url 2', (done, fail) => {
|
test('200 other url 2', (done, fail) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
path.join(dataDir, testTemplate)
|
path.join(dataDir, testTemplate),
|
||||||
]);
|
]);
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
request(app).get('/2019/05/05/').then((response) => {
|
request(app).get('/2019/05/05/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
}, fail);
|
}, fail);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('404 article no stats', (done) => {
|
||||||
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
|
utils.createEmptyFiles([
|
||||||
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
|
path.join(dataDir, testTemplate),
|
||||||
|
]);
|
||||||
|
app.reload(() => {
|
||||||
|
request(app).get('/2019/05/05/hello/stats')
|
||||||
|
.then((response) => {
|
||||||
|
expect(response.statusCode).toBe(404);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('200 index stats', (done) => {
|
||||||
|
config['modules']['hit_counter'] = true;
|
||||||
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
|
utils.createEmptyFiles([
|
||||||
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
|
path.join(dataDir, testTemplate),
|
||||||
|
]);
|
||||||
|
app.reload(() => {
|
||||||
|
request(app).get('/2019/05/05/anything/stats')
|
||||||
|
.then((response) => {
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.body).toEqual({ hits: 0, visitors: 0 });
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('Test static files', () => {
|
describe('Test static files', () => {
|
||||||
test('404 invalid file no error page', (done) => {
|
test('404 invalid file no error page', (done) => {
|
||||||
request(app).get('/somefile.txt').then((response) => {
|
request(app).get('/somefile.txt')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(404);
|
expect(response.statusCode).toBe(404);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('404 invalid file but error page', (done) => {
|
test('404 invalid file but error page', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
||||||
request(app).get('/somefile.txt').then((response) => {
|
request(app).get('/somefile.txt')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(404);
|
expect(response.statusCode).toBe(404);
|
||||||
expect(response.text).toBe('error 404');
|
expect(response.text).toBe('error 404');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('404 hidden file', (done) => {
|
test('404 hidden file', (done) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, 'tmp')]);
|
utils.createEmptyDirs([ path.join(dataDir, 'tmp') ]);
|
||||||
fs.writeFileSync(path.join(dataDir, 'tmp', 'somefile.ejs'), '');
|
fs.writeFileSync(path.join(dataDir, 'tmp', 'somefile.ejs'), '');
|
||||||
request(app).get('/tmp/somefile.ejs').then((response) => {
|
request(app).get('/tmp/somefile.ejs')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(404);
|
expect(response.statusCode).toBe(404);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('404 hidden folder', (done) => {
|
test('404 hidden folder', (done) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '.git')]);
|
utils.createEmptyDirs([ path.join(dataDir, '.git') ]);
|
||||||
fs.writeFileSync(path.join(dataDir, '.git', 'file.txt'), '');
|
fs.writeFileSync(path.join(dataDir, '.git', 'file.txt'), '');
|
||||||
request(app).get('/.git/file.txt').then((response) => {
|
request(app).get('/.git/file.txt')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(404);
|
expect(response.statusCode).toBe(404);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('200 valid file', (done) => {
|
test('200 valid file', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, 'somefile.css'), 'filecontent');
|
fs.writeFileSync(path.join(dataDir, 'somefile.css'), 'filecontent');
|
||||||
request(app).get('/somefile.css').then((response) => {
|
request(app).get('/somefile.css')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.type).toBe('text/css');
|
expect(response.type).toBe('text/css');
|
||||||
expect(response.text).toBe('filecontent');
|
expect(response.text).toBe('filecontent');
|
||||||
@@ -441,9 +532,10 @@ describe('Test static files', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('200 valid resource of article', (done) => {
|
test('200 valid resource of article', (done) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
|
||||||
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'somefile.txt'), 'filecontent');
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'somefile.txt'), 'filecontent');
|
||||||
request(app).get('/2019/05/05/title/somefile.txt').then((response) => {
|
request(app).get('/2019/05/05/title/somefile.txt')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.text).toBe('filecontent');
|
expect(response.text).toBe('filecontent');
|
||||||
done();
|
done();
|
||||||
@@ -453,19 +545,22 @@ describe('Test static files', () => {
|
|||||||
|
|
||||||
describe('Test other requests', () => {
|
describe('Test other requests', () => {
|
||||||
test('400 POST', (done) => {
|
test('400 POST', (done) => {
|
||||||
request(app).post('/').then((response) => {
|
request(app).post('/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(400);
|
expect(response.statusCode).toBe(400);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('400 PUT', (done) => {
|
test('400 PUT', (done) => {
|
||||||
request(app).put('/').then((response) => {
|
request(app).put('/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(400);
|
expect(response.statusCode).toBe(400);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('400 DELETE', (done) => {
|
test('400 DELETE', (done) => {
|
||||||
request(app).delete('/').then((response) => {
|
request(app).delete('/')
|
||||||
|
.then((response) => {
|
||||||
expect(response.statusCode).toBe(400);
|
expect(response.statusCode).toBe(400);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|||||||
+10
-4
@@ -34,8 +34,8 @@ test('example config', () => {
|
|||||||
fs.unlinkSync(configFile);
|
fs.unlinkSync(configFile);
|
||||||
}
|
}
|
||||||
fs.copyFileSync(path.join('src', 'config.default.json'), configFile);
|
fs.copyFileSync(path.join('src', 'config.default.json'), configFile);
|
||||||
const data = fs.readFileSync(configFile, {encoding: 'UTF-8'});
|
const data = fs.readFileSync(configFile, { encoding: 'UTF-8' });
|
||||||
fs.writeFileSync(configFile, data.replace('3000', '3333'), {encoding: 'UTF-8'});
|
fs.writeFileSync(configFile, data.replace('3000', '3333'), { encoding: 'UTF-8' });
|
||||||
const config = require('../src/config')();
|
const config = require('../src/config')();
|
||||||
expect(config).toBeDefined();
|
expect(config).toBeDefined();
|
||||||
expect(config['node_port']).toBe(3333);
|
expect(config['node_port']).toBe(3333);
|
||||||
@@ -70,12 +70,18 @@ test('array parsing', () => {
|
|||||||
fs.writeFileSync(configFile, '{"home":{"hidden":["item1","item2"]}}');
|
fs.writeFileSync(configFile, '{"home":{"hidden":["item1","item2"]}}');
|
||||||
const config = require('../src/config')();
|
const config = require('../src/config')();
|
||||||
expect(config).toBeDefined();
|
expect(config).toBeDefined();
|
||||||
expect(config['home']['hidden']).toEqual(['item1', 'item2']);
|
expect(config['home']['hidden']).toEqual([
|
||||||
|
'item1',
|
||||||
|
'item2',
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('array fix', () => {
|
test('array fix', () => {
|
||||||
fs.writeFileSync(configFile, '{"home":{"hidden":{}}}');
|
fs.writeFileSync(configFile, '{"home":{"hidden":{}}}');
|
||||||
const config = require('../src/config')();
|
const config = require('../src/config')();
|
||||||
expect(config).toBeDefined();
|
expect(config).toBeDefined();
|
||||||
expect(config['home']['hidden']).toEqual(['*.ejs', '/.git*']);
|
expect(config['home']['hidden']).toEqual([
|
||||||
|
'*.ejs',
|
||||||
|
'/.git*',
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
+29
-26
@@ -8,15 +8,15 @@ const testIndex = 'testindex.md';
|
|||||||
const joinUrl = (...paths) => path.join(...paths).replace(/\\/g, '/');
|
const joinUrl = (...paths) => path.join(...paths).replace(/\\/g, '/');
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
'test': true,
|
test: true,
|
||||||
'data_dir': dataDir,
|
data_dir: dataDir,
|
||||||
'article': {
|
article: {
|
||||||
'index': testIndex,
|
index: testIndex,
|
||||||
'draft': 'draft.md',
|
draft: 'draft.md',
|
||||||
'default_title': 'Untitled',
|
default_title: 'Untitled',
|
||||||
'default_thumbnail': 'default.png',
|
default_thumbnail: 'default.png',
|
||||||
'thumbnail_tag': 'thumbnail'
|
thumbnail_tag: 'thumbnail',
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const fw = require('../src/file_walker')(config);
|
const fw = require('../src/file_walker')(config);
|
||||||
@@ -46,7 +46,7 @@ describe('Test function fileTree', () => {
|
|||||||
utils.createEmptyDirs([
|
utils.createEmptyDirs([
|
||||||
path.join(dataDir, 'test', 'test'),
|
path.join(dataDir, 'test', 'test'),
|
||||||
path.join(dataDir, 'test', 'test2'),
|
path.join(dataDir, 'test', 'test2'),
|
||||||
path.join(dataDir, 'test2')
|
path.join(dataDir, 'test2'),
|
||||||
]);
|
]);
|
||||||
fw.fileTree(dataDir, (err, list) => {
|
fw.fileTree(dataDir, (err, list) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
@@ -58,7 +58,7 @@ describe('Test function fileTree', () => {
|
|||||||
test('simple files', (done) => {
|
test('simple files', (done) => {
|
||||||
const fileList = [
|
const fileList = [
|
||||||
path.join(dataDir, 'f1.txt'),
|
path.join(dataDir, 'f1.txt'),
|
||||||
path.join(dataDir, 'f2.txt')
|
path.join(dataDir, 'f2.txt'),
|
||||||
];
|
];
|
||||||
utils.createEmptyFiles(fileList);
|
utils.createEmptyFiles(fileList);
|
||||||
fw.fileTree(dataDir, (err, list) => {
|
fw.fileTree(dataDir, (err, list) => {
|
||||||
@@ -72,13 +72,13 @@ describe('Test function fileTree', () => {
|
|||||||
test('nested files', (done) => {
|
test('nested files', (done) => {
|
||||||
utils.createEmptyDirs([
|
utils.createEmptyDirs([
|
||||||
path.join(dataDir, 'test', 'test'),
|
path.join(dataDir, 'test', 'test'),
|
||||||
path.join(dataDir, 'test2')
|
path.join(dataDir, 'test2'),
|
||||||
]);
|
]);
|
||||||
const fileList = [
|
const fileList = [
|
||||||
path.join(dataDir, 'f1.txt'),
|
path.join(dataDir, 'f1.txt'),
|
||||||
path.join(dataDir, 'test', 'f2.txt'),
|
path.join(dataDir, 'test', 'f2.txt'),
|
||||||
path.join(dataDir, 'test', 'test', 'f3.txt'),
|
path.join(dataDir, 'test', 'test', 'f3.txt'),
|
||||||
path.join(dataDir, 'test2', 'f4.txt')
|
path.join(dataDir, 'test2', 'f4.txt'),
|
||||||
];
|
];
|
||||||
utils.createEmptyFiles(fileList);
|
utils.createEmptyFiles(fileList);
|
||||||
fw.fileTree(dataDir, (err, list) => {
|
fw.fileTree(dataDir, (err, list) => {
|
||||||
@@ -120,7 +120,7 @@ describe('Test index article reading', () => {
|
|||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(info).toEqual({
|
expect(info).toEqual({
|
||||||
title: 'This is an awesome title !?¤',
|
title: 'This is an awesome title !?¤',
|
||||||
thumbnail: './thumbnail.jpg'
|
thumbnail: './thumbnail.jpg',
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -136,7 +136,7 @@ describe('Test index article reading', () => {
|
|||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(info).toEqual({
|
expect(info).toEqual({
|
||||||
title: undefined,
|
title: undefined,
|
||||||
thumbnail: './thumbnail.jpg'
|
thumbnail: './thumbnail.jpg',
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -148,7 +148,7 @@ describe('Test index article reading', () => {
|
|||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(info).toEqual({
|
expect(info).toEqual({
|
||||||
title: 'title',
|
title: 'title',
|
||||||
thumbnail: undefined
|
thumbnail: undefined,
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -164,7 +164,7 @@ describe('Test index article reading', () => {
|
|||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(info).toEqual({
|
expect(info).toEqual({
|
||||||
title: 'This is an awesome title !?¤',
|
title: 'This is an awesome title !?¤',
|
||||||
thumbnail: undefined
|
thumbnail: undefined,
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -181,7 +181,7 @@ describe('Test index article reading', () => {
|
|||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(info).toEqual({
|
expect(info).toEqual({
|
||||||
title: 'This is an awesome title !?¤',
|
title: 'This is an awesome title !?¤',
|
||||||
thumbnail: './thumbnail.jpg'
|
thumbnail: './thumbnail.jpg',
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -208,12 +208,12 @@ describe('Test article fetching', () => {
|
|||||||
test('misplaced index file', (done) => {
|
test('misplaced index file', (done) => {
|
||||||
utils.createEmptyDirs([
|
utils.createEmptyDirs([
|
||||||
path.join(dataDir, 'test', 'test'),
|
path.join(dataDir, 'test', 'test'),
|
||||||
path.join(dataDir, '2019', '05', '05')
|
path.join(dataDir, '2019', '05', '05'),
|
||||||
]);
|
]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, testIndex),
|
path.join(dataDir, testIndex),
|
||||||
path.join(dataDir, 'test', 'test', testIndex),
|
path.join(dataDir, 'test', 'test', testIndex),
|
||||||
path.join(dataDir, '2019', '05', testIndex)
|
path.join(dataDir, '2019', '05', testIndex),
|
||||||
]);
|
]);
|
||||||
fw.fetchArticles((err, dict) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
@@ -225,8 +225,8 @@ describe('Test article fetching', () => {
|
|||||||
test('empty index file', (done) => {
|
test('empty index file', (done) => {
|
||||||
const dir = path.join(dataDir, '2019', '05', '05');
|
const dir = path.join(dataDir, '2019', '05', '05');
|
||||||
const file = path.join(dir, testIndex);
|
const file = path.join(dir, testIndex);
|
||||||
utils.createEmptyDirs([dir]);
|
utils.createEmptyDirs([ dir ]);
|
||||||
utils.createEmptyFiles([file]);
|
utils.createEmptyFiles([ file ]);
|
||||||
const date = new Date(2019, 5, 5);
|
const date = new Date(2019, 5, 5);
|
||||||
date.setUTCHours(0);
|
date.setUTCHours(0);
|
||||||
fw.fetchArticles((err, dict) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
@@ -252,7 +252,7 @@ describe('Test article fetching', () => {
|
|||||||
test('correct index file', (done) => {
|
test('correct index file', (done) => {
|
||||||
const dir = path.join(dataDir, '2019', '05', '05');
|
const dir = path.join(dataDir, '2019', '05', '05');
|
||||||
const file = path.join(dir, testIndex);
|
const file = path.join(dir, testIndex);
|
||||||
utils.createEmptyDirs([dir]);
|
utils.createEmptyDirs([ dir ]);
|
||||||
fs.writeFileSync(file, `
|
fs.writeFileSync(file, `
|
||||||
# Title with : info !
|
# Title with : info !
|
||||||

|

|
||||||
@@ -283,7 +283,7 @@ describe('Test article fetching', () => {
|
|||||||
test('correct draft file', (done) => {
|
test('correct draft file', (done) => {
|
||||||
const dir = path.join(dataDir, '2019', '05', '05');
|
const dir = path.join(dataDir, '2019', '05', '05');
|
||||||
const file = path.join(dir, 'draft.md');
|
const file = path.join(dir, 'draft.md');
|
||||||
utils.createEmptyDirs([dir]);
|
utils.createEmptyDirs([ dir ]);
|
||||||
fs.writeFileSync(file, `
|
fs.writeFileSync(file, `
|
||||||
# Title with : info !
|
# Title with : info !
|
||||||

|

|
||||||
@@ -315,8 +315,11 @@ describe('Test article fetching', () => {
|
|||||||
const dir = path.join(dataDir, '2019', '05', '05');
|
const dir = path.join(dataDir, '2019', '05', '05');
|
||||||
const file = path.join(dir, testIndex);
|
const file = path.join(dir, testIndex);
|
||||||
const file2 = path.join(dir, 'draft.md');
|
const file2 = path.join(dir, 'draft.md');
|
||||||
utils.createEmptyDirs([dir]);
|
utils.createEmptyDirs([ dir ]);
|
||||||
utils.createEmptyFiles([file, file2]);
|
utils.createEmptyFiles([
|
||||||
|
file,
|
||||||
|
file2,
|
||||||
|
]);
|
||||||
const date = new Date(2019, 5, 5);
|
const date = new Date(2019, 5, 5);
|
||||||
date.setUTCHours(0);
|
date.setUTCHours(0);
|
||||||
fw.fetchArticles((err, dict) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
|
|||||||
@@ -0,0 +1,229 @@
|
|||||||
|
const mockClient = {
|
||||||
|
options: {},
|
||||||
|
connected: true,
|
||||||
|
on: () => { /* ignore */ },
|
||||||
|
};
|
||||||
|
|
||||||
|
jest.mock('redis', () => {
|
||||||
|
return {
|
||||||
|
createClient: (options) => {
|
||||||
|
mockClient.options = options;
|
||||||
|
return mockClient;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
test: true,
|
||||||
|
modules: {
|
||||||
|
hit_counter: true,
|
||||||
|
},
|
||||||
|
redis: {
|
||||||
|
host: 'test-host',
|
||||||
|
port: 'test-port',
|
||||||
|
},
|
||||||
|
hit_counter: {
|
||||||
|
unique_visitor_timeout: -1,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const hc = require('../src/hit_counter')(config, () => { /* ignore */ }, () => { /* ignore */ });
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
jest.resetModules();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('options passed to redis', () => {
|
||||||
|
expect(mockClient.options).toEqual(config['redis']);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('read()', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mockClient.hgetall = (_, cb) => {
|
||||||
|
cb();
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
test('read path', (done) => {
|
||||||
|
mockClient.hgetall = (path, cb) => {
|
||||||
|
expect(path).toBe('/test/path/');
|
||||||
|
cb(undefined, { h: 12, v: 34 });
|
||||||
|
};
|
||||||
|
hc.read('/test/path/', (data) => {
|
||||||
|
expect(data).toBeDefined();
|
||||||
|
expect(data.hits).toBe(12);
|
||||||
|
expect(data.visitors).toBe(34);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('read path with error', (done) => {
|
||||||
|
mockClient.hgetall = (path, cb) => {
|
||||||
|
expect(path).toBe('/test/path/');
|
||||||
|
cb('error', undefined);
|
||||||
|
};
|
||||||
|
hc.read('/test/path/', (data) => {
|
||||||
|
expect(data).toBeDefined();
|
||||||
|
expect(data.hits).toBe(0);
|
||||||
|
expect(data.visitors).toBe(0);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('read path with error 2', (done) => {
|
||||||
|
mockClient.hgetall = (path, cb) => {
|
||||||
|
expect(path).toBe('/test/path/');
|
||||||
|
cb(undefined, {});
|
||||||
|
};
|
||||||
|
hc.read('/test/path/', (data) => {
|
||||||
|
expect(data).toBeDefined();
|
||||||
|
expect(data.hits).toBe(0);
|
||||||
|
expect(data.visitors).toBe(0);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('count()', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mockClient.multi = () => mockClient;
|
||||||
|
mockClient.hincrby = () => mockClient;
|
||||||
|
mockClient.exec = (cb) => {
|
||||||
|
cb();
|
||||||
|
};
|
||||||
|
config['hit_counter']['unique_visitor_timeout'] = -1;
|
||||||
|
});
|
||||||
|
|
||||||
|
test('simple visit', (done) => {
|
||||||
|
let multiCalled = false;
|
||||||
|
let execCalled = false;
|
||||||
|
let hincrbyCalls = [];
|
||||||
|
mockClient.multi = () => {
|
||||||
|
multiCalled = true;
|
||||||
|
return mockClient;
|
||||||
|
};
|
||||||
|
mockClient.hincrby = (hash, key, value) => {
|
||||||
|
hincrbyCalls.push([
|
||||||
|
hash,
|
||||||
|
key,
|
||||||
|
value,
|
||||||
|
]);
|
||||||
|
return mockClient;
|
||||||
|
};
|
||||||
|
mockClient.exec = (cb) => {
|
||||||
|
execCalled = true;
|
||||||
|
cb();
|
||||||
|
};
|
||||||
|
hc.count({
|
||||||
|
headers: {},
|
||||||
|
connection: { remoteAddress: 'test1' },
|
||||||
|
}, '/test/path/1', () => {
|
||||||
|
expect(multiCalled).toBeTruthy();
|
||||||
|
expect(hincrbyCalls).toEqual([
|
||||||
|
[
|
||||||
|
'/test/path/1',
|
||||||
|
'h',
|
||||||
|
1,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'/test/path/1',
|
||||||
|
'v',
|
||||||
|
1,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
expect(execCalled).toBeTruthy();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('re-visit after long time', (done) => {
|
||||||
|
let hincrbyCalls = [];
|
||||||
|
mockClient.hincrby = (hash, key, value) => {
|
||||||
|
hincrbyCalls.push([
|
||||||
|
hash,
|
||||||
|
key,
|
||||||
|
value,
|
||||||
|
]);
|
||||||
|
return mockClient;
|
||||||
|
};
|
||||||
|
hc.count({
|
||||||
|
headers: {},
|
||||||
|
connection: { remoteAddress: 'test2' },
|
||||||
|
}, '/test/path/2', () => {
|
||||||
|
hc.count({
|
||||||
|
headers: {},
|
||||||
|
connection: { remoteAddress: 'test2' },
|
||||||
|
}, '/test/path/2', () => {
|
||||||
|
expect(hincrbyCalls).toEqual([
|
||||||
|
[
|
||||||
|
'/test/path/2',
|
||||||
|
'h',
|
||||||
|
1,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'/test/path/2',
|
||||||
|
'v',
|
||||||
|
1,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'/test/path/2',
|
||||||
|
'h',
|
||||||
|
1,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'/test/path/2',
|
||||||
|
'v',
|
||||||
|
1,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('re-visit after short time', (done) => {
|
||||||
|
config['hit_counter']['unique_visitor_timeout'] = 10000;
|
||||||
|
let hincrbyCalls = [];
|
||||||
|
mockClient.hincrby = (hash, key, value) => {
|
||||||
|
hincrbyCalls.push([
|
||||||
|
hash,
|
||||||
|
key,
|
||||||
|
value,
|
||||||
|
]);
|
||||||
|
return mockClient;
|
||||||
|
};
|
||||||
|
hc.count({
|
||||||
|
headers: {},
|
||||||
|
connection: { remoteAddress: 'test3' },
|
||||||
|
}, '/test/path/3', () => {
|
||||||
|
hc.count({
|
||||||
|
headers: {},
|
||||||
|
connection: { remoteAddress: 'test3' },
|
||||||
|
}, '/test/path/3', () => {
|
||||||
|
expect(hincrbyCalls).toEqual([
|
||||||
|
[
|
||||||
|
'/test/path/3',
|
||||||
|
'h',
|
||||||
|
1,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'/test/path/3',
|
||||||
|
'v',
|
||||||
|
1,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'/test/path/3',
|
||||||
|
'h',
|
||||||
|
1,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'/test/path/3',
|
||||||
|
'v',
|
||||||
|
0,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
+17
-19
@@ -6,24 +6,24 @@ const dataDir = 'test_data';
|
|||||||
const file = path.join(dataDir, 'test.md');
|
const file = path.join(dataDir, 'test.md');
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
'test': true,
|
test: true,
|
||||||
'modules': {
|
modules: {
|
||||||
'prism': true,
|
'prism': true,
|
||||||
'mathjax': true,
|
'mathjax': true,
|
||||||
'plantuml': true,
|
'plantuml': true,
|
||||||
'fa-diagrams': true,
|
'fa-diagrams': true,
|
||||||
},
|
},
|
||||||
'showdown': {
|
showdown: {
|
||||||
'simplifiedAutoLink': true,
|
simplifiedAutoLink: true,
|
||||||
'smartIndentationFix': true
|
smartIndentationFix: true,
|
||||||
},
|
},
|
||||||
'mathjax': {
|
mathjax: {
|
||||||
'output_format': 'html',
|
output_format: 'html',
|
||||||
'speak_text': false
|
speak_text: false,
|
||||||
|
},
|
||||||
|
plantuml: {
|
||||||
|
output_format: 'svg',
|
||||||
},
|
},
|
||||||
'plantuml': {
|
|
||||||
'output_format': 'svg'
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderer = require('../src/renderer')(config);
|
const renderer = require('../src/renderer')(config);
|
||||||
@@ -47,9 +47,7 @@ describe('get parts', () => {
|
|||||||
test('normal', () => {
|
test('normal', () => {
|
||||||
const data = 'Hello\nthere\ngeneral\nkenobi';
|
const data = 'Hello\nthere\ngeneral\nkenobi';
|
||||||
const parts = renderer.getParts(data);
|
const parts = renderer.getParts(data);
|
||||||
expect(parts.map(p => p.text)).toEqual([
|
expect(parts.map(p => p.text)).toEqual([ 'Hello\nthere\ngeneral\nkenobi' ]);
|
||||||
'Hello\nthere\ngeneral\nkenobi'
|
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
test('lot of stuff', () => {
|
test('lot of stuff', () => {
|
||||||
const data = 'Hello\nthere\n```code```\ngeneral<script>script</script>\n<script>script2</script>\n```<script>script3</script>```kenobi';
|
const data = 'Hello\nthere\n```code```\ngeneral<script>script</script>\n<script>script2</script>\n```<script>script3</script>```kenobi';
|
||||||
@@ -58,27 +56,27 @@ describe('get parts', () => {
|
|||||||
{
|
{
|
||||||
index: 0,
|
index: 0,
|
||||||
end: 12,
|
end: 12,
|
||||||
text: 'Hello\nthere\n'
|
text: 'Hello\nthere\n',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
index: 22,
|
index: 22,
|
||||||
end: 30,
|
end: 30,
|
||||||
text: '\ngeneral'
|
text: '\ngeneral',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
index: 53,
|
index: 53,
|
||||||
end: 54,
|
end: 54,
|
||||||
text: '\n'
|
text: '\n',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
index: 78,
|
index: 78,
|
||||||
end: 79,
|
end: 79,
|
||||||
text: '\n'
|
text: '\n',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
index: 109,
|
index: 109,
|
||||||
end: 115,
|
end: 115,
|
||||||
text: 'kenobi'
|
text: 'kenobi',
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|||||||
+2
-2
@@ -14,7 +14,7 @@ const deleteFolderSync = (dir) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
do {
|
do {
|
||||||
items = fs.readdirSync(dir, {withFileTypes: true});
|
items = fs.readdirSync(dir, { withFileTypes: true });
|
||||||
try {
|
try {
|
||||||
items.forEach(deleteItem);
|
items.forEach(deleteItem);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -26,6 +26,6 @@ const deleteFolderSync = (dir) => {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
deleteFolderSync: deleteFolderSync,
|
deleteFolderSync: deleteFolderSync,
|
||||||
createEmptyDirs: (list) => list.forEach((path) => fs.mkdirSync(path, {recursive: true})),
|
createEmptyDirs: (list) => list.forEach((path) => fs.mkdirSync(path, { recursive: true })),
|
||||||
createEmptyFiles: (list) => list.forEach((file) => fs.writeFileSync(file, '')),
|
createEmptyFiles: (list) => list.forEach((file) => fs.writeFileSync(file, '')),
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user