Skip to content

Commit

Permalink
comment
Browse files Browse the repository at this point in the history
  • Loading branch information
nitish-egov committed Dec 18, 2024
1 parent b373ac4 commit 443bcf5
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions health-services/project-factory/src/server/utils/gzipHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import zlib from "zlib";
import { Request, Response, NextFunction } from "express";
import { logger } from "./logger";

export const handleGzipRequest = (
req: Request,
res: Response,
next: NextFunction
) => {
const buffers: Buffer[] = [];

req.on("data", (chunk) => buffers.push(chunk));
req.on("end", () => {
try {
const gzipBuffer = Buffer.concat(buffers);

// Decompress the Gzip data
zlib.gunzip(gzipBuffer, (err, decompressedBuffer) => {
if (err) {
logger.error("Error decompressing Gzip file:", err);
res.status(500).send("Invalid Gzip file");
return;
}

try {
// Convert the decompressed buffer to string and parse as JSON
const jsonData = decompressedBuffer.toString();
const parsedData = JSON.parse(jsonData);
req.body = parsedData; // Attach parsed data to the request body
next(); // Proceed to next middleware
} catch (parseError) {
logger.error("Error parsing JSON data:", parseError);
res.status(500).send("Invalid JSON in Gzip content");
}
});
} catch (err) {
logger.error("Error processing Gzip content:", err);
res.status(500).send("Invalid Gzip content");
}
});
};

// nitishhhhhhhhhhhhhhhh

0 comments on commit 443bcf5

Please sign in to comment.