From 87bfb71c063b40d484093ba93e6a74019c455ccb Mon Sep 17 00:00:00 2001 From: Lucas Araujo Date: Mon, 14 Feb 2022 17:45:35 -0300 Subject: [PATCH 01/10] [DDW_812] Remove unnecessary localhost read --- source/renderer/app/api/api.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/source/renderer/app/api/api.ts b/source/renderer/app/api/api.ts index 9396032cff..b2ea3d97b3 100644 --- a/source/renderer/app/api/api.ts +++ b/source/renderer/app/api/api.ts @@ -246,10 +246,7 @@ export default class AdaApi { getWallets = async (): Promise> => { logger.debug('AdaApi::getWallets called'); - const { - getHardwareWalletLocalData, - getHardwareWalletsLocalData, - } = global.daedalus.api.localStorage; + const { getHardwareWalletsLocalData } = global.daedalus.api.localStorage; try { const wallets: Array = await getWallets( @@ -281,7 +278,9 @@ export default class AdaApi { return await Promise.all( wallets.map(async (wallet: AdaWallet) => { const { id } = wallet; - const walletData = await getHardwareWalletLocalData(id); + + const walletData = hwLocalData[id]; + return _createWalletFromServerData({ ...wallet, isHardwareWallet: From 5ac0e2febc9d1639d2b1ee2e86d3712e978ce014 Mon Sep 17 00:00:00 2001 From: Lucas Araujo Date: Thu, 17 Feb 2022 16:39:33 -0300 Subject: [PATCH 02/10] [DDW_812] Stop ipc sync messages after pushingLedger 100% --- source/main/utils/handleCheckBlockReplayProgress.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/source/main/utils/handleCheckBlockReplayProgress.ts b/source/main/utils/handleCheckBlockReplayProgress.ts index cde73b58e6..aeb12906f6 100644 --- a/source/main/utils/handleCheckBlockReplayProgress.ts +++ b/source/main/utils/handleCheckBlockReplayProgress.ts @@ -81,15 +81,19 @@ export const handleCheckBlockReplayProgress = ( { progress: finalProgressPercentage, type: progressType }, mainWindow.webContents ); + + if (finalProgressPercentage === 100 && progressType === 'pushingLedger') { + clearInterval(checkBlockReplayProgressInterval); + } }; const setBlockReplayProgressCheckingInterval = () => { - setInterval(async () => { + return setInterval(async () => { checkBlockReplayProgress(); }, BLOCK_REPLAY_PROGRESS_CHECK_INTERVAL); }; // Start default interval - setBlockReplayProgressCheckingInterval(); + const checkBlockReplayProgressInterval = setBlockReplayProgressCheckingInterval(); return checkBlockReplayProgress; }; From 9a50aa740b68213bf362bbd7032e618216310727 Mon Sep 17 00:00:00 2001 From: Lucas Araujo Date: Mon, 21 Feb 2022 23:41:18 -0300 Subject: [PATCH 03/10] [DDW-812] Use value from enum --- source/main/utils/handleCheckBlockReplayProgress.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/source/main/utils/handleCheckBlockReplayProgress.ts b/source/main/utils/handleCheckBlockReplayProgress.ts index aeb12906f6..8b7a6544ad 100644 --- a/source/main/utils/handleCheckBlockReplayProgress.ts +++ b/source/main/utils/handleCheckBlockReplayProgress.ts @@ -82,7 +82,10 @@ export const handleCheckBlockReplayProgress = ( mainWindow.webContents ); - if (finalProgressPercentage === 100 && progressType === 'pushingLedger') { + if ( + finalProgressPercentage === 100 && + progressType === BlockSyncType.pushingLedger + ) { clearInterval(checkBlockReplayProgressInterval); } }; From 8443e76a149d7f4f1c0046f9080a9ed23e180246 Mon Sep 17 00:00:00 2001 From: Lucas Araujo Date: Mon, 21 Feb 2022 23:47:24 -0300 Subject: [PATCH 04/10] [DDW-812] CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bdca52f9e..b95f516a72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Features +- Improved IPC by reducing the amount of messages from periodic events ([PR 2892](https://github.com/input-output-hk/daedalus/pull/2892)) - Improved RTS flags splash screen message ([PR 2901](https://github.com/input-output-hk/daedalus/pull/2901)) - Implemented error message when trying to leave wallet without enough ada to support tokens ([PR 2783](https://github.com/input-output-hk/daedalus/pull/2783)) From 1b42c937c34a61ff895234445b4f0a7c1a2e3589 Mon Sep 17 00:00:00 2001 From: Lucas Araujo Date: Mon, 7 Mar 2022 16:36:14 -0300 Subject: [PATCH 05/10] [DDW-812] Remove interval and watch for new log file lines --- package.json | 1 + source/main/config.ts | 2 - source/main/index.ts | 2 +- source/main/utils/blockSyncProgressHelpers.ts | 5 +- .../utils/handleCheckBlockReplayProgress.ts | 63 ++++++------------- yarn.lock | 4 ++ 6 files changed, 28 insertions(+), 49 deletions(-) diff --git a/package.json b/package.json index 7c994f729d..ca2c28763d 100644 --- a/package.json +++ b/package.json @@ -271,6 +271,7 @@ "shasum": "1.0.2", "source-map-support": "0.5.19", "spectron-fake-dialog": "0.0.1", + "tail": "2.2.4", "tcp-port-used": "1.0.1", "trezor-connect": "8.2.4-extended", "unorm": "1.6.0", diff --git a/source/main/config.ts b/source/main/config.ts index f5b24e03b0..c5ea5c0972 100644 --- a/source/main/config.ts +++ b/source/main/config.ts @@ -172,8 +172,6 @@ export const DISK_SPACE_RECOMMENDED_PERCENTAGE = 15; // 15% of the total disk sp export const DISK_SPACE_CHECK_TIMEOUT = 9 * 1000; // Timeout for checking disks pace -export const BLOCK_REPLAY_PROGRESS_CHECK_INTERVAL = 1 * 1000; // 1 seconds | unit: milliseconds - // Used if token metadata server URL is not defined in launcher config export const FALLBACK_TOKEN_METADATA_SERVER_URL = 'https://metadata.cardano-testnet.iohkdev.io'; diff --git a/source/main/index.ts b/source/main/index.ts index d176250f9f..c3d1794ec7 100644 --- a/source/main/index.ts +++ b/source/main/index.ts @@ -248,7 +248,7 @@ const onAppReady = async () => { }; mainErrorHandler(onMainError); - await handleCheckBlockReplayProgress(mainWindow, launcherConfig.logsPrefix); + handleCheckBlockReplayProgress(mainWindow, launcherConfig.logsPrefix); await handleCheckDiskSpace(); if (isWatchMode) { diff --git a/source/main/utils/blockSyncProgressHelpers.ts b/source/main/utils/blockSyncProgressHelpers.ts index 7d862f84c7..85c4827b53 100644 --- a/source/main/utils/blockSyncProgressHelpers.ts +++ b/source/main/utils/blockSyncProgressHelpers.ts @@ -1,8 +1,7 @@ import moment, { Moment } from 'moment'; export function isItFreshLog(applicationStartDate: Moment, line: string) { - const [, logDate] = line.match( - /\[(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d+) UTC]/ - ); + const [, logDate] = + line.match(/\[(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d+) UTC]/) || []; return applicationStartDate.isBefore(moment.utc(logDate)); } diff --git a/source/main/utils/handleCheckBlockReplayProgress.ts b/source/main/utils/handleCheckBlockReplayProgress.ts index 8b7a6544ad..8dac130839 100644 --- a/source/main/utils/handleCheckBlockReplayProgress.ts +++ b/source/main/utils/handleCheckBlockReplayProgress.ts @@ -1,11 +1,10 @@ import { BrowserWindow } from 'electron'; import fs from 'fs'; -import moment, { Moment } from 'moment'; -import readline from 'readline'; +import moment from 'moment'; import path from 'path'; +import { Tail } from 'tail'; import { getBlockSyncProgressChannel } from '../ipc/get-block-sync-progress'; import type { GetBlockSyncProgressType } from '../../common/ipc/api'; -import { BLOCK_REPLAY_PROGRESS_CHECK_INTERVAL } from '../config'; import { BlockSyncType } from '../../common/types/cardano-node.types'; import { isItFreshLog } from './blockSyncProgressHelpers'; @@ -44,34 +43,29 @@ function getProgressType(line: string): GetBlockSyncProgressType | null { const applicationStartDate = moment.utc(); -export const handleCheckBlockReplayProgress = ( +export const handleCheckBlockReplayProgress = async ( mainWindow: BrowserWindow, logsDirectoryPath: string ) => { - const checkBlockReplayProgress = async () => { - const filename = 'node.log'; - const logFilePath = `${logsDirectoryPath}/pub/`; - const filePath = path.join(logFilePath, filename); - if (!fs.existsSync(filePath)) return; - const fileStream = fs.createReadStream(filePath); - const rl = readline.createInterface({ - input: fileStream, - }); - const progress = []; + const filename = 'node.log'; + const logFilePath = `${logsDirectoryPath}/pub/`; + const filePath = path.join(logFilePath, filename); + if (!fs.existsSync(filePath)) return; - for await (const line of rl) { - if ( - containProgressKeywords(line) && - isItFreshLog(applicationStartDate, line) - ) { - progress.push(line); - } + const tail = new Tail(filePath); + + tail.on('line', (line) => { + if ( + !( + isItFreshLog(applicationStartDate, line) && + containProgressKeywords(line) + ) + ) { + return; } - if (!progress.length) return; - const finalProgress = progress.slice(-1).pop(); - const percentage = finalProgress.match(/Progress:([\s\d.,]+)%/)?.[1]; - const progressType = getProgressType(finalProgress); + const percentage = line.match(/Progress:([\s\d.,]+)%/)?.[1]; + const progressType = getProgressType(line); if (!percentage || !progressType) { return; } @@ -81,22 +75,5 @@ export const handleCheckBlockReplayProgress = ( { progress: finalProgressPercentage, type: progressType }, mainWindow.webContents ); - - if ( - finalProgressPercentage === 100 && - progressType === BlockSyncType.pushingLedger - ) { - clearInterval(checkBlockReplayProgressInterval); - } - }; - - const setBlockReplayProgressCheckingInterval = () => { - return setInterval(async () => { - checkBlockReplayProgress(); - }, BLOCK_REPLAY_PROGRESS_CHECK_INTERVAL); - }; - - // Start default interval - const checkBlockReplayProgressInterval = setBlockReplayProgressCheckingInterval(); - return checkBlockReplayProgress; + }); }; diff --git a/yarn.lock b/yarn.lock index 9c2fd2c4c5..bbb9526835 100644 --- a/yarn.lock +++ b/yarn.lock @@ -16456,6 +16456,10 @@ table@^6.0.1: slice-ansi "^4.0.0" string-width "^4.2.0" +tail@2.2.4: + version "2.2.4" + resolved "https://registry.yarnpkg.com/tail/-/tail-2.2.4.tgz#90dd4c5a174a3fa39dcb65a1df1950a4a0093a41" + tapable@^0.1.8: version "0.1.10" resolved "https://registry.yarnpkg.com/tapable/-/tapable-0.1.10.tgz#29c35707c2b70e50d07482b5d202e8ed446dafd4" From 6ec57d83edf07be116918c9ccafc0beda0c8e323 Mon Sep 17 00:00:00 2001 From: Lucas Araujo Date: Mon, 7 Mar 2022 17:16:09 -0300 Subject: [PATCH 06/10] [DDW-812] Remove unused --- source/main/utils/handleCheckBlockReplayProgress.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/main/utils/handleCheckBlockReplayProgress.ts b/source/main/utils/handleCheckBlockReplayProgress.ts index 8dac130839..a8d36f6211 100644 --- a/source/main/utils/handleCheckBlockReplayProgress.ts +++ b/source/main/utils/handleCheckBlockReplayProgress.ts @@ -43,7 +43,7 @@ function getProgressType(line: string): GetBlockSyncProgressType | null { const applicationStartDate = moment.utc(); -export const handleCheckBlockReplayProgress = async ( +export const handleCheckBlockReplayProgress = ( mainWindow: BrowserWindow, logsDirectoryPath: string ) => { From 9c86f809a507d71be9cb648bb3c3a80da67928b4 Mon Sep 17 00:00:00 2001 From: Lucas Araujo Date: Thu, 10 Mar 2022 14:16:51 -0300 Subject: [PATCH 07/10] [DDW-812] Review: logDate fallback; Refactor logic --- source/main/utils/blockSyncProgressHelpers.ts | 3 ++- source/main/utils/handleCheckBlockReplayProgress.ts | 6 ++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/source/main/utils/blockSyncProgressHelpers.ts b/source/main/utils/blockSyncProgressHelpers.ts index 85c4827b53..6c2abfe846 100644 --- a/source/main/utils/blockSyncProgressHelpers.ts +++ b/source/main/utils/blockSyncProgressHelpers.ts @@ -3,5 +3,6 @@ import moment, { Moment } from 'moment'; export function isItFreshLog(applicationStartDate: Moment, line: string) { const [, logDate] = line.match(/\[(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d+) UTC]/) || []; - return applicationStartDate.isBefore(moment.utc(logDate)); + + return !!logDate && applicationStartDate.isBefore(moment.utc(logDate)); } diff --git a/source/main/utils/handleCheckBlockReplayProgress.ts b/source/main/utils/handleCheckBlockReplayProgress.ts index a8d36f6211..60510f5f93 100644 --- a/source/main/utils/handleCheckBlockReplayProgress.ts +++ b/source/main/utils/handleCheckBlockReplayProgress.ts @@ -56,10 +56,8 @@ export const handleCheckBlockReplayProgress = ( tail.on('line', (line) => { if ( - !( - isItFreshLog(applicationStartDate, line) && - containProgressKeywords(line) - ) + !isItFreshLog(applicationStartDate, line) || + !containProgressKeywords(line) ) { return; } From bf5d7becd73f1e1fad90f0ab5b3a799e0221f6d5 Mon Sep 17 00:00:00 2001 From: Lucas Araujo Date: Fri, 11 Mar 2022 15:50:29 -0300 Subject: [PATCH 08/10] [DDW-812] Review: Clearer logic --- source/main/utils/blockSyncProgressHelpers.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/source/main/utils/blockSyncProgressHelpers.ts b/source/main/utils/blockSyncProgressHelpers.ts index 6c2abfe846..d7802474ad 100644 --- a/source/main/utils/blockSyncProgressHelpers.ts +++ b/source/main/utils/blockSyncProgressHelpers.ts @@ -4,5 +4,9 @@ export function isItFreshLog(applicationStartDate: Moment, line: string) { const [, logDate] = line.match(/\[(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d+) UTC]/) || []; - return !!logDate && applicationStartDate.isBefore(moment.utc(logDate)); + if (!logDate) { + return false; + } + + return applicationStartDate.isBefore(moment.utc(logDate)); } From c1867973f5dedfff646b351ff9bf53a21956e3ea Mon Sep 17 00:00:00 2001 From: Lucas Araujo Date: Tue, 15 Mar 2022 11:20:52 -0300 Subject: [PATCH 09/10] [DDW-812] Remove tail dependency and copy only the necessary code from it --- package.json | 1 - .../utils/handleCheckBlockReplayProgress.ts | 2 +- source/main/utils/tail.ts | 183 ++++++++++++++++++ yarn.lock | 4 - 4 files changed, 184 insertions(+), 6 deletions(-) create mode 100644 source/main/utils/tail.ts diff --git a/package.json b/package.json index ca2c28763d..7c994f729d 100644 --- a/package.json +++ b/package.json @@ -271,7 +271,6 @@ "shasum": "1.0.2", "source-map-support": "0.5.19", "spectron-fake-dialog": "0.0.1", - "tail": "2.2.4", "tcp-port-used": "1.0.1", "trezor-connect": "8.2.4-extended", "unorm": "1.6.0", diff --git a/source/main/utils/handleCheckBlockReplayProgress.ts b/source/main/utils/handleCheckBlockReplayProgress.ts index 60510f5f93..8f423b3a60 100644 --- a/source/main/utils/handleCheckBlockReplayProgress.ts +++ b/source/main/utils/handleCheckBlockReplayProgress.ts @@ -2,7 +2,7 @@ import { BrowserWindow } from 'electron'; import fs from 'fs'; import moment from 'moment'; import path from 'path'; -import { Tail } from 'tail'; +import { Tail } from './tail'; import { getBlockSyncProgressChannel } from '../ipc/get-block-sync-progress'; import type { GetBlockSyncProgressType } from '../../common/ipc/api'; import { BlockSyncType } from '../../common/types/cardano-node.types'; diff --git a/source/main/utils/tail.ts b/source/main/utils/tail.ts new file mode 100644 index 0000000000..a0c41832f3 --- /dev/null +++ b/source/main/utils/tail.ts @@ -0,0 +1,183 @@ +import events from 'events'; +import fs from 'fs'; +import path from 'path'; +import { logger as defaultLogger } from './logging'; +import type { Logger } from '../../common/types/logging.types'; + +const ENCODING = 'utf-8'; + +export class Tail extends events.EventEmitter { + filename: string; + buffer: string; + internalDispatcher: events.EventEmitter; + queue: { start: number; end: number }[]; + isWatching: boolean; + currentCursorPos: number; + watcher: fs.FSWatcher; + rewatchId: NodeJS.Timeout; + logger: Logger; + + constructor(filename: string, logger: Logger = defaultLogger) { + super(); + this.filename = filename; + this.logger = logger; + + this.logger.info(`Tail starting...`); + this.logger.info(`filename: ${this.filename}`); + + try { + fs.accessSync(this.filename, fs.constants.F_OK); + } catch (err) { + if (err.code === 'ENOENT') { + throw err; + } + } + + this.buffer = ''; + this.internalDispatcher = new events.EventEmitter(); + this.queue = []; + this.isWatching = false; + + this.internalDispatcher.on('next', () => { + this.readBlock(); + }); + + try { + this.watch(this.latestPosition()); + } catch (err) { + this.logger.error(`watch for ${this.filename} failed: ${err}`); + this.emit('error', `watch for ${this.filename} failed: ${err}`); + } + } + + latestPosition() { + try { + return fs.statSync(this.filename).size; + } catch (err) { + this.logger.error(`size check for ${this.filename} failed: ${err}`); + this.emit('error', `size check for ${this.filename} failed: ${err}`); + throw err; + } + } + + readBlock() { + if (this.queue.length >= 1) { + const block = this.queue[0]; + if (block.end > block.start) { + const stream = fs.createReadStream(this.filename, { + start: block.start, + end: block.end - 1, + encoding: ENCODING, + }); + stream.on('error', (error) => { + this.logger.error(`Tail error: ${error}`); + this.emit('error', error); + }); + stream.on('end', () => { + this.queue.shift(); + if (this.queue.length > 0) { + this.internalDispatcher.emit('next'); + } + }); + stream.on('data', (d) => { + this.buffer += d; + const parts = this.buffer.split(/[\r]{0,1}\n/); + this.buffer = parts.pop(); + for (const chunk of parts) { + this.emit('line', chunk); + } + }); + } + } + } + + change() { + const p = this.latestPosition(); + if (p < this.currentCursorPos) { + this.currentCursorPos = p; + } else if (p > this.currentCursorPos) { + this.queue.push({ start: this.currentCursorPos, end: p }); + this.currentCursorPos = p; + if (this.queue.length === 1) { + this.internalDispatcher.emit('next'); + } + } + } + + watch(startingCursor) { + if (this.isWatching) return; + this.logger.info(`filesystem.watch present? ${fs.watch !== undefined}`); + + this.isWatching = true; + this.currentCursorPos = startingCursor; + + if (fs.watch) { + this.logger.info(`watch strategy: watch`); + this.watcher = fs.watch(this.filename, {}, (e, filename) => { + this.watchEvent(e, filename); + }); + } else { + this.logger.info(`watch strategy: watchFile`); + fs.watchFile(this.filename, {}, (curr, prev) => { + this.watchFileEvent(curr, prev); + }); + } + } + + rename(filename) { + if (filename === undefined || filename !== this.filename) { + this.unwatch(); + this.filename = path.join(path.dirname(this.filename), filename); + this.rewatchId = setTimeout(() => { + try { + this.watch(this.currentCursorPos); + } catch (ex) { + this.logger.error( + `'rename' event for ${this.filename}. File not available anymore.` + ); + this.emit('error', ex); + } + }, 1000); + } else { + this.logger.info('rename event but same filename'); + } + } + + watchEvent(e, evtFilename) { + try { + if (e === 'change') { + this.change(); + } else if (e === 'rename') { + this.rename(evtFilename); + } + } catch (err) { + this.logger.error(`watchEvent for ${this.filename} failed: ${err}`); + this.emit('error', `watchEvent for ${this.filename} failed: ${err}`); + } + } + + watchFileEvent(curr, prev) { + if (curr.size > prev.size) { + this.currentCursorPos = curr.size; + this.queue.push({ start: prev.size, end: curr.size }); + if (this.queue.length === 1) { + this.internalDispatcher.emit('next'); + } + } + } + + unwatch() { + if (this.watcher) { + this.watcher.close(); + } else { + fs.unwatchFile(this.filename); + } + if (this.rewatchId) { + clearTimeout(this.rewatchId); + this.rewatchId = undefined; + } + this.isWatching = false; + this.queue = []; + this.logger.info(`Unwatch ${this.filename}`); + } +} diff --git a/yarn.lock b/yarn.lock index bbb9526835..9c2fd2c4c5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -16456,10 +16456,6 @@ table@^6.0.1: slice-ansi "^4.0.0" string-width "^4.2.0" -tail@2.2.4: - version "2.2.4" - resolved "https://registry.yarnpkg.com/tail/-/tail-2.2.4.tgz#90dd4c5a174a3fa39dcb65a1df1950a4a0093a41" - tapable@^0.1.8: version "0.1.10" resolved "https://registry.yarnpkg.com/tapable/-/tapable-0.1.10.tgz#29c35707c2b70e50d07482b5d202e8ed446dafd4" From 72644571b3c331e447af21bca4312d448effb589 Mon Sep 17 00:00:00 2001 From: Lucas Araujo Date: Thu, 17 Mar 2022 10:10:20 -0300 Subject: [PATCH 10/10] Revert "[DDW-812] Remove tail dependency and copy only the necessary code from it" This reverts commit 920c67f5e8c3930f98a54705efa5df6caefb5515. --- package.json | 1 + .../utils/handleCheckBlockReplayProgress.ts | 2 +- source/main/utils/tail.ts | 183 ------------------ yarn.lock | 4 + 4 files changed, 6 insertions(+), 184 deletions(-) delete mode 100644 source/main/utils/tail.ts diff --git a/package.json b/package.json index 7c994f729d..ca2c28763d 100644 --- a/package.json +++ b/package.json @@ -271,6 +271,7 @@ "shasum": "1.0.2", "source-map-support": "0.5.19", "spectron-fake-dialog": "0.0.1", + "tail": "2.2.4", "tcp-port-used": "1.0.1", "trezor-connect": "8.2.4-extended", "unorm": "1.6.0", diff --git a/source/main/utils/handleCheckBlockReplayProgress.ts b/source/main/utils/handleCheckBlockReplayProgress.ts index 8f423b3a60..60510f5f93 100644 --- a/source/main/utils/handleCheckBlockReplayProgress.ts +++ b/source/main/utils/handleCheckBlockReplayProgress.ts @@ -2,7 +2,7 @@ import { BrowserWindow } from 'electron'; import fs from 'fs'; import moment from 'moment'; import path from 'path'; -import { Tail } from './tail'; +import { Tail } from 'tail'; import { getBlockSyncProgressChannel } from '../ipc/get-block-sync-progress'; import type { GetBlockSyncProgressType } from '../../common/ipc/api'; import { BlockSyncType } from '../../common/types/cardano-node.types'; diff --git a/source/main/utils/tail.ts b/source/main/utils/tail.ts deleted file mode 100644 index a0c41832f3..0000000000 --- a/source/main/utils/tail.ts +++ /dev/null @@ -1,183 +0,0 @@ -import events from 'events'; -import fs from 'fs'; -import path from 'path'; -import { logger as defaultLogger } from './logging'; -import type { Logger } from '../../common/types/logging.types'; - -const ENCODING = 'utf-8'; - -export class Tail extends events.EventEmitter { - filename: string; - buffer: string; - internalDispatcher: events.EventEmitter; - queue: { start: number; end: number }[]; - isWatching: boolean; - currentCursorPos: number; - watcher: fs.FSWatcher; - rewatchId: NodeJS.Timeout; - logger: Logger; - - constructor(filename: string, logger: Logger = defaultLogger) { - super(); - this.filename = filename; - this.logger = logger; - - this.logger.info(`Tail starting...`); - this.logger.info(`filename: ${this.filename}`); - - try { - fs.accessSync(this.filename, fs.constants.F_OK); - } catch (err) { - if (err.code === 'ENOENT') { - throw err; - } - } - - this.buffer = ''; - this.internalDispatcher = new events.EventEmitter(); - this.queue = []; - this.isWatching = false; - - this.internalDispatcher.on('next', () => { - this.readBlock(); - }); - - try { - this.watch(this.latestPosition()); - } catch (err) { - this.logger.error(`watch for ${this.filename} failed: ${err}`); - this.emit('error', `watch for ${this.filename} failed: ${err}`); - } - } - - latestPosition() { - try { - return fs.statSync(this.filename).size; - } catch (err) { - this.logger.error(`size check for ${this.filename} failed: ${err}`); - this.emit('error', `size check for ${this.filename} failed: ${err}`); - throw err; - } - } - - readBlock() { - if (this.queue.length >= 1) { - const block = this.queue[0]; - if (block.end > block.start) { - const stream = fs.createReadStream(this.filename, { - start: block.start, - end: block.end - 1, - encoding: ENCODING, - }); - stream.on('error', (error) => { - this.logger.error(`Tail error: ${error}`); - this.emit('error', error); - }); - stream.on('end', () => { - this.queue.shift(); - if (this.queue.length > 0) { - this.internalDispatcher.emit('next'); - } - }); - stream.on('data', (d) => { - this.buffer += d; - const parts = this.buffer.split(/[\r]{0,1}\n/); - this.buffer = parts.pop(); - for (const chunk of parts) { - this.emit('line', chunk); - } - }); - } - } - } - - change() { - const p = this.latestPosition(); - if (p < this.currentCursorPos) { - this.currentCursorPos = p; - } else if (p > this.currentCursorPos) { - this.queue.push({ start: this.currentCursorPos, end: p }); - this.currentCursorPos = p; - if (this.queue.length === 1) { - this.internalDispatcher.emit('next'); - } - } - } - - watch(startingCursor) { - if (this.isWatching) return; - this.logger.info(`filesystem.watch present? ${fs.watch !== undefined}`); - - this.isWatching = true; - this.currentCursorPos = startingCursor; - - if (fs.watch) { - this.logger.info(`watch strategy: watch`); - this.watcher = fs.watch(this.filename, {}, (e, filename) => { - this.watchEvent(e, filename); - }); - } else { - this.logger.info(`watch strategy: watchFile`); - fs.watchFile(this.filename, {}, (curr, prev) => { - this.watchFileEvent(curr, prev); - }); - } - } - - rename(filename) { - if (filename === undefined || filename !== this.filename) { - this.unwatch(); - this.filename = path.join(path.dirname(this.filename), filename); - this.rewatchId = setTimeout(() => { - try { - this.watch(this.currentCursorPos); - } catch (ex) { - this.logger.error( - `'rename' event for ${this.filename}. File not available anymore.` - ); - this.emit('error', ex); - } - }, 1000); - } else { - this.logger.info('rename event but same filename'); - } - } - - watchEvent(e, evtFilename) { - try { - if (e === 'change') { - this.change(); - } else if (e === 'rename') { - this.rename(evtFilename); - } - } catch (err) { - this.logger.error(`watchEvent for ${this.filename} failed: ${err}`); - this.emit('error', `watchEvent for ${this.filename} failed: ${err}`); - } - } - - watchFileEvent(curr, prev) { - if (curr.size > prev.size) { - this.currentCursorPos = curr.size; - this.queue.push({ start: prev.size, end: curr.size }); - if (this.queue.length === 1) { - this.internalDispatcher.emit('next'); - } - } - } - - unwatch() { - if (this.watcher) { - this.watcher.close(); - } else { - fs.unwatchFile(this.filename); - } - if (this.rewatchId) { - clearTimeout(this.rewatchId); - this.rewatchId = undefined; - } - this.isWatching = false; - this.queue = []; - this.logger.info(`Unwatch ${this.filename}`); - } -} diff --git a/yarn.lock b/yarn.lock index 9c2fd2c4c5..bbb9526835 100644 --- a/yarn.lock +++ b/yarn.lock @@ -16456,6 +16456,10 @@ table@^6.0.1: slice-ansi "^4.0.0" string-width "^4.2.0" +tail@2.2.4: + version "2.2.4" + resolved "https://registry.yarnpkg.com/tail/-/tail-2.2.4.tgz#90dd4c5a174a3fa39dcb65a1df1950a4a0093a41" + tapable@^0.1.8: version "0.1.10" resolved "https://registry.yarnpkg.com/tapable/-/tapable-0.1.10.tgz#29c35707c2b70e50d07482b5d202e8ed446dafd4"