haste-server/lib/document_handler.js

97 lines
3.1 KiB
JavaScript
Raw Normal View History

2011-11-19 04:51:38 +08:00
var winston = require('winston');
// For handling serving stored documents
var DocumentHandler = function(options) {
2011-11-19 05:57:23 +08:00
if (options) {
2011-11-19 06:54:57 +08:00
this.keyLength = options.keyLength || 10;
2011-11-21 23:17:23 +08:00
this.maxLength = options.maxLength; // none by default
2011-11-19 06:54:57 +08:00
this.store = options.store;
2011-11-19 05:57:23 +08:00
}
2011-11-19 04:51:38 +08:00
};
2011-11-19 05:45:48 +08:00
// Handle retrieving a document
2011-11-28 14:13:14 +08:00
DocumentHandler.prototype.handleGet = function(key, response, skipExpire) {
2011-11-19 06:54:57 +08:00
this.store.get(key, function(ret) {
2011-11-19 06:12:28 +08: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' });
response.end(JSON.stringify({ message: 'document not found' }));
}
2011-11-28 14:13:14 +08:00
}, skipExpire);
2011-11-19 04:51:38 +08:00
};
2011-11-19 05:45:48 +08:00
// Handle adding a new Document
2011-11-19 04:51:38 +08:00
DocumentHandler.prototype.handlePost = function(request, response) {
2011-11-19 06:54:57 +08:00
var _this = this;
2011-11-19 06:12:28 +08:00
var buffer = '';
2011-11-19 04:51:38 +08:00
request.on('data', function(data) {
2011-11-19 06:12:28 +08:00
if (!buffer) {
2011-11-19 05:28:09 +08:00
response.writeHead(200, { 'content-type': 'application/json' });
2011-11-19 04:51:38 +08:00
}
2011-11-19 06:12:28 +08:00
buffer += data.toString();
2011-11-21 23:17:23 +08:00
if (_this.maxLength && buffer.length > _this.maxLength) {
_this.cancelled = true;
2011-11-22 10:48:09 +08:00
winston.warn('document >maxLength', { maxLength: _this.maxLength });
2011-11-21 23:17:23 +08:00
response.writeHead(400, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'document exceeds maximum length' }));
}
2011-11-19 04:51:38 +08:00
});
request.on('end', function(end) {
2011-11-21 23:17:23 +08:00
if (_this.cancelled) return;
2011-11-22 10:48:09 +08: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' });
response.end(JSON.stringify({ message: 'error adding document' }));
}
});
2011-11-19 06:12:28 +08:00
});
2011-11-19 04:51:38 +08:00
});
request.on('error', function(error) {
2011-11-19 05:28:09 +08:00
winston.error('connection error: ' + error.message);
response.writeHead(500, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'connection error' }));
2011-11-19 04:51:38 +08:00
});
};
2011-11-22 10:48:09 +08:00
// Get a random key that hasn't been already used
DocumentHandler.prototype.chooseKey = function(callback) {
var key = this.randomKey();
var _this = this;
this.store.get(key, function(ret) {
if (ret) {
_this.chooseKey(callback);
} else {
callback(key);
}
});
};
2011-11-22 12:13:46 +08:00
// Return a boolean indicating whether or not something can be a key
DocumentHandler.potentialKey = function(key) {
2011-11-24 00:34:08 +08:00
return key.match(/^[a-zA-Z0-9]+(\.[a-zA-Z]+)?$/);
2011-11-22 12:13:46 +08:00
};
2011-11-19 05:45:48 +08:00
// Generate a random key
DocumentHandler.prototype.randomKey = function() {
var text = '';
var keyspace = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < this.keyLength; i++) {
2011-11-19 05:45:48 +08:00
text += keyspace.charAt(Math.floor(Math.random() * keyspace.length));
}
return text;
};
2011-11-19 05:22:00 +08:00
2011-11-19 04:51:38 +08:00
module.exports = DocumentHandler;