@itentialopensource/adapter-utils 5.6.0 → 5.7.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 CHANGED
@@ -1,4 +1,12 @@
1
1
 
2
+ ## 5.7.0 [08-21-2024]
3
+
4
+ * Update gzip logic
5
+
6
+ See merge request itentialopensource/adapter-utils!297
7
+
8
+ ---
9
+
2
10
  ## 5.6.0 [07-31-2024]
3
11
 
4
12
  * Add abilities to refresh token and set msa query
@@ -780,34 +780,61 @@ function makeRequest(request, entitySchema, callProperties, startTrip, attempt,
780
780
  // make the call to System
781
781
  const httpRequest = useProt.request(request.header, (res) => {
782
782
  let respStr = '';
783
- res.setEncoding('utf8');
784
-
783
+ if (res.headers['content-encoding'] !== 'gzip') {
784
+ res.setEncoding('utf8');
785
+ }
786
+ const chunks = [];
785
787
  // process data from response
786
788
  res.on('data', (replyData) => {
789
+ chunks.push(replyData);
787
790
  respStr += replyData;
788
791
  });
789
792
 
790
793
  // process end of response
791
794
  res.on('end', () => {
795
+ let buffer = '';
796
+ if (res.headers['content-encoding'] === 'gzip') {
797
+ buffer = Buffer.concat(chunks);
798
+ }
799
+
792
800
  const tripDiff = process.hrtime(roundTTime);
793
801
  const tripEnd = `${Math.round(((tripDiff[0] * NS_PER_SEC) + tripDiff[1]) / 1000000)}ms`;
794
- const callResp = {
795
- status: 'success',
796
- code: res.statusCode,
797
- headers: res.headers,
798
- response: respStr,
799
- request: {
800
- protocol: protStr,
801
- host: request.header.hostname,
802
- port: request.header.port,
803
- uri: request.header.path,
804
- method: request.header.method,
805
- payload: request.body
806
- },
807
- redirects: attempt,
808
- tripTime: tripEnd
809
- };
810
-
802
+ let callResp = {};
803
+ if (res.headers['content-encoding'] === 'gzip') {
804
+ callResp = {
805
+ status: 'success',
806
+ code: res.statusCode,
807
+ headers: res.headers,
808
+ response: buffer,
809
+ request: {
810
+ protocol: protStr,
811
+ host: request.header.hostname,
812
+ port: request.header.port,
813
+ uri: request.header.path,
814
+ method: request.header.method,
815
+ payload: request.body
816
+ },
817
+ redirects: attempt,
818
+ tripTime: tripEnd
819
+ };
820
+ } else {
821
+ callResp = {
822
+ status: 'success',
823
+ code: res.statusCode,
824
+ headers: res.headers,
825
+ response: respStr,
826
+ request: {
827
+ protocol: protStr,
828
+ host: request.header.hostname,
829
+ port: request.header.port,
830
+ uri: request.header.path,
831
+ method: request.header.method,
832
+ payload: request.body
833
+ },
834
+ redirects: attempt,
835
+ tripTime: tripEnd
836
+ };
837
+ }
811
838
  // if there was a cookie or biscotti returned, need to set cookie on the new request
812
839
  if (request.header.headers) {
813
840
  const headKeys = Object.keys(request.header.headers);
@@ -534,8 +534,8 @@ class AdapterPropertyUtil {
534
534
  }
535
535
  if (entitySchema.responseDatatype.toUpperCase() !== 'JSON' && entitySchema.responseDatatype.toUpperCase() !== 'XML'
536
536
  && entitySchema.responseDatatype.toUpperCase() !== 'URLENCODE' && entitySchema.responseDatatype.toUpperCase() !== 'XML2JSON'
537
- && entitySchema.requestDatatype.toUpperCase() !== 'GZIP2XML2JSON' && entitySchema.requestDatatype.toUpperCase() !== 'GZIP2XML'
538
- && entitySchema.requestDatatype.toUpperCase() !== 'GZIP2JSON') {
537
+ && entitySchema.responseDatatype.toUpperCase() !== 'GZIP2XML2JSON' && entitySchema.responseDatatype.toUpperCase() !== 'GZIP2XML'
538
+ && entitySchema.responseDatatype.toUpperCase() !== 'GZIP2JSON' && entitySchema.responseDatatype.toUpperCase() !== 'GZIP2PLAIN') {
539
539
  entitySchema.responseDatatype = 'PLAIN';
540
540
  }
541
541
 
@@ -952,7 +952,9 @@ class AdapterPropertyUtil {
952
952
  entitySchema.requestDatatype = 'PLAIN';
953
953
  }
954
954
  if (entitySchema.responseDatatype.toUpperCase() !== 'JSON' && entitySchema.responseDatatype.toUpperCase() !== 'XML'
955
- && entitySchema.responseDatatype.toUpperCase() !== 'URLENCODE' && entitySchema.responseDatatype.toUpperCase() !== 'XML2JSON') {
955
+ && entitySchema.responseDatatype.toUpperCase() !== 'URLENCODE' && entitySchema.responseDatatype.toUpperCase() !== 'XML2JSON'
956
+ && entitySchema.responseDatatype.toUpperCase() !== 'GZIP2XML2JSON' && entitySchema.responseDatatype.toUpperCase() !== 'GZIP2XML'
957
+ && entitySchema.responseDatatype.toUpperCase() !== 'GZIP2JSON' && entitySchema.responseDatatype.toUpperCase() !== 'GZIP2PLAIN') {
956
958
  entitySchema.responseDatatype = 'PLAIN';
957
959
  }
958
960
 
@@ -8,7 +8,7 @@ const querystring = require('querystring');
8
8
  const jsonQuery = require('json-query');
9
9
  const jsonxml = require('jsontoxml');
10
10
  const xml2js = require('xml2js');
11
- const pako = require('pako');
11
+ const zlib = require('zlib');
12
12
 
13
13
  const globalSchema = JSON.parse(require('fs').readFileSync(require('path').join(__dirname, '/../schemas/globalSchema.json')));
14
14
 
@@ -53,19 +53,6 @@ function matchResponse(uriPath, method, type, mockresponses) {
53
53
  return null;
54
54
  }
55
55
 
56
- /*
57
- * INTERNAL FUNCTION: Parse XML
58
- */
59
- function parseXML(xmlString, callback) {
60
- const parser = new xml2js.Parser({ explicitArray: false, attrkey: '_attr' });
61
- parser.parseString(xmlString, (error, result) => {
62
- if (error) {
63
- return callback(error);
64
- }
65
- return callback(null, result);
66
- });
67
- }
68
-
69
56
  /*
70
57
  * INTERNAL FUNCTION: recursively inspect body data if heirarchical
71
58
  */
@@ -491,62 +478,58 @@ function handleRestRequest(request, entityId, entitySchema, callProperties, filt
491
478
  if (entitySchema && entitySchema.responseDatatype && entitySchema.responseDatatype.toUpperCase() === 'URLENCODE') {
492
479
  // return the response
493
480
  retResponse = querystring.parse(resObj.response.trim());
494
- } else if (entitySchema && entitySchema.responseDatatype && (entitySchema.responseDatatype.toUpperCase() === 'GZIP2PLAIN'
495
- || entitySchema.responseDatatype.toUpperCase() === 'GZIP2JSON' || entitySchema.responseDatatype.toUpperCase() === 'GZIP2XML'
496
- || entitySchema.responseDatatype.toUpperCase() === 'GZIP2XML2JSON')) {
497
- // If response is GZIP
481
+ } if (entitySchema && entitySchema.responseDatatype
482
+ && ['GZIP2PLAIN', 'GZIP2JSON', 'GZIP2XML', 'GZIP2XML2JSON'].includes(entitySchema.responseDatatype.toUpperCase())) {
498
483
  const responseDatatype = entitySchema.responseDatatype.toUpperCase();
499
- const responseBody = pako.inflate(Buffer.from(resObj.response, 'binary'), { to: 'string' });
500
- // Handle conversion based on responseDatatype
501
- if (responseDatatype === 'GZIP2PLAIN') {
502
- retObject.response = responseBody;
503
- } else if (responseDatatype === 'GZIP2JSON') {
504
- // Parse responseBody as JSON
505
- retObject.response = JSON.parse(responseBody);
506
- } else if (responseDatatype === 'GZIP2XML' || responseDatatype === 'GZIP2XML2JSON') {
507
- parseXML(responseBody, (error, xmlResult) => {
508
- if (error) {
509
- log.warn(`${origin}: Error parsing XML response: ${error}`);
484
+
485
+ return zlib.gunzip(resObj.response, (err, unzipresponse) => {
486
+ if (err) {
487
+ log.warn(`${origin}: Error inflating gzip response: ${err}`);
488
+ return callback(retObject);
489
+ }
490
+
491
+ const responseBody = unzipresponse.toString('utf8'); // Convert buffer to string
492
+ if (responseDatatype === 'GZIP2PLAIN') {
493
+ retObject.response = responseBody;
494
+ log.debug(`${origin}: RESPONSE: ${retObject.response}`);
495
+ return callback(retObject);
496
+ } if (responseDatatype === 'GZIP2JSON') {
497
+ try {
498
+ retObject.response = JSON.parse(responseBody);
499
+ log.debug(`${origin}: RESPONSE: ${retObject.response}`);
500
+ return callback(retObject); // Parse responseBody as JSON
501
+ } catch (jsonErr) {
502
+ log.warn(`${origin}: Error parsing JSON response: ${jsonErr}`);
510
503
  return callback(retObject);
511
504
  }
512
-
513
- retObject.response = xmlResult;
514
-
515
- if (responseDatatype === 'GZIP2XML2JSON') {
516
- // Optionally convert XML to JSON
517
- try {
518
- const parser = new xml2js.Parser({ explicitArray: false, attrkey: '_attr' });
519
- return parser.parseString(xmlResult, (err, result) => {
520
- if (err) {
521
- log.warn(`${origin}: Unable to parse xml to json ${err}`);
522
- return callback(retObject);
523
- }
524
- // Logic to convert keys specified to array if object
525
- if (xmlArrayKeys) {
526
- setArrays(result, xmlArrayKeys);
527
- }
528
- retObject.response = result;
529
- return callback(retObject);
530
- });
531
- } catch (ex) {
532
- log.warn(`${origin}: Unable to get json from xml ${ex}`);
505
+ } else if (responseDatatype === 'GZIP2XML' || responseDatatype === 'GZIP2XML2JSON') {
506
+ xml2js.parseString(responseBody, { explicitArray: false, attrkey: '_attr' }, (xmlErr, xmlResult) => {
507
+ if (xmlErr) {
508
+ log.warn(`${origin}: Error parsing XML response: ${xmlErr}`);
533
509
  return callback(retObject);
534
510
  }
535
- }
536
511
 
537
- log.debug(`${origin}: RESPONSE: ${retObject.response}`);
538
- return callback(retObject);
539
- });
540
- }
541
- } else {
542
- // process the response - parse it
543
- try {
544
- retResponse = JSON.parse(resObj.response.trim());
545
- } catch (ex) {
546
- // otherwise log parse failure and return the unparsed response
547
- log.warn(`${origin}: An error occurred parsing the resulting JSON: ${ex}: Actual Response is: ${resObj}`);
512
+ retObject.response = xmlResult;
513
+
514
+ if (responseDatatype === 'GZIP2XML2JSON') {
515
+ // Optionally convert XML to JSON, already done above by xml2js
516
+ if (xmlArrayKeys) {
517
+ setArrays(retObject.response, xmlArrayKeys); // Handle array conversion
518
+ }
519
+ }
520
+ return callback(retObject);
521
+ });
522
+ }
548
523
  return callback(retObject);
549
- }
524
+ });
525
+ }
526
+ // process the response - parse it
527
+ try {
528
+ retResponse = JSON.parse(resObj.response.trim());
529
+ } catch (ex) {
530
+ // otherwise log parse failure and return the unparsed response
531
+ log.warn(`${origin}: An error occurred parsing the resulting JSON: ${ex}: Actual Response is: ${resObj}`);
532
+ return callback(retObject);
550
533
  }
551
534
 
552
535
  // Make the call to translate the received Entity to Pronghorn Entity
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itentialopensource/adapter-utils",
3
- "version": "5.6.0",
3
+ "version": "5.7.0",
4
4
  "description": "Itential Adapter Utility Libraries",
5
5
  "scripts": {
6
6
  "postinstall": "node utils/setup.js",
@@ -39,12 +39,12 @@
39
39
  "jsontoxml": "^1.0.1",
40
40
  "jsonwebtoken": "^9.0.1",
41
41
  "mongodb": "^3.7.4",
42
- "pako": "^2.1.0",
43
42
  "querystring": "^0.2.0",
44
43
  "readline-sync": "^1.4.10",
45
44
  "socks-proxy-agent": "^8.0.1",
46
45
  "uuid": "^9.0.0",
47
- "xml2js": "^0.6.0"
46
+ "xml2js": "^0.6.0",
47
+ "zlib": "^1.0.5"
48
48
  },
49
49
  "devDependencies": {
50
50
  "chai": "^4.3.7",
Binary file