2020-08-26 04:54:58 +02:00
|
|
|
const winston = require('winston');
|
|
|
|
const Busboy = require('busboy');
|
2011-11-18 21:51:38 +01:00
|
|
|
|
|
|
|
// For handling serving stored documents
|
|
|
|
|
2020-08-26 04:54:58 +02:00
|
|
|
const DocumentHandler = function(options) {
|
|
|
|
if (!options) options = new Object;
|
2011-11-28 22:46:59 +01:00
|
|
|
this.keyLength = options.keyLength || DocumentHandler.defaultKeyLength;
|
|
|
|
this.maxLength = options.maxLength; // none by default
|
|
|
|
this.store = options.store;
|
2012-01-07 17:35:11 +01:00
|
|
|
this.keyGenerator = options.keyGenerator;
|
2011-11-18 21:51:38 +01:00
|
|
|
};
|
|
|
|
|
2011-11-28 22:46:59 +01:00
|
|
|
DocumentHandler.defaultKeyLength = 10;
|
|
|
|
|
2011-11-18 22:45:48 +01:00
|
|
|
// Handle retrieving a document
|
2020-08-27 00:39:06 +02:00
|
|
|
DocumentHandler.prototype.handleGet = async function(key, res, skipExpire) {
|
|
|
|
await this.store.get(key, function(ret) {
|
2011-11-18 23:12:28 +01:00
|
|
|
if (ret) {
|
|
|
|
winston.verbose('retrieved document', { key: key });
|
2020-08-26 04:54:58 +02:00
|
|
|
res.writeHead(200, { 'content-type': 'application/json' });
|
|
|
|
res.end(JSON.stringify({ data: ret, key: key }));
|
2011-11-18 23:12:28 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
winston.warn('document not found', { key: key });
|
2020-08-26 04:54:58 +02:00
|
|
|
res.writeHead(404, { 'content-type': 'application/json' });
|
|
|
|
res.end(JSON.stringify({ message: 'Document not found.' }));
|
2011-11-18 23:12:28 +01:00
|
|
|
}
|
2011-11-28 07:13:14 +01:00
|
|
|
}, skipExpire);
|
2011-11-18 21:51:38 +01:00
|
|
|
};
|
|
|
|
|
2011-12-16 13:57:09 +01:00
|
|
|
// Handle retrieving the raw version of a document
|
2020-08-27 00:39:06 +02:00
|
|
|
DocumentHandler.prototype.handleGetRaw = async function(key, res, skipExpire) {
|
|
|
|
await this.store.get(key, function(ret) {
|
2011-12-16 13:57:09 +01:00
|
|
|
if (ret) {
|
|
|
|
winston.verbose('retrieved raw document', { key: key });
|
2020-08-26 04:54:58 +02:00
|
|
|
res.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' });
|
|
|
|
res.end(ret);
|
2011-12-16 13:57:09 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
winston.warn('raw document not found', { key: key });
|
2020-08-26 04:54:58 +02:00
|
|
|
res.writeHead(404, { 'content-type': 'application/json' });
|
|
|
|
res.end(JSON.stringify({ message: 'Document not found.' }));
|
2011-12-16 13:57:09 +01:00
|
|
|
}
|
|
|
|
}, skipExpire);
|
|
|
|
};
|
|
|
|
|
2011-11-18 22:45:48 +01:00
|
|
|
// Handle adding a new Document
|
2020-08-27 00:39:06 +02:00
|
|
|
DocumentHandler.prototype.handlePost = async function (req, res) {
|
2020-08-26 04:54:58 +02:00
|
|
|
let _this = this;
|
|
|
|
let buffer = '';
|
|
|
|
let cancelled = false;
|
2014-04-21 20:16:23 +02:00
|
|
|
|
|
|
|
// What to do when done
|
2020-08-27 00:39:06 +02:00
|
|
|
let onSuccess = async function () {
|
2014-04-21 20:16:23 +02:00
|
|
|
// Check length
|
2011-11-21 16:17:23 +01:00
|
|
|
if (_this.maxLength && buffer.length > _this.maxLength) {
|
2011-12-19 21:20:22 +01:00
|
|
|
cancelled = true;
|
2011-11-22 03:48:09 +01:00
|
|
|
winston.warn('document >maxLength', { maxLength: _this.maxLength });
|
2020-08-26 04:54:58 +02:00
|
|
|
res.writeHead(400, { 'content-type': 'application/json' });
|
|
|
|
res.end(
|
2012-01-21 21:19:55 +01:00
|
|
|
JSON.stringify({ message: 'Document exceeds maximum length.' })
|
|
|
|
);
|
2014-04-21 20:16:23 +02:00
|
|
|
return;
|
2011-11-21 16:17:23 +01:00
|
|
|
}
|
2014-04-21 20:16:23 +02:00
|
|
|
// And then save if we should
|
2020-08-27 00:39:06 +02:00
|
|
|
await _this.chooseKey(async function (key) {
|
|
|
|
await _this.store.set(key, buffer, function (resp) {
|
2020-08-26 04:54:58 +02:00
|
|
|
if (resp) {
|
2011-11-22 03:48:09 +01:00
|
|
|
winston.verbose('added document', { key: key });
|
2020-08-26 04:54:58 +02:00
|
|
|
res.writeHead(200, { 'content-type': 'application/json' });
|
|
|
|
res.end(JSON.stringify({ key: key }));
|
2011-11-22 03:48:09 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
winston.verbose('error adding document');
|
2020-08-26 04:54:58 +02:00
|
|
|
res.writeHead(500, { 'content-type': 'application/json' });
|
|
|
|
res.end(JSON.stringify({ message: 'Error adding document.' }));
|
2011-11-22 03:48:09 +01:00
|
|
|
}
|
|
|
|
});
|
2011-11-18 23:12:28 +01:00
|
|
|
});
|
2014-04-21 20:16:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// If we should, parse a form to grab the data
|
2020-08-26 04:54:58 +02:00
|
|
|
let ct = req.headers['content-type'];
|
|
|
|
if (ct && ct.split(';')[0] == 'multipart/form-data') {
|
|
|
|
let busboy = new Busboy({ headers: req.headers });
|
2014-04-21 20:16:23 +02:00
|
|
|
busboy.on('field', function (fieldname, val) {
|
2020-08-26 04:54:58 +02:00
|
|
|
if (fieldname == 'data') {
|
2014-04-21 20:16:23 +02:00
|
|
|
buffer = val;
|
|
|
|
}
|
|
|
|
});
|
2020-08-27 00:39:06 +02:00
|
|
|
busboy.on('finish', async function () {
|
|
|
|
await onSuccess();
|
2014-04-21 20:16:23 +02:00
|
|
|
});
|
2020-08-26 04:54:58 +02:00
|
|
|
req.pipe(busboy);
|
2014-04-21 20:16:23 +02:00
|
|
|
// Otherwise, use our own and just grab flat data from POST body
|
|
|
|
} else {
|
2020-08-26 04:54:58 +02:00
|
|
|
req.on('data', function (data) {
|
2014-04-21 20:16:23 +02:00
|
|
|
buffer += data.toString();
|
|
|
|
});
|
2020-08-27 00:39:06 +02:00
|
|
|
req.on('end', async function () {
|
2014-04-21 20:16:23 +02:00
|
|
|
if (cancelled) { return; }
|
2020-08-27 00:39:06 +02:00
|
|
|
await onSuccess();
|
2014-04-21 20:16:23 +02:00
|
|
|
});
|
2020-08-26 04:54:58 +02:00
|
|
|
req.on('error', function (error) {
|
2014-04-21 20:16:23 +02:00
|
|
|
winston.error('connection error: ' + error.message);
|
2020-08-26 04:54:58 +02:00
|
|
|
res.writeHead(500, { 'content-type': 'application/json' });
|
|
|
|
res.end(JSON.stringify({ message: 'Connection error.' }));
|
2014-04-21 20:16:23 +02:00
|
|
|
cancelled = true;
|
|
|
|
});
|
|
|
|
}
|
2011-11-18 21:51:38 +01:00
|
|
|
};
|
|
|
|
|
2012-01-13 17:16:42 +01:00
|
|
|
// Keep choosing keys until one isn't taken
|
2020-08-27 00:39:06 +02:00
|
|
|
DocumentHandler.prototype.chooseKey = async function(callback) {
|
2020-08-26 04:54:58 +02:00
|
|
|
let key = this.acceptableKey();
|
|
|
|
let _this = this;
|
2020-08-27 00:39:06 +02:00
|
|
|
await this.store.get(key, function(ret) {
|
2011-11-22 03:48:09 +01:00
|
|
|
if (ret) {
|
|
|
|
_this.chooseKey(callback);
|
|
|
|
} else {
|
|
|
|
callback(key);
|
|
|
|
}
|
2018-02-16 15:52:44 +01:00
|
|
|
}, true); // Don't bump expirations when key searching
|
2011-11-22 03:48:09 +01:00
|
|
|
};
|
|
|
|
|
2012-01-13 17:16:42 +01:00
|
|
|
DocumentHandler.prototype.acceptableKey = function() {
|
|
|
|
return this.keyGenerator.createKey(this.keyLength);
|
|
|
|
};
|
|
|
|
|
2011-11-18 21:51:38 +01:00
|
|
|
module.exports = DocumentHandler;
|