@requestly/requestly-proxy 1.1.15 → 1.1.17

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.
@@ -19,8 +19,9 @@ const get_original_request_headers = (ctx) => {
19
19
  };
20
20
  exports.get_original_request_headers = get_original_request_headers;
21
21
  const get_original_response_headers = (ctx) => {
22
+ var _a;
22
23
  // TODO: This needs to be fetched from ctx.clientToProxy headers
23
- return ctx.serverToProxyResponse.headers;
24
+ return ((_a = ctx === null || ctx === void 0 ? void 0 : ctx.serverToProxyResponse) === null || _a === void 0 ? void 0 : _a.headers) || {};
24
25
  };
25
26
  exports.get_original_response_headers = get_original_response_headers;
26
27
  const is_request_preflight = (ctx) => {
@@ -87,6 +87,28 @@ class ProxyMiddlewareManager {
87
87
  // Figure out a way to enable/disable middleware dynamically
88
88
  // instead of re-initing this again
89
89
  const rules_middleware = new rules_middleware_1.default(this.config[exports.MIDDLEWARE_TYPE.RULES], ctx, this.rulesHelper);
90
+ ctx.onError(function (ctx, err, kind, callback) {
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ // Should only modify response body & headers
93
+ ctx.rq_response_body = "" + kind + ": " + err, "utf8";
94
+ const { action_result_objs, continue_request } = yield rules_middleware.on_response(ctx);
95
+ // Only modify response if any modify_response action is applied
96
+ const modifyResponseActionExist = action_result_objs.some((action_result_obj) => { var _a; return ((_a = action_result_obj === null || action_result_obj === void 0 ? void 0 : action_result_obj.action) === null || _a === void 0 ? void 0 : _a.action) === "modify_response"; });
97
+ if (modifyResponseActionExist) {
98
+ const statusCode = ctx.rq_response_status_code || 404;
99
+ const responseHeaders = (0, proxy_ctx_helper_1.getResponseHeaders)(ctx) || {};
100
+ ctx.proxyToClientResponse.writeHead(statusCode, http_1.default.STATUS_CODES[statusCode], responseHeaders);
101
+ ctx.proxyToClientResponse.end(ctx.rq_response_body);
102
+ ctx.rq.set_final_response({
103
+ status_code: statusCode,
104
+ headers: responseHeaders,
105
+ body: ctx.rq_response_body,
106
+ });
107
+ logger_middleware.send_network_log(ctx, rules_middleware.action_result_objs, requestly_core_1.CONSTANTS.REQUEST_STATE.COMPLETE);
108
+ }
109
+ return callback();
110
+ });
111
+ });
90
112
  let request_body_chunks = [];
91
113
  ctx.onRequestData(function (ctx, chunk, callback) {
92
114
  return __awaiter(this, void 0, void 0, function* () {
@@ -15,9 +15,11 @@ class AmisuingMiddleware {
15
15
  if (!this.is_active) {
16
16
  return true;
17
17
  }
18
- Object.assign(ctx.proxyToServerRequestOptions.headers, {
19
- ["amiusingrequestly"]: "true",
20
- });
18
+ if (ctx.proxyToServerRequestOptions.host === "amiusing.requestly.io") {
19
+ Object.assign(ctx.proxyToServerRequestOptions.headers, {
20
+ ["amiusingrequestly"]: "true",
21
+ });
22
+ }
21
23
  });
22
24
  this.is_active = is_active;
23
25
  }
@@ -7,6 +7,7 @@ const process_modify_header_action = (action, ctx) => {
7
7
  const allowed_handlers = [
8
8
  proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST,
9
9
  proxy_1.PROXY_HANDLER_TYPE.ON_RESPONSE,
10
+ proxy_1.PROXY_HANDLER_TYPE.ON_ERROR,
10
11
  ];
11
12
  if (!allowed_handlers.includes(ctx.currentHandler)) {
12
13
  return (0, utils_1.build_action_processor_response)(action, false);
@@ -14,7 +15,7 @@ const process_modify_header_action = (action, ctx) => {
14
15
  if (ctx.currentHandler == proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST) {
15
16
  modify_request_headers(action, ctx);
16
17
  }
17
- else if (ctx.currentHandler == proxy_1.PROXY_HANDLER_TYPE.ON_RESPONSE) {
18
+ else if (ctx.currentHandler === proxy_1.PROXY_HANDLER_TYPE.ON_RESPONSE || ctx.currentHandler === proxy_1.PROXY_HANDLER_TYPE.ON_ERROR) {
18
19
  modify_response_headers(action, ctx);
19
20
  }
20
21
  return (0, utils_1.build_action_processor_response)(action, true);
@@ -30,6 +31,8 @@ const modify_request_headers = (action, ctx) => {
30
31
  newRequestHeaders.forEach((pair) => (ctx.proxyToServerRequestOptions.headers[pair.name] = pair.value));
31
32
  };
32
33
  const modify_response_headers = (action, ctx) => {
34
+ ctx.serverToProxyResponse = ctx.serverToProxyResponse || {};
35
+ ctx.serverToProxyResponse.headers = ctx.serverToProxyResponse.headers || {};
33
36
  // {"header1":"val1", "header2":"val2"}
34
37
  const originalResponseHeadersObject = ctx.serverToProxyResponse.headers;
35
38
  // ["header1","header2"]
@@ -22,7 +22,7 @@ const capture_console_logs_1 = __importDefault(require("capture-console-logs"));
22
22
  const utils_2 = require("../../../../utils");
23
23
  const { types } = require("util");
24
24
  const process_modify_response_action = (action, ctx) => __awaiter(void 0, void 0, void 0, function* () {
25
- const allowed_handlers = [proxy_1.PROXY_HANDLER_TYPE.ON_RESPONSE_END];
25
+ const allowed_handlers = [proxy_1.PROXY_HANDLER_TYPE.ON_RESPONSE_END, proxy_1.PROXY_HANDLER_TYPE.ON_ERROR];
26
26
  if (!allowed_handlers.includes(ctx.currentHandler)) {
27
27
  return (0, utils_1.build_action_processor_response)(action, false);
28
28
  }
@@ -55,7 +55,7 @@ const modify_response_using_local = (action, ctx) => {
55
55
  }
56
56
  };
57
57
  const modify_response_using_code = (action, ctx) => __awaiter(void 0, void 0, void 0, function* () {
58
- var _a, _b;
58
+ var _a, _b, _c, _d;
59
59
  let userFunction = null;
60
60
  try {
61
61
  userFunction = (0, utils_2.getFunctionFromString)(action.response);
@@ -78,16 +78,16 @@ const modify_response_using_code = (action, ctx) => __awaiter(void 0, void 0, vo
78
78
  ? ctx.clientToProxyRequest.method
79
79
  : null
80
80
  : null,
81
- response: ctx.rq_response_body,
81
+ response: ctx === null || ctx === void 0 ? void 0 : ctx.rq_response_body,
82
82
  url: (0, proxy_ctx_helper_1.get_request_url)(ctx),
83
- responseType: ctx.serverToProxyResponse.headers["content-type"],
83
+ responseType: (_b = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.serverToProxyResponse) === null || _a === void 0 ? void 0 : _a.headers) === null || _b === void 0 ? void 0 : _b["content-type"],
84
84
  requestHeaders: ctx.clientToProxyRequest.headers,
85
- requestData: (0, http_helpers_1.parseJsonBody)((_b = (_a = ctx.rq) === null || _a === void 0 ? void 0 : _a.final_request) === null || _b === void 0 ? void 0 : _b.body) || null,
85
+ requestData: (0, http_helpers_1.parseJsonBody)((_d = (_c = ctx.rq) === null || _c === void 0 ? void 0 : _c.final_request) === null || _d === void 0 ? void 0 : _d.body) || null,
86
86
  };
87
87
  try {
88
88
  args.responseJSON = JSON.parse(args.response);
89
89
  }
90
- catch (_c) {
90
+ catch (_e) {
91
91
  /*Do nothing -- could not parse body as JSON */
92
92
  }
93
93
  const consoleCapture = new capture_console_logs_1.default();
@@ -31,6 +31,7 @@ declare const PROXY_HANDLER_TYPE: {
31
31
  ON_RESPONSE_HEADERS: string;
32
32
  ON_RESPONSE_DATA: string;
33
33
  ON_RESPONSE_END: string;
34
+ ON_ERROR: string;
34
35
  };
35
36
  declare var ProxyFinalRequestFilter: (proxy: any, ctx: any) => void;
36
37
  declare var ProxyFinalResponseFilter: (proxy: any, ctx: any) => any;
@@ -49,6 +49,7 @@ const PROXY_HANDLER_TYPE = {
49
49
  ON_RESPONSE_HEADERS: "ON_RESPONSE_HEADERS",
50
50
  ON_RESPONSE_DATA: "ON_RESPONSE_DATA",
51
51
  ON_RESPONSE_END: "ON_RESPONSE_END",
52
+ ON_ERROR: "ON_ERROR",
52
53
  };
53
54
  module.exports.PROXY_HANDLER_TYPE = PROXY_HANDLER_TYPE;
54
55
  Proxy.prototype.listen = function (options, callback = (e) => { }) {
@@ -561,20 +562,24 @@ Proxy.prototype.onCertificateMissing = function (ctx, files, callback) {
561
562
  return this;
562
563
  };
563
564
  Proxy.prototype._onError = function (kind, ctx, err) {
564
- this.onErrorHandlers.forEach(function (handler) {
565
- return handler(ctx, err, kind);
566
- });
567
565
  if (ctx) {
568
- ctx.onErrorHandlers.forEach(function (handler) {
569
- return handler(ctx, err, kind);
570
- });
571
- if (ctx.proxyToClientResponse && !ctx.proxyToClientResponse.headersSent) {
572
- ctx.proxyToClientResponse.writeHead(504, "Proxy Error");
566
+ ctx.currentHandler = PROXY_HANDLER_TYPE.ON_ERROR;
567
+ }
568
+ async.forEach(this.onErrorHandlers.concat((ctx === null || ctx === void 0 ? void 0 : ctx.onErrorHandlers) || []), function (fn, callback) {
569
+ if (fn) {
570
+ return fn(ctx, err, kind, callback);
573
571
  }
574
- if (ctx.proxyToClientResponse && !ctx.proxyToClientResponse.finished) {
575
- ctx.proxyToClientResponse.end("" + kind + ": " + err, "utf8");
572
+ callback();
573
+ }, function () {
574
+ if (ctx) {
575
+ if (ctx.proxyToClientResponse && !ctx.proxyToClientResponse.headersSent) {
576
+ ctx.proxyToClientResponse.writeHead(504, "Proxy Error");
577
+ }
578
+ if (ctx.proxyToClientResponse && !ctx.proxyToClientResponse.finished) {
579
+ ctx.proxyToClientResponse.end("" + kind + ": " + err, "utf8");
580
+ }
576
581
  }
577
- }
582
+ });
578
583
  };
579
584
  Proxy.prototype._onWebSocketServerConnect = function (isSSL, ws, upgradeReq) {
580
585
  var self = this;
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@requestly/requestly-proxy",
3
- "version": "1.1.15",
3
+ "version": "1.1.17",
4
4
  "description": "Proxy that gives superpowers to all the Requestly clients",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1",
8
8
  "dev": "nodemon src/test.ts",
9
- "build": "tsc"
9
+ "build": "tsc",
10
+ "watch": "tsc --watch"
10
11
  },
11
12
  "repository": {
12
13
  "type": "git",