Update document_handler.js

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

View File

@ -1,155 +1,165 @@
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 = {}; }
} this.keyLength = options.keyLength || DocumentHandler.defaultKeyLength;
this.keyLength = options.keyLength || DocumentHandler.defaultKeyLength; this.maxLength = options.maxLength; // none by default
this.maxLength = options.maxLength; // none by default this.store = options.store;
this.store = options.store; 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);
}; };
// 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; // What to do when done
var buffer = ''; let _this = this, buffer = '', cancelled = false, onSuccess = function () {
var cancelled = false; // 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(
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) {
winston.verbose('added document', {key: key});
response.writeHead(200, {'content-type': 'application/json'});
response.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.'}));
}
});
});
};
// What to do when done // If we should, parse a form to grab the data
var onSuccess = function () { const ct = request.headers['content-type'];
// Check length if (ct) {
if (_this.maxLength && buffer.length > _this.maxLength) { if (ct.split(';')[0] === 'multipart/form-data') {
cancelled = true; var busboy = new Busboy({headers: request.headers});
winston.warn('document >maxLength', { maxLength: _this.maxLength }); busboy.on('field', function (fieldname, val) {
response.writeHead(400, { 'content-type': 'application/json' }); if (fieldname === 'data') {
response.end( buffer = val;
JSON.stringify({ message: 'Document exceeds maximum length.' }) }
); });
return; busboy.on('finish', function () {
onSuccess();
});
request.pipe(busboy);
// Otherwise, use our own and just grab flat data from POST body
} 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;
});
}
} 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;
});
} }
// And then save if we should
_this.chooseKey(function (key) {
_this.store.set(key, buffer, function (res) {
if (res) {
winston.verbose('added document', { key: key });
response.writeHead(200, { 'content-type': 'application/json' });
response.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.' }));
}
});
});
};
// 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 });
busboy.on('field', function (fieldname, val) {
if (fieldname === 'data') {
buffer = val;
}
});
busboy.on('finish', function () {
onSuccess();
});
request.pipe(busboy);
// Otherwise, use our own and just grab flat data from POST body
} 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;
});
}
}; };
// 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 {
callback(key); callback(key);
} }
}, 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);
}; };
module.exports = DocumentHandler; module.exports = DocumentHandler;