mirror of
https://github.com/SunRed/haste-server.git
synced 2025-09-04 04:50:14 +02:00
Made server run async
This commit is contained in:
parent
78aabf90bb
commit
f3288c4e39
3 changed files with 28 additions and 23 deletions
|
@ -14,8 +14,8 @@ const DocumentHandler = function(options) {
|
|||
DocumentHandler.defaultKeyLength = 10;
|
||||
|
||||
// Handle retrieving a document
|
||||
DocumentHandler.prototype.handleGet = function(key, res, skipExpire) {
|
||||
this.store.get(key, function(ret) {
|
||||
DocumentHandler.prototype.handleGet = async function(key, res, skipExpire) {
|
||||
await this.store.get(key, function(ret) {
|
||||
if (ret) {
|
||||
winston.verbose('retrieved document', { key: key });
|
||||
res.writeHead(200, { 'content-type': 'application/json' });
|
||||
|
@ -30,8 +30,8 @@ DocumentHandler.prototype.handleGet = function(key, res, skipExpire) {
|
|||
};
|
||||
|
||||
// Handle retrieving the raw version of a document
|
||||
DocumentHandler.prototype.handleGetRaw = function(key, res, skipExpire) {
|
||||
this.store.get(key, function(ret) {
|
||||
DocumentHandler.prototype.handleGetRaw = async function(key, res, skipExpire) {
|
||||
await this.store.get(key, function(ret) {
|
||||
if (ret) {
|
||||
winston.verbose('retrieved raw document', { key: key });
|
||||
res.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' });
|
||||
|
@ -46,13 +46,13 @@ DocumentHandler.prototype.handleGetRaw = function(key, res, skipExpire) {
|
|||
};
|
||||
|
||||
// Handle adding a new Document
|
||||
DocumentHandler.prototype.handlePost = function (req, res) {
|
||||
DocumentHandler.prototype.handlePost = async function (req, res) {
|
||||
let _this = this;
|
||||
let buffer = '';
|
||||
let cancelled = false;
|
||||
|
||||
// What to do when done
|
||||
let onSuccess = function () {
|
||||
let onSuccess = async function () {
|
||||
// Check length
|
||||
if (_this.maxLength && buffer.length > _this.maxLength) {
|
||||
cancelled = true;
|
||||
|
@ -64,8 +64,8 @@ DocumentHandler.prototype.handlePost = function (req, res) {
|
|||
return;
|
||||
}
|
||||
// And then save if we should
|
||||
_this.chooseKey(function (key) {
|
||||
_this.store.set(key, buffer, function (resp) {
|
||||
await _this.chooseKey(async function (key) {
|
||||
await _this.store.set(key, buffer, function (resp) {
|
||||
if (resp) {
|
||||
winston.verbose('added document', { key: key });
|
||||
res.writeHead(200, { 'content-type': 'application/json' });
|
||||
|
@ -89,8 +89,8 @@ DocumentHandler.prototype.handlePost = function (req, res) {
|
|||
buffer = val;
|
||||
}
|
||||
});
|
||||
busboy.on('finish', function () {
|
||||
onSuccess();
|
||||
busboy.on('finish', async function () {
|
||||
await onSuccess();
|
||||
});
|
||||
req.pipe(busboy);
|
||||
// Otherwise, use our own and just grab flat data from POST body
|
||||
|
@ -98,9 +98,9 @@ DocumentHandler.prototype.handlePost = function (req, res) {
|
|||
req.on('data', function (data) {
|
||||
buffer += data.toString();
|
||||
});
|
||||
req.on('end', function () {
|
||||
req.on('end', async function () {
|
||||
if (cancelled) { return; }
|
||||
onSuccess();
|
||||
await onSuccess();
|
||||
});
|
||||
req.on('error', function (error) {
|
||||
winston.error('connection error: ' + error.message);
|
||||
|
@ -112,10 +112,10 @@ DocumentHandler.prototype.handlePost = function (req, res) {
|
|||
};
|
||||
|
||||
// Keep choosing keys until one isn't taken
|
||||
DocumentHandler.prototype.chooseKey = function(callback) {
|
||||
DocumentHandler.prototype.chooseKey = async function(callback) {
|
||||
let key = this.acceptableKey();
|
||||
let _this = this;
|
||||
this.store.get(key, function(ret) {
|
||||
await this.store.get(key, function(ret) {
|
||||
if (ret) {
|
||||
_this.chooseKey(callback);
|
||||
} else {
|
||||
|
|
|
@ -46,7 +46,7 @@ FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
|
|||
// Get data from a file from key
|
||||
FileDocumentStore.prototype.get = function(key, callback, skipExpire) {
|
||||
const _this = this;
|
||||
const fn = this.basePath + '/' + FileDocumentStore.md5(key);
|
||||
const fn = require('path').join(this.basePath, FileDocumentStore.md5(key));
|
||||
fs.readFile(fn, 'utf8', function(err, data) {
|
||||
if (err) {
|
||||
callback(false);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue