feat: add IPC handlers for Playwright browser download
This commit is contained in:
@@ -16,6 +16,7 @@ import { registerUserErpConfigHandlers } from './user-erp-config-handler'
|
||||
import { registerLoggerHandlers } from './logger-handler'
|
||||
import { registerReportHandlers } from './report-handler'
|
||||
import { registerUpdateHandlers } from './update-handler'
|
||||
import { registerPlaywrightBrowserHandlers } from './playwright-browser'
|
||||
import { createLogger, logError } from '../services/logger'
|
||||
import { serializeError, sanitizeError } from '../services/logger/error-utils'
|
||||
import { getErrorMessage, getErrorCode, isBaseError } from '../types/errors'
|
||||
@@ -105,5 +106,6 @@ export function registerIpcHandlers(): void {
|
||||
registerLoggerHandlers()
|
||||
registerReportHandlers()
|
||||
registerUpdateHandlers()
|
||||
registerPlaywrightBrowserHandlers()
|
||||
log.info('All IPC handlers registered')
|
||||
}
|
||||
|
||||
70
src/main/ipc/playwright-browser.ts
Normal file
70
src/main/ipc/playwright-browser.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Playwright Browser IPC Handlers
|
||||
* Handles browser download requests from renderer process
|
||||
*/
|
||||
|
||||
import { ipcMain, IpcMainInvokeEvent } from 'electron'
|
||||
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||
import { withErrorHandling, type IpcResult } from './index'
|
||||
import { DownloadService } from '../services/playwright-browser'
|
||||
import { ConfigManager } from '../services/config/config-manager'
|
||||
import { S3Client } from '@aws-sdk/client-s3'
|
||||
import type { DownloadProgress } from '../services/playwright-browser'
|
||||
|
||||
/**
|
||||
* Create S3 client from config
|
||||
*/
|
||||
function createS3Client(): S3Client {
|
||||
const config = ConfigManager.getInstance().getConfig().update
|
||||
if (!config) {
|
||||
throw new Error('Update config is not available')
|
||||
}
|
||||
|
||||
return new S3Client({
|
||||
region: config.region,
|
||||
endpoint: config.endpoint,
|
||||
credentials: {
|
||||
accessKeyId: config.accessKey,
|
||||
secretAccessKey: config.secretKey
|
||||
},
|
||||
forcePathStyle: true
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Track active download for cancellation
|
||||
*/
|
||||
let activeDownload: { service: DownloadService; cancelled: boolean } | null = null
|
||||
|
||||
export function registerPlaywrightBrowserHandlers(): void {
|
||||
ipcMain.handle(
|
||||
IPC_CHANNELS.PLAYWRIGHT_BROWSER_DOWNLOAD,
|
||||
async (event: IpcMainInvokeEvent): Promise<IpcResult<void>> => {
|
||||
return withErrorHandling(async () => {
|
||||
const s3Client = createS3Client()
|
||||
const service = new DownloadService({ s3Client })
|
||||
|
||||
activeDownload = { service, cancelled: false }
|
||||
|
||||
await service.downloadAll((progress: DownloadProgress) => {
|
||||
if (activeDownload?.cancelled) {
|
||||
throw new Error('Download cancelled by user')
|
||||
}
|
||||
|
||||
event.sender.send(IPC_CHANNELS.PLAYWRIGHT_BROWSER_PROGRESS, progress)
|
||||
})
|
||||
|
||||
activeDownload = null
|
||||
}, 'playwright-browser:download')
|
||||
}
|
||||
)
|
||||
|
||||
ipcMain.handle(IPC_CHANNELS.PLAYWRIGHT_BROWSER_CANCEL, async (): Promise<IpcResult<void>> => {
|
||||
return withErrorHandling(async () => {
|
||||
if (activeDownload) {
|
||||
activeDownload.cancelled = true
|
||||
activeDownload = null
|
||||
}
|
||||
}, 'playwright-browser:cancel')
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user