AppView in a box as a Vite plugin thing hatk.dev
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

add gzip compression for JSON API responses

Compresses JSON responses > 1KB when the client supports gzip.
Uses a shared sendJson helper so jsonResponse/jsonError signatures
stay unchanged.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

+23 -4
+23 -4
packages/hatk/src/server.ts
··· 1 1 import { createServer, type Server, type IncomingMessage } from 'node:http' 2 + import { gzipSync } from 'node:zlib' 2 3 import { existsSync } from 'node:fs' 3 4 import { readFile } from 'node:fs/promises' 4 5 import { join, extname } from 'node:path' ··· 1046 1047 return server 1047 1048 } 1048 1049 1050 + function sendJson(res: any, status: number, body: Buffer): void { 1051 + const acceptEncoding = res.req?.headers['accept-encoding'] || '' 1052 + if (body.length > 1024 && /\bgzip\b/.test(acceptEncoding as string)) { 1053 + const compressed = gzipSync(body) 1054 + res.writeHead(status, { 1055 + 'Content-Type': 'application/json', 1056 + 'Content-Encoding': 'gzip', 1057 + 'Vary': 'Accept-Encoding', 1058 + ...(status === 200 ? { 'Cache-Control': 'no-store' } : {}), 1059 + }) 1060 + res.end(compressed) 1061 + } else { 1062 + res.writeHead(status, { 1063 + 'Content-Type': 'application/json', 1064 + ...(status === 200 ? { 'Cache-Control': 'no-store' } : {}), 1065 + }) 1066 + res.end(body) 1067 + } 1068 + } 1069 + 1049 1070 function jsonResponse(res: any, data: any): void { 1050 - res.writeHead(200, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' }) 1051 - res.end(JSON.stringify(data, (_, v) => normalizeValue(v))) 1071 + sendJson(res, 200, Buffer.from(JSON.stringify(data, (_, v) => normalizeValue(v)))) 1052 1072 } 1053 1073 1054 1074 function jsonError(res: any, status: number, message: string): void { 1055 1075 if (res.headersSent) return 1056 - res.writeHead(status, { 'Content-Type': 'application/json' }) 1057 - res.end(JSON.stringify({ error: message })) 1076 + sendJson(res, status, Buffer.from(JSON.stringify({ error: message }))) 1058 1077 } 1059 1078 1060 1079 /** Proxy a request to the user's PDS with DPoP + automatic nonce retry + token refresh. */