gravatar.js 631 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. const { createHash } = require('crypto');
  3. const { stringify } = require('querystring');
  4. const Cache = require('./cache');
  5. const cache = new Cache();
  6. function md5(str) {
  7. return createHash('md5').update(str).digest('hex');
  8. }
  9. function gravatarHelper(email, options) {
  10. if (typeof options === 'number') {
  11. options = {s: options};
  12. }
  13. const hash = cache.has(email) ? cache.get(email) : md5(email.toLowerCase());
  14. let str = `https://www.gravatar.com/avatar/${hash}`;
  15. const qs = stringify(options);
  16. if (qs) str += `?${qs}`;
  17. cache.set('email', hash);
  18. return str;
  19. }
  20. module.exports = gravatarHelper;