*Update types to reflect relay refactoring and new plugin api

*Clean up old code
This commit is contained in:
Derrick Hammer 2022-12-19 12:37:59 -05:00
parent 2d7bed64d5
commit 5242c16367
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 101 additions and 58 deletions

View File

@ -5,9 +5,16 @@
"type": "module", "type": "module",
"devDependencies": { "devDependencies": {
"@lumeweb/cfg": "https://github.com/LumeWeb/bcfg.git", "@lumeweb/cfg": "https://github.com/LumeWeb/bcfg.git",
"@types/node": "^18.7.16", "@types/node": "^18.11.17",
"express": "^4.18.1", "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"
} }
} }

43
src/config.ts Normal file
View File

@ -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<any>): void;
parseEnv(env?: object): void;
}
export default Config;

View File

@ -1,20 +1,12 @@
import Config from "@lumeweb/cfg"; import type { EventEmitter2 } from "eventemitter2";
import tls from "tls";
import { Logger } from "loglevel";
import { RPCMethod, RPCServer } from "./rpc.js"; import { RPCMethod, RPCServer } from "./rpc.js";
import { IndependentFileSmall, SavedSslData, SslData } from "./files.js"; import { Logger } from "pino";
import type { express } from "express"; import SSLManager from "./ssl.js";
// @ts-ignore import type { HDKey } from "micro-ed25519-hdkey";
import type { Err } from "libskynet"; import Config from "./config.js";
export type PluginFunction = (api: PluginAPI) => Promise<void>; export type PluginFunction = (api: PluginAPI) => Promise<void>;
export type DnsProvider = (ipAddress: string, domain: string) => Promise<void>;
export type OverwriteDataFn = (newData: Uint8Array) => Promise<Err>;
export type ReadDataFn = () => Promise<[Uint8Array, Err]>;
export interface Plugin { export interface Plugin {
name: string; name: string;
plugin: PluginFunction; plugin: PluginFunction;
@ -22,46 +14,29 @@ export interface Plugin {
default?: Plugin; default?: Plugin;
} }
export interface PluginAPI { declare class PluginAPI extends EventEmitter2 {
private _server;
constructor({
config,
logger,
server,
swarm,
}: {
config: Config; config: Config;
registerMethod: (methodName: string, method: RPCMethod) => void;
loadPlugin: (moduleName: string) => Promise<Plugin>;
getRpcServer: () => RPCServer;
ssl: {
setContext: (context: tls.SecureContext) => void;
getContext: () => tls.SecureContext;
getSaved: (retry: boolean) => Promise<boolean | SavedSslData>;
set: (
cert: IndependentFileSmall | Uint8Array,
key: IndependentFileSmall | Uint8Array
) => void;
get: () => SslData;
save: () => Promise<void>;
setCheck(checker: () => Promise<void>): 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<Err>;
};
dns: {
setProvider(provider: DnsProvider): void;
};
logger: Logger; logger: Logger;
getSeed: () => Uint8Array; 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<Plugin>;
registerMethod(methodName: string, method: RPCMethod): void;
} }

18
src/ssl.ts Normal file
View File

@ -0,0 +1,18 @@
import type tls from "tls";
export type SSLManagerRenewHandler = (domain: string) => Promise<boolean>;
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<boolean>;
get enabled(): any;
}
export default SSLManager;