js-libp2p/src/dht.js

43 lines
1.0 KiB
JavaScript
Raw Normal View History

2018-02-07 07:48:37 +00:00
'use strict'
const nextTick = require('async/nextTick')
const errCode = require('err-code')
const { messages, codes } = require('./errors')
2018-02-07 07:48:37 +00:00
module.exports = (node) => {
return {
put: (key, value, callback) => {
if (!node._dht) {
return nextTick(callback, errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED))
2018-02-07 07:48:37 +00:00
}
node._dht.put(key, value, callback)
},
2018-09-20 09:47:55 +01:00
get: (key, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
2018-02-07 07:48:37 +00:00
if (!node._dht) {
return nextTick(callback, errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED))
2018-02-07 07:48:37 +00:00
}
2018-09-20 09:47:55 +01:00
node._dht.get(key, options, callback)
2018-02-07 07:48:37 +00:00
},
2018-09-20 09:47:55 +01:00
getMany: (key, nVals, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
2018-02-07 07:48:37 +00:00
if (!node._dht) {
return nextTick(callback, errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED))
2018-02-07 07:48:37 +00:00
}
2018-09-20 09:47:55 +01:00
node._dht.getMany(key, nVals, options, callback)
2018-02-07 07:48:37 +00:00
}
}
}