agentleFS
Workshop-VenturesWorkshop-Ventures/skills/backend-compression/SKILL.md

backend-compression

Skill1 starsChanged 10 months agoMITView on GitHub

Add response compression (gzip/deflate) to the backend for improved performance. Use when asked to "add compression", "enable gzip", or "optimize response size".

  • Installs packages

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 3 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Only compress text-based content types:

    line 51
  2. Distinctive:

    Add compression early in the middleware chain, but after logging:

    line 92
  3. Distinctive:

    Verify compression is working:

    line 113

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Backend Response Compression

This skill adds response compression to a Koa backend using koa-compress.

Overview

Response compression reduces payload size for text-based content (JSON, HTML, JS, CSS), improving network performance especially for mobile clients.

Installation

npm install koa-compress

Implementation

Add to apps/backend/src/main.ts:

import compress from 'koa-compress';
import { constants as zlibConstants } from 'zlib';

// Add compression middleware (before routes)
app.use(
  compress({
    filter(content_type) {
      // Compress JSON, text, and other compressible types
      return /text|json|javascript|xml|svg/i.test(content_type);
    },
    threshold: 1024, // Only compress responses larger than 1KB
    gzip: {
      flush: zlibConstants.Z_SYNC_FLUSH,
    },
    deflate: {
      flush: zlibConstants.Z_SYNC_FLUSH,
    },
    br: false, // Disable brotli (CPU intensive, gzip is sufficient)
  })
);

Configuration Options

Filter

Only compress text-based content types:

filter(content_type) {
  return /text|json|javascript|xml|svg/i.test(content_type);
}

Why? Binary content (images, videos, PDFs) is already compressed. Attempting to compress them wastes CPU and can actually increase size.

Threshold

threshold: 1024, // 1KB minimum

Why? Small responses have minimal benefit from compression, but still incur CPU cost. 1KB is a good balance.

Brotli

br: false,

Why? Brotli provides better compression but is significantly more CPU intensive. For most APIs, gzip provides sufficient compression with lower server load. Enable brotli only if:

  • You have CPU headroom
  • Your responses are large and benefit from higher compression ratios
  • Your clients primarily support brotli

Flush Mode

gzip: {
  flush: zlibConstants.Z_SYNC_FLUSH,
},

Why? Z_SYNC_FLUSH ensures data is flushed immediately, important for streaming responses and keeping memory usage predictable.

Middleware Order

Add compression early in the middleware chain, but after logging:

// 1. Error handling
app.use(errorHandler);

// 2. Logging
app.use(requestLogger);

// 3. Compression (before routes)
app.use(compress({ ... }));

// 4. Body parsing
app.use(bodyParser());

// 5. Routes
app.use(mount('/api/resources', resourceRoutes.routes()));

Testing Compression

Verify compression is working:

# Request with gzip support
curl -H "Accept-Encoding: gzip" -I http://localhost:3000/api/resources

# Should see: Content-Encoding: gzip

Checklist

  1. Install koa-compress: npm install koa-compress
  2. Add middleware to main.ts before routes
  3. Verify with curl that Content-Encoding: gzip header is present

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.