This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
dht.js
155 lines (131 loc) · 3.26 KB
/
dht.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'use strict'
const Joi = require('@hapi/joi')
const Boom = require('@hapi/boom')
const CID = require('cids')
const debug = require('debug')
const log = debug('ipfs:http-api:dht')
log.error = debug('ipfs:http-api:dht:error')
exports = module.exports
exports.findPeer = {
validate: {
query: Joi.object().keys({
arg: Joi.string().required()
}).unknown()
},
async handler (request, h) {
const ipfs = request.server.app.ipfs
const { arg } = request.query
let res
try {
res = await ipfs.dht.findPeer(arg)
} catch (err) {
if (err.code === 'ERR_LOOKUP_FAILED') {
throw Boom.notFound(err.toString())
} else {
throw Boom.boomify(err, { message: err.toString() })
}
}
return h.response({
Responses: [{
ID: res.id.toB58String(),
Addrs: res.multiaddrs.toArray().map((a) => a.toString())
}],
Type: 2
})
}
}
exports.findProvs = {
validate: {
query: Joi.object().keys({
arg: Joi.string().required(),
'num-providers': Joi.number().integer().default(20),
timeout: Joi.number()
}).unknown()
},
async handler (request, h) {
const ipfs = request.server.app.ipfs
const { arg } = request.query
request.query.maxNumProviders = request.query['num-providers']
const res = await ipfs.dht.findProvs(arg, request.query)
return h.response({
Responses: res.map((peerInfo) => ({
ID: peerInfo.id.toB58String(),
Addrs: peerInfo.multiaddrs.toArray().map((a) => a.toString())
})),
Type: 4
})
}
}
exports.get = {
validate: {
query: Joi.object().keys({
arg: Joi.string().required(),
timeout: Joi.number()
}).unknown()
},
async handler (request, h) {
const ipfs = request.server.app.ipfs
const { arg } = request.query
const res = await ipfs.dht.get(Buffer.from(arg))
return h.response({
Extra: res.toString(),
Type: 5
})
}
}
exports.provide = {
validate: {
query: Joi.object().keys({
arg: Joi.string().required()
}).unknown()
},
async handler (request, h) {
const ipfs = request.server.app.ipfs
const { arg } = request.query
let cid
try {
cid = new CID(arg)
} catch (err) {
log.error(err)
throw Boom.boomify(err, { message: err.toString() })
}
await ipfs.dht.provide(cid)
return h.response()
}
}
exports.put = {
validate: {
query: Joi.object().keys({
arg: Joi.array().items(Joi.string()).length(2).required()
}).unknown()
},
parseArgs: (request, h) => {
return {
key: request.query.arg[0],
value: request.query.arg[1]
}
},
async handler (request, h) {
const key = request.pre.args.key
const value = request.pre.args.value
const ipfs = request.server.app.ipfs
await ipfs.dht.put(Buffer.from(key), Buffer.from(value))
return h.response()
}
}
exports.query = {
validate: {
query: Joi.object().keys({
arg: Joi.string().required()
}).unknown()
},
async handler (request, h) {
const ipfs = request.server.app.ipfs
const { arg } = request.query
const res = await ipfs.dht.query(arg)
const response = res.map((peerInfo) => ({
ID: peerInfo.id.toB58String()
}))
return h.response(response)
}
}