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

phonedb

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

phonedb - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

25

package.json
{
"name": "phonedb",
"version": "0.3.0",
"version": "0.3.1",
"description": "Contact list storage and management for Node",

@@ -19,17 +19,20 @@ "keywords": [

"scripts": {
"test": "istanbul cover node_modules/mocha/bin/_mocha && codecov"
"redis-start": "redis-server /usr/local/etc/redis.conf &",
"redis-stop": "redis-cli shutdown",
"test": "nyc mocha",
"coverage": "nyc report --reporter=text-lcov > coverage/coverage.lcov && ./node_modules/.bin/codecov"
},
"dependencies": {
"debug": "^2.6.3",
"google-libphonenumber": "^2.0.11",
"redis": "^2.7.1"
"debug": "^3.1.0",
"google-libphonenumber": "^3.0.8",
"redis": "^2.8.0"
},
"devDependencies": {
"codecov": "^2.0.1",
"eslint": "^3.17.1",
"eslint-config-airbnb-base": "^11.1.1",
"eslint-plugin-import": "^2.2.0",
"istanbul": "^0.4.5",
"mocha": "^3.2.0"
"codecov": "^2.3.1",
"eslint": "^4.11.0",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.8.0",
"mocha": "^4.0.1",
"nyc": "*"
}
}

@@ -8,2 +8,3 @@ /**

const phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();
const { promisify } = require('util');

@@ -18,49 +19,34 @@ const REGISTERED_KEY = 'phonedb:registered';

this.client = redis;
this.sadd = promisify(redis.sadd).bind(redis);
this.sinter = promisify(redis.sinter).bind(redis);
this.smembers = promisify(redis.smembers).bind(redis);
}
/**
* register() adds a phone number to the app phone number set
* register() adds a phone number to the app phone number set.
* Throws an error for an invalid number.
* Returns an empty Promise.
*/
register(phone) {
return new Promise((resolve, reject) => {
if (phoneUtil.isValidNumber(phoneUtil.parse(phone)) === false) {
throw new Error(`Invalid phone number: ${phone}`);
}
this.client.sadd(REGISTERED_KEY, phone, (err) => {
if (err) { reject(err); }
debug(`[PhoneDB] Added ${phone} to ${REGISTERED_KEY}`);
resolve();
});
});
if (phoneUtil.isValidNumber(phoneUtil.parse(phone)) === false) {
throw new Error(`Invalid phone number: ${phone}`);
}
return this.sadd(REGISTERED_KEY, phone);
}
/**
* addContacts() stores a user's contact list
* addContacts() stores a user's contact list.
* Throws an error for an invalid number.
* Returns a Promise with the number of contacts added.
*/
addContacts(userId, contacts) {
return new Promise((resolve, reject) => {
if (userId == null) {
reject(new Error('A userId is required.'));
}
try {
// filter out invalid numbers
const numbers = contacts.filter(
contact => phoneUtil.isValidNumber(phoneUtil.parse(contact)));
if (userId == null) {
throw new Error('A userId is required.');
}
// filter out invalid numbers
const numbers = contacts.filter(contact => phoneUtil.isValidNumber(phoneUtil.parse(contact)));
// create key for user
const key = `user:${userId}:contacts`;
// add filtered numbers to Redis
this.client.sadd(key, numbers, (err, res) => {
if (err) { reject(err); }
debug(`[PhoneDB] Added ${res} contacts to ${key}`);
resolve(res);
});
} catch (err) {
debug(`[PhoneDB] Caught ${err}. Rejecting Promise.`);
reject(err);
}
});
// add filtered numbers to Redis
return this.sadd(`user:${userId}:contacts`, numbers);
}

@@ -72,3 +58,3 @@

*/
getMutualContacts(userId = null, otherUserId = null, registered = false) {
async getMutualContacts(userId = null, otherUserId = null, registered = false) {
if ((userId == null) || (otherUserId == null)) {

@@ -81,18 +67,10 @@ throw new Error('A userId and otherUserId are required.');

if (registered === false) {
return new Promise((resolve, reject) => {
this.client.sinter(userKey, otherUserKey, (err, res) => {
if (err) { reject(err); }
debug(`[PhoneDB] Found ${res.length} mutual contacts between ${userId} and ${otherUserId}`);
resolve(res);
});
});
const result = await this.sinter(userKey, otherUserKey);
debug(`[PhoneDB] Found ${result.length} mutual contacts between ${userId} and ${otherUserId}`);
return result;
}
return new Promise((resolve, reject) => {
this.client.sinter(userKey, otherUserKey, REGISTERED_KEY, (err, res) => {
if (err) { reject(err); }
debug(`[PhoneDB] Found ${res.length} mutual registered contacts between ${userId} and ${otherUserId}`);
resolve(res);
});
});
const result = this.sinter(userKey, otherUserKey, REGISTERED_KEY);
debug(`[PhoneDB] Found ${result.length} mutual registered contacts between ${userId} and ${otherUserId}`);
return result;
}

@@ -104,22 +82,14 @@

*/
getContacts(userId = null, registered = false) {
async getContacts(userId = null, registered = false) {
const userContactsKey = `user:${userId}:contacts`;
if (registered === false) {
return new Promise((resolve, reject) => {
this.client.smembers(userContactsKey, (err, res) => {
if (err) { reject(err); }
debug(`[PhoneDB] Found ${res.length} contacts for ${userId}`);
resolve(res);
});
});
const result = await this.smembers(userContactsKey);
debug(`[PhoneDB] Found ${result.length} contacts for ${userId}`);
return result;
}
return new Promise((resolve, reject) => {
this.client.sinter(userContactsKey, REGISTERED_KEY, (err, res) => {
if (err) { reject(err); }
debug(`[PhoneDB] Found ${res.length} of ${userId}'s contacts on app`);
resolve(res);
});
});
const result = await this.sinter(userContactsKey, REGISTERED_KEY);
debug(`[PhoneDB] Found ${result.length} of ${userId}'s contacts on app`);
return result;
}

@@ -126,0 +96,0 @@ }

@@ -13,19 +13,15 @@ # PhoneDB

If you are using yarn:
```sh
yarn add phonedb
npm install phonedb
```
or npm:
### Run Redis server
```sh
npm install phonedb --save
```
Check out [Redis quickstart](https://redis.io/topics/quickstart) to install for your platform, or use one of the many cloud providers.
Run Redis server:
A convenience script is provided for macOS default Homebrew Redis installs:
```sh
redis-server
npm run redis-start
```
Check out [Redis quickstart](https://redis.io/topics/quickstart) to install.

@@ -86,8 +82,5 @@ ## Usage

```sh
yarn install # or npm install
npm install
npm test
npm run coverage
```
## Author
Shane Vitarana :: [http://shanev.me](http://shanev.me) :: [@shanev](https://twitter.com/shanev)
const assert = require('assert');
const redis = require('redis');
const { promisify } = require('util');
const client = redis.createClient();
const scard = promisify(client.scard).bind(client);

@@ -24,27 +26,22 @@ client.on('error', (err) => {

describe('.register()', () => {
it('should register a valid phone number', (done) => {
phoneDB.register('+18475557777').then(() => {
client.scard('phonedb:registered', (err, res) => {
assert.equal(1, res);
done();
});
});
it('should register a valid phone number', async () => {
await phoneDB.register('+18475557777');
const result = await scard('phonedb:registered');
assert.equal(1, result);
});
it('should not register an invalid phone number', (done) => {
phoneDB.register('+1847555777').then(() => {
assert(false);
done();
}).catch(() => {
done();
});
it('should not register an invalid phone number', async () => {
try {
await phoneDB.register('+1847555777');
} catch (err) {
assert(err);
}
});
it('should not register another invalid phone number', (done) => {
phoneDB.register('FAKE NEWS! SAD!').then(() => {
assert(false);
}).catch((err) => {
it('should not register another invalid phone number', async () => {
try {
await phoneDB.register('FAKE NEWS! SAD!');
} catch (err) {
assert(err);
done();
});
}
});

@@ -54,30 +51,23 @@ });

describe('.addContacts()', () => {
it('should add a list of valid contacts', (done) => {
it('should add a list of valid contacts', async () => {
const contacts = ['+18475557777', '+14157775555'];
phoneDB.addContacts('user1', contacts).then((res) => {
assert.equal(2, res);
done();
});
const result = await phoneDB.addContacts('user1', contacts);
assert.equal(2, result);
});
it('should throw error if contact is not a number', (done) => {
it('should throw error if contact is not a number', async () => {
const contacts = ['FAKE NEWS! SAD!', '+14157775555'];
phoneDB.addContacts('user1', contacts).then(() => {
client.scard('user:user1:contacts', () => {
assert(false);
});
}).catch((err) => {
assert(err);
done();
});
try {
await phoneDB.addContacts('user1', contacts);
} catch (err) {
const result = scard('user:user1:contacts');
assert(result);
}
});
it('should add one valid contact out of a total of 2', (done) => {
it('should add one valid contact out of a total of 2', async () => {
const contacts = ['+1847555777', '+14157775555'];
phoneDB.addContacts('user1', contacts).then(() => {
client.scard('user:user1:contacts', (_, res) => {
assert.equal(1, res);
done();
});
});
await phoneDB.addContacts('user1', contacts);
const result = client.scard('user:user1:contacts');
assert.equal(1, result);
});

@@ -108,8 +98,6 @@ });

describe('.getContacts()', () => {
it('should find 3 contacts for a user', (done) => {
phoneDB.addContacts('user1', ['+18475557777', '+14157775555', '+14157775556']);
phoneDB.getContacts('user1', false).then((users) => {
assert.equal(3, users.length);
done();
});
it('should find 3 contacts for a user', async () => {
await phoneDB.addContacts('user1', ['+18475557777', '+14157775555', '+14157775556']);
const result = await phoneDB.getContacts('user1');
assert.equal(3, result.length);
});

@@ -116,0 +104,0 @@

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