Skip to content
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
3 changes: 2 additions & 1 deletion src/commands/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { setImmediate } from "node:timers/promises";
// Import Third-party Dependencies
import prettyJson from "@topcli/pretty-json";
import * as i18n from "@nodesecure/i18n";
import { cache } from "@nodesecure/server";
import { cache, config } from "@nodesecure/server";

export async function main(options) {
const {
Expand Down Expand Up @@ -54,6 +54,7 @@ async function clearCache(full) {
});
}

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

console.log(styleText("green", i18n.getTokenSync("cli.commands.cache.cleared")));
Expand Down
124 changes: 4 additions & 120 deletions workspaces/cache/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `cache`

[![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)
[![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)
[![OpenSSF
Scorecard](https://api.securityscorecards.dev/projects/github.com/NodeSecure/cli/badge?style=for-the-badge)](https://api.securityscorecards.dev/projects/github.com/NodeSecure/cli)
[![mit](https://img.shields.io/github/license/NodeSecure/Cli?style=for-the-badge)](https://github.com/NodeSecure/cli/blob/master/LICENSE)
Expand All @@ -11,7 +11,7 @@ Caching layer for NodeSecure CLI and server, handling configuration, analysis pa

## Requirements

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

## Getting Started

Expand Down Expand Up @@ -43,121 +43,5 @@ await cache.setRootPayload(payload);

## API

### `updateConfig(config: AppConfig): Promise<void>`

Stores a new configuration object in the cache.

### `getConfig(): Promise<AppConfig>`

Retrieves the current configuration object from the cache.

### `updatePayload(packageName: string, payload: Payload): void`

Saves an analysis payload for a given package.

**Parameters**:
- `pkg` (`string`): Package name (e.g., `"@nodesecure/[email protected]"`).
- `payload` (`object`): The analysis result to store.

> [!NOTE]
> Payloads are stored in the user's home directory under `~/.nsecure/payloads/`

### `getPayload(packageName: string): Payload`

Loads an analysis payload for a given package.

**Parameters**:
`pkg` (`string`): Package name.

### `availablePayloads(): string[]`

Lists all available payloads (package names) in the cache.

### `getPayloadOrNull(packageName: string): Payload | null`

Loads an analysis payload for a given package, or returns `null` if not found.

**Parameters**:

- `pkg` (`string`): Package name.

Returns `null` if not found.

### `updatePayloadsList(payloadsList: PayloadsList): Promise<void>`

Updates the internal MRU/LRU and available payloads list.

**Parameters**:

- `payloadsList` (`object`): The new payloads list object.

### `payloadsList(): Promise<PayloadsList>`

Retrieves the current MRU/LRU and available payloads list.

### `initPayloadsList(options: InitPayloadListOptions = {}): Promise<void>`

Initializes the payloads list, optionally resetting the cache.

**Parameters**:

- `options` (`object`, *optional*):
- `logging` (`boolean`, default: `true`): Enable logging.
- `reset` (`boolean`, default: `false`): If `true`, reset the cache before initializing.

### `removePayload(packageName: string): void`

Removes a payload for a given package from the cache.

**Parameters**:
- `pkg` (`string`): Package name.

### `removeLastMRU(): Promise<PayloadsList>`

Removes the least recently used payload if the MRU exceeds the maximum allowed.

### `setRootPayload(payload: Payload, options: SetRootPayloadOptions = {}): Promise<void>`

Sets a new root payload, updates MRU/LRU, and manages cache state.

**Parameters**:

- `payload` (`object`): The analysis result to set as root.
- `options` (`object`):
- `logging` (`boolean`, default: `true`): Enable logging.
- `local` (`boolean`, default: `false`): Mark the payload as local.

## Interfaces

```ts
interface AppConfig {
defaultPackageMenu: string;
ignore: {
flags: Flag[];
warnings: WarningName[];
};
theme?: "light" | "dark";
disableExternalRequests: boolean;
}

interface PayloadsList {
mru: string[];
lru: string[];
current: string;
availables: string[];
lastUsed: Record<string, number>;
root: string | null;
}

interface LoggingOption {
logging?: boolean;
}

interface InitPayloadListOptions extends LoggingOption {
reset?: boolean;
}

interface SetRootPayloadOptions extends LoggingOption {
local?: boolean;
}
```
- [AppCache](./docs/AppCache.md)
- [FilePersistanceProvider](./docs/FilePersistanceProvider.md)
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling error: "Persistance" should be "Persistence". Update the documentation link to use the corrected filename.

Suggested change
- [FilePersistanceProvider](./docs/FilePersistanceProvider.md)
- [FilePersistenceProvider](./docs/FilePersistenceProvider.md)

Copilot uses AI. Check for mistakes.
104 changes: 104 additions & 0 deletions workspaces/cache/docs/AppCache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# AppCache

## API

### `updatePayload(packageName: string, payload: Payload): void`

Saves an analysis payload for a given package.

**Parameters**:
- `packageName` (`string`): Package name (e.g., `"@nodesecure/[email protected]"`).
- `payload` (`object`): The analysis result to store.

> [!NOTE]
> Payloads are stored in the user's home directory under `~/.nsecure/payloads/`

### `getPayload(packageName: string): Payload`

Loads an analysis payload for a given package.

**Parameters**:
`packageName` (`string`): Package name.

### `availablePayloads(): string[]`

Lists all available payloads (package names) in the cache.

### `getPayloadOrNull(packageName: string): Payload | null`

Loads an analysis payload for a given package, or returns `null` if not found.

**Parameters**:

- `packageName` (`string`): Package name.

Returns `null` if not found.

### `updatePayloadsList(payloadsList: PayloadsList): Promise<void>`

Updates the internal MRU/LRU and available payloads list.

**Parameters**:

- `payloadsList` (`object`): The new payloads list object.

### `payloadsList(): Promise<PayloadsList>`

Retrieves the current MRU/LRU and available payloads list.

### `initPayloadsList(options: InitPayloadListOptions = {}): Promise<void>`

Initializes the payloads list, optionally resetting the cache.

**Parameters**:

- `options` (`object`, *optional*):
- `logging` (`boolean`, default: `true`): Enable logging.
- `reset` (`boolean`, default: `false`): If `true`, reset the cache before initializing.

### `removePayload(packageName: string): void`

Removes a payload for a given package from the cache.

**Parameters**:
- `packageName` (`string`): Package name.

### `removeLastMRU(): Promise<PayloadsList>`

Removes the least recently used payload if the MRU exceeds the maximum allowed.

### `setRootPayload(payload: Payload, options: SetRootPayloadOptions = {}): Promise<void>`

Sets a new root payload, updates MRU/LRU, and manages cache state.

**Parameters**:

- `payload` (`object`): The analysis result to set as root.
- `options` (`object`):
- `logging` (`boolean`, default: `true`): Enable logging.
- `local` (`boolean`, default: `false`): Mark the payload as local.

## Interfaces

```ts
interface PayloadsList {
mru: string[];
lru: string[];
current: string;
availables: string[];
lastUsed: Record<string, number>;
root: string | null;
}

interface LoggingOption {
logging?: boolean;
}

interface InitPayloadListOptions extends LoggingOption {
reset?: boolean;
}

interface SetRootPayloadOptions extends LoggingOption {
local?: boolean;
}
```
77 changes: 77 additions & 0 deletions workspaces/cache/docs/FilePersistanceProvider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# FilePersistanceProvider
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling error: "Persistance" should be "Persistence". Update the documentation title to use the corrected class name.

Copilot uses AI. Check for mistakes.

A generic file-based cache provider using [cacache](https://www.npmjs.com/package/cacache) for persistent storage.

## Usage Example

```ts
import { FilePersistanceProvider } from "@nodesecure/cache";

interface MyData {
name: string;
value: number;
}

const cache = new FilePersistanceProvider<MyData>("my-cache-key");

// Store data
await cache.set({ name: "example", value: 42 });

// Retrieve data
const data = await cache.get();
console.log(data); // { name: "example", value: 42 }

// Remove data
await cache.remove();
```

## Interfaces

```ts
interface BasePersistanceProvider<T> {
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling error: "Persistance" should be "Persistence". Update the interface name in the documentation.

Suggested change
interface BasePersistanceProvider<T> {
interface BasePersistenceProvider<T> {

Copilot uses AI. Check for mistakes.
get(): Promise<T | null>;
set(value: T): Promise<boolean>;
remove(): Promise<void>;
}
```

## API

### `static PATH`

The base path for the cache storage.

**Default**: `os.tmpdir()/nsecure-cli`

### `constructor(cacheKey: string)`

Creates a new instance of the persistence provider.

**Parameters**:

- `cacheKey` (`string`): A unique key to identify the cached data.

### `get(): Promise<T | null>`

Retrieves a cached value by its key.

**Returns**:

- `Promise<T | null>`: The cached value parsed from JSON, or `null` if not found or on error.

### `set(value: T): Promise<boolean>`

Stores a value in the cache.

**Parameters**:

- `value` (`T`): The value to cache (will be JSON stringified).

**Returns**:

- `Promise<boolean>`: `true` if the value was successfully stored, `false` on error.

### `remove(): Promise<void>`

Removes the cached entry associated with the key.

2 changes: 1 addition & 1 deletion workspaces/cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build": "tsc",
"prepublishOnly": "npm run build",
"lint": "eslint src test",
"test": "node --test test/index.test.ts",
"test": "node --test test/**.test.ts",
"test:c8": "c8 npm run test"
},
"author": "GENTILHOMME Thomas <[email protected]>",
Expand Down
Loading