Comparing version 1.0.4 to 1.0.5
@@ -35,3 +35,3 @@ 'use strict'; | ||
let headers = Object.assign({}, calculateHeaderExtension(req, Object.assign({}, u, u.serviceCfg)), u.headers); | ||
let headers = Object.assign({}, utils.calculateHeaderExtension(req, Object.assign({}, u, u.serviceCfg)), u.headers); | ||
let serviceClient = new ServiceClient({ | ||
@@ -99,30 +99,1 @@ endpoint, | ||
function calculateHeaderExtension(req, serviceCfg) { | ||
const headers = {}; | ||
if (serviceCfg.remoteApp) { | ||
headers['X-Remote-App'] = serviceCfg.remoteApp; | ||
} | ||
if (serviceCfg.rid) { | ||
headers['EagleEye-TraceId'] = serviceCfg.rid; | ||
} | ||
serviceCfg.headerExtension.forEach(e => { | ||
// 1. 如果是函数,直接执行 | ||
// 2. 如果是string,加载内置的模块 | ||
// 3. 如果是object,merge到headers | ||
if (typeof e === 'function') { | ||
Object.assign(headers, e(req, serviceCfg)); | ||
} else if (typeof e === 'string') { | ||
try { | ||
const m = require('hc-service-client/lib/extension/' + e); | ||
Object.assign(headers, m(req, serviceCfg)); | ||
} catch (e) { | ||
serviceCfg.log.error(e); | ||
} | ||
} else if (e && typeof e === 'object') { | ||
Object.assign(headers, e); | ||
} | ||
}); | ||
return headers; | ||
} |
@@ -36,2 +36,4 @@ 'use strict'; | ||
}); | ||
let headers = Object.assign({}, utils.calculateHeaderExtension(req, Object.assign({}, u, u.serviceCfg)), u.headers); | ||
Object.assign(options.headers, headers); | ||
@@ -38,0 +40,0 @@ path = utils.processUrl(path, routeKeys, routePathGrep, req); |
@@ -60,1 +60,31 @@ 'use strict'; | ||
exports.calculateHeaderExtension = function calculateHeaderExtension(req, serviceCfg) { | ||
const headers = {}; | ||
if (serviceCfg.remoteApp) { | ||
headers['X-Remote-App'] = serviceCfg.remoteApp; | ||
} | ||
if (serviceCfg.rid) { | ||
headers['EagleEye-TraceId'] = serviceCfg.rid; | ||
} | ||
serviceCfg.headerExtension.forEach(e => { | ||
// 1. 如果是函数,直接执行 | ||
// 2. 如果是string,加载内置的模块 | ||
// 3. 如果是object,merge到headers | ||
if (typeof e === 'function') { | ||
Object.assign(headers, e(req, serviceCfg)); | ||
} else if (typeof e === 'string') { | ||
try { | ||
const m = require('hc-service-client/lib/extension/' + e); | ||
Object.assign(headers, m(req, serviceCfg)); | ||
} catch (e) { | ||
serviceCfg.log.error(e); | ||
} | ||
} else if (e && typeof e === 'object') { | ||
Object.assign(headers, e); | ||
} | ||
}); | ||
return headers; | ||
} |
@@ -13,13 +13,5 @@ 'use strict'; | ||
let serviceInfo = url.parse(endpoint); | ||
let route = u.route; | ||
let method = u.method; | ||
let client = u.client; | ||
let timeout = u.timeout; | ||
let ws = u.ws; | ||
let routeKeys = []; | ||
let routePathGrep = pathToRegexp(u.route, routeKeys, utils.pathToRegexpOption); | ||
return (clientReq, socket, clientHead, proxyUrl) => { | ||
const clientReqUrlInfo = url.parse(clientReq.url); | ||
socket.setTimeout(0); | ||
@@ -32,3 +24,4 @@ socket.setNoDelay(true); | ||
options.timeout = timeout || 0; // ws 一直连着 | ||
options.headers = { | ||
let headers = Object.assign({}, utils.calculateHeaderExtension(clientReq, Object.assign({}, u, u.serviceCfg)), u.headers); | ||
options.headers = Object.assign(headers, { | ||
'Connection': 'Upgrade', | ||
@@ -39,3 +32,3 @@ 'Upgrade': 'websocket', | ||
'Sec-Websocket-Extensions': clientReq.headers['sec-websocket-extensions'] || 'permessage-deflate; client_max_window_bits' | ||
}; | ||
}); | ||
// options.protocol = serviceInfo.protocol || 'ws'; | ||
@@ -42,0 +35,0 @@ options.hostname = serviceInfo.hostname; |
{ | ||
"name": "hc-proxy", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "honeycomb api proxy express middleware.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -17,2 +17,3 @@ 'use strict'; | ||
} else { | ||
console.log('req.headers[test-header]', req.headers['test-header']); | ||
res.end(req.url); | ||
@@ -19,0 +20,0 @@ } |
@@ -39,2 +39,9 @@ 'use strict'; | ||
client: 'http', | ||
headerExtension: [ | ||
function (req, serviceCfg) { | ||
return { | ||
'test-header': 123 | ||
}; | ||
} | ||
], | ||
api: [ | ||
@@ -41,0 +48,0 @@ '/urllib', |
Sorry, the diff of this file is not supported yet
45335
1009