Skip to content
This repository was archived by the owner on Jan 7, 2022. It is now read-only.

Commit 83cf42b

Browse files
committed
use parse dat url, closes #11
1 parent e1582ae commit 83cf42b

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
language: node_js
22
node_js:
3-
- '6'
43
- '8'
54
- '10'
5+
- 'latest'
6+
- 'lts/*'
67
cache:
78
directories:
89
- node_modules

index.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const assert = require('assert')
22
const stringKey = require('dat-encoding').toStr
33
const get = require('simple-get')
4+
const parseUrl = require('parse-dat-url')
45
const datDns = require('dat-dns')()
56
const debug = require('debug')('dat-link-resolve')
67

@@ -14,6 +15,7 @@ function resolve (link) {
1415
try {
1516
// validates + removes dat://
1617
// also works for http urls with keys in them
18+
1719
key = stringKey(link)
1820
} catch (e) { } // needs lookup
1921

@@ -28,29 +30,17 @@ function resolve (link) {
2830

2931
function lookup () {
3032
return new Promise((resolve, reject) => {
31-
// if it starts with http or dat: use as is, otherwise prepend http://
32-
const urlLink = (link.indexOf('http') && link.indexOf('dat:')) ? ('http://' + link) : link
33-
34-
function resolveName () {
35-
debug('resolveName', urlLink)
36-
datDns.resolveName(urlLink).then((key) => {
37-
debug('resolved', key)
38-
if (key) return resolve(key)
39-
}).catch((err) => {
40-
if (err) debug('datDns.resolveName() error')
41-
reject(err)
42-
})
43-
}
33+
const urlp = parseUrl(link)
4434

45-
debug('resolveKey', link, urlLink)
35+
debug('resolveKey', link, urlp)
4636
get({
47-
url: urlLink.replace('dat://', 'http://'),
37+
url: `https://${urlp.host}${urlp.path}`,
4838
json: true,
4939
timeout: 1500
5040
}, function (err, resp, body) {
51-
// no ressource at given URL
41+
// no resource at given URL
5242
if (err || resp.statusCode !== 200) {
53-
return resolveName()
43+
return resolveDns()
5444
}
5545

5646
// first check if key is in header response
@@ -65,11 +55,21 @@ function resolve (link) {
6555
key = stringKey(body.url)
6656
debug('Received key via json:', key, typeof body, body && typeof body.url)
6757
if (key) resolve(key)
68-
} catch (e) {
69-
// fall back to datDns
70-
resolveName()
71-
}
58+
} catch (e) { }
59+
// fall back to datDns
60+
resolveDns()
7261
})
62+
63+
async function resolveDns () {
64+
debug('resolveDns', urlp.host)
65+
try {
66+
key = await datDns.resolveName(urlp.host)
67+
resolve(key)
68+
} catch (err) {
69+
if (err) debug('datDns.resolveName() error')
70+
reject(err)
71+
}
72+
}
7373
})
7474
}
7575
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"dat-dns": "^3.2.1",
2828
"dat-encoding": "^5.0.1",
2929
"debug": "^4.1.1",
30+
"parse-dat-url": "^3.0.1",
3031
"simple-get": "^3.0.3"
3132
}
3233
}

test/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ test('resolve dat hostname with path', async function (t) {
4848
t.end()
4949
})
5050

51+
test('resolve dat hostname with path and version', async function (t) {
52+
try {
53+
const newKey = await datResolve('dat://beakerbrowser.com/path+5')
54+
t.ok(newKey, 'is a key')
55+
} catch (err) {
56+
t.err(err, 'not expected error')
57+
}
58+
t.end()
59+
})
60+
61+
test('resolve dat hostname with version', async function (t) {
62+
try {
63+
const newKey = await datResolve('dat://beakerbrowser.com+5')
64+
t.ok(newKey, 'is a key')
65+
} catch (err) {
66+
t.err(err, 'not expected error')
67+
}
68+
t.end()
69+
})
70+
5171
test('resolve hostname with path', async function (t) {
5272
try {
5373
const newKey = await datResolve('beakerbrowser.com/path')

0 commit comments

Comments
 (0)