Skip to content

Commit

Permalink
Add new hooks system
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyPesse committed Feb 11, 2016
1 parent 3ecbc78 commit 73330da
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 52 deletions.
72 changes: 49 additions & 23 deletions bin/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,6 @@ var myNuts = nuts.Nuts({
cache: process.env.VERSIONS_CACHE,
refreshSecret: process.env.GITHUB_SECRET,

onDownload: function(download, req, res, next) {
console.log('download', download.platform.filename, "for version", download.version.tag, "on channel", download.version.channel, "for", download.platform.type);

// Track on segment if enabled
if (analytics) {
var userId = req.query.user;

analytics.track({
event: downloadEvent,
anonymousId: userId? null : uuid.v4(),
userId: userId,
properties: {
version: download.version.tag,
channel: download.version.channel,
platform: download.platform.type,
os: nuts.platforms.toType(download.platform.type)
}
});
}

next();
},

onAPIAccess: function(req, res, next) {
if (!apiAuth.username) return next();

Expand All @@ -71,6 +48,55 @@ var myNuts = nuts.Nuts({
}
});

// Control access to API
myNuts.before('api', function(access, next) {
if (!apiAuth.username) return next();

function unauthorized() {
next(new Error('Invalid username/password for API'));
};

var user = basicAuth(req);
if (!user || !user.name || !user.pass) {
return unauthorized();
};

if (user.name === apiAuth.username && user.pass === apiAuth.password) {
return next();
} else {
return unauthorized();
};
});

// Log download
myNuts.before('download', function(download, next) {
console.log('download', download.platform.filename, "for version", download.version.tag, "on channel", download.version.channel, "for", download.platform.type);

next();
});
myNuts.after('download', function(download, next) {
console.log('downloaded', download.platform.filename, "for version", download.version.tag, "on channel", download.version.channel, "for", download.platform.type);

// Track on segment if enabled
if (analytics) {
var userId = req.query.user;

analytics.track({
event: downloadEvent,
anonymousId: userId? null : uuid.v4(),
userId: userId,
properties: {
version: download.version.tag,
channel: download.version.channel,
platform: download.platform.type,
os: nuts.platforms.toType(download.platform.type)
}
});
}

next();
});

app.use(myNuts.router);

// Error handling
Expand Down
23 changes: 0 additions & 23 deletions lib/config.js

This file was deleted.

39 changes: 34 additions & 5 deletions lib/nuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var Q = require('q');
var url = require('url');
var os = require('os');
var path = require('path');
var hooks = require('hooks-fixed');
var Understudy = require('understudy');
var LRU = require('lru-diskcache');
var express = require('express');
var useragent = require('express-useragent');
Expand All @@ -23,6 +23,7 @@ function Nuts(opts) {
if (!(this instanceof Nuts)) return new Nuts(opts);
var that = this;

Understudy.call(this);
_.bindAll(this);

this.opts = _.defaults(opts || {}, {
Expand Down Expand Up @@ -78,6 +79,8 @@ function Nuts(opts) {

this.router.get('/notes/:version?', this.onServeNotes);

// Bind API
this.router.use('/api', this.onAPIAccessControl);
_.each(API_METHODS, function(method, route) {
this.router.get('/api/' + route, function(req, res, next) {
return Q()
Expand Down Expand Up @@ -106,6 +109,22 @@ Nuts.prototype.init = function() {
});
};

// Perform a hook using promised functions
Nuts.prototype.performQ = function(name, arg, fn) {
var that = this;
fn = fn || function() { };

return Q.nfcall(this.perform, name, arg, function (next) {
Q()
.then(function() {
return fn.call(that, arg);
})
.then(function() {
next();
}, next);
})
};

// Serve an asset to the response
Nuts.prototype.serveAsset = function(req, res, version, asset) {
var that = this;
Expand All @@ -115,12 +134,11 @@ Nuts.prototype.serveAsset = function(req, res, version, asset) {
stream.pipe(res);
}

// Call middleware
return Q.nfcall(this.opts.onDownload, {
return this.performQ('download', {
req: req,
version: version,
platform: asset
}, req, res)
.then(function() {
}, function() {
var d = Q.defer();
res.header('Content-Length', asset.size);
res.attachment(asset.filename);
Expand Down Expand Up @@ -350,5 +368,16 @@ Nuts.prototype.onServeNotes = function(req, res, next) {
.fail(next);
};

// Control access to the API
Nuts.prototype.onAPIAccessControl = function(req, res, next) {
this.performQ('api', {
req: req,
res: res
})
.then(function() {
next();
}, next);
};


module.exports = Nuts;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"github-webhook-handler": "0.5.0",
"strip-bom": "2.0.0",
"destroy": "1.0.3",
"hooks-fixed": "1.1.0"
"understudy": "4.1.0"
},
"devDependencies": {
"mocha": "1.18.2",
Expand Down

0 comments on commit 73330da

Please sign in to comment.