Skip to content

Commit 660cf6d

Browse files
committed
replace fs.watch with chokidar in order to prevent blocking the process
1 parent 7e31b34 commit 660cf6d

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

examples/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@types/node": "^20.19.0",
2020
"ts-node": "^10.9.2",
2121
"tsx": "^4.7.0",
22-
"typescript": "^5.3.0"
22+
"typescript": "^5.3.0",
23+
"why-is-node-running": "^3.2.2"
2324
}
2425
}

packages/core/src/subsystems/Security/Vault.service/connectors/JSONFileVault.class.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import crypto from 'crypto';
1313
import fs from 'fs';
1414
import * as readlineSync from 'readline-sync';
1515
import path from 'path';
16+
import * as chokidar from 'chokidar';
1617
import { findSmythPath } from '../../../../helpers/Sysconfig.helper';
1718

1819
const console = Logger('JSONFileVault');
@@ -29,6 +30,7 @@ export class JSONFileVault extends VaultConnector {
2930
private index: any;
3031
private shared: string;
3132
private vaultFile: string;
33+
private watcher: chokidar.FSWatcher | null = null;
3234

3335
constructor(protected _settings: JSONFileVaultConfig) {
3436
super(_settings);
@@ -156,7 +158,6 @@ export class JSONFileVault extends VaultConnector {
156158
}
157159

158160
private fetchVaultData(vaultFile: string, _settings: JSONFileVaultConfig) {
159-
160161
if (fs.existsSync(vaultFile)) {
161162
try {
162163
if (_settings.fileKey && fs.existsSync(_settings.fileKey)) {
@@ -200,10 +201,21 @@ export class JSONFileVault extends VaultConnector {
200201
}
201202

202203
private initFileWatcher() {
203-
fs.watch(this.vaultFile, (eventType, filename) => {
204-
if (eventType === 'change') {
205-
this.fetchVaultData(this.vaultFile, this._settings);
206-
}
204+
this.watcher = chokidar.watch(this.vaultFile, {
205+
persistent: false, // Don't keep the process running
206+
ignoreInitial: true,
207+
});
208+
209+
this.watcher.on('change', () => {
210+
this.fetchVaultData(this.vaultFile, this._settings);
207211
});
208212
}
213+
214+
public async stop() {
215+
super.stop();
216+
if (this.watcher) {
217+
this.watcher.close();
218+
this.watcher = null;
219+
}
220+
}
209221
}

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)