Skip to content

fix: send binary events without data with the HTTP transport - #681

Open
rida-ghreizy wants to merge 1 commit into
cloudevents:mainfrom
rida-ghreizy:fix/http-transport-no-body
Open

rida-ghreizy wants to merge 1 commit into
cloudevents:mainfrom
rida-ghreizy:fix/http-transport-no-body

Conversation

@rida-ghreizy

Copy link
Copy Markdown

Proposed Changes

The built-in httpTransport (src/transport/http/index.ts) always calls req.write(message.body). In binary mode, an event without data produces a message whose body is undefined. Node's HTTP client rejects that, so the emitter fails before anything is sent:

const emit = emitterFor(httpTransport("http://localhost:8080/"));
await emit(new CloudEvent({ source: "/demo", type: "demo" }));
// TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received undefined

Events without data are valid CloudEvents (for example notifications that only carry attributes). Structured mode already works because the body is the JSON envelope.

This PR only writes the body when there is one. The request is then sent with the ce-* headers and an empty body.

Description

New test in test/integration/emitter_factory_test.ts, "Sends a binary event that has no data". It starts a local HTTP server, because the nock interceptor used by the other tests accepts write(undefined) and would hide the error, then checks that the server received the event's ce-id.

Without the change the test fails with the chunk TypeError. With it, the full suite passes (npx mocha --exit --require ts-node/register ./test/integration/**/*.ts: 235 passing), and ESLint is clean on the changed files.

#657 replaces this transport with the Fetch API. This is a small fix for the current transport in the meantime.

httpTransport always called req.write(message.body). For a binary-mode
event without data the body is undefined, and Node throws
ERR_INVALID_ARG_TYPE, so the event was never sent. Only write the body
when there is one, and add a test against a real local server.

Signed-off-by: rida <ghreizyrida1@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant