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

Commit 9f8e35b

Browse files
author
Joe Hand
authored
Merge pull request #2 from joehand/well-known
add .well-known resolution
2 parents 2b3fea5 + 66b187c commit 9f8e35b

File tree

3 files changed

+48
-26
lines changed

3 files changed

+48
-26
lines changed

index.js

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var assert = require('assert')
22
var stringKey = require('dat-encoding').toStr
33
var nets = require('nets')
4+
var parseUrl = require('node-parse-url')
5+
var datDns = require('dat-dns')()
46
var debug = require('debug')('dat-link-resolve')
57

68
module.exports = resolve
@@ -22,30 +24,40 @@ function resolve (link, cb) {
2224

2325
function lookup () {
2426
var url = link.indexOf('http') > -1 ? link : 'http://' + link
25-
26-
// TODO: if TLD, check .well-known first
27-
28-
debug('lookup()', url)
29-
nets({ url: url, json: true }, function (err, resp, body) {
30-
if (err) return cb(err)
31-
if (resp.statusCode !== 200) return cb(body.message)
32-
33-
// first check if key is in header response
34-
key = resp.headers['hyperdrive-key'] || resp.headers['dat-key']
35-
if (key) {
36-
debug('Received key from http header:', key)
37-
return cb(null, key)
38-
}
39-
40-
// else fall back to parsing the body
41-
try {
42-
key = stringKey(body.url)
43-
debug('Received key via json:', key)
44-
if (key) return cb(null, key)
45-
} catch (e) {
46-
cb(new Error(e))
47-
}
48-
cb(new Error('Unable to lookup key from http link.'))
27+
var parsed = parseUrl(url)
28+
debug('parsed url', parsed)
29+
if (parsed.path && parsed.path !== '/') return resolveKey()
30+
31+
// If no path, check .well-known first
32+
url = parsed.subdomain ? `${parsed.subdomain}.${parsed.domain}` : parsed.domain
33+
datDns.resolveName(parsed.domain, function (err, key) {
34+
if (key) return cb(null, key)
35+
if (err) debug('datDns.resolveName() error', err)
36+
resolveKey()
4937
})
38+
39+
function resolveKey () {
40+
nets({ url: url, json: true }, function (err, resp, body) {
41+
if (err) return cb(err)
42+
if (resp.statusCode !== 200) return cb(body.message)
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)
55+
if (key) return cb(null, key)
56+
} catch (e) {
57+
cb(new Error(e))
58+
}
59+
cb(new Error('Unable to lookup key from http link.'))
60+
})
61+
}
5062
}
5163
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
"test": "standard && tape test/*.js | tap-spec"
2626
},
2727
"dependencies": {
28+
"dat-dns": "^1.1.0",
2829
"dat-encoding": "^4.0.2",
2930
"debug": "^2.6.6",
30-
"nets": "^3.2.0"
31+
"nets": "^3.2.0",
32+
"node-parse-url": "^3.0.0"
3133
}
3234
}

test/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var stringKeys = [
1414
]
1515

1616
test('resolve key without http', function (t) {
17-
t.plan(3 * 7) // 5 tests for 7 keys
17+
t.plan(3 * 7) // 3 tests for 7 keys
1818
stringKeys.forEach(function (key) {
1919
datResolve(key.key, function (err, newKey) {
2020
t.error(err, 'no error')
@@ -23,3 +23,11 @@ test('resolve key without http', function (t) {
2323
})
2424
})
2525
})
26+
27+
test('resolve beaker browser', function (t) {
28+
datResolve('beakerbrowser.com', function (err, key) {
29+
t.error(err, 'no error')
30+
t.ok(key, 'got key')
31+
t.end()
32+
})
33+
})

0 commit comments

Comments
 (0)