diff --git a/src/commands/create.ts b/src/commands/create.ts index b1f6b64..a74140c 100644 --- a/src/commands/create.ts +++ b/src/commands/create.ts @@ -24,8 +24,10 @@ export default async function create(module: string, name: string, id: string) { const functions = 'default' in imported ? imported.default : imported; id ??= uuid.v4().replace(/-/g, '').toUpperCase(); system.instances.set(id, { - config: {}, - ram: {}, + privateScope: { + config: {}, + ram: {} + }, module: module, functions }); diff --git a/src/commands/ls.ts b/src/commands/ls.ts index 17b6ba6..541d101 100644 --- a/src/commands/ls.ts +++ b/src/commands/ls.ts @@ -13,7 +13,7 @@ export default function ls(flags: any) { console.log(' ' + autoColorString(id.substring(0, 8)) + chalk.ansi256(242)(' (' + instance.module + ')')); } - for(const [configKey, configVal] of Object.entries(instance.config)) { + for(const [configKey, configVal] of Object.entries(instance.privateScope.config)) { console.log(' ' + chalk.ansi256(240)(configKey + ': ' + configVal)); } } diff --git a/src/commands/save.ts b/src/commands/save.ts index 5e97c1f..0603984 100644 --- a/src/commands/save.ts +++ b/src/commands/save.ts @@ -11,7 +11,7 @@ export default function save() { }; for(const [id, info] of system.instances.entries()) { obj.instances[id] = { - config: info.config, + config: info.privateScope.config, module: info.module } } diff --git a/src/externals.d.ts b/src/externals.d.ts index 23a0863..737a2e5 100644 --- a/src/externals.d.ts +++ b/src/externals.d.ts @@ -1,11 +1,42 @@ declare module "serverline"; declare module "telnet"; -declare module "@kernel:base"; +declare module "@kernel:base" { + function exec(command: string, echo?: boolean): any; + function autoColorString(str: string): string; + function reverseAliasMap(): Map + type Instance = { + module: string; + functions: { + [name: string]: (...args: any[]) => any + }; + privateScope: { + config: { [name: string]: any }; + ram: { [name: string]: any }; + } + }; + type Id = string; + type ParsedSystemState = { + handoff: string, + instances: { + [id: Id]: { + config: any, + module: string + } + }, + aliases: { + [alias: string]: Id + } + }; + type System = { + aliases: Map; + instances: Map; + handoff: string; + } + const system: System; +} declare module "@kernel:log-hook"; declare module "@commands:executor"; declare module "@commands:create"; declare module "@commands:ls"; declare module "@commands:save"; -declare module "@builtin:systemd"; -declare module "@builtin:sshd"; declare module "@echo off"; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 827e2ee..b633848 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ /// +import { Instance, ParsedSystemState } from '@kernel:base'; import '@kernel:log-hook'; import createExecutor from '@commands:executor'; import create from '@commands:create'; @@ -15,12 +16,7 @@ import md5 from 'md5'; const args = process.argv.slice(2); const [ startupFile ] = args; -type Instance = { - config: any; - ram: any; - module: string; - functions: any; -}; +export type Id = string; export const system = { instances: new Map(), @@ -80,7 +76,7 @@ export const kernel = { if(!(fn in instance.functions)) { throw new Error('FUNCTION_DOES_NOT_EXIST_ON_INVOCATION_TARGET'); } - const bound = instance.functions[fn].bind(instance); + const bound = instance.functions[fn].bind(instance.privateScope); await bound(...args); }, async script(path: string) { @@ -100,15 +96,12 @@ const executor = createExecutor(kernel); (async () => { if(existsSync('.system')) { - const state: any = JSON.parse(readFileSync('.system').toString()); + const state: ParsedSystemState = JSON.parse(readFileSync('.system').toString()); system.handoff = state.handoff; - for(const [id, info] of Object.entries(state.instances)) { - const [alias] = - Object.entries(state.aliases) - .find(([,tryId]) => tryId === id) - ?? [undefined]; + for(const [id, info] of Object.entries(state.instances)) { + const [alias] = Object.entries(state.aliases).find(([,tryId]) => tryId === id) ?? [undefined]; await kernel.create(info.module, alias, id); - system.instances.get(id).config = info.config; + system.instances.get(id).privateScope.config = info.config; } checkpoint('System State Restored'); } diff --git a/src/logHook.ts b/src/logHook.ts index dc4f769..12592b0 100644 --- a/src/logHook.ts +++ b/src/logHook.ts @@ -4,12 +4,13 @@ import { EventEmitter } from 'events' const events = new EventEmitter(); -process.stdout.write = (function(write): any { - return function(string: string, encoding: any, fileDescriptor: any) { - // const ansiCodes = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g; - // appendFileSync('.log', string); +type WriteFunction = typeof process.stdout.write; + +process.stdout.write = (function(write: WriteFunction): WriteFunction { + return function(string: string): boolean { events.emit('data', string); - write.apply(process.stdout, arguments); + + return write.apply(process.stdout, arguments); }; })(process.stdout.write); diff --git a/src/modules/sshd.ts b/src/modules/sshd.ts index 5d9ecea..496cb1d 100644 --- a/src/modules/sshd.ts +++ b/src/modules/sshd.ts @@ -1,8 +1,10 @@ import telnet from 'telnet'; -import { exec } from '@kernel:base'; +import { exec, Instance } from '@kernel:base'; import log from '@kernel:log-hook'; -export function start() { + + +export function start(this: Instance["privateScope"]) { telnet.createServer(function (client: any) { // make unicode characters work properly diff --git a/src/modules/systemd.ts b/src/modules/systemd.ts index 896701c..199cdda 100644 --- a/src/modules/systemd.ts +++ b/src/modules/systemd.ts @@ -1,8 +1,14 @@ import { exec } from '@kernel:base'; +// export default { +// config: { + +// } +// } + export function boot() { for(const [name, script] of Object.entries(this.config)) { - exec(script); + exec(script as string); } }