From fb8abc6ad658fa0e45fef389cc7610a264d49c78 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 31 Jan 2023 05:02:20 -0500 Subject: [PATCH] *Initial version --- package.json | 18 +++++++ pnpm-lock.yaml | 62 ++++++++++++++++++++++++ src/index.ts | 127 +++++++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 23 +++++++++ 4 files changed, 230 insertions(+) create mode 100644 package.json create mode 100644 pnpm-lock.yaml create mode 100644 src/index.ts create mode 100644 tsconfig.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..9479db8 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml new file mode 100644 index 0000000..d0dcff0 --- /dev/null +++ b/pnpm-lock.yaml @@ -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 diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..64c0935 --- /dev/null +++ b/src/index.ts @@ -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; +type connectModuleBound = ( + method: string, + data: any, + receiveUpdate: DataFn +) => [sendUpdate: DataFn, response: Promise]; + +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 { + 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 { + 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 { + const ret = await this.callModule(method, data); + + return ret[0]; + } +} + +export async function load( + module?: string +): Promise { + 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 = new (...args: any[]) => U; + +export const factory = function ( + type: ClientConstructor, + module: string +) { + return function (...args: any): T { + return new Proxy(new type(...args), { + get(target: T, property: string) { + return async (): Promise => { + await target.loadLibs(module); + + return target[property as keyof T]; + }; + }, + }); + }; +}; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..a4d5cd4 --- /dev/null +++ b/tsconfig.json @@ -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" + ] +}