Osjs proc provider: 'data' event handler called multiple times for 1 pty request


#1

here is my code:

and here is my output:

I am running ubuntu 18.04.

This code is located inside an application index.js file (located in $OSJSFOLDER/src/packages/RemoteMenu/index.js)

Description of code:
I am using the proc provider to run a linux command into a pseudo terminal. The command gets all of the desktop files located in /usr/share/applications where the .desktop file category is ‘Office’ and then prints their filenames.

I am expecting console.log(the one in the data event) to print out 1(and only 1) output which contains the list of desktop files that have a category of office.

what is actually happening: The console.log isn’t running 1 time, but (as shown in the picture) 7 times! where the output contains only a subset of desktop files with the category of office.

Why is this, and is there a way to change it?


#2

You can reproduce this by running this in a regular terminal: (cd /usr/share/applications && grep -l Categories.*Office *.desktop). You’ll get X amount of lines back.

[I] ➜ grep -l Categories.*Office *.desktop
calibre-ebook-edit.desktop
calibre-gui.desktop
foxitreader.desktop
libreoffice-base.desktop
libreoffice-calc.desktop
libreoffice-draw.desktop
libreoffice-impress.desktop
libreoffice-math.desktop
libreoffice-startcenter.desktop
libreoffice-writer.desktop
xpdf.desktop

Each line will emit a data event.

Simply remove the newline character:

[I] ➜ grep -l Categories.*Office *.desktop | tr  '\n' ' '
calibre-ebook-edit.desktop calibre-gui.desktop foxitreader.desktop libreoffice-base.desktop libreoffice-calc.desktop libreoffice-draw.desktop libreoffice-impress.desktop libreoffice-math.desktop libreoffice-startcenter.desktop libreoffice-writer.desktop xpdf.desktop

#3

I’ve released a new version of @osjs/proc-provider that does not do argument expansion (so you can use * in commands etc) and supports setting the working directory.

You might get some use for this combined with the exec() API. You don’t have to use a pseudo-terminal for this:

.make('osjs/proc')
  .exec({cmd: '/usr/bin/grep', cwd: '/usr/share/applications'}, '-l', 'Categories.*Office', '*.desktop')
  .then(({stdout}) => console.log(stdout)) // All lines here

#4

ahhh each line emits a data event, I didn’t expect it to work like that.

Can I ask 1 more question? I’m trying to run a gui native application through the proc provider. I want to run a particular desktop file. If I do pty('export DISPLAY=thedisplay && export XAUTHORITY=theauthority && xdg-open /usr/share/applications/mydesktopfile.desktop')
nothing happens (except the pty times out).

but if I do: pty('export DISPLAY=thedisplay && export XAUTHORITY=theauthority && firefox')

it works? Why would a desktop file not work but the respective command does? Also its not possible to always use a command instead of a desktop file, because the exec of the desktop file has arguments.


#5

Btw, you can pass environmental variables like this:

pty({
  cmd: 'xdg-open',
  env: {
    DISPLAY: 'thedisplay',
    XAUTHORITY: 'theauthority'
  }
}, '/usr/share/applications/mydesktopfile.desktop')

I’m actually not sure why it happens. Have you tried spawn() instead of pty() ? Also, what is the exit code (and do you get an error) ?


#6

hmmm the code is 0 buut it times out:

with this code:

I shall try your suggestions. when I use the osjs xterm app and type it in at runtime, it actually works.
I did notice that this code is located inside a panelitem which has been statically loaded (client.js not server.js). I wonder if that has something to do with it.

My current desktop display is also xfce4 not gnome or kde or unity or anything common like that. This could also be something


#7

I’m pretty sure you can safely ignore that EIO error. It’s just the PTY getting upset no input was provided or something.

This does not matter.


#8

Finally found the solution.

I’m not sure if this is obvious or not (due to my lack of experience with node’s child process functionality) but environment variables must be passed throug the optional arguments of exec, rather than doing ‘export blablabla’ for this thing to work.