add gzip support
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
"dependencies": {
|
||||
"@baselime/pino-transport": "^0.1.5",
|
||||
"@clerk/fastify": "^1.0.0",
|
||||
"@fastify/compress": "^7.0.3",
|
||||
"@fastify/cookie": "^9.3.1",
|
||||
"@fastify/cors": "^9.0.0",
|
||||
"@fastify/websocket": "^8.3.1",
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import zlib from 'zlib';
|
||||
import { clerkPlugin } from '@clerk/fastify';
|
||||
import compress from '@fastify/compress';
|
||||
import cookie from '@fastify/cookie';
|
||||
import cors from '@fastify/cors';
|
||||
import type { FastifyTRPCPluginOptions } from '@trpc/server/adapters/fastify';
|
||||
@@ -53,6 +55,45 @@ const startServer = async () => {
|
||||
bodyLimit: 1048576 * 500, // 500MB
|
||||
});
|
||||
|
||||
fastify.register(compress, {
|
||||
global: false,
|
||||
encodings: ['gzip', 'deflate'],
|
||||
});
|
||||
|
||||
fastify.addContentTypeParser(
|
||||
'application/json',
|
||||
{ parseAs: 'buffer' },
|
||||
function (req, body, done) {
|
||||
const isGzipped = req.headers['content-encoding'] === 'gzip';
|
||||
|
||||
if (isGzipped) {
|
||||
zlib.gunzip(body, (err, decompressedBody) => {
|
||||
console.log(
|
||||
'decompressedBody',
|
||||
decompressedBody.toString().slice(0, 100)
|
||||
);
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
try {
|
||||
const json = JSON.parse(decompressedBody.toString());
|
||||
done(null, json);
|
||||
} catch (parseError) {
|
||||
done(new Error('Invalid JSON'));
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
const json = JSON.parse(body.toString());
|
||||
done(null, json);
|
||||
} catch (parseError) {
|
||||
done(new Error('Invalid JSON'));
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
await fastify.register(metricsPlugin, { endpoint: '/metrics' });
|
||||
|
||||
fastify.register(cors, {
|
||||
|
||||
61
pnpm-lock.yaml
generated
61
pnpm-lock.yaml
generated
@@ -37,6 +37,9 @@ importers:
|
||||
'@clerk/fastify':
|
||||
specifier: ^1.0.0
|
||||
version: 1.0.0(fastify-plugin@4.5.1)(fastify@4.26.1)(react@18.2.0)
|
||||
'@fastify/compress':
|
||||
specifier: ^7.0.3
|
||||
version: 7.0.3
|
||||
'@fastify/cookie':
|
||||
specifier: ^9.3.1
|
||||
version: 9.3.1
|
||||
@@ -3951,6 +3954,11 @@ packages:
|
||||
js-yaml: 4.1.0
|
||||
dev: false
|
||||
|
||||
/@fastify/accept-negotiator@1.1.0:
|
||||
resolution: {integrity: sha512-OIHZrb2ImZ7XG85HXOONLcJWGosv7sIvM2ifAPQVhg9Lv7qdmMBNVaai4QTdyuaqbKM5eO6sLSQOYI7wEQeCJQ==}
|
||||
engines: {node: '>=14'}
|
||||
dev: false
|
||||
|
||||
/@fastify/ajv-compiler@3.5.0:
|
||||
resolution: {integrity: sha512-ebbEtlI7dxXF5ziNdr05mOY8NnDiPB1XvAlLHctRt/Rc+C3LCOVW5imUVX+mhvUhnNzmPBHewUkOFgGlCxgdAA==}
|
||||
dependencies:
|
||||
@@ -3959,6 +3967,19 @@ packages:
|
||||
fast-uri: 2.3.0
|
||||
dev: false
|
||||
|
||||
/@fastify/compress@7.0.3:
|
||||
resolution: {integrity: sha512-xa9fo5/DgK1s0bkS6xrYgNn8HmofO5tJvbCDk8QuXshSgLd2cFZANv1ox/Qv7zswS7JroHwTlCVv/XGTVO98tg==}
|
||||
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.0
|
||||
pumpify: 2.0.1
|
||||
readable-stream: 4.5.2
|
||||
dev: false
|
||||
|
||||
/@fastify/cookie@9.3.1:
|
||||
resolution: {integrity: sha512-h1NAEhB266+ZbZ0e9qUE6NnNR07i7DnNXWG9VbbZ8uC6O/hxHpl+Zoe5sw1yfdZ2U6XhToUGDnzQtWJdCaPwfg==}
|
||||
dependencies:
|
||||
@@ -10117,6 +10138,24 @@ packages:
|
||||
engines: {node: '>=12'}
|
||||
dev: false
|
||||
|
||||
/duplexify@3.7.1:
|
||||
resolution: {integrity: sha512-07z8uv2wMyS51kKhD1KsdXJg5WQ6t93RneqRxUHnskXVtlYYkLqM0gqStQZ3pj073g687jPCHrqNfCzawLYh5g==}
|
||||
dependencies:
|
||||
end-of-stream: 1.4.4
|
||||
inherits: 2.0.4
|
||||
readable-stream: 2.3.8
|
||||
stream-shift: 1.0.3
|
||||
dev: false
|
||||
|
||||
/duplexify@4.1.3:
|
||||
resolution: {integrity: sha512-M3BmBhwJRZsSx38lZyhE53Csddgzl5R7xGJNk7CVddZD6CcmwMCH8J+7AprIrQKH7TonKxaCjcv27Qmf+sQ+oA==}
|
||||
dependencies:
|
||||
end-of-stream: 1.4.4
|
||||
inherits: 2.0.4
|
||||
readable-stream: 3.6.2
|
||||
stream-shift: 1.0.3
|
||||
dev: false
|
||||
|
||||
/eastasianwidth@0.2.0:
|
||||
resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==}
|
||||
|
||||
@@ -15427,7 +15466,7 @@ packages:
|
||||
engines: {node: '>=16 || 14 >=14.17'}
|
||||
dependencies:
|
||||
lru-cache: 10.2.0
|
||||
minipass: 7.0.4
|
||||
minipass: 7.1.2
|
||||
dev: false
|
||||
|
||||
/path-scurry@1.11.1:
|
||||
@@ -15449,6 +15488,14 @@ packages:
|
||||
resolution: {integrity: sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==}
|
||||
engines: {node: '>=8'}
|
||||
|
||||
/peek-stream@1.1.3:
|
||||
resolution: {integrity: sha512-FhJ+YbOSBb9/rIl2ZeE/QHEsWn7PqNYt8ARAY3kIgNGOk13g9FGyIY6JIl/xB/3TFRVoTv5as0l11weORrTekA==}
|
||||
dependencies:
|
||||
buffer-from: 1.1.2
|
||||
duplexify: 3.7.1
|
||||
through2: 2.0.5
|
||||
dev: false
|
||||
|
||||
/periscopic@3.1.0:
|
||||
resolution: {integrity: sha512-vKiQ8RRtkl9P+r/+oefh25C3fhybptkHKCZSPlcXiJux2tJF55GnEj3BVn4A5gKfq9NWWXXrxkHBwVPUfH0opw==}
|
||||
dependencies:
|
||||
@@ -15887,6 +15934,14 @@ packages:
|
||||
once: 1.4.0
|
||||
dev: false
|
||||
|
||||
/pumpify@2.0.1:
|
||||
resolution: {integrity: sha512-m7KOje7jZxrmutanlkS1daj1dS6z6BgslzOXmcSEpIlCxM3VJH7lG5QLeck/6hgF6F4crFf01UtQmNsJfweTAw==}
|
||||
dependencies:
|
||||
duplexify: 4.1.3
|
||||
inherits: 2.0.4
|
||||
pump: 3.0.0
|
||||
dev: false
|
||||
|
||||
/punycode@2.3.1:
|
||||
resolution: {integrity: sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==}
|
||||
engines: {node: '>=6'}
|
||||
@@ -17403,6 +17458,10 @@ packages:
|
||||
engines: {node: '>= 0.10.0'}
|
||||
dev: false
|
||||
|
||||
/stream-shift@1.0.3:
|
||||
resolution: {integrity: sha512-76ORR0DO1o1hlKwTbi/DM3EXWGf3ZJYO8cXX5RJwnul2DEg2oyoZyjLNoQM8WsvZiFKCRfC1O0J7iCvie3RZmQ==}
|
||||
dev: false
|
||||
|
||||
/streamsearch@1.1.0:
|
||||
resolution: {integrity: sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==}
|
||||
engines: {node: '>=10.0.0'}
|
||||
|
||||
Reference in New Issue
Block a user