2022-08-27 01:52:19 +00:00
|
|
|
import { JSONSchemaType } from "ajv";
|
2022-08-27 02:14:02 +00:00
|
|
|
import { PluginApiManager } from "./plugin.js";
|
2022-08-27 01:52:19 +00:00
|
|
|
|
|
|
|
export interface RPCRequest {
|
|
|
|
bypassCache?: boolean;
|
|
|
|
module: string;
|
|
|
|
method: string;
|
|
|
|
data: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RPCResponse {
|
|
|
|
updated?: number;
|
|
|
|
data?: any;
|
|
|
|
error?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RPCMethod {
|
2022-08-27 02:30:11 +00:00
|
|
|
cacheable: boolean;
|
2022-08-27 01:52:19 +00:00
|
|
|
handler: (
|
|
|
|
request: RPCRequest,
|
|
|
|
sendStream: (stream: AsyncIterable<Uint8Array>) => void
|
2022-08-27 02:21:21 +00:00
|
|
|
) => Promise<RPCResponse | null>;
|
2022-08-27 01:52:19 +00:00
|
|
|
}
|
2022-08-27 02:12:13 +00:00
|
|
|
|
2022-08-27 01:52:19 +00:00
|
|
|
export const RPC_REQUEST_SCHEMA: JSONSchemaType<RPCRequest> = {
|
|
|
|
anyOf: [],
|
|
|
|
oneOf: [],
|
|
|
|
type: "object",
|
|
|
|
properties: {
|
|
|
|
module: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
method: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
type: "string",
|
|
|
|
anyOf: [
|
|
|
|
{ type: "string" },
|
|
|
|
{ type: "number" },
|
|
|
|
{ type: "integer" },
|
|
|
|
{ type: "object" },
|
|
|
|
{ type: "array" },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
bypassCache: {
|
|
|
|
type: "boolean",
|
|
|
|
nullable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2022-08-27 02:12:13 +00:00
|
|
|
|
2022-08-27 01:52:19 +00:00
|
|
|
export interface StreamFileResponse {
|
|
|
|
data?: Uint8Array;
|
|
|
|
done: boolean;
|
|
|
|
}
|
2022-08-27 02:12:13 +00:00
|
|
|
|
2022-08-27 02:14:02 +00:00
|
|
|
export interface PluginAPI {
|
2022-08-27 01:52:19 +00:00
|
|
|
config: any;
|
2022-08-27 02:12:13 +00:00
|
|
|
registerMethod: (methodName: string, method: RPCMethod) => void;
|
2022-08-27 02:14:02 +00:00
|
|
|
loadPlugin: PluginApiManager["loadPlugin"];
|
2022-08-27 01:52:19 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 02:14:02 +00:00
|
|
|
export type PluginFunction = (api: PluginAPI) => Promise<void>;
|
2022-08-27 02:12:13 +00:00
|
|
|
|
2022-08-27 01:52:19 +00:00
|
|
|
export interface Plugin {
|
|
|
|
name: string;
|
|
|
|
plugin: PluginFunction;
|
|
|
|
exports?: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type RPCStreamHandler = (
|
|
|
|
stream: AsyncIterable<Uint8Array>
|
|
|
|
) => Promise<RPCResponse>;
|