@helia/verified-fetch 0.0.0-5c0c39c → 0.0.0-6f8c15b
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/README.md +313 -44
- package/dist/index.min.js +7 -29
- package/dist/src/index.d.ts +369 -54
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +326 -49
- package/dist/src/index.js.map +1 -1
- package/dist/src/singleton.d.ts +3 -0
- package/dist/src/singleton.d.ts.map +1 -0
- package/dist/src/singleton.js +15 -0
- package/dist/src/singleton.js.map +1 -0
- package/dist/src/types.d.ts +2 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/utils/dag-cbor-to-safe-json.d.ts +7 -0
- package/dist/src/utils/dag-cbor-to-safe-json.d.ts.map +1 -0
- package/dist/src/utils/dag-cbor-to-safe-json.js +37 -0
- package/dist/src/utils/dag-cbor-to-safe-json.js.map +1 -0
- package/dist/src/utils/get-content-disposition-filename.d.ts +6 -0
- package/dist/src/utils/get-content-disposition-filename.d.ts.map +1 -0
- package/dist/src/utils/get-content-disposition-filename.js +16 -0
- package/dist/src/utils/get-content-disposition-filename.js.map +1 -0
- package/dist/src/utils/get-e-tag.d.ts +28 -0
- package/dist/src/utils/get-e-tag.d.ts.map +1 -0
- package/dist/src/utils/get-e-tag.js +18 -0
- package/dist/src/utils/get-e-tag.js.map +1 -0
- package/dist/src/utils/get-stream-from-async-iterable.d.ts +10 -0
- package/dist/src/utils/get-stream-from-async-iterable.d.ts.map +1 -0
- package/dist/src/utils/{get-stream-and-content-type.js → get-stream-from-async-iterable.js} +10 -9
- package/dist/src/utils/get-stream-from-async-iterable.js.map +1 -0
- package/dist/src/utils/get-tar-stream.d.ts +4 -0
- package/dist/src/utils/get-tar-stream.d.ts.map +1 -0
- package/dist/src/utils/get-tar-stream.js +46 -0
- package/dist/src/utils/get-tar-stream.js.map +1 -0
- package/dist/src/utils/parse-url-string.d.ts +7 -1
- package/dist/src/utils/parse-url-string.d.ts.map +1 -1
- package/dist/src/utils/parse-url-string.js +6 -0
- package/dist/src/utils/parse-url-string.js.map +1 -1
- package/dist/src/utils/responses.d.ts +5 -0
- package/dist/src/utils/responses.d.ts.map +1 -0
- package/dist/src/utils/responses.js +27 -0
- package/dist/src/utils/responses.js.map +1 -0
- package/dist/src/utils/select-output-type.d.ts +12 -0
- package/dist/src/utils/select-output-type.d.ts.map +1 -0
- package/dist/src/utils/select-output-type.js +148 -0
- package/dist/src/utils/select-output-type.js.map +1 -0
- package/dist/src/verified-fetch.d.ts +23 -27
- package/dist/src/verified-fetch.d.ts.map +1 -1
- package/dist/src/verified-fetch.js +288 -141
- package/dist/src/verified-fetch.js.map +1 -1
- package/package.json +40 -18
- package/src/index.ts +377 -55
- package/src/singleton.ts +20 -0
- package/src/types.ts +1 -0
- package/src/utils/dag-cbor-to-safe-json.ts +44 -0
- package/src/utils/get-content-disposition-filename.ts +18 -0
- package/src/utils/get-e-tag.ts +36 -0
- package/src/utils/{get-stream-and-content-type.ts → get-stream-from-async-iterable.ts} +9 -8
- package/src/utils/get-tar-stream.ts +68 -0
- package/src/utils/parse-url-string.ts +17 -2
- package/src/utils/responses.ts +29 -0
- package/src/utils/select-output-type.ts +167 -0
- package/src/verified-fetch.ts +340 -153
- package/dist/src/utils/get-content-type.d.ts +0 -11
- package/dist/src/utils/get-content-type.d.ts.map +0 -1
- package/dist/src/utils/get-content-type.js +0 -43
- package/dist/src/utils/get-content-type.js.map +0 -1
- package/dist/src/utils/get-stream-and-content-type.d.ts +0 -10
- package/dist/src/utils/get-stream-and-content-type.d.ts.map +0 -1
- package/dist/src/utils/get-stream-and-content-type.js.map +0 -1
- package/src/utils/get-content-type.ts +0 -55
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-content-disposition-filename.js","sourceRoot":"","sources":["../../../src/utils/get-content-disposition-filename.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,6BAA6B,CAAE,QAAgB;IAC7D,MAAM,SAAS,GAAG,yBAAyB,CAAC,QAAQ,CAAC,CAAA;IAErD,IAAI,SAAS,KAAK,QAAQ,EAAE,CAAC;QAC3B,OAAO,aAAa,QAAQ,GAAG,CAAA;IACjC,CAAC;IAED,OAAO,aAAa,SAAS,uBAAuB,kBAAkB,CAAC,QAAQ,CAAC,EAAE,CAAA;AACpF,CAAC;AAED,SAAS,yBAAyB,CAAE,QAAgB;IAClD,4CAA4C;IAC5C,OAAO,QAAQ,CAAC,OAAO,CAAC,eAAe,EAAE,GAAG,CAAC,CAAA;AAC/C,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { RequestFormatShorthand } from '../types.js';
|
|
2
|
+
import type { CID } from 'multiformats/cid';
|
|
3
|
+
interface GetETagArg {
|
|
4
|
+
cid: CID;
|
|
5
|
+
reqFormat?: RequestFormatShorthand;
|
|
6
|
+
rangeStart?: number;
|
|
7
|
+
rangeEnd?: number;
|
|
8
|
+
/**
|
|
9
|
+
* Weak Etag is used when we can't guarantee byte-for-byte-determinism (generated, or mutable content).
|
|
10
|
+
* Some examples:
|
|
11
|
+
* - IPNS requests
|
|
12
|
+
* - CAR streamed with blocks in non-deterministic order
|
|
13
|
+
* - TAR streamed with files in non-deterministic order
|
|
14
|
+
*/
|
|
15
|
+
weak?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* etag
|
|
19
|
+
* you need to wrap cid with ""
|
|
20
|
+
* we use strong Etags for immutable responses and weak one (prefixed with W/ ) for mutable/generated ones (ipns and generated HTML).
|
|
21
|
+
* block and car responses should have different etag than deserialized one, so you can add some prefix like we do in existing gateway
|
|
22
|
+
*
|
|
23
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
|
|
24
|
+
* @see https://specs.ipfs.tech/http-gateways/path-gateway/#etag-response-header
|
|
25
|
+
*/
|
|
26
|
+
export declare function getETag({ cid, reqFormat, weak, rangeStart, rangeEnd }: GetETagArg): string;
|
|
27
|
+
export {};
|
|
28
|
+
//# sourceMappingURL=get-e-tag.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-e-tag.d.ts","sourceRoot":"","sources":["../../../src/utils/get-e-tag.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;AACzD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAE3C,UAAU,UAAU;IAClB,GAAG,EAAE,GAAG,CAAA;IACR,SAAS,CAAC,EAAE,sBAAsB,CAAA;IAClC,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,OAAO,CAAA;CACf;AAED;;;;;;;;GAQG;AACH,wBAAgB,OAAO,CAAE,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,EAAE,UAAU,GAAG,MAAM,CAQ3F"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* etag
|
|
3
|
+
* you need to wrap cid with ""
|
|
4
|
+
* we use strong Etags for immutable responses and weak one (prefixed with W/ ) for mutable/generated ones (ipns and generated HTML).
|
|
5
|
+
* block and car responses should have different etag than deserialized one, so you can add some prefix like we do in existing gateway
|
|
6
|
+
*
|
|
7
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
|
|
8
|
+
* @see https://specs.ipfs.tech/http-gateways/path-gateway/#etag-response-header
|
|
9
|
+
*/
|
|
10
|
+
export function getETag({ cid, reqFormat, weak, rangeStart, rangeEnd }) {
|
|
11
|
+
const prefix = weak === true ? 'W/' : '';
|
|
12
|
+
let suffix = reqFormat == null ? '' : `.${reqFormat}`;
|
|
13
|
+
if (rangeStart != null || rangeEnd != null) {
|
|
14
|
+
suffix += `.${rangeStart ?? '0'}-${rangeEnd ?? 'N'}`;
|
|
15
|
+
}
|
|
16
|
+
return `${prefix}"${cid.toString()}${suffix}"`;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=get-e-tag.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-e-tag.js","sourceRoot":"","sources":["../../../src/utils/get-e-tag.ts"],"names":[],"mappings":"AAkBA;;;;;;;;GAQG;AACH,MAAM,UAAU,OAAO,CAAE,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAc;IACjF,MAAM,MAAM,GAAG,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAA;IACxC,IAAI,MAAM,GAAG,SAAS,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,SAAS,EAAE,CAAA;IACrD,IAAI,UAAU,IAAI,IAAI,IAAI,QAAQ,IAAI,IAAI,EAAE,CAAC;QAC3C,MAAM,IAAI,IAAI,UAAU,IAAI,GAAG,IAAI,QAAQ,IAAI,GAAG,EAAE,CAAA;IACtD,CAAC;IAED,OAAO,GAAG,MAAM,IAAI,GAAG,CAAC,QAAQ,EAAE,GAAG,MAAM,GAAG,CAAA;AAChD,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { VerifiedFetchInit } from '../index.js';
|
|
2
|
+
import type { ComponentLogger } from '@libp2p/interface';
|
|
3
|
+
/**
|
|
4
|
+
* Converts an async iterator of Uint8Array bytes to a stream and returns the first chunk of bytes
|
|
5
|
+
*/
|
|
6
|
+
export declare function getStreamFromAsyncIterable(iterator: AsyncIterable<Uint8Array>, path: string, logger: ComponentLogger, options?: Pick<VerifiedFetchInit, 'onProgress'>): Promise<{
|
|
7
|
+
stream: ReadableStream<Uint8Array>;
|
|
8
|
+
firstChunk: Uint8Array;
|
|
9
|
+
}>;
|
|
10
|
+
//# sourceMappingURL=get-stream-from-async-iterable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-stream-from-async-iterable.d.ts","sourceRoot":"","sources":["../../../src/utils/get-stream-from-async-iterable.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AACpD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAExD;;GAEG;AACH,wBAAsB,0BAA0B,CAAE,QAAQ,EAAE,aAAa,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,iBAAiB,EAAE,YAAY,CAAC,GAAG,OAAO,CAAC;IAAE,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,CAAC;IAAC,UAAU,EAAE,UAAU,CAAA;CAAE,CAAC,CAqCtP"}
|
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
import { CustomProgressEvent } from 'progress-events';
|
|
2
|
-
import { getContentType } from './get-content-type.js';
|
|
3
2
|
/**
|
|
4
|
-
* Converts an async iterator of Uint8Array bytes to a stream and
|
|
3
|
+
* Converts an async iterator of Uint8Array bytes to a stream and returns the first chunk of bytes
|
|
5
4
|
*/
|
|
6
|
-
export async function
|
|
7
|
-
const log = logger.forComponent('helia:verified-fetch:get-stream-
|
|
5
|
+
export async function getStreamFromAsyncIterable(iterator, path, logger, options) {
|
|
6
|
+
const log = logger.forComponent('helia:verified-fetch:get-stream-from-async-iterable');
|
|
8
7
|
const reader = iterator[Symbol.asyncIterator]();
|
|
9
|
-
const { value, done } = await reader.next();
|
|
8
|
+
const { value: firstChunk, done } = await reader.next();
|
|
10
9
|
if (done === true) {
|
|
11
10
|
log.error('No content found for path', path);
|
|
12
11
|
throw new Error('No content found');
|
|
13
12
|
}
|
|
14
|
-
const contentType = await getContentType({ bytes: value, path });
|
|
15
13
|
const stream = new ReadableStream({
|
|
16
14
|
async start(controller) {
|
|
17
15
|
// the initial value is already available
|
|
18
16
|
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:progress:chunk'));
|
|
19
|
-
controller.enqueue(
|
|
17
|
+
controller.enqueue(firstChunk);
|
|
20
18
|
},
|
|
21
19
|
async pull(controller) {
|
|
22
20
|
const { value, done } = await reader.next();
|
|
@@ -32,6 +30,9 @@ export async function getStreamAndContentType(iterator, path, logger, options) {
|
|
|
32
30
|
controller.enqueue(value);
|
|
33
31
|
}
|
|
34
32
|
});
|
|
35
|
-
return {
|
|
33
|
+
return {
|
|
34
|
+
stream,
|
|
35
|
+
firstChunk
|
|
36
|
+
};
|
|
36
37
|
}
|
|
37
|
-
//# sourceMappingURL=get-stream-
|
|
38
|
+
//# sourceMappingURL=get-stream-from-async-iterable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-stream-from-async-iterable.js","sourceRoot":"","sources":["../../../src/utils/get-stream-from-async-iterable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAIrD;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAAE,QAAmC,EAAE,IAAY,EAAE,MAAuB,EAAE,OAA+C;IAC3K,MAAM,GAAG,GAAG,MAAM,CAAC,YAAY,CAAC,qDAAqD,CAAC,CAAA;IACtF,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAA;IAC/C,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAA;IAEvD,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;QAClB,GAAG,CAAC,KAAK,CAAC,2BAA2B,EAAE,IAAI,CAAC,CAAA;QAC5C,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAA;IACrC,CAAC;IAED,MAAM,MAAM,GAAG,IAAI,cAAc,CAAC;QAChC,KAAK,CAAC,KAAK,CAAE,UAAU;YACrB,yCAAyC;YACzC,OAAO,EAAE,UAAU,EAAE,CAAC,IAAI,mBAAmB,CAAO,uCAAuC,CAAC,CAAC,CAAA;YAC7F,UAAU,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;QAChC,CAAC;QACD,KAAK,CAAC,IAAI,CAAE,UAAU;YACpB,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAA;YAE3C,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;gBAClB,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;oBAClB,OAAO,EAAE,UAAU,EAAE,CAAC,IAAI,mBAAmB,CAAO,uCAAuC,CAAC,CAAC,CAAA;oBAC7F,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;gBAC3B,CAAC;gBACD,UAAU,CAAC,KAAK,EAAE,CAAA;gBAClB,OAAM;YACR,CAAC;YAED,OAAO,EAAE,UAAU,EAAE,CAAC,IAAI,mBAAmB,CAAO,uCAAuC,CAAC,CAAC,CAAA;YAC7F,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAC3B,CAAC;KACF,CAAC,CAAA;IAEF,OAAO;QACL,MAAM;QACN,UAAU;KACX,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { AbortOptions } from '@libp2p/interface';
|
|
2
|
+
import type { Blockstore } from 'interface-blockstore';
|
|
3
|
+
export declare function tarStream(ipfsPath: string, blockstore: Blockstore, options?: AbortOptions): AsyncGenerator<Uint8Array>;
|
|
4
|
+
//# sourceMappingURL=get-tar-stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-tar-stream.d.ts","sourceRoot":"","sources":["../../../src/utils/get-tar-stream.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAsCtD,wBAAwB,SAAS,CAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,cAAc,CAAC,UAAU,CAAC,CAuB/H"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { CodeError } from '@libp2p/interface';
|
|
2
|
+
import { exporter, recursive } from 'ipfs-unixfs-exporter';
|
|
3
|
+
import map from 'it-map';
|
|
4
|
+
import { pipe } from 'it-pipe';
|
|
5
|
+
import { pack } from 'it-tar';
|
|
6
|
+
const EXPORTABLE = ['file', 'raw', 'directory'];
|
|
7
|
+
function toHeader(file) {
|
|
8
|
+
let mode;
|
|
9
|
+
let mtime;
|
|
10
|
+
if (file.type === 'file' || file.type === 'directory') {
|
|
11
|
+
mode = file.unixfs.mode;
|
|
12
|
+
mtime = file.unixfs.mtime != null ? new Date(Number(file.unixfs.mtime.secs * 1000n)) : undefined;
|
|
13
|
+
}
|
|
14
|
+
return {
|
|
15
|
+
name: file.path,
|
|
16
|
+
mode,
|
|
17
|
+
mtime,
|
|
18
|
+
size: Number(file.size),
|
|
19
|
+
type: file.type === 'directory' ? 'directory' : 'file'
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function toTarImportCandidate(entry) {
|
|
23
|
+
if (!EXPORTABLE.includes(entry.type)) {
|
|
24
|
+
throw new CodeError('Not a UnixFS node', 'ERR_NOT_UNIXFS');
|
|
25
|
+
}
|
|
26
|
+
const candidate = {
|
|
27
|
+
header: toHeader(entry)
|
|
28
|
+
};
|
|
29
|
+
if (entry.type === 'file' || entry.type === 'raw') {
|
|
30
|
+
candidate.body = entry.content();
|
|
31
|
+
}
|
|
32
|
+
return candidate;
|
|
33
|
+
}
|
|
34
|
+
export async function* tarStream(ipfsPath, blockstore, options) {
|
|
35
|
+
const file = await exporter(ipfsPath, blockstore, options);
|
|
36
|
+
if (file.type === 'file' || file.type === 'raw') {
|
|
37
|
+
yield* pipe([toTarImportCandidate(file)], pack());
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (file.type === 'directory') {
|
|
41
|
+
yield* pipe(recursive(ipfsPath, blockstore, options), (source) => map(source, (entry) => toTarImportCandidate(entry)), pack());
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
throw new CodeError('Not a UnixFS node', 'ERR_NOT_UNIXFS');
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=get-tar-stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-tar-stream.js","sourceRoot":"","sources":["../../../src/utils/get-tar-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAoB,MAAM,sBAAsB,CAAA;AAC5E,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAE,IAAI,EAAgD,MAAM,QAAQ,CAAA;AAI3E,MAAM,UAAU,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,WAAW,CAAC,CAAA;AAE/C,SAAS,QAAQ,CAAE,IAAiB;IAClC,IAAI,IAAwB,CAAA;IAC5B,IAAI,KAAuB,CAAA;IAE3B,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,IAAI,IAAI,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;QACtD,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAA;QACvB,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,C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U,EAAE,OAAO,CAAC,EACxC,CAAC,MAAM,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,oBAAoB,CAAC,KAAK,CAAC,CAAC,EAC/D,IAAI,EAAE,CACP,CAAA;QAED,OAAM;IACR,CAAC;IAED,MAAM,IAAI,SAAS,CAAC,mBAAmB,EAAE,gBAAgB,CAAC,CAAA;AAC5D,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CID } from 'multiformats/cid';
|
|
2
|
+
import type { RequestFormatShorthand } from '../types.js';
|
|
2
3
|
import type { IPNS, IPNSRoutingEvents, ResolveDnsLinkProgressEvents, ResolveProgressEvents } from '@helia/ipns';
|
|
3
4
|
import type { ComponentLogger } from '@libp2p/interface';
|
|
4
5
|
import type { ProgressOptions } from 'progress-events';
|
|
@@ -9,11 +10,16 @@ export interface ParseUrlStringInput {
|
|
|
9
10
|
}
|
|
10
11
|
export interface ParseUrlStringOptions extends ProgressOptions<ResolveProgressEvents | IPNSRoutingEvents | ResolveDnsLinkProgressEvents> {
|
|
11
12
|
}
|
|
13
|
+
export interface ParsedUrlQuery extends Record<string, string | unknown> {
|
|
14
|
+
format?: RequestFormatShorthand;
|
|
15
|
+
download?: boolean;
|
|
16
|
+
filename?: string;
|
|
17
|
+
}
|
|
12
18
|
export interface ParsedUrlStringResults {
|
|
13
19
|
protocol: string;
|
|
14
20
|
path: string;
|
|
15
21
|
cid: CID;
|
|
16
|
-
query:
|
|
22
|
+
query: ParsedUrlQuery;
|
|
17
23
|
}
|
|
18
24
|
/**
|
|
19
25
|
* A function that parses ipfs:// and ipns:// URLs, returning an object with easily recognizable properties.
|
|
@@ -1 +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,EA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AAG,OAAO,CAAC;IACtE,MAAM,CAAC,EAAE,sBAAsB,CAAA;IAC/B,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,sBAAsB;IACrC,QAAQ,EAAE,MAAM,CAAA;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,GAAG,CAAA;IACR,KAAK,EAAE,cAAc,CAAA;CACtB;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,CAgHxJ"}
|
|
@@ -85,6 +85,12 @@ export async function parseUrlString({ urlString, ipns, logger }, options) {
|
|
|
85
85
|
const [key, value] = part.split('=');
|
|
86
86
|
query[key] = decodeURIComponent(value);
|
|
87
87
|
}
|
|
88
|
+
if (query.download != null) {
|
|
89
|
+
query.download = query.download === 'true';
|
|
90
|
+
}
|
|
91
|
+
if (query.filename != null) {
|
|
92
|
+
query.filename = query.filename.toString();
|
|
93
|
+
}
|
|
88
94
|
}
|
|
89
95
|
/**
|
|
90
96
|
* join the path from resolve result & given path.
|
|
@@ -1 +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;
|
|
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,kB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,GAAG,kBAAkB,CAAC,KAAK,CAAC,CAAA;QACxC,CAAC;QAED,IAAI,KAAK,CAAC,QAAQ,IAAI,IAAI,EAAE,CAAC;YAC3B,KAAK,CAAC,QAAQ,GAAG,KAAK,CAAC,QAAQ,KAAK,MAAM,CAAA;QAC5C,CAAC;QAED,IAAI,KAAK,CAAC,QAAQ,IAAI,IAAI,EAAE,CAAC;YAC3B,KAAK,CAAC,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAA;QAC5C,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,5 @@
|
|
|
1
|
+
export declare function okResponse(body?: BodyInit | null): Response;
|
|
2
|
+
export declare function notSupportedResponse(body?: BodyInit | null): Response;
|
|
3
|
+
export declare function notAcceptableResponse(body?: BodyInit | null): Response;
|
|
4
|
+
export declare function badRequestResponse(body?: BodyInit | null): Response;
|
|
5
|
+
//# sourceMappingURL=responses.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"responses.d.ts","sourceRoot":"","sources":["../../../src/utils/responses.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CAAE,IAAI,CAAC,EAAE,QAAQ,GAAG,IAAI,GAAG,QAAQ,CAK5D;AAED,wBAAgB,oBAAoB,CAAE,IAAI,CAAC,EAAE,QAAQ,GAAG,IAAI,GAAG,QAAQ,CAOtE;AAED,wBAAgB,qBAAqB,CAAE,IAAI,CAAC,EAAE,QAAQ,GAAG,IAAI,GAAG,QAAQ,CAKvE;AAED,wBAAgB,kBAAkB,CAAE,IAAI,CAAC,EAAE,QAAQ,GAAG,IAAI,GAAG,QAAQ,CAKpE"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export function okResponse(body) {
|
|
2
|
+
return new Response(body, {
|
|
3
|
+
status: 200,
|
|
4
|
+
statusText: 'OK'
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
export function notSupportedResponse(body) {
|
|
8
|
+
const response = new Response(body, {
|
|
9
|
+
status: 501,
|
|
10
|
+
statusText: 'Not Implemented'
|
|
11
|
+
});
|
|
12
|
+
response.headers.set('X-Content-Type-Options', 'nosniff'); // see https://specs.ipfs.tech/http-gateways/path-gateway/#x-content-type-options-response-header
|
|
13
|
+
return response;
|
|
14
|
+
}
|
|
15
|
+
export function notAcceptableResponse(body) {
|
|
16
|
+
return new Response(body, {
|
|
17
|
+
status: 406,
|
|
18
|
+
statusText: 'Not Acceptable'
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
export function badRequestResponse(body) {
|
|
22
|
+
return new Response(body, {
|
|
23
|
+
status: 400,
|
|
24
|
+
statusText: 'Bad Request'
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=responses.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"responses.js","sourceRoot":"","sources":["../../../src/utils/responses.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,UAAU,CAAE,IAAsB;IAChD,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE;QACxB,MAAM,EAAE,GAAG;QACX,UAAU,EAAE,IAAI;KACjB,CAAC,CAAA;AACJ,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAE,IAAsB;IAC1D,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,IAAI,EAAE;QAClC,MAAM,EAAE,GAAG;QACX,UAAU,EAAE,iBAAiB;KAC9B,CAAC,CAAA;IACF,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,wBAAwB,EAAE,SAAS,CAAC,CAAA,CAAC,iGAAiG;IAC3J,OAAO,QAAQ,CAAA;AACjB,CAAC;AAED,MAAM,UAAU,qBAAqB,CAAE,IAAsB;IAC3D,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE;QACxB,MAAM,EAAE,GAAG;QACX,UAAU,EAAE,gBAAgB;KAC7B,CAAC,CAAA;AACJ,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAE,IAAsB;IACxD,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE;QACxB,MAAM,EAAE,GAAG;QACX,UAAU,EAAE,aAAa;KAC1B,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { RequestFormatShorthand } from '../types.js';
|
|
2
|
+
import type { CID } from 'multiformats/cid';
|
|
3
|
+
/**
|
|
4
|
+
* Selects an output mime-type based on the CID and a passed `Accept` header
|
|
5
|
+
*/
|
|
6
|
+
export declare function selectOutputType(cid: CID, accept?: string): string | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Converts a `format=...` query param to a mime type as would be found in the
|
|
9
|
+
* `Accept` header, if a valid mapping is available
|
|
10
|
+
*/
|
|
11
|
+
export declare function queryFormatToAcceptHeader(format?: RequestFormatShorthand): string | undefined;
|
|
12
|
+
//# sourceMappingURL=select-output-type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"select-output-type.d.ts","sourceRoot":"","sources":["../../../src/utils/select-output-type.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;AACzD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAwD3C;;GAEG;AACH,wBAAgB,gBAAgB,CAAE,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAM/E;AAuFD;;;GAGG;AACH,wBAAgB,yBAAyB,CAAE,MAAM,CAAC,EAAE,sBAAsB,GAAG,MAAM,GAAG,SAAS,CAI9F"}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { code as dagCborCode } from '@ipld/dag-cbor';
|
|
2
|
+
import { code as dagJsonCode } from '@ipld/dag-json';
|
|
3
|
+
import { code as dagPbCode } from '@ipld/dag-pb';
|
|
4
|
+
import { code as jsonCode } from 'multiformats/codecs/json';
|
|
5
|
+
import { code as rawCode } from 'multiformats/codecs/raw';
|
|
6
|
+
/**
|
|
7
|
+
* This maps supported response types for each codec supported by verified-fetch
|
|
8
|
+
*/
|
|
9
|
+
const CID_TYPE_MAP = {
|
|
10
|
+
[dagCborCode]: [
|
|
11
|
+
'application/json',
|
|
12
|
+
'application/vnd.ipld.dag-cbor',
|
|
13
|
+
'application/cbor',
|
|
14
|
+
'application/vnd.ipld.dag-json',
|
|
15
|
+
'application/octet-stream',
|
|
16
|
+
'application/vnd.ipld.raw',
|
|
17
|
+
'application/vnd.ipfs.ipns-record',
|
|
18
|
+
'application/vnd.ipld.car'
|
|
19
|
+
],
|
|
20
|
+
[dagJsonCode]: [
|
|
21
|
+
'application/json',
|
|
22
|
+
'application/vnd.ipld.dag-cbor',
|
|
23
|
+
'application/cbor',
|
|
24
|
+
'application/vnd.ipld.dag-json',
|
|
25
|
+
'application/octet-stream',
|
|
26
|
+
'application/vnd.ipld.raw',
|
|
27
|
+
'application/vnd.ipfs.ipns-record',
|
|
28
|
+
'application/vnd.ipld.car'
|
|
29
|
+
],
|
|
30
|
+
[jsonCode]: [
|
|
31
|
+
'application/json',
|
|
32
|
+
'application/vnd.ipld.dag-cbor',
|
|
33
|
+
'application/cbor',
|
|
34
|
+
'application/vnd.ipld.dag-json',
|
|
35
|
+
'application/octet-stream',
|
|
36
|
+
'application/vnd.ipld.raw',
|
|
37
|
+
'application/vnd.ipfs.ipns-record',
|
|
38
|
+
'application/vnd.ipld.car'
|
|
39
|
+
],
|
|
40
|
+
[dagPbCode]: [
|
|
41
|
+
'application/octet-stream',
|
|
42
|
+
'application/json',
|
|
43
|
+
'application/vnd.ipld.dag-cbor',
|
|
44
|
+
'application/cbor',
|
|
45
|
+
'application/vnd.ipld.dag-json',
|
|
46
|
+
'application/vnd.ipld.raw',
|
|
47
|
+
'application/vnd.ipfs.ipns-record',
|
|
48
|
+
'application/vnd.ipld.car',
|
|
49
|
+
'application/x-tar'
|
|
50
|
+
],
|
|
51
|
+
[rawCode]: [
|
|
52
|
+
'application/octet-stream',
|
|
53
|
+
'application/vnd.ipld.raw',
|
|
54
|
+
'application/vnd.ipfs.ipns-record',
|
|
55
|
+
'application/vnd.ipld.car',
|
|
56
|
+
'application/x-tar'
|
|
57
|
+
]
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Selects an output mime-type based on the CID and a passed `Accept` header
|
|
61
|
+
*/
|
|
62
|
+
export function selectOutputType(cid, accept) {
|
|
63
|
+
const cidMimeTypes = CID_TYPE_MAP[cid.code];
|
|
64
|
+
if (accept != null) {
|
|
65
|
+
return chooseMimeType(accept, cidMimeTypes);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function chooseMimeType(accept, validMimeTypes) {
|
|
69
|
+
const requestedMimeTypes = accept
|
|
70
|
+
.split(',')
|
|
71
|
+
.map(s => {
|
|
72
|
+
const parts = s.trim().split(';');
|
|
73
|
+
return {
|
|
74
|
+
mimeType: `${parts[0]}`.trim(),
|
|
75
|
+
weight: parseQFactor(parts[1])
|
|
76
|
+
};
|
|
77
|
+
})
|
|
78
|
+
.sort((a, b) => {
|
|
79
|
+
if (a.weight === b.weight) {
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
if (a.weight > b.weight) {
|
|
83
|
+
return -1;
|
|
84
|
+
}
|
|
85
|
+
return 1;
|
|
86
|
+
})
|
|
87
|
+
.map(s => s.mimeType);
|
|
88
|
+
for (const headerFormat of requestedMimeTypes) {
|
|
89
|
+
for (const mimeType of validMimeTypes) {
|
|
90
|
+
if (headerFormat.includes(mimeType)) {
|
|
91
|
+
return mimeType;
|
|
92
|
+
}
|
|
93
|
+
if (headerFormat === '*/*') {
|
|
94
|
+
return mimeType;
|
|
95
|
+
}
|
|
96
|
+
if (headerFormat.startsWith('*/') && mimeType.split('/')[1] === headerFormat.split('/')[1]) {
|
|
97
|
+
return mimeType;
|
|
98
|
+
}
|
|
99
|
+
if (headerFormat.endsWith('/*') && mimeType.split('/')[0] === headerFormat.split('/')[0]) {
|
|
100
|
+
return mimeType;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Parses q-factor weighting from the accept header to allow letting some mime
|
|
107
|
+
* types take precedence over others.
|
|
108
|
+
*
|
|
109
|
+
* If the q-factor for an acceptable mime representation is omitted it defaults
|
|
110
|
+
* to `1`.
|
|
111
|
+
*
|
|
112
|
+
* All specified values should be in the range 0-1.
|
|
113
|
+
*
|
|
114
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept#q
|
|
115
|
+
*/
|
|
116
|
+
function parseQFactor(str) {
|
|
117
|
+
if (str != null) {
|
|
118
|
+
str = str.trim();
|
|
119
|
+
}
|
|
120
|
+
if (str == null || !str.startsWith('q=')) {
|
|
121
|
+
return 1;
|
|
122
|
+
}
|
|
123
|
+
const factor = parseFloat(str.replace('q=', ''));
|
|
124
|
+
if (isNaN(factor)) {
|
|
125
|
+
return 0;
|
|
126
|
+
}
|
|
127
|
+
return factor;
|
|
128
|
+
}
|
|
129
|
+
const FORMAT_TO_MIME_TYPE = {
|
|
130
|
+
raw: 'application/vnd.ipld.raw',
|
|
131
|
+
car: 'application/vnd.ipld.car',
|
|
132
|
+
'dag-json': 'application/vnd.ipld.dag-json',
|
|
133
|
+
'dag-cbor': 'application/vnd.ipld.dag-cbor',
|
|
134
|
+
json: 'application/json',
|
|
135
|
+
cbor: 'application/cbor',
|
|
136
|
+
'ipns-record': 'application/vnd.ipfs.ipns-record',
|
|
137
|
+
tar: 'application/x-tar'
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Converts a `format=...` query param to a mime type as would be found in the
|
|
141
|
+
* `Accept` header, if a valid mapping is available
|
|
142
|
+
*/
|
|
143
|
+
export function queryFormatToAcceptHeader(format) {
|
|
144
|
+
if (format != null) {
|
|
145
|
+
return FORMAT_TO_MIME_TYPE[format];
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=select-output-type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"select-output-type.js","sourceRoot":"","sources":["../../../src/utils/select-output-type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,IAAI,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACpD,OAAO,EAAE,IAAI,IAAI,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACpD,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,cAAc,CAAA;AAChD,OAAO,EAAE,IAAI,IAAI,QAAQ,EAAE,MAAM,0BAA0B,CAAA;AAC3D,OAAO,EAAE,IAAI,IAAI,OAAO,EAAE,MAAM,yBAAyB,CAAA;AAIzD;;GAEG;AACH,MAAM,YAAY,GAA6B;IAC7C,CAAC,WAAW,CAAC,EAAE;QACb,kBAAkB;QAClB,+BAA+B;QAC/B,kBAAkB;QAClB,+BAA+B;QAC/B,0BAA0B;QAC1B,0BAA0B;QAC1B,kCAAkC;QAClC,0BAA0B;KAC3B;IACD,CAAC,WAAW,CAAC,EAAE;QACb,kBAAkB;QAClB,+BAA+B;QAC/B,kBAAkB;QAClB,+BAA+B;QAC/B,0BAA0B;QAC1B,0BAA0B;QAC1B,kCAAkC;QAClC,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I,CAAC,EAAE,CAAC;QACzC,OAAO,CAAC,CAAA;IACV,CAAC;IAED,MAAM,MAAM,GAAG,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAA;IAEhD,IAAI,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;QAClB,OAAO,CAAC,CAAA;IACV,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC;AAED,MAAM,mBAAmB,GAA2C;IAClE,GAAG,EAAE,0BAA0B;IAC/B,GAAG,EAAE,0BAA0B;IAC/B,UAAU,EAAE,+BAA+B;IAC3C,UAAU,EAAE,+BAA+B;IAC3C,IAAI,EAAE,kBAAkB;IACxB,IAAI,EAAE,kBAAkB;IACxB,aAAa,EAAE,kCAAkC;IACjD,GAAG,EAAE,mBAAmB;CACzB,CAAA;AAED;;;GAGG;AACH,MAAM,UAAU,yBAAyB,CAAE,MAA+B;IACxE,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;QACnB,OAAO,mBAAmB,CAAC,MAAM,CAAC,CAAA;IACpC,CAAC;AACH,CAAC"}
|
|
@@ -1,54 +1,50 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import { type DAGJSON } from '@helia/dag-json';
|
|
3
|
-
import { type IPNS } from '@helia/ipns';
|
|
4
|
-
import { type JSON } from '@helia/json';
|
|
1
|
+
import { type DNSResolver, type IPNS } from '@helia/ipns';
|
|
5
2
|
import { type UnixFS as HeliaUnixFs } from '@helia/unixfs';
|
|
6
|
-
import {
|
|
7
|
-
import type { Resource, VerifiedFetchInit as VerifiedFetchOptions } from './index.js';
|
|
3
|
+
import type { ContentTypeParser, Resource, VerifiedFetchInit as VerifiedFetchOptions } from './index.js';
|
|
8
4
|
import type { Helia } from '@helia/interface';
|
|
9
5
|
interface VerifiedFetchComponents {
|
|
10
6
|
helia: Helia;
|
|
11
7
|
ipns?: IPNS;
|
|
12
8
|
unixfs?: HeliaUnixFs;
|
|
13
|
-
dagJson?: DAGJSON;
|
|
14
|
-
json?: JSON;
|
|
15
|
-
dagCbor?: DAGCBOR;
|
|
16
|
-
pathWalker?: PathWalkerFn;
|
|
17
9
|
}
|
|
18
10
|
/**
|
|
19
11
|
* Potential future options for the VerifiedFetch constructor.
|
|
20
12
|
*/
|
|
21
13
|
interface VerifiedFetchInit {
|
|
14
|
+
contentTypeParser?: ContentTypeParser;
|
|
15
|
+
dnsResolvers?: DNSResolver[];
|
|
22
16
|
}
|
|
23
17
|
export declare class VerifiedFetch {
|
|
24
18
|
private readonly helia;
|
|
25
19
|
private readonly ipns;
|
|
26
20
|
private readonly unixfs;
|
|
27
|
-
private readonly dagJson;
|
|
28
|
-
private readonly dagCbor;
|
|
29
|
-
private readonly json;
|
|
30
|
-
private readonly pathWalker;
|
|
31
21
|
private readonly log;
|
|
32
|
-
|
|
22
|
+
private readonly contentTypeParser;
|
|
23
|
+
constructor({ helia, ipns, unixfs }: VerifiedFetchComponents, init?: VerifiedFetchInit);
|
|
24
|
+
/**
|
|
25
|
+
* Accepts an `ipns://...` URL as a string and returns a `Response` containing
|
|
26
|
+
* a raw IPNS record.
|
|
27
|
+
*/
|
|
33
28
|
private handleIPNSRecord;
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Accepts a `CID` and returns a `Response` with a body stream that is a CAR
|
|
31
|
+
* of the `DAG` referenced by the `CID`.
|
|
32
|
+
*/
|
|
33
|
+
private handleCar;
|
|
34
|
+
/**
|
|
35
|
+
* Accepts a UnixFS `CID` and returns a `.tar` file containing the file or
|
|
36
|
+
* directory structure referenced by the `CID`.
|
|
37
|
+
*/
|
|
38
|
+
private handleTar;
|
|
36
39
|
private handleJson;
|
|
37
40
|
private handleDagCbor;
|
|
38
41
|
private handleDagPb;
|
|
39
42
|
private handleRaw;
|
|
43
|
+
private setContentType;
|
|
40
44
|
/**
|
|
41
|
-
*
|
|
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
|
|
45
|
+
* If the user has not specified an Accept header or format query string arg,
|
|
46
|
+
* use the CID codec to choose an appropriate handler for the block data.
|
|
50
47
|
*/
|
|
51
|
-
private readonly formatHandlers;
|
|
52
48
|
private readonly codecHandlers;
|
|
53
49
|
fetch(resource: Resource, opts?: VerifiedFetchOptions): Promise<Response>;
|
|
54
50
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verified-fetch.d.ts","sourceRoot":"","sources":["../../src/verified-fetch.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"verified-fetch.d.ts","sourceRoot":"","sources":["../../src/verified-fetch.ts"],"names":[],"mappings":"AACA,OAAO,EAAqB,KAAK,WAAW,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAA;AAE5E,OAAO,EAAyB,KAAK,MAAM,IAAI,WAAW,EAAoB,MAAM,eAAe,CAAA;AAwBnG,OAAO,KAAK,EAAa,iBAAiB,EAAE,QAAQ,EAAE,iBAAiB,IAAI,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAEnH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAK7C,UAAU,uBAAuB;IAC/B,KAAK,EAAE,KAAK,CAAA;IACZ,IAAI,CAAC,EAAE,IAAI,CAAA;IACX,MAAM,CAAC,EAAE,WAAW,CAAA;CACrB;AAED;;GAEG;AACH,UAAU,iBAAiB;IACzB,iBAAiB,CAAC,EAAE,iBAAiB,CAAA;IACrC,YAAY,CAAC,EAAE,WAAW,EAAE,CAAA;CAC7B;AAwED,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAO;IAC7B,OAAO,CAAC,QA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