Skip to content

Commit 9c6c4ba

Browse files
committed
refactor: websocket messages & server logs
1 parent 2757be5 commit 9c6c4ba

7 files changed

Lines changed: 155 additions & 163 deletions

File tree

public/components/views/search/search.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,8 @@ export class SearchView {
248248
}
249249

250250
fetchPackage(packageName, version) {
251-
const pkg = `${packageName}@${version}`;
252-
253-
window.socket.commands.search(pkg);
251+
const spec = `${packageName}@${version}`;
252+
window.socket.commands.search(spec);
254253
}
255254

256255
async fetchPackageVersions(packageName) {
@@ -259,7 +258,7 @@ export class SearchView {
259258
return versions.reverse();
260259
}
261260

262-
onScan(pkg) {
261+
onScan(spec) {
263262
const searchViewForm = document.querySelector("#search--view form");
264263
searchViewForm?.remove();
265264
const containerResult = document.querySelector("#search--view .result-container");
@@ -268,7 +267,7 @@ export class SearchView {
268267
const searchViewContainer = document.querySelector("#search--view .container");
269268
const scanInfo = document.createElement("div");
270269
scanInfo.classList.add("scan-info");
271-
scanInfo.textContent = `Scanning ${pkg}.`;
270+
scanInfo.textContent = `Scanning ${spec}`;
272271
const spinner = document.createElement("div");
273272
spinner.classList.add("spinner");
274273
searchViewContainer.appendChild(scanInfo);

public/main.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ document.addEventListener("DOMContentLoaded", async() => {
4949
socket.addEventListener("SCAN", (event) => {
5050
const data = event.detail;
5151

52-
searchview.onScan(data.pkg);
52+
searchview.onScan(data.spec);
5353
});
5454
});
5555

@@ -73,9 +73,10 @@ async function onSocketPayload(event) {
7373

7474
async function onSocketInitOrReload(event) {
7575
const data = event.detail;
76+
const { cache } = data;
7677

77-
window.scannedPackageCache = data.availables;
78-
window.recentPackageCache = data.lru;
78+
window.scannedPackageCache = cache.availables;
79+
window.recentPackageCache = cache.lru;
7980
console.log(
8081
"[INFO] Older packages are loaded!",
8182
window.scannedPackageCache
@@ -85,7 +86,7 @@ async function onSocketInitOrReload(event) {
8586
window.recentPackageCache
8687
);
8788

88-
initSearchNav(data, {
89+
initSearchNav(cache, {
8990
searchOptions: {
9091
nsn,
9192
secureDataSet
@@ -107,9 +108,10 @@ async function onSocketInitOrReload(event) {
107108
await init();
108109

109110
// FIXME: initSearchNav is called twice, we need to fix this
110-
initSearchNav(data, {
111+
initSearchNav(cache, {
111112
searchOptions: {
112-
nsn, secureDataSet
113+
nsn,
114+
secureDataSet
113115
}
114116
});
115117
}

public/websocket.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export class WebSocketClient extends EventTarget {
1010
this.client = new WebSocket(url);
1111
this.client.addEventListener("message", this.#messageHandler.bind(this));
1212
this.commands = {
13-
search: (pkg) => this.send({ action: "SEARCH", pkg }),
14-
remove: (pkg) => this.send({ action: "REMOVE", pkg })
13+
search: (spec) => this.send({ commandName: "SEARCH", spec }),
14+
remove: (spec) => this.send({ commandName: "REMOVE", spec })
1515
};
1616

1717
window.socket = this;

workspaces/server/src/websocket/commands/remove.ts

Lines changed: 84 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -8,111 +8,101 @@ import type {
88
} from "../websocket.types.js";
99

1010
export async function* remove(
11-
pkg: string,
11+
spec: string,
1212
context: WebSocketContext
1313
): AsyncGenerator<WebSocketResponse, void, unknown> {
1414
const { cache, logger } = context;
1515

16-
logger.info(`[ws|remove](pkg: ${pkg})`);
17-
18-
try {
19-
const { mru, lru, current, lastUsed, root, availables } = await cache.payloadsList();
20-
delete lastUsed[pkg];
21-
if (availables.includes(pkg)) {
22-
logger.info("[ws|remove] remove from availables");
23-
cache.removePayload(pkg);
24-
const updatedList: PayloadsList = {
25-
mru,
26-
current,
27-
lru,
28-
lastUsed: {
29-
...lastUsed
30-
},
31-
root,
32-
availables: availables.filter((pkgName) => pkgName !== pkg)
33-
};
34-
await cache.updatePayloadsList(updatedList);
35-
36-
yield {
37-
status: "RELOAD",
38-
...updatedList
39-
};
40-
41-
return;
42-
}
43-
44-
logger.debug(`[ws|remove](lru: ${lru}|current: ${current})`);
45-
46-
if (mru.length === 1 && lru.length === 0) {
47-
throw new Error("Cannot remove the last package.");
48-
}
49-
50-
const mruIndex = mru.findIndex((pkgName) => pkgName === pkg);
51-
const lruIndex = lru.findIndex((pkgName) => pkgName === pkg);
52-
53-
if (mruIndex === -1 && lruIndex === -1) {
54-
throw new Error("Package not found in cache.");
55-
}
16+
const { mru, lru, current, lastUsed, root, availables } = await cache.payloadsList();
17+
delete lastUsed[spec];
18+
if (availables.includes(spec)) {
19+
logger.info("[ws|command.remove] remove from availables");
20+
cache.removePayload(spec);
21+
const updatedList: PayloadsList = {
22+
mru,
23+
current,
24+
lru,
25+
lastUsed: {
26+
...lastUsed
27+
},
28+
root,
29+
availables: availables.filter((iterSpec) => iterSpec !== spec)
30+
};
31+
await cache.updatePayloadsList(updatedList);
32+
33+
yield {
34+
status: "RELOAD",
35+
cache: updatedList
36+
};
37+
38+
return;
39+
}
5640

57-
if (mruIndex > -1) {
58-
logger.info("[ws|remove](remove from lru)");
59-
const updatedMru = mru.filter((pkgName) => pkgName !== pkg);
60-
if (lru.length > 0) {
61-
// We need to move the first lru package to the mru list
62-
const olderLruPkg = lru.sort((a, b) => {
63-
const aDate = lastUsed[a];
64-
const bDate = lastUsed[b];
41+
logger.debug(`[ws|command.remove](lru: ${lru}|current: ${current})`);
6542

66-
return aDate - bDate;
67-
});
68-
updatedMru.push(olderLruPkg[0]);
69-
lru.splice(lru.indexOf(olderLruPkg[0]), 1);
70-
}
43+
if (mru.length === 1 && lru.length === 0) {
44+
throw new Error("Cannot remove the last package.");
45+
}
7146

72-
const updatedList: PayloadsList = {
73-
mru: updatedMru,
74-
lru,
75-
lastUsed: {
76-
...lastUsed
77-
},
78-
current: current === pkg ? updatedMru[0] : current,
79-
root,
80-
availables
81-
};
82-
await cache.updatePayloadsList(updatedList);
47+
const mruIndex = mru.findIndex((iterSpec) => iterSpec === spec);
48+
const lruIndex = lru.findIndex((iterSpec) => iterSpec === spec);
8349

84-
yield {
85-
status: "RELOAD",
86-
...updatedList
87-
};
88-
}
89-
else {
90-
logger.info("[ws|remove](remove from lru)");
91-
const updatedLru = lru.filter((pkgName) => pkgName !== pkg);
92-
const updatedList: PayloadsList = {
93-
mru,
94-
lru: updatedLru,
95-
availables,
96-
lastUsed: {
97-
...lastUsed
98-
},
99-
current,
100-
root
101-
};
102-
await cache.updatePayloadsList(updatedList);
50+
if (mruIndex === -1 && lruIndex === -1) {
51+
throw new Error("Package not found in cache.");
52+
}
10353

104-
yield {
105-
status: "RELOAD",
106-
...updatedList
107-
};
54+
if (mruIndex > -1) {
55+
logger.info("[ws|command.remove] removed from mru");
56+
const updatedMru = mru.filter((iterSpec) => iterSpec !== spec);
57+
if (lru.length > 0) {
58+
// We need to move the first lru package to the mru list
59+
const olderLruPkg = lru.sort((a, b) => {
60+
const aDate = lastUsed[a];
61+
const bDate = lastUsed[b];
62+
63+
return aDate - bDate;
64+
});
65+
updatedMru.push(olderLruPkg[0]);
66+
lru.splice(lru.indexOf(olderLruPkg[0]), 1);
10867
}
10968

110-
cache.removePayload(pkg);
69+
const updatedList: PayloadsList = {
70+
mru: updatedMru,
71+
lru,
72+
lastUsed: {
73+
...lastUsed
74+
},
75+
current: current === spec ? updatedMru[0] : current,
76+
root,
77+
availables
78+
};
79+
await cache.updatePayloadsList(updatedList);
80+
81+
yield {
82+
status: "RELOAD",
83+
cache: updatedList
84+
};
11185
}
112-
catch (error: any) {
113-
logger.error(`[ws|remove](error: ${error.message})`);
114-
logger.debug(error);
115-
116-
throw error;
86+
else {
87+
logger.info("[ws|command.remove] removed from lru");
88+
const updatedLru = lru.filter((iterSpec) => iterSpec !== spec);
89+
const updatedList: PayloadsList = {
90+
mru,
91+
lru: updatedLru,
92+
availables,
93+
lastUsed: {
94+
...lastUsed
95+
},
96+
current,
97+
root
98+
};
99+
await cache.updatePayloadsList(updatedList);
100+
101+
yield {
102+
status: "RELOAD",
103+
cache: updatedList
104+
};
117105
}
106+
107+
cache.removePayload(spec);
118108
}

workspaces/server/src/websocket/commands/search.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@ import type {
99
} from "../websocket.types.js";
1010

1111
export async function* search(
12-
pkg: string,
12+
spec: string,
1313
context: WebSocketContext
1414
): AsyncGenerator<WebSocketResponse, void, unknown> {
1515
const { logger, cache } = context;
16-
logger.info(`[ws|search](pkg: ${pkg})`);
1716

18-
const cachedPayload = cache.getPayloadOrNull(pkg);
17+
const cachedPayload = cache.getPayloadOrNull(spec);
1918
if (cachedPayload) {
20-
logger.info(`[ws|search](payload: ${pkg} found in cache)`);
19+
logger.info("[ws|command.search] one entry found in cache");
2120
const cacheList = await cache.payloadsList();
22-
if (cacheList.mru.includes(pkg)) {
23-
logger.info(`[ws|search](payload: ${pkg} is already in the MRU)`);
21+
if (cacheList.mru.includes(spec)) {
22+
logger.info("[ws|command.search] payload is already in the MRU");
2423
const updatedList: PayloadsList = {
2524
...cacheList,
26-
current: pkg,
27-
lastUsed: { ...cacheList.lastUsed, [pkg]: Date.now() }
25+
current: spec,
26+
lastUsed: { ...cacheList.lastUsed, [spec]: Date.now() }
2827
};
2928
await cache.updatePayloadsList(updatedList);
3029
yield {
@@ -35,7 +34,7 @@ export async function* search(
3534
if (cache.startFromZero) {
3635
yield {
3736
status: "RELOAD" as const,
38-
...updatedList
37+
cache: updatedList
3938
};
4039
cache.startFromZero = false;
4140
}
@@ -46,11 +45,11 @@ export async function* search(
4645
const { mru, lru, availables, lastUsed, ...updatedCache } = await cache.removeLastMRU();
4746
const updatedList: PayloadsList = {
4847
...updatedCache,
49-
mru: [...new Set([...mru, pkg])],
50-
current: pkg,
51-
lru: lru.filter((pckg) => pckg !== pkg),
52-
availables: availables.filter((pckg) => pckg !== pkg),
53-
lastUsed: { ...lastUsed, [pkg]: Date.now() }
48+
mru: [...new Set([...mru, spec])],
49+
current: spec,
50+
lru: lru.filter((pckg) => pckg !== spec),
51+
availables: availables.filter((pckg) => pckg !== spec),
52+
lastUsed: { ...lastUsed, [spec]: Date.now() }
5453
};
5554
await cache.updatePayloadsList(updatedList);
5655

@@ -60,7 +59,7 @@ export async function* search(
6059
};
6160
yield {
6261
status: "RELOAD" as const,
63-
...updatedList
62+
cache: updatedList
6463
};
6564

6665
cache.startFromZero = false;
@@ -69,29 +68,29 @@ export async function* search(
6968
}
7069

7170
// at this point we don't have the payload in cache so we have to scan it.
72-
logger.info(`[ws|search](scan ${pkg} in progress)`);
73-
yield { status: "SCAN" as const, pkg };
71+
logger.info(`[ws|command.search](scan ${spec} in progress)`);
72+
yield { status: "SCAN" as const, spec };
7473

75-
const payload = await scanner.from(pkg, { maxDepth: 4 });
74+
const payload = await scanner.from(spec, { maxDepth: 4 });
7675
const name = payload.rootDependencyName;
7776
const version = Object.keys(payload.dependencies[name].versions)[0];
7877

7978
{
8079
// save the payload in cache
81-
const pkg = `${name}@${version}`;
82-
logger.info(`[ws|search](scan ${pkg} done|cache: updated)`);
80+
const inScanPackageSpec = `${name}@${version}`;
81+
logger.info(`[ws|command.search](scan ${inScanPackageSpec} done|cache: updated)`);
8382

8483
// update the payloads list
8584
const { mru, lru, availables, lastUsed, ...appCache } = await cache.removeLastMRU();
86-
mru.push(pkg);
87-
cache.updatePayload(pkg, payload);
85+
mru.push(inScanPackageSpec);
86+
cache.updatePayload(inScanPackageSpec, payload);
8887
const updatedList: PayloadsList = {
8988
...appCache,
9089
mru: [...new Set(mru)],
9190
lru,
9291
availables,
93-
lastUsed: { ...lastUsed, [pkg]: Date.now() },
94-
current: pkg
92+
lastUsed: { ...lastUsed, [inScanPackageSpec]: Date.now() },
93+
current: inScanPackageSpec
9594
};
9695
await cache.updatePayloadsList(updatedList);
9796

@@ -101,11 +100,11 @@ export async function* search(
101100
};
102101
yield {
103102
status: "RELOAD" as const,
104-
...updatedList
103+
cache: updatedList
105104
};
106105

107106
cache.startFromZero = false;
108107

109-
logger.info("[ws|search](data sent to client|cache: updated)");
108+
logger.info("[ws|command.search](data sent to client|cache: updated)");
110109
}
111110
}

0 commit comments

Comments
 (0)