Compare commits

...

8 Commits

6 changed files with 41 additions and 57 deletions

62
package-lock.json generated
View File

@ -30,15 +30,14 @@
"loady": "github:LumeWeb/loady",
"msgpackr": "^1.8.5",
"node-cache": "^5.1.2",
"node-cron": "^3.0.2",
"node-fetch": "^2.6.9",
"p-defer": "^4.0.0",
"p-timeout": "git+https://git.lumeweb.com/LumeWeb/p-timeout.git",
"pino": "^8.11.0",
"pino-pretty": "^9.4.0",
"promise-retry": "^2.0.1",
"protomux": "^3.4.1",
"protomux-rpc": "^1.3.0",
"random-access-memory": "^4.1.0",
"random-key": "^0.3.2",
"slugify": "^1.6.6",
"sodium-universal": "^4.0.0"
@ -4336,14 +4335,6 @@
"node": ">=8"
}
},
"node_modules/is-options": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/is-options/-/is-options-1.0.2.tgz",
"integrity": "sha512-u+Ai74c8Q74aS8BuHwPdI1jptGOT1FQXgCq8/zv0xRuE+wRgSMEJLj8lVO8Zp9BeGb29BXY6AsNPinfqjkr7Fg==",
"dependencies": {
"b4a": "^1.1.1"
}
},
"node_modules/is-plain-obj": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-1.1.0.tgz",
@ -5405,17 +5396,6 @@
"node": ">= 8.0.0"
}
},
"node_modules/node-cron": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.2.tgz",
"integrity": "sha512-iP8l0yGlNpE0e6q1o185yOApANRe47UPbLf4YxfbiNHt/RU5eBcGB/e0oudruheSf+LQeDMezqC5BVAb5wwRcQ==",
"dependencies": {
"uuid": "8.3.2"
},
"engines": {
"node": ">=6.0.0"
}
},
"node_modules/node-domexception": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
@ -8642,6 +8622,17 @@
"resolved": "https://registry.npmjs.org/ordered-object/-/ordered-object-0.2.3.tgz",
"integrity": "sha512-UKBtJiO7PsKqAAenewZ/moHQIRbcjZ4HE0J/+RyzgnpCTIn5ZLe3N2izno1kViTCXtHB4xuewjPgYLEiuS6t5A=="
},
"node_modules/p-defer": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/p-defer/-/p-defer-4.0.0.tgz",
"integrity": "sha512-Vb3QRvQ0Y5XnF40ZUWW7JfLogicVh/EnA5gBIvKDJoYpeI82+1E3AlB9yOcKFS0AhHrWVnAQO39fbR0G99IVEQ==",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/p-each-series": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/p-each-series/-/p-each-series-3.0.0.tgz",
@ -9362,27 +9353,6 @@
"node": ">=8"
}
},
"node_modules/random-access-memory": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/random-access-memory/-/random-access-memory-4.1.0.tgz",
"integrity": "sha512-vVt75UQpWcaZWlysSmk7isqdhiV10WCovxuKZmBKg5zSsiEXIclJmAlIsMr8usYqfFmXzOGIonWOAyR5u3jhXQ==",
"dependencies": {
"b4a": "^1.1.0",
"inherits": "^2.0.3",
"is-options": "^1.0.1",
"random-access-storage": "^1.1.1"
}
},
"node_modules/random-access-storage": {
"version": "1.4.3",
"resolved": "https://registry.npmjs.org/random-access-storage/-/random-access-storage-1.4.3.tgz",
"integrity": "sha512-D5e2iIC5dNENWyBxsjhEnNOMCwZZ64TARK6dyMN+3g4OTC4MJxyjh9hKLjTGoNhDOPrgjI+YlFEHFnrp/cSnzQ==",
"dependencies": {
"events": "^3.3.0",
"inherits": "^2.0.3",
"queue-tick": "^1.0.0"
}
},
"node_modules/random-key": {
"version": "0.3.2",
"resolved": "https://registry.npmjs.org/random-key/-/random-key-0.3.2.tgz",
@ -10955,14 +10925,6 @@
"integrity": "sha512-mLs5zAK+ctllYBj+iAQvlDCwoxU/WDOUaJkcFudeiAX6OajC6BKXJUa9a+tbtkC11dz2Ufb7h0lyvIOVn4LADA==",
"dev": true
},
"node_modules/uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/validate-npm-package-license": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz",

View File

@ -42,6 +42,7 @@
"msgpackr": "^1.8.5",
"node-cache": "^5.1.2",
"node-fetch": "^2.6.9",
"p-defer": "^4.0.0",
"p-timeout": "git+https://git.lumeweb.com/LumeWeb/p-timeout.git",
"pino": "^8.11.0",
"pino-pretty": "^9.4.0",

View File

@ -7,16 +7,24 @@ import { errorExit } from "../lib/error.js";
import stringify from "json-stable-stringify";
import {
getRpcServer,
RPC_PROTOCOL_ID,
RPC_PROTOCOL_SYMBOL,
setupStream,
} from "./rpc/server.js";
import { get as getSwarm, SecretStream } from "./swarm.js";
import b4a from "b4a";
// @ts-ignore
import Protomux from "protomux";
export async function start() {
getSwarm().on("connection", (stream: SecretStream) =>
getRpcServer().setup(stream)
);
getSwarm().on("connection", (stream: SecretStream) => {
Protomux.from(stream).pair(
{ protocol: "protomux-rpc", id: RPC_PROTOCOL_ID },
async () => {
getRpcServer().setup(stream);
}
);
});
}
export async function getRpcByPeer(peer: Buffer | string) {
@ -30,15 +38,16 @@ export async function getRpcByPeer(peer: Buffer | string) {
}
return new Promise((resolve) => {
const listener = () => {};
swarm.on("connection", (peer: any, info: any) => {
const listener = (peer: any, info: any) => {
if (info.publicKey.toString("hex") !== peer.toString("hex")) {
return;
}
swarm.removeListener("connection", listener);
resolve(setupStream(peer));
});
};
swarm.on("connection", listener);
swarm.joinPeer(peer);
});

View File

@ -21,7 +21,7 @@ import jsonStringify from "json-stringify-deterministic";
const sodium = require("sodium-universal");
let server: RPCServer;
const RPC_PROTOCOL_ID = b4a.from("lumeweb");
export const RPC_PROTOCOL_ID = b4a.from("lumeweb");
export const RPC_PROTOCOL_SYMBOL = Symbol.for(RPC_PROTOCOL_ID.toString());
export function getRpcServer(): RPCServer {

View File

@ -69,6 +69,9 @@ export class ProtocolManager {
this._swarm = swarm;
this._swarm.on("connection", (peer: any) => {
if (!peer.userData) {
peer.userData = null;
}
for (const protocol of this._protocols) {
Protomux.from(peer).pair(
{ protocol: protocol[0] },

View File

@ -1,8 +1,15 @@
import { Plugin, PluginAPI } from "@lumeweb/interface-relay";
import defer from "p-defer";
const plugin: Plugin = {
name: "core",
async plugin(api: PluginAPI): Promise<void> {
const pluginsLoaded = defer();
api.once("core.pluginsLoaded", () => {
pluginsLoaded.resolve();
});
api.registerMethod("ping", {
cacheable: false,
async handler(): Promise<any> {
@ -13,6 +20,8 @@ const plugin: Plugin = {
api.registerMethod("get_methods", {
cacheable: false,
async handler(): Promise<any> {
await pluginsLoaded.promise;
return api.rpcServer.getMethods();
},
});