Skip to content

Commit 5d4fd0c

Browse files
authored
refactor(server): enhance @nodesecure/report implementation and tests (#705)
1 parent 40c9023 commit 5d4fd0c

8 files changed

Lines changed: 34 additions & 23 deletions

File tree

.changeset/thick-moons-begin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nodesecure/server": minor
3+
---
4+
5+
Improve report test and implementation

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"@nodesecure/npm-registry-sdk": "4.5.2",
107107
"@nodesecure/ossf-scorecard-sdk": "4.0.1",
108108
"@nodesecure/rc": "5.5.0",
109-
"@nodesecure/report": "4.2.1",
109+
"@nodesecure/report": "4.2.2",
110110
"@nodesecure/scanner": "10.7.0",
111111
"@nodesecure/server": "1.0.0",
112112
"@nodesecure/utils": "^2.2.0",

public/components/views/home/report/report.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,8 @@ class PopupReport extends LitElement {
252252
"Content-Type": "application/json"
253253
}
254254
}).then(async(response) => {
255-
const { data: json } = await response.json();
256-
const url = window.URL.createObjectURL(
257-
new Blob(
258-
[new Uint8Array(json.data).buffer], { type: "application/pdf" }
259-
)
260-
);
255+
const blob = await response.blob();
256+
const url = window.URL.createObjectURL(blob);
261257
const link = document.createElement("a");
262258
link.href = url;
263259
link.target = "_blank";

workspaces/server/src/ALS.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { AsyncLocalStorage } from "node:async_hooks";
33

44
// Import Third-party Dependencies
55
import type { PayloadCache } from "@nodesecure/cache";
6+
import type { report } from "@nodesecure/report";
67

78
// Import Internal Dependencies
89
import type { ViewBuilder } from "./ViewBuilder.class.ts";
@@ -18,6 +19,7 @@ export interface AsyncStoreContext {
1819
french: NestedStringRecord;
1920
};
2021
viewBuilder: ViewBuilder;
22+
reporter?: typeof report;
2123
}
2224

2325
export const context = new AsyncLocalStorage<AsyncStoreContext>();

workspaces/server/src/endpoints/report.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export async function post(
6161
const body = await bodyParser<ReportRequestBody>(req);
6262
const { title, includesAllDeps, theme } = body;
6363

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

6666
const currentSpec = cache.getCurrentSpec();
6767
if (currentSpec === null) {
@@ -87,10 +87,12 @@ export async function post(
8787
const reportPayload = structuredClone({
8888
...kReportPayload,
8989
title,
90-
npm: repo === undefined ? undefined : {
91-
organizationPrefix,
92-
packages: [repo]
93-
},
90+
npm: repo === undefined ?
91+
undefined :
92+
{
93+
organizationPrefix,
94+
packages: [repo]
95+
},
9496
theme
9597
});
9698

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

104-
const data = await report(
106+
const data = await reportFn(
105107
dependencies,
106108
reportPayload
107109
);
108110

109-
return send(res, {
110-
data
111-
}, {
111+
return send(res, data, {
112112
headers: {
113113
"content-type": "application/pdf"
114114
}

workspaces/server/src/endpoints/util/send.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface SendOptions {
1010
headers?: OutgoingHttpHeaders;
1111
}
1212

13-
type SendData = string | object;
13+
type SendData = string | object | Buffer;
1414

1515
export function send(
1616
res: ServerResponse,
@@ -21,9 +21,12 @@ export function send(
2121

2222
let contentType = headers["content-type"] as string | undefined
2323
?? res.getHeader("content-type") as string | undefined;
24-
25-
let body: string;
26-
if (typeof data === "object") {
24+
let body: string | Buffer;
25+
if (Buffer.isBuffer(data)) {
26+
body = data;
27+
contentType ??= "application/octet-stream";
28+
}
29+
else if (typeof data === "object") {
2730
body = JSON.stringify(data);
2831
contentType ??= "application/json;charset=utf-8";
2932
}

workspaces/server/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import http from "node:http";
77
import sirv from "sirv";
88
import { PayloadCache } from "@nodesecure/cache";
99
import type { Payload } from "@nodesecure/scanner";
10+
import type { report } from "@nodesecure/report";
1011

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

3739
export async function buildServer(
@@ -47,7 +49,8 @@ export async function buildServer(
4749
scanType = "from",
4850
projectRootDir,
4951
componentsDir,
50-
i18n
52+
i18n,
53+
reporter
5154
} = options;
5255
const cache = await new PayloadCache().load();
5356

@@ -58,7 +61,8 @@ export async function buildServer(
5861
const store: AsyncStoreContext = {
5962
i18n,
6063
viewBuilder,
61-
cache
64+
cache,
65+
reporter
6266
};
6367
if (runFromPayload && dataFilePath !== undefined) {
6468
const payloadStr = await fs.readFile(dataFilePath, "utf-8");

workspaces/server/test/httpServer.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe("httpServer", () => {
4141
({ httpServer } = await buildServer(JSON_PATH, {
4242
projectRootDir: kProjectRootDir,
4343
componentsDir: kComponentsDir,
44+
reporter: async() => Buffer.from("fake-pdf-data"),
4445
i18n: {
4546
english: {
4647
ui: {}
@@ -282,7 +283,7 @@ describe("httpServer", () => {
282283
});
283284
});
284285

285-
test.skip("'/report' should return a Buffer", async() => {
286+
test("'/report' should return a Buffer", async() => {
286287
const result = await post<Buffer>(
287288
new URL("/report", kHttpURL),
288289
{

0 commit comments

Comments
 (0)