diff --git a/public/core/search-nav.js b/public/core/search-nav.js index 589ef1ec..3e90d24f 100644 --- a/public/core/search-nav.js +++ b/public/core/search-nav.js @@ -44,7 +44,7 @@ export function initSearchNav(data, options) { function initPackagesNavigation(data) { const fragment = document.createDocumentFragment(); - const packages = data.lru; + const packages = data.mru; const hasAtLeast2Packages = packages.length > 1; const hasExactly2Packages = packages.length === 2; diff --git a/public/main.js b/public/main.js index db1928d7..2c203314 100644 --- a/public/main.js +++ b/public/main.js @@ -55,7 +55,7 @@ document.addEventListener("DOMContentLoaded", async() => { }); } else if (data.status === "INIT" || data.status === "RELOAD") { - window.scannedPackageCache = data.older; + window.scannedPackageCache = data.availables; console.log( "[INFO] Older packages are loaded!", window.scannedPackageCache diff --git a/src/cache.js b/src/cache.js index 071cf67a..2f62e81f 100644 --- a/src/cache.js +++ b/src/cache.js @@ -80,9 +80,10 @@ class _AppCache { async #initDefaultPayloadsList() { if (this.startFromZero) { const payloadsList = { + mru: [], lru: [], current: null, - older: [], + availables: [], lastUsed: {}, root: null }; @@ -97,9 +98,10 @@ class _AppCache { const version = Object.keys(payload.dependencies[payload.rootDependencyName].versions)[0]; const formatted = `${payload.rootDependencyName}@${version}`; const payloadsList = { - lru: [formatted], + mru: [formatted], + lru: [], current: formatted, - older: [], + availables: [], lastUsed: { [formatted]: Date.now() }, @@ -135,8 +137,9 @@ class _AppCache { } await cacache.put(CACHE_PATH, `${this.prefix}${kPayloadsCache}`, JSON.stringify({ - older: packagesInFolder, + availables: packagesInFolder, current: null, + mru: [], lru: [] })); } @@ -145,24 +148,24 @@ class _AppCache { fs.rmSync(path.join(kPayloadsPath, pkg.replaceAll("/", "-")), { force: true }); } - async removeLastLRU() { - const { lru, lastUsed, older, ...cache } = await this.payloadsList(); - if (lru.length < kMaxPayloads) { + async removeLastMRU() { + const { mru, lastUsed, lru, ...cache } = await this.payloadsList(); + if (mru.length < kMaxPayloads) { return { ...cache, + mru, lru, - older, lastUsed }; } const packageToBeRemoved = Object.keys(lastUsed) - .filter((key) => lru.includes(key)) + .filter((key) => mru.includes(key)) .sort((a, b) => lastUsed[a] - lastUsed[b])[0]; return { ...cache, - lru: lru.filter((pkg) => pkg !== packageToBeRemoved), - older: [...older, packageToBeRemoved], + mru: mru.filter((pkg) => pkg !== packageToBeRemoved), + lru: [...lru, packageToBeRemoved], lastUsed }; } @@ -176,11 +179,12 @@ class _AppCache { await this.initPayloadsList({ logging }); - const { lru, older, lastUsed } = await this.removeLastLRU(); + const { mru, lru, availables, lastUsed } = await this.removeLastMRU(); const updatedPayloadsCache = { - lru: [...new Set([...lru, pkg])], - older, + mru: [...new Set([...mru, pkg])], + lru, + availables, lastUsed: { ...lastUsed, [pkg]: Date.now() }, current: pkg, root: pkg diff --git a/src/http-server/endpoints/data.js b/src/http-server/endpoints/data.js index 722da167..9e5a04f1 100644 --- a/src/http-server/endpoints/data.js +++ b/src/http-server/endpoints/data.js @@ -21,9 +21,9 @@ export async function get(_req, res) { } try { - const { current, lru } = await appCache.payloadsList(); + const { current, mru } = await appCache.payloadsList(); logger.info(`[data|get](current: ${current})`); - logger.debug(`[data|get](lru: ${lru})`); + logger.debug(`[data|get](lru: ${mru})`); send(res, 200, await appCache.getPayload(current)); } @@ -34,9 +34,10 @@ export async function get(_req, res) { const version = Object.keys(payload.dependencies[payload.rootDependencyName].versions)[0]; const formatted = `${payload.rootDependencyName}@${version}${payload.local ? "#local" : ""}`; const payloadsList = { - lru: [formatted], + mru: [formatted], current: formatted, - older: [], + lru: [], + availables: [], lastUsed: { [formatted]: Date.now() }, diff --git a/src/http-server/websocket/commands/remove.js b/src/http-server/websocket/commands/remove.js index 5103d642..2a469ab0 100644 --- a/src/http-server/websocket/commands/remove.js +++ b/src/http-server/websocket/commands/remove.js @@ -8,44 +8,45 @@ export async function* remove( logger.info(`[ws|remove](pkg: ${pkg}|formatted: ${formattedPkg})`); try { - const { lru, older, current, lastUsed, root } = await cache.payloadsList(); + const { mru, lru, current, lastUsed, root, availables } = await cache.payloadsList(); logger.debug(`[ws|remove](lru: ${lru}|current: ${current})`); - if (lru.length === 1 && older.length === 0) { + if (mru.length === 1 && lru.length === 0) { throw new Error("Cannot remove the last package."); } + const mruIndex = mru.findIndex((pkgName) => pkgName === pkg); const lruIndex = lru.findIndex((pkgName) => pkgName === pkg); - const olderIndex = older.findIndex((pkgName) => pkgName === pkg); - if (lruIndex === -1 && olderIndex === -1) { + if (mruIndex === -1 && lruIndex === -1) { throw new Error("Package not found in cache."); } - if (lruIndex > -1) { + if (mruIndex > -1) { 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 - const olderPkg = older.sort((a, b) => { + const updatedMru = mru.filter((pkgName) => pkgName !== pkg); + if (lru.length > 0) { + // We need to move the first lru package to the mru list + const olderLruPkg = lru.sort((a, b) => { const aDate = lastUsed[a]; const bDate = lastUsed[b]; return aDate - bDate; }); - updatedLru.push(olderPkg[0]); - older.splice(older.indexOf(olderPkg[0]), 1); + updatedMru.push(olderLruPkg[0]); + lru.splice(lru.indexOf(olderLruPkg[0]), 1); } const updatedList = { - lru: updatedLru, - older, + mru: updatedMru, + lru, lastUsed: { ...lastUsed, [pkg]: void 0 }, - current: current === pkg ? updatedLru[0] : current, - root + current: current === pkg ? updatedMru[0] : current, + root, + availables }; await cache.updatePayloadsList(updatedList); @@ -55,11 +56,12 @@ export async function* remove( }; } else { - logger.info(`[ws|remove](remove from older)`); - const updatedOlder = older.filter((pkgName) => pkgName !== pkg); + logger.info(`[ws|remove](remove from lru)`); + const updatedLru = lru.filter((pkgName) => pkgName !== pkg); const updatedList = { - lru, - older: updatedOlder, + mru, + lru: updatedLru, + availables, lastUsed: { ...lastUsed, [pkg]: void 0 diff --git a/src/http-server/websocket/commands/search.js b/src/http-server/websocket/commands/search.js index d9b1f9e2..2ac38685 100644 --- a/src/http-server/websocket/commands/search.js +++ b/src/http-server/websocket/commands/search.js @@ -12,8 +12,8 @@ export async function* search( if (cachedPayload) { logger.info(`[ws|search](payload: ${pkg} found in cache)`); const cacheList = await cache.payloadsList(); - if (cacheList.lru.includes(pkg)) { - logger.info(`[ws|search](payload: ${pkg} is already in the LRU)`); + if (cacheList.mru.includes(pkg)) { + logger.info(`[ws|search](payload: ${pkg} is already in the MRU)`); const updatedList = { ...cacheList, current: pkg, @@ -33,12 +33,13 @@ export async function* search( return; } - const { lru, older, lastUsed, ...updatedCache } = await cache.removeLastLRU(); + const { mru, lru, availables, lastUsed, ...updatedCache } = await cache.removeLastMRU(); const updatedList = { ...updatedCache, - lru: [...new Set([...lru, pkg])], + mru: [...new Set([...mru, pkg])], current: pkg, - older: older.filter((pckg) => pckg !== pkg), + lru: lru.filter((pckg) => pckg !== pkg), + availables: availables.filter((pckg) => pckg !== pkg), lastUsed: { ...lastUsed, [pkg]: Date.now() } }; await cache.updatePayloadsList(updatedList); @@ -68,13 +69,14 @@ export async function* search( logger.info(`[ws|search](scan ${pkg} done|cache: updated)`); // update the payloads list - const { lru, older, lastUsed, ...LRUCache } = await cache.removeLastLRU(); - lru.push(pkg); + const { mru, lru, availables, lastUsed, ...appCache } = await cache.removeLastMRU(); + mru.push(pkg); cache.updatePayload(pkg, payload); const updatedList = { - ...LRUCache, - lru: [...new Set(lru)], - older, + ...appCache, + mru: [...new Set(mru)], + lru, + availables, lastUsed: { ...lastUsed, [pkg]: Date.now() }, current: pkg }; diff --git a/src/http-server/websocket/index.js b/src/http-server/websocket/index.js index ac9d71b9..2fefbf2e 100644 --- a/src/http-server/websocket/index.js +++ b/src/http-server/websocket/index.js @@ -48,18 +48,19 @@ export class WebSocketServerInstanciator { stopInitializationOnError = false ) { try { - const { current, lru, older, root } = await appCache.payloadsList(); - logger.info(`[ws|init](lru: ${lru}|older: ${older}|current: ${current}|root: ${root})`); + const { current, mru, lru, availables, root } = await appCache.payloadsList(); + logger.info(`[ws|init](mru: ${mru}|lru: ${lru}|availables: ${availables}|current: ${current}|root: ${root})`); - if (lru === void 0 || current === void 0) { + if (mru === void 0 || current === void 0) { throw new Error("Payloads list not found in cache."); } return { status: "INIT", current, + mru, lru, - older, + availables, root }; } diff --git a/test/cache.test.js b/test/cache.test.js index 4a8858ae..92068a19 100644 --- a/test/cache.test.js +++ b/test/cache.test.js @@ -115,9 +115,10 @@ describe("appCache", () => { const payloadsList = await appCache.payloadsList(); assert.deepEqual(payloadsList, { + mru: [], lru: [], current: null, - older: [], + availables: [], lastUsed: {}, root: null }); @@ -149,9 +150,10 @@ describe("appCache", () => { const payloadsList = await appCache.payloadsList(); assert.deepEqual(payloadsList, { - lru: ["test_runner@1.0.0"], + mru: ["test_runner@1.0.0"], + lru: [], current: "test_runner@1.0.0", - older: [], + availables: [], lastUsed: { "test_runner@1.0.0": 1234567890 }, root: "test_runner@1.0.0" }); @@ -170,8 +172,9 @@ describe("appCache", () => { const payloadsList = await appCache.payloadsList(); assert.deepEqual(payloadsList, { - older: ["test_runner@1.0.0", "test_runner@2.0.0"], + availables: ["test_runner@1.0.0", "test_runner@2.0.0"], current: null, + mru: [], lru: [] }); }); @@ -187,13 +190,14 @@ describe("appCache", () => { assert.equal(removedPath, path.join(kPayloadsPath, "foo-bar")); }); - it("should not remove the last LRU when LRU is not full", async(t) => { + it("should not remove the last MRU when MRU is not full", async(t) => { t.mock.method(cacache, "get", () => { return { data: { toString: () => JSON.stringify({ - lru: ["foo"], - older: ["bar"], + mru: ["foo"], + lru: ["bar"], + availables: [], lastUsed: { foo: 1234567890 }, foo: "bar" }) @@ -201,23 +205,25 @@ describe("appCache", () => { }; }); - const result = await appCache.removeLastLRU(); + const result = await appCache.removeLastMRU(); assert.deepEqual(result, { - lru: ["foo"], - older: ["bar"], + mru: ["foo"], + lru: ["bar"], + availables: [], lastUsed: { foo: 1234567890 }, foo: "bar" }); }); - it("should remove the last LRU when LRU is full", async(t) => { + it("should remove the last MRU when MRU is full", async(t) => { t.mock.method(cacache, "get", () => { return { data: { toString: () => JSON.stringify({ - lru: ["foo", "foz", "bar"], - older: ["boz"], + mru: ["foo", "foz", "bar"], + lru: ["boz"], + availables: [], lastUsed: { foo: 123, foz: 1234, @@ -229,11 +235,12 @@ describe("appCache", () => { }; }); - const result = await appCache.removeLastLRU(); + const result = await appCache.removeLastMRU(); assert.deepEqual(result, { - lru: ["foz", "bar"], - older: ["boz", "foo"], + mru: ["foz", "bar"], + lru: ["boz", "foo"], + availables: [], lastUsed: { foo: 123, foz: 1234, @@ -247,9 +254,10 @@ describe("appCache", () => { t.mock.method(fs, "writeFileSync", () => void 0); t.mock.method(Date, "now", () => 1234567890); await appCache.updatePayloadsList({ + mru: [], lru: [], current: null, - older: [], + availables: [], lastUsed: {}, root: null }); @@ -268,9 +276,10 @@ describe("appCache", () => { const result = await appCache.payloadsList(); assert.deepEqual(result, { - lru: ["test_runner-local@1.0.0#local"], + mru: ["test_runner-local@1.0.0#local"], + lru: [], current: "test_runner-local@1.0.0#local", - older: [], + availables: [], lastUsed: { "test_runner-local@1.0.0#local": 1234567890 }, @@ -282,9 +291,10 @@ describe("appCache", () => { t.mock.method(fs, "writeFileSync", () => void 0); t.mock.method(Date, "now", () => 1234567890); await appCache.updatePayloadsList({ + mru: [], lru: [], current: null, - older: [], + availables: [], lastUsed: {}, root: null }); @@ -303,9 +313,10 @@ describe("appCache", () => { const result = await appCache.payloadsList(); assert.deepEqual(result, { - lru: ["test_runner-local@1.0.0"], + mru: ["test_runner-local@1.0.0"], + lru: [], current: "test_runner-local@1.0.0", - older: [], + availables: [], lastUsed: { "test_runner-local@1.0.0": 1234567890 },