-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from SergioCrisostomo/restore-hash
Restore builder hashes.
- Loading branch information
Showing
17 changed files
with
321 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ node_modules/ | |
*.DS_Store | ||
*.tern-port | ||
config/api_keys.json | ||
config/databases.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"core": "tests/database/core.db", | ||
"more": "tests/database/more.db" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
"use strict"; | ||
|
||
var path = require('path'); | ||
var async = require('async'); | ||
var sqlite3 = require('sqlite3'); | ||
var md5 = require('md5'); | ||
var waitForIt = require('../lib/waitForIt'); | ||
|
||
function BuilderDatabase(paths){ | ||
this.paths = paths; | ||
this.databases = {}; | ||
} | ||
|
||
BuilderDatabase.prototype.getDatabase = function(project, callback){ | ||
var getDB = this.databases[project]; | ||
if (getDB){ | ||
return getDB.get(callback); | ||
} | ||
var dbPath = path.join(__dirname, '..', this.paths[project]); | ||
if (!path){ | ||
return callback(Error('No database found for "' + project + '".')); | ||
} | ||
getDB = waitForIt(function(cb){ | ||
var db = new sqlite3.Database(dbPath, sqlite3.OPEN_READWRITE, function(error){ | ||
if (error) cb(error); | ||
else cb(null, db); | ||
}); | ||
}); | ||
getDB.get(callback); | ||
}; | ||
|
||
BuilderDatabase.prototype.loadHash = function(project, hash, callback){ | ||
function getHash(db, cb){ | ||
db.get('SELECT * FROM hashes WHERE md5 = ?', {1: hash}, function(err, row){ | ||
cb(err, row ? {hash: row.md5, packages: row.packages.split(';')} : null); | ||
}); | ||
} | ||
async.compose( | ||
getHash, | ||
this.getDatabase.bind(this, project) | ||
)(callback); | ||
}; | ||
|
||
BuilderDatabase.prototype.saveHash = function(project, packages, callback){ | ||
if (!packages || !packages.length){ | ||
if (callback) callback(null); | ||
return; | ||
} | ||
|
||
var packageString = typeof packages == 'string' ? packages : packages.join(';'); | ||
var hash = md5.digest_s(packageString); | ||
|
||
function hashCount(db, cb){ | ||
db.get('SELECT COUNT(*) AS count FROM hashes WHERE md5 = ?', {1: hash}, function(err, row){ | ||
cb(err, row && {count: row.count, db: db}); | ||
}); | ||
} | ||
function insertIfNotExisting(res, cb){ | ||
if (res.hash){ | ||
cb(null, {hash: hash, packages: packages}); | ||
} else { | ||
var values = {1: hash, 2: packageString, 3: Math.round(Date.now() / 1000)}; | ||
res.db.run('INSERT INTO hashes (md5, packages, date) VALUES (?, ?, ?)', values, function(error){ | ||
if (error) cb(error); | ||
else cb(null, {hash: hash, packages: packages}); | ||
}); | ||
} | ||
} | ||
|
||
async.compose( | ||
insertIfNotExisting, | ||
hashCount, | ||
this.getDatabase.bind(this, project) | ||
)(callback); | ||
}; | ||
|
||
module.exports = function(paths){ | ||
var db = new BuilderDatabase(paths); | ||
return { | ||
load: db.loadHash.bind(db), | ||
save: db.saveHash.bind(db) | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"use strict"; | ||
|
||
var builderHash = require('../lib/BuilderHash')(require('../config/databases.json')); | ||
|
||
module.exports = function(project){ | ||
|
||
return function(req, res, next){ | ||
var hash = req.params.hash; | ||
if (hash){ | ||
builderHash.load(project, hash, function(err, data) { | ||
if (data){ | ||
res.locals.hash = data.packages; | ||
} | ||
next(err); | ||
}); | ||
} else { | ||
return next(); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.