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

Commit 2b3fea5

Browse files
committed
dat-link-resolve
0 parents  commit 2b3fea5

7 files changed

Lines changed: 208 additions & 0 deletions

File tree

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# node-waf configuration
20+
.lock-wscript
21+
22+
# Compiled binary addons (http://nodejs.org/api/addons.html)
23+
build/Release
24+
25+
# Dependency directory
26+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27+
node_modules

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: node_js
2+
node_js:
3+
- '4'
4+
- '6'
5+
cache:
6+
directories:
7+
- node_modules

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# [MIT License](https://spdx.org/licenses/MIT)
2+
3+
Copyright (c) 2017 Joe Hand <joe@hand.email>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# dat-link-resolve
2+
3+
resolve urls, links to a dat key using common methods
4+
5+
[![npm][npm-image]][npm-url]
6+
[![travis][travis-image]][travis-url]
7+
[![standard][standard-image]][standard-url]
8+
9+
### Supports
10+
11+
* Common dat key representations (`dat://`, etc.)
12+
* URLs with keys in them (`datproject.org/6161616161616161616161616161616161616161616161616161616161616161`)
13+
* `hyperdrive-key` or `dat-key` headers
14+
* Url to JSON http request that returns `{key: <dat-key>}`
15+
16+
## Install
17+
18+
```
19+
npm install dat-link-resolve
20+
```
21+
22+
## Usage
23+
24+
```js
25+
var datResolve = require('dat-link-resolve')
26+
27+
datResolve(link, function (err, key) {
28+
console.log('found key', key)
29+
})
30+
```
31+
32+
## API
33+
34+
### `datResolve(link, callback(err, key))`
35+
36+
Link can be string or buffer.
37+
38+
Resolution order:
39+
40+
1. Validate buffers or any strings with 64 character hashes in them via [dat-encoding](https://github.com/juliangruber/dat-encoding)
41+
2. Check headers in http request
42+
3. Check JSON request response for `key`
43+
44+
## Contributing
45+
46+
Contributions welcome! Please read the [contributing guidelines](CONTRIBUTING.md) first.
47+
48+
## License
49+
50+
[MIT](LICENSE.md)
51+
52+
[npm-image]: https://img.shields.io/npm/v/dat-link-resolve.svg?style=flat-square
53+
[npm-url]: https://www.npmjs.com/package/dat-link-resolve
54+
[travis-image]: https://img.shields.io/travis/joehand/dat-link-resolve.svg?style=flat-square
55+
[travis-url]: https://travis-ci.org/joehand/dat-link-resolve
56+
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
57+
[standard-url]: http://npm.im/standard

index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var assert = require('assert')
2+
var stringKey = require('dat-encoding').toStr
3+
var nets = require('nets')
4+
var debug = require('debug')('dat-link-resolve')
5+
6+
module.exports = resolve
7+
8+
function resolve (link, cb) {
9+
assert.ok(link, 'dat-link-resolve: link required')
10+
assert.equal(typeof cb, 'function', 'dat-link-resolve: callback required')
11+
12+
var key = null
13+
14+
try {
15+
// validates + removes dat://
16+
// also works for http urls with keys in them
17+
key = stringKey(link)
18+
cb(null, key)
19+
} catch (e) {
20+
return lookup()
21+
}
22+
23+
function lookup () {
24+
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.'))
49+
})
50+
}
51+
}

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "dat-link-resolve",
3+
"description": "resolve urls, links to a dat key",
4+
"version": "1.0.0",
5+
"author": "Joe Hand <joe@hand.email>",
6+
"bugs": {
7+
"url": "https://github.com/joehand/dat-link-resolve/issues"
8+
},
9+
"devDependencies": {
10+
"standard": "*",
11+
"tap-spec": "^4.0.2",
12+
"tape": "^4.0.0"
13+
},
14+
"homepage": "https://github.com/joehand/dat-link-resolve",
15+
"keywords": [
16+
"dat"
17+
],
18+
"license": "MIT",
19+
"main": "index.js",
20+
"repository": {
21+
"type": "git",
22+
"url": "https://github.com/joehand/dat-link-resolve.git"
23+
},
24+
"scripts": {
25+
"test": "standard && tape test/*.js | tap-spec"
26+
},
27+
"dependencies": {
28+
"dat-encoding": "^4.0.2",
29+
"debug": "^2.6.6",
30+
"nets": "^3.2.0"
31+
}
32+
}

test/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var test = require('tape')
2+
var enc = require('dat-encoding')
3+
var datResolve = require('..')
4+
5+
// Strings that do not require lookup
6+
var stringKeys = [
7+
{type: 'valid', key: '6161616161616161616161616161616161616161616161616161616161616161'},
8+
{type: 'valid', key: Buffer.from('6161616161616161616161616161616161616161616161616161616161616161', 'hex')},
9+
{type: 'valid', key: 'dat://6161616161616161616161616161616161616161616161616161616161616161'},
10+
{type: 'valid', key: 'datproject.org/6161616161616161616161616161616161616161616161616161616161616161'},
11+
{type: 'valid', key: 'dat://6161616161616161616161616161616161616161616161616161616161616161/'},
12+
{type: 'valid', key: 'datproject.org/6161616161616161616161616161616161616161616161616161616161616161/'},
13+
{type: 'valid', key: 'host.com/whatever/6161616161616161616161616161616161616161616161616161616161616161'}
14+
]
15+
16+
test('resolve key without http', function (t) {
17+
t.plan(3 * 7) // 5 tests for 7 keys
18+
stringKeys.forEach(function (key) {
19+
datResolve(key.key, function (err, newKey) {
20+
t.error(err, 'no error')
21+
t.equal(newKey, '6161616161616161616161616161616161616161616161616161616161616161', 'link correct')
22+
t.ok(enc.encode(newKey), 'valid key')
23+
})
24+
})
25+
})

0 commit comments

Comments
 (0)