@helia/block-brokers 2.1.2 → 2.1.3
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 +3 -2
- package/dist/src/trustless-gateway/broker.d.ts.map +1 -1
- package/dist/src/trustless-gateway/broker.js +18 -9
- package/dist/src/trustless-gateway/broker.js.map +1 -1
- package/dist/src/trustless-gateway/index.d.ts +2 -16
- package/dist/src/trustless-gateway/index.d.ts.map +1 -1
- package/dist/src/trustless-gateway/index.js +8 -2
- 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 +34 -3
- 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 +1 -8
- package/dist/src/trustless-gateway/trustless-gateway.js.map +1 -1
- package/package.json +6 -8
- package/src/trustless-gateway/broker.ts +20 -13
- package/src/trustless-gateway/index.ts +11 -17
- package/src/trustless-gateway/session.ts +43 -4
- package/src/trustless-gateway/trustless-gateway.ts +1 -10
- package/dist/src/trustless-gateway/utils.d.ts +0 -8
- package/dist/src/trustless-gateway/utils.d.ts.map +0 -1
- package/dist/src/trustless-gateway/utils.js +0 -34
- package/dist/src/trustless-gateway/utils.js.map +0 -1
- package/dist/typedoc-urls.json +0 -4
- package/src/trustless-gateway/utils.ts +0 -45
|
@@ -22,12 +22,13 @@ 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 components;
|
|
26
|
+
private readonly gateways;
|
|
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;
|
|
31
32
|
retrieve(cid: CID, options?: BlockRetrievalOptions<TrustlessGatewayGetBlockProgressEvents>): Promise<Uint8Array>;
|
|
32
33
|
createSession(options?: CreateTrustlessGatewaySessionOptions): BlockBroker<TrustlessGatewayGetBlockProgressEvents>;
|
|
33
34
|
}
|
|
@@ -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,UAAU,CAA4B;IACvD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAoB;IAC7C,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;IAW/F,UAAU,CAAE,YAAY,EAAE,MAAM,GAAG,IAAI;IAIjC,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,26 +1,35 @@
|
|
|
1
1
|
import { createTrustlessGatewaySession } from './session.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { TrustlessGateway } from './trustless-gateway.js';
|
|
3
|
+
import { DEFAULT_TRUSTLESS_GATEWAYS } 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
|
+
components;
|
|
10
|
+
gateways;
|
|
11
11
|
routing;
|
|
12
12
|
log;
|
|
13
13
|
logger;
|
|
14
14
|
constructor(components, init = {}) {
|
|
15
|
+
this.components = components;
|
|
15
16
|
this.log = components.logger.forComponent('helia:trustless-gateway-block-broker');
|
|
16
17
|
this.logger = components.logger;
|
|
17
18
|
this.routing = components.routing;
|
|
18
|
-
this.
|
|
19
|
-
|
|
19
|
+
this.gateways = (init.gateways ?? DEFAULT_TRUSTLESS_GATEWAYS)
|
|
20
|
+
.map((gatewayOrUrl) => {
|
|
21
|
+
return new TrustlessGateway(gatewayOrUrl, components.logger);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
addGateway(gatewayOrUrl) {
|
|
25
|
+
this.gateways.push(new TrustlessGateway(gatewayOrUrl, this.components.logger));
|
|
20
26
|
}
|
|
21
27
|
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());
|
|
22
31
|
const aggregateErrors = [];
|
|
23
|
-
for
|
|
32
|
+
for (const gateway of sortedGateways) {
|
|
24
33
|
this.log('getting block for %c from %s', cid, gateway.url);
|
|
25
34
|
try {
|
|
26
35
|
const block = await gateway.getRawBlock(cid, options.signal);
|
|
@@ -30,8 +39,8 @@ export class TrustlessGatewayBlockBroker {
|
|
|
30
39
|
}
|
|
31
40
|
catch (err) {
|
|
32
41
|
this.log.error('failed to validate block for %c from %s', cid, gateway.url, err);
|
|
33
|
-
|
|
34
|
-
|
|
42
|
+
gateway.incrementInvalidBlocks();
|
|
43
|
+
throw new Error(`Block for CID ${cid} from gateway ${gateway.url} failed validation`);
|
|
35
44
|
}
|
|
36
45
|
return block;
|
|
37
46
|
}
|
|
@@ -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,EAAE,
|
|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CAAC,CAAC,CAAA;gBACzG,CAAC;gBACD,uCAAuC;gBACvC,IAAI,OAAO,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,EAAE,CAAC;oBACrC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,qEAAqE,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,CAAA;oBACvG,MAAK;gBACP,CAAC;YACH,CAAC;QACH,CAAC;QAED,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC/B,MAAM,IAAI,cAAc,CAAC,eAAe,EAAE,qCAAqC,GAAG,mBAAmB,CAAC,CAAA;QACxG,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,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,24 +1,10 @@
|
|
|
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
|
|
5
|
-
export declare const DEFAULT_ALLOW_LOCAL = false;
|
|
4
|
+
export declare const DEFAULT_TRUSTLESS_GATEWAYS: string[];
|
|
6
5
|
export type TrustlessGatewayGetBlockProgressEvents = ProgressEvent<'trustless-gateway:get-block:fetch', URL>;
|
|
7
6
|
export interface TrustlessGatewayBlockBrokerInit {
|
|
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;
|
|
7
|
+
gateways?: Array<string | URL>;
|
|
22
8
|
}
|
|
23
9
|
export interface TrustlessGatewayComponents {
|
|
24
10
|
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,0BAA0B,UAStC,CAAA;AAED,MAAM,MAAM,sCAAsC,GAChD,aAAa,CAAC,mCAAmC,EAAE,GAAG,CAAC,CAAA;AAEzD,MAAM,WAAW,+BAA+B;IAC9C,QAAQ,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,GAAG,CAAC,CAAA;CAC/B;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,6 +1,12 @@
|
|
|
1
1
|
import { TrustlessGatewayBlockBroker } from './broker.js';
|
|
2
|
-
export const
|
|
3
|
-
|
|
2
|
+
export const DEFAULT_TRUSTLESS_GATEWAYS = [
|
|
3
|
+
// 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/
|
|
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
|
+
];
|
|
4
10
|
export function trustlessGateway(init = {}) {
|
|
5
11
|
return (components) => new TrustlessGatewayBlockBroker(components, init);
|
|
6
12
|
}
|
|
@@ -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,0BAA0B,GAAG;IACxC,wGAAwG;IACxG,gCAAgC;IAEhC,wGAAwG;IACxG,6BAA6B;IAE7B,wGAAwG;IACxG,sBAAsB;CACvB,CAAA;AAcD,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';
|
|
2
3
|
import type { CreateTrustlessGatewaySessionOptions } from './broker.js';
|
|
3
4
|
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,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../../src/trustless-gateway/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAI9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AACzD,OAAO,KAAK,EAAE,oCAAoC,EAAE,MAAM,aAAa,CAAA;AACvE,OAAO,KAAK,EAAE,sCAAsC,EAAE,MAAM,YAAY,CAAA;AACxE,OAAO,KAAK,EAAE,qBAAqB,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AACtE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAExD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AACnD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAK3C,MAAM,WAAW,iCAAiC;IAChD,MAAM,EAAE,eAAe,CAAA;IACvB,OAAO,EAAE,OAAO,CAAA;CACjB;AAED,cAAM,uBAAwB,SAAQ,eAAe,CAAC,gBAAgB,EAAE,sCAAsC,CAAC;IAC7G,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IACvC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;gBAEvB,UAAU,EAAE,iCAAiC,EAAE,IAAI,EAAE,oCAAoC;IAWhG,aAAa,CAAE,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,gBAAgB,EAAE,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,UAAU,CAAC;IAWvG,gBAAgB,CAAE,GAAG,EAAE,GAAG,EAAE,OAAO,GAAE,YAAiB,GAAG,cAAc,CAAC,gBAAgB,CAAC;IAoBjG,aAAa,CAAE,QAAQ,EAAE,gBAAgB,GAAG,UAAU,GAAG,MAAM;IAI/D,MAAM,CAAE,SAAS,EAAE,gBAAgB,EAAE,SAAS,EAAE,gBAAgB,GAAG,OAAO;CAG3E;AAoBD,wBAAgB,6BAA6B,CAAE,UAAU,EAAE,iCAAiC,EAAE,IAAI,EAAE,oCAAoC,GAAG,uBAAuB,CAEjK"}
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { AbstractSession } from '@helia/utils';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { isPrivateIp } from '@libp2p/utils/private-ip';
|
|
3
|
+
import { DNS, HTTP, HTTPS } from '@multiformats/multiaddr-matcher';
|
|
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;
|
|
4
8
|
class TrustlessGatewaySession extends AbstractSession {
|
|
5
9
|
routing;
|
|
6
10
|
allowInsecure;
|
|
@@ -22,7 +26,20 @@ class TrustlessGatewaySession extends AbstractSession {
|
|
|
22
26
|
return block;
|
|
23
27
|
}
|
|
24
28
|
async *findNewProviders(cid, options = {}) {
|
|
25
|
-
|
|
29
|
+
for await (const provider of this.routing.findProviders(cid, options)) {
|
|
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
|
+
}
|
|
26
43
|
}
|
|
27
44
|
toEvictionKey(provider) {
|
|
28
45
|
return provider.url.toString();
|
|
@@ -31,6 +48,20 @@ class TrustlessGatewaySession extends AbstractSession {
|
|
|
31
48
|
return providerA.url.toString() === providerB.url.toString();
|
|
32
49
|
}
|
|
33
50
|
}
|
|
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
|
+
}
|
|
34
65
|
export function createTrustlessGatewaySession(components, init) {
|
|
35
66
|
return new TrustlessGatewaySession(components, init);
|
|
36
67
|
}
|
|
@@ -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":["../../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E,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,UAAU,6BAA6B,CAAE,UAA6C,EAAE,IAA0C;IACtI,OAAO,IAAI,uBAAuB,CAAC,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;IAgDvE;;;;;;;OAOG;IACH,WAAW,IAAK,MAAM;IAyBtB;;OAEG;IACH,sBAAsB,IAAK,IAAI;IAI/B,QAAQ,IAAK,qBAAqB;CASnC"}
|
|
@@ -69,18 +69,12 @@ 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);
|
|
78
72
|
try {
|
|
79
73
|
let pendingResponse = this.#pendingResponses.get(blockId);
|
|
80
74
|
if (pendingResponse == null) {
|
|
81
75
|
this.#attempts++;
|
|
82
76
|
pendingResponse = fetch(gwUrl.toString(), {
|
|
83
|
-
signal
|
|
77
|
+
signal,
|
|
84
78
|
headers: {
|
|
85
79
|
Accept: 'application/vnd.ipld.raw'
|
|
86
80
|
},
|
|
@@ -108,7 +102,6 @@ export class TrustlessGateway {
|
|
|
108
102
|
throw new Error(`unable to fetch raw block for CID ${cid}`);
|
|
109
103
|
}
|
|
110
104
|
finally {
|
|
111
|
-
signal?.removeEventListener('abort', abortInnerSignal);
|
|
112
105
|
this.#pendingResponses.delete(blockId);
|
|
113
106
|
}
|
|
114
107
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trustless-gateway.js","sourceRoot":"","sources":["../../../src/trustless-gateway/trustless-gateway.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAYlD;;;;;GAKG;AACH,MAAM,OAAO,gBAAgB;IACX,GAAG,CAAK;IACxB;;;;;OAKG;IACH,SAAS,GAAG,CAAC,CAAA;IAEb;;;;;OAKG;IACH,OAAO,GAAG,CAAC,CAAA;IAEX;;;;OAIG;IACH,cAAc,GAAG,CAAC,CAAA;IAElB;;OAEG;IACH,UAAU,GAAG,CAAC,CAAA;IAEd;;;;OAIG;IACH,iBAAiB,GAAG,IAAI,GAAG,EAA+B,CAAA;IAEzC,GAAG,CAAQ;IAE5B,YAAa,GAAiB,EAAE,MAAuB;QACrD,IAAI,CAAC,GAAG,GAAG,GAAG,YAAY,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAA;QAClD,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,YAAY,CAAC,wCAAwC,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAA;IAC7F,CAAC;IAED;;;;;;;;OAQG;IACH,cAAc,CAAE,GAAQ;QACtB,MAAM,cAAc,GAAG,GAAG,CAAC,SAAS,CAAC,KAAK,CAAA;QAC1C,OAAO,MAAM,C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,SAAS,GAAG,CAAC,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,CAAA;IAChE,CAAC;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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helia/block-brokers",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.3",
|
|
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,8 +12,7 @@
|
|
|
12
12
|
"url": "https://github.com/ipfs/helia/issues"
|
|
13
13
|
},
|
|
14
14
|
"publishConfig": {
|
|
15
|
-
"access": "public"
|
|
16
|
-
"provenance": true
|
|
15
|
+
"access": "public"
|
|
17
16
|
},
|
|
18
17
|
"keywords": [
|
|
19
18
|
"IPFS"
|
|
@@ -53,9 +52,9 @@
|
|
|
53
52
|
"test:electron-main": "aegir test -t electron-main"
|
|
54
53
|
},
|
|
55
54
|
"dependencies": {
|
|
56
|
-
"@helia/bitswap": "^1.1
|
|
57
|
-
"@helia/interface": "^4.
|
|
58
|
-
"@helia/utils": "^0.
|
|
55
|
+
"@helia/bitswap": "^1.0.1",
|
|
56
|
+
"@helia/interface": "^4.2.0",
|
|
57
|
+
"@helia/utils": "^0.2.0",
|
|
59
58
|
"@libp2p/interface": "^1.1.4",
|
|
60
59
|
"@libp2p/utils": "^5.2.6",
|
|
61
60
|
"@multiformats/multiaddr": "^12.2.1",
|
|
@@ -76,7 +75,6 @@
|
|
|
76
75
|
"cors": "^2.8.5",
|
|
77
76
|
"polka": "^0.5.2",
|
|
78
77
|
"sinon": "^17.0.1",
|
|
79
|
-
"sinon-ts": "^2.0.0"
|
|
80
|
-
"uint8arrays": "^5.0.3"
|
|
78
|
+
"sinon-ts": "^2.0.0"
|
|
81
79
|
}
|
|
82
80
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createTrustlessGatewaySession } from './session.js'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { TrustlessGateway } from './trustless-gateway.js'
|
|
3
|
+
import { DEFAULT_TRUSTLESS_GATEWAYS } 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,48 +29,55 @@ 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 components: TrustlessGatewayComponents
|
|
33
|
+
private readonly gateways: TrustlessGateway[]
|
|
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
|
|
39
40
|
this.log = components.logger.forComponent('helia:trustless-gateway-block-broker')
|
|
40
41
|
this.logger = components.logger
|
|
41
42
|
this.routing = components.routing
|
|
42
|
-
this.
|
|
43
|
-
|
|
43
|
+
this.gateways = (init.gateways ?? DEFAULT_TRUSTLESS_GATEWAYS)
|
|
44
|
+
.map((gatewayOrUrl) => {
|
|
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))
|
|
44
51
|
}
|
|
45
52
|
|
|
46
53
|
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())
|
|
47
57
|
const aggregateErrors: Error[] = []
|
|
48
58
|
|
|
49
|
-
for
|
|
59
|
+
for (const gateway of sortedGateways) {
|
|
50
60
|
this.log('getting block for %c from %s', cid, gateway.url)
|
|
51
|
-
|
|
52
61
|
try {
|
|
53
62
|
const block = await gateway.getRawBlock(cid, options.signal)
|
|
54
63
|
this.log.trace('got block for %c from %s', cid, gateway.url)
|
|
55
|
-
|
|
56
64
|
try {
|
|
57
65
|
await options.validateFn?.(block)
|
|
58
66
|
} catch (err) {
|
|
59
67
|
this.log.error('failed to validate block for %c from %s', cid, gateway.url, err)
|
|
60
|
-
|
|
61
|
-
|
|
68
|
+
gateway.incrementInvalidBlocks()
|
|
69
|
+
|
|
70
|
+
throw new Error(`Block for CID ${cid} from gateway ${gateway.url} failed validation`)
|
|
62
71
|
}
|
|
63
72
|
|
|
64
73
|
return block
|
|
65
74
|
} catch (err: unknown) {
|
|
66
75
|
this.log.error('failed to get block for %c from %s', cid, gateway.url, err)
|
|
67
|
-
|
|
68
76
|
if (err instanceof Error) {
|
|
69
77
|
aggregateErrors.push(err)
|
|
70
78
|
} else {
|
|
71
79
|
aggregateErrors.push(new Error(`Unable to fetch raw block for CID ${cid} from gateway ${gateway.url}`))
|
|
72
80
|
}
|
|
73
|
-
|
|
74
81
|
// if signal was aborted, exit the loop
|
|
75
82
|
if (options.signal?.aborted === true) {
|
|
76
83
|
this.log.trace('request aborted while fetching raw block for CID %c from gateway %s', cid, gateway.url)
|
|
@@ -3,28 +3,22 @@ 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
|
-
|
|
6
|
+
export const DEFAULT_TRUSTLESS_GATEWAYS = [
|
|
7
|
+
// 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/
|
|
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
|
+
]
|
|
8
16
|
|
|
9
17
|
export type TrustlessGatewayGetBlockProgressEvents =
|
|
10
18
|
ProgressEvent<'trustless-gateway:get-block:fetch', URL>
|
|
11
19
|
|
|
12
20
|
export interface TrustlessGatewayBlockBrokerInit {
|
|
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
|
|
21
|
+
gateways?: Array<string | URL>
|
|
28
22
|
}
|
|
29
23
|
|
|
30
24
|
export interface TrustlessGatewayComponents {
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { AbstractSession } from '@helia/utils'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { isPrivateIp } from '@libp2p/utils/private-ip'
|
|
3
|
+
import { DNS, HTTP, HTTPS } from '@multiformats/multiaddr-matcher'
|
|
4
|
+
import { multiaddrToUri } from '@multiformats/multiaddr-to-uri'
|
|
5
|
+
import { TrustlessGateway } from './trustless-gateway.js'
|
|
4
6
|
import type { CreateTrustlessGatewaySessionOptions } from './broker.js'
|
|
5
7
|
import type { TrustlessGatewayGetBlockProgressEvents } from './index.js'
|
|
6
|
-
import type { TrustlessGateway } from './trustless-gateway.js'
|
|
7
8
|
import type { BlockRetrievalOptions, Routing } from '@helia/interface'
|
|
8
9
|
import type { ComponentLogger } from '@libp2p/interface'
|
|
10
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
9
11
|
import type { AbortOptions } from 'interface-store'
|
|
10
12
|
import type { CID } from 'multiformats/cid'
|
|
11
13
|
|
|
14
|
+
const DEFAULT_ALLOW_INSECURE = false
|
|
15
|
+
const DEFAULT_ALLOW_LOCAL = false
|
|
16
|
+
|
|
12
17
|
export interface TrustlessGatewaySessionComponents {
|
|
13
18
|
logger: ComponentLogger
|
|
14
19
|
routing: Routing
|
|
@@ -42,7 +47,23 @@ class TrustlessGatewaySession extends AbstractSession<TrustlessGateway, Trustles
|
|
|
42
47
|
}
|
|
43
48
|
|
|
44
49
|
async * findNewProviders (cid: CID, options: AbortOptions = {}): AsyncGenerator<TrustlessGateway> {
|
|
45
|
-
|
|
50
|
+
for await (const provider of this.routing.findProviders(cid, options)) {
|
|
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
|
+
}
|
|
46
67
|
}
|
|
47
68
|
|
|
48
69
|
toEvictionKey (provider: TrustlessGateway): Uint8Array | string {
|
|
@@ -54,6 +75,24 @@ class TrustlessGatewaySession extends AbstractSession<TrustlessGateway, Trustles
|
|
|
54
75
|
}
|
|
55
76
|
}
|
|
56
77
|
|
|
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
|
+
|
|
57
96
|
export function createTrustlessGatewaySession (components: TrustlessGatewaySessionComponents, init: CreateTrustlessGatewaySessionOptions): TrustlessGatewaySession {
|
|
58
97
|
return new TrustlessGatewaySession(components, init)
|
|
59
98
|
}
|
|
@@ -91,20 +91,12 @@ 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
|
-
|
|
102
94
|
try {
|
|
103
95
|
let pendingResponse: Promise<Uint8Array> | undefined = this.#pendingResponses.get(blockId)
|
|
104
96
|
if (pendingResponse == null) {
|
|
105
97
|
this.#attempts++
|
|
106
98
|
pendingResponse = fetch(gwUrl.toString(), {
|
|
107
|
-
signal
|
|
99
|
+
signal,
|
|
108
100
|
headers: {
|
|
109
101
|
Accept: 'application/vnd.ipld.raw'
|
|
110
102
|
},
|
|
@@ -130,7 +122,6 @@ export class TrustlessGateway {
|
|
|
130
122
|
this.#errors++
|
|
131
123
|
throw new Error(`unable to fetch raw block for CID ${cid}`)
|
|
132
124
|
} finally {
|
|
133
|
-
signal?.removeEventListener('abort', abortInnerSignal)
|
|
134
125
|
this.#pendingResponses.delete(blockId)
|
|
135
126
|
}
|
|
136
127
|
}
|
|
@@ -1,8 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,34 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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,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