@helia/libp2p 1.0.4 → 1.0.5
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 +28 -28
- package/dist/index.min.js.map +4 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +42 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/routing.js +2 -2
- package/dist/src/routing.js.map +1 -1
- package/package.json +5 -3
- package/src/index.ts +53 -3
- package/src/routing.ts +2 -2
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAQH,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA;AACvE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAC5D,OAAO,KAAK,EAAE,KAAK,EAAoB,MAAM,kBAAkB,CAAA;AAC/D,OAAO,KAAK,EAAE,MAAM,EAAY,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAIrE,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,YAAY,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA;AACvE,YAAY,EAAE,mBAAmB,EAAE,CAAA;AAEnC,MAAM,WAAW,eAAe,CAAC,CAAC,SAAS,UAAU,GAAG,qBAAqB,CAAE,SAAQ,KAAK;IAC1F;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAA;CAClB;AAeD;;GAEG;AACH,wBAAgB,UAAU,CAAE,CAAC,SAAS,KAAK,EAAE,CAAC,SAAS,UAAU,GAAG,UAAU,EAAE,CAAC,SAAS,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,EAAG,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC,CAAA;AACzJ,wBAAgB,UAAU,CAAE,CAAC,SAAS,KAAK,EAAE,CAAC,SAAS,UAAU,GAAG,UAAU,EAAG,KAAK,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC,CAAA"}
|
package/dist/src/index.js
CHANGED
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
17
|
import { NotStartedError } from '@libp2p/interface';
|
|
18
|
+
import { peerIdFromCID } from '@libp2p/peer-id';
|
|
19
|
+
import forEach from 'it-foreach';
|
|
18
20
|
import { isLibp2p } from 'libp2p';
|
|
19
21
|
import { libp2pRouting } from "./routing.js";
|
|
20
22
|
import { createLibp2p } from "./utils/libp2p.js";
|
|
@@ -48,6 +50,40 @@ export function withLibp2p(helia, opts) {
|
|
|
48
50
|
start: async (helia) => {
|
|
49
51
|
if (libp2p == null) {
|
|
50
52
|
libp2p = await getLibp2p(helia, opts);
|
|
53
|
+
// override peer discovery methods to ensure we persist peer data in the
|
|
54
|
+
// peer store, otherwise we can't dial by peer id without extra lookups
|
|
55
|
+
const findProviders = helia.routing.findProviders.bind(helia.routing);
|
|
56
|
+
helia.routing.findProviders = async function* (cid, options) {
|
|
57
|
+
yield* forEach(findProviders.call(helia.routing, cid, options), async (peer) => {
|
|
58
|
+
if (peer.router !== 'libp2p-router') {
|
|
59
|
+
// only need to do this for peers not found via the libp2p router
|
|
60
|
+
const info = toPeerInfo(peer);
|
|
61
|
+
await libp2p.peerStore.merge(info.id, info);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
const findPeer = helia.routing.findPeer.bind(helia.routing);
|
|
66
|
+
helia.routing.findPeer = async function (cid, options) {
|
|
67
|
+
const peer = await findPeer(cid, options);
|
|
68
|
+
if (peer.router !== 'libp2p-router') {
|
|
69
|
+
// only need to do this for peers not found via the libp2p router
|
|
70
|
+
const info = toPeerInfo(peer);
|
|
71
|
+
await libp2p.peerStore.merge(info.id, info);
|
|
72
|
+
}
|
|
73
|
+
return peer;
|
|
74
|
+
};
|
|
75
|
+
// override peer discovery methods to ensure we persist peer data in the
|
|
76
|
+
// peer store, otherwise we can't dial by peer id without extra lookups
|
|
77
|
+
const getClosestPeers = helia.routing.getClosestPeers.bind(helia.routing);
|
|
78
|
+
helia.routing.getClosestPeers = async function* (cid, options) {
|
|
79
|
+
yield* forEach(getClosestPeers.call(helia.routing, cid, options), async (peer) => {
|
|
80
|
+
if (peer.router !== 'libp2p-router') {
|
|
81
|
+
// only need to do this for peers not found via the libp2p router
|
|
82
|
+
const info = toPeerInfo(peer);
|
|
83
|
+
await libp2p.peerStore.merge(info.id, info);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
};
|
|
51
87
|
}
|
|
52
88
|
try {
|
|
53
89
|
if (!helia.hasRouter('libp2p-router')) {
|
|
@@ -72,4 +108,10 @@ export function withLibp2p(helia, opts) {
|
|
|
72
108
|
// @ts-expect-error libp2p property is missing, even though we just defined it
|
|
73
109
|
return helia;
|
|
74
110
|
}
|
|
111
|
+
function toPeerInfo(peer) {
|
|
112
|
+
return {
|
|
113
|
+
id: peerIdFromCID(peer.id),
|
|
114
|
+
multiaddrs: peer.multiaddrs
|
|
115
|
+
};
|
|
116
|
+
}
|
|
75
117
|
//# sourceMappingURL=index.js.map
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,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A;gBACX,CAAC;YACH,CAAC;QACH,CAAC;QAED,IAAI,EAAE,KAAK,IAAI,EAAE;YACf,MAAM,MAAM,EAAE,IAAI,EAAE,CAAA;QACtB,CAAC;KACF,CAAA;IAED,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;IAErB,8EAA8E;IAC9E,OAAO,KAAK,CAAA;AACd,CAAC;AAED,SAAS,UAAU,CAAE,IAA0C;IAC7D,OAAO;QACL,EAAE,EAAE,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC;QAC1B,UAAU,EAAE,IAAI,CAAC,UAAU;KAC5B,CAAA;AACH,CAAC"}
|
package/dist/src/routing.js
CHANGED
|
@@ -4,7 +4,8 @@ import { withArrayBuffer } from 'uint8arrays/with-array-buffer';
|
|
|
4
4
|
function peerInfoToPeer(info) {
|
|
5
5
|
return {
|
|
6
6
|
...info,
|
|
7
|
-
id: info.id.toCID()
|
|
7
|
+
id: info.id.toCID(),
|
|
8
|
+
router: 'libp2p-router'
|
|
8
9
|
};
|
|
9
10
|
}
|
|
10
11
|
class Libp2pRouter {
|
|
@@ -21,7 +22,6 @@ class Libp2pRouter {
|
|
|
21
22
|
}
|
|
22
23
|
async *findProviders(cid, options) {
|
|
23
24
|
yield* map(this.libp2p.contentRouting.findProviders(cid, options), prov => ({
|
|
24
|
-
routing: this.name,
|
|
25
25
|
...peerInfoToPeer(prov)
|
|
26
26
|
}));
|
|
27
27
|
}
|
package/dist/src/routing.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routing.js","sourceRoot":"","sources":["../../src/routing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAK/D,SAAS,cAAc,CAAE,IAAc;IACrC,OAAO;QACL,GAAG,IAAI;QACP,EAAE,EAAE,IAAI,C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CAAC,GAAG,EAAE,KAAK,EAAE,OAAO,CAAC,CAAA;IAC3D,CAAC;IAED,KAAK,CAAC,GAAG,CAAE,GAAe,EAAE,OAAwB;QAClD,OAAO,eAAe,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAA;IAC5E,CAAC;IAED,KAAK,CAAC,QAAQ,CAAE,MAAW,EAAE,OAAwB;QACnD,OAAO,cAAc,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CAAC,CAAA;IAC/F,CAAC;IAED,KAAK,CAAC,CAAE,eAAe,CAAE,GAAe,EAAE,OAAwB;QAChE,KAAM,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,eAAe,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE,cAAc,CAAC,CAAA;IACpF,CAAC;IAED,QAAQ;QACN,OAAO,gBAAgB,CAAA;IACzB,CAAC;CACF;AAED,MAAM,UAAU,aAAa,CAAE,MAAc;IAC3C,OAAO,IAAI,YAAY,CAAC,MAAM,CAAC,CAAA;AACjC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helia/libp2p",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "libp2p functionality for Helia",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/ipfs/helia/tree/main/packages/libp2p#readme",
|
|
@@ -50,9 +50,9 @@
|
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"@chainsafe/libp2p-noise": "^17.0.0",
|
|
52
52
|
"@chainsafe/libp2p-yamux": "^8.0.1",
|
|
53
|
-
"@helia/delegated-routing-client": "^1.0.
|
|
53
|
+
"@helia/delegated-routing-client": "^1.0.4",
|
|
54
54
|
"@helia/delegated-routing-v1-http-api-client": "^9.0.0",
|
|
55
|
-
"@helia/interface": "^7.0.
|
|
55
|
+
"@helia/interface": "^7.0.3",
|
|
56
56
|
"@ipshipyard/libp2p-auto-tls": "^2.0.1",
|
|
57
57
|
"@libp2p/autonat": "^3.0.21",
|
|
58
58
|
"@libp2p/bootstrap": "^12.0.24",
|
|
@@ -74,6 +74,8 @@
|
|
|
74
74
|
"@libp2p/webrtc": "^6.0.24",
|
|
75
75
|
"@libp2p/websockets": "^10.1.14",
|
|
76
76
|
"@multiformats/dns": "^1.0.13",
|
|
77
|
+
"@multiformats/multiaddr": "^13.0.3",
|
|
78
|
+
"it-foreach": "^2.1.7",
|
|
77
79
|
"it-map": "^3.1.6",
|
|
78
80
|
"libp2p": "^3.3.3",
|
|
79
81
|
"multiformats": "^14.0.0",
|
package/src/index.ts
CHANGED
|
@@ -16,13 +16,17 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import { NotStartedError } from '@libp2p/interface'
|
|
19
|
+
import { peerIdFromCID } from '@libp2p/peer-id'
|
|
20
|
+
import forEach from 'it-foreach'
|
|
19
21
|
import { isLibp2p } from 'libp2p'
|
|
20
22
|
import { libp2pRouting } from './routing.ts'
|
|
21
23
|
import { createLibp2p } from './utils/libp2p.ts'
|
|
22
24
|
import type { DefaultLibp2pServices } from './utils/libp2p-defaults.ts'
|
|
23
25
|
import type { CreateLibp2pOptions } from './utils/libp2p.ts'
|
|
24
|
-
import type { Helia, HeliaMixin } from '@helia/interface'
|
|
25
|
-
import type { Libp2p, ServiceMap } from '@libp2p/interface'
|
|
26
|
+
import type { Helia, HeliaMixin, Peer } from '@helia/interface'
|
|
27
|
+
import type { Libp2p, PeerInfo, ServiceMap } from '@libp2p/interface'
|
|
28
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
29
|
+
import type { CID } from 'multiformats'
|
|
26
30
|
|
|
27
31
|
export { libp2pDefaults } from './utils/libp2p-defaults.ts'
|
|
28
32
|
export type { DefaultLibp2pServices } from './utils/libp2p-defaults.ts'
|
|
@@ -54,7 +58,7 @@ async function getLibp2p <H extends Helia, M extends ServiceMap = ServiceMap> (h
|
|
|
54
58
|
export function withLibp2p <H extends Helia, M extends ServiceMap = ServiceMap, L extends Libp2p = Libp2p<M>> (helia: H, opts: L): H & HeliaWithLibp2p<M>
|
|
55
59
|
export function withLibp2p <H extends Helia, M extends ServiceMap = ServiceMap> (helia: H, opts?: CreateLibp2pOptions<M>): H & HeliaWithLibp2p<M>
|
|
56
60
|
export function withLibp2p <H extends Helia, M extends ServiceMap = ServiceMap> (helia: H, opts?: CreateLibp2pOptions<M>): H & HeliaWithLibp2p {
|
|
57
|
-
let libp2p:
|
|
61
|
+
let libp2p: Libp2p
|
|
58
62
|
|
|
59
63
|
// add a getter that informs the user they need to start Helia
|
|
60
64
|
Object.defineProperty(helia, 'libp2p', {
|
|
@@ -74,6 +78,45 @@ export function withLibp2p <H extends Helia, M extends ServiceMap = ServiceMap>
|
|
|
74
78
|
start: async (helia) => {
|
|
75
79
|
if (libp2p == null) {
|
|
76
80
|
libp2p = await getLibp2p(helia, opts)
|
|
81
|
+
|
|
82
|
+
// override peer discovery methods to ensure we persist peer data in the
|
|
83
|
+
// peer store, otherwise we can't dial by peer id without extra lookups
|
|
84
|
+
const findProviders = helia.routing.findProviders.bind(helia.routing)
|
|
85
|
+
helia.routing.findProviders = async function * (cid, options): AsyncIterable<Peer> {
|
|
86
|
+
yield * forEach(findProviders.call(helia.routing, cid, options), async (peer) => {
|
|
87
|
+
if (peer.router !== 'libp2p-router') {
|
|
88
|
+
// only need to do this for peers not found via the libp2p router
|
|
89
|
+
const info = toPeerInfo(peer)
|
|
90
|
+
await libp2p.peerStore.merge(info.id, info)
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const findPeer = helia.routing.findPeer.bind(helia.routing)
|
|
96
|
+
helia.routing.findPeer = async function (cid, options): Promise<Peer> {
|
|
97
|
+
const peer = await findPeer(cid, options)
|
|
98
|
+
|
|
99
|
+
if (peer.router !== 'libp2p-router') {
|
|
100
|
+
// only need to do this for peers not found via the libp2p router
|
|
101
|
+
const info = toPeerInfo(peer)
|
|
102
|
+
await libp2p.peerStore.merge(info.id, info)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return peer
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// override peer discovery methods to ensure we persist peer data in the
|
|
109
|
+
// peer store, otherwise we can't dial by peer id without extra lookups
|
|
110
|
+
const getClosestPeers = helia.routing.getClosestPeers.bind(helia.routing)
|
|
111
|
+
helia.routing.getClosestPeers = async function * (cid, options): AsyncIterable<Peer> {
|
|
112
|
+
yield * forEach(getClosestPeers.call(helia.routing, cid, options), async (peer) => {
|
|
113
|
+
if (peer.router !== 'libp2p-router') {
|
|
114
|
+
// only need to do this for peers not found via the libp2p router
|
|
115
|
+
const info = toPeerInfo(peer)
|
|
116
|
+
await libp2p.peerStore.merge(info.id, info)
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
}
|
|
77
120
|
}
|
|
78
121
|
|
|
79
122
|
try {
|
|
@@ -102,3 +145,10 @@ export function withLibp2p <H extends Helia, M extends ServiceMap = ServiceMap>
|
|
|
102
145
|
// @ts-expect-error libp2p property is missing, even though we just defined it
|
|
103
146
|
return helia
|
|
104
147
|
}
|
|
148
|
+
|
|
149
|
+
function toPeerInfo (peer: { id: CID, multiaddrs: Multiaddr[] }): PeerInfo {
|
|
150
|
+
return {
|
|
151
|
+
id: peerIdFromCID(peer.id),
|
|
152
|
+
multiaddrs: peer.multiaddrs
|
|
153
|
+
}
|
|
154
|
+
}
|
package/src/routing.ts
CHANGED
|
@@ -8,7 +8,8 @@ import type { CID } from 'multiformats'
|
|
|
8
8
|
function peerInfoToPeer (info: PeerInfo): Peer {
|
|
9
9
|
return {
|
|
10
10
|
...info,
|
|
11
|
-
id: info.id.toCID()
|
|
11
|
+
id: info.id.toCID(),
|
|
12
|
+
router: 'libp2p-router'
|
|
12
13
|
}
|
|
13
14
|
}
|
|
14
15
|
|
|
@@ -30,7 +31,6 @@ class Libp2pRouter implements Router {
|
|
|
30
31
|
|
|
31
32
|
async * findProviders (cid: CID, options?: RoutingOptions): AsyncIterable<Provider> {
|
|
32
33
|
yield * map(this.libp2p.contentRouting.findProviders(cid, options), prov => ({
|
|
33
|
-
routing: this.name,
|
|
34
34
|
...peerInfoToPeer(prov)
|
|
35
35
|
}))
|
|
36
36
|
}
|