Skip to content

Commit

Permalink
refactor: IPFS_GATEWAY (#94)
Browse files Browse the repository at this point in the history
* Using async-await
* feat: support IPFS_GATEWAY env

ipfs/specs#280

Signed-off-by: Marcin Rataj <lidel@lidel.org>

Signed-off-by: Marcin Rataj <lidel@lidel.org>
Co-authored-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
whizzzkid and lidel committed Oct 18, 2022
1 parent 4fabe56 commit 7d93596
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,16 @@ in multiple locations, and stored as the new `GEOIP_ROOT` in `src/lookup.js`

## Testing in CLI

It is possible to run tests against a local gateway by passing `IPFS_GATEWAY`:

```console
$ IPFS_GATEWAY="http://127.0.0.1:8080" npm test
```

You can find an example of how to use this in [`example/lookup.js`](example/lookup.js), which you can use like this:

```bash
$ export IPFS_GATEWAY="http://127.0.0.1:8080"
$ node example/lookup.js 66.6.44.4
Result: {
"country_name": "USA",
Expand Down
21 changes: 9 additions & 12 deletions bin/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ const carFilename = 'ipfs-geoip.car'
const ipfs = create()

// -- CLI interaction
ipfs.id()
.then((id) => {
async function generate () {
try {
const id = await ipfs.id()
if (!id) handleNoApi()
}, handleNoApi)
.then(async () => {
const gauge = new Gauge()
let length = 0
let counter = 0
const fakeRoot = CID.parse('bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354') // will be replaced with the real root before writer.close()
const { writer, out } = await CarWriter.create([fakeRoot])
Readable.from(out).pipe(fs.createWriteStream(carFilename))

gen.progress.on('progress', (event) => {
if (event.type === 'node') {
length = event.length
Expand All @@ -53,18 +51,17 @@ ipfs.id()

gauge.show('Starting', 0.0001)
const rootCid = await gen.main(ipfs, writer)
return { rootCid, writer }
})
.then(async ({ rootCid, writer }) => {
const newRoots = [CID.asCID(rootCid)]
await writer.close()
const fd = await fsopen(carFilename, 'r+')
await CarWriter.updateRootsInFile(fd, newRoots)
await fsclose(fd)
console.log('Finished with root CID %s, all blocks exported to ' + carFilename, rootCid)
console.log(`Finished with root CID ${rootCid}, all blocks exported to ${carFilename}`)
process.exit(0)
})
.catch((err) => {
} catch (err) {
console.error(err.stack)
process.exit(1)
})
}
}

generate()
7 changes: 3 additions & 4 deletions example/lookup.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as geoip from '../src/index.js'
import { create } from 'ipfs-http-client'

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

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

(async function() {
try {
const result = await geoip.lookup(ipfs, process.argv[2])
const result = await geoip.lookup(ipfsGw, process.argv[2])
console.log('Result: ' + JSON.stringify(result, null, 2))
} catch (err) {
console.log('Error: ' + err)
}

try {
const result = await geoip.lookupPretty(ipfs, '/ip4/' + process.argv[2])
const result = await geoip.lookupPretty(ipfsGw, '/ip4/' + process.argv[2])
console.log('Pretty result: %s', result.formatted)
} catch (err) {
console.log('Error: ' + err)
Expand Down
10 changes: 5 additions & 5 deletions test/lookup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import * as geoip from '../src/index.js'
describe('lookup via HTTP Gateway supporting application/vnd.ipld.raw responses', function () {
this.timeout(100 * 1000)

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

it('fails on 127.0.0.1', async () => {
try {
await geoip.lookup(ipfs, '127.0.0.1')
await geoip.lookup(ipfsGW, '127.0.0.1')
} catch (err) {
expect(err).to.have.property('message', 'Unmapped range')
}
})

it('looks up 66.6.44.4', async () => {
const result = await geoip.lookup(ipfs, '66.6.44.4')
const result = await geoip.lookup(ipfsGW, '66.6.44.4')
expect(
result
).to.be.eql({
Expand All @@ -33,14 +33,14 @@ describe('lookup via HTTP Gateway supporting application/vnd.ipld.raw responses'
describe('lookupPretty', () => {
it('fails on 127.0.0.1', async () => {
try {
await geoip.lookupPretty(ipfs, '/ip4/127.0.0.1')
await geoip.lookupPretty(ipfsGW, '/ip4/127.0.0.1')
} catch (err) {
expect(err).to.have.property('message', 'Unmapped range')
}
})

it('looks up 66.6.44.4', async () => {
const result = await geoip.lookupPretty(ipfs, '/ip4/66.6.44.4')
const result = await geoip.lookupPretty(ipfsGW, '/ip4/66.6.44.4')
expect(
result.formatted
).to.be.eql('Ashburn, VA, USA, Earth')
Expand Down

0 comments on commit 7d93596

Please sign in to comment.