Commit c0039350 authored by xzj's avatar xzj

统一参数为uri;write逻辑移到v2版本中

parent 6c8b87b8
'use strict' 'use strict'
const request = require('request') const request = require('request')
const debug = require('debug')('request-proxy') const debug = require('debug')('request-proxy')
const qs = require('querystring')
const Agent = require('agentkeepalive'); const Agent = require('agentkeepalive');
const HttpsAgent = require('agentkeepalive').HttpsAgent; const HttpsAgent = require('agentkeepalive').HttpsAgent;
...@@ -49,11 +48,6 @@ module.exports = function (options) { ...@@ -49,11 +48,6 @@ module.exports = function (options) {
timeout: options.timeout timeout: options.timeout
} }
const isProduction = process.env.NODE_ENV === 'production'
const rewriteUrl = function (source, to) {
return source.replace(/\/\/([^\/]+)\//, `//${to}/`).replace(/_ip_=[\w\.:%]+&?/, '')
}
const httpAgent = new Agent(options.agent); const httpAgent = new Agent(options.agent);
const httpsAgent = new HttpsAgent(); const httpsAgent = new HttpsAgent();
...@@ -73,19 +67,11 @@ module.exports = function (options) { ...@@ -73,19 +67,11 @@ module.exports = function (options) {
option = Object.assign({}, curlDefault, option) option = Object.assign({}, curlDefault, option)
extra = Object.assign({}, extraDefault, extra) extra = Object.assign({}, extraDefault, extra)
if (!isProduction && ctx.request.query['_ip_']) {
if (option.url) {
option.url = rewriteUrl(option.url, ctx.request.query['_ip_'])
} else {
option.uri = rewriteUrl(option.uri, ctx.request.query['_ip_'])
}
}
debug(option) debug(option)
if (!option.agent && !/^https/.test(option.url)) { if (!option.agent && !/^https/.test(option.uri)) {
option.agent = httpAgent option.agent = httpAgent
} else if (!option.agent && /^https/.test(option.url)) { } else if (!option.agent && /^https/.test(option.uri)) {
option.agent = httpsAgent option.agent = httpsAgent
} }
...@@ -114,29 +100,21 @@ module.exports = function (options) { ...@@ -114,29 +100,21 @@ module.exports = function (options) {
} }
option = Object.assign({}, pipeDefault, option) option = Object.assign({}, pipeDefault, option)
if (!isProduction && ctx.request.query['_ip_']) {
if (option.url) {
option.url = rewriteUrl(option.url, ctx.request.query['_ip_'])
} else {
option.uri = rewriteUrl(option.uri, ctx.request.query['_ip_'])
}
}
if (!option.method) { if (!option.method) {
option.method = ctx.method option.method = ctx.method
} }
debug(option) debug(option)
if (!option.agent && !/^https/.test(option.url)) { if (!option.agent && !/^https/.test(option.uri)) {
option.agent = httpAgent option.agent = httpAgent
} else if (!option.agent && /^https/.test(option.url)) { } else if (!option.agent && /^https/.test(option.uri)) {
option.agent = httpsAgent option.agent = httpsAgent
} }
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
let r = ctx.req.pipe(request(option, function (err, response) { let r = ctx.req.pipe(request(option, function (err, response) {
if (option.time && options.logger) { if (option.time && options.logger) {
options.logger.info(option.url || option.uri, JSON.stringify(response && response.timings || '')) options.logger.info(option.uri, JSON.stringify(response && response.timings || ''))
} }
})) }))
r.on('error', function (err) { r.on('error', function (err) {
......
{ {
"name": "request-proxy", "name": "request-proxy",
"version": "1.1.2", "version": "1.2.0",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment