From 29499457bb551df5968a3e7bf4867c4b233d706a Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Fri, 5 May 2023 23:14:54 -0500 Subject: [PATCH] chore: revert multi and mov sernEmitter --- src/core/io.ts | 67 +++++++++++++++++++++++ src/core/structures/errors.ts | 2 +- src/core/structures/index.ts | 2 +- src/core/structures/sernEmitter.ts | 87 ++++++++++++++++++++++++++++++ src/core/structures/wrapper.ts | 5 +- 5 files changed, 158 insertions(+), 5 deletions(-) create mode 100644 src/core/io.ts create mode 100644 src/core/structures/sernEmitter.ts diff --git a/src/core/io.ts b/src/core/io.ts new file mode 100644 index 0000000..5c3a8ec --- /dev/null +++ b/src/core/io.ts @@ -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( + input: ObservableInput +): Observable, SernError>> { + return from(input).pipe(mergeMap(defaultModuleLoader)); +} + +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 { + 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; +} diff --git a/src/core/structures/errors.ts b/src/core/structures/errors.ts index f082485..3b15bc1 100644 --- a/src/core/structures/errors.ts +++ b/src/core/structures/errors.ts @@ -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 diff --git a/src/core/structures/index.ts b/src/core/structures/index.ts index b5a4e4b..93ab4a6 100644 --- a/src/core/structures/index.ts +++ b/src/core/structures/index.ts @@ -1,4 +1,4 @@ -export * from './errors'; export * from './enums'; export * from './moduleStore'; export * from './context' +export * from './sernEmitter' diff --git a/src/core/structures/sernEmitter.ts b/src/core/structures/sernEmitter.ts new file mode 100644 index 0000000..d7bcce1 --- /dev/null +++ b/src/core/structures/sernEmitter.ts @@ -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( + 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( + 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( + eventName: T, + ...args: SernEventsMapping[T] + ): boolean { + return super.emit(eventName, ...args); + } + private static 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( + PayloadType.Failure, + module, + reason, + ); + } + /** + * Creates a compliant SernEmitter module success payload + * @param module + */ + static success(module: Module) { + return SernEmitter.payload( + PayloadType.Success, + module, + ); + } + /** + * Creates a compliant SernEmitter module warning payload + * @param reason + */ + static warning(reason: unknown) { + return SernEmitter.payload( + PayloadType.Warning, + undefined, + reason, + ); + } +} + diff --git a/src/core/structures/wrapper.ts b/src/core/structures/wrapper.ts index 3295a6f..7009705 100644 --- a/src/core/structures/wrapper.ts +++ b/src/core/structures/wrapper.ts @@ -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 =