haste-server/lib/document_handler.js

109 lines
3.4 KiB
JavaScript
Raw Normal View History

2011-11-18 20:51:38 +00:00
var winston = require('winston');
// For handling serving stored documents
var DocumentHandler = function(options) {
2011-11-28 21:46:59 +00:00
if (!options) {
options = {};
2011-11-18 21:57:23 +00:00
}
2011-11-28 21:46:59 +00:00
this.keyLength = options.keyLength || DocumentHandler.defaultKeyLength;
this.maxLength = options.maxLength; // none by default
this.store = options.store;
this.keyGenerator = options.keyGenerator;
2011-11-18 20:51:38 +00:00
};
2011-11-28 21:46:59 +00:00
DocumentHandler.defaultKeyLength = 10;
2011-11-18 21:45:48 +00:00
// Handle retrieving a document
2011-11-28 06:13:14 +00:00
DocumentHandler.prototype.handleGet = function(key, response, skipExpire) {
2011-11-18 22:54:57 +00:00
this.store.get(key, function(ret) {
2011-11-18 22:12:28 +00:00
if (ret) {
winston.verbose('retrieved document', { key: key });
response.writeHead(200, { 'content-type': 'application/json' });
response.end(JSON.stringify({ data: ret, key: key }));
}
else {
winston.warn('document not found', { key: key });
response.writeHead(404, { 'content-type': 'application/json' });
2011-12-19 17:44:12 +00:00
response.end(JSON.stringify({ message: 'Document not found.' }));
2011-11-18 22:12:28 +00:00
}
2011-11-28 06:13:14 +00:00
}, skipExpire);
2011-11-18 20:51:38 +00:00
};
// Handle retrieving the raw version of a document
DocumentHandler.prototype.handleRawGet = function(key, response, skipExpire) {
this.store.get(key, function(ret) {
if (ret) {
winston.verbose('retrieved raw document', { key: key });
response.writeHead(200, { 'content-type': 'text/plain' });
response.end(ret);
}
else {
winston.warn('raw document not found', { key: key });
response.writeHead(404, { 'content-type': 'application/json' });
2011-12-19 17:44:12 +00:00
response.end(JSON.stringify({ message: 'Document not found.' }));
}
}, skipExpire);
};
2011-11-18 21:45:48 +00:00
// Handle adding a new Document
2011-11-18 20:51:38 +00:00
DocumentHandler.prototype.handlePost = function(request, response) {
2011-11-18 22:54:57 +00:00
var _this = this;
2011-11-18 22:12:28 +00:00
var buffer = '';
var cancelled = false;
2011-11-18 20:51:38 +00:00
request.on('data', function(data) {
2011-11-18 22:12:28 +00:00
if (!buffer) {
2011-11-18 21:28:09 +00:00
response.writeHead(200, { 'content-type': 'application/json' });
}
buffer += data.toString();
2011-11-21 15:17:23 +00:00
if (_this.maxLength && buffer.length > _this.maxLength) {
cancelled = true;
2011-11-22 02:48:09 +00:00
winston.warn('document >maxLength', { maxLength: _this.maxLength });
2011-11-21 15:17:23 +00:00
response.writeHead(400, { 'content-type': 'application/json' });
response.end(
JSON.stringify({ message: 'Document exceeds maximum length.' })
);
2011-11-21 15:17:23 +00:00
}
2011-11-18 20:51:38 +00:00
});
request.on('end', function(end) {
if (cancelled) return;
2011-11-22 02:48:09 +00:00
_this.chooseKey(function(key) {
_this.store.set(key, buffer, function(res) {
if (res) {
winston.verbose('added document', { key: key });
response.end(JSON.stringify({ key: key }));
}
else {
winston.verbose('error adding document');
response.writeHead(500, { 'content-type': 'application/json' });
2011-12-19 17:44:12 +00:00
response.end(JSON.stringify({ message: 'Error adding document.' }));
2011-11-22 02:48:09 +00:00
}
});
2011-11-18 22:12:28 +00:00
});
2011-11-18 20:51:38 +00:00
});
request.on('error', function(error) {
2011-11-18 21:28:09 +00:00
winston.error('connection error: ' + error.message);
response.writeHead(500, { 'content-type': 'application/json' });
2011-12-19 17:44:12 +00:00
response.end(JSON.stringify({ message: 'Connection error.' }));
2011-11-18 20:51:38 +00:00
});
};
// Keep choosing keys until one isn't taken
2011-11-22 02:48:09 +00:00
DocumentHandler.prototype.chooseKey = function(callback) {
var key = this.acceptableKey();
2011-11-22 02:48:09 +00:00
var _this = this;
this.store.get(key, function(ret) {
if (ret) {
_this.chooseKey(callback);
} else {
callback(key);
}
});
2011-11-22 02:48:09 +00:00
};
DocumentHandler.prototype.acceptableKey = function() {
return this.keyGenerator.createKey(this.keyLength);
};
2011-11-18 20:51:38 +00:00
module.exports = DocumentHandler;