@adobe/spacecat-shared-http-utils 1.17.9 → 1.18.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/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/index.js +16 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [@adobe/spacecat-shared-http-utils-v1.18.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-http-utils-v1.17.9...@adobe/spacecat-shared-http-utils-v1.18.0) (2025-11-06)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* allow brotli compression for json responses ([#1095](https://github.com/adobe/spacecat-shared/issues/1095)) ([b81f349](https://github.com/adobe/spacecat-shared/commit/b81f3497d7a64dd1b2a888f98971f7913e3ff1db))
|
|
7
|
+
|
|
1
8
|
# [@adobe/spacecat-shared-http-utils-v1.17.9](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-http-utils-v1.17.8...@adobe/spacecat-shared-http-utils-v1.17.9) (2025-10-28)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import { Response } from '@adobe/fetch';
|
|
14
|
-
import { gzipSync } from 'zlib';
|
|
14
|
+
import { brotliCompressSync, gzipSync, constants as zlibConstants } from 'zlib';
|
|
15
15
|
|
|
16
16
|
import LegacyApiKeyHandler from './auth/handlers/legacy-api-key.js';
|
|
17
17
|
import AdobeImsHandler from './auth/handlers/ims.js';
|
|
@@ -64,6 +64,7 @@ export function createResponse(body, status = 200, headers = {}) {
|
|
|
64
64
|
const contentType = getHeader(headers, HEADER_CONTENT_TYPE);
|
|
65
65
|
const contentEncoding = getHeader(headers, HEADER_CONTENT_ENCODING);
|
|
66
66
|
const wantsGzip = contentEncoding?.toLowerCase() === 'gzip';
|
|
67
|
+
const wantsBrotli = contentEncoding?.toLowerCase() === 'br';
|
|
67
68
|
|
|
68
69
|
// default to application/json if content-type not set
|
|
69
70
|
if (!contentType) {
|
|
@@ -72,10 +73,22 @@ export function createResponse(body, status = 200, headers = {}) {
|
|
|
72
73
|
|
|
73
74
|
const finalContentType = getHeader(headers, HEADER_CONTENT_TYPE);
|
|
74
75
|
|
|
75
|
-
// if content-type is JSON, serialize and optionally
|
|
76
|
+
// if content-type is JSON, serialize and optionally compress
|
|
76
77
|
if (finalContentType?.toLowerCase().includes(CONTENT_TYPE_JSON)) {
|
|
77
78
|
const jsonBody = body === '' ? '' : JSON.stringify(body);
|
|
78
|
-
|
|
79
|
+
|
|
80
|
+
if (wantsGzip) {
|
|
81
|
+
responseBody = gzipSync(Buffer.from(jsonBody));
|
|
82
|
+
} else if (wantsBrotli) {
|
|
83
|
+
responseBody = brotliCompressSync(Buffer.from(jsonBody), {
|
|
84
|
+
params: {
|
|
85
|
+
// the default quality is too complex for the lambda and can lead to 503s
|
|
86
|
+
[zlibConstants.BROTLI_PARAM_QUALITY]: 4,
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
} else {
|
|
90
|
+
responseBody = jsonBody;
|
|
91
|
+
}
|
|
79
92
|
}
|
|
80
93
|
|
|
81
94
|
return new Response(responseBody, {
|