Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly handle child process stdio chunking #409

Merged
merged 3 commits into from
Feb 7, 2024

Conversation

addaleax
Copy link
Contributor

Converting individual chunks from UTF-8 to JS strings
is problematic because it does not handle UTF-8 characters
that are split across chunks properly.

Use the proper way of reading string data from streams instead.

Converting individual chunks from UTF-8 to JS strings
is problematic because it does not handle UTF-8 characters
that are split across chunks properly.

Use the proper way of reading string data from streams instead.
@addaleax
Copy link
Contributor Author

@dsanders11 Just for context, I don’t have merge access here :)

@dsanders11 dsanders11 requested a review from malept March 16, 2022 16:57
@dsanders11
Copy link
Member

dsanders11 commented Mar 16, 2022

@addaleax, noted. 🙂 I'm not familiar with this codebase so I don't want to merge this without getting another approval, although it looks fine on face value. I'll ping @malept.

@malept
Copy link
Member

malept commented Mar 17, 2022

Is there a testcase for this? I need to port this to @malept/cross-spawn-promise I think (which will eventually replace this file in #373).

@addaleax
Copy link
Contributor Author

addaleax commented Mar 17, 2022

@malept This is a standard bugfix that has happened in many places before. It is even explicitly mentioned in the Node.js streams docs for setEncoding():

The Readable stream will properly handle multi-byte characters delivered through the stream that would otherwise become improperly decoded if simply pulled from the stream as Buffer objects.

I can add a test case but it doesn’t seem that there are tests specifically for this file. This would be an example test case that could be added to your library, though:

import spawnPromise from './spawn-promise';
const result = await spawnPromise('node', ['-e', 'process.stdout.write(Buffer.from([0xe5, 0xa5])); setTimeout(() => process.stdout.write(Buffer.from([0xbd])), 20)']);
expect(result).to.equal('好'); // adjust to your favorite assertion library

(It may look contrived to write the bytes of a multibyte character individually. The more common form of this bug is that a character just happens to end up on a chunk boundary when a medium-to-large-size chunk of data is being written/read, but that’s less reliable to test because the chunk boundaries are not necessarily consistent across OSes, Node.js versions, etc., so a timer will generally do a better job for testing.)

Copy link
Member

@VerteDinde VerteDinde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved pending merge conflict resolving - just resolved it, this looks good to me

@addaleax addaleax requested a review from a team as a code owner February 7, 2024 18:16
@VerteDinde VerteDinde merged commit 233603a into electron:main Feb 7, 2024
3 checks passed
Copy link

🎉 This PR is included in version 5.2.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants