From 5242c16367ee1d4307c1912cdd437296a94b635d Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 19 Dec 2022 12:37:59 -0500 Subject: [PATCH] *Update types to reflect relay refactoring and new plugin api *Clean up old code --- package.json | 13 ++++++-- src/config.ts | 43 ++++++++++++++++++++++++++ src/plugin.ts | 85 ++++++++++++++++++--------------------------------- src/ssl.ts | 18 +++++++++++ 4 files changed, 101 insertions(+), 58 deletions(-) create mode 100644 src/config.ts create mode 100644 src/ssl.ts diff --git a/package.json b/package.json index 7e1581e..0dba80b 100644 --- a/package.json +++ b/package.json @@ -5,9 +5,16 @@ "type": "module", "devDependencies": { "@lumeweb/cfg": "https://github.com/LumeWeb/bcfg.git", - "@types/node": "^18.7.16", + "@types/node": "^18.11.17", "express": "^4.18.1", - "loglevel": "^1.8.0", - "node-cache": "^5.1.2" + "node-cache": "^5.1.2", + "prettier": "^2.8.1" + }, + "dependencies": { + "@types/eventemitter2": "^4.1.0", + "arg": "^5.0.2", + "eventemitter2": "^6.4.9", + "micro-ed25519-hdkey": "^0.1.2", + "pino": "^8.8.0" } } diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..157ba84 --- /dev/null +++ b/src/config.ts @@ -0,0 +1,43 @@ +import type arg from "arg"; +declare class Config { + private module; + private data; + constructor(module: string); + inject(options: object): void; + load(): void; + openDir(dir: string): void; + open(file: string): void; + save(file: string, data: object): void; + set(key: string, value: any): void; + has(key: string): any; + private normalize; + get(key: string, fallback?: any): any; + typeOf( + key: string + ): + | "string" + | "number" + | "bigint" + | "boolean" + | "symbol" + | "undefined" + | "object" + | "function" + | "null"; + str(key: string, fallback?: any): any; + int(key: any, fallback?: any): any; + uint(key: any, fallback?: any): any; + float(key: any, fallback?: any): any; + ufloat(key: any, fallback?: any): any; + fixed(key: any, exp: any, fallback?: any): any; + ufixed(key: any, exp: any, fallback?: any): any; + bool(key: any, fallback?: any): any; + buf(key: string, fallback?: any, enc?: BufferEncoding): any; + array(key: string, fallback?: any): any; + obj(key: string, fallback?: any): any; + func(key: string, fallback?: any): any; + mb(key: string, fallback?: any): any; + parseArg(args: arg.Result): void; + parseEnv(env?: object): void; +} +export default Config; diff --git a/src/plugin.ts b/src/plugin.ts index 01df88a..06500a9 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,20 +1,12 @@ -import Config from "@lumeweb/cfg"; -import tls from "tls"; -import { Logger } from "loglevel"; +import type { EventEmitter2 } from "eventemitter2"; import { RPCMethod, RPCServer } from "./rpc.js"; -import { IndependentFileSmall, SavedSslData, SslData } from "./files.js"; -import type { express } from "express"; -// @ts-ignore -import type { Err } from "libskynet"; +import { Logger } from "pino"; +import SSLManager from "./ssl.js"; +import type { HDKey } from "micro-ed25519-hdkey"; +import Config from "./config.js"; export type PluginFunction = (api: PluginAPI) => Promise; -export type DnsProvider = (ipAddress: string, domain: string) => Promise; - -export type OverwriteDataFn = (newData: Uint8Array) => Promise; - -export type ReadDataFn = () => Promise<[Uint8Array, Err]>; - export interface Plugin { name: string; plugin: PluginFunction; @@ -22,46 +14,29 @@ export interface Plugin { default?: Plugin; } -export interface PluginAPI { - config: Config; - registerMethod: (methodName: string, method: RPCMethod) => void; - loadPlugin: (moduleName: string) => Promise; - getRpcServer: () => RPCServer; - ssl: { - setContext: (context: tls.SecureContext) => void; - getContext: () => tls.SecureContext; - getSaved: (retry: boolean) => Promise; - set: ( - cert: IndependentFileSmall | Uint8Array, - key: IndependentFileSmall | Uint8Array - ) => void; - get: () => SslData; - save: () => Promise; - setCheck(checker: () => Promise): void; - }; - appRouter: { - get: () => express.Router; - set: (newRouter: express.Router) => void; - reset: () => void; - }; - files: { - createIndependentFileSmall( - seed: Uint8Array, - userInode: string, - fileData: Uint8Array - ): Promise<[IndependentFileSmall, Err]>; - openIndependentFileSmall( - seed: Uint8Array, - userInode: string - ): Promise<[IndependentFileSmall, Err]>; - overwriteIndependentFileSmall( - file: IndependentFileSmall, - newData: Uint8Array - ): Promise; - }; - dns: { - setProvider(provider: DnsProvider): void; - }; - logger: Logger; - getSeed: () => Uint8Array; +declare class PluginAPI extends EventEmitter2 { + private _server; + constructor({ + config, + logger, + server, + swarm, + }: { + config: Config; + logger: Logger; + server: RPCServer; + swarm: any; + }); + private _swarm; + get swarm(): any; + private _config; + get config(): Config; + private _logger; + get logger(): Logger; + get rpcServer(): RPCServer; + get seed(): Uint8Array; + get identity(): HDKey; + get ssl(): SSLManager; + loadPlugin(moduleName: string): (moduleName: string) => Promise; + registerMethod(methodName: string, method: RPCMethod): void; } diff --git a/src/ssl.ts b/src/ssl.ts new file mode 100644 index 0000000..bed3131 --- /dev/null +++ b/src/ssl.ts @@ -0,0 +1,18 @@ +import type tls from "tls"; + +export type SSLManagerRenewHandler = (domain: string) => Promise; +declare class SSLManager { + private _context?; + private _key?; + private _cert?; + private _domain; + private _renewHandler?; + constructor(domain: string); + get context(): tls.SecureContext; + set privateKey(key: Buffer); + set cert(cert: Buffer); + private _maybeUpdateContext; + renew(): Promise; + get enabled(): any; +} +export default SSLManager;