Skip to content

Commit aec4a94

Browse files
committed
Add info IPC command
1 parent 45dc8e6 commit aec4a94

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/ipc/index.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import path from "path";
21
import { fork } from "child_process";
32
import { connect } from "node:net";
43
import { existsSync, unlinkSync } from "fs";
54

65
import ffmpeg from "../ffmpeg";
6+
import { getStore } from "../store";
7+
import { getHumanSize, getHumanTime } from "../utils";
78

89
import type { ChildProcess, Serializable } from "child_process";
910

@@ -13,7 +14,7 @@ const SOCKETFILE = "/tmp/scrast.sock";
1314
const start = async () => {
1415
await checkSocket();
1516

16-
listener = fork(`${path.dirname(__filename)}/listener`);
17+
listener = fork(`${__dirname}/listener`);
1718
listener.on("message", handleMessage);
1819
};
1920

@@ -37,14 +38,36 @@ const handleMessage = (message: Serializable) => {
3738
case "resume":
3839
ffmpeg.resume();
3940
break;
41+
case "info":
42+
listener.send(getInfo());
43+
break;
4044
}
4145
};
4246

47+
const getInfo = () => {
48+
const { status, size, seconds } = getStore();
49+
50+
return (
51+
`Status: ${status}\n` +
52+
`Elapsed: ${getHumanTime(seconds)}\n` +
53+
`Size: ${getHumanSize(size)}`
54+
);
55+
};
56+
4357
const sendMessageToSocket = (message: string) =>
44-
new Promise((resolve, reject) => {
58+
new Promise<void | string>((resolve, reject) => {
4559
const connection = connect(SOCKETFILE);
4660

47-
connection.on("ready", () => resolve(connection.write(message)));
61+
if (message === "info") {
62+
connection.write(message);
63+
connection.on("data", (data) => resolve(data.toString()));
64+
} else {
65+
connection.on("ready", () => {
66+
connection.write(message);
67+
resolve();
68+
});
69+
}
70+
4871
connection.on("error", () => reject("socket is inactive"));
4972
});
5073

src/ipc/listener.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@ const listener = net.createServer((connection) => {
1010

1111
switch (command) {
1212
case "stop":
13-
process.send("stop");
13+
process.send(command);
14+
connection.end();
1415
break;
1516
case "pause":
16-
process.send("pause");
17+
process.send(command);
18+
connection.end();
1719
break;
1820
case "resume":
19-
process.send("resume");
21+
process.send(command);
22+
connection.end();
23+
break;
24+
case "info":
25+
process.send(command);
26+
process.on("message", (message) => connection.end(`${message}\n`));
2027
break;
2128
case "ping":
2229
connection.end("pong narashite!\n");
@@ -27,9 +34,9 @@ const listener = net.createServer((connection) => {
2734
case "ping pong narashite":
2835
connection.end("ping pong pong, girls ganbare!🍀\n");
2936
break;
37+
default:
38+
connection.end();
3039
}
31-
32-
connection.end();
3340
});
3441
});
3542

0 commit comments

Comments
 (0)