buddy-workbench 0.1.35 → 0.1.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "buddy-workbench",
3
- "version": "0.1.35",
3
+ "version": "0.1.37",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "bin": {
@@ -0,0 +1,43 @@
1
+ import { gzipSync } from 'node:zlib';
2
+
3
+ const COMPRESSIBLE_TYPES = /^(text\/|application\/(javascript|json|xml|wasm)|image\/svg\+xml)/i;
4
+ const MIN_SIZE = 1024;
5
+
6
+ // Express static sends the bundled assets in one response. Buffering only those
7
+ // responses lets us compress them without adding another runtime dependency.
8
+ export function compression(req, res, next) {
9
+ const isUiAsset = req.path === '/' || req.path === '/index.html' || req.path.startsWith('/assets/');
10
+ if (!isUiAsset) return next();
11
+
12
+ const chunks = [];
13
+ const originalEnd = res.end;
14
+
15
+ res.write = function write(chunk, encoding) {
16
+ if (chunk) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding));
17
+ return true;
18
+ };
19
+
20
+ res.end = function end(chunk, encoding) {
21
+ if (chunk) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding));
22
+ const body = Buffer.concat(chunks);
23
+ const contentType = String(res.getHeader('content-type') || '');
24
+ const acceptsGzip = /\bgzip\b/i.test(req.headers['accept-encoding'] || '');
25
+ const shouldCompress = acceptsGzip
26
+ && body.length >= MIN_SIZE
27
+ && COMPRESSIBLE_TYPES.test(contentType)
28
+ && !res.getHeader('content-encoding')
29
+ && req.method !== 'HEAD';
30
+
31
+ if (shouldCompress) {
32
+ const compressed = gzipSync(body, { level: 6 });
33
+ res.removeHeader('content-length');
34
+ res.setHeader('Content-Encoding', 'gzip');
35
+ res.setHeader('Vary', 'Accept-Encoding');
36
+ return originalEnd.call(res, compressed);
37
+ }
38
+
39
+ return originalEnd.call(res, body);
40
+ };
41
+
42
+ next();
43
+ }
package/server.js CHANGED
@@ -26,10 +26,12 @@ import dataBackupRoutes from './server/routes/data-backup.js';
26
26
  import presentationsRoutes from './server/routes/presentations.js';
27
27
  import { addErrorRecord } from './server/repositories/errors.js';
28
28
  import { startClipboardCapture } from './server/services/clipboard-history.js';
29
+ import { compression } from './server/middleware/compression.js';
29
30
 
30
31
  const port = Number(process.env.PORT || 3100);
31
32
  const app = express();
32
33
  app.use(express.json());
34
+ app.use(compression);
33
35
 
34
36
  // Intercept error responses (HTTP status >= 400) from any API route
35
37
  app.use((req, res, next) => {