From 1cdb039ea23bd61345fc346ac39ab309f957d1aa Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 19 Apr 2018 11:23:08 +0100 Subject: [PATCH] Move log and i18n to their own utils module --- red/api/auth/index.js | 10 +++--- red/api/auth/strategies.js | 5 +-- red/api/editor/index.js | 11 +++--- red/api/index.js | 3 +- red/api/util.js | 9 ++--- red/red.js | 13 +++---- red/runtime-api/index.js | 3 +- red/runtime/index.js | 22 ++++++------ red/runtime/nodes/Node.js | 2 +- red/runtime/nodes/credentials.js | 3 +- red/runtime/nodes/flows/Flow.js | 2 +- red/runtime/nodes/flows/index.js | 2 +- red/runtime/nodes/index.js | 3 +- red/runtime/nodes/registry/installer.js | 2 +- red/runtime/nodes/registry/localfilesystem.js | 6 ++-- red/runtime/settings.js | 2 +- red/runtime/storage/index.js | 2 +- red/runtime/storage/localfilesystem/index.js | 3 +- .../storage/localfilesystem/sessions.js | 3 +- .../storage/localfilesystem/settings.js | 2 +- red/runtime/storage/localfilesystem/util.js | 2 +- red/{runtime => util}/i18n.js | 35 +++++++++++-------- red/util/index.js | 28 +++++++++++++++ red/{runtime => util}/log.js | 0 24 files changed, 101 insertions(+), 72 deletions(-) rename red/{runtime => util}/i18n.js (83%) create mode 100644 red/util/index.js rename red/{runtime => util}/log.js (100%) diff --git a/red/api/auth/index.js b/red/api/auth/index.js index 211a712a7..355226a3f 100644 --- a/red/api/auth/index.js +++ b/red/api/auth/index.js @@ -25,7 +25,7 @@ var permissions = require("./permissions"); var theme = require("../editor/theme"); var settings = null; -var log = null +var log = require("../../util").log; // TODO: separate module passport.use(strategies.bearerStrategy.BearerStrategy); @@ -36,13 +36,11 @@ var server = oauth2orize.createServer(); server.exchange(oauth2orize.exchange.password(strategies.passwordTokenExchange)); -function init(runtime) { - settings = runtime.settings; - log = runtime.log; +function init(_settings,storage) { + settings = _settings; if (settings.adminAuth) { Users.init(settings.adminAuth); - Tokens.init(settings.adminAuth,runtime.storage); - strategies.init(runtime); + Tokens.init(settings.adminAuth,storage); } } diff --git a/red/api/auth/strategies.js b/red/api/auth/strategies.js index 0f5b554e4..e203437d1 100644 --- a/red/api/auth/strategies.js +++ b/red/api/auth/strategies.js @@ -26,7 +26,7 @@ var Users = require("./users"); var Clients = require("./clients"); var permissions = require("./permissions"); -var log; +var log = require("../../util").log; // TODO: separate module var bearerStrategy = function (accessToken, done) { // is this a valid token? @@ -124,9 +124,6 @@ AnonymousStrategy.prototype.authenticate = function(req) { } module.exports = { - init: function(runtime) { - log = runtime.log; - }, bearerStrategy: bearerStrategy, clientPasswordStrategy: clientPasswordStrategy, passwordTokenExchange: passwordTokenExchange, diff --git a/red/api/editor/index.js b/red/api/editor/index.js index ee992bdf2..d204d9411 100644 --- a/red/api/editor/index.js +++ b/red/api/editor/index.js @@ -26,7 +26,9 @@ var nodes = require("../admin/nodes"); // TODO: move /icons into here var needsPermission; var runtime; var runtimeAPI; -var log; +var log = require("../../util").log; // TODO: separate module +var i18n = require("../../util").i18n; // TODO: separate module + var apiUtil = require("../util"); var ensureRuntimeStarted = function(req,res,next) { @@ -42,15 +44,16 @@ module.exports = { init: function(server, settings, _runtime, _runtimeAPI) { runtime = _runtime; runtimeAPI = _runtimeAPI; - log = runtime.log; needsPermission = auth.needsPermission; if (!settings.disableEditor) { info.init(runtimeAPI); comms.init(server,runtime); var ui = require("./ui"); - // ui is passed runtime so it get access runtime.nodes.getNodeIconPath + + // TODO: ui is passed runtime so it get access runtime.nodes.getNodeIconPath ui.init(runtime); + var editorApp = express(); if (settings.requireHttps === true) { editorApp.enable('trust proxy'); @@ -112,7 +115,7 @@ module.exports = { }, start: function() { var catalogPath = path.resolve(path.join(__dirname,"locales")); - return runtime.i18n.registerMessageCatalogs([ + return i18n.registerMessageCatalogs([ {namespace: "editor", dir: catalogPath, file:"editor.json"}, {namespace: "jsonata", dir: catalogPath, file:"jsonata.json"}, {namespace: "infotips", dir: catalogPath, file:"infotips.json"} diff --git a/red/api/index.js b/red/api/index.js index 7ee242ac9..a46ba6968 100644 --- a/red/api/index.js +++ b/red/api/index.js @@ -31,9 +31,8 @@ var editor; function init(_server,settings,runtime,runtimeAPI) { server = _server; if (settings.httpAdminRoot !== false) { - apiUtil.init(runtime); adminApp = express(); - auth.init(runtime); + auth.init(settings,runtime.storage); var maxApiRequestSize = settings.apiMaxLength || '5mb'; adminApp.use(bodyParser.json({limit:maxApiRequestSize})); diff --git a/red/api/util.js b/red/api/util.js index 4a160a23d..6a6d97e1d 100644 --- a/red/api/util.js +++ b/red/api/util.js @@ -15,14 +15,11 @@ **/ -var i18n; -var log; +var log = require("../util").log; // TODO: separate module +var i18n = require("../util").i18n; // TODO: separate module + module.exports = { - init: function(_runtime) { - log = _runtime.log; - i18n = _runtime.i18n; - }, errorHandler: function(err,req,res,next) { console.error(err.stack); if (err.message === "request entity too large") { diff --git a/red/red.js b/red/red.js index 15259330d..405364a94 100644 --- a/red/red.js +++ b/red/red.js @@ -19,6 +19,7 @@ var path = require('path'); var runtime = require("./runtime"); var runtimeAPI = require("./runtime-api"); +var redUtil = require("./util"); var api = require("./api"); @@ -66,18 +67,18 @@ module.exports = { if (!userSettings.coreNodesDir) { userSettings.coreNodesDir = path.resolve(path.join(__dirname,"..","nodes")); } - + redUtil.init(userSettings); if (userSettings.httpAdminRoot !== false) { - runtime.init(userSettings,api); + runtime.init(userSettings,redUtil,api); - runtimeAPI.init(runtime); - api.init(httpServer,userSettings,runtime,runtimeAPI); + runtimeAPI.init(runtime,redUtil); + api.init(httpServer,userSettings,runtime,runtimeAPI,redUtil); apiEnabled = true; server = runtime.adminApi.server; runtime.server = runtime.adminApi.server; } else { - runtime.init(userSettings); + runtime.init(userSettings,redUtil); apiEnabled = false; if (httpServer){ server = httpServer; @@ -106,7 +107,7 @@ module.exports = { }) }, nodes: runtime.nodes, - log: runtime.log, + get log() { return redUtil.log }, settings:runtime.settings, util: runtime.util, version: runtime.version, diff --git a/red/runtime-api/index.js b/red/runtime-api/index.js index 75ee05602..ab1342953 100644 --- a/red/runtime-api/index.js +++ b/red/runtime-api/index.js @@ -21,12 +21,11 @@ * @type {object} */ - /** * @namespace RED */ var api = module.exports = { - init: function(runtime) { + init: function(runtime, redUtil) { api.flows.init(runtime); api.nodes.init(runtime); api.settings.init(runtime); diff --git a/red/runtime/index.js b/red/runtime/index.js index e8fc0a44b..05f7fcdba 100644 --- a/red/runtime/index.js +++ b/red/runtime/index.js @@ -19,8 +19,6 @@ var when = require('when'); var redNodes = require("./nodes"); var storage = require("./storage"); var library = require("./library"); -var log = require("./log"); -var i18n = require("./i18n"); var events = require("./events"); var settings = require("./settings"); @@ -29,6 +27,10 @@ var path = require('path'); var fs = require("fs"); var os = require("os"); +var redUtil; +var log; +var i18n; + var runtimeMetricInterval = null; var started = false; @@ -55,9 +57,12 @@ var adminApi = { var nodeApp; -function init(userSettings,_adminApi) { +function init(userSettings,_redUtil,_adminApi) { + redUtil = _redUtil; + log = redUtil.log; + i18n = redUtil.i18n; + userSettings.version = getVersion(); - log.init(userSettings); settings.init(userSettings); nodeApp = express(); @@ -86,10 +91,7 @@ function getVersion() { } function start() { - return i18n.init() - .then(function() { - return i18n.registerMessageCatalog("runtime",path.resolve(path.join(__dirname,"locales")),"runtime.json") - }) + return i18n.registerMessageCatalog("runtime",path.resolve(path.join(__dirname,"locales")),"runtime.json") .then(function() { return storage.init(runtime)}) .then(function() { return settings.load(storage)}) .then(function() { @@ -241,8 +243,8 @@ var runtime = module.exports = { version: getVersion, - log: log, - i18n: i18n, + get log() { return log }, + get i18n() { return i18n }, settings: settings, storage: storage, events: events, diff --git a/red/runtime/nodes/Node.js b/red/runtime/nodes/Node.js index b8cfa6532..989998665 100644 --- a/red/runtime/nodes/Node.js +++ b/red/runtime/nodes/Node.js @@ -19,7 +19,7 @@ var EventEmitter = require("events").EventEmitter; var when = require("when"); var redUtil = require("../util"); -var Log = require("../log"); +var Log = require("../../util").log; // TODO: separate module var context = require("./context"); var flows = require("./flows"); diff --git a/red/runtime/nodes/credentials.js b/red/runtime/nodes/credentials.js index 36b97b0b5..5e8b339f8 100644 --- a/red/runtime/nodes/credentials.js +++ b/red/runtime/nodes/credentials.js @@ -18,7 +18,8 @@ var when = require("when"); var crypto = require('crypto'); var runtime; var settings; -var log; +var log = require("../../util").log; // TODO: separate module + var encryptedCredentials = null; var credentialCache = {}; diff --git a/red/runtime/nodes/flows/Flow.js b/red/runtime/nodes/flows/Flow.js index 5d054a8a9..175bb6fe1 100644 --- a/red/runtime/nodes/flows/Flow.js +++ b/red/runtime/nodes/flows/Flow.js @@ -17,7 +17,7 @@ var when = require("when"); var clone = require("clone"); var typeRegistry = require("../registry"); -var Log = require("../../log"); +var Log = require("../../../util").log; // TODO: separate module var redUtil = require("../../util"); var flowUtil = require("./util"); diff --git a/red/runtime/nodes/flows/index.js b/red/runtime/nodes/flows/index.js index d1ba541f5..eef1c9774 100644 --- a/red/runtime/nodes/flows/index.js +++ b/red/runtime/nodes/flows/index.js @@ -24,7 +24,7 @@ var context = require("../context") var credentials = require("../credentials"); var flowUtil = require("./util"); -var log = require("../../log"); +var log = require("../../../util").log; // TODO: separate module var events = require("../../events"); var redUtil = require("../../util"); var deprecated = require("../registry/deprecated"); diff --git a/red/runtime/nodes/index.js b/red/runtime/nodes/index.js index b2f8ff3b5..ae1e3f253 100644 --- a/red/runtime/nodes/index.js +++ b/red/runtime/nodes/index.js @@ -25,7 +25,7 @@ var flows = require("./flows"); var flowUtil = require("./flows/util") var context = require("./context"); var Node = require("./Node"); -var log = null; +var log = require("../../util").log; // TODO: separate module var library = require("./library"); var events = require("../events"); @@ -94,7 +94,6 @@ function createNode(node,def) { function init(runtime) { settings = runtime.settings; - log = runtime.log; credentials.init(runtime); flows.init(runtime); registry.init(runtime); diff --git a/red/runtime/nodes/registry/installer.js b/red/runtime/nodes/registry/installer.js index 4e185c257..bceae2e6a 100644 --- a/red/runtime/nodes/registry/installer.js +++ b/red/runtime/nodes/registry/installer.js @@ -20,7 +20,7 @@ var path = require("path"); var fs = require("fs"); var registry = require("./registry"); -var log = require("../../log"); +var log = require("../../../util").log; // TODO: separate module var events = require("../../events"); diff --git a/red/runtime/nodes/registry/localfilesystem.js b/red/runtime/nodes/registry/localfilesystem.js index c2f0f9359..d9f170b78 100644 --- a/red/runtime/nodes/registry/localfilesystem.js +++ b/red/runtime/nodes/registry/localfilesystem.js @@ -19,8 +19,8 @@ var fs = require("fs"); var path = require("path"); var events; -var log; -var i18n; +var log = require("../../../util").log; // TODO: separate module +var i18n = require("../../../util").i18n; // TODO: separate module var settings; var disableNodePathScan = false; @@ -29,8 +29,6 @@ var iconFileExtensions = [".png", ".gif"]; function init(runtime) { settings = runtime.settings; events = runtime.events; - log = runtime.log; - i18n = runtime.i18n; } function isIncluded(name) { diff --git a/red/runtime/settings.js b/red/runtime/settings.js index 1addf7c47..780faf590 100644 --- a/red/runtime/settings.js +++ b/red/runtime/settings.js @@ -17,7 +17,7 @@ var when = require("when"); var clone = require("clone"); var assert = require("assert"); -var log = require("./log"); +var log = require("../util").log; // TODO: separate module var util = require("./util"); // localSettings are those provided in the runtime settings.js file diff --git a/red/runtime/storage/index.js b/red/runtime/storage/index.js index e0ef6d6b6..3f1833bc3 100644 --- a/red/runtime/storage/index.js +++ b/red/runtime/storage/index.js @@ -18,7 +18,7 @@ var when = require('when'); var Path = require('path'); var crypto = require('crypto'); -var log = require("../log"); +var log = require("../../util").log; // TODO: separate module var runtime; var storageModule; diff --git a/red/runtime/storage/localfilesystem/index.js b/red/runtime/storage/localfilesystem/index.js index 2a57c7323..af9d103fb 100644 --- a/red/runtime/storage/localfilesystem/index.js +++ b/red/runtime/storage/localfilesystem/index.js @@ -18,7 +18,8 @@ var fs = require('fs-extra'); var when = require('when'); var fspath = require("path"); -var log = require("../../log"); +var log = require("../../../util").log; // TODO: separate module + var util = require("./util"); var library = require("./library"); var sessions = require("./sessions"); diff --git a/red/runtime/storage/localfilesystem/sessions.js b/red/runtime/storage/localfilesystem/sessions.js index 29c6871a2..b4d024723 100644 --- a/red/runtime/storage/localfilesystem/sessions.js +++ b/red/runtime/storage/localfilesystem/sessions.js @@ -18,7 +18,8 @@ var when = require('when'); var fs = require('fs-extra'); var fspath = require("path"); -var log = require("../../log"); +var log = require("../../../util").log; // TODO: separate module + var util = require("./util"); var sessionsFile; diff --git a/red/runtime/storage/localfilesystem/settings.js b/red/runtime/storage/localfilesystem/settings.js index 17ece58d7..8837fcc2a 100644 --- a/red/runtime/storage/localfilesystem/settings.js +++ b/red/runtime/storage/localfilesystem/settings.js @@ -18,7 +18,7 @@ var when = require('when'); var fs = require('fs-extra'); var fspath = require("path"); -var log = require("../../log"); +var log = require("../../../util").log; // TODO: separate module var util = require("./util"); var globalSettingsFile; diff --git a/red/runtime/storage/localfilesystem/util.js b/red/runtime/storage/localfilesystem/util.js index f677bdd38..8164147ab 100644 --- a/red/runtime/storage/localfilesystem/util.js +++ b/red/runtime/storage/localfilesystem/util.js @@ -18,7 +18,7 @@ var fs = require('fs-extra'); var when = require('when'); var nodeFn = require('when/node/function'); -var log = require("../../log"); +var log = require("../../../util").log; // TODO: separate module function parseJSON(data) { if (data.charCodeAt(0) === 0xFEFF) { diff --git a/red/runtime/i18n.js b/red/util/i18n.js similarity index 83% rename from red/runtime/i18n.js rename to red/util/i18n.js index fc3599c87..b809f8081 100644 --- a/red/runtime/i18n.js +++ b/red/util/i18n.js @@ -23,6 +23,7 @@ var defaultLang = "en-US"; var resourceMap = {}; var resourceCache = {}; +var initPromise; function registerMessageCatalogs(catalogs) { var promises = catalogs.map(function(catalog) { @@ -32,10 +33,12 @@ function registerMessageCatalogs(catalogs) { } function registerMessageCatalog(namespace,dir,file) { - return when.promise(function(resolve,reject) { - resourceMap[namespace] = { basedir:dir, file:file}; - i18n.loadNamespace(namespace,function() { - resolve(); + return initPromise.then(function() { + when.promise(function(resolve,reject) { + resourceMap[namespace] = { basedir:dir, file:file}; + i18n.loadNamespace(namespace,function() { + resolve(); + }); }); }); } @@ -82,18 +85,20 @@ var MessageFileLoader = { } function init() { - return when.promise(function(resolve,reject) { - i18n.backend(MessageFileLoader); - i18n.init({ - ns: { - namespaces: [], - defaultNs: "runtime" - }, - fallbackLng: [defaultLang] - },function() { - resolve(); + if (!initPromise) { + initPromise = when.promise(function(resolve,reject) { + i18n.backend(MessageFileLoader); + i18n.init({ + ns: { + namespaces: [], + defaultNs: "runtime" + }, + fallbackLng: [defaultLang] + },function() { + resolve(); + }); }); - }); + } } function getCatalog(namespace,lang) { diff --git a/red/util/index.js b/red/util/index.js new file mode 100644 index 000000000..342d2dfff --- /dev/null +++ b/red/util/index.js @@ -0,0 +1,28 @@ +/** + * Copyright JS Foundation and other contributors, http://js.foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + + +var log = require("./log"); +var i18n = require("./i18n"); + +module.exports = { + init: function(settings) { + log.init(settings); + i18n.init(); + }, + log: log, + i18n: i18n +} diff --git a/red/runtime/log.js b/red/util/log.js similarity index 100% rename from red/runtime/log.js rename to red/util/log.js