Skip to content

Commit da53ee2

Browse files
authored
fix: serve without hitting server for modfy graphql response (#64)
1 parent ab4b510 commit da53ee2

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

src/components/proxy-middleware/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,15 @@ class ProxyMiddlewareManager {
157157
headers: responseHeaders,
158158
body: ctx.rq_response_body,
159159
});
160-
logger_middleware.send_network_log(
160+
if(!ctx.rq.request_finished) {
161+
logger_middleware.send_network_log(
161162
ctx,
162163
rules_middleware.action_result_objs,
163164
GLOBAL_CONSTANTS.REQUEST_STATE.COMPLETE
164165
)
166+
} else {
167+
console.log("Expected Error after early termination of request: ", err);
168+
}
165169
}
166170

167171
return callback();
@@ -188,8 +192,15 @@ class ProxyMiddlewareManager {
188192

189193
if (parsedBody && RQ_INTERCEPTED_CONTENT_TYPES.includes(contentType)) {
190194
// Do modifications, if any
191-
const { action_result_objs, continue_request } =
192-
await rules_middleware.on_request_end(ctx);
195+
const { action_result_objs, continue_request } = await rules_middleware.on_request_end(ctx);
196+
if(!continue_request) {
197+
logger_middleware.send_network_log(
198+
ctx,
199+
rules_middleware.action_result_objs,
200+
GLOBAL_CONSTANTS.REQUEST_STATE.COMPLETE
201+
);
202+
return;
203+
}
193204
}
194205

195206
// Use the updated request

src/components/proxy-middleware/middlewares/rules_middleware.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ class RulesMiddleware {
129129
}
130130
this._update_request_data({ request_body: ctx.rq.get_json_request_body() });
131131

132+
this.on_request_actions = this._process_rules();
133+
132134
const { action_result_objs, continue_request } =
133135
await this.rule_action_processor.process_actions(
134136
this.on_request_actions,

src/components/proxy-middleware/rule_action_processor/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ class RuleActionProcessor {
4747
if(typeof(body) !== 'string') {
4848
body = JSON.stringify(body);
4949
}
50+
5051
ctx.proxyToClientResponse.writeHead(status_code, headers).end(body);
52+
ctx.rq.request_finished = true;
5153

5254
ctx.rq.set_final_response({
5355
status_code: status_code,

src/components/proxy-middleware/rule_action_processor/processors/modify_response_processor.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@ import { executeUserFunction, getFunctionFromString } from "../../../../utils";
1010
import { RQ_INTERCEPTED_CONTENT_TYPES } from "../../constants";
1111

1212
const process_modify_response_action = async (action, ctx) => {
13-
const allowed_handlers = [PROXY_HANDLER_TYPE.ON_REQUEST,PROXY_HANDLER_TYPE.ON_RESPONSE_END, PROXY_HANDLER_TYPE.ON_ERROR];
13+
const allowed_handlers = [
14+
PROXY_HANDLER_TYPE.ON_REQUEST,
15+
PROXY_HANDLER_TYPE.ON_REQUEST_END,
16+
PROXY_HANDLER_TYPE.ON_RESPONSE_END,
17+
PROXY_HANDLER_TYPE.ON_ERROR
18+
];
1419

1520
if (!allowed_handlers.includes(ctx.currentHandler)) {
1621
return build_action_processor_response(action, false);
1722
}
1823

19-
if(ctx.currentHandler === PROXY_HANDLER_TYPE.ON_REQUEST) {
24+
if(
25+
ctx.currentHandler === PROXY_HANDLER_TYPE.ON_REQUEST ||
26+
ctx.currentHandler === PROXY_HANDLER_TYPE.ON_REQUEST_END
27+
) {
2028
if(action.serveWithoutRequest) {
2129
let contentType, finalBody;
2230
if(action.responseType === GLOBAL_CONSTANTS.RESPONSE_BODY_TYPES.LOCAL_FILE) {
@@ -44,7 +52,13 @@ const process_modify_response_action = async (action, ctx) => {
4452

4553
const status = action.statusCode || 200
4654

47-
const finalHeaders = {"Content-Type": contentType}
55+
const finalHeaders = {
56+
"content-type": contentType,
57+
"access-control-allow-origin": "*",
58+
"access-control-allow-methods": "*",
59+
"access-control-allow-headers": "*",
60+
"access-control-allow-credentials": "true",
61+
}
4862
modify_response(ctx, finalBody, status)
4963
return build_action_processor_response(
5064
action,

0 commit comments

Comments
 (0)