diff --git a/src/http-server/context.js b/src/ALS.js similarity index 100% rename from src/http-server/context.js rename to src/ALS.js diff --git a/src/http-server/cache.js b/src/cache.js similarity index 100% rename from src/http-server/cache.js rename to src/cache.js diff --git a/src/commands/config.js b/src/commands/config.js index 988daf80..256156ec 100644 --- a/src/commands/config.js +++ b/src/commands/config.js @@ -3,11 +3,11 @@ import os from "node:os"; import path from "node:path"; import fs from "node:fs/promises"; import { existsSync } from "node:fs"; +import { spawn } from "node:child_process"; // Import Third-party Dependencies import * as RC from "@nodesecure/rc"; import kleur from "kleur"; -import { spawn } from "node:child_process"; const K_HOME_PATH = path.join(os.homedir(), "nodesecure"); diff --git a/src/commands/http.js b/src/commands/http.js index f64fbb31..5df99bf1 100644 --- a/src/commands/http.js +++ b/src/commands/http.js @@ -10,7 +10,7 @@ import * as i18n from "@nodesecure/i18n"; // Import Internal Dependencies import { buildServer } from "../http-server/index.js"; -import { appCache } from "../http-server/cache.js"; +import { appCache } from "../cache.js"; // CONSTANTS const kRequiredScannerRange = ">=5.1.0"; diff --git a/src/commands/scanner.js b/src/commands/scanner.js index 195e53af..89d0ab40 100644 --- a/src/commands/scanner.js +++ b/src/commands/scanner.js @@ -13,7 +13,7 @@ import * as Scanner from "@nodesecure/scanner"; // Import Internal Dependencies import * as http from "./http.js"; -import { appCache } from "../http-server/cache.js"; +import { appCache } from "../cache.js"; export async function auto(spec, options) { const { keep, ...commandOptions } = options; diff --git a/src/commands/scorecard.js b/src/commands/scorecard.js index 416c1ba1..dd57b7fd 100644 --- a/src/commands/scorecard.js +++ b/src/commands/scorecard.js @@ -7,6 +7,7 @@ import kleur from "kleur"; import * as scorecard from "@nodesecure/ossf-scorecard-sdk"; import ini from "ini"; import { Ok, Err } from "@openally/result"; + // VARS const { yellow, grey, cyan, white } = kleur; diff --git a/src/http-server/ViewBuilder.class.js b/src/http-server/ViewBuilder.class.js index a417cd2f..fbb2ea67 100644 --- a/src/http-server/ViewBuilder.class.js +++ b/src/http-server/ViewBuilder.class.js @@ -10,7 +10,7 @@ import chokidar from "chokidar"; import { globStream } from "glob"; // Import Internal Dependencies -import { logger } from "./logger.js"; +import { logger } from "../logger.js"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const kProjectRootDir = path.join(__dirname, "..", ".."); diff --git a/src/http-server/config.js b/src/http-server/config.js index 4f0998d5..68616c86 100644 --- a/src/http-server/config.js +++ b/src/http-server/config.js @@ -1,6 +1,6 @@ // Import Internal Dependencies -import { appCache } from "./cache.js"; -import { logger } from "./logger.js"; +import { appCache } from "../cache.js"; +import { logger } from "../logger.js"; // CONSTANTS const kDefaultConfig = { diff --git a/src/http-server/endpoints/bundle.js b/src/http-server/endpoints/bundle.js index d2298894..49e088bd 100644 --- a/src/http-server/endpoints/bundle.js +++ b/src/http-server/endpoints/bundle.js @@ -1,5 +1,5 @@ // Import Third-party Dependencikes -import { get as getRequest } from "@myunisoft/httpie"; +import * as httpie from "@myunisoft/httpie"; import send from "@polka/send-type"; // CONSTANTS @@ -10,7 +10,7 @@ export async function get(req, res) { const pkgTemplate = version ? `${pkgName.replaceAll("%2F", "/")}@${version}` : pkgName; try { - const { data } = await getRequest(`${kBaseBundlePhobiaUrl}/size?package=${pkgTemplate}`); + const { data } = await httpie.get(`${kBaseBundlePhobiaUrl}/size?package=${pkgTemplate}`); const { gzip, size, dependencySizes } = data; return send(res, 200, { diff --git a/src/http-server/endpoints/config.js b/src/http-server/endpoints/config.js index fc79e35c..a7650724 100644 --- a/src/http-server/endpoints/config.js +++ b/src/http-server/endpoints/config.js @@ -3,7 +3,7 @@ import send from "@polka/send-type"; // Import Internal Dependencies import * as config from "../config.js"; -import { bodyParser } from "../bodyParser.js"; +import { bodyParser } from "../middlewares/bodyParser.js"; export async function get(_req, res) { const result = await config.get(); diff --git a/src/http-server/endpoints/data.js b/src/http-server/endpoints/data.js index b40f6ef9..722da167 100644 --- a/src/http-server/endpoints/data.js +++ b/src/http-server/endpoints/data.js @@ -6,8 +6,8 @@ import path from "node:path"; import send from "@polka/send-type"; // Import Internal Dependencies -import { appCache } from "../cache.js"; -import { logger } from "../logger.js"; +import { appCache } from "../../cache.js"; +import { logger } from "../../logger.js"; // CONSTANTS const kDefaultPayloadPath = path.join(process.cwd(), "nsecure-result.json"); diff --git a/src/http-server/endpoints/i18n.js b/src/http-server/endpoints/i18n.js index d4838456..5bd5d6b7 100644 --- a/src/http-server/endpoints/i18n.js +++ b/src/http-server/endpoints/i18n.js @@ -6,5 +6,12 @@ import english from "../../../i18n/english.js"; import french from "../../../i18n/french.js"; export async function get(_req, res) { - send(res, 200, { english: english.ui, french: french.ui }); + send( + res, + 200, + { + english: english.ui, + french: french.ui + } + ); } diff --git a/src/http-server/endpoints/report.js b/src/http-server/endpoints/report.js index 6fdb8676..594c0f69 100644 --- a/src/http-server/endpoints/report.js +++ b/src/http-server/endpoints/report.js @@ -6,8 +6,8 @@ import { report } from "@nodesecure/report"; import send from "@polka/send-type"; // Import Internal Dependencies -import { context } from "../context.js"; -import { bodyParser } from "../bodyParser.js"; +import { context } from "../../ALS.js"; +import { bodyParser } from "../middlewares/bodyParser.js"; // TODO: provide a non-file-based API on RC side ? const kReportPayload = { diff --git a/src/http-server/endpoints/root.js b/src/http-server/endpoints/root.js index ef554117..daab7c71 100644 --- a/src/http-server/endpoints/root.js +++ b/src/http-server/endpoints/root.js @@ -2,7 +2,7 @@ import send from "@polka/send-type"; // Import Internal Dependencies -import { context } from "../context.js"; +import { context } from "../../ALS.js"; export async function get(_req, res) { try { diff --git a/src/http-server/endpoints/search.js b/src/http-server/endpoints/search.js index 2f221981..327d9cd1 100644 --- a/src/http-server/endpoints/search.js +++ b/src/http-server/endpoints/search.js @@ -3,7 +3,7 @@ import send from "@polka/send-type"; import * as npm from "@nodesecure/npm-registry-sdk"; // Import Internal Dependencies -import { logger } from "../logger.js"; +import { logger } from "../../logger.js"; export async function get(req, res) { const { packageName } = req.params; diff --git a/src/http-server/index.js b/src/http-server/index.js index 62200567..62ca41ce 100644 --- a/src/http-server/index.js +++ b/src/http-server/index.js @@ -19,10 +19,10 @@ import * as npmDownloads from "./endpoints/npm-downloads.js"; import * as scorecard from "./endpoints/ossf-scorecard.js"; import * as locali18n from "./endpoints/i18n.js"; import * as report from "./endpoints/report.js"; -import * as middleware from "./middleware.js"; +import * as middlewares from "./middlewares/index.js"; import * as wsHandlers from "./websocket/index.js"; -import { logger } from "./logger.js"; -import { appCache } from "./cache.js"; +import { logger } from "../logger.js"; +import { appCache } from "../cache.js"; export function buildServer(dataFilePath, options = {}) { const httpConfigPort = typeof options.port === "number" ? options.port : 0; @@ -36,14 +36,14 @@ export function buildServer(dataFilePath, options = {}) { if (runFromPayload) { fs.accessSync(dataFilePath, fs.constants.R_OK | fs.constants.W_OK); httpServer.use( - middleware.buildContextMiddleware(dataFilePath, hotReload) + middlewares.buildContextMiddleware(dataFilePath, hotReload) ); } else { appCache.startFromZero = true; } - httpServer.use(middleware.addStaticFiles); + httpServer.use(middlewares.addStaticFiles); httpServer.get("/", root.get); httpServer.get("/data", data.get); diff --git a/src/http-server/middleware.js b/src/http-server/middleware.js deleted file mode 100644 index 36935975..00000000 --- a/src/http-server/middleware.js +++ /dev/null @@ -1,29 +0,0 @@ -// Import Node.js Dependencies -import { join, dirname } from "node:path"; -import { fileURLToPath } from "node:url"; - -// Import Third-party Dependencies -import sirv from "sirv"; - -// Import Internal Dependencies -import { context } from "./context.js"; -import { ViewBuilder } from "./ViewBuilder.class.js"; - -export function buildContextMiddleware( - dataFilePath, - autoReload = false -) { - const viewBuilder = new ViewBuilder({ - autoReload - }); - - return function addContext(_req, _res, next) { - const store = { dataFilePath, viewBuilder }; - context.run(store, next); - }; -} - -const __dirname = dirname(fileURLToPath(import.meta.url)); -const kProjectRootDir = join(__dirname, "..", ".."); - -export const addStaticFiles = sirv(join(kProjectRootDir, "dist"), { dev: true }); diff --git a/src/http-server/bodyParser.js b/src/http-server/middlewares/bodyParser.js similarity index 100% rename from src/http-server/bodyParser.js rename to src/http-server/middlewares/bodyParser.js diff --git a/src/http-server/middlewares/context.js b/src/http-server/middlewares/context.js new file mode 100644 index 00000000..950c9905 --- /dev/null +++ b/src/http-server/middlewares/context.js @@ -0,0 +1,17 @@ +// Import Internal Dependencies +import { context } from "../../ALS.js"; +import { ViewBuilder } from "../ViewBuilder.class.js"; + +export function buildContextMiddleware( + dataFilePath, + autoReload = false +) { + const viewBuilder = new ViewBuilder({ + autoReload + }); + + return function addContext(_req, _res, next) { + const store = { dataFilePath, viewBuilder }; + context.run(store, next); + }; +} diff --git a/src/http-server/middlewares/index.js b/src/http-server/middlewares/index.js new file mode 100644 index 00000000..78d18387 --- /dev/null +++ b/src/http-server/middlewares/index.js @@ -0,0 +1,3 @@ +export * from "./bodyParser.js"; +export * from "./context.js"; +export * from "./static.js"; diff --git a/src/http-server/middlewares/static.js b/src/http-server/middlewares/static.js new file mode 100644 index 00000000..8b77d2cc --- /dev/null +++ b/src/http-server/middlewares/static.js @@ -0,0 +1,14 @@ +// Import Node.js Dependencies +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +// Import Third-party Dependencies +import sirv from "sirv"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const kProjectRootDir = path.join(__dirname, "..", "..", ".."); + +export const addStaticFiles = sirv( + path.join(kProjectRootDir, "dist"), + { dev: true } +); diff --git a/src/http-server/websocket/init.js b/src/http-server/websocket/init.js index fe3a84ac..a2189f1e 100644 --- a/src/http-server/websocket/init.js +++ b/src/http-server/websocket/init.js @@ -1,6 +1,6 @@ // Import Internal Dependencies -import { appCache } from "../cache.js"; -import { logger } from "../logger.js"; +import { appCache } from "../../cache.js"; +import { logger } from "../../logger.js"; export async function init(socket, lock = false) { try { diff --git a/src/http-server/websocket/remove.js b/src/http-server/websocket/remove.js index abdac323..2baaabf3 100644 --- a/src/http-server/websocket/remove.js +++ b/src/http-server/websocket/remove.js @@ -1,6 +1,6 @@ // Import Internal Dependencies -import { appCache } from "../cache.js"; -import { logger } from "../logger.js"; +import { appCache } from "../../cache.js"; +import { logger } from "../../logger.js"; export async function remove(ws, pkg) { const formattedPkg = pkg.replace("/", "-"); diff --git a/src/http-server/websocket/search.js b/src/http-server/websocket/search.js index c100712b..eb2bef7b 100644 --- a/src/http-server/websocket/search.js +++ b/src/http-server/websocket/search.js @@ -2,8 +2,8 @@ import * as Scanner from "@nodesecure/scanner"; // Import Internal Dependencies -import { logger } from "../logger.js"; -import { appCache } from "../cache.js"; +import { logger } from "../../logger.js"; +import { appCache } from "../../cache.js"; export async function search(ws, pkg) { logger.info(`[ws|search](pkg: ${pkg})`); diff --git a/src/http-server/logger.js b/src/logger.js similarity index 100% rename from src/http-server/logger.js rename to src/logger.js diff --git a/src/utils.js b/src/utils.js deleted file mode 100644 index f363bc09..00000000 --- a/src/utils.js +++ /dev/null @@ -1,20 +0,0 @@ -// Import Third-party Dependencies -import * as i18n from "@nodesecure/i18n"; -import kleur from "kleur"; - -export function taggedI18nString(strings, ...keys) { - return function cur(...i18nParameters) { - const finalString = [strings[0]]; - - keys.forEach((currentKey, index) => { - const args = i18nParameters.shift() ?? []; - - finalString.push( - i18n.getTokenSync(currentKey, ...args), - strings[index + 1] - ); - }); - - return kleur.white().bold(finalString.join("")); - }; -} diff --git a/test/bodyPaser.test.js b/test/bodyPaser.test.js index d038996f..6c7db594 100644 --- a/test/bodyPaser.test.js +++ b/test/bodyPaser.test.js @@ -3,7 +3,7 @@ import { test } from "node:test"; import assert from "node:assert"; // Import Internal Dependencies -import { bodyParser } from "../src/http-server/bodyParser.js"; +import { bodyParser } from "../src/http-server/middlewares/bodyParser.js"; function generateFakeReq(headers = {}) { return { diff --git a/test/cache.test.js b/test/cache.test.js index 7ba2c969..e7e742fa 100644 --- a/test/cache.test.js +++ b/test/cache.test.js @@ -9,7 +9,7 @@ import os from "node:os"; import cacache from "cacache"; // Import Internal Dependencies -import { appCache } from "../src/http-server/cache.js"; +import { appCache } from "../src/cache.js"; // CONSTANTS const kPayloadsPath = path.join(os.homedir(), ".nsecure", "payloads"); diff --git a/test/config.test.js b/test/config.test.js index 37c79d11..795d243d 100644 --- a/test/config.test.js +++ b/test/config.test.js @@ -7,7 +7,7 @@ import cacache from "cacache"; // Import Internal Dependencies import { get, set } from "../src/http-server/config.js"; -import { CACHE_PATH } from "../src/http-server/cache.js"; +import { CACHE_PATH } from "../src/cache.js"; // CONSTANTS const kConfigKey = "___config"; diff --git a/test/httpServer.test.js b/test/httpServer.test.js index 3c23e18e..ef735c94 100644 --- a/test/httpServer.test.js +++ b/test/httpServer.test.js @@ -17,7 +17,7 @@ import cacache from "cacache"; // Require Internal Dependencies import { buildServer } from "../src/http-server/index.js"; import { ViewBuilder } from "../src/http-server/ViewBuilder.class.js"; -import { CACHE_PATH } from "../src/http-server/cache.js"; +import { CACHE_PATH } from "../src/cache.js"; // CONSTANTS const HTTP_PORT = 17049;