-
Notifications
You must be signed in to change notification settings - Fork 21
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
1 parent
b373ac4
commit 05f3cbf
Showing
3 changed files
with
58 additions
and
8 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
41 changes: 41 additions & 0 deletions
41
health-services/project-factory/src/server/utils/gzipHandler.ts
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,41 @@ | ||
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"); | ||
} | ||
}); | ||
}; |
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