You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
631 B
30 lines
631 B
'use strict'; |
|
|
|
const { createHash } = require('crypto'); |
|
const { stringify } = require('querystring'); |
|
|
|
const Cache = require('./cache'); |
|
const cache = new Cache(); |
|
|
|
function md5(str) { |
|
return createHash('md5').update(str).digest('hex'); |
|
} |
|
|
|
function gravatarHelper(email, options) { |
|
if (typeof options === 'number') { |
|
options = {s: options}; |
|
} |
|
|
|
const hash = cache.has(email) ? cache.get(email) : md5(email.toLowerCase()); |
|
let str = `https://www.gravatar.com/avatar/${hash}`; |
|
|
|
const qs = stringify(options); |
|
|
|
if (qs) str += `?${qs}`; |
|
|
|
cache.set('email', hash); |
|
|
|
return str; |
|
} |
|
|
|
module.exports = gravatarHelper;
|
|
|