Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

network-avatar-picker

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

network-avatar-picker - npm Package Compare versions

Comparing version 1.3.9 to 1.4.0

lib/cache/index.js

30

lib/index.js

@@ -5,22 +5,20 @@ 'use strict';

var facebook = require('./providers/facebook');
var github = require('./providers/github');
var gmail = require('./providers/gmail');
var instagram = require('./providers/instagram');
var tumblr = require('./providers/tumblr');
var twitter = require('./providers/twitter');
var vimeo = require('./providers/vimeo');
var youtube = require('./providers/youtube');
var cache = require('./cache');
var providersService = require('./providers');
var NetworkAvatarPicker = function NetworkAvatarPicker() {
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, NetworkAvatarPicker);
this.facebook = facebook.init();
this.github = github.init();
this.gmail = gmail.init();
this.instagram = instagram.init();
this.tumblr = tumblr.init();
this.twitter = twitter.init();
this.vimeo = vimeo.init();
this.youtube = youtube.init();
var cacheService = cache.init(config);
var providers = providersService.init(cacheService);
this.facebook = providers.facebook;
this.github = providers.github;
this.gmail = providers.gmail;
this.instagram = providers.instagram;
this.tumblr = providers.tumblr;
this.twitter = providers.twitter;
this.vimeo = providers.vimeo;
this.youtube = providers.youtube;
if (new.target === NetworkAvatarPicker) {

@@ -27,0 +25,0 @@ Object.freeze(this);

'use strict';
var _require = require('../utils/avatarService'),
extractProfileImageUrl = _require.extractProfileImageUrl,
downloadImage = _require.downloadImage;
extractProfileImageUrl = _require.extractProfileImageUrl;

@@ -10,5 +9,7 @@ var _require2 = require('../utils/validationService'),

var _require3 = require('../utils/common'),
var _require3 = require('../common/constants'),
FACEBOOK = _require3.FACEBOOK;
var baseProvider = require('./base');
function getUserProfileUrl(username) {

@@ -18,15 +19,19 @@ return 'https://mobile.facebook.com/' + username;

var facebookProvider = {
getAvatarUrl: async function getAvatarUrl(username) {
validateUsernameInput(username);
return extractProfileImageUrl(getUserProfileUrl(username), FACEBOOK);
},
getAvatar: async function getAvatar(username) {
var profileImageUrl = await this.getAvatarUrl(username);
return downloadImage(profileImageUrl, FACEBOOK);
}
};
module.exports.init = function () {
return Object.assign(Object.create(facebookProvider));
module.exports.init = function (cacheService) {
var base = baseProvider.init(cacheService);
return Object.assign(Object.create(base), {
provider: FACEBOOK,
getAvatarUrl: async function getAvatarUrl(username) {
validateUsernameInput(username);
if (cacheService) {
var _url = await cacheService.getCachedValue(this.provider + '/profileUrl/' + username);
if (_url) return _url;
}
var url = await extractProfileImageUrl(getUserProfileUrl(username), this.provider);
if (cacheService) {
await cacheService.setCachedValue(this.provider + '/profileUrl/' + username, url);
}
return url;
}
});
};
'use strict';
var _require = require('../utils/avatarService'),
downloadImage = _require.downloadImage;
var _require = require('../utils/validationService'),
validateUsernameInput = _require.validateUsernameInput;
var _require2 = require('../utils/validationService'),
validateUsernameInput = _require2.validateUsernameInput;
var _require2 = require('../common/constants'),
GITHUB = _require2.GITHUB;
var _require3 = require('../utils/common'),
GITHUB = _require3.GITHUB;
var baseProvider = require('./base');
var githubProvider = {
getAvatarUrl: async function getAvatarUrl(username) {
validateUsernameInput(username);
return 'https://github.com/' + username + '.png';
},
getAvatar: async function getAvatar(username) {
var profileImageUrl = await this.getAvatarUrl(username);
return downloadImage(profileImageUrl, GITHUB);
}
};
module.exports.init = function () {
return Object.assign(Object.create(githubProvider));
module.exports.init = function (cacheService) {
var base = baseProvider.init(cacheService);
return Object.assign(Object.create(base), {
provider: GITHUB,
getAvatarUrl: async function getAvatarUrl(username) {
validateUsernameInput(username);
return 'https://github.com/' + username + '.png';
}
});
};
'use strict';
var _require = require('../utils/avatarService'),
downloadImage = _require.downloadImage;
var _require = require('../utils/validationService'),
validateGmail = _require.validateGmail;
var _require2 = require('../utils/validationService'),
validateGmail = _require2.validateGmail;
var _require2 = require('../utils/hashService'),
md5 = _require2.md5;
var _require3 = require('../utils/common'),
md5 = _require3.md5,
var _require3 = require('../common/constants'),
GMAIL = _require3.GMAIL;
var gmailProvider = {
getAvatarUrl: async function getAvatarUrl(username) {
validateGmail(username);
return 'https://gravatar.com/avatar/' + md5(username) + '?size=500';
},
getAvatar: async function getAvatar(username) {
var profileImageUrl = await this.getAvatarUrl(username);
return downloadImage(profileImageUrl, GMAIL);
}
};
var baseProvider = require('./base');
module.exports.init = function () {
return Object.assign(Object.create(gmailProvider));
module.exports.init = function (cacheService) {
var base = baseProvider.init(cacheService);
return Object.assign(Object.create(base), {
provider: GMAIL,
getAvatarUrl: async function getAvatarUrl(email) {
validateGmail(email);
return 'https://gravatar.com/avatar/' + md5(email) + '?size=500';
}
});
};
'use strict';
var _require = require('../utils/avatarService'),
extractProfileImageUrl = _require.extractProfileImageUrl,
downloadImage = _require.downloadImage;
extractProfileImageUrl = _require.extractProfileImageUrl;

@@ -10,5 +9,7 @@ var _require2 = require('../utils/validationService'),

var _require3 = require('../utils/common'),
var _require3 = require('../common/constants'),
INSTAGRAM = _require3.INSTAGRAM;
var baseProvider = require('./base');
function getUserProfileUrl(username) {

@@ -18,15 +19,20 @@ return 'https://www.instagram.com/' + username;

var instagramProvider = {
getAvatarUrl: async function getAvatarUrl(username) {
validateUsernameInput(username);
return extractProfileImageUrl(getUserProfileUrl(username), INSTAGRAM);
},
getAvatar: async function getAvatar(username) {
var profileImageUrl = await this.getAvatarUrl(username);
return downloadImage(profileImageUrl, INSTAGRAM);
}
};
module.exports.init = function () {
return Object.assign(Object.create(instagramProvider));
module.exports.init = function (cacheService) {
var base = baseProvider.init(cacheService);
return Object.assign(Object.create(base), {
provider: INSTAGRAM,
getAvatarUrl: async function getAvatarUrl(username) {
validateUsernameInput(username);
var cache = this.getCache();
if (cache) {
var _url = await cache.getCachedValue(this.provider + '/profileUrl/' + username);
if (_url) return _url;
}
var url = await extractProfileImageUrl(getUserProfileUrl(username), this.provider);
if (cache) {
await cache.setCachedValue(this.provider + '/profileUrl/' + username, url);
}
return url;
}
});
};
'use strict';
var _require = require('../utils/avatarService'),
downloadImage = _require.downloadImage;
var _require = require('../utils/validationService'),
validateUsernameInput = _require.validateUsernameInput;
var _require2 = require('../utils/validationService'),
validateUsernameInput = _require2.validateUsernameInput;
var _require2 = require('../common/constants'),
TUMBLR = _require2.TUMBLR;
var _require3 = require('../utils/common'),
TUMBLR = _require3.TUMBLR;
var baseProvider = require('./base');
var tumblrProvider = {
getAvatarUrl: async function getAvatarUrl(username) {
validateUsernameInput(username);
return 'https://api.tumblr.com/v2/blog/' + username + '/avatar';
},
getAvatar: async function getAvatar(username) {
var profileImageUrl = await this.getAvatarUrl(username);
return downloadImage(profileImageUrl, TUMBLR);
}
};
module.exports.init = function () {
return Object.assign(Object.create(tumblrProvider));
module.exports.init = function (cacheService) {
var base = baseProvider.init(cacheService);
return Object.assign(Object.create(base), {
provider: TUMBLR,
getAvatarUrl: async function getAvatarUrl(username) {
validateUsernameInput(username);
return 'https://api.tumblr.com/v2/blog/' + username + '/avatar';
}
});
};
'use strict';
var _require = require('../utils/avatarService'),
downloadImage = _require.downloadImage;
var _require = require('../utils/validationService'),
validateUsernameInput = _require.validateUsernameInput;
var _require2 = require('../utils/validationService'),
validateUsernameInput = _require2.validateUsernameInput;
var _require2 = require('../common/constants'),
TWITTER = _require2.TWITTER;
var _require3 = require('../utils/common'),
TWITTER = _require3.TWITTER;
var baseProvider = require('./base');
var twitterProvider = {
getAvatarUrl: async function getAvatarUrl(username) {
validateUsernameInput(username);
return 'https://twitter.com/' + username + '/profile_image?size=original';
},
getAvatar: async function getAvatar(username) {
var profileImageUrl = await this.getAvatarUrl(username);
return downloadImage(profileImageUrl, TWITTER);
}
};
module.exports.init = function () {
return Object.assign(Object.create(twitterProvider));
module.exports.init = function (cacheService) {
var base = baseProvider.init(cacheService);
return Object.assign(Object.create(base), {
provider: TWITTER,
getAvatarUrl: async function getAvatarUrl(username) {
validateUsernameInput(username);
return 'https://twitter.com/' + username + '/profile_image?size=original';
}
});
};
'use strict';
var _require = require('../utils/avatarService'),
extractProfileImageUrl = _require.extractProfileImageUrl,
downloadImage = _require.downloadImage;
extractProfileImageUrl = _require.extractProfileImageUrl;

@@ -10,5 +9,7 @@ var _require2 = require('../utils/validationService'),

var _require3 = require('../utils/common'),
var _require3 = require('../common/constants'),
VIMEO = _require3.VIMEO;
var baseProvider = require('./base');
function getUserProfileUrl(username) {

@@ -18,15 +19,20 @@ return 'https://www.vimeo.com/' + username;

var vimeoProvider = {
getAvatarUrl: async function getAvatarUrl(username) {
validateUsernameInput(username);
return extractProfileImageUrl(getUserProfileUrl(username), VIMEO);
},
getAvatar: async function getAvatar(username) {
var profileImageUrl = await this.getAvatarUrl(username);
return downloadImage(profileImageUrl, VIMEO);
}
};
module.exports.init = function () {
return Object.assign(Object.create(vimeoProvider));
module.exports.init = function (cacheService) {
var base = baseProvider.init(cacheService);
return Object.assign(Object.create(base), {
provider: VIMEO,
getAvatarUrl: async function getAvatarUrl(username) {
validateUsernameInput(username);
var cache = this.getCache();
if (cache) {
var _url = await cache.getCachedValue(this.provider + '/profileUrl/' + username);
if (_url) return _url;
}
var url = await extractProfileImageUrl(getUserProfileUrl(username), this.provider);
if (cache) {
await cache.setCachedValue(this.provider + '/profileUrl/' + username, url);
}
return url;
}
});
};
'use strict';
var _require = require('../utils/avatarService'),
extractProfileImageUrl = _require.extractProfileImageUrl,
downloadImage = _require.downloadImage;
extractProfileImageUrl = _require.extractProfileImageUrl;

@@ -10,5 +9,7 @@ var _require2 = require('../utils/validationService'),

var _require3 = require('../utils/common'),
var _require3 = require('../common/constants'),
YOUTUBE = _require3.YOUTUBE;
var baseProvider = require('./base');
function getUserProfileUrl(username) {

@@ -18,15 +19,20 @@ return 'https://www.youtube.com/user/' + username;

var youtubeProvider = {
getAvatarUrl: async function getAvatarUrl(username) {
validateUsernameInput(username);
return extractProfileImageUrl(getUserProfileUrl(username), YOUTUBE);
},
getAvatar: async function getAvatar(username) {
var profileImageUrl = await this.getAvatarUrl(username);
return downloadImage(profileImageUrl, YOUTUBE);
}
};
module.exports.init = function () {
return Object.assign(Object.create(youtubeProvider));
module.exports.init = function (cacheService) {
var base = baseProvider.init(cacheService);
return Object.assign(Object.create(base), {
provider: YOUTUBE,
getAvatarUrl: async function getAvatarUrl(username) {
validateUsernameInput(username);
var cache = this.getCache();
if (cache) {
var _url = await cache.getCachedValue(this.provider + '/profileUrl/' + username);
if (_url) return _url;
}
var url = await extractProfileImageUrl(getUserProfileUrl(username), this.provider);
if (cache) {
await cache.setCachedValue(this.provider + '/profileUrl/' + username, url);
}
return url;
}
});
};

@@ -15,2 +15,4 @@ 'use strict';

var TTL_REDIS = 3600;
var md5 = function md5(input) {

@@ -30,3 +32,4 @@ return crypto.createHash('md5').update(input).digest('hex');

GRAVATAR: GRAVATAR,
md5: md5
md5: md5,
TTL_REDIS: TTL_REDIS
};

@@ -32,6 +32,19 @@ 'use strict';

function validateRedisConfig(redisConfig) {
if (!redisConfig) {
throw new Error('Redis configuration required');
}
if (!redisConfig.host) {
throw new Error('Redis host required');
}
if (!redisConfig.port) {
throw new Error('Redis port required');
}
}
module.exports = {
validateUsernameInput: validateUsernameInput,
validateEmail: validateEmail,
validateGmail: validateGmail
validateGmail: validateGmail,
validateRedisConfig: validateRedisConfig
};
{
"name": "network-avatar-picker",
"version": "1.3.9",
"version": "1.4.0",
"description": "A picker for user's networks profile image.",

@@ -11,3 +11,3 @@ "main": "lib/index.js",

"coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
"tests": "nyc mocha tests --recursive --timeout 15000"
"tests": "nyc mocha tests --recursive --timeout 15000 --exit"
},

@@ -42,2 +42,3 @@ "repository": {

"cheerio": "^1.0.0-rc.3",
"redis": "^2.8.0",
"request": "^2.82.0"

@@ -59,2 +60,3 @@ },

"nyc": "^14.0.0",
"redis-mock": "^0.43.0",
"sinon": "^7.3.2",

@@ -61,0 +63,0 @@ "supertest": "^4.0.2"

@@ -19,4 +19,6 @@ # network-avatar-picker

## Usage
## Getting Started
### Installing
First, install `network-avatar-picker` as a dependency:

@@ -28,13 +30,37 @@

Then you should require in order to be able use it:
### Usage
#### Basic configuration
You should require the dependency in order to be able use it:
```javascript
const AvatarPickerService = require('network-avatar-picker');
const avatarPicker = new AvatarPickerService();
const NetworkAvatarPicker = require('network-avatar-picker');
const avatarPicker = new NetworkAvatarPicker();
```
Use the `async` methods of the `avatarPicker` instance to get user avatars:
#### Cache configuration using Redis
**Get avatar as Buffer:**
In 1.4.0 we have introduced support to cache results with Redis! Just pass redis config as param to the NetworkAvatarPicker and it will create a new redis client. Then, we will store the images and avatar's URL to redis.
```javascript
const NetworkAvatarPicker = require('network-avatar-picker');
const avatarPicker = new NetworkAvatarPicker({
redis: {
host: '127.0.0.1', // required
port: '6379', // required
password : 'your password', // optional: replace with your password
ttl: 3600, // optional: Add your expiration caching time in seconds. Default value: 3600
}
});
```
This way we create a local Redis client with expiration caching time 3600sec.
### Methods
Use the `async` methods of the `avatarPicker` instance to fetch user avatars:
A) **getAvatar**: Fetch avatar image as **Buffer**
- `avatarPicker.facebook.getAvatar(username)`

@@ -49,3 +75,3 @@ - `avatarPicker.twitter.getAvatar(username)`

**Get avatar as URL:**
B) **getAvatarUrl**: Fetch avatar image as **URL**

@@ -62,3 +88,3 @@ - `avatarPicker.facebook.getAvatarUrl(username)`

## Example
## Examples

@@ -142,1 +168,9 @@ 1) Fetch **twitter's** cnn user profile picture:

```
## Running the tests
In order to run tests you have to run:
```shell
npm run tests
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc