From 3213106fca8b359193193faa6c7c8b770a6fc948 Mon Sep 17 00:00:00 2001 From: chencheng Date: Thu, 14 Aug 2014 11:35:08 +0800 Subject: [PATCH] add beforeProxy option to handle request before send to proxy --- .gitignore | 1 + README.md | 2 ++ index.js | 2 ++ lib/file.js | 24 +++++++++++++++++------- test/file.js | 20 ++++++++++++++++++++ 5 files changed, 42 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index d36b871..4b66016 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ test/fixture/seajs/ test/fixture/ajax/ examples/sea-modules/arale/class/ coverage +.idea diff --git a/README.md b/README.md index 39743d4..20bdcf3 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,8 @@ See [example](https://github.com/popomore/connect-combo/blob/master/examples/) - `log` show log on terminal, default `false`. +- `beforeProxy` handle request before send to proxy, default `null`. + ## License MIT diff --git a/index.js b/index.js index 5ac4825..347e563 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,8 @@ var defaults = { // local directory directory: process.cwd(), + beforeProxy: null, + // remote server proxy: '', diff --git a/lib/file.js b/lib/file.js index 4932ad1..a55cb66 100644 --- a/lib/file.js +++ b/lib/file.js @@ -10,6 +10,7 @@ module.exports = File; function File(urlPath, options) { urlPath = url.parse(urlPath).pathname; this.options = options; + this.urlPath = urlPath; this.localPath = path.join(options.directory, urlPath); this.remotePath = url.resolve(options.proxy, urlPath); } @@ -25,12 +26,21 @@ File.prototype.end = function(cb) { cb(null, data.toString()); } else { that.emit('not found', that.localPath); - if (that.options.proxy) { - // find file from remote server - that.getRemote(cb); + if (that.options.beforeProxy) { + that.emit('before proxy', that.localPath); + that.options.beforeProxy.call(that, that.urlPath, cb, requestProxy); } else { - // not found - cb(err); + requestProxy(); + } + + function requestProxy() { + if (that.options.proxy) { + // find file from remote server + that.getRemote(cb); + } else { + // not found + cb(err); + } } } }); @@ -46,7 +56,7 @@ File.prototype.getRemote = function(cb) { var that = this; var opt = { gzip: true, - followRedirect: true + followRedirect: false }; request(this.remotePath, opt, function(err, data, res) { if (err) { @@ -54,7 +64,7 @@ File.prototype.getRemote = function(cb) { return cb(err); } - if (res.statusCode >= 400) { + if (res.statusCode >= 300) { that.emit('not found', that.remotePath); return cb(new Error(res.statusCode)); } diff --git a/test/file.js b/test/file.js index 2209caf..381162b 100644 --- a/test/file.js +++ b/test/file.js @@ -17,6 +17,26 @@ describe('File', function() { }; }); + it('before proxy', function(done) { + options.beforeProxy = function(urlPath, cb, next) { + if (urlPath === 'beforeproxy.js') { + this.emit('found'); + cb(null, 'beforeproxy'); + } else { + next(); + } + }; + + var spy = sinon.spy(); + new File('beforeproxy.js', options) + .on('found', spy) + .end(function(err, data) { + data.should.be.eql('beforeproxy'); + spy.calledOnce.should.be.true; + done(); + }); + }); + it('should response from local', function(done) { var spy = sinon.spy(); new File('a.js', options)