forked from http-party/node-http-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (64 loc) · 1.92 KB
/
index.js
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
var caronte = exports,
extend = require('util')._extend,
parse_url = require('url').parse,
web = require('./passes/web-incoming'),
ws = require('./passes/ws-incoming');
caronte.createWebProxy = createRightProxy('web');
caronte.createWsProxy = createRightProxy('ws');
/**
* Returns a function that creates the loader for
* either `ws` or `web`'s passes.
*
* Examples:
*
* caronte.createRightProxy('ws')
* // => [Function]
*
* @param {String} Type Either 'ws' or 'web'
*
* @return {Function} Loader Function that when called returns an iterator for the right passes
*
* @api private
*/
function createRightProxy(type) {
var passes = (type === 'ws') ? ws : web;
return function(options) {
passes = Object.keys(passes).map(function(pass) {
return passes[pass];
});
return function(req, res /*, [head], [opts] */) {
var self = this,
args = [].slice.call(arguments),
cntr = args.length - 1,
ev = 'caronte:' + type + ':incoming:',
head;
if(
!(args[cntr] instanceof Buffer) &&
args[cntr] !== res
) {
//Copy global options
options = extend({}, options);
//Overwrite with request options
extend(options, args[cntr]);
cntr--;
}
if(args[cntr] instanceof Buffer) {
head = args[cntr];
}
options.ee.emit(ev + 'begin', req, res);
['target', 'forward'].forEach(
function(e) {
if (typeof options[e] === 'string')
options[e] = parse_url(options[e]);
});
passes.some(function(pass) {
var evnt = ev + pass.name.toLowerCase() + ':';
options.ee.emit(evnt + 'begin', req, res);
var val = pass(req, res, options, head);
options.ee.emit(evnt + 'end');
return val;
});
options.ee.emit(ev + 'end');
};
};
}