add gzip support
This commit is contained in:
@@ -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, {
|
||||
|
||||
Reference in New Issue
Block a user