Skip to content

Commit

Permalink
feat: apply settings on save (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreDemailly authored Sep 13, 2023
1 parent 45ce309 commit 65144fa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
2 changes: 2 additions & 0 deletions public/js/components/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export class Settings {
});
this.config = newConfig;
this.saveButton.classList.add("disabled");

window.dispatchEvent(new CustomEvent("settings-saved", { detail: this.config }));
}

updateSettings() {
Expand Down
23 changes: 21 additions & 2 deletions public/js/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ document.addEventListener("DOMContentLoaded", async () => {
window.settings = await new Settings().fetchUserConfig();
window.navigation = new ViewNavigation();
window.wiki = new Wiki();
let currentNodeParams = null;

const secureDataSet = new NodeSecureDataSet({
flagsToIgnore: window.settings.config.ignore.flags,
Expand All @@ -30,6 +31,21 @@ document.addEventListener("DOMContentLoaded", async () => {
const nsn = new NodeSecureNetwork(secureDataSet);
nsn.network.on("click", updateShowInfoMenu);

window.addEventListener("settings-saved", async(event) => {
secureDataSet.warningsToIgnore = new Set(event.detail.ignore.warnings);
secureDataSet.flagsToIgnore = new Set(event.detail.ignore.flags);

await secureDataSet.init(secureDataSet.data);
const { nodes } = secureDataSet.build();
nsn.nodes.update(nodes.get());

if (currentNodeParams) {
window.navigation.setNavByName("network--view");
nsn.neighbourHighlight(currentNodeParams);
updateShowInfoMenu(currentNodeParams);
}
});

// Initialize searchbar
{
const dataListElement = document.getElementById("package-list");
Expand All @@ -42,12 +58,15 @@ document.addEventListener("DOMContentLoaded", async () => {

async function updateShowInfoMenu(params) {
if (params.nodes.length === 0) {
currentNodeParams = null;

return PackageInfo.close();
}

const currentNode = params.nodes[0];
currentNodeParams = params;
const currentNode = currentNodeParams.nodes[0];
const selectedNode = secureDataSet.linker.get(
Number(params.nodes[0])
Number(currentNode)
);
new PackageInfo(selectedNode, currentNode, secureDataSet.data.dependencies[selectedNode.name], nsn);
}
Expand Down
35 changes: 32 additions & 3 deletions test/httpServer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import assert from "node:assert";
import { once } from "node:events";

// Import Third-party Dependencies
import { get } from "@myunisoft/httpie";
import { get, MockAgent, getGlobalDispatcher, setGlobalDispatcher } from "@myunisoft/httpie";
import zup from "zup";
import * as i18n from "@nodesecure/i18n";
import * as flags from "@nodesecure/flags";
Expand All @@ -18,8 +18,6 @@ import cacache from "cacache";

// Require Internal Dependencies
import { buildServer } from "../src/http-server/index.js";
import * as root from "../src/http-server/endpoints/root.js";
import * as config from "../src/http-server/endpoints/config.js";

// CONSTANTS
const HTTP_PORT = 17049;
Expand All @@ -31,11 +29,16 @@ const INDEX_HTML = readFileSync(path.join(__dirname, "..", "views", "index.html"

const kCachePath = path.join(os.tmpdir(), "nsecure-cli");
const kConfigKey = "cli-config";
const kGlobalDispatcher = getGlobalDispatcher();
const kMockAgent = new MockAgent();
const kBundlephobiaPool = kMockAgent.get("https://bundlephobia.com");

describe("httpServer", () => {
let httpServer;

before(async() => {
setGlobalDispatcher(kMockAgent);

httpServer = buildServer(JSON_PATH, {
port: HTTP_PORT,
openLink: false
Expand All @@ -47,6 +50,8 @@ describe("httpServer", () => {

after(() => {
httpServer.server.destroy();
kBundlephobiaPool.close();
setGlobalDispatcher(kGlobalDispatcher);
});

test("'/' should return index.html content", async() => {
Expand Down Expand Up @@ -154,6 +159,15 @@ describe("httpServer", () => {
});

test("'/bundle/:name/:version' should return the bundle size", async() => {
kBundlephobiaPool.intercept({
path: () => true
}).reply(200, {
gzip: 1,
size: 1,
dependencySizes: {
foo: 1
}
}, { headers: { "content-type": "application/json" } });
const result = await get(new URL("/bundle/flatstr/1.0.12", HTTP_URL));

assert.equal(result.statusCode, 200);
Expand All @@ -162,6 +176,9 @@ describe("httpServer", () => {
});

test("'/bundle/:name/:version' should return an error if it fails", async() => {
kBundlephobiaPool.intercept({
path: () => true
}).reply(404);
const wrongVersion = undefined;

await assert.rejects(async() => {
Expand All @@ -175,6 +192,15 @@ describe("httpServer", () => {
});

test("'/bundle/:name' should return the bundle size of the last version", async() => {
kBundlephobiaPool.intercept({
path: () => true
}).reply(200, {
gzip: 1,
size: 1,
dependencySizes: {
foo: 1
}
}, { headers: { "content-type": "application/json" } });
const result = await get(new URL("/bundle/flatstr", HTTP_URL));

assert.equal(result.statusCode, 200);
Expand All @@ -183,6 +209,9 @@ describe("httpServer", () => {
});

test("'/bundle/:name' should return an error if it fails", async() => {
kBundlephobiaPool.intercept({
path: () => true
}).reply(404);
const wrongPackageName = "br-br-br-brah";

await assert.rejects(async() => {
Expand Down
3 changes: 3 additions & 0 deletions workspaces/vis-network/src/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export default class NodeSecureDataSet extends EventTarget {
const dataEntries = Object.entries(data.dependencies);
this.dependenciesCount = dataEntries.length;

this.rawEdgesData = [];
this.rawNodesData = [];

for (const [packageName, descriptor] of dataEntries) {
for (const [currVersion, opt] of Object.entries(descriptor.versions)) {
const { id, usedBy, flags, size, license, author, composition, warnings } = opt;
Expand Down
1 change: 1 addition & 0 deletions workspaces/vis-network/src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export default class NodeSecureNetwork {
this.isLoaded = true;
this.network.stopSimulation();
this.network.on("click", this.neighbourHighlight.bind(this));
this.network.setOptions({ physics: false });
});

this.network.stabilize(500);
Expand Down

0 comments on commit 65144fa

Please sign in to comment.