2
2
3
3
import { expect } from 'aegir/chai'
4
4
import { Controller , createFactory } from 'ipfsd-ctl'
5
- import { create , CID } from 'ipfs-http-client'
5
+ import { create , CID as IPFSCCID } from 'ipfs-http-client'
6
6
import all from 'it-all'
7
7
import drain from 'it-drain'
8
8
import { isElectronMain , isNode } from 'wherearewe'
9
9
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
10
- import { DelegatedContentRouting } from '../src/index.js'
10
+ import { delegatedContentRouting } from '../src/index.js'
11
11
// @ts -expect-error no types
12
12
import goIpfs from 'go-ipfs'
13
13
import pDefer from 'p-defer'
14
+ import { CID } from 'multiformats/cid'
14
15
import type { PeerId } from '@libp2p/interface-peer-id'
15
16
import type { IDResult } from 'ipfs-core-types/src/root'
16
17
import type { PeerInfo } from '@libp2p/interface-peer-info'
18
+ import { stop } from '@libp2p/interfaces/startable'
19
+ import { TimeoutController } from 'timeout-abort-controller'
17
20
18
21
const factory = createFactory ( {
19
22
type : 'go' ,
@@ -78,7 +81,7 @@ describe('DelegatedContentRouting', function () {
78
81
describe ( 'create' , ( ) => {
79
82
it ( 'should require ipfs http client' , ( ) => {
80
83
// @ts -expect-error missing parameters
81
- expect ( ( ) => new DelegatedContentRouting ( ) ) . to . throw ( )
84
+ expect ( ( ) => delegatedContentRouting ( ) ( ) ) . to . throw ( )
82
85
} )
83
86
84
87
it ( 'should accept an http api client instance at construction time' , ( ) => {
@@ -87,7 +90,7 @@ describe('DelegatedContentRouting', function () {
87
90
port : 8000 ,
88
91
host : 'localhost'
89
92
} )
90
- const router = new DelegatedContentRouting ( client )
93
+ const router = delegatedContentRouting ( client ) ( )
91
94
92
95
expect ( router ) . to . have . property ( 'client' )
93
96
. that . has . property ( 'getEndpointConfig' )
@@ -103,7 +106,7 @@ describe('DelegatedContentRouting', function () {
103
106
104
107
describe ( 'findProviders' , ( ) => {
105
108
const data = uint8ArrayFromString ( 'some data' )
106
- const cid = CID . parse ( 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS' ) // 'some data'
109
+ const cid = IPFSCCID . parse ( 'QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS' ) // 'some data'
107
110
108
111
before ( 'register providers' , async ( ) => {
109
112
await Promise . all ( [
@@ -118,13 +121,13 @@ describe('DelegatedContentRouting', function () {
118
121
119
122
it ( 'should be able to find providers through the delegate node' , async function ( ) {
120
123
const opts = delegateNode . apiAddr . toOptions ( )
121
- const routing = new DelegatedContentRouting ( create ( {
124
+ const routing = delegatedContentRouting ( create ( {
122
125
protocol : 'http' ,
123
126
port : opts . port ,
124
127
host : opts . host
125
- } ) )
128
+ } ) ) ( )
126
129
127
- const providers = await all ( routing . findProviders ( cid ) )
130
+ const providers = await all ( routing . findProviders ( CID . parse ( cid . toString ( ) ) ) )
128
131
129
132
// We should get the bootstrap node as provider
130
133
// The delegate node is not included, because it is handling the requests
@@ -134,30 +137,33 @@ describe('DelegatedContentRouting', function () {
134
137
135
138
it ( 'should be able to specify a timeout' , async ( ) => {
136
139
const opts = delegateNode . apiAddr . toOptions ( )
137
- const routing = new DelegatedContentRouting ( create ( {
140
+ const routing = delegatedContentRouting ( create ( {
138
141
protocol : 'http' ,
139
142
port : opts . port ,
140
143
host : opts . host
141
- } ) )
144
+ } ) ) ( )
145
+ const controller = new TimeoutController ( 5e3 )
142
146
143
- const providers = await all ( routing . findProviders ( cid , { timeout : 5e3 } ) )
147
+ const providers = await all ( routing . findProviders ( CID . parse ( cid . toString ( ) ) , { signal : controller . signal } ) )
144
148
145
149
expect ( providers . map ( ( p ) => p . id . toString ( ) ) ) . to . include ( bootstrapId . id . toString ( ) , 'Did not include bootstrap node' )
150
+
151
+ controller . clear ( )
146
152
} )
147
153
} )
148
154
149
155
describe ( 'provide' , ( ) => {
150
156
it ( 'should be able to register as a content provider to the delegate node' , async ( ) => {
151
157
const opts = delegateNode . apiAddr . toOptions ( )
152
- const contentRouter = new DelegatedContentRouting ( create ( {
158
+ const contentRouter = delegatedContentRouting ( create ( {
153
159
protocol : 'http' ,
154
160
port : opts . port ,
155
161
host : opts . host
156
- } ) )
162
+ } ) ) ( )
157
163
158
164
const { cid } = await selfNode . api . add ( uint8ArrayFromString ( `hello-${ Math . random ( ) } ` ) )
159
165
160
- await contentRouter . provide ( cid )
166
+ await contentRouter . provide ( CID . parse ( cid . toString ( ) ) )
161
167
162
168
const providers : PeerInfo [ ] = [ ]
163
169
@@ -173,18 +179,18 @@ describe('DelegatedContentRouting', function () {
173
179
174
180
it ( 'should provide non-dag-pb nodes via the delegate node' , async ( ) => {
175
181
const opts = delegateNode . apiAddr . toOptions ( )
176
- const contentRouter = new DelegatedContentRouting ( create ( {
182
+ const contentRouter = delegatedContentRouting ( create ( {
177
183
protocol : 'http' ,
178
184
port : opts . port ,
179
185
host : opts . host
180
- } ) )
186
+ } ) ) ( )
181
187
182
188
const cid = await selfNode . api . dag . put ( `hello-${ Math . random ( ) } ` , {
183
189
storeCodec : 'dag-cbor' ,
184
190
hashAlg : 'sha2-256'
185
191
} )
186
192
187
- await contentRouter . provide ( cid )
193
+ await contentRouter . provide ( CID . parse ( cid . toString ( ) ) )
188
194
189
195
const providers : PeerInfo [ ] = [ ]
190
196
@@ -202,11 +208,11 @@ describe('DelegatedContentRouting', function () {
202
208
describe ( 'get' , ( ) => {
203
209
it ( 'should get a value' , async ( ) => {
204
210
const opts = delegateNode . apiAddr . toOptions ( )
205
- const contentRouter = new DelegatedContentRouting ( create ( {
211
+ const contentRouter = delegatedContentRouting ( create ( {
206
212
protocol : 'http' ,
207
213
port : opts . port ,
208
214
host : opts . host
209
- } ) )
215
+ } ) ) ( )
210
216
211
217
const cid = await selfNode . api . dag . put ( `hello-${ Math . random ( ) } ` , {
212
218
storeCodec : 'dag-cbor' ,
@@ -224,11 +230,11 @@ describe('DelegatedContentRouting', function () {
224
230
describe ( 'put' , ( ) => {
225
231
it ( 'should put a value' , async ( ) => {
226
232
const opts = delegateNode . apiAddr . toOptions ( )
227
- const contentRouter = new DelegatedContentRouting ( create ( {
233
+ const contentRouter = delegatedContentRouting ( create ( {
228
234
protocol : 'http' ,
229
235
port : opts . port ,
230
236
host : opts . host
231
- } ) )
237
+ } ) ) ( )
232
238
233
239
const cid = await selfNode . api . dag . put ( `hello-${ Math . random ( ) } ` , {
234
240
storeCodec : 'dag-cbor' ,
@@ -259,11 +265,11 @@ describe('DelegatedContentRouting', function () {
259
265
describe ( 'stop' , ( ) => {
260
266
it ( 'should cancel in-flight requests when stopping' , async ( ) => {
261
267
const opts = delegateNode . apiAddr . toOptions ( )
262
- const contentRouter = new DelegatedContentRouting ( create ( {
268
+ const contentRouter = delegatedContentRouting ( create ( {
263
269
protocol : 'http' ,
264
270
port : opts . port ,
265
271
host : opts . host
266
- } ) )
272
+ } ) ) ( )
267
273
268
274
const deferred = pDefer < Error > ( )
269
275
// non-existent CID
@@ -277,7 +283,7 @@ describe('DelegatedContentRouting', function () {
277
283
deferred . resolve ( err )
278
284
} )
279
285
280
- await contentRouter . stop ( )
286
+ await stop ( contentRouter )
281
287
await expect ( deferred . promise ) . to . eventually . have . property ( 'message' ) . that . matches ( / a b o r t e d / )
282
288
} )
283
289
} )
0 commit comments