From 72644571b3c331e447af21bca4312d448effb589 Mon Sep 17 00:00:00 2001 From: Lucas Araujo Date: Thu, 17 Mar 2022 10:10:20 -0300 Subject: [PATCH] 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"