forked from edorivai/koa-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
166 lines (142 loc) · 4.45 KB
/
Copy pathindex.js
File metadata and controls
166 lines (142 loc) · 4.45 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
'use strict';
var Stream = require('stream');
var join = require('url').resolve;
var rp = require('request-promise-native');
var requestLib = require('request');
var pauseStream = require('pause-stream');
module.exports = function(options) {
options || (options = {});
if (!(options.host || options.map || options.url)) {
throw new Error('miss options');
}
return async function proxy(ctx, next) {
var url = resolve(ctx.path, options);
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 (typeof options.match === 'function' ? !options.match(ctx.path) :
!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];
}
}
if (parsedBody || ctx.method === 'GET') {
var res = await rp(opt);
} else {
var res = await pipe(ctx.req, opt);
}
for (var name in res.headers) {
// http://stackoverflow.com/questions/35525715/http-get-parse-error-code-hpe-unexpected-content-length
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) {
var 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);
}
return options.host ? join(options.host, path) : null;
}
function ignoreQuery(url) {
return url ? url.split('?')[0] : null;
}
function getParsedBody(ctx) {
var body = ctx.request.body;
if (body === undefined || body === null) {
return undefined;
}
var 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()
*/
function pipe(incomingRequest, opt) {
return new Promise((resolve, reject) => {
incomingRequest.pipe(requestLib(opt, (error, response) => {
if (error) return reject(error);
resolve(response);
}))
});
}