-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathadmin.api.v1.gc.ts
More file actions
48 lines (36 loc) · 1.15 KB
/
admin.api.v1.gc.ts
File metadata and controls
48 lines (36 loc) · 1.15 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
import { type DataFunctionArgs } from "@remix-run/node";
import { PerformanceObserver } from "node:perf_hooks";
import { runInNewContext } from "node:vm";
import v8 from "v8";
import { requireAdminApiRequest } 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) {
await requireAdminApiRequest(request);
const entry = await waitTillGcFinishes();
return new Response(JSON.stringify(entry), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
}