-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
323 lines (249 loc) · 8.16 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
'use strict';
/*
Proxy Cache
==================================================================
proxy cache is a module that allows you to run a proxy server that
will cache request.
*/
var http = require( 'http' ),
qs = require( 'querystring' ),
util = require( 'util' ),
extend = require( 'node.extend' ),
EventEmitter = require( 'events' ).EventEmitter,
httpProxy = require( 'http-proxy' ),
sortObject = require( 'sorted-object' ),
sha1 = require( 'sha1' ),
urlParse = require( 'url' ).parse,
pkg = require( './package.json' );
module.exports = Woden;
/*
Woden::Constructor
params
options { Object } - set some base configuration, also gets passed to `http-proxy`
returns
instance
*/
function Woden( options ) {
this.options = options || {};
this.settings = [];
this.proxy = httpProxy.createProxyServer( options );
this.server = http.createServer( this._onRequest.bind( this ) );
this.proxy.on( 'proxyRes', this.emit.bind( this, 'response' ) );
this.proxy.on( 'proxyRes', this._cacheResponse.bind( this ) ); // optionally caches response
this.proxy.on( 'proxyReq', this._onProxyReq.bind( this ) );
this.proxy.on( 'start', this._onProxyStart.bind( this ) );
this.storageAdapter = require( './src/store' );
this.proxy.on( 'error', this._onError.bind( this ) );
}
// inherit Event Emitter
util.inherits( Woden, EventEmitter );
/*
Woden::when - allows for custom settings `when` url proxy is requested
params
regexp { RegExp } - pattern to match against the request param $url
settings { Object } - an object with some common settings
*/
Woden.prototype.when = function( regexp, settings ) {
this.settings.push( [ regexp, settings ] );
};
/*
Woden::store - Allows a custom storageAdapter
params
adapter { Object } - an object that has the methods `get` and `set`
*/
Woden.prototype.store = function( adapter ) {
// maybe do som testing before setting adapter
this.storageAdapter = adapter;
};
/*
Woden::listen - Allows a custom port
params
port { Number } - port to listen proxy server on
*/
Woden.prototype.listen = function( port, ipaddress ) {
port = port || 5050;
if ( this.options.output ) {
this.options.output.write( 'listening on port ' + port ); // assumes its a writable stream
}
this.server.listen( port, ipaddress );
};
/*
Woden::_onRequest - Private handler of incoming request
params
req { Request } - node request object
res { Response } - node response object
*/
Woden.prototype._onRequest = function( req, res ) {
this.emit( 'request', req );
var payload = '',
arg = req.url.split( '?' ),
path = arg.shift( ),
query = qs.parse( arg.pop( ) ),
target = query.$url || '',
key,
self = this;
delete query.$url;
req.url = ( path !== '/' ? path : '' ) + ( Object.keys( query ).length ? ( '?' + qs.stringify( query ) ) : '' );
req._target = target;
var settings = this._getSettings( req._target + req.url );
req._settings = settings;
if(settings.params && Object.keys( settings.params ).length) {
query = extend( true, {},
settings.params, query );
req.url += req.url.indexOf('?') > -1 ? '&' : '?';
req.url += qs.stringify( settings.params );
}
query = sortObject( query );
key = settings.getKey( req._target + req.url, query );
if ( req.method.toLowerCase() === 'options' ) {
if ( self.options.onOptions ) {
self.options.onOptions( req, res );
return;
}
}
// right now only get request are supported
if ( req.method.toLowerCase() !== 'get' ) {
res.writeHead( 501 );
res.write( 'Methods that are not "GET" are not implemented in proxy cache' );
res.end( );
res.cache = true;
self.emit( 'reponse', res );
return;
}
// test for a proper url
if ( !/^(http|https):\/\//.test( req._target ) ) {
res.writeHead( 400 );
res.write( '$url query param requires a valid url to use proxy cache' );
res.end( );
res.cache = true;
self.emit( 'reponse', res );
return;
}
req.on( 'data', function( data ) {
payload += data.toString( data );
});
this.storageAdapter.get( key, function( err, cache ) {
if ( cache && !err ) {
// add header to signify a cache hit
cache.headers[ 'cache-agent' ] = pkg.name;
res.writeHead( cache.statusCode, cache.headers );
res.write( cache.body );
res.end( );
res.cache = true;
self.emit( 'reponse', res );
return;
}
self.proxy.web( req, res, {
target: req._target,
toProxy: req.url.length ? false : true
} );
} );
};
/*
Woden::_cacheResponse - Private caching handler
params
proxyRes { Response } - node response object from proxy
req { Request } - node request object
res { Response } - node response object
*/
Woden.prototype._cacheResponse = function( proxyRes, req ) {
if ( !req._settings.caching ) {
return;
}
var arg = req.url.split( '?' ),
query = sortObject( qs.parse( arg.pop( ) ) ),
headers = proxyRes.headers,
settings = req._settings,
key = settings.getKey( req._target + req.url, query ),
self = this,
arr = [];
function cached( cache ) {
return function ( err ) {
if ( err ) {
this._onError( err );
}
if ( !err && settings.onCache ) {
self.emit( 'cached', cache );
settings.onCache( cache );
}
};
}
proxyRes.on( 'data', function( data ) {
arr.push( data );
} );
proxyRes.on( 'end', function() {
var cache = {
headers: headers,
statusCode: proxyRes.statusCode,
body: Buffer.concat( arr )
};
var timeout = settings.cacheTimeout( cache, req, proxyRes );
if ( timeout < 0 ) {
return;
}
self.storageAdapter.set( {
key: key,
value: cache,
timeout: timeout
}, cached( cache ) );
} );
};
/*
Woden::_getSettings - Private utility to grab the current url's setting
params
url { String } - url that proxy is going to be requesting
*/
Woden.prototype._getSettings = function( url ) {
var settings;
for( var i = 0; i < this.settings.length; ++i ) {
var curSettings = this.settings[ i ];
if ( Array.isArray( curSettings ) && curSettings[ 0 ].test( url ) && curSettings[ 1 ] ) {
settings = curSettings[ 1 ];
}
}
return extend( true, {}, {
getKey: getKey,
caching: true,
cacheTimeout: function() {
return 3600000; // default to an hour
}
}, settings );
};
/*
Woden::_onProxyStart - Private handler of request proxy start
params
req { Request } - node request object
*/
Woden.prototype._onProxyStart = function( req ) {
var parsed = urlParse( req._target, false, true );
req.headers.host = parsed.host;
}
/*
Woden::_onProxyReq - Private handler of request pre proxy
params
proxyRes { Request } - node request object for proxy
req { Request } - node request object
*/
Woden.prototype._onProxyReq = function( proxyReq, req ) {
var headers = req._settings.headers || {};
for ( var key in headers ) {
proxyReq.setHeader( key, headers[ key ] );
}
};
/*
Woden::_onError - Private handler of errors
params
error { Error } - error object ( not a string )
*/
Woden.prototype._onError = function( error, req, res, url ) {
this.emit( 'error', error, req, res, url );
};
/*
getKey - Utility for default key
params
url { String } - the full target url of the request
payload { Object } - query object from initial request
*/
function getKey( url, payload ) {
return sha1( url + ':' + qs.stringify( payload ) );
}