1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Move log and i18n to their own utils module

This commit is contained in:
Nick O'Leary 2018-04-19 11:23:08 +01:00
parent 7409cb3abb
commit 1cdb039ea2
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
24 changed files with 101 additions and 72 deletions

View File

@ -25,7 +25,7 @@ var permissions = require("./permissions");
var theme = require("../editor/theme"); var theme = require("../editor/theme");
var settings = null; var settings = null;
var log = null var log = require("../../util").log; // TODO: separate module
passport.use(strategies.bearerStrategy.BearerStrategy); passport.use(strategies.bearerStrategy.BearerStrategy);
@ -36,13 +36,11 @@ var server = oauth2orize.createServer();
server.exchange(oauth2orize.exchange.password(strategies.passwordTokenExchange)); server.exchange(oauth2orize.exchange.password(strategies.passwordTokenExchange));
function init(runtime) { function init(_settings,storage) {
settings = runtime.settings; settings = _settings;
log = runtime.log;
if (settings.adminAuth) { if (settings.adminAuth) {
Users.init(settings.adminAuth); Users.init(settings.adminAuth);
Tokens.init(settings.adminAuth,runtime.storage); Tokens.init(settings.adminAuth,storage);
strategies.init(runtime);
} }
} }

View File

@ -26,7 +26,7 @@ var Users = require("./users");
var Clients = require("./clients"); var Clients = require("./clients");
var permissions = require("./permissions"); var permissions = require("./permissions");
var log; var log = require("../../util").log; // TODO: separate module
var bearerStrategy = function (accessToken, done) { var bearerStrategy = function (accessToken, done) {
// is this a valid token? // is this a valid token?
@ -124,9 +124,6 @@ AnonymousStrategy.prototype.authenticate = function(req) {
} }
module.exports = { module.exports = {
init: function(runtime) {
log = runtime.log;
},
bearerStrategy: bearerStrategy, bearerStrategy: bearerStrategy,
clientPasswordStrategy: clientPasswordStrategy, clientPasswordStrategy: clientPasswordStrategy,
passwordTokenExchange: passwordTokenExchange, passwordTokenExchange: passwordTokenExchange,

View File

@ -26,7 +26,9 @@ var nodes = require("../admin/nodes"); // TODO: move /icons into here
var needsPermission; var needsPermission;
var runtime; var runtime;
var runtimeAPI; 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 apiUtil = require("../util");
var ensureRuntimeStarted = function(req,res,next) { var ensureRuntimeStarted = function(req,res,next) {
@ -42,15 +44,16 @@ module.exports = {
init: function(server, settings, _runtime, _runtimeAPI) { init: function(server, settings, _runtime, _runtimeAPI) {
runtime = _runtime; runtime = _runtime;
runtimeAPI = _runtimeAPI; runtimeAPI = _runtimeAPI;
log = runtime.log;
needsPermission = auth.needsPermission; needsPermission = auth.needsPermission;
if (!settings.disableEditor) { if (!settings.disableEditor) {
info.init(runtimeAPI); info.init(runtimeAPI);
comms.init(server,runtime); comms.init(server,runtime);
var ui = require("./ui"); 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); ui.init(runtime);
var editorApp = express(); var editorApp = express();
if (settings.requireHttps === true) { if (settings.requireHttps === true) {
editorApp.enable('trust proxy'); editorApp.enable('trust proxy');
@ -112,7 +115,7 @@ module.exports = {
}, },
start: function() { start: function() {
var catalogPath = path.resolve(path.join(__dirname,"locales")); var catalogPath = path.resolve(path.join(__dirname,"locales"));
return runtime.i18n.registerMessageCatalogs([ return i18n.registerMessageCatalogs([
{namespace: "editor", dir: catalogPath, file:"editor.json"}, {namespace: "editor", dir: catalogPath, file:"editor.json"},
{namespace: "jsonata", dir: catalogPath, file:"jsonata.json"}, {namespace: "jsonata", dir: catalogPath, file:"jsonata.json"},
{namespace: "infotips", dir: catalogPath, file:"infotips.json"} {namespace: "infotips", dir: catalogPath, file:"infotips.json"}

View File

@ -31,9 +31,8 @@ var editor;
function init(_server,settings,runtime,runtimeAPI) { function init(_server,settings,runtime,runtimeAPI) {
server = _server; server = _server;
if (settings.httpAdminRoot !== false) { if (settings.httpAdminRoot !== false) {
apiUtil.init(runtime);
adminApp = express(); adminApp = express();
auth.init(runtime); auth.init(settings,runtime.storage);
var maxApiRequestSize = settings.apiMaxLength || '5mb'; var maxApiRequestSize = settings.apiMaxLength || '5mb';
adminApp.use(bodyParser.json({limit:maxApiRequestSize})); adminApp.use(bodyParser.json({limit:maxApiRequestSize}));

View File

@ -15,14 +15,11 @@
**/ **/
var i18n; var log = require("../util").log; // TODO: separate module
var log; var i18n = require("../util").i18n; // TODO: separate module
module.exports = { module.exports = {
init: function(_runtime) {
log = _runtime.log;
i18n = _runtime.i18n;
},
errorHandler: function(err,req,res,next) { errorHandler: function(err,req,res,next) {
console.error(err.stack); console.error(err.stack);
if (err.message === "request entity too large") { if (err.message === "request entity too large") {

View File

@ -19,6 +19,7 @@ var path = require('path');
var runtime = require("./runtime"); var runtime = require("./runtime");
var runtimeAPI = require("./runtime-api"); var runtimeAPI = require("./runtime-api");
var redUtil = require("./util");
var api = require("./api"); var api = require("./api");
@ -66,18 +67,18 @@ module.exports = {
if (!userSettings.coreNodesDir) { if (!userSettings.coreNodesDir) {
userSettings.coreNodesDir = path.resolve(path.join(__dirname,"..","nodes")); userSettings.coreNodesDir = path.resolve(path.join(__dirname,"..","nodes"));
} }
redUtil.init(userSettings);
if (userSettings.httpAdminRoot !== false) { if (userSettings.httpAdminRoot !== false) {
runtime.init(userSettings,api); runtime.init(userSettings,redUtil,api);
runtimeAPI.init(runtime); runtimeAPI.init(runtime,redUtil);
api.init(httpServer,userSettings,runtime,runtimeAPI); api.init(httpServer,userSettings,runtime,runtimeAPI,redUtil);
apiEnabled = true; apiEnabled = true;
server = runtime.adminApi.server; server = runtime.adminApi.server;
runtime.server = runtime.adminApi.server; runtime.server = runtime.adminApi.server;
} else { } else {
runtime.init(userSettings); runtime.init(userSettings,redUtil);
apiEnabled = false; apiEnabled = false;
if (httpServer){ if (httpServer){
server = httpServer; server = httpServer;
@ -106,7 +107,7 @@ module.exports = {
}) })
}, },
nodes: runtime.nodes, nodes: runtime.nodes,
log: runtime.log, get log() { return redUtil.log },
settings:runtime.settings, settings:runtime.settings,
util: runtime.util, util: runtime.util,
version: runtime.version, version: runtime.version,

View File

@ -21,12 +21,11 @@
* @type {object} * @type {object}
*/ */
/** /**
* @namespace RED * @namespace RED
*/ */
var api = module.exports = { var api = module.exports = {
init: function(runtime) { init: function(runtime, redUtil) {
api.flows.init(runtime); api.flows.init(runtime);
api.nodes.init(runtime); api.nodes.init(runtime);
api.settings.init(runtime); api.settings.init(runtime);

View File

@ -19,8 +19,6 @@ var when = require('when');
var redNodes = require("./nodes"); var redNodes = require("./nodes");
var storage = require("./storage"); var storage = require("./storage");
var library = require("./library"); var library = require("./library");
var log = require("./log");
var i18n = require("./i18n");
var events = require("./events"); var events = require("./events");
var settings = require("./settings"); var settings = require("./settings");
@ -29,6 +27,10 @@ var path = require('path');
var fs = require("fs"); var fs = require("fs");
var os = require("os"); var os = require("os");
var redUtil;
var log;
var i18n;
var runtimeMetricInterval = null; var runtimeMetricInterval = null;
var started = false; var started = false;
@ -55,9 +57,12 @@ var adminApi = {
var nodeApp; var nodeApp;
function init(userSettings,_adminApi) { function init(userSettings,_redUtil,_adminApi) {
redUtil = _redUtil;
log = redUtil.log;
i18n = redUtil.i18n;
userSettings.version = getVersion(); userSettings.version = getVersion();
log.init(userSettings);
settings.init(userSettings); settings.init(userSettings);
nodeApp = express(); nodeApp = express();
@ -86,10 +91,7 @@ function getVersion() {
} }
function start() { 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 storage.init(runtime)})
.then(function() { return settings.load(storage)}) .then(function() { return settings.load(storage)})
.then(function() { .then(function() {
@ -241,8 +243,8 @@ var runtime = module.exports = {
version: getVersion, version: getVersion,
log: log, get log() { return log },
i18n: i18n, get i18n() { return i18n },
settings: settings, settings: settings,
storage: storage, storage: storage,
events: events, events: events,

View File

@ -19,7 +19,7 @@ var EventEmitter = require("events").EventEmitter;
var when = require("when"); var when = require("when");
var redUtil = require("../util"); var redUtil = require("../util");
var Log = require("../log"); var Log = require("../../util").log; // TODO: separate module
var context = require("./context"); var context = require("./context");
var flows = require("./flows"); var flows = require("./flows");

View File

@ -18,7 +18,8 @@ var when = require("when");
var crypto = require('crypto'); var crypto = require('crypto');
var runtime; var runtime;
var settings; var settings;
var log; var log = require("../../util").log; // TODO: separate module
var encryptedCredentials = null; var encryptedCredentials = null;
var credentialCache = {}; var credentialCache = {};

View File

@ -17,7 +17,7 @@
var when = require("when"); var when = require("when");
var clone = require("clone"); var clone = require("clone");
var typeRegistry = require("../registry"); var typeRegistry = require("../registry");
var Log = require("../../log"); var Log = require("../../../util").log; // TODO: separate module
var redUtil = require("../../util"); var redUtil = require("../../util");
var flowUtil = require("./util"); var flowUtil = require("./util");

View File

@ -24,7 +24,7 @@ var context = require("../context")
var credentials = require("../credentials"); var credentials = require("../credentials");
var flowUtil = require("./util"); var flowUtil = require("./util");
var log = require("../../log"); var log = require("../../../util").log; // TODO: separate module
var events = require("../../events"); var events = require("../../events");
var redUtil = require("../../util"); var redUtil = require("../../util");
var deprecated = require("../registry/deprecated"); var deprecated = require("../registry/deprecated");

View File

@ -25,7 +25,7 @@ var flows = require("./flows");
var flowUtil = require("./flows/util") var flowUtil = require("./flows/util")
var context = require("./context"); var context = require("./context");
var Node = require("./Node"); var Node = require("./Node");
var log = null; var log = require("../../util").log; // TODO: separate module
var library = require("./library"); var library = require("./library");
var events = require("../events"); var events = require("../events");
@ -94,7 +94,6 @@ function createNode(node,def) {
function init(runtime) { function init(runtime) {
settings = runtime.settings; settings = runtime.settings;
log = runtime.log;
credentials.init(runtime); credentials.init(runtime);
flows.init(runtime); flows.init(runtime);
registry.init(runtime); registry.init(runtime);

View File

@ -20,7 +20,7 @@ var path = require("path");
var fs = require("fs"); var fs = require("fs");
var registry = require("./registry"); var registry = require("./registry");
var log = require("../../log"); var log = require("../../../util").log; // TODO: separate module
var events = require("../../events"); var events = require("../../events");

View File

@ -19,8 +19,8 @@ var fs = require("fs");
var path = require("path"); var path = require("path");
var events; var events;
var log; var log = require("../../../util").log; // TODO: separate module
var i18n; var i18n = require("../../../util").i18n; // TODO: separate module
var settings; var settings;
var disableNodePathScan = false; var disableNodePathScan = false;
@ -29,8 +29,6 @@ var iconFileExtensions = [".png", ".gif"];
function init(runtime) { function init(runtime) {
settings = runtime.settings; settings = runtime.settings;
events = runtime.events; events = runtime.events;
log = runtime.log;
i18n = runtime.i18n;
} }
function isIncluded(name) { function isIncluded(name) {

View File

@ -17,7 +17,7 @@
var when = require("when"); var when = require("when");
var clone = require("clone"); var clone = require("clone");
var assert = require("assert"); var assert = require("assert");
var log = require("./log"); var log = require("../util").log; // TODO: separate module
var util = require("./util"); var util = require("./util");
// localSettings are those provided in the runtime settings.js file // localSettings are those provided in the runtime settings.js file

View File

@ -18,7 +18,7 @@ var when = require('when');
var Path = require('path'); var Path = require('path');
var crypto = require('crypto'); var crypto = require('crypto');
var log = require("../log"); var log = require("../../util").log; // TODO: separate module
var runtime; var runtime;
var storageModule; var storageModule;

View File

@ -18,7 +18,8 @@ var fs = require('fs-extra');
var when = require('when'); var when = require('when');
var fspath = require("path"); var fspath = require("path");
var log = require("../../log"); var log = require("../../../util").log; // TODO: separate module
var util = require("./util"); var util = require("./util");
var library = require("./library"); var library = require("./library");
var sessions = require("./sessions"); var sessions = require("./sessions");

View File

@ -18,7 +18,8 @@ var when = require('when');
var fs = require('fs-extra'); var fs = require('fs-extra');
var fspath = require("path"); var fspath = require("path");
var log = require("../../log"); var log = require("../../../util").log; // TODO: separate module
var util = require("./util"); var util = require("./util");
var sessionsFile; var sessionsFile;

View File

@ -18,7 +18,7 @@ var when = require('when');
var fs = require('fs-extra'); var fs = require('fs-extra');
var fspath = require("path"); var fspath = require("path");
var log = require("../../log"); var log = require("../../../util").log; // TODO: separate module
var util = require("./util"); var util = require("./util");
var globalSettingsFile; var globalSettingsFile;

View File

@ -18,7 +18,7 @@ var fs = require('fs-extra');
var when = require('when'); var when = require('when');
var nodeFn = require('when/node/function'); var nodeFn = require('when/node/function');
var log = require("../../log"); var log = require("../../../util").log; // TODO: separate module
function parseJSON(data) { function parseJSON(data) {
if (data.charCodeAt(0) === 0xFEFF) { if (data.charCodeAt(0) === 0xFEFF) {

View File

@ -23,6 +23,7 @@ var defaultLang = "en-US";
var resourceMap = {}; var resourceMap = {};
var resourceCache = {}; var resourceCache = {};
var initPromise;
function registerMessageCatalogs(catalogs) { function registerMessageCatalogs(catalogs) {
var promises = catalogs.map(function(catalog) { var promises = catalogs.map(function(catalog) {
@ -32,12 +33,14 @@ function registerMessageCatalogs(catalogs) {
} }
function registerMessageCatalog(namespace,dir,file) { function registerMessageCatalog(namespace,dir,file) {
return when.promise(function(resolve,reject) { return initPromise.then(function() {
when.promise(function(resolve,reject) {
resourceMap[namespace] = { basedir:dir, file:file}; resourceMap[namespace] = { basedir:dir, file:file};
i18n.loadNamespace(namespace,function() { i18n.loadNamespace(namespace,function() {
resolve(); resolve();
}); });
}); });
});
} }
function mergeCatalog(fallback,catalog) { function mergeCatalog(fallback,catalog) {
@ -82,7 +85,8 @@ var MessageFileLoader = {
} }
function init() { function init() {
return when.promise(function(resolve,reject) { if (!initPromise) {
initPromise = when.promise(function(resolve,reject) {
i18n.backend(MessageFileLoader); i18n.backend(MessageFileLoader);
i18n.init({ i18n.init({
ns: { ns: {
@ -95,6 +99,7 @@ function init() {
}); });
}); });
} }
}
function getCatalog(namespace,lang) { function getCatalog(namespace,lang) {
var result = null; var result = null;

28
red/util/index.js Normal file
View File

@ -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
}