0.9.1 • Published 9 days ago

@e-mc/module v0.9.1

Weekly downloads
-
License
BSD 3-Clause
Repository
github
Last release
9 days ago

@e-mc/module

  • NodeJS 16
  • ES2020

General Usage

Interface

import type { LogStatus } from "./squared";

import type { IHost } from "./index";
import type { IAbortComponent, IPermission } from "./core";
import type { LOG_TYPE, STATUS_TYPE, ExecCommand, LogArguments, LogComponent, LogDate, LogFailOptions, LogMessageOptions, LogOptions, LogProcessOptions, LogTime, LogType, LogValue, LoggerFormat, StatusType } from "./logger";
import type { AsHashOptions, CheckSemVerOptions, CopyDirOptions, CopyDirResult, CopyFileOptions, CreateDirOptions, DeleteFileOptions, GetTempDirOptions, MoveFileOptions, NormalizeFlags, ParseFunctionOptions, PermissionOptions, ProtocolType, ReadBufferOptions, ReadFileCallback, ReadFileOptions, ReadHashOptions, ReadTextOptions, RemoveDirOptions, WriteFileOptions } from "./module";
import type { Settings } from "./node";
import type { LoggerFormatSettings } from "/settings";

import type { SpawnOptions } from "child_process";
import type { BinaryLike } from "crypto";
import type { FileTypeResult } from "file-type";
import type { NoParamCallback } from "fs";

import type * as EventEmitter from "events";

type BufferView = Buffer | string | NodeJS.ArrayBufferView;
type CpuUsage = NodeJS.CpuUsage;

interface IModule extends EventEmitter, IAbortComponent {
    readonly status: LogStatus<StatusType>[];
    readonly errors: unknown[];
    supported(major: number, minor?: number, patch?: number, lts?: boolean): boolean;
    supports(name: string, value?: boolean): boolean;
    getTempDir(options: GetTempDirOptions): string;
    getTempDir(uuidDir: boolean, createDir: boolean): string;
    getTempDir(pathname: string, createDir: boolean): string;
    getTempDir(uuidDir: boolean, filename?: string, createDir?: boolean): string;
    getTempDir(pathname?: string, filename?: string, createDir?: boolean): string;
    canRead(uri: string, options?: PermissionOptions): boolean;
    canWrite(uri: string, options?: PermissionOptions): boolean;
    readFile(src: string): Buffer | undefined;
    readFile(src: string, options?: ReadFileOptions): Promise<Buffer | string> | Buffer | string | undefined;
    readFile(src: string, promises: true): Promise<Buffer | undefined>;
    readFile(src: string, options: ReadFileOptions, promises: true): Promise<Buffer | string | undefined>;
    readFile(src: string, callback: ReadFileCallback<Buffer | string>): Buffer | string | undefined;
    readFile(src: string, options: ReadFileOptions, callback: ReadFileCallback<Buffer | string>): Buffer | string | undefined;
    writeFile(src: string, data: BufferView, options?: WriteFileOptions): boolean;
    writeFile(src: string, data: BufferView, promises: true): Promise<boolean>;
    writeFile(src: string, data: BufferView, options: WriteFileOptions, promises: true): Promise<boolean>;
    writeFile(src: string, data: BufferView, callback: NoParamCallback): void;
    writeFile(src: string, data: BufferView, options: WriteFileOptions, callback: NoParamCallback): void;
    deleteFile(src: string, options?: DeleteFileOptions): boolean;
    deleteFile(src: string, promises: true): Promise<boolean>;
    deleteFile(src: string, options: DeleteFileOptions, promises: true): Promise<boolean>;
    deleteFile(src: string, callback: NoParamCallback): void;
    deleteFile(src: string, options: DeleteFileOptions, callback: NoParamCallback): void;
    copyFile(src: string, dest: string, options?: CopyFileOptions): boolean;
    copyFile(src: string, dest: string, promises: true): Promise<boolean>;
    copyFile(src: string, dest: string, options: CopyFileOptions, promises: true): Promise<boolean>;
    copyFile(src: string, dest: string, callback: NoParamCallback): void;
    copyFile(src: string, dest: string, options: CopyFileOptions, callback: NoParamCallback): void;
    moveFile(src: string, dest: string, options?: MoveFileOptions): boolean;
    moveFile(src: string, dest: string, promises: true): Promise<boolean>;
    moveFile(src: string, dest: string, options: MoveFileOptions, promises: true): Promise<boolean>;
    moveFile(src: string, dest: string, callback: NoParamCallback): void;
    moveFile(src: string, dest: string, options: MoveFileOptions, callback: NoParamCallback): void;
    createDir(src: string, options?: CreateDirOptions): boolean;
    createDir(src: string, promises: true): Promise<boolean>;
    createDir(src: string, options: CreateDirOptions, promises: true): Promise<boolean>;
    createDir(src: string, callback: NoParamCallback): void;
    createDir(src: string, options: CreateDirOptions, callback: NoParamCallback): void;
    removeDir(src: string, options?: RemoveDirOptions): boolean;
    removeDir(src: string, promises: true): Promise<boolean>;
    removeDir(src: string, options: RemoveDirOptions, promises: true): Promise<boolean>;
    removeDir(src: string, callback: NoParamCallback): void;
    removeDir(src: string, options: RemoveDirOptions, callback: NoParamCallback): void;
    allSettled(values: readonly PromiseLike<unknown>[], rejected?: LogValue, type?: LogType): Promise<PromiseFulfilledResult<unknown>[]>;
    allSettled(values: readonly PromiseLike<unknown>[], rejected?: LogValue, options?: LogFailOptions): Promise<PromiseFulfilledResult<unknown>[]>;
    formatMessage(type: LogType, title: string, value: LogValue, message?: unknown, options?: LogMessageOptions): void;
    formatFail(type: LogType, title: string, value: LogValue, message?: unknown, options?: LogFailOptions): void;
    writeFail(value: LogValue, message?: unknown, type?: LogType): void;
    writeFail(value: LogValue, message?: unknown, options?: LogFailOptions): void;
    writeTimeProcess(title: string, value: string, startTime: LogTime, options?: LogProcessOptions): void;
    writeTimeElapsed(title: string, value: LogValue, startTime: LogTime, options?: LogMessageOptions): void;
    checkPackage(err: unknown, name: string | undefined, type: LogType): boolean;
    checkPackage(err: unknown, name: string | undefined, options: LogFailOptions): boolean;
    checkPackage(err: unknown, name: string | undefined, value?: LogValue, options?: LogFailOptions | LogType): boolean;
    checkFail(message: unknown, options: LogFailOptions): LogArguments | false | undefined;
    writeLog(component: LogComponent, queue?: boolean): void;
    writeLog(type: StatusType, value: unknown, options: LogOptions): void;
    writeLog(type: StatusType, value: unknown, timeStamp?: LogDate, duration?: number): void;
    addLog(component: LogComponent, queue?: boolean): void;
    addLog(type: StatusType, value: unknown, options: LogOptions): void;
    addLog(type: StatusType, value: unknown, from: string, source?: string): void;
    addLog(type: StatusType, value: unknown, timeStamp?: LogDate, from?: string, source?: string): void;
    addLog(type: StatusType, value: unknown, timeStamp?: LogDate, duration?: number, from?: string, source?: string): void;
    getLog(...type: StatusType[]): LogStatus<StatusType>[];
    flushLog(): void;
    willAbort(value: unknown): boolean;
    hasOwnPermission(): boolean;
    isFatal(err?: unknown): boolean;
    detach(): void;
    reset(): void;
    get moduleName(): string;
    set host(value);
    get host(): IHost | null;
    set permission(value);
    get permission(): IPermission | null;
    get aborted(): boolean;
    set abortable(value);
    get abortable(): boolean;
    get threadable(): boolean;
    set sessionId(value);
    get sessionId(): string;
    set broadcastId(value);
    get broadcastId(): string | string[];
    get logType(): LOG_TYPE;
    set logLevel(value: number | string);
    get logLevel(): number;
    get statusType(): STATUS_TYPE;
    set tempDir(value);
    get tempDir(): string;

    /* EventEmitter */
    on(event: "exec", listener: (command: ExecCommand, options?: SpawnOptions) => void): this;
    on(event: "error", listener: (err: Error) => void): this;
    on(event: "file:read", listener: (src: string, data: Buffer | string, options?: ReadFileOptions) => void): this;
    on(event: "file:write", listener: (src: string, options?: WriteFileOptions) => void): this;
    on(event: "file:delete", listener: (src: string, options?: DeleteFileOptions) => void): this;
    on(event: "file:copy", listener: (dest: string, options?: CopyFileOptions) => void): this;
    on(event: "file:move", listener: (dest: string, options?: MoveFileOptions) => void): this;
    on(event: "dir:create", listener: (src: string, options?: CreateDirOptions) => void): this;
    on(event: "dir:remove", listener: (src: string, options?: RemoveDirOptions) => void): this;
    once(event: "exec", listener: (command: ExecCommand, options?: SpawnOptions) => void): this;
    once(event: "error", listener: (err: Error) => void): this;
    once(event: "file:read", listener: (src: string, data: Buffer | string, options?: ReadFileOptions) => void): this;
    once(event: "file:write", listener: (src: string, options?: WriteFileOptions) => void): this;
    once(event: "file:delete", listener: (src: string, options?: DeleteFileOptions) => void): this;
    once(event: "file:copy", listener: (dest: string, options?: CopyFileOptions) => void): this;
    once(event: "file:move", listener: (dest: string, options?: MoveFileOptions) => void): this;
    once(event: "dir:create", listener: (src: string, options?: CreateDirOptions) => void): this;
    once(event: "dir:remove", listener: (src: string, options?: RemoveDirOptions) => void): this;
    emit(event: "exec", command: ExecCommand, options?: SpawnOptions): boolean;
    emit(event: "error", err: Error): boolean;
    emit(event: "file:read", src: string, data: Buffer | string, options?: ReadFileOptions): boolean;
    emit(event: "file:write", src: string, options?: WriteFileOptions): boolean;
    emit(event: "file:delete", src: string, options?: DeleteFileOptions): boolean;
    emit(event: "file:copy", dest: string, options?: CopyFileOptions): boolean;
    emit(event: "file:move", dest: string, options?: MoveFileOptions): boolean;
    emit(event: "dir:create", src: string, options?: CreateDirOptions): boolean;
    emit(event: "dir:remove", src: string, options?: RemoveDirOptions): boolean;
}

interface ModuleConstructor {
    PROCESS_TIMEOUT: number;
    LOG_STYLE_FAIL: LogMessageOptions;
    LOG_STYLE_SUCCESS: LogMessageOptions;
    LOG_STYLE_INFO: LogMessageOptions;
    LOG_STYLE_WARN: LogMessageOptions;
    LOG_STYLE_NOTICE: LogMessageOptions;
    LOG_STYLE_REVERSE: LogMessageOptions;
    readonly VERSION: string;
    readonly LOG_TYPE: LOG_TYPE;
    readonly LOG_FORMAT: LoggerFormatSettings<LoggerFormat<number>>;
    readonly STATUS_TYPE: STATUS_TYPE;
    readonly MAX_TIMEOUT: number;
    readonly TEMP_DIR: string;
    supported(major: number, minor?: number, patch?: number, lts?: boolean): boolean;
    formatMessage(type: LogType, title: string, value: LogValue, message?: unknown, options?: LogMessageOptions): void;
    writeFail(value: LogValue, message?: unknown, options?: LogFailOptions | LogType): void;
    enabled(key: string, username?: string): boolean;
    parseFunction(value: unknown, options?: ParseFunctionOptions): ((...args: unknown[]) => Promise<unknown> | unknown) | null;
    parseFunction(value: unknown, absolute: boolean, sync?: boolean): ((...args: unknown[]) => Promise<unknown> | unknown) | null;
    asString(value: unknown, cacheKey?: boolean | "throws"): string;
    asHash(data: BinaryLike, minLength: number): string;
    asHash(data: BinaryLike, algorithm: string, minLength?: number): string;
    asHash(data: BinaryLike, algorithm?: string, options?: AsHashOptions): string;
    asHash(data: BinaryLike, options?: AsHashOptions): string;
    readHash(value: string | URL, options?: ReadHashOptions): Promise<string>;
    toPosix(value: unknown, normalize: boolean): string;
    toPosix(value: unknown, filename?: string, normalize?: boolean): string;
    hasLogType(value: LogType): boolean;
    isURL(value: string, ...exclude: string[]): boolean;
    isFile(value: string | URL, type?: ProtocolType): boolean;
    isDir(value: string | URL): boolean;
    isPath(value: string | URL, type?: "unc" | "unc-exists"): boolean;
    isPath(value: string | URL, isFile?: boolean): boolean;
    isErrorCode(err: unknown, ...code: string[]): boolean;
    fromLocalPath(value: string): string;
    resolveFile(value: string): string;
    resolvePath(value: string, base: string | URL): string;
    joinPath(...values: [...paths: unknown[], normalize: boolean][]): string;
    joinPath(...values: unknown[]): string;
    normalizePath(value: unknown, flags?: boolean | NormalizeFlags): string;
    createDir(value: string | URL, overwrite?: boolean): boolean;
    removeDir(value: string | URL, sinceCreated: number, recursive?: boolean): boolean;
    removeDir(value: string | URL, empty?: boolean, recursive?: boolean): boolean;
    copyDir(src: string | URL, dest: string | URL, move?: boolean, recursive?: boolean): Promise<CopyDirResult>;
    copyDir(src: string | URL, dest: string | URL, options?: CopyDirOptions): Promise<CopyDirResult>;
    renameFile(src: string | URL, dest: string | URL, throws?: boolean): boolean;
    streamFile(src: string, cache: boolean): Promise<Buffer | string>;
    streamFile(src: string, options: ReadBufferOptions): Promise<Buffer | string>;
    streamFile(src: string, cache?: boolean | ReadBufferOptions, options?: ReadBufferOptions): Promise<Buffer | string>;
    readText(value: string | URL, cache: boolean): string;
    readText(value: string | URL, options: ReadTextOptions): Promise<string> | string;
    readText(value: string | URL, encoding?: BufferEncoding | ReadTextOptions, cache?: boolean): string;
    readBuffer(value: string | URL, options: ReadBufferOptions): Promise<Buffer | null> | Buffer | null;
    readBuffer(value: string | URL, cache?: boolean | ReadBufferOptions): Buffer | null;
    resolveMime(data: string | Buffer | Uint8Array | ArrayBuffer): Promise<FileTypeResult | undefined>;
    lookupMime(value: string, extension?: boolean): string;
    initCpuUsage(instance?: IModule): CpuUsage;
    getCpuUsage(start: CpuUsage, format: true): string;
    getCpuUsage(start: CpuUsage, format?: boolean): number;
    getMemUsage(format: true): string;
    getMemUsage(format?: boolean): number;
    formatCpuMem(start: CpuUsage, all?: boolean): string;
    getPackageVersion(name: string | [string, string], startDir: string, baseDir?: string): string;
    getPackageVersion(name: string | [string, string], unstable?: boolean, startDir?: string, baseDir?: string): string;
    checkSemVer(name: string | [string, string], options: CheckSemVerOptions): boolean;
    checkSemVer(name: string | [string, string], min: number | string, max?: number | string, unstable?: boolean, startDir?: string): boolean;
    checkSemVer(name: string | [string, string], min: number | string, max: number | string, options?: CheckSemVerOptions): boolean;
    sanitizeCmd(value: string): string;
    sanitizeArgs(value: string, doubleQuote?: boolean): string;
    sanitizeArgs(values: string[], doubleQuote?: boolean): string[];
    purgeMemory(percent: number, parent: boolean): Promise<number>;
    purgeMemory(percent: number, limit: number, parent?: boolean): Promise<number>;
    purgeMemory(percent?: number, limit?: number | boolean, parent?: unknown): Promise<number>;
    canWrite(name: "temp" | "home"): boolean;
    loadSettings(settings: Settings, password?: string): boolean;
    readonly prototype: IModule<IHost>;
    new(): IModule<IHost>;
}

Settings

import type { LogMessageOptions, LogTypeValue, LoggerStatus } from "./logger";
import type { LoggerProcessSettings } from "./settings";

import type { BackgroundColor as IBackgroundColor, ForegroundColor as IForegroundColor } from "chalk";
import type { BinaryLike, CipherGCMTypes } from "crypto";
import type { SecureVersion } from "tls";

interface NodeModule {
    process?: {
        cpu_usage?: boolean;
        memory_usage?: boolean;
        inline?: boolean;
    };
    require?: {
        ext?: string | string[] | boolean;
        npm?: boolean;
        inline?: boolean;
    };
}

interface ProcessModule {
    env?: {
        apply?: boolean;
    };
    cipher?: {
        algorithm?: CipherGCMTypes;
        key?: BinaryLike;
        iv?: BinaryLike;
    };
    password?: string;
}

interface MemoryModule {
    settings?: {
        users?: boolean | string[];
        cache_disk?: {
            enabled?: boolean;
            min_size?: number | string;
            max_size?: number | string;
            include?: string[];
            exclude?: string[];
            expires?: number | string;
        };
    };
}

interface PermissionModule {
    home_read?: boolean;
    home_write?: boolean;
    process_exec?: (string | ExecOptions)[];
}

interface ErrorModule {
    out?: string | (err: Error, data: LogTypeValue, require?: NodeRequire) => void;
    fatal?: boolean;
}

interface TempModule {
    dir?: string;
    write?: boolean;
}

interface LoggerModule {
    enabled?: boolean;
    level?: number,
    production?: string[];
    format?: {
        title?: {
            width?: number;
            color?: ForegroundColor;
            bg_color?: BackgroundColor;
            bold?: boolean;
            justify?: "left" | "center" | "right";
            as?: StringMap;
        };
        value?: {
            width?: number;
            color?: ForegroundColor;
            bg_color?: BackgroundColor;
            bold?: boolean;
            justify?: "left" | "center" | "right";
        },
        hint?: {
            width?: number;
            color?: ForegroundColor;
            bg_color?: BackgroundColor;
            bold?: boolean;
            as?: StringMap;
            unit?: "auto" | "s" | "ms";
        };
        message?: {
            width?: number;
            color?: ForegroundColor;
            bg_color?: BackgroundColor;
            bold?: boolean;
        };
        meter?: {
            color?: ForegroundColor;
            bg_color?: BackgroundColor;
            bg_alt_color?: BackgroundColor;
            bold?: boolean;
        };
    };
    meter?: {
        http?: number;
        image?: number;
        compress?: number;
        process?: number;
    };
    broadcast?: {
        enabled?: boolean;
        out?: string | (value: string, options: LogMessageOptions, require?: NodeRequire) => void;
        color?: boolean;
        port?: number | number[];
        secure?: {
            port?: number | number[];
            ca?: string;
            key?: string;
            cert?: string;
            version?: SecureVersion
        };
    };
    color?: boolean;
    message?: boolean;
    stdout?: boolean;
    abort?: boolean;
    status?: boolean | LoggerStatus;
    unknown?: boolean | LoggerColor;
    system?: boolean | LoggerColor;
    process?: boolean | LoggerProcessSettings;
    image?: boolean | LoggerColor;
    compress?: boolean | LoggerColor;
    watch?: boolean | LoggerColor;
    file?: boolean | LoggerColor;
    cloud?: boolean | LoggerColor;
    db?: boolean | LoggerColor;
    time_elapsed?: boolean | LoggerColor;
    time_process?: boolean | LoggerColor;
    exec?: boolean | LoggerColor;
    http?: boolean | LoggerColor;
    node?: boolean | LoggerColor;
    session_id?: boolean | number;
    stack_trace?: boolean | number;
}

type BackgroundColor = typeof IBackgroundColor | `#${string}`;
type ForegroundColor = typeof IForegroundColor | `#${string}`;

References

LICENSE

BSD 3-Clause

0.8.9

9 days ago

0.8.8

9 days ago

0.7.6

9 days ago

0.7.5

9 days ago

0.8.10

9 days ago

0.6.3

9 days ago

0.7.4

9 days ago

0.5.6

9 days ago

0.9.1

9 days ago

0.9.0

10 days ago

0.8.7

1 month ago

0.6.2

1 month ago

0.7.3

1 month ago

0.5.5

1 month ago

0.8.6

2 months ago

0.7.2

2 months ago

0.5.4

2 months ago

0.6.1

2 months ago

0.8.5

2 months ago

0.8.4

3 months ago

0.8.3

3 months ago

0.8.2

3 months ago

0.8.1

4 months ago

0.8.0

5 months ago

0.7.1

5 months ago

0.7.0

5 months ago

0.6.0

9 months ago

0.5.3

12 months ago

0.5.2

1 year ago

0.5.1

1 year ago

0.5.0

1 year ago

0.4.2

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.3

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago

0.1.0

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago