fix: recover when TextDecoder rejects Uint8Array views#965
Open
jeanpierrecarvalho wants to merge 1 commit intoanthropics:mainfrom
Open
fix: recover when TextDecoder rejects Uint8Array views#965jeanpierrecarvalho wants to merge 1 commit intoanthropics:mainfrom
jeanpierrecarvalho wants to merge 1 commit intoanthropics:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #883
Summary
This hardens the SDK stream decoding path for environments where
TextDecoder.decode()rejectsUint8Arrayviews or subarrays and only accepts a plainArrayBuffer.The runtime change keeps the normal path the same, but retries with
bytes.slice().bufferwhendecodeUTF8()hits thatTypeError.Why
On AWS Lambda
nodejs22.x, users reported stream decoding failures with:Failed to execute 'decode' on 'TextDecoder': parameter 1 is not of type 'ArrayBuffer'I reproduced the SDK-level failure mode around the public streaming helper path and verified that the fallback recovers cleanly.
What changed
TextDecoderinstance instead of caching a bounddecodefunctiondecoder.decode(bytes)firstTypeError, retry withdecoder.decode(bytes.slice().buffer)decodeUTF8()messages.stream()under stricterTextDecoderbehaviorValidation
./node_modules/.bin/jest tests/internal/utils/bytes.test.ts tests/api-resources/MessageStream.test.ts --runInBandnodejs22.xrepro using the publicmessages.stream()pathArrayBuffererrorNotes
I could not make a minimal direct
TextDecoder.decode(Uint8Array view)probe fail natively on the exact Lambda runtime I tested in isolation, so I don't want to overclaim there.But the SDK-level failure mode is reproducible under stricter decoder behavior, and this fallback fixes that path while preserving the normal case.