I want to send messages received from a front-end through a stream object representing the TCP connection of a libp2p node to another node.
I am trying to use the pipe() function from it-pipe npm module, as used in the chat example available in js-libp2p repo.
The problem:
The example pipes the standard input of console to the stream object:
pipe(
// Read from stdin (the source)
process.stdin,
// Turn strings into buffers
(source) => map(source, (string) => uint8ArrayFromString(string)),
// Encode with length prefix (so receiving side knows how much data is coming)
(source) => lp.encode(source),
// Write to the stream (the sink)
stream.sink
)
How can I instead pipe my message strings to this object? Something like:
pipe(
chat_message, // Use a string as the source
(source) => uint8ArrayFromString(source),
(source) => lp.encode(source),
stream.sink
)
I have tried using and changing the above code but always encounter the following error:
res = fns.shift()(res);
^
TypeError: fns.shift(...) is not a function
at rawPipe (file:///home/username/projects/Dash/test-Dash/backend/node-libp2p/node_modules/it-pipe/dist/src/index.js:37:26)
I looked at index.js inside node_modules and what I gathered was that if you pass an iterable object (like string), it replaces it with an arrow function taking no arguments, that returns the iterable itself. It later on tries to call this arrow function with res argument which I think causes the above error.
I have also tried methods similar to:
import stream from "stream";
var chat_message = "hello";
var stuff = stream.PassThrough();
stuff.write(uint8ArrayFromString(chat_message));
stuff.pipe(stream);
but I get the error:
dest.on('unpipe', onunpipe);
^
TypeError: Cannot read properties of undefined (reading 'on')