-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
187 lines (165 loc) · 4.92 KB
/
index.js
File metadata and controls
187 lines (165 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'use strict';
/**
* http 请求动态代理。可根据一些条件动态请求到目标机器
* add by jbfang
* @type {(from: string, to: string) => string}
*/
const join = require('url').resolve;
const rp = require('request-promise-native');
const requestLib = require('request');
module.exports = function(options) {
options || (options = {});
if (!(options.host || options.map || options.url)) {
throw new Error('miss options');
}
return async function proxy(ctx, next) {
let url;
if (options.map){
if(options.map.constructor.name === 'AsyncFunction'){
url = await asyncResolve(ctx.path, options, ctx)
}
}
if (!url){
url = resolve(ctx.path, options, ctx);
}
if (typeof options.suppressRequestHeaders === 'object') {
options.suppressRequestHeaders.forEach(function(h, i) {
options.suppressRequestHeaders[i] = h.toLowerCase();
});
}
var suppressResponseHeaders = []; // We should not be overwriting the options object!
if (typeof options.suppressResponseHeaders === 'object') {
options.suppressResponseHeaders.forEach(function(h, i) {
suppressResponseHeaders.push(h.toLowerCase());
});
}
// don't match
if (!url) {
return next();
}
// if match option supplied, restrict proxy to that match
if (options.match) {
if (!ctx.path.match(options.match)) {
return next();
}
}
var parsedBody = getParsedBody(ctx);
var opt = {
jar: options.jar === true,
url: url + (ctx.querystring ? '?' + ctx.querystring : ''),
headers: ctx.request.header,
encoding: null,
followRedirect: options.followRedirect === false ? false : true,
method: ctx.method,
body: parsedBody,
simple: false,
resolveWithFullResponse: true, // make request-promise respond with the complete response object
};
// set "Host" header to options.host (without protocol prefix), strip trailing slash
if (options.host) {
opt.headers.host = options.host
.slice(options.host.indexOf('://') + 3)
.replace(/\/$/, '');
}
if (options.requestOptions) {
if (typeof options.requestOptions === 'function') {
opt = options.requestOptions(ctx.request, opt);
} else {
Object.keys(options.requestOptions).forEach(function(option) {
opt[option] = options.requestOptions[option];
});
}
}
for (let name in opt.headers) {
if (
options.suppressRequestHeaders &&
options.suppressRequestHeaders.indexOf(name.toLowerCase()) >= 0
) {
delete opt.headers[name];
}
}
let res;
if (parsedBody || ctx.method === 'GET') {
res = await rp(opt);
} else {
res = await pipe(ctx.req, opt);
}
for (var name in res.headers) {
if (suppressResponseHeaders.indexOf(name.toLowerCase()) >= 0) {
continue;
}
if (name === 'transfer-encoding') {
continue;
}
ctx.set(name, res.headers[name]);
}
if (options.overrideResponseHeaders) {
for (let headerKey in options.overrideResponseHeaders) {
ctx.set(headerKey, options.overrideResponseHeaders[headerKey]);
}
}
ctx.body = ctx.body || res.body;
ctx.status = res.statusCode;
if (options.yieldNext) {
return next();
}
};
};
function resolve(path, options, ctx) {
let url = options.url;
if (url) {
if (!/^http/.test(url)) {
url = options.host ? join(options.host, url) : null;
}
return ignoreQuery(url);
}
if (typeof options.map === 'object') {
if (options.map && options.map[path]) {
path = ignoreQuery(options.map[path]);
}
} else if (typeof options.map === 'function') {
path = options.map(path, ctx);
}
// console.log('path--->', join(options.host, path))
return options.host ? join(options.host, path) : path;
}
async function asyncResolve(path, options, ctx) {
let url = options.url;
if (url) {
if (!/^http/.test(url)) {
url = options.host ? join(options.host, url) : null;
}
return ignoreQuery(url);
}
path = await options.map(path, ctx)
return options.host ? join(options.host, path) : path;
}
function ignoreQuery(url) {
return url ? url.split('?')[0] : null;
}
function getParsedBody(ctx) {
let body = ctx.request.body;
if (body === undefined || body === null) {
return undefined;
}
let contentType = ctx.request.header['content-type'];
if (!Buffer.isBuffer(body) && typeof body !== 'string') {
if (contentType && contentType.indexOf('json') !== -1) {
body = JSON.stringify(body);
} else {
body = body + '';
}
}
return body;
}
/**
* Pipes the incoming request body through request()
*/
async function pipe(incomingRequest, opt) {
return new Promise((resolve, reject) => {
incomingRequest.pipe(requestLib(opt, (error, response) => {
if (error) return reject(error);
resolve(response);
}));
});
}