-
Notifications
You must be signed in to change notification settings - Fork 48
refactor(cache): separate web-ui-settings cache from payload cache #632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| # `cache` | ||
|
|
||
| [](https://www.npmjs.com/package/@nodesecure/cache) | ||
| [](https://www.npmjs.com/package/@nodesecure/cache) | ||
| [](https://api.securityscorecards.dev/projects/github.com/NodeSecure/cli) | ||
| [](https://github.com/NodeSecure/cli/blob/master/LICENSE) | ||
|
|
@@ -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 | ||
|
|
||
|
|
@@ -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) | ||
| 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; | ||
| } | ||
| ``` |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,77 @@ | ||||||
| # FilePersistanceProvider | ||||||
|
||||||
|
|
||||||
| 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> { | ||||||
|
||||||
| interface BasePersistanceProvider<T> { | |
| interface BasePersistenceProvider<T> { |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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]>", | ||
|
|
||
There was a problem hiding this comment.
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.