Skip to content
This repository was archived by the owner on Jan 7, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package-lock.json
# Logs
logs
*.log
Expand Down
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
language: node_js
node_js:
- '6'
- '8'
- '10'
- 'node'
- 'lts/*'
cache:
directories:
- node_modules
21 changes: 5 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ resolve urls, links to a dat key using common methods
[![travis][travis-image]][travis-url]
[![standard][standard-image]][standard-url]

This module combines [dat-dns](https://github.com/datprotocol/dat-dns) and [parse-dat-url](https://github.com/pfrazee/parse-dat-url) for basic uses. If you need more control over dns resolution, we recommend using those directly.

### Supports

* Common dat key representations (`dat://`, etc.)
Expand All @@ -25,14 +27,13 @@ npm install dat-link-resolve
```js
var datResolve = require('dat-link-resolve')

datResolve(link, function (err, key) {
console.log('found key', key)
})
var key = await datResolve(link)
console.log('dat key:', key)
```

## API

### `datResolve(link, callback(err, key))`
### `await datResolve(link)`

Link can be string or buffer.

Expand All @@ -43,18 +44,6 @@ Resolution order:
3. Check JSON request response for `key`
4. Dat-DNS resolution via [dat-dns](https://github.com/datprotocol/dat-dns)

## Refering to dats
Trying to tighten up a bit dat-link-resolve (and its dependencies dat-dns and dat-decode). I am noticing a few inconsistencies as I'm writing dat-shell.

Ideally, I'd like to launch dat-shell like this:
```sh
$ dat-shell dat://40a7f6b6147ae695bcbcff432f684c7bb5291ea339c28c1755896cdeb80bd2f9+5/path4
```

and have it open the dat at version 5 and change directory to /path4.

Currently ```dat-shell google-fonts-kewitz.hashbase.io/fonts/``` [fails somewhere in dat-link-resolve](https://github.com/millette/dat-shell/issues/5).

### Examples
Note that dat-link-resolve also supports other methods, such as detection of dat keys in paths and http headers.

Expand Down
102 changes: 55 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,74 @@
var assert = require('assert')
var stringKey = require('dat-encoding').toStr
var get = require('simple-get')
var datDns = require('dat-dns')()
var debug = require('debug')('dat-link-resolve')
const assert = require('assert')
const stringKey = require('dat-encoding').toStr
const get = require('simple-get')
const parseUrl = require('parse-dat-url')
const datDns = require('dat-dns')()
const debug = require('debug')('dat-link-resolve')

module.exports = resolve

function resolve (link, cb) {
function resolve (link) {
assert.ok(link, 'dat-link-resolve: link required')
assert.strictEqual(typeof cb, 'function', 'dat-link-resolve: callback required')

var key = null
let key = null

try {
// validates + removes dat://
// also works for http urls with keys in them
key = stringKey(link)
} catch (e) {
lookup()
return
}
cb(null, key)

function lookup () {
// if it starts with http or dat: use as is, otherwise prepend http://
var urlLink = (link.indexOf('http') && link.indexOf('dat:')) ? ('http://' + link) : link
key = stringKey(link)
} catch (e) { } // needs lookup

function resolveName () {
datDns.resolveName(urlLink, function (err, key) {
debug('resolveName', urlLink, err, key)
if (key) return cb(null, key)
if (err) debug('datDns.resolveName() error')
cb(err)
})
return new Promise(async (resolve, reject) => {
if (key) return resolve(key)
try {
resolve(await lookup())
} catch (e) {
reject(e)
}
})

debug('resolveKey', link, urlLink)
get({
url: urlLink.replace('dat://', 'http://'),
json: true,
timeout: 1500
}, function (err, resp, body) {
// no ressource at given URL
if (err || resp.statusCode !== 200) {
return resolveName()
}
function lookup () {
return new Promise((resolve, reject) => {
const urlp = parseUrl(link)

// first check if key is in header response
key = resp.headers['hyperdrive-key'] || resp.headers['dat-key']
if (key) {
debug('Received key from http header:', key)
return cb(null, key)
}
debug('resolveKey', link, urlp)
get({
url: `https://${urlp.host}${urlp.path}`,
json: true,
timeout: 1500
}, function (err, resp, body) {
// no resource at given URL
if (err || resp.statusCode !== 200) {
return resolveDns()
}

// first check if key is in header response
key = resp.headers['hyperdrive-key'] || resp.headers['dat-key']
if (key) {
debug('Received key from http header:', key)
return resolve(key)
}

// else fall back to parsing the body
try {
key = stringKey(body.url)
debug('Received key via json:', key, typeof body, body && typeof body.url)
if (key) return cb(null, key)
} catch (e) {
// else fall back to parsing the body
try {
key = stringKey(body.url)
debug('Received key via json:', key, typeof body, body && typeof body.url)
if (key) resolve(key)
} catch (e) { }
// fall back to datDns
resolveName()
resolveDns()
})

async function resolveDns () {
debug('resolveDns', urlp.host)
try {
key = await datDns.resolveName(urlp.host)
resolve(key)
} catch (err) {
if (err) debug('datDns.resolveName() error')
reject(err)
}
}
})
}
Expand Down
Loading