Permalink
Browse files

First pass at adding build statuses

  • Loading branch information...
1 parent 14a8465 commit 9edb499c4fab9a34731ed372cbe2aa9ca5a74fe8 @jfhbrook committed Jan 26, 2014
Showing with 122 additions and 19 deletions.
  1. +17 −1 bundler/cache.js
  2. +43 −13 bundler/index.js
  3. +7 −1 index.js
  4. +4 −2 package.json
  5. +49 −0 statuses.js
  6. +1 −1 test/multiple.js
  7. +1 −1 test/singular.js
View
@@ -14,6 +14,14 @@ var Cache = function (name, db, opts) {
this.ttl = opts.ttl;
};
+Cache.prototype.get = function get(key, cb) {
+ this.db.get(this.hashfxn(key), cb);
+};
+
+Cache.prototype.put = function put(key, val, cb) {
+ this.db.put(this.hashfxn(key), val, cb);
+};
+
Cache.prototype.check = function check(body, generate, cb) {
var hash = this.hashfxn(body),
db = this.db,
@@ -65,7 +73,7 @@ var c = module.exports = function (location) {
var db = sublevel(ttl(level(location)));
- var bundles, multibundles, aliases;
+ var bundles, multibundles, aliases, statuses;
bundles = new Cache('bundles', db, {
ttl: 30 * DAYS
@@ -89,10 +97,18 @@ var c = module.exports = function (location) {
ttl: 30 * DAYS
});
+ statuses = new Cache('statuses', db, {
+ hashfxn: function (o) {
+ return o.module + '@' + o.semver;
+ },
+ ttl: 30 * DAYS
+ });
+
return {
bundles: bundles,
multibundles: multibundles,
aliases: aliases,
+ statuses: statuses,
defaultHashFxn: defaultHashFxn
};
};
View
@@ -1,7 +1,8 @@
var path = require('path'),
EventEmitter = require('events').EventEmitter;
-var log = require('minilog')('bundler');
+var log = require('minilog')('bundler'),
+ xtend = require('xtend');
var cache = require('./cache'),
core = require('./node-core'),
@@ -21,10 +22,10 @@ module.exports = function bundler(opts) {
var c = cache(db);
- //
- // Used to handle the case where a build is already in-progress
- //
- var inProgress = {};
+ //
+ // Used to handle the case where a build is already in-progress
+ //
+ var inProgress = {};
var _bundle = function bundle(pkg, callback) {
@@ -36,13 +37,7 @@ module.exports = function bundler(opts) {
return checkBundles(null, process.version);
}
- c.aliases.check({ module: module, semver: semver }, function resolve(cb) {
- registry.resolve(module, semver, function (err, v) {
- if (err) return callback(err);
-
- cb(null, v);
- });
- }, checkBundles);
+ alias(module, semver, checkBundles);
function checkBundles(err, version) {
if (err) return callback(err);
@@ -129,10 +124,13 @@ module.exports = function bundler(opts) {
function finish(err, bundle, json) {
if (err) return handleError(err);
- log.info('bundler: successfully browserified `' + module + '@' + semver + '`.');
+ log.info('bundler: successfully browserified `' + module + '@' + version + '`.');
var result = { package: json, bundle: bundle };
+ // Save build metadata to c.builds
+ c.statuses.put(pkg, { module: module, version: version, ok: true });
+
inProgress[key].emit('bundle', result);
destroyInProgress();
@@ -146,6 +144,19 @@ module.exports = function bundler(opts) {
inProgress[key].emit('error', err);
destroyInProgress();
+ c.statuses.db.put(pkg, {
+ module: module,
+ version: version,
+ ok: false,
+ error: xtend(
+ {
+ message: err.message,
+ stack: err.stack
+ },
+ err
+ )
+ });
+
return cb(err);
}
@@ -157,7 +168,26 @@ module.exports = function bundler(opts) {
}
};
+ function alias(module, semver, callback) {
+ c.aliases.check({ module: module, semver: semver }, function resolve(cb) {
+ registry.resolve(module, semver, function (err, v) {
+ if (err) return callback(err);
+
+ cb(null, v);
+ });
+ }, callback);
+ }
+
+ _bundle.status = function status(module, semver, callback) {
+ registry.resolve(module, semver, function (err, versions) {
+ if (err) return callback(err);
+
+ _bundle.builds.get({ module: module, version: version }, callback);
+ });
+ }
+
_bundle.cache = c;
+ _bundle.alias = alias;
return _bundle;
};
View
@@ -10,7 +10,8 @@ var app = express(),
bundle = bundler(defaults());
var singular = require('./singular'),
- multiple = require('./multiple');
+ multiple = require('./multiple'),
+ statuses = require('./statuses');
//
// Add static assets
@@ -31,6 +32,11 @@ singular(app, bundle);
multiple(app, bundle);
//
+// Build statuses
+//
+statuses(app, bundle);
+
+//
// Exports
//
exports.app = app;
View
@@ -1,6 +1,7 @@
{
"name": "browserify-cdn",
"version": "0.3.1",
+ "repository": "https://github.com/jesusabdullah/browserify-cdn.git",
"bin": {
"browserify-cdn": "./bin/browserify-cdn"
},
@@ -19,14 +20,15 @@
"level-sublevel": "~5.2.0",
"cors": "~2.1.1",
"npm-publish-stream": "~0.3.1",
- "glob": "3.2.6"
+ "glob": "3.2.6",
+ "xtend": "~2.1.2"
},
"bundledDependencies": [
"temp",
"scopedfs"
],
"scripts": {
- "test": "tap ./test/*.js && rm -rf ./test/cdn.db",
+ "test": "rm -rf ./cdn.db && rm -rf ./test/cdn.db && tap ./test/*.js",
"start": "node ./bin/browserify-cdn",
"index-page": "marked README.md | sed s_http://wzrd.in/standalone/concat-stream@latest_/standalone/concat-stream@latest_ | sed 's_<p>Also, <a href=\"http://wzrd.in\">wzrd.in</a> has a nice url generating form.</p>_<form method=\"get\" id=\"url-generator\"><h3>Or choose a module here:</h3><label>Module:<input type=\"text\" name=\"module\" value=\"concat-stream\" required /></label><label>Version:<input type=\"text\" name=\"ver\" value=\"latest\" required /></label><input type=\"submit\" value=\"Go!\" /></form>_' | exercise-bike --readme :stdin: ./templates/index.handlebars ./public/index.html"
},
View
@@ -0,0 +1,49 @@
+var stringifyError = require('./stringify-error');
+
+module.exports = function (app, bundle) {
+ //
+ // Build statuses
+ //
+ app.get('/status/:module', status(bundle));
+};
+
+function status(bundle) {
+ return function (req, res) {
+ var t = req.params.module.split('@'),
+ module = t.shift(),
+ semver,
+ subfile = module.split('/');
+
+ var serve = serveBundle(res);
+
+ if (t.length) {
+ semver = t.shift();
+ }
+ else {
+ semver = 'latest';
+ }
+
+ if (subfile.length > 1) {
+ module = subfile.shift();
+ subfile = subfile.join('/');
+ }
+
+ bundle.status(module, semver, function (err, st) {
+ if (err) {
+ if (err.code == 'NotFoundError') {
+ return res.json(404, {
+ message: 'This module has not been built yet.',
+ hints: 'Try GETing /bundle/' + module + '@' + version
+ });
+ }
+
+ return res.json(500, {
+ ok: false,
+ message: err.message,
+ hints: stringifyError.goodbye
+ });
+ }
+ res.json(st);
+ });
+ };
+}
View
@@ -88,7 +88,7 @@ tap.test('multi-bundles are cached the second time', function (t) {
});
tap.test('teardown', function (t) {
- rimraf(path.resolve(__dirname, '../cdn.db'), function (err) {
+ rimraf('./cdn.db', function (err) {
t.error(err, 'removed the database');
t.end();
setTimeout(function () {
View
@@ -55,7 +55,7 @@ tap.test('singular bundles of standalone core modules build the first time', fun
});
tap.test('teardown', function (t) {
- rimraf(path.resolve(__dirname, '../cdn.db'), function (err) {
+ rimraf('../cdn.db', function (err) {
t.error(err, 'removed the database');
t.end();
setTimeout(function () {

0 comments on commit 9edb499

Please sign in to comment.