Decreased master load time by 30%, web postJSON now returns a promise

This commit is contained in:
Daniel Vestøl committed 2018-06-09 00:00:18 +02:00
1 parent ea8f819bd5
commit 1b6b0abaeb
4 files changed
+34 -20

No files matched your search

+2
View File
@@ -50,3 +50,5 @@ sharedPlugins/*
!sharedPlugins/remoteCommands/
!sharedPlugins/researchSync/
!sharedPlugins/globalChat/
\.cache-require-paths\.json
+6 -2
View File
@@ -20,6 +20,10 @@ process.on('unhandledRejection', r => console.log(r));
// configgy stuff
debug = false;
// reduces loadtime from 1700 ms to 1200 ms on my i3 + SSD
// tested by logging Date.now() here and when the webserver started listening
require('cache-require-paths');
// constants
const masterModFolder = "./database/masterMods/";
const config = require('./config');
@@ -46,8 +50,8 @@ const https = require("https");
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
var crypto = require('crypto');
var base64url = require('base64url');
const crypto = require('crypto');
const base64url = require('base64url');
/** Sync */
function randomStringAsBase64Url(size) {
+3
View File
@@ -22,8 +22,11 @@
"as-table": "^1.0.31",
"averaged-timeseries": "^1.4.0",
"base64url": "^3.0.0",
"bcrypt": "^2.0.1",
"bcrypt-promise": "^2.0.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.2",
"cache-require-paths": "^0.3.0",
"codecov": "^3.0.1",
"compression": "^1.7.2",
"deepmerge": "^1.5.2",
+23 -18
View File
@@ -94,7 +94,7 @@ function getJSON(url, callback) {
resolve(xhr.response);
} else {
if(callback) callback(status);
resolve(status);
reject(status);
}
};
// triggers if connection is refused
@@ -107,23 +107,28 @@ function getJSON(url, callback) {
};
// callback(err, json)
function postJSON(url, data, callback) {
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
callback(null, xhr.response);
} else {
callback(status);
}
};
// triggers if connection is refused
xhr.onerror = function(e){
callback(e);
};
xhr.send(JSON.stringify(data));
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
if(callback) callback(null, xhr.response);
resolve(xhr.response);
} else {
if(callback) callback(status);
reject(status);
}
};
// triggers if connection is refused
xhr.onerror = function(e){
if(callback) callback(e);
reject(e);
};
xhr.send(JSON.stringify(data));
});
};
// return Boolean
function isJSON(string){