2018-02-07 07:48:37 +00:00
|
|
|
'use strict'
|
|
|
|
|
2019-02-26 14:15:30 +00:00
|
|
|
const nextTick = require('async/nextTick')
|
|
|
|
const errCode = require('err-code')
|
2019-07-29 14:40:40 +01:00
|
|
|
const promisify = require('promisify-es6')
|
2019-02-26 14:15:30 +00:00
|
|
|
|
|
|
|
const { messages, codes } = require('./errors')
|
|
|
|
|
2018-02-07 07:48:37 +00:00
|
|
|
module.exports = (node) => {
|
|
|
|
return {
|
2019-07-29 14:40:40 +01:00
|
|
|
put: promisify((key, value, callback) => {
|
2018-02-07 07:48:37 +00:00
|
|
|
if (!node._dht) {
|
2019-02-26 14:15:30 +00:00
|
|
|
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)
|
2019-07-29 14:40:40 +01:00
|
|
|
}),
|
|
|
|
get: promisify((key, options, callback) => {
|
2018-09-20 09:47:55 +01:00
|
|
|
if (typeof options === 'function') {
|
|
|
|
callback = options
|
|
|
|
options = {}
|
2018-09-19 18:31:36 +01:00
|
|
|
}
|
|
|
|
|
2018-02-07 07:48:37 +00:00
|
|
|
if (!node._dht) {
|
2019-02-26 14:15:30 +00:00
|
|
|
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)
|
2019-07-29 14:40:40 +01:00
|
|
|
}),
|
|
|
|
getMany: promisify((key, nVals, options, callback) => {
|
2018-09-20 09:47:55 +01:00
|
|
|
if (typeof options === 'function') {
|
|
|
|
callback = options
|
|
|
|
options = {}
|
2018-09-19 18:31:36 +01:00
|
|
|
}
|
|
|
|
|
2018-02-07 07:48:37 +00:00
|
|
|
if (!node._dht) {
|
2019-02-26 14:15:30 +00:00
|
|
|
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)
|
2019-07-29 14:40:40 +01:00
|
|
|
})
|
2018-02-07 07:48:37 +00:00
|
|
|
}
|
|
|
|
}
|