1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2025-09-05 21:10:16 +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

@ -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;