Skip to content

Commit

Permalink
add cache option
Browse files Browse the repository at this point in the history
  • Loading branch information
Dong-Gao committed Aug 7, 2020
1 parent b93f948 commit f4a4ae1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Recorder extends events.EventEmitter {
constructor(config) {
super(config);
this.globalId = 1;
this.fake = false;
this.cachePath = getCacheDir();
this.db = new Datastore();

Expand Down
22 changes: 14 additions & 8 deletions lib/requestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,14 @@ function getUserReqHandler(userRule, recorder) {
req,
startTime: new Date().getTime()
};
resourceInfoId = recorder.appendRecord(resourceInfo);
if (!recorder.fake) {
resourceInfoId = recorder.appendRecord(resourceInfo);
}
}

try {
resourceInfo.reqBody = reqData.toString(); //TODO: deal reqBody in webInterface.js
recorder && recorder.updateRecord(resourceInfoId, resourceInfo);
recorder && !recorder.fake && recorder.updateRecord(resourceInfoId, resourceInfo);
} catch (e) { }
})

Expand Down Expand Up @@ -505,7 +507,7 @@ function getUserReqHandler(userRule, recorder) {

// console.info('===> resbody in record', resourceInfo);

recorder && recorder.updateRecord(resourceInfoId, resourceInfo);
recorder && !recorder.fake && recorder.updateRecord(resourceInfoId, resourceInfo);
})
.catch((e) => {
logUtil.printLog(color.green('Send final response failed:' + e.message), logUtil.T_ERR);
Expand Down Expand Up @@ -619,7 +621,9 @@ function getConnectReqHandler(userRule, recorder, httpsServerMgr) {
req,
startTime: new Date().getTime()
};
resourceInfoId = recorder.appendRecord(resourceInfo);
if (!recorder.fake) {
resourceInfoId = recorder.appendRecord(resourceInfo);
}
}
})
.then(() => {
Expand Down Expand Up @@ -677,7 +681,7 @@ function getConnectReqHandler(userRule, recorder, httpsServerMgr) {
resourceInfo.resBody = '';
resourceInfo.length = 0;

recorder && recorder.updateRecord(resourceInfoId, resourceInfo);
recorder && !recorder.fake && recorder.updateRecord(resourceInfoId, resourceInfo);
}
})
.catch(co.wrap(function *(error) {
Expand Down Expand Up @@ -726,7 +730,9 @@ function getWsHandler(userRule, recorder, wsClient, wsReq) {
req: wsReq,
startTime: new Date().getTime()
});
resourceInfoId = recorder.appendRecord(resourceInfo);
if (!recorder.fake) {
resourceInfoId = recorder.appendRecord(resourceInfo);
}
}

/**
Expand Down Expand Up @@ -794,7 +800,7 @@ function getWsHandler(userRule, recorder, wsClient, wsReq) {
};

// resourceInfo.wsMessages.push(message);
recorder && recorder.updateRecordWsMessage(resourceInfoId, message);
recorder && !recorder.fake && recorder.updateRecordWsMessage(resourceInfoId, message);
};

proxyWs.onopen = () => {
Expand All @@ -815,7 +821,7 @@ function getWsHandler(userRule, recorder, wsClient, wsReq) {
resourceInfo.resBody = '';
resourceInfo.length = resourceInfo.resBody.length;

recorder && recorder.updateRecord(resourceInfoId, resourceInfo);
recorder && !recorder.fake && recorder.updateRecord(resourceInfoId, resourceInfo);
});

proxyWs.onerror = (e) => {
Expand Down
18 changes: 12 additions & 6 deletions proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class ProxyCore extends events.EventEmitter {

// init recorder
this.recorder = config.recorder;

// init request handler
const RequestHandler = util.freshRequire('./requestHandler');
this.requestHandler = new RequestHandler({
Expand Down Expand Up @@ -293,13 +292,20 @@ class ProxyServer extends ProxyCore {
* @param {object} [config.webInterface] - config of the web interface
* @param {boolean} [config.webInterface.enable=false] - if web interface is enabled
* @param {number} [config.webInterface.webPort=8002] - http port of the web interface
* @param {boolean} [config.cache=false] - never store cache on disk if cache is false
*/
constructor(config) {
// prepare a recorder
const recorder = new Recorder();
const configForCore = Object.assign({
if (!('cache' in config)) {
config.cache = true;
}
const recorder = config.cache ? new Recorder() : { fake: true };
if (!config.cache && config.webInterface) {
config.webInterface.enable = false;
}
const configForCore = Object.assign(config, {
recorder,
}, config);
});

super(configForCore);

Expand All @@ -309,7 +315,7 @@ class ProxyServer extends ProxyCore {
}

start() {
if (this.recorder) {
if (this.recorder && !this.recorder.fake) {
this.recorder.setDbAutoCompact();
}

Expand All @@ -333,7 +339,7 @@ class ProxyServer extends ProxyCore {
close() {
const self = this;
// release recorder
if (self.recorder) {
if (self.recorder && !self.recorder.fake) {
self.recorder.stopDbAutoCompact();
self.recorder.clear();
}
Expand Down

0 comments on commit f4a4ae1

Please sign in to comment.