Skip to content

Commit 88dea39

Browse files
authored
feat(cli): load payload from cache when available (#725)
1 parent c70c65f commit 88dea39

7 files changed

Lines changed: 46 additions & 10 deletions

File tree

i18n/english.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const cli = {
1919
executionTime: tS`The error occured at ${0} during the execution`,
2020
stack: tS`Stack: ${0}`
2121
},
22+
cache: {
23+
found: tS`${0} found in the cache`
24+
},
2225
commands: {
2326
option_depth: "Maximum dependencies depth to fetch",
2427
option_output: "Json file output name",

i18n/french.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const cli = {
1919
executionTime: tS`L'erreur s'est produite à ${0} pendant l'exécution`,
2020
stack: tS`Stack: ${0}`
2121
},
22+
cache: {
23+
found: tS`${0} trouvé dans le cache`
24+
},
2225
commands: {
2326
option_depth: "Niveau de profondeur de dépendances maximum à aller chercher",
2427
option_output: "Nom de sortie du fichier json",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
"@nodesecure/ossf-scorecard-sdk": "4.0.1",
111111
"@nodesecure/rc": "5.5.0",
112112
"@nodesecure/report": "4.2.2",
113-
"@nodesecure/scanner": "10.8.0",
113+
"@nodesecure/scanner": "10.10.0",
114114
"@nodesecure/server": "1.0.0",
115115
"@nodesecure/utils": "^2.2.0",
116116
"@nodesecure/vulnera": "3.1.0",

src/commands/scanner.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import filenamify from "filenamify";
99
import { Spinner } from "@topcli/spinner";
1010
import * as i18n from "@nodesecure/i18n";
1111
import * as scanner from "@nodesecure/scanner";
12+
import { PayloadCache } from "@nodesecure/cache";
1213

1314
// Import Internal Dependencies
1415
import kleur from "../utils/styleText.js";
@@ -30,10 +31,13 @@ export async function auto(spec, options) {
3031
}
3132
};
3233

34+
const cache = new PayloadCache();
35+
await cache.load();
36+
3337
const payloadFile = await (
3438
typeof spec === "string" ?
35-
from(spec, optionsWithContacts) :
36-
cwd(optionsWithContacts)
39+
from(spec, optionsWithContacts, cache) :
40+
cwd(optionsWithContacts, cache)
3741
);
3842
try {
3943
if (payloadFile !== null) {
@@ -62,7 +66,7 @@ export async function auto(spec, options) {
6266
}
6367
}
6468

65-
export async function cwd(options) {
69+
export async function cwd(options, cache) {
6670
const {
6771
depth: maxDepth = Infinity,
6872
output,
@@ -85,15 +89,30 @@ export async function cwd(options) {
8589
contacts: parseContacts(contacts)
8690
},
8791
isVerbose: verbose,
88-
workers: true
92+
workers: true,
93+
async cacheLookup(packageJSON, integrity) {
94+
if (integrity === null) {
95+
return null;
96+
}
97+
98+
const spec = `${packageJSON.name}@${packageJSON.version}`;
99+
const cachedPayload = await cache.findByIntegrity(integrity);
100+
if (cachedPayload !== null) {
101+
console.log(
102+
kleur.gray().bold(i18n.getTokenSync("cli.cache.found", kleur.green().bold(spec)))
103+
);
104+
}
105+
106+
return cachedPayload;
107+
}
89108
},
90109
initLogger(void 0, !silent)
91110
);
92111

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

96-
export async function from(spec, options) {
115+
export async function from(spec, options, cache) {
97116
const {
98117
depth: maxDepth = Infinity,
99118
output,
@@ -112,7 +131,18 @@ export async function from(spec, options) {
112131
contacts: parseContacts(contacts)
113132
},
114133
isVerbose: verbose,
115-
workers: true
134+
workers: true,
135+
async cacheLookup(manifest) {
136+
const spec = `${manifest.name}@${manifest.version}`;
137+
const cachedPayload = await cache.findBySpec(spec);
138+
if (cachedPayload !== null) {
139+
console.log(
140+
kleur.gray().bold(i18n.getTokenSync("cli.cache.found", kleur.green().bold(spec)))
141+
);
142+
}
143+
144+
return cachedPayload;
145+
}
116146
},
117147
initLogger(spec, !silent)
118148
);

workspaces/cache/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"author": "GENTILHOMME Thomas <gentilhomme.thomas@gmail.com>",
2929
"license": "MIT",
3030
"dependencies": {
31-
"@nodesecure/scanner": "10.8.0",
31+
"@nodesecure/scanner": "10.10.0",
3232
"cacache": "20.0.4"
3333
},
3434
"devDependencies": {

workspaces/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"@nodesecure/i18n": "4.1.0",
3838
"@nodesecure/npm-registry-sdk": "4.5.2",
3939
"@nodesecure/ossf-scorecard-sdk": "4.0.1",
40-
"@nodesecure/scanner": "10.8.0",
40+
"@nodesecure/scanner": "10.10.0",
4141
"cacache": "20.0.4",
4242
"chokidar": "5.0.0",
4343
"find-my-way": "9.5.0",

workspaces/vis-network/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@
4343
},
4444
"devDependencies": {
4545
"@nodesecure/flags": "3.0.3",
46-
"@nodesecure/scanner": "10.7.0"
46+
"@nodesecure/scanner": "10.10.0"
4747
}
4848
}

0 commit comments

Comments
 (0)