This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathput.js
45 lines (39 loc) · 1.49 KB
/
put.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict'
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const ndjson = require('iterable-ndjson')
const configure = require('../lib/configure')
const toAsyncIterable = require('../lib/stream-to-async-iterable')
const encodeBufferURIComponent = require('../lib/encode-buffer-uri-component')
const toCamel = require('../lib/object-to-camel')
module.exports = configure(({ ky }) => {
return async function * put (key, value, options) {
options = options || {}
const searchParams = new URLSearchParams(options.searchParams)
if (options.verbose != null) searchParams.set('verbose', options.verbose)
key = Buffer.isBuffer(key)
? encodeBufferURIComponent(key)
: encodeURIComponent(key)
value = Buffer.isBuffer(value)
? encodeBufferURIComponent(value)
: encodeURIComponent(value)
const url = `dht/put?arg=${key}&arg=${value}&${searchParams}`
const res = await ky.post(url, {
timeout: options.timeout,
signal: options.signal,
headers: options.headers
})
for await (let message of ndjson(toAsyncIterable(res))) {
message = toCamel(message)
if (message.responses) {
message.responses = message.responses.map(({ ID, Addrs }) => {
const peerInfo = new PeerInfo(PeerId.createFromB58String(ID))
if (Addrs) Addrs.forEach(a => peerInfo.multiaddrs.add(multiaddr(a)))
return peerInfo
})
}
yield message
}
}
})