mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Compare commits
92 Commits
runtime-ap
...
0.19.5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c685a31056 | ||
|
|
c32ce3bb7b | ||
|
|
4f87ebdf0a | ||
|
|
19b6cba398 | ||
|
|
5cf9c07b73 | ||
|
|
211eeea05d | ||
|
|
51a3521834 | ||
|
|
6a9575e9f4 | ||
|
|
62088259ae | ||
|
|
925ebcc06e | ||
|
|
673a6bbe2c | ||
|
|
cf32a33984 | ||
|
|
98c1bc276d | ||
|
|
629536b562 | ||
|
|
1441042458 | ||
|
|
58c8311d56 | ||
|
|
e03a0fffa9 | ||
|
|
8e2c12f8d9 | ||
|
|
7cec7ae608 | ||
|
|
c49f722e4f | ||
|
|
d4d95a43b6 | ||
|
|
a345089c8b | ||
|
|
67c268e13d | ||
|
|
ce85c8d986 | ||
|
|
cb35604ef5 | ||
|
|
61681bb1d6 | ||
|
|
1a226c4dc6 | ||
|
|
08fccc4e77 | ||
|
|
c1d50e82e1 | ||
|
|
9777af7cb5 | ||
|
|
fd86035865 | ||
|
|
a8ec032553 | ||
|
|
66ee27c5fa | ||
|
|
17a737ca88 | ||
|
|
75e7c0e50d | ||
|
|
fc0cf1ff51 | ||
|
|
0f4d46671f | ||
|
|
048f9c0294 | ||
|
|
ca77842b5b | ||
|
|
6fa8b7f5f1 | ||
|
|
a2d03c14ae | ||
|
|
c667a0e74c | ||
|
|
8123828113 | ||
|
|
72b8dbb45b | ||
|
|
7703875740 | ||
|
|
6442bb8a13 | ||
|
|
f29d7c9252 | ||
|
|
2f7f53ed96 | ||
|
|
94031a52a5 | ||
|
|
d67f91e7ed | ||
|
|
f37697c4fb | ||
|
|
69448c7329 | ||
|
|
8e9815fb91 | ||
|
|
4cdd7978cf | ||
|
|
40d81358f4 | ||
|
|
c7b62aed91 | ||
|
|
c0e7d6d826 | ||
|
|
f809377de8 | ||
|
|
9767bd9697 | ||
|
|
3a55528552 | ||
|
|
56197ffe3a | ||
|
|
0f0d0c046c | ||
|
|
ecc4973645 | ||
|
|
3169f93cc2 | ||
|
|
c1a1a73599 | ||
|
|
db1b0ccb79 | ||
|
|
df161ce672 | ||
|
|
72fe30892e | ||
|
|
d373105b32 | ||
|
|
28b311b7ed | ||
|
|
dcda513901 | ||
|
|
72c400794c | ||
|
|
042409f870 | ||
|
|
5b8f4f4069 | ||
|
|
d132d63c1d | ||
|
|
4374506981 | ||
|
|
ef8b936069 | ||
|
|
36e3bfffb4 | ||
|
|
91a38bdb60 | ||
|
|
f169a68319 | ||
|
|
ee886f98dd | ||
|
|
a3826cc6a7 | ||
|
|
ba33b832ba | ||
|
|
f6c017176b | ||
|
|
7a01b115bb | ||
|
|
1dc021e871 | ||
|
|
c9f916ebab | ||
|
|
ff627fd128 | ||
|
|
695873d35a | ||
|
|
15da19dcea | ||
|
|
d5bdc1600b | ||
|
|
dfa077fd5f |
77
CHANGELOG.md
77
CHANGELOG.md
@@ -1,3 +1,79 @@
|
||||
#### 0.19.5: Maintenance Release
|
||||
|
||||
- Recognize pip installs of RPi.GPIO (#1934)
|
||||
- Merge pull request #1941 from node-red-hitachi/master-batch
|
||||
- Merge pull request #1931 from node-red-hitachi/master-typedinput
|
||||
- Set min value of properties and spinners for batch
|
||||
- Fix that unnecessary optionMenu remains
|
||||
- Merge pull request #1894 from node-red-hitachi/fix-overlapping-file-node-execution
|
||||
- Merge pull request #1924 from imZack/patch-1
|
||||
- Add missing comma
|
||||
- Do not disable context sidebar during node edit Fixes #1921
|
||||
- Don't allow virtual links to be spliced Fixes #1920
|
||||
- Merge project package changes to avoid overwritten changes
|
||||
- Handle manually added project deps that are unused Fixes #1908
|
||||
- update close & input handling of File node
|
||||
- make close handler argument only one
|
||||
- Merge pull request #1907 from amilajack/patch-2
|
||||
- Change repo badge to point to master branch
|
||||
- invoke callbacks if async handler is specified
|
||||
- Merge pull request #1891 from camlow325/resolve-example-path-for-windows-support
|
||||
- Merge pull request #1900 from kazuhitoyokoi/master-addtestcases4settings.js
|
||||
- wait closing while pending messages exist
|
||||
- Add test cases for red/api/editor/settings.js
|
||||
- Ensure all palette categories are opened properly Closes #1893
|
||||
- Resolve path when sending example file for Windows support
|
||||
- fix multiple input message processing of file node
|
||||
|
||||
#### 0.19.4: Maintenance Release
|
||||
|
||||
- Fix race condition in non-cache lfs context Fixes #1888
|
||||
- LocalFileSystem Context: Remove extra flush code
|
||||
- Prevent race condition in caching mode of lfs context (#1889)
|
||||
- Allow context store name to be provided in the key
|
||||
- Switch node: only use promises when absolutely necessary
|
||||
- Fix dbl-click handling on webkit-based browsers
|
||||
- Ensure context.flow/global cannot be deleted or enumerated
|
||||
- Handle context.get with multiple levels of unknown key Fixes #1883
|
||||
- Fix global.get("foo.bar") for functionGlobalContext set values
|
||||
- Fix node color bug (#1877)
|
||||
- Merge pull request #1857 from cclauss/patch-1
|
||||
- Define raw_input() in Python 3 & fix time.sleep()
|
||||
|
||||
#### 0.19.3: Maintenance Release
|
||||
|
||||
- Split node - fix complete to send msg for k/v object
|
||||
- Remove unused Join node merged object key typed input
|
||||
- Set the JavaScript editor to full-screen
|
||||
- Filter global modules installed locally
|
||||
- Add svg to permitted icon extension list
|
||||
- Debug node - indicate status all the time if selected to do so
|
||||
- pi nodes - increase test coverage slightly
|
||||
- TCP-request node - only write payload
|
||||
- JSON schema: perform validation when obj -> obj or str -> str
|
||||
- JSON schema: add draft-06 support (via $schema keyword)
|
||||
- Mqtt proxy configuration for websocket connection, #1651.
|
||||
- Allows MQTT Shared Subscriptions for MQTT-In core node
|
||||
- Fix use of HTML tag or CSS class specification as icon of typedInput
|
||||
|
||||
#### 0.19.2: Maintenance Release
|
||||
|
||||
- Ensure node default colour is used if palette.theme has no match
|
||||
- fix lost messages / properties in TCPRequest Node; closes #1863 (#1864)
|
||||
- Fix typo in template.html
|
||||
- Improve error reporting from context plugin loading
|
||||
- Prevent no-op edit of node marking as changed due to icon
|
||||
- Change node must handle empty rule set
|
||||
|
||||
#### 0.19.1: Maintenance Release
|
||||
|
||||
- Pull in latest twitter node
|
||||
- Handle windows paths for context storage
|
||||
- Handle persisting objects with circular refs in context
|
||||
- Ensure js editor can expand to fill available space
|
||||
- Add example localfilesystem contextStorage to settings
|
||||
- Fix template node handling of nested context tags
|
||||
|
||||
#### 0.19: Milestone Release
|
||||
|
||||
Editor
|
||||
@@ -59,6 +135,7 @@ Runtime
|
||||
- Handle loading empty nodesDir
|
||||
- Add 'private' property to userDir generated package.json
|
||||
- Add RED.require to allow nodes to access other modules
|
||||
- Ensure add/remove modules are run sequentially
|
||||
|
||||
#### 0.18.7: Maintenance Release
|
||||
|
||||
|
||||
24
Gruntfile.js
24
Gruntfile.js
@@ -414,24 +414,6 @@ module.exports = function(grunt) {
|
||||
cwd: '<%= paths.dist %>/',
|
||||
src: ['node-red-<%= pkg.version %>/**']
|
||||
}
|
||||
},
|
||||
jsdoc : {
|
||||
runtimeAPI: {
|
||||
src: 'red/runtime-api/*.js',
|
||||
options: {
|
||||
destination: 'docs',
|
||||
configure: './jsdoc.json'
|
||||
}
|
||||
}
|
||||
},
|
||||
jsdoc2md: {
|
||||
runtimeAPI: {
|
||||
options: {
|
||||
separators: true
|
||||
},
|
||||
src: 'red/runtime-api/*.js',
|
||||
dest: 'docs/runtime-api.md'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -450,8 +432,6 @@ module.exports = function(grunt) {
|
||||
grunt.loadNpmTasks('grunt-jsonlint');
|
||||
grunt.loadNpmTasks('grunt-mocha-istanbul');
|
||||
grunt.loadNpmTasks('grunt-webdriver');
|
||||
grunt.loadNpmTasks('grunt-jsdoc');
|
||||
grunt.loadNpmTasks('grunt-jsdoc-to-markdown');
|
||||
|
||||
grunt.registerMultiTask('attachCopyright', function() {
|
||||
var files = this.data.src;
|
||||
@@ -534,8 +514,4 @@ module.exports = function(grunt) {
|
||||
grunt.registerTask('coverage',
|
||||
'Run Istanbul code test coverage task',
|
||||
['build','mocha_istanbul:all']);
|
||||
|
||||
grunt.registerTask('docs',
|
||||
'Generates API documentation',
|
||||
['jsdoc','jsdoc2md']);
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
http://nodered.org
|
||||
|
||||
[](https://travis-ci.org/node-red/node-red)
|
||||
[](https://travis-ci.org/node-red/node-red)
|
||||
[](https://coveralls.io/r/node-red/node-red?branch=master)
|
||||
|
||||
A visual tool for wiring the Internet of Things.
|
||||
|
||||
@@ -28,24 +28,14 @@ RED.comms = (function() {
|
||||
|
||||
function connectWS() {
|
||||
active = true;
|
||||
var wspath;
|
||||
|
||||
if (RED.settings.apiRootUrl) {
|
||||
var m = /^(https?):\/\/(.*)$/.exec(RED.settings.apiRootUrl);
|
||||
if (m) {
|
||||
console.log(m);
|
||||
wspath = "ws"+(m[1]==="https"?"s":"")+"://"+m[2]+"comms";
|
||||
}
|
||||
} else {
|
||||
var path = location.hostname;
|
||||
var port = location.port;
|
||||
if (port.length !== 0) {
|
||||
path = path+":"+port;
|
||||
}
|
||||
path = path+document.location.pathname;
|
||||
path = path+(path.slice(-1) == "/"?"":"/")+"comms";
|
||||
wspath = "ws"+(document.location.protocol=="https:"?"s":"")+"://"+path;
|
||||
var path = location.hostname;
|
||||
var port = location.port;
|
||||
if (port.length !== 0) {
|
||||
path = path+":"+port;
|
||||
}
|
||||
path = path+document.location.pathname;
|
||||
path = path+(path.slice(-1) == "/"?"":"/")+"comms";
|
||||
path = "ws"+(document.location.protocol=="https:"?"s":"")+"://"+path;
|
||||
|
||||
var auth_tokens = RED.settings.get("auth-tokens");
|
||||
pendingAuth = (auth_tokens!=null);
|
||||
@@ -58,7 +48,7 @@ RED.comms = (function() {
|
||||
}
|
||||
}
|
||||
|
||||
ws = new WebSocket(wspath);
|
||||
ws = new WebSocket(path);
|
||||
ws.onopen = function() {
|
||||
reconnectAttempts = 0;
|
||||
if (errornotification) {
|
||||
|
||||
@@ -16,13 +16,10 @@
|
||||
|
||||
RED.i18n = (function() {
|
||||
|
||||
var apiRootUrl;
|
||||
|
||||
return {
|
||||
init: function(options, done) {
|
||||
apiRootUrl = options.apiRootUrl||"";
|
||||
init: function(done) {
|
||||
i18n.init({
|
||||
resGetPath: apiRootUrl+'locales/__ns__?lng=__lng__',
|
||||
resGetPath: 'locales/__ns__?lng=__lng__',
|
||||
dynamicLoad: false,
|
||||
load:'current',
|
||||
ns: {
|
||||
@@ -39,7 +36,7 @@ RED.i18n = (function() {
|
||||
}
|
||||
|
||||
},
|
||||
loadNodeCatalog: function(namespace,done) {
|
||||
loadCatalog: function(namespace,done) {
|
||||
var languageList = i18n.functions.toLanguages(i18n.detectLanguage());
|
||||
var toLoad = languageList.length;
|
||||
languageList.forEach(function(lang) {
|
||||
@@ -48,7 +45,7 @@ RED.i18n = (function() {
|
||||
"Accept":"application/json"
|
||||
},
|
||||
cache: false,
|
||||
url: apiRootUrl+'nodes/'+namespace+'/messages?lng='+lang,
|
||||
url: 'locales/'+namespace+'?lng='+lang,
|
||||
success: function(data) {
|
||||
i18n.addResourceBundle(lang,namespace,data);
|
||||
toLoad--;
|
||||
@@ -71,7 +68,7 @@ RED.i18n = (function() {
|
||||
"Accept":"application/json"
|
||||
},
|
||||
cache: false,
|
||||
url: apiRootUrl+'nodes/messages?lng='+lang,
|
||||
url: 'locales/nodes?lng='+lang,
|
||||
success: function(data) {
|
||||
var namespaces = Object.keys(data);
|
||||
namespaces.forEach(function(ns) {
|
||||
|
||||
@@ -13,12 +13,489 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
(function() {
|
||||
|
||||
$(function() {
|
||||
if ((window.location.hostname !== "localhost") && (window.location.hostname !== "127.0.0.1")) {
|
||||
document.title = document.title+" : "+window.location.hostname;
|
||||
function appendNodeConfig(nodeConfig) {
|
||||
var m = /<!-- --- \[red-module:(\S+)\] --- -->/.exec(nodeConfig.trim());
|
||||
var moduleId;
|
||||
if (m) {
|
||||
moduleId = m[1];
|
||||
} else {
|
||||
moduleId = "unknown";
|
||||
}
|
||||
try {
|
||||
$("body").append(nodeConfig);
|
||||
} catch(err) {
|
||||
RED.notify(RED._("notification.errors.failedToAppendNode",{module:moduleId, error:err.toString()}),{
|
||||
type: "error",
|
||||
timeout: 10000
|
||||
});
|
||||
console.log("["+moduleId+"] "+err.toString());
|
||||
}
|
||||
}
|
||||
RED.init({
|
||||
apiRootUrl: ""
|
||||
|
||||
function loadNodeList() {
|
||||
$.ajax({
|
||||
headers: {
|
||||
"Accept":"application/json"
|
||||
},
|
||||
cache: false,
|
||||
url: 'nodes',
|
||||
success: function(data) {
|
||||
RED.nodes.setNodeList(data);
|
||||
RED.i18n.loadNodeCatalogs(function() {
|
||||
loadIconList(loadNodes);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function loadIconList(done) {
|
||||
$.ajax({
|
||||
headers: {
|
||||
"Accept":"application/json"
|
||||
},
|
||||
cache: false,
|
||||
url: 'icons',
|
||||
success: function(data) {
|
||||
RED.nodes.setIconSets(data);
|
||||
if (done) {
|
||||
done();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function loadNodes() {
|
||||
$.ajax({
|
||||
headers: {
|
||||
"Accept":"text/html"
|
||||
},
|
||||
cache: false,
|
||||
url: 'nodes',
|
||||
success: function(data) {
|
||||
var configs = data.trim().split(/(?=<!-- --- \[red-module:\S+\] --- -->)/);
|
||||
configs.forEach(function(data) {
|
||||
appendNodeConfig(data);
|
||||
});
|
||||
|
||||
$("body").i18n();
|
||||
$("#palette > .palette-spinner").hide();
|
||||
$(".palette-scroll").removeClass("hide");
|
||||
$("#palette-search").removeClass("hide");
|
||||
loadFlows(function() {
|
||||
if (RED.settings.theme("projects.enabled",false)) {
|
||||
RED.projects.refresh(function(activeProject) {
|
||||
RED.sidebar.info.refresh()
|
||||
if (!activeProject) {
|
||||
// Projects enabled but no active project
|
||||
RED.menu.setDisabled('menu-item-projects-open',true);
|
||||
RED.menu.setDisabled('menu-item-projects-settings',true);
|
||||
if (activeProject === false) {
|
||||
// User previously decline the migration to projects.
|
||||
} else { // null/undefined
|
||||
RED.projects.showStartup();
|
||||
}
|
||||
}
|
||||
completeLoad();
|
||||
});
|
||||
} else {
|
||||
// Projects disabled by the user
|
||||
RED.sidebar.info.refresh()
|
||||
completeLoad();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function loadFlows(done) {
|
||||
$.ajax({
|
||||
headers: {
|
||||
"Accept":"application/json",
|
||||
},
|
||||
cache: false,
|
||||
url: 'flows',
|
||||
success: function(nodes) {
|
||||
if (nodes) {
|
||||
var currentHash = window.location.hash;
|
||||
RED.nodes.version(nodes.rev);
|
||||
RED.nodes.import(nodes.flows);
|
||||
RED.nodes.dirty(false);
|
||||
RED.view.redraw(true);
|
||||
if (/^#flow\/.+$/.test(currentHash)) {
|
||||
RED.workspaces.show(currentHash.substring(6));
|
||||
}
|
||||
}
|
||||
done();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function completeLoad() {
|
||||
var persistentNotifications = {};
|
||||
RED.comms.subscribe("notification/#",function(topic,msg) {
|
||||
var parts = topic.split("/");
|
||||
var notificationId = parts[1];
|
||||
if (notificationId === "runtime-deploy") {
|
||||
// handled in ui/deploy.js
|
||||
return;
|
||||
}
|
||||
if (notificationId === "node") {
|
||||
// handled below
|
||||
return;
|
||||
}
|
||||
if (notificationId === "project-update") {
|
||||
RED.nodes.clear();
|
||||
RED.history.clear();
|
||||
RED.view.redraw(true);
|
||||
RED.projects.refresh(function() {
|
||||
loadFlows(function() {
|
||||
var project = RED.projects.getActiveProject();
|
||||
var message = {
|
||||
"change-branch": RED._("notification.project.change-branch", {project: project.git.branches.local}),
|
||||
"merge-abort": RED._("notification.project.merge-abort"),
|
||||
"loaded": RED._("notification.project.loaded", {project: msg.project}),
|
||||
"updated": RED._("notification.project.updated", {project: msg.project}),
|
||||
"pull": RED._("notification.project.pull", {project: msg.project}),
|
||||
"revert": RED._("notification.project.revert", {project: msg.project}),
|
||||
"merge-complete": RED._("notification.project.merge-complete")
|
||||
}[msg.action];
|
||||
RED.notify("<p>"+message+"</p>");
|
||||
RED.sidebar.info.refresh()
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.text) {
|
||||
msg.default = msg.text;
|
||||
var text = RED._(msg.text,msg);
|
||||
var options = {
|
||||
type: msg.type,
|
||||
fixed: msg.timeout === undefined,
|
||||
timeout: msg.timeout,
|
||||
id: notificationId
|
||||
}
|
||||
if (notificationId === "runtime-state") {
|
||||
if (msg.error === "missing-types") {
|
||||
text+="<ul><li>"+msg.types.join("</li><li>")+"</li></ul>";
|
||||
if (!!RED.projects.getActiveProject()) {
|
||||
options.buttons = [
|
||||
{
|
||||
text: RED._("notification.label.manage-project-dep"),
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
RED.projects.settings.show('deps');
|
||||
}
|
||||
}
|
||||
]
|
||||
// } else if (RED.settings.theme('palette.editable') !== false) {
|
||||
} else {
|
||||
options.buttons = [
|
||||
{
|
||||
text: RED._("common.label.close"),
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
} else if (msg.error === "credentials_load_failed") {
|
||||
if (RED.settings.theme("projects.enabled",false)) {
|
||||
// projects enabled
|
||||
if (RED.user.hasPermission("projects.write")) {
|
||||
options.buttons = [
|
||||
{
|
||||
text: RED._("notification.label.setup-cred"),
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
RED.projects.showCredentialsPrompt();
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
} else {
|
||||
options.buttons = [
|
||||
{
|
||||
text: RED._("common.label.close"),
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
} else if (msg.error === "missing_flow_file") {
|
||||
if (RED.user.hasPermission("projects.write")) {
|
||||
options.buttons = [
|
||||
{
|
||||
text: RED._("notification.label.setup-project"),
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
RED.projects.showFilesPrompt();
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
} else if (msg.error === "missing_package_file") {
|
||||
if (RED.user.hasPermission("projects.write")) {
|
||||
options.buttons = [
|
||||
{
|
||||
text: RED._("notification.label.create-default-package"),
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
RED.projects.createDefaultPackageFile();
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
} else if (msg.error === "project_empty") {
|
||||
if (RED.user.hasPermission("projects.write")) {
|
||||
options.buttons = [
|
||||
{
|
||||
text: RED._("notification.label.no-thanks"),
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
}
|
||||
},
|
||||
{
|
||||
text: RED._("notification.label.create-default-project"),
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
RED.projects.createDefaultFileSet();
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
} else if (msg.error === "git_merge_conflict") {
|
||||
RED.nodes.clear();
|
||||
RED.sidebar.versionControl.refresh(true);
|
||||
if (RED.user.hasPermission("projects.write")) {
|
||||
options.buttons = [
|
||||
{
|
||||
text: RED._("notification.label.show-merge-conflicts"),
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
RED.sidebar.versionControl.showLocalChanges();
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!persistentNotifications.hasOwnProperty(notificationId)) {
|
||||
persistentNotifications[notificationId] = RED.notify(text,options);
|
||||
} else {
|
||||
persistentNotifications[notificationId].update(text,options);
|
||||
}
|
||||
} else if (persistentNotifications.hasOwnProperty(notificationId)) {
|
||||
persistentNotifications[notificationId].close();
|
||||
delete persistentNotifications[notificationId];
|
||||
}
|
||||
});
|
||||
RED.comms.subscribe("status/#",function(topic,msg) {
|
||||
var parts = topic.split("/");
|
||||
var node = RED.nodes.node(parts[1]);
|
||||
if (node) {
|
||||
if (msg.hasOwnProperty("text")) {
|
||||
if (msg.text[0] !== ".") {
|
||||
msg.text = node._(msg.text.toString(),{defaultValue:msg.text.toString()});
|
||||
}
|
||||
}
|
||||
node.status = msg;
|
||||
node.dirty = true;
|
||||
RED.view.redraw();
|
||||
}
|
||||
});
|
||||
RED.comms.subscribe("notification/node/#",function(topic,msg) {
|
||||
var i,m;
|
||||
var typeList;
|
||||
var info;
|
||||
if (topic == "notification/node/added") {
|
||||
var addedTypes = [];
|
||||
msg.forEach(function(m) {
|
||||
var id = m.id;
|
||||
RED.nodes.addNodeSet(m);
|
||||
addedTypes = addedTypes.concat(m.types);
|
||||
RED.i18n.loadCatalog(id, function() {
|
||||
$.get('nodes/'+id, function(data) {
|
||||
appendNodeConfig(data);
|
||||
});
|
||||
});
|
||||
});
|
||||
if (addedTypes.length) {
|
||||
typeList = "<ul><li>"+addedTypes.join("</li><li>")+"</li></ul>";
|
||||
RED.notify(RED._("palette.event.nodeAdded", {count:addedTypes.length})+typeList,"success");
|
||||
}
|
||||
loadIconList();
|
||||
} else if (topic == "notification/node/removed") {
|
||||
for (i=0;i<msg.length;i++) {
|
||||
m = msg[i];
|
||||
info = RED.nodes.removeNodeSet(m.id);
|
||||
if (info.added) {
|
||||
typeList = "<ul><li>"+m.types.join("</li><li>")+"</li></ul>";
|
||||
RED.notify(RED._("palette.event.nodeRemoved", {count:m.types.length})+typeList,"success");
|
||||
}
|
||||
}
|
||||
loadIconList();
|
||||
} else if (topic == "notification/node/enabled") {
|
||||
if (msg.types) {
|
||||
info = RED.nodes.getNodeSet(msg.id);
|
||||
if (info.added) {
|
||||
RED.nodes.enableNodeSet(msg.id);
|
||||
typeList = "<ul><li>"+msg.types.join("</li><li>")+"</li></ul>";
|
||||
RED.notify(RED._("palette.event.nodeEnabled", {count:msg.types.length})+typeList,"success");
|
||||
} else {
|
||||
$.get('nodes/'+msg.id, function(data) {
|
||||
appendNodeConfig(data);
|
||||
typeList = "<ul><li>"+msg.types.join("</li><li>")+"</li></ul>";
|
||||
RED.notify(RED._("palette.event.nodeAdded", {count:msg.types.length})+typeList,"success");
|
||||
});
|
||||
}
|
||||
}
|
||||
} else if (topic == "notification/node/disabled") {
|
||||
if (msg.types) {
|
||||
RED.nodes.disableNodeSet(msg.id);
|
||||
typeList = "<ul><li>"+msg.types.join("</li><li>")+"</li></ul>";
|
||||
RED.notify(RED._("palette.event.nodeDisabled", {count:msg.types.length})+typeList,"success");
|
||||
}
|
||||
} else if (topic == "node/upgraded") {
|
||||
RED.notify(RED._("palette.event.nodeUpgraded", {module:msg.module,version:msg.version}),"success");
|
||||
RED.nodes.registry.setModulePendingUpdated(msg.module,msg.version);
|
||||
}
|
||||
// Refresh flow library to ensure any examples are updated
|
||||
RED.library.loadFlowLibrary();
|
||||
});
|
||||
}
|
||||
|
||||
function showAbout() {
|
||||
$.get('red/about', function(data) {
|
||||
var aboutHeader = '<div style="text-align:center;">'+
|
||||
'<img width="50px" src="red/images/node-red-icon.svg" />'+
|
||||
'</div>';
|
||||
|
||||
RED.sidebar.info.set(aboutHeader+marked(data));
|
||||
RED.sidebar.info.show();
|
||||
});
|
||||
}
|
||||
|
||||
function loadEditor() {
|
||||
var menuOptions = [];
|
||||
if (RED.settings.theme("projects.enabled",false)) {
|
||||
menuOptions.push({id:"menu-item-projects-menu",label:RED._("menu.label.projects"),options:[
|
||||
{id:"menu-item-projects-new",label:RED._("menu.label.projects-new"),disabled:false,onselect:"core:new-project"},
|
||||
{id:"menu-item-projects-open",label:RED._("menu.label.projects-open"),disabled:false,onselect:"core:open-project"},
|
||||
{id:"menu-item-projects-settings",label:RED._("menu.label.projects-settings"),disabled:false,onselect:"core:show-project-settings"}
|
||||
]});
|
||||
}
|
||||
|
||||
|
||||
menuOptions.push({id:"menu-item-view-menu",label:RED._("menu.label.view.view"),options:[
|
||||
// {id:"menu-item-view-show-grid",setting:"view-show-grid",label:RED._("menu.label.view.showGrid"),toggle:true,onselect:"core:toggle-show-grid"},
|
||||
// {id:"menu-item-view-snap-grid",setting:"view-snap-grid",label:RED._("menu.label.view.snapGrid"),toggle:true,onselect:"core:toggle-snap-grid"},
|
||||
// {id:"menu-item-status",setting:"node-show-status",label:RED._("menu.label.displayStatus"),toggle:true,onselect:"core:toggle-status", selected: true},
|
||||
//null,
|
||||
// {id:"menu-item-bidi",label:RED._("menu.label.view.textDir"),options:[
|
||||
// {id:"menu-item-bidi-default",toggle:"text-direction",label:RED._("menu.label.view.defaultDir"),selected: true, onselect:function(s) { if(s){RED.text.bidi.setTextDirection("")}}},
|
||||
// {id:"menu-item-bidi-ltr",toggle:"text-direction",label:RED._("menu.label.view.ltr"), onselect:function(s) { if(s){RED.text.bidi.setTextDirection("ltr")}}},
|
||||
// {id:"menu-item-bidi-rtl",toggle:"text-direction",label:RED._("menu.label.view.rtl"), onselect:function(s) { if(s){RED.text.bidi.setTextDirection("rtl")}}},
|
||||
// {id:"menu-item-bidi-auto",toggle:"text-direction",label:RED._("menu.label.view.auto"), onselect:function(s) { if(s){RED.text.bidi.setTextDirection("auto")}}}
|
||||
// ]},
|
||||
// null,
|
||||
{id:"menu-item-sidebar",label:RED._("menu.label.sidebar.show"),toggle:true,onselect:"core:toggle-sidebar", selected: true},
|
||||
null
|
||||
]});
|
||||
menuOptions.push(null);
|
||||
menuOptions.push({id:"menu-item-import",label:RED._("menu.label.import"),options:[
|
||||
{id:"menu-item-import-clipboard",label:RED._("menu.label.clipboard"),onselect:"core:show-import-dialog"},
|
||||
{id:"menu-item-import-library",label:RED._("menu.label.library"),options:[]}
|
||||
]});
|
||||
menuOptions.push({id:"menu-item-export",label:RED._("menu.label.export"),options:[
|
||||
{id:"menu-item-export-clipboard",label:RED._("menu.label.clipboard"),onselect:"core:show-export-dialog"},
|
||||
{id:"menu-item-export-library",label:RED._("menu.label.library"),disabled:true,onselect:"core:library-export"}
|
||||
]});
|
||||
menuOptions.push(null);
|
||||
menuOptions.push({id:"menu-item-search",label:RED._("menu.label.search"),onselect:"core:search"});
|
||||
menuOptions.push(null);
|
||||
menuOptions.push({id:"menu-item-config-nodes",label:RED._("menu.label.displayConfig"),onselect:"core:show-config-tab"});
|
||||
menuOptions.push({id:"menu-item-workspace",label:RED._("menu.label.flows"),options:[
|
||||
{id:"menu-item-workspace-add",label:RED._("menu.label.add"),onselect:"core:add-flow"},
|
||||
{id:"menu-item-workspace-edit",label:RED._("menu.label.rename"),onselect:"core:edit-flow"},
|
||||
{id:"menu-item-workspace-delete",label:RED._("menu.label.delete"),onselect:"core:remove-flow"}
|
||||
]});
|
||||
menuOptions.push({id:"menu-item-subflow",label:RED._("menu.label.subflows"), options: [
|
||||
{id:"menu-item-subflow-create",label:RED._("menu.label.createSubflow"),onselect:"core:create-subflow"},
|
||||
{id:"menu-item-subflow-convert",label:RED._("menu.label.selectionToSubflow"),disabled:true,onselect:"core:convert-to-subflow"},
|
||||
]});
|
||||
menuOptions.push(null);
|
||||
if (RED.settings.theme('palette.editable') !== false) {
|
||||
menuOptions.push({id:"menu-item-edit-palette",label:RED._("menu.label.editPalette"),onselect:"core:manage-palette"});
|
||||
menuOptions.push(null);
|
||||
}
|
||||
|
||||
menuOptions.push({id:"menu-item-user-settings",label:RED._("menu.label.settings"),onselect:"core:show-user-settings"});
|
||||
menuOptions.push(null);
|
||||
|
||||
menuOptions.push({id:"menu-item-keyboard-shortcuts",label:RED._("menu.label.keyboardShortcuts"),onselect:"core:show-help"});
|
||||
menuOptions.push({id:"menu-item-help",
|
||||
label: RED.settings.theme("menu.menu-item-help.label",RED._("menu.label.help")),
|
||||
href: RED.settings.theme("menu.menu-item-help.url","http://nodered.org/docs")
|
||||
});
|
||||
menuOptions.push({id:"menu-item-node-red-version", label:"v"+RED.settings.version, onselect: "core:show-about" });
|
||||
|
||||
|
||||
RED.view.init();
|
||||
RED.userSettings.init();
|
||||
RED.user.init();
|
||||
RED.library.init();
|
||||
RED.keyboard.init();
|
||||
RED.palette.init();
|
||||
if (RED.settings.theme('palette.editable') !== false) {
|
||||
RED.palette.editor.init();
|
||||
} else {
|
||||
console.log("Palette editor disabled");
|
||||
}
|
||||
|
||||
RED.sidebar.init();
|
||||
|
||||
if (RED.settings.theme("projects.enabled",false)) {
|
||||
RED.projects.init();
|
||||
} else {
|
||||
console.log("Projects disabled");
|
||||
}
|
||||
|
||||
RED.subflow.init();
|
||||
RED.workspaces.init();
|
||||
RED.clipboard.init();
|
||||
RED.search.init();
|
||||
RED.editor.init();
|
||||
RED.diff.init();
|
||||
|
||||
RED.menu.init({id:"btn-sidemenu",options: menuOptions});
|
||||
|
||||
RED.deploy.init(RED.settings.theme("deployButton",null));
|
||||
RED.notifications.init();
|
||||
|
||||
RED.actions.add("core:show-about", showAbout);
|
||||
RED.nodes.init();
|
||||
RED.comms.connect();
|
||||
|
||||
$("#main-container").show();
|
||||
$(".header-toolbar").show();
|
||||
|
||||
loadNodeList();
|
||||
}
|
||||
|
||||
$(function() {
|
||||
|
||||
if ((window.location.hostname !== "localhost") && (window.location.hostname !== "127.0.0.1")) {
|
||||
document.title = document.title+" : "+window.location.hostname;
|
||||
}
|
||||
|
||||
ace.require("ace/ext/language_tools");
|
||||
|
||||
RED.i18n.init(function() {
|
||||
RED.settings.init(loadEditor);
|
||||
})
|
||||
});
|
||||
});
|
||||
})();
|
||||
|
||||
519
editor/js/red.js
519
editor/js/red.js
@@ -13,521 +13,4 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
var RED = (function() {
|
||||
|
||||
function appendNodeConfig(nodeConfig,done) {
|
||||
done = done || function(){};
|
||||
var m = /<!-- --- \[red-module:(\S+)\] --- -->/.exec(nodeConfig.trim());
|
||||
var moduleId;
|
||||
if (m) {
|
||||
moduleId = m[1];
|
||||
} else {
|
||||
moduleId = "unknown";
|
||||
}
|
||||
try {
|
||||
var hasDeferred = false;
|
||||
|
||||
var nodeConfigEls = $("<div>"+nodeConfig+"</div>");
|
||||
nodeConfigEls.find("script").each(function(i,el) {
|
||||
var srcUrl = $(el).attr('src');
|
||||
if (srcUrl && !/^\s*(https?:|\/|\.)/.test(srcUrl)) {
|
||||
$(el).remove();
|
||||
var newScript = document.createElement("script");
|
||||
newScript.onload = function() { $("body").append(nodeConfigEls); done() }
|
||||
$('body').append(newScript);
|
||||
newScript.src = RED.settings.apiRootUrl+srcUrl;
|
||||
hasDeferred = true;
|
||||
}
|
||||
})
|
||||
if (!hasDeferred) {
|
||||
$("body").append(nodeConfigEls);
|
||||
done();
|
||||
}
|
||||
} catch(err) {
|
||||
RED.notify(RED._("notification.errors.failedToAppendNode",{module:moduleId, error:err.toString()}),{
|
||||
type: "error",
|
||||
timeout: 10000
|
||||
});
|
||||
console.log("["+moduleId+"] "+err.toString());
|
||||
done();
|
||||
}
|
||||
}
|
||||
|
||||
function loadNodeList() {
|
||||
$.ajax({
|
||||
headers: {
|
||||
"Accept":"application/json"
|
||||
},
|
||||
cache: false,
|
||||
url: 'nodes',
|
||||
success: function(data) {
|
||||
RED.nodes.setNodeList(data);
|
||||
RED.i18n.loadNodeCatalogs(function() {
|
||||
loadIconList(loadNodes);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function loadIconList(done) {
|
||||
$.ajax({
|
||||
headers: {
|
||||
"Accept":"application/json"
|
||||
},
|
||||
cache: false,
|
||||
url: 'icons',
|
||||
success: function(data) {
|
||||
RED.nodes.setIconSets(data);
|
||||
if (done) {
|
||||
done();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function loadNodes() {
|
||||
$.ajax({
|
||||
headers: {
|
||||
"Accept":"text/html"
|
||||
},
|
||||
cache: false,
|
||||
url: 'nodes',
|
||||
success: function(data) {
|
||||
var configs = data.trim().split(/(?=<!-- --- \[red-module:\S+\] --- -->)/);
|
||||
var stepConfig = function() {
|
||||
if (configs.length === 0) {
|
||||
$("body").i18n();
|
||||
$("#palette > .palette-spinner").hide();
|
||||
$(".palette-scroll").removeClass("hide");
|
||||
$("#palette-search").removeClass("hide");
|
||||
loadFlows(function() {
|
||||
if (RED.settings.theme("projects.enabled",false)) {
|
||||
RED.projects.refresh(function(activeProject) {
|
||||
RED.sidebar.info.refresh()
|
||||
if (!activeProject) {
|
||||
// Projects enabled but no active project
|
||||
RED.menu.setDisabled('menu-item-projects-open',true);
|
||||
RED.menu.setDisabled('menu-item-projects-settings',true);
|
||||
if (activeProject === false) {
|
||||
// User previously decline the migration to projects.
|
||||
} else { // null/undefined
|
||||
RED.projects.showStartup();
|
||||
}
|
||||
}
|
||||
completeLoad();
|
||||
});
|
||||
} else {
|
||||
// Projects disabled by the user
|
||||
RED.sidebar.info.refresh()
|
||||
completeLoad();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
var config = configs.shift();
|
||||
appendNodeConfig(config,stepConfig);
|
||||
}
|
||||
}
|
||||
stepConfig();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function loadFlows(done) {
|
||||
$.ajax({
|
||||
headers: {
|
||||
"Accept":"application/json",
|
||||
},
|
||||
cache: false,
|
||||
url: 'flows',
|
||||
success: function(nodes) {
|
||||
if (nodes) {
|
||||
var currentHash = window.location.hash;
|
||||
RED.nodes.version(nodes.rev);
|
||||
RED.nodes.import(nodes.flows);
|
||||
RED.nodes.dirty(false);
|
||||
RED.view.redraw(true);
|
||||
if (/^#flow\/.+$/.test(currentHash)) {
|
||||
RED.workspaces.show(currentHash.substring(6));
|
||||
}
|
||||
}
|
||||
done();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function completeLoad() {
|
||||
var persistentNotifications = {};
|
||||
RED.comms.subscribe("notification/#",function(topic,msg) {
|
||||
var parts = topic.split("/");
|
||||
var notificationId = parts[1];
|
||||
if (notificationId === "runtime-deploy") {
|
||||
// handled in ui/deploy.js
|
||||
return;
|
||||
}
|
||||
if (notificationId === "node") {
|
||||
// handled below
|
||||
return;
|
||||
}
|
||||
if (notificationId === "project-update") {
|
||||
RED.nodes.clear();
|
||||
RED.history.clear();
|
||||
RED.view.redraw(true);
|
||||
RED.projects.refresh(function() {
|
||||
loadFlows(function() {
|
||||
var project = RED.projects.getActiveProject();
|
||||
var message = {
|
||||
"change-branch": RED._("notification.project.change-branch", {project: project.git.branches.local}),
|
||||
"merge-abort": RED._("notification.project.merge-abort"),
|
||||
"loaded": RED._("notification.project.loaded", {project: msg.project}),
|
||||
"updated": RED._("notification.project.updated", {project: msg.project}),
|
||||
"pull": RED._("notification.project.pull", {project: msg.project}),
|
||||
"revert": RED._("notification.project.revert", {project: msg.project}),
|
||||
"merge-complete": RED._("notification.project.merge-complete")
|
||||
}[msg.action];
|
||||
RED.notify("<p>"+message+"</p>");
|
||||
RED.sidebar.info.refresh()
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.text) {
|
||||
msg.default = msg.text;
|
||||
var text = RED._(msg.text,msg);
|
||||
var options = {
|
||||
type: msg.type,
|
||||
fixed: msg.timeout === undefined,
|
||||
timeout: msg.timeout,
|
||||
id: notificationId
|
||||
}
|
||||
if (notificationId === "runtime-state") {
|
||||
if (msg.error === "missing-types") {
|
||||
text+="<ul><li>"+msg.types.join("</li><li>")+"</li></ul>";
|
||||
if (!!RED.projects.getActiveProject()) {
|
||||
options.buttons = [
|
||||
{
|
||||
text: RED._("notification.label.manage-project-dep"),
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
RED.projects.settings.show('deps');
|
||||
}
|
||||
}
|
||||
]
|
||||
// } else if (RED.settings.theme('palette.editable') !== false) {
|
||||
} else {
|
||||
options.buttons = [
|
||||
{
|
||||
text: RED._("common.label.close"),
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
} else if (msg.error === "credentials_load_failed") {
|
||||
if (RED.settings.theme("projects.enabled",false)) {
|
||||
// projects enabled
|
||||
if (RED.user.hasPermission("projects.write")) {
|
||||
options.buttons = [
|
||||
{
|
||||
text: "Setup credentials",
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
RED.projects.showCredentialsPrompt();
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
} else {
|
||||
options.buttons = [
|
||||
{
|
||||
text: "Close",
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
} else if (msg.error === "missing_flow_file") {
|
||||
if (RED.user.hasPermission("projects.write")) {
|
||||
options.buttons = [
|
||||
{
|
||||
text: "Setup project files",
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
RED.projects.showFilesPrompt();
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
} else if (msg.error === "missing_package_file") {
|
||||
if (RED.user.hasPermission("projects.write")) {
|
||||
options.buttons = [
|
||||
{
|
||||
text: "Create default package file",
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
RED.projects.createDefaultPackageFile();
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
} else if (msg.error === "project_empty") {
|
||||
if (RED.user.hasPermission("projects.write")) {
|
||||
options.buttons = [
|
||||
{
|
||||
text: "No thanks",
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
}
|
||||
},
|
||||
{
|
||||
text: "Create default project files",
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
RED.projects.createDefaultFileSet();
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
} else if (msg.error === "git_merge_conflict") {
|
||||
RED.nodes.clear();
|
||||
RED.sidebar.versionControl.refresh(true);
|
||||
if (RED.user.hasPermission("projects.write")) {
|
||||
options.buttons = [
|
||||
{
|
||||
text: "Show merge conflicts",
|
||||
click: function() {
|
||||
persistentNotifications[notificationId].hideNotification();
|
||||
RED.sidebar.versionControl.showLocalChanges();
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!persistentNotifications.hasOwnProperty(notificationId)) {
|
||||
persistentNotifications[notificationId] = RED.notify(text,options);
|
||||
} else {
|
||||
persistentNotifications[notificationId].update(text,options);
|
||||
}
|
||||
} else if (persistentNotifications.hasOwnProperty(notificationId)) {
|
||||
persistentNotifications[notificationId].close();
|
||||
delete persistentNotifications[notificationId];
|
||||
}
|
||||
});
|
||||
RED.comms.subscribe("status/#",function(topic,msg) {
|
||||
var parts = topic.split("/");
|
||||
var node = RED.nodes.node(parts[1]);
|
||||
if (node) {
|
||||
if (msg.hasOwnProperty("text")) {
|
||||
if (msg.text[0] !== ".") {
|
||||
msg.text = node._(msg.text.toString(),{defaultValue:msg.text.toString()});
|
||||
}
|
||||
}
|
||||
node.status = msg;
|
||||
node.dirty = true;
|
||||
RED.view.redraw();
|
||||
}
|
||||
});
|
||||
RED.comms.subscribe("notification/node/#",function(topic,msg) {
|
||||
var i,m;
|
||||
var typeList;
|
||||
var info;
|
||||
if (topic == "notification/node/added") {
|
||||
var addedTypes = [];
|
||||
msg.forEach(function(m) {
|
||||
var id = m.id;
|
||||
RED.nodes.addNodeSet(m);
|
||||
addedTypes = addedTypes.concat(m.types);
|
||||
RED.i18n.loadNodeCatalog(id, function() {
|
||||
$.get('nodes/'+id, function(data) {
|
||||
appendNodeConfig(data);
|
||||
});
|
||||
});
|
||||
});
|
||||
if (addedTypes.length) {
|
||||
typeList = "<ul><li>"+addedTypes.join("</li><li>")+"</li></ul>";
|
||||
RED.notify(RED._("palette.event.nodeAdded", {count:addedTypes.length})+typeList,"success");
|
||||
}
|
||||
loadIconList();
|
||||
} else if (topic == "notification/node/removed") {
|
||||
for (i=0;i<msg.length;i++) {
|
||||
m = msg[i];
|
||||
info = RED.nodes.removeNodeSet(m.id);
|
||||
if (info.added) {
|
||||
typeList = "<ul><li>"+m.types.join("</li><li>")+"</li></ul>";
|
||||
RED.notify(RED._("palette.event.nodeRemoved", {count:m.types.length})+typeList,"success");
|
||||
}
|
||||
}
|
||||
loadIconList();
|
||||
} else if (topic == "notification/node/enabled") {
|
||||
if (msg.types) {
|
||||
info = RED.nodes.getNodeSet(msg.id);
|
||||
if (info.added) {
|
||||
RED.nodes.enableNodeSet(msg.id);
|
||||
typeList = "<ul><li>"+msg.types.join("</li><li>")+"</li></ul>";
|
||||
RED.notify(RED._("palette.event.nodeEnabled", {count:msg.types.length})+typeList,"success");
|
||||
} else {
|
||||
$.get('nodes/'+msg.id, function(data) {
|
||||
appendNodeConfig(data);
|
||||
typeList = "<ul><li>"+msg.types.join("</li><li>")+"</li></ul>";
|
||||
RED.notify(RED._("palette.event.nodeAdded", {count:msg.types.length})+typeList,"success");
|
||||
});
|
||||
}
|
||||
}
|
||||
} else if (topic == "notification/node/disabled") {
|
||||
if (msg.types) {
|
||||
RED.nodes.disableNodeSet(msg.id);
|
||||
typeList = "<ul><li>"+msg.types.join("</li><li>")+"</li></ul>";
|
||||
RED.notify(RED._("palette.event.nodeDisabled", {count:msg.types.length})+typeList,"success");
|
||||
}
|
||||
} else if (topic == "node/upgraded") {
|
||||
RED.notify(RED._("palette.event.nodeUpgraded", {module:msg.module,version:msg.version}),"success");
|
||||
RED.nodes.registry.setModulePendingUpdated(msg.module,msg.version);
|
||||
}
|
||||
// Refresh flow library to ensure any examples are updated
|
||||
RED.library.loadFlowLibrary();
|
||||
});
|
||||
}
|
||||
|
||||
function showAbout() {
|
||||
$.get('red/about', function(data) {
|
||||
var aboutHeader = '<div style="text-align:center;">'+
|
||||
'<img width="50px" src="red/images/node-red-icon.svg" />'+
|
||||
'</div>';
|
||||
|
||||
RED.sidebar.info.set(aboutHeader+marked(data));
|
||||
RED.sidebar.info.show();
|
||||
});
|
||||
}
|
||||
|
||||
function loadEditor() {
|
||||
var menuOptions = [];
|
||||
if (RED.settings.theme("projects.enabled",false)) {
|
||||
menuOptions.push({id:"menu-item-projects-menu",label:"Projects",options:[
|
||||
{id:"menu-item-projects-new",label:"New",disabled:false,onselect:"core:new-project"},
|
||||
{id:"menu-item-projects-open",label:"Open",disabled:false,onselect:"core:open-project"},
|
||||
{id:"menu-item-projects-settings",label:"Project Settings",disabled:false,onselect:"core:show-project-settings"}
|
||||
]});
|
||||
}
|
||||
|
||||
|
||||
menuOptions.push({id:"menu-item-view-menu",label:RED._("menu.label.view.view"),options:[
|
||||
// {id:"menu-item-view-show-grid",setting:"view-show-grid",label:RED._("menu.label.view.showGrid"),toggle:true,onselect:"core:toggle-show-grid"},
|
||||
// {id:"menu-item-view-snap-grid",setting:"view-snap-grid",label:RED._("menu.label.view.snapGrid"),toggle:true,onselect:"core:toggle-snap-grid"},
|
||||
// {id:"menu-item-status",setting:"node-show-status",label:RED._("menu.label.displayStatus"),toggle:true,onselect:"core:toggle-status", selected: true},
|
||||
//null,
|
||||
// {id:"menu-item-bidi",label:RED._("menu.label.view.textDir"),options:[
|
||||
// {id:"menu-item-bidi-default",toggle:"text-direction",label:RED._("menu.label.view.defaultDir"),selected: true, onselect:function(s) { if(s){RED.text.bidi.setTextDirection("")}}},
|
||||
// {id:"menu-item-bidi-ltr",toggle:"text-direction",label:RED._("menu.label.view.ltr"), onselect:function(s) { if(s){RED.text.bidi.setTextDirection("ltr")}}},
|
||||
// {id:"menu-item-bidi-rtl",toggle:"text-direction",label:RED._("menu.label.view.rtl"), onselect:function(s) { if(s){RED.text.bidi.setTextDirection("rtl")}}},
|
||||
// {id:"menu-item-bidi-auto",toggle:"text-direction",label:RED._("menu.label.view.auto"), onselect:function(s) { if(s){RED.text.bidi.setTextDirection("auto")}}}
|
||||
// ]},
|
||||
// null,
|
||||
{id:"menu-item-sidebar",label:RED._("menu.label.sidebar.show"),toggle:true,onselect:"core:toggle-sidebar", selected: true},
|
||||
null
|
||||
]});
|
||||
menuOptions.push(null);
|
||||
menuOptions.push({id:"menu-item-import",label:RED._("menu.label.import"),options:[
|
||||
{id:"menu-item-import-clipboard",label:RED._("menu.label.clipboard"),onselect:"core:show-import-dialog"},
|
||||
{id:"menu-item-import-library",label:RED._("menu.label.library"),options:[]}
|
||||
]});
|
||||
menuOptions.push({id:"menu-item-export",label:RED._("menu.label.export"),options:[
|
||||
{id:"menu-item-export-clipboard",label:RED._("menu.label.clipboard"),onselect:"core:show-export-dialog"},
|
||||
{id:"menu-item-export-library",label:RED._("menu.label.library"),disabled:true,onselect:"core:library-export"}
|
||||
]});
|
||||
menuOptions.push(null);
|
||||
menuOptions.push({id:"menu-item-search",label:RED._("menu.label.search"),onselect:"core:search"});
|
||||
menuOptions.push(null);
|
||||
menuOptions.push({id:"menu-item-config-nodes",label:RED._("menu.label.displayConfig"),onselect:"core:show-config-tab"});
|
||||
menuOptions.push({id:"menu-item-workspace",label:RED._("menu.label.flows"),options:[
|
||||
{id:"menu-item-workspace-add",label:RED._("menu.label.add"),onselect:"core:add-flow"},
|
||||
{id:"menu-item-workspace-edit",label:RED._("menu.label.rename"),onselect:"core:edit-flow"},
|
||||
{id:"menu-item-workspace-delete",label:RED._("menu.label.delete"),onselect:"core:remove-flow"}
|
||||
]});
|
||||
menuOptions.push({id:"menu-item-subflow",label:RED._("menu.label.subflows"), options: [
|
||||
{id:"menu-item-subflow-create",label:RED._("menu.label.createSubflow"),onselect:"core:create-subflow"},
|
||||
{id:"menu-item-subflow-convert",label:RED._("menu.label.selectionToSubflow"),disabled:true,onselect:"core:convert-to-subflow"},
|
||||
]});
|
||||
menuOptions.push(null);
|
||||
if (RED.settings.theme('palette.editable') !== false) {
|
||||
menuOptions.push({id:"menu-item-edit-palette",label:RED._("menu.label.editPalette"),onselect:"core:manage-palette"});
|
||||
menuOptions.push(null);
|
||||
}
|
||||
|
||||
menuOptions.push({id:"menu-item-user-settings",label:RED._("menu.label.settings"),onselect:"core:show-user-settings"});
|
||||
menuOptions.push(null);
|
||||
|
||||
menuOptions.push({id:"menu-item-keyboard-shortcuts",label:RED._("menu.label.keyboardShortcuts"),onselect:"core:show-help"});
|
||||
menuOptions.push({id:"menu-item-help",
|
||||
label: RED.settings.theme("menu.menu-item-help.label",RED._("menu.label.help")),
|
||||
href: RED.settings.theme("menu.menu-item-help.url","http://nodered.org/docs")
|
||||
});
|
||||
menuOptions.push({id:"menu-item-node-red-version", label:"v"+RED.settings.version, onselect: "core:show-about" });
|
||||
|
||||
|
||||
RED.view.init();
|
||||
RED.userSettings.init();
|
||||
RED.user.init();
|
||||
RED.library.init();
|
||||
RED.keyboard.init();
|
||||
RED.palette.init();
|
||||
if (RED.settings.theme('palette.editable') !== false) {
|
||||
RED.palette.editor.init();
|
||||
} else {
|
||||
console.log("Palette editor disabled");
|
||||
}
|
||||
|
||||
RED.sidebar.init();
|
||||
|
||||
if (RED.settings.theme("projects.enabled",false)) {
|
||||
RED.projects.init();
|
||||
} else {
|
||||
console.log("Projects disabled");
|
||||
}
|
||||
|
||||
RED.subflow.init();
|
||||
RED.workspaces.init();
|
||||
RED.clipboard.init();
|
||||
RED.search.init();
|
||||
RED.editor.init();
|
||||
RED.diff.init();
|
||||
|
||||
RED.menu.init({id:"btn-sidemenu",options: menuOptions});
|
||||
|
||||
RED.deploy.init(RED.settings.theme("deployButton",null));
|
||||
RED.notifications.init();
|
||||
|
||||
RED.actions.add("core:show-about", showAbout);
|
||||
RED.nodes.init();
|
||||
RED.comms.connect();
|
||||
|
||||
$("#main-container").show();
|
||||
$(".header-toolbar").show();
|
||||
|
||||
loadNodeList();
|
||||
}
|
||||
|
||||
var initialised = false;
|
||||
|
||||
function init(options) {
|
||||
if (initialised) {
|
||||
throw new Error("RED already initialised");
|
||||
}
|
||||
initialised = true;
|
||||
ace.require("ace/ext/language_tools");
|
||||
options = options || {};
|
||||
options.apiRootUrl = options.apiRootUrl || "";
|
||||
if (options.apiRootUrl && !/\/$/.test(options.apiRootUrl)) {
|
||||
options.apiRootUrl = options.apiRootUrl+"/";
|
||||
}
|
||||
RED.i18n.init(options, function() {
|
||||
RED.settings.init(options, loadEditor);
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
init: init
|
||||
}
|
||||
})();
|
||||
var RED = {};
|
||||
|
||||
@@ -89,22 +89,18 @@ RED.settings = (function () {
|
||||
userSettings = data;
|
||||
}
|
||||
|
||||
var init = function (options, done) {
|
||||
var init = function (done) {
|
||||
var accessTokenMatch = /[?&]access_token=(.*?)(?:$|&)/.exec(window.location.search);
|
||||
if (accessTokenMatch) {
|
||||
var accessToken = accessTokenMatch[1];
|
||||
RED.settings.set("auth-tokens",{access_token: accessToken});
|
||||
window.location.search = "";
|
||||
}
|
||||
RED.settings.apiRootUrl = options.apiRootUrl;
|
||||
|
||||
$.ajaxSetup({
|
||||
beforeSend: function(jqXHR,settings) {
|
||||
// Only attach auth header for requests to relative paths
|
||||
if (!/^\s*(https?:|\/|\.)/.test(settings.url)) {
|
||||
if (options.apiRootUrl) {
|
||||
settings.url = options.apiRootUrl+settings.url;
|
||||
}
|
||||
var auth_tokens = RED.settings.get("auth-tokens");
|
||||
if (auth_tokens) {
|
||||
jqXHR.setRequestHeader("Authorization","Bearer "+auth_tokens.access_token);
|
||||
|
||||
@@ -522,13 +522,25 @@
|
||||
this.selectLabel.empty();
|
||||
var image;
|
||||
if (opt.icon) {
|
||||
image = new Image();
|
||||
image.name = opt.icon;
|
||||
image.src = opt.icon;
|
||||
$('<img>',{src:opt.icon,style:"margin-right: 4px;height: 18px;"}).prependTo(this.selectLabel);
|
||||
if (opt.icon.indexOf("<") === 0) {
|
||||
$(opt.icon).prependTo(this.selectLabel);
|
||||
}
|
||||
else if (opt.icon.indexOf("/") !== -1) {
|
||||
image = new Image();
|
||||
image.name = opt.icon;
|
||||
image.src = opt.icon;
|
||||
$('<img>',{src:opt.icon,style:"margin-right: 4px;height: 18px;"}).prependTo(this.selectLabel);
|
||||
}
|
||||
else {
|
||||
$('<i>',{class:"red-ui-typedInput-icon "+opt.icon}).prependTo(this.selectLabel);
|
||||
}
|
||||
} else {
|
||||
this.selectLabel.text(opt.label);
|
||||
}
|
||||
if (this.optionMenu) {
|
||||
this.optionMenu.remove();
|
||||
this.optionMenu = null;
|
||||
}
|
||||
if (opt.options) {
|
||||
if (this.optionExpandButton) {
|
||||
this.optionExpandButton.hide();
|
||||
@@ -619,10 +631,6 @@
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (this.optionMenu) {
|
||||
this.optionMenu.remove();
|
||||
this.optionMenu = null;
|
||||
}
|
||||
if (this.optionSelectTrigger) {
|
||||
this.optionSelectTrigger.hide();
|
||||
}
|
||||
|
||||
@@ -881,7 +881,6 @@ RED.diff = (function() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var properties = Object.keys(node).filter(function(p) { return p!='inputLabels'&&p!='outputLabels'&&p!='z'&&p!='wires'&&p!=='x'&&p!=='y'&&p!=='id'&&p!=='type'&&(!def.defaults||!def.defaults.hasOwnProperty(p))});
|
||||
if (def.defaults) {
|
||||
properties = properties.concat(Object.keys(def.defaults));
|
||||
@@ -889,6 +888,13 @@ RED.diff = (function() {
|
||||
if (node.type !== 'tab') {
|
||||
properties = properties.concat(['inputLabels','outputLabels']);
|
||||
}
|
||||
if ( ((localNode && localNode.hasOwnProperty('icon')) || (remoteNode && remoteNode.hasOwnProperty('icon'))) &&
|
||||
properties.indexOf('icon') === -1
|
||||
) {
|
||||
properties.unshift('icon');
|
||||
}
|
||||
|
||||
|
||||
properties.forEach(function(d) {
|
||||
localChanged = false;
|
||||
remoteChanged = false;
|
||||
|
||||
@@ -1110,7 +1110,7 @@ RED.editor = (function() {
|
||||
changed = true;
|
||||
}
|
||||
} else {
|
||||
if (icon !== defaultIcon) {
|
||||
if (icon !== "" && icon !== defaultIcon) {
|
||||
changes.icon = editing_node.icon;
|
||||
editing_node.icon = icon;
|
||||
changed = true;
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
RED.editor.types._js = (function() {
|
||||
|
||||
|
||||
var template = '<script type="text/x-red" data-template-name="_js"><div class="form-row node-text-editor-row" style="width: 700px"><div style="height: 200px;min-height: 150px;" class="node-text-editor" id="node-input-js"></div></div></script>';
|
||||
var template = '<script type="text/x-red" data-template-name="_js"><div class="form-row node-text-editor-row"><div style="height: 200px;min-height: 150px;" class="node-text-editor" id="node-input-js"></div></div></script>';
|
||||
|
||||
return {
|
||||
init: function() {
|
||||
@@ -32,7 +32,7 @@ RED.editor.types._js = (function() {
|
||||
|
||||
var trayOptions = {
|
||||
title: options.title,
|
||||
width: "inherit",
|
||||
width: options.width||"inherit",
|
||||
buttons: [
|
||||
{
|
||||
id: "node-dialog-cancel",
|
||||
@@ -58,7 +58,6 @@ RED.editor.types._js = (function() {
|
||||
for (var i=0;i<rows.size();i++) {
|
||||
height -= $(rows[i]).outerHeight(true);
|
||||
}
|
||||
height -= (parseInt($("#dialog-form").css("marginTop"))+parseInt($("#dialog-form").css("marginBottom")));
|
||||
$(".node-text-editor").css("height",height+"px");
|
||||
expressionEditor.resize();
|
||||
},
|
||||
|
||||
@@ -18,12 +18,6 @@ RED.library = (function() {
|
||||
var exportToLibraryDialog;
|
||||
var elementPrefix = "node-input-";
|
||||
|
||||
|
||||
var _librarySaveConfirm = '<div id="node-dialog-library-save-confirm" class="hide"><form class="form-horizontal"><div style="text-align: center; padding-top: 30px;" id="node-dialog-library-save-content"></div></form></div>';
|
||||
var _librarySave = '<div id="node-dialog-library-save" class="hide"><form class="form-horizontal"><div class="form-row"><label for="node-dialog-library-save-folder" data-i18n="[append]library.folder"><i class="fa fa-folder-open"></i> </label><input type="text" id="node-dialog-library-save-folder" data-i18n="[placeholder]library.folderPlaceholder"></div><div class="form-row"><label for="node-dialog-library-save-filename" data-i18n="[append]library.filename"><i class="fa fa-file"></i> </label><input type="text" id="node-dialog-library-save-filename" data-i18n="[placeholder]library.filenamePlaceholder"></div></form></div>';
|
||||
var _libraryLookup = '<div id="node-dialog-library-lookup" class="hide"><form class="form-horizontal"><div class="form-row"><ul id="node-dialog-library-breadcrumbs" class="breadcrumb"><li class="active"><a href="#" data-i18n="[append]library.breadcrumb"></a></li></ul></div><div class="form-row"><div style="vertical-align: top; display: inline-block; height: 100%; width: 30%; padding-right: 20px;"><div id="node-select-library" style="border: 1px solid #999; width: 100%; height: 100%; overflow:scroll;"><ul></ul></div></div><div style="vertical-align: top; display: inline-block;width: 65%; height: 100%;"><div style="height: 100%; width: 95%;" class="node-text-editor" id="node-select-library-text" ></div></div></div></form></div>';
|
||||
|
||||
|
||||
function loadFlowLibrary() {
|
||||
$.getJSON("library/flows",function(data) {
|
||||
//console.log(data);
|
||||
@@ -416,11 +410,6 @@ RED.library = (function() {
|
||||
return {
|
||||
init: function() {
|
||||
|
||||
|
||||
$(_librarySave).appendTo(document.body);
|
||||
$(_librarySaveConfirm).appendTo(document.body);
|
||||
$(_libraryLookup).appendTo(document.body);
|
||||
|
||||
RED.actions.add("core:library-export",exportFlow);
|
||||
|
||||
RED.events.on("view:selection-changed",function(selection) {
|
||||
|
||||
@@ -275,7 +275,8 @@ RED.palette = (function() {
|
||||
}
|
||||
|
||||
for (var i=0;i<nodes.length;i++) {
|
||||
if (d3.select(nodes[i]).classed('link_background')) {
|
||||
var node = d3.select(nodes[i]);
|
||||
if (node.classed('link_background') && !node.classed('link_link')) {
|
||||
var length = nodes[i].getTotalLength();
|
||||
for (var j=0;j<length;j+=10) {
|
||||
var p = nodes[i].getPointAtLength(j);
|
||||
@@ -320,9 +321,9 @@ RED.palette = (function() {
|
||||
}
|
||||
setLabel(nt,$(d),label,nodeInfo);
|
||||
|
||||
var categoryNode = $("#palette-container-"+category);
|
||||
var categoryNode = $("#palette-container-"+rootCategory);
|
||||
if (categoryNode.find(".palette_node").length === 1) {
|
||||
categoryContainers[category].open();
|
||||
categoryContainers[rootCategory].open();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -461,7 +461,11 @@ RED.projects.settings = (function() {
|
||||
setTimeout(function() {
|
||||
depsList.editableList('removeItem',entry);
|
||||
refreshModuleInUseCounts();
|
||||
entry.count = modulesInUse[entry.id].count;
|
||||
if (modulesInUse.hasOwnProperty(entry.id)) {
|
||||
entry.count = modulesInUse[entry.id].count;
|
||||
} else {
|
||||
entry.count = 0;
|
||||
}
|
||||
depsList.editableList('addItem',entry);
|
||||
},500);
|
||||
}
|
||||
|
||||
@@ -17,10 +17,6 @@
|
||||
RED.subflow = (function() {
|
||||
|
||||
|
||||
var _subflowEditTemplate = '<script type="text/x-red" data-template-name="subflow"><div class="form-row"><label for="node-input-name" data-i18n="[append]editor:common.label.name"><i class="fa fa-tag"></i> </label><input type="text" id="node-input-name"></div></script>';
|
||||
var _subflowTemplateEditTemplate = '<script type="text/x-red" data-template-name="subflow-template"><div class="form-row"><i class="fa fa-tag"></i><label for="subflow-input-name" data-i18n="common.label.name"></label><input type="text" id="subflow-input-name"></div><div class="form-row" style="margin-bottom: 0px;"><label for="subflow-input-info" data-i18n="editor:subflow.info"></label><a href="https://help.github.com/articles/markdown-basics/" style="font-size: 0.8em; float: right;" data-i18n="[html]subflow.format"></a></div><div class="form-row node-text-editor-row"><div style="height: 250px;" class="node-text-editor" id="subflow-input-info-editor"></div></div><div class="form-row form-tips" id="subflow-dialog-user-count"></div></script>';
|
||||
|
||||
|
||||
function getSubflow() {
|
||||
return RED.nodes.subflow(RED.workspaces.active());
|
||||
}
|
||||
@@ -390,10 +386,6 @@ RED.subflow = (function() {
|
||||
|
||||
RED.actions.add("core:create-subflow",createSubflow);
|
||||
RED.actions.add("core:convert-to-subflow",convertToSubflow);
|
||||
|
||||
$(_subflowEditTemplate).appendTo(document.body);
|
||||
$(_subflowTemplateEditTemplate).appendTo(document.body);
|
||||
|
||||
}
|
||||
|
||||
function createSubflow() {
|
||||
|
||||
@@ -132,7 +132,7 @@ RED.sidebar.context = (function() {
|
||||
content: content,
|
||||
toolbar: footerToolbar,
|
||||
// pinned: true,
|
||||
enableOnEdit: false
|
||||
enableOnEdit: true
|
||||
});
|
||||
|
||||
// var toggleLiveButton = $("#sidebar-context-toggle-live");
|
||||
|
||||
@@ -280,8 +280,7 @@ RED.sidebar.info = (function() {
|
||||
if (infoText) {
|
||||
setInfoText(infoText);
|
||||
}
|
||||
|
||||
|
||||
$(".sidebar-node-info-stack").scrollTop(0);
|
||||
$(".node-info-property-header").click(function(e) {
|
||||
e.preventDefault();
|
||||
expandedSections["property"] = !expandedSections["property"];
|
||||
@@ -395,10 +394,9 @@ RED.sidebar.info = (function() {
|
||||
function set(html,title) {
|
||||
// tips.stop();
|
||||
// sections.show();
|
||||
// nodeSection.container.hide();
|
||||
infoSection.title.text(title||"");
|
||||
refresh(null);
|
||||
$(infoSection.content).empty();
|
||||
nodeSection.container.hide();
|
||||
infoSection.title.text(title||RED._("sidebar.info.info"));
|
||||
setInfoText(html);
|
||||
$(".sidebar-node-info-stack").scrollTop(0);
|
||||
}
|
||||
|
||||
@@ -756,25 +756,25 @@ RED.utils = (function() {
|
||||
|
||||
function getNodeIcon(def,node) {
|
||||
if (def.category === 'config') {
|
||||
return RED.settings.apiRootUrl+"icons/node-red/cog.png"
|
||||
return "icons/node-red/cog.png"
|
||||
} else if (node && node.type === 'tab') {
|
||||
return RED.settings.apiRootUrl+"icons/node-red/subflow.png"
|
||||
return "icons/node-red/subflow.png"
|
||||
} else if (node && node.type === 'unknown') {
|
||||
return RED.settings.apiRootUrl+"icons/node-red/alert.png"
|
||||
return "icons/node-red/alert.png"
|
||||
} else if (node && node.icon) {
|
||||
var iconPath = separateIconPath(node.icon);
|
||||
if (isIconExists(iconPath)) {
|
||||
return RED.settings.apiRootUrl+"icons/" + node.icon;
|
||||
return "icons/" + node.icon;
|
||||
}
|
||||
}
|
||||
|
||||
var iconPath = getDefaultNodeIcon(def, node);
|
||||
if (def.category === 'subflows') {
|
||||
if (!isIconExists(iconPath)) {
|
||||
return RED.settings.apiRootUrl+"icons/node-red/subflow.png";
|
||||
return "icons/node-red/subflow.png";
|
||||
}
|
||||
}
|
||||
return RED.settings.apiRootUrl+"icons/"+iconPath.module+"/"+iconPath.file;
|
||||
return "icons/"+iconPath.module+"/"+iconPath.file;
|
||||
}
|
||||
|
||||
function getNodeLabel(node,defaultLabel) {
|
||||
@@ -800,8 +800,9 @@ RED.utils = (function() {
|
||||
var paletteTheme = RED.settings.theme('palette.theme') || [];
|
||||
if (paletteTheme.length > 0) {
|
||||
if (!nodeColorCache.hasOwnProperty(type)) {
|
||||
nodeColorCache[type] = def.color;
|
||||
var l = paletteTheme.length;
|
||||
for (var i=0;i<l;i++ ){
|
||||
for (var i = 0; i < l; i++ ){
|
||||
var themeRule = paletteTheme[i];
|
||||
if (themeRule.hasOwnProperty('category')) {
|
||||
if (!themeRule.hasOwnProperty('_category')) {
|
||||
@@ -825,7 +826,11 @@ RED.utils = (function() {
|
||||
}
|
||||
result = nodeColorCache[type];
|
||||
}
|
||||
return result;
|
||||
if (result) {
|
||||
return result;
|
||||
} else {
|
||||
return "#ddd";
|
||||
}
|
||||
}
|
||||
|
||||
function addSpinnerOverlay(container,contain) {
|
||||
|
||||
@@ -65,13 +65,28 @@
|
||||
.attr('height',chartSize[1]/nav_scale/scaleFactor)
|
||||
}
|
||||
}
|
||||
function toggle() {
|
||||
if (!isShowing) {
|
||||
isShowing = true;
|
||||
$("#btn-navigate").addClass("selected");
|
||||
resizeNavBorder();
|
||||
refreshNodes();
|
||||
$("#chart").on("scroll",onScroll);
|
||||
navContainer.fadeIn(200);
|
||||
} else {
|
||||
isShowing = false;
|
||||
navContainer.fadeOut(100);
|
||||
$("#chart").off("scroll",onScroll);
|
||||
$("#btn-navigate").removeClass("selected");
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
init: function() {
|
||||
|
||||
$(window).resize(resizeNavBorder);
|
||||
RED.events.on("sidebar:resize",resizeNavBorder);
|
||||
|
||||
RED.actions.add("core:toggle-navigator",toggle);
|
||||
var hideTimeout;
|
||||
|
||||
navContainer = $('<div>').css({
|
||||
@@ -141,23 +156,12 @@
|
||||
|
||||
$("#btn-navigate").click(function(evt) {
|
||||
evt.preventDefault();
|
||||
if (!isShowing) {
|
||||
isShowing = true;
|
||||
$("#btn-navigate").addClass("selected");
|
||||
resizeNavBorder();
|
||||
refreshNodes();
|
||||
$("#chart").on("scroll",onScroll);
|
||||
navContainer.fadeIn(200);
|
||||
} else {
|
||||
isShowing = false;
|
||||
navContainer.fadeOut(100);
|
||||
$("#chart").off("scroll",onScroll);
|
||||
$("#btn-navigate").removeClass("selected");
|
||||
}
|
||||
toggle();
|
||||
})
|
||||
},
|
||||
refresh: refreshNodes,
|
||||
resize: resizeNavBorder
|
||||
resize: resizeNavBorder,
|
||||
toggle: toggle
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1766,7 +1766,7 @@ RED.view = (function() {
|
||||
clickTime = now;
|
||||
|
||||
dblClickPrimed = (lastClickNode == mousedown_node &&
|
||||
d3.event.buttons === 1 &&
|
||||
d3.event.button === 0 &&
|
||||
!d3.event.shiftKey && !d3.event.metaKey && !d3.event.altKey && !d3.event.ctrlKey);
|
||||
lastClickNode = mousedown_node;
|
||||
|
||||
@@ -2415,6 +2415,7 @@ RED.view = (function() {
|
||||
var l = d3.select(this);
|
||||
d.added = true;
|
||||
l.append("svg:path").attr("class","link_background link_path")
|
||||
.classed("link_link", function(d) { return d.link })
|
||||
.on("mousedown",function(d) {
|
||||
mousedown_link = d;
|
||||
clearSelection();
|
||||
|
||||
@@ -61,7 +61,13 @@
|
||||
}
|
||||
|
||||
#user-settings-tab-view {
|
||||
position: absolute;
|
||||
top:0;
|
||||
right: 0;
|
||||
left: 0;
|
||||
bottom: 0;
|
||||
padding: 8px 20px 20px;
|
||||
overflow-y: scroll;
|
||||
}
|
||||
.user-settings-row {
|
||||
padding: 5px 10px 2px;
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
</head>
|
||||
<body spellcheck="false">
|
||||
<div id="header">
|
||||
<span class="logo">{{#header.url}}<a href="{{.}}">{{/header.url}}{{#header.image}}<img src="{{.}}">{{/header.image}} <span>{{ header.title }}</span>{{#header.url}}</a>{{/header.url}}</span>
|
||||
<span class="logo">{{#header.url}}<a href="{{.}}">{{/header.url}}{{#header.image}}<img src="{{.}}" title="{{version}}">{{/header.image}} <span>{{ header.title }}</span>{{#header.url}}</a>{{/header.url}}</span>
|
||||
<ul class="header-toolbar hide">
|
||||
<li><a id="btn-sidemenu" class="button" data-toggle="dropdown" href="#"><i class="fa fa-bars"></i></a></li>
|
||||
</ul>
|
||||
@@ -83,6 +83,68 @@
|
||||
<div id="notifications"></div>
|
||||
<div id="dropTarget"><div data-i18n="[append]workspace.dropFlowHere"><br/><i class="fa fa-download"></i></div></div>
|
||||
|
||||
<div id="node-dialog-library-save-confirm" class="hide">
|
||||
<form class="form-horizontal">
|
||||
<div style="text-align: center; padding-top: 30px;" id="node-dialog-library-save-content">
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div id="node-dialog-library-save" class="hide">
|
||||
<form class="form-horizontal">
|
||||
<div class="form-row">
|
||||
<label for="node-dialog-library-save-folder" data-i18n="[append]library.folder"><i class="fa fa-folder-open"></i> </label>
|
||||
<input type="text" id="node-dialog-library-save-folder" data-i18n="[placeholder]library.folderPlaceholder">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-dialog-library-save-filename" data-i18n="[append]library.filename"><i class="fa fa-file"></i> </label>
|
||||
<input type="text" id="node-dialog-library-save-filename" data-i18n="[placeholder]library.filenamePlaceholder">
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div id="node-dialog-library-lookup" class="hide">
|
||||
<form class="form-horizontal">
|
||||
<div class="form-row">
|
||||
<ul id="node-dialog-library-breadcrumbs" class="breadcrumb">
|
||||
<li class="active"><a href="#" data-i18n="[append]library.breadcrumb"></a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<div style="vertical-align: top; display: inline-block; height: 100%; width: 30%; padding-right: 20px;">
|
||||
<div id="node-select-library" style="border: 1px solid #999; width: 100%; height: 100%; overflow:scroll;"><ul></ul></div>
|
||||
</div>
|
||||
<div style="vertical-align: top; display: inline-block;width: 65%; height: 100%;">
|
||||
<div style="height: 100%; width: 95%;" class="node-text-editor" id="node-select-library-text" ></div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<script type="text/x-red" data-template-name="subflow">
|
||||
<div class="form-row">
|
||||
<label for="node-input-name" data-i18n="[append]editor:common.label.name"><i class="fa fa-tag"></i> </label>
|
||||
<input type="text" id="node-input-name">
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-template-name="subflow-template">
|
||||
<div class="form-row">
|
||||
<i class="fa fa-tag"></i>
|
||||
<label for="subflow-input-name" data-i18n="common.label.name"></label><input type="text" id="subflow-input-name">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<i class="fa fa-folder-o"></i>
|
||||
<label for="subflow-input-category" data-i18n="editor:subflow.category"></label><select style="width: 250px;" id="subflow-input-category"></select><input style="display:none; margin-left: 10px; width:calc(100% - 250px)" type="text" id="subflow-input-custom-category">
|
||||
</div>
|
||||
<div class="form-row" style="margin-bottom: 0px;">
|
||||
<label for="subflow-input-info" data-i18n="editor:subflow.info"></label>
|
||||
<a href="https://help.github.com/articles/markdown-basics/" style="font-size: 0.8em; float: right;" data-i18n="[html]subflow.format"></a>
|
||||
</div>
|
||||
<div class="form-row node-text-editor-row">
|
||||
<div style="height: 250px;" class="node-text-editor" id="subflow-input-info-editor"></div>
|
||||
</div>
|
||||
<div class="form-row form-tips" id="subflow-dialog-user-count"></div>
|
||||
</script>
|
||||
<script src="vendor/vendor.js"></script>
|
||||
<script src="vendor/jsonata/jsonata.min.js"></script>
|
||||
<script src="vendor/ace/ace.js"></script>
|
||||
|
||||
22
jsdoc.json
22
jsdoc.json
@@ -1,22 +0,0 @@
|
||||
{
|
||||
"opts": {
|
||||
"template": "./node_modules/ink-docstrap/template",
|
||||
"destination": "./docs",
|
||||
"recurse": true
|
||||
},
|
||||
"tags": {
|
||||
"allowUnknownTags": false,
|
||||
"dictionaries": ["jsdoc"]
|
||||
},
|
||||
"source": {
|
||||
"include": [
|
||||
"./red/runtime-api"
|
||||
]
|
||||
},
|
||||
"templates": {
|
||||
"systemName": "Node-RED Runtime API",
|
||||
"theme":"yeti",
|
||||
"footer": "",
|
||||
"copyright": "Released under the Apache License v2.0"
|
||||
}
|
||||
}
|
||||
@@ -67,6 +67,7 @@
|
||||
},
|
||||
inputs:1, // set the number of inputs - only 0 or 1
|
||||
outputs:1, // set the number of outputs - 0 to n
|
||||
color: "#ddd", // set icon color
|
||||
// set the icon (held in icons dir below where you save the node)
|
||||
icon: "myicon.png", // saved in icons/myicon.png
|
||||
label: function() { // sets the default label contents
|
||||
|
||||
@@ -19,7 +19,11 @@ module.exports = function(RED) {
|
||||
if (this.tosidebar === undefined) { this.tosidebar = true; }
|
||||
this.severity = n.severity || 40;
|
||||
this.active = (n.active === null || typeof n.active === "undefined") || n.active;
|
||||
this.status({});
|
||||
if (this.tostatus) {
|
||||
this.oldStatus = {fill:"grey", shape:"ring"};
|
||||
this.status(this.oldStatus);
|
||||
}
|
||||
else { this.status({}); }
|
||||
|
||||
var node = this;
|
||||
var levels = {
|
||||
@@ -122,12 +126,12 @@ module.exports = function(RED) {
|
||||
if (state === "enable") {
|
||||
node.active = true;
|
||||
res.sendStatus(200);
|
||||
if (node.tostatus) { node.status({}); }
|
||||
if (node.tostatus) { node.status({fill:"grey", shape:"dot"}); }
|
||||
} else if (state === "disable") {
|
||||
node.active = false;
|
||||
res.sendStatus(201);
|
||||
if (node.tostatus && node.hasOwnProperty("oldStatus")) {
|
||||
node.oldStatus.shape = "ring";
|
||||
node.oldStatus.shape = "dot";
|
||||
node.status(node.oldStatus);
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -126,6 +126,7 @@
|
||||
var value = that.editor.getValue();
|
||||
RED.editor.editJavaScript({
|
||||
value: value,
|
||||
width: "Infinity",
|
||||
cursor: that.editor.getCursorPosition(),
|
||||
complete: function(v,cursor) {
|
||||
that.editor.setValue(v, -1);
|
||||
|
||||
@@ -42,7 +42,7 @@ module.exports = function(RED) {
|
||||
if (type === 'object') {
|
||||
type = Buffer.isBuffer(msg)?'Buffer':(util.isArray(msg)?'Array':'Date');
|
||||
}
|
||||
node.error(RED._("function.error.non-message-returned",{ type: type }))
|
||||
node.error(RED._("function.error.non-message-returned",{ type: type }));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -203,9 +203,9 @@ module.exports = function(RED) {
|
||||
if (util.hasOwnProperty('promisify')) {
|
||||
sandbox.setTimeout[util.promisify.custom] = function(after, value) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
sandbox.setTimeout(function(){ resolve(value) }, after);
|
||||
sandbox.setTimeout(function(){ resolve(value); }, after);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
var context = vm.createContext(sandbox);
|
||||
try {
|
||||
@@ -241,7 +241,6 @@ module.exports = function(RED) {
|
||||
|
||||
var line = 0;
|
||||
var errorMessage;
|
||||
var stack = err.stack.split(/\r?\n/);
|
||||
if (stack.length > 0) {
|
||||
while (line < stack.length && stack[line].indexOf("ReferenceError") !== 0) {
|
||||
line++;
|
||||
@@ -265,13 +264,13 @@ module.exports = function(RED) {
|
||||
});
|
||||
this.on("close", function() {
|
||||
while (node.outstandingTimers.length > 0) {
|
||||
clearTimeout(node.outstandingTimers.pop())
|
||||
clearTimeout(node.outstandingTimers.pop());
|
||||
}
|
||||
while (node.outstandingIntervals.length > 0) {
|
||||
clearInterval(node.outstandingIntervals.pop())
|
||||
clearInterval(node.outstandingIntervals.pop());
|
||||
}
|
||||
this.status({});
|
||||
})
|
||||
});
|
||||
} catch(err) {
|
||||
// eg SyntaxError - which v8 doesn't include line number information
|
||||
// so we can't do better than this
|
||||
@@ -280,4 +279,4 @@ module.exports = function(RED) {
|
||||
}
|
||||
RED.nodes.registerType("function",FunctionNode);
|
||||
RED.library.register("functions");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -77,7 +77,9 @@
|
||||
}</pre>
|
||||
<p>The resulting property will be:
|
||||
<pre>Hello Fred. Today is Monday</pre>
|
||||
<p>It is possible to use a property from the flow context or global context. Just use <code>{{flow.name}}</code> or <code>{{global.name}}</code>, or for persistable store <code>store</code> use <code>{{flow[store].name}}</code> or <code>{{flobal[store].name}}</code>.
|
||||
<p>It is possible to use a property from the flow context or global context. Just use <code>{{flow.name}}</code> or
|
||||
<code>{{global.name}}</code>, or for persistable store <code>store</code> use <code>{{flow[store].name}}</code> or
|
||||
<code>{{global[store].name}}</code>.
|
||||
<p><b>Note: </b>By default, <i>mustache</i> will escape any HTML entities in the values it substitutes.
|
||||
To prevent this, use <code>{{{triple}}}</code> braces.
|
||||
</script>
|
||||
|
||||
@@ -19,6 +19,19 @@ module.exports = function(RED) {
|
||||
var mustache = require("mustache");
|
||||
var yaml = require("js-yaml");
|
||||
|
||||
function extractTokens(tokens,set) {
|
||||
set = set || new Set();
|
||||
tokens.forEach(function(token) {
|
||||
if (token[0] !== 'text') {
|
||||
set.add(token[1]);
|
||||
if (token.length > 4) {
|
||||
extractTokens(token[4],set);
|
||||
}
|
||||
}
|
||||
});
|
||||
return set;
|
||||
}
|
||||
|
||||
function parseContext(key) {
|
||||
var match = /^(flow|global)(\[(\w+)\])?\.(.+)/.exec(key);
|
||||
if (match) {
|
||||
@@ -36,22 +49,16 @@ module.exports = function(RED) {
|
||||
* flow and global context
|
||||
*/
|
||||
|
||||
function NodeContext(msg, nodeContext, parent, escapeStrings, promises, results) {
|
||||
function NodeContext(msg, nodeContext, parent, escapeStrings, cachedContextTokens) {
|
||||
this.msgContext = new mustache.Context(msg,parent);
|
||||
this.nodeContext = nodeContext;
|
||||
this.escapeStrings = escapeStrings;
|
||||
this.promises = promises;
|
||||
this.results = results;
|
||||
this.cachedContextTokens = cachedContextTokens;
|
||||
}
|
||||
|
||||
NodeContext.prototype = new mustache.Context();
|
||||
|
||||
NodeContext.prototype.lookup = function (name) {
|
||||
var results = this.results;
|
||||
if (results) {
|
||||
var val = results.shift();
|
||||
return val;
|
||||
}
|
||||
// try message first:
|
||||
try {
|
||||
var value = this.msgContext.lookup(name);
|
||||
@@ -64,7 +71,6 @@ module.exports = function(RED) {
|
||||
value = value.replace(/\f/g, "\\f");
|
||||
value = value.replace(/[\b]/g, "\\b");
|
||||
}
|
||||
this.promises.push(Promise.resolve(value));
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -76,28 +82,10 @@ module.exports = function(RED) {
|
||||
var field = context.field;
|
||||
var target = this.nodeContext[type];
|
||||
if (target) {
|
||||
var promise = new Promise((resolve, reject) => {
|
||||
var callback = (err, val) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(val);
|
||||
}
|
||||
};
|
||||
target.get(field, store, callback);
|
||||
});
|
||||
this.promises.push(promise);
|
||||
return '';
|
||||
}
|
||||
else {
|
||||
this.promises.push(Promise.resolve(''));
|
||||
return '';
|
||||
return this.cachedContextTokens[name];
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.promises.push(Promise.resolve(''));
|
||||
return '';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
catch(err) {
|
||||
throw err;
|
||||
@@ -105,7 +93,7 @@ module.exports = function(RED) {
|
||||
}
|
||||
|
||||
NodeContext.prototype.push = function push (view) {
|
||||
return new NodeContext(view, this.nodeContext, this.msgContext, undefined, this.promises, this.results);
|
||||
return new NodeContext(view, this.nodeContext, this.msgContext, undefined, this.cachedContextTokens);
|
||||
};
|
||||
|
||||
function TemplateNode(n) {
|
||||
@@ -118,33 +106,36 @@ module.exports = function(RED) {
|
||||
this.outputFormat = n.output || "str";
|
||||
|
||||
var node = this;
|
||||
node.on("input", function(msg) {
|
||||
function output(value) {
|
||||
/* istanbul ignore else */
|
||||
if (node.outputFormat === "json") {
|
||||
value = JSON.parse(value);
|
||||
}
|
||||
/* istanbul ignore else */
|
||||
if (node.outputFormat === "yaml") {
|
||||
value = yaml.load(value);
|
||||
}
|
||||
|
||||
if (node.fieldType === 'msg') {
|
||||
RED.util.setMessageProperty(msg, node.field, value);
|
||||
node.send(msg);
|
||||
} else if ((node.fieldType === 'flow') ||
|
||||
(node.fieldType === 'global')) {
|
||||
var context = RED.util.parseContextStore(node.field);
|
||||
var target = node.context()[node.fieldType];
|
||||
target.set(context.key, value, context.store, function (err) {
|
||||
if (err) {
|
||||
node.error(err, msg);
|
||||
} else {
|
||||
node.send(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
function output(msg,value) {
|
||||
/* istanbul ignore else */
|
||||
if (node.outputFormat === "json") {
|
||||
value = JSON.parse(value);
|
||||
}
|
||||
/* istanbul ignore else */
|
||||
if (node.outputFormat === "yaml") {
|
||||
value = yaml.load(value);
|
||||
}
|
||||
|
||||
if (node.fieldType === 'msg') {
|
||||
RED.util.setMessageProperty(msg, node.field, value);
|
||||
node.send(msg);
|
||||
} else if ((node.fieldType === 'flow') ||
|
||||
(node.fieldType === 'global')) {
|
||||
var context = RED.util.parseContextStore(node.field);
|
||||
var target = node.context()[node.fieldType];
|
||||
target.set(context.key, value, context.store, function (err) {
|
||||
if (err) {
|
||||
node.error(err, msg);
|
||||
} else {
|
||||
node.send(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
node.on("input", function(msg) {
|
||||
|
||||
try {
|
||||
/***
|
||||
* Allow template contents to be defined externally
|
||||
@@ -160,19 +151,44 @@ module.exports = function(RED) {
|
||||
if (node.syntax === "mustache") {
|
||||
var is_json = (node.outputFormat === "json");
|
||||
var promises = [];
|
||||
mustache.render(template, new NodeContext(msg, node.context(), null, is_json, promises, null));
|
||||
Promise.all(promises).then(function (values) {
|
||||
var value = mustache.render(template, new NodeContext(msg, node.context(), null, is_json, null, values));
|
||||
output(value);
|
||||
var tokens = extractTokens(mustache.parse(template));
|
||||
var resolvedTokens = {};
|
||||
tokens.forEach(function(name) {
|
||||
var context = parseContext(name);
|
||||
if (context) {
|
||||
var type = context.type;
|
||||
var store = context.store;
|
||||
var field = context.field;
|
||||
var target = node.context()[type];
|
||||
if (target) {
|
||||
var promise = new Promise((resolve, reject) => {
|
||||
target.get(field, store, (err, val) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolvedTokens[name] = val;
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
promises.push(promise);
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Promise.all(promises).then(function() {
|
||||
var value = mustache.render(template, new NodeContext(msg, node.context(), null, is_json, resolvedTokens));
|
||||
output(msg, value);
|
||||
}).catch(function (err) {
|
||||
node.error(err.message);
|
||||
node.error(err.message,msg);
|
||||
});
|
||||
} else {
|
||||
output(template);
|
||||
output(msg, template);
|
||||
}
|
||||
}
|
||||
catch(err) {
|
||||
node.error(err.message);
|
||||
node.error(err.message, msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
$(function() {
|
||||
RED.i18n.init({},function() {
|
||||
RED.i18n.init(function() {
|
||||
var options = {
|
||||
messageMouseEnter: function(sourceId) {
|
||||
window.opener.postMessage({event:"mouseEnter",id:sourceId},'*');
|
||||
|
||||
@@ -24,8 +24,12 @@ module.exports = function(RED) {
|
||||
try {
|
||||
fs.statSync("/usr/lib/python2.7/dist-packages/RPi/GPIO"); // test on Hypriot
|
||||
} catch(err) {
|
||||
RED.log.warn("rpi-gpio : "+RED._("rpi-gpio.errors.libnotfound"));
|
||||
allOK = false;
|
||||
try {
|
||||
fs.statSync("/usr/local/lib/python2.7/dist-packages/RPi/GPIO"); // installed with pip
|
||||
} catch(err) {
|
||||
RED.log.warn("rpi-gpio : "+RED._("rpi-gpio.errors.libnotfound"));
|
||||
allOK = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,12 @@ import os
|
||||
import subprocess
|
||||
from time import sleep
|
||||
|
||||
bounce = 25;
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
bounce = 25
|
||||
|
||||
if len(sys.argv) > 2:
|
||||
cmd = sys.argv[1].lower()
|
||||
@@ -198,7 +203,7 @@ if len(sys.argv) > 2:
|
||||
elif cmd == "kbd": # catch keyboard button events
|
||||
try:
|
||||
while not os.path.isdir("/dev/input/by-path"):
|
||||
time.sleep(10)
|
||||
sleep(10)
|
||||
infile = subprocess.check_output("ls /dev/input/by-path/ | grep -m 1 'kbd'", shell=True).strip()
|
||||
infile_path = "/dev/input/by-path/" + infile
|
||||
EVENT_SIZE = struct.calcsize('llHHI')
|
||||
|
||||
@@ -19,11 +19,26 @@ module.exports = function(RED) {
|
||||
var mqtt = require("mqtt");
|
||||
var util = require("util");
|
||||
var isUtf8 = require('is-utf8');
|
||||
var HttpsProxyAgent = require('https-proxy-agent');
|
||||
var url = require('url');
|
||||
|
||||
function matchTopic(ts,t) {
|
||||
if (ts == "#") {
|
||||
return true;
|
||||
}
|
||||
/* The following allows shared subscriptions (as in MQTT v5)
|
||||
http://docs.oasis-open.org/mqtt/mqtt/v5.0/cs02/mqtt-v5.0-cs02.html#_Toc514345522
|
||||
|
||||
4.8.2 describes shares like:
|
||||
$share/{ShareName}/{filter}
|
||||
$share is a literal string that marks the Topic Filter as being a Shared Subscription Topic Filter.
|
||||
{ShareName} is a character string that does not include "/", "+" or "#"
|
||||
{filter} The remainder of the string has the same syntax and semantics as a Topic Filter in a non-shared subscription. Refer to section 4.7.
|
||||
*/
|
||||
else if(ts.startsWith("$share")){
|
||||
ts = ts.replace(/^\$share\/[^#+/]+\/(.*)/g,"$1");
|
||||
|
||||
}
|
||||
var re = new RegExp("^"+ts.replace(/([\[\]\?\(\)\\\\$\^\*\.|])/g,"\\$1").replace(/\+/g,"[^/]+").replace(/\/#$/,"(\/.*)?")+"$");
|
||||
return re.test(t);
|
||||
}
|
||||
@@ -96,12 +111,29 @@ module.exports = function(RED) {
|
||||
if (typeof this.cleansession === 'undefined') {
|
||||
this.cleansession = true;
|
||||
}
|
||||
var prox;
|
||||
if (process.env.http_proxy != null) { prox = process.env.http_proxy; }
|
||||
if (process.env.HTTP_PROXY != null) { prox = process.env.HTTP_PROXY; }
|
||||
|
||||
// Create the URL to pass in to the MQTT.js library
|
||||
if (this.brokerurl === "") {
|
||||
// if the broker may be ws:// or wss:// or even tcp://
|
||||
if (this.broker.indexOf("://") > -1) {
|
||||
this.brokerurl = this.broker;
|
||||
// Only for ws or wss, check if proxy env var for additional configuration
|
||||
if (this.brokerurl.indexOf("wss://") > -1 || this.brokerurl.indexOf("ws://") > -1 )
|
||||
// check if proxy is set in env
|
||||
if (prox) {
|
||||
var parsedUrl = url.parse(this.brokerurl);
|
||||
var proxyOpts = url.parse(prox);
|
||||
// true for wss
|
||||
proxyOpts.secureEndpoint = parsedUrl.protocol ? parsedUrl.protocol === 'wss:' : true;
|
||||
// Set Agent for wsOption in MQTT
|
||||
var agent = new HttpsProxyAgent(proxyOpts);
|
||||
this.options.wsOptions = {
|
||||
agent: agent
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// construct the std mqtt:// url
|
||||
if (this.usetls) {
|
||||
@@ -435,4 +467,4 @@ module.exports = function(RED) {
|
||||
}
|
||||
}
|
||||
RED.nodes.registerType("mqtt out",MQTTOutNode);
|
||||
};
|
||||
};
|
||||
@@ -33,9 +33,7 @@ module.exports = function(RED) {
|
||||
*/
|
||||
const enqueue = (queue, item) => {
|
||||
// drop msgs from front of queue if size is going to be exceeded
|
||||
if (queue.size() === msgQueueSize) {
|
||||
queue.shift();
|
||||
}
|
||||
if (queue.size() === msgQueueSize) { queue.shift(); }
|
||||
queue.push(item);
|
||||
return queue;
|
||||
};
|
||||
@@ -467,6 +465,7 @@ module.exports = function(RED) {
|
||||
connecting: false
|
||||
};
|
||||
enqueue(clients[connection_id].msgQueue, msg);
|
||||
clients[connection_id].lastMsg = msg;
|
||||
|
||||
if (!clients[connection_id].connecting && !clients[connection_id].connected) {
|
||||
var buf;
|
||||
@@ -507,8 +506,7 @@ module.exports = function(RED) {
|
||||
clients[connection_id].client.on('data', function(data) {
|
||||
if (node.out === "sit") { // if we are staying connected just send the buffer
|
||||
if (clients[connection_id]) {
|
||||
let msg = dequeue(clients[connection_id].msgQueue) || {};
|
||||
clients[connection_id].msgQueue.unshift(msg);
|
||||
const msg = clients[connection_id].lastMsg || {};
|
||||
msg.payload = data;
|
||||
node.send(RED.util.cloneMessage(msg));
|
||||
}
|
||||
@@ -530,8 +528,7 @@ module.exports = function(RED) {
|
||||
clients[connection_id].timeout = setTimeout(function () {
|
||||
if (clients[connection_id]) {
|
||||
clients[connection_id].timeout = null;
|
||||
let msg = dequeue(clients[connection_id].msgQueue) || {};
|
||||
clients[connection_id].msgQueue.unshift(msg);
|
||||
const msg = clients[connection_id].lastMsg || {};
|
||||
msg.payload = Buffer.alloc(i+1);
|
||||
buf.copy(msg.payload,0,0,i+1);
|
||||
node.send(msg);
|
||||
@@ -553,8 +550,7 @@ module.exports = function(RED) {
|
||||
i += 1;
|
||||
if ( i >= node.splitc) {
|
||||
if (clients[connection_id]) {
|
||||
let msg = dequeue(clients[connection_id].msgQueue) || {};
|
||||
clients[connection_id].msgQueue.unshift(msg);
|
||||
const msg = clients[connection_id].lastMsg || {};
|
||||
msg.payload = Buffer.alloc(i);
|
||||
buf.copy(msg.payload,0,0,i);
|
||||
node.send(msg);
|
||||
@@ -573,8 +569,7 @@ module.exports = function(RED) {
|
||||
i += 1;
|
||||
if (data[j] == node.splitc) {
|
||||
if (clients[connection_id]) {
|
||||
let msg = dequeue(clients[connection_id].msgQueue) || {};
|
||||
clients[connection_id].msgQueue.unshift(msg);
|
||||
const msg = clients[connection_id].lastMsg || {};
|
||||
msg.payload = Buffer.alloc(i);
|
||||
buf.copy(msg.payload,0,0,i);
|
||||
node.send(msg);
|
||||
@@ -649,7 +644,7 @@ module.exports = function(RED) {
|
||||
}
|
||||
else if (!clients[connection_id].connecting && clients[connection_id].connected) {
|
||||
if (clients[connection_id] && clients[connection_id].client) {
|
||||
clients[connection_id].client.write(dequeue(clients[connection_id].msgQueue));
|
||||
clients[connection_id].client.write(dequeue(clients[connection_id].msgQueue).payload);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -92,31 +92,80 @@ module.exports = function(RED) {
|
||||
}
|
||||
|
||||
function getProperty(node,msg) {
|
||||
return new Promise((resolve,reject) => {
|
||||
if (node.useAsyncRules) {
|
||||
return new Promise((resolve,reject) => {
|
||||
if (node.propertyType === 'jsonata') {
|
||||
RED.util.evaluateJSONataExpression(node.property,msg,(err,value) => {
|
||||
if (err) {
|
||||
reject(RED._("switch.errors.invalid-expr",{error:err.message}));
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
RED.util.evaluateNodeProperty(node.property,node.propertyType,node,msg,(err,value) => {
|
||||
if (err) {
|
||||
resolve(undefined);
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (node.propertyType === 'jsonata') {
|
||||
RED.util.evaluateJSONataExpression(node.property,msg,(err,value) => {
|
||||
if (err) {
|
||||
reject(RED._("switch.errors.invalid-expr",{error:err.message}));
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
try {
|
||||
return RED.util.evaluateJSONataExpression(node.property,msg);
|
||||
} catch(err) {
|
||||
throw new Error(RED._("switch.errors.invalid-expr",{error:err.message}))
|
||||
}
|
||||
} else {
|
||||
RED.util.evaluateNodeProperty(node.property,node.propertyType,node,msg,(err,value) => {
|
||||
if (err) {
|
||||
resolve(undefined);
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
try {
|
||||
return RED.util.evaluateNodeProperty(node.property,node.propertyType,node,msg);
|
||||
} catch(err) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getV1(node,msg,rule,hasParts) {
|
||||
return new Promise( (resolve,reject) => {
|
||||
if (node.useAsyncRules) {
|
||||
return new Promise( (resolve,reject) => {
|
||||
if (rule.vt === 'prev') {
|
||||
resolve(node.previousValue);
|
||||
} else if (rule.vt === 'jsonata') {
|
||||
var exp = rule.v;
|
||||
if (rule.t === 'jsonata_exp') {
|
||||
if (hasParts) {
|
||||
exp.assign("I", msg.parts.index);
|
||||
exp.assign("N", msg.parts.count);
|
||||
}
|
||||
}
|
||||
RED.util.evaluateJSONataExpression(exp,msg,(err,value) => {
|
||||
if (err) {
|
||||
reject(RED._("switch.errors.invalid-expr",{error:err.message}));
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
} else if (rule.vt === 'json') {
|
||||
resolve("json"); // TODO: ?! invalid case
|
||||
} else if (rule.vt === 'null') {
|
||||
resolve("null");
|
||||
} else {
|
||||
RED.util.evaluateNodeProperty(rule.v,rule.vt,node,msg, function(err,value) {
|
||||
if (err) {
|
||||
resolve(undefined);
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (rule.vt === 'prev') {
|
||||
resolve(node.previousValue);
|
||||
return node.previousValue;
|
||||
} else if (rule.vt === 'jsonata') {
|
||||
var exp = rule.v;
|
||||
if (rule.t === 'jsonata_exp') {
|
||||
@@ -125,83 +174,120 @@ module.exports = function(RED) {
|
||||
exp.assign("N", msg.parts.count);
|
||||
}
|
||||
}
|
||||
RED.util.evaluateJSONataExpression(exp,msg,(err,value) => {
|
||||
if (err) {
|
||||
reject(RED._("switch.errors.invalid-expr",{error:err.message}));
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
try {
|
||||
return RED.util.evaluateJSONataExpression(exp,msg);
|
||||
} catch(err) {
|
||||
throw new Error(RED._("switch.errors.invalid-expr",{error:err.message}))
|
||||
}
|
||||
} else if (rule.vt === 'json') {
|
||||
resolve("json");
|
||||
return "json"; // TODO: ?! invalid case
|
||||
} else if (rule.vt === 'null') {
|
||||
resolve("null");
|
||||
return "null";
|
||||
} else {
|
||||
RED.util.evaluateNodeProperty(rule.v,rule.vt,node,msg, function(err,value) {
|
||||
if (err) {
|
||||
resolve(undefined);
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
try {
|
||||
return RED.util.evaluateNodeProperty(rule.v,rule.vt,node,msg);
|
||||
} catch(err) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getV2(node,msg,rule) {
|
||||
return new Promise((resolve,reject) => {
|
||||
if (node.useAsyncRules) {
|
||||
return new Promise((resolve,reject) => {
|
||||
var v2 = rule.v2;
|
||||
if (rule.v2t === 'prev') {
|
||||
resolve(node.previousValue);
|
||||
} else if (rule.v2t === 'jsonata') {
|
||||
RED.util.evaluateJSONataExpression(rule.v2,msg,(err,value) => {
|
||||
if (err) {
|
||||
reject(RED._("switch.errors.invalid-expr",{error:err.message}));
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
} else if (typeof v2 !== 'undefined') {
|
||||
RED.util.evaluateNodeProperty(rule.v2,rule.v2t,node,msg, function(err,value) {
|
||||
if (err) {
|
||||
resolve(undefined);
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
resolve(v2);
|
||||
}
|
||||
})
|
||||
} else {
|
||||
var v2 = rule.v2;
|
||||
if (rule.v2t === 'prev') {
|
||||
resolve(node.previousValue);
|
||||
return node.previousValue;
|
||||
} else if (rule.v2t === 'jsonata') {
|
||||
RED.util.evaluateJSONataExpression(rule.v2,msg,(err,value) => {
|
||||
if (err) {
|
||||
reject(RED._("switch.errors.invalid-expr",{error:err.message}));
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
try {
|
||||
return RED.util.evaluateJSONataExpression(rule.v2,msg);
|
||||
} catch(err) {
|
||||
throw new Error(RED._("switch.errors.invalid-expr",{error:err.message}))
|
||||
}
|
||||
} else if (typeof v2 !== 'undefined') {
|
||||
RED.util.evaluateNodeProperty(rule.v2,rule.v2t,node,msg, function(err,value) {
|
||||
if (err) {
|
||||
resolve(undefined);
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
try {
|
||||
return RED.util.evaluateNodeProperty(rule.v2,rule.v2t,node,msg);
|
||||
} catch(err) {
|
||||
return undefined;
|
||||
}
|
||||
} else {
|
||||
resolve(v2);
|
||||
return v2;
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function applyRule(node, msg, property, state) {
|
||||
return new Promise((resolve,reject) => {
|
||||
if (node.useAsyncRules) {
|
||||
return new Promise((resolve,reject) => {
|
||||
|
||||
var rule = node.rules[state.currentRule];
|
||||
var v1,v2;
|
||||
var rule = node.rules[state.currentRule];
|
||||
var v1,v2;
|
||||
|
||||
getV1(node,msg,rule,state.hasParts).then(value => {
|
||||
v1 = value;
|
||||
}).then(()=>getV2(node,msg,rule)).then(value => {
|
||||
v2 = value;
|
||||
}).then(() => {
|
||||
if (rule.t == "else") {
|
||||
property = state.elseflag;
|
||||
state.elseflag = true;
|
||||
}
|
||||
if (operators[rule.t](property,v1,v2,rule.case,msg.parts)) {
|
||||
state.onward.push(msg);
|
||||
state.elseflag = false;
|
||||
if (node.checkall == "false") {
|
||||
return resolve(false);
|
||||
getV1(node,msg,rule,state.hasParts).then(value => {
|
||||
v1 = value;
|
||||
}).then(()=>getV2(node,msg,rule)).then(value => {
|
||||
v2 = value;
|
||||
}).then(() => {
|
||||
if (rule.t == "else") {
|
||||
property = state.elseflag;
|
||||
state.elseflag = true;
|
||||
}
|
||||
} else {
|
||||
state.onward.push(null);
|
||||
if (operators[rule.t](property,v1,v2,rule.case,msg.parts)) {
|
||||
state.onward.push(msg);
|
||||
state.elseflag = false;
|
||||
if (node.checkall == "false") {
|
||||
return resolve(false);
|
||||
}
|
||||
} else {
|
||||
state.onward.push(null);
|
||||
}
|
||||
resolve(state.currentRule < node.rules.length - 1);
|
||||
});
|
||||
})
|
||||
} else {
|
||||
var rule = node.rules[state.currentRule];
|
||||
var v1 = getV1(node,msg,rule,state.hasParts);
|
||||
var v2 = getV2(node,msg,rule);
|
||||
if (rule.t == "else") {
|
||||
property = state.elseflag;
|
||||
state.elseflag = true;
|
||||
}
|
||||
if (operators[rule.t](property,v1,v2,rule.case,msg.parts)) {
|
||||
state.onward.push(msg);
|
||||
state.elseflag = false;
|
||||
if (node.checkall == "false") {
|
||||
return false;
|
||||
}
|
||||
resolve(state.currentRule < node.rules.length - 1);
|
||||
});
|
||||
})
|
||||
} else {
|
||||
state.onward.push(null);
|
||||
}
|
||||
return state.currentRule < node.rules.length - 1
|
||||
}
|
||||
}
|
||||
|
||||
function applyRules(node, msg, property,state) {
|
||||
@@ -215,7 +301,18 @@ module.exports = function(RED) {
|
||||
msg.parts.hasOwnProperty("index")
|
||||
}
|
||||
}
|
||||
return applyRule(node,msg,property,state).then(hasMore => {
|
||||
if (node.useAsyncRules) {
|
||||
return applyRule(node,msg,property,state).then(hasMore => {
|
||||
if (hasMore) {
|
||||
state.currentRule++;
|
||||
return applyRules(node,msg,property,state);
|
||||
} else {
|
||||
node.previousValue = property;
|
||||
return state.onward;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
var hasMore = applyRule(node,msg,property,state);
|
||||
if (hasMore) {
|
||||
state.currentRule++;
|
||||
return applyRules(node,msg,property,state);
|
||||
@@ -223,7 +320,7 @@ module.exports = function(RED) {
|
||||
node.previousValue = property;
|
||||
return state.onward;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -248,6 +345,14 @@ module.exports = function(RED) {
|
||||
var valid = true;
|
||||
var repair = n.repair;
|
||||
var needsCount = repair;
|
||||
this.useAsyncRules = (
|
||||
this.propertyType === 'flow' ||
|
||||
this.propertyType === 'global' || (
|
||||
this.propertyType === 'jsonata' &&
|
||||
/\$(flow|global)Context/.test(this.property)
|
||||
)
|
||||
);
|
||||
|
||||
for (var i=0; i<this.rules.length; i+=1) {
|
||||
var rule = this.rules[i];
|
||||
needsCount = needsCount || ((rule.t === "tail") || (rule.t === "jsonata_exp"));
|
||||
@@ -258,6 +363,13 @@ module.exports = function(RED) {
|
||||
rule.vt = 'str';
|
||||
}
|
||||
}
|
||||
this.useAsyncRules = this.useAsyncRules || (
|
||||
rule.vt === 'flow' ||
|
||||
rule.vt === 'global' || (
|
||||
rule.vt === 'jsonata' &&
|
||||
/\$(flow|global)Context/.test(rule.v)
|
||||
)
|
||||
);
|
||||
if (rule.vt === 'num') {
|
||||
if (!isNaN(Number(rule.v))) {
|
||||
rule.v = Number(rule.v);
|
||||
@@ -270,6 +382,9 @@ module.exports = function(RED) {
|
||||
valid = false;
|
||||
}
|
||||
}
|
||||
if (rule.vt === 'flow' || rule.vt === 'global' || rule.vt === 'jsonata') {
|
||||
this.useAsyncRules = true;
|
||||
}
|
||||
if (typeof rule.v2 !== 'undefined') {
|
||||
if (!rule.v2t) {
|
||||
if (!isNaN(Number(rule.v2))) {
|
||||
@@ -278,6 +393,13 @@ module.exports = function(RED) {
|
||||
rule.v2t = 'str';
|
||||
}
|
||||
}
|
||||
this.useAsyncRules = this.useAsyncRules || (
|
||||
rule.v2t === 'flow' ||
|
||||
rule.v2t === 'global' || (
|
||||
rule.v2t === 'jsonata' &&
|
||||
/\$(flow|global)Context/.test(rule.v2)
|
||||
)
|
||||
);
|
||||
if (rule.v2t === 'num') {
|
||||
rule.v2 = Number(rule.v2);
|
||||
} else if (rule.v2t === 'jsonata') {
|
||||
@@ -290,7 +412,6 @@ module.exports = function(RED) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!valid) {
|
||||
return;
|
||||
}
|
||||
@@ -420,18 +541,32 @@ module.exports = function(RED) {
|
||||
if (needsCount && checkParts && hasParts) {
|
||||
return addMessageToPending(msg);
|
||||
}
|
||||
return getProperty(node,msg)
|
||||
.then(property => applyRules(node,msg,property))
|
||||
.then(onward => {
|
||||
if (!repair || !hasParts) {
|
||||
node.send(onward);
|
||||
}
|
||||
else {
|
||||
sendGroupMessages(onward, msg);
|
||||
}
|
||||
}).catch(err => {
|
||||
node.warn(err);
|
||||
});
|
||||
if (node.useAsyncRules) {
|
||||
return getProperty(node,msg)
|
||||
.then(property => applyRules(node,msg,property))
|
||||
.then(onward => {
|
||||
if (!repair || !hasParts) {
|
||||
node.send(onward);
|
||||
}
|
||||
else {
|
||||
sendGroupMessages(onward, msg);
|
||||
}
|
||||
}).catch(err => {
|
||||
node.warn(err);
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
var property = getProperty(node,msg);
|
||||
var onward = applyRules(node,msg,property);
|
||||
if (!repair || !hasParts) {
|
||||
node.send(onward);
|
||||
} else {
|
||||
sendGroupMessages(onward, msg);
|
||||
}
|
||||
} catch(err) {
|
||||
node.warn(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function clearPending() {
|
||||
@@ -473,7 +608,11 @@ module.exports = function(RED) {
|
||||
}
|
||||
|
||||
this.on('input', function(msg) {
|
||||
processMessageQueue(msg);
|
||||
if (node.useAsyncRules) {
|
||||
processMessageQueue(msg);
|
||||
} else {
|
||||
processMessage(msg,true);
|
||||
}
|
||||
});
|
||||
|
||||
this.on('close', function() {
|
||||
|
||||
@@ -283,6 +283,9 @@ module.exports = function(RED) {
|
||||
}
|
||||
}
|
||||
function applyRules(msg, currentRule) {
|
||||
if (currentRule >= node.rules.length) {
|
||||
return Promise.resolve(msg);
|
||||
}
|
||||
var r = node.rules[currentRule];
|
||||
var rulePromise;
|
||||
if (r.t === "move") {
|
||||
|
||||
@@ -295,7 +295,8 @@
|
||||
For object outputs, once this count has been reached, the node can be configured to send a message for each subsequent message
|
||||
received.</p>
|
||||
<p>A <i>timeout</i> can be set to trigger sending the new message using whatever has been received so far.</p>
|
||||
<p>If a message is received with the <b>msg.complete</b> property set, the output message is sent.</p>
|
||||
<p>If a message is received with the <b>msg.complete</b> property set, the output message is finalised and sent.
|
||||
This resets any part counts.</p>
|
||||
|
||||
<h4>Reduce Sequence mode</h4>
|
||||
<p>When configured to join in reduce mode, an expression is applied to each
|
||||
@@ -439,10 +440,7 @@
|
||||
$("#node-input-joiner").typedInput({
|
||||
default: 'str',
|
||||
typeField: $("#node-input-joinerType"),
|
||||
types:[
|
||||
'str',
|
||||
'bin'
|
||||
]
|
||||
types:['str', 'bin']
|
||||
});
|
||||
|
||||
$("#node-input-property").typedInput({
|
||||
@@ -451,7 +449,7 @@
|
||||
});
|
||||
|
||||
$("#node-input-key").typedInput({
|
||||
types:['msg', {value:"merge", label:"", hasValue:false}]
|
||||
types:['msg']
|
||||
});
|
||||
|
||||
$("#node-input-build").change();
|
||||
|
||||
@@ -586,7 +586,10 @@ module.exports = function(RED) {
|
||||
}
|
||||
else {
|
||||
if (msg.hasOwnProperty('complete')) {
|
||||
completeSend(partId);
|
||||
if (inflight[partId]) {
|
||||
inflight[partId].msg.complete = msg.complete;
|
||||
completeSend(partId);
|
||||
}
|
||||
}
|
||||
else {
|
||||
node.warn("Message missing key property 'msg."+node.key+"' - cannot add to object")
|
||||
@@ -594,6 +597,7 @@ module.exports = function(RED) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!inflight.hasOwnProperty(partId)) {
|
||||
if (payloadType === 'object' || payloadType === 'merged') {
|
||||
inflight[partId] = {
|
||||
@@ -604,19 +608,6 @@ module.exports = function(RED) {
|
||||
msg:RED.util.cloneMessage(msg)
|
||||
};
|
||||
}
|
||||
else if (node.accumulate === true) {
|
||||
if (msg.hasOwnProperty("reset")) { delete inflight[partId]; }
|
||||
inflight[partId] = inflight[partId] || {
|
||||
currentCount:0,
|
||||
payload:{},
|
||||
targetCount:targetCount,
|
||||
type:payloadType,
|
||||
msg:RED.util.cloneMessage(msg)
|
||||
}
|
||||
if (payloadType === 'string' || payloadType === 'array' || payloadType === 'buffer') {
|
||||
inflight[partId].payload = [];
|
||||
}
|
||||
}
|
||||
else {
|
||||
inflight[partId] = {
|
||||
currentCount:0,
|
||||
|
||||
@@ -196,9 +196,10 @@ module.exports = function(RED) {
|
||||
}).catch(err => {
|
||||
node.error(err,msg);
|
||||
});
|
||||
return;
|
||||
}
|
||||
var parts = msg.parts;
|
||||
if (!parts.hasOwnProperty("id") || !parts.hasOwnProperty("index")) {
|
||||
if (!parts || !parts.hasOwnProperty("id") || !parts.hasOwnProperty("index")) {
|
||||
return;
|
||||
}
|
||||
var gid = parts.id;
|
||||
@@ -242,7 +243,8 @@ module.exports = function(RED) {
|
||||
delete pending[key];
|
||||
}
|
||||
}
|
||||
pending_count = 0; })
|
||||
pending_count = 0;
|
||||
});
|
||||
}
|
||||
|
||||
RED.nodes.registerType("sort", SortNode);
|
||||
|
||||
@@ -100,9 +100,9 @@
|
||||
defaults: {
|
||||
name: {value:""},
|
||||
mode: {value:"count"},
|
||||
count: {value:10},
|
||||
overlap: {value:0},
|
||||
interval: {value:10},
|
||||
count: {value:10,validate:function(v) { return RED.validators.number(v) && (v >= 1); }},
|
||||
overlap: {value:0,validate:function(v) { return RED.validators.number(v) && (v >= 0); }},
|
||||
interval: {value:10,validate:function(v) { return RED.validators.number(v) && (v >= 1); }},
|
||||
allowEmptySequence: {value:false},
|
||||
topics: {value:[{topic:""}]}
|
||||
},
|
||||
@@ -149,12 +149,9 @@
|
||||
removable: true
|
||||
});
|
||||
|
||||
$("#node-input-count").spinner({
|
||||
});
|
||||
$("#node-input-overlap").spinner({
|
||||
});
|
||||
$("#node-input-interval").spinner({
|
||||
});
|
||||
$("#node-input-count").spinner({min:1});
|
||||
$("#node-input-overlap").spinner({min:0});
|
||||
$("#node-input-interval").spinner({min:1});
|
||||
$("#node-input-mode").change(function(e) {
|
||||
var val = $(this).val();
|
||||
$(".node-row-msg-count").toggle(val==="count");
|
||||
|
||||
@@ -19,6 +19,7 @@ module.exports = function(RED) {
|
||||
const Ajv = require('ajv');
|
||||
const ajv = new Ajv({allErrors: true, schemaId: 'auto'});
|
||||
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
|
||||
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
|
||||
|
||||
function JSONNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
@@ -29,6 +30,7 @@ module.exports = function(RED) {
|
||||
this.compiledSchema = null;
|
||||
|
||||
var node = this;
|
||||
|
||||
this.on("input", function(msg) {
|
||||
var validate = false;
|
||||
if (msg.schema) {
|
||||
@@ -65,7 +67,17 @@ module.exports = function(RED) {
|
||||
}
|
||||
catch(e) { node.error(e.message,msg); }
|
||||
} else {
|
||||
node.send(msg);
|
||||
// If node.action is str and value is str
|
||||
if (validate) {
|
||||
if (this.compiledSchema(JSON.parse(msg[node.property]))) {
|
||||
node.send(msg);
|
||||
} else {
|
||||
msg.schemaError = this.compiledSchema.errors;
|
||||
node.error(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`, msg);
|
||||
}
|
||||
} else {
|
||||
node.send(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (typeof value === "object") {
|
||||
@@ -84,13 +96,22 @@ module.exports = function(RED) {
|
||||
RED.util.setMessageProperty(msg,node.property,JSON.stringify(value,null,node.indent));
|
||||
node.send(msg);
|
||||
}
|
||||
|
||||
}
|
||||
catch(e) { node.error(RED._("json.errors.dropped-error")); }
|
||||
}
|
||||
else { node.warn(RED._("json.errors.dropped-object")); }
|
||||
} else {
|
||||
node.send(msg);
|
||||
// If node.action is obj and value is object
|
||||
if (validate) {
|
||||
if (this.compiledSchema(value)) {
|
||||
node.send(msg);
|
||||
} else {
|
||||
msg.schemaError = this.compiledSchema.errors;
|
||||
node.error(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`, msg);
|
||||
}
|
||||
} else {
|
||||
node.send(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
else { node.warn(RED._("json.errors.dropped")); }
|
||||
|
||||
@@ -28,9 +28,11 @@ module.exports = function(RED) {
|
||||
this.createDir = n.createDir || false;
|
||||
var node = this;
|
||||
node.wstream = null;
|
||||
node.data = [];
|
||||
node.msgQueue = [];
|
||||
node.closing = false;
|
||||
node.closeCallback = null;
|
||||
|
||||
this.on("input",function(msg) {
|
||||
function processMsg(msg, done) {
|
||||
var filename = node.filename || msg.filename || "";
|
||||
if ((!node.filename) && (!node.tout)) {
|
||||
node.tout = setTimeout(function() {
|
||||
@@ -41,6 +43,7 @@ module.exports = function(RED) {
|
||||
}
|
||||
if (filename === "") {
|
||||
node.warn(RED._("file.errors.nofilename"));
|
||||
done();
|
||||
} else if (node.overwriteFile === "delete") {
|
||||
fs.unlink(filename, function (err) {
|
||||
if (err) {
|
||||
@@ -51,6 +54,7 @@ module.exports = function(RED) {
|
||||
}
|
||||
node.send(msg);
|
||||
}
|
||||
done();
|
||||
});
|
||||
} else if (msg.hasOwnProperty("payload") && (typeof msg.payload !== "undefined")) {
|
||||
var dir = path.dirname(filename);
|
||||
@@ -59,6 +63,7 @@ module.exports = function(RED) {
|
||||
fs.ensureDirSync(dir);
|
||||
} catch(err) {
|
||||
node.error(RED._("file.errors.createfail",{error:err.toString()}),msg);
|
||||
done();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -70,85 +75,142 @@ module.exports = function(RED) {
|
||||
if (typeof data === "boolean") { data = data.toString(); }
|
||||
if (typeof data === "number") { data = data.toString(); }
|
||||
if ((node.appendNewline) && (!Buffer.isBuffer(data))) { data += os.EOL; }
|
||||
node.data.push({msg:msg,data:Buffer.from(data)});
|
||||
|
||||
while (node.data.length > 0) {
|
||||
if (node.overwriteFile === "true") {
|
||||
(function(packet) {
|
||||
node.wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'w', autoClose:true });
|
||||
node.wstream.on("error", function(err) {
|
||||
node.error(RED._("file.errors.writefail",{error:err.toString()}),msg);
|
||||
});
|
||||
node.wstream.on("open", function() {
|
||||
node.wstream.end(packet.data, function() {
|
||||
node.send(packet.msg);
|
||||
});
|
||||
})
|
||||
})(node.data.shift());
|
||||
}
|
||||
else {
|
||||
// Append mode
|
||||
var recreateStream = !node.wstream || !node.filename;
|
||||
if (node.wstream && node.wstreamIno) {
|
||||
// There is already a stream open and we have the inode
|
||||
// of the file. Check the file hasn't been deleted
|
||||
// or deleted and recreated.
|
||||
try {
|
||||
var stat = fs.statSync(filename);
|
||||
// File exists - check the inode matches
|
||||
if (stat.ino !== node.wstreamIno) {
|
||||
// The file has been recreated. Close the current
|
||||
// stream and recreate it
|
||||
recreateStream = true;
|
||||
node.wstream.end();
|
||||
delete node.wstream;
|
||||
delete node.wstreamIno;
|
||||
}
|
||||
} catch(err) {
|
||||
// File does not exist
|
||||
if (node.overwriteFile === "true") {
|
||||
var wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'w', autoClose:true });
|
||||
node.wstream = wstream;
|
||||
wstream.on("error", function(err) {
|
||||
node.error(RED._("file.errors.writefail",{error:err.toString()}),msg);
|
||||
done();
|
||||
});
|
||||
wstream.on("open", function() {
|
||||
wstream.end(data, function() {
|
||||
node.send(msg);
|
||||
done();
|
||||
});
|
||||
})
|
||||
return;
|
||||
}
|
||||
else {
|
||||
// Append mode
|
||||
var recreateStream = !node.wstream || !node.filename;
|
||||
if (node.wstream && node.wstreamIno) {
|
||||
// There is already a stream open and we have the inode
|
||||
// of the file. Check the file hasn't been deleted
|
||||
// or deleted and recreated.
|
||||
try {
|
||||
var stat = fs.statSync(filename);
|
||||
// File exists - check the inode matches
|
||||
if (stat.ino !== node.wstreamIno) {
|
||||
// The file has been recreated. Close the current
|
||||
// stream and recreate it
|
||||
recreateStream = true;
|
||||
node.wstream.end();
|
||||
delete node.wstream;
|
||||
delete node.wstreamIno;
|
||||
}
|
||||
}
|
||||
if (recreateStream) {
|
||||
node.wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'a', autoClose:true });
|
||||
node.wstream.on("open", function(fd) {
|
||||
try {
|
||||
var stat = fs.statSync(filename);
|
||||
node.wstreamIno = stat.ino;
|
||||
} catch(err) {
|
||||
}
|
||||
});
|
||||
node.wstream.on("error", function(err) {
|
||||
node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg);
|
||||
});
|
||||
}
|
||||
if (node.filename) {
|
||||
// Static filename - write and reuse the stream next time
|
||||
var packet = node.data.shift()
|
||||
node.wstream.write(packet.data, function() {
|
||||
node.send(packet.msg);
|
||||
});
|
||||
|
||||
} else {
|
||||
// Dynamic filename - write and close the stream
|
||||
var packet = node.data.shift()
|
||||
node.wstream.end(packet.data, function() {
|
||||
node.send(packet.msg);
|
||||
});
|
||||
} catch(err) {
|
||||
// File does not exist
|
||||
recreateStream = true;
|
||||
node.wstream.end();
|
||||
delete node.wstream;
|
||||
delete node.wstreamIno;
|
||||
}
|
||||
}
|
||||
if (recreateStream) {
|
||||
node.wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'a', autoClose:true });
|
||||
node.wstream.on("open", function(fd) {
|
||||
try {
|
||||
var stat = fs.statSync(filename);
|
||||
node.wstreamIno = stat.ino;
|
||||
} catch(err) {
|
||||
}
|
||||
});
|
||||
node.wstream.on("error", function(err) {
|
||||
node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg);
|
||||
done();
|
||||
});
|
||||
}
|
||||
if (node.filename) {
|
||||
// Static filename - write and reuse the stream next time
|
||||
node.wstream.write(data, function() {
|
||||
node.send(msg);
|
||||
done();
|
||||
});
|
||||
} else {
|
||||
// Dynamic filename - write and close the stream
|
||||
node.wstream.end(data, function() {
|
||||
node.send(msg);
|
||||
delete node.wstream;
|
||||
delete node.wstreamIno;
|
||||
done();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
done();
|
||||
}
|
||||
}
|
||||
|
||||
function processQ(queue) {
|
||||
var msg = queue[0];
|
||||
processMsg(msg, function() {
|
||||
queue.shift();
|
||||
if (queue.length > 0) {
|
||||
processQ(queue);
|
||||
}
|
||||
else if (node.closing) {
|
||||
closeNode();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
this.on("input", function(msg) {
|
||||
var msgQueue = node.msgQueue;
|
||||
if (msgQueue.push(msg) > 1) {
|
||||
// pending write exists
|
||||
return;
|
||||
}
|
||||
try {
|
||||
processQ(msgQueue);
|
||||
}
|
||||
catch (e) {
|
||||
node.msgQueue = [];
|
||||
if (node.closing) {
|
||||
closeNode();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
this.on('close', function() {
|
||||
|
||||
function closeNode() {
|
||||
if (node.wstream) { node.wstream.end(); }
|
||||
if (node.tout) { clearTimeout(node.tout); }
|
||||
node.status({});
|
||||
var cb = node.closeCallback;
|
||||
node.closeCallback = null;
|
||||
node.closing = false;
|
||||
if (cb) {
|
||||
cb();
|
||||
}
|
||||
}
|
||||
|
||||
this.on('close', function(done) {
|
||||
if (node.closing) {
|
||||
// already closing
|
||||
return;
|
||||
}
|
||||
node.closing = true;
|
||||
if (done) {
|
||||
node.closeCallback = done;
|
||||
}
|
||||
if (node.msgQueue.length > 0) {
|
||||
// close after queue processed
|
||||
return;
|
||||
}
|
||||
else {
|
||||
closeNode();
|
||||
}
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("file",FileNode);
|
||||
|
||||
236
package.json
236
package.json
@@ -1,124 +1,120 @@
|
||||
{
|
||||
"name": "node-red",
|
||||
"version": "0.18.7",
|
||||
"description": "A visual tool for wiring the Internet of Things",
|
||||
"homepage": "http://nodered.org",
|
||||
"license": "Apache-2.0",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/node-red/node-red.git"
|
||||
"name": "node-red",
|
||||
"version": "0.19.5",
|
||||
"description": "A visual tool for wiring the Internet of Things",
|
||||
"homepage": "http://nodered.org",
|
||||
"license": "Apache-2.0",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/node-red/node-red.git"
|
||||
},
|
||||
"main": "red/red.js",
|
||||
"scripts": {
|
||||
"start": "node red.js",
|
||||
"test": "grunt",
|
||||
"build": "grunt build"
|
||||
},
|
||||
"bin": {
|
||||
"node-red": "./red.js",
|
||||
"node-red-pi": "bin/node-red-pi"
|
||||
},
|
||||
"contributors": [
|
||||
{
|
||||
"name": "Nick O'Leary"
|
||||
},
|
||||
"main": "red/red.js",
|
||||
"scripts": {
|
||||
"start": "node red.js",
|
||||
"test": "grunt",
|
||||
"build": "grunt build",
|
||||
"docs": "grunt docs"
|
||||
},
|
||||
"bin": {
|
||||
"node-red": "./red.js",
|
||||
"node-red-pi": "bin/node-red-pi"
|
||||
},
|
||||
"contributors": [
|
||||
{
|
||||
"name": "Nick O'Leary"
|
||||
},
|
||||
{
|
||||
"name": "Dave Conway-Jones"
|
||||
}
|
||||
],
|
||||
"keywords": [
|
||||
"editor",
|
||||
"messaging",
|
||||
"iot",
|
||||
"flow"
|
||||
],
|
||||
"dependencies": {
|
||||
"ajv": "6.5.2",
|
||||
"basic-auth": "2.0.0",
|
||||
"bcryptjs": "2.4.3",
|
||||
"body-parser": "1.18.3",
|
||||
"cheerio": "0.22.0",
|
||||
"clone": "2.1.1",
|
||||
"cookie": "0.3.1",
|
||||
"cookie-parser": "1.4.3",
|
||||
"cors": "2.8.4",
|
||||
"cron": "1.3.0",
|
||||
"denque": "1.3.0",
|
||||
"express": "4.16.3",
|
||||
"express-session": "1.15.6",
|
||||
"fs-extra": "5.0.0",
|
||||
"fs.notify": "0.0.4",
|
||||
"hash-sum": "1.0.2",
|
||||
"i18next": "^11.4.0",
|
||||
"is-utf8": "0.2.1",
|
||||
"js-yaml": "3.12.0",
|
||||
"json-stringify-safe": "5.0.1",
|
||||
"jsonata": "1.5.4",
|
||||
"media-typer": "0.3.0",
|
||||
"memorystore": "1.6.0",
|
||||
"mime": "1.4.1",
|
||||
"mqtt": "2.18.3",
|
||||
"multer": "1.3.1",
|
||||
"mustache": "2.3.0",
|
||||
"node-red-node-email": "0.1.*",
|
||||
"node-red-node-feedparser": "^0.1.12",
|
||||
"node-red-node-rbe": "0.2.*",
|
||||
"node-red-node-twitter": "*",
|
||||
"nopt": "4.0.1",
|
||||
"oauth2orize": "1.11.0",
|
||||
"on-headers": "1.0.1",
|
||||
"passport": "0.4.0",
|
||||
"passport-http-bearer": "1.0.1",
|
||||
"passport-oauth2-client-password": "0.1.2",
|
||||
"raw-body": "2.3.3",
|
||||
"request": "2.87.0",
|
||||
"semver": "5.5.0",
|
||||
"sentiment": "2.1.0",
|
||||
"uglify-js": "3.4.5",
|
||||
"when": "3.7.8",
|
||||
"ws": "1.1.5",
|
||||
"xml2js": "0.4.19"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"bcrypt": "~2.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chromedriver": "^2.40.0",
|
||||
"grunt": "~1.0.3",
|
||||
"grunt-chmod": "~1.1.1",
|
||||
"grunt-cli": "~1.2.0",
|
||||
"grunt-concurrent": "~2.3.1",
|
||||
"grunt-contrib-clean": "~1.1.0",
|
||||
"grunt-contrib-compress": "~1.4.0",
|
||||
"grunt-contrib-concat": "~1.0.1",
|
||||
"grunt-contrib-copy": "~1.0.0",
|
||||
"grunt-contrib-jshint": "~1.1.0",
|
||||
"grunt-contrib-uglify": "~3.3.0",
|
||||
"grunt-contrib-watch": "~1.1.0",
|
||||
"grunt-jsdoc": "^2.2.1",
|
||||
"grunt-jsdoc-to-markdown": "^4.0.0",
|
||||
"grunt-jsonlint": "~1.1.0",
|
||||
"grunt-mocha-istanbul": "5.0.2",
|
||||
"grunt-nodemon": "~0.4.2",
|
||||
"grunt-sass": "~2.0.0",
|
||||
"grunt-simple-mocha": "~0.4.1",
|
||||
"grunt-webdriver": "^2.0.3",
|
||||
"http-proxy": "^1.16.2",
|
||||
"ink-docstrap": "^1.3.2",
|
||||
"istanbul": "0.4.5",
|
||||
"mocha": "^5.2.0",
|
||||
"should": "^8.4.0",
|
||||
"sinon": "1.17.7",
|
||||
"stoppable": "^1.0.6",
|
||||
"supertest": "3.1.0",
|
||||
"wdio-chromedriver-service": "^0.1.3",
|
||||
"wdio-mocha-framework": "^0.6.2",
|
||||
"wdio-spec-reporter": "^0.1.5",
|
||||
"webdriverio": "^4.13.1",
|
||||
"node-red-node-test-helper": "^0.1.7"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
{
|
||||
"name": "Dave Conway-Jones"
|
||||
}
|
||||
],
|
||||
"keywords": [
|
||||
"editor",
|
||||
"messaging",
|
||||
"iot",
|
||||
"flow"
|
||||
],
|
||||
"dependencies": {
|
||||
"ajv": "6.5.4",
|
||||
"basic-auth": "2.0.1",
|
||||
"bcryptjs": "2.4.3",
|
||||
"body-parser": "1.18.3",
|
||||
"cheerio": "0.22.0",
|
||||
"clone": "2.1.2",
|
||||
"cookie": "0.3.1",
|
||||
"cookie-parser": "1.4.3",
|
||||
"cors": "2.8.4",
|
||||
"cron": "1.5.0",
|
||||
"denque": "1.3.0",
|
||||
"express": "4.16.4",
|
||||
"express-session": "1.15.6",
|
||||
"fs-extra": "5.0.0",
|
||||
"fs.notify": "0.0.4",
|
||||
"hash-sum": "1.0.2",
|
||||
"https-proxy-agent": "2.2.1",
|
||||
"i18next": "11.6.0",
|
||||
"is-utf8": "0.2.1",
|
||||
"js-yaml": "3.12.0",
|
||||
"json-stringify-safe": "5.0.1",
|
||||
"jsonata": "1.5.4",
|
||||
"media-typer": "0.3.0",
|
||||
"memorystore": "1.6.0",
|
||||
"mqtt": "2.18.8",
|
||||
"multer": "1.4.1",
|
||||
"mustache": "2.3.2",
|
||||
"node-red-node-email": "0.1.*",
|
||||
"node-red-node-feedparser": "^0.1.12",
|
||||
"node-red-node-rbe": "0.2.*",
|
||||
"node-red-node-twitter": "^1.1.0",
|
||||
"nopt": "4.0.1",
|
||||
"oauth2orize": "1.11.0",
|
||||
"on-headers": "1.0.1",
|
||||
"passport": "0.4.0",
|
||||
"passport-http-bearer": "1.0.1",
|
||||
"passport-oauth2-client-password": "0.1.2",
|
||||
"raw-body": "2.3.3",
|
||||
"request": "2.88.0",
|
||||
"semver": "5.6.0",
|
||||
"sentiment": "2.1.0",
|
||||
"uglify-js": "3.4.9",
|
||||
"when": "3.7.8",
|
||||
"ws": "1.1.5",
|
||||
"xml2js": "0.4.19"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"bcrypt": "~2.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chromedriver": "2.41.0",
|
||||
"grunt": "~1.0.3",
|
||||
"grunt-chmod": "~1.1.1",
|
||||
"grunt-cli": "~1.3.1",
|
||||
"grunt-concurrent": "~2.3.1",
|
||||
"grunt-contrib-clean": "~1.1.0",
|
||||
"grunt-contrib-compress": "~1.4.0",
|
||||
"grunt-contrib-concat": "~1.0.1",
|
||||
"grunt-contrib-copy": "~1.0.0",
|
||||
"grunt-contrib-jshint": "~1.1.0",
|
||||
"grunt-contrib-uglify": "~3.4.0",
|
||||
"grunt-contrib-watch": "~1.1.0",
|
||||
"grunt-jsonlint": "~1.1.0",
|
||||
"grunt-mocha-istanbul": "5.0.2",
|
||||
"grunt-nodemon": "~0.4.2",
|
||||
"grunt-sass": "~2.0.0",
|
||||
"grunt-simple-mocha": "~0.4.1",
|
||||
"grunt-webdriver": "^2.0.3",
|
||||
"http-proxy": "^1.16.2",
|
||||
"istanbul": "0.4.5",
|
||||
"mocha": "^5.2.0",
|
||||
"node-red-node-test-helper": "0.1.7",
|
||||
"should": "^8.4.0",
|
||||
"sinon": "1.17.7",
|
||||
"stoppable": "^1.0.7",
|
||||
"supertest": "3.3.0",
|
||||
"wdio-chromedriver-service": "^0.1.3",
|
||||
"wdio-mocha-framework": "^0.6.2",
|
||||
"wdio-spec-reporter": "^0.1.5",
|
||||
"webdriverio": "^4.14.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,28 +14,120 @@
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var apiUtils = require("../util");
|
||||
|
||||
var runtimeAPI;
|
||||
var log;
|
||||
var redNodes;
|
||||
var util;
|
||||
var settings;
|
||||
|
||||
function exportContextStore(scope,ctx, store, result, callback) {
|
||||
ctx.keys(store,function(err, keys) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
result[store] = {};
|
||||
var c = keys.length;
|
||||
if (c === 0) {
|
||||
callback(null);
|
||||
} else {
|
||||
keys.forEach(function(key) {
|
||||
ctx.get(key,store,function(err, v) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
if (scope !== 'global' ||
|
||||
store === redNodes.listContextStores().default ||
|
||||
!settings.hasOwnProperty("functionGlobalContext") ||
|
||||
!settings.functionGlobalContext.hasOwnProperty(key) ||
|
||||
settings.functionGlobalContext[key] !== v) {
|
||||
result[store][key] = util.encodeObject({msg:v});
|
||||
}
|
||||
c--;
|
||||
if (c === 0) {
|
||||
callback(null);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: function(_runtimeAPI) {
|
||||
runtimeAPI = _runtimeAPI;
|
||||
init: function(runtime) {
|
||||
redNodes = runtime.nodes;
|
||||
log = runtime.log;
|
||||
util = runtime.util;
|
||||
settings = runtime.settings;
|
||||
},
|
||||
|
||||
get: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
scope: req.params.scope,
|
||||
id: req.params.id,
|
||||
key: req.params[0],
|
||||
store: req.query['store']
|
||||
var scope = req.params.scope;
|
||||
var id = req.params.id;
|
||||
var key = req.params[0];
|
||||
var availableStores = redNodes.listContextStores();
|
||||
//{ default: 'default', stores: [ 'default', 'file' ] }
|
||||
var store = req.query['store'];
|
||||
if (store && availableStores.stores.indexOf(store) === -1) {
|
||||
return res.status(404).end();
|
||||
}
|
||||
var ctx;
|
||||
if (scope === 'global') {
|
||||
ctx = redNodes.getContext('global');
|
||||
} else if (scope === 'flow') {
|
||||
ctx = redNodes.getContext(id);
|
||||
} else if (scope === 'node') {
|
||||
var node = redNodes.getNode(id);
|
||||
if (node) {
|
||||
ctx = node.context();
|
||||
}
|
||||
}
|
||||
if (ctx) {
|
||||
if (key) {
|
||||
store = store || availableStores.default;
|
||||
ctx.get(key,store,function(err, v) {
|
||||
var encoded = util.encodeObject({msg:v});
|
||||
if (store !== availableStores.default) {
|
||||
encoded.store = store;
|
||||
}
|
||||
res.json(encoded);
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
var stores;
|
||||
if (!store) {
|
||||
stores = availableStores.stores;
|
||||
} else {
|
||||
stores = [store];
|
||||
}
|
||||
|
||||
var result = {};
|
||||
var c = stores.length;
|
||||
var errorReported = false;
|
||||
stores.forEach(function(store) {
|
||||
exportContextStore(scope,ctx,store,result,function(err) {
|
||||
if (err) {
|
||||
// TODO: proper error reporting
|
||||
if (!errorReported) {
|
||||
errorReported = true;
|
||||
res.end(400);
|
||||
}
|
||||
return;
|
||||
}
|
||||
c--;
|
||||
if (c === 0) {
|
||||
if (!errorReported) {
|
||||
if (stores.length > 1 && scope === 'global') {
|
||||
}
|
||||
res.json(result);
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
|
||||
|
||||
}
|
||||
} else {
|
||||
res.json({});
|
||||
}
|
||||
runtimeAPI.context.getValue(opts).then(function(result) {
|
||||
res.json(result);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,56 +14,72 @@
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var runtimeAPI;
|
||||
var apiUtils = require("../util");
|
||||
var log;
|
||||
var redNodes;
|
||||
|
||||
module.exports = {
|
||||
init: function(_runtimeAPI) {
|
||||
runtimeAPI = _runtimeAPI;
|
||||
init: function(runtime) {
|
||||
redNodes = runtime.nodes;
|
||||
log = runtime.log;
|
||||
},
|
||||
get: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id
|
||||
var id = req.params.id;
|
||||
var flow = redNodes.getFlow(id);
|
||||
if (flow) {
|
||||
log.audit({event: "flow.get",id:id},req);
|
||||
res.json(flow);
|
||||
} else {
|
||||
log.audit({event: "flow.get",id:id,error:"not_found"},req);
|
||||
res.status(404).end();
|
||||
}
|
||||
runtimeAPI.flows.getFlow(opts).then(function(result) {
|
||||
return res.json(result);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
},
|
||||
post: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
flow: req.body
|
||||
}
|
||||
runtimeAPI.flows.addFlow(opts).then(function(id) {
|
||||
return res.json({id:id});
|
||||
var flow = req.body;
|
||||
redNodes.addFlow(flow).then(function(id) {
|
||||
log.audit({event: "flow.add",id:id},req);
|
||||
res.json({id:id});
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
log.audit({event: "flow.add",error:err.code||"unexpected_error",message:err.toString()},req);
|
||||
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
||||
})
|
||||
|
||||
},
|
||||
put: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
flow: req.body
|
||||
var id = req.params.id;
|
||||
var flow = req.body;
|
||||
try {
|
||||
redNodes.updateFlow(id,flow).then(function() {
|
||||
log.audit({event: "flow.update",id:id},req);
|
||||
res.json({id:id});
|
||||
}).catch(function(err) {
|
||||
log.audit({event: "flow.update",error:err.code||"unexpected_error",message:err.toString()},req);
|
||||
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
||||
})
|
||||
} catch(err) {
|
||||
if (err.code === 404) {
|
||||
log.audit({event: "flow.update",id:id,error:"not_found"},req);
|
||||
res.status(404).end();
|
||||
} else {
|
||||
log.audit({event: "flow.update",error:err.code||"unexpected_error",message:err.toString()},req);
|
||||
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
||||
}
|
||||
}
|
||||
runtimeAPI.flows.updateFlow(opts).then(function(id) {
|
||||
return res.json({id:id});
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
},
|
||||
delete: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id
|
||||
var id = req.params.id;
|
||||
try {
|
||||
redNodes.removeFlow(id).then(function() {
|
||||
log.audit({event: "flow.remove",id:id},req);
|
||||
res.status(204).end();
|
||||
})
|
||||
} catch(err) {
|
||||
if (err.code === 404) {
|
||||
log.audit({event: "flow.remove",id:id,error:"not_found"},req);
|
||||
res.status(404).end();
|
||||
} else {
|
||||
log.audit({event: "flow.remove",id:id,error:err.code||"unexpected_error",message:err.toString()},req);
|
||||
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
||||
}
|
||||
}
|
||||
runtimeAPI.flows.deleteFlow(opts).then(function() {
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,57 +14,72 @@
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var runtimeAPI;
|
||||
var apiUtils = require("../util");
|
||||
var log;
|
||||
var redNodes;
|
||||
|
||||
module.exports = {
|
||||
init: function(_runtimeAPI) {
|
||||
runtimeAPI = _runtimeAPI;
|
||||
init: function(runtime) {
|
||||
redNodes = runtime.nodes;
|
||||
log = runtime.log;
|
||||
},
|
||||
get: function(req,res) {
|
||||
var version = req.get("Node-RED-API-Version")||"v1";
|
||||
if (!/^v[12]$/.test(version)) {
|
||||
return res.status(400).json({code:"invalid_api_version", message:"Invalid API Version requested"});
|
||||
if (version === "v1") {
|
||||
log.audit({event: "flows.get",version:"v1"},req);
|
||||
res.json(redNodes.getFlows().flows);
|
||||
} else if (version === "v2") {
|
||||
log.audit({event: "flows.get",version:"v2"},req);
|
||||
res.json(redNodes.getFlows());
|
||||
} else {
|
||||
log.audit({event: "flows.get",version:version,error:"invalid_api_version"},req);
|
||||
res.status(400).json({code:"invalid_api_version", message:"Invalid API Version requested"});
|
||||
}
|
||||
var opts = {
|
||||
user: req.user
|
||||
}
|
||||
runtimeAPI.flows.getFlows(opts).then(function(result) {
|
||||
if (version === "v1") {
|
||||
res.json(result.flows);
|
||||
} else if (version === "v2") {
|
||||
res.json(result);
|
||||
}
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
},
|
||||
post: function(req,res) {
|
||||
var version = req.get("Node-RED-API-Version")||"v1";
|
||||
if (!/^v[12]$/.test(version)) {
|
||||
return res.status(400).json({code:"invalid_api_version", message:"Invalid API Version requested"});
|
||||
log.audit({event: "flows.set",version:version,error:"invalid_api_version"},req);
|
||||
res.status(400).json({code:"invalid_api_version", message:"Invalid API Version requested"});
|
||||
return;
|
||||
}
|
||||
var opts = {
|
||||
user: req.user,
|
||||
deploymentType: req.get("Node-RED-Deployment-Type")||"full"
|
||||
}
|
||||
|
||||
if (opts.deploymentType !== 'reload') {
|
||||
if (version === "v1") {
|
||||
opts.flows = {flows: req.body}
|
||||
} else {
|
||||
opts.flows = req.body;
|
||||
var flows = req.body;
|
||||
var deploymentType = req.get("Node-RED-Deployment-Type")||"full";
|
||||
log.audit({event: "flows.set",type:deploymentType,version:version},req);
|
||||
if (deploymentType === 'reload') {
|
||||
redNodes.loadFlows().then(function(flowId) {
|
||||
if (version === "v1") {
|
||||
res.status(204).end();
|
||||
} else {
|
||||
res.json({rev:flowId});
|
||||
}
|
||||
}).catch(function(err) {
|
||||
log.warn(log._("api.flows.error-reload",{message:err.message}));
|
||||
log.warn(err.stack);
|
||||
res.status(500).json({error:"unexpected_error", message:err.message});
|
||||
});
|
||||
} else {
|
||||
var flowConfig = flows;
|
||||
if (version === "v2") {
|
||||
flowConfig = flows.flows;
|
||||
if (flows.hasOwnProperty('rev')) {
|
||||
var currentVersion = redNodes.getFlows().rev;
|
||||
if (currentVersion !== flows.rev) {
|
||||
//TODO: log warning
|
||||
return res.status(409).json({code:"version_mismatch"});
|
||||
}
|
||||
}
|
||||
}
|
||||
redNodes.setFlows(flowConfig,deploymentType).then(function(flowId) {
|
||||
if (version === "v1") {
|
||||
res.status(204).end();
|
||||
} else if (version === "v2") {
|
||||
res.json({rev:flowId});
|
||||
}
|
||||
}).catch(function(err) {
|
||||
log.warn(log._("api.flows.error-save",{message:err.message}));
|
||||
log.warn(err.stack);
|
||||
res.status(500).json({error:err.code || "unexpected_error", message:err.message});
|
||||
});
|
||||
}
|
||||
|
||||
runtimeAPI.flows.setFlows(opts).then(function(result) {
|
||||
if (version === "v1") {
|
||||
res.status(204).end();
|
||||
} else {
|
||||
res.json(result);
|
||||
}
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,11 +25,11 @@ var auth = require("../auth");
|
||||
var apiUtil = require("../util");
|
||||
|
||||
module.exports = {
|
||||
init: function(runtimeAPI) {
|
||||
flows.init(runtimeAPI);
|
||||
flow.init(runtimeAPI);
|
||||
nodes.init(runtimeAPI);
|
||||
context.init(runtimeAPI);
|
||||
init: function(runtime) {
|
||||
flows.init(runtime);
|
||||
flow.init(runtime);
|
||||
nodes.init(runtime);
|
||||
context.init(runtime);
|
||||
|
||||
var needsPermission = auth.needsPermission;
|
||||
|
||||
@@ -48,8 +48,6 @@ module.exports = {
|
||||
// Nodes
|
||||
adminApp.get("/nodes",needsPermission("nodes.read"),nodes.getAll,apiUtil.errorHandler);
|
||||
adminApp.post("/nodes",needsPermission("nodes.write"),nodes.post,apiUtil.errorHandler);
|
||||
adminApp.get(/\/nodes\/messages/,needsPermission("nodes.read"),nodes.getModuleCatalogs,apiUtil.errorHandler);
|
||||
adminApp.get(/\/nodes\/((@[^\/]+\/)?[^\/]+\/[^\/]+)\/messages/,needsPermission("nodes.read"),nodes.getModuleCatalog,apiUtil.errorHandler);
|
||||
adminApp.get(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,needsPermission("nodes.read"),nodes.getModule,apiUtil.errorHandler);
|
||||
adminApp.put(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,needsPermission("nodes.write"),nodes.putModule,apiUtil.errorHandler);
|
||||
adminApp.delete(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,needsPermission("nodes.write"),nodes.delete,apiUtil.errorHandler);
|
||||
|
||||
@@ -14,160 +14,230 @@
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var when = require("when");
|
||||
var apiUtils = require("../util");
|
||||
|
||||
var runtimeAPI;
|
||||
var redNodes;
|
||||
var log;
|
||||
var settings;
|
||||
|
||||
module.exports = {
|
||||
init: function(_runtimeAPI) {
|
||||
runtimeAPI = _runtimeAPI;
|
||||
init: function(runtime) {
|
||||
redNodes = runtime.nodes;
|
||||
log = runtime.log;
|
||||
settings = runtime.settings;
|
||||
},
|
||||
getAll: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user
|
||||
}
|
||||
if (req.get("accept") == "application/json") {
|
||||
runtimeAPI.nodes.getNodeList(opts).then(function(list) {
|
||||
res.json(list);
|
||||
})
|
||||
log.audit({event: "nodes.list.get"},req);
|
||||
res.json(redNodes.getNodeList());
|
||||
} else {
|
||||
opts.lang = apiUtils.determineLangFromHeaders(req.acceptsLanguages());
|
||||
runtimeAPI.nodes.getNodeConfigs(opts).then(function(configs) {
|
||||
res.send(configs);
|
||||
})
|
||||
var lang = apiUtils.determineLangFromHeaders(req.acceptsLanguages());
|
||||
log.audit({event: "nodes.configs.get"},req);
|
||||
res.send(redNodes.getNodeConfigs(lang));
|
||||
}
|
||||
},
|
||||
|
||||
post: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
module: req.body.module,
|
||||
version: req.body.version
|
||||
if (!settings.available()) {
|
||||
log.audit({event: "nodes.install",error:"settings_unavailable"},req);
|
||||
res.status(400).json({error:"settings_unavailable", message:"Settings unavailable"});
|
||||
return;
|
||||
}
|
||||
runtimeAPI.nodes.addModule(opts).then(function(info) {
|
||||
res.json(info);
|
||||
var node = req.body;
|
||||
var promise;
|
||||
var isUpgrade = false;
|
||||
if (node.module) {
|
||||
var module = redNodes.getModuleInfo(node.module);
|
||||
if (module) {
|
||||
if (!node.version || module.version === node.version) {
|
||||
log.audit({event: "nodes.install",module:node.module, version:node.version, error:"module_already_loaded"},req);
|
||||
res.status(400).json({error:"module_already_loaded", message:"Module already loaded"});
|
||||
return;
|
||||
}
|
||||
if (!module.local) {
|
||||
log.audit({event: "nodes.install",module:node.module, version:node.version, error:"module_not_local"},req);
|
||||
res.status(400).json({error:"module_not_local", message:"Module not locally installed"});
|
||||
return;
|
||||
}
|
||||
isUpgrade = true;
|
||||
}
|
||||
promise = redNodes.installModule(node.module,node.version);
|
||||
} else {
|
||||
log.audit({event: "nodes.install",module:node.module,error:"invalid_request"},req);
|
||||
res.status(400).json({error:"invalid_request", message:"Invalid request"});
|
||||
return;
|
||||
}
|
||||
promise.then(function(info) {
|
||||
if (node.module) {
|
||||
log.audit({event: "nodes.install",module:node.module,version:node.version},req);
|
||||
res.json(info);
|
||||
}
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
if (err.code === 404) {
|
||||
log.audit({event: "nodes.install",module:node.module,version:node.version,error:"not_found"},req);
|
||||
res.status(404).end();
|
||||
} else if (err.code) {
|
||||
log.audit({event: "nodes.install",module:node.module,version:node.version,error:err.code},req);
|
||||
res.status(400).json({error:err.code, message:err.message});
|
||||
} else {
|
||||
log.audit({event: "nodes.install",module:node.module,version:node.version,error:err.code||"unexpected_error",message:err.toString()},req);
|
||||
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
delete: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
module: req.params[0]
|
||||
if (!settings.available()) {
|
||||
log.audit({event: "nodes.remove",error:"settings_unavailable"},req);
|
||||
res.status(400).json({error:"settings_unavailable", message:"Settings unavailable"});
|
||||
return;
|
||||
}
|
||||
var mod = req.params[0];
|
||||
try {
|
||||
var promise = null;
|
||||
var module = redNodes.getModuleInfo(mod);
|
||||
if (!module) {
|
||||
log.audit({event: "nodes.remove",module:mod,error:"not_found"},req);
|
||||
res.status(404).end();
|
||||
return;
|
||||
} else {
|
||||
promise = redNodes.uninstallModule(mod);
|
||||
}
|
||||
|
||||
promise.then(function(list) {
|
||||
log.audit({event: "nodes.remove",module:mod},req);
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
log.audit({event: "nodes.remove",module:mod,error:err.code||"unexpected_error",message:err.toString()},req);
|
||||
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
||||
});
|
||||
} catch(err) {
|
||||
log.audit({event: "nodes.remove",module:mod,error:err.code||"unexpected_error",message:err.toString()},req);
|
||||
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
||||
}
|
||||
runtimeAPI.nodes.removeModule(opts).then(function() {
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
},
|
||||
|
||||
getSet: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params[0] + "/" + req.params[2]
|
||||
}
|
||||
var id = req.params[0] + "/" + req.params[2];
|
||||
var result = null;
|
||||
if (req.get("accept") === "application/json") {
|
||||
runtimeAPI.nodes.getNodeInfo(opts).then(function(result) {
|
||||
result = redNodes.getNodeInfo(id);
|
||||
if (result) {
|
||||
log.audit({event: "nodes.info.get",id:id},req);
|
||||
delete result.loaded;
|
||||
res.send(result);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
} else {
|
||||
log.audit({event: "nodes.info.get",id:id,error:"not_found"},req);
|
||||
res.status(404).end();
|
||||
}
|
||||
} else {
|
||||
opts.lang = apiUtils.determineLangFromHeaders(req.acceptsLanguages());
|
||||
runtimeAPI.nodes.getNodeConfig(opts).then(function(result) {
|
||||
return res.send(result);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
var lang = apiUtils.determineLangFromHeaders(req.acceptsLanguages());
|
||||
result = redNodes.getNodeConfig(id,lang);
|
||||
if (result) {
|
||||
log.audit({event: "nodes.config.get",id:id},req);
|
||||
res.send(result);
|
||||
} else {
|
||||
log.audit({event: "nodes.config.get",id:id,error:"not_found"},req);
|
||||
res.status(404).end();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
getModule: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
module: req.params[0]
|
||||
var module = req.params[0];
|
||||
var result = redNodes.getModuleInfo(module);
|
||||
if (result) {
|
||||
log.audit({event: "nodes.module.get",module:module},req);
|
||||
res.json(result);
|
||||
} else {
|
||||
log.audit({event: "nodes.module.get",module:module,error:"not_found"},req);
|
||||
res.status(404).end();
|
||||
}
|
||||
runtimeAPI.nodes.getModuleInfo(opts).then(function(result) {
|
||||
res.send(result);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
},
|
||||
|
||||
putSet: function(req,res) {
|
||||
var body = req.body;
|
||||
if (!body.hasOwnProperty("enabled")) {
|
||||
// log.audit({event: "nodes.module.set",error:"invalid_request"},req);
|
||||
res.status(400).json({code:"invalid_request", message:"Invalid request"});
|
||||
if (!settings.available()) {
|
||||
log.audit({event: "nodes.info.set",error:"settings_unavailable"},req);
|
||||
res.status(400).json({error:"settings_unavailable", message:"Settings unavailable"});
|
||||
return;
|
||||
}
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params[0] + "/" + req.params[2],
|
||||
enabled: body.enabled
|
||||
var body = req.body;
|
||||
if (!body.hasOwnProperty("enabled")) {
|
||||
log.audit({event: "nodes.info.set",error:"invalid_request"},req);
|
||||
res.status(400).json({error:"invalid_request", message:"Invalid request"});
|
||||
return;
|
||||
}
|
||||
var id = req.params[0] + "/" + req.params[2];
|
||||
try {
|
||||
var node = redNodes.getNodeInfo(id);
|
||||
var info;
|
||||
if (!node) {
|
||||
log.audit({event: "nodes.info.set",id:id,error:"not_found"},req);
|
||||
res.status(404).end();
|
||||
} else {
|
||||
delete node.loaded;
|
||||
putNode(node, body.enabled).then(function(result) {
|
||||
log.audit({event: "nodes.info.set",id:id,enabled:body.enabled},req);
|
||||
res.json(result);
|
||||
});
|
||||
}
|
||||
} catch(err) {
|
||||
log.audit({event: "nodes.info.set",id:id,enabled:body.enabled,error:err.code||"unexpected_error",message:err.toString()},req);
|
||||
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
||||
}
|
||||
runtimeAPI.nodes.setNodeSetState(opts).then(function(result) {
|
||||
res.send(result);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
},
|
||||
|
||||
putModule: function(req,res) {
|
||||
var body = req.body;
|
||||
if (!body.hasOwnProperty("enabled")) {
|
||||
// log.audit({event: "nodes.module.set",error:"invalid_request"},req);
|
||||
res.status(400).json({code:"invalid_request", message:"Invalid request"});
|
||||
if (!settings.available()) {
|
||||
log.audit({event: "nodes.module.set",error:"settings_unavailable"},req);
|
||||
res.status(400).json({error:"settings_unavailable", message:"Settings unavailable"});
|
||||
return;
|
||||
}
|
||||
var opts = {
|
||||
user: req.user,
|
||||
module: req.params[0],
|
||||
enabled: body.enabled
|
||||
var body = req.body;
|
||||
if (!body.hasOwnProperty("enabled")) {
|
||||
log.audit({event: "nodes.module.set",error:"invalid_request"},req);
|
||||
res.status(400).json({error:"invalid_request", message:"Invalid request"});
|
||||
return;
|
||||
}
|
||||
runtimeAPI.nodes.setModuleState(opts).then(function(result) {
|
||||
res.send(result);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
var mod = req.params[0];
|
||||
try {
|
||||
var module = redNodes.getModuleInfo(mod);
|
||||
if (!module) {
|
||||
log.audit({event: "nodes.module.set",module:mod,error:"not_found"},req);
|
||||
return res.status(404).end();
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
getModuleCatalog: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
module: req.params[0],
|
||||
lang: req.query.lng
|
||||
var nodes = module.nodes;
|
||||
var promises = [];
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
promises.push(putNode(nodes[i],body.enabled));
|
||||
}
|
||||
when.settle(promises).then(function() {
|
||||
res.json(redNodes.getModuleInfo(mod));
|
||||
});
|
||||
} catch(err) {
|
||||
log.audit({event: "nodes.module.set",module:mod,enabled:body.enabled,error:err.code||"unexpected_error",message:err.toString()},req);
|
||||
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
||||
}
|
||||
runtimeAPI.nodes.getModuleCatalog(opts).then(function(result) {
|
||||
res.json(result);
|
||||
}).catch(function(err) {
|
||||
console.log(err.stack);
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
},
|
||||
|
||||
getModuleCatalogs: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
lang: req.query.lng
|
||||
}
|
||||
runtimeAPI.nodes.getModuleCatalogs(opts).then(function(result) {
|
||||
res.json(result);
|
||||
}).catch(function(err) {
|
||||
console.log(err.stack);
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
},
|
||||
|
||||
getIcons: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user
|
||||
}
|
||||
runtimeAPI.nodes.getIconList(opts).then(function(list) {
|
||||
res.json(list);
|
||||
});
|
||||
log.audit({event: "nodes.icons.get"},req);
|
||||
res.json(redNodes.getNodeIcons());
|
||||
}
|
||||
};
|
||||
|
||||
function putNode(node, enabled) {
|
||||
var info;
|
||||
var promise;
|
||||
if (!node.err && node.enabled === enabled) {
|
||||
promise = when.resolve(node);
|
||||
} else {
|
||||
if (enabled) {
|
||||
promise = redNodes.enableNode(node.id);
|
||||
} else {
|
||||
promise = redNodes.disableNode(node.id);
|
||||
}
|
||||
}
|
||||
return promise;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ var permissions = require("./permissions");
|
||||
var theme = require("../editor/theme");
|
||||
|
||||
var settings = null;
|
||||
var log = require("../../util").log; // TODO: separate module
|
||||
var log = null
|
||||
|
||||
|
||||
passport.use(strategies.bearerStrategy.BearerStrategy);
|
||||
@@ -36,11 +36,13 @@ var server = oauth2orize.createServer();
|
||||
|
||||
server.exchange(oauth2orize.exchange.password(strategies.passwordTokenExchange));
|
||||
|
||||
function init(_settings,storage) {
|
||||
settings = _settings;
|
||||
function init(runtime) {
|
||||
settings = runtime.settings;
|
||||
log = runtime.log;
|
||||
if (settings.adminAuth) {
|
||||
Users.init(settings.adminAuth);
|
||||
Tokens.init(settings.adminAuth,storage);
|
||||
Tokens.init(settings.adminAuth,runtime.storage);
|
||||
strategies.init(runtime);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ var Users = require("./users");
|
||||
var Clients = require("./clients");
|
||||
var permissions = require("./permissions");
|
||||
|
||||
var log = require("../../util").log; // TODO: separate module
|
||||
var log;
|
||||
|
||||
var bearerStrategy = function (accessToken, done) {
|
||||
// is this a valid token?
|
||||
@@ -124,6 +124,9 @@ AnonymousStrategy.prototype.authenticate = function(req) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: function(runtime) {
|
||||
log = runtime.log;
|
||||
},
|
||||
bearerStrategy: bearerStrategy,
|
||||
clientPasswordStrategy: clientPasswordStrategy,
|
||||
passwordTokenExchange: passwordTokenExchange,
|
||||
|
||||
@@ -15,168 +15,45 @@
|
||||
**/
|
||||
|
||||
var ws = require("ws");
|
||||
|
||||
var log = require("../../util").log; // TODO: separate module
|
||||
var Tokens;
|
||||
var Users;
|
||||
var Permissions;
|
||||
var log;
|
||||
|
||||
var server;
|
||||
var settings;
|
||||
var runtimeAPI;
|
||||
|
||||
var wsServer;
|
||||
var pendingConnections = [];
|
||||
var activeConnections = [];
|
||||
|
||||
var anonymousUser;
|
||||
|
||||
var retained = {};
|
||||
|
||||
var heartbeatTimer;
|
||||
var lastSentTime;
|
||||
|
||||
function init(_server,_settings,_runtimeAPI) {
|
||||
function handleStatus(event) {
|
||||
publish("status/"+event.id,event.status,true);
|
||||
}
|
||||
function handleRuntimeEvent(event) {
|
||||
log.trace("runtime event: "+JSON.stringify(event));
|
||||
publish("notification/"+event.id,event.payload||{},event.retain);
|
||||
}
|
||||
function init(_server,runtime) {
|
||||
server = _server;
|
||||
settings = _settings;
|
||||
runtimeAPI = _runtimeAPI;
|
||||
Tokens = require("../auth/tokens");
|
||||
Users = require("../auth/users");
|
||||
Permissions = require("../auth/permissions");
|
||||
settings = runtime.settings;
|
||||
log = runtime.log;
|
||||
|
||||
}
|
||||
runtime.events.removeListener("node-status",handleStatus);
|
||||
runtime.events.on("node-status",handleStatus);
|
||||
|
||||
function generateSession(length) {
|
||||
var c = "ABCDEFGHIJKLMNOPQRSTUZWXYZabcdefghijklmnopqrstuvwxyz1234567890";
|
||||
var token = [];
|
||||
for (var i=0;i<length;i++) {
|
||||
token.push(c[Math.floor(Math.random()*c.length)]);
|
||||
}
|
||||
return token.join("");
|
||||
}
|
||||
|
||||
function CommsConnection(ws) {
|
||||
this.session = generateSession(32);
|
||||
this.ws = ws;
|
||||
this.stack = [];
|
||||
this.user = null;
|
||||
this.lastSentTime = 0;
|
||||
var self = this;
|
||||
|
||||
log.audit({event: "comms.open"});
|
||||
log.trace("comms.open "+self.session);
|
||||
var pendingAuth = (settings.adminAuth != null);
|
||||
|
||||
if (!pendingAuth) {
|
||||
addActiveConnection(self);
|
||||
}
|
||||
ws.on('close',function() {
|
||||
log.audit({event: "comms.close",user:self.user, session: self.session});
|
||||
log.trace("comms.close "+self.session);
|
||||
removeActiveConnection(self);
|
||||
});
|
||||
ws.on('message', function(data,flags) {
|
||||
var msg = null;
|
||||
try {
|
||||
msg = JSON.parse(data);
|
||||
} catch(err) {
|
||||
log.trace("comms received malformed message : "+err.toString());
|
||||
return;
|
||||
}
|
||||
if (!pendingAuth) {
|
||||
if (msg.subscribe) {
|
||||
self.subscribe(msg.subscribe);
|
||||
// handleRemoteSubscription(ws,msg.subscribe);
|
||||
}
|
||||
} else {
|
||||
var completeConnection = function(userScope,sendAck) {
|
||||
try {
|
||||
if (!userScope || !Permissions.hasPermission(userScope,"status.read")) {
|
||||
ws.send(JSON.stringify({auth:"fail"}));
|
||||
ws.close();
|
||||
} else {
|
||||
pendingAuth = false;
|
||||
addActiveConnection(self);
|
||||
if (sendAck) {
|
||||
ws.send(JSON.stringify({auth:"ok"}));
|
||||
}
|
||||
}
|
||||
} catch(err) {
|
||||
console.log(err.stack);
|
||||
// Just in case the socket closes before we attempt
|
||||
// to send anything.
|
||||
}
|
||||
}
|
||||
if (msg.auth) {
|
||||
Tokens.get(msg.auth).then(function(client) {
|
||||
if (client) {
|
||||
Users.get(client.user).then(function(user) {
|
||||
if (user) {
|
||||
self.user = user;
|
||||
log.audit({event: "comms.auth",user:self.user});
|
||||
completeConnection(client.scope,true);
|
||||
} else {
|
||||
log.audit({event: "comms.auth.fail"});
|
||||
completeConnection(null,false);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
log.audit({event: "comms.auth.fail"});
|
||||
completeConnection(null,false);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (anonymousUser) {
|
||||
log.audit({event: "comms.auth",user:anonymousUser});
|
||||
self.user = anonymousUser;
|
||||
completeConnection(anonymousUser.permissions,false);
|
||||
//TODO: duplicated code - pull non-auth message handling out
|
||||
if (msg.subscribe) {
|
||||
self.subscribe(msg.subscribe);
|
||||
}
|
||||
} else {
|
||||
log.audit({event: "comms.auth.fail"});
|
||||
completeConnection(null,false);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
ws.on('error', function(err) {
|
||||
log.warn(log._("comms.error",{message:err.toString()}));
|
||||
});
|
||||
}
|
||||
|
||||
CommsConnection.prototype.send = function(topic,data) {
|
||||
var self = this;
|
||||
if (topic && data) {
|
||||
this.stack.push({topic:topic,data:data});
|
||||
}
|
||||
if (!this._xmitTimer) {
|
||||
this._xmitTimer = setTimeout(function() {
|
||||
try {
|
||||
self.ws.send(JSON.stringify(self.stack));
|
||||
self.lastSentTime = Date.now();
|
||||
} catch(err) {
|
||||
removeActiveConnection(self);
|
||||
log.warn(log._("comms.error-send",{message:err.toString()}));
|
||||
}
|
||||
delete self._xmitTimer;
|
||||
self.stack = [];
|
||||
},50);
|
||||
}
|
||||
}
|
||||
|
||||
CommsConnection.prototype.subscribe = function(topic) {
|
||||
runtimeAPI.comms.subscribe({
|
||||
user: this.user,
|
||||
client: this,
|
||||
topic: topic
|
||||
})
|
||||
runtime.events.removeListener("runtime-event",handleRuntimeEvent);
|
||||
runtime.events.on("runtime-event",handleRuntimeEvent);
|
||||
}
|
||||
|
||||
function start() {
|
||||
var Tokens = require("../auth/tokens");
|
||||
var Users = require("../auth/users");
|
||||
var Permissions = require("../auth/permissions");
|
||||
if (!settings.disableEditor) {
|
||||
Users.default().then(function(_anonymousUser) {
|
||||
anonymousUser = _anonymousUser;
|
||||
Users.default().then(function(anonymousUser) {
|
||||
var webSocketKeepAliveTime = settings.webSocketKeepAliveTime || 15000;
|
||||
var path = settings.httpAdminRoot || "/";
|
||||
path = (path.slice(0,1) != "/" ? "/":"") + path + (path.slice(-1) == "/" ? "":"/") + "comms";
|
||||
@@ -191,9 +68,89 @@ function start() {
|
||||
});
|
||||
|
||||
wsServer.on('connection',function(ws) {
|
||||
var commsConnection = new CommsConnection(ws);
|
||||
});
|
||||
log.audit({event: "comms.open"});
|
||||
var pendingAuth = (settings.adminAuth != null);
|
||||
ws._nr_stack = [];
|
||||
ws._nr_ok2tx = true;
|
||||
|
||||
if (!pendingAuth) {
|
||||
activeConnections.push(ws);
|
||||
} else {
|
||||
pendingConnections.push(ws);
|
||||
}
|
||||
ws.on('close',function() {
|
||||
log.audit({event: "comms.close",user:ws.user});
|
||||
removeActiveConnection(ws);
|
||||
removePendingConnection(ws);
|
||||
});
|
||||
ws.on('message', function(data,flags) {
|
||||
var msg = null;
|
||||
try {
|
||||
msg = JSON.parse(data);
|
||||
} catch(err) {
|
||||
log.trace("comms received malformed message : "+err.toString());
|
||||
return;
|
||||
}
|
||||
if (!pendingAuth) {
|
||||
if (msg.subscribe) {
|
||||
handleRemoteSubscription(ws,msg.subscribe);
|
||||
}
|
||||
} else {
|
||||
var completeConnection = function(userScope,sendAck) {
|
||||
try {
|
||||
if (!userScope || !Permissions.hasPermission(userScope,"status.read")) {
|
||||
ws.send(JSON.stringify({auth:"fail"}));
|
||||
ws.close();
|
||||
} else {
|
||||
pendingAuth = false;
|
||||
removePendingConnection(ws);
|
||||
activeConnections.push(ws);
|
||||
if (sendAck) {
|
||||
ws.send(JSON.stringify({auth:"ok"}));
|
||||
}
|
||||
}
|
||||
} catch(err) {
|
||||
// Just in case the socket closes before we attempt
|
||||
// to send anything.
|
||||
}
|
||||
}
|
||||
if (msg.auth) {
|
||||
Tokens.get(msg.auth).then(function(client) {
|
||||
if (client) {
|
||||
Users.get(client.user).then(function(user) {
|
||||
if (user) {
|
||||
ws.user = user;
|
||||
log.audit({event: "comms.auth",user:ws.user});
|
||||
completeConnection(client.scope,true);
|
||||
} else {
|
||||
log.audit({event: "comms.auth.fail"});
|
||||
completeConnection(null,false);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
log.audit({event: "comms.auth.fail"});
|
||||
completeConnection(null,false);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (anonymousUser) {
|
||||
log.audit({event: "comms.auth",user:anonymousUser});
|
||||
completeConnection(anonymousUser.permissions,false);
|
||||
//TODO: duplicated code - pull non-auth message handling out
|
||||
if (msg.subscribe) {
|
||||
handleRemoteSubscription(ws,msg.subscribe);
|
||||
}
|
||||
} else {
|
||||
log.audit({event: "comms.auth.fail"});
|
||||
completeConnection(null,false);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
ws.on('error', function(err) {
|
||||
log.warn(log._("comms.error",{message:err.toString()}));
|
||||
});
|
||||
});
|
||||
|
||||
wsServer.on('error', function(err) {
|
||||
log.warn(log._("comms.error-server",{message:err.toString()}));
|
||||
@@ -204,7 +161,7 @@ function start() {
|
||||
heartbeatTimer = setInterval(function() {
|
||||
var now = Date.now();
|
||||
if (now-lastSentTime > webSocketKeepAliveTime) {
|
||||
activeConnections.forEach(connection => connection.send("hb",lastSentTime));
|
||||
publish("hb",lastSentTime);
|
||||
}
|
||||
}, webSocketKeepAliveTime);
|
||||
});
|
||||
@@ -222,15 +179,63 @@ function stop() {
|
||||
}
|
||||
}
|
||||
|
||||
function addActiveConnection(connection) {
|
||||
activeConnections.push(connection);
|
||||
runtimeAPI.comms.addConnection({client: connection});
|
||||
function publish(topic,data,retain) {
|
||||
if (server) {
|
||||
if (retain) {
|
||||
retained[topic] = data;
|
||||
} else {
|
||||
delete retained[topic];
|
||||
}
|
||||
lastSentTime = Date.now();
|
||||
activeConnections.forEach(function(conn) {
|
||||
publishTo(conn,topic,data);
|
||||
});
|
||||
}
|
||||
}
|
||||
function removeActiveConnection(connection) {
|
||||
|
||||
function publishTo(ws,topic,data) {
|
||||
if (topic && data) {
|
||||
ws._nr_stack.push({topic:topic,data:data});
|
||||
}
|
||||
if (ws._nr_ok2tx && (ws._nr_stack.length > 0)) {
|
||||
ws._nr_ok2tx = false;
|
||||
try {
|
||||
ws.send(JSON.stringify(ws._nr_stack));
|
||||
} catch(err) {
|
||||
removeActiveConnection(ws);
|
||||
removePendingConnection(ws);
|
||||
log.warn(log._("comms.error-send",{message:err.toString()}));
|
||||
}
|
||||
ws._nr_stack = [];
|
||||
setTimeout(function() {
|
||||
ws._nr_ok2tx = true;
|
||||
publishTo(ws);
|
||||
}, 50); // TODO: OK so a 50mS update rate should prob not be hard-coded
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function handleRemoteSubscription(ws,topic) {
|
||||
var re = new RegExp("^"+topic.replace(/([\[\]\?\(\)\\\\$\^\*\.|])/g,"\\$1").replace(/\+/g,"[^/]+").replace(/\/#$/,"(\/.*)?")+"$");
|
||||
for (var t in retained) {
|
||||
if (re.test(t)) {
|
||||
publishTo(ws,t,retained[t]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function removeActiveConnection(ws) {
|
||||
for (var i=0;i<activeConnections.length;i++) {
|
||||
if (activeConnections[i] === connection) {
|
||||
if (activeConnections[i] === ws) {
|
||||
activeConnections.splice(i,1);
|
||||
runtimeAPI.comms.removeConnection({client:connection})
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
function removePendingConnection(ws) {
|
||||
for (var i=0;i<pendingConnections.length;i++) {
|
||||
if (pendingConnections[i] === ws) {
|
||||
pendingConnections.splice(i,1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -239,5 +244,6 @@ function removeActiveConnection(connection) {
|
||||
module.exports = {
|
||||
init:init,
|
||||
start:start,
|
||||
stop:stop
|
||||
stop:stop,
|
||||
publish:publish
|
||||
}
|
||||
|
||||
@@ -14,23 +14,39 @@
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var runtimeAPI;
|
||||
var apiUtils = require("../util");
|
||||
var log;
|
||||
var api;
|
||||
|
||||
module.exports = {
|
||||
init: function(_runtimeAPI) {
|
||||
runtimeAPI = _runtimeAPI
|
||||
init: function(runtime) {
|
||||
log = runtime.log;
|
||||
api = runtime.nodes;
|
||||
},
|
||||
get: function (req, res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
type: req.params.type,
|
||||
id: req.params.id
|
||||
// TODO: It should verify the given node id is of the type specified -
|
||||
// but that would add a dependency from this module to the
|
||||
// registry module that knows about node types.
|
||||
var nodeType = req.params.type;
|
||||
var nodeID = req.params.id;
|
||||
log.audit({event: "credentials.get",type:nodeType,id:nodeID},req);
|
||||
var credentials = api.getCredentials(nodeID);
|
||||
if (!credentials) {
|
||||
res.json({});
|
||||
return;
|
||||
}
|
||||
runtimeAPI.flows.getNodeCredentials(opts).then(function(result) {
|
||||
res.json(result);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
var definition = api.getCredentialDefinition(nodeType);
|
||||
|
||||
var sendCredentials = {};
|
||||
for (var cred in definition) {
|
||||
if (definition.hasOwnProperty(cred)) {
|
||||
if (definition[cred].type == "password") {
|
||||
var key = 'has_' + cred;
|
||||
sendCredentials[key] = credentials[cred] != null && credentials[cred] !== '';
|
||||
continue;
|
||||
}
|
||||
sendCredentials[cred] = credentials[cred] || '';
|
||||
}
|
||||
}
|
||||
res.json(sendCredentials);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,35 +24,31 @@ var info = require("./settings");
|
||||
var auth = require("../auth");
|
||||
var nodes = require("../admin/nodes"); // TODO: move /icons into here
|
||||
var needsPermission;
|
||||
var runtimeAPI;
|
||||
var log = require("../../util").log; // TODO: separate module
|
||||
var i18n = require("../../util").i18n; // TODO: separate module
|
||||
|
||||
var runtime;
|
||||
var log;
|
||||
var apiUtil = require("../util");
|
||||
|
||||
var ensureRuntimeStarted = function(req,res,next) {
|
||||
runtimeAPI.isStarted().then( started => {
|
||||
if (!started) {
|
||||
log.error("Node-RED runtime not started");
|
||||
res.status(503).send("Not started");
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
})
|
||||
if (!runtime.isStarted()) {
|
||||
log.error("Node-RED runtime not started");
|
||||
res.status(503).send("Not started");
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: function(server, settings, _runtimeAPI) {
|
||||
runtimeAPI = _runtimeAPI;
|
||||
init: function(server, _runtime) {
|
||||
runtime = _runtime;
|
||||
log = runtime.log;
|
||||
needsPermission = auth.needsPermission;
|
||||
var settings = runtime.settings;
|
||||
if (!settings.disableEditor) {
|
||||
info.init(runtimeAPI);
|
||||
comms.init(server,settings,runtimeAPI);
|
||||
info.init(runtime);
|
||||
comms.init(server,runtime);
|
||||
|
||||
var ui = require("./ui");
|
||||
|
||||
ui.init(runtimeAPI);
|
||||
|
||||
ui.init(runtime);
|
||||
var editorApp = express();
|
||||
if (settings.requireHttps === true) {
|
||||
editorApp.enable('trust proxy');
|
||||
@@ -71,31 +67,31 @@ module.exports = {
|
||||
editorApp.get("/icons/:scope/:module/:icon",ui.icon);
|
||||
|
||||
var theme = require("./theme");
|
||||
theme.init(settings);
|
||||
theme.init(runtime);
|
||||
editorApp.use("/theme",theme.app());
|
||||
editorApp.use("/",ui.editorResources);
|
||||
|
||||
//Projects
|
||||
var projects = require("./projects");
|
||||
projects.init(runtimeAPI);
|
||||
projects.init(runtime);
|
||||
editorApp.use("/projects",projects.app());
|
||||
|
||||
// Locales
|
||||
var locales = require("./locales");
|
||||
locales.init(runtimeAPI);
|
||||
locales.init(runtime);
|
||||
editorApp.get('/locales/nodes',locales.getAllNodes,apiUtil.errorHandler);
|
||||
editorApp.get(/locales\/(.+)\/?$/,locales.get,apiUtil.errorHandler);
|
||||
|
||||
// Library
|
||||
var library = require("./library");
|
||||
library.init(runtimeAPI);
|
||||
library.init(editorApp,runtime);
|
||||
editorApp.post(new RegExp("/library/flows\/(.*)"),needsPermission("library.write"),library.post,apiUtil.errorHandler);
|
||||
editorApp.get("/library/flows",needsPermission("library.read"),library.getAll,apiUtil.errorHandler);
|
||||
editorApp.get(/library\/([^\/]+)(?:$|\/(.*))/,needsPermission("library.read"),library.getEntry);
|
||||
editorApp.post(/library\/([^\/]+)\/(.*)/,needsPermission("library.write"),library.saveEntry);
|
||||
|
||||
editorApp.get(new RegExp("/library/flows\/(.*)"),needsPermission("library.read"),library.get,apiUtil.errorHandler);
|
||||
|
||||
// Credentials
|
||||
var credentials = require("./credentials");
|
||||
credentials.init(runtimeAPI);
|
||||
credentials.init(runtime);
|
||||
editorApp.get('/credentials/:type/:id', needsPermission("credentials.read"),credentials.get,apiUtil.errorHandler);
|
||||
|
||||
// Settings
|
||||
@@ -104,15 +100,18 @@ module.exports = {
|
||||
editorApp.get("/settings/user",needsPermission("settings.read"),info.userSettings,apiUtil.errorHandler);
|
||||
// User Settings
|
||||
editorApp.post("/settings/user",needsPermission("settings.write"),info.updateUserSettings,apiUtil.errorHandler);
|
||||
|
||||
// SSH keys
|
||||
editorApp.use("/settings/user/keys",needsPermission("settings.write"),info.sshkeys());
|
||||
var sshkeys = require("./sshkeys");
|
||||
sshkeys.init(runtime);
|
||||
editorApp.use("/settings/user/keys",sshkeys.app());
|
||||
|
||||
return editorApp;
|
||||
}
|
||||
},
|
||||
start: function() {
|
||||
var catalogPath = path.resolve(path.join(__dirname,"locales"));
|
||||
return i18n.registerMessageCatalogs([
|
||||
return runtime.i18n.registerMessageCatalogs([
|
||||
{namespace: "editor", dir: catalogPath, file:"editor.json"},
|
||||
{namespace: "jsonata", dir: catalogPath, file:"jsonata.json"},
|
||||
{namespace: "infotips", dir: catalogPath, file:"infotips.json"}
|
||||
@@ -120,5 +119,7 @@ module.exports = {
|
||||
comms.start();
|
||||
});
|
||||
},
|
||||
stop: comms.stop
|
||||
stop: comms.stop,
|
||||
publish: comms.publish,
|
||||
registerLibrary: library.register
|
||||
}
|
||||
|
||||
@@ -13,71 +13,150 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var apiUtils = require("../util");
|
||||
var fs = require('fs');
|
||||
var fspath = require('path');
|
||||
var when = require('when');
|
||||
|
||||
var runtimeAPI;
|
||||
var redApp = null;
|
||||
var storage;
|
||||
var log;
|
||||
var redNodes;
|
||||
var needsPermission = require("../auth").needsPermission;
|
||||
|
||||
module.exports = {
|
||||
init: function(_runtimeAPI) {
|
||||
runtimeAPI = _runtimeAPI;
|
||||
},
|
||||
|
||||
getAll: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
type: 'flows'
|
||||
}
|
||||
runtimeAPI.library.getEntries(opts).then(function(result) {
|
||||
res.json(result);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
});
|
||||
},
|
||||
getEntry: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
type: req.params[0],
|
||||
path: req.params[1]||""
|
||||
}
|
||||
runtimeAPI.library.getEntry(opts).then(function(result) {
|
||||
if (typeof result === "string") {
|
||||
if (opts.type === 'flows') {
|
||||
res.writeHead(200, {'Content-Type': 'application/json'});
|
||||
} else {
|
||||
function createLibrary(type) {
|
||||
if (redApp) {
|
||||
redApp.get(new RegExp("/library/"+type+"($|\/(.*))"),needsPermission("library.read"),function(req,res) {
|
||||
var path = req.params[1]||"";
|
||||
storage.getLibraryEntry(type,path).then(function(result) {
|
||||
log.audit({event: "library.get",type:type},req);
|
||||
if (typeof result === "string") {
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.write(result);
|
||||
res.end();
|
||||
} else {
|
||||
res.json(result);
|
||||
}
|
||||
res.write(result);
|
||||
res.end();
|
||||
} else {
|
||||
res.json(result);
|
||||
}
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
}).catch(function(err) {
|
||||
if (err) {
|
||||
log.warn(log._("api.library.error-load-entry",{path:path,message:err.toString()}));
|
||||
if (err.code === 'forbidden') {
|
||||
log.audit({event: "library.get",type:type,error:"forbidden"},req);
|
||||
res.status(403).end();
|
||||
return;
|
||||
}
|
||||
}
|
||||
log.audit({event: "library.get",type:type,error:"not_found"},req);
|
||||
res.status(404).end();
|
||||
});
|
||||
});
|
||||
},
|
||||
saveEntry: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
type: req.params[0],
|
||||
path: req.params[1]||""
|
||||
}
|
||||
// TODO: horrible inconsistencies between flows and all other types
|
||||
if (opts.type === "flows") {
|
||||
opts.meta = {};
|
||||
opts.body = JSON.stringify(req.body);
|
||||
} else {
|
||||
opts.meta = req.body;
|
||||
opts.body = opts.meta.text;
|
||||
delete opts.meta.text;
|
||||
}
|
||||
runtimeAPI.library.saveEntry(opts).then(function(result) {
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
|
||||
redApp.post(new RegExp("/library/"+type+"\/(.*)"),needsPermission("library.write"),function(req,res) {
|
||||
var path = req.params[0];
|
||||
var meta = req.body;
|
||||
var text = meta.text;
|
||||
delete meta.text;
|
||||
|
||||
storage.saveLibraryEntry(type,path,meta,text).then(function() {
|
||||
log.audit({event: "library.set",type:type},req);
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
log.warn(log._("api.library.error-save-entry",{path:path,message:err.toString()}));
|
||||
if (err.code === 'forbidden') {
|
||||
log.audit({event: "library.set",type:type,error:"forbidden"},req);
|
||||
res.status(403).end();
|
||||
return;
|
||||
}
|
||||
log.audit({event: "library.set",type:type,error:"unexpected_error",message:err.toString()},req);
|
||||
res.status(500).json({error:"unexpected_error", message:err.toString()});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: function(app,runtime) {
|
||||
redApp = app;
|
||||
log = runtime.log;
|
||||
storage = runtime.storage;
|
||||
redNodes = runtime.nodes;
|
||||
},
|
||||
register: createLibrary,
|
||||
|
||||
getAll: function(req,res) {
|
||||
storage.getAllFlows().then(function(flows) {
|
||||
log.audit({event: "library.get.all",type:"flow"},req);
|
||||
var examples = redNodes.getNodeExampleFlows();
|
||||
if (examples) {
|
||||
flows.d = flows.d||{};
|
||||
flows.d._examples_ = redNodes.getNodeExampleFlows();
|
||||
}
|
||||
res.json(flows);
|
||||
});
|
||||
},
|
||||
get: function(req,res) {
|
||||
if (req.params[0].indexOf("_examples_/") === 0) {
|
||||
var m = /^_examples_\/(@.*?\/[^\/]+|[^\/]+)\/(.*)$/.exec(req.params[0]);
|
||||
if (m) {
|
||||
var module = m[1];
|
||||
var path = m[2];
|
||||
var fullPath = redNodes.getNodeExampleFlowPath(module,path);
|
||||
if (fullPath) {
|
||||
try {
|
||||
var resolvedPath = fspath.resolve(fullPath);
|
||||
fs.statSync(resolvedPath);
|
||||
log.audit({event: "library.get",type:"flow",path:req.params[0]},req);
|
||||
return res.sendFile(resolvedPath,{
|
||||
headers:{
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
} catch(err) {
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
// IF we get here, we didn't find the file
|
||||
log.audit({event: "library.get",type:"flow",path:req.params[0],error:"not_found"},req);
|
||||
return res.status(404).end();
|
||||
} else {
|
||||
storage.getFlow(req.params[0]).then(function(data) {
|
||||
// data is already a JSON string
|
||||
log.audit({event: "library.get",type:"flow",path:req.params[0]},req);
|
||||
res.set('Content-Type', 'application/json');
|
||||
res.send(data);
|
||||
}).catch(function(err) {
|
||||
if (err) {
|
||||
log.warn(log._("api.library.error-load-flow",{path:req.params[0],message:err.toString()}));
|
||||
if (err.code === 'forbidden') {
|
||||
log.audit({event: "library.get",type:"flow",path:req.params[0],error:"forbidden"},req);
|
||||
res.status(403).end();
|
||||
return;
|
||||
}
|
||||
}
|
||||
log.audit({event: "library.get",type:"flow",path:req.params[0],error:"not_found"},req);
|
||||
res.status(404).end();
|
||||
});
|
||||
}
|
||||
},
|
||||
post: function(req,res) {
|
||||
// if (req.params[0].indexOf("_examples_/") === 0) {
|
||||
// log.warn(log._("api.library.error-save-flow",{path:req.params[0],message:"forbidden"}));
|
||||
// log.audit({event: "library.set",type:"flow",path:req.params[0],error:"forbidden"},req);
|
||||
// return res.status(403).send({error:"unexpected_error", message:"forbidden"});
|
||||
// }
|
||||
var flow = JSON.stringify(req.body);
|
||||
storage.saveFlow(req.params[0],flow).then(function() {
|
||||
log.audit({event: "library.set",type:"flow",path:req.params[0]},req);
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
log.warn(log._("api.library.error-save-flow",{path:req.params[0],message:err.toString()}));
|
||||
if (err.code === 'forbidden') {
|
||||
log.audit({event: "library.set",type:"flow",path:req.params[0],error:"forbidden"},req);
|
||||
res.status(403).end();
|
||||
return;
|
||||
}
|
||||
log.audit({event: "library.set",type:"flow",path:req.params[0],error:"unexpected_error",message:err.toString()},req);
|
||||
res.status(500).send({error:"unexpected_error", message:err.toString()});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,14 +16,13 @@
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
//var apiUtil = require('../util');
|
||||
|
||||
var i18n = require("../../util").i18n; // TODO: separate module
|
||||
|
||||
var runtimeAPI;
|
||||
var i18n;
|
||||
var redNodes;
|
||||
|
||||
module.exports = {
|
||||
init: function(_runtimeAPI) {
|
||||
runtimeAPI = _runtimeAPI;
|
||||
init: function(runtime) {
|
||||
i18n = runtime.i18n;
|
||||
redNodes = runtime.nodes;
|
||||
},
|
||||
get: function(req,res) {
|
||||
var namespace = req.params[0];
|
||||
@@ -37,5 +36,17 @@ module.exports = {
|
||||
res.json(catalog||{});
|
||||
});
|
||||
i18n.i.changeLanguage(prevLang);
|
||||
|
||||
},
|
||||
getAllNodes: function(req,res) {
|
||||
var lngs = req.query.lng;
|
||||
var nodeList = redNodes.getNodeList();
|
||||
var result = {};
|
||||
nodeList.forEach(function(n) {
|
||||
if (n.module !== "node-red") {
|
||||
result[n.id] = i18n.i.getResourceBundle(lngs, n.id)||{};
|
||||
}
|
||||
});
|
||||
res.json(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,507 +0,0 @@
|
||||
/**
|
||||
* 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 express = require("express");
|
||||
var apiUtils = require("../util");
|
||||
|
||||
var runtimeAPI;
|
||||
var needsPermission = require("../auth").needsPermission;
|
||||
|
||||
module.exports = {
|
||||
init: function(_runtimeAPI) {
|
||||
runtimeAPI = _runtimeAPI;
|
||||
},
|
||||
app: function() {
|
||||
var app = express();
|
||||
|
||||
app.use(function(req,res,next) {
|
||||
runtimeAPI.projects.available().then(function(available) {
|
||||
if (!available) {
|
||||
res.status(404).end();
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Projects
|
||||
|
||||
// List all projects
|
||||
app.get("/", needsPermission("projects.read"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user
|
||||
}
|
||||
runtimeAPI.projects.listProjects(opts).then(function(result) {
|
||||
res.json(result);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
});
|
||||
});
|
||||
|
||||
// Create project
|
||||
app.post("/", needsPermission("projects.write"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
project: req.body
|
||||
}
|
||||
runtimeAPI.projects.createProject(opts).then(function(result) {
|
||||
res.json(result);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
});
|
||||
});
|
||||
|
||||
// Update a project
|
||||
app.put("/:id", needsPermission("projects.write"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
project: req.body
|
||||
}
|
||||
|
||||
if (req.body.active) {
|
||||
runtimeAPI.projects.setActiveProject(opts).then(function() {
|
||||
res.redirect(303,req.baseUrl + '/');
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
} else if (req.body.initialise) {
|
||||
runtimeAPI.projects.initialiseProject(opts).then(function() {
|
||||
res.redirect(303,req.baseUrl + '/'+ req.params.id);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
} else if (req.body.hasOwnProperty('credentialSecret') ||
|
||||
req.body.hasOwnProperty('description') ||
|
||||
req.body.hasOwnProperty('dependencies')||
|
||||
req.body.hasOwnProperty('summary') ||
|
||||
req.body.hasOwnProperty('files') ||
|
||||
req.body.hasOwnProperty('git')) {
|
||||
runtimeAPI.projects.updateProject(opts).then(function() {
|
||||
res.redirect(303,req.baseUrl + '/'+ req.params.id);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:"invalid_request"});
|
||||
}
|
||||
});
|
||||
|
||||
// Get project metadata
|
||||
app.get("/:id", needsPermission("projects.read"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id
|
||||
}
|
||||
runtimeAPI.projects.getProject(opts).then(function(data) {
|
||||
if (data) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.status(404).end();
|
||||
}
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Delete project
|
||||
app.delete("/:id", needsPermission("projects.write"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id
|
||||
}
|
||||
runtimeAPI.projects.deleteProject(opts).then(function() {
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
// Get project status - files, commit counts, branch info
|
||||
app.get("/:id/status", needsPermission("projects.read"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
remote: req.query.remote
|
||||
}
|
||||
runtimeAPI.projects.getStatus(opts).then(function(data){
|
||||
if (data) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.status(404).end();
|
||||
}
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
// Project file listing
|
||||
app.get("/:id/files", needsPermission("projects.read"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id
|
||||
}
|
||||
runtimeAPI.projects.getFiles(opts).then(function(data) {
|
||||
res.json(data);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
|
||||
});
|
||||
|
||||
|
||||
// Get file content in a given tree (index/stage)
|
||||
app.get("/:id/files/:treeish/*", needsPermission("projects.read"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
path: req.params[0],
|
||||
tree: req.params.treeish
|
||||
}
|
||||
runtimeAPI.projects.getFile(opts).then(function(data) {
|
||||
res.json({content:data});
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Revert a file
|
||||
app.delete("/:id/files/_/*", needsPermission("projects.write"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
path: req.params[0]
|
||||
}
|
||||
|
||||
runtimeAPI.projects.revertFile(opts).then(function() {
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Stage a file
|
||||
app.post("/:id/stage/*", needsPermission("projects.write"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
path: req.params[0]
|
||||
}
|
||||
runtimeAPI.projects.stageFile(opts).then(function() {
|
||||
res.redirect(303,req.baseUrl+"/"+opts.id+"/status");
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Stage multiple files
|
||||
app.post("/:id/stage", needsPermission("projects.write"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
path: req.body.files
|
||||
}
|
||||
runtimeAPI.projects.stageFile(opts).then(function() {
|
||||
res.redirect(303,req.baseUrl+"/"+opts.id+"/status");
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Commit changes
|
||||
app.post("/:id/commit", needsPermission("projects.write"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
message: req.body.message
|
||||
}
|
||||
runtimeAPI.projects.commit(opts).then(function() {
|
||||
res.redirect(303,req.baseUrl+"/"+opts.id+"/status");
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Unstage a file
|
||||
app.delete("/:id/stage/*", needsPermission("projects.write"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
path: req.params[0]
|
||||
}
|
||||
runtimeAPI.projects.unstageFile(opts).then(function() {
|
||||
res.redirect(303,req.baseUrl+"/"+opts.id+"/status");
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Unstage multiple files
|
||||
app.delete("/:id/stage", needsPermission("projects.write"), function(req, res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id
|
||||
}
|
||||
runtimeAPI.projects.unstageFile(opts).then(function() {
|
||||
res.redirect(303,req.baseUrl+"/"+opts.id+"/status");
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Get a file diff
|
||||
app.get("/:id/diff/:type/*", needsPermission("projects.read"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
path: req.params[0],
|
||||
type: req.params.type
|
||||
}
|
||||
runtimeAPI.projects.getFileDiff(opts).then(function(data) {
|
||||
res.json({
|
||||
diff: data
|
||||
})
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Get a list of commits
|
||||
app.get("/:id/commits", needsPermission("projects.read"), function(req, res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
limit: req.query.limit || 20,
|
||||
before: req.query.before
|
||||
}
|
||||
runtimeAPI.projects.getCommits(opts).then(function(data) {
|
||||
res.json(data);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Get an individual commit details
|
||||
app.get("/:id/commits/:sha", needsPermission("projects.read"), function(req, res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
sha: req.params.sha
|
||||
}
|
||||
runtimeAPI.projects.getCommit(opts).then(function(data) {
|
||||
res.json({commit:data});
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Push local commits to remote
|
||||
app.post("/:id/push/?*", needsPermission("projects.write"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
remote: req.params[0],
|
||||
track: req.query.u
|
||||
}
|
||||
runtimeAPI.projects.push(opts).then(function(data) {
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Pull remote commits
|
||||
app.post("/:id/pull/?*", needsPermission("projects.write"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
remote: req.params[0],
|
||||
track: req.query.setUpstream,
|
||||
allowUnrelatedHistories: req.query.allowUnrelatedHistories
|
||||
}
|
||||
runtimeAPI.projects.pull(opts).then(function(data) {
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Abort an ongoing merge
|
||||
app.delete("/:id/merge", needsPermission("projects.write"), function(req, res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id
|
||||
}
|
||||
runtimeAPI.projects.abortMerge(opts).then(function() {
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Resolve a merge
|
||||
app.post("/:id/resolve/*", needsPermission("projects.write"), function(req, res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
path: req.params[0],
|
||||
resolution: req.body.resolutions
|
||||
}
|
||||
runtimeAPI.projects.resolveMerge(opts).then(function() {
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Get a list of local branches
|
||||
app.get("/:id/branches", needsPermission("projects.read"), function(req, res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
remote: false
|
||||
}
|
||||
runtimeAPI.projects.getBranches(opts).then(function(data) {
|
||||
res.json(data);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Delete a local branch - ?force=true
|
||||
app.delete("/:id/branches/:branchName", needsPermission("projects.write"), function(req, res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
branch: req.params.branchName,
|
||||
force: !!req.query.force
|
||||
}
|
||||
runtimeAPI.projects.deleteBranch(opts).then(function(data) {
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Get a list of remote branches
|
||||
app.get("/:id/branches/remote", needsPermission("projects.read"), function(req, res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
remote: true
|
||||
}
|
||||
runtimeAPI.projects.getBranches(opts).then(function(data) {
|
||||
res.json(data);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Get branch status - commit counts/ahead/behind
|
||||
app.get("/:id/branches/remote/*/status", needsPermission("projects.read"), function(req, res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
branch: req.params[0]
|
||||
}
|
||||
runtimeAPI.projects.getBranchStatus(opts).then(function(data) {
|
||||
res.json(data);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Set the active local branch
|
||||
app.post("/:id/branches", needsPermission("projects.write"), function(req, res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
branch: req.body.name,
|
||||
create: req.body.create
|
||||
}
|
||||
runtimeAPI.projects.setBranch(opts).then(function(data) {
|
||||
res.json(data);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Get a list of remotes
|
||||
app.get("/:id/remotes", needsPermission("projects.read"), function(req, res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id
|
||||
}
|
||||
runtimeAPI.projects.getRemotes(opts).then(function(data) {
|
||||
res.json(data);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Add a remote
|
||||
app.post("/:id/remotes", needsPermission("projects.write"), function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
remote: req.body
|
||||
}
|
||||
if (/^https?:\/\/[^/]+@/i.test(req.body.url)) {
|
||||
res.status(400).json({error:"unexpected_error", message:"Git http url must not include username/password"});
|
||||
return;
|
||||
}
|
||||
runtimeAPI.projects.addRemote(opts).then(function(data) {
|
||||
res.redirect(303,req.baseUrl+"/"+opts.id+"/remotes");
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Delete a remote
|
||||
app.delete("/:id/remotes/:remoteName", needsPermission("projects.write"), function(req, res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
remote: req.params.remoteName
|
||||
}
|
||||
runtimeAPI.projects.removeRemote(opts).then(function(data) {
|
||||
res.redirect(303,req.baseUrl+"/"+opts.id+"/remotes");
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
// Update a remote
|
||||
app.put("/:id/remotes/:remoteName", needsPermission("projects.write"), function(req,res) {
|
||||
var remote = req.body || {};
|
||||
remote.name = req.params.remoteName;
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id,
|
||||
remote: remote
|
||||
}
|
||||
runtimeAPI.projects.updateRemote(opts).then(function() {
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
559
red/api/editor/projects/index.js
Normal file
559
red/api/editor/projects/index.js
Normal file
@@ -0,0 +1,559 @@
|
||||
/**
|
||||
* 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 express = require("express");
|
||||
var runtime;
|
||||
var needsPermission = require("../../auth").needsPermission;
|
||||
|
||||
module.exports = {
|
||||
init: function(_runtime) {
|
||||
runtime = _runtime;
|
||||
},
|
||||
app: function() {
|
||||
var app = express();
|
||||
|
||||
app.use(function(req,res,next) {
|
||||
if (!runtime.storage.projects) {
|
||||
res.status(404).end();
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
// Projects
|
||||
|
||||
// List all projects
|
||||
app.get("/", needsPermission("projects.read"), function(req,res) {
|
||||
runtime.storage.projects.listProjects(req.user, req.user).then(function(list) {
|
||||
var active = runtime.storage.projects.getActiveProject(req.user);
|
||||
var response = {
|
||||
projects: list
|
||||
};
|
||||
if (active) {
|
||||
response.active = active.name;
|
||||
}
|
||||
res.json(response);
|
||||
}).catch(function(err) {
|
||||
console.log(err.stack);
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Create project
|
||||
app.post("/", needsPermission("projects.write"), function(req,res) {
|
||||
runtime.storage.projects.createProject(req.user, req.body).then(function(data) {
|
||||
res.json(data);
|
||||
}).catch(function(err) {
|
||||
console.log(err.stack);
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Update a project
|
||||
app.put("/:id", needsPermission("projects.write"), function(req,res) {
|
||||
//TODO: validate the payload properly
|
||||
if (req.body.active) {
|
||||
var currentProject = runtime.storage.projects.getActiveProject(req.user);
|
||||
if (!currentProject || req.params.id !== currentProject.name) {
|
||||
runtime.storage.projects.setActiveProject(req.user, req.params.id).then(function() {
|
||||
res.redirect(303,req.baseUrl + '/');
|
||||
}).catch(function(err) {
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
} else {
|
||||
res.redirect(303,req.baseUrl + '/'+ req.params.id);
|
||||
}
|
||||
} else if (req.body.initialise) {
|
||||
// Initialised set when creating default files for an empty repo
|
||||
runtime.storage.projects.initialiseProject(req.user, req.params.id, req.body).then(function() {
|
||||
res.redirect(303,req.baseUrl + '/'+ req.params.id);
|
||||
}).catch(function(err) {
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
} else if (req.body.hasOwnProperty('credentialSecret') ||
|
||||
req.body.hasOwnProperty('description') ||
|
||||
req.body.hasOwnProperty('dependencies')||
|
||||
req.body.hasOwnProperty('summary') ||
|
||||
req.body.hasOwnProperty('files') ||
|
||||
req.body.hasOwnProperty('git')) {
|
||||
runtime.storage.projects.updateProject(req.user, req.params.id, req.body).then(function() {
|
||||
res.redirect(303,req.baseUrl + '/'+ req.params.id);
|
||||
}).catch(function(err) {
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:"invalid_request"});
|
||||
}
|
||||
});
|
||||
|
||||
// Get project metadata
|
||||
app.get("/:id", needsPermission("projects.read"), function(req,res) {
|
||||
runtime.storage.projects.getProject(req.user, req.params.id).then(function(data) {
|
||||
if (data) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.status(404).end();
|
||||
}
|
||||
}).catch(function(err) {
|
||||
console.log(err.stack);
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
})
|
||||
});
|
||||
|
||||
// Delete project
|
||||
app.delete("/:id", needsPermission("projects.write"), function(req,res) {
|
||||
runtime.storage.projects.deleteProject(req.user, req.params.id).then(function() {
|
||||
res.status(204).end();
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()})
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// Get project status - files, commit counts, branch info
|
||||
app.get("/:id/status", needsPermission("projects.read"), function(req,res) {
|
||||
var includeRemote = req.query.remote;
|
||||
|
||||
runtime.storage.projects.getStatus(req.user, req.params.id, includeRemote).then(function(data) {
|
||||
if (data) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.status(404).end();
|
||||
}
|
||||
}).catch(function(err) {
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
// Project file listing
|
||||
app.get("/:id/files", needsPermission("projects.read"), function(req,res) {
|
||||
runtime.storage.projects.getFiles(req.user, req.params.id).then(function(data) {
|
||||
// console.log("TODO: REMOVE /:id/files as /:id/status is better!")
|
||||
res.json(data);
|
||||
})
|
||||
.catch(function(err) {
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
// Get file content in a given tree (index/stage)
|
||||
app.get("/:id/files/:treeish/*", needsPermission("projects.read"), function(req,res) {
|
||||
var projectId = req.params.id;
|
||||
var treeish = req.params.treeish;
|
||||
var filePath = req.params[0];
|
||||
|
||||
runtime.storage.projects.getFile(req.user, projectId,filePath,treeish).then(function(data) {
|
||||
res.json({content:data});
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
})
|
||||
});
|
||||
|
||||
// Revert a file
|
||||
app.delete("/:id/files/_/*", needsPermission("projects.write"), function(req,res) {
|
||||
var projectId = req.params.id;
|
||||
var filePath = req.params[0];
|
||||
|
||||
runtime.storage.projects.revertFile(req.user, projectId,filePath).then(function() {
|
||||
res.status(204).end();
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
|
||||
// Stage a file
|
||||
app.post("/:id/stage/*", needsPermission("projects.write"), function(req,res) {
|
||||
var projectName = req.params.id;
|
||||
var file = req.params[0];
|
||||
|
||||
runtime.storage.projects.stageFile(req.user, projectName,file).then(function(data) {
|
||||
res.redirect(303,req.baseUrl+"/"+projectName+"/status");
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
})
|
||||
});
|
||||
|
||||
// Stage multiple files
|
||||
app.post("/:id/stage", needsPermission("projects.write"), function(req,res) {
|
||||
var projectName = req.params.id;
|
||||
var files = req.body.files;
|
||||
|
||||
runtime.storage.projects.stageFile(req.user, projectName,files).then(function(data) {
|
||||
res.redirect(303,req.baseUrl+"/"+projectName+"/status");
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
})
|
||||
});
|
||||
|
||||
// Commit changes
|
||||
app.post("/:id/commit", needsPermission("projects.write"), function(req,res) {
|
||||
var projectName = req.params.id;
|
||||
|
||||
runtime.storage.projects.commit(req.user, projectName,req.body).then(function(data) {
|
||||
res.redirect(303,req.baseUrl+"/"+projectName+"/status");
|
||||
})
|
||||
.catch(function(err) {
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Unstage a file
|
||||
app.delete("/:id/stage/*", needsPermission("projects.write"), function(req,res) {
|
||||
var projectName = req.params.id;
|
||||
var file = req.params[0];
|
||||
|
||||
runtime.storage.projects.unstageFile(req.user, projectName,file).then(function(data) {
|
||||
res.redirect(303,req.baseUrl+"/"+projectName+"/status");
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
})
|
||||
});
|
||||
|
||||
// Unstage multiple files
|
||||
app.delete("/:id/stage", needsPermission("projects.write"), function(req, res) {
|
||||
var projectName = req.params.id;
|
||||
runtime.storage.projects.unstageFile(req.user, projectName).then(function(data) {
|
||||
res.redirect(303,req.baseUrl+"/"+projectName+"/status");
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
})
|
||||
});
|
||||
|
||||
// Get a file diff
|
||||
app.get("/:id/diff/:type/*", needsPermission("projects.read"), function(req,res) {
|
||||
var projectName = req.params.id;
|
||||
var type = req.params.type;
|
||||
var file = req.params[0];
|
||||
runtime.storage.projects.getFileDiff(req.user, projectName,file,type).then(function(data) {
|
||||
res.json({
|
||||
diff: data
|
||||
})
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
})
|
||||
});
|
||||
|
||||
// Get a list of commits
|
||||
app.get("/:id/commits", needsPermission("projects.read"), function(req, res) {
|
||||
var projectName = req.params.id;
|
||||
var options = {
|
||||
limit: req.query.limit||20,
|
||||
before: req.query.before
|
||||
};
|
||||
runtime.storage.projects.getCommits(req.user, projectName,options).then(function(data) {
|
||||
res.json(data);
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Get an individual commit details
|
||||
app.get("/:id/commits/:sha", needsPermission("projects.read"), function(req, res) {
|
||||
var projectName = req.params.id;
|
||||
var sha = req.params.sha;
|
||||
|
||||
runtime.storage.projects.getCommit(req.user, projectName,sha).then(function(data) {
|
||||
res.json({commit:data});
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
})
|
||||
});
|
||||
|
||||
// Push local commits to remote
|
||||
app.post("/:id/push/?*", needsPermission("projects.write"), function(req,res) {
|
||||
var projectName = req.params.id;
|
||||
var remoteBranchName = req.params[0]
|
||||
var setRemote = req.query.u;
|
||||
runtime.storage.projects.push(req.user, projectName,remoteBranchName,setRemote).then(function(data) {
|
||||
res.status(204).end();
|
||||
})
|
||||
.catch(function(err) {
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Pull remote commits
|
||||
app.post("/:id/pull/?*", needsPermission("projects.write"), function(req,res) {
|
||||
var projectName = req.params.id;
|
||||
var remoteBranchName = req.params[0];
|
||||
var setUpstream = req.query.setUpstream;
|
||||
var allowUnrelatedHistories = req.query.allowUnrelatedHistories;
|
||||
runtime.storage.projects.pull(req.user, projectName,remoteBranchName,setUpstream,allowUnrelatedHistories).then(function(data) {
|
||||
res.status(204).end();
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Abort an ongoing merge
|
||||
app.delete("/:id/merge", needsPermission("projects.write"), function(req, res) {
|
||||
var projectName = req.params.id;
|
||||
runtime.storage.projects.abortMerge(req.user, projectName).then(function(data) {
|
||||
res.status(204).end();
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Resolve a merge
|
||||
app.post("/:id/resolve/*", needsPermission("projects.write"), function(req, res) {
|
||||
var projectName = req.params.id;
|
||||
var file = req.params[0];
|
||||
var resolution = req.body.resolutions;
|
||||
runtime.storage.projects.resolveMerge(req.user, projectName,file,resolution).then(function(data) {
|
||||
res.status(204).end();
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Get a list of local branches
|
||||
app.get("/:id/branches", needsPermission("projects.read"), function(req, res) {
|
||||
var projectName = req.params.id;
|
||||
runtime.storage.projects.getBranches(req.user, projectName,false).then(function(data) {
|
||||
res.json(data);
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Delete a local branch - ?force=true
|
||||
app.delete("/:id/branches/:branchName", needsPermission("projects.write"), function(req, res) {
|
||||
var projectName = req.params.id;
|
||||
var branchName = req.params.branchName;
|
||||
var force = !!req.query.force;
|
||||
runtime.storage.projects.deleteBranch(req.user, projectName, branchName, false, force).then(function(data) {
|
||||
res.status(204).end();
|
||||
})
|
||||
.catch(function(err) {
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Get a list of remote branches
|
||||
app.get("/:id/branches/remote", needsPermission("projects.read"), function(req, res) {
|
||||
var projectName = req.params.id;
|
||||
runtime.storage.projects.getBranches(req.user, projectName,true).then(function(data) {
|
||||
res.json(data);
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err);
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message, remote: err.remote});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Get branch status - commit counts/ahead/behind
|
||||
app.get("/:id/branches/remote/*/status", needsPermission("projects.read"), function(req, res) {
|
||||
var projectName = req.params.id;
|
||||
var branch = req.params[0];
|
||||
runtime.storage.projects.getBranchStatus(req.user, projectName,branch).then(function(data) {
|
||||
res.json(data);
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Set the active local branch
|
||||
app.post("/:id/branches", needsPermission("projects.write"), function(req, res) {
|
||||
var projectName = req.params.id;
|
||||
var branchName = req.body.name;
|
||||
var isCreate = req.body.create;
|
||||
runtime.storage.projects.setBranch(req.user, projectName,branchName,isCreate).then(function(data) {
|
||||
res.json(data);
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Get a list of remotes
|
||||
app.get("/:id/remotes", needsPermission("projects.read"), function(req, res) {
|
||||
var projectName = req.params.id;
|
||||
runtime.storage.projects.getRemotes(req.user, projectName).then(function(data) {
|
||||
res.json(data);
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err.stack);
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Add a remote
|
||||
app.post("/:id/remotes", needsPermission("projects.write"), function(req,res) {
|
||||
var projectName = req.params.id;
|
||||
if (/^https?:\/\/[^/]+@/i.test(req.body.url)) {
|
||||
res.status(400).json({error:"unexpected_error", message:"Git http url must not include username/password"});
|
||||
return;
|
||||
}
|
||||
runtime.storage.projects.addRemote(req.user, projectName, req.body).then(function() {
|
||||
res.redirect(303,req.baseUrl+"/"+projectName+"/remotes");
|
||||
}).catch(function(err) {
|
||||
console.log(err.stack);
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Delete a remote
|
||||
app.delete("/:id/remotes/:remoteName", needsPermission("projects.write"), function(req, res) {
|
||||
var projectName = req.params.id;
|
||||
var remoteName = req.params.remoteName;
|
||||
runtime.storage.projects.removeRemote(req.user, projectName, remoteName).then(function(data) {
|
||||
res.redirect(303,req.baseUrl+"/"+projectName+"/remotes");
|
||||
})
|
||||
.catch(function(err) {
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Update a remote
|
||||
app.put("/:id/remotes/:remoteName", needsPermission("projects.write"), function(req,res) {
|
||||
var projectName = req.params.id;
|
||||
var remoteName = req.params.remoteName;
|
||||
runtime.storage.projects.updateRemote(req.user, projectName, remoteName, req.body).then(function(data) {
|
||||
res.status(204).end();
|
||||
})
|
||||
.catch(function(err) {
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
@@ -13,48 +13,127 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
var apiUtils = require("../util");
|
||||
var runtimeAPI;
|
||||
var sshkeys = require("./sshkeys");
|
||||
var theme = require("./theme");
|
||||
var theme = require("../editor/theme");
|
||||
var util = require('util');
|
||||
var runtime;
|
||||
var settings;
|
||||
var log;
|
||||
|
||||
module.exports = {
|
||||
init: function(_runtimeAPI) {
|
||||
runtimeAPI = _runtimeAPI;
|
||||
sshkeys.init(runtimeAPI);
|
||||
init: function(_runtime) {
|
||||
runtime = _runtime;
|
||||
settings = runtime.settings;
|
||||
log = runtime.log;
|
||||
},
|
||||
runtimeSettings: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user
|
||||
var safeSettings = {
|
||||
httpNodeRoot: settings.httpNodeRoot||"/",
|
||||
version: settings.version
|
||||
}
|
||||
runtimeAPI.settings.getRuntimeSettings(opts).then(function(result) {
|
||||
var themeSettings = theme.settings();
|
||||
if (themeSettings) {
|
||||
result.editorTheme = themeSettings;
|
||||
if (req.user) {
|
||||
safeSettings.user = {}
|
||||
var props = ["anonymous","username","image","permissions"];
|
||||
props.forEach(prop => {
|
||||
if (req.user.hasOwnProperty(prop)) {
|
||||
safeSettings.user[prop] = req.user[prop];
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
safeSettings.context = runtime.nodes.listContextStores();
|
||||
|
||||
var themeSettings = theme.settings();
|
||||
if (themeSettings) {
|
||||
safeSettings.editorTheme = themeSettings;
|
||||
}
|
||||
|
||||
if (util.isArray(settings.paletteCategories)) {
|
||||
safeSettings.paletteCategories = settings.paletteCategories;
|
||||
}
|
||||
|
||||
if (settings.flowFilePretty) {
|
||||
safeSettings.flowFilePretty = settings.flowFilePretty;
|
||||
}
|
||||
|
||||
if (!runtime.nodes.paletteEditorEnabled()) {
|
||||
safeSettings.editorTheme = safeSettings.editorTheme || {};
|
||||
safeSettings.editorTheme.palette = safeSettings.editorTheme.palette || {};
|
||||
safeSettings.editorTheme.palette.editable = false;
|
||||
}
|
||||
if (runtime.storage.projects) {
|
||||
var activeProject = runtime.storage.projects.getActiveProject();
|
||||
if (activeProject) {
|
||||
safeSettings.project = activeProject;
|
||||
} else if (runtime.storage.projects.flowFileExists()) {
|
||||
safeSettings.files = {
|
||||
flow: runtime.storage.projects.getFlowFilename(),
|
||||
credentials: runtime.storage.projects.getCredentialsFilename()
|
||||
}
|
||||
}
|
||||
res.json(result);
|
||||
});
|
||||
safeSettings.git = {
|
||||
globalUser: runtime.storage.projects.getGlobalGitUser()
|
||||
}
|
||||
}
|
||||
|
||||
safeSettings.flowEncryptionType = runtime.nodes.getCredentialKeyType();
|
||||
|
||||
settings.exportNodeSettings(safeSettings);
|
||||
res.json(safeSettings);
|
||||
},
|
||||
userSettings: function(req, res) {
|
||||
var opts = {
|
||||
user: req.user
|
||||
var username;
|
||||
if (!req.user || req.user.anonymous) {
|
||||
username = '_';
|
||||
} else {
|
||||
username = req.user.username;
|
||||
}
|
||||
runtimeAPI.settings.getUserSettings(opts).then(function(result) {
|
||||
res.json(result);
|
||||
});
|
||||
res.json(settings.getUserSettings(username)||{});
|
||||
},
|
||||
updateUserSettings: function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
settings: req.body
|
||||
var username;
|
||||
if (!req.user || req.user.anonymous) {
|
||||
username = '_';
|
||||
} else {
|
||||
username = req.user.username;
|
||||
}
|
||||
var currentSettings = settings.getUserSettings(username)||{};
|
||||
currentSettings = extend(currentSettings, req.body);
|
||||
try {
|
||||
settings.setUserSettings(username, currentSettings).then(function() {
|
||||
log.audit({event: "settings.update",username:username},req);
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
log.audit({event: "settings.update",username:username,error:err.code||"unexpected_error",message:err.toString()},req);
|
||||
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
||||
});
|
||||
} catch(err) {
|
||||
log.warn(log._("settings.user-not-available",{message:log._("settings.not-available")}));
|
||||
log.audit({event: "settings.update",username:username,error:err.code||"unexpected_error",message:err.toString()},req);
|
||||
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
||||
}
|
||||
runtimeAPI.settings.updateUserSettings(opts).then(function(result) {
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
});
|
||||
},
|
||||
sshkeys: function() {
|
||||
return sshkeys.app()
|
||||
}
|
||||
}
|
||||
|
||||
function extend(target, source) {
|
||||
var keys = Object.keys(source);
|
||||
var i = keys.length;
|
||||
while(i--) {
|
||||
var value = source[keys[i]]
|
||||
var type = typeof value;
|
||||
if (type === 'string' || type === 'number' || type === 'boolean' || Array.isArray(value)) {
|
||||
target[keys[i]] = value;
|
||||
} else if (value === null) {
|
||||
if (target.hasOwnProperty(keys[i])) {
|
||||
delete target[keys[i]];
|
||||
}
|
||||
} else {
|
||||
// Object
|
||||
if (target.hasOwnProperty(keys[i])) {
|
||||
target[keys[i]] = extend(target[keys[i]],value);
|
||||
} else {
|
||||
target[keys[i]] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
@@ -14,9 +14,10 @@
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var apiUtils = require("../util");
|
||||
var express = require("express");
|
||||
var runtimeAPI;
|
||||
var os = require("os");
|
||||
var runtime;
|
||||
var needsPermission = require("../auth").needsPermission;
|
||||
|
||||
function getUsername(userObj) {
|
||||
var username = '__default';
|
||||
@@ -27,72 +28,94 @@ function getUsername(userObj) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: function(_runtimeAPI) {
|
||||
runtimeAPI = _runtimeAPI;
|
||||
init: function(_runtime) {
|
||||
runtime = _runtime;
|
||||
},
|
||||
app: function() {
|
||||
var app = express();
|
||||
|
||||
// SSH keys
|
||||
|
||||
// List all SSH keys
|
||||
app.get("/", function(req,res) {
|
||||
var opts = {
|
||||
user: req.user
|
||||
}
|
||||
runtimeAPI.settings.getUserKeys(opts).then(function(list) {
|
||||
app.get("/", needsPermission("settings.read"), function(req,res) {
|
||||
var username = getUsername(req.user);
|
||||
runtime.storage.projects.ssh.listSSHKeys(username)
|
||||
.then(function(list) {
|
||||
res.json({
|
||||
keys: list
|
||||
});
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
.catch(function(err) {
|
||||
// console.log(err.stack);
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Get SSH key detail
|
||||
app.get("/:id", function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id
|
||||
}
|
||||
runtimeAPI.settings.getUserKey(opts).then(function(data) {
|
||||
res.json({
|
||||
publickey: data
|
||||
});
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
app.get("/:id", needsPermission("settings.read"), function(req,res) {
|
||||
var username = getUsername(req.user);
|
||||
// console.log('username:', username);
|
||||
runtime.storage.projects.ssh.getSSHKey(username, req.params.id)
|
||||
.then(function(data) {
|
||||
if (data) {
|
||||
res.json({
|
||||
publickey: data
|
||||
});
|
||||
} else {
|
||||
res.status(404).end();
|
||||
}
|
||||
})
|
||||
.catch(function(err) {
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Generate a SSH key
|
||||
app.post("/", function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id
|
||||
}
|
||||
// TODO: validate params
|
||||
opts.name = req.body.name;
|
||||
opts.password = req.body.password;
|
||||
opts.comment = req.body.comment;
|
||||
opts.size = req.body.size;
|
||||
|
||||
runtimeAPI.settings.generateUserKey(opts).then(function(name) {
|
||||
res.json({
|
||||
name: name
|
||||
app.post("/", needsPermission("settings.write"), function(req,res) {
|
||||
var username = getUsername(req.user);
|
||||
// console.log('req.body:', req.body);
|
||||
if ( req.body && req.body.name && /^[a-zA-Z0-9\-_]+$/.test(req.body.name)) {
|
||||
runtime.storage.projects.ssh.generateSSHKey(username, req.body)
|
||||
.then(function(name) {
|
||||
// console.log('generate key --- success name:', name);
|
||||
res.json({
|
||||
name: name
|
||||
});
|
||||
})
|
||||
.catch(function(err) {
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
});
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
});
|
||||
}
|
||||
else {
|
||||
res.status(400).json({error:"unexpected_error", message:"You need to have body or body.name"});
|
||||
}
|
||||
});
|
||||
|
||||
// Delete a SSH key
|
||||
app.delete("/:id", function(req,res) {
|
||||
var opts = {
|
||||
user: req.user,
|
||||
id: req.params.id
|
||||
}
|
||||
runtimeAPI.settings.removeUserKey(opts).then(function(name) {
|
||||
app.delete("/:id", needsPermission("settings.write"), function(req,res) {
|
||||
var username = getUsername(req.user);
|
||||
runtime.storage.projects.ssh.deleteSSHKey(username, req.params.id)
|
||||
.then(function() {
|
||||
res.status(204).end();
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
.catch(function(err) {
|
||||
if (err.code) {
|
||||
res.status(400).json({error:err.code, message: err.message});
|
||||
} else {
|
||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ var defaultContext = {
|
||||
var theme = null;
|
||||
var themeContext = clone(defaultContext);
|
||||
var themeSettings = null;
|
||||
var runtime = null;
|
||||
|
||||
var themeApp;
|
||||
|
||||
@@ -77,8 +78,12 @@ function serveFilesFromTheme(themeValue, themeApp, directory) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: function(settings) {
|
||||
init: function(runtime) {
|
||||
var settings = runtime.settings;
|
||||
themeContext = clone(defaultContext);
|
||||
if (runtime.version) {
|
||||
themeContext.version = runtime.version();
|
||||
}
|
||||
themeSettings = null;
|
||||
theme = settings.editorTheme || {};
|
||||
},
|
||||
|
||||
@@ -17,20 +17,17 @@ var express = require('express');
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var Mustache = require("mustache");
|
||||
var mime = require("mime");
|
||||
|
||||
var apiUtils = require("../util");
|
||||
|
||||
var theme = require("./theme");
|
||||
|
||||
var runtimeAPI;
|
||||
var redNodes;
|
||||
|
||||
var templateDir = path.resolve(__dirname+"/../../../editor/templates");
|
||||
var editorTemplate;
|
||||
|
||||
module.exports = {
|
||||
init: function(_runtimeAPI) {
|
||||
runtimeAPI = _runtimeAPI;
|
||||
init: function(runtime) {
|
||||
redNodes = runtime.nodes;
|
||||
editorTemplate = fs.readFileSync(path.join(templateDir,"index.mst"),"utf8");
|
||||
Mustache.parse(editorTemplate);
|
||||
},
|
||||
@@ -49,18 +46,8 @@ module.exports = {
|
||||
var icon = req.params.icon;
|
||||
var scope = req.params.scope;
|
||||
var module = scope ? scope + '/' + req.params.module : req.params.module;
|
||||
var opts = {
|
||||
user: req.user,
|
||||
module: module,
|
||||
icon: icon
|
||||
}
|
||||
runtimeAPI.nodes.getIcon(opts).then(function(data) {
|
||||
var contentType = mime.lookup(icon);
|
||||
res.set("Content-Type", contentType);
|
||||
res.send(data);
|
||||
}).catch(function(err) {
|
||||
apiUtils.rejectHandler(req,res,err);
|
||||
})
|
||||
var iconPath = redNodes.getNodeIconPath(module,icon);
|
||||
res.sendFile(iconPath);
|
||||
},
|
||||
editor: function(req,res) {
|
||||
res.send(Mustache.render(editorTemplate,theme.context()));
|
||||
|
||||
@@ -26,21 +26,17 @@ var apiUtil = require("./util");
|
||||
|
||||
var adminApp;
|
||||
var server;
|
||||
var runtime;
|
||||
var editor;
|
||||
|
||||
function init(_server,settings,storage,runtimeAPI) {
|
||||
function init(_server,_runtime) {
|
||||
server = _server;
|
||||
runtime = _runtime;
|
||||
var settings = runtime.settings;
|
||||
if (settings.httpAdminRoot !== false) {
|
||||
apiUtil.init(runtime);
|
||||
adminApp = express();
|
||||
|
||||
var cors = require('cors');
|
||||
var corsHandler = cors({
|
||||
origin: "*",
|
||||
methods: "GET,PUT,POST,DELETE"
|
||||
});
|
||||
adminApp.use(corsHandler);
|
||||
|
||||
auth.init(settings,storage);
|
||||
auth.init(runtime);
|
||||
|
||||
var maxApiRequestSize = settings.apiMaxLength || '5mb';
|
||||
adminApp.use(bodyParser.json({limit:maxApiRequestSize}));
|
||||
@@ -65,7 +61,7 @@ function init(_server,settings,storage,runtimeAPI) {
|
||||
// Editor
|
||||
if (!settings.disableEditor) {
|
||||
editor = require("./editor");
|
||||
var editorApp = editor.init(server, settings, runtimeAPI);
|
||||
var editorApp = editor.init(server, runtime);
|
||||
adminApp.use(editorApp);
|
||||
}
|
||||
|
||||
@@ -74,7 +70,7 @@ function init(_server,settings,storage,runtimeAPI) {
|
||||
adminApp.use(corsHandler);
|
||||
}
|
||||
|
||||
var adminApiApp = require("./admin").init(runtimeAPI);
|
||||
var adminApiApp = require("./admin").init(runtime);
|
||||
adminApp.use(adminApiApp);
|
||||
} else {
|
||||
adminApp = null;
|
||||
@@ -97,9 +93,23 @@ module.exports = {
|
||||
init: init,
|
||||
start: start,
|
||||
stop: stop,
|
||||
library: {
|
||||
register: function(type) {
|
||||
if (editor) {
|
||||
editor.registerLibrary(type);
|
||||
}
|
||||
}
|
||||
},
|
||||
auth: {
|
||||
needsPermission: auth.needsPermission
|
||||
},
|
||||
comms: {
|
||||
publish: function(topic,data,retain) {
|
||||
if (editor) {
|
||||
editor.publish(topic,data,retain);
|
||||
}
|
||||
}
|
||||
},
|
||||
get adminApp() { return adminApp; },
|
||||
get server() { return server; }
|
||||
};
|
||||
|
||||
@@ -15,12 +15,16 @@
|
||||
**/
|
||||
|
||||
|
||||
var log = require("../util").log; // TODO: separate module
|
||||
var i18n = require("../util").i18n; // TODO: separate module
|
||||
|
||||
var i18n;
|
||||
var log;
|
||||
|
||||
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") {
|
||||
log.error(err);
|
||||
} else {
|
||||
@@ -37,11 +41,5 @@ module.exports = {
|
||||
lang = acceptedLanguages[0];
|
||||
}
|
||||
return lang;
|
||||
},
|
||||
rejectHandler: function(req,res,err) {
|
||||
res.status(err.status||500).json({
|
||||
code: err.code||"unexpected_error",
|
||||
message: err.message||err.toString()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
32
red/red.js
32
red/red.js
@@ -18,9 +18,6 @@ var fs = require("fs");
|
||||
var path = require('path');
|
||||
|
||||
var runtime = require("./runtime");
|
||||
var runtimeAPI = require("./runtime-api");
|
||||
var redUtil = require("./util");
|
||||
|
||||
var api = require("./api");
|
||||
|
||||
process.env.NODE_RED_HOME = process.env.NODE_RED_HOME || path.resolve(__dirname+"/..");
|
||||
@@ -67,17 +64,15 @@ module.exports = {
|
||||
if (!userSettings.coreNodesDir) {
|
||||
userSettings.coreNodesDir = path.resolve(path.join(__dirname,"..","nodes"));
|
||||
}
|
||||
redUtil.init(userSettings);
|
||||
if (userSettings.httpAdminRoot !== false) {
|
||||
runtime.init(userSettings,redUtil,api);
|
||||
runtimeAPI.init(runtime,redUtil);
|
||||
api.init(httpServer,userSettings,runtime.storage,runtimeAPI);
|
||||
|
||||
if (userSettings.httpAdminRoot !== false) {
|
||||
runtime.init(userSettings,api);
|
||||
api.init(httpServer,runtime);
|
||||
apiEnabled = true;
|
||||
server = runtime.adminApi.server;
|
||||
runtime.server = runtime.adminApi.server;
|
||||
} else {
|
||||
runtime.init(userSettings,redUtil);
|
||||
runtime.init(userSettings);
|
||||
apiEnabled = false;
|
||||
if (httpServer){
|
||||
server = httpServer;
|
||||
@@ -106,25 +101,14 @@ module.exports = {
|
||||
})
|
||||
},
|
||||
nodes: runtime.nodes,
|
||||
log: redUtil.log,
|
||||
log: runtime.log,
|
||||
settings:runtime.settings,
|
||||
util: runtime.util,
|
||||
version: runtime.version,
|
||||
events: runtime.events,
|
||||
comms: {
|
||||
publish: function(topic,data,retain) {
|
||||
runtime.events.emit("comms",{
|
||||
topic: topic,
|
||||
data: data,
|
||||
retain: retain
|
||||
})
|
||||
}
|
||||
},
|
||||
library: {
|
||||
register: function(type) {
|
||||
return runtime.library.register(null,type);
|
||||
}
|
||||
},
|
||||
|
||||
comms: api.comms,
|
||||
library: api.library,
|
||||
auth: api.auth,
|
||||
|
||||
get app() { console.log("Deprecated use of RED.app - use RED.httpAdmin instead"); return runtime.app },
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
|
||||
/**
|
||||
* @namespace RED.comms
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef CommsConnection
|
||||
* @type {object}
|
||||
* @property {string} session - a unique session identifier
|
||||
* @property {Object} user - the user associated with the connection
|
||||
* @property {Function} send - publish a message to the connection
|
||||
*/
|
||||
|
||||
|
||||
var runtime;
|
||||
var retained = {};
|
||||
var connections = [];
|
||||
|
||||
|
||||
function handleCommsEvent(event) {
|
||||
publish(event.topic,event.data,event.retain);
|
||||
}
|
||||
function handleStatusEvent(event) {
|
||||
publish("status/"+event.id,event.status,true);
|
||||
}
|
||||
function handleRuntimeEvent(event) {
|
||||
runtime.log.trace("runtime event: "+JSON.stringify(event));
|
||||
publish("notification/"+event.id,event.payload||{},event.retain);
|
||||
}
|
||||
|
||||
function publish(topic,data,retain) {
|
||||
if (retain) {
|
||||
retained[topic] = data;
|
||||
} else {
|
||||
delete retained[topic];
|
||||
}
|
||||
connections.forEach(connection => connection.send(topic,data))
|
||||
}
|
||||
|
||||
|
||||
var api = module.exports = {
|
||||
init: function(_runtime) {
|
||||
runtime = _runtime;
|
||||
connections = [];
|
||||
retained = {};
|
||||
runtime.events.removeListener("node-status",handleStatusEvent);
|
||||
runtime.events.on("node-status",handleStatusEvent);
|
||||
runtime.events.removeListener("runtime-event",handleRuntimeEvent);
|
||||
runtime.events.on("runtime-event",handleRuntimeEvent);
|
||||
runtime.events.removeListener("comms",handleCommsEvent);
|
||||
runtime.events.on("comms",handleCommsEvent);
|
||||
},
|
||||
|
||||
/**
|
||||
* Registers a new comms connection
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {CommsConnection} opts.client - the client connection
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.comms
|
||||
*/
|
||||
addConnection: function(opts) {
|
||||
connections.push(opts.client);
|
||||
return Promise.resolve();
|
||||
},
|
||||
|
||||
/**
|
||||
* Unregisters a comms connection
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {CommsConnection} opts.client - the client connection
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.comms
|
||||
*/
|
||||
removeConnection: function(opts) {
|
||||
for (var i=0;i<connections.length;i++) {
|
||||
if (connections[i] === opts.client) {
|
||||
connections.splice(i,1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Promise.resolve();
|
||||
},
|
||||
|
||||
/**
|
||||
* Subscribes a comms connection to a given topic. Currently, all clients get
|
||||
* automatically subscribed to everything and cannot unsubscribe. Sending a subscribe
|
||||
* request will trigger retained messages to be sent.
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {CommsConnection} opts.client - the client connection
|
||||
* @param {String} opts.topic - the topic to subscribe to
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.comms
|
||||
*/
|
||||
subscribe: function(opts) {
|
||||
var re = new RegExp("^"+opts.topic.replace(/([\[\]\?\(\)\\\\$\^\*\.|])/g,"\\$1").replace(/\+/g,"[^/]+").replace(/\/#$/,"(\/.*)?")+"$");
|
||||
for (var t in retained) {
|
||||
if (re.test(t)) {
|
||||
opts.client.send(t,retained[t]);
|
||||
}
|
||||
}
|
||||
return Promise.resolve();
|
||||
},
|
||||
|
||||
/**
|
||||
* TODO: Unsubscribes a comms connection from a given topic
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {CommsConnection} opts.client - the client connection
|
||||
* @param {String} opts.topic - the topic to unsubscribe from
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.comms
|
||||
*/
|
||||
unsubscribe: function(opts) {}
|
||||
};
|
||||
@@ -1,156 +0,0 @@
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
|
||||
/**
|
||||
* @namespace RED.context
|
||||
*/
|
||||
|
||||
var runtime;
|
||||
|
||||
// TODO: move runtime/util to util/index
|
||||
var util = require("../runtime/util");
|
||||
|
||||
function exportContextStore(scope,ctx, store, result, callback) {
|
||||
ctx.keys(store,function(err, keys) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
result[store] = {};
|
||||
var c = keys.length;
|
||||
if (c === 0) {
|
||||
callback(null);
|
||||
} else {
|
||||
keys.forEach(function(key) {
|
||||
ctx.get(key,store,function(err, v) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
if (scope !== 'global' ||
|
||||
store === runtime.nodes.listContextStores().default ||
|
||||
!runtime.settings.hasOwnProperty("functionGlobalContext") ||
|
||||
!runtime.settings.functionGlobalContext.hasOwnProperty(key) ||
|
||||
runtime.settings.functionGlobalContext[key] !== v) {
|
||||
result[store][key] = util.encodeObject({msg:v});
|
||||
}
|
||||
c--;
|
||||
if (c === 0) {
|
||||
callback(null);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
var api = module.exports = {
|
||||
init: function(_runtime) {
|
||||
runtime = _runtime;
|
||||
},
|
||||
/**
|
||||
* Gets the info of an individual node set
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.scope - the scope of the context
|
||||
* @param {String} opts.id - the id of the context
|
||||
* @param {String} opts.store - the context store
|
||||
* @param {String} opts.key - the context key
|
||||
|
||||
* @return {Promise} - the node information
|
||||
* @memberof RED.nodes
|
||||
*/
|
||||
getValue: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
var scope = opts.scope;
|
||||
var id = opts.id;
|
||||
var store = opts.store;
|
||||
var key = opts.key;
|
||||
|
||||
var availableStores = runtime.nodes.listContextStores();
|
||||
//{ default: 'default', stores: [ 'default', 'file' ] }
|
||||
if (store && availableStores.stores.indexOf(store) === -1) {
|
||||
runtime.log.audit({event: "context.get",scope:scope,id:id,store:store,key:key,error:"not_found"});
|
||||
var err = new Error();
|
||||
err.code = "not_found";
|
||||
err.status = 404;
|
||||
return reject(err);
|
||||
}
|
||||
var ctx;
|
||||
if (scope === 'global') {
|
||||
ctx = runtime.nodes.getContext('global');
|
||||
} else if (scope === 'flow') {
|
||||
ctx = runtime.nodes.getContext(id);
|
||||
} else if (scope === 'node') {
|
||||
var node = runtime.nodes.getNode(id);
|
||||
if (node) {
|
||||
ctx = node.context();
|
||||
}
|
||||
}
|
||||
if (ctx) {
|
||||
if (key) {
|
||||
store = store || availableStores.default;
|
||||
ctx.get(key,store,function(err, v) {
|
||||
var encoded = util.encodeObject({msg:v});
|
||||
if (store !== availableStores.default) {
|
||||
encoded.store = store;
|
||||
}
|
||||
runtime.log.audit({event: "context.get",scope:scope,id:id,store:store,key:key});
|
||||
resolve(encoded);
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
var stores;
|
||||
if (!store) {
|
||||
stores = availableStores.stores;
|
||||
} else {
|
||||
stores = [store];
|
||||
}
|
||||
|
||||
var result = {};
|
||||
var c = stores.length;
|
||||
var errorReported = false;
|
||||
stores.forEach(function(store) {
|
||||
exportContextStore(scope,ctx,store,result,function(err) {
|
||||
if (err) {
|
||||
// TODO: proper error reporting
|
||||
if (!errorReported) {
|
||||
errorReported = true;
|
||||
runtime.log.audit({event: "context.get",scope:scope,id:id,store:store,key:key,error:"unexpected_error"});
|
||||
var err = new Error();
|
||||
err.code = "unexpected_error";
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
c--;
|
||||
if (c === 0) {
|
||||
if (!errorReported) {
|
||||
runtime.log.audit({event: "context.get",scope:scope,id:id,store:store,key:key});
|
||||
resolve(result);
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
} else {
|
||||
runtime.log.audit({event: "context.get",scope:scope,id:id,store:store,key:key});
|
||||
resolve({});
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,251 +0,0 @@
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
|
||||
/**
|
||||
* @namespace RED.flows
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef Flows
|
||||
* @type {object}
|
||||
* @property {string} rev - the flow revision identifier
|
||||
* @property {Array} flows - the flow configuration, an array of node configuration objects
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef Flow
|
||||
* @type {object}
|
||||
* @property {string} id - the flow identifier
|
||||
* @property {string} label - a label for the flow
|
||||
* @property {Array} nodes - an array of node configuration objects
|
||||
*/
|
||||
|
||||
var runtime;
|
||||
|
||||
var api = module.exports = {
|
||||
init: function(_runtime) {
|
||||
runtime = _runtime;
|
||||
},
|
||||
/**
|
||||
* Gets the current flow configuration
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @return {Promise<Flows>} - the active flow configuration
|
||||
* @memberof RED.flows
|
||||
*/
|
||||
getFlows: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
runtime.log.audit({event: "flows.get"}/*,req*/);
|
||||
return resolve(runtime.nodes.getFlows());
|
||||
});
|
||||
},
|
||||
/**
|
||||
* Sets the current flow configuration
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @return {Promise<Flows>} - the active flow configuration
|
||||
* @memberof RED.flows
|
||||
*/
|
||||
setFlows: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
|
||||
var flows = opts.flows;
|
||||
var deploymentType = opts.deploymentType||"full";
|
||||
runtime.log.audit({event: "flows.set",type:deploymentType}/*,req*/);
|
||||
|
||||
var apiPromise;
|
||||
if (deploymentType === 'reload') {
|
||||
apiPromise = runtime.nodes.loadFlows();
|
||||
} else {
|
||||
if (flows.hasOwnProperty('rev')) {
|
||||
var currentVersion = runtime.nodes.getFlows().rev;
|
||||
if (currentVersion !== flows.rev) {
|
||||
var err;
|
||||
err = new Error();
|
||||
err.code = "version_mismatch";
|
||||
err.status = 409;
|
||||
//TODO: log warning
|
||||
return reject(err);
|
||||
}
|
||||
}
|
||||
apiPromise = runtime.nodes.setFlows(flows.flows,deploymentType);
|
||||
}
|
||||
apiPromise.then(function(flowId) {
|
||||
return resolve({rev:flowId});
|
||||
}).catch(function(err) {
|
||||
runtime.log.warn(runtime.log._("api.flows.error-"+(deploymentType === 'reload'?'reload':'save'),{message:err.message}));
|
||||
runtime.log.warn(err.stack);
|
||||
return reject(err);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Adds a flow configuration
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {Object} opts.flow - the flow to add
|
||||
* @return {Promise<String>} - the id of the added flow
|
||||
* @memberof RED.flows
|
||||
*/
|
||||
addFlow: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
var flow = opts.flow;
|
||||
runtime.nodes.addFlow(flow).then(function(id) {
|
||||
runtime.log.audit({event: "flow.add",id:id});
|
||||
return resolve(id);
|
||||
}).catch(function(err) {
|
||||
runtime.log.audit({event: "flow.add",error:err.code||"unexpected_error",message:err.toString()});
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets an individual flow configuration
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {Object} opts.id - the id of the flow to retrieve
|
||||
* @return {Promise<Flow>} - the active flow configuration
|
||||
* @memberof RED.flows
|
||||
*/
|
||||
getFlow: function(opts) {
|
||||
return new Promise(function (resolve,reject) {
|
||||
var flow = runtime.nodes.getFlow(opts.id);
|
||||
if (flow) {
|
||||
runtime.log.audit({event: "flow.get",id:opts.id});
|
||||
return resolve(flow);
|
||||
} else {
|
||||
runtime.log.audit({event: "flow.get",id:opts.id,error:"not_found"});
|
||||
var err = new Error();
|
||||
err.code = "not_found";
|
||||
err.status = 404;
|
||||
return reject(err);
|
||||
}
|
||||
})
|
||||
|
||||
},
|
||||
/**
|
||||
* Updates an existing flow configuration
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {Object} opts.id - the id of the flow to update
|
||||
* @param {Object} opts.flow - the flow configuration
|
||||
* @return {Promise<String>} - the id of the updated flow
|
||||
* @memberof RED.flows
|
||||
*/
|
||||
updateFlow: function(opts) {
|
||||
return new Promise(function (resolve,reject) {
|
||||
var flow = opts.flow;
|
||||
var id = opts.id;
|
||||
try {
|
||||
runtime.nodes.updateFlow(id,flow).then(function() {
|
||||
runtime.log.audit({event: "flow.update",id:id});
|
||||
return resolve(id);
|
||||
}).catch(function(err) {
|
||||
runtime.log.audit({event: "flow.update",error:err.code||"unexpected_error",message:err.toString()});
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
})
|
||||
} catch(err) {
|
||||
if (err.code === 404) {
|
||||
runtime.log.audit({event: "flow.update",id:id,error:"not_found"});
|
||||
// TODO: this swap around of .code and .status isn't ideal
|
||||
err.status = 404;
|
||||
err.code = "not_found";
|
||||
return reject(err);
|
||||
} else {
|
||||
runtime.log.audit({event: "flow.update",error:err.code||"unexpected_error",message:err.toString()});
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
},
|
||||
/**
|
||||
* Deletes a flow
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {Object} opts.id - the id of the flow to delete
|
||||
* @return {Promise} - resolves if successful
|
||||
* @memberof RED.flows
|
||||
*/
|
||||
deleteFlow: function(opts) {
|
||||
return new Promise(function (resolve,reject) {
|
||||
var id = opts.id;
|
||||
try {
|
||||
runtime.nodes.removeFlow(id).then(function() {
|
||||
runtime.log.audit({event: "flow.remove",id:id});
|
||||
return resolve();
|
||||
}).catch(function(err) {
|
||||
runtime.log.audit({event: "flow.remove",id:id,error:err.code||"unexpected_error",message:err.toString()});
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
});
|
||||
} catch(err) {
|
||||
if (err.code === 404) {
|
||||
runtime.log.audit({event: "flow.remove",id:id,error:"not_found"});
|
||||
// TODO: this swap around of .code and .status isn't ideal
|
||||
err.status = 404;
|
||||
err.code = "not_found";
|
||||
return reject(err);
|
||||
} else {
|
||||
runtime.log.audit({event: "flow.remove",id:id,error:err.code||"unexpected_error",message:err.toString()});
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the safe credentials for a node
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.type - the node type to return the credential information for
|
||||
* @param {String} opts.id - the node id
|
||||
* @return {Promise<Object>} - the safe credentials
|
||||
* @memberof RED.flows
|
||||
*/
|
||||
getNodeCredentials: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
runtime.log.audit({event: "credentials.get",type:opts.type,id:opts.id});
|
||||
var credentials = runtime.nodes.getCredentials(opts.id);
|
||||
if (!credentials) {
|
||||
return resolve({});
|
||||
}
|
||||
var definition = runtime.nodes.getCredentialDefinition(opts.type) || {};
|
||||
|
||||
var sendCredentials = {};
|
||||
for (var cred in definition) {
|
||||
if (definition.hasOwnProperty(cred)) {
|
||||
if (definition[cred].type == "password") {
|
||||
var key = 'has_' + cred;
|
||||
sendCredentials[key] = credentials[cred] != null && credentials[cred] !== '';
|
||||
continue;
|
||||
}
|
||||
sendCredentials[cred] = credentials[cred] || '';
|
||||
}
|
||||
}
|
||||
resolve(sendCredentials);
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
|
||||
/**
|
||||
* A user accessing the API
|
||||
* @typedef User
|
||||
* @type {object}
|
||||
*/
|
||||
|
||||
var runtime;
|
||||
/**
|
||||
* @namespace RED
|
||||
*/
|
||||
var api = module.exports = {
|
||||
init: function(_runtime, redUtil) {
|
||||
runtime = _runtime;
|
||||
api.comms.init(runtime);
|
||||
api.flows.init(runtime);
|
||||
api.nodes.init(runtime);
|
||||
api.settings.init(runtime);
|
||||
api.library.init(runtime);
|
||||
api.projects.init(runtime);
|
||||
api.context.init(runtime);
|
||||
},
|
||||
|
||||
comms: require("./comms"),
|
||||
flows: require("./flows"),
|
||||
library: require("./library"),
|
||||
nodes: require("./nodes"),
|
||||
settings: require("./settings"),
|
||||
projects: require("./projects"),
|
||||
context: require("./context"),
|
||||
|
||||
/**
|
||||
* Returns whether the runtime is started
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @return {Promise<Boolean>} - whether the runtime is started
|
||||
* @memberof RED
|
||||
*/
|
||||
isStarted: function(opts) {
|
||||
return Promise.resolve(runtime.isStarted());
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns version number of the runtime
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @return {Promise<String>} - the runtime version number
|
||||
* @memberof RED
|
||||
*/
|
||||
version: function(opts) {
|
||||
return Promise.resolve(runtime.version());
|
||||
}
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
|
||||
/**
|
||||
* @namespace RED.library
|
||||
*/
|
||||
|
||||
var runtime;
|
||||
|
||||
var api = module.exports = {
|
||||
init: function(_runtime) {
|
||||
runtime = _runtime;
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets an entry from the library.
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.type - the type of entry
|
||||
* @param {String} opts.path - the path of the entry
|
||||
* @return {Promise<String|Object>} - resolves when complete
|
||||
* @memberof RED.library
|
||||
*/
|
||||
getEntry: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
runtime.library.getEntry(opts.type,opts.path).then(function(result) {
|
||||
runtime.log.audit({event: "library.get",type:opts.type,path:opts.path});
|
||||
return resolve(result);
|
||||
}).catch(function(err) {
|
||||
if (err) {
|
||||
runtime.log.warn(runtime.log._("api.library.error-load-entry",{path:opts.path,message:err.toString()}));
|
||||
if (err.code === 'forbidden') {
|
||||
err.status = 403;
|
||||
return reject(err);
|
||||
} else if (err.code === "not_found") {
|
||||
err.status = 404;
|
||||
} else {
|
||||
err.status = 400;
|
||||
}
|
||||
runtime.log.audit({event: "library.get",type:opts.type,path:opts.path,error:err.code});
|
||||
return reject(err);
|
||||
}
|
||||
runtime.log.audit({event: "library.get",type:opts.type,error:"not_found"});
|
||||
var error = new Error();
|
||||
error.code = "not_found";
|
||||
error.status = 404;
|
||||
return reject(error);
|
||||
});
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Saves an entry to the library
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.type - the type of entry
|
||||
* @param {String} opts.path - the path of the entry
|
||||
* @param {Object} opts.meta - any meta data associated with the entry
|
||||
* @param {String} opts.body - the body of the entry
|
||||
* @return {Promise} - resolves when complete
|
||||
* @memberof RED.library
|
||||
*/
|
||||
saveEntry: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
runtime.library.saveEntry(opts.type,opts.path,opts.meta,opts.body).then(function() {
|
||||
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path});
|
||||
return resolve();
|
||||
}).catch(function(err) {
|
||||
runtime.log.warn(runtime.log._("api.library.error-save-entry",{path:opts.path,message:err.toString()}));
|
||||
if (err.code === 'forbidden') {
|
||||
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"forbidden"});
|
||||
err.status = 403;
|
||||
return reject(err);
|
||||
}
|
||||
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"unexpected_error",message:err.toString()});
|
||||
var error = new Error();
|
||||
error.status = 400;
|
||||
return reject(error);
|
||||
});
|
||||
})
|
||||
},
|
||||
/**
|
||||
* Returns a complete listing of all entries of a given type in the library.
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.type - the type of entry
|
||||
* @return {Promise<Object>} - the entry listing
|
||||
* @memberof RED.library
|
||||
*/
|
||||
getEntries: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
if (opts.type !== 'flows') {
|
||||
return reject(new Error("API only supports flows"));
|
||||
|
||||
}
|
||||
runtime.storage.getAllFlows().then(function(flows) {
|
||||
runtime.log.audit({event: "library.get.all",type:"flow"});
|
||||
var examples = runtime.nodes.getNodeExampleFlows();
|
||||
if (examples) {
|
||||
flows.d = flows.d||{};
|
||||
flows.d._examples_ = examples;
|
||||
}
|
||||
return resolve(flows);
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,438 +0,0 @@
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
|
||||
/**
|
||||
* @namespace RED.nodes
|
||||
*/
|
||||
|
||||
var fs = require("fs");
|
||||
|
||||
var runtime;
|
||||
|
||||
function putNode(node, enabled) {
|
||||
var info;
|
||||
var promise;
|
||||
if (!node.err && node.enabled === enabled) {
|
||||
promise = Promise.resolve(node);
|
||||
} else {
|
||||
if (enabled) {
|
||||
promise = runtime.nodes.enableNode(node.id);
|
||||
} else {
|
||||
promise = runtime.nodes.disableNode(node.id);
|
||||
}
|
||||
}
|
||||
return promise;
|
||||
}
|
||||
|
||||
var api = module.exports = {
|
||||
init: function(_runtime) {
|
||||
runtime = _runtime;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Gets the info of an individual node set
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the node set to return
|
||||
* @return {Promise<NodeInfo>} - the node information
|
||||
* @memberof RED.nodes
|
||||
*/
|
||||
getNodeInfo: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
var id = opts.id;
|
||||
var result = runtime.nodes.getNodeInfo(id);
|
||||
if (result) {
|
||||
runtime.log.audit({event: "nodes.info.get",id:id});
|
||||
delete result.loaded;
|
||||
return resolve(result);
|
||||
} else {
|
||||
runtime.log.audit({event: "nodes.info.get",id:id,error:"not_found"});
|
||||
var err = new Error();
|
||||
err.code = "not_found";
|
||||
err.status = 404;
|
||||
return reject(err);
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the list of node modules installed in the runtime
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @return {Promise<NodeList>} - the list of node modules
|
||||
* @memberof RED.nodes
|
||||
*/
|
||||
getNodeList: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
runtime.log.audit({event: "nodes.list.get"});
|
||||
return resolve(runtime.nodes.getNodeList());
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets an individual node's html content
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the node set to return
|
||||
* @param {String} opts.lang - the locale language to return
|
||||
* @return {Promise<String>} - the node html content
|
||||
* @memberof RED.nodes
|
||||
*/
|
||||
getNodeConfig: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
var id = opts.id;
|
||||
var lang = opts.lang;
|
||||
var result = runtime.nodes.getNodeConfig(id,lang);
|
||||
if (result) {
|
||||
runtime.log.audit({event: "nodes.config.get",id:id});
|
||||
return resolve(result);
|
||||
} else {
|
||||
runtime.log.audit({event: "nodes.config.get",id:id,error:"not_found"});
|
||||
var err = new Error();
|
||||
err.code = "not_found";
|
||||
err.status = 404;
|
||||
return reject(err);
|
||||
}
|
||||
});
|
||||
},
|
||||
/**
|
||||
* Gets all node html content
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.lang - the locale language to return
|
||||
* @return {Promise<String>} - the node html content
|
||||
* @memberof RED.nodes
|
||||
*/
|
||||
getNodeConfigs: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
runtime.log.audit({event: "nodes.configs.get"});
|
||||
return resolve(runtime.nodes.getNodeConfigs(opts.lang));
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the info of a node module
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.module - the id of the module to return
|
||||
* @return {Promise<ModuleInfo>} - the node module info
|
||||
* @memberof RED.nodes
|
||||
*/
|
||||
getModuleInfo: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
var result = runtime.nodes.getModuleInfo(opts.module);
|
||||
if (result) {
|
||||
runtime.log.audit({event: "nodes.module.get",id:opts.module});
|
||||
return resolve(result);
|
||||
} else {
|
||||
runtime.log.audit({event: "nodes.module.get",id:opts.module,error:"not_found"});
|
||||
var err = new Error();
|
||||
err.code = "not_found";
|
||||
err.status = 404;
|
||||
return reject(err);
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Install a new module into the runtime
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.module - the id of the module to install
|
||||
* @param {String} opts.version - (optional) the version of the module to install
|
||||
* @return {Promise<ModuleInfo>} - the node module info
|
||||
* @memberof RED.nodes
|
||||
*/
|
||||
addModule: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
if (!runtime.settings.available()) {
|
||||
runtime.log.audit({event: "nodes.install",error:"settings_unavailable"});
|
||||
var err = new Error("Settings unavailable");
|
||||
err.code = "settings_unavailable";
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
}
|
||||
if (opts.module) {
|
||||
var existingModule = runtime.nodes.getModuleInfo(opts.module);
|
||||
if (existingModule) {
|
||||
if (!opts.version || existingModule.version === opts.version) {
|
||||
runtime.log.audit({event: "nodes.install",module:opts.module, version:opts.version, error:"module_already_loaded"});
|
||||
var err = new Error("Module already loaded");
|
||||
err.code = "module_already_loaded";
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
}
|
||||
if (!existingModule.local) {
|
||||
runtime.log.audit({event: "nodes.install",module:opts.module, version:opts.version, error:"module_not_local"});
|
||||
var err = new Error("Module not locally installed");
|
||||
err.code = "module_not_local";
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
}
|
||||
}
|
||||
runtime.nodes.installModule(opts.module,opts.version).then(function(info) {
|
||||
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version});
|
||||
return resolve(info);
|
||||
}).catch(function(err) {
|
||||
if (err.code === 404) {
|
||||
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,error:"not_found"});
|
||||
// TODO: code/status
|
||||
err.status = 404;
|
||||
} else if (err.code) {
|
||||
err.status = 400;
|
||||
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,error:err.code});
|
||||
} else {
|
||||
err.status = 400;
|
||||
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,error:err.code||"unexpected_error",message:err.toString()});
|
||||
}
|
||||
return reject(err);
|
||||
})
|
||||
} else {
|
||||
runtime.log.audit({event: "nodes.install",module:opts.module,error:"invalid_request"});
|
||||
var err = new Error("Invalid request");
|
||||
err.code = "invalid_request";
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
});
|
||||
},
|
||||
/**
|
||||
* Removes a module from the runtime
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.module - the id of the module to remove
|
||||
* @return {Promise} - resolves when complete
|
||||
* @memberof RED.nodes
|
||||
*/
|
||||
removeModule: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
if (!runtime.settings.available()) {
|
||||
runtime.log.audit({event: "nodes.install",error:"settings_unavailable"});
|
||||
var err = new Error("Settings unavailable");
|
||||
err.code = "settings_unavailable";
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
}
|
||||
var module = runtime.nodes.getModuleInfo(opts.module);
|
||||
if (!module) {
|
||||
runtime.log.audit({event: "nodes.remove",module:opts.module,error:"not_found"});
|
||||
var err = new Error();
|
||||
err.code = "not_found";
|
||||
err.status = 404;
|
||||
return reject(err);
|
||||
}
|
||||
try {
|
||||
runtime.nodes.uninstallModule(opts.module).then(function() {
|
||||
runtime.log.audit({event: "nodes.remove",module:opts.module});
|
||||
resolve();
|
||||
}).catch(function(err) {
|
||||
err.status = 400;
|
||||
runtime.log.audit({event: "nodes.remove",module:opts.module,error:err.code||"unexpected_error",message:err.toString()});
|
||||
return reject(err);
|
||||
})
|
||||
} catch(error) {
|
||||
runtime.log.audit({event: "nodes.remove",module:opts.module,error:error.code||"unexpected_error",message:error.toString()});
|
||||
error.status = 400;
|
||||
return reject(error);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Enables or disables a module in the runtime
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.module - the id of the module to enable or disable
|
||||
* @param {String} opts.enabled - whether the module should be enabled or disabled
|
||||
* @return {Promise<ModuleInfo>} - the module info object
|
||||
* @memberof RED.nodes
|
||||
*/
|
||||
setModuleState: function(opts) {
|
||||
var mod = opts.module;
|
||||
return new Promise(function(resolve,reject) {
|
||||
if (!runtime.settings.available()) {
|
||||
runtime.log.audit({event: "nodes.module.set",error:"settings_unavailable"});
|
||||
var err = new Error("Settings unavailable");
|
||||
err.code = "settings_unavailable";
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
}
|
||||
try {
|
||||
var module = runtime.nodes.getModuleInfo(mod);
|
||||
if (!module) {
|
||||
runtime.log.audit({event: "nodes.module.set",module:mod,error:"not_found"});
|
||||
var err = new Error();
|
||||
err.code = "not_found";
|
||||
err.status = 404;
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
var nodes = module.nodes;
|
||||
var promises = [];
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
promises.push(putNode(nodes[i],opts.enabled));
|
||||
}
|
||||
Promise.all(promises).then(function() {
|
||||
return resolve(runtime.nodes.getModuleInfo(mod));
|
||||
}).catch(function(err) {
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
});
|
||||
} catch(error) {
|
||||
runtime.log.audit({event: "nodes.module.set",module:mod,enabled:opts.enabled,error:error.code||"unexpected_error",message:error.toString()});
|
||||
error.status = 400;
|
||||
return reject(error);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Enables or disables a n individual node-set in the runtime
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the node-set to enable or disable
|
||||
* @param {String} opts.enabled - whether the module should be enabled or disabled
|
||||
* @return {Promise<ModuleInfo>} - the module info object
|
||||
* @memberof RED.nodes
|
||||
*/
|
||||
setNodeSetState: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
if (!runtime.settings.available()) {
|
||||
runtime.log.audit({event: "nodes.info.set",error:"settings_unavailable"});
|
||||
var err = new Error("Settings unavailable");
|
||||
err.code = "settings_unavailable";
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
var id = opts.id;
|
||||
var enabled = opts.enabled;
|
||||
try {
|
||||
var node = runtime.nodes.getNodeInfo(id);
|
||||
if (!node) {
|
||||
runtime.log.audit({event: "nodes.info.set",id:id,error:"not_found"});
|
||||
var err = new Error();
|
||||
err.code = "not_found";
|
||||
err.status = 404;
|
||||
return reject(err);
|
||||
} else {
|
||||
delete node.loaded;
|
||||
putNode(node,enabled).then(function(result) {
|
||||
runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled});
|
||||
return resolve(result);
|
||||
}).catch(function(err) {
|
||||
runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled,error:err.code||"unexpected_error",message:err.toString()});
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
});
|
||||
}
|
||||
} catch(error) {
|
||||
runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled,error:error.code||"unexpected_error",message:error.toString()});
|
||||
error.status = 400;
|
||||
return reject(error);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets all registered module message catalogs
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {User} opts.lang - the i18n language to return. If not set, uses runtime default (en-US)
|
||||
* @return {Promise<Object>} - the message catalogs
|
||||
* @memberof RED.nodes
|
||||
*/
|
||||
getModuleCatalogs: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
var namespace = opts.module;
|
||||
var lang = opts.lang;
|
||||
var prevLang = runtime.i18n.i.language;
|
||||
// Trigger a load from disk of the language if it is not the default
|
||||
runtime.i18n.i.changeLanguage(lang, function(){
|
||||
var nodeList = runtime.nodes.getNodeList();
|
||||
var result = {};
|
||||
nodeList.forEach(function(n) {
|
||||
if (n.module !== "node-red") {
|
||||
result[n.id] = runtime.i18n.i.getResourceBundle(lang, n.id)||{};
|
||||
}
|
||||
});
|
||||
resolve(result);
|
||||
});
|
||||
runtime.i18n.i.changeLanguage(prevLang);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets a modules message catalog
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {User} opts.module - the module
|
||||
* @param {User} opts.lang - the i18n language to return. If not set, uses runtime default (en-US)
|
||||
* @return {Promise<Object>} - the message catalog
|
||||
* @memberof RED.nodes
|
||||
*/
|
||||
getModuleCatalog: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
var namespace = opts.module;
|
||||
var lang = opts.lang;
|
||||
var prevLang = runtime.i18n.i.lng();
|
||||
// Trigger a load from disk of the language if it is not the default
|
||||
runtime.i18n.i.changeLanguage(lang, function(){
|
||||
var catalog = runtime.i18n.getResourceBundle(lang, namespace);
|
||||
resolve(catalog||{});
|
||||
});
|
||||
runtime.i18n.i.changeLanguage(prevLang);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the list of all icons available in the modules installed within the runtime
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @return {Promise<IconList>} - the list of all icons
|
||||
* @memberof RED.nodes
|
||||
*/
|
||||
getIconList: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
runtime.log.audit({event: "nodes.icons.get"});
|
||||
return resolve(runtime.nodes.getNodeIcons());
|
||||
});
|
||||
|
||||
},
|
||||
/**
|
||||
* Gets a node icon
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.module - the id of the module requesting the icon
|
||||
* @param {String} opts.icon - the name of the icon
|
||||
* @return {Promise<Buffer>} - the icon file as a Buffer
|
||||
* @memberof RED.nodes
|
||||
*/
|
||||
getIcon: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
var iconPath = runtime.nodes.getNodeIconPath(opts.module,opts.icon);
|
||||
fs.readFile(iconPath,function(err,data) {
|
||||
if (err) {
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(data)
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,440 +0,0 @@
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
|
||||
/**
|
||||
* @namespace RED.projects
|
||||
*/
|
||||
|
||||
var runtime;
|
||||
|
||||
var api = module.exports = {
|
||||
init: function(_runtime) {
|
||||
runtime = _runtime;
|
||||
},
|
||||
available: function(opts) {
|
||||
return Promise.resolve(!!runtime.storage.projects);
|
||||
},
|
||||
|
||||
/**
|
||||
* List projects known to the runtime
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
listProjects: function(opts) {
|
||||
return runtime.storage.projects.listProjects(opts.user).then(function(list) {
|
||||
var active = runtime.storage.projects.getActiveProject(opts.user);
|
||||
var response = {
|
||||
projects: list
|
||||
};
|
||||
if (active) {
|
||||
response.active = active.name;
|
||||
}
|
||||
return response;
|
||||
}).catch(function(err) {
|
||||
err.status = 400;
|
||||
throw err;
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Create a new project
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {Object} opts.project - the project information
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
createProject: function(opts) {
|
||||
return runtime.storage.projects.createProject(opts.user, opts.project)
|
||||
},
|
||||
|
||||
/**
|
||||
* Initialises an empty project
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project to initialise
|
||||
* @param {Object} opts.project - the project information
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
initialiseProject: function(opts) {
|
||||
// Initialised set when creating default files for an empty repo
|
||||
return runtime.storage.projects.initialiseProject(opts.user, opts.id, opts.project)
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the active project
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @return {Promise<Object>} - the active project
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
getActiveProject: function(opts) {
|
||||
return Promise.resolve(runtime.storage.projects.getActiveProject(opts.user));
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project to activate
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
setActiveProject: function(opts) {
|
||||
var currentProject = runtime.storage.projects.getActiveProject(opts.user);
|
||||
if (!currentProject || opts.id !== currentProject.name) {
|
||||
return runtime.storage.projects.setActiveProject(opts.user, opts.id);
|
||||
} else {
|
||||
return Promise.resolve();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets a projects metadata
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project to get
|
||||
* @return {Promise<Object>} - the project metadata
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
getProject: function(opts) {
|
||||
return runtime.storage.projects.getProject(opts.user, opts.id)
|
||||
},
|
||||
|
||||
/**
|
||||
* Updates the metadata of an existing project
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project to update
|
||||
* @param {Object} opts.project - the project information
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
updateProject: function(opts) {
|
||||
return runtime.storage.projects.updateProject(opts.user, opts.id, opts.project);
|
||||
},
|
||||
|
||||
/**
|
||||
* Deletes a project
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project to update
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
deleteProject: function(opts) {
|
||||
return runtime.storage.projects.deleteProject(opts.user, opts.id);
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets current git status of a project
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {Boolean} opts.remote - whether to include status of remote repos
|
||||
* @return {Promise<Object>} - the project status
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
getStatus: function(opts) {
|
||||
return runtime.storage.projects.getStatus(opts.user, opts.id, opts.remote)
|
||||
},
|
||||
|
||||
/**
|
||||
* Get a list of local branches
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {Boolean} opts.remote - whether to return remote branches (true) or local (false)
|
||||
* @return {Promise<Object>} - a list of the local branches
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
getBranches: function(opts) {
|
||||
return runtime.storage.projects.getBranches(opts.user, opts.id, opts.remote);
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the status of a branch
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {String} opts.branch - the name of the branch
|
||||
* @return {Promise<Object>} - the status of the branch
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
getBranchStatus: function(opts) {
|
||||
return runtime.storage.projects.getBranchStatus(opts.user, opts.id, opts.branch);
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the current local branch
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {String} opts.branch - the name of the branch
|
||||
* @param {Boolean} opts.create - whether to create the branch if it doesn't exist
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
setBranch: function(opts) {
|
||||
return runtime.storage.projects.setBranch(opts.user, opts.id, opts.branch, opts.create)
|
||||
},
|
||||
|
||||
/**
|
||||
* Deletes a branch
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {String} opts.branch - the name of the branch
|
||||
* @param {Boolean} opts.force - whether to force delete
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
deleteBranch: function(opts) {
|
||||
return runtime.storage.projects.deleteBranch(opts.user, opts.id, opts.branch, false, opts.force);
|
||||
},
|
||||
|
||||
/**
|
||||
* Commits the current staged files
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {String} opts.message - the message to associate with the commit
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
commit: function(opts) {
|
||||
return runtime.storage.projects.commit(opts.user, opts.id,{message: opts.message});
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the details of a single commit
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {String} opts.sha - the sha of the commit to return
|
||||
* @return {Promise<Object>} - the commit details
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
getCommit: function(opts) {
|
||||
return runtime.storage.projects.getCommit(opts.user, opts.id, opts.sha);
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the commit history of the project
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {String} opts.limit - limit how many to return
|
||||
* @param {String} opts.before - id of the commit to work back from
|
||||
* @return {Promise<Array>} - an array of commits
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
getCommits: function(opts) {
|
||||
return runtime.storage.projects.getCommits(opts.user, opts.id, {
|
||||
limit: opts.limit || 20,
|
||||
before: opts.before
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Abort an in-progress merge
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
abortMerge: function(opts) {
|
||||
return runtime.storage.projects.abortMerge(opts.user, opts.id);
|
||||
},
|
||||
|
||||
/**
|
||||
* Resolves a merge conflict
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {String} opts.path - the path of the file being merged
|
||||
* @param {String} opts.resolutions - how to resolve the merge conflict
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
resolveMerge: function(opts) {
|
||||
return runtime.storage.projects.resolveMerge(opts.user, opts.id, opts.path, opts.resolution);
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets a listing of the files in the project
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @return {Promise<Object>} - the file listing
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
getFiles: function(opts) {
|
||||
return runtime.storage.projects.getFiles(opts.user, opts.id);
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the contents of a file
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {String} opts.path - the path of the file
|
||||
* @param {String} opts.tree - the version control tree to use
|
||||
* @return {Promise<String>} - the content of the file
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
getFile: function(opts) {
|
||||
return runtime.storage.projects.getFile(opts.user, opts.id,opts.path,opts.tree);
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {String|Array} opts.path - the path of the file, or an array of paths
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
stageFile: function(opts) {
|
||||
return runtime.storage.projects.stageFile(opts.user, opts.id, opts.path);
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {String} opts.path - the path of the file. If not set, all staged files are unstaged
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
unstageFile: function(opts) {
|
||||
return runtime.storage.projects.unstageFile(opts.user, opts.id, opts.path);
|
||||
},
|
||||
|
||||
/**
|
||||
* Reverts changes to a file back to its commited version
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {String} opts.path - the path of the file
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
revertFile: function(opts) {
|
||||
return runtime.storage.projects.revertFile(opts.user, opts.id,opts.path)
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the diff of a file
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {String} opts.path - the path of the file
|
||||
* @param {String} opts.type - the type of diff
|
||||
* @return {Promise<Object>} - the requested diff
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
getFileDiff: function(opts) {
|
||||
return runtime.storage.projects.getFileDiff(opts.user, opts.id, opts.path, opts.type);
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets a list of the project remotes
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @return {Promise<Object>} - a list of project remotes
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
getRemotes: function(opts) {
|
||||
return runtime.storage.projects.getRemotes(opts.user, opts.id);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {Object} opts.remote - the remote metadata
|
||||
* @param {String} opts.remote.name - the name of the remote
|
||||
* @param {String} opts.remote.url - the url of the remote
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
addRemote: function(opts) {
|
||||
return runtime.storage.projects.addRemote(opts.user, opts.id, opts.remote)
|
||||
},
|
||||
|
||||
/**
|
||||
* Remove a project remote
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {String} opts.remote - the name of the remote
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
removeRemote: function(opts) {
|
||||
return runtime.storage.projects.removeRemote(opts.user, opts.id, opts.remote);
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {Object} opts.remote - the remote metadata
|
||||
* @param {String} opts.remote.name - the name of the remote
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
updateRemote: function(opts) {
|
||||
return runtime.storage.projects.updateRemote(opts.user, opts.id, opts.remote.name, opts.remote)
|
||||
},
|
||||
|
||||
/**
|
||||
* Pull changes from the remote
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
pull: function(opts) {
|
||||
return runtime.storage.projects.pull(opts.user, opts.id, opts.remote, opts.track, opts.allowUnrelatedHistories);
|
||||
},
|
||||
|
||||
/**
|
||||
* Push changes to a remote
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {String} opts.id - the id of the project
|
||||
* @param {String} opts.remote - the name of the remote
|
||||
* @param {String} opts.track - whether to set the remote as the upstream
|
||||
* @return {Promise<Object>} - resolves when complete
|
||||
* @memberof RED.projects
|
||||
*/
|
||||
push: function(opts) {
|
||||
return runtime.storage.projects.push(opts.user, opts.id, opts.remote, opts.track);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,267 +0,0 @@
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
|
||||
/**
|
||||
* @namespace RED.settings
|
||||
*/
|
||||
|
||||
var util = require("util");
|
||||
var runtime;
|
||||
|
||||
function extend(target, source) {
|
||||
var keys = Object.keys(source);
|
||||
var i = keys.length;
|
||||
while(i--) {
|
||||
var value = source[keys[i]]
|
||||
var type = typeof value;
|
||||
if (type === 'string' || type === 'number' || type === 'boolean' || Array.isArray(value)) {
|
||||
target[keys[i]] = value;
|
||||
} else if (value === null) {
|
||||
if (target.hasOwnProperty(keys[i])) {
|
||||
delete target[keys[i]];
|
||||
}
|
||||
} else {
|
||||
// Object
|
||||
if (target.hasOwnProperty(keys[i])) {
|
||||
target[keys[i]] = extend(target[keys[i]],value);
|
||||
} else {
|
||||
target[keys[i]] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
function getSSHKeyUsername(userObj) {
|
||||
var username = '__default';
|
||||
if ( userObj && userObj.name ) {
|
||||
username = userObj.name;
|
||||
}
|
||||
return username;
|
||||
}
|
||||
var api = module.exports = {
|
||||
init: function(_runtime) {
|
||||
runtime = _runtime;
|
||||
},
|
||||
/**
|
||||
* Gets the runtime settings object
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @return {Promise<Object>} - the runtime settings
|
||||
* @memberof RED.settings
|
||||
*/
|
||||
getRuntimeSettings: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
try {
|
||||
var safeSettings = {
|
||||
httpNodeRoot: runtime.settings.httpNodeRoot||"/",
|
||||
version: runtime.settings.version
|
||||
}
|
||||
if (opts.user) {
|
||||
safeSettings.user = {}
|
||||
var props = ["anonymous","username","image","permissions"];
|
||||
props.forEach(prop => {
|
||||
if (opts.user.hasOwnProperty(prop)) {
|
||||
safeSettings.user[prop] = opts.user[prop];
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
safeSettings.context = runtime.nodes.listContextStores();
|
||||
|
||||
if (util.isArray(runtime.settings.paletteCategories)) {
|
||||
safeSettings.paletteCategories = runtime.settings.paletteCategories;
|
||||
}
|
||||
|
||||
if (runtime.settings.flowFilePretty) {
|
||||
safeSettings.flowFilePretty = runtime.settings.flowFilePretty;
|
||||
}
|
||||
|
||||
if (!runtime.nodes.paletteEditorEnabled()) {
|
||||
safeSettings.editorTheme = safeSettings.editorTheme || {};
|
||||
safeSettings.editorTheme.palette = safeSettings.editorTheme.palette || {};
|
||||
safeSettings.editorTheme.palette.editable = false;
|
||||
}
|
||||
if (runtime.storage.projects) {
|
||||
var activeProject = runtime.storage.projects.getActiveProject();
|
||||
if (activeProject) {
|
||||
safeSettings.project = activeProject;
|
||||
} else if (runtime.storage.projects.flowFileExists()) {
|
||||
safeSettings.files = {
|
||||
flow: runtime.storage.projects.getFlowFilename(),
|
||||
credentials: runtime.storage.projects.getCredentialsFilename()
|
||||
}
|
||||
}
|
||||
safeSettings.git = {
|
||||
globalUser: runtime.storage.projects.getGlobalGitUser()
|
||||
}
|
||||
}
|
||||
|
||||
safeSettings.flowEncryptionType = runtime.nodes.getCredentialKeyType();
|
||||
|
||||
runtime.settings.exportNodeSettings(safeSettings);
|
||||
|
||||
resolve(safeSettings);
|
||||
}catch(err) {
|
||||
console.log(err);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets an individual user's settings object
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @return {Promise<Object>} - the user settings
|
||||
* @memberof RED.settings
|
||||
*/
|
||||
getUserSettings: function(opts) {
|
||||
var username;
|
||||
if (!opts.user || opts.user.anonymous) {
|
||||
username = '_';
|
||||
} else {
|
||||
username = opts.user.username;
|
||||
}
|
||||
return Promise.resolve(runtime.settings.getUserSettings(username)||{});
|
||||
},
|
||||
|
||||
/**
|
||||
* Updates an individual user's settings object.
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {Object} opts.settings - the updates to the user settings
|
||||
* @return {Promise<Object>} - the user settings
|
||||
* @memberof RED.settings
|
||||
*/
|
||||
updateUserSettings: function(opts) {
|
||||
var username;
|
||||
if (!opts.user || opts.user.anonymous) {
|
||||
username = '_';
|
||||
} else {
|
||||
username = opts.user.username;
|
||||
}
|
||||
return new Promise(function(resolve,reject) {
|
||||
var currentSettings = runtime.settings.getUserSettings(username)||{};
|
||||
currentSettings = extend(currentSettings, opts.settings);
|
||||
try {
|
||||
runtime.settings.setUserSettings(username, currentSettings).then(function() {
|
||||
runtime.log.audit({event: "settings.update",username:username});
|
||||
return resolve();
|
||||
}).catch(function(err) {
|
||||
runtime.log.audit({event: "settings.update",username:username,error:err.code||"unexpected_error",message:err.toString()});
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
});
|
||||
} catch(err) {
|
||||
log.warn(log._("settings.user-not-available",{message:log._("settings.not-available")}));
|
||||
log.audit({event: "settings.update",username:username,error:err.code||"unexpected_error",message:err.toString()});
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets a list of a user's ssh keys
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @return {Promise<Object>} - the user's ssh keys
|
||||
* @memberof RED.settings
|
||||
*/
|
||||
getUserKeys: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
var username = getSSHKeyUsername(opts.user);
|
||||
runtime.storage.projects.ssh.listSSHKeys(username).then(function(list) {
|
||||
return resolve(list);
|
||||
}).catch(function(err) {
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets a user's ssh public key
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {User} opts.id - the id of the key to return
|
||||
* @return {Promise<String>} - the user's ssh public key
|
||||
* @memberof RED.settings
|
||||
*/
|
||||
getUserKey: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
var username = getSSHKeyUsername(opts.user);
|
||||
// console.log('username:', username);
|
||||
runtime.storage.projects.ssh.getSSHKey(username, opts.id).then(function(data) {
|
||||
if (data) {
|
||||
return resolve(data);
|
||||
} else {
|
||||
var err = new Error("Key not found");
|
||||
err.code = "not_found";
|
||||
err.status = 404;
|
||||
return reject(err);
|
||||
}
|
||||
}).catch(function(err) {
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Generates a new ssh key pair
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {User} opts.name - the id of the key to return
|
||||
* @param {User} opts.password - (optional) the password for the key pair
|
||||
* @param {User} opts.comment - (option) a comment to associate with the key pair
|
||||
* @param {User} opts.size - (optional) the size of the key. Default: 2048
|
||||
* @return {Promise<String>} - the id of the generated key
|
||||
* @memberof RED.settings
|
||||
*/
|
||||
generateUserKey: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
var username = getSSHKeyUsername(opts.user);
|
||||
runtime.storage.projects.ssh.generateSSHKey(username, opts).then(function(name) {
|
||||
return resolve(name);
|
||||
}).catch(function(err) {
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Deletes a user's ssh key pair
|
||||
* @param {Object} opts
|
||||
* @param {User} opts.user - the user calling the api
|
||||
* @param {User} opts.id - the id of the key to delete
|
||||
* @return {Promise} - resolves when deleted
|
||||
* @memberof RED.settings
|
||||
*/
|
||||
removeUserKey: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
var username = getSSHKeyUsername(req.user);
|
||||
runtime.storage.projects.ssh.deleteSSHKey(username, opts.id).then(function() {
|
||||
return resolve();
|
||||
}).catch(function(err) {
|
||||
err.status = 400;
|
||||
return reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,6 @@ var defaultLang = "en-US";
|
||||
|
||||
var resourceMap = {};
|
||||
var resourceCache = {};
|
||||
var initPromise;
|
||||
|
||||
function registerMessageCatalogs(catalogs) {
|
||||
var promises = catalogs.map(function(catalog) {
|
||||
@@ -34,12 +33,10 @@ function registerMessageCatalogs(catalogs) {
|
||||
}
|
||||
|
||||
function registerMessageCatalog(namespace,dir,file) {
|
||||
return initPromise.then(function() {
|
||||
return new Promise((resolve,reject) => {
|
||||
resourceMap[namespace] = { basedir:dir, file:file};
|
||||
i18n.loadNamespaces(namespace,function() {
|
||||
resolve();
|
||||
});
|
||||
return when.promise(function(resolve,reject) {
|
||||
resourceMap[namespace] = { basedir:dir, file:file};
|
||||
i18n.loadNamespaces(namespace,function() {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -99,32 +96,28 @@ function getCurrentLocale() {
|
||||
}
|
||||
|
||||
function init() {
|
||||
if (!initPromise) {
|
||||
// Keep this as a 'when' promise as top-level red.js uses 'otherwise'
|
||||
// and embedded users of NR may have copied that.
|
||||
initPromise = when.promise((resolve,reject) => {
|
||||
i18n.use(MessageFileLoader);
|
||||
var opt = {
|
||||
// debug: true,
|
||||
defaultNS: "runtime",
|
||||
ns: [],
|
||||
fallbackLng: defaultLang,
|
||||
interpolation: {
|
||||
unescapeSuffix: 'HTML',
|
||||
escapeValue: false,
|
||||
prefix: '__',
|
||||
suffix: '__'
|
||||
}
|
||||
};
|
||||
var lang = getCurrentLocale();
|
||||
if (lang) {
|
||||
opt.lng = lang;
|
||||
return when.promise(function(resolve,reject) {
|
||||
i18n.use(MessageFileLoader);
|
||||
var opt = {
|
||||
// debug: true,
|
||||
defaultNS: "runtime",
|
||||
ns: [],
|
||||
fallbackLng: defaultLang,
|
||||
interpolation: {
|
||||
unescapeSuffix: 'HTML',
|
||||
escapeValue: false,
|
||||
prefix: '__',
|
||||
suffix: '__'
|
||||
}
|
||||
i18n.init(opt ,function() {
|
||||
resolve();
|
||||
});
|
||||
};
|
||||
var lang = getCurrentLocale();
|
||||
if (lang) {
|
||||
opt.lng = lang;
|
||||
}
|
||||
i18n.init(opt ,function() {
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getCatalog(namespace,lang) {
|
||||
@@ -18,7 +18,8 @@ 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");
|
||||
|
||||
@@ -27,10 +28,6 @@ var path = require('path');
|
||||
var fs = require("fs");
|
||||
var os = require("os");
|
||||
|
||||
var redUtil;
|
||||
var log;
|
||||
var i18n;
|
||||
|
||||
var runtimeMetricInterval = null;
|
||||
|
||||
var started = false;
|
||||
@@ -42,6 +39,9 @@ var stubbedExpressApp = {
|
||||
delete: function() {}
|
||||
}
|
||||
var adminApi = {
|
||||
library: {
|
||||
register: function() {}
|
||||
},
|
||||
auth: {
|
||||
needsPermission: function() {}
|
||||
},
|
||||
@@ -54,12 +54,9 @@ var adminApi = {
|
||||
|
||||
var nodeApp;
|
||||
|
||||
function init(userSettings,_redUtil,_adminApi) {
|
||||
redUtil = _redUtil;
|
||||
log = redUtil.log;
|
||||
i18n = redUtil.i18n;
|
||||
|
||||
function init(userSettings,_adminApi) {
|
||||
userSettings.version = getVersion();
|
||||
log.init(userSettings);
|
||||
settings.init(userSettings);
|
||||
|
||||
nodeApp = express();
|
||||
@@ -68,7 +65,6 @@ function init(userSettings,_redUtil,_adminApi) {
|
||||
adminApi = _adminApi;
|
||||
}
|
||||
redNodes.init(runtime);
|
||||
library.init(runtime);
|
||||
}
|
||||
|
||||
var version;
|
||||
@@ -88,8 +84,10 @@ function getVersion() {
|
||||
}
|
||||
|
||||
function start() {
|
||||
|
||||
return i18n.registerMessageCatalog("runtime",path.resolve(path.join(__dirname,"locales")),"runtime.json")
|
||||
return i18n.init()
|
||||
.then(function() {
|
||||
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() {
|
||||
@@ -164,12 +162,10 @@ function start() {
|
||||
if (settings.httpStatic) {
|
||||
log.info(log._("runtime.paths.httpStatic",{path:path.resolve(settings.httpStatic)}));
|
||||
}
|
||||
redNodes.loadContextsPlugin().then(function () {
|
||||
return redNodes.loadContextsPlugin().then(function () {
|
||||
redNodes.loadFlows().then(redNodes.startFlows).catch(function(err) {});
|
||||
started = true;
|
||||
});
|
||||
}).catch(function(err) {
|
||||
console.log(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -245,13 +241,12 @@ var runtime = module.exports = {
|
||||
|
||||
version: getVersion,
|
||||
|
||||
get log() { return log },
|
||||
get i18n() { return i18n },
|
||||
log: log,
|
||||
i18n: i18n,
|
||||
settings: settings,
|
||||
storage: storage,
|
||||
events: events,
|
||||
nodes: redNodes,
|
||||
library: library,
|
||||
util: require("./util"),
|
||||
get adminApi() { return adminApi },
|
||||
get nodeApp() { return nodeApp },
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
/**
|
||||
* 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 fspath = require('path');
|
||||
|
||||
var runtime;
|
||||
var knownTypes = {};
|
||||
|
||||
var storage;
|
||||
|
||||
function init(_runtime) {
|
||||
runtime = _runtime;
|
||||
storage = runtime.storage;
|
||||
knownTypes = {};
|
||||
}
|
||||
|
||||
function registerType(id,type) {
|
||||
// TODO: would like to enforce this, but currently the tests register the same type multiple
|
||||
// times and have no way to remove themselves.
|
||||
// if (knownTypes.hasOwnProperty(type)) {
|
||||
// throw new Error(`Library type '${type}' already registered by ${id}'`)
|
||||
// }
|
||||
knownTypes[type] = id;
|
||||
}
|
||||
|
||||
// function getAllEntries(type) {
|
||||
// if (!knownTypes.hasOwnProperty(type)) {
|
||||
// throw new Error(`Unknown library type '${type}'`);
|
||||
// }
|
||||
// }
|
||||
|
||||
function getEntry(type,path) {
|
||||
if (type !== 'flows') {
|
||||
if (!knownTypes.hasOwnProperty(type)) {
|
||||
throw new Error(`Unknown library type '${type}'`);
|
||||
}
|
||||
return storage.getLibraryEntry(type,path);
|
||||
} else {
|
||||
return new Promise(function(resolve,reject) {
|
||||
if (path.indexOf("_examples_/") === 0) {
|
||||
var m = /^_examples_\/(@.*?\/[^\/]+|[^\/]+)\/(.*)$/.exec(path);
|
||||
if (m) {
|
||||
var module = m[1];
|
||||
var entryPath = m[2];
|
||||
var fullPath = runtime.nodes.getNodeExampleFlowPath(module,entryPath);
|
||||
if (fullPath) {
|
||||
try {
|
||||
fs.readFile(fullPath,'utf8',function(err, data) {
|
||||
runtime.log.audit({event: "library.get",type:"flow",path:path});
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(data);
|
||||
})
|
||||
} catch(err) {
|
||||
return reject(err);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
// IF we get here, we didn't find the file
|
||||
var error = new Error("not_found");
|
||||
error.code = "not_found";
|
||||
return reject(error);
|
||||
} else {
|
||||
resolve(storage.getFlow(path));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
function saveEntry(type,path,meta,body) {
|
||||
if (type !== 'flows') {
|
||||
if (!knownTypes.hasOwnProperty(type)) {
|
||||
throw new Error(`Unknown library type '${type}'`);
|
||||
}
|
||||
return storage.saveLibraryEntry(type,path,meta,body);
|
||||
} else {
|
||||
return storage.saveFlow(path,body);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: init,
|
||||
register: registerType,
|
||||
// getAllEntries: getAllEntries,
|
||||
getEntry: getEntry,
|
||||
saveEntry: saveEntry
|
||||
|
||||
}
|
||||
@@ -163,7 +163,12 @@
|
||||
"error-module-not-defined": "Context store '__storage__' missing 'module' option",
|
||||
"error-invalid-module-name": "Invalid context store name: '__name__'",
|
||||
"error-invalid-default-module": "Default context store unknown: '__storage__'",
|
||||
"unknown-store": "Unknown context store '__name__' specified. Using default store."
|
||||
"unknown-store": "Unknown context store '__name__' specified. Using default store.",
|
||||
"error-loading-module": "Error loading context store: __message__",
|
||||
"localfilesystem": {
|
||||
"error-circular": "Context __scope__ contains a circular reference that cannot be persisted",
|
||||
"error-write": "Error writing context: __message__"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ var EventEmitter = require("events").EventEmitter;
|
||||
var when = require("when");
|
||||
|
||||
var redUtil = require("../util");
|
||||
var Log = require("../../util").log; // TODO: separate module
|
||||
var Log = require("../log");
|
||||
var context = require("./context");
|
||||
var flows = require("./flows");
|
||||
|
||||
|
||||
@@ -15,8 +15,9 @@
|
||||
**/
|
||||
|
||||
var clone = require("clone");
|
||||
var log = require("../../../util").log; // TODO: separate module
|
||||
var log = require("../../log");
|
||||
var memory = require("./memory");
|
||||
var util = require("../../util");
|
||||
|
||||
var settings;
|
||||
|
||||
@@ -112,7 +113,15 @@ function load() {
|
||||
try {
|
||||
// Create a new instance of the plugin by calling its module function
|
||||
stores[pluginName] = plugin(config);
|
||||
log.info(log._("context.log-store-init", {name:pluginName, info:"module="+plugins[pluginName].module}));
|
||||
var moduleInfo = plugins[pluginName].module;
|
||||
if (typeof moduleInfo !== 'string') {
|
||||
if (moduleInfo.hasOwnProperty("toString")) {
|
||||
moduleInfo = moduleInfo.toString();
|
||||
} else {
|
||||
moduleInfo = "custom";
|
||||
}
|
||||
}
|
||||
log.info(log._("context.log-store-init", {name:pluginName, info:"module="+moduleInfo}));
|
||||
} catch(err) {
|
||||
return reject(new Error(log._("context.error-loading-module",{module:pluginName,message:err.toString()})));
|
||||
}
|
||||
@@ -162,6 +171,8 @@ function load() {
|
||||
defaultStore = "memory";
|
||||
}
|
||||
return resolve(Promise.all(promises));
|
||||
}).catch(function(err) {
|
||||
throw new Error(log._("context.error-loading-module",{message:err.toString()}));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -199,104 +210,131 @@ function createContext(id,seed) {
|
||||
insertSeedValues = function(keys,values) {
|
||||
if (!Array.isArray(keys)) {
|
||||
if (values[0] === undefined) {
|
||||
values[0] = seed[keys];
|
||||
values[0] = util.getObjectProperty(seed,keys);
|
||||
}
|
||||
} else {
|
||||
for (var i=0;i<keys.length;i++) {
|
||||
if (values[i] === undefined) {
|
||||
values[i] = seed[keys[i]];
|
||||
values[i] = util.getObjectProperty(seed,keys[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Object.defineProperties(obj, {
|
||||
get: {
|
||||
value: function(key, storage, callback) {
|
||||
var context;
|
||||
|
||||
obj.get = function(key, storage, callback) {
|
||||
var context;
|
||||
if (!storage && !callback) {
|
||||
context = stores["_"];
|
||||
} else {
|
||||
if (typeof storage === 'function') {
|
||||
callback = storage;
|
||||
storage = "_";
|
||||
}
|
||||
if (callback && typeof callback !== 'function'){
|
||||
throw new Error("Callback must be a function");
|
||||
}
|
||||
context = getContextStorage(storage);
|
||||
}
|
||||
if (callback) {
|
||||
if (!seed) {
|
||||
context.get(scope,key,callback);
|
||||
} else {
|
||||
context.get(scope,key,function() {
|
||||
if (arguments[0]) {
|
||||
callback(arguments[0]);
|
||||
return;
|
||||
if (!callback && typeof storage === 'function') {
|
||||
callback = storage;
|
||||
storage = undefined;
|
||||
}
|
||||
if (callback && typeof callback !== 'function'){
|
||||
throw new Error("Callback must be a function");
|
||||
}
|
||||
|
||||
if (!Array.isArray(key)) {
|
||||
var keyParts = util.parseContextStore(key);
|
||||
key = keyParts.key;
|
||||
if (!storage) {
|
||||
storage = keyParts.store || "_";
|
||||
}
|
||||
var results = Array.prototype.slice.call(arguments,[1]);
|
||||
insertSeedValues(key,results);
|
||||
// Put the err arg back
|
||||
results.unshift(undefined);
|
||||
callback.apply(null,results);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// No callback, attempt to do this synchronously
|
||||
var results = context.get(scope,key);
|
||||
if (seed) {
|
||||
if (Array.isArray(key)) {
|
||||
insertSeedValues(key,results);
|
||||
} else if (results === undefined){
|
||||
results = seed[key];
|
||||
} else {
|
||||
if (!storage) {
|
||||
storage = "_";
|
||||
}
|
||||
}
|
||||
context = getContextStorage(storage);
|
||||
|
||||
if (callback) {
|
||||
if (!seed) {
|
||||
context.get(scope,key,callback);
|
||||
} else {
|
||||
context.get(scope,key,function() {
|
||||
if (arguments[0]) {
|
||||
callback(arguments[0]);
|
||||
return;
|
||||
}
|
||||
var results = Array.prototype.slice.call(arguments,[1]);
|
||||
insertSeedValues(key,results);
|
||||
// Put the err arg back
|
||||
results.unshift(undefined);
|
||||
callback.apply(null,results);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// No callback, attempt to do this synchronously
|
||||
var results = context.get(scope,key);
|
||||
if (seed) {
|
||||
if (Array.isArray(key)) {
|
||||
insertSeedValues(key,results);
|
||||
} else if (results === undefined){
|
||||
results = util.getObjectProperty(seed,key);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
},
|
||||
set: {
|
||||
value: function(key, value, storage, callback) {
|
||||
var context;
|
||||
|
||||
if (!callback && typeof storage === 'function') {
|
||||
callback = storage;
|
||||
storage = undefined;
|
||||
}
|
||||
if (callback && typeof callback !== 'function'){
|
||||
throw new Error("Callback must be a function");
|
||||
}
|
||||
|
||||
if (!Array.isArray(key)) {
|
||||
var keyParts = util.parseContextStore(key);
|
||||
key = keyParts.key;
|
||||
if (!storage) {
|
||||
storage = keyParts.store || "_";
|
||||
}
|
||||
} else {
|
||||
if (!storage) {
|
||||
storage = "_";
|
||||
}
|
||||
}
|
||||
context = getContextStorage(storage);
|
||||
|
||||
context.set(scope, key, value, callback);
|
||||
}
|
||||
},
|
||||
keys: {
|
||||
value: function(storage, callback) {
|
||||
var context;
|
||||
if (!storage && !callback) {
|
||||
context = stores["_"];
|
||||
} else {
|
||||
if (typeof storage === 'function') {
|
||||
callback = storage;
|
||||
storage = "_";
|
||||
}
|
||||
if (callback && typeof callback !== 'function') {
|
||||
throw new Error("Callback must be a function");
|
||||
}
|
||||
context = getContextStorage(storage);
|
||||
}
|
||||
if (seed) {
|
||||
if (callback) {
|
||||
context.keys(scope, function(err,keys) {
|
||||
callback(err,Array.from(new Set(seedKeys.concat(keys)).keys()));
|
||||
});
|
||||
} else {
|
||||
var keys = context.keys(scope);
|
||||
return Array.from(new Set(seedKeys.concat(keys)).keys())
|
||||
}
|
||||
} else {
|
||||
return context.keys(scope, callback);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
};
|
||||
obj.set = function(key, value, storage, callback) {
|
||||
var context;
|
||||
if (!storage && !callback) {
|
||||
context = stores["_"];
|
||||
} else {
|
||||
if (typeof storage === 'function') {
|
||||
callback = storage;
|
||||
storage = "_";
|
||||
}
|
||||
if (callback && typeof callback !== 'function') {
|
||||
throw new Error("Callback must be a function");
|
||||
}
|
||||
context = getContextStorage(storage);
|
||||
}
|
||||
context.set(scope, key, value, callback);
|
||||
};
|
||||
obj.keys = function(storage, callback) {
|
||||
var context;
|
||||
if (!storage && !callback) {
|
||||
context = stores["_"];
|
||||
} else {
|
||||
if (typeof storage === 'function') {
|
||||
callback = storage;
|
||||
storage = "_";
|
||||
}
|
||||
if (callback && typeof callback !== 'function') {
|
||||
throw new Error("Callback must be a function");
|
||||
}
|
||||
context = getContextStorage(storage);
|
||||
}
|
||||
if (seed) {
|
||||
if (callback) {
|
||||
context.keys(scope, function(err,keys) {
|
||||
callback(err,Array.from(new Set(seedKeys.concat(keys)).keys()));
|
||||
});
|
||||
} else {
|
||||
var keys = context.keys(scope);
|
||||
return Array.from(new Set(seedKeys.concat(keys)).keys())
|
||||
}
|
||||
} else {
|
||||
return context.keys(scope, callback);
|
||||
}
|
||||
};
|
||||
});
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -310,9 +348,13 @@ function getContext(localId,flowId) {
|
||||
}
|
||||
var newContext = createContext(contextId);
|
||||
if (flowId) {
|
||||
newContext.flow = getContext(flowId);
|
||||
Object.defineProperty(newContext, 'flow', {
|
||||
value: getContext(flowId)
|
||||
});
|
||||
}
|
||||
newContext.global = contexts['global'];
|
||||
Object.defineProperty(newContext, 'global', {
|
||||
value: contexts['global']
|
||||
})
|
||||
contexts[contextId] = newContext;
|
||||
return newContext;
|
||||
}
|
||||
|
||||
@@ -19,12 +19,16 @@
|
||||
*
|
||||
* Configuration options:
|
||||
* {
|
||||
* base: "contexts", // the base directory to use
|
||||
* // default: "contexts"
|
||||
* base: "context", // the base directory to use
|
||||
* // default: "context"
|
||||
* dir: "/path/to/storage", // the directory to create the base directory in
|
||||
* // default: settings.userDir
|
||||
* cache: true // whether to cache contents in memory
|
||||
* cache: true, // whether to cache contents in memory
|
||||
* // default: true
|
||||
* flushInterval: 30 // if cache is enabled, the minimum interval
|
||||
* // between writes to storage, in seconds. This
|
||||
* can be used to reduce wear on underlying storage.
|
||||
* default: 30 seconds
|
||||
* }
|
||||
*
|
||||
*
|
||||
@@ -44,9 +48,12 @@
|
||||
var fs = require('fs-extra');
|
||||
var path = require("path");
|
||||
var util = require("../../util");
|
||||
var log = require("../../log");
|
||||
|
||||
var safeJSONStringify = require("json-stringify-safe");
|
||||
var MemoryStore = require("./memory");
|
||||
|
||||
|
||||
function getStoragePath(storageBaseDir, scope) {
|
||||
if(scope.indexOf(":") === -1){
|
||||
if(scope === "global"){
|
||||
@@ -61,7 +68,7 @@ function getStoragePath(storageBaseDir, scope) {
|
||||
}
|
||||
|
||||
function getBasePath(config) {
|
||||
var base = config.base || "contexts";
|
||||
var base = config.base || "context";
|
||||
var storageBaseDir;
|
||||
if (!config.dir) {
|
||||
if(config.settings && config.settings.userDir){
|
||||
@@ -97,17 +104,54 @@ function loadFile(storagePath){
|
||||
}else{
|
||||
return Promise.resolve(undefined);
|
||||
}
|
||||
}).catch(function(err){
|
||||
throw Promise.reject(err);
|
||||
});
|
||||
}
|
||||
|
||||
function listFiles(storagePath) {
|
||||
var promises = [];
|
||||
return fs.readdir(storagePath).then(function(files) {
|
||||
files.forEach(function(file) {
|
||||
if (!/^\./.test(file)) {
|
||||
var fullPath = path.join(storagePath,file);
|
||||
var stats = fs.statSync(fullPath);
|
||||
if (stats.isDirectory()) {
|
||||
promises.push(fs.readdir(fullPath).then(function(subdirFiles) {
|
||||
var result = [];
|
||||
subdirFiles.forEach(subfile => {
|
||||
if (/\.json$/.test(subfile)) {
|
||||
result.push(path.join(file,subfile))
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}))
|
||||
}
|
||||
}
|
||||
});
|
||||
return Promise.all(promises);
|
||||
}).then(dirs => dirs.reduce((acc, val) => acc.concat(val), []));
|
||||
}
|
||||
|
||||
function stringify(value) {
|
||||
var hasCircular;
|
||||
var result = safeJSONStringify(value,null,4,function(k,v){hasCircular = true})
|
||||
return { json: result, circular: hasCircular };
|
||||
}
|
||||
|
||||
function LocalFileSystem(config){
|
||||
this.config = config;
|
||||
this.storageBaseDir = getBasePath(this.config);
|
||||
this.writePromise = Promise.resolve();
|
||||
if (config.hasOwnProperty('cache')?config.cache:true) {
|
||||
this.cache = MemoryStore({});
|
||||
}
|
||||
this.pendingWrites = {};
|
||||
this.knownCircularRefs = {};
|
||||
|
||||
if (config.hasOwnProperty('flushInterval')) {
|
||||
this.flushInterval = Math.max(0,config.flushInterval) * 1000;
|
||||
} else {
|
||||
this.flushInterval = 30000;
|
||||
}
|
||||
}
|
||||
|
||||
LocalFileSystem.prototype.open = function(){
|
||||
@@ -115,25 +159,17 @@ LocalFileSystem.prototype.open = function(){
|
||||
if (this.cache) {
|
||||
var scopes = [];
|
||||
var promises = [];
|
||||
var subdirs = [];
|
||||
var subdirPromises = [];
|
||||
return fs.readdir(self.storageBaseDir).then(function(dirs){
|
||||
dirs.forEach(function(fn) {
|
||||
var p = getStoragePath(self.storageBaseDir ,fn)+".json";
|
||||
scopes.push(fn);
|
||||
promises.push(loadFile(p));
|
||||
subdirs.push(path.join(self.storageBaseDir,fn));
|
||||
subdirPromises.push(fs.readdir(path.join(self.storageBaseDir,fn)));
|
||||
})
|
||||
return Promise.all(subdirPromises);
|
||||
}).then(function(dirs) {
|
||||
dirs.forEach(function(files,i) {
|
||||
files.forEach(function(fn) {
|
||||
if (fn !== 'flow.json' && fn !== 'global.json') {
|
||||
scopes.push(fn.substring(0,fn.length-5)+":"+scopes[i]);
|
||||
promises.push(loadFile(path.join(subdirs[i],fn)))
|
||||
}
|
||||
});
|
||||
return listFiles(self.storageBaseDir).then(function(files) {
|
||||
files.forEach(function(file) {
|
||||
var parts = file.split(path.sep);
|
||||
if (parts[0] === 'global') {
|
||||
scopes.push("global");
|
||||
} else if (parts[1] === 'flow.json') {
|
||||
scopes.push(parts[0])
|
||||
} else {
|
||||
scopes.push(parts[1].substring(0,parts[1].length-5)+":"+parts[0]);
|
||||
}
|
||||
promises.push(loadFile(path.join(self.storageBaseDir,file)));
|
||||
})
|
||||
return Promise.all(promises);
|
||||
}).then(function(res) {
|
||||
@@ -147,16 +183,44 @@ LocalFileSystem.prototype.open = function(){
|
||||
if(err.code == 'ENOENT') {
|
||||
return fs.ensureDir(self.storageBaseDir);
|
||||
}else{
|
||||
return Promise.reject(err);
|
||||
throw err;
|
||||
}
|
||||
}).then(function() {
|
||||
self._flushPendingWrites = function() {
|
||||
var scopes = Object.keys(self.pendingWrites);
|
||||
self.pendingWrites = {};
|
||||
var promises = [];
|
||||
var newContext = self.cache._export();
|
||||
scopes.forEach(function(scope) {
|
||||
var storagePath = getStoragePath(self.storageBaseDir,scope);
|
||||
var context = newContext[scope];
|
||||
var stringifiedContext = stringify(context);
|
||||
if (stringifiedContext.circular && !self.knownCircularRefs[scope]) {
|
||||
log.warn(log._("error-circular",{scope:scope}));
|
||||
self.knownCircularRefs[scope] = true;
|
||||
} else {
|
||||
delete self.knownCircularRefs[scope];
|
||||
}
|
||||
log.debug("Flushing localfilesystem context scope "+scope);
|
||||
promises.push(fs.outputFile(storagePath + ".json", stringifiedContext.json, "utf8"));
|
||||
});
|
||||
delete self._pendingWriteTimeout;
|
||||
return Promise.all(promises);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return Promise.resolve();
|
||||
return fs.ensureDir(self.storageBaseDir);
|
||||
}
|
||||
}
|
||||
|
||||
LocalFileSystem.prototype.close = function(){
|
||||
return Promise.resolve();
|
||||
var self = this;
|
||||
if (this.cache && this._pendingWriteTimeout) {
|
||||
clearTimeout(this._pendingWriteTimeout);
|
||||
delete this._pendingWriteTimeout;
|
||||
this.flushInterval = 0;
|
||||
}
|
||||
return this.writePromise;
|
||||
}
|
||||
|
||||
LocalFileSystem.prototype.get = function(scope, key, callback) {
|
||||
@@ -168,14 +232,31 @@ LocalFileSystem.prototype.get = function(scope, key, callback) {
|
||||
}
|
||||
var storagePath = getStoragePath(this.storageBaseDir ,scope);
|
||||
loadFile(storagePath + ".json").then(function(data){
|
||||
var value;
|
||||
if(data){
|
||||
data = JSON.parse(data);
|
||||
if (!Array.isArray(key)) {
|
||||
callback(null, util.getObjectProperty(data,key));
|
||||
try {
|
||||
value = util.getObjectProperty(data,key);
|
||||
} catch(err) {
|
||||
if (err.code === "INVALID_EXPR") {
|
||||
throw err;
|
||||
}
|
||||
value = undefined;
|
||||
}
|
||||
callback(null, value);
|
||||
} else {
|
||||
var results = [undefined];
|
||||
for (var i=0;i<key.length;i++) {
|
||||
results.push(util.getObjectProperty(data,key[i]))
|
||||
try {
|
||||
value = util.getObjectProperty(data,key[i]);
|
||||
} catch(err) {
|
||||
if (err.code === "INVALID_EXPR") {
|
||||
throw err;
|
||||
}
|
||||
value = undefined;
|
||||
}
|
||||
results.push(value)
|
||||
}
|
||||
callback.apply(null,results);
|
||||
}
|
||||
@@ -188,17 +269,27 @@ LocalFileSystem.prototype.get = function(scope, key, callback) {
|
||||
};
|
||||
|
||||
LocalFileSystem.prototype.set = function(scope, key, value, callback) {
|
||||
var self = this;
|
||||
var storagePath = getStoragePath(this.storageBaseDir ,scope);
|
||||
if (this.cache) {
|
||||
this.cache.set(scope,key,value,callback);
|
||||
// With cache enabled, no need to re-read the file prior to writing.
|
||||
var newContext = this.cache._export()[scope];
|
||||
fs.outputFile(storagePath + ".json", JSON.stringify(newContext, undefined, 4), "utf8").catch(function(err) {
|
||||
});
|
||||
this.pendingWrites[scope] = true;
|
||||
if (this._pendingWriteTimeout) {
|
||||
// there's a pending write which will handle this
|
||||
return;
|
||||
} else {
|
||||
this._pendingWriteTimeout = setTimeout(function() {
|
||||
self.writePromise = self.writePromise.then(function(){
|
||||
return self._flushPendingWrites.call(self).catch(function(err) {
|
||||
log.error(log._("context.localfilesystem.error-write",{message:err.toString()}));
|
||||
});
|
||||
});
|
||||
}, this.flushInterval);
|
||||
}
|
||||
} else if (callback && typeof callback !== 'function') {
|
||||
throw new Error("Callback must be a function");
|
||||
} else {
|
||||
loadFile(storagePath + ".json").then(function(data){
|
||||
self.writePromise = self.writePromise.then(function() { return loadFile(storagePath + ".json") }).then(function(data){
|
||||
var obj = data ? JSON.parse(data) : {}
|
||||
if (!Array.isArray(key)) {
|
||||
key = [key];
|
||||
@@ -214,7 +305,14 @@ LocalFileSystem.prototype.set = function(scope, key, value, callback) {
|
||||
}
|
||||
util.setObjectProperty(obj,key[i],v);
|
||||
}
|
||||
return fs.outputFile(storagePath + ".json", JSON.stringify(obj, undefined, 4), "utf8");
|
||||
var stringifiedContext = stringify(obj);
|
||||
if (stringifiedContext.circular && !self.knownCircularRefs[scope]) {
|
||||
log.warn(log._("error-circular",{scope:scope}));
|
||||
self.knownCircularRefs[scope] = true;
|
||||
} else {
|
||||
delete self.knownCircularRefs[scope];
|
||||
}
|
||||
return fs.outputFile(storagePath + ".json", stringifiedContext.json, "utf8");
|
||||
}).then(function(){
|
||||
if(typeof callback === "function"){
|
||||
callback(null);
|
||||
@@ -254,36 +352,45 @@ LocalFileSystem.prototype.delete = function(scope){
|
||||
cachePromise = Promise.resolve();
|
||||
}
|
||||
var that = this;
|
||||
delete this.pendingWrites[scope];
|
||||
return cachePromise.then(function() {
|
||||
var storagePath = getStoragePath(that.storageBaseDir,scope);
|
||||
return fs.remove(storagePath + ".json");
|
||||
});
|
||||
}
|
||||
|
||||
LocalFileSystem.prototype.clean = function(activeNodes){
|
||||
LocalFileSystem.prototype.clean = function(_activeNodes) {
|
||||
var activeNodes = {};
|
||||
_activeNodes.forEach(function(node) { activeNodes[node] = true });
|
||||
var self = this;
|
||||
var cachePromise;
|
||||
if (this.cache) {
|
||||
cachePromise = this.cache.clean(activeNodes);
|
||||
cachePromise = this.cache.clean(_activeNodes);
|
||||
} else {
|
||||
cachePromise = Promise.resolve();
|
||||
}
|
||||
return cachePromise.then(function() {
|
||||
return fs.readdir(self.storageBaseDir).then(function(dirs){
|
||||
return Promise.all(dirs.reduce(function(result, item){
|
||||
if(item !== "global" && activeNodes.indexOf(item) === -1){
|
||||
result.push(fs.remove(path.join(self.storageBaseDir,item)));
|
||||
}
|
||||
return result;
|
||||
},[]));
|
||||
}).catch(function(err){
|
||||
if(err.code == 'ENOENT') {
|
||||
return Promise.resolve();
|
||||
}else{
|
||||
return Promise.reject(err);
|
||||
this.knownCircularRefs = {};
|
||||
return cachePromise.then(() => listFiles(self.storageBaseDir)).then(function(files) {
|
||||
var promises = [];
|
||||
files.forEach(function(file) {
|
||||
var parts = file.split(path.sep);
|
||||
var removePromise;
|
||||
if (parts[0] === 'global') {
|
||||
// never clean global
|
||||
return;
|
||||
} else if (!activeNodes[parts[0]]) {
|
||||
// Flow removed - remove the whole dir
|
||||
removePromise = fs.remove(path.join(self.storageBaseDir,parts[0]));
|
||||
} else if (parts[1] !== 'flow.json' && !activeNodes[parts[1].substring(0,parts[1].length-5)]) {
|
||||
// Node removed - remove the context file
|
||||
removePromise = fs.remove(path.join(self.storageBaseDir,file));
|
||||
}
|
||||
if (removePromise) {
|
||||
promises.push(removePromise);
|
||||
}
|
||||
});
|
||||
});
|
||||
return Promise.all(promises)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = function(config){
|
||||
|
||||
@@ -32,7 +32,14 @@ Memory.prototype._getOne = function(scope, key) {
|
||||
var value;
|
||||
var error;
|
||||
if(this.data[scope]){
|
||||
value = util.getObjectProperty(this.data[scope], key);
|
||||
try {
|
||||
value = util.getObjectProperty(this.data[scope], key);
|
||||
} catch(err) {
|
||||
if (err.code === "INVALID_EXPR") {
|
||||
throw err;
|
||||
}
|
||||
value = undefined;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@ var runtime;
|
||||
var settings;
|
||||
var log;
|
||||
|
||||
|
||||
var encryptedCredentials = null;
|
||||
var credentialCache = {};
|
||||
var credentialsDef = {};
|
||||
|
||||
@@ -16,11 +16,10 @@
|
||||
|
||||
var when = require("when");
|
||||
var clone = require("clone");
|
||||
var typeRegistry = require("../../../runtime-registry");
|
||||
var Log;
|
||||
var typeRegistry = require("../registry");
|
||||
var Log = require("../../log");
|
||||
var redUtil = require("../../util");
|
||||
var flowUtil = require("./util");
|
||||
var Node;
|
||||
|
||||
var nodeCloseTimeout = 15000;
|
||||
|
||||
@@ -293,7 +292,6 @@ function Flow(global,flow) {
|
||||
|
||||
function createNode(type,config) {
|
||||
var nn = null;
|
||||
try {
|
||||
var nt = typeRegistry.get(type);
|
||||
if (nt) {
|
||||
var conf = clone(config);
|
||||
@@ -317,9 +315,6 @@ function createNode(type,config) {
|
||||
} else {
|
||||
Log.error(Log._("nodes.flow.unknown-type", {type:type}));
|
||||
}
|
||||
} catch(err) {
|
||||
Log.error(err);
|
||||
}
|
||||
return nn;
|
||||
}
|
||||
|
||||
@@ -500,10 +495,8 @@ function createSubflow(sf,sfn,subflows,globalSubflows,activeNodes) {
|
||||
|
||||
|
||||
module.exports = {
|
||||
init: function(runtime) {
|
||||
nodeCloseTimeout = runtime.settings.nodeCloseTimeout || 15000;
|
||||
Log = runtime.log;
|
||||
Node = require("../Node");
|
||||
init: function(settings) {
|
||||
nodeCloseTimeout = settings.nodeCloseTimeout || 15000;
|
||||
},
|
||||
create: function(global,conf) {
|
||||
return new Flow(global,conf);
|
||||
|
||||
@@ -19,17 +19,15 @@ var when = require("when");
|
||||
|
||||
var Flow = require('./Flow');
|
||||
|
||||
var typeRegistry = require("../../../runtime-registry");
|
||||
var deprecated = typeRegistry.deprecated;
|
||||
|
||||
|
||||
var typeRegistry = require("../registry");
|
||||
var context = require("../context")
|
||||
var credentials = require("../credentials");
|
||||
|
||||
var flowUtil = require("./util");
|
||||
var log;
|
||||
var log = require("../../log");
|
||||
var events = require("../../events");
|
||||
var redUtil = require("../../util");
|
||||
var deprecated = require("../registry/deprecated");
|
||||
|
||||
var storage = null;
|
||||
var settings = null;
|
||||
@@ -52,7 +50,6 @@ function init(runtime) {
|
||||
}
|
||||
settings = runtime.settings;
|
||||
storage = runtime.storage;
|
||||
log = runtime.log;
|
||||
started = false;
|
||||
if (!typeEventRegistered) {
|
||||
events.on('type-registered',function(type) {
|
||||
@@ -70,7 +67,7 @@ function init(runtime) {
|
||||
});
|
||||
typeEventRegistered = true;
|
||||
}
|
||||
Flow.init(runtime);
|
||||
Flow.init(settings);
|
||||
}
|
||||
|
||||
function loadFlows() {
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
var clone = require("clone");
|
||||
var redUtil = require("../../util");
|
||||
var subflowInstanceRE = /^subflow:(.+)$/;
|
||||
var typeRegistry = require("../../../runtime-registry");
|
||||
var typeRegistry = require("../registry");
|
||||
|
||||
function diffNodes(oldNode,newNode) {
|
||||
if (oldNode == null) {
|
||||
|
||||
@@ -18,16 +18,15 @@ var when = require("when");
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var clone = require("clone");
|
||||
var util = require("util");
|
||||
|
||||
var registry = require("../../runtime-registry");
|
||||
|
||||
var registry = require("./registry");
|
||||
var credentials = require("./credentials");
|
||||
var flows = require("./flows");
|
||||
var flowUtil = require("./flows/util")
|
||||
var context = require("./context");
|
||||
var Node = require("./Node");
|
||||
var log;
|
||||
var log = null;
|
||||
var library = require("./library");
|
||||
|
||||
var events = require("../events");
|
||||
|
||||
@@ -62,26 +61,6 @@ function registerType(nodeSet,type,constructor,opts) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!(constructor.prototype instanceof Node)) {
|
||||
if(Object.getPrototypeOf(constructor.prototype) === Object.prototype) {
|
||||
util.inherits(constructor,Node);
|
||||
} else {
|
||||
var proto = constructor.prototype;
|
||||
while(Object.getPrototypeOf(proto) !== Object.prototype) {
|
||||
proto = Object.getPrototypeOf(proto);
|
||||
}
|
||||
//TODO: This is a partial implementation of util.inherits >= node v5.0.0
|
||||
// which should be changed when support for node < v5.0.0 is dropped
|
||||
// see: https://github.com/nodejs/node/pull/3455
|
||||
proto.constructor.super_ = Node;
|
||||
if(Object.setPrototypeOf) {
|
||||
Object.setPrototypeOf(proto, Node.prototype);
|
||||
} else {
|
||||
// hack for node v0.10
|
||||
proto.__proto__ = Node.prototype;
|
||||
}
|
||||
}
|
||||
}
|
||||
registry.registerType(nodeSet,type,constructor);
|
||||
}
|
||||
|
||||
@@ -120,6 +99,7 @@ function init(runtime) {
|
||||
flows.init(runtime);
|
||||
registry.init(runtime);
|
||||
context.init(runtime.settings);
|
||||
library.init(runtime);
|
||||
}
|
||||
|
||||
function disableNode(id) {
|
||||
@@ -210,8 +190,8 @@ module.exports = {
|
||||
getNodeConfig: registry.getNodeConfig,
|
||||
getNodeIconPath: registry.getNodeIconPath,
|
||||
getNodeIcons: registry.getNodeIcons,
|
||||
getNodeExampleFlows: registry.getNodeExampleFlows,
|
||||
getNodeExampleFlowPath: registry.getNodeExampleFlowPath,
|
||||
getNodeExampleFlows: library.getExampleFlows,
|
||||
getNodeExampleFlowPath: library.getExampleFlowPath,
|
||||
|
||||
clearRegistry: registry.clear,
|
||||
cleanModuleList: registry.cleanModuleList,
|
||||
|
||||
@@ -59,11 +59,11 @@ function getFlowsFromPath(path) {
|
||||
})
|
||||
}
|
||||
|
||||
function addNodeExamplesDir(module,path) {
|
||||
exampleRoots[module] = path;
|
||||
return getFlowsFromPath(path).then(function(result) {
|
||||
function addNodeExamplesDir(module) {
|
||||
exampleRoots[module.name] = module.path;
|
||||
getFlowsFromPath(module.path).then(function(result) {
|
||||
exampleFlows = exampleFlows||{d:{}};
|
||||
exampleFlows.d[module] = result;
|
||||
exampleFlows.d[module.name] = result;
|
||||
});
|
||||
}
|
||||
function removeNodeExamplesDir(module) {
|
||||
@@ -77,9 +77,17 @@ function removeNodeExamplesDir(module) {
|
||||
}
|
||||
|
||||
|
||||
function init() {
|
||||
function init(_runtime) {
|
||||
|
||||
runtime = _runtime;
|
||||
|
||||
exampleRoots = {};
|
||||
exampleFlows = null;
|
||||
|
||||
runtime.events.removeListener("node-examples-dir",addNodeExamplesDir);
|
||||
runtime.events.on("node-examples-dir",addNodeExamplesDir);
|
||||
runtime.events.removeListener("node-module-uninstalled",removeNodeExamplesDir);
|
||||
runtime.events.on("node-module-uninstalled",removeNodeExamplesDir);
|
||||
}
|
||||
|
||||
function getExampleFlows() {
|
||||
@@ -95,8 +103,6 @@ function getExampleFlowPath(module,path) {
|
||||
|
||||
module.exports = {
|
||||
init: init,
|
||||
addExamplesDir: addNodeExamplesDir,
|
||||
removeExamplesDir: removeNodeExamplesDir,
|
||||
getExampleFlows: getExampleFlows,
|
||||
getExampleFlowPath: getExampleFlowPath
|
||||
}
|
||||
@@ -14,19 +14,22 @@
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var when = require("when");
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
|
||||
var events = require("../../events");
|
||||
var registry = require("./registry");
|
||||
var loader = require("./loader");
|
||||
var installer = require("./installer");
|
||||
var library = require("./library");
|
||||
|
||||
var settings;
|
||||
|
||||
function init(runtime) {
|
||||
settings = runtime.settings;
|
||||
installer.init(runtime);
|
||||
installer.init(runtime.settings);
|
||||
loader.init(runtime);
|
||||
registry.init(settings,loader,runtime.events);
|
||||
library.init();
|
||||
registry.init(settings,loader);
|
||||
}
|
||||
|
||||
function load() {
|
||||
@@ -48,7 +51,7 @@ function enableNodeSet(typeOrId) {
|
||||
return registry.getNodeInfo(typeOrId);
|
||||
});
|
||||
}
|
||||
return Promise.resolve(nodeSet);
|
||||
return when.resolve(nodeSet);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -81,11 +84,5 @@ module.exports = {
|
||||
|
||||
cleanModuleList: registry.cleanModuleList,
|
||||
|
||||
paletteEditorEnabled: installer.paletteEditorEnabled,
|
||||
|
||||
getNodeExampleFlows: library.getExampleFlows,
|
||||
getNodeExampleFlowPath: library.getExampleFlowPath,
|
||||
|
||||
deprecated: require("./deprecated")
|
||||
|
||||
paletteEditorEnabled: installer.paletteEditorEnabled
|
||||
};
|
||||
@@ -19,10 +19,9 @@ var path = require("path");
|
||||
var fs = require("fs");
|
||||
|
||||
var registry = require("./registry");
|
||||
var library = require("./library");
|
||||
var log;
|
||||
var log = require("../../log");
|
||||
|
||||
var events;
|
||||
var events = require("../../events");
|
||||
|
||||
var child_process = require('child_process');
|
||||
var npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
||||
@@ -32,10 +31,8 @@ var settings;
|
||||
var moduleRe = /^(@[^/]+?[/])?[^/]+?$/;
|
||||
var slashRe = process.platform === "win32" ? /\\|[/]/ : /[/]/;
|
||||
|
||||
function init(runtime) {
|
||||
events = runtime.events;
|
||||
settings = runtime.settings;
|
||||
log = runtime.log;
|
||||
function init(_settings) {
|
||||
settings = _settings;
|
||||
}
|
||||
|
||||
var activePromise = Promise.resolve();
|
||||
@@ -222,7 +219,8 @@ function uninstallModule(module) {
|
||||
} else {
|
||||
log.info(log._("server.install.uninstalled",{name:module}));
|
||||
reportRemovedModules(list);
|
||||
library.removeExamplesDir(module);
|
||||
// TODO: tidy up internal event names
|
||||
events.emit("node-module-uninstalled",module)
|
||||
resolve(list);
|
||||
}
|
||||
}
|
||||
@@ -22,8 +22,6 @@ var semver = require("semver");
|
||||
var localfilesystem = require("./localfilesystem");
|
||||
var registry = require("./registry");
|
||||
|
||||
var i18n = require("../util").i18n; // TODO: separate module
|
||||
|
||||
var settings;
|
||||
var runtime;
|
||||
|
||||
@@ -84,21 +82,7 @@ function createNodeApi(node) {
|
||||
events: runtime.events,
|
||||
util: runtime.util,
|
||||
version: runtime.version,
|
||||
require: requireModule,
|
||||
comms: {
|
||||
publish: function(topic,data,retain) {
|
||||
runtime.events.emit("comms",{
|
||||
topic: topic,
|
||||
data: data,
|
||||
retain: retain
|
||||
})
|
||||
}
|
||||
},
|
||||
library: {
|
||||
register: function(type) {
|
||||
return runtime.library.register(node.id,type);
|
||||
}
|
||||
}
|
||||
require: requireModule
|
||||
}
|
||||
copyObjectProperties(runtime.nodes,red.nodes,["createNode","getNode","eachNode","addCredentials","getCredentials","deleteCredentials" ]);
|
||||
red.nodes.registerType = function(type,constructor,opts) {
|
||||
@@ -107,6 +91,8 @@ function createNodeApi(node) {
|
||||
copyObjectProperties(runtime.log,red.log,null,["init"]);
|
||||
copyObjectProperties(runtime.settings,red.settings,null,["init","load","reset"]);
|
||||
if (runtime.adminApi) {
|
||||
red.comms = runtime.adminApi.comms;
|
||||
red.library = runtime.adminApi.library;
|
||||
red.auth = runtime.adminApi.auth;
|
||||
red.httpAdmin = runtime.adminApi.adminApp;
|
||||
red.httpNode = runtime.nodeApp;
|
||||
@@ -114,6 +100,12 @@ function createNodeApi(node) {
|
||||
} else {
|
||||
//TODO: runtime.adminApi is always stubbed if not enabled, so this block
|
||||
// is unused - but may be needed for the unit tests
|
||||
red.comms = {
|
||||
publish: function() {}
|
||||
};
|
||||
red.library = {
|
||||
register: function() {}
|
||||
};
|
||||
red.auth = {
|
||||
needsPermission: function() {}
|
||||
};
|
||||
@@ -124,7 +116,7 @@ function createNodeApi(node) {
|
||||
if (args[0].indexOf(":") === -1) {
|
||||
args[0] = node.namespace+":"+args[0];
|
||||
}
|
||||
return i18n._.apply(null,args);
|
||||
return runtime.i18n._.apply(null,args);
|
||||
}
|
||||
return red;
|
||||
}
|
||||
@@ -265,7 +257,7 @@ function loadNodeConfig(fileInfo) {
|
||||
index = regExp.lastIndex;
|
||||
var help = content.substring(regExp.lastIndex-match[1].length,regExp.lastIndex);
|
||||
|
||||
var lang = i18n.defaultLang;
|
||||
var lang = runtime.i18n.defaultLang;
|
||||
if ((match = langRegExp.exec(help)) !== null) {
|
||||
lang = match[1];
|
||||
}
|
||||
@@ -296,7 +288,7 @@ function loadNodeConfig(fileInfo) {
|
||||
fs.stat(path.join(path.dirname(file),"locales"),function(err,stat) {
|
||||
if (!err) {
|
||||
node.namespace = node.id;
|
||||
i18n.registerMessageCatalog(node.id,
|
||||
runtime.i18n.registerMessageCatalog(node.id,
|
||||
path.join(path.dirname(file),"locales"),
|
||||
path.basename(file,".js")+".json")
|
||||
.then(function() {
|
||||
@@ -440,10 +432,10 @@ function getNodeHelp(node,lang) {
|
||||
}
|
||||
if (help) {
|
||||
node.help[lang] = help;
|
||||
} else if (lang === i18n.defaultLang) {
|
||||
} else if (lang === runtime.i18n.defaultLang) {
|
||||
return null;
|
||||
} else {
|
||||
node.help[lang] = getNodeHelp(node, i18n.defaultLang);
|
||||
node.help[lang] = getNodeHelp(node, runtime.i18n.defaultLang);
|
||||
}
|
||||
}
|
||||
return node.help[lang];
|
||||
@@ -19,17 +19,17 @@ var path = require("path");
|
||||
|
||||
var events;
|
||||
var log;
|
||||
|
||||
var i18n = require("../util").i18n; // TODO: separate module
|
||||
var i18n;
|
||||
|
||||
var settings;
|
||||
var disableNodePathScan = false;
|
||||
var iconFileExtensions = [".png", ".gif"];
|
||||
var iconFileExtensions = [".png", ".gif", ".svg"];
|
||||
|
||||
function init(runtime) {
|
||||
settings = runtime.settings;
|
||||
events = runtime.events;
|
||||
log = runtime.log;
|
||||
i18n = runtime.i18n;
|
||||
}
|
||||
|
||||
function isIncluded(name) {
|
||||
@@ -228,8 +228,7 @@ function getModuleNodeFiles(module) {
|
||||
var examplesDir = path.join(moduleDir,"examples");
|
||||
try {
|
||||
fs.statSync(examplesDir)
|
||||
result.examples = {path:examplesDir};
|
||||
// events.emit("node-examples-dir",{name:pkg.name,path:examplesDir});
|
||||
events.emit("node-examples-dir",{name:pkg.name,path:examplesDir});
|
||||
} catch(err) {
|
||||
}
|
||||
return result;
|
||||
@@ -241,7 +240,7 @@ function getNodeFiles(disableNodePathScan) {
|
||||
var nodeFiles = [];
|
||||
var results;
|
||||
|
||||
var dir = path.resolve(__dirname + '/../../public/icons');
|
||||
var dir = path.resolve(__dirname + '/../../../../public/icons');
|
||||
var iconList = [{path:dir,icons:scanIconDir(dir)}];
|
||||
|
||||
if (settings.coreNodesDir) {
|
||||
@@ -291,6 +290,33 @@ function getNodeFiles(disableNodePathScan) {
|
||||
|
||||
if (!disableNodePathScan) {
|
||||
var moduleFiles = scanTreeForNodesModules();
|
||||
|
||||
// Filter the module list to ignore global modules
|
||||
// that have also been installed locally - allowing the user to
|
||||
// update a module they may not otherwise be able to touch
|
||||
|
||||
moduleFiles.sort(function(A,B) {
|
||||
if (A.local && !B.local) {
|
||||
return -1
|
||||
} else if (!A.local && B.local) {
|
||||
return 1
|
||||
}
|
||||
return 0;
|
||||
})
|
||||
var knownModules = {};
|
||||
moduleFiles = moduleFiles.filter(function(mod) {
|
||||
var result;
|
||||
if (!knownModules[mod.package.name]) {
|
||||
knownModules[mod.package.name] = true;
|
||||
result = true;
|
||||
} else {
|
||||
result = false;
|
||||
}
|
||||
log.debug("Module: "+mod.package.name+" "+mod.package.version+(result?"":" *ignored due to local copy*"));
|
||||
log.debug(" "+mod.dir);
|
||||
return result;
|
||||
});
|
||||
|
||||
moduleFiles.forEach(function(moduleFile) {
|
||||
var nodeModuleFiles = getModuleNodeFiles(moduleFile);
|
||||
nodeList[moduleFile.package.name] = {
|
||||
@@ -299,8 +325,7 @@ function getNodeFiles(disableNodePathScan) {
|
||||
path: moduleFile.dir,
|
||||
local: moduleFile.local||false,
|
||||
nodes: {},
|
||||
icons: nodeModuleFiles.icons,
|
||||
examples: nodeModuleFiles.examples
|
||||
icons: nodeModuleFiles.icons
|
||||
};
|
||||
if (moduleFile.package['node-red'].version) {
|
||||
nodeList[moduleFile.package.name].redVersion = moduleFile.package['node-red'].version;
|
||||
@@ -333,8 +358,7 @@ function getModuleFiles(module) {
|
||||
name: moduleFile.package.name,
|
||||
version: moduleFile.package.version,
|
||||
nodes: {},
|
||||
icons: nodeModuleFiles.icons,
|
||||
examples: nodeModuleFiles.examples
|
||||
icons: nodeModuleFiles.icons
|
||||
};
|
||||
if (moduleFile.package['node-red'].version) {
|
||||
nodeList[moduleFile.package.name].redVersion = moduleFile.package['node-red'].version;
|
||||
@@ -15,13 +15,17 @@
|
||||
**/
|
||||
|
||||
//var UglifyJS = require("uglify-js");
|
||||
var util = require("util");
|
||||
var when = require("when");
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
|
||||
var library = require("./library");
|
||||
var events = require("../../events");
|
||||
|
||||
var events;
|
||||
var settings;
|
||||
|
||||
var Node;
|
||||
|
||||
var loader;
|
||||
|
||||
var nodeConfigCache = null;
|
||||
@@ -31,15 +35,18 @@ var nodeConstructors = {};
|
||||
var nodeTypeToId = {};
|
||||
var moduleNodes = {};
|
||||
|
||||
function init(_settings,_loader, _events) {
|
||||
function init(_settings,_loader) {
|
||||
settings = _settings;
|
||||
loader = _loader;
|
||||
events = _events;
|
||||
moduleNodes = {};
|
||||
nodeTypeToId = {};
|
||||
nodeConstructors = {};
|
||||
nodeList = [];
|
||||
nodeConfigCache = null;
|
||||
Node = require("../Node");
|
||||
events.removeListener("node-icon-dir",nodeIconDir);
|
||||
events.on("node-icon-dir",nodeIconDir);
|
||||
|
||||
}
|
||||
|
||||
function load() {
|
||||
@@ -126,7 +133,7 @@ function saveNodeList() {
|
||||
if (settings.available()) {
|
||||
return settings.set("nodes",moduleList);
|
||||
} else {
|
||||
return Promise.reject("Settings unavailable");
|
||||
return when.reject("Settings unavailable");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,9 +214,6 @@ function addModule(module) {
|
||||
icon_paths[module.name] = [];
|
||||
module.icons.forEach(icon=>icon_paths[module.name].push(path.resolve(icon.path)) )
|
||||
}
|
||||
if (module.examples) {
|
||||
library.addExamplesDir(module.name,module.examples.path);
|
||||
}
|
||||
nodeConfigCache = null;
|
||||
}
|
||||
|
||||
@@ -365,6 +369,27 @@ function getCaller(){
|
||||
return stack[0].getFileName();
|
||||
}
|
||||
|
||||
function inheritNode(constructor) {
|
||||
if(Object.getPrototypeOf(constructor.prototype) === Object.prototype) {
|
||||
util.inherits(constructor,Node);
|
||||
} else {
|
||||
var proto = constructor.prototype;
|
||||
while(Object.getPrototypeOf(proto) !== Object.prototype) {
|
||||
proto = Object.getPrototypeOf(proto);
|
||||
}
|
||||
//TODO: This is a partial implementation of util.inherits >= node v5.0.0
|
||||
// which should be changed when support for node < v5.0.0 is dropped
|
||||
// see: https://github.com/nodejs/node/pull/3455
|
||||
proto.constructor.super_ = Node;
|
||||
if(Object.setPrototypeOf) {
|
||||
Object.setPrototypeOf(proto, Node.prototype);
|
||||
} else {
|
||||
// hack for node v0.10
|
||||
proto.__proto__ = Node.prototype;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function registerNodeConstructor(nodeSet,type,constructor) {
|
||||
if (nodeConstructors.hasOwnProperty(type)) {
|
||||
throw new Error(type+" already registered");
|
||||
@@ -372,6 +397,9 @@ function registerNodeConstructor(nodeSet,type,constructor) {
|
||||
//TODO: Ensure type is known - but doing so will break some tests
|
||||
// that don't have a way to register a node template ahead
|
||||
// of registering the constructor
|
||||
if(!(constructor.prototype instanceof Node)) {
|
||||
inheritNode(constructor);
|
||||
}
|
||||
|
||||
var nodeSetInfo = getFullNodeInfo(nodeSet);
|
||||
if (nodeSetInfo) {
|
||||
@@ -556,15 +584,37 @@ function setModulePendingUpdated(module,version) {
|
||||
}
|
||||
|
||||
var icon_paths = {
|
||||
"node-red":[path.resolve(__dirname + '/../../public/icons')]
|
||||
"node-red":[path.resolve(__dirname + '/../../../../public/icons')]
|
||||
};
|
||||
var iconCache = {};
|
||||
var defaultIcon = path.resolve(__dirname + '/../../public/icons/arrow-in.png');
|
||||
var defaultIcon = path.resolve(__dirname + '/../../../../public/icons/arrow-in.png');
|
||||
|
||||
function nodeIconDir(dir) {
|
||||
return;
|
||||
icon_paths[dir.name] = icon_paths[dir.name] || [];
|
||||
icon_paths[dir.name].push(path.resolve(dir.path));
|
||||
|
||||
if (dir.icons) {
|
||||
if (!moduleConfigs[dir.name]) {
|
||||
moduleConfigs[dir.name] = {
|
||||
name: dir.name,
|
||||
nodes: {},
|
||||
icons: []
|
||||
};
|
||||
}
|
||||
var module = moduleConfigs[dir.name];
|
||||
if (module.icons === undefined) {
|
||||
module.icons = [];
|
||||
}
|
||||
dir.icons.forEach(function(icon) {
|
||||
if (module.icons.indexOf(icon) === -1) {
|
||||
module.icons.push(icon);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getNodeIconPath(module,icon) {
|
||||
if (/\.\./.test(icon)) {
|
||||
throw new Error();
|
||||
}
|
||||
var iconName = module+"/"+icon;
|
||||
if (iconCache[iconName]) {
|
||||
return iconCache[iconName];
|
||||
@@ -597,7 +647,7 @@ function getNodeIcons() {
|
||||
if (moduleConfigs.hasOwnProperty(module)) {
|
||||
if (moduleConfigs[module].icons) {
|
||||
iconList[module] = [];
|
||||
moduleConfigs[module].icons.forEach(icon=>{ iconList[module] = iconList[module].concat(icon.icons)})
|
||||
moduleConfigs[module].icons.forEach(icon=>{ iconList[module] = iconList[module].concat(icon.icons) });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@
|
||||
var when = require("when");
|
||||
var clone = require("clone");
|
||||
var assert = require("assert");
|
||||
var log = require("../util").log; // TODO: separate module
|
||||
var log = require("./log");
|
||||
var util = require("./util");
|
||||
|
||||
// localSettings are those provided in the runtime settings.js file
|
||||
|
||||
@@ -18,7 +18,7 @@ var when = require('when');
|
||||
var Path = require('path');
|
||||
var crypto = require('crypto');
|
||||
|
||||
var log = require("../../util").log; // TODO: separate module
|
||||
var log = require("../log");
|
||||
|
||||
var runtime;
|
||||
var storageModule;
|
||||
|
||||
@@ -18,8 +18,7 @@ var fs = require('fs-extra');
|
||||
var when = require('when');
|
||||
var fspath = require("path");
|
||||
|
||||
var log = require("../../../util").log; // TODO: separate module
|
||||
|
||||
var log = require("../../log");
|
||||
var util = require("./util");
|
||||
var library = require("./library");
|
||||
var sessions = require("./sessions");
|
||||
|
||||
@@ -358,7 +358,15 @@ Project.prototype.update = function (user, data) {
|
||||
promises.push(util.writeFile(this.paths['README.md'], this.description));
|
||||
}
|
||||
if (savePackage) {
|
||||
promises.push(util.writeFile(this.paths['package.json'], JSON.stringify(this.package,"",4)));
|
||||
promises.push(fs.readFile(project.paths['package.json'],"utf8").then(content => {
|
||||
var currentPackage = {};
|
||||
try {
|
||||
currentPackage = util.parseJSON(content);
|
||||
} catch(err) {
|
||||
}
|
||||
this.package = Object.assign(currentPackage,this.package);
|
||||
return util.writeFile(this.paths['package.json'], JSON.stringify(this.package,"",4));
|
||||
}));
|
||||
}
|
||||
return when.settle(promises).then(function(res) {
|
||||
return {
|
||||
|
||||
@@ -107,11 +107,6 @@ function getSSHKey(username, name) {
|
||||
function generateSSHKey(username, options) {
|
||||
options = options || {};
|
||||
var name = options.name || "";
|
||||
if (!/^[a-zA-Z0-9\-_]+$/.test(options.name)) {
|
||||
var err = new Error("Invalid SSH Key name");
|
||||
e.code = "invalid_key_name";
|
||||
return Promise.reject(err);
|
||||
}
|
||||
return checkExistSSHKeyFiles(username, name)
|
||||
.then(function(result) {
|
||||
if ( result ) {
|
||||
|
||||
@@ -18,8 +18,7 @@ var when = require('when');
|
||||
var fs = require('fs-extra');
|
||||
var fspath = require("path");
|
||||
|
||||
var log = require("../../../util").log; // TODO: separate module
|
||||
|
||||
var log = require("../../log");
|
||||
var util = require("./util");
|
||||
|
||||
var sessionsFile;
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user