Skip to content
Permalink
Browse files

fix: Properly parse gravatar boolean and switch to standard linter.

Signed-off-by: Richie Bendall <richiebendall@gmail.com>
  • Loading branch information
Richienb committed Jan 14, 2020
1 parent dfcaa30 commit f189d33f29bb0e196ecc6422905b208cee6e5e93
Showing with 534 additions and 826 deletions.
  1. +4 −4 middleware/cors.js
  2. +22 −22 middleware/load-options.js
  3. +13 −13 middleware/load-user.js
  4. +6 −8 package.json
  5. +35 −35 routes/get.js
  6. +49 −34 routes/post.js
  7. +6 −6 routes/utils.js
  8. +31 −31 server.js
  9. +33 −33 test.js
  10. +2 −2 users/volmering.json
  11. +333 −638 yarn.lock
@@ -1,8 +1,8 @@
module.exports = (_req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Origin', '*')
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
};
)
next()
}
@@ -1,63 +1,63 @@
const currentYear = new Date().getFullYear();
const currentYear = new Date().getFullYear()

module.exports = (req, res, next) => {
const parts = req.url.split('/');
const parts = req.url.split('/')

res.locals.options = parts.reduce(
(acc, curr) => {
if (!curr) return acc;
if (!curr) return acc

let match = curr.match(/^@?(\d{4})$/) || [];
let match = curr.match(/^@?(\d{4})$/) || []

if (match.length) {
// Pinned year
if (curr.startsWith('@')) {
acc.pinnedYear = parseInt(curr.substr(1), 10);
acc.pinnedYear = parseInt(curr.substr(1), 10)
} else {
acc.startYear = parseInt(curr, 10);
acc.startYear = parseInt(curr, 10)
}
return acc;
return acc
}

match = curr.match(/^(\d{4})-(\d{4})$/) || [];
match = curr.match(/^(\d{4})-(\d{4})$/) || []

if (match.length) {
acc.startYear = parseInt(match[1], 10);
acc.endYear = parseInt(match[2], 10);
acc.startYear = parseInt(match[1], 10)
acc.endYear = parseInt(match[2], 10)

return acc;
return acc
}

if (curr.startsWith('license')) {
acc.format = curr
.split('.')
.pop()
.trim();
return acc;
.trim()
return acc
}

if (curr.startsWith('+')) {
acc.license = curr.substr(1).toUpperCase();
return acc;
acc.license = curr.substr(1).toUpperCase()
return acc
}

acc.sha = curr; // not actually supported now - 2019-06-19
return acc;
acc.sha = curr // not actually supported now - 2019-06-19
return acc
},
{
format: 'html',
startYear: null,
endYear: currentYear,
sha: null,
sha: null
}
);
)

if (res.locals.options.sha) {
res.setHeader(
'X-note',
'SHA and commit pinning is no longer supported, showing you latest release'
);
)
}

next();
};
next()
}
@@ -1,35 +1,35 @@
const fs = require('fs-extra');
const path = require('path');
const fs = require('fs-extra')
const path = require('path')

module.exports = async (req, res, next) => {
const id = req.hostname.split('.')[0];
res.locals.id = id;
const id = req.hostname.split('.')[0]
res.locals.id = id

if (req.method.toUpperCase() !== 'GET') {
return next();
return next()
}

// Otherwise load up the user json file
res.locals.user = {
copyright: '<copyright holders>',
};
copyright: '<copyright holders>'
}

try {
const data = await fs.readFile(
path.join(__dirname, '..', 'users', `${id}.json`),
'utf8'
);
res.locals.user = { ...res.locals.user, ...JSON.parse(data) };
)
res.locals.user = { ...res.locals.user, ...JSON.parse(data) }
} catch ({ code, message }) {
if (code !== 'ENOENT') {
res
.code(500)
.send(
`An internal error occurred - open an issue on https://github.com/remy/mit-license with the following information: ${message}`
);
return;
)
return
}
}

next();
};
next()
}
@@ -23,7 +23,7 @@
"dev": "nodemon .",
"serve": "node server.js",
"test": "node test.js",
"lint": "eslint server.js middleware/*.js routes/*.js --color"
"lint": "standard"
},
"bugs": {
"url": "https://github.com/remy/mit-license/issues"
@@ -41,17 +41,15 @@
"postcss-middleware": "^1.1.4",
"postcss-preset-env": "^6.7.0",
"serve-favicon": "^2.5.0",
"temp-dir": "^2.0.0"
"temp-dir": "^2.0.0",
"yn": "^4.0.0"
},
"devDependencies": {
"@remy/eslint": "^3.2.2",
"babel-eslint": "^10.0.3",
"css": "^2.2.4",
"eslint": "^6.8.0",
"eslint-plugin-node": "^11.0.0",
"has-flag": "^4.0.0",
"husky": "^3.1.0",
"nodemon": "^2.0.2"
"husky": "^4.0.7",
"nodemon": "^2.0.2",
"standard": "^14.3.1"
},
"resolutions": {
"postcss-middleware/vinyl-fs/glob-stream/micromatch/braces": "^3.0.2"
@@ -1,96 +1,96 @@
const md5 = require('md5');
const path = require('path');
const { stripTags, escapeTags, unescapeTags } = require('./utils');
const _ = require('lodash');
const md5 = require('md5')
const path = require('path')
const { stripTags, escapeTags, unescapeTags } = require('./utils')
const _ = require('lodash')

function getCopyrightHTML(user, plain) {
let html = '';
function getCopyrightHTML (user, plain) {
let html = ''

const name = _.isString(user)
? user
: plain
? user.name || user.copyright
: escapeTags(user.name || user.copyright);
: escapeTags(user.name || user.copyright)

if (user.url) {
html = `<a href="${stripTags(user.url)}">${name}</a>`;
html = `<a href="${stripTags(user.url)}">${name}</a>`
} else {
html = name;
html = name
}

if (user.email) {
html += ` &lt;<a href="mailto:${stripTags(user.email)}">${
plain ? user.email : escapeTags(user.email)
}</a>&gt;`;
}</a>&gt;`
}

return html;
return html
}

module.exports = (req, res) => {
const { user, options } = res.locals;
let name;
let gravatar;
const { user, options } = res.locals
let name
let gravatar

// No error and valid
if (user.copyright) {
if (_.isString(user.copyright)) {
name = getCopyrightHTML(user, options.format !== 'html');
name = getCopyrightHTML(user, options.format !== 'html')
} else if (_.isArray(user.copyright) && user.copyright.every(val => _.isString(val))) {
// Supports: ['Remy Sharp', 'Richie Bendall']
name = user.copyright
.map(v => (options.format !== 'html' ? v : escapeTags(v)))
.join(', ');
.join(', ')
} else {
name = user.copyright.map(getCopyrightHTML).join(', ');
name = user.copyright.map(getCopyrightHTML).join(', ')
}
}

if (user.gravatar && user.email) {
// Supports regular format
gravatar = `<img id="gravatar" alt="Profile image" src="https://www.gravatar.com/avatar/${md5(
user.email.trim().toLowerCase()
)}" />`;
)}" />`
} else if (_.isObject(user.copyright[0]) && user.gravatar) {
// Supports multi-user format
gravatar = `<img id="gravatar" alt="Profile image" src="https://www.gravatar.com/avatar/${md5(
user.copyright[0].email.trim().toLowerCase()
)}" />`;
)}" />`
}

const year = options.pinnedYear
? options.pinnedYear
: [options.startYear, options.endYear].filter(Boolean).join('-');
const license = (options.license || user.license || 'MIT').toUpperCase();
const format = options.format || user.format || 'html';
: [options.startYear, options.endYear].filter(Boolean).join('-')
const license = (options.license || user.license || 'MIT').toUpperCase()
const format = options.format || user.format || 'html'

const args = {
info: `${year} ${name}`,
theme: user.theme || 'default',
gravatar,
};
gravatar
}

const filename = path.join(__dirname, '..', 'licenses', license);
const filename = path.join(__dirname, '..', 'licenses', license)
req.app.render(filename, args, (error, content) => {
if (error) {
res.status(500).send(error);
return;
res.status(500).send(error)
return
}

if (format === 'txt') {
const plain = content.match(/<article>(.*)<\/article>/ms)[1];
const plain = content.match(/<article>(.*)<\/article>/ms)[1]

res
.set('Content-Type', 'text/plain; charset=UTF-8')
.send(unescapeTags(stripTags(plain)).trim());
return;
.send(unescapeTags(stripTags(plain)).trim())
return
}

if (format === 'html') {
res.send(content);
return;
res.send(content)
return
}

res.json({ ...user, ...options });
});
};
res.json({ ...user, ...options })
})
}

0 comments on commit f189d33

Please sign in to comment.
You can’t perform that action at this time.