@remix-run/compression-middleware 0.0.0

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Shopify Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,171 @@
1
+ # compression-middleware
2
+
3
+ Middleware for compressing HTTP responses for use with [`@remix-run/fetch-router`](https://github.com/remix-run/remix/tree/main/packages/fetch-router).
4
+
5
+ Automatically compresses responses using `gzip`, `brotli`, or `deflate` based on the client's `Accept-Encoding` header, with intelligent defaults for media type filtering and threshold-based compression.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ npm install @remix-run/compression-middleware
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { createRouter } from '@remix-run/fetch-router'
17
+ import { compression } from '@remix-run/compression-middleware'
18
+
19
+ let router = createRouter({
20
+ middleware: [compression()],
21
+ })
22
+ ```
23
+
24
+ The middleware will automatically compress responses for compressible MIME types when:
25
+
26
+ - The client supports compression (`Accept-Encoding` header with a supported encoding)
27
+ - The response is large enough to benefit from compression (≥1024 bytes if `Content-Length` is present, by default)
28
+ - The response hasn't already been compressed
29
+ - The response doesn't advertise range support (`Accept-Ranges: bytes`)
30
+
31
+ ### Threshold
32
+
33
+ **Default:** `1024` (only enforced if `Content-Length` is present)
34
+
35
+ Set the minimum response size in bytes to compress:
36
+
37
+ ```ts
38
+ import { createRouter } from '@remix-run/fetch-router'
39
+ import { compression } from '@remix-run/compression-middleware'
40
+
41
+ let router = createRouter({
42
+ middleware: [
43
+ compression({
44
+ threshold: 2048, // Only compress responses ≥2KB
45
+ }),
46
+ ],
47
+ })
48
+ ```
49
+
50
+ ### Encodings
51
+
52
+ **Default:** `['br', 'gzip', 'deflate']`
53
+
54
+ Customize which compression algorithms to support:
55
+
56
+ ```ts
57
+ import { createRouter } from '@remix-run/fetch-router'
58
+ import { compression } from '@remix-run/compression-middleware'
59
+
60
+ let router = createRouter({
61
+ middleware: [
62
+ compression({
63
+ encodings: ['br', 'gzip'], // Only use Brotli and Gzip
64
+ }),
65
+ ],
66
+ })
67
+ ```
68
+
69
+ The `encodings` option can also be a function that receives the response:
70
+
71
+ ```ts
72
+ import { createRouter } from '@remix-run/fetch-router'
73
+ import { compression } from '@remix-run/compression-middleware'
74
+
75
+ let router = createRouter({
76
+ middleware: [
77
+ compression({
78
+ encodings: (response) => {
79
+ // Use different encodings for server-sent events
80
+ let contentType = response.headers.get('Content-Type')
81
+ return contentType?.startsWith('text/event-stream;')
82
+ ? ['gzip', 'deflate']
83
+ : ['br', 'gzip', 'deflate']
84
+ },
85
+ }),
86
+ ],
87
+ })
88
+ ```
89
+
90
+ ### Filter Media Type
91
+
92
+ **Default:** Uses `isCompressibleMimeType()` from [`@remix-run/mime`](https://github.com/remix-run/remix/tree/main/packages/mime)
93
+
94
+ You can customize this behavior with the `filterMediaType` option:
95
+
96
+ ```ts
97
+ import { createRouter } from '@remix-run/fetch-router'
98
+ import { compression } from '@remix-run/compression-middleware'
99
+ import { isCompressibleMimeType } from '@remix-run/mime'
100
+
101
+ let router = createRouter({
102
+ middleware: [
103
+ compression({
104
+ filterMediaType(mediaType) {
105
+ // Add a custom media type to the default compressible list
106
+ return isCompressibleMimeType(mediaType) || mediaType === 'application/vnd.example+data'
107
+ },
108
+ }),
109
+ ],
110
+ })
111
+ ```
112
+
113
+ ### Compression Options
114
+
115
+ **Default:** Uses Node.js defaults for [zlib](https://nodejs.org/api/zlib.html#class-options) and [Brotli](https://nodejs.org/api/zlib.html#class-brotlioptions), with automatic flush handling for server-sent events.
116
+
117
+ You can pass options options to the underlying Node.js `zlib` and `brotli` compressors for fine-grained control:
118
+
119
+ ```ts
120
+ import { createRouter } from '@remix-run/fetch-router'
121
+ import { compression } from '@remix-run/compression-middleware'
122
+ import { zlib } from 'node:zlib'
123
+
124
+ let router = createRouter({
125
+ middleware: [
126
+ compression({
127
+ zlib: {
128
+ level: 6,
129
+ },
130
+ brotli: {
131
+ params: {
132
+ [zlib.constants.BROTLI_PARAM_QUALITY]: 4,
133
+ },
134
+ },
135
+ }),
136
+ ],
137
+ })
138
+ ```
139
+
140
+ Like `encodings`, both `zlib` and `brotli` options can also be functions that receive the response:
141
+
142
+ ```ts
143
+ import zlib from 'node:zlib'
144
+ import { createRouter } from '@remix-run/fetch-router'
145
+ import { compression } from '@remix-run/compression-middleware'
146
+
147
+ let router = createRouter({
148
+ middleware: [
149
+ compression({
150
+ brotli: (response) => {
151
+ let contentType = response.headers.get('Content-Type')
152
+ return {
153
+ params: {
154
+ [zlib.constants.BROTLI_PARAM_QUALITY]: contentType?.startsWith('text/html;') ? 4 : 11,
155
+ },
156
+ }
157
+ },
158
+ }),
159
+ ],
160
+ })
161
+ ```
162
+
163
+ ## Related Packages
164
+
165
+ - [`@remix-run/fetch-router`](https://github.com/remix-run/remix/tree/main/packages/fetch-router) - Router for the web Fetch API
166
+ - [`@remix-run/mime`](https://github.com/remix-run/remix/tree/main/packages/mime) - MIME type utilities
167
+ - [`@remix-run/response`](https://github.com/remix-run/remix/tree/main/packages/response) - Response helpers
168
+
169
+ ## License
170
+
171
+ MIT
@@ -0,0 +1,2 @@
1
+ export { compression, type CompressionOptions } from './lib/compression.ts';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,KAAK,kBAAkB,EAAE,MAAM,sBAAsB,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { compression } from "./lib/compression.js";
@@ -0,0 +1,53 @@
1
+ import type { BrotliOptions, ZlibOptions } from 'node:zlib';
2
+ import type { Middleware } from '@remix-run/fetch-router';
3
+ type Encoding = 'br' | 'gzip' | 'deflate';
4
+ export interface CompressionOptions {
5
+ /**
6
+ * Minimum size in bytes to compress (only enforced if Content-Length present).
7
+ * Default: 1024
8
+ */
9
+ threshold?: number;
10
+ /**
11
+ * Optional filter to control which responses get compressed based on media type.
12
+ * If not provided, uses compressible media types from mime-db.
13
+ */
14
+ filterMediaType?: (mediaType: string) => boolean;
15
+ /**
16
+ * Which encodings the server supports for negotiation in order of preference.
17
+ * Can be static or a function that returns encodings based on the response.
18
+ *
19
+ * Default: ['br', 'gzip', 'deflate']
20
+ */
21
+ encodings?: Encoding[] | ((response: Response) => Encoding[]);
22
+ /**
23
+ * node:zlib options for gzip/deflate compression.
24
+ * Can be static or a function that returns options based on the response.
25
+ *
26
+ * See: https://nodejs.org/api/zlib.html#class-options
27
+ */
28
+ zlib?: ZlibOptions | ((response: Response) => ZlibOptions);
29
+ /**
30
+ * node:zlib options for Brotli compression.
31
+ * Can be static or a function that returns options based on the response.
32
+ *
33
+ * See: https://nodejs.org/api/zlib.html#class-brotlioptions
34
+ */
35
+ brotli?: BrotliOptions | ((response: Response) => BrotliOptions);
36
+ }
37
+ /**
38
+ * Creates a middleware handler that automatically compresses responses based on the
39
+ * client's Accept-Encoding header, along with an additional Content-Type filter
40
+ * by default to only apply compression to appropriate media types.
41
+ *
42
+ * @param options Optional compression settings
43
+ * @returns A middleware handler that automatically compresses responses based on the client's Accept-Encoding header
44
+ * @example
45
+ * ```ts
46
+ * let router = createRouter({
47
+ * middleware: [compression()],
48
+ * })
49
+ * ```
50
+ */
51
+ export declare function compression(options?: CompressionOptions): Middleware;
52
+ export {};
53
+ //# sourceMappingURL=compression.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compression.d.ts","sourceRoot":"","sources":["../../src/lib/compression.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAC3D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAIzD,KAAK,QAAQ,GAAG,IAAI,GAAG,MAAM,GAAG,SAAS,CAAA;AAEzC,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAElB;;;OAGG;IACH,eAAe,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAA;IAEhD;;;;;OAKG;IACH,SAAS,CAAC,EAAE,QAAQ,EAAE,GAAG,CAAC,CAAC,QAAQ,EAAE,QAAQ,KAAK,QAAQ,EAAE,CAAC,CAAA;IAE7D;;;;;OAKG;IACH,IAAI,CAAC,EAAE,WAAW,GAAG,CAAC,CAAC,QAAQ,EAAE,QAAQ,KAAK,WAAW,CAAC,CAAA;IAE1D;;;;;OAKG;IACH,MAAM,CAAC,EAAE,aAAa,GAAG,CAAC,CAAC,QAAQ,EAAE,QAAQ,KAAK,aAAa,CAAC,CAAA;CACjE;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,UAAU,CAwCpE"}
@@ -0,0 +1,52 @@
1
+ import { compressResponse } from '@remix-run/response/compress';
2
+ import { isCompressibleMimeType } from '@remix-run/mime';
3
+ /**
4
+ * Creates a middleware handler that automatically compresses responses based on the
5
+ * client's Accept-Encoding header, along with an additional Content-Type filter
6
+ * by default to only apply compression to appropriate media types.
7
+ *
8
+ * @param options Optional compression settings
9
+ * @returns A middleware handler that automatically compresses responses based on the client's Accept-Encoding header
10
+ * @example
11
+ * ```ts
12
+ * let router = createRouter({
13
+ * middleware: [compression()],
14
+ * })
15
+ * ```
16
+ */
17
+ export function compression(options) {
18
+ return async (context, next) => {
19
+ let response = await next();
20
+ let contentTypeHeader = response.headers.get('Content-Type');
21
+ if (!contentTypeHeader) {
22
+ return response;
23
+ }
24
+ let mediaType = contentTypeHeader.split(';')[0].trim();
25
+ if (!mediaType) {
26
+ return response;
27
+ }
28
+ let filterMediaType = options?.filterMediaType ?? isCompressibleMimeType;
29
+ if (!filterMediaType(mediaType)) {
30
+ return response;
31
+ }
32
+ let compressOptions = {
33
+ threshold: options?.threshold,
34
+ encodings: options?.encodings
35
+ ? typeof options.encodings === 'function'
36
+ ? options.encodings(response)
37
+ : options.encodings
38
+ : undefined,
39
+ zlib: options?.zlib
40
+ ? typeof options.zlib === 'function'
41
+ ? options.zlib(response)
42
+ : options.zlib
43
+ : undefined,
44
+ brotli: options?.brotli
45
+ ? typeof options.brotli === 'function'
46
+ ? options.brotli(response)
47
+ : options.brotli
48
+ : undefined,
49
+ };
50
+ return compressResponse(response, context.request, compressOptions);
51
+ };
52
+ }
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@remix-run/compression-middleware",
3
+ "version": "0.0.0",
4
+ "description": "Middleware for compressing HTTP responses",
5
+ "author": "Michael Jackson <mjijackson@gmail.com>",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/remix-run/remix.git",
10
+ "directory": "packages/compression-middleware"
11
+ },
12
+ "homepage": "https://github.com/remix-run/remix/tree/main/packages/compression-middleware#readme",
13
+ "files": [
14
+ "LICENSE",
15
+ "README.md",
16
+ "dist",
17
+ "src",
18
+ "!src/**/*.test.ts"
19
+ ],
20
+ "type": "module",
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "default": "./dist/index.js"
25
+ },
26
+ "./package.json": "./package.json"
27
+ },
28
+ "devDependencies": {
29
+ "@types/node": "^24.6.0",
30
+ "typescript": "^5.9.3",
31
+ "@remix-run/fetch-router": "0.12.0",
32
+ "@remix-run/response": "0.1.0",
33
+ "@remix-run/mime": "0.1.0"
34
+ },
35
+ "peerDependencies": {
36
+ "@remix-run/fetch-router": "^0.12.0",
37
+ "@remix-run/mime": "^0.1.0",
38
+ "@remix-run/response": "^0.1.0"
39
+ },
40
+ "keywords": [
41
+ "fetch",
42
+ "router",
43
+ "middleware",
44
+ "compression",
45
+ "gzip",
46
+ "brotli",
47
+ "deflate"
48
+ ],
49
+ "scripts": {
50
+ "build": "tsc -p tsconfig.build.json",
51
+ "clean": "git clean -fdX",
52
+ "test": "node --disable-warning=ExperimentalWarning --test './src/**/*.test.ts'",
53
+ "typecheck": "tsc --noEmit"
54
+ }
55
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { compression, type CompressionOptions } from './lib/compression.ts'
@@ -0,0 +1,100 @@
1
+ import type { BrotliOptions, ZlibOptions } from 'node:zlib'
2
+ import type { Middleware } from '@remix-run/fetch-router'
3
+ import { compressResponse, type CompressResponseOptions } from '@remix-run/response/compress'
4
+ import { isCompressibleMimeType } from '@remix-run/mime'
5
+
6
+ type Encoding = 'br' | 'gzip' | 'deflate'
7
+
8
+ export interface CompressionOptions {
9
+ /**
10
+ * Minimum size in bytes to compress (only enforced if Content-Length present).
11
+ * Default: 1024
12
+ */
13
+ threshold?: number
14
+
15
+ /**
16
+ * Optional filter to control which responses get compressed based on media type.
17
+ * If not provided, uses compressible media types from mime-db.
18
+ */
19
+ filterMediaType?: (mediaType: string) => boolean
20
+
21
+ /**
22
+ * Which encodings the server supports for negotiation in order of preference.
23
+ * Can be static or a function that returns encodings based on the response.
24
+ *
25
+ * Default: ['br', 'gzip', 'deflate']
26
+ */
27
+ encodings?: Encoding[] | ((response: Response) => Encoding[])
28
+
29
+ /**
30
+ * node:zlib options for gzip/deflate compression.
31
+ * Can be static or a function that returns options based on the response.
32
+ *
33
+ * See: https://nodejs.org/api/zlib.html#class-options
34
+ */
35
+ zlib?: ZlibOptions | ((response: Response) => ZlibOptions)
36
+
37
+ /**
38
+ * node:zlib options for Brotli compression.
39
+ * Can be static or a function that returns options based on the response.
40
+ *
41
+ * See: https://nodejs.org/api/zlib.html#class-brotlioptions
42
+ */
43
+ brotli?: BrotliOptions | ((response: Response) => BrotliOptions)
44
+ }
45
+
46
+ /**
47
+ * Creates a middleware handler that automatically compresses responses based on the
48
+ * client's Accept-Encoding header, along with an additional Content-Type filter
49
+ * by default to only apply compression to appropriate media types.
50
+ *
51
+ * @param options Optional compression settings
52
+ * @returns A middleware handler that automatically compresses responses based on the client's Accept-Encoding header
53
+ * @example
54
+ * ```ts
55
+ * let router = createRouter({
56
+ * middleware: [compression()],
57
+ * })
58
+ * ```
59
+ */
60
+ export function compression(options?: CompressionOptions): Middleware {
61
+ return async (context, next) => {
62
+ let response = await next()
63
+
64
+ let contentTypeHeader = response.headers.get('Content-Type')
65
+ if (!contentTypeHeader) {
66
+ return response
67
+ }
68
+
69
+ let mediaType = contentTypeHeader.split(';')[0].trim()
70
+ if (!mediaType) {
71
+ return response
72
+ }
73
+
74
+ let filterMediaType = options?.filterMediaType ?? isCompressibleMimeType
75
+ if (!filterMediaType(mediaType)) {
76
+ return response
77
+ }
78
+
79
+ let compressOptions: CompressResponseOptions = {
80
+ threshold: options?.threshold,
81
+ encodings: options?.encodings
82
+ ? typeof options.encodings === 'function'
83
+ ? options.encodings(response)
84
+ : options.encodings
85
+ : undefined,
86
+ zlib: options?.zlib
87
+ ? typeof options.zlib === 'function'
88
+ ? options.zlib(response)
89
+ : options.zlib
90
+ : undefined,
91
+ brotli: options?.brotli
92
+ ? typeof options.brotli === 'function'
93
+ ? options.brotli(response)
94
+ : options.brotli
95
+ : undefined,
96
+ }
97
+
98
+ return compressResponse(response, context.request, compressOptions)
99
+ }
100
+ }