Skip to content

Commit 2bd9a5a

Browse files
authored
src!: remove support for 0.3 events (cloudevents#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>
1 parent 36f5e2b commit 2bd9a5a

File tree

9 files changed

+19
-546
lines changed

9 files changed

+19
-546
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/event/cloudevent.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@
66
import { v4 as uuidv4 } from "uuid";
77
import { Emitter } from "..";
88

9-
import {
10-
CloudEventV03,
11-
CloudEventV03Attributes,
12-
CloudEventV03OptionalAttributes,
13-
CloudEventV1,
14-
CloudEventV1Attributes,
15-
CloudEventV1OptionalAttributes,
16-
} from "./interfaces";
9+
import { CloudEventV1, CloudEventV1Attributes, CloudEventV1OptionalAttributes } from "./interfaces";
1710
import { validateCloudEvent } from "./spec";
1811
import { ValidationError, isBinary, asBase64, isValidType } from "./validation";
1912

@@ -30,7 +23,7 @@ export const enum Version {
3023
* interoperability across services, platforms and systems.
3124
* @see https://github.com/cloudevents/spec/blob/v1.0/spec.md
3225
*/
33-
export class CloudEvent implements CloudEventV1, CloudEventV03 {
26+
export class CloudEvent implements CloudEventV1 {
3427
id: string;
3528
type: string;
3629
source: string;
@@ -58,7 +51,7 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 {
5851
* @param {object} event the event properties
5952
* @param {boolean?} strict whether to perform event validation when creating the object - default: true
6053
*/
61-
constructor(event: CloudEventV1 | CloudEventV1Attributes | CloudEventV03 | CloudEventV03Attributes, strict = true) {
54+
constructor(event: CloudEventV1 | CloudEventV1Attributes, strict = true) {
6255
// copy the incoming event so that we can delete properties as we go
6356
// everything left after we have deleted know properties becomes an extension
6457
const properties = { ...event };
@@ -197,13 +190,7 @@ See: https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system`);
197190
* @return {CloudEvent} returns a new CloudEvent
198191
*/
199192
public cloneWith(
200-
options:
201-
| CloudEventV1
202-
| CloudEventV1Attributes
203-
| CloudEventV1OptionalAttributes
204-
| CloudEventV03
205-
| CloudEventV03Attributes
206-
| CloudEventV03OptionalAttributes,
193+
options: CloudEventV1 | CloudEventV1Attributes | CloudEventV1OptionalAttributes,
207194
strict = true,
208195
): CloudEvent {
209196
return new CloudEvent(Object.assign({}, this.toJSON(), options) as CloudEvent, strict);

src/event/interfaces.ts

Lines changed: 0 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -140,139 +140,3 @@ export interface CloudEventV1OptionalAttributes {
140140
*/
141141
[key: string]: unknown;
142142
}
143-
144-
/**
145-
* The object interface for CloudEvents 0.3.
146-
* @see https://github.com/cloudevents/spec/blob/v0.3/spec.md
147-
*/
148-
149-
export interface CloudEventV03 extends CloudEventV03Attributes {
150-
// REQUIRED Attributes
151-
/**
152-
* [REQUIRED] Identifies the event. Producers MUST ensure that `source` + `id`
153-
* is unique for each distinct event. If a duplicate event is re-sent (e.g. due
154-
* to a network error) it MAY have the same `id`. Consumers MAY assume that
155-
* Events with identical `source` and `id` are duplicates.
156-
* @required Non-empty string. Unique within producer.
157-
* @example An event counter maintained by the producer
158-
* @example A UUID
159-
*/
160-
id: string;
161-
/**
162-
* [REQUIRED] The version of the CloudEvents specification which the event
163-
* uses. This enables the interpretation of the context. Compliant event
164-
* producers MUST use a value of `1.0` when referring to this version of the
165-
* specification.
166-
* @required MUST be a non-empty string.
167-
*/
168-
specversion: string;
169-
}
170-
171-
export interface CloudEventV03Attributes extends CloudEventV03OptionalAttributes {
172-
/**
173-
* [REQUIRED] Identifies the context in which an event happened. Often this
174-
* will include information such as the type of the event source, the
175-
* organization publishing the event or the process that produced the event. The
176-
* exact syntax and semantics behind the data encoded in the URI is defined by
177-
* the event producer.
178-
* Producers MUST ensure that `source` + `id` is unique for each distinct event.
179-
* An application MAY assign a unique `source` to each distinct producer, which
180-
* makes it easy to produce unique IDs since no other producer will have the same
181-
* source. The application MAY use UUIDs, URNs, DNS authorities or an
182-
* application-specific scheme to create unique `source` identifiers.
183-
* A source MAY include more than one producer. In that case the producers MUST
184-
* collaborate to ensure that `source` + `id` is unique for each distinct event.
185-
* @required Non-empty URI-reference
186-
*/
187-
source: string;
188-
/**
189-
* [REQUIRED] This attribute contains a value describing the type of event
190-
* related to the originating occurrence. Often this attribute is used for
191-
* routing, observability, policy enforcement, etc. The format of this is
192-
* producer defined and might include information such as the version of the
193-
* `type` - see
194-
* [Versioning of Attributes in the Primer](primer.md#versioning-of-attributes)
195-
* for more information.
196-
* @required MUST be a non-empty string
197-
* @should SHOULD be prefixed with a reverse-DNS name. The prefixed domain dictates the
198-
* organization which defines the semantics of this event type.
199-
* @example com.github.pull.create
200-
* @example com.example.object.delete.v2
201-
*/
202-
type: string;
203-
}
204-
205-
export interface CloudEventV03OptionalAttributes {
206-
/**
207-
* The following fields are optional.
208-
*/
209-
210-
/**
211-
* [OPTIONAL] Describes the content encoding for the data attribute for when the
212-
* data field MUST be encoded as a string, like with structured transport binding
213-
* modes using the JSON event format, but the datacontenttype indicates a non-string
214-
* media type. When the data field's effective data type is not String, this attribute
215-
* MUST NOT be set and MUST be ignored when set.
216-
*/
217-
datacontentencoding?: string;
218-
219-
/**
220-
* [OPTIONAL] Content type of `data` value. This attribute enables `data` to
221-
* carry any type of content, whereby format and encoding might differ from that
222-
* of the chosen event format. For example, an event rendered using the
223-
* [JSON envelope](./json-format.md#3-envelope) format might carry an XML payload
224-
* in `data`, and the consumer is informed by this attribute being set to
225-
* "application/xml". The rules for how `data` content is rendered for different
226-
* `datacontenttype` values are defined in the event format specifications; for
227-
* example, the JSON event format defines the relationship in
228-
* [section 3.1](./json-format.md#31-handling-of-data).
229-
*/
230-
datacontenttype?: string;
231-
/**
232-
* [OPTIONAL] A link to the schema that the data attribute adheres to.
233-
* Incompatible changes to the schema SHOULD be reflected by a different URL.
234-
* If present, MUST be a non-empty URI.
235-
*/
236-
schemaurl?: string;
237-
/**
238-
* [OPTIONAL] This describes the subject of the event in the context of the
239-
* event producer (identified by `source`). In publish-subscribe scenarios, a
240-
* subscriber will typically subscribe to events emitted by a `source`, but the
241-
* `source` identifier alone might not be sufficient as a qualifier for any
242-
* specific event if the `source` context has internal sub-structure.
243-
*
244-
* Identifying the subject of the event in context metadata (opposed to only in
245-
* the `data` payload) is particularly helpful in generic subscription filtering
246-
* scenarios where middleware is unable to interpret the `data` content. In the
247-
* above example, the subscriber might only be interested in blobs with names
248-
* ending with '.jpg' or '.jpeg' and the `subject` attribute allows for
249-
* constructing a simple and efficient string-suffix filter for that subset of
250-
* events.
251-
*
252-
* If present, MUST be a non-empty string.
253-
* @example "https://example.com/storage/tenant/container"
254-
* @example "mynewfile.jpg"
255-
*/
256-
subject?: string;
257-
/**
258-
* [OPTIONAL] Timestamp of when the occurrence happened. If the time of the
259-
* occurrence cannot be determined then this attribute MAY be set to some other
260-
* time (such as the current time) by the CloudEvents producer, however all
261-
* producers for the same `source` MUST be consistent in this respect. In other
262-
* words, either they all use the actual time of the occurrence or they all use
263-
* the same algorithm to determine the value used.
264-
* @example "2020-08-08T14:48:09.769Z"
265-
*/
266-
time?: string;
267-
/**
268-
* [OPTIONAL] The event payload. This specification does not place any restriction
269-
* on the type of this information. It is encoded into a media format which is
270-
* specified by the datacontenttype attribute (e.g. application/json), and adheres
271-
* to the dataschema format when those respective attributes are present.
272-
*/
273-
data?: Record<string, unknown | string | number | boolean> | string | number | boolean | null | unknown;
274-
/**
275-
* [OPTIONAL] CloudEvents extension attributes.
276-
*/
277-
[key: string]: unknown;
278-
}

src/event/schemas.ts

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -83,76 +83,3 @@ export const schemaV1 = {
8383
},
8484
type: "object",
8585
};
86-
87-
export const schemaV03 = {
88-
$ref: "#/definitions/event",
89-
definitions: {
90-
specversion: {
91-
const: "0.3",
92-
},
93-
datacontenttype: {
94-
type: "string",
95-
},
96-
data: {
97-
type: ["object", "string", "array", "number", "boolean", "null"],
98-
},
99-
event: {
100-
properties: {
101-
specversion: {
102-
$ref: "#/definitions/specversion",
103-
},
104-
datacontenttype: {
105-
$ref: "#/definitions/datacontenttype",
106-
},
107-
data: {
108-
$ref: "#/definitions/data",
109-
},
110-
id: {
111-
$ref: "#/definitions/id",
112-
},
113-
time: {
114-
$ref: "#/definitions/time",
115-
},
116-
schemaurl: {
117-
$ref: "#/definitions/schemaurl",
118-
},
119-
subject: {
120-
$ref: "#/definitions/subject",
121-
},
122-
type: {
123-
$ref: "#/definitions/type",
124-
},
125-
source: {
126-
$ref: "#/definitions/source",
127-
},
128-
},
129-
required: ["specversion", "id", "type", "source"],
130-
type: "object",
131-
},
132-
id: {
133-
type: "string",
134-
minLength: 1,
135-
},
136-
time: {
137-
format: "js-date-time",
138-
type: "string",
139-
},
140-
schemaurl: {
141-
type: "string",
142-
format: "uri-reference",
143-
},
144-
subject: {
145-
type: "string",
146-
minLength: 1,
147-
},
148-
type: {
149-
type: "string",
150-
minLength: 1,
151-
},
152-
source: {
153-
format: "uri-reference",
154-
type: "string",
155-
},
156-
},
157-
type: "object",
158-
};

src/event/spec.ts

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import Ajv from "ajv";
7-
import { ValidationError, isBase64 } from "./validation";
6+
import Ajv, { Options } from "ajv";
7+
import { ValidationError } from "./validation";
88

9-
import { CloudEventV1, CloudEventV03 } from "./interfaces";
10-
import { schemaV03, schemaV1 } from "./schemas";
9+
import { CloudEventV1 } from "./interfaces";
10+
import { schemaV1 } from "./schemas";
1111
import { Version } from "./cloudevent";
12-
import CONSTANTS from "../constants";
1312

14-
const ajv = new Ajv({ extendRefs: true });
13+
const ajv = new Ajv({ extendRefs: true } as Options);
1514

1615
// handle date-time format specially because a user could pass
1716
// Date().toString(), which is not spec compliant date-time format
@@ -21,18 +20,12 @@ ajv.addFormat("js-date-time", function (dateTimeString) {
2120
});
2221

2322
const isValidAgainstSchemaV1 = ajv.compile(schemaV1);
24-
const isValidAgainstSchemaV03 = ajv.compile(schemaV03);
2523

26-
export function validateCloudEvent(event: CloudEventV03 | CloudEventV1): boolean {
24+
export function validateCloudEvent(event: CloudEventV1): boolean {
2725
if (event.specversion === Version.V1) {
2826
if (!isValidAgainstSchemaV1(event)) {
2927
throw new ValidationError("invalid payload", isValidAgainstSchemaV1.errors);
3028
}
31-
} else if (event.specversion === Version.V03) {
32-
if (!isValidAgainstSchemaV03(event)) {
33-
throw new ValidationError("invalid payload", isValidAgainstSchemaV03.errors);
34-
}
35-
checkDataContentEncoding(event);
3629
} else {
3730
return false;
3831
}
@@ -44,18 +37,3 @@ export function validateCloudEvent(event: CloudEventV03 | CloudEventV1): boolean
4437
}
4538
return true;
4639
}
47-
48-
function checkDataContentEncoding(event: CloudEventV03): boolean {
49-
if (event.datacontentencoding) {
50-
// we only support base64
51-
const encoding = event.datacontentencoding.toLocaleLowerCase();
52-
if (encoding !== CONSTANTS.ENCODING_BASE64) {
53-
throw new ValidationError("invalid payload", [`Unsupported content encoding: ${encoding}`]);
54-
} else {
55-
if (!isBase64(event.data)) {
56-
throw new ValidationError("invalid payload", [`Invalid content encoding of data: ${event.data}`]);
57-
}
58-
}
59-
}
60-
return true;
61-
}

src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { CloudEvent, Version } from "./event/cloudevent";
77
import { ValidationError } from "./event/validation";
8-
import { CloudEventV03, CloudEventV03Attributes, CloudEventV1, CloudEventV1Attributes } from "./event/interfaces";
8+
import { CloudEventV1, CloudEventV1Attributes } from "./event/interfaces";
99

1010
import { Options, TransportFunction, EmitterFunction, emitterFor, Emitter } from "./transport/emitter";
1111
import { Headers, Mode, Binding, HTTP, Message, Serializer, Deserializer } from "./message";
@@ -15,8 +15,6 @@ import CONSTANTS from "./constants";
1515
export {
1616
// From event
1717
CloudEvent,
18-
CloudEventV03,
19-
CloudEventV03Attributes,
2018
CloudEventV1,
2119
CloudEventV1Attributes,
2220
Version,

src/message/http/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { CloudEvent, CloudEventV03, CloudEventV1, CONSTANTS, Mode, Version } from "../..";
6+
import { CloudEvent, CloudEventV1, CONSTANTS, Mode, Version } from "../..";
77
import { Message, Headers } from "..";
88

99
import {
@@ -187,7 +187,7 @@ function parseBinary(message: Message, version: Version): CloudEvent {
187187
delete eventObj.datacontentencoding;
188188
}
189189

190-
return new CloudEvent({ ...eventObj, data: body } as CloudEventV1 | CloudEventV03, false);
190+
return new CloudEvent({ ...eventObj, data: body } as CloudEventV1, false);
191191
}
192192

193193
/**
@@ -240,5 +240,5 @@ function parseStructured(message: Message, version: Version): CloudEvent {
240240
delete eventObj.data_base64;
241241
delete eventObj.datacontentencoding;
242242
}
243-
return new CloudEvent(eventObj as CloudEventV1 | CloudEventV03, false);
243+
return new CloudEvent(eventObj as CloudEventV1, false);
244244
}

0 commit comments

Comments
 (0)