-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathdata.ts
More file actions
60 lines (48 loc) · 1.72 KB
/
data.ts
File metadata and controls
60 lines (48 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Import Node.js Dependencies
import fs from "node:fs";
import path from "node:path";
// Import Third-party Dependencies
import send from "@polka/send-type";
import type { Request, Response } from "express-serve-static-core";
// Import Internal Dependencies
import { context } from "../ALS.ts";
import { logger } from "../logger.ts";
import { cache } from "../cache.ts";
// CONSTANTS
const kDefaultPayloadPath = path.join(process.cwd(), "nsecure-result.json");
export async function get(_req: Request, res: Response) {
if (cache.startFromZero) {
logger.info("[data|get](no content)");
send(res, 204);
return;
}
try {
const { current, mru } = await cache.payloadsList();
logger.info(`[data|get](current: ${current})`);
logger.debug(`[data|get](lru: ${mru})`);
send(res, 200, cache.getPayload(current));
}
catch {
logger.error("[data|get](No cache yet. Creating one...)");
const { dataFilePath } = context.getStore()!;
const payloadPath = dataFilePath || kDefaultPayloadPath;
const payload = JSON.parse(fs.readFileSync(payloadPath, "utf-8"));
const { name, version } = payload.rootDependency;
const formatted = `${name}@${version}${payload.local ? "#local" : ""}`;
const payloadsList = {
mru: [formatted],
current: formatted,
lru: [],
availables: cache.availablePayloads().filter((pkg) => pkg !== formatted),
lastUsed: {
[formatted]: Date.now()
},
root: formatted
};
logger.info(`[data|get](dep: ${formatted})`);
await cache.updatePayloadsList(payloadsList);
cache.updatePayload(formatted, payload);
logger.info(`[data|get](cache: created|payloadsList: ${payloadsList.lru})`);
send(res, 200, payload);
}
}