@requestly/requestly-proxy 1.2.3 → 1.3.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/dist/components/interfaces/state.d.ts +4 -0
- package/dist/components/interfaces/state.js +2 -0
- package/dist/components/proxy-middleware/index.d.ts +1 -0
- package/dist/components/proxy-middleware/index.js +2 -0
- package/dist/components/proxy-middleware/middlewares/state.d.ts +15 -0
- package/dist/components/proxy-middleware/middlewares/state.js +46 -0
- package/dist/components/proxy-middleware/rule_action_processor/processors/modify_request_processor.js +1 -18
- package/dist/components/proxy-middleware/rule_action_processor/processors/modify_response_processor.js +1 -14
- package/dist/rq-proxy-provider.d.ts +2 -1
- package/dist/rq-proxy-provider.js +2 -2
- package/dist/rq-proxy.d.ts +4 -1
- package/dist/rq-proxy.js +4 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +52 -2
- package/package.json +1 -1
|
@@ -33,9 +33,11 @@ exports.MIDDLEWARE_TYPE = {
|
|
|
33
33
|
RULES: "RULES",
|
|
34
34
|
LOGGER: "LOGGER",
|
|
35
35
|
SSL_CERT: "SSL_CERT",
|
|
36
|
+
GLOBAL_STATE: "GLOBAL_STATE", // SEEMS UNUSUED, BUT ADDING FOR COMPLETENESS
|
|
36
37
|
};
|
|
37
38
|
class ProxyMiddlewareManager {
|
|
38
39
|
constructor(proxy, proxyConfig, rulesHelper, loggerService, sslConfigFetcher) {
|
|
40
|
+
/* NOT USEFUL */
|
|
39
41
|
this.init_config = (config = {}) => {
|
|
40
42
|
Object.keys(exports.MIDDLEWARE_TYPE).map((middleware_key) => {
|
|
41
43
|
this.config[middleware_key] =
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare class State {
|
|
2
|
+
private state;
|
|
3
|
+
constructor(sharedState: Record<string, any>, envVars: Record<string, any>);
|
|
4
|
+
setSharedState(newSharedState: Record<string, any>): void;
|
|
5
|
+
getSharedStateRef(): Record<string, any>;
|
|
6
|
+
getSharedStateCopy(): any;
|
|
7
|
+
setVariables(newVariables: Record<string, any>): void;
|
|
8
|
+
getVariablesRef(): Record<string, any>;
|
|
9
|
+
getVariablesCopy(): any;
|
|
10
|
+
}
|
|
11
|
+
export default class GlobalStateProvider {
|
|
12
|
+
private static instance;
|
|
13
|
+
static initInstance(sharedState?: Record<string, any>, envVars?: Record<string, any>): State;
|
|
14
|
+
static getInstance(): State;
|
|
15
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.State = void 0;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
class State {
|
|
6
|
+
constructor(sharedState, envVars) {
|
|
7
|
+
this.state = {
|
|
8
|
+
variables: envVars,
|
|
9
|
+
sharedState,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
setSharedState(newSharedState) {
|
|
13
|
+
this.state.sharedState = newSharedState;
|
|
14
|
+
}
|
|
15
|
+
getSharedStateRef() {
|
|
16
|
+
return this.state.sharedState;
|
|
17
|
+
}
|
|
18
|
+
getSharedStateCopy() {
|
|
19
|
+
return (0, lodash_1.cloneDeep)(this.state.sharedState);
|
|
20
|
+
}
|
|
21
|
+
setVariables(newVariables) {
|
|
22
|
+
this.state.variables = newVariables;
|
|
23
|
+
}
|
|
24
|
+
getVariablesRef() {
|
|
25
|
+
return this.state.variables;
|
|
26
|
+
}
|
|
27
|
+
getVariablesCopy() {
|
|
28
|
+
return (0, lodash_1.cloneDeep)(this.state.variables);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.State = State;
|
|
32
|
+
class GlobalStateProvider {
|
|
33
|
+
static initInstance(sharedState = {}, envVars = {}) {
|
|
34
|
+
if (!GlobalStateProvider.instance) {
|
|
35
|
+
GlobalStateProvider.instance = new State(sharedState !== null && sharedState !== void 0 ? sharedState : {}, envVars !== null && envVars !== void 0 ? envVars : {});
|
|
36
|
+
}
|
|
37
|
+
return GlobalStateProvider.instance;
|
|
38
|
+
}
|
|
39
|
+
static getInstance() {
|
|
40
|
+
if (!GlobalStateProvider.instance) {
|
|
41
|
+
console.error("[GlobalStateProvider]", "Init first");
|
|
42
|
+
}
|
|
43
|
+
return GlobalStateProvider.instance;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.default = GlobalStateProvider;
|
|
@@ -8,17 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
12
|
const proxy_1 = require("../../../../lib/proxy");
|
|
16
13
|
const requestly_core_1 = require("@requestly/requestly-core");
|
|
17
14
|
const proxy_ctx_helper_1 = require("../../helpers/proxy_ctx_helper");
|
|
18
15
|
const utils_1 = require("../utils");
|
|
19
|
-
const capture_console_logs_1 = __importDefault(require("capture-console-logs"));
|
|
20
16
|
const utils_2 = require("../../../../utils");
|
|
21
|
-
const { types } = require("util");
|
|
22
17
|
const process_modify_request_action = (action, ctx) => {
|
|
23
18
|
const allowed_handlers = [proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST_END];
|
|
24
19
|
if (!allowed_handlers.includes(ctx.currentHandler)) {
|
|
@@ -72,19 +67,7 @@ const modify_request_using_code = (action, ctx) => __awaiter(void 0, void 0, voi
|
|
|
72
67
|
catch (_a) {
|
|
73
68
|
/*Do nothing -- could not parse body as JSON */
|
|
74
69
|
}
|
|
75
|
-
|
|
76
|
-
consoleCapture.start(true);
|
|
77
|
-
finalRequest = userFunction(args);
|
|
78
|
-
if (types.isPromise(finalRequest)) {
|
|
79
|
-
finalRequest = yield finalRequest;
|
|
80
|
-
}
|
|
81
|
-
consoleCapture.stop();
|
|
82
|
-
const consoleLogs = consoleCapture.getCaptures();
|
|
83
|
-
ctx.rq.consoleLogs.push(...consoleLogs);
|
|
84
|
-
const isRequestJSON = !!args.bodyAsJson;
|
|
85
|
-
if (typeof finalRequest === "object" && isRequestJSON) {
|
|
86
|
-
finalRequest = JSON.stringify(finalRequest);
|
|
87
|
-
}
|
|
70
|
+
finalRequest = yield (0, utils_2.executeUserFunction)(ctx, userFunction, args);
|
|
88
71
|
if (finalRequest && typeof finalRequest === "string") {
|
|
89
72
|
return modify_request(ctx, finalRequest);
|
|
90
73
|
}
|
|
@@ -18,10 +18,8 @@ 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 http_helpers_1 = require("../../helpers/http_helpers");
|
|
21
|
-
const capture_console_logs_1 = __importDefault(require("capture-console-logs"));
|
|
22
21
|
const utils_2 = require("../../../../utils");
|
|
23
22
|
const constants_1 = require("../../constants");
|
|
24
|
-
const { types } = require("util");
|
|
25
23
|
const process_modify_response_action = (action, ctx) => __awaiter(void 0, void 0, void 0, function* () {
|
|
26
24
|
const allowed_handlers = [proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST, proxy_1.PROXY_HANDLER_TYPE.ON_RESPONSE_END, proxy_1.PROXY_HANDLER_TYPE.ON_ERROR];
|
|
27
25
|
if (!allowed_handlers.includes(ctx.currentHandler)) {
|
|
@@ -125,18 +123,7 @@ const modify_response_using_code = (action, ctx) => __awaiter(void 0, void 0, vo
|
|
|
125
123
|
catch (_f) {
|
|
126
124
|
/*Do nothing -- could not parse body as JSON */
|
|
127
125
|
}
|
|
128
|
-
|
|
129
|
-
consoleCapture.start(true);
|
|
130
|
-
finalResponse = userFunction(args);
|
|
131
|
-
if (types.isPromise(finalResponse)) {
|
|
132
|
-
finalResponse = yield finalResponse;
|
|
133
|
-
}
|
|
134
|
-
consoleCapture.stop();
|
|
135
|
-
const consoleLogs = consoleCapture.getCaptures();
|
|
136
|
-
ctx.rq.consoleLogs.push(...consoleLogs);
|
|
137
|
-
if (typeof finalResponse === "object") {
|
|
138
|
-
finalResponse = JSON.stringify(finalResponse);
|
|
139
|
-
}
|
|
126
|
+
finalResponse = yield (0, utils_2.executeUserFunction)(ctx, action.response, args);
|
|
140
127
|
if (finalResponse && typeof finalResponse === "string") {
|
|
141
128
|
return modify_response(ctx, finalResponse, action.statusCode);
|
|
142
129
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import RQProxy from "./rq-proxy";
|
|
2
2
|
import ILoggerService from "./components/interfaces/logger-service";
|
|
3
3
|
import IRulesDataSource from "./components/interfaces/rules-data-source";
|
|
4
|
+
import IInitialState from "./components/interfaces/state";
|
|
4
5
|
import { ProxyConfig } from "./types";
|
|
5
6
|
declare class RQProxyProvider {
|
|
6
7
|
static rqProxyInstance: any;
|
|
7
|
-
static createInstance: (proxyConfig: ProxyConfig, rulesDataSource: IRulesDataSource, loggerService: ILoggerService) => void;
|
|
8
|
+
static createInstance: (proxyConfig: ProxyConfig, rulesDataSource: IRulesDataSource, loggerService: ILoggerService, initialGlobalState?: IInitialState) => void;
|
|
8
9
|
static getInstance: () => RQProxy;
|
|
9
10
|
}
|
|
10
11
|
export default RQProxyProvider;
|
|
@@ -8,8 +8,8 @@ class RQProxyProvider {
|
|
|
8
8
|
}
|
|
9
9
|
RQProxyProvider.rqProxyInstance = null;
|
|
10
10
|
// TODO: rulesDataSource can be static here
|
|
11
|
-
RQProxyProvider.createInstance = (proxyConfig, rulesDataSource, loggerService) => {
|
|
12
|
-
RQProxyProvider.rqProxyInstance = new rq_proxy_1.default(proxyConfig, rulesDataSource, loggerService);
|
|
11
|
+
RQProxyProvider.createInstance = (proxyConfig, rulesDataSource, loggerService, initialGlobalState) => {
|
|
12
|
+
RQProxyProvider.rqProxyInstance = new rq_proxy_1.default(proxyConfig, rulesDataSource, loggerService, initialGlobalState);
|
|
13
13
|
};
|
|
14
14
|
RQProxyProvider.getInstance = () => {
|
|
15
15
|
if (!RQProxyProvider.rqProxyInstance) {
|
package/dist/rq-proxy.d.ts
CHANGED
|
@@ -3,12 +3,15 @@ import { ProxyConfig } from "./types";
|
|
|
3
3
|
import RulesHelper from "./utils/helpers/rules-helper";
|
|
4
4
|
import ProxyMiddlewareManager from "./components/proxy-middleware";
|
|
5
5
|
import ILoggerService from "./components/interfaces/logger-service";
|
|
6
|
+
import IInitialState from "./components/interfaces/state";
|
|
7
|
+
import { State } from "./components/proxy-middleware/middlewares/state";
|
|
6
8
|
declare class RQProxy {
|
|
7
9
|
proxy: any;
|
|
8
10
|
proxyMiddlewareManager: ProxyMiddlewareManager;
|
|
9
11
|
rulesHelper: RulesHelper;
|
|
10
12
|
loggerService: ILoggerService;
|
|
11
|
-
|
|
13
|
+
globalState: State;
|
|
14
|
+
constructor(proxyConfig: ProxyConfig, rulesDataSource: IRulesDataSource, loggerService: ILoggerService, initialGlobalState?: IInitialState);
|
|
12
15
|
initProxy: (proxyConfig: ProxyConfig) => void;
|
|
13
16
|
doSomething: () => void;
|
|
14
17
|
}
|
package/dist/rq-proxy.js
CHANGED
|
@@ -6,8 +6,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const proxy_1 = __importDefault(require("./lib/proxy"));
|
|
7
7
|
const rules_helper_1 = __importDefault(require("./utils/helpers/rules-helper"));
|
|
8
8
|
const proxy_middleware_1 = __importDefault(require("./components/proxy-middleware"));
|
|
9
|
+
const state_1 = __importDefault(require("./components/proxy-middleware/middlewares/state"));
|
|
9
10
|
class RQProxy {
|
|
10
|
-
constructor(proxyConfig, rulesDataSource, loggerService) {
|
|
11
|
+
constructor(proxyConfig, rulesDataSource, loggerService, initialGlobalState) {
|
|
12
|
+
var _a, _b;
|
|
11
13
|
this.initProxy = (proxyConfig) => {
|
|
12
14
|
console.log("initProxy");
|
|
13
15
|
console.log(proxyConfig);
|
|
@@ -51,6 +53,7 @@ class RQProxy {
|
|
|
51
53
|
this.initProxy(proxyConfig);
|
|
52
54
|
this.rulesHelper = new rules_helper_1.default(rulesDataSource);
|
|
53
55
|
this.loggerService = loggerService;
|
|
56
|
+
this.globalState = state_1.default.initInstance((_a = initialGlobalState === null || initialGlobalState === void 0 ? void 0 : initialGlobalState.sharedState) !== null && _a !== void 0 ? _a : {}, (_b = initialGlobalState === null || initialGlobalState === void 0 ? void 0 : initialGlobalState.variables) !== null && _b !== void 0 ? _b : {});
|
|
54
57
|
}
|
|
55
58
|
}
|
|
56
59
|
exports.default = RQProxy;
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -1,7 +1,57 @@
|
|
|
1
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
|
+
};
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getFunctionFromString = void 0;
|
|
15
|
+
exports.executeUserFunction = exports.getFunctionFromString = void 0;
|
|
16
|
+
const util_1 = require("util");
|
|
17
|
+
const capture_console_logs_1 = __importDefault(require("capture-console-logs"));
|
|
18
|
+
const state_1 = __importDefault(require("../components/proxy-middleware/middlewares/state"));
|
|
19
|
+
// Only used for verification now. For execution, we regenerate the function in executeUserFunction with the sharedState
|
|
4
20
|
const getFunctionFromString = function (functionStringEscaped) {
|
|
5
|
-
return new Function(
|
|
21
|
+
return new Function(`return ${functionStringEscaped}`)();
|
|
6
22
|
};
|
|
7
23
|
exports.getFunctionFromString = getFunctionFromString;
|
|
24
|
+
/* Expects that the functionString has already been validated to be representing a proper function */
|
|
25
|
+
function executeUserFunction(ctx, functionString, args) {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
const generateFunctionWithSharedState = function (functionStringEscaped) {
|
|
28
|
+
const SHARED_STATE_VAR_NAME = "$sharedState";
|
|
29
|
+
const sharedState = state_1.default.getInstance().getSharedStateCopy();
|
|
30
|
+
return new Function(`${SHARED_STATE_VAR_NAME}`, `return { func: ${functionStringEscaped}, updatedSharedState: ${SHARED_STATE_VAR_NAME}}`)(sharedState);
|
|
31
|
+
};
|
|
32
|
+
const { func: generatedFunction, updatedSharedState } = generateFunctionWithSharedState(functionString);
|
|
33
|
+
const consoleCapture = new capture_console_logs_1.default();
|
|
34
|
+
consoleCapture.start(true);
|
|
35
|
+
let finalResponse = generatedFunction(args);
|
|
36
|
+
if (util_1.types.isPromise(finalResponse)) {
|
|
37
|
+
finalResponse = yield finalResponse;
|
|
38
|
+
}
|
|
39
|
+
consoleCapture.stop();
|
|
40
|
+
const consoleLogs = consoleCapture.getCaptures();
|
|
41
|
+
ctx.rq.consoleLogs.push(...consoleLogs);
|
|
42
|
+
/**
|
|
43
|
+
* If we use GlobalState.getSharedStateRef instead of GlobalState.getSharedStateCopy
|
|
44
|
+
* then this update is completely unnecessary.
|
|
45
|
+
* Because then the function gets a reference to the global states,
|
|
46
|
+
* and any changes made inside the userFunction will directly be reflected there.
|
|
47
|
+
*
|
|
48
|
+
* But we are using it here to make the data flow obvious as we read this code.
|
|
49
|
+
*/
|
|
50
|
+
state_1.default.getInstance().setSharedState(updatedSharedState);
|
|
51
|
+
if (typeof finalResponse === "object") {
|
|
52
|
+
finalResponse = JSON.stringify(finalResponse);
|
|
53
|
+
}
|
|
54
|
+
return finalResponse;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
exports.executeUserFunction = executeUserFunction;
|