Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- 8
- 10
- node
- lts/*
9 changes: 9 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# 2.0.0

* Removed querystring dependency
* Update node minimum requirement to [node 10.X+](https://nodejs.org/docs/latest-v6.x/api/url.html)
* Officially support [current web browsers](https://caniuse.com/#search=URLSearchParams)
* Fix [command injection vulnerability](https://npmjs.com/advisories/146) - note this only affected testing, not using `gravatar`

# 1.8.0

* Allows pass `cdn` information when generating gravatar url (sqlwwx)

# 1.7.0

* yarn support
* Node 8+ compatibility
* Upgraded deps
Expand Down
14 changes: 7 additions & 7 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
[![Build Status](https://secure.travis-ci.org/emerleite/node-gravatar.svg)](http://travis-ci.org/emerleite/node-gravatar)

Node.js Gravatar library
Gravatar
========================
A library to generate Gravatar URLs in Node.js
A library to generate [Gravatar](http://gravatar.com) URLs in the browser or node.js
Based on gravatar specs - <http://en.gravatar.com/site/implement/hash/> and <http://en.gravatar.com/site/implement/images/>

Dependencies
------------

### Runtime
* Node 0.8.X+
* [node 10.X+](https://nodejs.org/docs/latest-v10.x/api/url.html) and newer
* A [current web browser](https://caniuse.com/#search=URLSearchParams)

### Development/Tests
* mocha
Expand Down Expand Up @@ -96,12 +97,11 @@ gravatar profile somebody@example.com
```


Running tests (3 ways)
Running tests (2 ways)
----------------------
```sh
$ npm test
$ mocha (installed global)
$ node_modules/mocha/bin/mocha
$ npm run test
$ npx mocha
```

To-Do
Expand Down
67 changes: 33 additions & 34 deletions lib/gravatar.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,58 @@
var md5 = require('blueimp-md5'),
querystring = require('querystring'),
MD5_REGEX = /^[0-9a-f]{32}$/;
MD5_REGEX = /^[0-9a-f]{32}$/;

function params(options) {
var params = {}, removing = {protocol:1, format:1};
var params = {}, removing = { protocol: 1, format: 1 };
for (var key in options) {
if (!removing[key]) params[key] = options[key];
}
return params;
}
function proto(options, protocol) {
if (!options) return;
if(typeof options.protocol === 'boolean') return options.protocol;
if (typeof options.protocol === 'boolean') return options.protocol;
return options.protocol === "http" ? false
: options.protocol === "https" ? true
: undefined;
: options.protocol === "https" ? true
: undefined;
}
function getHash(email){
function getHash(email) {
email = (typeof email === 'string') ? email.trim().toLowerCase() : 'unspecified';
return email.match(MD5_REGEX) ? email : md5(email);
}

function getQueryString(options){
var queryData = querystring.stringify(params(options));
function getQueryString(options) {
var queryData = new URLSearchParams(params(options)).toString();
return (queryData && "?" + queryData) || "";
}

var gravatar = module.exports = {

url: function (email, options, protocol) {
var baseURL = "//www.gravatar.com/avatar/";
if (options && options.cdn) {
baseURL = options.cdn + '/avatar/';
delete options.cdn;
} else {
if (options && options.protocol) protocol = proto(options);
if(typeof protocol !== 'undefined') {
baseURL = protocol ? "https://s.gravatar.com/avatar/" : 'http://www.gravatar.com/avatar/';
}
url: function (email, options, protocol) {
var baseURL = "//www.gravatar.com/avatar/";
if (options && options.cdn) {
baseURL = options.cdn + '/avatar/';
delete options.cdn;
} else {
if (options && options.protocol) protocol = proto(options);
if (typeof protocol !== 'undefined') {
baseURL = protocol ? "https://s.gravatar.com/avatar/" : 'http://www.gravatar.com/avatar/';
}
var query = getQueryString(options);
return baseURL + getHash(email) + query;
},
}
var query = getQueryString(options);
return baseURL + getHash(email) + query;
},

profile_url: function (email, options, https) {
var format = options != undefined && options.format != undefined ? String(options.format) : 'json'
var baseURL
if (options && options.cdn) {
baseURL = options.cdn + '/';
delete options.cdn;
} else {
if (options && options.protocol) https = proto(options);
var baseURL = (https && "https://secure.gravatar.com/") || 'http://www.gravatar.com/';
}
var query = getQueryString(options);
return baseURL + getHash(email) + '.' + format + query;
profile_url: function (email, options, https) {
var format = options != undefined && options.format != undefined ? String(options.format) : 'json'
var baseURL
if (options && options.cdn) {
baseURL = options.cdn + '/';
delete options.cdn;
} else {
if (options && options.protocol) https = proto(options);
var baseURL = (https && "https://secure.gravatar.com/") || 'http://www.gravatar.com/';
}
var query = getQueryString(options);
return baseURL + getHash(email) + '.' + format + query;
}
};
Loading