Skip to content

Commit

Permalink
Merge pull request #2 from sorrycc/master
Browse files Browse the repository at this point in the history
add beforeProxy option to handle request before send to proxy
  • Loading branch information
popomore committed Aug 14, 2014
2 parents 3286a14 + 3213106 commit fd9fd65
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ test/fixture/seajs/
test/fixture/ajax/
examples/sea-modules/arale/class/
coverage
.idea
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var defaults = {
// local directory
directory: process.cwd(),

beforeProxy: null,

// remote server
proxy: '',

Expand Down
24 changes: 17 additions & 7 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
}
}
});
Expand All @@ -46,15 +56,15 @@ 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) {
that.emit('not found', that.remotePath);
return cb(err);
}

if (res.statusCode >= 400) {
if (res.statusCode >= 300) {
that.emit('not found', that.remotePath);
return cb(new Error(res.statusCode));
}
Expand Down
20 changes: 20 additions & 0 deletions test/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fd9fd65

Please sign in to comment.