1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2025-09-05 21:10:16 +02:00

Huge refactor, switched to express

This commit is contained in:
zneix 2020-08-26 04:54:58 +02:00
parent 06315a91a8
commit c585e3b815
22 changed files with 813 additions and 406 deletions

View file

@ -1,18 +1,18 @@
/*global require,module,process*/
var AWS = require('aws-sdk');
var winston = require('winston');
const AWS = require('aws-sdk');
const winston = require('winston');
var AmazonS3DocumentStore = function(options) {
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) {
var _this = this;
const _this = this;
var req = {
const req = {
Bucket: _this.bucket,
Key: key
};
@ -31,9 +31,9 @@ AmazonS3DocumentStore.prototype.get = function(key, callback, skipExpire) {
}
AmazonS3DocumentStore.prototype.set = function(key, data, callback, skipExpire) {
var _this = this;
const _this = this;
var req = {
const req = {
Bucket: _this.bucket,
Key: key,
Body: data,

View file

@ -1,20 +1,20 @@
var fs = require('fs');
var crypto = require('crypto');
const fs = require('fs');
const crypto = require('crypto');
var winston = require('winston');
const winston = require('winston');
// For storing in files
// options[type] = file
// options[path] - Where to store
var FileDocumentStore = function(options) {
const FileDocumentStore = function(options) {
this.basePath = options.path || './data';
this.expire = options.expire;
};
// Generate md5 of a string
FileDocumentStore.md5 = function(str) {
var md5sum = crypto.createHash('md5');
let md5sum = crypto.createHash('md5');
md5sum.update(str);
return md5sum.digest('hex');
};
@ -23,9 +23,9 @@ FileDocumentStore.md5 = function(str) {
// be passed here
FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
try {
var _this = this;
const _this = this;
fs.mkdir(this.basePath, '700', function() {
var fn = _this.basePath + '/' + FileDocumentStore.md5(key);
const fn = _this.basePath + '/' + FileDocumentStore.md5(key);
fs.writeFile(fn, data, 'utf8', function(err) {
if (err) {
callback(false);
@ -45,8 +45,8 @@ 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;
var fn = this.basePath + '/' + FileDocumentStore.md5(key);
const _this = this;
const fn = this.basePath + '/' + FileDocumentStore.md5(key);
fs.readFile(fn, 'utf8', function(err, data) {
if (err) {
callback(false);

View file

@ -1,12 +1,12 @@
/*global require,module,process*/
var postgres = require('pg');
var winston = require('winston');
const postgres = require('pg');
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
var PostgresDocumentStore = function (options) {
const PostgresDocumentStore = function (options) {
this.expireJS = options.expire;
this.connectionUrl = process.env.DATABASE_URL || options.connectionUrl;
};
@ -15,8 +15,8 @@ PostgresDocumentStore.prototype = {
// Set a given key
set: function (key, data, callback, skipExpire) {
var now = Math.floor(new Date().getTime() / 1000);
var that = this;
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)', [
@ -36,8 +36,8 @@ PostgresDocumentStore.prototype = {
// Get a given key's data
get: function (key, callback, skipExpire) {
var now = Math.floor(new Date().getTime() / 1000);
var that = this;
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) {

View file

@ -1,5 +1,5 @@
var redis = require('redis');
var winston = require('winston');
const redis = require('redis');
const winston = require('winston');
// For storing in redis
// options[type] = redis