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

locate-user

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

locate-user - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

1

index.js
'use strict';
const getLocation = require('./lib/get-location');
module.exports = getLocation;

53

lib/get-location.js
'use strict';
const https = require('https');
const http = require('http');
const getUserIP = require('user-ip');

@@ -15,3 +15,3 @@

if (!req) {
const error = new Error('Request Object Should Not Be Null Or Undefined');
const error = new TypeError('Request Object Should Not Be Null Or Undefined');
reject(error);

@@ -21,25 +21,38 @@ }

const userIP = getUserIP(req);
const url = `https://freegeoip.net/json/${userIP}`;
const url = `http://ip-api.com/json/${userIP}`;
if (userIP) {
https.get(url, resStream => {
let data = '';
resStream.setEncoding('utf8');
resStream.on('data', chunk => data += chunk);
resStream.on('error', reject);
resStream.on('end', () => {
try {
const body = JSON.parse(data);
resolve(body);
} catch (error) {
reject(error);
if (!userIP) {
const error = new TypeError('IP Address Should Not Be Empty');
reject(error);
}
http.get(url, resStream => {
let data = '';
resStream.setEncoding('utf8');
resStream.on('error', reject);
resStream.on('data', chunk => data += chunk);
resStream.on('end', () => {
try {
const body = JSON.parse(data);
/*
* If status is fail should respond Bad Request (400)
*/
if (body && (body.status === 'fail')) {
const error = new Error(body.status);
error.status = 400;
return reject(error);
}
});
})
}
resolve(body);
} catch (error) {
reject(error);
}
});
})
});
});
}
module.exports = getLocation;
module.exports = getLocation;
{
"name": "locate-user",
"version": "1.0.1",
"version": "1.0.2",
"description": "Know Your Client/User's Location",

@@ -36,4 +36,4 @@ "main": "index.js",

"chai": "^4.1.0",
"mocha": "^3.4.2"
"mocha": "^5.2.0"
}
}

@@ -56,3 +56,4 @@ # locate-user

.catch(next);
})
});
app.listen(3000)

@@ -59,0 +60,0 @@

@@ -12,18 +12,17 @@ 'use strict';

const keys = ['ip', 'country_code', 'country_name', 'region_code', 'region_name', 'city', 'zip_code', 'time_zone', 'latitude', 'longitude', 'metro_code'];
const expected = {
ip: '1.1.1.1',
country_code: 'AU',
country_name: 'Australia',
region_code: 'VIC',
region_name: 'Victoria',
query: '1.1.1.1',
countryCode: 'AU',
country: 'Australia',
region: 'VIC',
regionName: 'Victoria',
city: 'Research',
zip_code: '3095',
time_zone: 'Australia/Melbourne',
latitude: -37.7,
longitude: 145.1833,
metro_code: 0
zip: '3095',
timezone: 'Australia/Melbourne',
lat: -37.7,
lon: 145.1833,
status: 'success'
};
const keys = Object.keys(expected);

@@ -37,3 +36,2 @@

.then(data => {
data.should.have.all.keys(keys);
keys.forEach(key => data.should.have.property(key, expected[key]));

@@ -43,4 +41,28 @@ done();

.catch(done);
})
});
it('should throw error with status 400 when status from IP API is fail', done => {
/*
* 127.0.0.1 .. querying this ip from IP API service will
* result in fail status...
*/
const mockReq = {
headers: {},
client: { remoteAddress: '127.0.0.1' }
};
locateUser(mockReq)
.catch(error => {
error.should.be.an.instanceof(Error);
if (error.status === 400) {
done()
} else {
done(error);
}
});
});
})

Sorry, the diff of this file is not supported yet

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