private scope and typings! :)

stable
Valerie 2021-12-22 04:25:12 -05:00
parent 4a74b36961
commit a539626667
8 changed files with 64 additions and 29 deletions

View File

@ -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
});

View File

@ -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));
}
}

View File

@ -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
}
}

37
src/externals.d.ts vendored
View File

@ -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<string, string>
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<string, string>;
instances: Map<string, Instance>;
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";

View File

@ -1,5 +1,6 @@
/// <reference path="./externals.d.ts" />
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<string, Instance>(),
@ -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<any>(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');
}

View File

@ -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);

View File

@ -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

View File

@ -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);
}
}