-
Notifications
You must be signed in to change notification settings - Fork 69
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
Binary data in binary format is not binary? #343
Comments
Hi @evankanderson can you please try the let newEvent = event.cloneWith({
source: sourceUrl,
type: responseEventType,
datacontenttype: resp.headers.get("Content-Type"),
data: new Uint32Array(await resp.arrayBuffer())
}); This will get around the assignment errors. But you are correct that when serializing an event, it will be encoded as base64. This is based on a reading of the JSON format spec.
The assumption made in this module is that when serializing for HTTP, the event is converted into a JSON representation, and that data should be encoded as base64. But your use case clearly shows that this should not happen. |
(I'm trying to avoid the overheads of base64 in my image pipeline, and my next step was expecting an image in the POST body.) Thanks for the suggestion on assigning inside |
Sure thing. The idea behind
It is. But I should have a fix soon so that the |
When setting an event's data attribute we were trying to be really clever and this is problematic. Instead, keep the data attribute unchanged. Per the 1.0 specification, the data attribute is still inspected to determine if it is binary, and if so, a data_base64 attribute is added with the contents of the data property encoded as base64. Fixes: cloudevents#343 Signed-off-by: Lance Ball <lball@redhat.com>
When setting an event's data attribute we were trying to be really clever and this is problematic. Instead, keep the data attribute unchanged. Per the 1.0 specification, the data attribute is still inspected to determine if it is binary, and if so, a data_base64 attribute is added with the contents of the data property encoded as base64. Fixes: cloudevents#343 Signed-off-by: Lance Ball <lball@redhat.com>
Hi @evankanderson - I have a fix in this PR #344. If you can, would you mind giving it a try with your use case? |
I think you should be able to do that with the changes in #344, but you could not convert it to a fetch(mediaUrl).then(async (resp) => {
const newEvent = new CloudEvent({
source: sourceUrl,
type: responseEventType,
datacontenttype: resp.headers.get("Content-Type"),
data: resp.body
})
// Do whatever you need to do to read newEvent.data as a ReadableStream,
// eventually setting a variable with the resultant value
const data = ...
const response = HTTP.binary(
newEvent.cloneWith( { data } );
);
res.status(200).set(response.headers).send(response.body);
}); |
I have a demo that I'm wrapping up, but I'll try figuring out how to patch this in on a branch probably tomorrow morning once I've got the demo recorded. 😁 |
* fix: do not alter an event's data attribute When setting an event's data attribute we were trying to be really clever and this is problematic. Instead, keep the data attribute unchanged. Per the 1.0 specification, the data attribute is still inspected to determine if it is binary, and if so, a data_base64 attribute is added with the contents of the data property encoded as base64. Fixes: #343 Signed-off-by: Lance Ball <lball@redhat.com>
Describe the Bug
I'm trying to send a cloudevent with
datacontenttype
ofimage/jpeg
and the actual payload being the bytes of the image. It seems like the current code inasData
will always base-64 the content when fetching data. (Possibly twice, if I read the code at line 12 ofhttp/index.js
correctly -- once forevent.data
, and once forasData
.)Steps to Reproduce
I'd like the following code to work, but it appears that
ArrayBuffer
andBlob
are not supported types for data:If I try to convert the arrayBuffer to a Uint32Array (
newEvent.data = new Uint32Array(await resp.arrayBuffer());
), I get the following exception:Expected Behavior
I'd like the above code to work, or to be able to find an example of sending a binary file (i.e. an image or a proto or something) as an HTTP CloudEvent.
Additional context
I am terrible at Javascript, so feel free to point out I'm holding it wrong.
The text was updated successfully, but these errors were encountered: