From 08c548e201a8657ba0f2c87697c88adfaaec686f Mon Sep 17 00:00:00 2001 From: Pierre Demailly Date: Tue, 14 Jan 2025 23:32:16 +0100 Subject: [PATCH] fix: update cache when scanning via cli --- public/components/views/search/search.css | 2 + src/commands/scanner.js | 9 +++-- src/http-server/cache.js | 48 +++++++++++++++++++---- src/http-server/endpoints/data.js | 5 +-- src/http-server/websocket/remove.js | 2 +- src/http-server/websocket/search.js | 2 +- 6 files changed, 52 insertions(+), 16 deletions(-) diff --git a/public/components/views/search/search.css b/public/components/views/search/search.css index aefdbc68..494df83e 100644 --- a/public/components/views/search/search.css +++ b/public/components/views/search/search.css @@ -234,6 +234,8 @@ input:-webkit-autofill { border: 1px solid #54688424; padding: 10px; border-radius: 4px; + max-height: calc(100vh - 380px); + overflow: auto; } .cache-packages h1 { diff --git a/src/commands/scanner.js b/src/commands/scanner.js index 01536dba..bfe2aa13 100644 --- a/src/commands/scanner.js +++ b/src/commands/scanner.js @@ -13,6 +13,7 @@ import * as Scanner from "@nodesecure/scanner"; // Import Internal Dependencies import * as http from "./http.js"; +import { appCache } from "../http-server/cache.js"; export async function auto(spec, options) { const { keep, ...commandOptions } = options; @@ -59,7 +60,7 @@ export async function cwd(options) { initLogger(void 0, !silent) ); - return logAndWrite(payload, output); + return await logAndWrite(payload, output); } export async function from(spec, options) { @@ -71,7 +72,7 @@ export async function from(spec, options) { initLogger(spec, !silent) ); - return logAndWrite(payload, output); + return await logAndWrite(payload, output); } function initLogger(spec, verbose = true) { @@ -153,7 +154,7 @@ function initLogger(spec, verbose = true) { return logger; } -function logAndWrite(payload, output = "nsecure-result") { +async function logAndWrite(payload, output = "nsecure-result") { if (payload === null) { console.log(i18n.getTokenSync("cli.no_dep_to_proceed")); @@ -179,5 +180,7 @@ function logAndWrite(payload, output = "nsecure-result") { console.log(kleur.white().bold(i18n.getTokenSync("cli.successfully_written_json", kleur.green().bold(filePath)))); console.log(""); + await appCache.setRootPayload(payload, { logging: false }); + return filePath; } diff --git a/src/http-server/cache.js b/src/http-server/cache.js index c2b9a753..2b6c312a 100644 --- a/src/http-server/cache.js +++ b/src/http-server/cache.js @@ -34,7 +34,7 @@ class _AppCache { } updatePayload(pkg, payload) { - fs.writeFileSync(path.join(kPayloadsPath, pkg), JSON.stringify(payload)); + fs.writeFileSync(path.join(kPayloadsPath, pkg.replaceAll("/", "-")), JSON.stringify(payload)); } async getPayload(pkg) { @@ -90,25 +90,37 @@ class _AppCache { logger.info(`[cache|init](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`); await cacache.put(CACHE_PATH, kPayloadsCache, JSON.stringify(payloadsList)); - this.updatePayload(formatted.replaceAll("/", "-"), payload); + this.updatePayload(formatted, payload); } - async initPayloadsList() { + async initPayloadsList(options = {}) { + const { logging = true } = options; + + try { + // prevent re-initialization of the cache + await cacache.get(CACHE_PATH, kPayloadsCache); + + return; + } + catch { + // Do nothing. + } const packagesInFolder = fs.readdirSync(kPayloadsPath); if (packagesInFolder.length === 0) { - this.#initDefaultPayloadsList(); + await this.#initDefaultPayloadsList(); return; } - const list = packagesInFolder.map(({ name }) => name); - logger.info(`[cache|init](list: ${list})`); + if (logging) { + logger.info(`[cache|init](packagesInFolder: ${packagesInFolder})`); + } - await cacache.put(CACHE_PATH, kPayloadsCache, JSON.stringify({ list, current: list[0] })); + await cacache.put(CACHE_PATH, kPayloadsCache, JSON.stringify({ older: packagesInFolder, current: null, lru: [] })); } removePayload(pkg) { - fs.rmSync(path.join(kPayloadsPath, pkg)); + fs.rmSync(path.join(kPayloadsPath, pkg.replaceAll("/", "-"))); } async removeLastLRU() { @@ -127,6 +139,26 @@ class _AppCache { root }; } + + async setRootPayload(payload, options) { + const { logging = true } = options; + + const version = Object.keys(payload.dependencies[payload.rootDependencyName].versions)[0]; + const pkg = `${payload.rootDependencyName}@${version}`; + this.updatePayload(pkg, payload); + + await this.initPayloadsList({ logging }); + + const { lru, older, lastUsed } = await this.removeLastLRU(); + const updatedPayloadsCache = { + lru: [...new Set([...lru, pkg])], + older, + lastUsed: { ...lastUsed, [pkg]: Date.now() }, + current: pkg, + root: pkg + }; + await this.updatePayloadsList(updatedPayloadsCache); + } } export const appCache = new _AppCache(); diff --git a/src/http-server/endpoints/data.js b/src/http-server/endpoints/data.js index 21914efb..aa28786a 100644 --- a/src/http-server/endpoints/data.js +++ b/src/http-server/endpoints/data.js @@ -18,8 +18,7 @@ export async function get(_req, res) { logger.info(`[data|get](current: ${current})`); logger.debug(`[data|get](lru: ${lru})`); - const formatted = current.replaceAll("/", "-"); - send(res, 200, await appCache.getPayload(formatted)); + send(res, 200, await appCache.getPayload(current)); } catch { logger.error(`[data|get](No cache yet. Creating one...)`); @@ -39,7 +38,7 @@ export async function get(_req, res) { logger.info(`[data|get](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`); await appCache.updatePayloadsList(payloadsList); - appCache.updatePayload(formatted.replaceAll("/", "-"), payload); + appCache.updatePayload(formatted, payload); logger.info(`[data|get](cache: created|payloadsList: ${payloadsList.lru})`); send(res, 200, payload); diff --git a/src/http-server/websocket/remove.js b/src/http-server/websocket/remove.js index 005529fe..abdac323 100644 --- a/src/http-server/websocket/remove.js +++ b/src/http-server/websocket/remove.js @@ -74,7 +74,7 @@ export async function remove(ws, pkg) { })); } - appCache.removePayload(formattedPkg.replaceAll("/", "-")); + appCache.removePayload(formattedPkg); } catch (error) { logger.error(`[ws|remove](error: ${error.message})`); diff --git a/src/http-server/websocket/search.js b/src/http-server/websocket/search.js index 8837b045..cad88c21 100644 --- a/src/http-server/websocket/search.js +++ b/src/http-server/websocket/search.js @@ -59,7 +59,7 @@ export async function search(ws, pkg) { // update the payloads list const { lru, older, lastUsed, root } = await appCache.removeLastLRU(); lru.push(pkg); - appCache.updatePayload(pkg.replaceAll("/", "-"), payload); + appCache.updatePayload(pkg, payload); const updatedList = { lru: [...new Set(lru)], older,