Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ipc): add powerSaveMode api in darwin #61

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/darwin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ export class DarwinOVM {
return this.request.state();
}

public async powerSaveMode(enable: boolean): Promise<void> {
await this.request.powerSaveMode(enable);
}

public async pause(): Promise<void> {
await this.request.pause();
}
Expand Down
28 changes: 21 additions & 7 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import http from "node:http";
import type { OVMDarwinInfo, OVMDarwinState } from "./type";
import type { RequestOptions } from "http";

const generateRequest = async (option: RequestOptions): Promise<string> => {
const generateRequest = async (option: RequestOptions, body?: Record<string, unknown>): Promise<string> => {
return new Promise<string>((resolve, reject) => {
http.request({
const r = http.request({
timeout: 200,
...option,
}, (response) => {
Expand All @@ -27,8 +27,13 @@ const generateRequest = async (option: RequestOptions): Promise<string> => {
})
.once("error", (error) => {
reject(error);
})
.end();
});

if (body && option.method !== "GET") {
r.write(JSON.stringify(body));
}

r.end();
});
};

Expand All @@ -39,15 +44,15 @@ export class RequestDarwin {
this.socketPath = path.join(socketDir, `${name}-restful.sock`);
}

private async request(p: string, method: string): Promise<string> {
private async request(p: string, method: string, body?: Record<string, unknown>): Promise<string> {
return await generateRequest({
socketPath: this.socketPath,
path: `http://ovm/${p}`,
method: method,
});
}, body);
}

private async send(p: string): Promise<string> {
private async send(p: string, body?: Record<string, unknown>): Promise<string> {
switch (p) {
case "info":
case "state": {
Expand All @@ -59,6 +64,9 @@ export class RequestDarwin {
case "stop": {
return this.request(p, "POST");
}
case "power-save-mode": {
return this.request(p, "PUT", body);
}
default: {
throw new Error(`Invalid request: ${p}`);
}
Expand Down Expand Up @@ -88,4 +96,10 @@ export class RequestDarwin {
public async stop(): Promise<void> {
await this.send("stop");
}

public async powerSaveMode(enable: boolean): Promise<void> {
await this.send("power-save-mode", {
enable,
});
}
}
Loading