Support Uint8Array
buffers in stdio
#4
Merged
+43
−11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Currently, uWASI converts buffers to strings for
stdout/stderr
and converts strings to buffers forstdin
. This is not deseriable in certain conditions and causes a real bug in WASI modules that are passing binary data (such as network protocol packets), due to howTextDecoder.decode
works.The Encoding/Decoding Bug
I discovered that converting binaries to strings using
(new TextDecoder).decode(buf)
is causing issues. Essentially, when non UTF-8 compliant byte sequences are found, it adds another character to the string. Running(new TextEncoder).encode(str)
then produces a buffer which is different from the original. This is causing issue in my WASI module as it's passing binary packets from WASI to a socket opened in JS. The example below reproduces the problem:Fixes
This PR fixes two related issues:
It now accepts an
outputBuffers
boolean in theuseStdio
options. By default its assumed asfalse
keeping the current behaviour unchanged. Iftrue
is passed, thestdout
andstderr
handlers will be passed Uint8Arrays instead of strings.It also accepts Uint8Arrays on
stdin
, along with strings. If a Uint8Array is passed, no encoding will be done, unlike for strings. Again, the current behaviour remains unchanged.I have also updated the readme documenting both the new features.
Let me know if I should improve the PR in any way.
Thanks!