Search code examples
node-red

Is it possible to know the user that is running a node from inside the 80-template.html and 10-function.html files on Node-RED?


Node-RED uses the Ace editor and the Ace editor has a vim mode that can be activated. For activating vim mode on Node-RED we need to do the following 4 steps:

  1. Download the file keybindings-vim.js on this GitHub page.
  2. Save the file keybindings-vim.js on /usr/local/lib/node_modules/node-red/node_modules/@node-red/editor-client/public/vendor/ace (for Linux users).
  3. Go inside the 80-template.html or 10-function.html code located on /usr/local/lib/node_modules/node-red/node_modules/@node-red/nodes/core/function/ and change the following piece of code:
//// add this block ////
    this.editor.setKeyboardHandler("ace/keyboard/vim"); 
    setTimeout(function() {
        let panel = $(".ace_text-input");
        panel.focus();
    },600);
////
    RED.library.create({
        url:"templates", // where to get the data from
        type:"template", // the type of object the library is for
        editor:that.editor, // the field name the main text body goes to
        fields:['name','format','output','syntax'],
        ext: "txt"
    });
    this.editor.focus();


  1. Restart the service that is running Node-RED.

These steps work perfectly fine for activating the vim mode on specific Node-RED nodes. However, by changing directly the 80-template.html or 10-function.html files I'm going to apply those changes globally on the system. That means that the vim mode will be activated for all users of the system who are using different instances of Node-RED. I'd like to activate the vim mode in a way that only my user has access to it. Is it possible?

The solution that I thought would be finding a way of getting the name of the user who's logged on Node-RED. The pseudocode would be something like this:

    const user = object.getUser();
    if ( user === 'myuser' ){
        this.editor.setKeyboardHandler("ace/keyboard/vim"); 
        setTimeout(function() {
            let panel = $(".ace_text-input");
            panel.focus();
        },600);
    }

    RED.library.create({
        url:"templates", // where to get the data from
        type:"template", // the type of object the library is for
        editor:that.editor, // the field name the main text body goes to
        fields:['name','format','output','syntax'],
        ext: "txt"
    });
    this.editor.focus();


Is there any command that allows me to get the name of the Node-RED user that's running the node? Or is there any alternative answer for limiting the vim mode only to a specific instance of Node-RED that is running on the system?


Solution

  • As stated by hardillb on the comments one solution would be installing a different instance of Node-RED from the one running globally.

    However, it's also possible to differentiate which instance of Node-RED is running by getting its port number instead of the user name. The following code worked fine for me:

    const port = location.port;
    if ( port === '1880' ){
        this.editor.setKeyboardHandler("ace/keyboard/vim");
        setTimeout(function() {
            let panel = $(".ace_text-input");
            panel.focus();
        },600);
    }
    RED.library.create({
        url:"templates", // where to get the data from
        type:"template", // the type of object the library is for
        editor:that.editor, // the field name the main text body goes to
        fields:['name','format','output','syntax'],
        ext: "txt"
    });
    this.editor.focus();