From 45bf49c1ba1d6962d2e922ed4266f91f4f79a475 Mon Sep 17 00:00:00 2001 From: geekgeekrun Date: Sat, 24 Feb 2024 14:30:49 +0800 Subject: [PATCH 1/4] fix app.dock is undefined caused issue on windows --- .../ui/src/main/flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/index.ts | 2 +- packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS.ts | 2 +- 2 files changed, 2 insertions(+), 2 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 89c5a17..f3a1c4b 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 @@ -9,7 +9,7 @@ export enum DOWNLOAD_ERROR_EXIT_CODE { } export const checkAndDownloadDependenciesForInit = async () => { process.on('disconnect', () => app.exit()) - app.dock.hide() + app.dock?.hide() let pipe: null | net.Socket = null try { pipe = new net.Socket({ fd: 3 }) 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 409c93d..089f953 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 @@ -25,7 +25,7 @@ export const runAutoChat = async () => { closeBrowserWindow() app.exit() }) - app.dock.hide() + app.dock?.hide() let pipe: null | net.Socket = null try { pipe = new net.Socket({ fd: 3 }) From b115f0fbc370c9928e3e4dba7181300080f5291f Mon Sep 17 00:00:00 2001 From: geekgeekrun Date: Sun, 25 Feb 2024 09:11:45 +0800 Subject: [PATCH 2/4] enhance checkAndDownloadDependenciesForInit - adjust promise logic --- .../CHECK_AND_DOWNLOAD_DEPENDENCIES/index.ts | 67 +++++++++++-------- 1 file changed, 40 insertions(+), 27 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 f3a1c4b..65efee7 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 @@ -26,37 +26,50 @@ export const checkAndDownloadDependenciesForInit = async () => { try { let timeoutTimer = 0 - await new Promise((resolve, reject) => { - checkAndDownloadPuppeteerExecutable({ - downloadProgressCallback(downloadedBytes: number, totalBytes: number) { - clearTimeout(timeoutTimer) - if (downloadedBytes !== totalBytes) { - timeoutTimer = setTimeout(() => { - // will encounter this when network disconnected when downloading - reject(new Error('PROGRESS_NOT_CHANGED_TOO_LONG')) - }, 30 * 1000) - } - console.log(downloadedBytes / totalBytes) - pipeWriteRegardlessError( - pipe, - JSON.stringify({ - type: 'PUPPETEER_DOWNLOAD_PROGRESS', - totalBytes, - downloadedBytes - }) - ) + '\r\n' + const promiseWithResolver = (() => { + const o = {} as unknown as { + promise: Promise + resolve: (result: unknown) => void + reject: (reason: unknown) => void + } + o.promise = new Promise((resolve, reject) => { + o.resolve = resolve + o.reject = reject + }) + return o + })() + + checkAndDownloadPuppeteerExecutable({ + downloadProgressCallback(downloadedBytes: number, totalBytes: number) { + clearTimeout(timeoutTimer) + if (downloadedBytes !== totalBytes) { + timeoutTimer = setTimeout(() => { + // will encounter this when network disconnected when downloading + promiseWithResolver.reject(new Error('PROGRESS_NOT_CHANGED_TOO_LONG')) + }, 5 * 1000) } - }).then( - () => { - resolve(void 0) - }, - (err) => { - reject(err) - } - ) + console.log(downloadedBytes / totalBytes) + pipeWriteRegardlessError( + pipe, + JSON.stringify({ + type: 'PUPPETEER_DOWNLOAD_PROGRESS', + totalBytes, + downloadedBytes + }) + ) + '\r\n' + } }) + .then(() => { + promiseWithResolver.resolve(void 0) + }) + .catch((err) => { + promiseWithResolver.reject(err) + }) + + await promiseWithResolver.promise app.exit(DOWNLOAD_ERROR_EXIT_CODE.NO_ERROR) } catch (err) { + console.error(err) app.exit(DOWNLOAD_ERROR_EXIT_CODE.DOWNLOAD_ERROR) } } From 6adaf280965172ad19d7f3fa97c9356c23b44b50 Mon Sep 17 00:00:00 2001 From: geekgeekrun Date: Sat, 24 Feb 2024 23:36:20 +0800 Subject: [PATCH 3/4] 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) + }) + }) } From 1ee8acd981cd07171b7ea64e11e18ca4ccd4519f Mon Sep 17 00:00:00 2001 From: geekgeekrun Date: Sun, 25 Feb 2024 14:03:32 +0800 Subject: [PATCH 4/4] remove console, and decrease the pipe write times for downloadProgressCallback, to solve the download process is badly block at a point on windows --- .../CHECK_AND_DOWNLOAD_DEPENDENCIES/index.ts | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 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 e9f5ce5..73a0849 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 @@ -39,6 +39,7 @@ export const checkAndDownloadDependenciesForInit = async () => { return o })() + let throttleProgressTimer: number | null = null checkAndDownloadPuppeteerExecutable({ downloadProgressCallback(downloadedBytes: number, totalBytes: number) { clearTimeout(timeoutTimer) @@ -47,16 +48,30 @@ export const checkAndDownloadDependenciesForInit = async () => { // will encounter this when network disconnected when downloading promiseWithResolver.reject(new Error('PROGRESS_NOT_CHANGED_TOO_LONG')) }, 5 * 1000) + } else { + clearTimeout(throttleProgressTimer) + throttleProgressTimer = null + } + + if (!throttleProgressTimer) { + // console.log(JSON.stringify({ + // type: 'DOWNLOAD_PROGRESS_UPDATE', + // level: 'DEBUG', + // percent: downloadedBytes / totalBytes + // })) + + pipeWriteRegardlessError( + pipe, + JSON.stringify({ + type: 'PUPPETEER_DOWNLOAD_PROGRESS', + totalBytes, + downloadedBytes + }) + '\r\n' + ) + throttleProgressTimer = setTimeout(() => { + throttleProgressTimer = null + }, 2500) } - console.log(downloadedBytes / totalBytes) - pipeWriteRegardlessError( - pipe, - JSON.stringify({ - type: 'PUPPETEER_DOWNLOAD_PROGRESS', - totalBytes, - downloadedBytes - }) - ) + '\r\n' } }) .then(() => {