Skip to content

Commit

Permalink
Adding a new "hyper" protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
martinheidegger committed Apr 15, 2021
1 parent 2ab60f4 commit 34e5bbf
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# hyper-dns

Advanced resolving of decentralized web links using different name systems such as DNS Txt entries and `.well-known` https lookups locations.
It implements various naming systems such as [dat][] or [cabal][] but is extensible to support other systems as well.
It implements various naming systems such as [dat][], [hyper][] and [cabal][] but is extensible to support other systems as well.

[hyper]: https://hypercore-protocol.org/
[dat]: https://www.datprotocol.com/deps/0005-dns/
[cabal]: https://cabal.chat/
[ara]: https://ara.one/
Expand Down
8 changes: 8 additions & 0 deletions docs/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ const { cabal } = require('hyper-dns').protocols

The [cabal chat](https://cabal.chat/) protocol works the same way as `dat` does but has a different lookups for DNS entries (cabalkey=) and uses the `/.well-known/cabal` lookup.

### hyper

```javascript
const { hyper } = require('hyper-dns').protocols
```

The second version of the `dat` protocol that should be outphasing the first version over long time.

### See a missing protocol?

If you have an implementation that should really find its way into `hyper-dns`: feel free to open a Pull Request and suggest it as default for a protocol!
26 changes: 26 additions & 0 deletions protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@ module.exports = Object.freeze({
}
return await context.fetchWellKnown(name, 'dat', /^\s*(?:(?:dat):)?(?:\/\/)?(?<key>[0-9a-f]{64})\s*$/i, 6)
},
async hyper (context, name) {
let record = context.matchRegex(name, /^(?<key>[0-9a-f]{64})$/i)
if (record !== undefined) {
return record
}
if (!context.isLocal(name)) {
const domain = `hyper-dns.${name}`
record = await context.getDNSTxtRecord(domain, /^\s*"?(?:hyperkey)=(?<key>(?:[0-9a-f]{64}|well-known))"?\s*$/i)
if (record === undefined) {
return
}
if (record.key !== 'well-known') {
return record
}
}
const wk = await context.fetchWellKnown(name, 'hyper', /^\s*(?:(?:hyper):)?(?:\/\/)?(?<key>[0-9a-f]{64})\s*$/i, 6)
if (wk === undefined) {
return
}
if (typeof wk.ttl !== 'number') {
wk.ttl = record.ttl
} else if (typeof record.ttl === 'number') {
wk.ttl = Math.min(wk.ttl, record.ttl)
}
return wk
},
async cabal (context, name) {
let record = context.matchRegex(name, /^(?<key>[0-9a-f]{64})$/i)
if (record !== undefined) {
Expand Down
5 changes: 3 additions & 2 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ test(`Successful test against ${ecosystem}`, async t => {
t.deepEquals(
results,
{
dat: key,
ara: null,
cabal: null,
ara: null
dat: key,
hyper: null
}
)
})
Expand Down
94 changes: 94 additions & 0 deletions test/protocols.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,100 @@ const { matchRegex } = require('../resolve-context.js')
})
})()

;(() => {
const { hyper } = protocols
const key = '100c77d788fdaf07b89b28e9d276e47f2e44011f4adb981921056e1b3b40e99e'
test('hyper: local urls', async t => {
t.deepEquals(
await hyper({ matchRegex }, key),
{ key, ttl: null }
)
})
test('hyper: looking for dns records', async t => {
const name = 'dat-ecosystem.org'
t.plan(4)
t.deepEquals(
await hyper({
matchRegex,
isLocal: (name) => {
t.equals(name, 'dat-ecosystem.org')
return false
},
async getDNSTxtRecord (domain, regex) {
t.equals(domain, `hyper-dns.${name}`)
t.match(`hyperkey=${key}`, regex)
return { key, ttl: 10 }
}
}, name),
{ key, ttl: 10 }
)
})
test('hyper: allowing for miss of dns records', async t => {
const name = 'dat-ecosystem.org'
t.deepEquals(
await hyper({
matchRegex,
isLocal: () => false,
async getDNSTxtRecord () {}
}, name),
undefined
)
})
test('hyper: looking for well-known record', async t => {
const name = 'dat-ecosystem.org'
t.deepEquals(
await hyper({
matchRegex,
isLocal: () => false,
async getDNSTxtRecord (_domain, regex) {
t.match('hyperkey=well-known', regex)
return { key: 'well-known', ttl: 2 }
},
async fetchWellKnown (domain, schema, regex, redirects) {
t.equals(redirects, 6)
t.equals(domain, name)
t.equals(schema, 'hyper')
t.match(key, regex)
t.match(`hyper:${key}`, regex)
t.match(`hyper://${key}`, regex)
return { key, ttl: 10 }
}
}, name),
{ key, ttl: 2 }
)
})
test('hyper: looking for well-known may miss', async t => {
const name = 'dat-ecosystem.org'
t.deepEquals(
await hyper({
matchRegex,
isLocal: () => false,
async getDNSTxtRecord () {
return { key: 'well-known', ttl: 2 }
},
async fetchWellKnown () {}
}, name),
undefined
)
})
test('hyper: looking for well-known uses ttl of dns record if missing', async t => {
const name = 'dat-ecosystem.org'
t.deepEquals(
await hyper({
matchRegex,
isLocal: () => false,
async getDNSTxtRecord () {
return { key: 'well-known', ttl: 2 }
},
async fetchWellKnown () {
return { key, ttl: null }
}
}, name),
{ key, ttl: 2 }
)
})
})()

;(() => {
const { cabal } = protocols
const key = '100c77d788fdaf07b89b28e9d276e47f2e44011f4adb981921056e1b3b40e99e'
Expand Down

0 comments on commit 34e5bbf

Please sign in to comment.