-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathadmin.api.v1.gc.ts
More file actions
63 lines (48 loc) · 1.61 KB
/
Copy pathadmin.api.v1.gc.ts
File metadata and controls
63 lines (48 loc) · 1.61 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
61
62
63
import { type DataFunctionArgs } from "@remix-run/node";
import { PerformanceObserver } from "node:perf_hooks";
import { runInNewContext } from "node:vm";
import v8 from "v8";
import { prisma } from "~/db.server";
import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server";
async function waitTillGcFinishes() {
let resolver: (value: PerformanceEntry) => void;
let rejector: (reason?: any) => void;
const promise = new Promise<PerformanceEntry>((resolve, reject) => {
resolver = resolve;
rejector = reject;
});
const obs = new PerformanceObserver((list) => {
const entry = list.getEntries()[0];
if (entry.name === "gc") {
resolver(entry);
}
});
obs.observe({ entryTypes: ["gc"] });
v8.setFlagsFromString("--expose-gc");
const gc = global.gc ?? runInNewContext("gc");
gc();
// disable expose-gc
v8.setFlagsFromString("--noexpose-gc");
return promise;
}
export async function loader({ request }: DataFunctionArgs) {
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request);
if (!authenticationResult) {
throw new Response("You must be an admin to perform this action", { status: 403 });
}
const user = await prisma.user.findFirst({
where: {
id: authenticationResult.userId,
},
});
if (!user?.admin) {
throw new Response("You must be an admin to perform this action", { status: 403 });
}
const entry = await waitTillGcFinishes();
return new Response(JSON.stringify(entry), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
}