*Wait until we have an active peer to try and do an IPNS lookup.
This commit is contained in:
parent
b9b8040b50
commit
cad93cbb21
28
src/index.ts
28
src/index.ts
|
@ -51,6 +51,7 @@ const basesByPrefix: { [prefix: string]: MultibaseDecoder<any> } = Object.keys(
|
||||||
onmessage = handleMessage;
|
onmessage = handleMessage;
|
||||||
|
|
||||||
const moduleDefer = defer();
|
const moduleDefer = defer();
|
||||||
|
let activePeersDefer = defer();
|
||||||
|
|
||||||
let swarm;
|
let swarm;
|
||||||
let proxy: Proxy;
|
let proxy: Proxy;
|
||||||
|
@ -166,7 +167,7 @@ async function handlePresentSeed() {
|
||||||
await blockstore.open();
|
await blockstore.open();
|
||||||
await datastore.open();
|
await datastore.open();
|
||||||
|
|
||||||
PeerManager.instance.ipfs = await createHelia({
|
const ipfs = await createHelia({
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
blockstore,
|
blockstore,
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
@ -174,6 +175,8 @@ async function handlePresentSeed() {
|
||||||
libp2p,
|
libp2p,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
PeerManager.instance.ipfs = ipfs;
|
||||||
|
|
||||||
proxy = new Proxy({
|
proxy = new Proxy({
|
||||||
swarm,
|
swarm,
|
||||||
listen: true,
|
listen: true,
|
||||||
|
@ -198,11 +201,21 @@ async function handlePresentSeed() {
|
||||||
await swarm.start();
|
await swarm.start();
|
||||||
await swarm.ready();
|
await swarm.ready();
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
fs = unixfs(PeerManager.instance.ipfs);
|
fs = unixfs(ipfs);
|
||||||
IPNS = ipns(PeerManager.instance.ipfs as any, [
|
IPNS = ipns(ipfs as any, [dht(ipfs), pubsub(ipfs as any)]);
|
||||||
dht(PeerManager.instance.ipfs),
|
|
||||||
pubsub(PeerManager.instance.ipfs as any),
|
ipfs.libp2p.addEventListener("peer:connect", () => {
|
||||||
]);
|
if (ipfs.libp2p.getPeers().length > 0) {
|
||||||
|
activePeersDefer.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ipfs.libp2p.addEventListener("peer:disconnect", () => {
|
||||||
|
if (ipfs.libp2p.getPeers().length === 0) {
|
||||||
|
activePeersDefer = defer();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
moduleDefer.resolve();
|
moduleDefer.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -287,6 +300,9 @@ async function handleCat(aq: ActiveQuery) {
|
||||||
|
|
||||||
async function handleIpnsResolve(aq: ActiveQuery) {
|
async function handleIpnsResolve(aq: ActiveQuery) {
|
||||||
await ready();
|
await ready();
|
||||||
|
|
||||||
|
await activePeersDefer.promise;
|
||||||
|
|
||||||
if (!aq.callerInput || !("cid" in aq.callerInput)) {
|
if (!aq.callerInput || !("cid" in aq.callerInput)) {
|
||||||
aq.reject("cid required");
|
aq.reject("cid required");
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue