@requestly/requestly-proxy 1.2.2 → 1.2.3

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.
@@ -1,3 +1,4 @@
1
+ export function getEncoding(contentTypeHeader: any, buffer: any): any;
1
2
  export function bodyParser(contentTypeHeader: any, buffer: any): any;
2
3
  export function getContentType(contentTypeHeader: any): any;
3
4
  export function parseJsonBody(data: any): any;
@@ -3,49 +3,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.parseJsonBody = exports.getContentType = exports.bodyParser = void 0;
6
+ exports.parseJsonBody = exports.getContentType = exports.bodyParser = exports.getEncoding = void 0;
7
7
  const charset_1 = __importDefault(require("charset"));
8
8
  const mime_types_1 = __importDefault(require("mime-types"));
9
+ const getEncoding = (contentTypeHeader, buffer) => {
10
+ const encoding = (0, charset_1.default)(contentTypeHeader, buffer) || mime_types_1.default.charset(contentTypeHeader) || "utf8";
11
+ return encoding;
12
+ };
13
+ exports.getEncoding = getEncoding;
9
14
  const bodyParser = (contentTypeHeader, buffer) => {
10
- let inherentEncoding = (0, charset_1.default)(contentTypeHeader) || mime_types_1.default.charset(contentTypeHeader);
11
- let str_buffer = null;
12
- const isEncodingValid = (givenEncoding) => givenEncoding && Buffer.isEncoding(givenEncoding);
13
- const setBufferString = (givenEncoding) => (str_buffer = buffer.toString(givenEncoding));
14
- if (isEncodingValid(inherentEncoding)) {
15
- setBufferString(inherentEncoding);
15
+ const encoding = (0, exports.getEncoding)(contentTypeHeader, buffer);
16
+ try {
17
+ return buffer.toString(encoding);
16
18
  }
17
- else {
18
- const mostCommonEncodings = [
19
- "utf8",
20
- "utf16le",
21
- "ascii",
22
- "ucs2",
23
- "base64",
24
- "latin1",
25
- "binary",
26
- "hex",
27
- ];
28
- mostCommonEncodings.every((newPossibleEncoding) => {
29
- if (isEncodingValid(newPossibleEncoding)) {
30
- setBufferString(newPossibleEncoding);
31
- return false;
32
- }
33
- });
19
+ catch (error) {
20
+ // some encodings are not supposed to be turned into string
21
+ return buffer;
34
22
  }
35
- return str_buffer;
36
- /*
37
- FOLLOWING IS HOW API CLIENT PARSES THE BODY
38
- much simpler than above, but requires thorough testing
39
- */
40
- // // todo: add support for other content types
41
- // let parsedResponse;
42
- // if (contentTypeHeader?.includes("image/")) {
43
- // const raw = Buffer.from(buffer).toString("base64");
44
- // parsedResponse = `data:${contentTypeHeader};base64,${raw}`;
45
- // } else {
46
- // parsedResponse = new TextDecoder().decode(buffer);
47
- // }
48
- // return parsedResponse;
49
23
  };
50
24
  exports.bodyParser = bodyParser;
51
25
  const getContentType = (contentTypeHeader) => {
@@ -1135,7 +1135,6 @@ Proxy.prototype._onRequestData = function (ctx, chunk, callback) {
1135
1135
  if (err) {
1136
1136
  return callback(err);
1137
1137
  }
1138
- chunk = newChunk;
1139
1138
  return callback(null, newChunk);
1140
1139
  });
1141
1140
  }, function (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@requestly/requestly-proxy",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "description": "Proxy that gives superpowers to all the Requestly clients",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {