Skip to content

Commit 6adef05

Browse files
committed
feat: 🎸 gzip and cache /schema output
1 parent 81d3b9b commit 6adef05

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/server/http1/RpcServer.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {ConnectionContext} from '../types';
1717
import {ObjectValueCaller} from '../../common/rpc/caller/ObjectValueCaller';
1818
import {ObjectValue} from 'json-joy/lib/json-type-value/ObjectValue';
1919
import {ObjectType} from 'json-joy/lib/json-type/type/classes';
20+
import {gzip} from '@jsonjoy.com/util/lib/compression/gzip';
2021

2122
const DEFAULT_MAX_PAYLOAD = 4 * 1024 * 1024;
2223

@@ -207,7 +208,7 @@ export class RpcServer implements Printable {
207208
*/
208209
public enableSchema(path: string = '/schema', method: string = 'GET'): void {
209210
const caller = this.opts.caller;
210-
let responseBody = Buffer.from('{}');
211+
let responseBody: Uint8Array = Buffer.from('{}');
211212
if (caller instanceof ObjectValueCaller) {
212213
const api = caller.router as ObjectValue<ObjectType<any>>;
213214
const schema = {
@@ -216,15 +217,20 @@ export class RpcServer implements Printable {
216217
};
217218
responseBody = Buffer.from(JSON.stringify(schema));
218219
}
220+
let responseBodyCompressed: Uint8Array = new Uint8Array(0);
221+
gzip(responseBody).then((compressed) => responseBodyCompressed = compressed);
219222
this.http1.route({
220223
method,
221224
path,
222225
handler: (ctx) => {
223226
const res = ctx.res;
224227
res.writeHead(200, 'OK', {
225228
'Content-Type': 'application/json',
229+
'Content-Encoding': 'gzip',
230+
'Cache-Control': 'public, max-age=3600, immutable',
231+
'Content-Length': responseBodyCompressed.length,
226232
});
227-
res.end(responseBody);
233+
res.end(responseBodyCompressed);
228234
},
229235
});
230236
}

0 commit comments

Comments
 (0)