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: don't always convert bytes to text in streamToPromise #182

Merged
merged 2 commits into from
Dec 16, 2021

Conversation

ricellis
Copy link
Member

@ricellis ricellis commented Dec 9, 2021

streamToPromise calls toString() on the concated Buffer which defaults to utf-8 encoding and consequently can mangle non utf-8 content. This problem surfaced via IBM/cloudant-node-sdk#546.

  • Remove toString() call when using Buffer type.

The problem is resolved by not calling toString() as the Buffer bytes may be passed as-is to the server for a byte stream. In cases where the stream is using string or objectMode not Buffer then the existing Buffer.isBuffer(results[0]) check puts the chunk as-is into the results array (i.e. it is an array of strings or objects).

  • Update doc in streamToPromise to indicate it is not always text.
  • Add test for binary type stream.
  • Update assertions for Buffer streams to assert length and checksums.
Checklist
  • npm test passes (tip: npm run lint-fix can correct most style issues)
  • tests are included
  • documentation is changed or added

Update doc in streamToPromise to indicate it is not always text.
Remove `toString()` call when using `Buffer` type.
Add test for binary type stream.
Update assertions for `Buffer` streams to assert length and checksums.
@padamstx padamstx requested a review from dpopp07 December 9, 2021 14:41
Copy link
Member

@dpopp07 dpopp07 left a comment

Choose a reason for hiding this comment

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

Looks good! 👍

@dpopp07
Copy link
Member

dpopp07 commented Dec 13, 2021

@ricellis Before merging, I want to make sure you're aware of this invocation - https://github.com/IBM/node-sdk-core/blob/main/lib/request-wrapper.ts#L445

It is calling Buffer.from(...), which may not be necessary now that Buffers will be returned. Perhaps another conditional is needed there to avoid a bad type error? Let me know what you think

@ricellis
Copy link
Member Author

It is calling Buffer.from(...), which may not be necessary now that Buffers will be returned. Perhaps another conditional is needed there to avoid a bad type error? Let me know what you think

Agreed. Though Buffer.from(buffer) is valid so it doesn't error (I did test streams going through the full core/client SDK). It just copies the Buffer again, but clearly it is wasteful to do so I'll add a check to avoid it.

@ricellis
Copy link
Member Author

@dpopp07 I added the check in 1e9e691. FWIW I'm actually a little unsure about the "else" case - I don't understand what the expectation is for a returned Array when the stream is of string or in objectMode so I don't know that Buffer.from is actually what is wanted then, but the behaviour of that part hasn't been changed at all by this and I'm reluctant to make alterations outside of the scope of fixing the observed byte stream problem.

@ricellis
Copy link
Member Author

@dpopp07 are you OK with the latest addition here? Would you like me to squash the commits?

@dpopp07
Copy link
Member

dpopp07 commented Dec 16, 2021

@ricellis Sorry, I've been distracted by urgent work the last few days. I looked at that last change and it looks great.

but the behaviour of that part hasn't been changed at all by this and I'm reluctant to make alterations outside of the scope of fixing the observed byte stream problem

Agreed, no need to start tweaking existing behavior. If that becomes an issue, we will address it but it never has been in the past.

Unless there are any more changes you're planning to make, I will merge today.

@ricellis
Copy link
Member Author

@dpopp07 np, I know the feeling! I'm not planning anything else for this so happy for it to merge whenever you get the chance, thanks so much.

@dpopp07 dpopp07 merged commit 7fe261b into main Dec 16, 2021
@dpopp07 dpopp07 deleted the 2974-binary-compression branch December 16, 2021 16:22
ibm-devx-sdk pushed a commit that referenced this pull request Dec 16, 2021
## [2.17.4](v2.17.3...v2.17.4) (2021-12-16)

### Bug Fixes

* don't always convert bytes to text in streamToPromise ([#182](#182)) ([7fe261b](7fe261b))
@ibm-devx-sdk
Copy link

🎉 This PR is included in version 2.17.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

JurajNyiri pushed a commit to JurajNyiri/node-sdk-core that referenced this pull request Aug 22, 2024
Generated SDK source code using:
- Generator version 3.26.0
- Specification version 1.0.0-dev0.0.31
- Automation (cloudant-sdks) version 7c23742
JurajNyiri pushed a commit to JurajNyiri/node-sdk-core that referenced this pull request Aug 22, 2024
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.

3 participants