Merge remote-tracking branch 'origin/wip' into develop
This commit is contained in:
commit
6964c6bed2
|
@ -7,16 +7,24 @@ import { errorExit } from "../lib/error.js";
|
||||||
import stringify from "json-stable-stringify";
|
import stringify from "json-stable-stringify";
|
||||||
import {
|
import {
|
||||||
getRpcServer,
|
getRpcServer,
|
||||||
|
RPC_PROTOCOL_ID,
|
||||||
RPC_PROTOCOL_SYMBOL,
|
RPC_PROTOCOL_SYMBOL,
|
||||||
setupStream,
|
setupStream,
|
||||||
} from "./rpc/server.js";
|
} from "./rpc/server.js";
|
||||||
import { get as getSwarm, SecretStream } from "./swarm.js";
|
import { get as getSwarm, SecretStream } from "./swarm.js";
|
||||||
import b4a from "b4a";
|
import b4a from "b4a";
|
||||||
|
// @ts-ignore
|
||||||
|
import Protomux from "protomux";
|
||||||
|
|
||||||
export async function start() {
|
export async function start() {
|
||||||
getSwarm().on("connection", (stream: SecretStream) =>
|
getSwarm().on("connection", (stream: SecretStream) => {
|
||||||
getRpcServer().setup(stream)
|
Protomux.from(stream).pair(
|
||||||
);
|
{ protocol: "protomux-rpc", id: RPC_PROTOCOL_ID },
|
||||||
|
async () => {
|
||||||
|
getRpcServer().setup(stream);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getRpcByPeer(peer: Buffer | string) {
|
export async function getRpcByPeer(peer: Buffer | string) {
|
||||||
|
@ -30,15 +38,16 @@ export async function getRpcByPeer(peer: Buffer | string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const listener = () => {};
|
const listener = (peer: any, info: any) => {
|
||||||
swarm.on("connection", (peer: any, info: any) => {
|
|
||||||
if (info.publicKey.toString("hex") !== peer.toString("hex")) {
|
if (info.publicKey.toString("hex") !== peer.toString("hex")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
swarm.removeListener("connection", listener);
|
swarm.removeListener("connection", listener);
|
||||||
|
|
||||||
resolve(setupStream(peer));
|
resolve(setupStream(peer));
|
||||||
});
|
};
|
||||||
|
|
||||||
|
swarm.on("connection", listener);
|
||||||
|
|
||||||
swarm.joinPeer(peer);
|
swarm.joinPeer(peer);
|
||||||
});
|
});
|
||||||
|
|
|
@ -21,7 +21,7 @@ import jsonStringify from "json-stringify-deterministic";
|
||||||
const sodium = require("sodium-universal");
|
const sodium = require("sodium-universal");
|
||||||
let server: RPCServer;
|
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 const RPC_PROTOCOL_SYMBOL = Symbol.for(RPC_PROTOCOL_ID.toString());
|
||||||
|
|
||||||
export function getRpcServer(): RPCServer {
|
export function getRpcServer(): RPCServer {
|
||||||
|
|
|
@ -69,6 +69,9 @@ export class ProtocolManager {
|
||||||
this._swarm = swarm;
|
this._swarm = swarm;
|
||||||
|
|
||||||
this._swarm.on("connection", (peer: any) => {
|
this._swarm.on("connection", (peer: any) => {
|
||||||
|
if (!peer.userData) {
|
||||||
|
peer.userData = null;
|
||||||
|
}
|
||||||
for (const protocol of this._protocols) {
|
for (const protocol of this._protocols) {
|
||||||
Protomux.from(peer).pair(
|
Protomux.from(peer).pair(
|
||||||
{ protocol: protocol[0] },
|
{ protocol: protocol[0] },
|
||||||
|
|
|
@ -1,8 +1,17 @@
|
||||||
import { Plugin, PluginAPI } from "@lumeweb/interface-relay";
|
import { Plugin, PluginAPI } from "@lumeweb/interface-relay";
|
||||||
|
|
||||||
|
let pluginsLoadedResolve: () => void;
|
||||||
|
let pluginsLoadedPromise = new Promise<void>((resolve) => {
|
||||||
|
pluginsLoadedResolve = resolve;
|
||||||
|
});
|
||||||
|
|
||||||
const plugin: Plugin = {
|
const plugin: Plugin = {
|
||||||
name: "core",
|
name: "core",
|
||||||
async plugin(api: PluginAPI): Promise<void> {
|
async plugin(api: PluginAPI): Promise<void> {
|
||||||
|
api.once("core.pluginsLoaded", () => {
|
||||||
|
pluginsLoadedResolve();
|
||||||
|
});
|
||||||
|
|
||||||
api.registerMethod("ping", {
|
api.registerMethod("ping", {
|
||||||
cacheable: false,
|
cacheable: false,
|
||||||
async handler(): Promise<any> {
|
async handler(): Promise<any> {
|
||||||
|
@ -13,6 +22,8 @@ const plugin: Plugin = {
|
||||||
api.registerMethod("get_methods", {
|
api.registerMethod("get_methods", {
|
||||||
cacheable: false,
|
cacheable: false,
|
||||||
async handler(): Promise<any> {
|
async handler(): Promise<any> {
|
||||||
|
await pluginsLoadedPromise;
|
||||||
|
|
||||||
return api.rpcServer.getMethods();
|
return api.rpcServer.getMethods();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue