Skip to content

Commit 1a08fb9

Browse files
nijynothacdias
authored andcommitted
refactor: to async await (#67)
1 parent 088098a commit 1a08fb9

File tree

6 files changed

+131
-122
lines changed

6 files changed

+131
-122
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ npm install --save ipfs-geoip
2121

2222
```js
2323
const geoip = require('ipfs-geoip')
24-
const ipfs = require('ipfs-api')()
24+
const ipfs = require('ipfs-http-client')()
2525

2626
var exampleIp = '89.114.95.36'
2727

@@ -113,7 +113,7 @@ This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/c
113113

114114
### Want to hack on IPFS?
115115

116-
[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/contributing.md)
116+
[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md)
117117

118118
## License
119119

bin/generate

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33

44
const Gauge = require('gauge')
55
const gen = require('../src/generate')
6-
const API = require('ipfs-api')
6+
const ipfs = require('ipfs-http-client')()
77

88
function handleNoApi () {
99
console.error('No ipfs daemon running. Please start one')
1010
process.exit(1)
1111
}
1212

1313
// -- CLI interaction
14-
15-
const ipfs = new API()
16-
1714
ipfs.id()
1815
.then((id) => {
1916
if (!id) handleNoApi()

example/lookup.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
'use strict'
22

33
const geoip = require('../')
4-
const ipfs = require('ipfs-api')()
4+
const ipfs = require('ipfs-http-client')()
55

66
if (process.argv.length !== 3) {
77
console.log('usage: node lookup.js <ip4-adr>')
88
process.exit(1)
99
}
1010

11-
geoip.lookup(ipfs, process.argv[2], (err, result) => {
12-
if (err) {
13-
console.log('Error: ' + err)
14-
} else {
11+
(async function() {
12+
try {
13+
const result = await geoip.lookup(ipfs, process.argv[2])
1514
console.log('Result: ' + JSON.stringify(result, null, 2))
15+
} catch (err) {
16+
console.log('Error: ' + err)
1617
}
17-
})
1818

19-
geoip.lookupPretty(ipfs, '/ip4/' + process.argv[2], (err, result) => {
20-
if (err) {
21-
console.log('Error: ' + err)
22-
} else {
19+
try {
20+
const result = await geoip.lookupPretty(ipfs, '/ip4/' + process.argv[2])
2321
console.log('Pretty result: %s', result.formatted)
22+
} catch (err) {
23+
console.log('Error: ' + err)
2424
}
25-
})
25+
})()

src/lookup.js

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,74 @@ const GEOIP_ROOT = mh.fromB58String('QmRn43NNNBEibc6m7zVNcS6UusB1u3qTTfyoLmkugbe
1010

1111
let memoizedLookup
1212

13-
function _lookup (ipfs, hash, lookfor, cb) {
14-
ipfs.object.get(hash, (err, res) => {
15-
if (err) return cb(err)
16-
17-
let obj
18-
try {
19-
obj = JSON.parse(res.data)
20-
} catch (err) {
21-
return cb(err)
22-
}
23-
24-
let child = 0
25-
26-
if (obj.type === 'Node') {
27-
while (obj.mins[child] && obj.mins[child] <= lookfor) {
28-
child++
13+
/**
14+
* @param {Object} ipfs
15+
* @param {string} hash
16+
* @param {string} lookfor - ip
17+
* @returns {Promise}
18+
*/
19+
function _lookup (ipfs, hash, lookfor) {
20+
return new Promise((resolve, reject) => {
21+
ipfs.object.get(hash, (err, res) => {
22+
if (err) reject(err)
23+
24+
let obj
25+
try {
26+
obj = JSON.parse(res.data)
27+
} catch (err) {
28+
reject(err)
2929
}
3030

31-
const next = res.links[child - 1]
31+
let child = 0
3232

33-
if (!next) {
34-
return cb(new Error('Failed to lookup node'))
35-
}
33+
if (obj.type === 'Node') {
34+
while (obj.mins[child] && obj.mins[child] <= lookfor) {
35+
child++
36+
}
3637

37-
const nextCid = getCid(next)
38+
const next = res.links[child - 1]
3839

39-
if (!nextCid) {
40-
return cb(new Error('Failed to lookup node'))
41-
}
40+
if (!next) {
41+
reject(new Error('Failed to lookup node'))
42+
}
4243

43-
return memoizedLookup(ipfs, nextCid, lookfor, cb)
44-
} else if (obj.type === 'Leaf') {
45-
while (obj.data[child] && obj.data[child].min <= lookfor) {
46-
child++
47-
}
44+
const nextCid = getCid(next)
4845

49-
const next = obj.data[child - 1]
46+
if (!nextCid) {
47+
reject(new Error('Failed to lookup node'))
48+
}
5049

51-
if (!next) {
52-
return cb(new Error('Failed to lookup leaf node'))
53-
}
50+
resolve(memoizedLookup(ipfs, nextCid, lookfor))
51+
} else if (obj.type === 'Leaf') {
52+
while (obj.data[child] && obj.data[child].min <= lookfor) {
53+
child++
54+
}
5455

55-
if (!next.data) {
56-
return cb(new Error('Unmapped range'), null)
57-
}
56+
const next = obj.data[child - 1]
57+
58+
if (!next) {
59+
reject(new Error('Failed to lookup leaf node'))
60+
}
5861

59-
return cb(null, formatData(next.data))
60-
}
62+
if (!next.data) {
63+
reject(new Error('Unmapped range'), null)
64+
}
65+
66+
resolve(formatData(next.data))
67+
}
68+
})
6169
})
6270
}
6371

6472
memoizedLookup = memoize(_lookup, { async: true })
6573

66-
module.exports = function lookup (ipfs, ip, cb) {
67-
memoizedLookup(ipfs, GEOIP_ROOT, inet.aton(ip), cb)
74+
/**
75+
* @param {Object} ipfs
76+
* @param {string} ip
77+
* @returns {Promise}
78+
*/
79+
module.exports = function lookup (ipfs, ip) {
80+
return memoizedLookup(ipfs, GEOIP_ROOT, inet.aton(ip))
6881
}
6982

7083
function getCid (node) {

src/pretty.js

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,47 @@ function isLocal (address) {
1111
return false
1212
}
1313

14-
module.exports = function lookupPretty (ipfs, multiaddrs, cb) {
15-
if (multiaddrs.length === 0) {
16-
return cb(new Error('lookup requires a multiaddr array with length > 0'), null)
17-
}
18-
19-
if (typeof multiaddrs === 'string') {
20-
multiaddrs = [multiaddrs]
21-
}
22-
23-
const current = multiaddrs[0].split('/')
24-
const address = current[2]
25-
26-
// No ip6 support at the moment
27-
if (isLocal(address) || current[1] === 'ip6') {
28-
const next = multiaddrs.slice(1)
29-
if (next.length > 0) {
30-
return lookupPretty(ipfs, multiaddrs.slice(1), cb)
14+
module.exports = function lookupPretty (ipfs, multiaddrs) {
15+
return new Promise(async (resolve, reject) => {
16+
if (multiaddrs.length === 0) {
17+
reject(new Error('lookup requires a multiaddr array with length > 0'), null)
3118
}
32-
return cb(new Error('Unmapped range'), null)
33-
}
3419

35-
lookup(ipfs, address, (err, res) => {
36-
if (err) { return cb(err) }
20+
if (typeof multiaddrs === 'string') {
21+
multiaddrs = [multiaddrs]
22+
}
23+
24+
const current = multiaddrs[0].split('/')
25+
const address = current[2]
3726

38-
if (!res.country_name && multiaddrs.length > 1) {
39-
return lookupPretty(ipfs, multiaddrs.slice(1), cb)
27+
// No ip6 support at the moment
28+
if (isLocal(address) || current[1] === 'ip6') {
29+
const next = multiaddrs.slice(1)
30+
if (next.length > 0) {
31+
resolve(lookupPretty(ipfs, multiaddrs.slice(1)))
32+
}
33+
reject(new Error('Unmapped range'))
4034
}
4135

42-
const location = []
36+
try {
37+
const res = await lookup(ipfs, address)
38+
39+
if (!res.country_name && multiaddrs.length > 1) {
40+
resolve(lookupPretty(ipfs, multiaddrs.slice(1)))
41+
}
4342

44-
if (res.planet) location.push(res.planet)
45-
if (res.country_name) location.unshift(res.country_name)
46-
if (res.region_code) location.unshift(res.region_code)
47-
if (res.city) location.unshift(res.city)
43+
const location = []
4844

49-
res.formatted = location.join(', ')
45+
if (res.planet) location.push(res.planet)
46+
if (res.country_name) location.unshift(res.country_name)
47+
if (res.region_code) location.unshift(res.region_code)
48+
if (res.city) location.unshift(res.city)
5049

51-
cb(null, res)
50+
res.formatted = location.join(', ')
51+
52+
resolve(res)
53+
} catch (err) {
54+
reject(err)
55+
}
5256
})
5357
}

test/lookup.spec.js

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,46 @@ describe('lookup', function () {
1818
})
1919
})
2020

21-
it('fails on 127.0.0.1', (done) => {
22-
geoip.lookup(ipfs, '127.0.0.1', function (err, result) {
21+
it('fails on 127.0.0.1', async () => {
22+
try {
23+
await geoip.lookup(ipfs, '127.0.0.1')
24+
} catch (err) {
2325
expect(err).to.have.property('message', 'Unmapped range')
24-
done()
25-
})
26+
}
2627
})
2728

28-
it('looks up 8.8.8.8', (done) => {
29-
geoip.lookup(ipfs, '8.8.8.8', function (err, result) {
30-
if (err) throw err
31-
expect(
32-
result
33-
).to.be.eql({
34-
country_name: 'United States',
35-
country_code: 'US',
36-
region_code: 'CA',
37-
city: 'Mountain View',
38-
postal_code: 94040,
39-
latitude: 37.386,
40-
longitude: -122.0838,
41-
metro_code: 807,
42-
area_code: 650,
43-
planet: 'Earth'
44-
})
45-
46-
done()
29+
it('looks up 8.8.8.8', async () => {
30+
const result = await geoip.lookup(ipfs, '8.8.8.8')
31+
expect(
32+
result
33+
).to.be.eql({
34+
country_name: 'United States',
35+
country_code: 'US',
36+
region_code: 'CA',
37+
city: 'Mountain View',
38+
postal_code: 94040,
39+
latitude: 37.386,
40+
longitude: -122.0838,
41+
metro_code: 807,
42+
area_code: 650,
43+
planet: 'Earth'
4744
})
4845
})
4946

5047
describe('lookupPretty', () => {
51-
it('fails on 127.0.0.1', (done) => {
52-
geoip.lookupPretty(ipfs, '/ip4/127.0.0.1', function (err, result) {
48+
it('fails on 127.0.0.1', async () => {
49+
try {
50+
await geoip.lookupPretty(ipfs, '/ip4/127.0.0.1')
51+
} catch (err) {
5352
expect(err).to.have.property('message', 'Unmapped range')
54-
done()
55-
})
53+
}
5654
})
5755

58-
it('looks up 8.8.8.8', (done) => {
59-
geoip.lookupPretty(ipfs, '/ip4/8.8.8.8', function (err, result) {
60-
if (err) throw err
61-
expect(
62-
result.formatted
63-
).to.be.eql('Mountain View, CA, United States, Earth')
64-
done()
65-
})
56+
it('looks up 8.8.8.8', async () => {
57+
const result = await geoip.lookupPretty(ipfs, '/ip4/8.8.8.8')
58+
expect(
59+
result.formatted
60+
).to.be.eql('Mountain View, CA, United States, Earth')
6661
})
6762
})
6863
})

0 commit comments

Comments
 (0)