zx-deno icon indicating copy to clipboard operation
zx-deno copied to clipboard

[DO NOT MERGE] Use TextDecoder in stream mode

Open tzik opened this issue 3 years ago • 1 comments

This is not a real fix. Just to report an issue of the current code.

If a command output contains a non-ASCII character, whose UTF-8 representation needs multiple bytes for that codepoint, and the chunk boundary splits the these bytes into separate chunks, then TextDecoder#decode fails to decode the character at the boundary.

E.g. console.log((await $`echo -ne '\\xc3'; sleep 0s; echo -e '\\xa7'`).stdout) on zx-deno prints "��", while echo -ne '\xc3'; sleep 0s; echo -e '\xa7' on bash prints "ç".

We could resolve the issue if we could use the TextDecoder in the stream mode. Though Deno's TextDecoder doesn't support the stream mode yet, (cf. https://github.com/denoland/deno/issues/5351).

Another solution is to accumulate copies of stdin, stderr, and combined outut into arrays of Uint8Array, and decode them at once at ProcessOutput construction. Though combined may be still broken in this way.

tzik avatar May 10 '21 11:05 tzik

@tzik Nice catch :wink: Usually I care about that kind of issue, I understand what you mean :+1:

However I suppose stdout_decoder and stderr_decoder should be moved to local variables to avoid issues with concurrent commands executions, and maybe we should call .decode(new Uint8Array(0), {stream:false}) to removed some possible characters left when a stream is closed?

Minigugus avatar May 10 '21 13:05 Minigugus