1- import path from "path" ;
21import { fork } from "child_process" ;
32import { connect } from "node:net" ;
43import { existsSync , unlinkSync } from "fs" ;
54
65import ffmpeg from "../ffmpeg" ;
6+ import { getStore } from "../store" ;
7+ import { getHumanSize , getHumanTime } from "../utils" ;
78
89import type { ChildProcess , Serializable } from "child_process" ;
910
@@ -13,7 +14,7 @@ const SOCKETFILE = "/tmp/scrast.sock";
1314const 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+
4357const 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
0 commit comments