mirror of
https://github.com/seejohnrun/haste-server.git
synced 2024-11-01 11:31:22 +00:00
Ensure conflict resolution in keyspace
This commit is contained in:
parent
4ed87c4ec7
commit
0f2075fcda
2 changed files with 28 additions and 14 deletions
4
TODO
4
TODO
|
@ -1,4 +1,4 @@
|
||||||
cache headers for static assets
|
cache headers for static assets (and on document GET)
|
||||||
tests
|
tests
|
||||||
fix any annoying visual quirks
|
fix any annoying visual quirks
|
||||||
add FAVICON
|
add FAVICON
|
||||||
|
@ -6,7 +6,7 @@ cache static in memory
|
||||||
add feedback for errors to UI - esp. too long
|
add feedback for errors to UI - esp. too long
|
||||||
copy URL to clipboard button
|
copy URL to clipboard button
|
||||||
add about page
|
add about page
|
||||||
add conflict resolution for keys when creating a post
|
support built-in expiration
|
||||||
|
|
||||||
# shared version only
|
# shared version only
|
||||||
some way to do announcements easily (and use for ads)
|
some way to do announcements easily (and use for ads)
|
||||||
|
|
|
@ -29,7 +29,6 @@ DocumentHandler.prototype.handleGet = function(key, response) {
|
||||||
// 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 _this = this;
|
||||||
var key = this.randomKey();
|
|
||||||
var buffer = '';
|
var buffer = '';
|
||||||
request.on('data', function(data) {
|
request.on('data', function(data) {
|
||||||
if (!buffer) {
|
if (!buffer) {
|
||||||
|
@ -38,23 +37,25 @@ DocumentHandler.prototype.handlePost = function(request, response) {
|
||||||
buffer += data.toString();
|
buffer += data.toString();
|
||||||
if (_this.maxLength && buffer.length > _this.maxLength) {
|
if (_this.maxLength && buffer.length > _this.maxLength) {
|
||||||
_this.cancelled = true;
|
_this.cancelled = true;
|
||||||
winston.warn('attempted to upload a 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(JSON.stringify({ message: 'document exceeds maximum length' }));
|
response.end(JSON.stringify({ message: 'document exceeds maximum length' }));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
request.on('end', function(end) {
|
request.on('end', function(end) {
|
||||||
if (_this.cancelled) return;
|
if (_this.cancelled) return;
|
||||||
_this.store.set(key, buffer, function(res) {
|
_this.chooseKey(function(key) {
|
||||||
if (res) {
|
_this.store.set(key, buffer, function(res) {
|
||||||
winston.verbose('added document', { key: key });
|
if (res) {
|
||||||
response.end(JSON.stringify({ key: key }));
|
winston.verbose('added document', { key: key });
|
||||||
}
|
response.end(JSON.stringify({ key: key }));
|
||||||
else {
|
}
|
||||||
winston.verbose('error adding document');
|
else {
|
||||||
response.writeHead(500, { 'content-type': 'application/json' });
|
winston.verbose('error adding document');
|
||||||
response.end(JSON.stringify({ message: 'error adding document' }));
|
response.writeHead(500, { 'content-type': 'application/json' });
|
||||||
}
|
response.end(JSON.stringify({ message: 'error adding document' }));
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
request.on('error', function(error) {
|
request.on('error', function(error) {
|
||||||
|
@ -64,6 +65,19 @@ DocumentHandler.prototype.handlePost = function(request, response) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// Generate a random key
|
// Generate a random key
|
||||||
DocumentHandler.prototype.randomKey = function() {
|
DocumentHandler.prototype.randomKey = function() {
|
||||||
var text = '';
|
var text = '';
|
||||||
|
|
Loading…
Reference in a new issue