Skip to content

Commit a40bf4f

Browse files
committed
Improve support for unusual identity encoding aliases further
This adds 'text', and makes sure the same set of encodings is supported in all the methods, not just decodeAsync.
1 parent 90474d1 commit a40bf4f

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The supported codecs are:
1212
* Brotli
1313
* Zstandard
1414

15-
The 'identity' and 'amz-1.0' encodings (no-op encodings) are also correctly supported.
15+
The 'identity', 'amz-1.0', 'none', 'text' and 'utf-8' encodings (no-op encodings) are also supported, passed through with no en/decoding at all. Only 'identity' is standard, but the others are all in common use regardless.
1616

1717
Found a codec used in real-world HTTP that isn't supported? Open an issue!
1818

src/index.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ const asBuffer = (input: Buffer | Uint8Array | ArrayBuffer): Buffer => {
6969
}
7070
};
7171

72+
const IDENTITY_ENCODINGS = [
73+
// Explicitly unencoded in the standard way:
74+
'identity',
75+
// Weird encoding used by some AWS requests, actually just unencoded JSON:
76+
// https://docs.aws.amazon.com/en_us/AmazonCloudWatch/latest/APIReference/making-api-requests.html
77+
'amz-1.0',
78+
// Workaround for Apache's mod_deflate handling of 'identity', used in the wild mostly with PHP.
79+
// https://github.com/curl/curl/pull/2298
80+
'none',
81+
// No idea where these comes from, but they definitely exist in real traffic and seem to come
82+
// from common confusion between content encodings and content types:
83+
'text',
84+
'utf-8'
85+
]
86+
7287
/**
7388
* Decodes a buffer, using the encodings as specified in a content-encoding header. Returns
7489
* a Buffer instance in Node, or a Uint8Array in a browser.
@@ -107,15 +122,7 @@ export async function decodeBuffer(body: Uint8Array | ArrayBuffer, encoding: str
107122
// No encoding set at all:
108123
!encoding ||
109124
// Explicitly unencoded:
110-
encoding === 'identity' ||
111-
// Weird encoding used by some AWS requests, actually just unencoded JSON:
112-
// https://docs.aws.amazon.com/en_us/AmazonCloudWatch/latest/APIReference/making-api-requests.html
113-
encoding === 'amz-1.0' ||
114-
// Workaround for Apache's mod_deflate handling of 'identity', used in the wild mostly with PHP.
115-
// https://github.com/curl/curl/pull/2298
116-
encoding === 'none' ||
117-
// Common misunderstanding, seen a few times in the wild:
118-
encoding.toLowerCase() === 'utf-8'
125+
IDENTITY_ENCODINGS.includes(encoding.toLowerCase())
119126
) {
120127
// All of the above are different ways of saying "no encoding at all"
121128
return asBuffer(bodyBuffer);
@@ -163,7 +170,12 @@ export async function decodeBuffer(body: Uint8Array | ArrayBuffer, encoding: str
163170
// Weird encoding used by some AWS requests, actually just unencoded JSON:
164171
// https://docs.aws.amazon.com/en_us/AmazonCloudWatch/latest/APIReference/making-api-requests.html
165172
return asBuffer(bodyBuffer);
166-
} else if (!encoding || encoding === 'identity') {
173+
} else if (
174+
// No encoding set at all:
175+
!encoding ||
176+
// Explicitly unencoded:
177+
IDENTITY_ENCODINGS.includes(encoding.toLowerCase())
178+
) {
167179
return asBuffer(bodyBuffer);
168180
}
169181

@@ -194,7 +206,12 @@ export async function decodeBuffer(body: Uint8Array | ArrayBuffer, encoding: str
194206
} : {}));
195207
} else if (encoding === 'zstd') {
196208
return asBuffer(await zstdCompress(bodyBuffer, level));
197-
} else if (!encoding || encoding === 'identity' || encoding === 'amz-1.0') {
209+
} else if (
210+
// No encoding set at all:
211+
!encoding ||
212+
// Explicitly unencoded:
213+
IDENTITY_ENCODINGS.includes(encoding.toLowerCase())
214+
) {
198215
return asBuffer(bodyBuffer);
199216
} else {
200217
throw new Error(`Unsupported encoding: ${encoding}`);

0 commit comments

Comments
 (0)