Skip to content

Commit

Permalink
feat(interface): differentiate local scans from npm scans
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreDemailly committed Jan 19, 2025
1 parent 88b6aaf commit 4230c5d
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 28 deletions.
5 changes: 3 additions & 2 deletions public/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ export function createLink(href, text = null) {

export function parseNpmSpec(spec) {
const parts = spec.split("@");
const [version, local] = parts[parts.length - 1].split("#");

return spec.startsWith("@") ?
{ name: `@${parts[1]}`, version: parts[2] } :
{ name: parts[0], version: parts[1] };
{ name: `@${parts[1]}`, version, local: Boolean(local) } :
{ name: parts[0], version, local: Boolean(local) };
}

export function parseRepositoryUrl(repository = {}, defaultValue = null) {
Expand Down
4 changes: 4 additions & 0 deletions public/components/searchbar/searchbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ div.search-result-pannel .package+.package {
display: block;
}

#search-nav .packages>.package>b:last-of-type:not(:first-of-type) {
background: #f57c00;
}

#search-nav .packages>.package>b{
font-weight: bold;
font-size: 12px;
Expand Down
12 changes: 12 additions & 0 deletions public/components/views/search/search.css
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,18 @@ input:-webkit-autofill {
border-radius: 4px;
}

.package-result>span>b {
background: #f57c00;
color: white;
font-weight: bold;
font-size: 12px;
margin-left: 5px;
padding: 0 5px;
border-radius: 2px;
font-family: "Roboto";
letter-spacing: 1px;
}

.package-result .description {
font-size: 14px;
color: rgb(78, 76, 76);
Expand Down
5 changes: 3 additions & 2 deletions public/components/views/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { getJSON, NodeSecureDataSet, NodeSecureNetwork } from "@nodesecure/vis-network";

// Import Internal Dependencies
import { currentLang, debounce, createDOMElement } from "../../../common/utils.js";
import { currentLang, debounce, createDOMElement, parseNpmSpec } from "../../../common/utils.js";

// CONSTANTS
const kMinPackageNameLength = 2;
Expand Down Expand Up @@ -162,10 +162,11 @@ export class SearchView {
cachePackagesElement.appendChild(h1Element);

for (const pkg of window.scannedPackageCache) {
const { name, version, local } = parseNpmSpec(pkg);
const pkgElement = document.createElement("div");
pkgElement.classList.add("package-result");
const pkgSpanElement = document.createElement("span");
pkgSpanElement.textContent = pkg;
pkgSpanElement.innerHTML = `${name}@${version}${local ? " <b>local</b>" : ""}`;
pkgSpanElement.addEventListener("click", () => {
window.socket.send(JSON.stringify({ action: "SEARCH", pkg }));
}, { once: true });
Expand Down
16 changes: 10 additions & 6 deletions public/core/search-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ function initPackagesNavigation(data) {
});

for (const pkg of packages) {
const { name, version } = parseNpmSpec(pkg);

const { name, version, local } = parseNpmSpec(pkg);

const childs = [
createDOMElement("p", { text: name }),
createDOMElement("b", { text: `v${version}` })
];
if (local) {
childs.push(createDOMElement("b", { text: "local" }));
}
const pkgElement = createDOMElement("div", {
classList: ["package"],
childs: [
createDOMElement("p", { text: name }),
createDOMElement("b", { text: `v${version}` })
]
childs
});
pkgElement.dataset.name = pkg;
if (pkg === data.current) {
Expand Down
13 changes: 10 additions & 3 deletions src/commands/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function cwd(options) {
initLogger(void 0, !silent)
);

return await logAndWrite(payload, output);
return await logAndWrite(payload, output, { local: true });
}

export async function from(spec, options) {
Expand Down Expand Up @@ -154,7 +154,9 @@ function initLogger(spec, verbose = true) {
return logger;
}

async function logAndWrite(payload, output = "nsecure-result") {
async function logAndWrite(payload, output = "nsecure-result", options = {}) {
const { local = false } = options;

if (payload === null) {
console.log(i18n.getTokenSync("cli.no_dep_to_proceed"));

Expand All @@ -170,6 +172,11 @@ async function logAndWrite(payload, output = "nsecure-result") {

const ret = JSON.stringify(payload, null, 2);

if (local) {
// FIXME: would it make more sense to manage this directly within Scanner?
Object.assign(ret, { local });
}

const fileName = path.extname(output) === ".json" ?
filenamify(output) :
`${filenamify(output)}.json`;
Expand All @@ -180,7 +187,7 @@ async 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 });
await appCache.setRootPayload(payload, { logging: false, local });

return filePath;
}
26 changes: 18 additions & 8 deletions src/http-server/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,40 +116,50 @@ class _AppCache {
logger.info(`[cache|init](packagesInFolder: ${packagesInFolder})`);
}

await cacache.put(CACHE_PATH, kPayloadsCache, JSON.stringify({ older: packagesInFolder, current: null, lru: [] }));
await cacache.put(CACHE_PATH, kPayloadsCache, JSON.stringify({
older: packagesInFolder,
current: null,
lru: []
}));
}

removePayload(pkg) {
fs.rmSync(path.join(kPayloadsPath, pkg.replaceAll("/", "-")));
fs.rmSync(path.join(kPayloadsPath, pkg.replaceAll("/", "-")), { force: true });
}

async removeLastLRU() {
const { lru, lastUsed, older, root } = await this.payloadsList();
const { lru, lastUsed, older, ...cache } = await this.payloadsList();
if (lru.length < kMaxPayloads) {
return { lru, older, lastUsed, root };
return {
...cache,
lru,
older,
lastUsed
};
}
const packageToBeRemoved = Object.keys(lastUsed)
.filter((key) => lru.includes(key))
.sort((a, b) => lastUsed[a] - lastUsed[b])[0];

return {
...cache,
lru: lru.filter((pkg) => pkg !== packageToBeRemoved),
older: [...older, packageToBeRemoved],
lastUsed,
root
lastUsed
};
}

async setRootPayload(payload, options) {
const { logging = true } = options;
const { logging = true, local = false } = options;

const version = Object.keys(payload.dependencies[payload.rootDependencyName].versions)[0];
const pkg = `${payload.rootDependencyName}@${version}`;
const pkg = `${payload.rootDependencyName}@${version}${local ? "#local" : ""}`;
this.updatePayload(pkg, payload);

await this.initPayloadsList({ logging });

const { lru, older, lastUsed } = await this.removeLastLRU();

const updatedPayloadsCache = {
lru: [...new Set([...lru, pkg])],
older,
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/endpoints/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function get(_req, res) {

const payload = JSON.parse(fs.readFileSync(kDefaultPayloadPath, "utf-8"));
const version = Object.keys(payload.dependencies[payload.rootDependencyName].versions)[0];
const formatted = `${payload.rootDependencyName}@${version}`;
const formatted = `${payload.rootDependencyName}@${version}${payload.local ? "#local" : ""}`;
const payloadsList = {
lru: [formatted],
current: formatted,
Expand Down
13 changes: 7 additions & 6 deletions src/http-server/websocket/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ export async function search(ws, pkg) {

return;
}
const { lru, older, lastUsed, root } = await appCache.removeLastLRU();

const { lru, older, lastUsed, ...updatedCache } = await appCache.removeLastLRU();
const updatedList = {
...updatedCache,
lru: [...new Set([...lru, pkg])],
current: pkg,
older: older.filter((pckg) => pckg !== pkg),
lastUsed: { ...lastUsed, [pkg]: Date.now() },
root
lastUsed: { ...lastUsed, [pkg]: Date.now() }
};
await appCache.updatePayloadsList(updatedList);

Expand All @@ -57,15 +58,15 @@ export async function search(ws, pkg) {
logger.info(`[ws|search](scan ${pkg} done|cache: updated)`);

// update the payloads list
const { lru, older, lastUsed, root } = await appCache.removeLastLRU();
const { lru, older, lastUsed, ...cache } = await appCache.removeLastLRU();
lru.push(pkg);
appCache.updatePayload(pkg, payload);
const updatedList = {
...cache,
lru: [...new Set(lru)],
older,
lastUsed: { ...lastUsed, [pkg]: Date.now() },
current: pkg,
root
current: pkg
};
await appCache.updatePayloadsList(updatedList);

Expand Down

0 comments on commit 4230c5d

Please sign in to comment.