1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2025-09-04 04:50:14 +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,12 +1,10 @@
var winston = require('winston');
var Busboy = require('busboy');
const winston = require('winston');
const Busboy = require('busboy');
// For handling serving stored documents
var DocumentHandler = function(options) {
if (!options) {
options = {};
}
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;
@ -16,98 +14,98 @@ var DocumentHandler = function(options) {
DocumentHandler.defaultKeyLength = 10;
// Handle retrieving a document
DocumentHandler.prototype.handleGet = function(key, response, skipExpire) {
DocumentHandler.prototype.handleGet = function(key, res, skipExpire) {
this.store.get(key, function(ret) {
if (ret) {
winston.verbose('retrieved document', { key: key });
response.writeHead(200, { 'content-type': 'application/json' });
response.end(JSON.stringify({ data: ret, 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 });
response.writeHead(404, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'Document not found.' }));
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.handleRawGet = function(key, response, skipExpire) {
DocumentHandler.prototype.handleGetRaw = function(key, res, skipExpire) {
this.store.get(key, function(ret) {
if (ret) {
winston.verbose('retrieved raw document', { key: key });
response.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' });
response.end(ret);
res.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' });
res.end(ret);
}
else {
winston.warn('raw document not found', { key: key });
response.writeHead(404, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'Document not found.' }));
res.writeHead(404, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Document not found.' }));
}
}, skipExpire);
};
// Handle adding a new Document
DocumentHandler.prototype.handlePost = function (request, response) {
var _this = this;
var buffer = '';
var cancelled = false;
DocumentHandler.prototype.handlePost = function (req, res) {
let _this = this;
let buffer = '';
let cancelled = false;
// What to do when done
var onSuccess = function () {
let onSuccess = function () {
// Check length
if (_this.maxLength && buffer.length > _this.maxLength) {
cancelled = true;
winston.warn('document >maxLength', { maxLength: _this.maxLength });
response.writeHead(400, { 'content-type': 'application/json' });
response.end(
res.writeHead(400, { 'content-type': 'application/json' });
res.end(
JSON.stringify({ message: 'Document exceeds maximum length.' })
);
return;
}
// And then save if we should
_this.chooseKey(function (key) {
_this.store.set(key, buffer, function (res) {
if (res) {
_this.store.set(key, buffer, function (resp) {
if (resp) {
winston.verbose('added document', { key: key });
response.writeHead(200, { 'content-type': 'application/json' });
response.end(JSON.stringify({ key: key }));
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ key: key }));
}
else {
winston.verbose('error adding document');
response.writeHead(500, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: '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
var ct = request.headers['content-type'];
if (ct && ct.split(';')[0] === 'multipart/form-data') {
var busboy = new Busboy({ headers: request.headers });
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') {
if (fieldname == 'data') {
buffer = val;
}
});
busboy.on('finish', function () {
onSuccess();
});
request.pipe(busboy);
req.pipe(busboy);
// Otherwise, use our own and just grab flat data from POST body
} else {
request.on('data', function (data) {
req.on('data', function (data) {
buffer += data.toString();
});
request.on('end', function () {
req.on('end', function () {
if (cancelled) { return; }
onSuccess();
});
request.on('error', function (error) {
req.on('error', function (error) {
winston.error('connection error: ' + error.message);
response.writeHead(500, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'Connection error.' }));
res.writeHead(500, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Connection error.' }));
cancelled = true;
});
}
@ -115,8 +113,8 @@ DocumentHandler.prototype.handlePost = function (request, response) {
// Keep choosing keys until one isn't taken
DocumentHandler.prototype.chooseKey = function(callback) {
var key = this.acceptableKey();
var _this = this;
let key = this.acceptableKey();
let _this = this;
this.store.get(key, function(ret) {
if (ret) {
_this.chooseKey(callback);

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

View file

@ -4,7 +4,7 @@ module.exports = class DictionaryGenerator {
constructor(options, readyCallback) {
// Check options format
if (!options) throw Error('No options passed to generator');
if (!options) throw Error('No options passed to generator');
if (!options.path) throw Error('No dictionary path specified in options');
// Load dictionary

View file

@ -15,7 +15,7 @@ module.exports = class PhoneticKeyGenerator {
// Generate a phonetic key of alternating consonant & vowel
createKey(keyLength) {
let text = '';
const start = Math.round(Math.random());
const start = Math.floor(Math.random() * 2);
for (let i = 0; i < keyLength; i++) {
text += (i % 2 == start) ? randConsonant() : randVowel();

View file

@ -7,9 +7,9 @@ module.exports = class RandomKeyGenerator {
// Generate a key of the given length
createKey(keyLength) {
var text = '';
let text = '';
for (var i = 0; i < keyLength; i++) {
for (let i = 0; i < keyLength; i++) {
const index = Math.floor(Math.random() * this.keyspace.length);
text += this.keyspace.charAt(index);
}

13
lib/util.js Normal file
View file

@ -0,0 +1,13 @@
module.exports = class HasteUtils {
//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(', ');
}
}