@@ -35,10 +35,17 @@ function createRightProxy(type) {
35
35
} ) ;
36
36
37
37
return function ( req , res /*, [head], [opts] */ ) {
38
- var self = this ,
38
+ var passes = this . passes || passes ,
39
39
args = [ ] . slice . call ( arguments ) ,
40
40
cntr = args . length - 1 ,
41
- head ;
41
+ head , cbl ;
42
+
43
+ /* optional args parse begin */
44
+ if ( typeof args [ cntr ] === 'function' ) {
45
+ cbl = args [ cntr ] ;
46
+
47
+ cntr -- ;
48
+ }
42
49
43
50
if (
44
51
! ( args [ cntr ] instanceof Buffer ) &&
@@ -56,6 +63,8 @@ function createRightProxy(type) {
56
63
head = args [ cntr ] ;
57
64
}
58
65
66
+ /* optional args parse end */
67
+
59
68
[ 'target' , 'forward' ] . forEach ( function ( e ) {
60
69
if ( typeof options [ e ] === 'string' )
61
70
options [ e ] = parse_url ( options [ e ] ) ;
@@ -71,7 +80,7 @@ function createRightProxy(type) {
71
80
* refer to the connection socket
72
81
* pass(req, socket, options, head)
73
82
*/
74
- if ( passes [ i ] ( req , res , this , head ) ) { // passes can return a truthy value to halt the loop
83
+ if ( passes [ i ] ( req , res , cbl ? this : false , head , cbl ) ) { // passes can return a truthy value to halt the loop
75
84
break ;
76
85
}
77
86
}
@@ -84,6 +93,10 @@ function ProxyServer(options, web, ws) {
84
93
this . web = web ;
85
94
this . ws = ws ;
86
95
this . options = options ;
96
+
97
+ this . passes = Object . keys ( passes ) . map ( function ( pass ) {
98
+ return passes [ pass ] ;
99
+ } ) ;
87
100
}
88
101
89
102
ProxyServer . prototype . listen = function ( port ) {
@@ -102,7 +115,25 @@ ProxyServer.prototype.listen = function(port) {
102
115
return this ;
103
116
} ;
104
117
105
- ProxyServer . prototype . before = function ( ) { } ;
106
- ProxyServer . prototype . after = function ( ) { } ;
118
+ ProxyServer . prototype . before = function ( passName , callback ) {
119
+ var i = false ;
120
+ this . passes . forEach ( function ( v , idx ) {
121
+ if ( v . name === passName ) i = idx ;
122
+ } )
123
+
124
+ if ( ! i ) throw new Error ( 'No such pass' ) ;
125
+
126
+ this . passes . splice ( i , 0 , callback ) ;
127
+ } ;
128
+ ProxyServer . prototype . after = function ( passName , callback ) {
129
+ var i = false ;
130
+ this . passes . forEach ( function ( v , idx ) {
131
+ if ( v . name === passName ) i = idx ;
132
+ } )
133
+
134
+ if ( ! i ) throw new Error ( 'No such pass' ) ;
135
+
136
+ this . passes . splice ( i ++ , 0 , callback ) ;
137
+ } ;
107
138
108
139
require ( 'util' ) . inherits ( ProxyServer , EE3 ) ;
0 commit comments