Skip to content

Commit

Permalink
Fully remove when.js dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
knolleary committed Nov 30, 2020
1 parent bbe3ee7 commit 0266c24
Show file tree
Hide file tree
Showing 49 changed files with 298 additions and 356 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -74,7 +74,6 @@
"semver": "6.3.0",
"tar": "6.0.5",
"uglify-js": "3.11.6",
"when": "3.7.8",
"ws": "6.2.1",
"xml2js": "0.4.23"
},
Expand Down
Expand Up @@ -17,7 +17,6 @@
var apiUtils = require("../util");
var fs = require('fs');
var fspath = require('path');
var when = require('when');

var runtimeAPI;

Expand Down
8 changes: 2 additions & 6 deletions packages/node_modules/@node-red/editor-api/lib/index.js
Expand Up @@ -28,7 +28,6 @@ var express = require("express");
var bodyParser = require("body-parser");
var util = require('util');
var passport = require('passport');
var when = require('when');
var cors = require('cors');

var auth = require("./auth");
Expand Down Expand Up @@ -111,11 +110,9 @@ function init(settings,_server,storage,runtimeAPI) {
* @return {Promise} resolves when the application is ready to handle requests
* @memberof @node-red/editor-api
*/
function start() {
async function start() {
if (editor) {
return editor.start();
} else {
return when.resolve();
}
}

Expand All @@ -124,11 +121,10 @@ function start() {
* @return {Promise} resolves when the application is stopped
* @memberof @node-red/editor-api
*/
function stop() {
async function stop() {
if (editor) {
editor.stop();
}
return when.resolve();
}
module.exports = {
init: init,
Expand Down
1 change: 0 additions & 1 deletion packages/node_modules/@node-red/editor-api/package.json
Expand Up @@ -32,7 +32,6 @@
"passport-http-bearer": "1.0.1",
"passport-oauth2-client-password": "0.1.2",
"passport": "0.4.1",
"when": "3.7.8",
"ws": "6.2.1"
},
"optionalDependencies": {
Expand Down
9 changes: 4 additions & 5 deletions packages/node_modules/@node-red/registry/lib/loader.js
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
**/

var when = require("when");
var fs = require("fs-extra");
var path = require("path");
var semver = require("semver");
Expand Down Expand Up @@ -92,7 +91,7 @@ function loadNodeFiles(nodeFiles) {
nodeFiles[m].nodes[n] = nodeSet;
nodes.push(nodeSet);
}
})()));
})()).catch(err => {}));
} catch(err) {
//
}
Expand All @@ -101,7 +100,7 @@ function loadNodeFiles(nodeFiles) {
}
}
}
return when.settle(promises).then(function(results) {
return Promise.all(promises).then(function(results) {
for (var module in nodeFiles) {
if (nodeFiles.hasOwnProperty(module)) {
if (!nodeFiles[module].err) {
Expand Down Expand Up @@ -293,13 +292,13 @@ function loadNodeSetList(nodes) {
var promises = [];
nodes.forEach(function(node) {
if (!node.err) {
promises.push(loadNodeSet(node));
promises.push(loadNodeSet(node).catch(err => {}));
} else {
promises.push(node);
}
});

return when.settle(promises).then(function() {
return Promise.all(promises).then(function() {
if (settings.available()) {
return registry.saveNodeList();
} else {
Expand Down
Expand Up @@ -17,7 +17,6 @@
var fs = require("fs");
var path = require("path");

var events;
var log;

var log = require("@node-red/util").log;
Expand All @@ -29,7 +28,6 @@ var iconFileExtensions = [".png", ".gif", ".svg"];

function init(runtime) {
settings = runtime.settings;
events = runtime.events;
}

function isIncluded(name) {
Expand Down Expand Up @@ -75,7 +73,6 @@ function getLocalFile(file) {

/**
* Synchronously walks the directory looking for node files.
* Emits 'node-icon-dir' events for an icon dirs found
* @param dir the directory to search
* @return an array of fully-qualified paths to .js files
*/
Expand Down Expand Up @@ -229,7 +226,6 @@ function getModuleNodeFiles(module) {
try {
fs.statSync(examplesDir)
result.examples = {path:examplesDir};
// events.emit("node-examples-dir",{name:pkg.name,path:examplesDir});
} catch(err) {
}
return result;
Expand Down
3 changes: 1 addition & 2 deletions packages/node_modules/@node-red/registry/package.json
Expand Up @@ -19,7 +19,6 @@
"@node-red/util": "1.3.0-beta.1",
"semver": "6.3.0",
"tar": "6.0.5",
"uglify-js": "3.11.6",
"when": "3.7.8"
"uglify-js": "3.11.6"
}
}
27 changes: 12 additions & 15 deletions packages/node_modules/@node-red/runtime/lib/index.js
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
**/

var when = require('when');

var externalAPI = require("./api");

var redNodes = require("./nodes");
Expand Down Expand Up @@ -197,25 +195,24 @@ function start() {
});
}

var reinstallAttempts;
var reinstallAttempts = 0;
var reinstallTimeout;
function reinstallModules(moduleList) {
var promises = [];
var failedModules = [];
var reinstallList = [];

for (var i=0;i<moduleList.length;i++) {
if (settings.autoInstallModules && i != "node-red") {
promises.push(redNodes.installModule(moduleList[i].id,moduleList[i].version));
if (settings.autoInstallModules && moduleList[i].id != "node-red") {
(function(mod) {
promises.push(redNodes.installModule(mod.id,mod.version).then(m => {
events.emit("runtime-event",{id:"node/added",retain:false,payload:m.nodes});
}).catch(err => {
reinstallList.push(mod);
}));
})(moduleList[i])
}
}
when.settle(promises).then(function(results) {
var reinstallList = [];
for (var i=0;i<results.length;i++) {
if (results[i].state === 'rejected') {
reinstallList.push(moduleList[i]);
} else {
events.emit("runtime-event",{id:"node/added",retain:false,payload:results[i].value.nodes});
}
}
Promise.all(promises).then(function(results) {
if (reinstallList.length > 0) {
reinstallAttempts++;
// First 5 at 1x timeout, next 5 at 2x, next 5 at 4x, then 8x
Expand Down
27 changes: 12 additions & 15 deletions packages/node_modules/@node-red/runtime/lib/nodes/credentials.js
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
**/

var when = require("when");
var crypto = require('crypto');
var runtime;
var settings;
Expand Down Expand Up @@ -60,7 +59,7 @@ var api = module.exports = {
/**
* Sets the credentials from storage.
*/
load: function (credentials) {
load: async function (credentials) {
dirty = false;
var credentialsEncrypted = credentials.hasOwnProperty("$") && Object.keys(credentials).length === 1;

Expand All @@ -77,7 +76,7 @@ var api = module.exports = {
// Case 4: credentialSecret set
// - use it

var setupEncryptionPromise = when.resolve();
var setupEncryptionPromise = Promise.resolve();

var projectKey = false;
var activeProject;
Expand Down Expand Up @@ -134,7 +133,7 @@ var api = module.exports = {
log.warn(log._("nodes.credentials.error",{message:err.toString()}))
var error = new Error("Failed to decrypt credentials");
error.code = "credentials_load_failed";
return when.reject(error);
throw error;
}
}
dirty = true;
Expand Down Expand Up @@ -163,7 +162,7 @@ var api = module.exports = {
log.warn(log._("nodes.credentials.error",{message:err.toString()}))
var error = new Error("Failed to decrypt credentials");
error.code = "credentials_load_failed";
return when.reject(error);
throw error;
}
}
dirty = true;
Expand Down Expand Up @@ -217,9 +216,9 @@ var api = module.exports = {
// This is a project with a bad key. Mark it as invalid
// TODO: this delves too deep into Project structure
activeProject.credentialSecretInvalid = true;
return when.reject(error);
throw error;
}
return when.reject(error);
throw error;
}
// These are encrypted credentials
try {
Expand All @@ -235,9 +234,9 @@ var api = module.exports = {
// This is a project with a bad key. Mark it as invalid
// TODO: this delves too deep into Project structure
activeProject.credentialSecretInvalid = true;
return when.reject(error);
throw error;
}
return when.reject(error);
throw error;
}
} else {
credentialCache = credentials;
Expand All @@ -257,12 +256,11 @@ var api = module.exports = {
* @param creds an object of credential key/value pairs
* @return a promise for backwards compatibility TODO: can this be removed?
*/
add: function (id, creds) {
add: async function (id, creds) {
if (!credentialCache.hasOwnProperty(id) || JSON.stringify(creds) !== JSON.stringify(credentialCache[id])) {
credentialCache[id] = creds;
dirty = true;
}
return when.resolve();
},

/**
Expand Down Expand Up @@ -293,7 +291,7 @@ var api = module.exports = {
* @param config a flow config
* @return a promise for the saving of credentials to storage
*/
clean: function (config) {
clean: async function (config) {
var existingIds = {};
config.forEach(function(n) {
existingIds[n.id] = true;
Expand All @@ -313,7 +311,6 @@ var api = module.exports = {
if (deletedCredentials) {
dirty = true;
}
return when.resolve();
},

/**
Expand Down Expand Up @@ -432,7 +429,7 @@ var api = module.exports = {
getKeyType: function() {
return encryptionKeyType;
},
export: function() {
export: async function() {
var result = credentialCache;

if (encryptionEnabled) {
Expand All @@ -455,7 +452,7 @@ var api = module.exports = {
return result;
})
} else {
return when.resolve(result);
return result;
}
}
}
1 change: 0 additions & 1 deletion packages/node_modules/@node-red/runtime/lib/nodes/index.js
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
**/

var when = require("when");
var path = require("path");
var fs = require("fs");
var clone = require("clone");
Expand Down
6 changes: 2 additions & 4 deletions packages/node_modules/@node-red/runtime/lib/settings.js
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
**/

var when = require("when");
var clone = require("clone");
var assert = require("assert");
var log = require("@node-red/util").log; // TODO: separate module
Expand Down Expand Up @@ -90,10 +89,10 @@ var persistentSettings = {
globalSettings[prop] = clone(value);
try {
assert.deepEqual(current,value);
return when.resolve();
} catch(err) {
return storage.saveSettings(clone(globalSettings));
}
return Promise.resolve();
},
delete: function(prop) {
if (localSettings.hasOwnProperty(prop)) {
Expand All @@ -106,7 +105,7 @@ var persistentSettings = {
delete globalSettings[prop];
return storage.saveSettings(clone(globalSettings));
}
return when.resolve();
return Promise.resolve();
},

available: function() {
Expand Down Expand Up @@ -180,7 +179,6 @@ var persistentSettings = {
userSettings[username] = settings;
try {
assert.deepEqual(current,settings);
return when.resolve();
} catch(err) {
globalSettings.users = userSettings;
return storage.saveSettings(clone(globalSettings));
Expand Down

0 comments on commit 0266c24

Please sign in to comment.