Having trouble with sending a typed array to a subprocess's stdin

I've encountered an issue where sending data from a typed array to a subprocess's stdin gets truncated if the data in the typed array is changed. Below is a small example demonstrating this problem.

The subprocess in question is a simple C++ app that reads all data from stdin and outputs the number of read bytes to stdout. The code for the C++ subprocess is as follows:

#include <iostream>
#include <limits>

int main(int argc, char **args) {
  std::cin.ignore(std::numeric_limits<std::streamsize>::max());
  std::cout << std::cin.gcount() << std::flush;
  return 0;
}

The Node.js app repeatedly calls this executable, passes 128 bytes of data to stdin, and checks if the response from the child process's stdout is "128". The code for the Node.js app is as follows:

const execFile = require('child_process').execFile;

var data = new Float64Array(16);

var generateError = true;
generateError && setInterval(_ => {
  for (var i = 0; i < 16; i++) {
    data[i] = Math.random();
  }
}, 1);

setInterval(_ => {
  var subproc = execFile('a.exe', [ '-c' ], (error, stdout, stderr) => {
    if (stdout.trim() !== '128' || error) {
      console.log('ERR: ' + stdout);
    } else {
      console.log('OK');
    }
  });
  subproc.stdin.write(Buffer.from(data.buffer)) || console.log('Use drain'); // Write Binary Data (128 Byte)
  subproc.stdin.end();
}, 100);

The output I receive is as follows:

OK
OK
OK
OK
OK
OK
ERR: 69
ERR: 114
OK
OK
OK
ERR: 41

What puzzles me are the following points:

  • When I set generateError to false, everything works as expected.
  • Using "wc.exe -c" (Word/Bytecount Tool from Cygwin) also yields the expected results, even when generateError is set to true.

My question is: What's causing this issue? Is it the C++ code or the Node.js code? Are we not allowed to change the buffer after passing it to subproc.stdin.write? If so, why does the variant with wc.exe work seamlessly?

Answer №1

When working with Windows, it's important to be mindful of the different types of streams available: binary streams and various text streams. By switching the input stream of the C++ application to a binary stream, you can effectively resolve the issue at hand. It's plausible that wc.exe utilizes the input stream as a binary stream, hence why the version involving wc.exe is functioning correctly:

#include <iostream>
#include <limits>

#include <fcntl.h>
#include <io.h>
int main(int argc, char **args) {
  _setmode(_fileno(stdin), _O_BINARY);

  std::cin.ignore(std::numeric_limits<std::streamsize>::max());
  std::cout << std::cin.gcount() << std::flush;
  return 0;
}

To delve deeper into this topic, check out Read binary data from std::cin

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing specifications throughout the entire nodejs route

In my Nodejs app, I have a RESTful API where I need to check for a user's role before sending a response with data or 404 error. apiRouter.route('/users') .get(function (req, res) { var currentUser = req.decoded; if(curr ...

Using the Express router to handle GET requests results in failure, however using the app

During my exploration of this particular guide, I encountered an issue where the router methods were not functioning as expected. Upon using npm start and trying to access localhost:3000/api/puppies, a 404 error would consistently appear. However, after mo ...

Changing the req.path property within an expressjs middleware

I'm currently in the process of developing a middleware that will help eliminate locale strings from the path, such as /de/about becoming /about. This project is being implemented using express. To achieve this, I initially tried out the following mid ...

Parsing error: Unforeseen token encountered. Consider adding a supplementary loader to manage the output of these loaders

Could someone please break down this syntax message?.length === 1 and show me how to convert it into standard JavaScript? https://i.stack.imgur.com/20Ui6.png I am encountering an error when trying to use a Vue.js component that I downloaded from another ...

Integrating JavaScript into HTML pages with corresponding file names

I am looking for a way to dynamically insert a .js file into an HTML page with the same filename using gulp-inject (for example, injecting index.min.js into index.html and data.min.js into data.html). The minified files are located in build/js and the HTML ...

What is the importance of having Express in the frontend?

As someone who is relatively new to the world of JavaScript and web applications, I recently came across an Express web application project that contained a public directory, client directory, and server directory. This has raised some questions for me. I ...

What are the reasons for a Lambda function not being triggered when added as a target to an Event Bridge Rule?

I have encountered an issue with setting a rule and a lambda target for EventBridge. Although I have successfully added the rule and target, the Lambda is not being triggered. Additionally, when I look at the target Lambda function on the AWS Console, I do ...

Preventing the creation of several Sequelize class instances

As I work on constructing a basic database schema using Express and Sequelize, I've organized my models in separate files. In a central file called models/index.js, I initialize an instance of the Sequelize class, import the models, and define relatio ...

Is it possible to exploit this specific route to gain unauthorized access to different sections of the server?

Is it possible to exploit this code to access additional server components? const downloadDirectory = './uploads/' app.get("/uploads/:id", (req, res) => { const fullFilePath = path.resolve(path.join(downloadDirectory, req.params. ...

Encountering issues with browser tabs and Socket.IO

I'm currently working on a real-time chat using Socket.IO, but I've encountered a major issue. The aim is to allow users to log in, select another connected user, and start chatting... var http = require('http'), fs = require(&ap ...

Exploring the depths of Node.js, Express, and MySQL integration

Hey there! I'm currently working on integrating the express framework for node.js with the mysql module found at https://npmjs.org/package/mysql. My setup involves a basic application configured using the express command line tool and a dedicated modu ...

The errors from Node.js interacting with Elasticsearch are not being displayed

Currently, I am encountering an issue with the node.js elasticsearch library. When executing the code below, it successfully produces results if the console.log(hello); line is removed. However, upon adding this line, no error appears even though I expec ...

File is indicating a status of 200 ok, however it is not being displayed on the screen (node.js, expressjs)

I'm trying to display a video file in the browser and access it like an API on my front end. My goal is to have my front end call the video using a simple <video> tag. <video> <source ="video/randomfile.mov" type="video/mov"> < ...

I encountered an issue while trying to generate a React app with npx

Struggling with setting up a React app on my Windows machine, looking for some assistance here. I've tried running the following command in Hyper Terminal: npx create-react-app new-app After checking that my Node.js version is 12.16.1 and both npm and ...

Setting up and customizing Express with Angular

Currently working on a straightforward Angular/Express todo-list app, I encountered some difficulties. As the project is still in progress, after running the code on localhost:3000, I noticed that {{ thing }} was displayed literally on the webpage. The di ...

I attempted to implement "cors" in my project, but encountered the error message "Access to XMLHttpRequest has been blocked". What could be causing this issue?

I am facing an issue with my express API where I receive a CORS error message after making a request. Error: Access to XMLHttpRequest at 'http://localhost:9000/api/courses' from origin 'http://localhost:4222' has been blocked by CORS p ...

Show customized/designed text - react

I'm currently in the process of developing a web application specifically for sharing announcements. One of the features I have implemented is a rich text editor, allowing users to format their text by making it bold, underlined, and more. The text en ...

Having trouble viewing the page of a new package you published on the NPM Website?

Today, I officially released an NPM package called jhp-serve. It can be easily installed using npm install or run with npx. You can even find it in the search results here: https://www.npmjs.com/search?q=jhp. However, when attempting to view its page by cl ...

The elusive Mongoose slipped through the MongoDB/Atlas, leaving behind nothing but an Express 404

I'm currently diving into MongoDB and facing an ongoing issue that's been puzzling me for quite some time now. Despite following various tutorials, I keep encountering a frustrating 404 error and I'm at a loss as to what might be causing it. ...

Gulp does not generate any files

Hey there, I'm brand new to using node.js and coding in general. I recently attempted to convert SCSS to CSS using gulp. My gulpfile.js seems to be correct, but when I try running "gulp styles" in the node.js command prompt, I receive the following ou ...