Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update cache when scanning via cli #447

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions public/components/views/search/search.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 6 additions & 3 deletions src/commands/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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"));

Expand All @@ -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;
}
48 changes: 40 additions & 8 deletions src/http-server/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand All @@ -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();
5 changes: 2 additions & 3 deletions src/http-server/endpoints/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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...)`);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/websocket/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})`);
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/websocket/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading