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