From f3288c4e39b9eccd08965aa9a4cc42b0490693b2 Mon Sep 17 00:00:00 2001 From: zneix Date: Thu, 27 Aug 2020 00:39:06 +0200 Subject: [PATCH] Made server run async --- lib/document_handler.js | 28 ++++++++++++++-------------- lib/document_stores/file.js | 2 +- server.js | 21 +++++++++++++-------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/document_handler.js b/lib/document_handler.js index c1860ed..5f7ca78 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -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 { diff --git a/lib/document_stores/file.js b/lib/document_stores/file.js index db8d715..8587653 100644 --- a/lib/document_stores/file.js +++ b/lib/document_stores/file.js @@ -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); diff --git a/server.js b/server.js index 020132d..0372f57 100644 --- a/server.js +++ b/server.js @@ -59,13 +59,16 @@ if (config.recompressStaticAssets){ winston.info(`compressed ${file} into ${dest}`); } } + +(async function(){ + //send the static documents into the preferred store, skipping expirations for (const name in config.documents){ let path = config.documents[name]; winston.info('loading static document', { name: name, path: path }); let data = fs.readFileSync(path, 'utf8'); if (data){ - preferredStore.set(name, data, doc => winston.debug('loaded static document', { success: doc }), true); + await preferredStore.set(name, data, doc => winston.debug('loaded static document', { success: doc }), true); } else { winston.warn('failed to load static document', { name: name, path: path }); @@ -92,22 +95,22 @@ if (config.rateLimits) app.use(expressRateLimit(config.rateLimits)); //try API first //get raw documents -app.get('/raw/:id', (req, res) => { +app.get('/raw/:id', async (req, res) => { const key = req.params.id.split('.')[0]; const skipExpire = Boolean(config.documents[key]); - return documentHandler.handleGetRaw(key, res, skipExpire); + return await documentHandler.handleGetRaw(key, res, skipExpire); }); //add documents -app.post('/documents', (req, res) => { - return documentHandler.handlePost(req, res); +app.post('/documents', async (req, res) => { + return await documentHandler.handlePost(req, res); }); //get documents -app.get('/documents/:id', (req, res) => { +app.get('/documents/:id', async (req, res) => { const key = req.params.id.split('.')[0]; const skipExpire = Boolean(config.documents[key]); - return documentHandler.handleGet(key, res, skipExpire); + return await documentHandler.handleGet(key, res, skipExpire); }); //try static next @@ -131,4 +134,6 @@ app.use(st({ index: 'index.html' })); -app.listen(config.port, config.host, () => winston.info(`listening on ${config.host}:${config.port}`)); \ No newline at end of file +app.listen(config.port, config.host, () => winston.info(`listening on ${config.host}:${config.port}`)); + +})(); \ No newline at end of file