Commit Graph

192 Commits

Author SHA1 Message Date
Calum Murray 6977113d7b
fix: creating an event does not error when the event attribute name is too long (#593)
* fix: creating an event does not error when the event attribute name is too long

* From the spec, the requirement for the length of attribute names is only a SHOULD, not a MUST. Currently, if the length is over 20 then the sdk throws an error, which I believe is incorrect because the length requirement is not a MUST.

Signed-off-by: Calum Murray <cmurray@redhat.com>

* fix(test): test expects not to throw

Signed-off-by: Calum Murray <cmurray@redhat.com>

---------

Signed-off-by: Calum Murray <cmurray@redhat.com>
2024-07-22 13:22:43 -04:00
Lance Ball 15f6505a58
BREAKING CHANGE: use string instead of enum for `Version` (#561)
TypeScript does not consider enum values equivalent, even if the string
representation is the same. So, when a module imports `cloudevents` and
also has a dependency on `cloudevents` this can cause conflicts where
the `CloudEvent.version` attribute is not considered equal when, in
fact, it is.

Changing the `enum` to a string is pretty straightforward, but should be
considered a breaking change since TypeScript dependents will
potentially fail the build with a change like this.

Signed-off-by: Lance Ball <lball@redhat.com>
2023-07-19 10:53:03 -04:00
Lance Ball 43c3584b98
fix: handle big integers in incoming events (#495)
* fix: handle big integers in incoming events

An event may have data that contains a BigInt. The builtin `JSON` parser
for JavaScript does not handle the `BigInt` types. The introduced
`json-bigint` dependency (34k) does.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/489

Signed-off-by: Lance Ball <lball@redhat.com>
2023-05-10 12:45:43 -04:00
Lucas Holmquist 2cb9364a25
feat!: remove node 12 and node 14 (#545)
* feat!: remove node 12 and node 14

Node 12 has been EOL since the end of April 2022 and Node 14 just became EOL at the end of April 2023

Signed-off-by: Lucas Holmquist <lholmqui@redhat.com>
2023-05-03 11:10:47 -04:00
Philip Sanetra 2d5fab1b71
fix: Make CloudEvent data field immutable and enumerable using Object.keys() (#515) (#516)
Signed-off-by: Philip Sanetra <code@psanetra.de>
2022-11-01 12:46:26 -04:00
Lance Ball ea94a4d779
fix: improve validation on extension attribute (#502)
* fix: improve validation on extension attribute

Fixes: https://github.com/cloudevents/sdk-javascript/issues/500

Adds a regular expression check to the attribute name validation code to
ensure that attribute names only use a-z0-9 (except for `data_base64`,
which apparently is an exception to the rule.

Signed-off-by: Lance Ball <lball@redhat.com>
2022-06-22 15:27:41 -04:00
Lance Ball 921e273ede
fix: allow `TypedArray` for binary data (#494)
* fix: allow `Uint16|8Array` for binary data

Previously we only considered `Uint32Array` binary data. This was an
oversight. This fixes that issue.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/491

Signed-off-by: Lance Ball <lball@redhat.com>
2022-06-15 00:51:36 -04:00
Lance Ball d6f52ca65f
fix: HTTP headers for extensions with false values (#493)
* fix: HTTP headers for extensions with false values

CloudEvent objects may include extensions that have a defined key and a
`false` value. This change ensures that HTTP messages for CloudEvents
containing these extension values include the appropriate headers.

Signed-off-by: Lance Ball <lball@redhat.com>
2022-06-14 17:44:06 -04:00
Lance Ball 0362a4f11c
feat!: add http transport and remove axios (#481)
* feat: add builtin HTTP emitter

Adds a builtin HTTP event emitter that can be used with `emitterFor()`
to send events over HTTP without pulling in any additional dependencies.

In the past we chose to keep this in our code base by considering axios a
peer dependency - users were required to include it in their projects
explicitly. In working on the HTTP emitter, it became more and more
apparent that the axios emitter was probably no longer needed, and in fact
I doubt it was really used at all. To use it, users would have been required
to do this, since it isn't exported at the top level.

const { axiosEmitter } = require("cloudevents/transport/http");

Based on this, I think the usage in the wild is probably very minimal,
and I like the idea of eliminating this dependency.

Signed-off-by: Lance Ball <lball@redhat.com>
2022-03-18 13:36:12 -04:00
Lance Ball b13bde9b49
feat: precompile cloudevent schema (#471)
* feat: precompile cloudevent schema

This commit modifies the build pipleline so that the cloudevent schema is
precompiled for runtime validation. This eliminates the need to compile the
schema at runtime, improving both performance and security.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/423

Signed-off-by: Lance Ball <lball@redhat.com>
2022-02-15 14:06:42 -05:00
Lance Ball 591d133f31
feat: add MQTT transport messaging (#459)
Add MQTT as a `Message` format.

This commit adds `MQTT` to the supported transport protocols by adding a `Binding` and the `MQTTMessage<T>` type, extending the base `Message` type, adding the MQTT fields for `payload`, `PUBLISH` and `User Properties`. The `payload` field directly maps to `Message#body`, while `User Properties` roughly maps to `Message#headers`, even though the properties here are not formatted with a `ce-` prefix like other transport protocols. This is per the spec. See: https://github.com/cloudevents/spec/blob/v1.0.1/mqtt-protocol-binding.md. 

Signed-off-by: Lance Ball <lball@redhat.com>
2022-01-14 11:41:15 -05:00
Lance Ball 5d1f744f50
feat: add support for kafka transport (#455)
This commit extends the `message` package to include Kafka transport.

Additionally, some of the type information has changed across the project
to more accurately reflect the type of `Message` (by including `T`).

Related: https://github.com/cloudevents/sdk-javascript/issues/390
Signed-off-by: Lance Ball <lball@redhat.com>
2022-01-07 16:14:09 -05:00
Lance Ball 2ac731eb88
chore(refactor): prefer interfaces over concrete classes (#457)
* chore(refactor): protocol bindings use interfaces

This change modifies the protocol binding interfaces such as `Binding`,
`Serializer` and the like to use the `CloudEventV1` interface instead of the
implementation class `CloudEvent`. This should make extending the interfaces
simpler as this work has grown out of efforts around the implementation of
a second transport interface, Kafka.

See: https://github.com/cloudevents/sdk-javascript/pull/455/

This commit also includes the addition of a generic type to the `Message`
interface, defaulting to `string`.

There is also some minor clean up involving what is exported from the
`message/http` modules. Now, instead of exporting the entire implementation,
only the `HTTP` binding implementation is exported, and it is then reexported
by `message`.

Also, a static `CloudEvent.cloneWith()` method has been added which the
instance methods now use.

Signed-off-by: Lance Ball <lball@redhat.com>

* fixup: make the `cloneWith()` method is dependent on interfaces

Signed-off-by: Lance Ball <lball@redhat.com>

* fixup: remove unnecessary cast

Signed-off-by: Lance Ball <lball@redhat.com>
2022-01-07 14:24:56 -05:00
Lance Ball 320354f750
chore: update cucumber dependency and remove prettier (#453)
The combination of prettier and eslint was causing some conflicting error
messages in formatting between VSCode and using npm in the CLI. For the most
part, there were only a couple of required formatting changes that prettier
was covering, so the change is minor.

The cucumber dependency had a major version bump and was carrying some unsafe
dependencies in the older version. This commit bumps to the new version and
makes appropriate configuration changes.

Signed-off-by: Lance Ball <lball@redhat.com>
2021-12-22 10:45:35 -05:00
Lance Ball 9a46e335f5
feat: add batch mode (#448)
Adds a batched content mode for incoming events.

```js
// It's possible for this to return 1:N events
const ceArray = HTTP.toEvent(req.headers, req.body);
```

Signed-off-by: Lance Ball <lball@redhat.com>
2021-12-07 15:36:10 -05:00
Lance Ball d941e2d4d9
feat: use generic type for CloudEvent data (#446)
Instead of using a big union of types, use a generic type for event data.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/445

Signed-off-by: Lance Ball <lball@redhat.com>
2021-11-29 17:03:12 -05:00
Lance Ball 52ea7de80d
fix: do not assume an empty content-type header is JSON (#444)
The parser for HTTP binary made the assumption that if there was no `content-type`
header in the incoming message, it should inject `application/json`. Discussion
about the rationale for this is in https://github.com/cloudevents/sdk-javascript/issues/441.

This commit, removes that injection and adds a test to ensure the bytes are
simply not parsed, but just passed along untouched.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/441

Signed-off-by: Lance Ball <lball@redhat.com>
2021-11-24 11:02:50 -05:00
Lance Ball b4266b1f37
chore: add test for text/plain data (#442)
Signed-off-by: Lance Ball <lball@redhat.com>
2021-11-23 18:05:15 -05:00
Grant Timmerman b38a48fa59
feat: add native logging with headers and body to CloudEvent (#437)
* feat: add native logging with headers and body to CloudEvent

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>

* ci: simplify

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>

* ci: simplify

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>

* ci: returns to return

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>

* ci: Update test/integration/cloud_event_test.ts

Co-authored-by: Lance Ball <lball@redhat.com>

* ci: revert symbol change

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>

* refactor: improve ts for toString

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>

Co-authored-by: Lance Ball <lball@redhat.com>
2021-10-04 11:11:38 -07:00
Lance Ball 2ff7852c36
fix: ensure source property has min length of 1 (#438)
Fixes: https://github.com/cloudevents/sdk-javascript/issues/383

Signed-off-by: Lance Ball <lball@redhat.com>
2021-09-29 09:55:37 -04:00
Lance Ball 2bd9a5a1e4
src!: remove support for 0.3 events (#425)
It has been nearly two years since 1.0 became final. This change removes
support for 0.3 events in the interest of simplifying the project a little.

Signed-off-by: Lance Ball <lball@redhat.com>
2021-08-27 10:34:32 -04:00
Lance Ball 061c122b86
chore: update eslint and prettier dependencies (#424)
There were some minor changes that resulted in a few code style changes, but not much.

Signed-off-by: Lance Ball <lball@redhat.com>
2021-08-04 15:51:37 -04:00
Lance Ball 7c05adee7b
fix: do not modify incoming event's specversion (#419)
Even if the specversion is totally invalid, we should not change the value
received in an incoming `Message`. Previously we defaulted to 1.0 if we did
not recognize the version number. This commit changes that, leaving the value
unmodified. We default to parsing this mystery event with the 1.0 spec. When
the event is validated with `event.validate()` we return `false`.

One additional small change to eliminate a prettier warning about `parer`
being previously declared.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/332
Fixes: https://github.com/cloudevents/sdk-javascript/issues/333

Signed-off-by: Lance Ball <lball@redhat.com>
2021-05-25 11:10:20 -04:00
Lance Ball 7f6b658858
fix: throw on validation if extensions are improperly named (#420)
Also fixes the case where UPPERCASED extension names were silently changed
to lowercase and then set as undefined. Even though uppercased extension
names are invalid, we should still accept them in incoming messsages and
only throw when validating the event.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/380

Signed-off-by: Lance Ball <lball@redhat.com>
2021-05-24 14:53:07 -04:00
Remi Cattiau 80d987c1f6
chore: add copyrights header and lint rules (#418)
Signed-off-by: Remi Cattiau <remi@cattiau.com>
2021-05-14 09:28:49 -04:00
Lance Ball db4be6b1da
src: be more forgiving parsing JSON as a string (#417)
* src: be more forgiving parsing JSON as a string

A simple string is considered valid JSON. However, our parsers do
not accept that unless the string has quotation marks. This commit
modifies the parser to look for strings declared as application/json
which do not begin with '[' '{' or '"' and surrounds them with
quotes.

Signed-off-by: Lance Ball <lball@redhat.com>
2021-05-13 12:46:36 -04:00
Lance Ball 0f11b02a01
fix: defaults properly handled for emitterFor() (#399)
* fix: defaults properly handled for emitterFor()

Fixes: https://github.com/cloudevents/sdk-javascript/issues/391
2021-04-05 13:14:04 -04:00
Lance Ball efe466ac7d
fix: ensure loose validation for isEvent and toEvent (#394)
The `HTTP.isEvent()` and `HTTP.toEvent()` functions both had some validation
code that violated the principle of loose validation when receiving an
event over HTTP. As discussed, the validation should be managed by the
receiver in this case with `event.validate()`

Signed-off-by: Lance Ball <lball@redhat.com>
2021-03-25 12:37:05 -04:00
Lance Ball e7d99eb882
test: add a test for extension names with all caps. (#389)
See: https://github.com/cloudevents/sdk-javascript/issues/380

Signed-off-by: Lance Ball <lball@redhat.com>
2021-03-08 16:07:24 -05:00
Remi Cattiau 43d9e01972 feat: allow ensureDelivery to be able to ensure delivery on emit
Signed-off-by: Remi Cattiau <remi@cattiau.com>
2020-11-17 08:41:16 -05:00
Remi Cattiau d418a50c56 ci: add unit test for emitter
Signed-off-by: Remi Cattiau <remi@cattiau.com>
2020-11-17 08:41:16 -05:00
Lucas Holmquist 875f70017a
feat!: Remove All API's that are labeled "Remove in 4.0" (#362)
* feat!: remove all 4.0 deprecation.

* This removes all the APIs that were deprecated in the 3.x releases and marked as "remove in 4.0".

* Also removes any tests associated with those API's

* squash: remove axios as a dependecy


Signed-off-by: Lucas Holmquist <lholmqui@redhat.com>
2020-11-13 14:12:26 -05:00
Lance Ball f6be285b83
fix: extend Node.js IncomingHttpHeaders in our Headers type (#346)
This commit extends Node.js IncomingHttpHeaders in our Headers type.
Changes the Headers type to make it more compatible with Node.js TypeScript projects.

Signed-off-by: Lance Ball <lball@redhat.com>
2020-10-06 15:50:33 -04:00
Lance Ball 14468980f7
fix: do not alter an event's data attribute (#344)
* 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: https://github.com/cloudevents/sdk-javascript/issues/343

Signed-off-by: Lance Ball <lball@redhat.com>
2020-10-06 08:20:54 -04:00
Lance Ball e334b6eceb
feat: add emitterFactory and friends (#342)
* feat: add emitterFactory and friends

This commit adds an emitterFactory function that returns an EmitterFunction
object. The EmitterFunction may be used to emit events over a supported
network transport layer. Currently, only HTTP is supported.

Parameters provided to the emitterFactory are the transport Binding (only
HTTP supported), the encoding mode (Mode.BINARY or Mode.STRUCTURED), and
a TransportFunction.

The implementation for emitBinary and emitStructured has been replaced
with this simple pattern and those two functions have been removed.

Example:

```js
// The endpoint URL that will receive the event
const sink = 'https://my-event-sink';

// A function that uses Axios to send a message over HTTP
function axiosEmitter(message: Message, options?: Options): Promise<unknown> {
  return axios.post(sink, message.body, { headers: message.headers, ...options });
}

// Create an event emitter
const emit = emitterFactory(HTTP, Mode.BINARY, axiosEmitter);

// Emit an event, sending it to the endpoint URL
emit(new CloudEvent{ source: '/example', type: 'example' });
```

Signed-off-by: Lance Ball <lball@redhat.com>
2020-09-25 17:25:15 -04:00
Lance Ball 1fa3a05aed
feat: add a constructor parameter for loose validation (#328)
* feat: add a constructor parameter for loose validation

This commit adds a second, optional boolean parameter to the `CloudEvent`
constructor. When `false` is provided, the event constructor will not
perform validation of the event properties, values and extension names.

This commit also modifies the ValidationError class so that the error message
string includes the JSON.stringified version of any schema validation
errors. It also makes the HTTP.toEvent() function create CloudEvent
objects with loose/no validation.

Incorporates comments from https://github.com/cloudevents/sdk-javascript/pull/328

Fixes: https://github.com/cloudevents/sdk-javascript/issues/325

Signed-off-by: Lance Ball <lball@redhat.com>
2020-09-08 15:32:16 -04:00
Lance Ball 6cd310c141
src(event)!: make the event's time property only a string (#330)
Previously, the event's `time` property could be either a string or a date.
this commit modifies that to ensure that the object can only be created with
a timestamp in string format. As long as the string is a valid date, that
can be parsed by `new Date(Date.parse(str))` then whenever the event is
serialized as JSON, the `time` attribute will be formatted as per RFC 3339.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/326

Signed-off-by: Lance Ball <lball@redhat.com>
2020-09-01 10:10:16 -04:00
Lance Ball f3953a9a5a
feat: introduce Message, Serializer, Deserializer and Binding interfaces (#324)
* lib(messages): Implement a 4.0 Messages and other supporting interfaces

This commit introduces the Message, Serializer and Deserializer, and Binding
interfaces used to convert a CloudEvent into a Message that can be sent across
a transport protocol. The first protocol implemented for this is HTTP, and some
of the functionality formerly in src/transport/http has been simplified,
reduced and/or moved to /src/messages/http.

Test for V1 and V3 events are in place. Conformance tests have been modified to use
these new interfaces vs. the HTTP Receiver class.

Signed-off-by: Lance Ball <lball@redhat.com>
2020-08-26 18:26:50 -04:00
Lance Ball b5cf8865b9
test: implement pending tests leftover from TS rewrite (#315)
This commit implements 4 of the 6 pending tests that were not completed
during the TypeScript rewrite. The two tests that were not implemented
were (one for each of v1 and v03):

```
it("returns a JSON string even if format is invalid");
```

I don't really know what that's supposed to be/mean, so I removed them.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/232

Signed-off-by: Lance Ball <lball@redhat.com>
2020-08-12 16:49:21 -04:00
Lucas Holmquist a5249de487
chore: fix promise tests to break the build when they fail (#305)
* squash: return the promise that is being tested

fixes #303 

Signed-off-by: Lucas Holmquist <lholmqui@redhat.com>
2020-08-07 09:00:49 -04:00
Lucas Holmquist ad0c4340b2
fix: Add Correct Headers to emitted Binary Event (#302)
* fix: Binary emitter should add a basic application/json header.

fixes #301

Signed-off-by: Lucas Holmquist <lholmqui@redhat.com>
2020-08-06 10:36:53 -04:00
Lucas Holmquist a7e0aa07ac
lib: expose constants in a more intuitive way (#299)
* Constants can now be accessed more easily from the top level import/require

users can now do `const { CONSTANTS } = require('cloudevents')`

fixes #298

Signed-off-by: Lucas Holmquist <lholmqui@redhat.com>
2020-08-04 09:42:28 -04:00
Lance Ball ed9ea956d7
fix: ensure that data encoded as base64 is parsed as an object (#285)
The 0.3 specification states that `datacontentencoding` may be set to base64.
If an incoming event arrives over HTTP with this value set, and the content type
is either application/json or application/cloudevents+json, then ensure that
the data is decoded and parsed.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/284
See: https://github.com/cloudevents/sdk-javascript/pull/282


Signed-off-by: Lance Ball <lball@redhat.com>
2020-07-30 08:39:47 -04:00
Lance Ball b99f728190
fix: ensure that event data can be an array, number, boolean or null (#281)
The schema incorrectly limits data values to only object and string. This is
incorrect, since JSON can be an array, boolean, a single number or null as well.

This commit modifies the schema to allow for array, boolean and null, and adds
tests.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/280

Signed-off-by: Lance Ball <lball@redhat.com>
2020-07-29 08:35:23 -04:00
Lucas Holmquist 3c8273f114
lib: validate extension values (#251)
BREAKING CHANGE:

This validates the value of the cloud event extension based on the spec, 
https://github.com/cloudevents/spec/blob/master/spec.md#type-system

Signed-off-by: Lucas Holmquist <lholmqui@redhat.com>
2020-07-27 13:19:09 -04:00
Grant Timmerman 129ec485d9
refactor: combine v03 and v1 event interfaces, specs and schemas into single files(#270)
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
2020-07-24 15:35:18 -04:00
Grant Timmerman a6124cc350
refactor: simplify parser logic and duplicated code (#269)
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
2020-07-24 15:08:52 -04:00
Lance Ball 9ae32c76cb
lib: change Receiver#accept() to be static Receiver.accept (#271)
Note that I did not add a third `Protocol` parameter to `Receiver.accept` since
that would not currently be used. It's non-breaking to add a third parameter
in the future once additional protocols are supported.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/266
Fixes: https://github.com/cloudevents/sdk-javascript/issues/261

BREAKING CHANGE

Signed-off-by: Lance Ball <lball@redhat.com>
2020-07-24 15:07:46 -04:00
Grant Timmerman 4b54b272a5
feat: simplify validation logic/imports (#265)
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
2020-07-23 16:09:07 -07:00
Lance Ball a7c326b48c fix: do not require an HTTP body on incoming binary event messages
This commit modifies the HTTP receivers/parsers to allow for the incoming body
of an HTTP request to be empty if the event message is sent using the binary
mode. In structured mode, a `ValidationError` will still be thrown, since the
entire event must be encoded in the HTTP body.

Signed-off-by: Lance Ball <lball@redhat.com>
2020-07-23 14:22:14 -04:00