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
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
2 changes: 1 addition & 1 deletion src/commands/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/commands/scorecard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/http-server/ViewBuilder.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, "..", "..");
Expand Down
4 changes: 2 additions & 2 deletions src/http-server/config.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions src/http-server/endpoints/bundle.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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, {
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/endpoints/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/http-server/endpoints/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
9 changes: 8 additions & 1 deletion src/http-server/endpoints/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
);
}
4 changes: 2 additions & 2 deletions src/http-server/endpoints/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/endpoints/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/endpoints/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/http-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
29 changes: 0 additions & 29 deletions src/http-server/middleware.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/http-server/middlewares/context.js
Original file line number Diff line number Diff line change
@@ -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);
};
}
3 changes: 3 additions & 0 deletions src/http-server/middlewares/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./bodyParser.js";
export * from "./context.js";
export * from "./static.js";
14 changes: 14 additions & 0 deletions src/http-server/middlewares/static.js
Original file line number Diff line number Diff line change
@@ -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 }
);
4 changes: 2 additions & 2 deletions src/http-server/websocket/init.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/http-server/websocket/remove.js
Original file line number Diff line number Diff line change
@@ -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("/", "-");
Expand Down
4 changes: 2 additions & 2 deletions src/http-server/websocket/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})`);
Expand Down
File renamed without changes.
20 changes: 0 additions & 20 deletions src/utils.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/bodyPaser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion test/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion test/httpServer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down