-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import fs from "node:fs" | ||
import os from "node:os" | ||
import path from "node:path" | ||
import v8 from "node:v8" | ||
import { requireToken } from "api/utils/auth" | ||
|
||
export async function GET(request: Request) { | ||
requireToken(request) | ||
|
||
const tempDir = os.tmpdir() | ||
const filepath = path.join( | ||
tempDir, | ||
`mskelton.dev-${new Date().toISOString()}.heapsnapshot`, | ||
) | ||
|
||
const snapshotPath = v8.writeHeapSnapshot(filepath) | ||
if (!snapshotPath) { | ||
throw new Response("No snapshot saved", { status: 500 }) | ||
} | ||
|
||
const body = new ReadableStream({ | ||
start(controller) { | ||
const stream = fs.createReadStream(snapshotPath) | ||
stream.on("data", (chunk) => controller.enqueue(chunk)) | ||
stream.on("end", () => controller.close()) | ||
stream.on("error", (err) => controller.error(err)) | ||
}, | ||
}) | ||
|
||
return new Response(body, { | ||
headers: { | ||
"Content-Disposition": `attachment; filename="${path.basename( | ||
snapshotPath, | ||
)}"`, | ||
"Content-Length": (await fs.promises.stat(snapshotPath)).size.toString(), | ||
"Content-Type": "application/octet-stream", | ||
}, | ||
status: 200, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { NextResponse } from "next/server" | ||
|
||
export function requireToken(request: Request) { | ||
const token = request.headers.get("Authorization")?.replace("Bearer ", "") | ||
|
||
if ( | ||
process.env.NODE_ENV === "production" && | ||
process.env.API_AUTH_TOKEN !== token | ||
) { | ||
return NextResponse.json({ message: "Forbidden" }, { status: 403 }) | ||
} | ||
} |