Skip to content

Commit 96af191

Browse files
refactor(cache): separate web-ui-settings cache from payload cache (#632)
Update workspaces/cache/docs/AppCache.md Update workspaces/cache/docs/AppCache.md Update workspaces/cache/docs/AppCache.md Update workspaces/cache/docs/AppCache.md Co-authored-by: PierreDemailly <39910767+PierreDemailly@users.noreply.github.com>
1 parent 74e2124 commit 96af191

File tree

20 files changed

+676
-479
lines changed

20 files changed

+676
-479
lines changed

src/commands/cache.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { setImmediate } from "node:timers/promises";
55
// Import Third-party Dependencies
66
import prettyJson from "@topcli/pretty-json";
77
import * as i18n from "@nodesecure/i18n";
8-
import { cache } from "@nodesecure/server";
8+
import { cache, config } from "@nodesecure/server";
99

1010
export async function main(options) {
1111
const {
@@ -54,6 +54,7 @@ async function clearCache(full) {
5454
});
5555
}
5656

57+
await config.setDefault();
5758
await cache.initPayloadsList({ logging: false, reset: true });
5859

5960
console.log(styleText("green", i18n.getTokenSync("cli.commands.cache.cleared")));

workspaces/cache/README.md

Lines changed: 4 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# `cache`
22

3-
[![version](https://img.shields.io/github/package-json/v/NodeSecure/Cli?filename=workspaces%cache%2Fpackage.json&style=for-the-badge)](https://www.npmjs.com/package/@nodesecure/cache)
3+
[![version](https://img.shields.io/github/package-json/v/NodeSecure/Cli?filename=workspaces%2Fcache%2Fpackage.json&style=for-the-badge)](https://www.npmjs.com/package/@nodesecure/cache)
44
[![OpenSSF
55
Scorecard](https://api.securityscorecards.dev/projects/github.com/NodeSecure/cli/badge?style=for-the-badge)](https://api.securityscorecards.dev/projects/github.com/NodeSecure/cli)
66
[![mit](https://img.shields.io/github/license/NodeSecure/Cli?style=for-the-badge)](https://github.com/NodeSecure/cli/blob/master/LICENSE)
@@ -11,7 +11,7 @@ Caching layer for NodeSecure CLI and server, handling configuration, analysis pa
1111

1212
## Requirements
1313

14-
- [Node.js](https://nodejs.org/en/) v20 or higher
14+
- [Node.js](https://nodejs.org/en/) v24 or higher
1515

1616
## Getting Started
1717

@@ -43,121 +43,5 @@ await cache.setRootPayload(payload);
4343

4444
## API
4545

46-
### `updateConfig(config: AppConfig): Promise<void>`
47-
48-
Stores a new configuration object in the cache.
49-
50-
### `getConfig(): Promise<AppConfig>`
51-
52-
Retrieves the current configuration object from the cache.
53-
54-
### `updatePayload(packageName: string, payload: Payload): void`
55-
56-
Saves an analysis payload for a given package.
57-
58-
**Parameters**:
59-
- `pkg` (`string`): Package name (e.g., `"@nodesecure/scanner@6.0.0"`).
60-
- `payload` (`object`): The analysis result to store.
61-
62-
> [!NOTE]
63-
> Payloads are stored in the user's home directory under `~/.nsecure/payloads/`
64-
65-
### `getPayload(packageName: string): Payload`
66-
67-
Loads an analysis payload for a given package.
68-
69-
**Parameters**:
70-
`pkg` (`string`): Package name.
71-
72-
### `availablePayloads(): string[]`
73-
74-
Lists all available payloads (package names) in the cache.
75-
76-
### `getPayloadOrNull(packageName: string): Payload | null`
77-
78-
Loads an analysis payload for a given package, or returns `null` if not found.
79-
80-
**Parameters**:
81-
82-
- `pkg` (`string`): Package name.
83-
84-
Returns `null` if not found.
85-
86-
### `updatePayloadsList(payloadsList: PayloadsList): Promise<void>`
87-
88-
Updates the internal MRU/LRU and available payloads list.
89-
90-
**Parameters**:
91-
92-
- `payloadsList` (`object`): The new payloads list object.
93-
94-
### `payloadsList(): Promise<PayloadsList>`
95-
96-
Retrieves the current MRU/LRU and available payloads list.
97-
98-
### `initPayloadsList(options: InitPayloadListOptions = {}): Promise<void>`
99-
100-
Initializes the payloads list, optionally resetting the cache.
101-
102-
**Parameters**:
103-
104-
- `options` (`object`, *optional*):
105-
- `logging` (`boolean`, default: `true`): Enable logging.
106-
- `reset` (`boolean`, default: `false`): If `true`, reset the cache before initializing.
107-
108-
### `removePayload(packageName: string): void`
109-
110-
Removes a payload for a given package from the cache.
111-
112-
**Parameters**:
113-
- `pkg` (`string`): Package name.
114-
115-
### `removeLastMRU(): Promise<PayloadsList>`
116-
117-
Removes the least recently used payload if the MRU exceeds the maximum allowed.
118-
119-
### `setRootPayload(payload: Payload, options: SetRootPayloadOptions = {}): Promise<void>`
120-
121-
Sets a new root payload, updates MRU/LRU, and manages cache state.
122-
123-
**Parameters**:
124-
125-
- `payload` (`object`): The analysis result to set as root.
126-
- `options` (`object`):
127-
- `logging` (`boolean`, default: `true`): Enable logging.
128-
- `local` (`boolean`, default: `false`): Mark the payload as local.
129-
130-
## Interfaces
131-
132-
```ts
133-
interface AppConfig {
134-
defaultPackageMenu: string;
135-
ignore: {
136-
flags: Flag[];
137-
warnings: WarningName[];
138-
};
139-
theme?: "light" | "dark";
140-
disableExternalRequests: boolean;
141-
}
142-
143-
interface PayloadsList {
144-
mru: string[];
145-
lru: string[];
146-
current: string;
147-
availables: string[];
148-
lastUsed: Record<string, number>;
149-
root: string | null;
150-
}
151-
152-
interface LoggingOption {
153-
logging?: boolean;
154-
}
155-
156-
interface InitPayloadListOptions extends LoggingOption {
157-
reset?: boolean;
158-
}
159-
160-
interface SetRootPayloadOptions extends LoggingOption {
161-
local?: boolean;
162-
}
163-
```
46+
- [AppCache](./docs/AppCache.md)
47+
- [FilePersistanceProvider](./docs/FilePersistanceProvider.md)

workspaces/cache/docs/AppCache.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# AppCache
2+
3+
## API
4+
5+
### `updatePayload(packageName: string, payload: Payload): void`
6+
7+
Saves an analysis payload for a given package.
8+
9+
**Parameters**:
10+
- `packageName` (`string`): Package name (e.g., `"@nodesecure/scanner@6.0.0"`).
11+
- `payload` (`object`): The analysis result to store.
12+
13+
> [!NOTE]
14+
> Payloads are stored in the user's home directory under `~/.nsecure/payloads/`
15+
16+
### `getPayload(packageName: string): Payload`
17+
18+
Loads an analysis payload for a given package.
19+
20+
**Parameters**:
21+
`packageName` (`string`): Package name.
22+
23+
### `availablePayloads(): string[]`
24+
25+
Lists all available payloads (package names) in the cache.
26+
27+
### `getPayloadOrNull(packageName: string): Payload | null`
28+
29+
Loads an analysis payload for a given package, or returns `null` if not found.
30+
31+
**Parameters**:
32+
33+
- `packageName` (`string`): Package name.
34+
35+
Returns `null` if not found.
36+
37+
### `updatePayloadsList(payloadsList: PayloadsList): Promise<void>`
38+
39+
Updates the internal MRU/LRU and available payloads list.
40+
41+
**Parameters**:
42+
43+
- `payloadsList` (`object`): The new payloads list object.
44+
45+
### `payloadsList(): Promise<PayloadsList>`
46+
47+
Retrieves the current MRU/LRU and available payloads list.
48+
49+
### `initPayloadsList(options: InitPayloadListOptions = {}): Promise<void>`
50+
51+
Initializes the payloads list, optionally resetting the cache.
52+
53+
**Parameters**:
54+
55+
- `options` (`object`, *optional*):
56+
- `logging` (`boolean`, default: `true`): Enable logging.
57+
- `reset` (`boolean`, default: `false`): If `true`, reset the cache before initializing.
58+
59+
### `removePayload(packageName: string): void`
60+
61+
Removes a payload for a given package from the cache.
62+
63+
**Parameters**:
64+
- `packageName` (`string`): Package name.
65+
66+
### `removeLastMRU(): Promise<PayloadsList>`
67+
68+
Removes the least recently used payload if the MRU exceeds the maximum allowed.
69+
70+
### `setRootPayload(payload: Payload, options: SetRootPayloadOptions = {}): Promise<void>`
71+
72+
Sets a new root payload, updates MRU/LRU, and manages cache state.
73+
74+
**Parameters**:
75+
76+
- `payload` (`object`): The analysis result to set as root.
77+
- `options` (`object`):
78+
- `logging` (`boolean`, default: `true`): Enable logging.
79+
- `local` (`boolean`, default: `false`): Mark the payload as local.
80+
81+
## Interfaces
82+
83+
```ts
84+
interface PayloadsList {
85+
mru: string[];
86+
lru: string[];
87+
current: string;
88+
availables: string[];
89+
lastUsed: Record<string, number>;
90+
root: string | null;
91+
}
92+
93+
interface LoggingOption {
94+
logging?: boolean;
95+
}
96+
97+
interface InitPayloadListOptions extends LoggingOption {
98+
reset?: boolean;
99+
}
100+
101+
interface SetRootPayloadOptions extends LoggingOption {
102+
local?: boolean;
103+
}
104+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# FilePersistanceProvider
2+
3+
A generic file-based cache provider using [cacache](https://www.npmjs.com/package/cacache) for persistent storage.
4+
5+
## Usage Example
6+
7+
```ts
8+
import { FilePersistanceProvider } from "@nodesecure/cache";
9+
10+
interface MyData {
11+
name: string;
12+
value: number;
13+
}
14+
15+
const cache = new FilePersistanceProvider<MyData>("my-cache-key");
16+
17+
// Store data
18+
await cache.set({ name: "example", value: 42 });
19+
20+
// Retrieve data
21+
const data = await cache.get();
22+
console.log(data); // { name: "example", value: 42 }
23+
24+
// Remove data
25+
await cache.remove();
26+
```
27+
28+
## Interfaces
29+
30+
```ts
31+
interface BasePersistanceProvider<T> {
32+
get(): Promise<T | null>;
33+
set(value: T): Promise<boolean>;
34+
remove(): Promise<void>;
35+
}
36+
```
37+
38+
## API
39+
40+
### `static PATH`
41+
42+
The base path for the cache storage.
43+
44+
**Default**: `os.tmpdir()/nsecure-cli`
45+
46+
### `constructor(cacheKey: string)`
47+
48+
Creates a new instance of the persistence provider.
49+
50+
**Parameters**:
51+
52+
- `cacheKey` (`string`): A unique key to identify the cached data.
53+
54+
### `get(): Promise<T | null>`
55+
56+
Retrieves a cached value by its key.
57+
58+
**Returns**:
59+
60+
- `Promise<T | null>`: The cached value parsed from JSON, or `null` if not found or on error.
61+
62+
### `set(value: T): Promise<boolean>`
63+
64+
Stores a value in the cache.
65+
66+
**Parameters**:
67+
68+
- `value` (`T`): The value to cache (will be JSON stringified).
69+
70+
**Returns**:
71+
72+
- `Promise<boolean>`: `true` if the value was successfully stored, `false` on error.
73+
74+
### `remove(): Promise<void>`
75+
76+
Removes the cached entry associated with the key.
77+

workspaces/cache/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"build": "tsc",
1313
"prepublishOnly": "npm run build",
1414
"lint": "eslint src test",
15-
"test": "node --test test/index.test.ts",
15+
"test": "node --test test/**.test.ts",
1616
"test:c8": "c8 npm run test"
1717
},
1818
"author": "GENTILHOMME Thomas <gentilhomme.thomas@gmail.com>",

0 commit comments

Comments
 (0)