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 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);
}
}

View File

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

View File

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

View File

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

View File

@ -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") {

View File

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

View File

@ -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);

View File

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

View File

@ -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");

View File

@ -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 = {};

View File

@ -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");

View File

@ -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");

View File

@ -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);

View File

@ -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");

View File

@ -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) {

View File

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

View File

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

View File

@ -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");

View File

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

View File

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

View File

@ -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) {

View File

@ -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) {

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
}