Skip to content

Commit

Permalink
tests (#13)
Browse files Browse the repository at this point in the history
* tests

* newline eof on travis yml
  • Loading branch information
davidmarkclements authored Aug 1, 2019
1 parent 531dd2f commit 9cc2ead
Show file tree
Hide file tree
Showing 10 changed files with 1,449 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
sandbox.js
.nyc_output
coverage
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: node_js
sudo: true
node_js:
- 8
- 10
- 12
os:
- windows
- linux
- osx
script:
- npm run ci

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2018 Mathias Buus
Copyright (c) 2018-2019 Mathias Buus, David Mark Clements & Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ Emitted when the node starts listening

Emitted when the node is fully closed.

#### `node.on('announce', topic, peer)
#### `node.on('announce', topic, peer)`

Emitted when an announce is received.

Expand Down
22 changes: 9 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ class HyperDHT extends DHT {
port: opts.port,
localAddress: encodeAddress(opts.localAddress)
}

return this.query('peers', key, opts, cb).map(mapPeers.bind(null, query.localAddress))
return this.query('peers', key, query, cb).map(mapPeers.bind(null, query.localAddress))
}

announce (key, opts, cb) {
Expand Down Expand Up @@ -87,17 +86,15 @@ class HyperDHT extends DHT {
}

_onpeers (query, cb) {
const value = query.value || {}
const value = query.value
const from = {
port: value.port || query.node.port,
host: query.node.host
}

if (!(from.port > 0 && from.port < 65536)) return cb(new Error('Invalid port'))

const localRecord = value.localAddress
const remoteRecord = peers.encode([ from ])

const remoteCache = query.target.toString('hex')
const localCache = localRecord &&
remoteCache + '@local.' + localRecord.slice(0, 2).toString('hex')
Expand All @@ -114,13 +111,12 @@ class HyperDHT extends DHT {
localPeers: local.length ? Buffer.concat(local) : null
})
}

if (value.unannounce) {
if (remoteRecord) this._peers.remove(remoteCache, remoteRecord)
this._peers.remove(remoteCache, remoteRecord)
if (localRecord) this._peers.remove(localCache, localSuffix)
this.emit('unannounce', query.target, from)
} else {
if (remoteRecord) this._peers.add(remoteCache, remoteRecord)
this._peers.add(remoteCache, remoteRecord)
if (localRecord) this._peers.add(localCache, localSuffix)
this.emit('announce', query.target, from)
}
Expand All @@ -134,31 +130,31 @@ function encodeAddress (addr) {
}

function filter (list, item) {
if (!item) return list

for (var i = 0; i < list.length; i++) {
if (list[i].equals(item)) {
list[i] = list[list.length - 1]
list.pop()
break
}
}

return list
}

function mapPeers (prefix, data) {
const v = data.value
if (!v || (!v.peers && !v.localPeers)) return null

try {
return {
node: data.node,
peers: v.peers && peers.decode(v.peers),
localPeers: prefix && v.localPeers && decodeLocalPeers(prefix, v.localPeers)
}
} catch (err) {
return null
return {
node: data.node,
peers: [],
localPeers: prefix && v.localPeers && decodeLocalPeers(prefix, v.localPeers)
}
}
}

Expand Down
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,27 @@
"sodium-universal": "^2.0.0"
},
"devDependencies": {
"get-port": "^5.0.0",
"nonsynchronous": "^1.0.1",
"protocol-buffers": "^4.1.0",
"standard": "^12.0.1"
"standard": "^12.0.1",
"tap": "^14.5.0"
},
"bin": {
"hyperswarm-dht": "./bin.js"
},
"scripts": {
"test": "standard",
"test": "tap -R classic test/*.test.js && standard --fix",
"cov": "tap -R classic --100 --coverage-report=html test/*.test.js",
"ci": "standard && npm test",
"protobuf": "protocol-buffers schema.proto -o messages.js"
},
"nyc": {
"exclude": [
"messages.js",
"**/test/**"
]
},
"repository": {
"type": "git",
"url": "https://github.com/mafintosh/dht.git"
Expand Down
23 changes: 8 additions & 15 deletions stores.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ const {
const { finished } = require('readable-stream')
const { Mutable } = require('./messages')

const mutableEncoding = {
encode (o) {
if (o == null) return o
return Mutable.encode(o)
},
decode (o) {
if (o == null) return o
return Mutable.decode(o)
}
}

// PUT_VALUE_MAX_SIZE + packet overhead (i.e. the key etc.)
// should be less than the network MTU, normally 1400 bytes
const PUT_VALUE_MAX_SIZE = 1000
Expand Down Expand Up @@ -55,7 +44,7 @@ class ImmutableStore {
})
if (value && hasCb === false) {
// push local cached value out of stream first
queryStream.push(value)
process.nextTick(() => queryStream.emit('data', value))
}
if (hasCb) {
queryStream.once('data', (value) => {
Expand All @@ -79,6 +68,7 @@ class ImmutableStore {
value.length <= PUT_VALUE_MAX_SIZE,
`Value size must be <= ${PUT_VALUE_MAX_SIZE}`
)
assert(typeof cb === 'function', 'Callback is required')
const { store, dht } = this
const key = Buffer.alloc(32)
hash(key, value)
Expand Down Expand Up @@ -140,6 +130,7 @@ class MutableStore {
get (key, opts = {}, cb = opts) {
const { dht } = this
const { salt, seq = 0 } = opts
assert(Buffer.isBuffer(key), 'Key must be a buffer')
assert(typeof seq === 'number', 'seq should be a number')
if (salt) {
assert(Buffer.isBuffer(salt), 'salt must be a buffer')
Expand Down Expand Up @@ -173,14 +164,15 @@ class MutableStore {
cb(err)
return
}
if (found === false) cb(null, null)
if (found === false) cb(null, { value: null })
})
}
return queryStream
}
put (value, opts, cb) {
assert(Buffer.isBuffer(value), 'Value must be a buffer')
assert(typeof opts === 'object', 'Options are required')
assert(typeof cb === 'function', 'Callback is required')
assert(value.length <= PUT_VALUE_MAX_SIZE, `Value size must be <= ${PUT_VALUE_MAX_SIZE}`)
const { dht } = this
const { seq = 0, salt, keypair } = opts
Expand Down Expand Up @@ -220,9 +212,9 @@ class MutableStore {
_command () {
const { store } = this
return {
valueEncoding: mutableEncoding,
valueEncoding: Mutable,
update (input, cb) {
if (input.value == null) {
if (input.value.value == null || input.value.sig == null) {
cb(null)
return
}
Expand All @@ -232,6 +224,7 @@ class MutableStore {
? publicKey.toString('hex') + salt.toString('hex')
: publicKey.toString('hex')
const local = store.get(key)

const msg = salt
? Buffer.concat([Buffer.from([salt.length]), salt, value])
: value
Expand Down
Loading

0 comments on commit 9cc2ead

Please sign in to comment.