diff --git a/package.json b/package.json index 8b963c9..876056d 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,6 @@ "typescript": "^5.2.2" }, "dependencies": { - "remitter": "^0.3.2" + "remitter": "^0.4.1" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4acf8a2..84afc3f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6,8 +6,8 @@ settings: dependencies: remitter: - specifier: ^0.3.2 - version: 0.3.2 + specifier: ^0.4.1 + version: 0.4.1 devDependencies: '@oomol-lab/eslint-config-basic': @@ -296,6 +296,7 @@ packages: /@humanwhocodes/config-array@0.11.11: resolution: {integrity: sha512-N2brEuAadi0CcdeMXUkhbZB84eskAc8MEX1By6qEchoVywSgXPIjou4rYsl0V3Hj0ZnuGycGCjdNgockbzeWNA==} engines: {node: '>=10.10.0'} + deprecated: Use @eslint/config-array instead dependencies: '@humanwhocodes/object-schema': 1.2.1 debug: 4.3.4 @@ -311,6 +312,7 @@ packages: /@humanwhocodes/object-schema@1.2.1: resolution: {integrity: sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==} + deprecated: Use @eslint/object-schema instead dev: true /@jridgewell/gen-mapping@0.3.3: @@ -1629,8 +1631,8 @@ packages: jsesc: 0.5.0 dev: true - /remitter@0.3.2: - resolution: {integrity: sha512-3d4MEPVWoQeMhgzoBmyAQO+bZMp0rJAi85zf92Neird63cwJZ17GSWRV7VCE+W1fwJO/qx/c665QtDpanyTS3A==} + /remitter@0.4.1: + resolution: {integrity: sha512-tS1FZX+FGdWnUkPoAPfiIO0FWg35vy+Gvvdbu6EpOI+5A5nAEqn29TXc1FXPmJ8yKic4ajvenAUR+a8H2fPe9g==} dev: false /resolve-from@4.0.0: @@ -1659,6 +1661,7 @@ packages: /rimraf@3.0.2: resolution: {integrity: sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==} + deprecated: Rimraf versions prior to v4 are no longer supported hasBin: true dependencies: glob: 7.1.6 diff --git a/src/darwin.ts b/src/darwin.ts index 28cb975..1b115ee 100644 --- a/src/darwin.ts +++ b/src/darwin.ts @@ -1,19 +1,22 @@ import cp from "node:child_process"; import fs from "node:fs/promises"; +import type { EventReceiver } from "remitter"; import { Remitter } from "remitter"; -import { OVMDarwinStatusName } from "./type"; -import type { OVMDarwinOptions, OVMDarwinEventData, OVMDarwinInfo, OVMDarwinState } from "./type"; +import type { OVMDarwinEventData, OVMDarwinInfo, OVMDarwinOptions, OVMDarwinState } from "./type"; import { Restful } from "./event_restful"; import { RequestDarwin } from "./request"; import path from "node:path"; import { tmpdir } from "node:os"; export class DarwinOVM { - private readonly remitter = new Remitter(); + public readonly events : EventReceiver; + readonly #events: Remitter; private eventSocketPath: string; private request: RequestDarwin; - private constructor(private options: OVMDarwinOptions) {} + private constructor(private options: OVMDarwinOptions) { + this.events = this.#events = new Remitter(); + } public static async create(options: OVMDarwinOptions): Promise { const ovm = new DarwinOVM(options); @@ -25,18 +28,13 @@ export class DarwinOVM { return ovm; } - public on(event: keyof OVMDarwinEventData, listener: (datum: OVMDarwinEventData["status"]) => void): void { - this.remitter.on(event, listener); - } - private async initEventRestful(): Promise { - const restful = new Restful((name, message) => { - if (name in OVMDarwinStatusName) { - this.remitter.emit("status", { - name: name as OVMDarwinStatusName, - message, - }); - } + const restful = new Restful(); + + this.#events.remitAny((o) => { + return restful.events.onAny((data) => { + o.emit(data.event as keyof OVMDarwinEventData, data.data); + }); }); const dir = await fs.mkdtemp(path.join(tmpdir(), "ovm-")); @@ -68,7 +66,7 @@ export class DarwinOVM { reject(); }, 10 * 1000); - const disposer = this.remitter.once("status", () => { + const disposer = this.#events.onceAny(() => { clearTimeout(id); resolve(); }); @@ -102,10 +100,7 @@ export class DarwinOVM { launchTimeout .catch(() => { - this.remitter.emit("status", { - name: OVMDarwinStatusName.Error, - message: "OVM start timeout", - }); + this.#events.emit("error", "OVM start timeout"); }); } diff --git a/src/event_restful.ts b/src/event_restful.ts index 7f05edd..f556ea5 100644 --- a/src/event_restful.ts +++ b/src/event_restful.ts @@ -1,13 +1,13 @@ import url from "node:url"; import http from "node:http"; +import { Remitter } from "remitter"; export class Restful { private readonly server: http.Server; + public readonly events = new Remitter(); - public constructor( - callback: (name: string, message: string) => void, - ) { + public constructor() { this.server = http.createServer((request, response) => { if (!request.url) { return; @@ -18,7 +18,7 @@ export class Restful { response.statusCode = 200; response.end("ok"); - callback(parsedUrl.query.event as string, parsedUrl.query.message as string); + this.events.emit(parsedUrl.query.event as string, parsedUrl.query.message as string); } else { response.statusCode = 404; response.end("Not Found"); diff --git a/src/index.ts b/src/index.ts index cb77eb8..5addad5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ export const createDarwinOVM = (options: OVMDarwinOptions): Promise = }; export { - OVMDarwinStatusName, + OVMDarwinAppEventValue, OVMDarwinVzState, } from "./type"; diff --git a/src/type.ts b/src/type.ts index 245ac72..87f1c11 100644 --- a/src/type.ts +++ b/src/type.ts @@ -17,21 +17,18 @@ export interface OVMDarwinOptions { extendShareDir?: string, } -export enum OVMDarwinStatusName { +export enum OVMDarwinAppEventValue { Initializing = "Initializing", GVProxyReady = "GVProxyReady", IgnitionProgress = "IgnitionProgress", IgnitionDone = "IgnitionDone", VMReady = "VMReady", Exit = "Exit", - Error = "Error", } export interface OVMDarwinEventData { - status: { - name: OVMDarwinStatusName, - message: string, - } + app: OVMDarwinAppEventValue, + error: string, } export interface OVMDarwinInfo {