import * as T from "@start9labs/start-sdk/lib/types" import * as net from "net" import { CallbackHolder } from "./CallbackHolder" const path = "/start9/sockets/rpcOut.sock" const MAIN = "main" as const export class Effects implements T.Effects { constructor(readonly method: string, readonly callbackHolder: CallbackHolder) {} id = 0 rpcRound(method: string, params: unknown) { const id = this.id++; const client = net.createConnection(path, () => { client.write(JSON.stringify({ id, method, params })); }); return new Promise((resolve, reject) => { client.on('data', (data) => { try { resolve(JSON.parse(data.toString())?.result) } catch (error) { reject(error) } client.end(); }); }) } started= this.method !== MAIN ? null : ()=> { return this.rpcRound('started', null) } bind(...[options]: Parameters) { return this.rpcRound('bind', (options)) as ReturnType } clearBindings(...[]: Parameters) { return this.rpcRound('clearBindings', null) as ReturnType } clearNetworkInterfaces( ...[]: Parameters ) { return this.rpcRound('clearNetworkInterfaces', null) as ReturnType } executeAction(...[options]: Parameters) { return this.rpcRound('executeAction', options) as ReturnType } exists(...[packageId]: Parameters) { return this.rpcRound('exists', packageId) as ReturnType } exportAction(...[options]: Parameters) { return this.rpcRound('exportAction', (options)) as ReturnType } exportNetworkInterface( ...[options]: Parameters ) { return this.rpcRound('exportNetworkInterface', (options)) as ReturnType } exposeForDependents(...[options]: any) { return this.rpcRound('exposeForDependents', (null)) as ReturnType } exposeUi(...[options]: Parameters) { return this.rpcRound('exposeUi', (options)) as ReturnType } getConfigured(...[]: Parameters) { return this.rpcRound('getConfigured',null) as ReturnType } getContainerIp(...[]: Parameters) { return this.rpcRound('getContainerIp', null) as ReturnType } getHostnames: any = (...[allOptions]: any[]) => { const options = { ...allOptions, callback: this.callbackHolder.addCallback(allOptions.callback) } return this.rpcRound('getHostnames', options) as ReturnType } getInterface(...[options]: Parameters) { return this.rpcRound('getInterface', {...options, callback: this.callbackHolder.addCallback(options.callback)}) as ReturnType } getIPHostname(...[]: Parameters) { return this.rpcRound('getIPHostname', (null)) as ReturnType } getLocalHostname(...[]: Parameters) { return this.rpcRound('getLocalHostname', null) as ReturnType } getPrimaryUrl(...[options]: Parameters) { return this.rpcRound('getPrimaryUrl', {...options, callback: this.callbackHolder.addCallback(options.callback)}) as ReturnType } getServicePortForward( ...[options]: Parameters ) { return this.rpcRound('getServicePortForward', (options)) as ReturnType } getServiceTorHostname( ...[interfaceId, packageId]: Parameters ) { return this.rpcRound('getServiceTorHostname', ({interfaceId, packageId})) as ReturnType } getSslCertificate(...[packageId, algorithm]: Parameters) { return this.rpcRound('getSslCertificate', ({packageId, algorithm})) as ReturnType } getSslKey(...[packageId, algorithm]: Parameters) { return this.rpcRound('getSslKey', ({packageId, algorithm})) as ReturnType } getSystemSmtp(...[options]: Parameters) { return this.rpcRound('getSystemSmtp', {...options, callback: this.callbackHolder.addCallback(options.callback)}) as ReturnType } is_sandboxed(...[]: Parameters) { return this.rpcRound('is_sandboxed', (null)) as ReturnType } listInterface(...[options]: Parameters) { return this.rpcRound('listInterface', {...options, callback: this.callbackHolder.addCallback(options.callback)}) as ReturnType } mount(...[options]: Parameters) { return this.rpcRound('mount', options) as ReturnType } removeAction(...[options]: Parameters) { return this.rpcRound('removeAction', options) as ReturnType } removeAddress(...[options]: Parameters) { return this.rpcRound('removeAddress', options) as ReturnType } restart(...[]: Parameters) { this.rpcRound('restart', null) } reverseProxy(...[options]: Parameters) { return this.rpcRound('reverseProxy', options) as ReturnType } running(...[packageId]: Parameters) { return this.rpcRound('running', {packageId}) as ReturnType } // runRsync(...[options]: Parameters) { // // return this.rpcRound('executeAction', options) as ReturnType // // return this.rpcRound('executeAction', options) as ReturnType // } setConfigured(...[configured]: Parameters) { return this.rpcRound('setConfigured', {configured}) as ReturnType } setDependencies(...[dependencies]: Parameters) { return this.rpcRound('setDependencies', {dependencies}) as ReturnType } setHealth(...[options]: Parameters) { return this.rpcRound('setHealth', options) as ReturnType } shutdown(...[]: Parameters) { return this.rpcRound('shutdown', null) } stopped(...[packageId]: Parameters) { return this.rpcRound('stopped', {packageId}) as ReturnType } store: T.Effects['store'] = { get:(options) => this.rpcRound('getStore', {...options, callback: this.callbackHolder.addCallback(options.callback)}) as ReturnType, set:(options) => this.rpcRound('setStore', options) as ReturnType } }