@helia/block-brokers 2.1.3 → 3.0.0
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 +1 -1
- package/dist/src/trustless-gateway/broker.d.ts +2 -3
- package/dist/src/trustless-gateway/broker.d.ts.map +1 -1
- package/dist/src/trustless-gateway/broker.js +9 -18
- package/dist/src/trustless-gateway/broker.js.map +1 -1
- package/dist/src/trustless-gateway/index.d.ts +16 -2
- package/dist/src/trustless-gateway/index.d.ts.map +1 -1
- package/dist/src/trustless-gateway/index.js +2 -8
- package/dist/src/trustless-gateway/index.js.map +1 -1
- package/dist/src/trustless-gateway/session.d.ts +1 -1
- package/dist/src/trustless-gateway/session.d.ts.map +1 -1
- package/dist/src/trustless-gateway/session.js +3 -34
- package/dist/src/trustless-gateway/session.js.map +1 -1
- package/dist/src/trustless-gateway/trustless-gateway.d.ts.map +1 -1
- package/dist/src/trustless-gateway/trustless-gateway.js +8 -1
- package/dist/src/trustless-gateway/trustless-gateway.js.map +1 -1
- package/dist/src/trustless-gateway/utils.d.ts +8 -0
- package/dist/src/trustless-gateway/utils.d.ts.map +1 -0
- package/dist/src/trustless-gateway/utils.js +34 -0
- package/dist/src/trustless-gateway/utils.js.map +1 -0
- package/dist/typedoc-urls.json +4 -0
- package/package.json +8 -6
- package/src/trustless-gateway/broker.ts +13 -20
- package/src/trustless-gateway/index.ts +17 -11
- package/src/trustless-gateway/session.ts +4 -43
- package/src/trustless-gateway/trustless-gateway.ts +10 -1
- package/src/trustless-gateway/utils.ts +45 -0
|
@@ -22,13 +22,12 @@ export interface CreateTrustlessGatewaySessionOptions extends CreateSessionOptio
|
|
|
22
22
|
* for blocks.
|
|
23
23
|
*/
|
|
24
24
|
export declare class TrustlessGatewayBlockBroker implements BlockBroker<TrustlessGatewayGetBlockProgressEvents> {
|
|
25
|
-
private readonly
|
|
26
|
-
private readonly
|
|
25
|
+
private readonly allowInsecure;
|
|
26
|
+
private readonly allowLocal;
|
|
27
27
|
private readonly routing;
|
|
28
28
|
private readonly log;
|
|
29
29
|
private readonly logger;
|
|
30
30
|
constructor(components: TrustlessGatewayComponents, init?: TrustlessGatewayBlockBrokerInit);
|
|
31
|
-
addGateway(gatewayOrUrl: string): void;
|
|
32
31
|
retrieve(cid: CID, options?: BlockRetrievalOptions<TrustlessGatewayGetBlockProgressEvents>): Promise<Uint8Array>;
|
|
33
32
|
createSession(options?: CreateTrustlessGatewaySessionOptions): BlockBroker<TrustlessGatewayGetBlockProgressEvents>;
|
|
34
33
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"broker.d.ts","sourceRoot":"","sources":["../../../src/trustless-gateway/broker.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,+BAA+B,EAAE,0BAA0B,EAAE,sCAAsC,EAAE,MAAM,YAAY,CAAA;AACrI,OAAO,KAAK,EAAW,qBAAqB,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAEzG,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAE3C,MAAM,WAAW,oCAAqC,SAAQ,oBAAoB,CAAC,sCAAsC,CAAC;IACxH;;;;;OAKG;IACH,aAAa,CAAC,EAAE,OAAO,CAAA;IAEvB;;;;;OAKG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB;AAED;;;GAGG;AACH,qBAAa,2BAA4B,YAAW,WAAW,CAAC,sCAAsC,CAAC;IACrG,OAAO,CAAC,QAAQ,CAAC,
|
|
1
|
+
{"version":3,"file":"broker.d.ts","sourceRoot":"","sources":["../../../src/trustless-gateway/broker.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,+BAA+B,EAAE,0BAA0B,EAAE,sCAAsC,EAAE,MAAM,YAAY,CAAA;AACrI,OAAO,KAAK,EAAW,qBAAqB,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAEzG,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAE3C,MAAM,WAAW,oCAAqC,SAAQ,oBAAoB,CAAC,sCAAsC,CAAC;IACxH;;;;;OAKG;IACH,aAAa,CAAC,EAAE,OAAO,CAAA;IAEvB;;;;;OAKG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB;AAED;;;GAGG;AACH,qBAAa,2BAA4B,YAAW,WAAW,CAAC,sCAAsC,CAAC;IACrG,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IACvC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;IAC5B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiB;gBAE3B,UAAU,EAAE,0BAA0B,EAAE,IAAI,GAAE,+BAAoC;IAQzF,QAAQ,CAAE,GAAG,EAAE,GAAG,EAAE,OAAO,GAAE,qBAAqB,CAAC,sCAAsC,CAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IA2C3H,aAAa,CAAE,OAAO,GAAE,oCAAyC,GAAG,WAAW,CAAC,sCAAsC,CAAC;CAMxH"}
|
|
@@ -1,35 +1,26 @@
|
|
|
1
1
|
import { createTrustlessGatewaySession } from './session.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { findHttpGatewayProviders } from './utils.js';
|
|
3
|
+
import { DEFAULT_ALLOW_INSECURE, DEFAULT_ALLOW_LOCAL } from './index.js';
|
|
4
4
|
/**
|
|
5
5
|
* A class that accepts a list of trustless gateways that are queried
|
|
6
6
|
* for blocks.
|
|
7
7
|
*/
|
|
8
8
|
export class TrustlessGatewayBlockBroker {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
allowInsecure;
|
|
10
|
+
allowLocal;
|
|
11
11
|
routing;
|
|
12
12
|
log;
|
|
13
13
|
logger;
|
|
14
14
|
constructor(components, init = {}) {
|
|
15
|
-
this.components = components;
|
|
16
15
|
this.log = components.logger.forComponent('helia:trustless-gateway-block-broker');
|
|
17
16
|
this.logger = components.logger;
|
|
18
17
|
this.routing = components.routing;
|
|
19
|
-
this.
|
|
20
|
-
|
|
21
|
-
return new TrustlessGateway(gatewayOrUrl, components.logger);
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
addGateway(gatewayOrUrl) {
|
|
25
|
-
this.gateways.push(new TrustlessGateway(gatewayOrUrl, this.components.logger));
|
|
18
|
+
this.allowInsecure = init.allowInsecure ?? DEFAULT_ALLOW_INSECURE;
|
|
19
|
+
this.allowLocal = init.allowLocal ?? DEFAULT_ALLOW_LOCAL;
|
|
26
20
|
}
|
|
27
21
|
async retrieve(cid, options = {}) {
|
|
28
|
-
// Loop through the gateways until we get a block or run out of gateways
|
|
29
|
-
// TODO: switch to toSorted when support is better
|
|
30
|
-
const sortedGateways = this.gateways.sort((a, b) => b.reliability() - a.reliability());
|
|
31
22
|
const aggregateErrors = [];
|
|
32
|
-
for (const gateway of
|
|
23
|
+
for await (const gateway of findHttpGatewayProviders(cid, this.routing, this.logger, this.allowInsecure, this.allowLocal, options)) {
|
|
33
24
|
this.log('getting block for %c from %s', cid, gateway.url);
|
|
34
25
|
try {
|
|
35
26
|
const block = await gateway.getRawBlock(cid, options.signal);
|
|
@@ -39,8 +30,8 @@ export class TrustlessGatewayBlockBroker {
|
|
|
39
30
|
}
|
|
40
31
|
catch (err) {
|
|
41
32
|
this.log.error('failed to validate block for %c from %s', cid, gateway.url, err);
|
|
42
|
-
gateway
|
|
43
|
-
|
|
33
|
+
// try another gateway
|
|
34
|
+
continue;
|
|
44
35
|
}
|
|
45
36
|
return block;
|
|
46
37
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"broker.js","sourceRoot":"","sources":["../../../src/trustless-gateway/broker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,MAAM,cAAc,CAAA;AAC5D,OAAO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AI,KAAK,CAAC,qCAAqC,GAAG,mBAAmB,CAAC,CAAA;QAC9E,CAAC;IACH,CAAC;IAED,aAAa,CAAE,UAAgD,EAAE;QAC/D,OAAO,6BAA6B,CAAC;YACnC,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,EAAE,OAAO,CAAC,CAAA;IACb,CAAC;CACF"}
|
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
import type { Routing, BlockBroker } from '@helia/interface';
|
|
2
2
|
import type { ComponentLogger } from '@libp2p/interface';
|
|
3
3
|
import type { ProgressEvent } from 'progress-events';
|
|
4
|
-
export declare const
|
|
4
|
+
export declare const DEFAULT_ALLOW_INSECURE = false;
|
|
5
|
+
export declare const DEFAULT_ALLOW_LOCAL = false;
|
|
5
6
|
export type TrustlessGatewayGetBlockProgressEvents = ProgressEvent<'trustless-gateway:get-block:fetch', URL>;
|
|
6
7
|
export interface TrustlessGatewayBlockBrokerInit {
|
|
7
|
-
|
|
8
|
+
/**
|
|
9
|
+
* By default we will only connect to peers with HTTPS addresses, pass true
|
|
10
|
+
* to also connect to HTTP addresses.
|
|
11
|
+
*
|
|
12
|
+
* @default false
|
|
13
|
+
*/
|
|
14
|
+
allowInsecure?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* By default we will only connect to peers with public or DNS addresses, pass
|
|
17
|
+
* true to also connect to private addresses.
|
|
18
|
+
*
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
allowLocal?: boolean;
|
|
8
22
|
}
|
|
9
23
|
export interface TrustlessGatewayComponents {
|
|
10
24
|
routing: Routing;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/trustless-gateway/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC5D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAEpD,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/trustless-gateway/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC5D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAEpD,eAAO,MAAM,sBAAsB,QAAQ,CAAA;AAC3C,eAAO,MAAM,mBAAmB,QAAQ,CAAA;AAExC,MAAM,MAAM,sCAAsC,GAChD,aAAa,CAAC,mCAAmC,EAAE,GAAG,CAAC,CAAA;AAEzD,MAAM,WAAW,+BAA+B;IAC9C;;;;;OAKG;IACH,aAAa,CAAC,EAAE,OAAO,CAAA;IAEvB;;;;;OAKG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB;AAED,MAAM,WAAW,0BAA0B;IACzC,OAAO,EAAE,OAAO,CAAA;IAChB,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,wBAAgB,gBAAgB,CAAE,IAAI,GAAE,+BAAoC,GAAG,CAAC,UAAU,EAAE,0BAA0B,KAAK,WAAW,CAAC,sCAAsC,CAAC,CAE7K"}
|
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import { TrustlessGatewayBlockBroker } from './broker.js';
|
|
2
|
-
export const
|
|
3
|
-
|
|
4
|
-
'https://trustless-gateway.link',
|
|
5
|
-
// 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/
|
|
6
|
-
'https://cloudflare-ipfs.com',
|
|
7
|
-
// 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/
|
|
8
|
-
'https://4everland.io'
|
|
9
|
-
];
|
|
2
|
+
export const DEFAULT_ALLOW_INSECURE = false;
|
|
3
|
+
export const DEFAULT_ALLOW_LOCAL = false;
|
|
10
4
|
export function trustlessGateway(init = {}) {
|
|
11
5
|
return (components) => new TrustlessGatewayBlockBroker(components, init);
|
|
12
6
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/trustless-gateway/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAA;AAKzD,MAAM,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/trustless-gateway/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAA;AAKzD,MAAM,CAAC,MAAM,sBAAsB,GAAG,KAAK,CAAA;AAC3C,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,CAAA;AA4BxC,MAAM,UAAU,gBAAgB,CAAE,OAAwC,EAAE;IAC1E,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,2BAA2B,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;AAC1E,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AbstractSession } from '@helia/utils';
|
|
2
|
-
import { TrustlessGateway } from './trustless-gateway.js';
|
|
3
2
|
import type { CreateTrustlessGatewaySessionOptions } from './broker.js';
|
|
4
3
|
import type { TrustlessGatewayGetBlockProgressEvents } from './index.js';
|
|
4
|
+
import type { TrustlessGateway } from './trustless-gateway.js';
|
|
5
5
|
import type { BlockRetrievalOptions, Routing } from '@helia/interface';
|
|
6
6
|
import type { ComponentLogger } from '@libp2p/interface';
|
|
7
7
|
import type { AbortOptions } from 'interface-store';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../../src/trustless-gateway/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,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AAgB,EAAE,SAAS,EAAE,gBAAgB,GAAG,OAAO;CAG3E;AAED,wBAAgB,6BAA6B,CAAE,UAAU,EAAE,iCAAiC,EAAE,IAAI,EAAE,oCAAoC,GAAG,uBAAuB,CAEjK"}
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { AbstractSession } from '@helia/utils';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { multiaddrToUri } from '@multiformats/multiaddr-to-uri';
|
|
5
|
-
import { TrustlessGateway } from './trustless-gateway.js';
|
|
6
|
-
const DEFAULT_ALLOW_INSECURE = false;
|
|
7
|
-
const DEFAULT_ALLOW_LOCAL = false;
|
|
2
|
+
import { findHttpGatewayProviders } from './utils.js';
|
|
3
|
+
import { DEFAULT_ALLOW_INSECURE, DEFAULT_ALLOW_LOCAL } from './index.js';
|
|
8
4
|
class TrustlessGatewaySession extends AbstractSession {
|
|
9
5
|
routing;
|
|
10
6
|
allowInsecure;
|
|
@@ -26,20 +22,7 @@ class TrustlessGatewaySession extends AbstractSession {
|
|
|
26
22
|
return block;
|
|
27
23
|
}
|
|
28
24
|
async *findNewProviders(cid, options = {}) {
|
|
29
|
-
|
|
30
|
-
// require http(s) addresses
|
|
31
|
-
const httpAddresses = filterMultiaddrs(provider.multiaddrs, this.allowInsecure, this.allowLocal);
|
|
32
|
-
if (httpAddresses.length === 0) {
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
// take first address?
|
|
36
|
-
// /ip4/x.x.x.x/tcp/31337/http
|
|
37
|
-
// /ip4/x.x.x.x/tcp/31337/https
|
|
38
|
-
// etc
|
|
39
|
-
const uri = multiaddrToUri(httpAddresses[0]);
|
|
40
|
-
this.log('found http-gateway provider %p %s for cid %c', provider.id, uri, cid);
|
|
41
|
-
yield new TrustlessGateway(uri, this.logger);
|
|
42
|
-
}
|
|
25
|
+
yield* findHttpGatewayProviders(cid, this.routing, this.logger, this.allowInsecure, this.allowLocal, options);
|
|
43
26
|
}
|
|
44
27
|
toEvictionKey(provider) {
|
|
45
28
|
return provider.url.toString();
|
|
@@ -48,20 +31,6 @@ class TrustlessGatewaySession extends AbstractSession {
|
|
|
48
31
|
return providerA.url.toString() === providerB.url.toString();
|
|
49
32
|
}
|
|
50
33
|
}
|
|
51
|
-
function filterMultiaddrs(multiaddrs, allowInsecure, allowLocal) {
|
|
52
|
-
return multiaddrs.filter(ma => {
|
|
53
|
-
if (HTTPS.matches(ma) || (allowInsecure && HTTP.matches(ma))) {
|
|
54
|
-
if (allowLocal) {
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
if (DNS.matches(ma)) {
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
return isPrivateIp(ma.toOptions().host) === false;
|
|
61
|
-
}
|
|
62
|
-
return false;
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
34
|
export function createTrustlessGatewaySession(components, init) {
|
|
66
35
|
return new TrustlessGatewaySession(components, init);
|
|
67
36
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../../src/trustless-gateway/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAC9C,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../../src/trustless-gateway/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAC9C,OAAO,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAA;AACrD,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAcxE,MAAM,uBAAwB,SAAQ,eAAyE;IAC5F,OAAO,CAAS;IAChB,aAAa,CAAS;IACtB,UAAU,CAAS;IAEpC,YAAa,UAA6C,EAAE,IAA0C;QACpG,KAAK,CAAC,UAAU,EAAE;YAChB,GAAG,IAAI;YACP,IAAI,EAAE,iCAAiC;SACxC,CA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AC,UAAU,EAAE,IAAI,CAAC,CAAA;AACtD,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trustless-gateway.d.ts","sourceRoot":"","sources":["../../../src/trustless-gateway/trustless-gateway.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAU,MAAM,mBAAmB,CAAA;AAChE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAE3C,MAAM,WAAW,qBAAqB;IACpC,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,aAAa,EAAE,MAAM,CAAA;IACrB,SAAS,EAAE,MAAM,CAAA;IACjB,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAC1B;AAED;;;;;GAKG;AACH,qBAAa,gBAAgB;;IAC3B,SAAgB,GAAG,EAAE,GAAG,CAAA;IAoCxB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;gBAEf,GAAG,EAAE,GAAG,GAAG,MAAM,EAAE,MAAM,EAAE,eAAe;IAmBvD;;;OAGG;IACG,WAAW,CAAE,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"trustless-gateway.d.ts","sourceRoot":"","sources":["../../../src/trustless-gateway/trustless-gateway.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAU,MAAM,mBAAmB,CAAA;AAChE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAE3C,MAAM,WAAW,qBAAqB;IACpC,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,aAAa,EAAE,MAAM,CAAA;IACrB,SAAS,EAAE,MAAM,CAAA;IACjB,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAC1B;AAED;;;;;GAKG;AACH,qBAAa,gBAAgB;;IAC3B,SAAgB,GAAG,EAAE,GAAG,CAAA;IAoCxB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;gBAEf,GAAG,EAAE,GAAG,GAAG,MAAM,EAAE,MAAM,EAAE,eAAe;IAmBvD;;;OAGG;IACG,WAAW,CAAE,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;IAyDvE;;;;;;;OAOG;IACH,WAAW,IAAK,MAAM;IAyBtB;;OAEG;IACH,sBAAsB,IAAK,IAAI;IAI/B,QAAQ,IAAK,qBAAqB;CASnC"}
|
|
@@ -69,12 +69,18 @@ export class TrustlessGateway {
|
|
|
69
69
|
throw new Error(`Signal to fetch raw block for CID ${cid} from gateway ${this.url} was aborted prior to fetch`);
|
|
70
70
|
}
|
|
71
71
|
const blockId = this.#uniqueBlockId(cid);
|
|
72
|
+
// workaround for https://github.com/nodejs/node/issues/52635
|
|
73
|
+
const innerController = new AbortController();
|
|
74
|
+
const abortInnerSignal = () => {
|
|
75
|
+
innerController.abort();
|
|
76
|
+
};
|
|
77
|
+
signal?.addEventListener('abort', abortInnerSignal);
|
|
72
78
|
try {
|
|
73
79
|
let pendingResponse = this.#pendingResponses.get(blockId);
|
|
74
80
|
if (pendingResponse == null) {
|
|
75
81
|
this.#attempts++;
|
|
76
82
|
pendingResponse = fetch(gwUrl.toString(), {
|
|
77
|
-
signal,
|
|
83
|
+
signal: innerController.signal,
|
|
78
84
|
headers: {
|
|
79
85
|
Accept: 'application/vnd.ipld.raw'
|
|
80
86
|
},
|
|
@@ -102,6 +108,7 @@ export class TrustlessGateway {
|
|
|
102
108
|
throw new Error(`unable to fetch raw block for CID ${cid}`);
|
|
103
109
|
}
|
|
104
110
|
finally {
|
|
111
|
+
signal?.removeEventListener('abort', abortInnerSignal);
|
|
105
112
|
this.#pendingResponses.delete(blockId);
|
|
106
113
|
}
|
|
107
114
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trustless-gateway.js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IAED;;OAEG;IACH,sBAAsB;QACpB,IAAI,CAAC,cAAc,EAAE,CAAA;IACvB,CAAC;IAED,QAAQ;QACN,OAAO;YACL,QAAQ,EAAE,IAAI,CAAC,SAAS;YACxB,MAAM,EAAE,IAAI,CAAC,OAAO;YACpB,aAAa,EAAE,IAAI,CAAC,cAAc;YAClC,SAAS,EAAE,IAAI,CAAC,UAAU;YAC1B,gBAAgB,EAAE,IAAI,CAAC,iBAAiB,CAAC,IAAI;SAC9C,CAAA;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { TrustlessGateway } from './trustless-gateway.js';
|
|
2
|
+
import type { Routing } from '@helia/interface';
|
|
3
|
+
import type { ComponentLogger } from '@libp2p/interface';
|
|
4
|
+
import type { AbortOptions, Multiaddr } from '@multiformats/multiaddr';
|
|
5
|
+
import type { CID } from 'multiformats/cid';
|
|
6
|
+
export declare function filterNonHTTPMultiaddrs(multiaddrs: Multiaddr[], allowInsecure: boolean, allowLocal: boolean): Multiaddr[];
|
|
7
|
+
export declare function findHttpGatewayProviders(cid: CID, routing: Routing, logger: ComponentLogger, allowInsecure: boolean, allowLocal: boolean, options?: AbortOptions): AsyncGenerator<TrustlessGateway>;
|
|
8
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/trustless-gateway/utils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AACzD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAC/C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACtE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAE3C,wBAAgB,uBAAuB,CAAE,UAAU,EAAE,SAAS,EAAE,EAAE,aAAa,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,GAAG,SAAS,EAAE,CAgB1H;AAED,wBAAwB,wBAAwB,CAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,aAAa,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,cAAc,CAAC,gBAAgB,CAAC,CAiB5M"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { isPrivateIp } from '@libp2p/utils/private-ip';
|
|
2
|
+
import { DNS, HTTP, HTTPS } from '@multiformats/multiaddr-matcher';
|
|
3
|
+
import { multiaddrToUri } from '@multiformats/multiaddr-to-uri';
|
|
4
|
+
import { TrustlessGateway } from './trustless-gateway.js';
|
|
5
|
+
export function filterNonHTTPMultiaddrs(multiaddrs, allowInsecure, allowLocal) {
|
|
6
|
+
return multiaddrs.filter(ma => {
|
|
7
|
+
if (HTTPS.matches(ma) || (allowInsecure && HTTP.matches(ma))) {
|
|
8
|
+
if (allowLocal) {
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
if (DNS.matches(ma)) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
return isPrivateIp(ma.toOptions().host) === false;
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
export async function* findHttpGatewayProviders(cid, routing, logger, allowInsecure, allowLocal, options) {
|
|
20
|
+
for await (const provider of routing.findProviders(cid, options)) {
|
|
21
|
+
// require http(s) addresses
|
|
22
|
+
const httpAddresses = filterNonHTTPMultiaddrs(provider.multiaddrs, allowInsecure, allowLocal);
|
|
23
|
+
if (httpAddresses.length === 0) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
// take first address?
|
|
27
|
+
// /ip4/x.x.x.x/tcp/31337/http
|
|
28
|
+
// /ip4/x.x.x.x/tcp/31337/https
|
|
29
|
+
// etc
|
|
30
|
+
const uri = multiaddrToUri(httpAddresses[0]);
|
|
31
|
+
yield new TrustlessGateway(uri, logger);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/trustless-gateway/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AACtD,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,iCAAiC,CAAA;AAClE,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAA;AAC/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAMzD,MAAM,UAAU,uBAAuB,CAAE,UAAuB,EAAE,aAAsB,EAAE,UAAmB;IAC3G,OAAO,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE;QAC5B,IAAI,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;YAC7D,IAAI,UAAU,EAAE,CAAC;gBACf,OAAO,IAAI,CAAA;YACb,CAAC;YAED,IAAI,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC;gBACpB,OAAO,IAAI,CAAA;YACb,CAAC;YAED,OAAO,WAAW,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,IAAI,CAAC,KAAK,KAAK,CAAA;QACnD,CAAC;QAED,OAAO,KAAK,CAAA;IACd,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,SAAU,CAAC,CAAC,wBAAwB,CAAE,GAAQ,EAAE,OAAgB,EAAE,MAAuB,EAAE,aAAsB,EAAE,UAAmB,EAAE,OAAsB;IACxK,IAAI,KAAK,EAAE,MAAM,QAAQ,IAAI,OAAO,CAAC,aAAa,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE,CAAC;QACjE,4BAA4B;QAC5B,MAAM,aAAa,GAAG,uBAAuB,CAAC,QAAQ,CAAC,UAAU,EAAE,aAAa,EAAE,UAAU,CAAC,CAAA;QAE7F,IAAI,aAAa,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC/B,SAAQ;QACV,CAAC;QAED,sBAAsB;QACtB,8BAA8B;QAC9B,+BAA+B;QAC/B,MAAM;QACN,MAAM,GAAG,GAAG,cAAc,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAA;QAE5C,MAAM,IAAI,gBAAgB,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;IACzC,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helia/block-brokers",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Block brokers for Helia",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/ipfs/helia/tree/main/packages/block-brokers#readme",
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"url": "https://github.com/ipfs/helia/issues"
|
|
13
13
|
},
|
|
14
14
|
"publishConfig": {
|
|
15
|
-
"access": "public"
|
|
15
|
+
"access": "public",
|
|
16
|
+
"provenance": true
|
|
16
17
|
},
|
|
17
18
|
"keywords": [
|
|
18
19
|
"IPFS"
|
|
@@ -52,9 +53,9 @@
|
|
|
52
53
|
"test:electron-main": "aegir test -t electron-main"
|
|
53
54
|
},
|
|
54
55
|
"dependencies": {
|
|
55
|
-
"@helia/bitswap": "^1.0
|
|
56
|
-
"@helia/interface": "^4.
|
|
57
|
-
"@helia/utils": "^0.
|
|
56
|
+
"@helia/bitswap": "^1.1.0",
|
|
57
|
+
"@helia/interface": "^4.3.0",
|
|
58
|
+
"@helia/utils": "^0.3.0",
|
|
58
59
|
"@libp2p/interface": "^1.1.4",
|
|
59
60
|
"@libp2p/utils": "^5.2.6",
|
|
60
61
|
"@multiformats/multiaddr": "^12.2.1",
|
|
@@ -75,6 +76,7 @@
|
|
|
75
76
|
"cors": "^2.8.5",
|
|
76
77
|
"polka": "^0.5.2",
|
|
77
78
|
"sinon": "^17.0.1",
|
|
78
|
-
"sinon-ts": "^2.0.0"
|
|
79
|
+
"sinon-ts": "^2.0.0",
|
|
80
|
+
"uint8arrays": "^5.0.3"
|
|
79
81
|
}
|
|
80
82
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createTrustlessGatewaySession } from './session.js'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { findHttpGatewayProviders } from './utils.js'
|
|
3
|
+
import { DEFAULT_ALLOW_INSECURE, DEFAULT_ALLOW_LOCAL } from './index.js'
|
|
4
4
|
import type { TrustlessGatewayBlockBrokerInit, TrustlessGatewayComponents, TrustlessGatewayGetBlockProgressEvents } from './index.js'
|
|
5
5
|
import type { Routing, BlockRetrievalOptions, BlockBroker, CreateSessionOptions } from '@helia/interface'
|
|
6
6
|
import type { ComponentLogger, Logger } from '@libp2p/interface'
|
|
@@ -29,55 +29,48 @@ export interface CreateTrustlessGatewaySessionOptions extends CreateSessionOptio
|
|
|
29
29
|
* for blocks.
|
|
30
30
|
*/
|
|
31
31
|
export class TrustlessGatewayBlockBroker implements BlockBroker<TrustlessGatewayGetBlockProgressEvents> {
|
|
32
|
-
private readonly
|
|
33
|
-
private readonly
|
|
32
|
+
private readonly allowInsecure: boolean
|
|
33
|
+
private readonly allowLocal: boolean
|
|
34
34
|
private readonly routing: Routing
|
|
35
35
|
private readonly log: Logger
|
|
36
36
|
private readonly logger: ComponentLogger
|
|
37
37
|
|
|
38
38
|
constructor (components: TrustlessGatewayComponents, init: TrustlessGatewayBlockBrokerInit = {}) {
|
|
39
|
-
this.components = components
|
|
40
39
|
this.log = components.logger.forComponent('helia:trustless-gateway-block-broker')
|
|
41
40
|
this.logger = components.logger
|
|
42
41
|
this.routing = components.routing
|
|
43
|
-
this.
|
|
44
|
-
|
|
45
|
-
return new TrustlessGateway(gatewayOrUrl, components.logger)
|
|
46
|
-
})
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
addGateway (gatewayOrUrl: string): void {
|
|
50
|
-
this.gateways.push(new TrustlessGateway(gatewayOrUrl, this.components.logger))
|
|
42
|
+
this.allowInsecure = init.allowInsecure ?? DEFAULT_ALLOW_INSECURE
|
|
43
|
+
this.allowLocal = init.allowLocal ?? DEFAULT_ALLOW_LOCAL
|
|
51
44
|
}
|
|
52
45
|
|
|
53
46
|
async retrieve (cid: CID, options: BlockRetrievalOptions<TrustlessGatewayGetBlockProgressEvents> = {}): Promise<Uint8Array> {
|
|
54
|
-
// Loop through the gateways until we get a block or run out of gateways
|
|
55
|
-
// TODO: switch to toSorted when support is better
|
|
56
|
-
const sortedGateways = this.gateways.sort((a, b) => b.reliability() - a.reliability())
|
|
57
47
|
const aggregateErrors: Error[] = []
|
|
58
48
|
|
|
59
|
-
for (const gateway of
|
|
49
|
+
for await (const gateway of findHttpGatewayProviders(cid, this.routing, this.logger, this.allowInsecure, this.allowLocal, options)) {
|
|
60
50
|
this.log('getting block for %c from %s', cid, gateway.url)
|
|
51
|
+
|
|
61
52
|
try {
|
|
62
53
|
const block = await gateway.getRawBlock(cid, options.signal)
|
|
63
54
|
this.log.trace('got block for %c from %s', cid, gateway.url)
|
|
55
|
+
|
|
64
56
|
try {
|
|
65
57
|
await options.validateFn?.(block)
|
|
66
58
|
} catch (err) {
|
|
67
59
|
this.log.error('failed to validate block for %c from %s', cid, gateway.url, err)
|
|
68
|
-
gateway
|
|
69
|
-
|
|
70
|
-
throw new Error(`Block for CID ${cid} from gateway ${gateway.url} failed validation`)
|
|
60
|
+
// try another gateway
|
|
61
|
+
continue
|
|
71
62
|
}
|
|
72
63
|
|
|
73
64
|
return block
|
|
74
65
|
} catch (err: unknown) {
|
|
75
66
|
this.log.error('failed to get block for %c from %s', cid, gateway.url, err)
|
|
67
|
+
|
|
76
68
|
if (err instanceof Error) {
|
|
77
69
|
aggregateErrors.push(err)
|
|
78
70
|
} else {
|
|
79
71
|
aggregateErrors.push(new Error(`Unable to fetch raw block for CID ${cid} from gateway ${gateway.url}`))
|
|
80
72
|
}
|
|
73
|
+
|
|
81
74
|
// if signal was aborted, exit the loop
|
|
82
75
|
if (options.signal?.aborted === true) {
|
|
83
76
|
this.log.trace('request aborted while fetching raw block for CID %c from gateway %s', cid, gateway.url)
|
|
@@ -3,22 +3,28 @@ import type { Routing, BlockBroker } from '@helia/interface'
|
|
|
3
3
|
import type { ComponentLogger } from '@libp2p/interface'
|
|
4
4
|
import type { ProgressEvent } from 'progress-events'
|
|
5
5
|
|
|
6
|
-
export const
|
|
7
|
-
|
|
8
|
-
'https://trustless-gateway.link',
|
|
9
|
-
|
|
10
|
-
// 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/
|
|
11
|
-
'https://cloudflare-ipfs.com',
|
|
12
|
-
|
|
13
|
-
// 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/
|
|
14
|
-
'https://4everland.io'
|
|
15
|
-
]
|
|
6
|
+
export const DEFAULT_ALLOW_INSECURE = false
|
|
7
|
+
export const DEFAULT_ALLOW_LOCAL = false
|
|
16
8
|
|
|
17
9
|
export type TrustlessGatewayGetBlockProgressEvents =
|
|
18
10
|
ProgressEvent<'trustless-gateway:get-block:fetch', URL>
|
|
19
11
|
|
|
20
12
|
export interface TrustlessGatewayBlockBrokerInit {
|
|
21
|
-
|
|
13
|
+
/**
|
|
14
|
+
* By default we will only connect to peers with HTTPS addresses, pass true
|
|
15
|
+
* to also connect to HTTP addresses.
|
|
16
|
+
*
|
|
17
|
+
* @default false
|
|
18
|
+
*/
|
|
19
|
+
allowInsecure?: boolean
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* By default we will only connect to peers with public or DNS addresses, pass
|
|
23
|
+
* true to also connect to private addresses.
|
|
24
|
+
*
|
|
25
|
+
* @default false
|
|
26
|
+
*/
|
|
27
|
+
allowLocal?: boolean
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
export interface TrustlessGatewayComponents {
|
|
@@ -1,19 +1,14 @@
|
|
|
1
1
|
import { AbstractSession } from '@helia/utils'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { multiaddrToUri } from '@multiformats/multiaddr-to-uri'
|
|
5
|
-
import { TrustlessGateway } from './trustless-gateway.js'
|
|
2
|
+
import { findHttpGatewayProviders } from './utils.js'
|
|
3
|
+
import { DEFAULT_ALLOW_INSECURE, DEFAULT_ALLOW_LOCAL } from './index.js'
|
|
6
4
|
import type { CreateTrustlessGatewaySessionOptions } from './broker.js'
|
|
7
5
|
import type { TrustlessGatewayGetBlockProgressEvents } from './index.js'
|
|
6
|
+
import type { TrustlessGateway } from './trustless-gateway.js'
|
|
8
7
|
import type { BlockRetrievalOptions, Routing } from '@helia/interface'
|
|
9
8
|
import type { ComponentLogger } from '@libp2p/interface'
|
|
10
|
-
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
11
9
|
import type { AbortOptions } from 'interface-store'
|
|
12
10
|
import type { CID } from 'multiformats/cid'
|
|
13
11
|
|
|
14
|
-
const DEFAULT_ALLOW_INSECURE = false
|
|
15
|
-
const DEFAULT_ALLOW_LOCAL = false
|
|
16
|
-
|
|
17
12
|
export interface TrustlessGatewaySessionComponents {
|
|
18
13
|
logger: ComponentLogger
|
|
19
14
|
routing: Routing
|
|
@@ -47,23 +42,7 @@ class TrustlessGatewaySession extends AbstractSession<TrustlessGateway, Trustles
|
|
|
47
42
|
}
|
|
48
43
|
|
|
49
44
|
async * findNewProviders (cid: CID, options: AbortOptions = {}): AsyncGenerator<TrustlessGateway> {
|
|
50
|
-
|
|
51
|
-
// require http(s) addresses
|
|
52
|
-
const httpAddresses = filterMultiaddrs(provider.multiaddrs, this.allowInsecure, this.allowLocal)
|
|
53
|
-
|
|
54
|
-
if (httpAddresses.length === 0) {
|
|
55
|
-
continue
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// take first address?
|
|
59
|
-
// /ip4/x.x.x.x/tcp/31337/http
|
|
60
|
-
// /ip4/x.x.x.x/tcp/31337/https
|
|
61
|
-
// etc
|
|
62
|
-
const uri = multiaddrToUri(httpAddresses[0])
|
|
63
|
-
|
|
64
|
-
this.log('found http-gateway provider %p %s for cid %c', provider.id, uri, cid)
|
|
65
|
-
yield new TrustlessGateway(uri, this.logger)
|
|
66
|
-
}
|
|
45
|
+
yield * findHttpGatewayProviders(cid, this.routing, this.logger, this.allowInsecure, this.allowLocal, options)
|
|
67
46
|
}
|
|
68
47
|
|
|
69
48
|
toEvictionKey (provider: TrustlessGateway): Uint8Array | string {
|
|
@@ -75,24 +54,6 @@ class TrustlessGatewaySession extends AbstractSession<TrustlessGateway, Trustles
|
|
|
75
54
|
}
|
|
76
55
|
}
|
|
77
56
|
|
|
78
|
-
function filterMultiaddrs (multiaddrs: Multiaddr[], allowInsecure: boolean, allowLocal: boolean): Multiaddr[] {
|
|
79
|
-
return multiaddrs.filter(ma => {
|
|
80
|
-
if (HTTPS.matches(ma) || (allowInsecure && HTTP.matches(ma))) {
|
|
81
|
-
if (allowLocal) {
|
|
82
|
-
return true
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (DNS.matches(ma)) {
|
|
86
|
-
return true
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return isPrivateIp(ma.toOptions().host) === false
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return false
|
|
93
|
-
})
|
|
94
|
-
}
|
|
95
|
-
|
|
96
57
|
export function createTrustlessGatewaySession (components: TrustlessGatewaySessionComponents, init: CreateTrustlessGatewaySessionOptions): TrustlessGatewaySession {
|
|
97
58
|
return new TrustlessGatewaySession(components, init)
|
|
98
59
|
}
|
|
@@ -91,12 +91,20 @@ export class TrustlessGateway {
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
const blockId = this.#uniqueBlockId(cid)
|
|
94
|
+
|
|
95
|
+
// workaround for https://github.com/nodejs/node/issues/52635
|
|
96
|
+
const innerController = new AbortController()
|
|
97
|
+
const abortInnerSignal = (): void => {
|
|
98
|
+
innerController.abort()
|
|
99
|
+
}
|
|
100
|
+
signal?.addEventListener('abort', abortInnerSignal)
|
|
101
|
+
|
|
94
102
|
try {
|
|
95
103
|
let pendingResponse: Promise<Uint8Array> | undefined = this.#pendingResponses.get(blockId)
|
|
96
104
|
if (pendingResponse == null) {
|
|
97
105
|
this.#attempts++
|
|
98
106
|
pendingResponse = fetch(gwUrl.toString(), {
|
|
99
|
-
signal,
|
|
107
|
+
signal: innerController.signal,
|
|
100
108
|
headers: {
|
|
101
109
|
Accept: 'application/vnd.ipld.raw'
|
|
102
110
|
},
|
|
@@ -122,6 +130,7 @@ export class TrustlessGateway {
|
|
|
122
130
|
this.#errors++
|
|
123
131
|
throw new Error(`unable to fetch raw block for CID ${cid}`)
|
|
124
132
|
} finally {
|
|
133
|
+
signal?.removeEventListener('abort', abortInnerSignal)
|
|
125
134
|
this.#pendingResponses.delete(blockId)
|
|
126
135
|
}
|
|
127
136
|
}
|