Update document_handler.js

This commit is contained in:
wioniqle-q 2021-11-15 14:56:37 +03:00 committed by GitHub
commit 87b83bb438
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,9 +1,5 @@
var winston = require('winston');
var Busboy = require('busboy');
// For handling serving stored documents // For handling serving stored documents
const winston = require('winston'), Busboy = require('busboy'), DocumentHandler = function (options) {
var DocumentHandler = function(options) {
if (!options) { if (!options) {
options = {}; options = {};
} }
@ -13,57 +9,56 @@ var DocumentHandler = function(options) {
this.keyGenerator = options.keyGenerator; this.keyGenerator = options.keyGenerator;
}; };
DocumentHandler.defaultKeyLength = 10; DocumentHandler.defaultKeyLength = 10;
// Handle retrieving a document // Handle retrieving a document
DocumentHandler.prototype.handleGet = function(request, response, config) { DocumentHandler.prototype.handleGet = function (request, response, config) {
const key = request.params.id.split('.')[0]; const key = request.params.id.split('.')[0];
const skipExpire = !!config.documents[key]; const skipExpire = !!config.documents[key];
this.store.get(key, function(ret) { this.store.get(key, function (ret) {
if (ret) { if (ret) {
winston.verbose('retrieved document', { key: key }); winston.verbose('retrieved document', {key: key});
response.writeHead(200, { 'content-type': 'application/json' }); response.writeHead(200, {'content-type': 'application/json'});
if (request.method === 'HEAD') { if (request.method === 'HEAD') {
response.end(); response.end();
} else { } else {
response.end(JSON.stringify({ data: ret, key: key })); response.end(JSON.stringify({data: ret, key: key}));
} }
} } else {
else { winston.warn('document not found', {key: key});
winston.warn('document not found', { key: key }); response.writeHead(404, {'content-type': 'application/json'});
response.writeHead(404, { 'content-type': 'application/json' });
if (request.method === 'HEAD') { if (request.method === 'HEAD') {
response.end(); response.end();
} else { } else {
response.end(JSON.stringify({ message: 'Document not found.' })); response.end(JSON.stringify({message: 'Document not found.'}));
} }
} }
}, skipExpire); }, skipExpire);
}; };
// Handle retrieving the raw version of a document // Handle retrieving the raw version of a document
DocumentHandler.prototype.handleRawGet = function(request, response, config) { DocumentHandler.prototype.handleRawGet = function (request, response, config) {
const key = request.params.id.split('.')[0]; const key = request.params.id.split('.')[0];
const skipExpire = !!config.documents[key]; const skipExpire = !!config.documents[key];
this.store.get(key, function(ret) { this.store.get(key, function (ret) {
if (ret) { if (ret) {
winston.verbose('retrieved raw document', { key: key }); winston.verbose('retrieved raw document', {key: key});
response.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' }); response.writeHead(200, {'content-type': 'text/plain; charset=UTF-8'});
if (request.method === 'HEAD') { if (request.method === 'HEAD') {
response.end(); response.end();
} else { } else {
response.end(ret); response.end(ret);
} }
} } else {
else { winston.warn('raw document not found', {key: key});
winston.warn('raw document not found', { key: key }); response.writeHead(404, {'content-type': 'application/json'});
response.writeHead(404, { 'content-type': 'application/json' });
if (request.method === 'HEAD') { if (request.method === 'HEAD') {
response.end(); response.end();
} else { } else {
response.end(JSON.stringify({ message: 'Document not found.' })); response.end(JSON.stringify({message: 'Document not found.'}));
} }
} }
}, skipExpire); }, skipExpire);
@ -71,19 +66,15 @@ DocumentHandler.prototype.handleRawGet = function(request, response, config) {
// Handle adding a new Document // Handle adding a new Document
DocumentHandler.prototype.handlePost = function (request, response) { DocumentHandler.prototype.handlePost = function (request, response) {
var _this = this;
var buffer = '';
var cancelled = false;
// What to do when done // What to do when done
var onSuccess = function () { let _this = this, buffer = '', cancelled = false, onSuccess = function () {
// Check length // Check length
if (_this.maxLength && buffer.length > _this.maxLength) { if (_this.maxLength && buffer.length > _this.maxLength) {
cancelled = true; cancelled = true;
winston.warn('document >maxLength', { maxLength: _this.maxLength }); winston.warn('document >maxLength', {maxLength: _this.maxLength});
response.writeHead(400, { 'content-type': 'application/json' }); response.writeHead(400, {'content-type': 'application/json'});
response.end( response.end(
JSON.stringify({ message: 'Document exceeds maximum length.' }) JSON.stringify({message: 'Document exceeds maximum length.'})
); );
return; return;
} }
@ -91,23 +82,23 @@ DocumentHandler.prototype.handlePost = function (request, response) {
_this.chooseKey(function (key) { _this.chooseKey(function (key) {
_this.store.set(key, buffer, function (res) { _this.store.set(key, buffer, function (res) {
if (res) { if (res) {
winston.verbose('added document', { key: key }); winston.verbose('added document', {key: key});
response.writeHead(200, { 'content-type': 'application/json' }); response.writeHead(200, {'content-type': 'application/json'});
response.end(JSON.stringify({ key: key })); response.end(JSON.stringify({key: key}));
} } else {
else {
winston.verbose('error adding document'); winston.verbose('error adding document');
response.writeHead(500, { 'content-type': 'application/json' }); response.writeHead(500, {'content-type': 'application/json'});
response.end(JSON.stringify({ message: 'Error adding document.' })); response.end(JSON.stringify({message: 'Error adding document.'}));
} }
}); });
}); });
}; };
// If we should, parse a form to grab the data // If we should, parse a form to grab the data
var ct = request.headers['content-type']; const ct = request.headers['content-type'];
if (ct && ct.split(';')[0] === 'multipart/form-data') { if (ct) {
var busboy = new Busboy({ headers: request.headers }); if (ct.split(';')[0] === 'multipart/form-data') {
var busboy = new Busboy({headers: request.headers});
busboy.on('field', function (fieldname, val) { busboy.on('field', function (fieldname, val) {
if (fieldname === 'data') { if (fieldname === 'data') {
buffer = val; buffer = val;
@ -123,23 +114,42 @@ DocumentHandler.prototype.handlePost = function (request, response) {
buffer += data.toString(); buffer += data.toString();
}); });
request.on('end', function () { request.on('end', function () {
if (cancelled) { return; } if (cancelled) {
return;
}
onSuccess(); onSuccess();
}); });
request.on('error', function (error) { request.on('error', function (error) {
winston.error('connection error: ' + error.message); winston.error('connection error: ' + error.message);
response.writeHead(500, { 'content-type': 'application/json' }); response.writeHead(500, {'content-type': 'application/json'});
response.end(JSON.stringify({ message: 'Connection error.' })); response.end(JSON.stringify({message: 'Connection error.'}));
cancelled = true;
});
}
} else {
request.on('data', function (data) {
buffer += data.toString();
});
request.on('end', function () {
if (cancelled) {
return;
}
onSuccess();
});
request.on('error', function (error) {
winston.error('connection error: ' + error.message);
response.writeHead(500, {'content-type': 'application/json'});
response.end(JSON.stringify({message: 'Connection error.'}));
cancelled = true; cancelled = true;
}); });
} }
}; };
// Keep choosing keys until one isn't taken // Keep choosing keys until one isn't taken
DocumentHandler.prototype.chooseKey = function(callback) { DocumentHandler.prototype.chooseKey = function (callback) {
var key = this.acceptableKey(); const key = this.acceptableKey();
var _this = this; const _this = this;
this.store.get(key, function(ret) { this.store.get(key, function (ret) {
if (ret) { if (ret) {
_this.chooseKey(callback); _this.chooseKey(callback);
} else { } else {
@ -148,7 +158,7 @@ DocumentHandler.prototype.chooseKey = function(callback) {
}, true); // Don't bump expirations when key searching }, true); // Don't bump expirations when key searching
}; };
DocumentHandler.prototype.acceptableKey = function() { DocumentHandler.prototype.acceptableKey = function () {
return this.keyGenerator.createKey(this.keyLength); return this.keyGenerator.createKey(this.keyLength);
}; };