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.

Emit Binary, but receives Structured

See original GitHub issue

Describe the Bug

I was playing around with a small example where i Emit a binary cloud event. something like this:

'use strict';

const { Emitter, CloudEvent } = require('cloudevents');

const em = new Emitter({ url: 'http://localhost:3000/cloudeventy' });
const ce = new CloudEvent({
  type: 'com.lholmquist.cloudeventy.fun',
  source: 'fun-with-cloud-events',
  data: 'DATA'
});


em.send(ce)
  .then(response => {
    console.log(response);
  })
  .catch(err => {
    console.log(err);
  });

Then i have a basic express.js application running on port 3000 with the route cloudeventy to receive the event coming in, which looks similar to this:

app.post('/cloudeventy', (req, res) => {
  console.log(req.headers);
  const ce = Receiver.accept(req.headers, req.body);
  res.send('...');
});

However, I get an error saying that “Cannot read property specversion of undefned”.

I also console logged out my request headers too.

{
  accept: 'application/json, text/plain, */*',
  'content-type': 'application/cloudevents+json; charset=utf-8',
  'ce-id': '51568118-f04b-4491-9895-a0e1bfa4ac09',
  'ce-type': 'com.lholmquist.cloudeventy.fun',
  'ce-source': 'fun-with-cloud-events',
  'ce-specversion': '1.0',
  'ce-time': '2020-08-04T23:39:35.162Z',
  'user-agent': 'axios/0.19.2',
  'content-length': '4',
  host: 'localhost:3000',
  connection: 'close'
}
TypeError: Cannot read property 'specversion' of undefined
    at getVersion (/home/lucasholmquist/develop/just-playing/fun-with-cloudevents/node_modules/cloudevents/dist/transport/receiver.js:92:79)
    at Function.accept (/home/lucasholmquist/develop/just-playing/fun-with-cloudevents/node_modules/cloudevents/dist/transport/receiver.js:45:25)
    at /home/lucasholmquist/develop/just-playing/fun-with-cloudevents/index.js:14:23
    at Layer.handle [as handle_request] (/home/lucasholmquist/develop/just-playing/fun-with-cloudevents/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/lucasholmquist/develop/just-playing/fun-with-cloudevents/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/lucasholmquist/develop/just-playing/fun-with-cloudevents/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/lucasholmquist/develop/just-playing/fun-with-cloudevents/node_modules/express/lib/router/layer.js:95:5)
    at /home/lucasholmquist/develop/just-playing/fun-with-cloudevents/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/lucasholmquist/develop/just-playing/fun-with-cloudevents/node_modules/express/lib/router/index.js:335:12)
    at next (/home/lucasholmquist/develop/just-playing/fun-with-cloudevents/node_modules/express/lib/router/index.js:275:10)

When i step through the code on the receiving end, it treats it as a structured event, not a binary event, even though that was how it was sent.

There is code to determine the mode, that parses the content-type header. https://github.com/cloudevents/sdk-javascript/blob/master/src/transport/receiver.ts#L66 and if it sees application/cloudevents then it says it is a “structured” event. The problem is that both the structured and binary emitters add the same content-type header.

Binary Emitter Headers: https://github.com/cloudevents/sdk-javascript/blob/master/src/transport/http/binary_emitter.ts#L23

Structured Emitter Headers: https://github.com/cloudevents/sdk-javascript/blob/master/src/transport/http/structured_emitter.ts#L8

It seems that the check for “Binary Headers” should happen first, unless the headers are wrong for the binary version

Extra Context:

These code examples i gave above i think would make a good example, since we don’t have an emitter example yet

@lance @grant thoughts?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
lancecommented, Aug 5, 2020
1reaction
lholmquistcommented, Aug 5, 2020

assigning to myself to fix if no one minds

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I properly emit binary data from a SecureString, so ...
Within my application, the data from the PasswordBox gets passed as a SecureString to an encryption method. What I need to be able...
Read more >
DRY reading binary structured data - New to Julia
Hi, I'm trying to create a Julia reader for a binary block-based file format and run into different issues depending on which approach...
Read more >
Parsing Binary Data Formats | Cloudbees Blog
Most binary file formats start with some "signature bytes," sometimes also known as "magic numbers." This allows decoders to quickly identify ...
Read more >
Encoding | Protocol Buffers - Google Developers
Message Structure. A protocol buffer message is a series of key-value pairs. The binary version of a message just uses the field's number...
Read more >
JavaScript/TypeScript SDK for CloudEvents - GitHub
Receiving and Emitting Events. Receiving Events. You can choose any popular web framework for port binding. A CloudEvent object can be created by ......
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