From 6adaf280965172ad19d7f3fa97c9356c23b44b50 Mon Sep 17 00:00:00 2001 From: geekgeekrun Date: Sat, 24 Feb 2024 23:36:20 +0800 Subject: [PATCH] WIP: use fs.WriteStream to replace net.Socket to fix stdio data event not trigger on windows TODO: the content expect no print seems printed to stdout, normal JSON mix with console.log, may cause download process of puppeteer interrupted (near 44%) --- .../CHECK_AND_DOWNLOAD_DEPENDENCIES/index.ts | 6 ++--- .../flow/GEEK_AUTO_START_CHAT_WITH_BOSS.ts | 6 ++--- packages/ui/src/main/flow/utils/pipe.ts | 23 +++++++++++-------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/packages/ui/src/main/flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/index.ts b/packages/ui/src/main/flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/index.ts index 65efee7..e9f5ce5 100644 --- a/packages/ui/src/main/flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/index.ts +++ b/packages/ui/src/main/flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/index.ts @@ -1,6 +1,6 @@ import { app } from 'electron' import checkAndDownloadPuppeteerExecutable from './check-and-download-puppeteer-executable' -import * as net from 'net' +import * as fs from 'fs' import { pipeWriteRegardlessError } from '../utils/pipe' export enum DOWNLOAD_ERROR_EXIT_CODE { @@ -10,9 +10,9 @@ export enum DOWNLOAD_ERROR_EXIT_CODE { export const checkAndDownloadDependenciesForInit = async () => { process.on('disconnect', () => app.exit()) app.dock?.hide() - let pipe: null | net.Socket = null + let pipe: null | fs.WriteStream = null try { - pipe = new net.Socket({ fd: 3 }) + pipe = fs.createWriteStream(null, { fd: 3 }) } catch { console.warn('pipe is not available') } diff --git a/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS.ts b/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS.ts index 089f953..65b90d2 100644 --- a/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS.ts +++ b/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS.ts @@ -2,7 +2,7 @@ import DingtalkPlugin from '@geekgeekrun/dingtalk-plugin/index.mjs' import { app } from 'electron' import { SyncHook, AsyncSeriesHook } from 'tapable' import { readConfigFile } from '@geekgeekrun/geek-auto-start-chat-with-boss/runtime-file-utils.mjs' -import * as net from 'net' +import * as fs from 'fs' import { checkPuppeteerExecutable, } from './CHECK_AND_DOWNLOAD_DEPENDENCIES/check-and-download-puppeteer-executable' @@ -26,9 +26,9 @@ export const runAutoChat = async () => { app.exit() }) app.dock?.hide() - let pipe: null | net.Socket = null + let pipe: null | fs.WriteStream = null try { - pipe = new net.Socket({ fd: 3 }) + pipe = fs.createWriteStream(null, { fd: 3 }) } catch { console.warn('pipe is not available') } diff --git a/packages/ui/src/main/flow/utils/pipe.ts b/packages/ui/src/main/flow/utils/pipe.ts index 69961e8..5dcadb0 100644 --- a/packages/ui/src/main/flow/utils/pipe.ts +++ b/packages/ui/src/main/flow/utils/pipe.ts @@ -1,12 +1,17 @@ -import * as net from 'net' +import * as fs from 'fs' -export const pipeWriteRegardlessError = ( - pipe: net.Socket | null, - ...writeArgs: Parameters +export const pipeWriteRegardlessError = async ( + pipe: fs.WriteStream | null, + chunk: unknown, + option? ) => { - try { - pipe?.write(...writeArgs) - } catch (error) { - console.log('pipe.write Error', error) - } + return new Promise((resolve) => { + // debugger + pipe?.write(chunk, option, (error) => { + if (error) { + console.log('pipe.write Error', error) + } + resolve(undefined) + }) + }) }