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

Commit 5725706

Browse files
committed
Large change to revolve URLs with paths or versions.
1 parent 30d07ca commit 5725706

File tree

1 file changed

+35
-37
lines changed

1 file changed

+35
-37
lines changed

index.js

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var assert = require('assert')
2-
var url = require('url')
32
var stringKey = require('dat-encoding').toStr
43
var nets = require('nets')
54
var datDns = require('dat-dns')()
@@ -19,46 +18,45 @@ function resolve (link, cb) {
1918
key = stringKey(link)
2019
cb(null, key)
2120
} catch (e) {
22-
return lookup()
21+
lookup()
2322
}
2423

2524
function lookup () {
26-
debug('lookup', link)
27-
var parsed = url.parse(link)
28-
29-
// parsed.host check b/c url.parse('beakerbrowser.com') returns parsed.pathname = 'beakerbrowser.com'
30-
if (parsed.host && parsed.path && parsed.path !== '/') return resolveKey()
31-
32-
// If no path, check .well-known first
33-
datDns.resolveName(link, function (err, key) {
34-
if (key) return cb(null, key)
35-
if (err) debug('datDns.resolveName() error', err)
36-
resolveKey()
37-
})
38-
39-
function resolveKey () {
40-
var urlLink = link.indexOf('http') > -1 ? link : 'http://' + link
41-
nets({ url: urlLink, json: true }, function (err, resp, body) {
42-
if (err) return cb(err)
43-
if (resp.statusCode !== 200) return cb(body.message)
44-
45-
// first check if key is in header response
46-
key = resp.headers['hyperdrive-key'] || resp.headers['dat-key']
47-
if (key) {
48-
debug('Received key from http header:', key)
49-
return cb(null, key)
50-
}
51-
52-
// else fall back to parsing the body
53-
try {
54-
key = stringKey(body.url)
55-
debug('Received key via json:', key)
56-
if (key) return cb(null, key)
57-
} catch (e) {
58-
cb(new Error(e))
59-
}
60-
cb(new Error('Unable to lookup key from http link.'))
25+
// if it starts with http or dat: use as is, otherwise prepend http://
26+
var urlLink = (link.indexOf('http') && link.indexOf('dat:')) ? ('http://' + link) : link
27+
28+
function resolveName () {
29+
datDns.resolveName(urlLink, function (err, key) {
30+
debug('resolveName', urlLink, err, key)
31+
if (key) return cb(null, key)
32+
if (err) debug('datDns.resolveName() error')
33+
cb(err)
6134
})
6235
}
36+
37+
debug('resolveKey', link, urlLink)
38+
nets({ url: urlLink, json: true }, function (err, resp, body) {
39+
// no ressource at given URL
40+
if (err || resp.statusCode !== 200) {
41+
return resolveName()
42+
}
43+
44+
// first check if key is in header response
45+
key = resp.headers['hyperdrive-key'] || resp.headers['dat-key']
46+
if (key) {
47+
debug('Received key from http header:', key)
48+
return cb(null, key)
49+
}
50+
51+
// else fall back to parsing the body
52+
try {
53+
key = stringKey(body.url)
54+
debug('Received key via json:', key, typeof body, body && typeof body.url)
55+
if (key) return cb(null, key)
56+
} catch (e) {
57+
// fall back to datDns
58+
resolveName()
59+
}
60+
})
6361
}
6462
}

0 commit comments

Comments
 (0)