@requestly/requestly-proxy 1.1.3 → 1.1.5

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.
@@ -16,6 +16,7 @@ exports.RQ_INTERCEPTED_CONTENT_TYPES = [
16
16
  "text/plain",
17
17
  "text/javascript",
18
18
  "application/javascript",
19
+ "application/x-javascript",
19
20
  "text/css",
20
21
  "application/css",
21
22
  "application/json",
@@ -90,7 +90,7 @@ class RuleActionProcessor {
90
90
  action_result = (0, modify_request_processor_1.default)(rule_action, ctx);
91
91
  break;
92
92
  case constants_1.RULE_ACTION.MODIFY_RESPONSE:
93
- action_result = (0, modify_response_processor_1.default)(rule_action, ctx);
93
+ action_result = yield (0, modify_response_processor_1.default)(rule_action, ctx);
94
94
  break;
95
95
  case constants_1.RULE_ACTION.INSERT:
96
96
  action_result = (0, insert_processor_1.default)(rule_action, ctx);
@@ -1,6 +1,6 @@
1
1
  export default process_modify_response_action;
2
- declare function process_modify_response_action(action: any, ctx: any): {
2
+ declare function process_modify_response_action(action: any, ctx: any): Promise<{
3
3
  action: any;
4
4
  success: boolean;
5
5
  post_process_data: any;
6
- };
6
+ }>;
@@ -18,14 +18,14 @@ const proxy_ctx_helper_1 = require("../../helpers/proxy_ctx_helper");
18
18
  const utils_1 = require("../utils");
19
19
  const fs_1 = __importDefault(require("fs"));
20
20
  const { types } = require("util");
21
- const process_modify_response_action = (action, ctx) => {
21
+ const process_modify_response_action = (action, ctx) => __awaiter(void 0, void 0, void 0, function* () {
22
22
  const allowed_handlers = [proxy_1.PROXY_HANDLER_TYPE.ON_RESPONSE_END];
23
23
  if (!allowed_handlers.includes(ctx.currentHandler)) {
24
24
  return (0, utils_1.build_action_processor_response)(action, false);
25
25
  }
26
26
  if (action.responseType &&
27
27
  action.responseType === requestly_core_1.CONSTANTS.RESPONSE_BODY_TYPES.CODE) {
28
- modify_response_using_code(action, ctx);
28
+ yield modify_response_using_code(action, ctx);
29
29
  return (0, utils_1.build_action_processor_response)(action, true);
30
30
  }
31
31
  else if (action.responseType === requestly_core_1.CONSTANTS.RESPONSE_BODY_TYPES.LOCAL_FILE) {
@@ -36,7 +36,7 @@ const process_modify_response_action = (action, ctx) => {
36
36
  modify_response(ctx, action.response, action.statusCode);
37
37
  return (0, utils_1.build_action_processor_response)(action, true);
38
38
  }
39
- };
39
+ });
40
40
  const modify_response = (ctx, new_resp, status_code) => {
41
41
  ctx.rq_response_body = new_resp;
42
42
  ctx.rq_response_status_code = status_code;
@@ -90,8 +90,7 @@ const modify_response_using_code = (action, ctx) => __awaiter(void 0, void 0, vo
90
90
  if (types.isPromise(finalResponse)) {
91
91
  finalResponse = yield finalResponse;
92
92
  }
93
- const isResponseJSON = args.responseType && args.responseType.includes("application/json");
94
- if (typeof finalResponse === "object" && isResponseJSON) {
93
+ if (typeof finalResponse === "object") {
95
94
  finalResponse = JSON.stringify(finalResponse);
96
95
  }
97
96
  if (finalResponse && typeof finalResponse === "string") {
@@ -15,7 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const proxy_1 = require("../../../../lib/proxy");
16
16
  const proxy_ctx_helper_1 = require("../../helpers/proxy_ctx_helper");
17
17
  const modified_requests_pool_1 = __importDefault(require("../modified_requests_pool"));
18
- const redirectHelper_1 = require("../../helpers/redirectHelper");
18
+ const handle_mixed_response_1 = __importDefault(require("../handle_mixed_response"));
19
19
  const utils_1 = require("../utils");
20
20
  const { URL } = require("url");
21
21
  // adding util to get origin header for handling cors
@@ -41,12 +41,9 @@ const process_redirect_action = (action, ctx) => __awaiter(void 0, void 0, void
41
41
  else {
42
42
  modified_requests_pool_1.default.add(new_url);
43
43
  }
44
- // handle mixed content and redirect with preserve cookie
45
- if ((0, redirectHelper_1.shouldMakeExternalRequest)(ctx, action)) {
46
- const { status: wasExternalRequestSuccessful, responseData } = yield (0, redirectHelper_1.makeExternalRequest)(ctx, new_url);
47
- if (wasExternalRequestSuccessful) {
48
- return (0, utils_1.build_action_processor_response)(action, true, (0, utils_1.build_post_process_data)(responseData.status_code, responseData.headers, responseData.body));
49
- }
44
+ const { status: isMixedResponse, response_data } = yield (0, handle_mixed_response_1.default)(ctx, new_url);
45
+ if (isMixedResponse) {
46
+ return (0, utils_1.build_action_processor_response)(action, true, (0, utils_1.build_post_process_data)(response_data.status_code, response_data.headers, response_data.body));
50
47
  }
51
48
  // If this is a pre-flight request, don't redirect it
52
49
  if ((0, proxy_ctx_helper_1.is_request_preflight)(ctx))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@requestly/requestly-proxy",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "description": "Proxy that gives superpowers to all the Requestly clients",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -1,9 +0,0 @@
1
- export function shouldMakeExternalRequest(ctx: any, action: any): boolean;
2
- export function makeExternalRequest(ctx: any, url: any): Promise<{
3
- status: boolean;
4
- responseData: {
5
- headers: any;
6
- status_code: any;
7
- body: any;
8
- };
9
- }>;
@@ -1,166 +0,0 @@
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
- var __importDefault = (this && this.__importDefault) || function (mod) {
35
- return (mod && mod.__esModule) ? mod : { "default": mod };
36
- };
37
- Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.makeExternalRequest = exports.shouldMakeExternalRequest = void 0;
39
- const parser = require("ua-parser-js");
40
- const Sentry = __importStar(require("@sentry/browser"));
41
- const https_1 = __importDefault(require("https"));
42
- const http_1 = __importDefault(require("http"));
43
- const willCreateMixedResponseThatCanBeHandled = (ctx, destinationUrl) => {
44
- var _a, _b, _c;
45
- let user_agent_str = null;
46
- user_agent_str = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.clientToProxyRequest) === null || _a === void 0 ? void 0 : _a.headers["user-agent"];
47
- 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;
48
- const LOCAL_DOMAINS = ["localhost", "127.0.0.1"];
49
- return ctx.isSSL && destinationUrl.includes("http:") && (user_agent === "Safari" ||
50
- !LOCAL_DOMAINS.some((domain) => destinationUrl.includes(domain)));
51
- };
52
- const canPreserveCookie = (ctx, destinationUrl) => {
53
- /* DOES NOT WORK */
54
- // CAN'T DIFFERENTIATE CALL TO TOP LEVEL DOCUMENT
55
- // // had to create this because host and referer were sometimes same
56
- // // but had additional prefixes like `/`
57
- // // note: neither referer nor host have search params
58
- // const areUrlsSame = (u1, u2) => {
59
- // try {
60
- // let u1obj = new URL(u1)
61
- // let u2obj = new URL(u2)
62
- // return u1obj.href === u2obj.href
63
- // } catch {
64
- // // when url objects were not properly formed
65
- // return false
66
- // }
67
- // }
68
- // const requestHeaders = ctx?.clientToProxyRequest?.headers;
69
- // const referer = requestHeaders["referer"] || requestHeaders["Referer"]
70
- // const origin = requestHeaders["origin"] || requestHeaders["Origin"]
71
- // // cannot preserve cookie on request for toplevel document
72
- // if (
73
- // // navigating to the domain directly from the search bar
74
- // !referer ||
75
- // // when navigating using google search result
76
- // // referrer is present but origin is not
77
- // !origin || // bug: origin not passed by firefox in some cases
78
- // !areUrlsSame(referer, origin)
79
- // ) return false
80
- return true;
81
- };
82
- const shouldMakeExternalRequest = (ctx, action) => {
83
- return ((action.preserveCookie && canPreserveCookie(ctx, action.url)) ||
84
- willCreateMixedResponseThatCanBeHandled(ctx, action.url));
85
- };
86
- exports.shouldMakeExternalRequest = shouldMakeExternalRequest;
87
- function makeRequest(requestOptions) {
88
- return __awaiter(this, void 0, void 0, function* () {
89
- let requestAgent = http_1.default;
90
- if (requestOptions.url.includes("https"))
91
- requestAgent = https_1.default;
92
- requestOptions.headers["Cache-Control"] = "no-cache";
93
- try {
94
- const { data, response } = yield new Promise((resolve, reject) => {
95
- // `.request` wasn't working well with the provided request options
96
- // node only provides wrapper for get, hence did not implement other methods
97
- if (requestOptions.method === "GET") {
98
- let request = requestAgent.get(requestOptions.url, requestOptions, (res) => {
99
- const dataBuffers = [];
100
- res.on('data', (buffer) => {
101
- dataBuffers.push(buffer);
102
- });
103
- res.on('end', () => {
104
- resolve({ data: Buffer.concat(dataBuffers).toString(), response: res });
105
- });
106
- });
107
- request.on('error', (error) => {
108
- console.log("requestError", requestOptions);
109
- console.error(error);
110
- reject(error);
111
- });
112
- }
113
- else {
114
- // hack: to return an understandable response back to user
115
- // @nsr fix: implement all other methods using some workaround of http.request bug
116
- const errMsg = "Can only preserve cookies for get requests";
117
- const customError = new Error(errMsg);
118
- customError.response = { data: errMsg };
119
- throw customError;
120
- }
121
- });
122
- return { success: true, response, data };
123
- }
124
- catch (error) {
125
- Sentry.captureException(error);
126
- console.error(error);
127
- return { success: false, error };
128
- }
129
- });
130
- }
131
- const makeExternalRequest = (ctx, url) => __awaiter(void 0, void 0, void 0, function* () {
132
- const requestOptions = ctx.proxyToServerRequestOptions;
133
- // can't pass all request options because they o
134
- // verride some attrubutes of node http request options
135
- // in the wrong way
136
- let finalRequestOptions = {
137
- headers: Object.assign({}, requestOptions.headers),
138
- url,
139
- method: requestOptions.method
140
- };
141
- if (url.includes("https")) {
142
- finalRequestOptions = Object.assign(Object.assign({}, finalRequestOptions), { rejectUnauthorized: false, requestCert: true, agent: false, strictSSL: false });
143
- }
144
- const { success, error, response, data } = yield makeRequest(finalRequestOptions);
145
- if (success) {
146
- return {
147
- status: true,
148
- responseData: {
149
- headers: Object.assign(Object.assign({}, response.headers), { "connection": "close", "Cache-Control": "no-cache" }),
150
- status_code: response.statuscode,
151
- body: data,
152
- },
153
- };
154
- }
155
- else {
156
- return {
157
- status: true,
158
- responseData: {
159
- headers: { "Cache-Control": "no-cache" },
160
- status_code: 502,
161
- body: error.response ? error.response.data : null,
162
- },
163
- };
164
- }
165
- });
166
- exports.makeExternalRequest = makeExternalRequest;
@@ -1,22 +0,0 @@
1
- export function handleMixedResponse(ctx: any, destinationUrl: any): Promise<{
2
- status: boolean;
3
- response_data: {
4
- headers: any;
5
- status_code: any;
6
- body: any;
7
- };
8
- } | {
9
- status: boolean;
10
- response_data?: undefined;
11
- }>;
12
- export function handleServerSideRedirect(ctx: any, destinationUrl: any): Promise<{
13
- status: boolean;
14
- response_data: {
15
- headers: any;
16
- status_code: any;
17
- body: any;
18
- };
19
- } | {
20
- status: boolean;
21
- response_data?: undefined;
22
- }>;
@@ -1,149 +0,0 @@
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
- var __importDefault = (this && this.__importDefault) || function (mod) {
35
- return (mod && mod.__esModule) ? mod : { "default": mod };
36
- };
37
- Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.handleServerSideRedirect = exports.handleMixedResponse = void 0;
39
- const parser = require("ua-parser-js");
40
- const Sentry = __importStar(require("@sentry/browser"));
41
- const https_1 = __importDefault(require("https"));
42
- function makeSameRequestToDifferentUrl(ctx, url) {
43
- var _a;
44
- return __awaiter(this, void 0, void 0, function* () {
45
- try {
46
- const requestOptions = {
47
- headers: Object.assign(Object.assign({}, (_a = ctx === null || ctx === void 0 ? void 0 : ctx.clientToProxyRequest) === null || _a === void 0 ? void 0 : _a.headers), { "Cache-Control": "no-cache" }),
48
- rejectUnauthorized: false,
49
- requestCert: true,
50
- agent: false,
51
- strictSSL: false,
52
- };
53
- const { data, response } = yield new Promise((resolve, reject) => {
54
- let request = https_1.default.get(url, requestOptions, (res) => {
55
- const dataBuffers = [];
56
- res.on('data', (buffer) => {
57
- dataBuffers.push(buffer);
58
- });
59
- res.on('end', () => {
60
- resolve({ data: Buffer.concat(dataBuffers).toString(), response: res });
61
- });
62
- });
63
- request.on('error', (error) => {
64
- console.error(error);
65
- reject(error);
66
- });
67
- });
68
- return { success: true, response, data };
69
- }
70
- catch (error) {
71
- Sentry.captureException(error);
72
- console.error(error);
73
- return { success: false, error };
74
- }
75
- });
76
- }
77
- const willCreateMixedResponse = (ctx, destinationUrl) => {
78
- var _a, _b, _c, _d;
79
- let user_agent_str = null;
80
- user_agent_str = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.clientToProxyRequest) === null || _a === void 0 ? void 0 : _a.headers["user-agent"];
81
- console.log("handling mixed response. i got headers", (_b = ctx === null || ctx === void 0 ? void 0 : ctx.clientToProxyRequest) === null || _b === void 0 ? void 0 : _b.headers);
82
- const user_agent = (_d = (_c = parser(user_agent_str)) === null || _c === void 0 ? void 0 : _c.browser) === null || _d === void 0 ? void 0 : _d.name;
83
- const LOCAL_DOMAINS = ["localhost", "127.0.0.1"];
84
- return ctx.isSSL && (user_agent === "Safari" ||
85
- !LOCAL_DOMAINS.some((domain) => destinationUrl.includes(domain)));
86
- };
87
- const handleMixedResponse = (ctx, destinationUrl) => __awaiter(void 0, void 0, void 0, function* () {
88
- if (willCreateMixedResponse(ctx, destinationUrl)) {
89
- const { success, error, response, data } = yield makeSameRequestToDifferentUrl(ctx, destinationUrl);
90
- if (success) {
91
- return {
92
- status: true,
93
- response_data: {
94
- headers: Object.assign(Object.assign({}, response.headers), { "connection": "close", "Cache-Control": "no-cache" }),
95
- status_code: response.statuscode,
96
- body: data,
97
- },
98
- };
99
- }
100
- else {
101
- return {
102
- status: true,
103
- response_data: {
104
- headers: { "Cache-Control": "no-cache" },
105
- status_code: 502,
106
- body: error.response ? error.responserror.data : null,
107
- },
108
- };
109
- }
110
- }
111
- return { status: false };
112
- });
113
- exports.handleMixedResponse = handleMixedResponse;
114
- const canMakeServerSideRedirect = (ctx, destinationUrl) => {
115
- // todo: no redirects for html documents
116
- // content-type
117
- // will be handled by mixed content case
118
- if (destinationUrl.includes("http"))
119
- return false;
120
- return true;
121
- };
122
- const handleServerSideRedirect = (ctx, destinationUrl) => __awaiter(void 0, void 0, void 0, function* () {
123
- if (canMakeServerSideRedirect(ctx, destinationUrl)) {
124
- const { success, error, response, data } = yield makeSameRequestToDifferentUrl(ctx, destinationUrl);
125
- if (success) {
126
- return {
127
- status: true,
128
- response_data: {
129
- headers: Object.assign(Object.assign({}, response.headers), { "connection": "close", "Cache-Control": "no-cache" }),
130
- status_code: response.statuscode,
131
- body: data,
132
- },
133
- };
134
- }
135
- else {
136
- return {
137
- status: true,
138
- response_data: {
139
- headers: { "Cache-Control": "no-cache" },
140
- status_code: 502,
141
- body: error.response ? error.responserror.data : null,
142
- },
143
- };
144
- }
145
- }
146
- return { status: false };
147
- });
148
- exports.handleServerSideRedirect = handleServerSideRedirect;
149
- // export default handleMixedResponse; // change exports