@helia/verified-fetch 0.0.0-8a5bc6f
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/LICENSE +4 -0
- package/README.md +262 -0
- package/dist/index.min.js +140 -0
- package/dist/src/index.d.ts +259 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +251 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/utils/get-content-type.d.ts +11 -0
- package/dist/src/utils/get-content-type.d.ts.map +1 -0
- package/dist/src/utils/get-content-type.js +43 -0
- package/dist/src/utils/get-content-type.js.map +1 -0
- package/dist/src/utils/get-stream-and-content-type.d.ts +10 -0
- package/dist/src/utils/get-stream-and-content-type.d.ts.map +1 -0
- package/dist/src/utils/get-stream-and-content-type.js +37 -0
- package/dist/src/utils/get-stream-and-content-type.js.map +1 -0
- package/dist/src/utils/parse-resource.d.ts +18 -0
- package/dist/src/utils/parse-resource.d.ts.map +1 -0
- package/dist/src/utils/parse-resource.js +24 -0
- package/dist/src/utils/parse-resource.js.map +1 -0
- package/dist/src/utils/parse-url-string.d.ts +26 -0
- package/dist/src/utils/parse-url-string.d.ts.map +1 -0
- package/dist/src/utils/parse-url-string.js +109 -0
- package/dist/src/utils/parse-url-string.js.map +1 -0
- package/dist/src/utils/tlru.d.ts +15 -0
- package/dist/src/utils/tlru.d.ts.map +1 -0
- package/dist/src/utils/tlru.js +40 -0
- package/dist/src/utils/tlru.js.map +1 -0
- package/dist/src/utils/walk-path.d.ts +13 -0
- package/dist/src/utils/walk-path.d.ts.map +1 -0
- package/dist/src/utils/walk-path.js +17 -0
- package/dist/src/utils/walk-path.js.map +1 -0
- package/dist/src/verified-fetch.d.ts +64 -0
- package/dist/src/verified-fetch.d.ts.map +1 -0
- package/dist/src/verified-fetch.js +261 -0
- package/dist/src/verified-fetch.js.map +1 -0
- package/package.json +176 -0
- package/src/index.ts +310 -0
- package/src/utils/get-content-type.ts +55 -0
- package/src/utils/get-stream-and-content-type.ts +44 -0
- package/src/utils/parse-resource.ts +40 -0
- package/src/utils/parse-url-string.ts +139 -0
- package/src/utils/tlru.ts +52 -0
- package/src/utils/walk-path.ts +34 -0
- package/src/verified-fetch.ts +323 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ParsedUrlStringResults } from './parse-url-string.js';
|
|
2
|
+
import type { Resource } from '../index.js';
|
|
3
|
+
import type { IPNS, IPNSRoutingEvents, ResolveDnsLinkProgressEvents, ResolveProgressEvents } from '@helia/ipns';
|
|
4
|
+
import type { ComponentLogger } from '@libp2p/interface';
|
|
5
|
+
import type { ProgressOptions } from 'progress-events';
|
|
6
|
+
export interface ParseResourceComponents {
|
|
7
|
+
ipns: IPNS;
|
|
8
|
+
logger: ComponentLogger;
|
|
9
|
+
}
|
|
10
|
+
export interface ParseResourceOptions extends ProgressOptions<ResolveProgressEvents | IPNSRoutingEvents | ResolveDnsLinkProgressEvents> {
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Handles the different use cases for the `resource` argument.
|
|
14
|
+
* The resource can represent an IPFS path, IPNS path, or CID.
|
|
15
|
+
* If the resource represents an IPNS path, we need to resolve it to a CID.
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseResource(resource: Resource, { ipns, logger }: ParseResourceComponents, options?: ParseResourceOptions): Promise<ParsedUrlStringResults>;
|
|
18
|
+
//# sourceMappingURL=parse-resource.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-resource.d.ts","sourceRoot":"","sources":["../../../src/utils/parse-resource.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAA;AACnE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,KAAK,EAAE,IAAI,EAAE,iBAAiB,EAAE,4BAA4B,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AAC/G,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACxD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEtD,MAAM,WAAW,uBAAuB;IACtC,IAAI,EAAE,IAAI,CAAA;IACV,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,MAAM,WAAW,oBAAqB,SAAQ,eAAe,CAAC,qBAAqB,GAAG,iBAAiB,GAAG,4BAA4B,CAAC;CAEtI;AACD;;;;GAIG;AACH,wBAAsB,aAAa,CAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,uBAAuB,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAkBnK"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { CID } from 'multiformats/cid';
|
|
2
|
+
import { parseUrlString } from './parse-url-string.js';
|
|
3
|
+
/**
|
|
4
|
+
* Handles the different use cases for the `resource` argument.
|
|
5
|
+
* The resource can represent an IPFS path, IPNS path, or CID.
|
|
6
|
+
* If the resource represents an IPNS path, we need to resolve it to a CID.
|
|
7
|
+
*/
|
|
8
|
+
export async function parseResource(resource, { ipns, logger }, options) {
|
|
9
|
+
if (typeof resource === 'string') {
|
|
10
|
+
return parseUrlString({ urlString: resource, ipns, logger }, { onProgress: options?.onProgress });
|
|
11
|
+
}
|
|
12
|
+
const cid = CID.asCID(resource);
|
|
13
|
+
if (cid != null) {
|
|
14
|
+
// an actual CID
|
|
15
|
+
return {
|
|
16
|
+
cid,
|
|
17
|
+
protocol: 'ipfs',
|
|
18
|
+
path: '',
|
|
19
|
+
query: {}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
throw new TypeError(`Invalid resource. Cannot determine CID from resource: ${resource}`);
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=parse-resource.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-resource.js","sourceRoot":"","sources":["../../../src/utils/parse-resource.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAetD;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAE,QAAkB,EAAE,EAAE,IAAI,EAAE,MAAM,EAA2B,EAAE,OAA8B;IAChI,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;QACjC,OAAO,cAAc,CAAC,EAAE,SAAS,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC,CAAA;IACnG,CAAC;IAED,MAAM,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;IAE/B,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC;QAChB,gBAAgB;QAChB,OAAO;YACL,GAAG;YACH,QAAQ,EAAE,MAAM;YAChB,IAAI,EAAE,EAAE;YACR,KAAK,EAAE,EAAE;SACV,CAAA;IACH,CAAC;IAED,MAAM,IAAI,SAAS,CAAC,yDAAyD,QAAQ,EAAE,CAAC,CAAA;AAC1F,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { CID } from 'multiformats/cid';
|
|
2
|
+
import type { IPNS, IPNSRoutingEvents, ResolveDnsLinkProgressEvents, ResolveProgressEvents } from '@helia/ipns';
|
|
3
|
+
import type { ComponentLogger } from '@libp2p/interface';
|
|
4
|
+
import type { ProgressOptions } from 'progress-events';
|
|
5
|
+
export interface ParseUrlStringInput {
|
|
6
|
+
urlString: string;
|
|
7
|
+
ipns: IPNS;
|
|
8
|
+
logger: ComponentLogger;
|
|
9
|
+
}
|
|
10
|
+
export interface ParseUrlStringOptions extends ProgressOptions<ResolveProgressEvents | IPNSRoutingEvents | ResolveDnsLinkProgressEvents> {
|
|
11
|
+
}
|
|
12
|
+
export interface ParsedUrlStringResults {
|
|
13
|
+
protocol: string;
|
|
14
|
+
path: string;
|
|
15
|
+
cid: CID;
|
|
16
|
+
query: Record<string, string>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A function that parses ipfs:// and ipns:// URLs, returning an object with easily recognizable properties.
|
|
20
|
+
*
|
|
21
|
+
* After determining the protocol successfully, we process the cidOrPeerIdOrDnsLink:
|
|
22
|
+
* * If it's ipfs, it parses the CID or throws an Aggregate error
|
|
23
|
+
* * If it's ipns, it attempts to resolve the PeerId and then the DNSLink. If both fail, an Aggregate error is thrown.
|
|
24
|
+
*/
|
|
25
|
+
export declare function parseUrlString({ urlString, ipns, logger }: ParseUrlStringInput, options?: ParseUrlStringOptions): Promise<ParsedUrlStringResults>;
|
|
26
|
+
//# sourceMappingURL=parse-url-string.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-url-string.d.ts","sourceRoot":"","sources":["../../../src/utils/parse-url-string.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAEtC,OAAO,KAAK,EAAE,IAAI,EAAE,iBAAiB,EAAE,4BAA4B,EAAE,qBAAqB,EAAiB,MAAM,aAAa,CAAA;AAC9H,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACxD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAItD,MAAM,WAAW,mBAAmB;IAClC,SAAS,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,IAAI,CAAA;IACV,MAAM,EAAE,eAAe,CAAA;CACxB;AACD,MAAM,WAAW,qBAAsB,SAAQ,eAAe,CAAC,qBAAqB,GAAG,iBAAiB,GAAG,4BAA4B,CAAC;CAEvI;AAED,MAAM,WAAW,sBAAsB;IACrC,QAAQ,EAAE,MAAM,CAAA;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,GAAG,CAAA;IACR,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAC9B;AAID;;;;;;GAMG;AACH,wBAAsB,cAAc,CAAE,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,mBAAmB,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAwGxJ"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { peerIdFromString } from '@libp2p/peer-id';
|
|
2
|
+
import { CID } from 'multiformats/cid';
|
|
3
|
+
import { TLRU } from './tlru.js';
|
|
4
|
+
const ipnsCache = new TLRU(1000);
|
|
5
|
+
const URL_REGEX = /^(?<protocol>ip[fn]s):\/\/(?<cidOrPeerIdOrDnsLink>[^/$?]+)\/?(?<path>[^$?]*)\??(?<queryString>.*)$/;
|
|
6
|
+
/**
|
|
7
|
+
* A function that parses ipfs:// and ipns:// URLs, returning an object with easily recognizable properties.
|
|
8
|
+
*
|
|
9
|
+
* After determining the protocol successfully, we process the cidOrPeerIdOrDnsLink:
|
|
10
|
+
* * If it's ipfs, it parses the CID or throws an Aggregate error
|
|
11
|
+
* * If it's ipns, it attempts to resolve the PeerId and then the DNSLink. If both fail, an Aggregate error is thrown.
|
|
12
|
+
*/
|
|
13
|
+
export async function parseUrlString({ urlString, ipns, logger }, options) {
|
|
14
|
+
const log = logger.forComponent('helia:verified-fetch:parse-url-string');
|
|
15
|
+
const match = urlString.match(URL_REGEX);
|
|
16
|
+
if (match == null || match.groups == null) {
|
|
17
|
+
throw new TypeError(`Invalid URL: ${urlString}, please use ipfs:// or ipns:// URLs only.`);
|
|
18
|
+
}
|
|
19
|
+
const { protocol, cidOrPeerIdOrDnsLink, path: urlPath, queryString } = match.groups;
|
|
20
|
+
let cid;
|
|
21
|
+
let resolvedPath;
|
|
22
|
+
const errors = [];
|
|
23
|
+
if (protocol === 'ipfs') {
|
|
24
|
+
try {
|
|
25
|
+
cid = CID.parse(cidOrPeerIdOrDnsLink);
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
log.error(err);
|
|
29
|
+
errors.push(new TypeError('Invalid CID for ipfs://<cid> URL'));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
let resolveResult = ipnsCache.get(cidOrPeerIdOrDnsLink);
|
|
34
|
+
if (resolveResult != null) {
|
|
35
|
+
cid = resolveResult.cid;
|
|
36
|
+
resolvedPath = resolveResult.path;
|
|
37
|
+
log.trace('resolved %s to %c from cache', cidOrPeerIdOrDnsLink, cid);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
// protocol is ipns
|
|
41
|
+
log.trace('Attempting to resolve PeerId for %s', cidOrPeerIdOrDnsLink);
|
|
42
|
+
let peerId = null;
|
|
43
|
+
try {
|
|
44
|
+
peerId = peerIdFromString(cidOrPeerIdOrDnsLink);
|
|
45
|
+
resolveResult = await ipns.resolve(peerId, { onProgress: options?.onProgress });
|
|
46
|
+
cid = resolveResult?.cid;
|
|
47
|
+
resolvedPath = resolveResult?.path;
|
|
48
|
+
log.trace('resolved %s to %c', cidOrPeerIdOrDnsLink, cid);
|
|
49
|
+
ipnsCache.set(cidOrPeerIdOrDnsLink, resolveResult, 60 * 1000 * 2);
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
if (peerId == null) {
|
|
53
|
+
log.error('Could not parse PeerId string "%s"', cidOrPeerIdOrDnsLink, err);
|
|
54
|
+
errors.push(new TypeError(`Could not parse PeerId in ipns url "${cidOrPeerIdOrDnsLink}", ${err.message}`));
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
log.error('Could not resolve PeerId %c', peerId, err);
|
|
58
|
+
errors.push(new TypeError(`Could not resolve PeerId "${cidOrPeerIdOrDnsLink}", ${err.message}`));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (cid == null) {
|
|
62
|
+
log.trace('Attempting to resolve DNSLink for %s', cidOrPeerIdOrDnsLink);
|
|
63
|
+
try {
|
|
64
|
+
resolveResult = await ipns.resolveDns(cidOrPeerIdOrDnsLink, { onProgress: options?.onProgress });
|
|
65
|
+
cid = resolveResult?.cid;
|
|
66
|
+
resolvedPath = resolveResult?.path;
|
|
67
|
+
log.trace('resolved %s to %c', cidOrPeerIdOrDnsLink, cid);
|
|
68
|
+
ipnsCache.set(cidOrPeerIdOrDnsLink, resolveResult, 60 * 1000 * 2);
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
log.error('Could not resolve DnsLink for "%s"', cidOrPeerIdOrDnsLink, err);
|
|
72
|
+
errors.push(err);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (cid == null) {
|
|
78
|
+
throw new AggregateError(errors, `Invalid resource. Cannot determine CID from URL "${urlString}"`);
|
|
79
|
+
}
|
|
80
|
+
// parse query string
|
|
81
|
+
const query = {};
|
|
82
|
+
if (queryString != null && queryString.length > 0) {
|
|
83
|
+
const queryParts = queryString.split('&');
|
|
84
|
+
for (const part of queryParts) {
|
|
85
|
+
const [key, value] = part.split('=');
|
|
86
|
+
query[key] = decodeURIComponent(value);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* join the path from resolve result & given path.
|
|
91
|
+
* e.g. /ipns/<peerId>/ that is resolved to /ipfs/<cid>/<path1>, when requested as /ipns/<peerId>/<path2>, should be
|
|
92
|
+
* resolved to /ipfs/<cid>/<path1>/<path2>
|
|
93
|
+
*/
|
|
94
|
+
const pathParts = [];
|
|
95
|
+
if (urlPath.length > 0) {
|
|
96
|
+
pathParts.push(urlPath);
|
|
97
|
+
}
|
|
98
|
+
if (resolvedPath != null && resolvedPath.length > 0) {
|
|
99
|
+
pathParts.push(resolvedPath);
|
|
100
|
+
}
|
|
101
|
+
const path = pathParts.join('/');
|
|
102
|
+
return {
|
|
103
|
+
protocol,
|
|
104
|
+
cid,
|
|
105
|
+
path,
|
|
106
|
+
query
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=parse-url-string.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-url-string.js","sourceRoot":"","sources":["../../../src/utils/parse-url-string.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAKhC,MAAM,SAAS,GAAG,IAAI,IAAI,CAAgB,IAAI,CAAC,CAAA;AAkB/C,MAAM,SAAS,GAAG,oGAAoG,CAAA;AAEtH;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAE,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAuB,EAAE,OAA+B;IACrH,MAAM,GAAG,GAAG,MAAM,CAAC,YAAY,CAAC,uCAAuC,CAAC,CAAA;IACxE,MAAM,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;IAExC,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,CAAC,MAAM,IAAI,IAAI,EAAE,CAAC;QAC1C,MAAM,IAAI,SAAS,CAAC,gBAAgB,SAAS,4CAA4C,CAAC,CAAA;IAC5F,CAAC;IAED,MAAM,EAAE,QAAQ,EAAE,oBAAoB,EAAE,IAAI,EAAE,OAAO,EAAE,WAAW,EAAE,GAAG,KAAK,CAAC,MAAM,CAAA;IAEnF,IAAI,GAAoB,CAAA;IACxB,IAAI,YAAgC,CAAA;IACpC,MAAM,MAAM,GAAY,EAAE,CAAA;IAE1B,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxB,IAAI,CAAC;YACH,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAA;QACvC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,GAAG,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,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAClD,MAAM,UAAU,GAAG,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACzC,KAAK,MAAM,IAAI,IAAI,UAAU,EAAE,CAAC;YAC9B,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YACpC,KAAK,CAAC,GAAG,CAAC,GAAG,kBAAkB,CAAC,KAAK,CAAC,CAAA;QACxC,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,MAAM,SAAS,GAAG,EAAE,CAAA;IAEpB,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACvB,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IACzB,CAAC;IAED,IAAI,YAAY,IAAI,IAAI,IAAI,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACpD,SAAS,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;IAC9B,CAAC;IACD,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;IAEhC,OAAO;QACL,QAAQ;QACR,GAAG;QACH,IAAI;QACJ,KAAK;KACN,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Time Aware Least Recent Used Cache
|
|
3
|
+
*
|
|
4
|
+
* @see https://arxiv.org/pdf/1801.00390
|
|
5
|
+
*/
|
|
6
|
+
export declare class TLRU<T> {
|
|
7
|
+
private readonly lru;
|
|
8
|
+
constructor(maxSize: number);
|
|
9
|
+
get(key: string): T | undefined;
|
|
10
|
+
set(key: string, value: T, ttl: number): void;
|
|
11
|
+
has(key: string): boolean;
|
|
12
|
+
remove(key: string): void;
|
|
13
|
+
clear(): void;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=tlru.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tlru.d.ts","sourceRoot":"","sources":["../../../src/utils/tlru.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,qBAAa,IAAI,CAAC,CAAC;IACjB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAA4B;gBAEnC,OAAO,EAAE,MAAM;IAI5B,GAAG,CAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS;IAgBhC,GAAG,CAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI;IAI9C,GAAG,CAAE,GAAG,EAAE,MAAM,GAAG,OAAO;IAU1B,MAAM,CAAE,GAAG,EAAE,MAAM,GAAG,IAAI;IAI1B,KAAK,IAAK,IAAI;CAGf"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import hashlru from 'hashlru';
|
|
2
|
+
/**
|
|
3
|
+
* Time Aware Least Recent Used Cache
|
|
4
|
+
*
|
|
5
|
+
* @see https://arxiv.org/pdf/1801.00390
|
|
6
|
+
*/
|
|
7
|
+
export class TLRU {
|
|
8
|
+
lru;
|
|
9
|
+
constructor(maxSize) {
|
|
10
|
+
this.lru = hashlru(maxSize);
|
|
11
|
+
}
|
|
12
|
+
get(key) {
|
|
13
|
+
const value = this.lru.get(key);
|
|
14
|
+
if (value != null) {
|
|
15
|
+
if (value.expire != null && value.expire < Date.now()) {
|
|
16
|
+
this.lru.remove(key);
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
return value.value;
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
set(key, value, ttl) {
|
|
24
|
+
this.lru.set(key, { value, expire: Date.now() + ttl });
|
|
25
|
+
}
|
|
26
|
+
has(key) {
|
|
27
|
+
const value = this.get(key);
|
|
28
|
+
if (value != null) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
remove(key) {
|
|
34
|
+
this.lru.remove(key);
|
|
35
|
+
}
|
|
36
|
+
clear() {
|
|
37
|
+
this.lru.clear();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=tlru.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tlru.js","sourceRoot":"","sources":["../../../src/utils/tlru.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAA;AAE7B;;;;GAIG;AACH,MAAM,OAAO,IAAI;IACE,GAAG,CAA4B;IAEhD,YAAa,OAAe;QAC1B,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAC7B,CAAC;IAED,GAAG,CAAE,GAAW;QACd,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAE/B,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;YAClB,IAAI,KAAK,CAAC,MAAM,IAAI,IAAI,IAAI,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;gBACtD,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;gBAEpB,OAAO,SAAS,CAAA;YAClB,CAAC;YAED,OAAO,KAAK,CAAC,KAAK,CAAA;QACpB,CAAC;QAED,OAAO,SAAS,CAAA;IAClB,CAAC;IAED,GAAG,CAAE,GAAW,EAAE,KAAQ,EAAE,GAAW;QACrC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,EAAE,CAAC,CAAA;IACxD,CAAC;IAED,GAAG,CAAE,GAAW;QACd,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAE3B,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;YAClB,OAAO,IAAI,CAAA;QACb,CAAC;QAED,OAAO,KAAK,CAAA;IACd,CAAC;IAED,MAAM,CAAE,GAAW;QACjB,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IACtB,CAAC;IAED,KAAK;QACH,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAA;IAClB,CAAC;CACF"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type ExporterOptions, type ReadableStorage, type UnixFSEntry } from 'ipfs-unixfs-exporter';
|
|
2
|
+
import type { CID } from 'multiformats/cid';
|
|
3
|
+
export interface PathWalkerOptions extends ExporterOptions {
|
|
4
|
+
}
|
|
5
|
+
export interface PathWalkerResponse {
|
|
6
|
+
ipfsRoots: CID[];
|
|
7
|
+
terminalElement: UnixFSEntry;
|
|
8
|
+
}
|
|
9
|
+
export interface PathWalkerFn {
|
|
10
|
+
(blockstore: ReadableStorage, path: string, options?: PathWalkerOptions): Promise<PathWalkerResponse>;
|
|
11
|
+
}
|
|
12
|
+
export declare function walkPath(blockstore: ReadableStorage, path: string, options?: PathWalkerOptions): Promise<PathWalkerResponse>;
|
|
13
|
+
//# sourceMappingURL=walk-path.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"walk-path.d.ts","sourceRoot":"","sources":["../../../src/utils/walk-path.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4B,KAAK,eAAe,EAAE,KAAK,eAAe,EAAE,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAC7H,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAE3C,MAAM,WAAW,iBAAkB,SAAQ,eAAe;CAEzD;AACD,MAAM,WAAW,kBAAkB;IACjC,SAAS,EAAE,GAAG,EAAE,CAAA;IAChB,eAAe,EAAE,WAAW,CAAA;CAE7B;AAED,MAAM,WAAW,YAAY;IAC3B,CAAC,UAAU,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAA;CACtG;AAED,wBAAsB,QAAQ,CAAE,UAAU,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAiBnI"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { walkPath as exporterWalk } from 'ipfs-unixfs-exporter';
|
|
2
|
+
export async function walkPath(blockstore, path, options) {
|
|
3
|
+
const ipfsRoots = [];
|
|
4
|
+
let terminalElement;
|
|
5
|
+
for await (const entry of exporterWalk(path, blockstore, options)) {
|
|
6
|
+
ipfsRoots.push(entry.cid);
|
|
7
|
+
terminalElement = entry;
|
|
8
|
+
}
|
|
9
|
+
if (terminalElement == null) {
|
|
10
|
+
throw new Error('No terminal element found');
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
ipfsRoots,
|
|
14
|
+
terminalElement
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=walk-path.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"walk-path.js","sourceRoot":"","sources":["../../../src/utils/walk-path.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,YAAY,EAAgE,MAAM,sBAAsB,CAAA;AAgB7H,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAE,UAA2B,EAAE,IAAY,EAAE,OAA2B;IACpG,MAAM,SAAS,GAAU,EAAE,CAAA;IAC3B,IAAI,eAAwC,CAAA;IAE5C,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,YAAY,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,CAAC;QAClE,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACzB,eAAe,GAAG,KAAK,CAAA;IACzB,CAAC;IAED,IAAI,eAAe,IAAI,IAAI,EAAE,CAAC;QAC5B,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAA;IAC9C,CAAC;IAED,OAAO;QACL,SAAS;QACT,eAAe;KAChB,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { type DAGCBOR } from '@helia/dag-cbor';
|
|
2
|
+
import { type DAGJSON } from '@helia/dag-json';
|
|
3
|
+
import { type IPNS } from '@helia/ipns';
|
|
4
|
+
import { type JSON } from '@helia/json';
|
|
5
|
+
import { type UnixFS as HeliaUnixFs } from '@helia/unixfs';
|
|
6
|
+
import { type PathWalkerFn } from './utils/walk-path.js';
|
|
7
|
+
import type { Resource, VerifiedFetchInit as VerifiedFetchOptions } from './index.js';
|
|
8
|
+
import type { Helia } from '@helia/interface';
|
|
9
|
+
interface VerifiedFetchComponents {
|
|
10
|
+
helia: Helia;
|
|
11
|
+
ipns?: IPNS;
|
|
12
|
+
unixfs?: HeliaUnixFs;
|
|
13
|
+
dagJson?: DAGJSON;
|
|
14
|
+
json?: JSON;
|
|
15
|
+
dagCbor?: DAGCBOR;
|
|
16
|
+
pathWalker?: PathWalkerFn;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Potential future options for the VerifiedFetch constructor.
|
|
20
|
+
*/
|
|
21
|
+
interface VerifiedFetchInit {
|
|
22
|
+
}
|
|
23
|
+
export declare class VerifiedFetch {
|
|
24
|
+
private readonly helia;
|
|
25
|
+
private readonly ipns;
|
|
26
|
+
private readonly unixfs;
|
|
27
|
+
private readonly dagJson;
|
|
28
|
+
private readonly dagCbor;
|
|
29
|
+
private readonly json;
|
|
30
|
+
private readonly pathWalker;
|
|
31
|
+
private readonly log;
|
|
32
|
+
constructor({ helia, ipns, unixfs, dagJson, json, dagCbor, pathWalker }: VerifiedFetchComponents, init?: VerifiedFetchInit);
|
|
33
|
+
private handleIPNSRecord;
|
|
34
|
+
private handleIPLDCar;
|
|
35
|
+
private handleDagJson;
|
|
36
|
+
private handleJson;
|
|
37
|
+
private handleDagCbor;
|
|
38
|
+
private handleDagPb;
|
|
39
|
+
private handleRaw;
|
|
40
|
+
/**
|
|
41
|
+
* Determines the format requested by the client, defaults to `null` if no format is requested.
|
|
42
|
+
*
|
|
43
|
+
* @see https://specs.ipfs.tech/http-gateways/path-gateway/#format-request-query-parameter
|
|
44
|
+
* @default 'raw'
|
|
45
|
+
*/
|
|
46
|
+
private getFormat;
|
|
47
|
+
/**
|
|
48
|
+
* Map of format to specific handlers for that format.
|
|
49
|
+
* These format handlers should adjust the response headers as specified in https://specs.ipfs.tech/http-gateways/path-gateway/#response-headers
|
|
50
|
+
*/
|
|
51
|
+
private readonly formatHandlers;
|
|
52
|
+
private readonly codecHandlers;
|
|
53
|
+
fetch(resource: Resource, opts?: VerifiedFetchOptions): Promise<Response>;
|
|
54
|
+
/**
|
|
55
|
+
* Start the Helia instance
|
|
56
|
+
*/
|
|
57
|
+
start(): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Shut down the Helia instance
|
|
60
|
+
*/
|
|
61
|
+
stop(): Promise<void>;
|
|
62
|
+
}
|
|
63
|
+
export {};
|
|
64
|
+
//# sourceMappingURL=verified-fetch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verified-fetch.d.ts","sourceRoot":"","sources":["../../src/verified-fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2B,KAAK,OAAO,EAAE,MAAM,iBAAiB,CAAA;AACvE,OAAO,EAA2B,KAAK,OAAO,EAAE,MAAM,iBAAiB,CAAA;AACvE,OAAO,EAAqB,KAAK,IAAI,EAAE,MAAM,aAAa,CAAA;AAE1D,OAAO,EAAqB,KAAK,IAAI,EAAE,MAAM,aAAa,CAAA;AAC1D,OAAO,EAAyB,KAAK,MAAM,IAAI,WAAW,EAAoB,MAAM,eAAe,CAAA;AASnG,OAAO,EAAY,KAAK,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAClE,OAAO,KAAK,EAAa,QAAQ,EAAE,iBAAiB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AAG,OAAO,CAAC,QAAQ,CAAC;IAuDhF;;OAEG;IACG,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;IAI7B;;OAEG;IACG,IAAI,IAAK,OAAO,CAAC,IAAI,CAAC;CAG7B"}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { dagCbor as heliaDagCbor } from '@helia/dag-cbor';
|
|
2
|
+
import { dagJson as heliaDagJson } from '@helia/dag-json';
|
|
3
|
+
import { ipns as heliaIpns } from '@helia/ipns';
|
|
4
|
+
import { dnsJsonOverHttps } from '@helia/ipns/dns-resolvers';
|
|
5
|
+
import { json as heliaJson } from '@helia/json';
|
|
6
|
+
import { unixfs as heliaUnixFs } from '@helia/unixfs';
|
|
7
|
+
import { code as dagCborCode } from '@ipld/dag-cbor';
|
|
8
|
+
import { code as dagJsonCode } from '@ipld/dag-json';
|
|
9
|
+
import { code as dagPbCode } from '@ipld/dag-pb';
|
|
10
|
+
import { code as jsonCode } from 'multiformats/codecs/json';
|
|
11
|
+
import { decode, code as rawCode } from 'multiformats/codecs/raw';
|
|
12
|
+
import { CustomProgressEvent } from 'progress-events';
|
|
13
|
+
import { getStreamAndContentType } from './utils/get-stream-and-content-type.js';
|
|
14
|
+
import { parseResource } from './utils/parse-resource.js';
|
|
15
|
+
import { walkPath } from './utils/walk-path.js';
|
|
16
|
+
function convertOptions(options) {
|
|
17
|
+
if (options == null) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
let signal;
|
|
21
|
+
if (options?.signal === null) {
|
|
22
|
+
signal = undefined;
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
...options,
|
|
26
|
+
signal
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export class VerifiedFetch {
|
|
30
|
+
helia;
|
|
31
|
+
ipns;
|
|
32
|
+
unixfs;
|
|
33
|
+
dagJson;
|
|
34
|
+
dagCbor;
|
|
35
|
+
json;
|
|
36
|
+
pathWalker;
|
|
37
|
+
log;
|
|
38
|
+
constructor({ helia, ipns, unixfs, dagJson, json, dagCbor, pathWalker }, init) {
|
|
39
|
+
this.helia = helia;
|
|
40
|
+
this.log = helia.logger.forComponent('helia:verified-fetch');
|
|
41
|
+
this.ipns = ipns ?? heliaIpns(helia, {
|
|
42
|
+
resolvers: [
|
|
43
|
+
dnsJsonOverHttps('https://mozilla.cloudflare-dns.com/dns-query'),
|
|
44
|
+
dnsJsonOverHttps('https://dns.google/resolve')
|
|
45
|
+
]
|
|
46
|
+
});
|
|
47
|
+
this.unixfs = unixfs ?? heliaUnixFs(helia);
|
|
48
|
+
this.dagJson = dagJson ?? heliaDagJson(helia);
|
|
49
|
+
this.json = json ?? heliaJson(helia);
|
|
50
|
+
this.dagCbor = dagCbor ?? heliaDagCbor(helia);
|
|
51
|
+
this.pathWalker = pathWalker ?? walkPath;
|
|
52
|
+
this.log.trace('created VerifiedFetch instance');
|
|
53
|
+
}
|
|
54
|
+
// handle vnd.ipfs.ipns-record
|
|
55
|
+
async handleIPNSRecord({ cid, path, options }) {
|
|
56
|
+
const response = new Response('vnd.ipfs.ipns-record support is not implemented', { status: 501 });
|
|
57
|
+
response.headers.set('X-Content-Type-Options', 'nosniff'); // see https://specs.ipfs.tech/http-gateways/path-gateway/#x-content-type-options-response-header
|
|
58
|
+
return response;
|
|
59
|
+
}
|
|
60
|
+
// handle vnd.ipld.car
|
|
61
|
+
async handleIPLDCar({ cid, path, options }) {
|
|
62
|
+
const response = new Response('vnd.ipld.car support is not implemented', { status: 501 });
|
|
63
|
+
response.headers.set('X-Content-Type-Options', 'nosniff'); // see https://specs.ipfs.tech/http-gateways/path-gateway/#x-content-type-options-response-header
|
|
64
|
+
return response;
|
|
65
|
+
}
|
|
66
|
+
async handleDagJson({ cid, path, options }) {
|
|
67
|
+
this.log.trace('fetching %c/%s', cid, path);
|
|
68
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:start', { cid: cid.toString(), path }));
|
|
69
|
+
const result = await this.dagJson.get(cid, {
|
|
70
|
+
signal: options?.signal,
|
|
71
|
+
onProgress: options?.onProgress
|
|
72
|
+
});
|
|
73
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:end', { cid: cid.toString(), path }));
|
|
74
|
+
const response = new Response(JSON.stringify(result), { status: 200 });
|
|
75
|
+
response.headers.set('content-type', 'application/json');
|
|
76
|
+
return response;
|
|
77
|
+
}
|
|
78
|
+
async handleJson({ cid, path, options }) {
|
|
79
|
+
this.log.trace('fetching %c/%s', cid, path);
|
|
80
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:start', { cid: cid.toString(), path }));
|
|
81
|
+
const result = await this.json.get(cid, {
|
|
82
|
+
signal: options?.signal,
|
|
83
|
+
onProgress: options?.onProgress
|
|
84
|
+
});
|
|
85
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:end', { cid: cid.toString(), path }));
|
|
86
|
+
const response = new Response(JSON.stringify(result), { status: 200 });
|
|
87
|
+
response.headers.set('content-type', 'application/json');
|
|
88
|
+
return response;
|
|
89
|
+
}
|
|
90
|
+
async handleDagCbor({ cid, path, options }) {
|
|
91
|
+
this.log.trace('fetching %c/%s', cid, path);
|
|
92
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:start', { cid: cid.toString(), path }));
|
|
93
|
+
const result = await this.dagCbor.get(cid, {
|
|
94
|
+
signal: options?.signal,
|
|
95
|
+
onProgress: options?.onProgress
|
|
96
|
+
});
|
|
97
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:end', { cid: cid.toString(), path }));
|
|
98
|
+
const response = new Response(JSON.stringify(result), { status: 200 });
|
|
99
|
+
response.headers.set('content-type', 'application/json');
|
|
100
|
+
return response;
|
|
101
|
+
}
|
|
102
|
+
async handleDagPb({ cid, path, options, terminalElement }) {
|
|
103
|
+
this.log.trace('fetching %c/%s', cid, path);
|
|
104
|
+
let resolvedCID = terminalElement?.cid ?? cid;
|
|
105
|
+
let stat;
|
|
106
|
+
if (terminalElement?.type === 'directory') {
|
|
107
|
+
const dirCid = terminalElement.cid;
|
|
108
|
+
const rootFilePath = 'index.html';
|
|
109
|
+
try {
|
|
110
|
+
this.log.trace('found directory at %c/%s, looking for index.html', cid, path);
|
|
111
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:start', { cid: dirCid.toString(), path: rootFilePath }));
|
|
112
|
+
stat = await this.unixfs.stat(dirCid, {
|
|
113
|
+
path: rootFilePath,
|
|
114
|
+
signal: options?.signal,
|
|
115
|
+
onProgress: options?.onProgress
|
|
116
|
+
});
|
|
117
|
+
this.log.trace('found root file at %c/%s with cid %c', dirCid, rootFilePath, stat.cid);
|
|
118
|
+
path = rootFilePath;
|
|
119
|
+
resolvedCID = stat.cid;
|
|
120
|
+
// terminalElement = stat
|
|
121
|
+
}
|
|
122
|
+
catch (err) {
|
|
123
|
+
this.log('error loading path %c/%s', dirCid, rootFilePath, err);
|
|
124
|
+
return new Response('Unable to find index.html for directory at given path. Support for directories with implicit root is not implemented', { status: 501 });
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:end', { cid: dirCid.toString(), path: rootFilePath }));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:start', { cid: resolvedCID.toString(), path: '' }));
|
|
131
|
+
const asyncIter = this.unixfs.cat(resolvedCID, {
|
|
132
|
+
signal: options?.signal,
|
|
133
|
+
onProgress: options?.onProgress
|
|
134
|
+
});
|
|
135
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:end', { cid: resolvedCID.toString(), path: '' }));
|
|
136
|
+
this.log('got async iterator for %c/%s', cid, path);
|
|
137
|
+
const { contentType, stream } = await getStreamAndContentType(asyncIter, path ?? '', this.helia.logger, {
|
|
138
|
+
onProgress: options?.onProgress
|
|
139
|
+
});
|
|
140
|
+
const response = new Response(stream, { status: 200 });
|
|
141
|
+
response.headers.set('content-type', contentType);
|
|
142
|
+
return response;
|
|
143
|
+
}
|
|
144
|
+
async handleRaw({ cid, path, options }) {
|
|
145
|
+
this.log.trace('fetching %c/%s', cid, path);
|
|
146
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:start', { cid: cid.toString(), path }));
|
|
147
|
+
const result = await this.helia.blockstore.get(cid);
|
|
148
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:end', { cid: cid.toString(), path }));
|
|
149
|
+
const response = new Response(decode(result), { status: 200 });
|
|
150
|
+
response.headers.set('content-type', 'application/octet-stream');
|
|
151
|
+
return response;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Determines the format requested by the client, defaults to `null` if no format is requested.
|
|
155
|
+
*
|
|
156
|
+
* @see https://specs.ipfs.tech/http-gateways/path-gateway/#format-request-query-parameter
|
|
157
|
+
* @default 'raw'
|
|
158
|
+
*/
|
|
159
|
+
getFormat({ headerFormat, queryFormat }) {
|
|
160
|
+
const formatMap = {
|
|
161
|
+
'vnd.ipld.raw': 'raw',
|
|
162
|
+
'vnd.ipld.car': 'car',
|
|
163
|
+
'application/x-tar': 'tar',
|
|
164
|
+
'application/vnd.ipld.dag-json': 'dag-json',
|
|
165
|
+
'application/vnd.ipld.dag-cbor': 'dag-cbor',
|
|
166
|
+
'application/json': 'json',
|
|
167
|
+
'application/cbor': 'cbor',
|
|
168
|
+
'vnd.ipfs.ipns-record': 'ipns-record'
|
|
169
|
+
};
|
|
170
|
+
if (headerFormat != null) {
|
|
171
|
+
for (const format in formatMap) {
|
|
172
|
+
if (headerFormat.includes(format)) {
|
|
173
|
+
return formatMap[format];
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else if (queryFormat != null) {
|
|
178
|
+
return queryFormat;
|
|
179
|
+
}
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Map of format to specific handlers for that format.
|
|
184
|
+
* These format handlers should adjust the response headers as specified in https://specs.ipfs.tech/http-gateways/path-gateway/#response-headers
|
|
185
|
+
*/
|
|
186
|
+
formatHandlers = {
|
|
187
|
+
raw: async () => new Response('application/vnd.ipld.raw support is not implemented', { status: 501 }),
|
|
188
|
+
car: this.handleIPLDCar,
|
|
189
|
+
'ipns-record': this.handleIPNSRecord,
|
|
190
|
+
tar: async () => new Response('application/x-tar support is not implemented', { status: 501 }),
|
|
191
|
+
'dag-json': async () => new Response('application/vnd.ipld.dag-json support is not implemented', { status: 501 }),
|
|
192
|
+
'dag-cbor': async () => new Response('application/vnd.ipld.dag-cbor support is not implemented', { status: 501 }),
|
|
193
|
+
json: async () => new Response('application/json support is not implemented', { status: 501 }),
|
|
194
|
+
cbor: async () => new Response('application/cbor support is not implemented', { status: 501 })
|
|
195
|
+
};
|
|
196
|
+
codecHandlers = {
|
|
197
|
+
[dagJsonCode]: this.handleDagJson,
|
|
198
|
+
[dagPbCode]: this.handleDagPb,
|
|
199
|
+
[jsonCode]: this.handleJson,
|
|
200
|
+
[dagCborCode]: this.handleDagCbor,
|
|
201
|
+
[rawCode]: this.handleRaw
|
|
202
|
+
};
|
|
203
|
+
async fetch(resource, opts) {
|
|
204
|
+
const options = convertOptions(opts);
|
|
205
|
+
const { path, query, ...rest } = await parseResource(resource, { ipns: this.ipns, logger: this.helia.logger }, options);
|
|
206
|
+
const cid = rest.cid;
|
|
207
|
+
let response;
|
|
208
|
+
const format = this.getFormat({ headerFormat: new Headers(options?.headers).get('accept'), queryFormat: query.format ?? null });
|
|
209
|
+
if (format != null) {
|
|
210
|
+
// TODO: These should be handled last when they're returning something other than 501
|
|
211
|
+
const formatHandler = this.formatHandlers[format];
|
|
212
|
+
if (formatHandler != null) {
|
|
213
|
+
response = await formatHandler.call(this, { cid, path, options });
|
|
214
|
+
if (response.status === 501) {
|
|
215
|
+
return response;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
let terminalElement;
|
|
220
|
+
let ipfsRoots;
|
|
221
|
+
try {
|
|
222
|
+
const pathDetails = await this.pathWalker(this.helia.blockstore, `${cid.toString()}/${path}`, options);
|
|
223
|
+
ipfsRoots = pathDetails.ipfsRoots;
|
|
224
|
+
terminalElement = pathDetails.terminalElement;
|
|
225
|
+
}
|
|
226
|
+
catch (err) {
|
|
227
|
+
this.log.error('Error walking path %s', path, err);
|
|
228
|
+
// return new Response(`Error walking path: ${(err as Error).message}`, { status: 500 })
|
|
229
|
+
}
|
|
230
|
+
if (response == null) {
|
|
231
|
+
const codecHandler = this.codecHandlers[cid.code];
|
|
232
|
+
if (codecHandler != null) {
|
|
233
|
+
response = await codecHandler.call(this, { cid, path, options, terminalElement });
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
return new Response(`Support for codec with code ${cid.code} is not yet implemented. Please open an issue at https://github.com/ipfs/helia/issues/new`, { status: 501 });
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
response.headers.set('etag', cid.toString()); // https://specs.ipfs.tech/http-gateways/path-gateway/#etag-response-header
|
|
240
|
+
response.headers.set('cache-cotrol', 'public, max-age=29030400, immutable');
|
|
241
|
+
response.headers.set('X-Ipfs-Path', resource.toString()); // https://specs.ipfs.tech/http-gateways/path-gateway/#x-ipfs-path-response-header
|
|
242
|
+
if (ipfsRoots != null) {
|
|
243
|
+
response.headers.set('X-Ipfs-Roots', ipfsRoots.map(cid => cid.toV1().toString()).join(',')); // https://specs.ipfs.tech/http-gateways/path-gateway/#x-ipfs-roots-response-header
|
|
244
|
+
}
|
|
245
|
+
// response.headers.set('Content-Disposition', `TODO`) // https://specs.ipfs.tech/http-gateways/path-gateway/#content-disposition-response-header
|
|
246
|
+
return response;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Start the Helia instance
|
|
250
|
+
*/
|
|
251
|
+
async start() {
|
|
252
|
+
await this.helia.start();
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Shut down the Helia instance
|
|
256
|
+
*/
|
|
257
|
+
async stop() {
|
|
258
|
+
await this.helia.stop();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
//# sourceMappingURL=verified-fetch.js.map
|