js-libp2p/src/dht.js

44 lines
1.1 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 promisify = require('promisify-es6')
const { messages, codes } = require('./errors')
2018-02-07 07:48:37 +00:00
module.exports = (node) => {
return {
put: promisify((key, value, callback) => {
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
}
node._dht.put(key, value, callback)
}),
get: promisify((key, options, callback) => {
2018-09-20 09:47:55 +01:00
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)
}),
getMany: promisify((key, nVals, options, callback) => {
2018-09-20 09:47:55 +01:00
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
}
}