Skip to content

Commit 28dbf97

Browse files
committed
refactor: implement new payload cache
1 parent 96af191 commit 28dbf97

File tree

31 files changed

+1259
-1138
lines changed

31 files changed

+1259
-1138
lines changed

bin/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ prog
124124
.command("cache")
125125
.option("-l, --list", i18n.getTokenSync("cli.commands.cache.option_list"), false)
126126
.option("-c, --clear", i18n.getTokenSync("cli.commands.cache.option_clear"), false)
127-
.option("-f, --full", i18n.getTokenSync("cli.commands.cache.option_full"), false)
128127
.describe(i18n.getTokenSync("cli.commands.cache.desc"))
129128
.action(commands.cache.main);
130129

i18n/english.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ const cli = {
7272
missingAction: "No valid action specified. Use --help to see options.",
7373
option_list: "List cache files",
7474
option_clear: "Clear the cache",
75-
option_full: "Clear or list the full cache, including payloads",
7675
cacheTitle: "NodeSecure Cache:",
77-
scannedPayloadsTitle: "Scanned payloads available on disk:",
7876
cleared: "Cache cleared successfully!"
7977
},
8078
extractIntegrity: {

i18n/french.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ const cli = {
7272
missingAction: "Aucune action valide spécifiée. Utilisez --help pour voir les options.",
7373
option_list: "Lister les fichiers du cache",
7474
option_clear: "Nettoyer le cache",
75-
option_full: "Nettoyer ou lister le cache complet, y compris les payloads",
7675
cacheTitle: "Cache NodeSecure:",
77-
scannedPayloadsTitle: "Payloads scannés disponibles sur le disque:",
7876
cleared: "Cache nettoyé avec succès !"
7977
},
8078
extractIntegrity: {

public/components/views/search/search.css

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ body.dark .description {
289289
height: 20px;
290290
}
291291

292-
.cache-packages, .recent-packages {
292+
.cache-packages {
293293
display: flex;
294294
flex-direction: column;
295295
align-items: center;
@@ -306,20 +306,20 @@ body.dark .description {
306306
overflow: auto;
307307
}
308308

309-
body.dark .cache-packages, body.dark .recent-packages {
309+
body.dark .cache-packages {
310310
color: var(--secondary-darker);
311311
}
312312

313-
.cache-packages h1, .recent-packages h1 {
313+
.cache-packages h1 {
314314
font-family: mononoki;
315315
color: #546884;
316316
}
317317

318-
body.dark .cache-packages h1, body.dark .recent-packages h1 {
318+
body.dark .cache-packages h1 {
319319
color: white;
320320
}
321321

322-
.cache-packages .package-cache-result:has(span:hover), .recent-packages .package-cache-result:has(span:hover) {
322+
.cache-packages .package-cache-result:has(span:hover) {
323323
color: var(--secondary-darker);
324324
background: #5468842a;
325325
cursor: pointer;

public/components/views/search/search.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
>
1414
</div>
1515
</form>
16-
<div class="recent-packages">
17-
<h1>[[=z.token('search.recentPackages')]]</h1>
18-
</div>
1916
<div class="cache-packages">
2017
<h1>[[=z.token('search.packagesCache')]]</h1>
2118
</div>

public/components/views/search/search.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ export class SearchView {
5656

5757
this.#initializePackages(
5858
".cache-packages",
59-
window.scannedPackageCache
60-
);
61-
this.#initializePackages(
62-
".recent-packages",
63-
window.recentPackageCache
59+
window.cachedSpecs
6460
);
6561
}
6662

public/core/search-nav.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,25 @@ export function initSearchNav(data, options) {
4242
}
4343
}
4444

45-
function initPackagesNavigation(data) {
45+
function initPackagesNavigation(
46+
metadata
47+
) {
4648
const fragment = document.createDocumentFragment();
47-
const packages = data.mru;
4849

49-
const hasAtLeast2Packages = packages.length > 1;
50-
const hasExactly2Packages = packages.length === 2;
50+
const hasAtLeast2Packages = metadata.length > 1;
51+
const hasExactly2Packages = metadata.length === 2;
5152
const container = createDOMElement("div", {
5253
classList: ["packages"]
5354
});
5455

55-
if (packages.length === 0) {
56+
if (metadata.length === 0) {
5657
return fragment;
5758
}
59+
const current = metadata[0].spec;
5860

59-
for (const pkg of packages) {
60-
const { name, version, local } = parseNpmSpec(pkg);
61+
for (const { spec, scanType } of metadata) {
62+
const local = scanType === "cwd";
63+
const { name, version } = parseNpmSpec(spec);
6164

6265
const childs = [
6366
createDOMElement("p", { text: name }),
@@ -70,18 +73,18 @@ function initPackagesNavigation(data) {
7073
classList: ["package"],
7174
childs
7275
});
73-
pkgElement.dataset.name = pkg;
74-
if (pkg === data.current) {
75-
window.activePackage = pkg;
76+
pkgElement.dataset.name = spec;
77+
if (spec === current) {
78+
window.activePackage = spec;
7679
pkgElement.classList.add("active");
7780
}
7881
pkgElement.addEventListener("click", () => {
79-
if (window.activePackage !== pkg) {
80-
window.socket.commands.search(pkg);
82+
if (window.activePackage !== spec) {
83+
window.socket.commands.search(spec);
8184
}
8285
});
8386

84-
if (hasAtLeast2Packages && pkg !== data.root) {
87+
if (hasAtLeast2Packages) {
8588
pkgElement.appendChild(
8689
renderPackageRemoveButton(pkgElement.dataset.name, { hasExactly2Packages })
8790
);

public/main.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ let searchview;
2626
let packageInfoOpened = false;
2727

2828
document.addEventListener("DOMContentLoaded", async() => {
29-
window.scannedPackageCache = [];
30-
window.recentPackageCache = [];
29+
window.cachedSpecs = [];
3130
window.locker = null;
3231
window.settings = await new Settings().fetchUserConfig();
3332
window.i18n = await new i18n().fetch();
@@ -74,15 +73,10 @@ async function onSocketInitOrReload(event) {
7473
const data = event.detail;
7574
const { cache } = data;
7675

77-
window.scannedPackageCache = cache.availables;
78-
window.recentPackageCache = cache.lru;
76+
window.cachedSpecs = cache.map((metadata) => metadata.spec);
7977
console.log(
80-
"[INFO] Older packages are loaded!",
81-
window.scannedPackageCache
82-
);
83-
console.log(
84-
"[INFO] Recent packages are loaded!",
85-
window.recentPackageCache
78+
"[INFO] Cached specs are loaded!",
79+
window.cachedSpecs
8680
);
8781

8882
initSearchNav(cache, {

src/commands/cache.js

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
// Import Node.js Dependencies
22
import { styleText } from "node:util";
3-
import { setImmediate } from "node:timers/promises";
43

54
// Import Third-party Dependencies
65
import prettyJson from "@topcli/pretty-json";
76
import * as i18n from "@nodesecure/i18n";
8-
import { cache, config } from "@nodesecure/server";
7+
import { PayloadCache } from "@nodesecure/cache";
8+
import { config } from "@nodesecure/server";
99

1010
export async function main(options) {
1111
const {
1212
list,
13-
clear,
14-
full
13+
clear
1514
} = options;
1615

1716
await i18n.getLocalLang();
@@ -22,40 +21,35 @@ export async function main(options) {
2221
}
2322

2423
if (list) {
25-
listCache(full);
24+
await listCache();
2625
}
2726
if (clear) {
28-
await setImmediate();
29-
await clearCache(full);
27+
await clearCache();
3028
}
29+
console.log();
3130
}
3231

33-
async function listCache(full) {
34-
const paylodsList = await cache.payloadsList();
35-
console.log(styleText(["underline"], i18n.getTokenSync("cli.commands.cache.cacheTitle")));
36-
prettyJson(paylodsList);
37-
38-
if (full) {
39-
console.log(styleText(["underline"], i18n.getTokenSync("cli.commands.cache.scannedPayloadsTitle")));
40-
try {
41-
const payloads = cache.availablePayloads();
42-
prettyJson(payloads);
43-
}
44-
catch {
45-
prettyJson([]);
46-
}
47-
}
48-
}
32+
async function listCache() {
33+
const cache = new PayloadCache();
34+
await cache.load();
35+
36+
const metadata = Array.from(cache);
37+
console.log(
38+
styleText(["underline"], i18n.getTokenSync("cli.commands.cache.cacheTitle"))
39+
);
4940

50-
async function clearCache(full) {
51-
if (full) {
52-
cache.availablePayloads().forEach((pkg) => {
53-
cache.removePayload(pkg);
54-
});
41+
for (const data of metadata) {
42+
prettyJson(data);
5543
}
44+
}
5645

46+
async function clearCache() {
5747
await config.setDefault();
58-
await cache.initPayloadsList({ logging: false, reset: true });
5948

60-
console.log(styleText("green", i18n.getTokenSync("cli.commands.cache.cleared")));
49+
const cache = new PayloadCache();
50+
await cache.clear();
51+
52+
console.log(
53+
styleText("green", i18n.getTokenSync("cli.commands.cache.cleared"))
54+
);
6155
}

src/commands/http.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// Import Node.js Dependencies
22
import fs from "node:fs";
33
import path from "node:path";
4-
import crypto from "node:crypto";
54

65
// Import Third-party Dependencies
76
import kleur from "kleur";
87
import open from "open";
98
import * as SemVer from "semver";
109
import * as i18n from "@nodesecure/i18n";
1110
import {
12-
cache,
1311
logger,
1412
buildServer,
1513
WebSocketServerInstanciator
@@ -47,11 +45,8 @@ export async function start(
4745
if (runFromPayload) {
4846
assertScannerVersion(dataFilePath);
4947
}
50-
else {
51-
cache.prefix = crypto.randomBytes(4).toString("hex");
52-
}
5348

54-
const httpServer = buildServer(dataFilePath, {
49+
const { httpServer, cache } = await buildServer(dataFilePath, {
5550
port: httpPort,
5651
hotReload: enableDeveloperMode,
5752
runFromPayload,

0 commit comments

Comments
 (0)