-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Marcin Rataj <[email protected]> BREAKING CHANGE: minified version is ESM now
- Loading branch information
Showing
13 changed files
with
166 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export default { | ||
tsRepo: false, | ||
build: { | ||
config: { | ||
format: 'esm', | ||
banner: { | ||
js: '' | ||
}, | ||
footer: { | ||
js: '' | ||
} | ||
} | ||
}, | ||
test: { | ||
before: (...args) => { | ||
if (args[0].runner === 'node') { | ||
return { | ||
env: { | ||
NODE_OPTIONS: '--loader=esmock' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
root=true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,39 +42,54 @@ npm install --save ipfs-geoip | |
|
||
### CDN | ||
|
||
Instead of a local installation (and browserification) you may request a [remote copy from jsDelivr](https://www.jsdelivr.com/package/npm/ipfs-geoip): | ||
Instead of a local installation (and browserification) you may request a specific | ||
version `N.N.N` as a [remote copy from jsDelivr](https://www.jsdelivr.com/package/npm/ipfs-geoip): | ||
|
||
```html | ||
<!-- loading the minified version using jsDelivr --> | ||
<script src="https://cdn.jsdelivr.net/npm/ipfs-geoip/dist/index.min.js"></script> | ||
<script type="module"> | ||
import { lookup } from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js'; | ||
const gateway = 'https://ipfs.io' | ||
console.log(await lookup(gateway, '66.6.44.4')) | ||
</script> | ||
``` | ||
|
||
When using prebuilt bundle from CDN, `ipfs-geoip` will be exposed under `window.IpfsGeoip` | ||
|
||
The response in the console should look similar to: | ||
```js | ||
{ | ||
"country_name": "USA", | ||
"country_code": "US", | ||
"region_code": "VA", | ||
"city": "Ashburn", | ||
"postal_code": "20149", | ||
"latitude": 39.0469, | ||
"longitude": -77.4903, | ||
"planet": "Earth" | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
### With public gateways (default) | ||
|
||
If `ipfs` is a string or array of strings with public gateway URLs, it will be used for | ||
If `gateways` is a string or array of strings with public gateway URLs, it will be used for | ||
fetching IPFS blocks as [`application/vnd.ipld.raw`](https://www.iana.org/assignments/media-types/application/vnd.ipld.raw) | ||
and parsing them as DAG-CBOR locally: | ||
and parsing them as DAG-CBOR locally via [@ipld/dag-cbor](https://www.npmjs.com/package/@ipld/dag-cbor): | ||
|
||
```js | ||
const geoip = require('ipfs-geoip') | ||
const exampleIp = '66.6.44.4' | ||
|
||
const ipfsGw = ['https://ipfs.io', 'https://dweb.link'] | ||
const gateways = ['https://ipfs.io', 'https://dweb.link'] | ||
|
||
try { | ||
const result = await geoip.lookup(ipfsGw, exampleIp) | ||
const result = await geoip.lookup(gateways, exampleIp) | ||
console.log('Result: ', result) | ||
} catch (err) { | ||
console.log('Error: ' + err) | ||
} | ||
|
||
try { | ||
const result = await geoip.lookupPretty(ipfsGw, '/ip4/' + exampleIp) | ||
const result = await geoip.lookupPretty(gateways, '/ip4/' + exampleIp) | ||
console.log('Pretty result: %s', result.formatted) | ||
} catch (err) { | ||
console.log('Error: ' + err) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const MAX_LOOKUP_RETRIES = 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { decode as dagCborDecode } from '@ipld/dag-cbor' | ||
import esmock from 'esmock' | ||
import { expect } from 'chai' | ||
|
||
describe('[Runner Node]: lookup via HTTP Gateway supporting application/vnd.ipld.raw responses', function () { | ||
const ipfsGW = process?.env?.IPFS_GATEWAY || 'https://ipfs.io' | ||
|
||
it('looks up multiple times before failing', async () => { | ||
let decodeCallCount = 0 | ||
const rewiredGeoIp = await esmock('../src/index.js', {}, { | ||
'@ipld/dag-cbor': { | ||
decode: (...args) => { | ||
decodeCallCount += 1 | ||
if (decodeCallCount === 1) { | ||
throw new Error('Decode Failed') | ||
} | ||
return dagCborDecode(...args) | ||
} | ||
} | ||
}) | ||
|
||
const result = await rewiredGeoIp.lookup(ipfsGW, '66.6.44.4') | ||
expect(decodeCallCount).to.be.greaterThan(1) | ||
expect( | ||
result | ||
).to.be.eql({ | ||
country_name: 'USA', | ||
country_code: 'US', | ||
region_code: 'VA', | ||
city: 'Ashburn', | ||
postal_code: '20149', | ||
latitude: 39.0469, | ||
longitude: -77.4903, | ||
planet: 'Earth' | ||
}) | ||
}) | ||
}) |