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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"@nodesecure/npm-registry-sdk": "^4.4.0",
"@nodesecure/ossf-scorecard-sdk": "^3.2.1",
"@nodesecure/rc": "^5.0.0",
"@nodesecure/report": "^3.0.0",
"@nodesecure/report": "4.0.0",
"@nodesecure/scanner": "^7.1.0",
"@nodesecure/utils": "^2.2.0",
"@nodesecure/vulnera": "^2.0.1",
Expand Down
27 changes: 20 additions & 7 deletions workspaces/server/src/endpoints/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
import fs from "node:fs";

// Import Third-party Dependencies
import { report } from "@nodesecure/report";
import send from "@polka/send-type";
import type { Request, Response } from "express-serve-static-core";
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";

// TODO: provide a non-file-based API on RC side ?
const kReportPayload = {
const kReportPayload: Partial<RC["report"]> = {
includeTransitiveInternal: false,
reporters: [
"pdf"
Expand Down Expand Up @@ -46,17 +47,23 @@ const kReportPayload = {
};

export async function post(req: Request, res: Response) {
const body = await bodyParser(req);
const body = await bodyParser(req) as {
title: string;
includesAllDeps: boolean;
theme: "light" | "dark";
};
const { title, includesAllDeps, theme } = body;

const { dataFilePath } = context.getStore()!;

const scannerPayload = dataFilePath ?
JSON.parse(fs.readFileSync(dataFilePath, "utf-8")) :
appCache.getPayload((await appCache.payloadsList()).current);
const reportPayload = structuredClone(kReportPayload);

const rootDependencyName = scannerPayload.rootDependencyName;
const [organizationPrefixOrRepo, repo] = rootDependencyName.split("/");
Object.assign(reportPayload, {
const reportPayload = structuredClone({
...kReportPayload,
title,
npm: {
organizationPrefix: repo === undefined ? null : organizationPrefixOrRepo,
Expand All @@ -66,8 +73,14 @@ export async function post(req: Request, res: Response) {
});

try {
const dependencies = includesAllDeps ?
scannerPayload.dependencies :
{
[rootDependencyName]: scannerPayload.dependencies[rootDependencyName]
};

const data = await report(
includesAllDeps ? scannerPayload.dependencies : { [rootDependencyName]: scannerPayload.dependencies[rootDependencyName] },
dependencies,
reportPayload
);

Expand Down
4 changes: 3 additions & 1 deletion workspaces/server/src/middlewares/bodyParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import type { Request } from "express-serve-static-core";
* @param {*} req
* @returns {Promise<any>}
*/
export async function bodyParser(req: Request) {
export async function bodyParser(
req: Request
) {
let rawBody = "";
for await (const chunk of req) {
rawBody += chunk;
Expand Down
1 change: 0 additions & 1 deletion workspaces/server/test/httpServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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";
Expand Down