Skip to main content
Deno 2 is finally here 🎉️
Learn more

http-content-minify

HTTP message content minification middleware for standard Request and Response.

What

Middleware for HTTP message content(≒body, content) minification.

Declaratively maps media types to transformers.

Middleware

For a definition of Universal HTTP middleware, see the http-middleware project.

Usage

Middleware factory is exported by default.

You map media type to transformer using whatever you like. The map is called a “manifest”.

import contentMinify from "https://deno.land/x/http_content_minify@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

interface Transformer {
  (contents: ArrayBuffer): BodyInit | Promise<BodyInit>;
}
declare const minifyJs: Transformer;
declare const minifyCss: Transformer;
declare const request: Request;

const middleware = contentMinify({
  "text/javascript": minifyJs,
  "text/css": minifyCss,
});
const dirtyBody = `

console.log("hello");

`;
const handler = () =>
  new Response(dirtyBody, {
    headers: { "content-type": "text/javascript;utf=8" },
  });

const response = await middleware(request, handler);

assertEquals(
  await response.text(),
  `console.log("hello");`,
);

Effects

Middleware will effect following:

  • HTTP content

Conditions

For safety, middleware will execute only if the following conditions are met:

  • The body text is readable
  • The Content-Type header exists
  • Media type matches the manifest
  • The Content-Encoding header does not exist
  • The Content-Length header does not exist
  • The ETag header does not exist

Note that middleware will not execute if there are already headers that would be affected by changes to the body.

License

Copyright © 2023-present httpland.

Released under the MIT license