diff --git a/README.md b/README.md index 84c77544..168c6697 100644 --- a/README.md +++ b/README.md @@ -41,9 +41,15 @@ $ npm install This service requires to be configured using environment variables: ``` -# Set the port for the service +# Set the port for the service (default is 5000) $ export PORT=6000 +# Set the host for the service (default is 0.0.0.0) +$ export HOST=127.0.0.1 + +# Set a base URL for the service (default is '/') +$ export BASE_URL=/release + # Access token for the GitHub API (requires permissions to access the repository) # If the repository is public you do not need to provide an access token # you can also use GITHUB_USERNAME and GITHUB_PASSWORD diff --git a/bin/web.js b/bin/web.js index 3d7ec5be..acfff8a1 100644 --- a/bin/web.js +++ b/bin/web.js @@ -4,6 +4,11 @@ var basicAuth = require('basic-auth'); var Analytics = require('analytics-node'); var nuts = require('../'); +const + BASE_URL = process.env.BASE_URL || '/', + PORT = process.env.PORT || 5000, + HOST = process.env.HOST || '0.0.0.0'; + var app = express(); var apiAuth = { @@ -70,7 +75,7 @@ var myNuts = nuts({ } }); -app.use(myNuts.router); +app.use(BASE_URL, myNuts.router); // Error handling app.use(function(req, res, next) { @@ -99,9 +104,6 @@ app.use(function(err, req, res, next) { }); }); -var server = app.listen(process.env.PORT || 5000, function () { - var host = server.address().address; - var port = server.address().port; - - console.log('Listening at http://%s:%s', host, port); +var server = app.listen(PORT, HOST, function () { + console.log('Listening at http://%s:%s%s', HOST, PORT, BASE_URL); }); diff --git a/lib/github.js b/lib/github.js index 39b15fcc..befad820 100644 --- a/lib/github.js +++ b/lib/github.js @@ -1,5 +1,6 @@ var _ = require('lodash'); var Q = require('q'); +var destroy = require('destroy'); var github = require('octonode'); var request = require('request'); var Buffer = require('buffer').Buffer; @@ -18,7 +19,7 @@ module.exports = function(opts) { } // List releases - var listReleases = function(page) { + function listReleases(page) { page = page || 1; var uri = "/repos/"+opts.repository+"/releases"; @@ -38,18 +39,18 @@ module.exports = function(opts) { return releases.concat(r); }); }); - }; + } var cacheListReleases =_.memoize(listReleases, function() { return cacheInstance+Math.ceil(Date.now()/opts.timeout) }); - var clearCache = function() { + function clearCache() { cacheInstance = cacheInstance + 1; - }; + } // Stream a download to res - var streamAsset = function (uri) { + function streamAsset(uri) { var headers = { 'User-Agent': "releaser-server", 'Accept': "application/octet-stream" @@ -72,23 +73,31 @@ module.exports = function(opts) { headers: headers, auth: httpAuth }); - }; + } // Read a asset - var readAsset = function(uri) { + function readAsset(uri) { var d = Q.defer(); var output = Buffer([]); + var res = streamAsset(uri); - streamAsset(uri) - .on('data', function(buf) { - output = Buffer.concat([output, buf]); - }) - .once('error', function(err) { - d.reject(err); - }) - .once('end', function() { - d.resolve(output); - }); + var cleanup = function() { + destroy(res); + res.removeAllListeners(); + }; + + res + .on('data', function(buf) { + output = Buffer.concat([output, buf]); + }) + .on('error', function(err) { + cleanup(); + d.reject(err); + }) + .on('end', function() { + cleanup(); + d.resolve(output); + }); return d.promise } diff --git a/lib/index.js b/lib/index.js index 58e8eb8e..cc1dc526 100644 --- a/lib/index.js +++ b/lib/index.js @@ -211,6 +211,7 @@ module.exports = function nuts(opts) { .then(function(versions) { // Update needed? var latest = _.first(versions); + if (!latest) throw new Error("Version not found"); // File exists var asset = _.find(latest.platforms, { @@ -224,14 +225,9 @@ module.exports = function nuts(opts) { releases = _.chain(releases) - // Exclude deltas and other versions - .filter(function(entry) { - return (!entry.isDelta && entry.version == winReleases.normVersion(latest.tag)); - }) - - // Change filename to use downlaodp roxy + // Change filename to use download proxy .map(function(entry) { - entry.filename = url.resolve(fullUrl, '/download/'+latest.tag+'/'+entry.filename); + entry.filename = url.resolve(fullUrl, '/download/'+entry.version+'/'+entry.filename); return entry; }) diff --git a/package.json b/package.json index 037dc871..db6bd176 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nuts-serve", - "version": "2.6.1", + "version": "2.6.2", "description": "Server to make GitHub releases (private) available to download with Squirrel support", "main": "./lib/index.js", "homepage": "https://github.com/GitbookIO/nuts", @@ -20,7 +20,8 @@ "analytics-node": "1.2.2", "uuid": "2.0.1", "github-webhook-handler": "0.5.0", - "strip-bom": "2.0.0" + "strip-bom": "2.0.0", + "destroy": "1.0.3" }, "devDependencies": { "mocha": "1.18.2",