Compare commits

..

7 Commits

Author SHA1 Message Date
Derrick Hammer da14bac9b2
*Update types 2023-01-07 23:42:14 -05:00
Derrick Hammer 8d52e40f20
*Add new utilities on plugin api
*add access to compact-encoding via binaryEncoding
*add access to b4a via bufferEncoding
*add crypto utility with createHash helper
2023-01-07 23:34:06 -05:00
Derrick Hammer 72c663795a
*Update relay-types 2023-01-07 23:32:33 -05:00
Derrick Hammer 74c20c6042
*Don't use getRpcServer directly 2023-01-07 23:04:36 -05:00
Derrick Hammer ce23f0a7b8
*Refactor how core plugins are loaded 2023-01-07 23:02:13 -05:00
Derrick Hammer 5c85ad2962
*Move core plugins outside modules folder 2023-01-07 22:58:06 -05:00
Derrick Hammer f8a698c656
*Remove unneeded import 2023-01-07 22:56:16 -05:00
8 changed files with 60 additions and 15 deletions

View File

@ -8,9 +8,6 @@ import path from "path";
import type { Logger } from "pino"; import type { Logger } from "pino";
import { getHDKey, getSeed } from "../lib/seed.js"; import { getHDKey, getSeed } from "../lib/seed.js";
import pluginRpc from "./plugins/rpc";
import pluginCore from "./plugins/core";
import pluginDht from "./plugins/dht";
import type Config from "@lumeweb/cfg"; import type Config from "@lumeweb/cfg";
import EventEmitter2 from "eventemitter2"; import EventEmitter2 from "eventemitter2";
import log from "../log.js"; import log from "../log.js";
@ -21,6 +18,8 @@ import {
} from "./swarm.js"; } from "./swarm.js";
import { get as getSSl, SSLManager } from "./ssl.js"; import { get as getSSl, SSLManager } from "./ssl.js";
import type { HDKey } from "micro-ed25519-hdkey"; import type { HDKey } from "micro-ed25519-hdkey";
import corePlugins from "../plugins";
import Util from "./plugin/util";
let pluginAPIManager: PluginAPIManager; let pluginAPIManager: PluginAPIManager;
let pluginAPI: PluginAPI; let pluginAPI: PluginAPI;
@ -53,6 +52,12 @@ class PluginAPI extends EventEmitter2 {
this._swarm = swarm; this._swarm = swarm;
} }
private _util: Util = new Util();
get util(): Util {
return this._util;
}
private _swarm: any; private _swarm: any;
get swarm(): any { get swarm(): any {
@ -228,9 +233,9 @@ export function getPluginAPIManager(): PluginAPIManager {
export async function loadPlugins() { export async function loadPlugins() {
const apiManager = getPluginAPIManager(); const apiManager = getPluginAPIManager();
apiManager.loadPluginInstance(pluginCore); for (const plugin of corePlugins) {
apiManager.loadPluginInstance(pluginRpc); await apiManager.loadPluginInstance(plugin);
apiManager.loadPluginInstance(pluginDht); }
for (const plugin of [...new Set(config.array("plugins", []))] as []) { for (const plugin of [...new Set(config.array("plugins", []))] as []) {
await apiManager.loadPlugin(plugin); await apiManager.loadPlugin(plugin);

View File

@ -0,0 +1,19 @@
import Crypto from "./util/crypto";
import b4a from "b4a";
// @ts-ignore
import c from "compact-encoding";
export default class Util {
private _crypto: Crypto = new Crypto();
get crypto(): Crypto {
return this._crypto;
}
get bufferEncoding(): typeof b4a {
return b4a;
}
get binaryEncoding(): typeof c {
return c;
}
}

View File

@ -0,0 +1,14 @@
// @ts-ignore
import sodium from "sodium-universal";
import { getPluginAPI } from "../../plugin";
export default class Crypto {
createHash(data: string): Buffer {
const b4a = getPluginAPI().util.bufferEncoding;
const buffer = b4a.from(data);
let hash = b4a.allocUnsafe(32) as Buffer;
sodium.crypto_generichash(hash, buffer);
return hash;
}
}

View File

@ -1,5 +1,4 @@
import { Plugin, PluginAPI } from "@lumeweb/relay-types"; import { Plugin, PluginAPI } from "@lumeweb/relay-types";
import { getRpcServer } from "../rpc/server";
const plugin: Plugin = { const plugin: Plugin = {
name: "core", name: "core",

View File

@ -1,5 +1,4 @@
import { Plugin, PluginAPI } from "@lumeweb/relay-types"; import { Plugin, PluginAPI } from "@lumeweb/relay-types";
import { getRpcServer } from "../rpc/server";
import b4a from "b4a"; import b4a from "b4a";
const plugin: Plugin = { const plugin: Plugin = {

7
src/plugins/index.ts Normal file
View File

@ -0,0 +1,7 @@
import core from "./core";
import rpc from "./rpc";
import dht from "./dht";
const corePlugins = [core, dht, rpc];
export default corePlugins;

View File

@ -1,4 +1,3 @@
import { getRpcServer } from "../rpc/server";
import { import {
Plugin, Plugin,
PluginAPI, PluginAPI,
@ -8,11 +7,13 @@ import {
RPCRequest, RPCRequest,
RPCResponse, RPCResponse,
} from "@lumeweb/relay-types"; } from "@lumeweb/relay-types";
import { getRpcByPeer } from "../rpc"; import { getRpcByPeer } from "../modules/rpc";
import { get as getSwarm, LUMEWEB_TOPIC_HASH } from "../swarm"; import { get as getSwarm, LUMEWEB_TOPIC_HASH } from "../modules/swarm";
import b4a from "b4a"; import b4a from "b4a";
import pTimeout, { ClearablePromise } from "p-timeout"; import pTimeout, { ClearablePromise } from "p-timeout";
let api: PluginAPI;
async function broadcastRequest( async function broadcastRequest(
request: RPCRequest, request: RPCRequest,
relays: string[], relays: string[],
@ -28,7 +29,7 @@ async function broadcastRequest(
for (const relay of relays) { for (const relay of relays) {
let req; let req;
if (b4a.equals(b4a.from(relay, "hex"), getSwarm().keyPair.publicKey)) { if (b4a.equals(b4a.from(relay, "hex"), getSwarm().keyPair.publicKey)) {
req = getRpcServer().handleRequest(request); req = api.rpcServer.handleRequest(request);
} else { } else {
req = makeRequest(relay); req = makeRequest(relay);
} }
@ -47,7 +48,8 @@ async function broadcastRequest(
const plugin: Plugin = { const plugin: Plugin = {
name: "rpc", name: "rpc",
async plugin(api: PluginAPI): Promise<void> { async plugin(_api: PluginAPI): Promise<void> {
api = _api;
if (api.config.bool("cache")) { if (api.config.bool("cache")) {
api.registerMethod("get_cached_item", { api.registerMethod("get_cached_item", {
cacheable: false, cacheable: false,

View File

@ -662,7 +662,7 @@ __metadata:
"@lumeweb/relay-types@https://git.lumeweb.com/LumeWeb/relay-types.git": "@lumeweb/relay-types@https://git.lumeweb.com/LumeWeb/relay-types.git":
version: 0.1.0 version: 0.1.0
resolution: "@lumeweb/relay-types@https://git.lumeweb.com/LumeWeb/relay-types.git#commit=0d6219837a743543f97eaf391895a6608e0fb7d5" resolution: "@lumeweb/relay-types@https://git.lumeweb.com/LumeWeb/relay-types.git#commit=4e460a182d93f6faf04f0d6bdd1c0a48c2eb0c71"
dependencies: dependencies:
"@lumeweb/dht-cache": "https://git.lumeweb.com/LumeWeb/dht-cache.git" "@lumeweb/dht-cache": "https://git.lumeweb.com/LumeWeb/dht-cache.git"
"@types/eventemitter2": "npm:^4.1.0" "@types/eventemitter2": "npm:^4.1.0"
@ -670,7 +670,7 @@ __metadata:
eventemitter2: "npm:^6.4.9" eventemitter2: "npm:^6.4.9"
micro-ed25519-hdkey: "npm:^0.1.2" micro-ed25519-hdkey: "npm:^0.1.2"
pino: "npm:^8.8.0" pino: "npm:^8.8.0"
checksum: b66513fa0735920f5df8d1f5c2325756293c68d63a7252ee7daf3740b3511c8981825c11e6bd345517769b28741f50045c3136a6e1aa1de2adbb408a12f37085 checksum: 85dc1fd5e15d84c219b220891204e3639f9ef544c61d05ce6dec3a0b542237b22c9f364b47b92c2be8bdc3159d5a3faddb46733dbdabc0c9823c018fa6f2bb4c
languageName: node languageName: node
linkType: hard linkType: hard