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

File gets corrupted with readStream() with base64 when using bufferSize defaults #349

Open
sam-maverick opened this issue Apr 9, 2024 · 0 comments

Comments

@sam-maverick
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch react-native-blob-util@0.19.8 for the project I'm working on.

I had an issue when using readStream() with base64 files when using the default for the bufferSize parameter. The issue was that the file contents were corrupted. At first glance they looked the same, but readStream() was giving me some extra bytes that I didn't know where they were coming from.

The docs state the following,

bufferSize: number (optional)
Buffer size of read stream, default to 4096 and 4095(when encoding is base64)

And

when reading file in BASE64 encoding, buffer size must be multiples of 3.

I discovered that the issue is: When you omit the bufferSize argument, the default value is taken from class/ReactNativeBlobUtilReadStream.js, which sets the default to 10240 regardless of the encoding. This value is not good for base64, as it is not a multiple of 3.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-blob-util/class/ReactNativeBlobUtilReadStream.js b/node_modules/react-native-blob-util/class/ReactNativeBlobUtilReadStream.js
index 490efb1..1256769 100644
--- a/node_modules/react-native-blob-util/class/ReactNativeBlobUtilReadStream.js
+++ b/node_modules/react-native-blob-util/class/ReactNativeBlobUtilReadStream.js
@@ -64,7 +64,7 @@ export default class ReactNativeBlobUtilReadStream {
 
     open() {
         if (!this.closed)
-            ReactNativeBlobUtil.readStream(this.path, this.encoding, this.bufferSize || 10240, this.tick || -1, this.streamId);
+            ReactNativeBlobUtil.readStream(this.path, this.encoding, this.bufferSize || (this.encoding=='base64' ? 4095 : 4096), this.tick || -1, this.streamId);
         else
             throw new Error('Stream closed');
     }

This issue body was partially generated by patch-package.

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

No branches or pull requests

1 participant