chore: revert multi and mov sernEmitter

This commit is contained in:
Jacob Nguyen
2023-05-05 23:14:54 -05:00
parent 084b5adb5c
commit 29499457bb
5 changed files with 158 additions and 5 deletions

67
src/core/io.ts Normal file
View File

@@ -0,0 +1,67 @@
import { Module } from "../types/module";
import { Result } from "ts-results-es";
import { Processed } from "../types/core";
import { SernError } from "./structures/errors";
import { readdir, stat } from 'fs/promises';
import { join, resolve } from 'path';
import { type Observable, from, mergeMap, ObservableInput } from 'rxjs';
import { defaultModuleLoader } from "./module-loading";
export const fmtFileName = (n: string) => n.substring(0, n.length - 3);
/**
* a directory string is converted into a stream of modules.
* starts the stream of modules that sern needs to process on init
* @returns {Observable<{ mod: Module; absPath: string; }[]>} data from command files
* @param commandDir
*/
export function buildModuleStream<T extends Module>(
input: ObservableInput<string>
): Observable<Result<Processed<T>, SernError>> {
return from(input).pipe(mergeMap(defaultModuleLoader<T>));
}
export function getCommands(dir: string) {
return readPath(resolve(dir));
}
export function filename(path: string) {
const i = path.lastIndexOf('/')
return fmtFileName(path.substring(i))
}
async function* readPath(dir: string): AsyncGenerator<string> {
try {
const files = await readdir(dir);
for (const file of files) {
const fullPath = join(dir, file);
const fileStats = await stat(fullPath);
if (fileStats.isDirectory()) {
yield* readPath(fullPath);
} else {
/// #if MODE === 'esm'
yield 'file:///'+fullPath;
/// #elif MODE === 'cjs'
yield fullPath;
/// #endif
}
}
} catch (err) {
throw err;
}
}
//https://stackoverflow.com/questions/16697791/nodejs-get-filename-of-caller-function
export function filePath() {
const err = new Error();
Error.prepareStackTrace = (_, stack) => stack;
const stack = err.stack as unknown as NodeJS.CallSite[];
Error.prepareStackTrace = undefined;
const path = stack[2].getFileName();
if(path === null) {
throw Error("Could not get the name of commandModule.")
}
return path;
}

View File

@@ -1,7 +1,7 @@
/**
* @enum { string }
*/
export enum SernError {
export const enum SernError {
/**
* Throws when registering an invalid module.
* This means it is undefined or an invalid command type was provided

View File

@@ -1,4 +1,4 @@
export * from './errors';
export * from './enums';
export * from './moduleStore';
export * from './context'
export * from './sernEmitter'

View File

@@ -0,0 +1,87 @@
import { EventEmitter } from 'node:events';
import type { Payload, SernEventsMapping } from '../types/handler';
import { PayloadType } from '../core/structures';
import type { Module } from '../types/module';
/**
* @since 1.0.0
*/
export class SernEmitter extends EventEmitter {
/**
* Listening to sern events with on. This event stays on until a crash or a normal exit
* @param eventName
* @param listener what to do with the data
*/
public override on<T extends keyof SernEventsMapping>(
eventName: T,
listener: (...args: SernEventsMapping[T][]) => void,
): this {
return super.on(eventName, listener);
}
/**
* Listening to sern events with on. This event stays on until a crash or a normal exit
* @param eventName
* @param listener what to do with the data
*/
public override once<T extends keyof SernEventsMapping>(
eventName: T,
listener: (...args: SernEventsMapping[T][]) => void,
): this {
return super.once(eventName, listener);
}
/**
* Listening to sern events with on. This event stays on until a crash or a normal exit
* @param eventName
* @param args the arguments for emitting the eventName
*/
public override emit<T extends keyof SernEventsMapping>(
eventName: T,
...args: SernEventsMapping[T]
): boolean {
return super.emit(eventName, ...args);
}
private static payload<T extends Payload>(
type: PayloadType,
module?: Module,
reason?: unknown,
) {
return { type, module, reason } as T;
}
/**
* Creates a compliant SernEmitter failure payload
* @param module
* @param reason
*/
static failure(module?: Module, reason?: unknown) {
//The generic cast Payload & { type : PayloadType.* } coerces the type to be a failure payload
// same goes to the other methods below
return SernEmitter.payload<Payload & { type: PayloadType.Failure }>(
PayloadType.Failure,
module,
reason,
);
}
/**
* Creates a compliant SernEmitter module success payload
* @param module
*/
static success(module: Module) {
return SernEmitter.payload<Payload & { type: PayloadType.Success }>(
PayloadType.Success,
module,
);
}
/**
* Creates a compliant SernEmitter module warning payload
* @param reason
*/
static warning(reason: unknown) {
return SernEmitter.payload<Payload & { type: PayloadType.Warning }>(
PayloadType.Warning,
undefined,
reason,
);
}
}

View File

@@ -32,12 +32,11 @@ export type Wrapper = WebsocketWrapper | ServerlessWrapper
export interface ServerlessWrapper {
readonly platform: ServerlessStrategy
commands: string;
events?: string;
commands: string[];
events?: string[];
containerConfig: {
get: (...keys: (keyof ServerlessDependencies)[]) => unknown[];
}
}
export type AnyWrapper =