@uoa/lambda-tracing 2.2.2 → 2.3.0-beta.1

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 CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.3.0
4
+ - Automatically decode response if Content-Encoding header is present. Supports gzip, br, and deflate encodings.
5
+
3
6
  ## 2.2.2
4
7
  - Change behaviour for optional audit parameters. Instead of removing empty optional parameters, they will be present with a hyphen ("-").
5
8
 
package/dist/uoaHttps.js CHANGED
@@ -26,6 +26,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  const https = __importStar(require("https"));
27
27
  const api_1 = require("@opentelemetry/api");
28
28
  const fast_xml_parser_1 = require("fast-xml-parser");
29
+ const zlib = __importStar(require("node:zlib"));
29
30
  function request(...args) {
30
31
  if (args[2]) {
31
32
  api_1.propagation.inject(api_1.context.active(), args[1].headers);
@@ -109,6 +110,11 @@ function doHttpsRequest(options, resolve, reject) {
109
110
  try {
110
111
  let body = Buffer.concat(chunks);
111
112
  let parsedBody = body.toString();
113
+ //Get the response content-encoding header value so we can decode before parsing
114
+ const responseContentEncoding = response.headers["content-encoding"]?.toLowerCase() ?? '';
115
+ if (responseContentEncoding !== '') {
116
+ parsedBody = decodeResponse(parsedBody, responseContentEncoding);
117
+ }
112
118
  //Get the response content-type header value so we can apply different parsing methods
113
119
  const responseContentType = response.headers["content-type"]?.toLowerCase() ?? '';
114
120
  if (responseContentType === '' || responseContentType.includes('json')) {
@@ -155,6 +161,23 @@ function setRequestBody(request, data) {
155
161
  }
156
162
  }
157
163
  }
164
+ function decodeResponse(responseBody, contentEncodingHeader) {
165
+ const contentEncodings = contentEncodingHeader.replace(/\s/g, '').split(',');
166
+ contentEncodings.forEach(contentEncoding => {
167
+ switch (contentEncoding) {
168
+ case 'gzip':
169
+ responseBody = zlib.gunzipSync(Buffer.from(responseBody)).toString();
170
+ break;
171
+ case 'deflate':
172
+ responseBody = zlib.inflateSync(Buffer.from(responseBody)).toString();
173
+ break;
174
+ case 'br':
175
+ responseBody = zlib.brotliDecompressSync(Buffer.from(responseBody)).toString();
176
+ break;
177
+ }
178
+ });
179
+ return responseBody;
180
+ }
158
181
  module.exports = {
159
182
  request,
160
183
  doGetRequest,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uoa/lambda-tracing",
3
- "version": "2.2.2",
3
+ "version": "2.3.0-beta.1",
4
4
  "description": "Library for logging & distributed tracing in UoA Lambda projects",
5
5
  "repository": {
6
6
  "type": "git",
package/uoaHttps.ts CHANGED
@@ -5,6 +5,7 @@ import {context, propagation} from "@opentelemetry/api";
5
5
  import {RequestOptions} from "https";
6
6
  import {ClientRequest} from "http";
7
7
  import {XMLParser} from "fast-xml-parser";
8
+ import * as zlib from "node:zlib";
8
9
 
9
10
  function request(options: RequestOptions | string | URL, callback?: (res: http.IncomingMessage) => void): http.ClientRequest;
10
11
  function request(url: string | URL, options: RequestOptions, callback?: (res: http.IncomingMessage) => void): http.ClientRequest;
@@ -101,6 +102,12 @@ function doHttpsRequest(options: RequestOptions, resolve: any, reject: any): Cli
101
102
  let body: Buffer = Buffer.concat(chunks);
102
103
  let parsedBody: any = body.toString();
103
104
 
105
+ //Get the response content-encoding header value so we can decode before parsing
106
+ const responseContentEncoding: string = response.headers["content-encoding"]?.toLowerCase() ?? '';
107
+ if (responseContentEncoding !== '') {
108
+ parsedBody = decodeResponse(parsedBody, responseContentEncoding);
109
+ }
110
+
104
111
  //Get the response content-type header value so we can apply different parsing methods
105
112
  const responseContentType: string = response.headers["content-type"]?.toLowerCase() ?? '';
106
113
  if (responseContentType === '' || responseContentType.includes('json')) {
@@ -146,6 +153,24 @@ function setRequestBody(request: ClientRequest, data: any) {
146
153
  }
147
154
  }
148
155
 
156
+ function decodeResponse(responseBody: string, contentEncodingHeader: string): string {
157
+ const contentEncodings: string[] = contentEncodingHeader.replace(/\s/g, '').split(',');
158
+ contentEncodings.forEach(contentEncoding => {
159
+ switch (contentEncoding) {
160
+ case 'gzip':
161
+ responseBody = zlib.gunzipSync(Buffer.from(responseBody)).toString();
162
+ break;
163
+ case 'deflate':
164
+ responseBody = zlib.inflateSync(Buffer.from(responseBody)).toString();
165
+ break;
166
+ case 'br':
167
+ responseBody = zlib.brotliDecompressSync(Buffer.from(responseBody)).toString();
168
+ break;
169
+ }
170
+ });
171
+ return responseBody;
172
+ }
173
+
149
174
  module.exports = {
150
175
  request,
151
176
  doGetRequest,