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
2 changes: 1 addition & 1 deletion public/core/search-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 18 additions & 14 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ class _AppCache {
async #initDefaultPayloadsList() {
if (this.startFromZero) {
const payloadsList = {
mru: [],
lru: [],
current: null,
older: [],
availables: [],
lastUsed: {},
root: null
};
Expand All @@ -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()
},
Expand Down Expand Up @@ -135,8 +137,9 @@ class _AppCache {
}

await cacache.put(CACHE_PATH, `${this.prefix}${kPayloadsCache}`, JSON.stringify({
older: packagesInFolder,
availables: packagesInFolder,
current: null,
mru: [],
lru: []
}));
}
Expand All @@ -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
};
}
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/http-server/endpoints/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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()
},
Expand Down
40 changes: 21 additions & 19 deletions src/http-server/websocket/commands/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down
22 changes: 12 additions & 10 deletions src/http-server/websocket/commands/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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
};
Expand Down
9 changes: 5 additions & 4 deletions src/http-server/websocket/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
Expand Down
Loading