Skip to content

Commit

Permalink
fix: support windows report with proxy (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
fXy-during authored and cpselvis committed Nov 25, 2019
1 parent aed5b58 commit 3cd0463
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/feflow-report/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@feflow/report",
"version": "0.1.2",
"version": "0.1.4",
"description": "feflow report sdk",
"main": "./lib/index.js",
"scripts": {
Expand Down
76 changes: 23 additions & 53 deletions packages/feflow-report/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,40 @@
import rp from 'request-promise';
import URI from './config';
import shell from 'shelljs';

export default {
retryCount: 0,
log: {},
report(param, log) {
report(param, log, needProxy = true) {
this.log = log || {};
const proxyIsOn = shell
.exec(`curl ${URI.REPORT_PROXY}`, { silent: true })
.stdout.trim();

if (proxyIsOn) {
log.debug('Report, chose proxy curl');
this.reportWithProxy(param);
} else {
log.debug('Report, chose normal http');
this.reportWithoutProxy(param);
}
},
reportWithoutProxy(param) {
rp({
const rpOption: any = {
method: 'POST',
uri: URI.REPORT_URL,
body: param,
json: true
})
json: true,
timeout: 600,
};

if (needProxy) {
this.log.debug('report with proxy');
rpOption.proxy = URI.REPORT_PROXY;
}

rp(rpOption)
.then(res => {
this.log.debug('got report response', res);
})
.catch(e => {
this.log.debug('feflow report fail', e);
this.log.debug('feflow report fail', e.message);
if (/ETIMEDOUT/.test(e.message || '')) {
if (this.retryCount > 2) return;
// timeout retry
this.log.debug('feflow report 超时重试', this.retryCount);
// 3nd rp request, remove rp proxy
const needProxy = this.retryCount != 2;
this.retryCount++;
this.report(Object.assign({}, param), this.log, needProxy);
}
});
},
reportWithProxy(param) {
let dataString = '';
// get formated report params
Object.keys(param).forEach(key => {
let value = param[key];
value = typeof value == 'object' ? JSON.stringify(value) : value;
if (value != undefined) {
dataString += `&${key}=${value}`;
}
});

const cmdWithProxy = [];

// curl
cmdWithProxy.push('curl');

// proxy
cmdWithProxy.push('-x');
cmdWithProxy.push(URI.REPORT_PROXY);

// url
cmdWithProxy.push(URI.REPORT_URL);

// params
cmdWithProxy.push('-d');
cmdWithProxy.push("'" + dataString.slice(1) + "'");

this.log.debug("cmdWithProxy.join(' ')", cmdWithProxy.join(' '));

const res = shell
.exec(cmdWithProxy.join(' '), { silent: true })
.stdout.trim();

this.log.debug('res', res);
}
};

0 comments on commit 3cd0463

Please sign in to comment.