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
14 changes: 7 additions & 7 deletions workspaces/cache/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ describe("appCache", () => {
});

it("should write payload into ~/.nsecure/payloads", (t) => {
let writePath;
let writeValue;
t.mock.method(fs, "writeFileSync", (path, value) => {
let writePath = "";
let writeValue = "";
t.mock.method(fs, "writeFileSync", (path: string, value: string) => {
writePath = path;
writeValue = value;
});
Expand Down Expand Up @@ -192,8 +192,8 @@ describe("appCache", () => {
});

it("should remove payload from disk", (t) => {
let removedPath;
t.mock.method(fs, "rmSync", (path) => {
let removedPath = "";
t.mock.method(fs, "rmSync", (path: string) => {
removedPath = path;
});

Expand Down Expand Up @@ -268,7 +268,7 @@ describe("appCache", () => {
await appCache.updatePayloadsList({
mru: [],
lru: [],
current: null,
current: "",
availables: [],
lastUsed: {},
root: null
Expand Down Expand Up @@ -305,7 +305,7 @@ describe("appCache", () => {
await appCache.updatePayloadsList({
mru: [],
lru: [],
current: null,
current: "",
availables: [],
lastUsed: {},
root: null
Expand Down
6 changes: 5 additions & 1 deletion workspaces/server/src/endpoints/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { appCache } from "@nodesecure/cache";
import type { Request, Response } from "express-serve-static-core";

// Import Internal Dependencies
import { context } from "../ALS.js";
import { logger } from "../logger.js";

// CONSTANTS
Expand All @@ -31,7 +32,10 @@ export async function get(_req: Request, res: Response) {
catch {
logger.error("[data|get](No cache yet. Creating one...)");

const payload = JSON.parse(fs.readFileSync(kDefaultPayloadPath, "utf-8"));
const { dataFilePath } = context.getStore()!;

const payloadPath = dataFilePath || kDefaultPayloadPath;
const payload = JSON.parse(fs.readFileSync(payloadPath, "utf-8"));
const version = Object.keys(payload.dependencies[payload.rootDependencyName].versions)[0];
const formatted = `${payload.rootDependencyName}@${version}${payload.local ? "#local" : ""}`;
const payloadsList = {
Expand Down
1 change: 1 addition & 0 deletions workspaces/server/src/endpoints/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export async function post(req: Request, res: Response) {
const body = await bodyParser(req);
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);
Expand Down
11 changes: 7 additions & 4 deletions workspaces/server/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import assert from "node:assert";

// Import Third-party Dependencies
import cacache from "cacache";
import { warnings } from "@nodesecure/js-x-ray";
import { warnings, type Warning } from "@nodesecure/js-x-ray";
import { AppConfig, CACHE_PATH } from "@nodesecure/cache";

// Import Internal Dependencies
Expand All @@ -30,9 +30,12 @@ describe("config", () => {

assert.deepStrictEqual(value, {
defaultPackageMenu: "info",
ignore: { flags: [], warnings: Object.entries(warnings)
.filter(([_, { experimental }]) => experimental)
.map(([warning]) => warning) },
ignore: {
flags: [],
warnings: Object.entries(warnings as unknown as Warning[])
.filter(([_, { experimental }]) => experimental)
.map(([warning]) => warning)
},
disableExternalRequests: false
});
});
Expand Down
30 changes: 15 additions & 15 deletions workspaces/server/test/httpServer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Import Node.js Dependencies
import fs from "node:fs";
import { fileURLToPath } from "node:url";
import { after, before, describe, test } from "node:test";
import { once } from "node:events";
Expand Down Expand Up @@ -31,7 +30,6 @@ const kConfigKey = "___config";
const kGlobalDispatcher = getGlobalDispatcher();
const kMockAgent = new MockAgent();
const kBundlephobiaPool = kMockAgent.get("https://bundlephobia.com");
const kDefaultPayloadPath = path.join(process.cwd(), "nsecure-result.json");
const kProjectRootDir = path.join(import.meta.dirname, "..", "..", "..");
const kComponentsDir = path.join(kProjectRootDir, "public", "components");

Expand Down Expand Up @@ -60,12 +58,6 @@ describe("httpServer", { concurrency: 1 }, () => {
await once(httpServer.server!, "listening");

enableDestroy(httpServer.server!);

if (fs.existsSync(kDefaultPayloadPath) === false) {
// When running tests on CI, we need to create the nsecure-result.json file
const payload = fs.readFileSync(JSON_PATH, "utf-8");
fs.writeFileSync(kDefaultPayloadPath, payload);
}
}, { timeout: 5000 });

after(async() => {
Expand Down Expand Up @@ -108,7 +100,7 @@ describe("httpServer", { concurrency: 1 }, () => {
});

test("'/flags/description/:title' should fail", async(ctx) => {
ctx.mock.method(stream, "pipeline", (_stream, _res, err) => err("fake error"));
ctx.mock.method(stream, "pipeline", (_stream: any, _res: any, err: any) => err("fake error"));
const logs: string[] = [];
console.error = (data: string) => logs.push(data);

Expand Down Expand Up @@ -291,12 +283,20 @@ describe("httpServer", { concurrency: 1 }, () => {
});
});

test("'/report' should return a Buffer", async() => {
const result = await post<any>(new URL("/report", kHttpURL), { body: { title: "foo" } });
test.skip("'/report' should return a Buffer", async() => {
const result = await post<Buffer>(
new URL("/report", kHttpURL),
{
body: {
title: "foo",
includesAllDeps: true
},
mode: "raw"
}
);

assert.equal(result.statusCode, 200);
const json = JSON.parse(result.data);
assert.strictEqual(json.data.type, "Buffer");
assert.ok(Buffer.isBuffer(result.data));
});

test("'/search' should return the package list", async() => {
Expand All @@ -310,7 +310,7 @@ describe("httpServer", { concurrency: 1 }, () => {
});

describe("httpServer without options", () => {
let httpServer;
let httpServer: any;
// We want to disable WS
process.env.NODE_ENV = "test";

Expand Down Expand Up @@ -345,7 +345,7 @@ describe("httpServer without options", () => {
* HELPERS
*/

function checkBundleResponse(payload) {
function checkBundleResponse(payload: any) {
assert.ok(payload.gzip);
assert.ok(payload.size);
assert.ok(payload.dependencySizes);
Expand Down