*Initial version

This commit is contained in:
Derrick Hammer 2023-01-31 05:02:20 -05:00
commit fb8abc6ad6
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 230 additions and 0 deletions

18
package.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "@lumeweb/libkernel-universal",
"type": "module",
"version": "0.1.0",
"main": "dist/index.js",
"scripts": {
"build": "rimraf dist && tsc"
},
"dependencies": {
"@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git",
"libkernel": "^0.1.48",
"libkmodule": "^0.2.53"
},
"devDependencies": {
"prettier": "^2.8.3",
"typescript": "^4.9.4"
}
}

62
pnpm-lock.yaml Normal file
View File

@ -0,0 +1,62 @@
lockfileVersion: 5.4
specifiers:
'@siaweb/libweb': git+https://git.lumeweb.com/LumeWeb/libsiaweb.git
libkernel: ^0.1.48
libkmodule: ^0.2.53
prettier: ^2.8.3
typescript: ^4.9.4
dependencies:
'@siaweb/libweb': git.lumeweb.com/LumeWeb/libsiaweb/5e6cdba3e7d9a4b94e21ddcd5f2b5138fb440ee8
libkernel: 0.1.48
libkmodule: 0.2.53
devDependencies:
prettier: 2.8.3
typescript: 4.9.4
packages:
/@skynetlabs/libskynet/0.0.48:
resolution: {integrity: sha512-wFqPLVuGyfuEU1PsE7AMC+ANcoJiz7iruJPUrNZC0riDv1qMrZw+tx44HlBXLJ1H50JZiGtRPr9zLgkPFhf9OA==}
dev: false
/libkernel/0.1.48:
resolution: {integrity: sha512-h86j/D+5gnpw/h8DTQE1C9hWFRmQnw5ZU9L3fYTZIPqPEIIU40z+tnDp66Zbb6mcfYCr8+SGkhcCy3XsyHOvyQ==}
dependencies:
'@skynetlabs/libskynet': 0.0.48
libskynet: 0.0.64
dev: false
/libkmodule/0.2.53:
resolution: {integrity: sha512-fwMcssu6mwH+xeuUwUFdGDYQgGvwuVs0RMtbBPEYHvPPOBDNbdesPmlax/+XwDn6XiGggg+wSvq7st2m4VbwEw==}
dependencies:
libskynet: 0.1.9
dev: false
/libskynet/0.0.64:
resolution: {integrity: sha512-OPIxvxNTbo7H6KWU1YgQkfksNGTSQYVDsmGTji05/Gv2ZpwTOVJF4AMvggCHyLije8fNe4ZXRrBEtLQI3kpAaQ==}
dev: false
/libskynet/0.1.9:
resolution: {integrity: sha512-lYP4GUbDZXUd0PyTyHz7RBPXGSBFmFZrc2JirnV7tlL4ii3Zi6m79z73zGWoiDBrHI25RR9s9gdm/LdBeXz9nQ==}
dev: false
/prettier/2.8.3:
resolution: {integrity: sha512-tJ/oJ4amDihPoufT5sM0Z1SKEuKay8LfVAMlbbhnnkvt6BUserZylqo2PN+p9KeljLr0OHa2rXHU1T8reeoTrw==}
engines: {node: '>=10.13.0'}
hasBin: true
dev: true
/typescript/4.9.4:
resolution: {integrity: sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==}
engines: {node: '>=4.2.0'}
hasBin: true
dev: true
git.lumeweb.com/LumeWeb/libsiaweb/5e6cdba3e7d9a4b94e21ddcd5f2b5138fb440ee8:
resolution: {commit: 5e6cdba3e7d9a4b94e21ddcd5f2b5138fb440ee8, repo: https://git.lumeweb.com/LumeWeb/libsiaweb.git, type: git}
name: '@siaweb/libweb'
version: 0.1.0
dev: false

127
src/index.ts Normal file
View File

@ -0,0 +1,127 @@
type callModule = typeof import("libkmodule").callModule;
type connectModule = typeof import("libkmodule").connectModule;
type logErr = typeof import("libkmodule").logErr;
type log = typeof import("libkmodule").log;
import type { ErrTuple, DataFn } from "@siaweb/libweb";
type callModuleBound = (method: string, data?: any) => Promise<ErrTuple>;
type connectModuleBound = (
method: string,
data: any,
receiveUpdate: DataFn
) => [sendUpdate: DataFn, response: Promise<ErrTuple>];
let callModule: callModule;
let connectModule: connectModule;
let log: log;
let logErr: logErr;
export interface ModuleBag {
callModule: callModule;
connectModule: connectModule;
log: log;
logErr: logErr;
}
export interface ModuleBagBound extends ModuleBag {
callModule: callModuleBound;
connectModule: connectModuleBound;
}
export abstract class Client {
private _callModule?: callModuleBound;
get callModule(): callModuleBound {
return this._callModule as callModuleBound;
}
private _connectModule?: connectModuleBound;
get connectModule(): connectModuleBound {
return this._connectModule as connectModuleBound;
}
public async loadLibs(module: string): Promise<void> {
if (this._callModule && this._connectModule) {
return;
}
const moduleBag = await this.loadBound(module);
this._callModule = async (...args) => {
const ret = await moduleBag.callModule(...args);
this.handleError(ret);
return ret;
};
this._connectModule = moduleBag.connectModule;
}
public async loadBound(module: string): Promise<ModuleBagBound> {
return (await load(module)) as ModuleBagBound;
}
protected handleError(ret: ErrTuple): void {
if (ret[1]) {
throw new Error(ret[1]);
}
}
protected async callModuleReturn(method: string, data?: any): Promise<any> {
const ret = await this.callModule(method, data);
return ret[0];
}
}
export async function load(
module?: string
): Promise<ModuleBag | ModuleBagBound> {
if (callModule !== null && connectModule !== null) {
if (module) {
return {
callModule: callModule.bind(undefined, module),
connectModule: connectModule.bind(undefined, module),
log,
logErr,
} as ModuleBagBound;
}
return {
callModule,
connectModule,
log,
logErr,
} as ModuleBag;
}
const pkgName =
typeof window !== "undefined" && window?.document
? "libkernel"
: "libkmodule";
const pkg = await import(pkgName);
callModule = pkg.callModule;
connectModule = pkg.connectModule;
log = pkg.log;
logErr = pkg.logErr;
return load(module);
}
type ClientConstructor<U> = new (...args: any[]) => U;
export const factory = function <T extends Client = Client>(
type: ClientConstructor<T>,
module: string
) {
return function (...args: any): T {
return new Proxy<T>(new type(...args), {
get(target: T, property: string) {
return async (): Promise<any> => {
await target.loadLibs(module);
return target[property as keyof T];
};
},
});
};
};

23
tsconfig.json Normal file
View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"declaration": true,
"strict": true,
"module": "esnext",
"target": "esnext",
"esModuleInterop": true,
"sourceMap": false,
"rootDir": "src",
"outDir": "dist",
"typeRoots": [
"node_modules/@types",
],
"moduleResolution": "node",
"declarationMap": true,
"declarationDir": "dist",
"emitDeclarationOnly": false,
"allowJs": true
},
"include": [
"src"
]
}