helia 2.0.2 → 2.0.3-0749cbf
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +38 -59
- package/dist/src/block-brokers/bitswap-block-broker.d.ts +20 -0
- package/dist/src/block-brokers/bitswap-block-broker.d.ts.map +1 -0
- package/dist/src/block-brokers/bitswap-block-broker.js +39 -0
- package/dist/src/block-brokers/bitswap-block-broker.js.map +1 -0
- package/dist/src/block-brokers/index.d.ts +3 -0
- package/dist/src/block-brokers/index.d.ts.map +1 -0
- package/dist/src/block-brokers/index.js +3 -0
- package/dist/src/block-brokers/index.js.map +1 -0
- package/dist/src/block-brokers/trustless-gateway-block-broker.d.ts +15 -0
- package/dist/src/block-brokers/trustless-gateway-block-broker.d.ts.map +1 -0
- package/dist/src/block-brokers/trustless-gateway-block-broker.js +60 -0
- package/dist/src/block-brokers/trustless-gateway-block-broker.js.map +1 -0
- package/dist/src/helia.d.ts +0 -1
- package/dist/src/helia.d.ts.map +1 -1
- package/dist/src/helia.js +5 -26
- package/dist/src/helia.js.map +1 -1
- package/dist/src/index.d.ts +9 -10
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +33 -35
- package/dist/src/index.js.map +1 -1
- package/dist/src/storage.d.ts +7 -2
- package/dist/src/storage.d.ts.map +1 -1
- package/dist/src/storage.js +14 -0
- package/dist/src/storage.js.map +1 -1
- package/dist/src/utils/default-hashers.d.ts +3 -0
- package/dist/src/utils/default-hashers.d.ts.map +1 -0
- package/dist/src/utils/default-hashers.js +11 -0
- package/dist/src/utils/default-hashers.js.map +1 -0
- package/dist/src/utils/libp2p-defaults.browser.d.ts +7 -3
- package/dist/src/utils/libp2p-defaults.browser.d.ts.map +1 -1
- package/dist/src/utils/libp2p-defaults.browser.js +7 -5
- package/dist/src/utils/libp2p-defaults.browser.js.map +1 -1
- package/dist/src/utils/libp2p-defaults.d.ts +7 -3
- package/dist/src/utils/libp2p-defaults.d.ts.map +1 -1
- package/dist/src/utils/libp2p-defaults.js +11 -5
- package/dist/src/utils/libp2p-defaults.js.map +1 -1
- package/dist/src/utils/libp2p.d.ts +2 -8
- package/dist/src/utils/libp2p.d.ts.map +1 -1
- package/dist/src/utils/libp2p.js.map +1 -1
- package/dist/src/utils/networked-storage.d.ts +16 -9
- package/dist/src/utils/networked-storage.d.ts.map +1 -1
- package/dist/src/utils/networked-storage.js +93 -15
- package/dist/src/utils/networked-storage.js.map +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js +1 -1
- package/dist/src/version.js.map +1 -1
- package/package.json +30 -4
- package/src/block-brokers/bitswap-block-broker.ts +58 -0
- package/src/block-brokers/index.ts +2 -0
- package/src/block-brokers/trustless-gateway-block-broker.ts +78 -0
- package/src/helia.ts +5 -33
- package/src/index.ts +50 -41
- package/src/storage.ts +19 -2
- package/src/utils/default-hashers.ts +12 -0
- package/src/utils/libp2p-defaults.browser.ts +18 -7
- package/src/utils/libp2p-defaults.ts +24 -7
- package/src/utils/libp2p.ts +2 -5
- package/src/utils/networked-storage.ts +116 -23
- package/src/version.ts +1 -1
- package/dist/typedoc-urls.json +0 -8
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import type { BlockRetriever } from '@helia/interface/blocks'
|
|
3
|
+
import type { AbortOptions } from 'interface-store'
|
|
4
|
+
import type { CID } from 'multiformats/cid'
|
|
5
|
+
import type { ProgressEvent, ProgressOptions } from 'progress-events'
|
|
6
|
+
|
|
7
|
+
const log = logger('helia:trustless-gateway-block-provider')
|
|
8
|
+
|
|
9
|
+
export type TrustlessGatewayGetBlockProgressEvents =
|
|
10
|
+
ProgressEvent<'trustless-gateway:get-block:fetch', URL>
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A class that accepts a list of trustless gateways that are queried
|
|
14
|
+
* for blocks.
|
|
15
|
+
*/
|
|
16
|
+
export class TrustedGatewayBlockBroker implements BlockRetriever<
|
|
17
|
+
ProgressOptions<TrustlessGatewayGetBlockProgressEvents>
|
|
18
|
+
> {
|
|
19
|
+
private readonly gateways: URL[]
|
|
20
|
+
|
|
21
|
+
constructor (urls: Array<string | URL>) {
|
|
22
|
+
this.gateways = urls.map(url => new URL(url.toString()))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async retrieve (cid: CID, options: AbortOptions & ProgressOptions<TrustlessGatewayGetBlockProgressEvents> = {}): Promise<Uint8Array> {
|
|
26
|
+
// choose a gateway
|
|
27
|
+
const url = this.gateways[Math.floor(Math.random() * this.gateways.length)]
|
|
28
|
+
|
|
29
|
+
log('getting block for %c from %s', cid, url)
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
const block = await getRawBlockFromGateway(url, cid, options.signal)
|
|
33
|
+
log('got block for %c from %s', cid, url)
|
|
34
|
+
|
|
35
|
+
return block
|
|
36
|
+
} catch (err: any) {
|
|
37
|
+
log.error('failed to get block for %c from %s', cid, url, err)
|
|
38
|
+
|
|
39
|
+
throw err
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function getRawBlockFromGateway (url: URL, cid: CID, signal?: AbortSignal): Promise<Uint8Array> {
|
|
45
|
+
const gwUrl = new URL(url)
|
|
46
|
+
gwUrl.pathname = `/ipfs/${cid.toString()}`
|
|
47
|
+
|
|
48
|
+
// necessary as not every gateway supports dag-cbor, but every should support
|
|
49
|
+
// sending raw block as-is
|
|
50
|
+
gwUrl.search = '?format=raw'
|
|
51
|
+
|
|
52
|
+
if (signal?.aborted === true) {
|
|
53
|
+
throw new Error(`Signal to fetch raw block for CID ${cid} from gateway ${gwUrl.toString()} was aborted prior to fetch`)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
const res = await fetch(gwUrl.toString(), {
|
|
58
|
+
signal,
|
|
59
|
+
headers: {
|
|
60
|
+
// also set header, just in case ?format= is filtered out by some
|
|
61
|
+
// reverse proxy
|
|
62
|
+
Accept: 'application/vnd.ipld.raw'
|
|
63
|
+
},
|
|
64
|
+
cache: 'force-cache'
|
|
65
|
+
})
|
|
66
|
+
if (!res.ok) {
|
|
67
|
+
throw new Error(`unable to fetch raw block for CID ${cid} from gateway ${gwUrl.toString()}`)
|
|
68
|
+
}
|
|
69
|
+
return new Uint8Array(await res.arrayBuffer())
|
|
70
|
+
} catch (cause) {
|
|
71
|
+
// @ts-expect-error - TS thinks signal?.aborted can only be false now
|
|
72
|
+
// because it was checked for true above.
|
|
73
|
+
if (signal?.aborted === true) {
|
|
74
|
+
throw new Error(`fetching raw block for CID ${cid} from gateway ${gwUrl.toString()} was aborted`)
|
|
75
|
+
}
|
|
76
|
+
throw new Error(`unable to fetch raw block for CID ${cid}`)
|
|
77
|
+
}
|
|
78
|
+
}
|
package/src/helia.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
+
import { start, stop } from '@libp2p/interface/startable'
|
|
1
2
|
import { logger } from '@libp2p/logger'
|
|
2
|
-
import { type Bitswap, createBitswap } from 'ipfs-bitswap'
|
|
3
3
|
import drain from 'it-drain'
|
|
4
|
-
import { identity } from 'multiformats/hashes/identity'
|
|
5
|
-
import { sha256, sha512 } from 'multiformats/hashes/sha2'
|
|
6
4
|
import { CustomProgressEvent } from 'progress-events'
|
|
7
5
|
import { PinsImpl } from './pins.js'
|
|
8
6
|
import { BlockStorage } from './storage.js'
|
|
@@ -15,7 +13,6 @@ import type { Libp2p } from '@libp2p/interface'
|
|
|
15
13
|
import type { Blockstore } from 'interface-blockstore'
|
|
16
14
|
import type { Datastore } from 'interface-datastore'
|
|
17
15
|
import type { CID } from 'multiformats/cid'
|
|
18
|
-
import type { MultihashHasher } from 'multiformats/hashes/interface'
|
|
19
16
|
|
|
20
17
|
const log = logger('helia')
|
|
21
18
|
|
|
@@ -31,34 +28,10 @@ export class HeliaImpl implements Helia {
|
|
|
31
28
|
public datastore: Datastore
|
|
32
29
|
public pins: Pins
|
|
33
30
|
|
|
34
|
-
#bitswap?: Bitswap
|
|
35
|
-
|
|
36
31
|
constructor (init: HeliaImplInit) {
|
|
37
|
-
const hashers: MultihashHasher[] = [
|
|
38
|
-
sha256,
|
|
39
|
-
sha512,
|
|
40
|
-
identity,
|
|
41
|
-
...(init.hashers ?? [])
|
|
42
|
-
]
|
|
43
|
-
|
|
44
|
-
this.#bitswap = createBitswap(init.libp2p, init.blockstore, {
|
|
45
|
-
hashLoader: {
|
|
46
|
-
getHasher: async (codecOrName: string | number): Promise<MultihashHasher<number>> => {
|
|
47
|
-
const hasher = hashers.find(hasher => {
|
|
48
|
-
return hasher.code === codecOrName || hasher.name === codecOrName
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
if (hasher != null) {
|
|
52
|
-
return hasher
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
throw new Error(`Could not load hasher for code/name "${codecOrName}"`)
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
})
|
|
59
|
-
|
|
60
32
|
const networkedStorage = new NetworkedStorage(init.blockstore, {
|
|
61
|
-
|
|
33
|
+
blockBrokers: init.blockBrokers,
|
|
34
|
+
hashers: init.hashers
|
|
62
35
|
})
|
|
63
36
|
|
|
64
37
|
this.pins = new PinsImpl(init.datastore, networkedStorage, init.dagWalkers ?? [])
|
|
@@ -72,14 +45,13 @@ export class HeliaImpl implements Helia {
|
|
|
72
45
|
|
|
73
46
|
async start (): Promise<void> {
|
|
74
47
|
await assertDatastoreVersionIsCurrent(this.datastore)
|
|
75
|
-
|
|
76
|
-
await this.#bitswap?.start()
|
|
48
|
+
await start(this.blockstore)
|
|
77
49
|
await this.libp2p.start()
|
|
78
50
|
}
|
|
79
51
|
|
|
80
52
|
async stop (): Promise<void> {
|
|
81
53
|
await this.libp2p.stop()
|
|
82
|
-
await
|
|
54
|
+
await stop(this.blockstore)
|
|
83
55
|
}
|
|
84
56
|
|
|
85
57
|
async gc (options: GCOptions = {}): Promise<void> {
|
package/src/index.ts
CHANGED
|
@@ -24,13 +24,15 @@
|
|
|
24
24
|
import { logger } from '@libp2p/logger'
|
|
25
25
|
import { MemoryBlockstore } from 'blockstore-core'
|
|
26
26
|
import { MemoryDatastore } from 'datastore-core'
|
|
27
|
+
import { BitswapBlockBroker, TrustedGatewayBlockBroker } from './block-brokers/index.js'
|
|
27
28
|
import { HeliaImpl } from './helia.js'
|
|
29
|
+
import { defaultHashers } from './utils/default-hashers.js'
|
|
28
30
|
import { createLibp2p } from './utils/libp2p.js'
|
|
29
31
|
import { name, version } from './version.js'
|
|
32
|
+
import type { DefaultLibp2pServices } from './utils/libp2p-defaults.js'
|
|
30
33
|
import type { Helia } from '@helia/interface'
|
|
34
|
+
import type { BlockBroker } from '@helia/interface/blocks'
|
|
31
35
|
import type { Libp2p } from '@libp2p/interface'
|
|
32
|
-
import type { PubSub } from '@libp2p/interface/pubsub'
|
|
33
|
-
import type { DualKadDHT } from '@libp2p/kad-dht'
|
|
34
36
|
import type { Blockstore } from 'interface-blockstore'
|
|
35
37
|
import type { Datastore } from 'interface-datastore'
|
|
36
38
|
import type { Libp2pOptions } from 'libp2p'
|
|
@@ -50,7 +52,7 @@ const log = logger('helia')
|
|
|
50
52
|
*/
|
|
51
53
|
export interface DAGWalker {
|
|
52
54
|
codec: number
|
|
53
|
-
walk
|
|
55
|
+
walk(block: Uint8Array): AsyncGenerator<CID, void, undefined>
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
/**
|
|
@@ -92,6 +94,12 @@ export interface HeliaInit<T extends Libp2p = Libp2p> {
|
|
|
92
94
|
*/
|
|
93
95
|
dagWalkers?: DAGWalker[]
|
|
94
96
|
|
|
97
|
+
/**
|
|
98
|
+
* A list of strategies used to fetch blocks when they are not present in
|
|
99
|
+
* the local blockstore
|
|
100
|
+
*/
|
|
101
|
+
blockBrokers?: BlockBroker[]
|
|
102
|
+
|
|
95
103
|
/**
|
|
96
104
|
* Pass `false` to not start the Helia node
|
|
97
105
|
*/
|
|
@@ -115,11 +123,28 @@ export interface HeliaInit<T extends Libp2p = Libp2p> {
|
|
|
115
123
|
holdGcLock?: boolean
|
|
116
124
|
}
|
|
117
125
|
|
|
126
|
+
const DEFAULT_TRUSTLESS_GATEWAYS = [
|
|
127
|
+
// 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/
|
|
128
|
+
'https://dweb.link',
|
|
129
|
+
|
|
130
|
+
// 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/
|
|
131
|
+
'https://cf-ipfs.com',
|
|
132
|
+
|
|
133
|
+
// 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/
|
|
134
|
+
'https://4everland.io',
|
|
135
|
+
|
|
136
|
+
// 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/
|
|
137
|
+
'https://w3s.link',
|
|
138
|
+
|
|
139
|
+
// 2023-10-03: IPNS, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/
|
|
140
|
+
'https://cloudflare-ipfs.com'
|
|
141
|
+
]
|
|
142
|
+
|
|
118
143
|
/**
|
|
119
144
|
* Create and return a Helia node
|
|
120
145
|
*/
|
|
121
146
|
export async function createHelia <T extends Libp2p> (init: HeliaInit<T>): Promise<Helia<T>>
|
|
122
|
-
export async function createHelia (init?: HeliaInit<Libp2p<
|
|
147
|
+
export async function createHelia (init?: HeliaInit<Libp2p<DefaultLibp2pServices>>): Promise<Helia<Libp2p<DefaultLibp2pServices>>>
|
|
123
148
|
export async function createHelia (init: HeliaInit = {}): Promise<Helia<unknown>> {
|
|
124
149
|
const datastore = init.datastore ?? new MemoryDatastore()
|
|
125
150
|
const blockstore = init.blockstore ?? new MemoryBlockstore()
|
|
@@ -132,11 +157,20 @@ export async function createHelia (init: HeliaInit = {}): Promise<Helia<unknown>
|
|
|
132
157
|
libp2p = await createLibp2p(datastore, init.libp2p)
|
|
133
158
|
}
|
|
134
159
|
|
|
160
|
+
const hashers = defaultHashers(init.hashers)
|
|
161
|
+
|
|
162
|
+
const blockBrokers = init.blockBrokers ?? [
|
|
163
|
+
new BitswapBlockBroker(libp2p, blockstore, hashers),
|
|
164
|
+
new TrustedGatewayBlockBroker(DEFAULT_TRUSTLESS_GATEWAYS)
|
|
165
|
+
]
|
|
166
|
+
|
|
135
167
|
const helia = new HeliaImpl({
|
|
136
168
|
...init,
|
|
137
169
|
datastore,
|
|
138
170
|
blockstore,
|
|
139
|
-
libp2p
|
|
171
|
+
libp2p,
|
|
172
|
+
blockBrokers,
|
|
173
|
+
hashers
|
|
140
174
|
})
|
|
141
175
|
|
|
142
176
|
if (init.start !== false) {
|
|
@@ -144,16 +178,7 @@ export async function createHelia (init: HeliaInit = {}): Promise<Helia<unknown>
|
|
|
144
178
|
}
|
|
145
179
|
|
|
146
180
|
// add helia to agent version
|
|
147
|
-
|
|
148
|
-
await addHeliaToAgentVersion(helia)
|
|
149
|
-
} else {
|
|
150
|
-
helia.libp2p.addEventListener('start', () => {
|
|
151
|
-
addHeliaToAgentVersion(helia)
|
|
152
|
-
.catch(err => {
|
|
153
|
-
log.error('could not add Helia to agent version', err)
|
|
154
|
-
})
|
|
155
|
-
})
|
|
156
|
-
}
|
|
181
|
+
addHeliaToAgentVersion(helia)
|
|
157
182
|
|
|
158
183
|
return helia
|
|
159
184
|
}
|
|
@@ -170,34 +195,18 @@ function isLibp2p (obj: any): obj is Libp2p {
|
|
|
170
195
|
return funcs.every(m => typeof obj[m] === 'function')
|
|
171
196
|
}
|
|
172
197
|
|
|
173
|
-
|
|
198
|
+
function addHeliaToAgentVersion (helia: Helia<any>): void {
|
|
174
199
|
// add helia to agent version
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
if (versionBuf == null) {
|
|
179
|
-
// identify was not configured
|
|
180
|
-
return
|
|
181
|
-
}
|
|
200
|
+
try {
|
|
201
|
+
const existingAgentVersion = helia.libp2p.services.identify.host.agentVersion
|
|
182
202
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
return
|
|
188
|
-
}
|
|
203
|
+
if (existingAgentVersion.match(/js-libp2p\/\d+\.\d+\.\d+\sUserAgent=/) == null) {
|
|
204
|
+
// the user changed the agent version
|
|
205
|
+
return
|
|
206
|
+
}
|
|
189
207
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
} else {
|
|
194
|
-
// just prepend version name
|
|
195
|
-
versionStr = `${name}/${version} ${versionStr}`
|
|
208
|
+
helia.libp2p.services.identify.host.agentVersion = `${name}/${version} ${helia.libp2p.services.identify.host.agentVersion}`
|
|
209
|
+
} catch (err) {
|
|
210
|
+
log.error('could not add Helia to agent version', err)
|
|
196
211
|
}
|
|
197
|
-
|
|
198
|
-
await helia.libp2p.peerStore.merge(helia.libp2p.peerId, {
|
|
199
|
-
metadata: {
|
|
200
|
-
AgentVersion: new TextEncoder().encode(versionStr)
|
|
201
|
-
}
|
|
202
|
-
})
|
|
203
212
|
}
|
package/src/storage.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { start, stop, type Startable } from '@libp2p/interface/startable'
|
|
1
2
|
import createMortice from 'mortice'
|
|
2
3
|
import type { Blocks, Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents, GetAllBlocksProgressEvents, GetOfflineOptions } from '@helia/interface/blocks'
|
|
3
4
|
import type { Pins } from '@helia/interface/pins'
|
|
@@ -13,7 +14,7 @@ export interface BlockStorageInit {
|
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
export interface GetOptions extends AbortOptions {
|
|
16
|
-
progress
|
|
17
|
+
progress?(evt: Event): void
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
/**
|
|
@@ -21,10 +22,11 @@ export interface GetOptions extends AbortOptions {
|
|
|
21
22
|
* blockstore (that may be on disk, s3, or something else). If the blocks are
|
|
22
23
|
* not present Bitswap will be used to fetch them from network peers.
|
|
23
24
|
*/
|
|
24
|
-
export class BlockStorage implements Blocks {
|
|
25
|
+
export class BlockStorage implements Blocks, Startable {
|
|
25
26
|
public lock: Mortice
|
|
26
27
|
private readonly child: Blockstore
|
|
27
28
|
private readonly pins: Pins
|
|
29
|
+
private started: boolean
|
|
28
30
|
|
|
29
31
|
/**
|
|
30
32
|
* Create a new BlockStorage
|
|
@@ -35,6 +37,21 @@ export class BlockStorage implements Blocks {
|
|
|
35
37
|
this.lock = createMortice({
|
|
36
38
|
singleProcess: options.holdGcLock
|
|
37
39
|
})
|
|
40
|
+
this.started = false
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
isStarted (): boolean {
|
|
44
|
+
return this.started
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async start (): Promise<void> {
|
|
48
|
+
await start(this.child)
|
|
49
|
+
this.started = true
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async stop (): Promise<void> {
|
|
53
|
+
await stop(this.child)
|
|
54
|
+
this.started = false
|
|
38
55
|
}
|
|
39
56
|
|
|
40
57
|
unwrap (): Blockstore {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { identity } from 'multiformats/hashes/identity'
|
|
2
|
+
import { sha256, sha512 } from 'multiformats/hashes/sha2'
|
|
3
|
+
import type { MultihashHasher } from 'multiformats/hashes/interface'
|
|
4
|
+
|
|
5
|
+
export function defaultHashers (hashers: MultihashHasher[] = []): MultihashHasher[] {
|
|
6
|
+
return [
|
|
7
|
+
sha256,
|
|
8
|
+
sha512,
|
|
9
|
+
identity,
|
|
10
|
+
...hashers
|
|
11
|
+
]
|
|
12
|
+
}
|
|
@@ -13,12 +13,22 @@ import { ipnsValidator } from 'ipns/validator'
|
|
|
13
13
|
import { autoNATService } from 'libp2p/autonat'
|
|
14
14
|
import { circuitRelayTransport } from 'libp2p/circuit-relay'
|
|
15
15
|
import { dcutrService } from 'libp2p/dcutr'
|
|
16
|
-
import { identifyService } from 'libp2p/identify'
|
|
16
|
+
import { type IdentifyService, identifyService } from 'libp2p/identify'
|
|
17
|
+
import { pingService, type PingService } from 'libp2p/ping'
|
|
17
18
|
import { bootstrapConfig } from './bootstrappers.js'
|
|
18
19
|
import type { PubSub } from '@libp2p/interface/pubsub'
|
|
19
20
|
import type { Libp2pOptions } from 'libp2p'
|
|
20
21
|
|
|
21
|
-
export
|
|
22
|
+
export interface DefaultLibp2pServices extends Record<string, unknown> {
|
|
23
|
+
dht: DualKadDHT
|
|
24
|
+
pubsub: PubSub
|
|
25
|
+
identify: IdentifyService
|
|
26
|
+
autoNAT: unknown
|
|
27
|
+
dcutr: unknown
|
|
28
|
+
ping: PingService
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function libp2pDefaults (): Libp2pOptions<DefaultLibp2pServices> {
|
|
22
32
|
return {
|
|
23
33
|
addresses: {
|
|
24
34
|
listen: [
|
|
@@ -26,13 +36,13 @@ export function libp2pDefaults (): Libp2pOptions<{ dht: DualKadDHT, pubsub: PubS
|
|
|
26
36
|
]
|
|
27
37
|
},
|
|
28
38
|
transports: [
|
|
39
|
+
circuitRelayTransport({
|
|
40
|
+
discoverRelays: 1
|
|
41
|
+
}),
|
|
29
42
|
webRTC(),
|
|
30
43
|
webRTCDirect(),
|
|
31
44
|
webTransport(),
|
|
32
|
-
webSockets()
|
|
33
|
-
circuitRelayTransport({
|
|
34
|
-
discoverRelays: 1
|
|
35
|
-
})
|
|
45
|
+
webSockets()
|
|
36
46
|
],
|
|
37
47
|
connectionEncryption: [
|
|
38
48
|
noise()
|
|
@@ -60,7 +70,8 @@ export function libp2pDefaults (): Libp2pOptions<{ dht: DualKadDHT, pubsub: PubS
|
|
|
60
70
|
selectors: {
|
|
61
71
|
ipns: ipnsSelector
|
|
62
72
|
}
|
|
63
|
-
})
|
|
73
|
+
}),
|
|
74
|
+
ping: pingService()
|
|
64
75
|
}
|
|
65
76
|
}
|
|
66
77
|
}
|
|
@@ -7,31 +7,47 @@ import { type DualKadDHT, kadDHT } from '@libp2p/kad-dht'
|
|
|
7
7
|
import { mdns } from '@libp2p/mdns'
|
|
8
8
|
import { mplex } from '@libp2p/mplex'
|
|
9
9
|
import { tcp } from '@libp2p/tcp'
|
|
10
|
+
import { webRTC, webRTCDirect } from '@libp2p/webrtc'
|
|
10
11
|
import { webSockets } from '@libp2p/websockets'
|
|
11
12
|
import { ipnsSelector } from 'ipns/selector'
|
|
12
13
|
import { ipnsValidator } from 'ipns/validator'
|
|
13
14
|
import { autoNATService } from 'libp2p/autonat'
|
|
14
15
|
import { circuitRelayTransport, circuitRelayServer, type CircuitRelayService } from 'libp2p/circuit-relay'
|
|
15
16
|
import { dcutrService } from 'libp2p/dcutr'
|
|
16
|
-
import { identifyService } from 'libp2p/identify'
|
|
17
|
+
import { type IdentifyService, identifyService } from 'libp2p/identify'
|
|
18
|
+
import { pingService, type PingService } from 'libp2p/ping'
|
|
17
19
|
import { uPnPNATService } from 'libp2p/upnp-nat'
|
|
18
20
|
import { bootstrapConfig } from './bootstrappers.js'
|
|
19
21
|
import type { PubSub } from '@libp2p/interface/pubsub'
|
|
20
22
|
import type { Libp2pOptions } from 'libp2p'
|
|
21
23
|
|
|
22
|
-
export
|
|
24
|
+
export interface DefaultLibp2pServices extends Record<string, unknown> {
|
|
25
|
+
dht: DualKadDHT
|
|
26
|
+
pubsub: PubSub
|
|
27
|
+
relay: CircuitRelayService
|
|
28
|
+
identify: IdentifyService
|
|
29
|
+
autoNAT: unknown
|
|
30
|
+
upnp: unknown
|
|
31
|
+
dcutr: unknown
|
|
32
|
+
ping: PingService
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function libp2pDefaults (): Libp2pOptions<DefaultLibp2pServices> {
|
|
23
36
|
return {
|
|
24
37
|
addresses: {
|
|
25
38
|
listen: [
|
|
26
|
-
'/ip4/0.0.0.0/tcp/0'
|
|
39
|
+
'/ip4/0.0.0.0/tcp/0',
|
|
40
|
+
'/webrtc'
|
|
27
41
|
]
|
|
28
42
|
},
|
|
29
43
|
transports: [
|
|
30
|
-
tcp(),
|
|
31
|
-
webSockets(),
|
|
32
44
|
circuitRelayTransport({
|
|
33
45
|
discoverRelays: 1
|
|
34
|
-
})
|
|
46
|
+
}),
|
|
47
|
+
tcp(),
|
|
48
|
+
webRTC(),
|
|
49
|
+
webRTCDirect(),
|
|
50
|
+
webSockets()
|
|
35
51
|
],
|
|
36
52
|
connectionEncryption: [
|
|
37
53
|
noise()
|
|
@@ -63,7 +79,8 @@ export function libp2pDefaults (): Libp2pOptions<{ dht: DualKadDHT, pubsub: PubS
|
|
|
63
79
|
}),
|
|
64
80
|
relay: circuitRelayServer({
|
|
65
81
|
advertise: true
|
|
66
|
-
})
|
|
82
|
+
}),
|
|
83
|
+
ping: pingService()
|
|
67
84
|
}
|
|
68
85
|
}
|
|
69
86
|
}
|
package/src/utils/libp2p.ts
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
import { createLibp2p as create, type Libp2pOptions } from 'libp2p'
|
|
2
|
-
import { libp2pDefaults } from './libp2p-defaults.js'
|
|
2
|
+
import { type DefaultLibp2pServices, libp2pDefaults } from './libp2p-defaults.js'
|
|
3
3
|
import type { Libp2p } from '@libp2p/interface'
|
|
4
|
-
import type { PubSub } from '@libp2p/interface/pubsub'
|
|
5
|
-
import type { DualKadDHT } from '@libp2p/kad-dht'
|
|
6
4
|
import type { Datastore } from 'interface-datastore'
|
|
7
|
-
import type { CircuitRelayService } from 'libp2p/circuit-relay'
|
|
8
5
|
|
|
9
6
|
export interface CreateLibp2pOptions {
|
|
10
7
|
datastore: Datastore
|
|
11
8
|
start?: boolean
|
|
12
9
|
}
|
|
13
10
|
|
|
14
|
-
export async function createLibp2p (datastore: Datastore, options?: Libp2pOptions<any>): Promise<Libp2p<
|
|
11
|
+
export async function createLibp2p (datastore: Datastore, options?: Libp2pOptions<any>): Promise<Libp2p<DefaultLibp2pServices>> {
|
|
15
12
|
const defaults = libp2pDefaults()
|
|
16
13
|
options = options ?? {}
|
|
17
14
|
|