add gzip support

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-07-23 22:52:03 +02:00
parent c7e6f1d470
commit d4bbad6eb9
3 changed files with 102 additions and 1 deletions

View File

@@ -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, {