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

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,10 +110,16 @@ function doHttpsRequest(options, resolve, reject) {
109
110
  try {
110
111
  let body = Buffer.concat(chunks);
111
112
  let parsedBody = body.toString();
113
+ let decodedBody = body;
114
+ //Get the response content-encoding header value so we can decode before parsing
115
+ const responseContentEncoding = response.headers["content-encoding"]?.toLowerCase() ?? '';
116
+ if (responseContentEncoding !== '') {
117
+ decodedBody = decodeResponse(body, responseContentEncoding);
118
+ }
112
119
  //Get the response content-type header value so we can apply different parsing methods
113
120
  const responseContentType = response.headers["content-type"]?.toLowerCase() ?? '';
114
121
  if (responseContentType === '' || responseContentType.includes('json')) {
115
- parsedBody = JSON.parse(body.toString());
122
+ parsedBody = JSON.parse(decodedBody.toString());
116
123
  }
117
124
  else if (responseContentType.includes('xml')) {
118
125
  const parser = new fast_xml_parser_1.XMLParser({
@@ -120,10 +127,10 @@ function doHttpsRequest(options, resolve, reject) {
120
127
  ignorePiTags: true,
121
128
  ignoreAttributes: false
122
129
  });
123
- parsedBody = parser.parse(body.toString());
130
+ parsedBody = parser.parse(decodedBody.toString());
124
131
  }
125
132
  else if (responseContentType.includes('image')) {
126
- parsedBody = body.toString('base64');
133
+ parsedBody = decodedBody.toString('base64');
127
134
  }
128
135
  if (response.statusCode !== undefined && response.statusCode >= 200 && response.statusCode < 300) {
129
136
  resolve(parsedBody);
@@ -155,6 +162,24 @@ function setRequestBody(request, data) {
155
162
  }
156
163
  }
157
164
  }
165
+ function decodeResponse(encodedBody, contentEncodingHeader) {
166
+ const contentEncodings = contentEncodingHeader.replace(/\s/g, '').split(',');
167
+ let decodedBody = encodedBody;
168
+ contentEncodings.forEach(contentEncoding => {
169
+ switch (contentEncoding) {
170
+ case 'gzip':
171
+ decodedBody = zlib.gunzipSync(decodedBody);
172
+ break;
173
+ case 'deflate':
174
+ decodedBody = zlib.inflateSync(decodedBody);
175
+ break;
176
+ case 'br':
177
+ decodedBody = zlib.brotliDecompressSync(decodedBody);
178
+ break;
179
+ }
180
+ });
181
+ return decodedBody;
182
+ }
158
183
  module.exports = {
159
184
  request,
160
185
  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.2",
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;
@@ -100,20 +101,27 @@ function doHttpsRequest(options: RequestOptions, resolve: any, reject: any): Cli
100
101
  try {
101
102
  let body: Buffer = Buffer.concat(chunks);
102
103
  let parsedBody: any = body.toString();
104
+ let decodedBody: Buffer = body;
105
+
106
+ //Get the response content-encoding header value so we can decode before parsing
107
+ const responseContentEncoding: string = response.headers["content-encoding"]?.toLowerCase() ?? '';
108
+ if (responseContentEncoding !== '') {
109
+ decodedBody = decodeResponse(body, responseContentEncoding);
110
+ }
103
111
 
104
112
  //Get the response content-type header value so we can apply different parsing methods
105
113
  const responseContentType: string = response.headers["content-type"]?.toLowerCase() ?? '';
106
114
  if (responseContentType === '' || responseContentType.includes('json')) {
107
- parsedBody = JSON.parse(body.toString());
115
+ parsedBody = JSON.parse(decodedBody.toString());
108
116
  } else if (responseContentType.includes('xml')) {
109
117
  const parser = new XMLParser({
110
118
  ignoreDeclaration: true,
111
119
  ignorePiTags: true,
112
120
  ignoreAttributes: false
113
121
  });
114
- parsedBody = parser.parse(body.toString());
122
+ parsedBody = parser.parse(decodedBody.toString());
115
123
  } else if (responseContentType.includes('image')) {
116
- parsedBody = body.toString('base64');
124
+ parsedBody = decodedBody.toString('base64');
117
125
  }
118
126
 
119
127
  if (response.statusCode !== undefined && response.statusCode >= 200 && response.statusCode < 300) {
@@ -146,6 +154,25 @@ function setRequestBody(request: ClientRequest, data: any) {
146
154
  }
147
155
  }
148
156
 
157
+ function decodeResponse(encodedBody: Buffer, contentEncodingHeader: string): Buffer {
158
+ const contentEncodings: string[] = contentEncodingHeader.replace(/\s/g, '').split(',');
159
+ let decodedBody: Buffer = encodedBody;
160
+ contentEncodings.forEach(contentEncoding => {
161
+ switch (contentEncoding) {
162
+ case 'gzip':
163
+ decodedBody = zlib.gunzipSync(decodedBody);
164
+ break;
165
+ case 'deflate':
166
+ decodedBody = zlib.inflateSync(decodedBody);
167
+ break;
168
+ case 'br':
169
+ decodedBody = zlib.brotliDecompressSync(decodedBody);
170
+ break;
171
+ }
172
+ });
173
+ return decodedBody;
174
+ }
175
+
149
176
  module.exports = {
150
177
  request,
151
178
  doGetRequest,