Skip to content

Commit 008780f

Browse files
committed
feat(middleware): change interface to accept iterable encoder
1 parent f6eb3a7 commit 008780f

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

deps.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export {
99
} from "https://deno.land/x/http_utils@1.0.0-beta.13/header.ts";
1010
export { acceptsEncodings } from "https://deno.land/std@0.180.0/http/negotiation.ts";
1111
export { parseMediaType } from "https://deno.land/std@0.180.0/media_types/mod.ts";
12-
export { isNull } from "https://deno.land/x/isx@1.0.0-beta.24/mod.ts";
12+
export {
13+
isIterable,
14+
isNull,
15+
} from "https://deno.land/x/isx@1.0.0-beta.24/mod.ts";
1316
export { default as compressible } from "https://esm.sh/v110/compressible@2.0.18";
1417
export { vary } from "https://deno.land/x/vary@1.0.0/mod.ts";

middleware.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import {
55
acceptsEncodings,
66
ContentNegotiationHeader,
7+
isIterable,
78
type Middleware,
89
vary,
910
} from "./deps.ts";
@@ -12,7 +13,7 @@ import { Gzip } from "./encoders/gzip.ts";
1213
import { Deflate } from "./encoders/deflate.ts";
1314
import type { Encode, Encoder, EncodingMap } from "./types.ts";
1415

15-
const DefaultEncoders: Encoder[] = [Gzip, Deflate];
16+
const BuiltInEncoders: Encoder[] = [Gzip, Deflate];
1617

1718
/** Create HTTP content compression middleware.
1819
*
@@ -37,10 +38,12 @@ const DefaultEncoders: Encoder[] = [Gzip, Deflate];
3738
* assertEquals(response.headers.get("content-encoding"), "gzip");
3839
* ```
3940
*/
40-
export function compression(encoders?: Encoder[] | EncodingMap): Middleware {
41+
export function compression(
42+
encoders?: Iterable<Encoder> | EncodingMap,
43+
): Middleware {
4144
const encodingMap = {
42-
...fromEncoders(DefaultEncoders),
43-
...Array.isArray(encoders) ? fromEncoders(encoders) : encoders,
45+
...fromEncoders(BuiltInEncoders),
46+
...isIterable(encoders) ? fromEncoders(encoders) : encoders,
4447
};
4548
const encodings = Object.keys(encodingMap);
4649

@@ -64,8 +67,8 @@ export function flat(encoder: Encoder): [encoding: string, encode: Encode] {
6467
return [encoder.encoding, encoder.encode];
6568
}
6669

67-
export function fromEncoders(encoders: readonly Encoder[]): EncodingMap {
68-
return Object.fromEntries(encoders.map(flat));
70+
export function fromEncoders(encoders: Iterable<Encoder>): EncodingMap {
71+
return Object.fromEntries(Array.from(encoders).map(flat));
6972
}
7073

7174
const IDENTITY = "identity";

0 commit comments

Comments
 (0)