diff --git a/scripts/resource.json b/scripts/resource.json index 483494e..cebdd17 100644 --- a/scripts/resource.json +++ b/scripts/resource.json @@ -2,9 +2,9 @@ "darwin-x64": [ { "name": "ovm", - "version": "v1.0.0-RC8", + "version": "v1.0.0-RC10", "download": "https://static.oomol.com/ovm-resources/Bauklotze/{version}/ovm-amd64", - "sha256": "09d3ba46ec931658090b9a351482a50982bffaf01a98ab39b05eb48b31ab7855", + "sha256": "86d7adfca2310ea112c919fbb453d940d3474cb2d03ffe9612730d5509c61336", "out": "bin/ovm" }, { @@ -25,9 +25,9 @@ "darwin-arm64": [ { "name": "ovm", - "version": "v1.0.0-RC8", + "version": "v1.0.0-RC10", "download": "https://static.oomol.com/ovm-resources/Bauklotze/{version}/ovm-arm64", - "sha256": "fb660439b2f9240821302baaf397ef7431a65a5184d142311b582e820d723b02", + "sha256": "4db337614bc079005bda2545d381963c777da1dce5b19c29f1d5834ca2df5886", "out": "bin/ovm" }, { @@ -48,9 +48,9 @@ "win32-x64": [ { "name": "ovm", - "version": "v1.0.0-RC15", + "version": "v1.0.0-RC16", "download": "https://static.oomol.com/ovm-resources/ovm-win/{version}/ovm-amd64.exe", - "sha256": "c6e1ff99d63cd3ebf5ffa9ce175db66b65d27270896964d162eb2091c3c03470", + "sha256": "4a4a01ea8dcbf7c5bd9ec65cb91a90d5aa0a0fb2c2f918d278a09afb893973ae", "out": "ovm.exe" }, { diff --git a/src/darwin.ts b/src/darwin.ts index 27dc631..b27a94c 100644 --- a/src/darwin.ts +++ b/src/darwin.ts @@ -2,7 +2,15 @@ import { RequestDarwin } from "./request"; import cp from "node:child_process"; import type { EventReceiver } from "remitter"; import { Remitter } from "remitter"; -import type { OVMDarwinInitEvent, OVMDarwinOptions, OVMDarwinStartEvent } from "./type"; +import { + OVMDarwinInitEventValue, + OVMDarwinRunEventValue, + type OVMDarwinInitEvent, + type OVMDarwinInitEventValueType, + type OVMDarwinOptions, + type OVMDarwinRunEvent, + type OVMDarwinRunEventValueType, +} from "./type"; import { Restful } from "./event_restful"; import fs from "node:fs/promises"; import path from "node:path"; @@ -12,22 +20,22 @@ import { enableDebug, resource } from "./utils"; export class DarwinOVM extends RequestDarwin { public readonly events: { init: EventReceiver; - start: EventReceiver; + run: EventReceiver; }; readonly #events: { init: Remitter; - start: Remitter; + run: Remitter; }; - private restful: Record<"init" | "start", Restful>; + private restful: Record<"init" | "run", Restful>; private restfulWithInit: string; - private restfulWithStart: string; + private restfulWithRun: string; private constructor(private options: OVMDarwinOptions) { super(options.workspace); this.events = this.#events = { init: new Remitter(), - start: new Remitter(), + run: new Remitter(), }; } @@ -45,27 +53,27 @@ export class DarwinOVM extends RequestDarwin { private async initEventRestful(): Promise { this.restful = { init: new Restful(), - start: new Restful(), + run: new Restful(), }; this.#events.init.remitAny((o) => { return this.restful.init.events.onAny((data) => { - o.emit(data.event as keyof OVMDarwinInitEvent, data.data); + o.emit(data.event as OVMDarwinInitEventValueType, data.data); }); }); - this.#events.start.remitAny((o) => { - return this.restful.start.events.onAny((data) => { - o.emit(data.event as keyof OVMDarwinStartEvent, data.data); + this.#events.run.remitAny((o) => { + return this.restful.run.events.onAny((data) => { + o.emit(data.event as OVMDarwinRunEventValueType, data.data); }); }); const dir = await fs.mkdtemp(path.join(tmpdir(), "ovm-")); this.restfulWithInit = path.join(dir, "event-restful-init.sock"); - this.restfulWithStart = path.join(dir, "event-restful-start.sock"); + this.restfulWithRun = path.join(dir, "event-restful-run.sock"); this.restful.init.start(this.restfulWithInit); - this.restful.start.start(this.restfulWithStart); + this.restful.run.start(this.restfulWithRun); } public init(): void { @@ -118,7 +126,9 @@ export class DarwinOVM extends RequestDarwin { initTimeout .catch(() => { - this.#events.init.emit("error", "OVM init timeout"); + this.#events.init.emit(OVMDarwinInitEventValue.Error, { + value: "OVM init timeout", + }); }); } @@ -131,7 +141,7 @@ export class DarwinOVM extends RequestDarwin { reject(); }, 10 * 1000); - const disposer = this.#events.start.onceAny(() => { + const disposer = this.#events.run.onceAny(() => { clearTimeout(id); resolve(); }); @@ -141,7 +151,7 @@ export class DarwinOVM extends RequestDarwin { const ovmArgs = [ "machine", "start", - "--report-url", `unix://${this.restfulWithStart}`, + "--report-url", `unix://${this.restfulWithRun}`, "--workspace", this.options.workspace, "--ppid", String(this.options.bindPID), "default", @@ -163,7 +173,9 @@ export class DarwinOVM extends RequestDarwin { startTimeout .catch(() => { - this.#events.start.emit("error", "OVM start timeout"); + this.#events.run.emit(OVMDarwinRunEventValue.Error, { + value: "OVM run timeout", + }); }); } } diff --git a/src/event_restful.ts b/src/event_restful.ts index b4ab5b7..94fc238 100644 --- a/src/event_restful.ts +++ b/src/event_restful.ts @@ -18,7 +18,9 @@ export class Restful { response.statusCode = 200; response.end("ok"); - this.events.emit(parsedUrl.query.event as string, parsedUrl.query.message as string); + this.events.emit(parsedUrl.query.name as string, { + value: parsedUrl.query.value as string, + }); } else { response.statusCode = 404; response.end("Not Found"); diff --git a/src/index.ts b/src/index.ts index 253739e..c0ead2a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,16 +10,26 @@ export const createWindowsOVM = (options: OVMWindowsOptions): WindowsOVM => { return WindowsOVM.create(options); }; -export { OVMWindowsRunEventValue, OVMWindowsPrepareEventValue } from "./type"; +export { + OVMDarwinInitEventValue, + OVMDarwinRunEventValue, + OVMWindowsInitEventValue, + OVMWindowsRunEventValue, +} from "./type"; export type { OVMDarwinOptions, + OVMDarwinInitEventValueType, + OVMDarwinRunEventValueType, OVMDarwinInitEvent, - OVMDarwinStartEvent, + OVMDarwinRunEvent, OVMDarwinInfo, OVMDarwinState, OVMWindowsOptions, - OVMWindowsEventData, + OVMWindowsInitEventValueType, + OVMWindowsRunEventValueType, + OVMWindowsInitEvent, + OVMWindowsRunEvent, OVMWindowsInfo, } from "./type"; export type { DarwinOVM, WindowsOVM }; diff --git a/src/type.ts b/src/type.ts index a715712..d583caf 100644 --- a/src/type.ts +++ b/src/type.ts @@ -15,18 +15,31 @@ export interface OVMDarwinOptions { appendVolume?: string[]; } -export interface OVMDarwinInitEvent { - decompress: "running" | "success"; - writeConfig: "running" | "success"; - exit: void; - error: string; +export type OVMDarwinInitEventValueType = keyof typeof OVMDarwinInitEventValue; + +export enum OVMDarwinInitEventValue { + Error = "Error", + Exit = "Exit", +} + +export type OVMDarwinRunEventValueType = keyof typeof OVMDarwinRunEventValue; + +export enum OVMDarwinRunEventValue { + Ready = "Ready", + Error = "Error", + Exit = "Exit", } -export interface OVMDarwinStartEvent { - start: string; - ready: void; - exit: void; - error: string; +export type OVMDarwinInitEvent = { + [k in OVMDarwinInitEventValueType]: { + value?: string; + } +} + +export type OVMDarwinRunEvent = { + [k in OVMDarwinRunEventValueType]: { + value?: string; + } } export interface OVMDarwinInfo { @@ -69,9 +82,9 @@ export interface OVMWindowsOptions { cwd: string; } -export type OVMWindowsPrepareEventValueType = keyof typeof OVMWindowsPrepareEventValue; +export type OVMWindowsInitEventValueType = keyof typeof OVMWindowsInitEventValue; -export enum OVMWindowsPrepareEventValue { +export enum OVMWindowsInitEventValue { SystemNotSupport = "SystemNotSupport", NotSupportVirtualization = "NotSupportVirtualization", @@ -85,27 +98,27 @@ export enum OVMWindowsPrepareEventValue { UpdatingWSL = "UpdatingWSL", UpdateWSLFailed = "UpdateWSLFailed", UpdateWSLSuccess = "UpdateWSLSuccess", + Exit = "Exit", + Error = "Error", } export type OVMWindowsRunEventValueType = keyof typeof OVMWindowsRunEventValue; export enum OVMWindowsRunEventValue { - UpdatingRootFS = "UpdatingRootFS", - UpdateRootFSFailed = "UpdateRootFSFailed", - UpdateRootFSSuccess = "UpdateRootFSSuccess", - - UpdatingData = "UpdatingData", - UpdateDataFailed = "UpdateDataFailed", - UpdateDataSuccess = "UpdateDataSuccess", - - Starting = "Starting", Ready = "Ready", + Exit = "Exit", + Error = "Error", +} + +export type OVMWindowsInitEvent = { + [k in OVMWindowsInitEventValueType]: { + value?: string; + } } -export interface OVMWindowsEventData { - run: OVMWindowsRunEventValue, - prepare: OVMWindowsPrepareEventValue, - error: string, - exit: void, +export type OVMWindowsRunEvent = { + [k in OVMWindowsRunEventValueType]: { + value?: string; + } } diff --git a/src/windows.ts b/src/windows.ts index 03532a3..08649c0 100644 --- a/src/windows.ts +++ b/src/windows.ts @@ -1,15 +1,28 @@ import cp from "node:child_process"; import type { EventReceiver } from "remitter"; import { Remitter } from "remitter"; -import type { OVMWindowsEventData, OVMWindowsOptions } from "./type"; +import type { + OVMWindowsInitEvent, + OVMWindowsOptions, + OVMWindowsRunEvent, + OVMWindowsRunEventValueType, + OVMWindowsInitEventValueType, +} from "./type"; +import { OVMWindowsInitEventValue, OVMWindowsRunEventValue } from "./type"; import { Restful } from "./event_restful"; import { RequestWindows } from "./request"; import { enableDebug, resource } from "./utils"; export class WindowsOVM extends RequestWindows { - public readonly events : EventReceiver; - readonly #events: Remitter; - private restful: Record<"run" | "prepare", Restful>; + public readonly events: { + init: EventReceiver; + run: EventReceiver; + }; + readonly #events: { + init: Remitter; + run: Remitter; + }; + private restful: Record<"run" | "init", Restful>; private readonly restfulNPipeRunName: string; private readonly restfulNPipePrepareName: string; @@ -18,7 +31,10 @@ export class WindowsOVM extends RequestWindows { this.restfulNPipeRunName = `ovm-${options.name}-restful`; this.restfulNPipePrepareName = `ovm-prepare-${options.name}-restful`; - this.events = this.#events = new Remitter(); + this.events = this.#events = { + init: new Remitter(), + run: new Remitter(), + }; } public static create(options: OVMWindowsOptions): WindowsOVM { @@ -30,34 +46,34 @@ export class WindowsOVM extends RequestWindows { private initEventRestful(): void { this.restful = { run: new Restful(), - prepare: new Restful(), + init: new Restful(), }; - this.#events.remitAny((o) => { - return this.restful.run.events.onAny((data) => { - o.emit(data.event as keyof Omit, data.data); + this.#events.init.remitAny((o) => { + return this.restful.init.events.onAny((data) => { + o.emit(data.event as OVMWindowsInitEventValueType, data.data); }); }); - this.#events.remitAny((o) => { - return this.restful.prepare.events.onAny((data) => { - o.emit(data.event as keyof Omit, data.data); + this.#events.run.remitAny((o) => { + return this.restful.run.events.onAny((data) => { + o.emit(data.event as OVMWindowsRunEventValueType, data.data); }); }); this.restful.run.start(`//./pipe/${this.restfulNPipeRunName}`); - this.restful.prepare.start(`//./pipe/${this.restfulNPipePrepareName}`); + this.restful.init.start(`//./pipe/${this.restfulNPipePrepareName}`); } - public prepare(): void { - const prepareTimeout = new Promise((resolve, reject) => { + public init(): void { + const initTimeout = new Promise((resolve, reject) => { const id = setTimeout(() => { disposer(); // eslint-disable-next-line prefer-promise-reject-errors reject(); }, 30 * 1000); - const disposer = this.#events.onceAny(() => { + const disposer = this.#events.init.onceAny(() => { clearTimeout(id); resolve(); }); @@ -87,9 +103,11 @@ export class WindowsOVM extends RequestWindows { ovm.unref(); - prepareTimeout + initTimeout .catch(() => { - this.#events.emit("error", "OVM prepare timeout"); + this.#events.init.emit(OVMWindowsInitEventValue.Error, { + value: "OVM prepare timeout", + }); }); } @@ -98,14 +116,14 @@ export class WindowsOVM extends RequestWindows { return `${key}=${this.options.versions[key]}`; }).join(","); - const launchTimeout = new Promise((resolve, reject) => { + const runTimeout = new Promise((resolve, reject) => { const id = setTimeout(() => { disposer(); // eslint-disable-next-line prefer-promise-reject-errors reject(); }, 30 * 1000); - const disposer = this.#events.onceAny(() => { + const disposer = this.#events.run.onceAny(() => { clearTimeout(id); resolve(); }); @@ -138,9 +156,11 @@ export class WindowsOVM extends RequestWindows { ovm.unref(); - launchTimeout + runTimeout .catch(() => { - this.#events.emit("error", "OVM run timeout"); + this.#events.run.emit(OVMWindowsRunEventValue.Error, { + value: "OVM run timeout", + }); }); } }