chore: move libp2p utils from @waku/core to new package (#1056)

* add: libp2p-utils

* address: comments
This commit is contained in:
Danish Arora 2022-12-06 15:46:10 +05:30 committed by GitHub
parent a15436d786
commit 03e00225bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 249 additions and 56 deletions

52
package-lock.json generated
View File

@ -8,6 +8,7 @@
"workspaces": [
"packages/byte-utils",
"packages/interfaces",
"packages/libp2p-utils",
"packages/core",
"packages/enr",
"packages/dns-discovery",
@ -3210,6 +3211,10 @@
"resolved": "packages/interfaces",
"link": true
},
"node_modules/@waku/libp2p-utils": {
"resolved": "packages/libp2p-utils",
"link": true
},
"node_modules/@waku/message-encryption": {
"resolved": "packages/message-encryption",
"link": true
@ -17457,6 +17462,32 @@
"node": ">=16"
}
},
"packages/libp2p-utils": {
"name": "@waku/libp2p-utils",
"version": "0.0.2",
"license": "MIT OR Apache-2.0",
"devDependencies": {
"@rollup/plugin-commonjs": "^22.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.3.0",
"@typescript-eslint/eslint-plugin": "^5.8.1",
"@typescript-eslint/parser": "^5.8.1",
"cspell": "^5.14.0",
"eslint": "^8.6.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^4.0.2",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^4.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.1.1",
"rollup": "^2.75.0",
"typescript": "^4.6.3"
},
"engines": {
"node": ">=16"
}
},
"packages/message-encryption": {
"name": "@waku/message-encryption",
"version": "0.0.4",
@ -20282,6 +20313,27 @@
"typescript": "^4.6.3"
}
},
"@waku/libp2p-utils": {
"version": "file:packages/libp2p-utils",
"requires": {
"@rollup/plugin-commonjs": "^22.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.3.0",
"@typescript-eslint/eslint-plugin": "^5.8.1",
"@typescript-eslint/parser": "^5.8.1",
"cspell": "^5.14.0",
"eslint": "^8.6.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^4.0.2",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^4.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.1.1",
"rollup": "^2.75.0",
"typescript": "^4.6.3"
}
},
"@waku/message-encryption": {
"version": "file:packages/message-encryption",
"requires": {

View File

@ -5,6 +5,7 @@
"workspaces": [
"packages/byte-utils",
"packages/interfaces",
"packages/libp2p-utils",
"packages/core",
"packages/enr",
"packages/dns-discovery",

View File

@ -1,17 +0,0 @@
import { PeerInfo } from "@libp2p/interface-peer-info";
import { peerIdFromString } from "@libp2p/peer-id";
import { Multiaddr } from "@multiformats/multiaddr";
export function multiaddrsToPeerInfo(mas: Multiaddr[]): PeerInfo[] {
return mas
.map((ma) => {
const peerIdStr = ma.getPeerId();
const protocols: string[] = [];
return {
id: peerIdStr ? peerIdFromString(peerIdStr) : null,
multiaddrs: [ma.decapsulateCode(421)],
protocols,
};
})
.filter((peerInfo): peerInfo is PeerInfo => peerInfo.id !== null);
}

View File

@ -1,24 +0,0 @@
import { Connection } from "@libp2p/interface-connection";
export function selectConnection(
connections: Connection[]
): Connection | undefined {
if (!connections.length) return;
if (connections.length === 1) return connections[0];
let latestConnection: Connection | undefined;
connections.forEach((connection) => {
if (connection.stat.status === "OPEN") {
if (!latestConnection) {
latestConnection = connection;
} else if (
connection.stat.timeline.open > latestConnection.stat.timeline.open
) {
latestConnection = connection;
}
}
});
return latestConnection;
}

View File

@ -13,6 +13,12 @@ import type {
Message,
ProtocolOptions,
} from "@waku/interfaces";
import {
getPeersForProtocol,
selectConnection,
selectPeerForProtocol,
selectRandomPeer,
} from "@waku/libp2p-utils";
import debug from "debug";
import all from "it-all";
import * as lp from "it-length-prefixed";
@ -21,12 +27,6 @@ import { pipe } from "it-pipe";
import { WakuMessage as WakuMessageProto } from "../../proto/message.js";
import { DefaultPubSubTopic } from "../constants.js";
import { groupByContentTopic } from "../group_by.js";
import { selectConnection } from "../select_connection.js";
import {
getPeersForProtocol,
selectPeerForProtocol,
selectRandomPeer,
} from "../select_peer.js";
import { toProtoMessage } from "../to_proto_message.js";
import { ContentFilter, FilterRPC } from "./filter_rpc.js";

View File

@ -9,6 +9,12 @@ import type {
ProtocolOptions,
SendResult,
} from "@waku/interfaces";
import {
getPeersForProtocol,
selectConnection,
selectPeerForProtocol,
selectRandomPeer,
} from "@waku/libp2p-utils";
import debug from "debug";
import all from "it-all";
import * as lp from "it-length-prefixed";
@ -17,12 +23,6 @@ import { Uint8ArrayList } from "uint8arraylist";
import { PushResponse } from "../../proto/light_push.js";
import { DefaultPubSubTopic } from "../constants.js";
import { selectConnection } from "../select_connection.js";
import {
getPeersForProtocol,
selectPeerForProtocol,
selectRandomPeer,
} from "../select_peer.js";
import { PushRPC } from "./push_rpc.js";

View File

@ -11,6 +11,11 @@ import {
Index,
Store,
} from "@waku/interfaces";
import {
getPeersForProtocol,
selectConnection,
selectPeerForProtocol,
} from "@waku/libp2p-utils";
import debug from "debug";
import all from "it-all";
import * as lp from "it-length-prefixed";
@ -19,8 +24,6 @@ import { Uint8ArrayList } from "uint8arraylist";
import * as proto from "../../proto/store.js";
import { DefaultPubSubTopic } from "../constants.js";
import { selectConnection } from "../select_connection.js";
import { getPeersForProtocol, selectPeerForProtocol } from "../select_peer.js";
import { toProtoMessage } from "../to_proto_message.js";
import { HistoryRPC, PageDirection, Params } from "./history_rpc.js";

View File

@ -0,0 +1,6 @@
module.exports = {
parserOptions: {
tsconfigRootDir: __dirname,
project: "./tsconfig.dev.json",
},
};

View File

@ -0,0 +1,4 @@
build
bundle
dist
node_modules

View File

@ -0,0 +1,93 @@
{
"name": "@waku/libp2p-utils",
"version": "0.0.2",
"description": "Utilities to help with libp2p",
"types": "./dist/index.d.ts",
"module": "./dist/index.js",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
"type": "module",
"author": "Waku Team",
"homepage": "https://github.com/waku-org/js-waku/tree/master/packages/libp2p-utils#readme",
"repository": {
"type": "git",
"url": "https://github.com/waku-org/js-waku.git"
},
"bugs": {
"url": "https://github.com/waku-org/js-waku/issues"
},
"license": "MIT OR Apache-2.0",
"keywords": [
"waku",
"decentralized",
"secure",
"communication",
"web3",
"ethereum",
"dapps",
"privacy"
],
"scripts": {
"build": "run-s build:**",
"build:esm": "tsc",
"build:bundle": "rollup --config rollup.config.js",
"fix": "run-s fix:*",
"fix:prettier": "prettier . --write",
"fix:lint": "eslint src --ext .ts --ext .cjs --fix",
"check": "run-s check:*",
"check:lint": "eslint src --ext .ts",
"check:prettier": "prettier . --list-different",
"check:spelling": "cspell \"{README.md,src/**/*.ts}\"",
"check:tsc": "tsc -p tsconfig.dev.json",
"prepublish": "npm run build",
"reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build"
},
"engines": {
"node": ">=16"
},
"dependencies": {
"@multiformats/multiaddr": "^11.0.6",
"@libp2p/interface-peer-info": "^1.0.1",
"@libp2p/peer-id": "^1.1.10",
"@libp2p/interface-connection": "^3.0.3",
"@libp2p/interface-peer-id": "^1.0.6",
"@libp2p/interface-peer-store": "^1.2.3"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^22.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.3.0",
"@typescript-eslint/eslint-plugin": "^5.8.1",
"@typescript-eslint/parser": "^5.8.1",
"cspell": "^5.14.0",
"eslint": "^8.6.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^4.0.2",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^4.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.1.1",
"rollup": "^2.75.0",
"typescript": "^4.6.3"
},
"typedoc": {
"entryPoint": "./src/index.ts"
},
"files": [
"dist",
"bundle",
"src/*.ts",
"src/lib/**/*.ts",
"src/proto/**/*.ts",
"!**/*.spec.*",
"!**/*.json",
"CHANGELOG.md",
"LICENSE",
"README.md"
]
}

View File

@ -0,0 +1,21 @@
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import { nodeResolve } from "@rollup/plugin-node-resolve";
export default {
input: {
index: "dist/index.js",
},
output: {
dir: "bundle",
format: "esm",
},
plugins: [
commonjs(),
json(),
nodeResolve({
browser: true,
preferBuiltins: false,
}),
],
};

View File

@ -1,8 +1,12 @@
import type { Connection } from "@libp2p/interface-connection";
import type { PeerId } from "@libp2p/interface-peer-id";
import type { PeerInfo } from "@libp2p/interface-peer-info";
import type { Peer, PeerStore } from "@libp2p/interface-peer-store";
import { peerIdFromString } from "@libp2p/peer-id";
import type { Multiaddr } from "@multiformats/multiaddr";
import debug from "debug";
const log = debug("waku:select-peer");
const log = debug("waku:libp2p-utils");
/**
* Returns a pseudo-random peer that supports the given protocol.
@ -75,3 +79,40 @@ export async function selectPeerForProtocol(
return { peer, protocol };
}
export function multiaddrsToPeerInfo(mas: Multiaddr[]): PeerInfo[] {
return mas
.map((ma) => {
const peerIdStr = ma.getPeerId();
const protocols: string[] = [];
return {
id: peerIdStr ? peerIdFromString(peerIdStr) : null,
multiaddrs: [ma.decapsulateCode(421)],
protocols,
};
})
.filter((peerInfo): peerInfo is PeerInfo => peerInfo.id !== null);
}
export function selectConnection(
connections: Connection[]
): Connection | undefined {
if (!connections.length) return;
if (connections.length === 1) return connections[0];
let latestConnection: Connection | undefined;
connections.forEach((connection) => {
if (connection.stat.status === "OPEN") {
if (!latestConnection) {
latestConnection = connection;
} else if (
connection.stat.timeline.open > latestConnection.stat.timeline.open
) {
latestConnection = connection;
}
}
});
return latestConnection;
}

View File

@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.dev"
}

View File

@ -0,0 +1,10 @@
{
"extends": "../../tsconfig",
"compilerOptions": {
"outDir": "dist/",
"rootDir": "src",
"tsBuildInfoFile": "dist/.tsbuildinfo"
},
"include": ["src"],
"exclude": ["src/**/*.spec.ts", "src/test_utils"]
}