-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restore hash #174
Restore hash #174
Changes from 4 commits
449a29b
f9ebb64
a37b5d4
5812937
0381191
f944570
be18961
a7ee207
cecc92c
18c03f5
1ccdd1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
var sqlite3 = require('sqlite3'), | ||
md5 = require('md5'); | ||
|
||
var databasePaths = {}, | ||
databases = {}; | ||
|
||
function getDatabase(project){ | ||
if (!databases[project]) { | ||
var path = databasePaths[project]; | ||
if (path){ | ||
databases[project] = new sqlite3.Database(path, sqlite3.OPEN_READWRITE, function(error){ | ||
if (error) throw error; | ||
}); | ||
} else { | ||
throw Error('No database found for "' + project + '".'); | ||
} | ||
} | ||
return databases[project]; | ||
} | ||
|
||
function loadHash(project, hash, callback){ | ||
getDatabase(project).get('SELECT * FROM hashes WHERE md5 = ?', {1: hash}, function(error, row){ | ||
var data = null; | ||
if (row) data = {hash: row.md5, packages: row.packages.split(';')}; | ||
if (callback) callback(data); | ||
}); | ||
} | ||
|
||
function saveHash(project, packages, callback){ | ||
if (packages && packages.length){ | ||
var db = getDatabase(project), | ||
packageString = typeof packages == 'string' ? packages : packages.join(';'), | ||
hash = md5.digest_s(packageString); | ||
db.get('SELECT COUNT(*) AS count FROM hashes WHERE md5 = ?', {1: hash}, function(error, row){ | ||
if (error) throw error; | ||
if (row.count){ | ||
if (callback) callback({hash: hash, packages: packages}); | ||
} else { | ||
var values = {1: hash, 2: packageString, 3: Math.round(Date.now() / 1000)}; | ||
db.run('INSERT INTO hashes (md5, packages, date) VALUES (?, ?, ?)', values, function(error){ | ||
if (error) throw error; | ||
if (callback) callback({hash: hash, packages: packages}); | ||
}); | ||
} | ||
}); | ||
} else { | ||
if (callback) callback(null); | ||
} | ||
} | ||
|
||
module.exports = function(paths){ | ||
for (var key in paths){ | ||
databasePaths[key] = paths[key]; | ||
} | ||
return { | ||
load: loadHash, | ||
save: saveHash | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be better if you create the functions where the module.exports = function(paths){
var db = new BuilderDatabase(paths);
return {load: db.load.bind(db), save: db.save.bind(db)};
}; As prime is still a dependency, you could use that, or just go the vanilla JS way, as you don't need anything special like inheritance anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quite possibly. I figured this way the things could be set up in a configuration part of the app, while in a next part you only need to |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,21 @@ var guides = require('../middleware/guides')('more', { | |
}); | ||
|
||
var project = 'more'; | ||
var lastVersion = require('../package.json')._projects[project].versions[0]; | ||
var pkgProject = require('../package.json')._projects[project]; | ||
var lastVersion = pkgProject.versions[0]; | ||
|
||
var builderHash = require('../middleware/builderHash')({ | ||
more: pkgProject.hashStorage | ||
}); | ||
|
||
function hash(req, res, next){ | ||
var hash = req.params.hash; | ||
if (!hash) return next(); | ||
builderHash.load(project, hash, function(data) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the thing I wrote above, you could pass the project into the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And as the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea was that you'd only need to call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the hash is required anyway, yes, export it from the thing I threw together. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, only thing i noticed was code duplication of this 'hash' function in core and more... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
res.locals.hash = data.packages; | ||
next(); | ||
}); | ||
} | ||
|
||
module.exports = function(app){ | ||
|
||
|
@@ -28,13 +42,15 @@ module.exports = function(app){ | |
}); | ||
}); | ||
|
||
app.get('/more/builder', function(req, res){ | ||
app.get('/more/builder/:hash?', hash, function(req, res){ | ||
var hash = req.params.hash; | ||
res.render('builder/index', { | ||
title: 'MooTools More Builder', | ||
navigation: 'more', | ||
page: 'builder', | ||
project: 'More', | ||
site: 'more', | ||
hashDependencies: res.locals.hash || [], | ||
version: lastVersion, | ||
dependencies: require('../builder/dependencies.js')(project, lastVersion) | ||
}); | ||
|
@@ -47,4 +63,11 @@ module.exports = function(app){ | |
app.get('/more/guides', more, guides.index); | ||
app.get('/more/guides/:guide', more, guides.article); | ||
|
||
// hash build redirect | ||
var regex = /more\/([a-z]+[0-9]+[a-z0-9]*|[0-9]+[a-z]+[a-z0-9]*)$/; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also makes it possible to have "custom" hashes, in case we'd want to do that for some reason. Old website functionality did have that as well, I don't know if any exist. |
||
app.get(regex, function(req, res){ | ||
var hash = req.url.match(regex)[1]; | ||
res.redirect('/more/builder#' + hash); | ||
}); | ||
|
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
var fs = require('fs'); | ||
var file = __dirname + '/../tests/database/test.db'; | ||
var exists = fs.existsSync(file); | ||
var sqlite3 = require('sqlite3').verbose(); | ||
var sampleData = require('../tests/database/sampleData.json'); | ||
|
||
if (!exists){ | ||
console.log('Creating DB file.'); | ||
fs.openSync(file, 'w'); | ||
} | ||
|
||
var db = new sqlite3.Database(file); | ||
db.serialize(function(){ | ||
|
||
if (!exists) db.run("CREATE TABLE hashes (md5, packages, date)"); | ||
|
||
if (!exists) sampleData.forEach(function (sample) { | ||
var values = sample; | ||
db.run('INSERT INTO hashes (md5, packages, date) VALUES (?, ?, ?)', values, function(error){ | ||
if (error) throw error; | ||
}); | ||
|
||
}); | ||
|
||
db.each("SELECT md5, packages, date FROM hashes", function(err, row){ | ||
console.log(row.md5); | ||
console.log(row.packages); | ||
console.log(row.date); | ||
console.log('.....................'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation? |
||
}); | ||
}); | ||
|
||
db.close(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[ | ||
{ | ||
"1": "e8f3003df2d0919c1091b11854a53e9b", | ||
"2": "Core/Core;Core/String;Core/Event;Core/Browser;Core/Class;Core/Element.Style;Core/Element.Event;Core/Element.Delegation;Core/Element.Dimensions;Core/Fx;Core/Fx.CSS;Core/Fx.Tween;Core/Fx.Morph;Core/Fx.Transitions;Core/Request.HTML;Core/Request.JSON;Core/Cookie;Core/DOMReady", | ||
"3": "1415017941" | ||
}, | ||
{ | ||
"1": "2aa60ee2887f132a617b2a49754ef7fd", | ||
"2": "Core/Core;Core/Array;Core/String;Core/Number;Core/Function;Core/Object;Core/Event;Core/Browser;Core/Request;Core/Request.HTML;Core/Request.JSON;Core/Cookie;Core/JSON", | ||
"3": "1415017221" | ||
}, | ||
{ | ||
"1": "d1d2b98d3cae0d77aa29422996e67802", | ||
"2": "Core/Core;Core/Event;Core/Element;Core/Element.Style;Core/Element.Delegation;Core/Element.Dimensions;Core/Fx;Core/Fx.Tween;Core/Fx.Morph;Core/JSON;Core/DOMReady", | ||
"3": "1414767773" | ||
}, | ||
{ | ||
"1": "73a246bd9f16ce89e3ab685ff1e08599", | ||
"2": "Core/Array;Core/Function;Core/Event;Core/Class;Core/Element;Core/Element.Event;Core/Fx", | ||
"3": "1414689276" | ||
}, | ||
{ | ||
"1": "22af847e17dad82b2f32682e0f617e3e", | ||
"2": "Core/Core;Core/Array;Core/String;Core/Number;Core/Function;Core/Object;Core/Event;Core/Browser;Core/Class;Core/Class.Extras;Core/Slick.Parser;Core/Slick.Finder;Core/Element;Core/Element.Style;Core/Element.Event;Core/Element.Dimensions;Core/Fx;Core/Fx.CSS;Core/Fx.Tween;Core/Fx.Morph;Core/Fx.Transitions;Core/Request;Core/Request.HTML;Core/Request.JSON;Core/Cookie;Core/JSON;Core/DOMReady", | ||
"3": "1414636765" | ||
} | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do nicer with
reduce
instead.And don't forget the
;
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And when you use the
reduce
version, you don't need the extra iife/function/closure.