2

I'm creating a pipe and trying to write to it. But the writing never works.

import * as fs from 'fs';
import * as mkfifo from 'mkfifo';

mkfifo.mkfifoSync('/tmp/my_fifo', 0o600);

fs.writeFileSync('/tmp/my_fifo', 'Hey there!');

console.log('here');

If I comment everything except mkfifo.mkfifoSync('/tmp/my_fifo', 0o600); I see the pipe is created ok.

However, fs.writeFileSync('/tmp/my_fifo', 'Hey there!'); never returns.

Even when I use the callback version `fs.writeFile()1 the callback is never fired.

fs.writeFile('/tmp/my_fifo', 'Hey there!', (err) => {
    if (err) {
        return console.log('Write error: ' + err);
    }

    console.log('Write success');

    process.exit();
});

The callback is never called.

What am I doing wrong?

Just as a check I open up and tried to read the pipe from another script:

fs.readFile('/tmp/my_fifo', (err, data) => {
    if (err) {
        return console.log('Read error: ' + err);
    }

    console.log('Read success. Data is: ' + data);
});

On the writer side I see the error:

Error: ESPIPE: invalid seek, write

On the reader side I see:

Read success. Data is: 

empty data, but a successful read.

Update

If I don't create the pipe at all via mkfifoSync(), and instead just call fs.writeFileSync(), it will create and write to the file just fine.

So my question is how do I write to this pipe?

lostdorje
  • 6,150
  • 9
  • 44
  • 86

1 Answers1

1

you have to use the appendFileSync method not the writeFileSync method for writing to a named pipe

append to named pipes

const fs = require("fs");
const mkfifo = require("mkfifo");

mkfifo.mkfifoSync("/tmp/my_fifo", 0o600);
fs.appendFileSync("/tmp/my_fifo", "hello world", ...);

read from named pipes

const fs = require("fs");
console.log(fs.readFileSync("/tmp/my_fifo").toString());
0.sh
  • 2,659
  • 16
  • 37
  • I'm actually using https://www.npmjs.com/package/mkfifo for fifo creation and fs.appendFileSync("/tmp/my_fifo", "hello world", ...); has the same problems....it just hangs. – lostdorje Feb 22 '18 at 01:42
  • reading from the named pipe is a separate script, writing to the named pipe is also a separate script, you have to execute the writing part before the reading part – 0.sh Feb 22 '18 at 01:43
  • @lostdorje did you put the same code snippet in one file? – 0.sh Feb 22 '18 at 01:43
  • I have 2 scripts. The writing one just hangs. (Shouldn't matter though -- writing and reading can be done in a single script because the '*Sync' versions of all methods are being used). When I remove the mkfifo.mkfifoSync("/tmp/my_fifo", 0o600); everything works fine, except it's not a pipe, it's a regular file. – lostdorje Feb 22 '18 at 01:45
  • did you confirm if `/tmp/my_fifo` was actually created ? – 0.sh Feb 22 '18 at 01:49
  • yep: prw------- 1 myuser wheel 0 Feb 22 09:53 /tmp/my_fifo and we can see it's a pipe. the writeFileSync() and appendFileSync() both just hang. after mkfifo.mkfifoSync() creates the pipe. there are other older SO posts claiming Node doesn't support Named Pipes, perhaps that still holds true. see: https://stackoverflow.com/questions/11750041/how-to-create-a-named-pipe-in-node-js – lostdorje Feb 22 '18 at 01:55
  • i don't think so, it runs here without glitch – 0.sh Feb 22 '18 at 01:56
  • version is v8.9.3 on mac os x 0.13.2 – lostdorje Feb 22 '18 at 02:00
  • 1
    ha! thanks for the proof. i needed that. i have no idea. i noticed some small differences between your code and mine and just copied your code directly and it worked. then i started changing it back to my original code piece by piece and sure enough it still worked! the point at which it stopped working was switching from appendFileSync() to writeFileSync(). which is what you stated to begin with. that's the first change i made to get my stuff working, but it didn't...but when i used your code sample and backed it up into my code, it all started working. derp. thanks! :-) – lostdorje Feb 22 '18 at 02:59