-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added gzip handler in middleware (#1290)
* added gzip handler in middleware * added gzip handler specific to request middleware * refactored gzip handler
- Loading branch information
1 parent
b373ac4
commit b69ba14
Showing
3 changed files
with
48 additions
and
7 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
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
import { Request } from "express"; | ||
import * as zlib from "zlib"; | ||
|
||
export const handleGzipRequest = async (req: Request): Promise<void> => { | ||
const buffers: Buffer[] = []; | ||
|
||
// Collect data chunks from the request | ||
await new Promise<void>((resolve, reject) => { | ||
req.on("data", (chunk: any) => buffers.push(chunk)); | ||
req.on("end", resolve); | ||
req.on("error", reject); | ||
}); | ||
|
||
// Concatenate and decompress the data | ||
const gzipBuffer = Buffer.concat(buffers); | ||
try { | ||
const decompressedData = await decompressGzip(gzipBuffer); | ||
req.body = decompressedData; // Assign the parsed data to req.body | ||
} catch (err: any) { | ||
throw new Error(`Failed to process Gzip data: ${err.message}`); | ||
} | ||
}; | ||
|
||
// Helper function to decompress Gzip data | ||
const decompressGzip = (gzipBuffer: Buffer): Promise<any> => { | ||
return new Promise((resolve, reject) => { | ||
zlib.gunzip(gzipBuffer, (err, result) => { | ||
if (err) return reject(err); | ||
try { | ||
resolve(JSON.parse(result.toString())); | ||
} catch (parseErr) { | ||
reject(new Error("Invalid JSON format in decompressed data")); | ||
} | ||
}); | ||
}); | ||
}; |
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