@@ -8,14 +8,21 @@ Official CloudEvents' SDK for JavaScript.
8
8
9
9
<img src =" https://github.com/cncf/artwork/blob/master/projects/cloudevents/horizontal/color/cloudevents-horizontal-color.png " width =" 300 " height =" 58 " alt =" CloudEvents logo " >
10
10
11
+ ## Contributing
12
+
13
+ Before create an awesome PR, please read our [ guidelines] ( ./CONTRIBUTING.md ) .
14
+
15
+ ## Examples
16
+
17
+ To see working examples, point to [ examples] ( ./examples ) .
18
+
11
19
## Versioning
12
20
13
21
### Before Spec reaches 1.0
14
22
15
- - ` 0.x.p ` : where ` x ` relates to spec version and ` p ` relates to fixes, see
16
- [ semver] ( https://semver.org/ ) .
23
+ - ` 0.x.p ` : where ` x ` relates to spec version and ` p ` relates to fixes and releases.
17
24
18
- ### After Spec reaches 1.0 __
25
+ ### After Spec reaches 1.0
19
26
20
27
- ` x.M.p ` : where ` x ` relates to spec version, ` M ` relates to minor and ` p ` relates
21
28
to fixes. See [ semver] ( https://semver.org/ )
@@ -40,12 +47,22 @@ These are the supported specifications by this version.
40
47
| HTTP Transport Binding - Binary | yes | yes |
41
48
| JSON Event Format | yes | yes |
42
49
50
+ ### What we can do?
51
+
52
+ | __ What__ | __ v0.1__ | __ v0.2__ |
53
+ | ------------------------------------| ----------| ----------|
54
+ | Create events | yes | yes |
55
+ | Emit Structured events over HTTP | yes | yes |
56
+ | Emit Binary events over HTTP | yes | yes |
57
+ | JSON Event Format | yes | yes |
58
+ | Receice Structured events over HTTP| no | yes |
59
+ | Receice Binary events over HTTP | no | yes |
60
+
43
61
## How to use
44
62
45
63
The ` Cloudevent ` constructor arguments.
46
64
47
65
``` js
48
-
49
66
/*
50
67
* spec : if is null, set the spec 0.1 impl
51
68
* format: if is null, set the JSON Format 0.1 impl
@@ -74,11 +91,11 @@ cloudevent01
74
91
.source (" urn:event:from:myapi/resourse/123" );
75
92
76
93
/*
77
- * Backward compatibility to spec 0.1 by injecting methods from spec implementation
94
+ * Backward compatibility to spec 0.1 by injecting methods from spec implementation
78
95
* to Cloudevent
79
96
*/
80
97
cloudevent01
81
- .eventTypeVersion (" 1.0" );
98
+ .eventTypeVersion (" 1.0" );
82
99
83
100
/*
84
101
* Constructs an instance with:
@@ -104,7 +121,7 @@ var Cloudevent = require("cloudevents-sdk");
104
121
/*
105
122
* Creates an instance with default spec and format
106
123
*/
107
- var cloudevent =
124
+ var cloudevent =
108
125
new Cloudevent ()
109
126
.type (" com.github.pull.create" )
110
127
.source (" urn:event:from:myapi/resourse/123" );
@@ -113,7 +130,6 @@ var cloudevent =
113
130
* Format the payload and return it
114
131
*/
115
132
var formatted = cloudevent .format ();
116
-
117
133
```
118
134
119
135
#### Emitting
@@ -122,9 +138,10 @@ var formatted = cloudevent.format();
122
138
var Cloudevent = require (" cloudevents-sdk" );
123
139
124
140
// The event
125
- var cloudevent = new Cloudevent ()
126
- .type (" com.github.pull.create" )
127
- .source (" urn:event:from:myapi/resourse/123" );
141
+ var cloudevent =
142
+ new Cloudevent ()
143
+ .type (" com.github.pull.create" )
144
+ .source (" urn:event:from:myapi/resourse/123" );
128
145
129
146
// The binding configuration using POST
130
147
var config = {
@@ -147,49 +164,85 @@ binding.emit(cloudevent)
147
164
console .log (response .data );
148
165
149
166
}).catch (err => {
150
- // Treat the error
167
+ // Deal with errors
168
+ console .error (err);
169
+ });
170
+ ```
171
+ #### Receiving Events
172
+
173
+ You can choose any framework for port binding. But, use the Unmarshaller
174
+ to process the HTTP Payload and HTTP Headers, extracting the CloudEvents.
175
+
176
+ The Unmarshaller will parse the HTTP Request and decides if it is a binary
177
+ or a structured version of transport binding.
178
+
179
+ __ :smiley : Checkout the full working example: [ here] ( ./examples/express-ex ) .__
180
+
181
+ ``` js
182
+ // some parts were removed //
183
+
184
+ var Unmarshaller02 = require (" cloudevents-sdk/http/unmarshaller/v02" );
185
+
186
+ // some parts were removed //
187
+
188
+ app .post (' /' , function (req , res ) {
189
+ unmarshaller .unmarshall (req .body , req .headers )
190
+ .then (cloudevent => {
191
+
192
+ // TODO use the cloudevent
193
+
194
+ res .status (201 )
195
+ .send (" Event Accepted" );
196
+ })
197
+ .catch (err => {
151
198
console .error (err);
199
+ res .status (400 )
200
+ .header (" Content-Type" , " application/json" )
201
+ .send (JSON .stringify (err));
152
202
});
203
+ });
204
+
205
+
153
206
```
154
207
155
208
## Repository Structure
156
209
157
210
``` text
158
211
├── index.js
212
+ ├── ext
159
213
├── lib
160
214
│ ├── bindings
161
215
│ │ └── http
162
216
│ ├── cloudevent.js
163
- │ ├── format
217
+ │ ├── formats
218
+ │ │ └── json
164
219
│ └── specs
165
220
├── LICENSE
166
221
├── package.json
167
222
├── README.md
168
223
```
169
224
170
225
- ` index.js ` : library exports
226
+ - ` ext ` : external stuff, e.g, Cloud Events JSONSchema
171
227
- ` lib/bindings ` : every binding implementation goes here
172
228
- ` lib/bindings/http ` : every http binding implementation goes here
173
229
- ` lib/cloudevent.js ` : implementation of Cloudevent, an interface
174
- - ` lib/format / ` : every format implementation goes here
230
+ - ` lib/formats / ` : every format implementation goes here
175
231
- ` lib/specs/ ` : every spec implementation goes here
176
232
177
233
## Unit Testing
178
234
179
235
The unit test checks the result of formatted payload and the constraints.
180
236
181
237
``` bash
182
-
183
238
npm test
184
-
185
239
```
186
240
187
241
## The API
188
242
189
243
### ` Cloudevent ` class
190
244
191
245
``` js
192
-
193
246
/*
194
247
* Format the payload and return an Object.
195
248
*/
@@ -200,49 +253,77 @@ Object Cloudevent.format()
200
253
*/
201
254
String Cloudevent .toString ()
202
255
256
+ /*
257
+ * Create a Cloudevent instance from String.
258
+ */
259
+ Cloudevent Cloudevent .fromString (String )
260
+
203
261
```
204
262
205
263
### ` Formatter ` classes
206
264
207
265
Every formatter class must implement these methods to work properly.
208
266
209
267
``` js
210
-
211
268
/*
212
269
* Format the Cloudevent payload argument and return an Object.
213
270
*/
214
- Object Formatter .format (payload )
271
+ Object Formatter .format (Object )
215
272
216
273
/*
217
274
* Format the Cloudevent payload as String.
218
275
*/
219
- String Formatter .toString (payload)
276
+ String Formatter .toString (Object )
277
+ ```
278
+
279
+ ### ` Parser ` classes
280
+
281
+ Every Parser class must implement these methods to work properly.
282
+
283
+ ``` js
284
+ /*
285
+ * The default constructor with Spec as parameter
286
+ */
287
+ Parser (Spec)
220
288
289
+ /*
290
+ * Try to parse the payload to some event format
291
+ */
292
+ Object Parser .parse (payload)
221
293
```
222
294
223
- ## ` Spec ` classes
295
+ ### ` Spec ` classes
224
296
225
297
Every Spec class must implement these methods to work properly.
226
298
227
299
``` js
228
-
229
300
/*
230
301
* The constructor must receives the Cloudevent type.
231
302
*/
232
303
Spec (Cloudevent)
233
304
234
305
/*
235
306
* Checks the spec constraints, throwing an error if do not pass.
307
+ * @throws Error when it is an invalid event
236
308
*/
237
309
Spec .check ()
238
310
311
+ /*
312
+ * Checks if the argument pass through the spec constraints
313
+ * @throws Error when it is an invalid event
314
+ */
315
+ Spec .check (Object )
239
316
```
317
+
240
318
### ` Binding ` classes
241
319
242
320
Every Binding class must implement these methods to work properly.
243
321
244
- ``` js
322
+ #### Emitter Binding
245
323
324
+ Following we have the signature for the binding to emit Cloudevents.
325
+
326
+ ``` js
246
327
/*
247
328
* The constructor must receives the map of configurations.
248
329
*/
@@ -252,7 +333,51 @@ Binding(config)
252
333
* Emits the event using an instance of Cloudevent.
253
334
*/
254
335
Binding .emit (cloudevent)
336
+ ```
337
+
338
+ #### Receiver Binding
339
+
340
+ Following we have the signature for the binding to receive Cloudevents.
341
+
342
+ ``` js
343
+ /*
344
+ * The constructor must receives the map of configurations.
345
+ */
346
+ Receiver (config)
347
+
348
+ /*
349
+ * Checks if some Object and a Map of headers
350
+ * follows the binding definition, throwing an error if did not follow
351
+ */
352
+ Receiver .check (Object , Map )
353
+
354
+ /*
355
+ * Checks and parse as Cloudevent
356
+ */
357
+ Cloudevent Receiver .parse (Object , Map )
358
+ ```
359
+
360
+ ### ` Unmarshaller ` classes
255
361
362
+ The Unmarshaller classes uses the receiver API, abstracting the formats:
363
+
364
+ - structured
365
+ - binary
366
+
367
+ Choosing the right implementation based on the ` headers ` map.
368
+
369
+ ``` js
370
+ /*
371
+ * Constructor without arguments
372
+ */
373
+ Unmarshaller ()
374
+
375
+ /*
376
+ * The method to unmarshall the payload.
377
+ * @arg payload could be a string or a object
378
+ * @arg headers a map of headers
379
+ */
380
+ Promise Unmarshaller .unmarshall (payload, headers)
256
381
```
257
382
258
383
> See how to implement the method injection [ here] ( lib/specs/spec_0_1.js#L17 )
0 commit comments