diff --git a/package.json b/package.json index e2cfd12..39fdc26 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,9 @@ "version": "0.0.1", "type": "module", "scripts": { - "start": "node server.js", - "dev": "nodemon server.js", - "test": "NODE_OPTIONS=--experimental-vm-modules npx mocha --timeout 10000 src/app.test.js " + "start": "node -r dotenv/config server.js", + "dev": "nodemon -r dotenv/config server.js", + "test": "NODE_OPTIONS=--experimental-vm-modules npx mocha --timeout 10000 -r dotenv/config dotenv_config_path=src/test-fixtures/.env.testing src/app.test.js " }, "dependencies": { "@digitalbazaar/did-io": "^2.0.0", @@ -25,6 +25,7 @@ "express": "~4.16.1", "jsonld-document-loader": "^2.0.0", "keyv": "^4.5.2", + "keyv-file": "^0.2.0", "morgan": "~1.9.1" }, "devDependencies": { diff --git a/src/test-fixtures/.env.testing b/src/test-fixtures/.env.testing new file mode 100644 index 0000000..3817e23 --- /dev/null +++ b/src/test-fixtures/.env.testing @@ -0,0 +1 @@ +PERSIST_TO_FILE=/Users/jameschartrand/Documents/github/dcc/transaction-service/src/data/transactionsFile.json \ No newline at end of file diff --git a/src/transactionManager.js b/src/transactionManager.js index 8588faf..daf732a 100644 --- a/src/transactionManager.js +++ b/src/transactionManager.js @@ -3,13 +3,32 @@ */ import crypto from 'crypto'; import Keyv from 'keyv'; +import {KeyvFile} from 'keyv-file' import { verifyDIDAuth } from './didAuth.js'; +const persistToFile = process.env.PERSIST_TO_FILE +const defaultTimeToLive = process.env.DEFAULT_TTL = 1000 * 60 * 10; // keyv entry expires after ten minutes + let keyv; -const expiresAfter = 1000 * 60 * 10; // keyv entry expires after ten minutes + export const initializeTransactionManager = () => { - if (!keyv) keyv = new Keyv(); + if (!keyv) { + if (persistToFile) { + keyv = new Keyv({ + store: new KeyvFile({ + filename: persistToFile, // the file path to store the data + expiredCheckDelay: 4 * 3600 * 1000, // ms (so every 4 hours) how often to check for and remove expired records + writeDelay: 100, // ms, batch write to disk in a specific duration, enhance write performance. + encode: JSON.stringify, // serialize function + decode: JSON.parse // deserialize function + }) + }) + } else { + keyv = new Keyv(); + } + } + } /** @@ -70,8 +89,9 @@ const getProcessRecordFnForExchangeHostAndTenant = (exchangeHost, tenantName) => record.exchangeHost = exchangeHost record.transactionId = crypto.randomUUID() record.exchangeId = crypto.randomUUID() - - await keyv.set(record.exchangeId, record, expiresAfter); + + const timeToLive = record.timeToLive || defaultTimeToLive + await keyv.set(record.exchangeId, record, timeToLive); // directDeepLink bypasses the VPR step and assumes the wallet knows to send a DIDAuth. const directDeepLink = `https://lcw.app/request.html?issuer=issuer.example.com&auth_type=bearer&challenge=${record.transactionId}&vc_request_url=${exchangeHost}/exchange/${record.exchangeId}/${record.transactionId}`