Skip to content

Non Uint32Array binary data does not set the CloudEvent's data_base64 value #491

Closed
@matthewchngshopback

Description

@matthewchngshopback

When binary data, e.. Buffer or Uint8Array, is not converted to Uint32Array before being set as a CloudEvent's data, the data_base64 will not be set because isBinary() detection fails. This results in structured mode to go out of spec by not having the expected data_base64 attribute but instead include an unexpected data object.

If we convert the Buffer to Uint32Array and set as CloudEvent's data, then binary mode message body will contain wrong byte sequence as it will be that of Uint32Array rather than the expected Uint8Array. This can be overcome by wrapping the body from HTTP.binary() with Buffer.from(body) again before feeding it axios(). Also, Uint32Array binary data in binary mode does not work with the provided emitterFor() as the req.write() only allows Buffer or Uint8Array.

Steps to Reproduce

const { CloudEvent, HTTP } = require("cloudevents")

const x = Buffer.from([0x01, 0x02, 0x03, 0x4])
const ce = new CloudEvent({ type: 'test', source:'testor', data: x })
const { body } = HTTP.structured(ce)

console.log(`ce: ${ce}`)
// ce: {"id":"084afd51-9dd2-4f0c-8f4f-517189d6ca13","time":"2022-06-02T07:34:30.982Z","type":"test","source":"testor","specversion":"1.0","data":{"type":"Buffer","data":[1,2,3,4]}}

console.log(`structured body: ${body}`)
// structured body: {"id":"084afd51-9dd2-4f0c-8f4f-517189d6ca13","time":"2022-06-02T07:34:30.982Z","type":"test","source":"testor","specversion":"1.0","data":{"type":"Buffer","data":[1,2,3,4]}}

const x32 = new Uint32Array(x)
const ceX32 = new CloudEvent({ type: 'test', source:'testor', data: x32 })
const { body: bodyX32 } = HTTP.structured(ceX32)

console.log(`ceX32: ${ceX32}`)
// ceX32: {"id":"ca9fb24f-adc1-4c8a-82e2-ff0f69b26cbd","time":"2022-06-02T07:38:27.833Z","type":"test","source":"testor","specversion":"1.0","data_base64":"AQIDBA==","data":{"0":1,"1":2,"2":3,"3":4}}

console.log(`structured bodyX32: ${bodyX32}`)
// structured bodyX32: {"id":"60c32fbd-3868-4e7a-bbd0-cf2920440ec7","time":"2022-06-02T07:39:00.382Z","type":"test","source":"testor","specversion":"1.0","data_base64":"AQIDBA=="}

const { body: bodyBinary } = HTTP.binary(ce)
const { body: bodyBinaryX32 } = HTTP.binary(ceX32)

console.log(x)
console.log(bodyBinary)                 // matched
console.log(bodyBinaryX32)              // no match
console.log(Buffer.from(bodyBinaryX32)) // matched

// <Buffer 01 02 03 04>
// <Buffer 01 02 03 04>
// Uint32Array(4) [ 1, 2, 3, 4 ]
// <Buffer 01 02 03 04>

Expected Behavior
We should be able to pass TypedArray directly as data and both the structured and binary modes should create valid bodies that matches the spec.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions