Skip to content

Commit 0cb2426

Browse files
authored
refactor: use Node.js native type striping (#617)
1 parent 5bde5f8 commit 0cb2426

36 files changed

+218
-146
lines changed

package.json

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
"build:front": "node ./esbuild.config.js",
2121
"build:workspaces": "npm run build --ws --if-present",
2222
"test": "npm run test:cli && npm run lint && npm run lint:css",
23-
"test:cli": "glob -c \"node --no-warnings --test-concurrency 1 --test\" \"test/**/*.test.js\"",
23+
"test:cli": "node --no-warnings --test test/**/*.test.js",
2424
"test:all": "npm run test --ws --if-present",
25-
"coverage": "c8 --reporter=lcov npm run test"
25+
"coverage": "c8 --reporter=lcov npm run test",
26+
"clean": "clear-ts-build"
2627
},
2728
"files": [
2829
"bin",
@@ -36,8 +37,8 @@
3637
"workspaces/documentation-ui",
3738
"workspaces/vis-network",
3839
"workspaces/size-satisfies",
39-
"workspaces/server",
40-
"workspaces/cache"
40+
"workspaces/cache",
41+
"workspaces/server"
4142
],
4243
"repository": {
4344
"type": "git",
@@ -71,7 +72,7 @@
7172
"@nodesecure/size-satisfies": "^1.1.0",
7273
"@nodesecure/vis-network": "^1.4.0",
7374
"@openally/config.eslint": "^2.1.0",
74-
"@openally/config.typescript": "^1.0.3",
75+
"@openally/config.typescript": "1.2.1",
7576
"@openally/httpie": "^1.0.0",
7677
"@types/node": "^24.0.3",
7778
"c8": "^10.1.2",
@@ -82,14 +83,13 @@
8283
"pkg-ok": "^3.0.0",
8384
"pretty-bytes": "^7.0.0",
8485
"rimraf": "^6.0.1",
85-
"server-destroy": "^1.0.1",
8686
"stylelint": "^16.20.0",
8787
"stylelint-config-standard": "^39.0.1",
88-
"tsx": "^4.20.3",
8988
"typescript": "^5.8.3"
9089
},
9190
"dependencies": {
9291
"@lit/task": "^1.0.3",
92+
"@nodesecure/cache": "1.0.0",
9393
"@nodesecure/documentation-ui": "^1.3.0",
9494
"@nodesecure/flags": "^3.0.3",
9595
"@nodesecure/i18n": "^4.0.2",
@@ -100,34 +100,24 @@
100100
"@nodesecure/rc": "^5.0.0",
101101
"@nodesecure/report": "4.0.0",
102102
"@nodesecure/scanner": "^7.1.0",
103+
"@nodesecure/server": "1.0.0",
103104
"@nodesecure/utils": "^2.2.0",
104105
"@nodesecure/vulnera": "^2.0.1",
105106
"@openally/result": "^1.3.0",
106-
"@polka/send-type": "^0.5.2",
107107
"@topcli/cliui": "^1.1.0",
108108
"@topcli/pretty-json": "^1.0.0",
109109
"@topcli/prompts": "^2.0.0",
110110
"@topcli/spinner": "^4.0.0",
111-
"cacache": "^20.0.1",
112-
"chokidar": "^4.0.3",
113111
"dotenv": "^17.0.0",
114112
"filenamify": "^7.0.0",
115-
"glob": "^11.0.1",
116113
"highlightjs-line-numbers.js": "^2.8.0",
117114
"ini": "^6.0.0",
118115
"json-diff-ts": "^4.8.1",
119116
"kleur": "^4.1.5",
120117
"lit": "^3.3.1",
121118
"ms": "^2.1.3",
122119
"open": "^10.1.0",
123-
"pino": "10.1.0",
124-
"pino-pretty": "^13.0.0",
125-
"polka": "^0.5.2",
126120
"sade": "^1.8.1",
127-
"semver": "^7.6.3",
128-
"sirv": "^3.0.0",
129-
"ts-pattern": "^5.6.2",
130-
"ws": "^8.18.0",
131-
"zup": "0.0.2"
121+
"semver": "^7.6.3"
132122
}
133123
}

src/commands/cache.js

Lines changed: 6 additions & 6 deletions
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 { appCache } from "@nodesecure/cache";
8+
import { cache } from "@nodesecure/server";
99

1010
export async function main(options) {
1111
const {
@@ -31,14 +31,14 @@ export async function main(options) {
3131
}
3232

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

3838
if (full) {
3939
console.log(styleText(["underline"], i18n.getTokenSync("cli.commands.cache.scannedPayloadsTitle")));
4040
try {
41-
const payloads = appCache.availablePayloads();
41+
const payloads = cache.availablePayloads();
4242
prettyJson(payloads);
4343
}
4444
catch {
@@ -49,12 +49,12 @@ async function listCache(full) {
4949

5050
async function clearCache(full) {
5151
if (full) {
52-
appCache.availablePayloads().forEach((pkg) => {
53-
appCache.removePayload(pkg);
52+
cache.availablePayloads().forEach((pkg) => {
53+
cache.removePayload(pkg);
5454
});
5555
}
5656

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

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

src/commands/http.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import kleur from "kleur";
88
import open from "open";
99
import * as SemVer from "semver";
1010
import * as i18n from "@nodesecure/i18n";
11-
import { buildServer, WebSocketServerInstanciator } from "@nodesecure/server";
12-
import { appCache } from "@nodesecure/cache";
11+
import {
12+
cache,
13+
buildServer,
14+
WebSocketServerInstanciator
15+
} from "@nodesecure/server";
1316

1417
// Import Internal Dependencies
1518
import english from "../../i18n/english.js";
@@ -44,7 +47,7 @@ export async function start(
4447
assertScannerVersion(dataFilePath);
4548
}
4649
else {
47-
appCache.prefix = crypto.randomBytes(4).toString("hex");
50+
cache.prefix = crypto.randomBytes(4).toString("hex");
4851
}
4952

5053
const httpServer = buildServer(dataFilePath, {

src/commands/scanner.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Spinner } from "@topcli/spinner";
1111
import ms from "ms";
1212
import * as i18n from "@nodesecure/i18n";
1313
import * as scanner from "@nodesecure/scanner";
14-
import { appCache } from "@nodesecure/cache";
14+
import { cache } from "@nodesecure/server";
1515

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

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

220220
return filePath;
221221
}

test/commands/cache.test.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { after, before, describe, it } from "node:test";
1111

1212
// Import Third-party Dependencies
1313
import * as i18n from "@nodesecure/i18n";
14-
import { appCache, DEFAULT_PAYLOAD_PATH } from "@nodesecure/cache";
14+
import { cache } from "@nodesecure/server";
15+
import { DEFAULT_PAYLOAD_PATH } from "@nodesecure/cache";
1516

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

4849
try {
49-
actualCache = await appCache.payloadsList();
50+
actualCache = await cache.payloadsList();
5051
}
5152
catch {
52-
await appCache.initPayloadsList({ logging: false });
53-
actualCache = await appCache.payloadsList();
53+
await cache.initPayloadsList({ logging: false });
54+
actualCache = await cache.payloadsList();
5455
}
5556

56-
appCache.updatePayload("test-package", { foo: "bar" });
57+
cache.updatePayload("test-package", { foo: "bar" });
5758
});
5859

5960
after(async() => {
6061
await i18n.setLocalLang(lang);
6162
await i18n.getLocalLang();
6263

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

6667
if (dummyPayload !== null) {
6768
fs.rmSync(DEFAULT_PAYLOAD_PATH);

tsconfig.base.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "@openally/config.typescript",
2+
"extends": "@openally/config.typescript/esm-ts-next",
33
"compilerOptions": {
44
"composite": true
55
}

workspaces/cache/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ $ yarn add @nodesecure/cache
3333
## Usage example
3434

3535
```js
36-
import { appCache } from "@nodesecure/cache"
36+
import { AppCache } from "@nodesecure/cache";
3737

38-
await appCache.initPayloadsList();
39-
await appCache.setRootPayload(payload);
38+
const cache = new AppCache();
39+
40+
await cache.initPayloadsList();
41+
await cache.setRootPayload(payload);
4042
```
4143

4244
## API

workspaces/cache/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@
99
"dist"
1010
],
1111
"scripts": {
12-
"build": "rimraf dist && tsc",
12+
"build": "tsc",
1313
"prepublishOnly": "npm run build",
1414
"lint": "eslint src test",
15-
"test": "tsx --test test/index.test.ts",
15+
"test": "node --test test/index.test.ts",
1616
"test:c8": "c8 npm run test"
1717
},
1818
"author": "GENTILHOMME Thomas <gentilhomme.thomas@gmail.com>",
1919
"license": "MIT",
2020
"devDependencies": {
2121
"@types/cacache": "^19.0.0"
22+
},
23+
"dependencies": {
24+
"@nodesecure/flags": "3.0.3",
25+
"@nodesecure/js-x-ray": "10.2.0",
26+
"@nodesecure/scanner": "7.2.0",
27+
"cacache": "20.0.3"
2228
}
2329
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function noop() {
2+
// VOID
3+
}
4+
5+
export interface AbstractLogger {
6+
fatal: (...args: any[]) => void;
7+
error: (...args: any[]) => void;
8+
warn: (...args: any[]) => void;
9+
info: (...args: any[]) => void;
10+
debug: (...args: any[]) => void;
11+
trace: (...args: any[]) => void;
12+
}
13+
14+
export function createNoopLogger(): AbstractLogger {
15+
const logger: AbstractLogger = {
16+
fatal: noop,
17+
error: noop,
18+
warn: noop,
19+
info: noop,
20+
debug: noop,
21+
trace: noop
22+
};
23+
24+
return logger;
25+
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import fs from "node:fs";
55

66
// Import Third-party Dependencies
77
import cacache from "cacache";
8-
9-
// Import Internal Dependencies
10-
import { logger } from "@nodesecure/server";
118
import type { Flag } from "@nodesecure/flags";
129
import type { WarningName } from "@nodesecure/js-x-ray";
1310
import type { Payload } from "@nodesecure/scanner";
1411

12+
// Import Internal Dependencies
13+
import { type AbstractLogger, createNoopLogger } from "./abstract-logging.ts";
14+
1515
// CONSTANTS
1616
const kConfigCache = "___config";
1717
const kPayloadsCache = "___payloads";
@@ -53,11 +53,16 @@ export interface SetRootPayloadOptions extends LoggingOption {
5353
local?: boolean;
5454
}
5555

56-
class _AppCache {
56+
export class AppCache {
57+
#logger: AbstractLogger;
58+
5759
prefix = "";
5860
startFromZero = false;
5961

60-
constructor() {
62+
constructor(
63+
logger: AbstractLogger = createNoopLogger()
64+
) {
65+
this.#logger = logger;
6166
fs.mkdirSync(kPayloadsPath, { recursive: true });
6267
}
6368

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

9297
throw err;
9398
}
@@ -119,7 +124,7 @@ class _AppCache {
119124
return JSON.parse(data.toString());
120125
}
121126
catch (err) {
122-
logger.error("[cache|get](cache: not found)");
127+
this.#logger.error("[cache|get](cache: not found)");
123128

124129
throw err;
125130
}
@@ -139,7 +144,7 @@ class _AppCache {
139144
};
140145

141146
if (logging) {
142-
logger.info("[cache|init](startFromZero)");
147+
this.#logger.info("[cache|init](startFromZero)");
143148
}
144149
await cacache.put(CACHE_PATH, `${this.prefix}${kPayloadsCache}`, JSON.stringify(payloadsList));
145150

@@ -161,7 +166,7 @@ class _AppCache {
161166
};
162167

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

196201
if (logging) {
197-
logger.info(`[cache|init](packagesInFolder: ${packagesInFolder})`);
202+
this.#logger.info(`[cache|init](packagesInFolder: ${packagesInFolder})`);
198203
}
199204

200205
await cacache.put(CACHE_PATH, `${this.prefix}${kPayloadsCache}`, JSON.stringify({
@@ -254,5 +259,3 @@ class _AppCache {
254259
await this.updatePayloadsList(updatedPayloadsCache);
255260
}
256261
}
257-
258-
export const appCache = new _AppCache();

0 commit comments

Comments
 (0)