@dvsa/appdev-api-common 0.9.2 → 0.9.3-canary.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/package.json
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dvsa/appdev-api-common",
|
|
3
|
-
"version": "0.9.
|
|
4
|
-
"keywords": [
|
|
3
|
+
"version": "0.9.3-canary.0",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"dvsa",
|
|
6
|
+
"nodejs",
|
|
7
|
+
"typescript"
|
|
8
|
+
],
|
|
5
9
|
"author": "DVSA",
|
|
6
10
|
"description": "Utils library for common API functionality",
|
|
7
11
|
"publishConfig": {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DecodeBase64GzipPayload = void 0;
|
|
4
|
+
const compression_1 = require("./compression");
|
|
5
|
+
const http_status_codes_1 = require("../api/http-status-codes");
|
|
6
|
+
const DecodeBase64GzipPayload = (request, response, next) => {
|
|
7
|
+
const compressionHeaderValue = 'base64+gzip';
|
|
8
|
+
try {
|
|
9
|
+
if (typeof request.body === 'string' && request.header('X-Payload-Encoding') === compressionHeaderValue) {
|
|
10
|
+
request.body = compression_1.DataCompression.decompress(request.body);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
catch (err) {
|
|
14
|
+
return response.status(http_status_codes_1.HttpStatus.BAD_REQUEST).send({ message: 'Failed to decode payload' });
|
|
15
|
+
}
|
|
16
|
+
next();
|
|
17
|
+
};
|
|
18
|
+
exports.DecodeBase64GzipPayload = DecodeBase64GzipPayload;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Request, Response } from 'express';
|
|
2
|
+
import type { APIGatewayProxyResult } from 'aws-lambda';
|
|
3
|
+
import type { Logger } from '@aws-lambda-powertools/logger';
|
|
4
|
+
export declare const EncodePayloadInterceptor: ({ response, request }: {
|
|
5
|
+
response: Response;
|
|
6
|
+
request: Request;
|
|
7
|
+
}, content: Partial<APIGatewayProxyResult>, logger: Logger) => Response<any, Record<string, any>>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EncodePayloadInterceptor = void 0;
|
|
4
|
+
const http_status_codes_1 = require("../api/http-status-codes");
|
|
5
|
+
const compression_1 = require("./compression");
|
|
6
|
+
const EncodePayloadInterceptor = ({ response, request }, content, logger) => {
|
|
7
|
+
const compressionHeaderValue = 'base64+gzip';
|
|
8
|
+
for (const [header, value] of Object.entries(content?.headers || {})) {
|
|
9
|
+
response.setHeader(header, value);
|
|
10
|
+
}
|
|
11
|
+
if (content.statusCode) {
|
|
12
|
+
response.status(content.statusCode);
|
|
13
|
+
}
|
|
14
|
+
// if there's no-body, return the response
|
|
15
|
+
if (!content.body) {
|
|
16
|
+
return response;
|
|
17
|
+
}
|
|
18
|
+
const didRequestCompressed = request?.headers?.['x-accept-encoding'] === compressionHeaderValue;
|
|
19
|
+
const shouldCompress = didRequestCompressed && content.statusCode === http_status_codes_1.HttpStatus.OK;
|
|
20
|
+
// Parse the body if it's a string
|
|
21
|
+
const bodyData = typeof content.body === 'string' ? JSON.parse(content.body) : content.body;
|
|
22
|
+
if (shouldCompress) {
|
|
23
|
+
logger.debug('Compressing response data');
|
|
24
|
+
try {
|
|
25
|
+
const compressedData = compression_1.DataCompression.compress(bodyData);
|
|
26
|
+
// Check if compressed data is actually smaller
|
|
27
|
+
const originalSize = Buffer.byteLength(JSON.stringify(bodyData), 'utf8');
|
|
28
|
+
const compressedSize = Buffer.byteLength(JSON.stringify(compressedData), 'utf8');
|
|
29
|
+
if (compressedSize >= originalSize) {
|
|
30
|
+
logger.debug('Compression not beneficial, using original data');
|
|
31
|
+
response.json(bodyData);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
// Set the content encoding headers
|
|
35
|
+
response.setHeader('Access-Control-Expose-Headers', 'content-encoding');
|
|
36
|
+
response.setHeader('content-encoding', compressionHeaderValue);
|
|
37
|
+
response.json(compressedData);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
logger.error('Error compressing response data', { err });
|
|
42
|
+
response.json(bodyData);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
response.json(bodyData);
|
|
47
|
+
}
|
|
48
|
+
return response;
|
|
49
|
+
};
|
|
50
|
+
exports.EncodePayloadInterceptor = EncodePayloadInterceptor;
|