Skip to content

Commit

Permalink
Add gzip and brotli compression in kitchen sink
Browse files Browse the repository at this point in the history
  • Loading branch information
wajeshubham committed Jul 9, 2023
1 parent ed5d15c commit e3bc731
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-compression": "^1.0.2",
"express-rate-limit": "^6.7.0",
"express-session": "^1.17.3",
"express-validator": "^7.0.1",
Expand Down
34 changes: 34 additions & 0 deletions src/controllers/kitchen-sink/responseinspection.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,43 @@ const sendXMLData = asyncHandler(async (req, res) => {
});
});

const sendGzipResponse = asyncHandler(async (req, res) => {
// This controller is guarded by compression middleware which compresses the response
const animal = "elephant";
// It will repeatedly send the word 'elephant' in a
// 'text/html' format file
res.status(200).send(
new ApiResponse(
200,
{
contentEncoding: "gzip",
string: animal.repeat(1000),
},
"Response compressed with gzip"
)
);
});

const sendBrotliResponse = asyncHandler(async (req, res) => {
// This controller is guarded by compression middleware which compresses the response in `br` encoding
const animal = "elephant";
res.status(200).send(
new ApiResponse(
200,
{
contentEncoding: "br",
string: animal.repeat(1000),
},
"Response compressed with brotli"
)
);
});

export {
getResponseHeaders,
setCacheControlHeader,
sendHTMLTemplate,
sendXMLData,
sendGzipResponse,
sendBrotliResponse,
};
15 changes: 15 additions & 0 deletions src/routes/kitchen-sink/responseinspection.routes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Router } from "express";
import {
getResponseHeaders,
sendBrotliResponse,
sendGzipResponse,
sendHTMLTemplate,
sendXMLData,
setCacheControlHeader,
} from "../../controllers/kitchen-sink/responseinspection.controllers.js";
import { setCacheControlHeaderValidator } from "../../validators/kitchen-sink/responseinspection.validators.js";
import { validate } from "../../validators/validate.js";
import compression from "express-compression";

const router = Router();

Expand All @@ -17,4 +20,16 @@ router.route("/headers").get(getResponseHeaders);
router.route("/html").get(sendHTMLTemplate);
router.route("/xml").get(sendXMLData);

router.use(compression()).route("/gzip").get(sendGzipResponse);

router
.use(
compression({
brotli: {
enabled: true,
zlib: {},
},
})
)
.get("/brotli", sendBrotliResponse);
export default router;
25 changes: 22 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ abbrev@1:
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
integrity sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==

accepts@~1.3.8:
accepts@~1.3.5, accepts@~1.3.8:
version "1.3.8"
resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.8.tgz#0bf0be125b67014adcb0b0921e62db7bffe16b2e"
integrity sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==
Expand Down Expand Up @@ -290,7 +290,7 @@ busboy@^1.0.0:
dependencies:
streamsearch "^1.1.0"

[email protected]:
[email protected], bytes@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.2.tgz#8b0beeb98605adf1b128fa4386403c009e0221a5"
integrity sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==
Expand Down Expand Up @@ -389,6 +389,13 @@ commander@^6.1.0:
resolved "https://registry.yarnpkg.com/commander/-/commander-6.2.1.tgz#0792eb682dfbc325999bb2b84fddddba110ac73c"
integrity sha512-U7VdrJFnJgo4xjrHpTzu0yrHPGImdsmD95ZlgYSEajAn2JKzDhDTPG9kBTefmObL2w/ngeZnilk+OV9CG3d7UA==

compressible@~2.0.16:
version "2.0.18"
resolved "https://registry.yarnpkg.com/compressible/-/compressible-2.0.18.tgz#af53cca6b070d4c3c0750fbd77286a6d7cc46fba"
integrity sha512-AF3r7P5dWxL8MxyITRMlORQNaOA2IkAFaTr4k7BUumjPtRpGDTZpl0Pb1XCO6JeDCBdp126Cgs9sMxqSjgYyRg==
dependencies:
mime-db ">= 1.43.0 < 2"

[email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
Expand Down Expand Up @@ -635,6 +642,18 @@ etag@~1.8.1:
resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887"
integrity sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==

express-compression@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/express-compression/-/express-compression-1.0.2.tgz#e24c452502c77fb6a90f84bf7820f90fde8c2580"
integrity sha512-kZ2xaL99J4bpnfLLcFIyhkkMMXPxYwFPNzbJ1tYxip1J49IrBWSdP9oSyFq7AHSW1wfaqFhd7N+C92bU8XA9fw==
dependencies:
accepts "~1.3.5"
bytes "^3.1.2"
compressible "~2.0.16"
debug "2.6.9"
on-headers "~1.0.2"
vary "~1.1.2"

express-rate-limit@^6.7.0:
version "6.7.0"
resolved "https://registry.yarnpkg.com/express-rate-limit/-/express-rate-limit-6.7.0.tgz#6aa8a1bd63dfe79702267b3af1161a93afc1d3c2"
Expand Down Expand Up @@ -1170,7 +1189,7 @@ methods@~1.1.2:
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
integrity sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==

[email protected]:
[email protected], "mime-db@>= 1.43.0 < 2":
version "1.52.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==
Expand Down

0 comments on commit e3bc731

Please sign in to comment.