*Organize types

*Refactor RPC JSON again
*Add types for rpc clear cache and broadcast request/response
*Add declares for new core classes
This commit is contained in:
Derrick Hammer 2022-11-26 02:53:45 -05:00
parent 4c04f4b11a
commit 516e82b7c1
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 182 additions and 125 deletions

33
src/files.ts Normal file
View File

@ -0,0 +1,33 @@
import type { Ed25519Keypair, Err } from "libskynet";
import { OverwriteDataFn, ReadDataFn } from "./index.js";
export interface IndependentFileSmallMetadata {
largestHistoricSize: bigint;
}
export interface IndependentFileSmall {
dataKey: Uint8Array;
fileData: Uint8Array;
inode: string;
keypair: Ed25519Keypair;
metadata: IndependentFileSmallMetadata;
revision: bigint;
seed: Uint8Array;
skylink: string;
viewKey: string;
overwriteData: OverwriteDataFn;
readData: ReadDataFn;
}
export interface SslData {
cert?: Uint8Array;
key?: Uint8Array;
}
export interface SavedSslData {
cert?: IndependentFileSmall;
key?: IndependentFileSmall;
}

View File

@ -1,125 +1,3 @@
import tls from "tls";
import type { Logger } from "loglevel";
import type { Ed25519Keypair, Err } from "libskynet";
import type express from "express";
import type Config from "@lumeweb/cfg";
export interface RPCRequest {
bypassCache?: boolean;
module: string;
method: string;
data: any;
}
export interface RPCResponse {
updated?: number;
data?: any;
error?: string;
}
export interface RPCMethod {
cacheable: boolean;
handler: (
request: RPCRequest,
sendStream: (stream: AsyncIterable<Uint8Array>) => void
) => Promise<RPCResponse | null>;
}
export interface StreamFileResponse {
data?: Uint8Array;
done: boolean;
}
export type DnsProvider = (ipAddress: string, domain: string) => Promise<void>;
export type PluginFunction = (api: PluginAPI) => Promise<void>;
export interface Plugin {
name: string;
plugin: PluginFunction;
exports?: any;
default?: Plugin;
}
export type RPCStreamHandler = (
stream: AsyncIterable<Uint8Array>
) => Promise<RPCResponse>;
export type OverwriteDataFn = (newData: Uint8Array) => Promise<Err>;
export type ReadDataFn = () => Promise<[Uint8Array, Err]>;
export interface IndependentFileSmallMetadata {
largestHistoricSize: bigint;
}
export interface IndependentFileSmall {
dataKey: Uint8Array;
fileData: Uint8Array;
inode: string;
keypair: Ed25519Keypair;
metadata: IndependentFileSmallMetadata;
revision: bigint;
seed: Uint8Array;
skylink: string;
viewKey: string;
overwriteData: OverwriteDataFn;
readData: ReadDataFn;
}
export interface SslData {
cert?: Uint8Array;
key?: Uint8Array;
}
export interface SavedSslData {
cert?: IndependentFileSmall;
key?: IndependentFileSmall;
}
export interface PluginAPI {
config: Config;
registerMethod: (methodName: string, method: RPCMethod) => void;
loadPlugin: (moduleName: string) => Promise<Plugin>;
getMethods: () => string[];
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;
getSeed: () => Uint8Array;
}
export * from "./rpc.js";
export * from "./plugin.js";
export * from "./files.js";

66
src/plugin.ts Normal file
View File

@ -0,0 +1,66 @@
import Config from "@lumeweb/cfg";
import tls from "tls";
import { Logger } from "loglevel";
import { RPCMethod, RPCServer } from "./rpc.js";
import { IndependentFileSmall, SavedSslData, SslData } from "./files.js";
import type { express } from "express";
import type { Err } from "libskynet";
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 {
name: string;
plugin: PluginFunction;
exports?: any;
default?: Plugin;
}
export interface PluginAPI {
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;
getSeed: () => Uint8Array;
}

80
src/rpc.ts Normal file
View File

@ -0,0 +1,80 @@
import type EventEmitter from "events";
export interface RPCRequest {
module: string;
method: string;
data: any;
}
export interface ClientRPCRequest extends RPCRequest {
bypassCache?: boolean;
}
export interface RPCResponse {
updated?: number;
data?: any | RPCResponse;
error?: string;
signature?: string;
signedField?: string;
}
export interface RPCMethod {
cacheable: boolean;
handler: (req: any) => Promise<any>;
}
export interface RPCCacheData {
[query: string]: RPCCacheItem | null;
}
export interface RPCCacheItem extends RPCResponse {
value: RPCResponse;
signature: string;
}
export interface RPCClearCacheRequest {
request: string;
relays?: string[];
}
export type RPCClearCacheResponseRelayList = {
[relay: string]: RPCClearCacheResponse;
};
export interface RPCClearCacheResponse extends RPCResponse {
relays?: RPCClearCacheResponseRelayList;
}
export interface RPCBroadcastRequest {
request: RPCRequest;
relays: string[];
}
export interface RPCBroadcastResponse extends RPCResponse {
relays: { [relay: string]: RPCResponse };
}
export declare class RPCServer extends EventEmitter {
get cache(): RPCCache;
registerMethod(
moduleName: string,
methodName: string,
options: RPCMethod
): void;
public getMethods(): string[];
public setup(stream: any): any;
public signData(data: any): string;
public static hashQuery(query: RPCRequest): string;
}
export declare class RPCCache extends EventEmitter {
get data(): RPCCacheData;
constructor(server: RPCServer);
public getNodeQuery(
node: string,
queryHash: string
): Promise<boolean | RPCResponse>;
public signResponse(item: RPCCacheItem): any;
public verifyResponse(pubkey: Buffer, item: RPCCacheItem): boolean | Buffer;
public addItem(query: RPCRequest, response: RPCResponse): void;
public deleteItem(queryHash: string): boolean;
}