({ request }: DataFunctionArgs)
| 24 | } |
| 25 | |
| 26 | export async function loader({ request }: DataFunctionArgs) { |
| 27 | const user = await requireUser(request); |
| 28 | |
| 29 | if (!user.admin) { |
| 30 | throw new Response("You must be an admin to perform this action", { status: 403 }); |
| 31 | } |
| 32 | |
| 33 | const tempDir = os.tmpdir(); |
| 34 | const filepath = path.join( |
| 35 | tempDir, |
| 36 | `${getTaskIdentifier()}-${formatDate(new Date())}.heapsnapshot` |
| 37 | ); |
| 38 | |
| 39 | const snapshotPath = v8.writeHeapSnapshot(filepath); |
| 40 | if (!snapshotPath) { |
| 41 | throw new Response("No snapshot saved", { status: 500 }); |
| 42 | } |
| 43 | |
| 44 | const body = new PassThrough(); |
| 45 | const stream = fs.createReadStream(snapshotPath); |
| 46 | stream.on("open", () => stream.pipe(body)); |
| 47 | stream.on("error", (err) => body.end(err)); |
| 48 | stream.on("end", () => body.end()); |
| 49 | |
| 50 | return new Response(body as any, { |
| 51 | status: 200, |
| 52 | headers: { |
| 53 | "Content-Type": "application/octet-stream", |
| 54 | "Content-Disposition": `attachment; filename="${path.basename(snapshotPath)}"`, |
| 55 | "Content-Length": (await fs.promises.stat(snapshotPath)).size.toString(), |
| 56 | }, |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | function getTaskIdentifier() { |
| 61 | if (!process.env.ECS_CONTAINER_METADATA_URI) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…