1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2025-09-04 04:50:14 +02:00

Updated eslint rules

This commit is contained in:
zneix 2020-08-28 04:39:03 +02:00
parent 4a583a52ce
commit e4eeec3d27
19 changed files with 1096 additions and 550 deletions

View file

@ -3,129 +3,129 @@ const Busboy = require('busboy');
// For handling serving stored documents
const DocumentHandler = function(options) {
if (!options) options = new Object;
this.keyLength = options.keyLength || DocumentHandler.defaultKeyLength;
this.maxLength = options.maxLength; // none by default
this.store = options.store;
this.keyGenerator = options.keyGenerator;
const DocumentHandler = function(options){
if (!options) options = new Object;
this.keyLength = options.keyLength || DocumentHandler.defaultKeyLength;
this.maxLength = options.maxLength; // none by default
this.store = options.store;
this.keyGenerator = options.keyGenerator;
};
DocumentHandler.defaultKeyLength = 10;
// Handle retrieving a document
DocumentHandler.prototype.handleGet = async function(key, res, skipExpire) {
await this.store.get(key, function(ret) {
if (ret) {
winston.verbose('retrieved document', { key: key });
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ data: ret, key: key }));
}
else {
winston.warn('document not found', { key: key });
res.writeHead(404, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Document not found.' }));
}
}, skipExpire);
DocumentHandler.prototype.handleGet = async function(key, res, skipExpire){
await this.store.get(key, function(ret){
if (ret){
winston.verbose('retrieved document', { key: key });
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ data: ret, key: key }));
}
else {
winston.warn('document not found', { key: key });
res.writeHead(404, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Document not found.' }));
}
}, skipExpire);
};
// Handle retrieving the raw version of a document
DocumentHandler.prototype.handleGetRaw = async function(key, res, skipExpire) {
await this.store.get(key, function(ret) {
if (ret) {
winston.verbose('retrieved raw document', { key: key });
res.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' });
res.end(ret);
}
else {
winston.warn('raw document not found', { key: key });
res.writeHead(404, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Document not found.' }));
}
}, skipExpire);
DocumentHandler.prototype.handleGetRaw = async function(key, res, skipExpire){
await this.store.get(key, function(ret){
if (ret){
winston.verbose('retrieved raw document', { key: key });
res.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' });
res.end(ret);
}
else {
winston.warn('raw document not found', { key: key });
res.writeHead(404, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Document not found.' }));
}
}, skipExpire);
};
// Handle adding a new Document
DocumentHandler.prototype.handlePost = async function (req, res) {
let _this = this;
let buffer = '';
let cancelled = false;
DocumentHandler.prototype.handlePost = function (req, res){
let _this = this;
let buffer = '';
let cancelled = false;
// What to do when done
let onSuccess = async function () {
// Check length
if (_this.maxLength && buffer.length > _this.maxLength) {
cancelled = true;
winston.warn('document >maxLength', { maxLength: _this.maxLength });
res.writeHead(400, { 'content-type': 'application/json' });
res.end(
JSON.stringify({ message: 'Document exceeds maximum length.' })
);
return;
}
// And then save if we should
await _this.chooseKey(async function (key) {
await _this.store.set(key, buffer, function (resp) {
if (resp) {
winston.verbose('added document', { key: key });
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ key: key }));
}
else {
winston.verbose('error adding document');
res.writeHead(500, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Error adding document.' }));
}
});
});
};
// What to do when done
let onSuccess = async function (){
// Check length
if (_this.maxLength && buffer.length > _this.maxLength){
cancelled = true;
winston.warn('document >maxLength', { maxLength: _this.maxLength });
res.writeHead(400, { 'content-type': 'application/json' });
res.end(
JSON.stringify({ message: 'Document exceeds maximum length.' })
);
return;
}
// And then save if we should
await _this.chooseKey(async function (key){
await _this.store.set(key, buffer, function (resp){
if (resp){
winston.verbose('added document', { key: key });
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ key: key }));
}
else {
winston.verbose('error adding document');
res.writeHead(500, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Error adding document.' }));
}
});
});
};
// If we should, parse a form to grab the data
let ct = req.headers['content-type'];
if (ct && ct.split(';')[0] == 'multipart/form-data') {
let busboy = new Busboy({ headers: req.headers });
busboy.on('field', function (fieldname, val) {
if (fieldname == 'data') {
buffer = val;
}
});
busboy.on('finish', async function () {
await onSuccess();
});
req.pipe(busboy);
// Otherwise, use our own and just grab flat data from POST body
} else {
req.on('data', function (data) {
buffer += data.toString();
});
req.on('end', async function () {
if (cancelled) { return; }
await onSuccess();
});
req.on('error', function (error) {
winston.error('connection error: ' + error.message);
res.writeHead(500, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Connection error.' }));
cancelled = true;
});
}
// If we should, parse a form to grab the data
let ct = req.headers['content-type'];
if (ct && ct.split(';')[0] == 'multipart/form-data'){
let busboy = new Busboy({ headers: req.headers });
busboy.on('field', function (fieldname, val){
if (fieldname == 'data'){
buffer = val;
}
});
busboy.on('finish', async function (){
await onSuccess();
});
req.pipe(busboy);
// Otherwise, use our own and just grab flat data from POST body
} else {
req.on('data', function (data){
buffer += data.toString();
});
req.on('end', async function (){
if (cancelled){ return; }
await onSuccess();
});
req.on('error', function (error){
winston.error('connection error: ' + error.message);
res.writeHead(500, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Connection error.' }));
cancelled = true;
});
}
};
// Keep choosing keys until one isn't taken
DocumentHandler.prototype.chooseKey = async function(callback) {
let key = this.acceptableKey();
let _this = this;
await this.store.get(key, function(ret) {
if (ret) {
_this.chooseKey(callback);
} else {
callback(key);
}
}, true); // Don't bump expirations when key searching
DocumentHandler.prototype.chooseKey = async function(callback){
let key = this.acceptableKey();
let _this = this;
await this.store.get(key, function(ret){
if (ret){
_this.chooseKey(callback);
} else {
callback(key);
}
}, true); // Don't bump expirations when key searching
};
DocumentHandler.prototype.acceptableKey = function() {
return this.keyGenerator.createKey(this.keyLength);
DocumentHandler.prototype.acceptableKey = function(){
return this.keyGenerator.createKey(this.keyLength);
};
module.exports = DocumentHandler;

View file

@ -1,56 +1,54 @@
/*global require,module,process*/
const AWS = require('aws-sdk');
const winston = require('winston');
const AmazonS3DocumentStore = function(options) {
this.expire = options.expire;
this.bucket = options.bucket;
this.client = new AWS.S3({region: options.region});
const AmazonS3DocumentStore = function(options){
this.expire = options.expire;
this.bucket = options.bucket;
this.client = new AWS.S3({region: options.region});
};
AmazonS3DocumentStore.prototype.get = function(key, callback, skipExpire) {
const _this = this;
AmazonS3DocumentStore.prototype.get = function(key, callback, skipExpire){
const _this = this;
const req = {
Bucket: _this.bucket,
Key: key
};
const req = {
Bucket: _this.bucket,
Key: key
};
_this.client.getObject(req, function(err, data) {
if(err) {
callback(false);
}
else {
callback(data.Body.toString('utf-8'));
if (_this.expire && !skipExpire) {
winston.warn('amazon s3 store cannot set expirations on keys');
}
}
});
}
_this.client.getObject(req, function(err, data){
if(err){
callback(false);
}
else {
callback(data.Body.toString('utf-8'));
if (_this.expire && !skipExpire){
winston.warn('amazon s3 store cannot set expirations on keys');
}
}
});
};
AmazonS3DocumentStore.prototype.set = function(key, data, callback, skipExpire) {
const _this = this;
AmazonS3DocumentStore.prototype.set = function(key, data, callback, skipExpire){
const _this = this;
const req = {
Bucket: _this.bucket,
Key: key,
Body: data,
ContentType: 'text/plain'
};
const req = {
Bucket: _this.bucket,
Key: key,
Body: data,
ContentType: 'text/plain'
};
_this.client.putObject(req, function(err, data) {
if (err) {
callback(false);
}
else {
callback(true);
if (_this.expire && !skipExpire) {
winston.warn('amazon s3 store cannot set expirations on keys');
}
}
});
}
_this.client.putObject(req, function(err, data){
if (err){
callback(false);
}
else {
callback(true);
if (_this.expire && !skipExpire){
winston.warn('amazon s3 store cannot set expirations on keys');
}
}
});
};
module.exports = AmazonS3DocumentStore;

View file

@ -7,57 +7,57 @@ const winston = require('winston');
// options[type] = file
// options[path] - Where to store
const FileDocumentStore = function(options) {
this.basePath = options.path || './data';
this.expire = options.expire;
const FileDocumentStore = function(options){
this.basePath = options.path || './data';
this.expire = options.expire;
};
// Generate md5 of a string
FileDocumentStore.md5 = function(str) {
let md5sum = crypto.createHash('md5');
md5sum.update(str);
return md5sum.digest('hex');
FileDocumentStore.md5 = function(str){
let 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 {
const _this = this;
fs.mkdir(this.basePath, '700', function() {
const fn = _this.basePath + '/' + FileDocumentStore.md5(key);
fs.writeFile(fn, data, 'utf8', function(err) {
if (err) {
callback(false);
}
else {
callback(true);
if (_this.expire && !skipExpire) {
winston.warn('file store cannot set expirations on keys');
}
}
});
});
} catch(err) {
callback(false);
}
FileDocumentStore.prototype.set = function(key, data, callback, skipExpire){
try {
const _this = this;
fs.mkdir(this.basePath, '700', function(){
const fn = _this.basePath + '/' + FileDocumentStore.md5(key);
fs.writeFile(fn, data, 'utf8', function(err){
if (err){
callback(false);
}
else {
callback(true);
if (_this.expire && !skipExpire){
winston.warn('file store cannot set expirations on keys');
}
}
});
});
} catch(err){
callback(false);
}
};
// Get data from a file from key
FileDocumentStore.prototype.get = function(key, callback, skipExpire) {
const _this = this;
const fn = require('path').join(this.basePath, FileDocumentStore.md5(key));
fs.readFile(fn, 'utf8', function(err, data) {
if (err) {
callback(false);
}
else {
callback(data);
if (_this.expire && !skipExpire) {
winston.warn('file store cannot set expirations on keys');
}
}
});
FileDocumentStore.prototype.get = function(key, callback, skipExpire){
const _this = this;
const fn = require('path').join(this.basePath, FileDocumentStore.md5(key));
fs.readFile(fn, 'utf8', function(err, data){
if (err){
callback(false);
}
else {
callback(data);
if (_this.expire && !skipExpire){
winston.warn('file store cannot set expirations on keys');
}
}
});
};
module.exports = FileDocumentStore;

View file

@ -3,49 +3,49 @@ const winston = require('winston');
class MemcachedDocumentStore {
// Create a new store with options
constructor(options) {
this.expire = options.expire;
// Create a new store with options
constructor(options){
this.expire = options.expire;
const host = options.host || '127.0.0.1';
const port = options.port || 11211;
const url = `${host}:${port}`;
this.connect(url);
}
const host = options.host || '127.0.0.1';
const port = options.port || 11211;
const url = `${host}:${port}`;
this.connect(url);
}
// Create a connection
connect(url) {
this.client = new memcached(url);
// Create a connection
connect(url){
this.client = new memcached(url);
winston.info(`connecting to memcached on ${url}`);
winston.info(`connecting to memcached on ${url}`);
this.client.on('failure', function(error) {
winston.info('error connecting to memcached', {error});
});
}
this.client.on('failure', function(error){
winston.info('error connecting to memcached', {error});
});
}
// Save file in a key
set(key, data, callback, skipExpire) {
this.client.set(key, data, skipExpire ? 0 : this.expire, (error) => {
callback(!error);
});
}
// Save file in a key
set(key, data, callback, skipExpire){
this.client.set(key, data, skipExpire ? 0 : this.expire, (error) => {
callback(!error);
});
}
// Get a file from a key
get(key, callback, skipExpire) {
this.client.get(key, (error, data) => {
callback(error ? false : data);
// Get a file from a key
get(key, callback, skipExpire){
this.client.get(key, (error, data) => {
callback(error ? false : data);
// Update the key so that the expiration is pushed forward
if (!skipExpire) {
this.set(key, data, (updateSucceeded) => {
if (!updateSucceeded) {
winston.error('failed to update expiration on GET', {key});
}
}, skipExpire);
}
});
}
// Update the key so that the expiration is pushed forward
if (!skipExpire){
this.set(key, data, (updateSucceeded) => {
if (!updateSucceeded){
winston.error('failed to update expiration on GET', {key});
}
}, skipExpire);
}
});
}
}

View file

@ -11,7 +11,7 @@ MongoDocumentStore.prototype.set = async function (key, data, callback, skipExpi
const now = Math.floor(Date.now() / 1000);
const that = this;
this.safeConnect(async ( {error} = {} ) => {
await this.safeConnect(async ( {error} = {} ) => {
if (error) return callback(false);
await this.MongoClient.db().collection('entries').updateOne(
@ -32,13 +32,14 @@ MongoDocumentStore.prototype.set = async function (key, data, callback, skipExpi
{
upsert: true
}
).then((err, result) => {
return callback(true);
})
.catch((err, result) => {
winston.error('error updating mongodb document', { error: err });
return callback(false);
});
)
.then((err, result) => {
return callback(true);
})
.catch((err, result) => {
winston.error('error updating mongodb document', { error: err });
return callback(false);
});
});
};
@ -47,20 +48,20 @@ MongoDocumentStore.prototype.get = async function (key, callback, skipExpire){
const now = Math.floor(Date.now() / 1000);
const that = this;
this.safeConnect(async ( {error} = {} ) => {
await this.safeConnect(async ( {error} = {} ) => {
if (error) return callback(false);
let document = await this.MongoClient.db().collection('entries').findOne({
'entry_id': key,
or: [
$or: [
{ expiration: -1 },
{ expiration: { $gt: now } }
]
})
.catch(err => {
winston.error('error finding mongodb document', { error: err });
return callback(false);
});
.catch(err => {
winston.error('error finding mongodb document', { error: err });
return callback(false);
});
callback(document ? document.value : false);
@ -71,7 +72,7 @@ MongoDocumentStore.prototype.get = async function (key, callback, skipExpire){
).catch(err => {
return winston.warn('error extending expiry of mongodb document', { error: err });
});
winston.silly('extended expiry of mongodb document', { key: key, timestamp: that.expire + now })
winston.silly('extended expiry of mongodb document', { key: key, timestamp: that.expire + now });
}
});
};

View file

@ -1,4 +1,4 @@
/*global require,module,process*/
// /*global require,module,process*/
const postgres = require('pg');
const winston = require('winston');
@ -6,73 +6,74 @@ const winston = require('winston');
// create table entries (id serial primary key, key varchar(255) not null, value text not null, expiration int, unique(key));
// A postgres document store
const PostgresDocumentStore = function (options) {
this.expireJS = options.expire;
this.connectionUrl = process.env.DATABASE_URL || options.connectionUrl;
const PostgresDocumentStore = function (options){
this.expireJS = options.expire;
this.connectionUrl = process.env.DATABASE_URL || options.connectionUrl;
};
PostgresDocumentStore.prototype = {
// Set a given key
set: function (key, data, callback, skipExpire) {
const now = Math.floor(Date.now() / 1000);
const that = this;
this.safeConnect(function (err, client, done) {
if (err) { return callback(false); }
client.query('INSERT INTO entries (key, value, expiration) VALUES ($1, $2, $3)', [
key,
data,
that.expireJS && !skipExpire ? that.expireJS + now : null
], function (err) {
if (err) {
winston.error('error persisting value to postgres', { error: err });
return callback(false);
}
callback(true);
done();
});
});
},
// Set a given key
set: function (key, data, callback, skipExpire){
const now = Math.floor(Date.now() / 1000);
const that = this;
this.safeConnect(function (err, client, done){
if (err){ return callback(false); }
client.query('INSERT INTO entries (key, value, expiration) VALUES ($1, $2, $3)', [
key,
data,
that.expireJS && !skipExpire ? that.expireJS + now : null
], function (err){
if (err){
winston.error('error persisting value to postgres', { error: err });
return callback(false);
}
callback(true);
done();
});
});
},
// Get a given key's data
get: function (key, callback, skipExpire) {
const now = Math.floor(Date.now() / 1000);
const that = this;
this.safeConnect(function (err, client, done) {
if (err) { return callback(false); }
client.query('SELECT id,value,expiration from entries where KEY = $1 and (expiration IS NULL or expiration > $2)', [key, now], function (err, result) {
if (err) {
winston.error('error retrieving value from postgres', { error: err });
return callback(false);
}
callback(result.rows.length ? result.rows[0].value : false);
if (result.rows.length && that.expireJS && !skipExpire) {
client.query('UPDATE entries SET expiration = $1 WHERE ID = $2', [
that.expireJS + now,
result.rows[0].id
], function (err) {
if (!err) {
done();
}
});
} else {
done();
}
});
});
},
// Get a given key's data
get: function (key, callback, skipExpire){
const now = Math.floor(Date.now() / 1000);
const that = this;
this.safeConnect(function (err, client, done){
if (err){ return callback(false); }
client.query('SELECT id,value,expiration from entries where KEY = $1 and (expiration IS NULL or expiration > $2)', [key, now],
function (err, result){
if (err){
winston.error('error retrieving value from postgres', { error: err });
return callback(false);
}
callback(result.rows.length ? result.rows[0].value : false);
if (result.rows.length && that.expireJS && !skipExpire){
client.query('UPDATE entries SET expiration = $1 WHERE ID = $2', [
that.expireJS + now,
result.rows[0].id
], function (err){
if (!err){
done();
}
});
} else {
done();
}
});
});
},
// A connection wrapper
safeConnect: function (callback) {
postgres.connect(this.connectionUrl, function (err, client, done) {
if (err) {
winston.error('error connecting to postgres', { error: err });
callback(err);
} else {
callback(undefined, client, done);
}
});
}
// A connection wrapper
safeConnect: function (callback){
postgres.connect(this.connectionUrl, function (err, client, done){
if (err){
winston.error('error connecting to postgres', { error: err });
callback(err);
} else {
callback(undefined, client, done);
}
});
}
};

View file

@ -8,82 +8,82 @@ const winston = require('winston');
// options[db] - The db to use (default 0)
// options[expire] - The time to live for each key set (default never)
var RedisDocumentStore = function(options, client) {
this.expire = options.expire;
if (client) {
winston.info('using predefined redis client');
RedisDocumentStore.client = client;
} else if (!RedisDocumentStore.client) {
winston.info('configuring redis');
RedisDocumentStore.connect(options);
}
var RedisDocumentStore = function(options, client){
this.expire = options.expire;
if (client){
winston.info('using predefined redis client');
RedisDocumentStore.client = client;
} else if (!RedisDocumentStore.client){
winston.info('configuring redis');
RedisDocumentStore.connect(options);
}
};
// Create a connection according to config
RedisDocumentStore.connect = function(options) {
var host = options.host || '127.0.0.1';
var port = options.port || 6379;
var index = options.db || 0;
RedisDocumentStore.client = redis.createClient(port, host);
// authenticate if password is provided
if (options.password) {
RedisDocumentStore.client.auth(options.password);
}
RedisDocumentStore.connect = function(options){
var host = options.host || '127.0.0.1';
var port = options.port || 6379;
var index = options.db || 0;
RedisDocumentStore.client = redis.createClient(port, host);
// authenticate if password is provided
if (options.password){
RedisDocumentStore.client.auth(options.password);
}
RedisDocumentStore.client.on('error', function(err) {
winston.error('redis disconnected', err);
});
RedisDocumentStore.client.on('error', function(err){
winston.error('redis disconnected', err);
});
RedisDocumentStore.client.select(index, function(err) {
if (err) {
winston.error(
'error connecting to redis index ' + index,
{ error: err }
);
process.exit(1);
}
else {
winston.info('connected to redis on ' + host + ':' + port + '/' + index);
}
});
RedisDocumentStore.client.select(index, function(err){
if (err){
winston.error(
'error connecting to redis index ' + index,
{ error: err }
);
process.exit(1);
}
else {
winston.info('connected to redis on ' + host + ':' + port + '/' + index);
}
});
};
// Save file in a key
RedisDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
var _this = this;
RedisDocumentStore.client.set(key, data, function(err) {
if (err) {
callback(false);
}
else {
if (!skipExpire) {
_this.setExpiration(key);
}
callback(true);
}
});
RedisDocumentStore.prototype.set = function(key, data, callback, skipExpire){
var _this = this;
RedisDocumentStore.client.set(key, data, function(err){
if (err){
callback(false);
}
else {
if (!skipExpire){
_this.setExpiration(key);
}
callback(true);
}
});
};
// Expire a key in expire time if set
RedisDocumentStore.prototype.setExpiration = function(key) {
if (this.expire) {
RedisDocumentStore.client.expire(key, this.expire, function(err) {
if (err) {
winston.error('failed to set expiry on key: ' + key);
}
});
}
RedisDocumentStore.prototype.setExpiration = function(key){
if (this.expire){
RedisDocumentStore.client.expire(key, this.expire, function(err){
if (err){
winston.error('failed to set expiry on key: ' + key);
}
});
}
};
// Get a file from a key
RedisDocumentStore.prototype.get = function(key, callback, skipExpire) {
var _this = this;
RedisDocumentStore.client.get(key, function(err, reply) {
if (!err && !skipExpire) {
_this.setExpiration(key);
}
callback(err ? false : reply);
});
RedisDocumentStore.prototype.get = function(key, callback, skipExpire){
var _this = this;
RedisDocumentStore.client.get(key, function(err, reply){
if (!err && !skipExpire){
_this.setExpiration(key);
}
callback(err ? false : reply);
});
};
module.exports = RedisDocumentStore;

View file

@ -3,44 +3,44 @@ const rethink = require('rethinkdbdash');
const winston = require('winston');
const md5 = (str) => {
const md5sum = crypto.createHash('md5');
md5sum.update(str);
return md5sum.digest('hex');
const md5sum = crypto.createHash('md5');
md5sum.update(str);
return md5sum.digest('hex');
};
class RethinkDBStore {
constructor(options) {
this.client = rethink({
silent: true,
host: options.host || '127.0.0.1',
port: options.port || 28015,
db: options.db || 'haste',
user: options.user || 'admin',
password: options.password || ''
});
}
constructor(options){
this.client = rethink({
silent: true,
host: options.host || '127.0.0.1',
port: options.port || 28015,
db: options.db || 'haste',
user: options.user || 'admin',
password: options.password || ''
});
}
set(key, data, callback) {
this.client.table('uploads').insert({ id: md5(key), data: data }).run((error) => {
if (error) {
callback(false);
winston.error('failed to insert to table', error);
return;
}
callback(true);
});
}
set(key, data, callback){
this.client.table('uploads').insert({ id: md5(key), data: data }).run((error) => {
if (error){
callback(false);
winston.error('failed to insert to table', error);
return;
}
callback(true);
});
}
get(key, callback) {
this.client.table('uploads').get(md5(key)).run((error, result) => {
if (error || !result) {
callback(false);
if (error) winston.error('failed to insert to table', error);
return;
}
callback(result.data);
});
}
get(key, callback){
this.client.table('uploads').get(md5(key)).run((error, result) => {
if (error || !result){
callback(false);
if (error) winston.error('failed to insert to table', error);
return;
}
callback(result.data);
});
}
}
module.exports = RethinkDBStore;

View file

@ -2,31 +2,31 @@ const fs = require('fs');
module.exports = class DictionaryGenerator {
constructor(options, readyCallback) {
// Check options format
if (!options) throw Error('No options passed to generator');
if (!options.path) throw Error('No dictionary path specified in options');
constructor(options, readyCallback){
// Check options format
if (!options) throw Error('No options passed to generator');
if (!options.path) throw Error('No dictionary path specified in options');
// Load dictionary
fs.readFile(options.path, 'utf8', (err, data) => {
if (err) throw err;
// Load dictionary
fs.readFile(options.path, 'utf8', (err, data) => {
if (err) throw err;
this.dictionary = data.split(/[\n\r]+/);
this.dictionary = data.split(/[\n\r]+/);
if (readyCallback) readyCallback();
});
}
if (readyCallback) readyCallback();
});
}
// Generates a dictionary-based key, of keyLength words
createKey(keyLength) {
let text = '';
// Generates a dictionary-based key, of keyLength words
createKey(keyLength){
let text = '';
for (let i = 0; i < keyLength; i++) {
const index = Math.floor(Math.random() * this.dictionary.length);
text += this.dictionary[index];
}
for (let i = 0; i < keyLength; i++){
const index = Math.floor(Math.random() * this.dictionary.length);
text += this.dictionary[index];
}
return text;
}
return text;
}
};

View file

@ -1,9 +1,9 @@
// Draws inspiration from pwgen and http://tools.arantius.com/password
const randOf = (collection) => {
return () => {
return collection[Math.floor(Math.random() * collection.length)];
};
return () => {
return collection[Math.floor(Math.random() * collection.length)];
};
};
// Helper methods to get an random vowel or consonant
@ -12,16 +12,16 @@ const randConsonant = randOf('bcdfghjklmnpqrstvwxyz');
module.exports = class PhoneticKeyGenerator {
// Generate a phonetic key of alternating consonant & vowel
createKey(keyLength) {
let text = '';
const start = Math.floor(Math.random() * 2);
// Generate a phonetic key of alternating consonant & vowel
createKey(keyLength){
let text = '';
const start = Math.floor(Math.random() * 2);
for (let i = 0; i < keyLength; i++) {
text += (i % 2 == start) ? randConsonant() : randVowel();
}
for (let i = 0; i < keyLength; i++){
text += (i % 2 == start) ? randConsonant() : randVowel();
}
return text;
}
return text;
}
};

View file

@ -1,20 +1,20 @@
module.exports = class RandomKeyGenerator {
// Initialize a new generator with the given keySpace
constructor(options = {}) {
this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
}
// Initialize a new generator with the given keySpace
constructor(options = {}){
this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
}
// Generate a key of the given length
createKey(keyLength) {
let text = '';
// Generate a key of the given length
createKey(keyLength){
let text = '';
for (let i = 0; i < keyLength; i++) {
const index = Math.floor(Math.random() * this.keyspace.length);
text += this.keyspace.charAt(index);
}
for (let i = 0; i < keyLength; i++){
const index = Math.floor(Math.random() * this.keyspace.length);
text += this.keyspace.charAt(index);
}
return text;
}
return text;
}
};

View file

@ -1,13 +1,13 @@
module.exports = class HasteUtils {
//init class
constructor(options = {}){
//xd no options for now
}
//init class
constructor(options = {}){
//xd no options for now
}
//stringify json objects from winston message to cool "key=value" format
stringifyJSONMessagetoLogs(info){
//really KKona solution, but works for now
let keys = Object.keys(info).filter(k => k != 'message' && k != 'level');
return keys.map(k => `${k}=${info[k]}`).join(', ');
}
}
//stringify json objects from winston message to cool "key=value" format
stringifyJSONMessagetoLogs(info){
//really KKona solution, but works for now
let keys = Object.keys(info).filter(k => k != 'message' && k != 'level');
return keys.map(k => `${k}=${info[k]}`).join(', ');
}
};