From 4a52bcfaf153f6e7c8173f77a599e2bc3e6bb56b Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Sat, 26 Oct 2024 13:29:53 -0400 Subject: [PATCH 1/2] fix: revert digcache --- package-lock.json | 8 ++++---- package.json | 2 +- src/controllers/merkleTreeController.ts | 10 +++++----- src/utils/nonce.ts | 16 ++++++++-------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index cd94d66..4faaeb0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "ISC", "dependencies": { "@dignetwork/datalayer-driver": "^0.1.28", - "@dignetwork/dig-sdk": "^0.0.1-alpha.173", + "@dignetwork/dig-sdk": "^0.0.1-alpha.174", "async-mutex": "^0.5.0", "busboy": "^1.6.0", "express": "^4.19.2", @@ -252,9 +252,9 @@ } }, "node_modules/@dignetwork/dig-sdk": { - "version": "0.0.1-alpha.173", - "resolved": "https://registry.npmjs.org/@dignetwork/dig-sdk/-/dig-sdk-0.0.1-alpha.173.tgz", - "integrity": "sha512-9zU6w+L3VOpSoR3tzHDgLeuep85sRfMPEQSuNy/gh89mfFjDwQaejCLCg8BQ1tE9eClwxRLZFux0DHiRYkXt4w==", + "version": "0.0.1-alpha.174", + "resolved": "https://registry.npmjs.org/@dignetwork/dig-sdk/-/dig-sdk-0.0.1-alpha.174.tgz", + "integrity": "sha512-dy5fXblcvgpZCkl46+LQyjiHwbtktn5nAb7Ia6GKGhpPANhCb1swm9gMPfn+SmmCUXUnFRkIcgJDm9Dk6nWJUw==", "dependencies": { "@dignetwork/datalayer-driver": "^0.1.29", "@dignetwork/dig-sdk": "^0.0.1-alpha.158", diff --git a/package.json b/package.json index ebbe8b3..5ab9861 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ ], "dependencies": { "@dignetwork/datalayer-driver": "^0.1.28", - "@dignetwork/dig-sdk": "^0.0.1-alpha.173", + "@dignetwork/dig-sdk": "^0.0.1-alpha.174", "async-mutex": "^0.5.0", "busboy": "^1.6.0", "express": "^4.19.2", diff --git a/src/controllers/merkleTreeController.ts b/src/controllers/merkleTreeController.ts index 3115771..b585e92 100644 --- a/src/controllers/merkleTreeController.ts +++ b/src/controllers/merkleTreeController.ts @@ -11,12 +11,12 @@ import { getFilePathFromSha256, Environment, DigNetwork, - DigCache, } from "@dignetwork/dig-sdk"; import { promisify } from "util"; import { getStorageLocation } from "../utils/storage"; import tmp from "tmp"; import { PassThrough } from "stream"; +import NodeCache from "node-cache"; import { generateNonce, validateNonce } from "../utils/nonce"; import Busboy from "busboy"; import fsExtra from "fs-extra"; @@ -41,7 +41,7 @@ const sessionCache: { resetTtl: () => void; }; } = {}; -const ownerCache = new DigCache({ stdTTL: ownerCacheTTL }); +const ownerCache = new NodeCache({ stdTTL: ownerCacheTTL }); /** * Creates a session directory with custom TTL logic. Each session has a TTL that can be reset @@ -450,7 +450,7 @@ export const generateFileNonce = async ( // If file does not exist, generate a nonce for the file if (!fileExists) { const nonceKey = `${storeId}_${sessionId}_${filename}`; - const nonce = await generateNonce(nonceKey); + const nonce = generateNonce(nonceKey); res.setHeader("x-nonce", nonce); } @@ -492,7 +492,7 @@ export const uploadFile = async ( ); } - if (!(await validateNonce(`${storeId}_${sessionId}_${filename}`, nonce))) { + if (!validateNonce(`${storeId}_${sessionId}_${filename}`, nonce)) { throw new HttpError(401, "Invalid nonce."); } @@ -517,7 +517,7 @@ export const uploadFile = async ( // Check if the user has write permissions to the store const cacheKey = `${publicKey}_${storeId}`; - let isOwner = await ownerCache.get(cacheKey); + let isOwner = ownerCache.get(cacheKey); if (isOwner === undefined) { const dataStore = new DataStore(storeId, { disableInitialize: true }); diff --git a/src/utils/nonce.ts b/src/utils/nonce.ts index d87b963..b6bfb57 100644 --- a/src/utils/nonce.ts +++ b/src/utils/nonce.ts @@ -1,19 +1,19 @@ import crypto from "crypto"; -import { DigCache } from '@dignetwork/dig-sdk' +import NodeCache from "node-cache"; -// 5-minute TTL for nonces -const nonceCache = new DigCache({ stdTTL: 10 * 60 }); +// Create a new NodeCache instance with a 5-minute TTL for nonces +const nonceCache = new NodeCache({ stdTTL: 10 * 60 }); /** - * Function to generate and store nonce in DigCache. + * Function to generate and store nonce in NodeCache. * @param {string} userId - Unique identifier for the user (or session). * @returns {string} - The generated nonce. */ -export const generateNonce = async (nonceKey: string): Promise => { +export const generateNonce = (nonceKey: string): string => { const nonce = crypto.randomBytes(16).toString("hex"); // Store the nonce in the cache with userId as the key - await nonceCache.set(nonceKey, nonce); + nonceCache.set(nonceKey, nonce); return nonce; }; @@ -24,8 +24,8 @@ export const generateNonce = async (nonceKey: string): Promise => { * @param {string} providedNonce - The nonce provided for validation. * @returns {boolean} - True if the nonce is valid, otherwise false. */ -export const validateNonce = async (nonceKey: string, providedNonce: string): Promise => { - const cachedNonce = await nonceCache.get(nonceKey); +export const validateNonce = (nonceKey: string, providedNonce: string): boolean => { + const cachedNonce = nonceCache.get(nonceKey); if (cachedNonce && cachedNonce === providedNonce) { nonceCache.del(nonceKey); // Delete nonce after successful validation From 314d713c26d7ef976a113ea5a83a74e575d5b063 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Sat, 26 Oct 2024 13:30:12 -0400 Subject: [PATCH 2/2] chore(release): 0.0.1-alpha.136 --- CHANGELOG.md | 7 +++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 237c727..dc3dfe4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [0.0.1-alpha.136](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.135...v0.0.1-alpha.136) (2024-10-26) + + +### Bug Fixes + +* revert digcache ([4a52bcf](https://github.com/DIG-Network/dig-propagation-server/commit/4a52bcfaf153f6e7c8173f77a599e2bc3e6bb56b)) + ### [0.0.1-alpha.135](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.134...v0.0.1-alpha.135) (2024-10-26) diff --git a/package-lock.json b/package-lock.json index 4faaeb0..d30f580 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "dig-propagation-server", - "version": "0.0.1-alpha.135", + "version": "0.0.1-alpha.136", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "dig-propagation-server", - "version": "0.0.1-alpha.135", + "version": "0.0.1-alpha.136", "license": "ISC", "dependencies": { "@dignetwork/datalayer-driver": "^0.1.28", diff --git a/package.json b/package.json index 5ab9861..d64a7fb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dig-propagation-server", - "version": "0.0.1-alpha.135", + "version": "0.0.1-alpha.136", "description": "", "type": "commonjs", "main": "./dist/index.js",