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
28 changes: 9 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
"build:front": "node ./esbuild.config.js",
"build:workspaces": "npm run build --ws --if-present",
"test": "npm run test:cli && npm run lint && npm run lint:css",
"test:cli": "glob -c \"node --no-warnings --test-concurrency 1 --test\" \"test/**/*.test.js\"",
"test:cli": "node --no-warnings --test test/**/*.test.js",
"test:all": "npm run test --ws --if-present",
"coverage": "c8 --reporter=lcov npm run test"
"coverage": "c8 --reporter=lcov npm run test",
"clean": "clear-ts-build"
},
"files": [
"bin",
Expand All @@ -36,8 +37,8 @@
"workspaces/documentation-ui",
"workspaces/vis-network",
"workspaces/size-satisfies",
"workspaces/server",
"workspaces/cache"
"workspaces/cache",
"workspaces/server"
],
"repository": {
"type": "git",
Expand Down Expand Up @@ -71,7 +72,7 @@
"@nodesecure/size-satisfies": "^1.1.0",
"@nodesecure/vis-network": "^1.4.0",
"@openally/config.eslint": "^2.1.0",
"@openally/config.typescript": "^1.0.3",
"@openally/config.typescript": "1.2.1",
"@openally/httpie": "^1.0.0",
"@types/node": "^24.0.3",
"c8": "^10.1.2",
Expand All @@ -82,14 +83,13 @@
"pkg-ok": "^3.0.0",
"pretty-bytes": "^7.0.0",
"rimraf": "^6.0.1",
"server-destroy": "^1.0.1",
"stylelint": "^16.20.0",
"stylelint-config-standard": "^39.0.1",
"tsx": "^4.20.3",
"typescript": "^5.8.3"
},
"dependencies": {
"@lit/task": "^1.0.3",
"@nodesecure/cache": "1.0.0",
"@nodesecure/documentation-ui": "^1.3.0",
"@nodesecure/flags": "^3.0.3",
"@nodesecure/i18n": "^4.0.2",
Expand All @@ -100,34 +100,24 @@
"@nodesecure/rc": "^5.0.0",
"@nodesecure/report": "4.0.0",
"@nodesecure/scanner": "^7.1.0",
"@nodesecure/server": "1.0.0",
"@nodesecure/utils": "^2.2.0",
"@nodesecure/vulnera": "^2.0.1",
"@openally/result": "^1.3.0",
"@polka/send-type": "^0.5.2",
"@topcli/cliui": "^1.1.0",
"@topcli/pretty-json": "^1.0.0",
"@topcli/prompts": "^2.0.0",
"@topcli/spinner": "^4.0.0",
"cacache": "^20.0.1",
"chokidar": "^4.0.3",
"dotenv": "^17.0.0",
"filenamify": "^7.0.0",
"glob": "^11.0.1",
"highlightjs-line-numbers.js": "^2.8.0",
"ini": "^6.0.0",
"json-diff-ts": "^4.8.1",
"kleur": "^4.1.5",
"lit": "^3.3.1",
"ms": "^2.1.3",
"open": "^10.1.0",
"pino": "10.1.0",
"pino-pretty": "^13.0.0",
"polka": "^0.5.2",
"sade": "^1.8.1",
"semver": "^7.6.3",
"sirv": "^3.0.0",
"ts-pattern": "^5.6.2",
"ws": "^8.18.0",
"zup": "0.0.2"
"semver": "^7.6.3"
}
}
12 changes: 6 additions & 6 deletions 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 { appCache } from "@nodesecure/cache";
import { cache } from "@nodesecure/server";

export async function main(options) {
const {
Expand All @@ -31,14 +31,14 @@ export async function main(options) {
}

async function listCache(full) {
const paylodsList = await appCache.payloadsList();
const paylodsList = await cache.payloadsList();
console.log(styleText(["underline"], i18n.getTokenSync("cli.commands.cache.cacheTitle")));
prettyJson(paylodsList);

if (full) {
console.log(styleText(["underline"], i18n.getTokenSync("cli.commands.cache.scannedPayloadsTitle")));
try {
const payloads = appCache.availablePayloads();
const payloads = cache.availablePayloads();
prettyJson(payloads);
}
catch {
Expand All @@ -49,12 +49,12 @@ async function listCache(full) {

async function clearCache(full) {
if (full) {
appCache.availablePayloads().forEach((pkg) => {
appCache.removePayload(pkg);
cache.availablePayloads().forEach((pkg) => {
cache.removePayload(pkg);
});
}

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

console.log(styleText("green", i18n.getTokenSync("cli.commands.cache.cleared")));
}
9 changes: 6 additions & 3 deletions src/commands/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import kleur from "kleur";
import open from "open";
import * as SemVer from "semver";
import * as i18n from "@nodesecure/i18n";
import { buildServer, WebSocketServerInstanciator } from "@nodesecure/server";
import { appCache } from "@nodesecure/cache";
import {
cache,
buildServer,
WebSocketServerInstanciator
} from "@nodesecure/server";

// Import Internal Dependencies
import english from "../../i18n/english.js";
Expand Down Expand Up @@ -44,7 +47,7 @@ export async function start(
assertScannerVersion(dataFilePath);
}
else {
appCache.prefix = crypto.randomBytes(4).toString("hex");
cache.prefix = crypto.randomBytes(4).toString("hex");
}

const httpServer = buildServer(dataFilePath, {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Spinner } from "@topcli/spinner";
import ms from "ms";
import * as i18n from "@nodesecure/i18n";
import * as scanner from "@nodesecure/scanner";
import { appCache } from "@nodesecure/cache";
import { cache } from "@nodesecure/server";

// Import Internal Dependencies
import * as http from "./http.js";
Expand Down Expand Up @@ -215,7 +215,7 @@ async function logAndWrite(
console.log(kleur.white().bold(i18n.getTokenSync("cli.successfully_written_json", kleur.green().bold(filePath))));
console.log("");

await appCache.setRootPayload(payload, { logging: false, local });
await cache.setRootPayload(payload, { logging: false, local });

return filePath;
}
Expand Down
15 changes: 8 additions & 7 deletions test/commands/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { after, before, describe, it } from "node:test";

// Import Third-party Dependencies
import * as i18n from "@nodesecure/i18n";
import { appCache, DEFAULT_PAYLOAD_PATH } from "@nodesecure/cache";
import { cache } from "@nodesecure/server";
import { DEFAULT_PAYLOAD_PATH } from "@nodesecure/cache";

// Import Internal Dependencies
import { arrayFromAsync } from "../helpers/utils.js";
Expand Down Expand Up @@ -46,22 +47,22 @@ describe("Cache command", { concurrency: 1 }, () => {
lang = await i18n.getLocalLang();

try {
actualCache = await appCache.payloadsList();
actualCache = await cache.payloadsList();
}
catch {
await appCache.initPayloadsList({ logging: false });
actualCache = await appCache.payloadsList();
await cache.initPayloadsList({ logging: false });
actualCache = await cache.payloadsList();
}

appCache.updatePayload("test-package", { foo: "bar" });
cache.updatePayload("test-package", { foo: "bar" });
});

after(async() => {
await i18n.setLocalLang(lang);
await i18n.getLocalLang();

await appCache.updatePayloadsList(actualCache, { logging: false });
appCache.removePayload("test-package");
await cache.updatePayloadsList(actualCache, { logging: false });
cache.removePayload("test-package");

if (dummyPayload !== null) {
fs.rmSync(DEFAULT_PAYLOAD_PATH);
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "@openally/config.typescript",
"extends": "@openally/config.typescript/esm-ts-next",
"compilerOptions": {
"composite": true
}
Expand Down
8 changes: 5 additions & 3 deletions workspaces/cache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ $ yarn add @nodesecure/cache
## Usage example

```js
import { appCache } from "@nodesecure/cache"
import { AppCache } from "@nodesecure/cache";

await appCache.initPayloadsList();
await appCache.setRootPayload(payload);
const cache = new AppCache();

await cache.initPayloadsList();
await cache.setRootPayload(payload);
```

## API
Expand Down
10 changes: 8 additions & 2 deletions workspaces/cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@
"dist"
],
"scripts": {
"build": "rimraf dist && tsc",
"build": "tsc",
"prepublishOnly": "npm run build",
"lint": "eslint src test",
"test": "tsx --test test/index.test.ts",
"test": "node --test test/index.test.ts",
"test:c8": "c8 npm run test"
},
"author": "GENTILHOMME Thomas <gentilhomme.thomas@gmail.com>",
"license": "MIT",
"devDependencies": {
"@types/cacache": "^19.0.0"
},
"dependencies": {
"@nodesecure/flags": "3.0.3",
"@nodesecure/js-x-ray": "10.2.0",
"@nodesecure/scanner": "7.2.0",
"cacache": "20.0.3"
}
}
25 changes: 25 additions & 0 deletions workspaces/cache/src/abstract-logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function noop() {
// VOID
}

export interface AbstractLogger {
fatal: (...args: any[]) => void;
error: (...args: any[]) => void;
warn: (...args: any[]) => void;
info: (...args: any[]) => void;
debug: (...args: any[]) => void;
trace: (...args: any[]) => void;
}

export function createNoopLogger(): AbstractLogger {
const logger: AbstractLogger = {
fatal: noop,
error: noop,
warn: noop,
info: noop,
debug: noop,
trace: noop
};

return logger;
}
27 changes: 15 additions & 12 deletions workspaces/cache/index.ts → workspaces/cache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import fs from "node:fs";

// Import Third-party Dependencies
import cacache from "cacache";

// Import Internal Dependencies
import { logger } from "@nodesecure/server";
import type { Flag } from "@nodesecure/flags";
import type { WarningName } from "@nodesecure/js-x-ray";
import type { Payload } from "@nodesecure/scanner";

// Import Internal Dependencies
import { type AbstractLogger, createNoopLogger } from "./abstract-logging.ts";

// CONSTANTS
const kConfigCache = "___config";
const kPayloadsCache = "___payloads";
Expand Down Expand Up @@ -53,11 +53,16 @@ export interface SetRootPayloadOptions extends LoggingOption {
local?: boolean;
}

class _AppCache {
export class AppCache {
#logger: AbstractLogger;

prefix = "";
startFromZero = false;

constructor() {
constructor(
logger: AbstractLogger = createNoopLogger()
) {
this.#logger = logger;
fs.mkdirSync(kPayloadsPath, { recursive: true });
}

Expand Down Expand Up @@ -87,7 +92,7 @@ class _AppCache {
return JSON.parse(fs.readFileSync(filePath, "utf-8"));
}
catch (err) {
logger.error(`[cache|get](pkg: ${packageName}|cache: not found)`);
this.#logger.error(`[cache|get](pkg: ${packageName}|cache: not found)`);

throw err;
}
Expand Down Expand Up @@ -119,7 +124,7 @@ class _AppCache {
return JSON.parse(data.toString());
}
catch (err) {
logger.error("[cache|get](cache: not found)");
this.#logger.error("[cache|get](cache: not found)");

throw err;
}
Expand All @@ -139,7 +144,7 @@ class _AppCache {
};

if (logging) {
logger.info("[cache|init](startFromZero)");
this.#logger.info("[cache|init](startFromZero)");
}
await cacache.put(CACHE_PATH, `${this.prefix}${kPayloadsCache}`, JSON.stringify(payloadsList));

Expand All @@ -161,7 +166,7 @@ class _AppCache {
};

if (logging) {
logger.info(`[cache|init](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`);
this.#logger.info(`[cache|init](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`);
}
await cacache.put(CACHE_PATH, `${this.prefix}${kPayloadsCache}`, JSON.stringify(payloadsList));
this.updatePayload(formatted, payload);
Expand Down Expand Up @@ -194,7 +199,7 @@ class _AppCache {
}

if (logging) {
logger.info(`[cache|init](packagesInFolder: ${packagesInFolder})`);
this.#logger.info(`[cache|init](packagesInFolder: ${packagesInFolder})`);
}

await cacache.put(CACHE_PATH, `${this.prefix}${kPayloadsCache}`, JSON.stringify({
Expand Down Expand Up @@ -254,5 +259,3 @@ class _AppCache {
await this.updatePayloadsList(updatedPayloadsCache);
}
}

export const appCache = new _AppCache();
9 changes: 6 additions & 3 deletions workspaces/cache/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ import os from "node:os";
import cacache from "cacache";

// Import Internal Dependencies
import { appCache } from "../index.js";
import { AppCache } from "../src/index.ts";

// CONSTANTS
const kPayloadsPath = path.join(os.homedir(), ".nsecure", "payloads");

describe("appCache", () => {
before(async() => {
let appCache: AppCache;

before(() => {
appCache = new AppCache();
appCache.prefix = "test_runner";
});

after(async() => {
after(() => {
appCache.availablePayloads().forEach((pkg) => {
appCache.removePayload(pkg);
});
Expand Down
Loading