node-red/packages/node_modules/node-red/lib/red.js

139 lines
4.2 KiB
JavaScript

/*!
* 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 fs = require("fs");
var path = require('path');
var runtime = require("@node-red/runtime");
var redUtil = require("@node-red/util");
var api = require("@node-red/editor-api");
var server = null;
var apiEnabled = false;
function checkVersion(userSettings) {
var semver = require('semver');
if (!semver.satisfies(process.version,">=4.8.0")) {
// TODO: in the future, make this a hard error.
// var e = new Error("Unsupported version of node.js");
// e.code = "unsupported_version";
// throw e;
userSettings.UNSUPPORTED_VERSION = process.version;
}
}
/**
* This module provides the full Node-RED application, with both the runtime
* and editor components built in.
*
* @namespace node-red
*/
module.exports = {
/**
* Initialise the Node-RED application.
* @param {server} httpServer - the HTTP server object to use
* @param {Object} userSettings - an object containing the runtime settings
* @memberof node-red
*/
init: function(httpServer, userSettings) {
if (!userSettings) {
userSettings = httpServer;
httpServer = null;
}
if (!userSettings.SKIP_BUILD_CHECK) {
checkVersion(userSettings);
}
if (!userSettings.coreNodesDir) {
userSettings.coreNodesDir = path.dirname(require.resolve("@node-red/nodes"))
}
redUtil.init(userSettings);
if (userSettings.httpAdminRoot !== false) {
runtime.init(userSettings,httpServer,api);
api.init(userSettings,httpServer,runtime.storage,runtime);
apiEnabled = true;
server = httpServer;
} else {
runtime.init(userSettings);
apiEnabled = false;
if (httpServer) {
server = httpServer;
} else {
server = null;
}
}
return;
},
/**
* Start the Node-RED application.
* @return {Promise} - resolves when complete
* @memberof node-red
*/
start: function() {
return runtime.start().then(function() {
if (apiEnabled) {
return api.start();
}
});
},
/**
* Stop the Node-RED application.
* @return {Promise} - resolves when complete
* @memberof node-red
*/
stop: function() {
return runtime.stop().then(function() {
if (apiEnabled) {
return api.stop();
}
})
},
log: redUtil.log,
util: redUtil.util,
get nodes() { console.log("Deprecated use of RED.nodes - refer to API documentation on RED.runtime.nodes"); return runtime._.nodes },
get settings() { console.log("Deprecated use of RED.settings - refer to API documentation on RED.runtime.settings"); return runtime._.settings },
get version() { console.log("Deprecated use of RED.version - refer to API documentation on RED.runtime.version"); return runtime._.version },
get events() { console.log("Deprecated use of RED.events - refer to API documentation on RED.runtime.events"); return runtime.events },
/**
* The express application for the Editor Admin API
* @memberof node-red
*/
get httpAdmin() { return api.adminApp },
/**
* The express application for HTTP Nodes
* @memberof node-red
*/
get httpNode() { return runtime.httpNode },
/**
* The HTTP Server used by the runtime
* @memberof node-red
*/
get server() { return server },
/**
* The runtime api
* @see @node-red/runtime
* @memberof node-red
*/
runtime: runtime
};