@requestly/requestly-proxy 1.0.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.
Files changed (89) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +5 -0
  3. package/dist/components/interfaces/logger-service.d.ts +4 -0
  4. package/dist/components/interfaces/logger-service.js +2 -0
  5. package/dist/components/interfaces/rules-data-source.d.ts +6 -0
  6. package/dist/components/interfaces/rules-data-source.js +2 -0
  7. package/dist/components/proxy-middleware/constants.d.ts +11 -0
  8. package/dist/components/proxy-middleware/constants.js +21 -0
  9. package/dist/components/proxy-middleware/helpers/ctx_rq_namespace.d.ts +35 -0
  10. package/dist/components/proxy-middleware/helpers/ctx_rq_namespace.js +96 -0
  11. package/dist/components/proxy-middleware/helpers/harObectCreator.d.ts +140 -0
  12. package/dist/components/proxy-middleware/helpers/harObectCreator.js +158 -0
  13. package/dist/components/proxy-middleware/helpers/http_helpers.d.ts +2 -0
  14. package/dist/components/proxy-middleware/helpers/http_helpers.js +47 -0
  15. package/dist/components/proxy-middleware/helpers/proxy_ctx_helper.d.ts +14 -0
  16. package/dist/components/proxy-middleware/helpers/proxy_ctx_helper.js +86 -0
  17. package/dist/components/proxy-middleware/helpers/response_helper.d.ts +3 -0
  18. package/dist/components/proxy-middleware/helpers/response_helper.js +17 -0
  19. package/dist/components/proxy-middleware/helpers/rule_processor_helper.d.ts +17 -0
  20. package/dist/components/proxy-middleware/helpers/rule_processor_helper.js +192 -0
  21. package/dist/components/proxy-middleware/index.d.ts +25 -0
  22. package/dist/components/proxy-middleware/index.js +257 -0
  23. package/dist/components/proxy-middleware/middlewares/amiusing_middleware.d.ts +6 -0
  24. package/dist/components/proxy-middleware/middlewares/amiusing_middleware.js +25 -0
  25. package/dist/components/proxy-middleware/middlewares/logger_middleware.d.ts +66 -0
  26. package/dist/components/proxy-middleware/middlewares/logger_middleware.js +89 -0
  27. package/dist/components/proxy-middleware/middlewares/rules_middleware.d.ts +40 -0
  28. package/dist/components/proxy-middleware/middlewares/rules_middleware.js +117 -0
  29. package/dist/components/proxy-middleware/middlewares/ssl_cert_middleware.d.ts +7 -0
  30. package/dist/components/proxy-middleware/middlewares/ssl_cert_middleware.js +33 -0
  31. package/dist/components/proxy-middleware/rule_action_processor/handle_mixed_response.d.ts +14 -0
  32. package/dist/components/proxy-middleware/rule_action_processor/handle_mixed_response.js +78 -0
  33. package/dist/components/proxy-middleware/rule_action_processor/index.d.ts +13 -0
  34. package/dist/components/proxy-middleware/rule_action_processor/index.js +101 -0
  35. package/dist/components/proxy-middleware/rule_action_processor/modified_requests_pool.d.ts +7 -0
  36. package/dist/components/proxy-middleware/rule_action_processor/modified_requests_pool.js +34 -0
  37. package/dist/components/proxy-middleware/rule_action_processor/processors/block_processor.d.ts +6 -0
  38. package/dist/components/proxy-middleware/rule_action_processor/processors/block_processor.js +21 -0
  39. package/dist/components/proxy-middleware/rule_action_processor/processors/delay_processor.d.ts +6 -0
  40. package/dist/components/proxy-middleware/rule_action_processor/processors/delay_processor.js +29 -0
  41. package/dist/components/proxy-middleware/rule_action_processor/processors/insert_processor.d.ts +6 -0
  42. package/dist/components/proxy-middleware/rule_action_processor/processors/insert_processor.js +154 -0
  43. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_header_processor.d.ts +6 -0
  44. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_header_processor.js +69 -0
  45. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_request_processor.d.ts +6 -0
  46. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_request_processor.js +89 -0
  47. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_response_processor.d.ts +6 -0
  48. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_response_processor.js +109 -0
  49. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_user_agent_processor.d.ts +6 -0
  50. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_user_agent_processor.js +25 -0
  51. package/dist/components/proxy-middleware/rule_action_processor/processors/redirect_processor.d.ts +6 -0
  52. package/dist/components/proxy-middleware/rule_action_processor/processors/redirect_processor.js +58 -0
  53. package/dist/components/proxy-middleware/rule_action_processor/utils.d.ts +11 -0
  54. package/dist/components/proxy-middleware/rule_action_processor/utils.js +33 -0
  55. package/dist/components/ssl-proxying/ssl-proxying-manager.d.ts +3 -0
  56. package/dist/components/ssl-proxying/ssl-proxying-manager.js +6 -0
  57. package/dist/constants/cert.d.ts +7 -0
  58. package/dist/constants/cert.js +13 -0
  59. package/dist/index.d.ts +4 -0
  60. package/dist/index.js +11 -0
  61. package/dist/lib/proxy/bin/mitm-proxy.d.ts +2 -0
  62. package/dist/lib/proxy/bin/mitm-proxy.js +30 -0
  63. package/dist/lib/proxy/custom/utils/checkInvalidHeaderChar.d.ts +8 -0
  64. package/dist/lib/proxy/custom/utils/checkInvalidHeaderChar.js +14 -0
  65. package/dist/lib/proxy/index.d.ts +2 -0
  66. package/dist/lib/proxy/index.js +2 -0
  67. package/dist/lib/proxy/lib/ca.d.ts +8 -0
  68. package/dist/lib/proxy/lib/ca.js +277 -0
  69. package/dist/lib/proxy/lib/middleware/gunzip.d.ts +2 -0
  70. package/dist/lib/proxy/lib/middleware/gunzip.js +17 -0
  71. package/dist/lib/proxy/lib/middleware/wildcard.d.ts +1 -0
  72. package/dist/lib/proxy/lib/middleware/wildcard.js +20 -0
  73. package/dist/lib/proxy/lib/proxy.d.ts +36 -0
  74. package/dist/lib/proxy/lib/proxy.js +1217 -0
  75. package/dist/rq-proxy-provider.d.ts +10 -0
  76. package/dist/rq-proxy-provider.js +20 -0
  77. package/dist/rq-proxy.d.ts +15 -0
  78. package/dist/rq-proxy.js +53 -0
  79. package/dist/test.d.ts +1 -0
  80. package/dist/test.js +111 -0
  81. package/dist/types/index.d.ts +12 -0
  82. package/dist/types/index.js +2 -0
  83. package/dist/utils/circularQueue.d.ts +2 -0
  84. package/dist/utils/circularQueue.js +31 -0
  85. package/dist/utils/helpers/rules-helper.d.ts +11 -0
  86. package/dist/utils/helpers/rules-helper.js +59 -0
  87. package/dist/utils/index.d.ts +0 -0
  88. package/dist/utils/index.js +0 -0
  89. package/package.json +43 -0
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ const axios = require("axios");
36
+ const parser = require("ua-parser-js");
37
+ const Sentry = __importStar(require("@sentry/browser"));
38
+ const handleMixedResponse = (ctx, destinationUrl) => __awaiter(void 0, void 0, void 0, function* () {
39
+ var _a, _b, _c;
40
+ // Handling mixed response from safari
41
+ let user_agent_str = null;
42
+ user_agent_str = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.clientToProxyRequest) === null || _a === void 0 ? void 0 : _a.headers["user-agent"];
43
+ const user_agent = (_c = (_b = parser(user_agent_str)) === null || _b === void 0 ? void 0 : _b.browser) === null || _c === void 0 ? void 0 : _c.name;
44
+ const LOCAL_DOMAINS = ["localhost", "127.0.0.1"];
45
+ if (ctx.isSSL && destinationUrl.includes("http:")) {
46
+ if (user_agent === "Safari" ||
47
+ !LOCAL_DOMAINS.some((domain) => destinationUrl.includes(domain))) {
48
+ try {
49
+ const resp = yield axios.get(destinationUrl, {
50
+ headers: {
51
+ "Cache-Control": "no-cache",
52
+ },
53
+ });
54
+ return {
55
+ status: true,
56
+ response_data: {
57
+ headers: { "Cache-Control": "no-cache" },
58
+ status_code: 200,
59
+ body: resp.data,
60
+ },
61
+ };
62
+ }
63
+ catch (e) {
64
+ Sentry.captureException(e);
65
+ return {
66
+ status: true,
67
+ response_data: {
68
+ headers: { "Cache-Control": "no-cache" },
69
+ status_code: 502,
70
+ body: e.response ? e.response.data : null,
71
+ },
72
+ };
73
+ }
74
+ }
75
+ }
76
+ return { status: false };
77
+ });
78
+ exports.default = handleMixedResponse;
@@ -0,0 +1,13 @@
1
+ export default RuleActionProcessor;
2
+ declare class RuleActionProcessor {
3
+ process_actions: (rule_actions: any, ctx: any) => Promise<{
4
+ action_result_objs: any[];
5
+ continue_request: boolean;
6
+ }>;
7
+ post_process_actions: (action_result_objs: any, ctx: any) => boolean;
8
+ process_action: (rule_action: any, ctx: any) => Promise<{
9
+ action: any;
10
+ success: boolean;
11
+ post_process_data: any;
12
+ }>;
13
+ }
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const constants_1 = require("../constants");
16
+ const redirect_processor_1 = __importDefault(require("./processors/redirect_processor"));
17
+ const modify_header_processor_1 = __importDefault(require("./processors/modify_header_processor"));
18
+ const modify_user_agent_processor_1 = __importDefault(require("./processors/modify_user_agent_processor"));
19
+ const delay_processor_1 = __importDefault(require("./processors/delay_processor"));
20
+ const block_processor_1 = __importDefault(require("./processors/block_processor"));
21
+ const modify_response_processor_1 = __importDefault(require("./processors/modify_response_processor"));
22
+ const modify_request_processor_1 = __importDefault(require("./processors/modify_request_processor"));
23
+ const insert_processor_1 = __importDefault(require("./processors/insert_processor"));
24
+ const utils_1 = require("./utils");
25
+ class RuleActionProcessor {
26
+ constructor() {
27
+ /*
28
+ @return: Whether to continue with the proxy callback. & of all the continue_request from actions
29
+ */
30
+ this.process_actions = (rule_actions, ctx) => __awaiter(this, void 0, void 0, function* () {
31
+ /*
32
+ action_result_objs = [{action: {}, result: {}}}]
33
+ */
34
+ const action_result_objs = yield Promise.all(rule_actions.map((action) => __awaiter(this, void 0, void 0, function* () {
35
+ let action_result_obj = yield this.process_action(action, ctx);
36
+ return action_result_obj;
37
+ })));
38
+ let continue_request = true;
39
+ continue_request = this.post_process_actions(action_result_objs, ctx);
40
+ return { action_result_objs, continue_request };
41
+ });
42
+ this.post_process_actions = (action_result_objs, ctx) => {
43
+ let continue_request = true;
44
+ action_result_objs.forEach((action_result) => {
45
+ if (!continue_request)
46
+ return; // Already finished the request
47
+ if (!action_result.post_process_data)
48
+ return; // No post processing
49
+ const status_code = action_result.post_process_data.status_code || 200;
50
+ const headers = action_result.post_process_data.headers || {};
51
+ const body = action_result.post_process_data.body || null;
52
+ // console.log("Log", ctx.rq.original_request);
53
+ ctx.proxyToClientResponse.writeHead(status_code, headers).end(body);
54
+ ctx.rq.set_final_response({
55
+ status_code: status_code,
56
+ headers: headers,
57
+ body: body,
58
+ });
59
+ continue_request = false;
60
+ });
61
+ return continue_request;
62
+ };
63
+ /*
64
+ @return: Whether to continue with the proxy callback
65
+ */
66
+ this.process_action = (rule_action, ctx) => __awaiter(this, void 0, void 0, function* () {
67
+ let action_result = (0, utils_1.build_action_processor_response)(rule_action, false);
68
+ if (!rule_action) {
69
+ return action_result;
70
+ }
71
+ switch (rule_action.action) {
72
+ case constants_1.RULE_ACTION.REDIRECT:
73
+ action_result = (0, redirect_processor_1.default)(rule_action, ctx);
74
+ break;
75
+ case constants_1.RULE_ACTION.MODIFY_HEADERS:
76
+ action_result = (0, modify_header_processor_1.default)(rule_action, ctx);
77
+ break;
78
+ case constants_1.RULE_ACTION.MODIFY_USER_AGENT:
79
+ action_result = (0, modify_user_agent_processor_1.default)(rule_action, ctx);
80
+ case constants_1.RULE_ACTION.DELAY:
81
+ action_result = yield (0, delay_processor_1.default)(rule_action, ctx);
82
+ break;
83
+ case constants_1.RULE_ACTION.BLOCK:
84
+ action_result = (0, block_processor_1.default)(rule_action, ctx);
85
+ break;
86
+ case constants_1.RULE_ACTION.MODIFY_REQUEST:
87
+ action_result = (0, modify_request_processor_1.default)(rule_action, ctx);
88
+ break;
89
+ case constants_1.RULE_ACTION.MODIFY_RESPONSE:
90
+ action_result = (0, modify_response_processor_1.default)(rule_action, ctx);
91
+ break;
92
+ case constants_1.RULE_ACTION.INSERT:
93
+ action_result = (0, insert_processor_1.default)(rule_action, ctx);
94
+ default:
95
+ break;
96
+ }
97
+ return action_result;
98
+ });
99
+ }
100
+ }
101
+ exports.default = RuleActionProcessor;
@@ -0,0 +1,7 @@
1
+ export default modifiedRequestsPool;
2
+ declare const modifiedRequestsPool: ModifiedRequestsPool;
3
+ declare class ModifiedRequestsPool {
4
+ queue: any;
5
+ add(url: any): void;
6
+ isURLModified(url: any): boolean;
7
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const circularQueue_1 = __importDefault(require("../../../utils/circularQueue"));
7
+ class ModifiedRequestsPool {
8
+ constructor() {
9
+ this.queue = new circularQueue_1.default(100);
10
+ }
11
+ add(url) {
12
+ url = url.replace(/www\./g, "");
13
+ this.queue.enQueue(url);
14
+ // logger.log(this.queue);
15
+ }
16
+ isURLModified(url) {
17
+ if (!url)
18
+ return false;
19
+ // logger.log("Current Url : ", url);
20
+ // logger.log(JSON.stringify(this.queue), "Looper");
21
+ let tempUrl = url;
22
+ if (url.endsWith("/")) {
23
+ tempUrl = tempUrl.slice(0, tempUrl.length - 1);
24
+ }
25
+ else {
26
+ tempUrl = tempUrl + "/";
27
+ }
28
+ // logger.log(tempUrl);
29
+ return (this.queue.getElementIndex(url) >= 0 ||
30
+ this.queue.getElementIndex(tempUrl) >= 0);
31
+ }
32
+ }
33
+ const modifiedRequestsPool = new ModifiedRequestsPool();
34
+ exports.default = modifiedRequestsPool;
@@ -0,0 +1,6 @@
1
+ export default process_block_action;
2
+ declare function process_block_action(action: any, ctx: any): Promise<{
3
+ action: any;
4
+ success: boolean;
5
+ post_process_data: any;
6
+ }>;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const proxy_1 = require("../../../../lib/proxy");
13
+ const utils_1 = require("../utils");
14
+ const process_block_action = (action, ctx) => __awaiter(void 0, void 0, void 0, function* () {
15
+ const allowed_handlers = [proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST];
16
+ if (!allowed_handlers.includes(ctx.currentHandler)) {
17
+ return (0, utils_1.build_action_processor_response)(action, false);
18
+ }
19
+ return (0, utils_1.build_action_processor_response)(action, true, (0, utils_1.build_post_process_data)(418 /** Move this to temporarily out of coffee (503) if causes issues in someone production use case. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/418 */, { "Cache-Control": "no-cache" }, "Access to this URL has been blocked by Requestly"));
20
+ });
21
+ exports.default = process_block_action;
@@ -0,0 +1,6 @@
1
+ export default process_delay_action;
2
+ declare function process_delay_action(action: any, ctx: any): Promise<{
3
+ action: any;
4
+ success: boolean;
5
+ post_process_data: any;
6
+ }>;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const proxy_1 = require("../../../../lib/proxy");
13
+ const utils_1 = require("../utils");
14
+ const resolveAfterDelay = (durationInMs) => __awaiter(void 0, void 0, void 0, function* () {
15
+ return new Promise((resolve) => {
16
+ setTimeout(() => {
17
+ resolve("resolved");
18
+ }, durationInMs);
19
+ });
20
+ });
21
+ const process_delay_action = (action, ctx) => __awaiter(void 0, void 0, void 0, function* () {
22
+ const allowed_handlers = [proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST];
23
+ if (!allowed_handlers.includes(ctx.currentHandler)) {
24
+ return (0, utils_1.build_action_processor_response)(action, false);
25
+ }
26
+ yield resolveAfterDelay(action.delay);
27
+ return (0, utils_1.build_action_processor_response)(action, true);
28
+ });
29
+ exports.default = process_delay_action;
@@ -0,0 +1,6 @@
1
+ export default process_insert_action;
2
+ declare function process_insert_action(action: any, ctx: any): {
3
+ action: any;
4
+ success: boolean;
5
+ post_process_data: any;
6
+ };
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const proxy_1 = require("../../../../lib/proxy");
4
+ const requestly_core_1 = require("@requestly/requestly-core");
5
+ const response_helper_1 = require("../../helpers/response_helper");
6
+ const utils_1 = require("../utils");
7
+ const process_insert_action = (action, ctx) => {
8
+ const allowed_handlers = [proxy_1.PROXY_HANDLER_TYPE.ON_RESPONSE_END];
9
+ if (!allowed_handlers.includes(ctx.currentHandler)) {
10
+ return (0, utils_1.build_action_processor_response)(action, false);
11
+ }
12
+ const success = insert_scripts(ctx, action);
13
+ return (0, utils_1.build_action_processor_response)(action, success);
14
+ };
15
+ const modify_response = (ctx, new_resp) => {
16
+ ctx.rq_response_body = new_resp;
17
+ };
18
+ const insert_scripts = (ctx, action) => {
19
+ if ((0, response_helper_1.isResponseHTML)(ctx)) {
20
+ const fullResponseBodyString = ctx.rq_response_body;
21
+ // Double check to ensure content is HTML
22
+ if (/<\/?[a-z][\s\S]*>/i.test(fullResponseBodyString)) {
23
+ // Parse Response body (string) to DOM
24
+ let incomingDOM = (0, response_helper_1.parseStringToDOM)(fullResponseBodyString);
25
+ // Modify DOM to inject scripts
26
+ handleCSSScripts(action.data.cssScripts, incomingDOM);
27
+ handleJSLibraries(action.data.libraries, incomingDOM);
28
+ handleJSScripts(action.data.jsScripts, incomingDOM);
29
+ // Parse back DOM to String to forward client as response body
30
+ const final_response_body = (0, response_helper_1.parseDOMToString)(incomingDOM);
31
+ modify_response(ctx, final_response_body);
32
+ return true;
33
+ }
34
+ }
35
+ return false;
36
+ };
37
+ /**
38
+ * Handles all CSS Scripts
39
+ */
40
+ const handleCSSScripts = (cssScripts, incomingDOM) => {
41
+ cssScripts.forEach((script) => {
42
+ includeCSS(script, incomingDOM);
43
+ });
44
+ };
45
+ /**
46
+ * Prepares to inject a CSS Scripts
47
+ * @returns {string} HTML Content
48
+ */
49
+ const includeCSS = (script, incomingDOM) => {
50
+ if (script.type === requestly_core_1.CONSTANTS.SCRIPT_TYPES.URL) {
51
+ addRemoteCSS(script.value, incomingDOM);
52
+ }
53
+ else if (script.type === requestly_core_1.CONSTANTS.SCRIPT_TYPES.CODE) {
54
+ embedCSS(script.value, incomingDOM);
55
+ }
56
+ };
57
+ const addRemoteCSS = (src, incomingDOM) => {
58
+ var link = incomingDOM.createElement("link");
59
+ link.href = src;
60
+ link.type = "text/css";
61
+ link.rel = "stylesheet";
62
+ link.className = getScriptClassAttribute();
63
+ (incomingDOM.head || incomingDOM.documentElement).appendChild(link);
64
+ };
65
+ const getScriptClassAttribute = () => {
66
+ return requestly_core_1.CONSTANTS.PUBLIC_NAMESPACE + "SCRIPT";
67
+ };
68
+ const embedCSS = (css, incomingDOM) => {
69
+ var style = incomingDOM.createElement("style");
70
+ style.type = "text/css";
71
+ style.appendChild(incomingDOM.createTextNode(css));
72
+ style.className = getScriptClassAttribute();
73
+ (incomingDOM.head || incomingDOM.documentElement).appendChild(style);
74
+ };
75
+ const handleJSLibraries = (libraries, incomingDOM) => {
76
+ addLibraries(libraries, null, incomingDOM);
77
+ };
78
+ const addLibraries = (libraries, indexArg, incomingDOM) => {
79
+ var index = indexArg || 0;
80
+ if (index >= libraries.length) {
81
+ return;
82
+ }
83
+ var libraryKey = libraries[index];
84
+ var library = requestly_core_1.CONSTANTS.SCRIPT_LIBRARIES[libraryKey];
85
+ var addNextLibraries = () => {
86
+ addLibraries(libraries, index + 1, incomingDOM);
87
+ };
88
+ if (library) {
89
+ addRemoteJS(library.src, addNextLibraries, incomingDOM);
90
+ }
91
+ else {
92
+ addNextLibraries();
93
+ }
94
+ };
95
+ const addRemoteJS = (src, callback, incomingDOM) => {
96
+ var script = incomingDOM.createElement("script");
97
+ // NOT WORKING
98
+ // if (typeof callback === "function") {
99
+ // script.onload = callback
100
+ // }
101
+ script.type = "text/javascript";
102
+ script.src = src;
103
+ script.className = getScriptClassAttribute();
104
+ (incomingDOM.head || incomingDOM.documentElement).appendChild(script);
105
+ // HOTFIX
106
+ callback();
107
+ };
108
+ const handleJSScripts = (jsScripts, incomingDOM) => {
109
+ var prePageLoadScripts = [];
110
+ var postPageLoadScripts = [];
111
+ jsScripts.forEach((script) => {
112
+ if (script.loadTime === requestly_core_1.CONSTANTS.SCRIPT_LOAD_TIME.BEFORE_PAGE_LOAD) {
113
+ prePageLoadScripts.push(script);
114
+ }
115
+ else {
116
+ postPageLoadScripts.push(script);
117
+ }
118
+ });
119
+ includeJSScriptsInOrder(prePageLoadScripts, () => {
120
+ includeJSScriptsInOrder(postPageLoadScripts, null, null, incomingDOM);
121
+ }, null, incomingDOM);
122
+ };
123
+ const includeJSScriptsInOrder = (scripts, callback, indexArg, incomingDOM) => {
124
+ var index = indexArg || 0;
125
+ if (index >= scripts.length) {
126
+ typeof callback === "function" && callback();
127
+ return;
128
+ }
129
+ includeJS(scripts[index], () => {
130
+ includeJSScriptsInOrder(scripts, callback, index + 1, incomingDOM);
131
+ }, incomingDOM);
132
+ };
133
+ const includeJS = (script, callback, incomingDOM) => {
134
+ if (script.type === requestly_core_1.CONSTANTS.SCRIPT_TYPES.URL) {
135
+ addRemoteJS(script.value, callback, incomingDOM);
136
+ return;
137
+ }
138
+ if (script.type === requestly_core_1.CONSTANTS.SCRIPT_TYPES.CODE) {
139
+ executeJS(script.value, null, incomingDOM);
140
+ }
141
+ typeof callback === "function" && callback();
142
+ };
143
+ const executeJS = (code, shouldRemove, incomingDOM) => {
144
+ const script = incomingDOM.createElement("script");
145
+ script.type = "text/javascript";
146
+ script.className = getScriptClassAttribute();
147
+ script.appendChild(incomingDOM.createTextNode(code));
148
+ const parent = incomingDOM.head || incomingDOM.documentElement;
149
+ parent.appendChild(script);
150
+ if (shouldRemove) {
151
+ parent.removeChild(script);
152
+ }
153
+ };
154
+ exports.default = process_insert_action;
@@ -0,0 +1,6 @@
1
+ export default process_modify_header_action;
2
+ declare function process_modify_header_action(action: any, ctx: any): {
3
+ action: any;
4
+ success: boolean;
5
+ post_process_data: any;
6
+ };
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const proxy_1 = require("../../../../lib/proxy");
4
+ const proxy_ctx_helper_1 = require("../../helpers/proxy_ctx_helper");
5
+ const utils_1 = require("../utils");
6
+ const process_modify_header_action = (action, ctx) => {
7
+ const allowed_handlers = [
8
+ proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST,
9
+ proxy_1.PROXY_HANDLER_TYPE.ON_RESPONSE,
10
+ ];
11
+ if (!allowed_handlers.includes(ctx.currentHandler)) {
12
+ return (0, utils_1.build_action_processor_response)(action, false);
13
+ }
14
+ if (ctx.currentHandler == proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST) {
15
+ modify_request_headers(action, ctx);
16
+ }
17
+ else if (ctx.currentHandler == proxy_1.PROXY_HANDLER_TYPE.ON_RESPONSE) {
18
+ modify_response_headers(action, ctx);
19
+ }
20
+ return (0, utils_1.build_action_processor_response)(action, true);
21
+ };
22
+ const modify_request_headers = (action, ctx) => {
23
+ const newRequestHeaders = action.newHeaders;
24
+ for (var headerName in ctx.proxyToServerRequestOptions.headers) {
25
+ if (ctx.proxyToServerRequestOptions.headers.hasOwnProperty(headerName)) {
26
+ delete ctx.proxyToServerRequestOptions.headers[headerName];
27
+ }
28
+ }
29
+ // Set Request Headers
30
+ newRequestHeaders.forEach((pair) => (ctx.proxyToServerRequestOptions.headers[pair.name] = pair.value));
31
+ };
32
+ const modify_response_headers = (action, ctx) => {
33
+ // {"header1":"val1", "header2":"val2"}
34
+ const originalResponseHeadersObject = ctx.serverToProxyResponse.headers;
35
+ // ["header1","header2"]
36
+ const originalResponseHeadersObjectKeys = Object.keys(originalResponseHeadersObject);
37
+ // [{name:"header1", value:"val1"},{name:"header2", value:"val2"}]
38
+ const originalResponseHeadersObjectKeysValuePairs = originalResponseHeadersObjectKeys.map((key) => {
39
+ return {
40
+ name: key,
41
+ value: originalResponseHeadersObject[key],
42
+ };
43
+ });
44
+ const requestURL = (0, proxy_ctx_helper_1.get_request_url)(ctx);
45
+ const getRequestOrigin = () => {
46
+ // array [{ name: header_name,value: header_val }] -> {headerName1:"value1",headerName2 :"value2"}
47
+ const originalRequestHeadersConvertedObject = Object.assign({}, ...originalRequestHeaders.map((header) => ({
48
+ [header.name]: header.value,
49
+ })));
50
+ if (originalRequestHeadersConvertedObject["Origin"])
51
+ return originalRequestHeadersConvertedObject["Origin"];
52
+ if (originalRequestHeadersConvertedObject["origin"])
53
+ return originalRequestHeadersConvertedObject["origin"];
54
+ if (originalRequestHeadersConvertedObject["ORIGIN"])
55
+ return originalRequestHeadersConvertedObject["ORIGIN"];
56
+ return "*";
57
+ };
58
+ const { newHeaders: newResponseHeaders } = action;
59
+ // Set Response headers
60
+ // Clear all existing Response headers (to handle "remove header" case)
61
+ for (var headerName in ctx.serverToProxyResponse.headers) {
62
+ if (ctx.serverToProxyResponse.headers.hasOwnProperty(headerName)) {
63
+ delete ctx.serverToProxyResponse.headers[headerName];
64
+ }
65
+ }
66
+ // Set new values
67
+ newResponseHeaders.forEach((pair) => (ctx.serverToProxyResponse.headers[pair.name] = pair.value));
68
+ };
69
+ exports.default = process_modify_header_action;
@@ -0,0 +1,6 @@
1
+ export default process_modify_request_action;
2
+ declare function process_modify_request_action(action: any, ctx: any): {
3
+ action: any;
4
+ success: boolean;
5
+ post_process_data: any;
6
+ };
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const proxy_1 = require("../../../../lib/proxy");
13
+ const requestly_core_1 = require("@requestly/requestly-core");
14
+ const proxy_ctx_helper_1 = require("../../helpers/proxy_ctx_helper");
15
+ const utils_1 = require("../utils");
16
+ const { types } = require("util");
17
+ const process_modify_request_action = (action, ctx) => {
18
+ const allowed_handlers = [proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST_END];
19
+ if (!allowed_handlers.includes(ctx.currentHandler)) {
20
+ return (0, utils_1.build_action_processor_response)(action, false);
21
+ }
22
+ if (action.requestType &&
23
+ action.requestType === requestly_core_1.CONSTANTS.REQUEST_BODY_TYPES.CODE) {
24
+ modify_request_using_code(action, ctx);
25
+ return (0, utils_1.build_action_processor_response)(action, true);
26
+ }
27
+ else {
28
+ modify_request(ctx, action.request);
29
+ return (0, utils_1.build_action_processor_response)(action, true);
30
+ }
31
+ };
32
+ const modify_request = (ctx, new_req) => {
33
+ if (new_req)
34
+ ctx.rq_request_body = new_req;
35
+ };
36
+ const modify_request_using_code = (action, ctx) => __awaiter(void 0, void 0, void 0, function* () {
37
+ let userFunction = null;
38
+ try {
39
+ userFunction = requestly_core_1.UTILS.GET_FUNCTION_FROM_STRING(action.request);
40
+ }
41
+ catch (error) {
42
+ // User has provided an invalid function
43
+ return modify_request(ctx, "Can't parse Requestly function. Please recheck. Error Code 7201. Actual Error: " +
44
+ error.message);
45
+ }
46
+ if (!userFunction || typeof userFunction !== "function") {
47
+ // User has provided an invalid function
48
+ return modify_request(ctx, "Can't parse Requestly function. Please recheck. Error Code 944.");
49
+ }
50
+ // Everything good so far. Now try to execute user's function
51
+ let finalRequest = null;
52
+ try {
53
+ const args = {
54
+ method: ctx.clientToProxyRequest
55
+ ? ctx.clientToProxyRequest.method
56
+ ? ctx.clientToProxyRequest.method
57
+ : null
58
+ : null,
59
+ request: ctx.rq_request_body,
60
+ url: (0, proxy_ctx_helper_1.get_request_url)(ctx),
61
+ requestHeaders: ctx.clientToProxyRequest.headers,
62
+ };
63
+ try {
64
+ args.requestJSON = JSON.parse(args.request);
65
+ }
66
+ catch (_a) {
67
+ /*Do nothing -- could not parse body as JSON */
68
+ }
69
+ finalRequest = userFunction(args);
70
+ if (types.isPromise(finalRequest)) {
71
+ finalRequest = yield finalRequest;
72
+ }
73
+ const isRequestJSON = !!args.requestJSON;
74
+ if (typeof finalRequest === "object" && isRequestJSON) {
75
+ finalRequest = JSON.stringify(finalRequest);
76
+ }
77
+ if (finalRequest && typeof finalRequest === "string") {
78
+ return modify_request(ctx, finalRequest);
79
+ }
80
+ else
81
+ throw new Error("Returned value is not a string");
82
+ }
83
+ catch (error) {
84
+ // Function parsed but failed to execute
85
+ return modify_request(ctx, "Can't execute Requestly function. Please recheck. Error Code 187. Actual Error: " +
86
+ error.message);
87
+ }
88
+ });
89
+ exports.default = process_modify_request_action;
@@ -0,0 +1,6 @@
1
+ export default process_modify_response_action;
2
+ declare function process_modify_response_action(action: any, ctx: any): {
3
+ action: any;
4
+ success: boolean;
5
+ post_process_data: any;
6
+ };