@helia/http 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,100 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * Exports a `createHeliaHTTP` function that returns an object that implements a lightweight version of the {@link Helia} API that functions only over HTTP.
5
+ *
6
+ * Pass it to other modules like {@link https://www.npmjs.com/package/@helia/unixfs | @helia/unixfs} to fetch files from the distributed web.
7
+ *
8
+ * @example
9
+ *
10
+ * ```typescript
11
+ * import { createHeliaHTTP } from '@helia/http'
12
+ * import { unixfs } from '@helia/unixfs'
13
+ * import { CID } from 'multiformats/cid'
14
+ *
15
+ * const helia = await createHeliaHTTP()
16
+ *
17
+ * const fs = unixfs(helia)
18
+ * fs.cat(CID.parse('bafyFoo'))
19
+ * ```
20
+ */
21
+ import type { DAGWalker, Helia } from '@helia/interface';
22
+ import type { BlockBroker } from '@helia/interface/blocks';
23
+ import type { ComponentLogger, Libp2p } from '@libp2p/interface';
24
+ import type { Blockstore } from 'interface-blockstore';
25
+ import type { Datastore } from 'interface-datastore';
26
+ import type { Libp2pOptions } from 'libp2p';
27
+ import type { MultihashHasher } from 'multiformats/hashes/interface';
28
+ export * from '@helia/interface';
29
+ export * from '@helia/interface/blocks';
30
+ export * from '@helia/interface/pins';
31
+ /**
32
+ * Options used to create a Helia node.
33
+ */
34
+ export interface HeliaHTTPInit<T extends Libp2p = Libp2p> {
35
+ /**
36
+ * A libp2p node is required to perform network operations. Either a
37
+ * preconfigured node or options to configure a node can be passed
38
+ * here.
39
+ *
40
+ * If node options are passed, they will be merged with the default
41
+ * config for the current platform. In this case all passed config
42
+ * keys will replace those from the default config.
43
+ */
44
+ libp2p?: T | Libp2pOptions;
45
+ /**
46
+ * The blockstore is where blocks are stored
47
+ */
48
+ blockstore?: Blockstore;
49
+ /**
50
+ * The datastore is where data is stored
51
+ */
52
+ datastore?: Datastore;
53
+ /**
54
+ * By default sha256, sha512 and identity hashes are supported for
55
+ * bitswap operations. To bitswap blocks with CIDs using other hashes
56
+ * pass appropriate MultihashHashers here.
57
+ */
58
+ hashers?: MultihashHasher[];
59
+ /**
60
+ * In order to pin CIDs that correspond to a DAG, it's necessary to know
61
+ * how to traverse that DAG. DAGWalkers take a block and yield any CIDs
62
+ * encoded within that block.
63
+ */
64
+ dagWalkers?: DAGWalker[];
65
+ /**
66
+ * A list of strategies used to fetch blocks when they are not present in
67
+ * the local blockstore
68
+ */
69
+ blockBrokers?: Array<(components: any) => BlockBroker>;
70
+ /**
71
+ * Pass `false` to not start the Helia node
72
+ */
73
+ start?: boolean;
74
+ /**
75
+ * Garbage collection requires preventing blockstore writes during searches
76
+ * for unpinned blocks as DAGs are typically pinned after they've been
77
+ * imported - without locking this could lead to the deletion of blocks while
78
+ * they are being added to the blockstore.
79
+ *
80
+ * By default this lock is held on the main process (e.g. node cluster's
81
+ * primary process, the renderer thread in browsers) and other processes will
82
+ * contact the main process for access (worker processes in node cluster,
83
+ * webworkers in the browser).
84
+ *
85
+ * If Helia is being run wholly in a non-primary process, with no other process
86
+ * expected to access the blockstore (e.g. being run in the background in a
87
+ * webworker), pass true here to hold the gc lock in this process.
88
+ */
89
+ holdGcLock?: boolean;
90
+ /**
91
+ * An optional logging component to pass to libp2p. If not specified the
92
+ * default implementation from libp2p will be used.
93
+ */
94
+ logger?: ComponentLogger;
95
+ }
96
+ /**
97
+ * Create and return a Helia node
98
+ */
99
+ export declare function createHeliaHTTP(init?: HeliaHTTPInit): Promise<Helia>;
100
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAQH,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACxD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAChE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAA;AAC3C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAIpE,cAAc,kBAAkB,CAAA;AAChC,cAAc,yBAAyB,CAAA;AACvC,cAAc,uBAAuB,CAAA;AAErC;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;IACtD;;;;;;;;OAQG;IACH,MAAM,CAAC,EAAE,CAAC,GAAG,aAAa,CAAA;IAE1B;;OAEG;IACH,UAAU,CAAC,EAAE,UAAU,CAAA;IAEvB;;OAEG;IACH,SAAS,CAAC,EAAE,SAAS,CAAA;IAErB;;;;OAIG;IACH,OAAO,CAAC,EAAE,eAAe,EAAE,CAAA;IAE3B;;;;OAIG;IACH,UAAU,CAAC,EAAE,SAAS,EAAE,CAAA;IAExB;;;OAGG;IACH,YAAY,CAAC,EAAE,KAAK,CAAC,CAAC,UAAU,EAAE,GAAG,KAAK,WAAW,CAAC,CAAA;IAEtD;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;IAEf;;;;;;;;;;;;;;OAcG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;IAEpB;;;OAGG;IACH,MAAM,CAAC,EAAE,eAAe,CAAA;CACzB;AAED;;GAEG;AACH,wBAAsB,eAAe,CAAE,IAAI,GAAE,aAAkB,GAAG,OAAO,CAAC,KAAK,CAAC,CA+B/E"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * Exports a `createHeliaHTTP` function that returns an object that implements a lightweight version of the {@link Helia} API that functions only over HTTP.
5
+ *
6
+ * Pass it to other modules like {@link https://www.npmjs.com/package/@helia/unixfs | @helia/unixfs} to fetch files from the distributed web.
7
+ *
8
+ * @example
9
+ *
10
+ * ```typescript
11
+ * import { createHeliaHTTP } from '@helia/http'
12
+ * import { unixfs } from '@helia/unixfs'
13
+ * import { CID } from 'multiformats/cid'
14
+ *
15
+ * const helia = await createHeliaHTTP()
16
+ *
17
+ * const fs = unixfs(helia)
18
+ * fs.cat(CID.parse('bafyFoo'))
19
+ * ```
20
+ */
21
+ import { trustlessGateway } from '@helia/block-brokers';
22
+ import { Helia as HeliaClass } from '@helia/core';
23
+ import { MemoryBlockstore } from 'blockstore-core';
24
+ import { MemoryDatastore } from 'datastore-core';
25
+ import { createLibp2p } from './utils/libp2p.js';
26
+ // re-export interface types so people don't have to depend on @helia/interface
27
+ // if they don't want to
28
+ export * from '@helia/interface';
29
+ export * from '@helia/interface/blocks';
30
+ export * from '@helia/interface/pins';
31
+ /**
32
+ * Create and return a Helia node
33
+ */
34
+ export async function createHeliaHTTP(init = {}) {
35
+ const datastore = init.datastore ?? new MemoryDatastore();
36
+ const blockstore = init.blockstore ?? new MemoryBlockstore();
37
+ let libp2p;
38
+ if (isLibp2p(init.libp2p)) {
39
+ libp2p = init.libp2p;
40
+ }
41
+ else {
42
+ libp2p = await createLibp2p({
43
+ ...init,
44
+ libp2p: init.libp2p,
45
+ datastore
46
+ });
47
+ }
48
+ const helia = new HeliaClass({
49
+ ...init,
50
+ libp2p,
51
+ datastore,
52
+ blockstore,
53
+ blockBrokers: init.blockBrokers ?? [
54
+ trustlessGateway()
55
+ ]
56
+ });
57
+ if (init.start !== false) {
58
+ await helia.start();
59
+ }
60
+ return helia;
61
+ }
62
+ function isLibp2p(obj) {
63
+ if (obj == null) {
64
+ return false;
65
+ }
66
+ // a non-exhaustive list of methods found on the libp2p object
67
+ const funcs = ['dial', 'dialProtocol', 'hangUp', 'handle', 'unhandle', 'getMultiaddrs', 'getProtocols'];
68
+ // if these are all functions it's probably a libp2p object
69
+ return funcs.every(m => typeof obj[m] === 'function');
70
+ }
71
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,EAAE,KAAK,IAAI,UAAU,EAAE,MAAM,aAAa,CAAA;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAUhD,+EAA+E;AAC/E,wBAAwB;AACxB,cAAc,kBAAkB,CAAA;AAChC,cAAc,yBAAyB,CAAA;AACvC,cAAc,uBAAuB,CAAA;AA4ErC;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAE,OAAsB,EAAE;IAC7D,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,IAAI,IAAI,eAAe,EAAE,CAAA;IACzD,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,IAAI,gBAAgB,EAAE,CAAA;IAE5D,IAAI,MAAqC,CAAA;IAEzC,IAAI,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QAC1B,MAAM,GAAG,IAAI,CAAC,MAAa,CAAA;IAC7B,CAAC;SAAM,CAAC;QACN,MAAM,GAAG,MAAM,YAAY,CAAwB;YACjD,GAAG,IAAI;YACP,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,SAAS;SACV,CAAC,CAAA;IACJ,CAAC;IAED,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC;QAC3B,GAAG,IAAI;QACP,MAAM;QACN,SAAS;QACT,UAAU;QACV,YAAY,EAAE,IAAI,CAAC,YAAY,IAAI;YACjC,gBAAgB,EAAE;SACnB;KACF,CAAC,CAAA;IAEF,IAAI,IAAI,CAAC,KAAK,KAAK,KAAK,EAAE,CAAC;QACzB,MAAM,KAAK,CAAC,KAAK,EAAE,CAAA;IACrB,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC;AAED,SAAS,QAAQ,CAAE,GAAQ;IACzB,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC;QAChB,OAAO,KAAK,CAAA;IACd,CAAC;IAED,8DAA8D;IAC9D,MAAM,KAAK,GAAG,CAAC,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,eAAe,EAAE,cAAc,CAAC,CAAA;IAEvG,2DAA2D;IAC3D,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,KAAK,UAAU,CAAC,CAAA;AACvD,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { Libp2pDefaultsOptions } from './libp2p.js';
2
+ import type { Libp2pOptions } from 'libp2p';
3
+ export interface DefaultLibp2pServices extends Record<string, unknown> {
4
+ delegatedRouting: unknown;
5
+ }
6
+ export declare function libp2pDefaults(options?: Libp2pDefaultsOptions): Libp2pOptions<DefaultLibp2pServices>;
7
+ //# sourceMappingURL=libp2p-defaults.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"libp2p-defaults.d.ts","sourceRoot":"","sources":["../../../src/utils/libp2p-defaults.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAA;AAE3C,MAAM,WAAW,qBAAsB,SAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACpE,gBAAgB,EAAE,OAAO,CAAA;CAC1B;AAED,wBAAgB,cAAc,CAAE,OAAO,GAAE,qBAA0B,GAAG,aAAa,CAAC,qBAAqB,CAAC,CAUzG"}
@@ -0,0 +1,13 @@
1
+ import { createDelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client';
2
+ export function libp2pDefaults(options = {}) {
3
+ return {
4
+ services: {
5
+ delegatedRouting: () => createDelegatedRoutingV1HttpApiClient('https://delegated-ipfs.dev')
6
+ },
7
+ connectionManager: {
8
+ minConnections: 0
9
+ },
10
+ ...options
11
+ };
12
+ }
13
+ //# sourceMappingURL=libp2p-defaults.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"libp2p-defaults.js","sourceRoot":"","sources":["../../../src/utils/libp2p-defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qCAAqC,EAAE,MAAM,6CAA6C,CAAA;AAQnG,MAAM,UAAU,cAAc,CAAE,UAAiC,EAAE;IACjE,OAAO;QACL,QAAQ,EAAE;YACR,gBAAgB,EAAE,GAAG,EAAE,CAAC,qCAAqC,CAAC,4BAA4B,CAAC;SAC5F;QACD,iBAAiB,EAAE;YACjB,cAAc,EAAE,CAAC;SAClB;QACD,GAAG,OAAO;KACX,CAAA;AACH,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { DefaultLibp2pServices } from './libp2p-defaults.js';
2
+ import type { ComponentLogger, Libp2p, PeerId } from '@libp2p/interface';
3
+ import type { Datastore } from 'interface-datastore';
4
+ import type { Libp2pOptions } from 'libp2p';
5
+ export interface CreateLibp2pOptions<T extends Record<string, unknown>> {
6
+ datastore: Datastore;
7
+ libp2p?: Libp2pOptions<T>;
8
+ logger?: ComponentLogger;
9
+ start?: boolean;
10
+ }
11
+ export interface Libp2pDefaultsOptions {
12
+ peerId?: PeerId;
13
+ }
14
+ export declare function createLibp2p<T extends Record<string, unknown> = DefaultLibp2pServices>(options: CreateLibp2pOptions<T>): Promise<Libp2p<T>>;
15
+ //# sourceMappingURL=libp2p.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"libp2p.d.ts","sourceRoot":"","sources":["../../../src/utils/libp2p.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AACjE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AACxE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAA;AAE3C,MAAM,WAAW,mBAAmB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACpE,SAAS,EAAE,SAAS,CAAA;IACpB,MAAM,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,CAAA;IACzB,MAAM,CAAC,EAAE,eAAe,CAAA;IACxB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAED,MAAM,WAAW,qBAAqB;IACpC,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,wBAAsB,YAAY,CAAE,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,qBAAqB,EAAG,OAAO,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAUnJ"}
@@ -0,0 +1,13 @@
1
+ import { createLibp2p as create } from 'libp2p';
2
+ import { libp2pDefaults } from './libp2p-defaults.js';
3
+ export async function createLibp2p(options) {
4
+ const defaults = libp2pDefaults();
5
+ options = options ?? {};
6
+ // @ts-expect-error derived ServiceMap is not compatible with ServiceFactoryMap
7
+ return create({
8
+ ...defaults,
9
+ ...options.libp2p,
10
+ start: false
11
+ });
12
+ }
13
+ //# sourceMappingURL=libp2p.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"libp2p.js","sourceRoot":"","sources":["../../../src/utils/libp2p.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,IAAI,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAiBrD,MAAM,CAAC,KAAK,UAAU,YAAY,CAA8D,OAA+B;IAC7H,MAAM,QAAQ,GAAG,cAAc,EAAE,CAAA;IACjC,OAAO,GAAG,OAAO,IAAI,EAAE,CAAA;IAEvB,+EAA+E;IAC/E,OAAO,MAAM,CAAC;QACZ,GAAG,QAAQ;QACX,GAAG,OAAO,CAAC,MAAM;QACjB,KAAK,EAAE,KAAK;KACb,CAAC,CAAA;AACJ,CAAC"}
@@ -0,0 +1,6 @@
1
+ {
2
+ "HeliaHTTPInit": "https://ipfs.github.io/helia/interfaces/_helia_http.HeliaHTTPInit.html",
3
+ ".:HeliaHTTPInit": "https://ipfs.github.io/helia/interfaces/_helia_http.HeliaHTTPInit.html",
4
+ "createHeliaHTTP": "https://ipfs.github.io/helia/functions/_helia_http.createHeliaHTTP.html",
5
+ ".:createHeliaHTTP": "https://ipfs.github.io/helia/functions/_helia_http.createHeliaHTTP.html"
6
+ }
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@helia/http",
3
+ "version": "0.9.0",
4
+ "description": "A lightweight implementation of IPFS over HTTP in JavaScript",
5
+ "license": "Apache-2.0 OR MIT",
6
+ "homepage": "https://github.com/ipfs/helia/tree/main/packages/http#readme",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/ipfs/helia.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/ipfs/helia/issues"
13
+ },
14
+ "keywords": [
15
+ "IPFS"
16
+ ],
17
+ "type": "module",
18
+ "types": "./dist/src/index.d.ts",
19
+ "files": [
20
+ "src",
21
+ "dist",
22
+ "!dist/test",
23
+ "!**/*.tsbuildinfo"
24
+ ],
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/src/index.d.ts",
28
+ "import": "./dist/src/index.js"
29
+ }
30
+ },
31
+ "eslintConfig": {
32
+ "extends": "ipfs",
33
+ "parserOptions": {
34
+ "project": true,
35
+ "sourceType": "module"
36
+ }
37
+ },
38
+ "scripts": {
39
+ "clean": "aegir clean",
40
+ "lint": "aegir lint",
41
+ "dep-check": "aegir dep-check",
42
+ "build": "aegir build",
43
+ "test": "aegir test",
44
+ "test:chrome": "aegir test -t browser --cov",
45
+ "test:chrome-webworker": "aegir test -t webworker",
46
+ "test:firefox": "aegir test -t browser -- --browser firefox",
47
+ "test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
48
+ "test:node": "aegir test -t node --cov",
49
+ "test:electron-main": "aegir test -t electron-main"
50
+ },
51
+ "dependencies": {
52
+ "@helia/block-brokers": "~0.0.0",
53
+ "@helia/core": "~0.0.0",
54
+ "@helia/delegated-routing-v1-http-api-client": "^1.1.2",
55
+ "@helia/interface": "^3.0.0",
56
+ "@libp2p/interface": "^1.1.1",
57
+ "blockstore-core": "^4.3.8",
58
+ "datastore-core": "^9.2.6",
59
+ "interface-blockstore": "^5.2.7",
60
+ "interface-datastore": "^8.2.9",
61
+ "libp2p": "^1.1.1",
62
+ "multiformats": "^13.0.0"
63
+ },
64
+ "devDependencies": {
65
+ "aegir": "^42.1.0"
66
+ }
67
+ }
package/src/index.ts ADDED
@@ -0,0 +1,162 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * Exports a `createHeliaHTTP` function that returns an object that implements a lightweight version of the {@link Helia} API that functions only over HTTP.
5
+ *
6
+ * Pass it to other modules like {@link https://www.npmjs.com/package/@helia/unixfs | @helia/unixfs} to fetch files from the distributed web.
7
+ *
8
+ * @example
9
+ *
10
+ * ```typescript
11
+ * import { createHeliaHTTP } from '@helia/http'
12
+ * import { unixfs } from '@helia/unixfs'
13
+ * import { CID } from 'multiformats/cid'
14
+ *
15
+ * const helia = await createHeliaHTTP()
16
+ *
17
+ * const fs = unixfs(helia)
18
+ * fs.cat(CID.parse('bafyFoo'))
19
+ * ```
20
+ */
21
+
22
+ import { trustlessGateway } from '@helia/block-brokers'
23
+ import { Helia as HeliaClass } from '@helia/core'
24
+ import { MemoryBlockstore } from 'blockstore-core'
25
+ import { MemoryDatastore } from 'datastore-core'
26
+ import { createLibp2p } from './utils/libp2p.js'
27
+ import type { DefaultLibp2pServices } from './utils/libp2p-defaults.js'
28
+ import type { DAGWalker, Helia } from '@helia/interface'
29
+ import type { BlockBroker } from '@helia/interface/blocks'
30
+ import type { ComponentLogger, Libp2p } from '@libp2p/interface'
31
+ import type { Blockstore } from 'interface-blockstore'
32
+ import type { Datastore } from 'interface-datastore'
33
+ import type { Libp2pOptions } from 'libp2p'
34
+ import type { MultihashHasher } from 'multiformats/hashes/interface'
35
+
36
+ // re-export interface types so people don't have to depend on @helia/interface
37
+ // if they don't want to
38
+ export * from '@helia/interface'
39
+ export * from '@helia/interface/blocks'
40
+ export * from '@helia/interface/pins'
41
+
42
+ /**
43
+ * Options used to create a Helia node.
44
+ */
45
+ export interface HeliaHTTPInit<T extends Libp2p = Libp2p> {
46
+ /**
47
+ * A libp2p node is required to perform network operations. Either a
48
+ * preconfigured node or options to configure a node can be passed
49
+ * here.
50
+ *
51
+ * If node options are passed, they will be merged with the default
52
+ * config for the current platform. In this case all passed config
53
+ * keys will replace those from the default config.
54
+ */
55
+ libp2p?: T | Libp2pOptions
56
+
57
+ /**
58
+ * The blockstore is where blocks are stored
59
+ */
60
+ blockstore?: Blockstore
61
+
62
+ /**
63
+ * The datastore is where data is stored
64
+ */
65
+ datastore?: Datastore
66
+
67
+ /**
68
+ * By default sha256, sha512 and identity hashes are supported for
69
+ * bitswap operations. To bitswap blocks with CIDs using other hashes
70
+ * pass appropriate MultihashHashers here.
71
+ */
72
+ hashers?: MultihashHasher[]
73
+
74
+ /**
75
+ * In order to pin CIDs that correspond to a DAG, it's necessary to know
76
+ * how to traverse that DAG. DAGWalkers take a block and yield any CIDs
77
+ * encoded within that block.
78
+ */
79
+ dagWalkers?: DAGWalker[]
80
+
81
+ /**
82
+ * A list of strategies used to fetch blocks when they are not present in
83
+ * the local blockstore
84
+ */
85
+ blockBrokers?: Array<(components: any) => BlockBroker>
86
+
87
+ /**
88
+ * Pass `false` to not start the Helia node
89
+ */
90
+ start?: boolean
91
+
92
+ /**
93
+ * Garbage collection requires preventing blockstore writes during searches
94
+ * for unpinned blocks as DAGs are typically pinned after they've been
95
+ * imported - without locking this could lead to the deletion of blocks while
96
+ * they are being added to the blockstore.
97
+ *
98
+ * By default this lock is held on the main process (e.g. node cluster's
99
+ * primary process, the renderer thread in browsers) and other processes will
100
+ * contact the main process for access (worker processes in node cluster,
101
+ * webworkers in the browser).
102
+ *
103
+ * If Helia is being run wholly in a non-primary process, with no other process
104
+ * expected to access the blockstore (e.g. being run in the background in a
105
+ * webworker), pass true here to hold the gc lock in this process.
106
+ */
107
+ holdGcLock?: boolean
108
+
109
+ /**
110
+ * An optional logging component to pass to libp2p. If not specified the
111
+ * default implementation from libp2p will be used.
112
+ */
113
+ logger?: ComponentLogger
114
+ }
115
+
116
+ /**
117
+ * Create and return a Helia node
118
+ */
119
+ export async function createHeliaHTTP (init: HeliaHTTPInit = {}): Promise<Helia> {
120
+ const datastore = init.datastore ?? new MemoryDatastore()
121
+ const blockstore = init.blockstore ?? new MemoryBlockstore()
122
+
123
+ let libp2p: Libp2p<DefaultLibp2pServices>
124
+
125
+ if (isLibp2p(init.libp2p)) {
126
+ libp2p = init.libp2p as any
127
+ } else {
128
+ libp2p = await createLibp2p<DefaultLibp2pServices>({
129
+ ...init,
130
+ libp2p: init.libp2p,
131
+ datastore
132
+ })
133
+ }
134
+
135
+ const helia = new HeliaClass({
136
+ ...init,
137
+ libp2p,
138
+ datastore,
139
+ blockstore,
140
+ blockBrokers: init.blockBrokers ?? [
141
+ trustlessGateway()
142
+ ]
143
+ })
144
+
145
+ if (init.start !== false) {
146
+ await helia.start()
147
+ }
148
+
149
+ return helia
150
+ }
151
+
152
+ function isLibp2p (obj: any): obj is Libp2p {
153
+ if (obj == null) {
154
+ return false
155
+ }
156
+
157
+ // a non-exhaustive list of methods found on the libp2p object
158
+ const funcs = ['dial', 'dialProtocol', 'hangUp', 'handle', 'unhandle', 'getMultiaddrs', 'getProtocols']
159
+
160
+ // if these are all functions it's probably a libp2p object
161
+ return funcs.every(m => typeof obj[m] === 'function')
162
+ }
@@ -0,0 +1,19 @@
1
+ import { createDelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
2
+ import type { Libp2pDefaultsOptions } from './libp2p.js'
3
+ import type { Libp2pOptions } from 'libp2p'
4
+
5
+ export interface DefaultLibp2pServices extends Record<string, unknown> {
6
+ delegatedRouting: unknown
7
+ }
8
+
9
+ export function libp2pDefaults (options: Libp2pDefaultsOptions = {}): Libp2pOptions<DefaultLibp2pServices> {
10
+ return {
11
+ services: {
12
+ delegatedRouting: () => createDelegatedRoutingV1HttpApiClient('https://delegated-ipfs.dev')
13
+ },
14
+ connectionManager: {
15
+ minConnections: 0
16
+ },
17
+ ...options
18
+ }
19
+ }
@@ -0,0 +1,29 @@
1
+ import { createLibp2p as create } from 'libp2p'
2
+ import { libp2pDefaults } from './libp2p-defaults.js'
3
+ import type { DefaultLibp2pServices } from './libp2p-defaults.js'
4
+ import type { ComponentLogger, Libp2p, PeerId } from '@libp2p/interface'
5
+ import type { Datastore } from 'interface-datastore'
6
+ import type { Libp2pOptions } from 'libp2p'
7
+
8
+ export interface CreateLibp2pOptions<T extends Record<string, unknown>> {
9
+ datastore: Datastore
10
+ libp2p?: Libp2pOptions<T>
11
+ logger?: ComponentLogger
12
+ start?: boolean
13
+ }
14
+
15
+ export interface Libp2pDefaultsOptions {
16
+ peerId?: PeerId
17
+ }
18
+
19
+ export async function createLibp2p <T extends Record<string, unknown> = DefaultLibp2pServices> (options: CreateLibp2pOptions<T>): Promise<Libp2p<T>> {
20
+ const defaults = libp2pDefaults()
21
+ options = options ?? {}
22
+
23
+ // @ts-expect-error derived ServiceMap is not compatible with ServiceFactoryMap
24
+ return create({
25
+ ...defaults,
26
+ ...options.libp2p,
27
+ start: false
28
+ })
29
+ }