import auth from "basic-auth";
import fs from "fs";
import v8 from "v8";
import { isAdminPasswordValid } from "../config/zero-config.js";
function handleHeapzRequest(lc, config, req, res) {
  const credentials = auth(req);
  if (!isAdminPasswordValid(lc, config, credentials?.pass)) {
    void res.code(401).header("WWW-Authenticate", 'Basic realm="Heapz Protected Area"').send("Unauthorized");
    return;
  }
  const filename = v8.writeHeapSnapshot();
  const stream = fs.createReadStream(filename);
  void res.header("Content-Type", "application/octet-stream").header("Content-Disposition", `attachment; filename=${filename}`).send(stream);
  stream.on("end", () => {
    fs.unlink(filename, (err) => {
      if (err) {
        lc.error?.("Error deleting heap snapshot:", err);
      }
    });
  });
}
export {
  handleHeapzRequest
};
//# sourceMappingURL=heapz.js.map
