@@ -6,6 +6,22 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
6
6
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
7
7
import { streamResponse } from '../../utils/stream-response.js'
8
8
import pushable from 'it-pushable'
9
+ import { base64url } from 'multiformats/bases/base64'
10
+
11
+ const preDecodeTopicFromHttpRpc = {
12
+ assign : 'topic' ,
13
+ /**
14
+ * @param {import('../../types').Request } request
15
+ * @param {import('@hapi/hapi').ResponseToolkit } _h
16
+ */
17
+ method : async ( request , _h ) => {
18
+ try {
19
+ return uint8ArrayToString ( base64url . decode ( request . query . topic ) )
20
+ } catch ( /** @type {any } */ err ) {
21
+ throw Boom . boomify ( err , { message : `Failed to decode topic from HTTP RPC form ${ request . query . topic } ` } )
22
+ }
23
+ }
24
+ }
9
25
10
26
export const subscribeResource = {
11
27
options : {
@@ -24,7 +40,8 @@ export const subscribeResource = {
24
40
override : true ,
25
41
ignoreUndefined : true
26
42
} )
27
- }
43
+ } ,
44
+ pre : [ preDecodeTopicFromHttpRpc ]
28
45
} ,
29
46
/**
30
47
* @param {import('../../types').Request } request
@@ -56,13 +73,11 @@ export const subscribeResource = {
56
73
* @type {import('ipfs-core-types/src/pubsub').MessageHandlerFn }
57
74
*/
58
75
const handler = ( msg ) => {
59
- // TODO: data, seqno and topicIDs in multibase
60
- // TODO: from should use canonical toString from peerid libp2p lib
61
76
output . push ( {
62
- from : uint8ArrayToString ( uint8ArrayFromString ( msg . from , 'base58btc' ) , 'base64pad' ) ,
63
- data : uint8ArrayToString ( msg . data , 'base64pad' ) ,
64
- seqno : uint8ArrayToString ( msg . seqno , 'base64pad' ) ,
65
- topicIDs : msg . topicIDs
77
+ from : msg . from , // TODO: switch to PeerId.parse(msg.from).toString() when go-ipfs defaults to CIDv1
78
+ data : base64url . encode ( msg . data ) ,
79
+ seqno : base64url . encode ( msg . seqno ) ,
80
+ topicIDs : msg . topicIDs . map ( t => base64url . encode ( uint8ArrayFromString ( t ) ) )
66
81
} )
67
82
}
68
83
@@ -92,7 +107,7 @@ export const publishResource = {
92
107
parse : false ,
93
108
output : 'stream'
94
109
} ,
95
- pre : [ {
110
+ pre : [ preDecodeTopicFromHttpRpc , {
96
111
assign : 'data' ,
97
112
/**
98
113
* @param {import('../../types').Request } request
@@ -158,7 +173,7 @@ export const publishResource = {
158
173
} = request
159
174
160
175
try {
161
- // TODO: unwrap topic from multibase ?
176
+ // TODO: move to reusable pre handler ?
162
177
await ipfs . pubsub . publish ( topic , data , {
163
178
signal,
164
179
timeout
@@ -212,8 +227,7 @@ export const lsResource = {
212
227
throw Boom . boomify ( err , { message : 'Failed to list subscriptions' } )
213
228
}
214
229
215
- // TODO: multibase topic names in Strings array
216
- return h . response ( { Strings : subscriptions } )
230
+ return h . response ( { Strings : subscriptions . map ( s => base64url . encode ( uint8ArrayFromString ( s ) ) ) } )
217
231
}
218
232
}
219
233
@@ -232,7 +246,8 @@ export const peersResource = {
232
246
override : true ,
233
247
ignoreUndefined : true
234
248
} )
235
- }
249
+ } ,
250
+ pre : [ preDecodeTopicFromHttpRpc ]
236
251
} ,
237
252
/**
238
253
* @param {import('../../types').Request } request
@@ -256,7 +271,6 @@ export const peersResource = {
256
271
257
272
let peers
258
273
try {
259
- // TODO: unwrap topic from multibase
260
274
peers = await ipfs . pubsub . peers ( topic , {
261
275
signal,
262
276
timeout
0 commit comments