Repository navigation
fix: encode the bytes of typed array data in data_base64 - #682
Open
rida-ghreizy wants to merge 1 commit into
Open
rida-ghreizy wants to merge 1 commit into
rida-ghreizy wants to merge 1 commit into
Conversation
asBuffer used Buffer.from(typedArray), which copies the element values truncated to one byte each, so Uint16Array, Int32Array, Float32Array and other wide arrays were corrupted in data_base64. Copy the underlying bytes of the view instead, and test every typed array type against its real bytes. Signed-off-by: rida <ghreizyrida1@gmail.com>
This branch has not been deployed
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.
Proposed Changes
When an event's
datais a typed array, theCloudEventconstructor setsdata_base64 = asBase64(data).asBuffer(src/event/validation.ts) converted the array withBuffer.from(typedArray). That copies the element values, one byte per element, not the array's bytes. Anything wider than 8 bits was corrupted, and structured messages (HTTP, Kafka, MQTT) carry thatdata_base64:datadata_base64beforenew Uint8Array([1, 2, 255])1,2,2551,2,255new Uint16Array([256, 1])0,10,1,1,0new Float32Array([1.5])10,0,192,63Uint16Arrayview over part of a larger bufferThis PR copies the underlying bytes with
Buffer.from(view.buffer, view.byteOffset, view.byteLength). Buffers are returned as before, and 8-bit arrays produce the same output as before.Description
The existing tests in
test/integration/spec_1_tests.tscomparedata_base64withasBase64(...)of the same data, so they pass whateverasBase64does. I added, for every typed array type, a test that comparesdata_base64with the base64 of the array's actual bytes, plus a test for a view over part of a larger buffer.Without the change, 7 of the new tests fail: the 16-, 32- and 64-bit types and the view. With it, the full suite passes (
npx mocha --exit --require ts-node/register ./test/integration/**/*.ts: 244 passing), and ESLint is clean on the changed files.