From 74804df51d5172a7b08d0192c5622830ce3d3a1b Mon Sep 17 00:00:00 2001 From: thesemaphoreslim Date: Tue, 15 Oct 2024 13:57:18 -0500 Subject: [PATCH 1/2] Fix for config.json getting created with empty contents. Also added .vs to .gitignore. --- .gitignore | 3 +- package-lock.json | 83 +++++++++++++++++++++------------------ package.json | 19 ++++----- src/utils/NconfManager.ts | 13 ++++-- 4 files changed, 67 insertions(+), 51 deletions(-) diff --git a/.gitignore b/.gitignore index c075b4a..55ba255 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules .dig dist -dig.config.json \ No newline at end of file +dig.config.json +.vs \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index cb12db6..8478a09 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@dignetwork/datalayer-driver": "^0.1.29", "@dignetwork/dig-sdk": "^0.0.1-alpha.158", "archiver": "^7.0.1", + "async-mutex": "^0.5.0", "axios": "^1.7.7", "bip39": "^3.1.0", "bottleneck": "^2.19.5", @@ -25,7 +26,7 @@ "figures": "^6.1.0", "fs-extra": "^11.2.0", "ignore": "^5.3.2", - "inquirer": "^10.1.8", + "inquirer": "^10.2.2", "lodash": "^4.17.21", "merkletreejs": "^0.4.0", "nanospinner": "^1.1.0", @@ -34,30 +35,30 @@ "node-stun": "^0.1.2", "progress-stream": "^2.0.0", "proper-lockfile": "^4.1.2", - "superagent": "^10.0.0", + "superagent": "^10.1.0", "unzipper": "^0.12.3" }, "devDependencies": { "@types/archiver": "^6.0.2", - "@types/chai": "^4.3.17", + "@types/chai": "^4.3.20", "@types/cli-progress": "^3.11.6", "@types/crypto-js": "^4.2.2", "@types/fs-extra": "^11.0.4", "@types/inquirer": "^9.0.7", - "@types/lodash": "^4.17.7", - "@types/mocha": "^10.0.7", + "@types/lodash": "^4.17.10", + "@types/mocha": "^10.0.9", "@types/nconf": "^0.10.7", - "@types/node": "^22.1.0", + "@types/node": "^22.7.5", "@types/progress-stream": "^2.0.5", "@types/proper-lockfile": "^4.1.4", - "@types/superagent": "^8.1.8", + "@types/superagent": "^8.1.9", "@types/unzipper": "^0.10.10", "chai": "^5.1.1", "copyfiles": "^2.4.1", - "mocha": "^10.7.0", + "mocha": "^10.7.3", "standard-version": "^9.5.0", "ts-node": "^10.9.2", - "typescript": "^5.5.4" + "typescript": "^5.6.3" } }, "node_modules/@babel/code-frame": { @@ -271,12 +272,12 @@ } }, "node_modules/@dignetwork/dig-sdk": { - "version": "0.0.1-alpha.158", - "resolved": "https://registry.npmjs.org/@dignetwork/dig-sdk/-/dig-sdk-0.0.1-alpha.158.tgz", - "integrity": "sha512-g2YTWUH8m81KLeTpwRRDReAb11Yww6cRceFGHVx7A5pvChtmfqBCnZxs3znXgoNTrueLMIRKza8dWerEuX52bw==", + "version": "0.0.1-alpha.170", + "resolved": "https://registry.npmjs.org/@dignetwork/dig-sdk/-/dig-sdk-0.0.1-alpha.170.tgz", + "integrity": "sha512-Qj91InaV7asLscCkYVnpKv5ncgehwHNynpvCPF9FYidDSQA94klafKFQzBtXLtmmXzMDGLhsES+p7+4BC8hQTQ==", "dependencies": { "@dignetwork/datalayer-driver": "^0.1.29", - "@dignetwork/dig-sdk": "^0.0.1-alpha.124", + "@dignetwork/dig-sdk": "^0.0.1-alpha.158", "archiver": "^7.0.1", "axios": "^1.7.7", "bip39": "^3.1.0", @@ -297,6 +298,7 @@ "nanospinner": "^1.1.0", "nconf": "^0.12.1", "node-cache": "^5.1.2", + "node-stun": "^0.1.2", "progress-stream": "^2.0.0", "proper-lockfile": "^4.1.2", "superagent": "^10.0.0", @@ -423,9 +425,9 @@ } }, "node_modules/@inquirer/figures": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/@inquirer/figures/-/figures-1.0.6.tgz", - "integrity": "sha512-yfZzps3Cso2UbM7WlxKwZQh2Hs6plrbjs1QnzQDZhK2DgyCo6D8AaHps9olkNcUFlcYERMqU3uJSp1gmy3s/qQ==", + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/@inquirer/figures/-/figures-1.0.7.tgz", + "integrity": "sha512-m+Trk77mp54Zma6xLkLuY+mvanPxlE4A7yNKs2HBiyZ4UkVs28Mv5c/pgWrHeInx+USHeX/WEPzjrWrcJiQgjw==", "engines": { "node": ">=18" } @@ -859,9 +861,9 @@ "dev": true }, "node_modules/@types/mocha": { - "version": "10.0.8", - "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-10.0.8.tgz", - "integrity": "sha512-HfMcUmy9hTMJh66VNcmeC9iVErIZJli2bszuXc6julh5YGuRb/W5OnkHjwLNYdFlMis0sY3If5SEAp+PktdJjw==", + "version": "10.0.9", + "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-10.0.9.tgz", + "integrity": "sha512-sicdRoWtYevwxjOHNMPTl3vSfJM6oyW8o1wXeI7uww6b6xHg8eBznQDNSGBCDJmsE8UMxP05JgZRtsKbTqt//Q==", "dev": true }, "node_modules/@types/mute-stream": { @@ -879,9 +881,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "22.7.4", - "resolved": "https://registry.npmjs.org/@types/node/-/node-22.7.4.tgz", - "integrity": "sha512-y+NPi1rFzDs1NdQHHToqeiX2TIS79SWEAw9GYhkkx8bD0ChpfqC+n2j5OXOCpzfojBEBt6DnEnnG9MY0zk1XLg==", + "version": "22.7.5", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.7.5.tgz", + "integrity": "sha512-jML7s2NAzMWc//QSJ1a3prpk78cOPchGvXJsC3C6R6PSMoooztvRVQEz89gmBTBY1SPMaqo5teB4uNHPdetShQ==", "dependencies": { "undici-types": "~6.19.2" } @@ -1138,6 +1140,14 @@ "resolved": "https://registry.npmjs.org/async/-/async-3.2.6.tgz", "integrity": "sha512-htCUDlxyyCLMgaM3xXg0C0LW2xqfuQ6p05pCEIsXuyQ+a1koYKTuBMzRNwmybfLgvJDMd0r1LTn4+E0Ti6C2AA==" }, + "node_modules/async-mutex": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/async-mutex/-/async-mutex-0.5.0.tgz", + "integrity": "sha512-1A94B18jkJ3DYq284ohPxoXbfTA5HsQ7/Mf4DEhcyLx3Bz27Rh59iScbB6EPiP+B+joue6YCxcMXSbFC1tZKwA==", + "dependencies": { + "tslib": "^2.4.0" + } + }, "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -2677,9 +2687,9 @@ } }, "node_modules/form-data": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", - "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.1.tgz", + "integrity": "sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw==", "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", @@ -3539,13 +3549,10 @@ } }, "node_modules/loupe": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/loupe/-/loupe-3.1.1.tgz", - "integrity": "sha512-edNu/8D5MKVfGVFRhFf8aAxiTM6Wumfz5XsaatSxlD3w4R1d/WEKUTydCdPGbl9K7QG/Ca3GnDV2sIKIpXRQcw==", - "dev": true, - "dependencies": { - "get-func-name": "^2.0.1" - } + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/loupe/-/loupe-3.1.2.tgz", + "integrity": "sha512-23I4pFZHmAemUnz8WZXbYRSKYj801VDaNv9ETuMh7IrMc7VuVVSo+Z9iLE3ni30+U48iDWfi30d3twAXBYmnCg==", + "dev": true }, "node_modules/lru-cache": { "version": "10.4.3", @@ -5282,9 +5289,9 @@ } }, "node_modules/tslib": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.7.0.tgz", - "integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==" + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.0.tgz", + "integrity": "sha512-jWVzBLplnCmoaTr13V9dYbiQ99wvZRd0vNWaDRg+aVYRcjDF3nDksxFDE/+fkXnKhpnUUkmx5pK/v8mCtLVqZA==" }, "node_modules/type-detect": { "version": "4.1.0", @@ -5312,9 +5319,9 @@ "dev": true }, "node_modules/typescript": { - "version": "5.6.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.2.tgz", - "integrity": "sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw==", + "version": "5.6.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.3.tgz", + "integrity": "sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw==", "dev": true, "bin": { "tsc": "bin/tsc", diff --git a/package.json b/package.json index 16d514a..59864c2 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@dignetwork/datalayer-driver": "^0.1.29", "@dignetwork/dig-sdk": "^0.0.1-alpha.158", "archiver": "^7.0.1", + "async-mutex": "^0.5.0", "axios": "^1.7.7", "bip39": "^3.1.0", "bottleneck": "^2.19.5", @@ -41,7 +42,7 @@ "figures": "^6.1.0", "fs-extra": "^11.2.0", "ignore": "^5.3.2", - "inquirer": "^10.1.8", + "inquirer": "^10.2.2", "lodash": "^4.17.21", "merkletreejs": "^0.4.0", "nanospinner": "^1.1.0", @@ -50,29 +51,29 @@ "node-stun": "^0.1.2", "progress-stream": "^2.0.0", "proper-lockfile": "^4.1.2", - "superagent": "^10.0.0", + "superagent": "^10.1.0", "unzipper": "^0.12.3" }, "devDependencies": { "@types/archiver": "^6.0.2", - "@types/chai": "^4.3.17", + "@types/chai": "^4.3.20", "@types/cli-progress": "^3.11.6", "@types/crypto-js": "^4.2.2", "@types/fs-extra": "^11.0.4", "@types/inquirer": "^9.0.7", - "@types/lodash": "^4.17.7", - "@types/mocha": "^10.0.7", + "@types/lodash": "^4.17.10", + "@types/mocha": "^10.0.9", "@types/nconf": "^0.10.7", - "@types/node": "^22.1.0", + "@types/node": "^22.7.5", "@types/progress-stream": "^2.0.5", "@types/proper-lockfile": "^4.1.4", - "@types/superagent": "^8.1.8", + "@types/superagent": "^8.1.9", "@types/unzipper": "^0.10.10", "chai": "^5.1.1", "copyfiles": "^2.4.1", - "mocha": "^10.7.0", + "mocha": "^10.7.3", "standard-version": "^9.5.0", "ts-node": "^10.9.2", - "typescript": "^5.5.4" + "typescript": "^5.6.3" } } diff --git a/src/utils/NconfManager.ts b/src/utils/NconfManager.ts index 27f9897..5862e6b 100644 --- a/src/utils/NconfManager.ts +++ b/src/utils/NconfManager.ts @@ -1,10 +1,12 @@ import nconf from "nconf"; import fs from "fs-extra"; +import { Mutex } from 'async-mutex'; import path from "path"; import { USER_DIR_PATH } from "./config"; import { Environment } from "./Environment"; const CONF_FOLDER_PATH = Environment.DIG_FOLDER_PATH || USER_DIR_PATH; +const fileMutex = new Mutex(); export class NconfManager { private configFilePath: string; @@ -14,7 +16,8 @@ export class NconfManager { this.initializeConfig(); } - private async initializeConfig(): Promise { +private async initializeConfig(): Promise { + const release = await fileMutex.acquire(); const directory = path.dirname(this.configFilePath); if (!(await fs.pathExists(directory))) { await fs.mkdirp(directory); @@ -22,8 +25,12 @@ export class NconfManager { } if (!(await fs.pathExists(this.configFilePath))) { - await fs.writeFile(this.configFilePath, "{}"); - console.log("Configuration file created:", this.configFilePath); + try { + await fs.writeFile(this.configFilePath, "{}"); + console.log("Configuration file created:", this.configFilePath); + } finally { + release(); + } } nconf.file({ file: this.configFilePath }); From 6e919a2c479092cef35238577cc5a1a6ff2edef2 Mon Sep 17 00:00:00 2001 From: thesemaphoreslim Date: Tue, 15 Oct 2024 14:07:36 -0500 Subject: [PATCH 2/2] Wrapped entirety of function in try/finally to ensure the mutex release gets called. --- src/utils/NconfManager.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/utils/NconfManager.ts b/src/utils/NconfManager.ts index 5862e6b..13ed104 100644 --- a/src/utils/NconfManager.ts +++ b/src/utils/NconfManager.ts @@ -19,20 +19,19 @@ export class NconfManager { private async initializeConfig(): Promise { const release = await fileMutex.acquire(); const directory = path.dirname(this.configFilePath); - if (!(await fs.pathExists(directory))) { - await fs.mkdirp(directory); - console.log("Directory created:", directory); - } + try { + if (!(await fs.pathExists(directory))) { + await fs.mkdirp(directory); + console.log("Directory created:", directory); + } - if (!(await fs.pathExists(this.configFilePath))) { - try { + if (!(await fs.pathExists(this.configFilePath))) { await fs.writeFile(this.configFilePath, "{}"); console.log("Configuration file created:", this.configFilePath); - } finally { - release(); } + } finally { + release(); } - nconf.file({ file: this.configFilePath }); }