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
5 changes: 5 additions & 0 deletions .changeset/thick-moons-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nodesecure/server": minor
---

Improve report test and implementation
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"@nodesecure/npm-registry-sdk": "4.5.2",
"@nodesecure/ossf-scorecard-sdk": "4.0.1",
"@nodesecure/rc": "5.5.0",
"@nodesecure/report": "4.2.1",
"@nodesecure/report": "4.2.2",
"@nodesecure/scanner": "10.7.0",
"@nodesecure/server": "1.0.0",
"@nodesecure/utils": "^2.2.0",
Expand Down
8 changes: 2 additions & 6 deletions public/components/views/home/report/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,8 @@ class PopupReport extends LitElement {
"Content-Type": "application/json"
}
}).then(async(response) => {
const { data: json } = await response.json();
const url = window.URL.createObjectURL(
new Blob(
[new Uint8Array(json.data).buffer], { type: "application/pdf" }
)
);
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.target = "_blank";
Expand Down
2 changes: 2 additions & 0 deletions workspaces/server/src/ALS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AsyncLocalStorage } from "node:async_hooks";

// Import Third-party Dependencies
import type { PayloadCache } from "@nodesecure/cache";
import type { report } from "@nodesecure/report";

// Import Internal Dependencies
import type { ViewBuilder } from "./ViewBuilder.class.ts";
Expand All @@ -18,6 +19,7 @@ export interface AsyncStoreContext {
french: NestedStringRecord;
};
viewBuilder: ViewBuilder;
reporter?: typeof report;
}

export const context = new AsyncLocalStorage<AsyncStoreContext>();
18 changes: 9 additions & 9 deletions workspaces/server/src/endpoints/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function post(
const body = await bodyParser<ReportRequestBody>(req);
const { title, includesAllDeps, theme } = body;

const { cache } = context.getStore()!;
const { cache, reporter: reportFn = report } = context.getStore()!;

const currentSpec = cache.getCurrentSpec();
if (currentSpec === null) {
Expand All @@ -87,10 +87,12 @@ export async function post(
const reportPayload = structuredClone({
...kReportPayload,
title,
npm: repo === undefined ? undefined : {
organizationPrefix,
packages: [repo]
},
npm: repo === undefined ?
undefined :
{
organizationPrefix,
packages: [repo]
},
theme
});

Expand All @@ -101,14 +103,12 @@ export async function post(
[name]: scannerPayload.dependencies[name]
} satisfies Dependencies;

const data = await report(
const data = await reportFn(
dependencies,
reportPayload
);

return send(res, {
data
}, {
return send(res, data, {
headers: {
"content-type": "application/pdf"
}
Expand Down
11 changes: 7 additions & 4 deletions workspaces/server/src/endpoints/util/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface SendOptions {
headers?: OutgoingHttpHeaders;
}

type SendData = string | object;
type SendData = string | object | Buffer;

export function send(
res: ServerResponse,
Expand All @@ -21,9 +21,12 @@ export function send(

let contentType = headers["content-type"] as string | undefined
?? res.getHeader("content-type") as string | undefined;

let body: string;
if (typeof data === "object") {
let body: string | Buffer;
if (Buffer.isBuffer(data)) {
body = data;
contentType ??= "application/octet-stream";
}
else if (typeof data === "object") {
body = JSON.stringify(data);
contentType ??= "application/json;charset=utf-8";
}
Expand Down
8 changes: 6 additions & 2 deletions workspaces/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import http from "node:http";
import sirv from "sirv";
import { PayloadCache } from "@nodesecure/cache";
import type { Payload } from "@nodesecure/scanner";
import type { report } from "@nodesecure/report";

// Import Internal Dependencies
import { getApiRouter } from "./endpoints/index.ts";
Expand All @@ -32,6 +33,7 @@ export interface BuildServerOptions {
res: http.ServerResponse,
next: () => void
) => void;
reporter?: typeof report;
}

export async function buildServer(
Expand All @@ -47,7 +49,8 @@ export async function buildServer(
scanType = "from",
projectRootDir,
componentsDir,
i18n
i18n,
reporter
} = options;
const cache = await new PayloadCache().load();

Expand All @@ -58,7 +61,8 @@ export async function buildServer(
const store: AsyncStoreContext = {
i18n,
viewBuilder,
cache
cache,
reporter
};
if (runFromPayload && dataFilePath !== undefined) {
const payloadStr = await fs.readFile(dataFilePath, "utf-8");
Expand Down
3 changes: 2 additions & 1 deletion workspaces/server/test/httpServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe("httpServer", () => {
({ httpServer } = await buildServer(JSON_PATH, {
projectRootDir: kProjectRootDir,
componentsDir: kComponentsDir,
reporter: async() => Buffer.from("fake-pdf-data"),
i18n: {
english: {
ui: {}
Expand Down Expand Up @@ -282,7 +283,7 @@ describe("httpServer", () => {
});
});

test.skip("'/report' should return a Buffer", async() => {
test("'/report' should return a Buffer", async() => {
const result = await post<Buffer>(
new URL("/report", kHttpURL),
{
Expand Down
Loading