Remove hashlib dependency and switch to mocha for testing
This commit is contained in:
parent
b251978422
commit
6e4c087319
7 changed files with 42 additions and 65 deletions
|
@ -86,9 +86,9 @@ DocumentHandler.prototype.handlePost = function(request, response) {
|
|||
});
|
||||
};
|
||||
|
||||
// Get a random key that hasn't been already used
|
||||
// Keep choosing keys until one isn't taken
|
||||
DocumentHandler.prototype.chooseKey = function(callback) {
|
||||
var key = this.keyGenerator.createKey(this.keyLength);
|
||||
var key = this.acceptableKey();
|
||||
var _this = this;
|
||||
this.store.get(key, function(ret) {
|
||||
if (ret) {
|
||||
|
@ -99,4 +99,8 @@ DocumentHandler.prototype.chooseKey = function(callback) {
|
|||
});
|
||||
};
|
||||
|
||||
DocumentHandler.prototype.acceptableKey = function() {
|
||||
return this.keyGenerator.createKey(this.keyLength);
|
||||
};
|
||||
|
||||
module.exports = DocumentHandler;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
var fs = require('fs');
|
||||
var crypto = require('crypto');
|
||||
|
||||
var winston = require('winston');
|
||||
var hashlib = require('hashlib');
|
||||
|
||||
// For storing in files
|
||||
// options[type] = file
|
||||
|
@ -12,12 +12,19 @@ var FileDocumentStore = function(options) {
|
|||
this.expire = options.expire;
|
||||
};
|
||||
|
||||
// Generate md5 of a string
|
||||
FileDocumentStore.md5 = function(str) {
|
||||
var md5sum = crypto.createHash('md5');
|
||||
md5sum.update(str);
|
||||
return md5sum.digest('hex');
|
||||
};
|
||||
|
||||
// Save data in a file, key as md5 - since we don't know what we could be passed here
|
||||
FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
|
||||
try {
|
||||
var _this = this;
|
||||
fs.mkdir(this.basePath, '700', function() {
|
||||
fs.writeFile(_this.basePath + '/' + hashlib.md5(key), data, 'utf8', function(err) {
|
||||
fs.writeFile(_this.basePath + '/' + _this.md5(key), data, 'utf8', function(err) {
|
||||
if (err) {
|
||||
callback(false);
|
||||
}
|
||||
|
@ -37,7 +44,7 @@ FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
|
|||
// Get data from a file from key
|
||||
FileDocumentStore.prototype.get = function(key, callback, skipExpire) {
|
||||
var _this = this;
|
||||
fs.readFile(this.basePath + '/' + hashlib.md5(key), 'utf8', function(err, data) {
|
||||
fs.readFile(this.basePath + '/' + _this..md5(key), 'utf8', function(err, data) {
|
||||
if (err) {
|
||||
callback(false);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
var RandomKeyGenerator = function(options) {
|
||||
if (!options) {
|
||||
options = {};
|
||||
}
|
||||
this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
};
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
var redis = require('redis');
|
||||
var winston = require('winston');
|
||||
var hashlib = require('hashlib');
|
||||
|
||||
// For storing in redis
|
||||
// options[type] = redis
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue