From fbcc82b08bb264e043982ace063b9efc8b5f59ac Mon Sep 17 00:00:00 2001 From: Lam Ngoc Khuong Date: Fri, 8 Nov 2024 20:59:03 +0700 Subject: [PATCH] feat: add fastify compression --- apps/admin-api/package.json | 1 + apps/admin-api/src/main.ts | 4 ++ pnpm-lock.yaml | 94 +++++++++++++++++++++++++++++++++---- 3 files changed, 91 insertions(+), 8 deletions(-) diff --git a/apps/admin-api/package.json b/apps/admin-api/package.json index ec5bbf9..fc56439 100644 --- a/apps/admin-api/package.json +++ b/apps/admin-api/package.json @@ -22,6 +22,7 @@ "test:e2e": "jest --config ./test/jest-e2e.json" }, "dependencies": { + "@fastify/compress": "^7.0.3", "@fastify/helmet": "^11.1.1", "@fastify/static": "^7.0.4", "@nestjs/common": "^10.4.6", diff --git a/apps/admin-api/src/main.ts b/apps/admin-api/src/main.ts index fa43270..1686c8f 100644 --- a/apps/admin-api/src/main.ts +++ b/apps/admin-api/src/main.ts @@ -1,3 +1,4 @@ +import compression from '@fastify/compress'; import helmet from '@fastify/helmet'; import { ClassSerializerInterceptor, @@ -53,6 +54,9 @@ async function bootstrap() { // Setup security headers app.register(helmet); + // For high-traffic websites in production, it is strongly recommended to offload compression from the application server - typically in a reverse proxy (e.g., Nginx). In that case, you should not use compression middleware. + app.register(compression); + const configService = app.get(ConfigService); const reflector = app.get(Reflector); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1287e71..1b2e1c7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -29,6 +29,9 @@ importers: apps/admin-api: dependencies: + '@fastify/compress': + specifier: ^7.0.3 + version: 7.0.3 '@fastify/helmet': specifier: ^11.1.1 version: 11.1.1 @@ -893,6 +896,9 @@ packages: '@fastify/ajv-compiler@3.6.0': resolution: {integrity: sha512-LwdXQJjmMD+GwLOkP7TVC68qa+pSSogeWWmznRJ/coyTcfe9qA05AHFSe1eZFwK6q+xVRpChnvFUkf1iYaSZsQ==} + '@fastify/compress@7.0.3': + resolution: {integrity: sha512-xa9fo5/DgK1s0bkS6xrYgNn8HmofO5tJvbCDk8QuXshSgLd2cFZANv1ox/Qv7zswS7JroHwTlCVv/XGTVO98tg==} + '@fastify/cors@9.0.1': resolution: {integrity: sha512-YY9Ho3ovI+QHIL2hW+9X4XqQjXLjJqsU+sMV/xFsxZkE8p3GNnYVFpoOxF7SsP5ZL76gwvbo3V9L+FIekBGU4Q==} @@ -2786,6 +2792,12 @@ packages: resolution: {integrity: sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==} engines: {node: '>=12'} + duplexify@3.7.1: + resolution: {integrity: sha512-07z8uv2wMyS51kKhD1KsdXJg5WQ6t93RneqRxUHnskXVtlYYkLqM0gqStQZ3pj073g687jPCHrqNfCzawLYh5g==} + + duplexify@4.1.3: + resolution: {integrity: sha512-M3BmBhwJRZsSx38lZyhE53Csddgzl5R7xGJNk7CVddZD6CcmwMCH8J+7AprIrQKH7TonKxaCjcv27Qmf+sQ+oA==} + eastasianwidth@0.2.0: resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==} @@ -2836,6 +2848,9 @@ packages: resolution: {integrity: sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg==} engines: {node: '>= 0.8'} + end-of-stream@1.4.4: + resolution: {integrity: sha512-+uw1inIHVPQoaVuHzRyXd21icM+cnt4CzD5rW+NC1wjOUSTOs+Te7FOv7AhN7vS9x/oIyhLP5PR1H+phQAHu5Q==} + enhanced-resolve@5.17.1: resolution: {integrity: sha512-LMHl3dXhTcfv8gM4kEzIUeTQ+7fpdA0l2tUf34BddXPkz2A5xJ5L/Pchd5BL6rdccM9QGvu0sWZzK1Z1t4wwyg==} engines: {node: '>=10.13.0'} @@ -5025,6 +5040,9 @@ packages: resolution: {integrity: sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==} engines: {node: '>=8'} + peek-stream@1.1.3: + resolution: {integrity: sha512-FhJ+YbOSBb9/rIl2ZeE/QHEsWn7PqNYt8ARAY3kIgNGOk13g9FGyIY6JIl/xB/3TFRVoTv5as0l11weORrTekA==} + periscopic@3.1.0: resolution: {integrity: sha512-vKiQ8RRtkl9P+r/+oefh25C3fhybptkHKCZSPlcXiJux2tJF55GnEj3BVn4A5gKfq9NWWXXrxkHBwVPUfH0opw==} @@ -5202,6 +5220,12 @@ packages: pseudomap@1.0.2: resolution: {integrity: sha512-b/YwNhb8lk1Zz2+bXXpS/LK9OisiZZ1SNsSLxN1x2OXVEhW2Ckr/7mWE5vrC1ZTiJlD9g19jWszTmJsB+oEpFQ==} + pump@3.0.2: + resolution: {integrity: sha512-tUPXtzlGM8FE3P0ZL6DVs/3P58k9nk8/jZeQCurTJylQA8qFYzHFfhBJkuqyE0FifOsQ0uKWekiZ5g8wtr28cw==} + + pumpify@2.0.1: + resolution: {integrity: sha512-m7KOje7jZxrmutanlkS1daj1dS6z6BgslzOXmcSEpIlCxM3VJH7lG5QLeck/6hgF6F4crFf01UtQmNsJfweTAw==} + punycode@2.3.1: resolution: {integrity: sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==} engines: {node: '>=6'} @@ -5659,6 +5683,9 @@ packages: std-env@3.7.0: resolution: {integrity: sha512-JPbdCEQLj1w5GilpiHAx3qJvFndqybBysA3qUOnznweH4QbNYUsW/ea8QzSrnh0vNsezMMw5bcVool8lM0gwzg==} + stream-shift@1.0.3: + resolution: {integrity: sha512-76ORR0DO1o1hlKwTbi/DM3EXWGf3ZJYO8cXX5RJwnul2DEg2oyoZyjLNoQM8WsvZiFKCRfC1O0J7iCvie3RZmQ==} + streamsearch@1.1.0: resolution: {integrity: sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==} engines: {node: '>=10.0.0'} @@ -5865,6 +5892,9 @@ packages: thread-stream@3.1.0: resolution: {integrity: sha512-OqyPZ9u96VohAyMfJykzmivOrY2wfMSf3C5TtFJVgN+Hm6aj+voFhlK+kZEIv2FBh1X6Xp3DlnCOfEQ3B2J86A==} + through2@2.0.5: + resolution: {integrity: sha512-/mrRod8xqpA+IHSLyGCQ2s8SPHiCDEeQJSep1jqLYeEUClOFG2Qsh+4FU6G9VeqpZnGW/Su8LQGc4YKni5rYSQ==} + through@2.3.8: resolution: {integrity: sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==} @@ -7038,6 +7068,17 @@ snapshots: ajv-formats: 2.1.1(ajv@8.12.0) fast-uri: 2.4.0 + '@fastify/compress@7.0.3': + dependencies: + '@fastify/accept-negotiator': 1.1.0 + fastify-plugin: 4.5.1 + mime-db: 1.52.0 + minipass: 7.1.2 + peek-stream: 1.1.3 + pump: 3.0.2 + pumpify: 2.0.1 + readable-stream: 4.5.2 + '@fastify/cors@9.0.1': dependencies: fastify-plugin: 4.5.1 @@ -9290,6 +9331,20 @@ snapshots: dotenv@16.4.5: {} + duplexify@3.7.1: + dependencies: + end-of-stream: 1.4.4 + inherits: 2.0.4 + readable-stream: 2.3.8 + stream-shift: 1.0.3 + + duplexify@4.1.3: + dependencies: + end-of-stream: 1.4.4 + inherits: 2.0.4 + readable-stream: 3.6.2 + stream-shift: 1.0.3 + eastasianwidth@0.2.0: {} ebec@1.1.1: @@ -9329,6 +9384,10 @@ snapshots: encodeurl@2.0.0: optional: true + end-of-stream@1.4.4: + dependencies: + once: 1.4.0 + enhanced-resolve@5.17.1: dependencies: graceful-fs: 4.2.11 @@ -10771,8 +10830,7 @@ snapshots: call-bind: 1.0.7 get-intrinsic: 1.2.4 - isarray@1.0.0: - optional: true + isarray@1.0.0: {} isarray@2.0.5: {} @@ -12421,6 +12479,12 @@ snapshots: path-type@4.0.0: {} + peek-stream@1.1.3: + dependencies: + buffer-from: 1.1.2 + duplexify: 3.7.1 + through2: 2.0.5 + periscopic@3.1.0: dependencies: '@types/estree': 1.0.6 @@ -12545,8 +12609,7 @@ snapshots: ansi-styles: 5.2.0 react-is: 18.3.1 - process-nextick-args@2.0.1: - optional: true + process-nextick-args@2.0.1: {} process-warning@3.0.0: {} @@ -12591,6 +12654,17 @@ snapshots: pseudomap@1.0.2: {} + pump@3.0.2: + dependencies: + end-of-stream: 1.4.4 + once: 1.4.0 + + pumpify@2.0.1: + dependencies: + duplexify: 4.1.3 + inherits: 2.0.4 + pump: 3.0.2 + punycode@2.3.1: {} pure-rand@6.1.0: {} @@ -12666,7 +12740,6 @@ snapshots: safe-buffer: 5.1.2 string_decoder: 1.1.1 util-deprecate: 1.0.2 - optional: true readable-stream@3.6.2: dependencies: @@ -12882,8 +12955,7 @@ snapshots: has-symbols: 1.0.3 isarray: 2.0.5 - safe-buffer@5.1.2: - optional: true + safe-buffer@5.1.2: {} safe-buffer@5.2.1: {} @@ -13125,6 +13197,8 @@ snapshots: std-env@3.7.0: {} + stream-shift@1.0.3: {} + streamsearch@1.1.0: {} string-argv@0.3.2: {} @@ -13200,7 +13274,6 @@ snapshots: string_decoder@1.1.1: dependencies: safe-buffer: 5.1.2 - optional: true string_decoder@1.3.0: dependencies: @@ -13346,6 +13419,11 @@ snapshots: dependencies: real-require: 0.2.0 + through2@2.0.5: + dependencies: + readable-stream: 2.3.8 + xtend: 4.0.2 + through@2.3.8: {} tinycolor2@1.6.0: {}