Skip to content

Commit

Permalink
chore: improve logs
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreDemailly committed Nov 30, 2024
1 parent 6ba5b71 commit c6f9d78
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 39 deletions.
1 change: 0 additions & 1 deletion public/core/search-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ function addRemoveButton(pkgElement, options) {
removeButton.textContent = "x";
removeButton.addEventListener("click", (event) => {
event.stopPropagation();
// we remove the x button from textContent
const pkgToRemove = pkgElement.dataset.name;
window.socket.send(JSON.stringify({ action: "REMOVE", pkg: pkgToRemove }));

Expand Down
2 changes: 1 addition & 1 deletion public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ async function init(options = { navigateToNetworkView: false }) {
const searchNavElement = document.getElementById("search-nav");
const pkgs = searchNavElement.querySelectorAll(".package");
for (const pkg of pkgs) {
if (pkg.textContent.startsWith(window.activePackage)) {
if (pkg.dataset.name.startsWith(window.activePackage)) {
pkg.classList.add("active");
}
else {
Expand Down
10 changes: 5 additions & 5 deletions src/http-server/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class _AppCache {
return JSON.parse(fs.readFileSync(path.join(kPayloadsPath, pkg.replaceAll("/", "-")), "utf-8"));
}
catch (err) {
logger.error(`[CACHE | GET_PAYLOAD](pkg: ${pkg}|cache: not found)`);
logger.error(`[cache|get](pkg: ${pkg}|cache: not found)`);

throw err;
}
Expand All @@ -68,7 +68,7 @@ class _AppCache {
return JSON.parse(data.toString());
}
catch (err) {
logger.error(`[CACHE | PAYLOADS_LIST](cache: not found)`);
logger.error(`[cache|get](cache: not found)`);

throw err;
}
Expand All @@ -87,8 +87,8 @@ class _AppCache {
},
root: formatted
};
// eslint-disable-next-line @stylistic/max-len
logger.info(`[CACHE | INIT_PAYLOADS_LIST](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`);

logger.info(`[cache|init](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`);
await cacache.put(CACHE_PATH, kPayloadsCache, JSON.stringify(payloadsList));
this.updatePayload(formatted.replaceAll("/", "-"), payload);
}
Expand All @@ -102,7 +102,7 @@ class _AppCache {
}

const list = packagesInFolder.map(({ name }) => name);
logger.info(`[CACHE | INIT_PAYLOADS_LIST](list: ${list})`);
logger.info(`[cache|init](list: ${list})`);

await cacache.put(CACHE_PATH, kPayloadsCache, JSON.stringify({ list, current: list[0] }));
}
Expand Down
19 changes: 13 additions & 6 deletions src/http-server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,37 @@ export async function get() {
try {
const config = await appCache.getConfig();

logger.info(`[CONFIG | GET](config: ${config})`);
const {
defaultPackageMenu,
ignore: {
flags,
warnings
}
} = config;
logger.info(`[config|get](defaultPackageMenu: ${defaultPackageMenu}|ignore-flag: ${flags}|ignore-warnings: ${warnings})`);

return config;
}
catch (err) {
logger.error(`[CONFIG | GET](error: ${err.message})`);
logger.error(`[config|get](error: ${err.message})`);

await appCache.updateConfig(kDefaultConfig);

logger.info(`[CONFIG | GET](fallback to default: ${JSON.stringify(kDefaultConfig)})`);
logger.info(`[config|get](fallback to default: ${JSON.stringify(kDefaultConfig)})`);

return kDefaultConfig;
}
}

export async function set(newValue) {
logger.info(`[CONFIG | SET](config: ${newValue})`);
logger.info(`[config|set](config: ${newValue})`);
try {
await appCache.updateConfig(newValue);

logger.info(`[CONFIG | SET](sucess)`);
logger.info(`[config|set](sucess)`);
}
catch (err) {
logger.error(`[CONFIG | SET](error: ${err.message})`);
logger.error(`[config|set](error: ${err.message})`);

throw err;
}
Expand Down
10 changes: 5 additions & 5 deletions src/http-server/endpoints/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ const kDefaultPayloadPath = path.join(process.cwd(), "nsecure-result.json");
export async function get(req, res) {
try {
const { current, lru } = await appCache.payloadsList();
logger.info(`[DATA | GET](current: ${current})`);
logger.debug(`[DATA | GET](lru: ${lru})`);
logger.info(`[data|get](current: ${current})`);
logger.debug(`[data|get](lru: ${lru})`);

const formatted = current.replaceAll("/", "-");
send(res, 200, await appCache.getPayload(formatted));
}
catch {
logger.error(`[DATA | GET](No cache yet. Creating one...)`);
logger.error(`[data|get](No cache yet. Creating one...)`);

const payload = JSON.parse(fs.readFileSync(kDefaultPayloadPath, "utf-8"));
const version = Object.keys(payload.dependencies[payload.rootDependencyName].versions)[0];
Expand All @@ -36,11 +36,11 @@ export async function get(req, res) {
},
root: formatted
};
logger.info(`[DATA | GET](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`);
logger.info(`[data|get](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`);

await appCache.updatePayloadsList(payloadsList);
appCache.updatePayload(formatted.replaceAll("/", "-"), payload);
logger.info(`[DATA | GET](cache: created|payloadsList: ${payloadsList.lru})`);
logger.info(`[data|get](cache: created|payloadsList: ${payloadsList.lru})`);

send(res, 200, payload);
}
Expand Down
10 changes: 5 additions & 5 deletions src/http-server/endpoints/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { logger } from "../logger.js";

export async function get(req, res) {
const { packageName } = req.params;
logger.info(`[SEARCH: GET](packageName: ${packageName}|formatted: ${decodeURIComponent(packageName)})`);
logger.info(`[search|get](packageName: ${packageName}|formatted: ${decodeURIComponent(packageName)})`);

const { objects, total } = await npm.search({
text: decodeURIComponent(packageName)
});
logger.debug(`[SEARCH: GET](npmSearchResult: ${JSON.stringify(objects.map((pkg) => pkg.package.name))})`);
logger.debug(`[search|get](npmSearchResult: ${JSON.stringify(objects.map((pkg) => pkg.package.name))})`);

send(res, 200, {
count: total,
Expand All @@ -29,13 +29,13 @@ export async function get(req, res) {
export async function versions(req, res) {
const { packageName } = req.params;

logger.info(`[SEARCH: VERSIONS](packageName: ${packageName}|formatted: ${decodeURIComponent(packageName)})`);
logger.info(`[search|versions](packageName: ${packageName}|formatted: ${decodeURIComponent(packageName)})`);

const packument = await npm.packument(decodeURIComponent(packageName));
const versions = Object.keys(packument.versions);

logger.info(`[SEARCH: VERSIONS](packageName: ${packageName}|versions: ${versions})`);
logger.debug(`[SEARCH: VERSIONS](packument: ${packument})`);
logger.info(`[search|versions](packageName: ${packageName}|versions: ${versions})`);
logger.debug(`[search|versions](packument: ${packument})`);

send(res, 200, versions);
}
2 changes: 1 addition & 1 deletion src/http-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function buildServer(dataFilePath, options = {}) {
websocket.on("connection", async(socket) => {
socket.on("message", async(rawMessage) => {
const message = JSON.parse(rawMessage);
logger.info(`[WEBSOCKET](message: ${JSON.stringify(message)})`);
logger.info(`[ws](message: ${JSON.stringify(message)})`);

if (message.action === "SEARCH") {
wsHandlers.search(socket, message.pkg);
Expand Down
6 changes: 4 additions & 2 deletions src/http-server/logger.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Import Third-party Dependencies
import pino from "pino";

// CONSTANTS
const kDefaultLogLevel = "info";

const logger = pino({
// TODO: info
level: "debug",
level: process.env.LOG_LEVEL ?? kDefaultLogLevel,
transport: {
target: "pino-pretty"
}
Expand Down
4 changes: 2 additions & 2 deletions src/http-server/websocket/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { logger } from "../logger.js";
export async function init(socket, lock = false) {
try {
const { current, lru, older, root } = await appCache.payloadsList();
logger.info(`[WEBSOCKET | INIT](lru: ${lru}|older: ${older}|current: ${current}|root: ${root})`);
logger.info(`[ws|init](lru: ${lru}|older: ${older}|current: ${current}|root: ${root})`);

if (lru === void 0 || current === void 0) {
throw new Error("Payloads list not found in cache.");
Expand All @@ -20,7 +20,7 @@ export async function init(socket, lock = false) {
}));
}
catch {
logger.error(`[WEBSOCKET | INIT](No cache yet. Creating one...)`);
logger.error(`[ws|init](No cache yet. Creating one...)`);

if (lock) {
return;
Expand Down
10 changes: 5 additions & 5 deletions src/http-server/websocket/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { logger } from "../logger.js";

export async function remove(ws, pkg) {
const formattedPkg = pkg.replace("/", "-");
logger.info(`[WEBSOCKET | REMOVE](pkg: ${pkg}|formatted: ${formattedPkg})`);
logger.info(`[ws|remove](pkg: ${pkg}|formatted: ${formattedPkg})`);

try {
const { lru, older, current, lastUsed, root } = await appCache.payloadsList();
logger.debug(`[WEBSOCKET | REMOVE](lru: ${lru}|current: ${current})`);
logger.debug(`[ws|remove](lru: ${lru}|current: ${current})`);

if (lru.length === 1 && older.length === 0) {
throw new Error("Cannot remove the last package.");
Expand All @@ -22,7 +22,7 @@ export async function remove(ws, pkg) {
}

if (lruIndex > -1) {
logger.info(`[WEBSOCKET | REMOVE](remove from lru)`);
logger.info(`[ws|remove](remove from lru)`);
const updatedLru = lru.filter((pkgName) => pkgName !== pkg);
if (older.length > 0) {
// We need to move the first older package to the lru list
Expand Down Expand Up @@ -54,7 +54,7 @@ export async function remove(ws, pkg) {
}));
}
else {
logger.info(`[WEBSOCKET | REMOVE](remove from older)`);
logger.info(`[ws|remove](remove from older)`);
const updatedOlder = older.filter((pkgName) => pkgName !== pkg);
const updatedList = {
lru,
Expand All @@ -77,7 +77,7 @@ export async function remove(ws, pkg) {
appCache.removePayload(formattedPkg.replaceAll("/", "-"));
}
catch (error) {
logger.error(`[WEBSOCKET | REMOVE](error: ${error.message})`);
logger.error(`[ws|remove](error: ${error.message})`);
logger.debug(error);

throw error;
Expand Down
12 changes: 6 additions & 6 deletions src/http-server/websocket/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { logger } from "../logger.js";
import { appCache } from "../cache.js";

export async function search(ws, pkg) {
logger.info(`[WEBSOCKET | SEARCH](pkg: ${pkg})`);
logger.info(`[ws|search](pkg: ${pkg})`);

const cache = await appCache.getPayloadOrNull(pkg);
if (cache) {
logger.info(`[WEBSOCKET | SEARCH](payload: ${pkg} found in cache)`);
logger.info(`[ws|search](payload: ${pkg} found in cache)`);
const cacheList = await appCache.payloadsList();
if (cacheList.lru.includes(pkg)) {
logger.info(`[WEBSOCKET | SEARCH](payload: ${pkg} is already in the LRU)`);
logger.info(`[ws|search](payload: ${pkg} is already in the LRU)`);
const updatedList = {
...cacheList,
current: pkg,
Expand Down Expand Up @@ -44,7 +44,7 @@ export async function search(ws, pkg) {
}

// at this point we don't have the payload in cache so we have to scan it.
logger.info(`[WEBSOCKET | SEARCH](scan ${pkg})`);
logger.info(`[ws|search](scan ${pkg} in progress)`);
ws.send(JSON.stringify({ status: "SCAN", pkg }));

const payload = await Scanner.from(pkg, { maxDepth: 4 });
Expand All @@ -54,7 +54,7 @@ export async function search(ws, pkg) {
{
// save the payload in cache
const pkg = `${name}@${version}`;
logger.info(`[WEBSOCKET | SEARCH](scan <${pkg}> done|cache: updated|pkg: ${pkg})`);
logger.info(`[ws|search](scan ${pkg} done|cache: updated)`);

// update the payloads list
const { lru, older, lastUsed, root } = await appCache.removeLastLRU();
Expand All @@ -75,6 +75,6 @@ export async function search(ws, pkg) {
...updatedList
}));

logger.info(`[WEBSOCKET | SEARCH](payloadsList updated|payload sent to client)`);
logger.info(`[ws|search](data sent to client|cache: updated)`);
}
}

0 comments on commit c6f9d78

Please sign in to comment.