Compare commits
No commits in common. "fd2d66d2c4713d266b4b906e9b560310d0e9a3a1" and "fe6dac6e4a301122ec39437c1b79a4d4d7c07a0d" have entirely different histories.
fd2d66d2c4
...
fe6dac6e4a
|
@ -39,7 +39,6 @@
|
||||||
"libkmodule": "^0.2.53",
|
"libkmodule": "^0.2.53",
|
||||||
"libp2p": "^0.42.2",
|
"libp2p": "^0.42.2",
|
||||||
"multiformats": "^11.0.2",
|
"multiformats": "^11.0.2",
|
||||||
"p-defer": "^4.0.0",
|
|
||||||
"p-queue": "^7.3.4",
|
"p-queue": "^7.3.4",
|
||||||
"private-ip": "^3.0.0",
|
"private-ip": "^3.0.0",
|
||||||
"rewire": "^6.0.0",
|
"rewire": "^6.0.0",
|
||||||
|
|
45
src/index.ts
45
src/index.ts
|
@ -38,7 +38,6 @@ import { peerIdFromCID } from "@libp2p/peer-id";
|
||||||
import { bootstrap } from "@libp2p/bootstrap";
|
import { bootstrap } from "@libp2p/bootstrap";
|
||||||
import { IDBBlockstore } from "blockstore-idb";
|
import { IDBBlockstore } from "blockstore-idb";
|
||||||
import { IDBDatastore } from "datastore-idb";
|
import { IDBDatastore } from "datastore-idb";
|
||||||
import defer from "p-defer";
|
|
||||||
|
|
||||||
const basesByPrefix: { [prefix: string]: MultibaseDecoder<any> } = Object.keys(
|
const basesByPrefix: { [prefix: string]: MultibaseDecoder<any> } = Object.keys(
|
||||||
bases
|
bases
|
||||||
|
@ -50,8 +49,10 @@ const basesByPrefix: { [prefix: string]: MultibaseDecoder<any> } = Object.keys(
|
||||||
|
|
||||||
onmessage = handleMessage;
|
onmessage = handleMessage;
|
||||||
|
|
||||||
const moduleDefer = defer();
|
let moduleLoadedResolve: Function;
|
||||||
let activePeersDefer = defer();
|
let moduleLoaded: Promise<void> = new Promise((resolve) => {
|
||||||
|
moduleLoadedResolve = resolve;
|
||||||
|
});
|
||||||
|
|
||||||
let swarm;
|
let swarm;
|
||||||
let proxy: Proxy;
|
let proxy: Proxy;
|
||||||
|
@ -68,7 +69,6 @@ addHandler("stat", handleStat);
|
||||||
addHandler("ls", handleLs, { receiveUpdates: true });
|
addHandler("ls", handleLs, { receiveUpdates: true });
|
||||||
addHandler("cat", handleCat, { receiveUpdates: true });
|
addHandler("cat", handleCat, { receiveUpdates: true });
|
||||||
addHandler("ipnsResolve", handleIpnsResolve);
|
addHandler("ipnsResolve", handleIpnsResolve);
|
||||||
addHandler("getActivePeers", handleGetActivePeers);
|
|
||||||
|
|
||||||
async function handlePresentSeed() {
|
async function handlePresentSeed() {
|
||||||
swarm = createClient();
|
swarm = createClient();
|
||||||
|
@ -168,7 +168,7 @@ async function handlePresentSeed() {
|
||||||
await blockstore.open();
|
await blockstore.open();
|
||||||
await datastore.open();
|
await datastore.open();
|
||||||
|
|
||||||
const ipfs = await createHelia({
|
PeerManager.instance.ipfs = await createHelia({
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
blockstore,
|
blockstore,
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
@ -176,8 +176,6 @@ async function handlePresentSeed() {
|
||||||
libp2p,
|
libp2p,
|
||||||
});
|
});
|
||||||
|
|
||||||
PeerManager.instance.ipfs = ipfs;
|
|
||||||
|
|
||||||
proxy = new Proxy({
|
proxy = new Proxy({
|
||||||
swarm,
|
swarm,
|
||||||
listen: true,
|
listen: true,
|
||||||
|
@ -202,22 +200,12 @@ async function handlePresentSeed() {
|
||||||
await swarm.start();
|
await swarm.start();
|
||||||
await swarm.ready();
|
await swarm.ready();
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
fs = unixfs(ipfs);
|
fs = unixfs(PeerManager.instance.ipfs);
|
||||||
IPNS = ipns(ipfs as any, [dht(ipfs), pubsub(ipfs as any)]);
|
IPNS = ipns(PeerManager.instance.ipfs as any, [
|
||||||
|
dht(PeerManager.instance.ipfs),
|
||||||
ipfs.libp2p.addEventListener("peer:connect", () => {
|
pubsub(PeerManager.instance.ipfs as any),
|
||||||
if (ipfs.libp2p.getPeers().length > 0) {
|
]);
|
||||||
activePeersDefer.resolve();
|
moduleLoadedResolve();
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
ipfs.libp2p.addEventListener("peer:disconnect", () => {
|
|
||||||
if (ipfs.libp2p.getPeers().length === 0) {
|
|
||||||
activePeersDefer = defer();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
moduleDefer.resolve();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleStat(aq: ActiveQuery) {
|
async function handleStat(aq: ActiveQuery) {
|
||||||
|
@ -301,9 +289,6 @@ 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;
|
||||||
|
@ -330,14 +315,8 @@ async function handleIpnsResolve(aq: ActiveQuery) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleGetActivePeers(aq: ActiveQuery) {
|
|
||||||
await ready();
|
|
||||||
|
|
||||||
aq.respond(PeerManager.instance.ipfs.libp2p.getPeers());
|
|
||||||
}
|
|
||||||
|
|
||||||
async function ready() {
|
async function ready() {
|
||||||
await moduleDefer.promise;
|
await moduleLoaded;
|
||||||
await PeerManager.instance.ipfsReady;
|
await PeerManager.instance.ipfsReady;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@ import { fixed32, json, raw, uint } from "compact-encoding";
|
||||||
import { TcpSocketConnectOpts } from "net";
|
import { TcpSocketConnectOpts } from "net";
|
||||||
import { Helia } from "@helia/interface";
|
import { Helia } from "@helia/interface";
|
||||||
import { deserializeError } from "serialize-error";
|
import { deserializeError } from "serialize-error";
|
||||||
import defer from "p-defer";
|
|
||||||
import {
|
import {
|
||||||
CloseSocketRequest,
|
CloseSocketRequest,
|
||||||
ErrorSocketRequest,
|
ErrorSocketRequest,
|
||||||
|
@ -128,14 +127,14 @@ export default class PeerManager {
|
||||||
this._ipfs = value as Helia;
|
this._ipfs = value as Helia;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _ipfsReady?: Promise<unknown>;
|
private _ipfsReady?: Promise<void>;
|
||||||
private _ipfsResolve?: () => void;
|
private _ipfsResolve?: () => void;
|
||||||
|
|
||||||
get ipfsReady(): Promise<void> {
|
get ipfsReady(): Promise<void> {
|
||||||
if (!this._ipfsReady) {
|
if (!this._ipfsReady) {
|
||||||
let ipfsDefer = defer();
|
this._ipfsReady = new Promise((resolve) => {
|
||||||
this._ipfsReady = ipfsDefer.promise;
|
this._ipfsResolve = resolve;
|
||||||
this._ipfsResolve = ipfsDefer.resolve;
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._ipfsReady as Promise<any>;
|
return this._ipfsReady as Promise<any>;
|
||||||
|
|
Loading…
Reference in New Issue