@helia/utils 1.2.2-325b36f → 1.2.2-3d77369
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +2 -1
- package/dist/index.min.js.map +7 -0
- package/dist/src/abstract-session.d.ts +9 -1
- package/dist/src/abstract-session.d.ts.map +1 -1
- package/dist/src/abstract-session.js +69 -22
- package/dist/src/abstract-session.js.map +1 -1
- package/package.json +3 -2
- package/src/abstract-session.ts +91 -23
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { TypedEventEmitter } from '@libp2p/interface';
|
|
2
2
|
import type { BlockBroker, BlockRetrievalOptions, CreateSessionOptions } from '@helia/interface';
|
|
3
|
-
import type { AbortOptions, ComponentLogger, Logger } from '@libp2p/interface';
|
|
3
|
+
import type { AbortOptions, ComponentLogger, Logger, PeerId } from '@libp2p/interface';
|
|
4
|
+
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
4
5
|
import type { CID } from 'multiformats/cid';
|
|
5
6
|
import type { ProgressEvent } from 'progress-events';
|
|
6
7
|
export interface AbstractSessionComponents {
|
|
@@ -22,12 +23,19 @@ export declare abstract class AbstractSession<Provider, RetrieveBlockProgressEve
|
|
|
22
23
|
private readonly maxProviders;
|
|
23
24
|
readonly providers: Provider[];
|
|
24
25
|
private readonly evictionFilter;
|
|
26
|
+
private readonly initialProviders;
|
|
25
27
|
constructor(components: AbstractSessionComponents, init: AbstractCreateSessionOptions);
|
|
26
28
|
retrieve(cid: CID, options?: BlockRetrievalOptions<RetrieveBlockProgressEvents>): Promise<Uint8Array>;
|
|
27
29
|
evict(provider: Provider): void;
|
|
28
30
|
isEvicted(provider: Provider): boolean;
|
|
29
31
|
hasProvider(provider: Provider): boolean;
|
|
30
32
|
private findProviders;
|
|
33
|
+
/**
|
|
34
|
+
* If this session was created with specific providers, convert one of them
|
|
35
|
+
* into the format required or return `undefined` if the provider is not
|
|
36
|
+
* compatible with this session implementation
|
|
37
|
+
*/
|
|
38
|
+
abstract convertToProvider(provider: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<Provider | undefined>;
|
|
31
39
|
/**
|
|
32
40
|
* This method should search for new providers and yield them.
|
|
33
41
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abstract-session.d.ts","sourceRoot":"","sources":["../../src/abstract-session.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"abstract-session.d.ts","sourceRoot":"","sources":["../../src/abstract-session.ts"],"names":[],"mappings":"AACA,OAAO,EAAc,iBAAiB,EAAmB,MAAM,mBAAmB,CAAA;AAKlF,OAAO,KAAK,EAAE,WAAW,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAChG,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAEtF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAE3C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAEpD,MAAM,WAAW,yBAAyB;IACxC,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,MAAM,WAAW,4BAA6B,SAAQ,oBAAoB;IACxE,IAAI,EAAE,MAAM,CAAA;CACb;AAED,MAAM,WAAW,uBAAuB,CAAC,QAAQ;IAC/C,QAAQ,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAA;CAChC;AAED,8BAAsB,eAAe,CAAC,QAAQ,EAAE,2BAA2B,SAAS,aAAa,CAAE,SAAQ,iBAAiB,CAAC,uBAAuB,CAAC,QAAQ,CAAC,CAAE,YAAW,WAAW,CAAC,2BAA2B,CAAC;IACjN,OAAO,CAAC,yBAAyB,CAAC,CAAe;IACjD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAkC;IAC3D,OAA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H,QAAQ,CAAC,MAAM,CAAE,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,GAAG,OAAO;CACpE"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DEFAULT_SESSION_MIN_PROVIDERS, DEFAULT_SESSION_MAX_PROVIDERS, InsufficientProvidersError } from '@helia/interface';
|
|
2
|
-
import { TypedEventEmitter, setMaxListeners } from '@libp2p/interface';
|
|
2
|
+
import { AbortError, TypedEventEmitter, setMaxListeners } from '@libp2p/interface';
|
|
3
3
|
import { createScalableCuckooFilter } from '@libp2p/utils/filters';
|
|
4
4
|
import { Queue } from '@libp2p/utils/queue';
|
|
5
5
|
import { base64 } from 'multiformats/bases/base64';
|
|
@@ -14,6 +14,7 @@ export class AbstractSession extends TypedEventEmitter {
|
|
|
14
14
|
maxProviders;
|
|
15
15
|
providers;
|
|
16
16
|
evictionFilter;
|
|
17
|
+
initialProviders;
|
|
17
18
|
constructor(components, init) {
|
|
18
19
|
super();
|
|
19
20
|
setMaxListeners(Infinity, this);
|
|
@@ -25,6 +26,7 @@ export class AbstractSession extends TypedEventEmitter {
|
|
|
25
26
|
this.maxProviders = init.maxProviders ?? DEFAULT_SESSION_MAX_PROVIDERS;
|
|
26
27
|
this.providers = [];
|
|
27
28
|
this.evictionFilter = createScalableCuckooFilter(this.maxProviders);
|
|
29
|
+
this.initialProviders = init.providers ?? [];
|
|
28
30
|
}
|
|
29
31
|
async retrieve(cid, options = {}) {
|
|
30
32
|
// see if we are already requesting this CID in this session
|
|
@@ -67,6 +69,7 @@ export class AbstractSession extends TypedEventEmitter {
|
|
|
67
69
|
});
|
|
68
70
|
queue.addEventListener('idle', () => {
|
|
69
71
|
if (foundBlock || options.signal?.aborted === true) {
|
|
72
|
+
this.log.trace('session idle, found block');
|
|
70
73
|
// we either found the block or the user gave up
|
|
71
74
|
return;
|
|
72
75
|
}
|
|
@@ -127,11 +130,17 @@ export class AbstractSession extends TypedEventEmitter {
|
|
|
127
130
|
}
|
|
128
131
|
this.log.error('error retrieving session block for %c', cid, err);
|
|
129
132
|
});
|
|
133
|
+
const signalAbortedListener = () => {
|
|
134
|
+
deferred.reject(new AbortError(options.signal?.reason ?? 'Session aborted'));
|
|
135
|
+
queue.abort();
|
|
136
|
+
};
|
|
137
|
+
options.signal?.addEventListener('abort', signalAbortedListener);
|
|
130
138
|
try {
|
|
131
139
|
return await deferred.promise;
|
|
132
140
|
}
|
|
133
141
|
finally {
|
|
134
142
|
this.removeEventListener('provider', peerAddedToSessionListener);
|
|
143
|
+
options.signal?.removeEventListener('abort', signalAbortedListener);
|
|
135
144
|
queue.clear();
|
|
136
145
|
this.requests.delete(cidStr);
|
|
137
146
|
}
|
|
@@ -166,28 +175,66 @@ export class AbstractSession extends TypedEventEmitter {
|
|
|
166
175
|
void Promise.resolve()
|
|
167
176
|
.then(async () => {
|
|
168
177
|
this.log('finding %d-%d new provider(s) for %c', count, this.maxProviders, cid);
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
178
|
+
// process any specific providers for this session
|
|
179
|
+
if (this.initialProviders.length > 0) {
|
|
180
|
+
while (found < count && this.initialProviders.length > 0) {
|
|
181
|
+
const prov = this.initialProviders.pop();
|
|
182
|
+
if (prov == null) {
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
const provider = await this.convertToProvider(prov, options);
|
|
186
|
+
if (options.signal?.aborted === true) {
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
if (provider == null) {
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (this.hasProvider(provider)) {
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
this.log('found %d/%d new providers', found, this.maxProviders);
|
|
196
|
+
this.providers.push(provider);
|
|
197
|
+
// let the new peer join current queries
|
|
198
|
+
this.safeDispatchEvent('provider', {
|
|
199
|
+
detail: provider
|
|
200
|
+
});
|
|
201
|
+
found++;
|
|
202
|
+
if (found === count) {
|
|
203
|
+
this.log('session is ready');
|
|
204
|
+
deferred.resolve();
|
|
205
|
+
// continue finding peers until we reach this.maxProviders
|
|
206
|
+
}
|
|
207
|
+
if (this.providers.length === this.maxProviders) {
|
|
208
|
+
this.log('found max session peers', found);
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
187
211
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
212
|
+
}
|
|
213
|
+
// still not got enough providers, search routing for more
|
|
214
|
+
if (found < this.maxProviders) {
|
|
215
|
+
for await (const provider of this.findNewProviders(cid, options)) {
|
|
216
|
+
if (found === this.maxProviders || options.signal?.aborted === true) {
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
if (this.hasProvider(provider)) {
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
this.log('found %d/%d new providers', found, this.maxProviders);
|
|
223
|
+
this.providers.push(provider);
|
|
224
|
+
// let the new peer join current queries
|
|
225
|
+
this.safeDispatchEvent('provider', {
|
|
226
|
+
detail: provider
|
|
227
|
+
});
|
|
228
|
+
found++;
|
|
229
|
+
if (found === count) {
|
|
230
|
+
this.log('session is ready');
|
|
231
|
+
deferred.resolve();
|
|
232
|
+
// continue finding peers until we reach this.maxProviders
|
|
233
|
+
}
|
|
234
|
+
if (this.providers.length === this.maxProviders) {
|
|
235
|
+
this.log('found max session peers', found);
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
191
238
|
}
|
|
192
239
|
}
|
|
193
240
|
this.log('found %d/%d new session peers', found, this.maxProviders);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abstract-session.js","sourceRoot":"","sources":["../../src/abstract-session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BA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AAU,EAAE;wBACjC,MAAM,EAAE,QAAQ;qBACjB,CAAC,CAAA;oBAEF,KAAK,EAAE,CAAA;oBAEP,IAAI,KAAK,KAAK,KAAK,EAAE,CAAC;wBACpB,IAAI,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAA;wBAC5B,QAAQ,CAAC,OAAO,EAAE,CAAA;wBAClB,0DAA0D;oBAC5D,CAAC;oBAED,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,IAAI,CAAC,YAAY,EAAE,CAAC;wBAChD,IAAI,CAAC,GAAG,CAAC,yBAAyB,EAAE,KAAK,CAAC,CAAA;wBAC1C,MAAK;oBACP,CAAC;gBACH,CAAC;YACH,CAAC;YAED,IAAI,CAAC,GAAG,CAAC,+BAA+B,EAAE,KAAK,EAAE,IAAI,CAAC,YAAY,CAAC,CAAA;YAEnE,IAAI,KAAK,GAAG,KAAK,EAAE,CAAC;gBAClB,MAAM,IAAI,0BAA0B,CAAC,SAAS,KAAK,OAAO,KAAK,IAAI,IAAI,CAAC,IAAI,kBAAkB,GAAG,EAAE,CAAC,CAAA;YACtG,CAAC;QACH,CAAC,CAAC;aACD,KAAK,CAAC,GAAG,CAAC,EAAE;YACX,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,4DAA4D,EAAE,GAAG,EAAE,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,CAAA;YACpG,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACtB,CAAC,CAAC,CAAA;QAEJ,OAAO,QAAQ,CAAC,OAAO,CAAA;IACzB,CAAC;CAiCF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helia/utils",
|
|
3
|
-
"version": "1.2.2-
|
|
3
|
+
"version": "1.2.2-3d77369",
|
|
4
4
|
"description": "Shared code that implements the Helia API",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/ipfs/helia/tree/main/packages/utils#readme",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"test:electron-main": "aegir test -t electron-main"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@helia/interface": "5.2.1-
|
|
57
|
+
"@helia/interface": "5.2.1-3d77369",
|
|
58
58
|
"@ipld/dag-cbor": "^9.2.2",
|
|
59
59
|
"@ipld/dag-json": "^10.2.3",
|
|
60
60
|
"@ipld/dag-pb": "^4.1.3",
|
|
@@ -62,6 +62,7 @@
|
|
|
62
62
|
"@libp2p/logger": "^5.1.8",
|
|
63
63
|
"@libp2p/utils": "^6.5.1",
|
|
64
64
|
"@multiformats/dns": "^1.0.6",
|
|
65
|
+
"@multiformats/multiaddr": "^12.4.0",
|
|
65
66
|
"any-signal": "^4.1.1",
|
|
66
67
|
"blockstore-core": "^5.0.2",
|
|
67
68
|
"cborg": "^4.2.6",
|
package/src/abstract-session.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { DEFAULT_SESSION_MIN_PROVIDERS, DEFAULT_SESSION_MAX_PROVIDERS, InsufficientProvidersError } from '@helia/interface'
|
|
2
|
-
import { TypedEventEmitter, setMaxListeners } from '@libp2p/interface'
|
|
2
|
+
import { AbortError, TypedEventEmitter, setMaxListeners } from '@libp2p/interface'
|
|
3
3
|
import { createScalableCuckooFilter } from '@libp2p/utils/filters'
|
|
4
4
|
import { Queue } from '@libp2p/utils/queue'
|
|
5
5
|
import { base64 } from 'multiformats/bases/base64'
|
|
6
6
|
import pDefer from 'p-defer'
|
|
7
7
|
import type { BlockBroker, BlockRetrievalOptions, CreateSessionOptions } from '@helia/interface'
|
|
8
|
-
import type { AbortOptions, ComponentLogger, Logger } from '@libp2p/interface'
|
|
8
|
+
import type { AbortOptions, ComponentLogger, Logger, PeerId } from '@libp2p/interface'
|
|
9
9
|
import type { Filter } from '@libp2p/utils/filters'
|
|
10
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
10
11
|
import type { CID } from 'multiformats/cid'
|
|
11
12
|
import type { DeferredPromise } from 'p-defer'
|
|
12
13
|
import type { ProgressEvent } from 'progress-events'
|
|
@@ -33,6 +34,7 @@ export abstract class AbstractSession<Provider, RetrieveBlockProgressEvents exte
|
|
|
33
34
|
private readonly maxProviders: number
|
|
34
35
|
public readonly providers: Provider[]
|
|
35
36
|
private readonly evictionFilter: Filter
|
|
37
|
+
private readonly initialProviders: Array<PeerId | Multiaddr | Multiaddr[]>
|
|
36
38
|
|
|
37
39
|
constructor (components: AbstractSessionComponents, init: AbstractCreateSessionOptions) {
|
|
38
40
|
super()
|
|
@@ -46,6 +48,7 @@ export abstract class AbstractSession<Provider, RetrieveBlockProgressEvents exte
|
|
|
46
48
|
this.maxProviders = init.maxProviders ?? DEFAULT_SESSION_MAX_PROVIDERS
|
|
47
49
|
this.providers = []
|
|
48
50
|
this.evictionFilter = createScalableCuckooFilter(this.maxProviders)
|
|
51
|
+
this.initialProviders = init.providers ?? []
|
|
49
52
|
}
|
|
50
53
|
|
|
51
54
|
async retrieve (cid: CID, options: BlockRetrievalOptions<RetrieveBlockProgressEvents> = {}): Promise<Uint8Array> {
|
|
@@ -97,6 +100,7 @@ export abstract class AbstractSession<Provider, RetrieveBlockProgressEvents exte
|
|
|
97
100
|
})
|
|
98
101
|
queue.addEventListener('idle', () => {
|
|
99
102
|
if (foundBlock || options.signal?.aborted === true) {
|
|
103
|
+
this.log.trace('session idle, found block')
|
|
100
104
|
// we either found the block or the user gave up
|
|
101
105
|
return
|
|
102
106
|
}
|
|
@@ -168,10 +172,18 @@ export abstract class AbstractSession<Provider, RetrieveBlockProgressEvents exte
|
|
|
168
172
|
this.log.error('error retrieving session block for %c', cid, err)
|
|
169
173
|
})
|
|
170
174
|
|
|
175
|
+
const signalAbortedListener = (): void => {
|
|
176
|
+
deferred.reject(new AbortError(options.signal?.reason ?? 'Session aborted'))
|
|
177
|
+
queue.abort()
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
options.signal?.addEventListener('abort', signalAbortedListener)
|
|
181
|
+
|
|
171
182
|
try {
|
|
172
183
|
return await deferred.promise
|
|
173
184
|
} finally {
|
|
174
185
|
this.removeEventListener('provider', peerAddedToSessionListener)
|
|
186
|
+
options.signal?.removeEventListener('abort', signalAbortedListener)
|
|
175
187
|
queue.clear()
|
|
176
188
|
this.requests.delete(cidStr)
|
|
177
189
|
}
|
|
@@ -216,34 +228,83 @@ export abstract class AbstractSession<Provider, RetrieveBlockProgressEvents exte
|
|
|
216
228
|
.then(async () => {
|
|
217
229
|
this.log('finding %d-%d new provider(s) for %c', count, this.maxProviders, cid)
|
|
218
230
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
231
|
+
// process any specific providers for this session
|
|
232
|
+
if (this.initialProviders.length > 0) {
|
|
233
|
+
while (found < count && this.initialProviders.length > 0) {
|
|
234
|
+
const prov = this.initialProviders.pop()
|
|
223
235
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
236
|
+
if (prov == null) {
|
|
237
|
+
break
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const provider = await this.convertToProvider(prov, options)
|
|
241
|
+
|
|
242
|
+
if (options.signal?.aborted === true) {
|
|
243
|
+
break
|
|
244
|
+
}
|
|
227
245
|
|
|
228
|
-
|
|
229
|
-
|
|
246
|
+
if (provider == null) {
|
|
247
|
+
continue
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (this.hasProvider(provider)) {
|
|
251
|
+
continue
|
|
252
|
+
}
|
|
230
253
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
detail: provider
|
|
234
|
-
})
|
|
254
|
+
this.log('found %d/%d new providers', found, this.maxProviders)
|
|
255
|
+
this.providers.push(provider)
|
|
235
256
|
|
|
236
|
-
|
|
257
|
+
// let the new peer join current queries
|
|
258
|
+
this.safeDispatchEvent('provider', {
|
|
259
|
+
detail: provider
|
|
260
|
+
})
|
|
237
261
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
262
|
+
found++
|
|
263
|
+
|
|
264
|
+
if (found === count) {
|
|
265
|
+
this.log('session is ready')
|
|
266
|
+
deferred.resolve()
|
|
267
|
+
// continue finding peers until we reach this.maxProviders
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (this.providers.length === this.maxProviders) {
|
|
271
|
+
this.log('found max session peers', found)
|
|
272
|
+
break
|
|
273
|
+
}
|
|
242
274
|
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// still not got enough providers, search routing for more
|
|
278
|
+
if (found < this.maxProviders) {
|
|
279
|
+
for await (const provider of this.findNewProviders(cid, options)) {
|
|
280
|
+
if (found === this.maxProviders || options.signal?.aborted === true) {
|
|
281
|
+
break
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (this.hasProvider(provider)) {
|
|
285
|
+
continue
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
this.log('found %d/%d new providers', found, this.maxProviders)
|
|
289
|
+
this.providers.push(provider)
|
|
290
|
+
|
|
291
|
+
// let the new peer join current queries
|
|
292
|
+
this.safeDispatchEvent('provider', {
|
|
293
|
+
detail: provider
|
|
294
|
+
})
|
|
243
295
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
296
|
+
found++
|
|
297
|
+
|
|
298
|
+
if (found === count) {
|
|
299
|
+
this.log('session is ready')
|
|
300
|
+
deferred.resolve()
|
|
301
|
+
// continue finding peers until we reach this.maxProviders
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (this.providers.length === this.maxProviders) {
|
|
305
|
+
this.log('found max session peers', found)
|
|
306
|
+
break
|
|
307
|
+
}
|
|
247
308
|
}
|
|
248
309
|
}
|
|
249
310
|
|
|
@@ -261,6 +322,13 @@ export abstract class AbstractSession<Provider, RetrieveBlockProgressEvents exte
|
|
|
261
322
|
return deferred.promise
|
|
262
323
|
}
|
|
263
324
|
|
|
325
|
+
/**
|
|
326
|
+
* If this session was created with specific providers, convert one of them
|
|
327
|
+
* into the format required or return `undefined` if the provider is not
|
|
328
|
+
* compatible with this session implementation
|
|
329
|
+
*/
|
|
330
|
+
abstract convertToProvider (provider: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<Provider | undefined>
|
|
331
|
+
|
|
264
332
|
/**
|
|
265
333
|
* This method should search for new providers and yield them.
|
|
266
334
|
*/
|