From 7d93596980c99a442f0f2c7e9fbf2c4a903ef39b Mon Sep 17 00:00:00 2001 From: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> Date: Tue, 18 Oct 2022 07:39:37 -0600 Subject: [PATCH] refactor: IPFS_GATEWAY (#94) * Using async-await * feat: support IPFS_GATEWAY env https://github.com/ipfs/specs/pull/280 Signed-off-by: Marcin Rataj Signed-off-by: Marcin Rataj Co-authored-by: Marcin Rataj --- README.md | 7 +++++++ bin/generate.js | 21 +++++++++------------ example/lookup.js | 7 +++---- test/lookup.spec.js | 10 +++++----- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index a2b77d4..0879e38 100644 --- a/README.md +++ b/README.md @@ -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", diff --git a/bin/generate.js b/bin/generate.js index 35848e8..e31e3b3 100755 --- a/bin/generate.js +++ b/bin/generate.js @@ -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 @@ -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() diff --git a/example/lookup.js b/example/lookup.js index dfd3fd4..b288ee8 100644 --- a/example/lookup.js +++ b/example/lookup.js @@ -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 ') @@ -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) diff --git a/test/lookup.spec.js b/test/lookup.spec.js index 02c8fe5..334f3fb 100644 --- a/test/lookup.spec.js +++ b/test/lookup.spec.js @@ -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({ @@ -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')