diff --git a/package.json b/package.json index b13b0877..2326ed67 100644 --- a/package.json +++ b/package.json @@ -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", @@ -36,8 +37,8 @@ "workspaces/documentation-ui", "workspaces/vis-network", "workspaces/size-satisfies", - "workspaces/server", - "workspaces/cache" + "workspaces/cache", + "workspaces/server" ], "repository": { "type": "git", @@ -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", @@ -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", @@ -100,19 +100,16 @@ "@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", @@ -120,14 +117,7 @@ "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" } } diff --git a/src/commands/cache.js b/src/commands/cache.js index 4c1a1080..fa4e6494 100644 --- a/src/commands/cache.js +++ b/src/commands/cache.js @@ -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 { @@ -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 { @@ -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"))); } diff --git a/src/commands/http.js b/src/commands/http.js index ea5a5eec..ebad023b 100644 --- a/src/commands/http.js +++ b/src/commands/http.js @@ -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"; @@ -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, { diff --git a/src/commands/scanner.js b/src/commands/scanner.js index f91a1d1e..8ddc1574 100644 --- a/src/commands/scanner.js +++ b/src/commands/scanner.js @@ -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"; @@ -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; } diff --git a/test/commands/cache.test.js b/test/commands/cache.test.js index 3fdd6a90..15c8d564 100644 --- a/test/commands/cache.test.js +++ b/test/commands/cache.test.js @@ -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"; @@ -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); diff --git a/tsconfig.base.json b/tsconfig.base.json index 7af87f94..76422932 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -1,5 +1,5 @@ { - "extends": "@openally/config.typescript", + "extends": "@openally/config.typescript/esm-ts-next", "compilerOptions": { "composite": true } diff --git a/workspaces/cache/README.md b/workspaces/cache/README.md index 9812e16a..49b0fb1b 100644 --- a/workspaces/cache/README.md +++ b/workspaces/cache/README.md @@ -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 diff --git a/workspaces/cache/package.json b/workspaces/cache/package.json index f9a6fdee..296a08ef 100644 --- a/workspaces/cache/package.json +++ b/workspaces/cache/package.json @@ -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 ", "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" } } diff --git a/workspaces/cache/src/abstract-logging.ts b/workspaces/cache/src/abstract-logging.ts new file mode 100644 index 00000000..5fb7f9da --- /dev/null +++ b/workspaces/cache/src/abstract-logging.ts @@ -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; +} diff --git a/workspaces/cache/index.ts b/workspaces/cache/src/index.ts similarity index 90% rename from workspaces/cache/index.ts rename to workspaces/cache/src/index.ts index c8e6dc83..df20e708 100644 --- a/workspaces/cache/index.ts +++ b/workspaces/cache/src/index.ts @@ -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"; @@ -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 }); } @@ -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; } @@ -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; } @@ -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)); @@ -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); @@ -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({ @@ -254,5 +259,3 @@ class _AppCache { await this.updatePayloadsList(updatedPayloadsCache); } } - -export const appCache = new _AppCache(); diff --git a/workspaces/cache/test/index.test.ts b/workspaces/cache/test/index.test.ts index e5f305fb..0b629080 100644 --- a/workspaces/cache/test/index.test.ts +++ b/workspaces/cache/test/index.test.ts @@ -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); }); diff --git a/workspaces/cache/tsconfig.json b/workspaces/cache/tsconfig.json index 07917f19..6f6a6d5a 100644 --- a/workspaces/cache/tsconfig.json +++ b/workspaces/cache/tsconfig.json @@ -1,8 +1,9 @@ { "extends": "../../tsconfig.base.json", "compilerOptions": { - "rootDir": "./", - "outDir": "./dist" + "outDir": "./dist", + "rootDir": "./src", }, - "exclude": ["test", "dist"] + "include": ["src"], + "exclude": ["node_modules", "dist"] } diff --git a/workspaces/server/package.json b/workspaces/server/package.json index f652381d..d85d7e5a 100644 --- a/workspaces/server/package.json +++ b/workspaces/server/package.json @@ -12,15 +12,29 @@ "build": "tsc", "prepublishOnly": "npm run build", "lint": "eslint src test", - "test": "tsx --test --test-concurrency 1 \"./test/**/*.test.ts\"", + "test": "node --test ./test/**/*.test.ts", "test:c8": "c8 npm run test" }, "author": "GENTILHOMME Thomas ", "license": "MIT", "devDependencies": { - "@polka/send-type": "^0.5.2", + "@polka/send-type": "0.5.2", "@types/polka": "^0.5.7", "@types/server-destroy": "^1.0.4", "@types/ws": "^8.18.1" + }, + "dependencies": { + "@nodesecure/cache": "1.0.0", + "cacache": "20.0.3", + "chokidar": "5.0.0", + "glob": "13.0.0", + "pino": "10.1.0", + "pino-pretty": "13.1.2", + "polka": "0.5.2", + "server-destroy": "1.0.1", + "sirv": "3.0.2", + "ts-pattern": "5.9.0", + "ws": "8.18.3", + "zup": "0.0.2" } } diff --git a/workspaces/server/src/ALS.ts b/workspaces/server/src/ALS.ts index f0187897..f63b1dcb 100644 --- a/workspaces/server/src/ALS.ts +++ b/workspaces/server/src/ALS.ts @@ -2,6 +2,6 @@ import { AsyncLocalStorage } from "node:async_hooks"; // Import Internal Dependencies -import type { AyncStoreContext } from "./middlewares/context.js"; +import type { AyncStoreContext } from "./middlewares/context.ts"; export const context = new AsyncLocalStorage(); diff --git a/workspaces/server/src/ViewBuilder.class.ts b/workspaces/server/src/ViewBuilder.class.ts index 6e96a562..f308a293 100644 --- a/workspaces/server/src/ViewBuilder.class.ts +++ b/workspaces/server/src/ViewBuilder.class.ts @@ -9,7 +9,7 @@ import chokidar from "chokidar"; import { globStream } from "glob"; // Import Internal Dependencies -import { logger } from "./logger.js"; +import { logger } from "./logger.ts"; export interface ViewBuilderOptions { autoReload?: boolean; diff --git a/workspaces/server/src/cache.ts b/workspaces/server/src/cache.ts new file mode 100644 index 00000000..f8fc489a --- /dev/null +++ b/workspaces/server/src/cache.ts @@ -0,0 +1,16 @@ +// Import Third-party Dependencies +import { + AppCache, + type AppConfig +} from "@nodesecure/cache"; + +// Import Internal Dependencies +import { logger } from "./logger.ts"; + +export const cache = new AppCache( + logger +); + +export type { + AppConfig +}; diff --git a/workspaces/server/src/config.ts b/workspaces/server/src/config.ts index b4051533..a0461ff4 100644 --- a/workspaces/server/src/config.ts +++ b/workspaces/server/src/config.ts @@ -1,9 +1,9 @@ // Import Third-party Dependencies import { warnings, type WarningName } from "@nodesecure/js-x-ray"; -import { appCache, type AppConfig } from "@nodesecure/cache"; // Import Internal Dependencies -import { logger } from "./logger.js"; +import { cache, type AppConfig } from "./cache.ts"; +import { logger } from "./logger.ts"; const experimentalWarnings = Object.entries(warnings) .flatMap(([warning, metadata]) => ("experimental" in metadata && metadata.experimental ? [warning] : [])) as WarningName[]; @@ -17,7 +17,7 @@ const kDefaultConfig = { export async function get(): Promise { try { - const config = await appCache.getConfig(); + const config = await cache.getConfig(); const { defaultPackageMenu, @@ -46,7 +46,7 @@ export async function get(): Promise { catch (err: any) { logger.error(`[config|get](error: ${err.message})`); - await appCache.updateConfig(kDefaultConfig); + await cache.updateConfig(kDefaultConfig); logger.info(`[config|get](fallback to default: ${JSON.stringify(kDefaultConfig)})`); @@ -57,7 +57,7 @@ export async function get(): Promise { export async function set(newValue: AppConfig) { logger.info(`[config|set](config: ${JSON.stringify(newValue)})`); try { - await appCache.updateConfig(newValue); + await cache.updateConfig(newValue); logger.info("[config|set](sucess)"); } diff --git a/workspaces/server/src/endpoints/config.ts b/workspaces/server/src/endpoints/config.ts index d78dc3e7..b4db78c8 100644 --- a/workspaces/server/src/endpoints/config.ts +++ b/workspaces/server/src/endpoints/config.ts @@ -3,8 +3,8 @@ import send from "@polka/send-type"; import type { Request, Response } from "express-serve-static-core"; // Import Internal Dependencies -import * as config from "../config.js"; -import { bodyParser } from "../middlewares/bodyParser.js"; +import * as config from "../config.ts"; +import { bodyParser } from "../middlewares/bodyParser.ts"; export async function get(_req: Request, res: Response) { const result = await config.get(); diff --git a/workspaces/server/src/endpoints/data.ts b/workspaces/server/src/endpoints/data.ts index fe096528..d04b38cc 100644 --- a/workspaces/server/src/endpoints/data.ts +++ b/workspaces/server/src/endpoints/data.ts @@ -4,18 +4,18 @@ import path from "node:path"; // Import Third-party Dependencies import send from "@polka/send-type"; -import { appCache } from "@nodesecure/cache"; import type { Request, Response } from "express-serve-static-core"; // Import Internal Dependencies -import { context } from "../ALS.js"; -import { logger } from "../logger.js"; +import { context } from "../ALS.ts"; +import { logger } from "../logger.ts"; +import { cache } from "../cache.ts"; // CONSTANTS const kDefaultPayloadPath = path.join(process.cwd(), "nsecure-result.json"); export async function get(_req: Request, res: Response) { - if (appCache.startFromZero) { + if (cache.startFromZero) { logger.info("[data|get](no content)"); send(res, 204); @@ -23,11 +23,11 @@ export async function get(_req: Request, res: Response) { } try { - const { current, mru } = await appCache.payloadsList(); + const { current, mru } = await cache.payloadsList(); logger.info(`[data|get](current: ${current})`); logger.debug(`[data|get](lru: ${mru})`); - send(res, 200, appCache.getPayload(current)); + send(res, 200, cache.getPayload(current)); } catch { logger.error("[data|get](No cache yet. Creating one...)"); @@ -42,7 +42,7 @@ export async function get(_req: Request, res: Response) { mru: [formatted], current: formatted, lru: [], - availables: appCache.availablePayloads().filter((pkg) => pkg !== formatted), + availables: cache.availablePayloads().filter((pkg) => pkg !== formatted), lastUsed: { [formatted]: Date.now() }, @@ -50,8 +50,8 @@ export async function get(_req: Request, res: Response) { }; logger.info(`[data|get](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`); - await appCache.updatePayloadsList(payloadsList); - appCache.updatePayload(formatted, payload); + await cache.updatePayloadsList(payloadsList); + cache.updatePayload(formatted, payload); logger.info(`[data|get](cache: created|payloadsList: ${payloadsList.lru})`); send(res, 200, payload); diff --git a/workspaces/server/src/endpoints/i18n.ts b/workspaces/server/src/endpoints/i18n.ts index a83812ab..ca1366aa 100644 --- a/workspaces/server/src/endpoints/i18n.ts +++ b/workspaces/server/src/endpoints/i18n.ts @@ -2,7 +2,7 @@ import send from "@polka/send-type"; // Import Internal Dependencies -import { context } from "../ALS.js"; +import { context } from "../ALS.ts"; export async function get(_req, res) { const { i18n } = context.getStore()!; diff --git a/workspaces/server/src/endpoints/report.ts b/workspaces/server/src/endpoints/report.ts index ae24c0ad..96942a1e 100644 --- a/workspaces/server/src/endpoints/report.ts +++ b/workspaces/server/src/endpoints/report.ts @@ -4,13 +4,13 @@ import fs from "node:fs"; // Import Third-party Dependencies import send from "@polka/send-type"; import { report } from "@nodesecure/report"; -import { appCache } from "@nodesecure/cache"; import type { Request, Response } from "express-serve-static-core"; import type { RC } from "@nodesecure/rc"; // Import Internal Dependencies -import { context } from "../ALS.js"; -import { bodyParser } from "../middlewares/bodyParser.js"; +import { context } from "../ALS.ts"; +import { cache } from "../cache.ts"; +import { bodyParser } from "../middlewares/bodyParser.ts"; // TODO: provide a non-file-based API on RC side ? const kReportPayload: Partial = { @@ -58,7 +58,7 @@ export async function post(req: Request, res: Response) { const scannerPayload = dataFilePath ? JSON.parse(fs.readFileSync(dataFilePath, "utf-8")) : - appCache.getPayload((await appCache.payloadsList()).current); + cache.getPayload((await cache.payloadsList()).current); const rootDependencyName = scannerPayload.rootDependencyName; const [organizationPrefixOrRepo, repo] = rootDependencyName.split("/"); diff --git a/workspaces/server/src/endpoints/root.ts b/workspaces/server/src/endpoints/root.ts index 7b0675de..6144cc73 100644 --- a/workspaces/server/src/endpoints/root.ts +++ b/workspaces/server/src/endpoints/root.ts @@ -3,7 +3,7 @@ import send from "@polka/send-type"; import type { Request, Response } from "express-serve-static-core"; // Import Internal Dependencies -import { context } from "../ALS.js"; +import { context } from "../ALS.ts"; export async function get(_req: Request, res: Response) { try { diff --git a/workspaces/server/src/endpoints/search.ts b/workspaces/server/src/endpoints/search.ts index 1c0ce1b3..f2c6b479 100644 --- a/workspaces/server/src/endpoints/search.ts +++ b/workspaces/server/src/endpoints/search.ts @@ -4,7 +4,7 @@ import * as npm from "@nodesecure/npm-registry-sdk"; import type { Request, Response } from "express-serve-static-core"; // Import Internal Dependencies -import { logger } from "../logger.js"; +import { logger } from "../logger.ts"; export async function get(req: Request, res: Response) { const { packageName } = req.params; diff --git a/workspaces/server/index.ts b/workspaces/server/src/index.ts similarity index 69% rename from workspaces/server/index.ts rename to workspaces/server/src/index.ts index 24c2f4ae..942c0fd5 100644 --- a/workspaces/server/index.ts +++ b/workspaces/server/src/index.ts @@ -3,23 +3,23 @@ import fs from "node:fs"; // Import Third-party Dependencies import polka from "polka"; -import { appCache } from "@nodesecure/cache"; // Import Internal Dependencies -import * as root from "./src/endpoints/root.js"; -import * as data from "./src/endpoints/data.js"; -import * as flags from "./src/endpoints/flags.js"; -import * as config from "./src/endpoints/config.js"; -import * as search from "./src/endpoints/search.js"; -import * as bundle from "./src/endpoints/bundle.js"; -import * as npmDownloads from "./src/endpoints/npm-downloads.js"; -import * as scorecard from "./src/endpoints/ossf-scorecard.js"; -import * as locali18n from "./src/endpoints/i18n.js"; -import * as report from "./src/endpoints/report.js"; -import * as middlewares from "./src/middlewares/index.js"; -import { type BuildContextMiddlewareOptions } from "./src/middlewares/context.js"; -import { WebSocketServerInstanciator } from "./src/websocket/index.js"; -import { logger } from "./src/logger.js"; +import * as root from "./endpoints/root.ts"; +import * as data from "./endpoints/data.ts"; +import * as flags from "./endpoints/flags.ts"; +import * as config from "./endpoints/config.ts"; +import * as search from "./endpoints/search.ts"; +import * as bundle from "./endpoints/bundle.ts"; +import * as npmDownloads from "./endpoints/npm-downloads.ts"; +import * as scorecard from "./endpoints/ossf-scorecard.ts"; +import * as locali18n from "./endpoints/i18n.ts"; +import * as report from "./endpoints/report.ts"; +import * as middlewares from "./middlewares/index.ts"; +import { type BuildContextMiddlewareOptions } from "./middlewares/context.ts"; +import { WebSocketServerInstanciator } from "./websocket/index.ts"; +import { logger } from "./logger.ts"; +import { cache } from "./cache.ts"; export type NestedStringRecord = { [key: string]: string | NestedStringRecord; @@ -55,7 +55,7 @@ export function buildServer(dataFilePath: string, options: BuildServerOptions) { asyncStoreProperties.dataFilePath = dataFilePath; } else { - appCache.startFromZero = true; + cache.startFromZero = true; } httpServer.use( middlewares.buildContextMiddleware({ @@ -91,5 +91,6 @@ export function buildServer(dataFilePath: string, options: BuildServerOptions) { export { WebSocketServerInstanciator, - logger + logger, + cache }; diff --git a/workspaces/server/src/middlewares/bodyParser.ts b/workspaces/server/src/middlewares/bodyParser.ts index f8ce6b32..fbbd6a22 100644 --- a/workspaces/server/src/middlewares/bodyParser.ts +++ b/workspaces/server/src/middlewares/bodyParser.ts @@ -1,5 +1,5 @@ -// Import Third-party Dependencies -import type { Request } from "express-serve-static-core"; +// Import Node.js Dependencies +import type { IncomingMessage } from "node:http"; /** * @async @@ -8,7 +8,7 @@ import type { Request } from "express-serve-static-core"; * @returns {Promise} */ export async function bodyParser( - req: Request + req: IncomingMessage ) { let rawBody = ""; for await (const chunk of req) { diff --git a/workspaces/server/src/middlewares/context.ts b/workspaces/server/src/middlewares/context.ts index 66d8307c..2c745e3f 100644 --- a/workspaces/server/src/middlewares/context.ts +++ b/workspaces/server/src/middlewares/context.ts @@ -2,9 +2,9 @@ import type { Request, Response, NextFunction } from "express-serve-static-core"; // Import Internal Dependencies -import { context } from "../ALS.js"; -import { ViewBuilder } from "../ViewBuilder.class.js"; -import type { NestedStringRecord } from "../../index.js"; +import { context } from "../ALS.ts"; +import { ViewBuilder } from "../ViewBuilder.class.ts"; +import type { NestedStringRecord } from "../index.ts"; export interface AyncStoreContext { dataFilePath?: string; diff --git a/workspaces/server/src/middlewares/index.ts b/workspaces/server/src/middlewares/index.ts index 78d18387..ad727c39 100644 --- a/workspaces/server/src/middlewares/index.ts +++ b/workspaces/server/src/middlewares/index.ts @@ -1,3 +1,3 @@ -export * from "./bodyParser.js"; -export * from "./context.js"; -export * from "./static.js"; +export * from "./bodyParser.ts"; +export * from "./context.ts"; +export * from "./static.ts"; diff --git a/workspaces/server/src/websocket/commands/remove.ts b/workspaces/server/src/websocket/commands/remove.ts index b8fc84d2..d0ad9d8f 100644 --- a/workspaces/server/src/websocket/commands/remove.ts +++ b/workspaces/server/src/websocket/commands/remove.ts @@ -2,11 +2,11 @@ import type { PayloadsList } from "@nodesecure/cache"; // Import Internal Dependencies -import { context } from "../websocket.als.js"; +import { context } from "../websocket.als.ts"; import type { WebSocketResponse, WebSocketContext -} from "../websocket.types.js"; +} from "../websocket.types.ts"; export async function* remove( spec: string diff --git a/workspaces/server/src/websocket/commands/search.ts b/workspaces/server/src/websocket/commands/search.ts index 3296a205..b30f4364 100644 --- a/workspaces/server/src/websocket/commands/search.ts +++ b/workspaces/server/src/websocket/commands/search.ts @@ -1,12 +1,12 @@ // Import Third-party Dependencies import * as scanner from "@nodesecure/scanner"; -import type { PayloadsList, appCache } from "@nodesecure/cache"; +import type { PayloadsList, AppCache } from "@nodesecure/cache"; // Import Internal Dependencies -import { context } from "../websocket.als.js"; +import { context } from "../websocket.als.ts"; import type { WebSocketResponse -} from "../websocket.types.js"; +} from "../websocket.types.ts"; export async function* search( spec: string @@ -72,7 +72,7 @@ async function* searchInCache( async function handleMruCache( spec: string, - cache: typeof appCache, + cache: AppCache, cacheList: PayloadsList ): Promise { const updatedList: PayloadsList = { @@ -88,7 +88,7 @@ async function handleMruCache( async function handleLruOrAvailableCache( spec: string, - cache: typeof appCache + cache: AppCache ): Promise { const { mru, lru, availables, lastUsed, diff --git a/workspaces/server/src/websocket/index.ts b/workspaces/server/src/websocket/index.ts index bef4ad27..de757027 100644 --- a/workspaces/server/src/websocket/index.ts +++ b/workspaces/server/src/websocket/index.ts @@ -1,18 +1,18 @@ // Import Third-party Dependencies import { WebSocketServer, type WebSocket } from "ws"; import { match } from "ts-pattern"; -import { appCache } from "@nodesecure/cache"; // Import Internal Dependencies -import { logger } from "../logger.js"; -import { search } from "./commands/search.js"; -import { remove } from "./commands/remove.js"; -import { context } from "./websocket.als.js"; +import { logger } from "../logger.ts"; +import { cache } from "../cache.ts"; +import { search } from "./commands/search.ts"; +import { remove } from "./commands/remove.ts"; +import { context } from "./websocket.als.ts"; import type { WebSocketResponse, WebSocketContext, WebSocketMessage -} from "./websocket.types.js"; +} from "./websocket.types.ts"; export class WebSocketServerInstanciator { constructor() { @@ -37,7 +37,7 @@ export class WebSocketServerInstanciator { ) { const ctx: WebSocketContext = { socket, - cache: appCache, + cache, logger }; @@ -68,20 +68,20 @@ export class WebSocketServerInstanciator { stopInitializationOnError = false ): Promise { try { - const cache = await appCache.payloadsList(); + const cached = await cache.payloadsList(); if ( - cache.mru === void 0 || - cache.current === void 0 + cached.mru === void 0 || + cached.current === void 0 ) { throw new Error("Payloads list not found in cache."); } logger.info( - `[ws|init](current: ${cache.current}|root: ${cache.root})` + `[ws|init](current: ${cached.current}|root: ${cached.root})` ); return { status: "INIT", - cache + cache: cached }; } catch { @@ -90,7 +90,7 @@ export class WebSocketServerInstanciator { } logger.error("[ws|init] creating new payloads list in cache"); - await appCache.initPayloadsList(); + await cache.initPayloadsList(); return this.initializeServer(true); } diff --git a/workspaces/server/src/websocket/websocket.als.ts b/workspaces/server/src/websocket/websocket.als.ts index ad00d7ee..3b7a4eb0 100644 --- a/workspaces/server/src/websocket/websocket.als.ts +++ b/workspaces/server/src/websocket/websocket.als.ts @@ -4,6 +4,6 @@ import { AsyncLocalStorage } from "node:async_hooks"; // Import Internal Dependencies import type { WebSocketContext -} from "./websocket.types.js"; +} from "./websocket.types.ts"; export const context = new AsyncLocalStorage(); diff --git a/workspaces/server/src/websocket/websocket.types.ts b/workspaces/server/src/websocket/websocket.types.ts index c7612fcd..b39827cc 100644 --- a/workspaces/server/src/websocket/websocket.types.ts +++ b/workspaces/server/src/websocket/websocket.types.ts @@ -1,10 +1,10 @@ // Import Third-party Dependencies import type { WebSocket } from "ws"; -import type { PayloadsList, appCache } from "@nodesecure/cache"; +import type { AppCache, PayloadsList } from "@nodesecure/cache"; import type { Payload } from "@nodesecure/scanner"; // Import Internal Dependencies -import type { logger } from "../logger.js"; +import type { logger } from "../logger.ts"; type PayloadResponse = { status: "PAYLOAD"; @@ -39,6 +39,6 @@ export type WebSocketMessage = { export interface WebSocketContext { socket: WebSocket; - cache: typeof appCache; + cache: AppCache; logger: typeof logger; } diff --git a/workspaces/server/test/bodyParser.test.ts b/workspaces/server/test/bodyParser.test.ts index de6606b6..be74d82f 100644 --- a/workspaces/server/test/bodyParser.test.ts +++ b/workspaces/server/test/bodyParser.test.ts @@ -3,7 +3,7 @@ import { test } from "node:test"; import assert from "node:assert"; // Import Internal Dependencies -import { bodyParser } from "../src/middlewares/bodyParser.js"; +import { bodyParser } from "../src/middlewares/bodyParser.ts"; function generateFakeReq(headers = {}): any { return { diff --git a/workspaces/server/test/config.test.ts b/workspaces/server/test/config.test.ts index 5518d9f1..477711df 100644 --- a/workspaces/server/test/config.test.ts +++ b/workspaces/server/test/config.test.ts @@ -5,10 +5,10 @@ import assert from "node:assert"; // Import Third-party Dependencies import cacache from "cacache"; import { warnings, type Warning } from "@nodesecure/js-x-ray"; -import { AppConfig, CACHE_PATH } from "@nodesecure/cache"; +import { type AppConfig, CACHE_PATH } from "@nodesecure/cache"; // Import Internal Dependencies -import { get, set } from "../src/config.js"; +import { get, set } from "../src/config.ts"; // CONSTANTS const kConfigKey = "___config"; diff --git a/workspaces/server/test/httpServer.test.ts b/workspaces/server/test/httpServer.test.ts index 99032e36..1667719c 100644 --- a/workspaces/server/test/httpServer.test.ts +++ b/workspaces/server/test/httpServer.test.ts @@ -13,10 +13,11 @@ import * as i18n from "@nodesecure/i18n"; import * as flags from "@nodesecure/flags"; import enableDestroy from "server-destroy"; import cacache from "cacache"; +import type { Polka } from "polka"; // Import Internal Dependencies -import { buildServer } from "../index.js"; -import * as flagsEndpoint from "../src/endpoints/flags.js"; +import { buildServer } from "../src/index.ts"; +import * as flagsEndpoint from "../src/endpoints/flags.ts"; // CONSTANTS const kHttpPort = 17049; diff --git a/workspaces/server/tsconfig.json b/workspaces/server/tsconfig.json index 6b088cd8..a1d397b7 100644 --- a/workspaces/server/tsconfig.json +++ b/workspaces/server/tsconfig.json @@ -1,8 +1,14 @@ { "extends": "../../tsconfig.base.json", "compilerOptions": { - "rootDir": "./", - "outDir": "dist", + "outDir": "./dist", + "rootDir": "./src", }, - "include": ["index.ts", "src"] + "include": ["src"], + "exclude": ["node_modules", "dist"], + "references": [ + { + "path": "../cache" + } + ] }