Skip to content

Commit 7d93596

Browse files
whizzzkidlidel
andauthored
refactor: IPFS_GATEWAY (#94)
* Using async-await * feat: support IPFS_GATEWAY env ipfs/specs#280 Signed-off-by: Marcin Rataj <[email protected]> Signed-off-by: Marcin Rataj <[email protected]> Co-authored-by: Marcin Rataj <[email protected]>
1 parent 4fabe56 commit 7d93596

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,16 @@ in multiple locations, and stored as the new `GEOIP_ROOT` in `src/lookup.js`
159159

160160
## Testing in CLI
161161

162+
It is possible to run tests against a local gateway by passing `IPFS_GATEWAY`:
163+
164+
```console
165+
$ IPFS_GATEWAY="http://127.0.0.1:8080" npm test
166+
```
167+
162168
You can find an example of how to use this in [`example/lookup.js`](example/lookup.js), which you can use like this:
163169

164170
```bash
171+
$ export IPFS_GATEWAY="http://127.0.0.1:8080"
165172
$ node example/lookup.js 66.6.44.4
166173
Result: {
167174
"country_name": "USA",

bin/generate.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,16 @@ const carFilename = 'ipfs-geoip.car'
2121
const ipfs = create()
2222

2323
// -- CLI interaction
24-
ipfs.id()
25-
.then((id) => {
24+
async function generate () {
25+
try {
26+
const id = await ipfs.id()
2627
if (!id) handleNoApi()
27-
}, handleNoApi)
28-
.then(async () => {
2928
const gauge = new Gauge()
3029
let length = 0
3130
let counter = 0
3231
const fakeRoot = CID.parse('bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354') // will be replaced with the real root before writer.close()
3332
const { writer, out } = await CarWriter.create([fakeRoot])
3433
Readable.from(out).pipe(fs.createWriteStream(carFilename))
35-
3634
gen.progress.on('progress', (event) => {
3735
if (event.type === 'node') {
3836
length = event.length
@@ -53,18 +51,17 @@ ipfs.id()
5351

5452
gauge.show('Starting', 0.0001)
5553
const rootCid = await gen.main(ipfs, writer)
56-
return { rootCid, writer }
57-
})
58-
.then(async ({ rootCid, writer }) => {
5954
const newRoots = [CID.asCID(rootCid)]
6055
await writer.close()
6156
const fd = await fsopen(carFilename, 'r+')
6257
await CarWriter.updateRootsInFile(fd, newRoots)
6358
await fsclose(fd)
64-
console.log('Finished with root CID %s, all blocks exported to ' + carFilename, rootCid)
59+
console.log(`Finished with root CID ${rootCid}, all blocks exported to ${carFilename}`)
6560
process.exit(0)
66-
})
67-
.catch((err) => {
61+
} catch (err) {
6862
console.error(err.stack)
6963
process.exit(1)
70-
})
64+
}
65+
}
66+
67+
generate()

example/lookup.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as geoip from '../src/index.js'
22
import { create } from 'ipfs-http-client'
33

4-
// This CLI tool requires Kubo RPC on 127.0.0.1:5001 to be running
5-
const ipfs = create(new URL('http://127.0.0.1:5001'))
4+
const ipfsGw = process?.env?.IPFS_GATEWAY || 'https://ipfs.io'
65

76
if (process.argv.length !== 3) {
87
console.log('usage: node lookup.js <ip4-adr>')
@@ -11,14 +10,14 @@ if (process.argv.length !== 3) {
1110

1211
(async function() {
1312
try {
14-
const result = await geoip.lookup(ipfs, process.argv[2])
13+
const result = await geoip.lookup(ipfsGw, process.argv[2])
1514
console.log('Result: ' + JSON.stringify(result, null, 2))
1615
} catch (err) {
1716
console.log('Error: ' + err)
1817
}
1918

2019
try {
21-
const result = await geoip.lookupPretty(ipfs, '/ip4/' + process.argv[2])
20+
const result = await geoip.lookupPretty(ipfsGw, '/ip4/' + process.argv[2])
2221
console.log('Pretty result: %s', result.formatted)
2322
} catch (err) {
2423
console.log('Error: ' + err)

test/lookup.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import * as geoip from '../src/index.js'
44
describe('lookup via HTTP Gateway supporting application/vnd.ipld.raw responses', function () {
55
this.timeout(100 * 1000)
66

7-
const ipfs = process.env.CI ? 'https://ipfs.io' : 'http://127.0.0.1:8080'
7+
const ipfsGW = process?.env?.IPFS_GATEWAY || 'https://ipfs.io'
88

99
it('fails on 127.0.0.1', async () => {
1010
try {
11-
await geoip.lookup(ipfs, '127.0.0.1')
11+
await geoip.lookup(ipfsGW, '127.0.0.1')
1212
} catch (err) {
1313
expect(err).to.have.property('message', 'Unmapped range')
1414
}
1515
})
1616

1717
it('looks up 66.6.44.4', async () => {
18-
const result = await geoip.lookup(ipfs, '66.6.44.4')
18+
const result = await geoip.lookup(ipfsGW, '66.6.44.4')
1919
expect(
2020
result
2121
).to.be.eql({
@@ -33,14 +33,14 @@ describe('lookup via HTTP Gateway supporting application/vnd.ipld.raw responses'
3333
describe('lookupPretty', () => {
3434
it('fails on 127.0.0.1', async () => {
3535
try {
36-
await geoip.lookupPretty(ipfs, '/ip4/127.0.0.1')
36+
await geoip.lookupPretty(ipfsGW, '/ip4/127.0.0.1')
3737
} catch (err) {
3838
expect(err).to.have.property('message', 'Unmapped range')
3939
}
4040
})
4141

4242
it('looks up 66.6.44.4', async () => {
43-
const result = await geoip.lookupPretty(ipfs, '/ip4/66.6.44.4')
43+
const result = await geoip.lookupPretty(ipfsGW, '/ip4/66.6.44.4')
4444
expect(
4545
result.formatted
4646
).to.be.eql('Ashburn, VA, USA, Earth')

0 commit comments

Comments
 (0)