question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Binary data in binary format is not binary?

See original GitHub issue

Describe the Bug I’m trying to send a cloudevent with datacontenttype of image/jpeg and the actual payload being the bytes of the image. It seems like the current code in asData will always base-64 the content when fetching data. (Possibly twice, if I read the code at line 12 of http/index.js correctly – once for event.data, and once for asData.)

Steps to Reproduce I’d like the following code to work, but it appears that ArrayBuffer and Blob are not supported types for data:

  fetch(mediaUrl).then(async (resp) => {
    let newEvent = event.cloneWith({
      source: sourceUrl,
      type: responseEventType,
      datacontenttype: resp.headers.get("Content-Type")
    })
    // I want this. This definitely does not work, and an empty object gets encoded.
    newEvent.data = await resp.arrayBuffer();  // or .blob()
    const response = HTTP.binary(newEvent);
    res.status(200).set(response.headers).send(response.body);
  });

If I try to convert the arrayBuffer to a Uint32Array (newEvent.data = new Uint32Array(await resp.arrayBuffer());), I get the following exception:

TypeError: Cannot assign to read only property ‘data_base64’ of object ‘[object Object]’

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
evankandersoncommented, Sep 29, 2020

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. 😁

0reactions
lancecommented, Sep 29, 2020

it would be nice if I could assign a ReadableStream to data for the binary case

I think you should be able to do that with the changes in #344, but you could not convert it to a Message. You would need to do something like this.

  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);
  });
Read more comments on GitHub >

github_iconTop Results From Across the Web

A little diddy about binary file formats - BetterExplained
One reason binary files are efficient is because they can use all 8 bits in a byte, while most text is constrained to...
Read more >
What is meant by binary and non-binary data? - Quora
Binary data means the data is defined by two symbols 0 and 1, non binary data can be anything other than binary like...
Read more >
Binary file - Wikipedia
A binary file is a computer file that is not a text file. The term "binary file" is often used as a term...
Read more >
What is a binary file and how does it work? - TechTarget
Binary files are not human readable and require a special program or hardware processor that knows how to read the data inside the...
Read more >
Is there such a thing as "non-binary" data? - Stack Overflow
Binary data has no intrinsic meaning. It means whatever the program that wrote it and the program that read it expect it to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found