Skip to content

Commit 14eacd5

Browse files
authored
Merge pull request #5 from cloudevents/develop
Develop
2 parents 0a0e24e + ba2d47f commit 14eacd5

16 files changed

+683
-234
lines changed

.travis.yml

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
language: node_js
22
node_js:
3-
- 8
4-
- 7
5-
- 6
6-
3+
- 8
4+
- 7
5+
- 6
76
cache: npm
8-
97
jobs:
108
include:
11-
- stage: github release
12-
node_js: "6"
13-
deploy:
14-
provider: releases
15-
api_key:
16-
secure: "ls/IjZdRpFZlEQsZrsmXn7h7QTZuM8x4gZq3r75sOyTWcmKgZLN27hYCtk8SPcuNbt+ZA3otQEQJvDuGTCAwtbU4lxQjfXswP9M/om01hf3J7uTcjtol4XOsmBFqMCktW94pVH4U7Q18IHeP5JBnlfLzAY/YifRKKNWNrsI6bbnfcGgsFJYvICwKL5LEj7bxtJk156lBBJm9TscMX48BFsUBvnCNTDEBYNaaGkoCz6DPM0Da7QPF5exHcNiiF9SoR60WeGI1q+MdZyE+1AG0VpUp7sMxJafXSBbgYquPEZmG9ScZbkVyWP7ps3sgkqDkxtH4kNe9zF4PySRYfxYKJ03ECwbSmj9gxsIc8o2zhrLFinmHydLxoOEh5EygB2euekgMhoEKL0kA8bteaaqmD+3sna8MbXG4HhvIrEAspNtOFZyUm/SmgvXXxaKiDtwTw/5CvPCc7VH51NuC5QZi0UehLzatDpDNsj/ffMwDp5F4SBIZgUUmmIWfnAODNGwSwBKxdPVS3etkTeiBxerw2uzC6qjNW1QJyb9s+iU3rC59IEAuq6u4ymv3caDIHLnJbCMHRHQoIbq86oErqb8DhH6KrTlBnwlGBpUVhdpYMt0QX2Uncu9uHMBZ2CQNZ819PJa4QWvc1Ixoj3auKqvoMfPNQFOskMWnrnxkOokyPZg="
17-
skip_cleanup: true
18-
on:
19-
tags: true
20-
- stage: npm release
21-
node_js: "6"
22-
deploy:
23-
provider: npm
24-
25-
api_key:
26-
secure: "o5aF53FTPUSiJBbCZ/anBWQCgEJCctpxuTgGJbO1NpiheOM/xENiSmv+n2a5sGrhhqT0h7k15mE/ZgtL8TnM+45AHOg3EGez5JoR1XMIXnSeCno1GFK4waHDcEn3eLW9P35r1S5/RMTqbEUvqpyK/fzVZ4ecyh9t7dVvJV8MyQGo+r+oO3SLYEIt7YC6vZRh+dV3cK8jr7MHkHmQfZ6tZrALMKsj6QNTUtmk+IU52WHi4oe1iPuypS5dlaVdmanX7ZRtC7gR0Dko2/wja+DPOAKgG/S0aS794cxal7P5k/K34mvqT8iaCl4vN5uIcRsipgsprIahk2G2NgnIVCetBda9LhpMNUdn3j+v4T0lx3jiqP1eq01nk8YFpTV4Xz9VlYHK/E6NYQhUmd7N6WO9vXmOEDGBvWrDfQ1QMx+/TM9r3vzK9ps3sjkDFaAtJ2ZQ0pvFMEQTaLKak24ntWltSWZKvxdjYnS+bcfyIQQGagvqgZsnkKzeudO9N9Atp4OGcst9CAvvykDsfmLlARAiyvpuDVyUivuaOlCB9J7VBt1sbBfsiHpnJcSsMVz1OMRX5EGewbla530guoePhjTUDVit3NyUMz3ZQQTN9VSK3tA+NLyR0Ex8Oel+byHJDYyf+36GCDvoXagaPUVk5M5BINiCw2IWhxDgRGrcEp9JIUI="
27-
skip_cleanup: true
28-
on:
29-
tags: true
9+
- stage: github release
10+
node_js: '6'
11+
deploy:
12+
provider: releases
13+
api_key:
14+
secure: ls/IjZdRpFZlEQsZrsmXn7h7QTZuM8x4gZq3r75sOyTWcmKgZLN27hYCtk8SPcuNbt+ZA3otQEQJvDuGTCAwtbU4lxQjfXswP9M/om01hf3J7uTcjtol4XOsmBFqMCktW94pVH4U7Q18IHeP5JBnlfLzAY/YifRKKNWNrsI6bbnfcGgsFJYvICwKL5LEj7bxtJk156lBBJm9TscMX48BFsUBvnCNTDEBYNaaGkoCz6DPM0Da7QPF5exHcNiiF9SoR60WeGI1q+MdZyE+1AG0VpUp7sMxJafXSBbgYquPEZmG9ScZbkVyWP7ps3sgkqDkxtH4kNe9zF4PySRYfxYKJ03ECwbSmj9gxsIc8o2zhrLFinmHydLxoOEh5EygB2euekgMhoEKL0kA8bteaaqmD+3sna8MbXG4HhvIrEAspNtOFZyUm/SmgvXXxaKiDtwTw/5CvPCc7VH51NuC5QZi0UehLzatDpDNsj/ffMwDp5F4SBIZgUUmmIWfnAODNGwSwBKxdPVS3etkTeiBxerw2uzC6qjNW1QJyb9s+iU3rC59IEAuq6u4ymv3caDIHLnJbCMHRHQoIbq86oErqb8DhH6KrTlBnwlGBpUVhdpYMt0QX2Uncu9uHMBZ2CQNZ819PJa4QWvc1Ixoj3auKqvoMfPNQFOskMWnrnxkOokyPZg=
15+
skip_cleanup: true
16+
on:
17+
tags: true
18+
- stage: npm release
19+
node_js: '6'
20+
deploy:
21+
provider: npm
22+
23+
api_key:
24+
secure: o5aF53FTPUSiJBbCZ/anBWQCgEJCctpxuTgGJbO1NpiheOM/xENiSmv+n2a5sGrhhqT0h7k15mE/ZgtL8TnM+45AHOg3EGez5JoR1XMIXnSeCno1GFK4waHDcEn3eLW9P35r1S5/RMTqbEUvqpyK/fzVZ4ecyh9t7dVvJV8MyQGo+r+oO3SLYEIt7YC6vZRh+dV3cK8jr7MHkHmQfZ6tZrALMKsj6QNTUtmk+IU52WHi4oe1iPuypS5dlaVdmanX7ZRtC7gR0Dko2/wja+DPOAKgG/S0aS794cxal7P5k/K34mvqT8iaCl4vN5uIcRsipgsprIahk2G2NgnIVCetBda9LhpMNUdn3j+v4T0lx3jiqP1eq01nk8YFpTV4Xz9VlYHK/E6NYQhUmd7N6WO9vXmOEDGBvWrDfQ1QMx+/TM9r3vzK9ps3sjkDFaAtJ2ZQ0pvFMEQTaLKak24ntWltSWZKvxdjYnS+bcfyIQQGagvqgZsnkKzeudO9N9Atp4OGcst9CAvvykDsfmLlARAiyvpuDVyUivuaOlCB9J7VBt1sbBfsiHpnJcSsMVz1OMRX5EGewbla530guoePhjTUDVit3NyUMz3ZQQTN9VSK3tA+NLyR0Ex8Oel+byHJDYyf+36GCDvoXagaPUVk5M5BINiCw2IWhxDgRGrcEp9JIUI=
25+
skip_cleanup: true
26+
on:
27+
tags: true
28+
env:
29+
global:
30+
secure: t0A5/Fk1Qj8WkAk0ZuQqpFjrpINV6gfL+d1fXgq03EOAG/7FEyyz+AKkYaLT6avp26VWBzLgEoWC3RNmWD0v1/Ruckb95YpQcb5e4JotAce1ZsHgGTgx0UO146tkyGvXBw6MmO2nX2O/sHzkmyR6rlKkBi9LEKokJ9OFd6fi9nNtksgUDxkqNlNTu100fIQAV2RcN47C6iUx+gJIa+H/8QX/Cz02MyheXLe8a763wMQapz5GHrk2KuVY6FpylcAlSi2so+PjEGfKq3MWaQnTPkevkRDLtGcecVtFAq/0VbvoKc97PHoVT0x+nf48k1gj2VnQtYj6EVB74yRvqM9KkNOsfAQ0z9zF5wXMRs11SZywSViLQsnD9Ue6eYbYmodOKn4DFlLtoRyLBUspzLuoL76lERGe2cHU+Ebz2Nb9jL88o+SjfGrSJQEJU7SLZLbzJ7T/3SspWcAlo+Fo9UTCOB4/yL22Yge/SWQkdd1orBhohpjpyrU9Z1+IRvVoU1EICr04zSFUwEwn6Yxbt0ArgTZBEf11bf+YpCo+rBXNAd+XbZ79PGOEppFb+Hxrt9i1S1RnesDJnwC0k7cswU55Rv5gcNCo9lC+N4ZwS5y+5r6DsBfB+12YdsFORFyNbClECro1ODv4STf3rBBnjL8+ziPFjP4d4OwiGY+vG8bvLXY=
31+
script: npm run coverage

README.md

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/bd66e7c52002481993cd6d610534b0f7)](https://www.codacy.com/app/fabiojose/sdk-javascript?utm_source=github.com&utm_medium=referral&utm_content=cloudevents/sdk-javascript&utm_campaign=Badge_Grade)
2+
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/bd66e7c52002481993cd6d610534b0f7)](https://www.codacy.com/app/fabiojose/sdk-javascript?utm_source=github.com&utm_medium=referral&utm_content=cloudevents/sdk-javascript&utm_campaign=Badge_Coverage)
23
[![Build Status](https://travis-ci.org/cloudevents/sdk-javascript.svg?branch=master)](https://travis-ci.org/cloudevents/sdk-javascript)
34

45
# sdk-javascript
5-
Javascript SDK for CloudEvents
66

7-
> This is a WIP
7+
Official CloudEvents' SDK for JavaScript.
8+
9+
<img src="https://raw.githubusercontent.com/cncf/artwork/master/cloudevents/horizontal/color/cloudevents-horizontal-color.png" width="300" height="58" alt="CloudEvents logo">
810

911
## Installation
1012

1113
This CloudEvents SDK requires nodejs 6.11+
1214

1315
### Nodejs
1416

15-
```
17+
```sh
1618
npm install cloudevents-sdk
1719
```
1820
## Specification Support
1921

2022
These are the supported specifications by this version.
2123

22-
| **Specifications** | **v0.1** | **v0.2** |
23-
|----------------------------|----------|----------|
24-
| CloudEvents | yes | yes |
25-
| HTTP Transport Binding | yes | yes |
26-
| JSON Event Format | yes | yes |
24+
| **Specifications** | **v0.1** | **v0.2** |
25+
|---------------------------------------|----------|----------|
26+
| CloudEvents | yes | yes |
27+
| HTTP Transport Binding - Structured | yes | yes |
28+
| HTTP Transport Binding - Binary | yes | yes |
29+
| JSON Event Format | yes | yes |
2730

2831
## How to use
2932

@@ -39,7 +42,7 @@ Cloudevent(spec, format);
3942

4043
```
4144

42-
### How to construct instances?
45+
### Usage
4346

4447
```js
4548
var Cloudevent = require("cloudevents-sdk");
@@ -59,7 +62,8 @@ cloudevent01
5962
.source("urn:event:from:myapi/resourse/123");
6063

6164
/*
62-
* Backward compatibility by injecting methods from spec implementation to Cloudevent
65+
* Backward compatibility to spec 0.1 by injecting methods from spec implementation
66+
* to Cloudevent
6367
*/
6468
cloudevent01
6569
.eventTypeVersion("1.0");
@@ -69,7 +73,7 @@ cloudevent01
6973
* - Spec 0.2
7074
* - JSON Format 0.1
7175
*/
72-
var cloudevent02 = new Cloudevent(Cloudevent.specs['0.2']);
76+
var cloudevent02 = new Cloudevent(Cloudevent.specs["0.2"]);
7377

7478
/*
7579
* Different specs, but the same API.
@@ -80,23 +84,27 @@ cloudevent02
8084

8185
```
8286

83-
### How to get the formatted payload?
87+
#### Formatting
8488

8589
```js
8690
var Cloudevent = require("cloudevents-sdk");
8791

88-
var cloudevent = new Cloudevent()
89-
.type("com.github.pull.create")
90-
.source("urn:event:from:myapi/resourse/123");
92+
/*
93+
* Creates an instance with default spec and format
94+
*/
95+
var cloudevent =
96+
new Cloudevent()
97+
.type("com.github.pull.create")
98+
.source("urn:event:from:myapi/resourse/123");
9199

92100
/*
93-
* Format the payload and return it.
101+
* Format the payload and return it
94102
*/
95103
var formatted = cloudevent.format();
96104

97105
```
98106

99-
### How to emit an event?
107+
#### Emitting
100108

101109
```js
102110
var Cloudevent = require("cloudevents-sdk");
@@ -108,12 +116,12 @@ var cloudevent = new Cloudevent()
108116

109117
// The binding configuration using POST
110118
var config = {
111-
method: 'POST',
112-
url : 'https://mywebhook.com'
119+
method: "POST",
120+
url : "https://myserver.com"
113121
};
114122

115123
// The binding instance
116-
var binding = Cloudevent.bindings['http-structured0.1'](config);
124+
var binding = Cloudevent.bindings["http-structured0.1"](config);
117125

118126
// Emit the event using Promise
119127
binding.emit(cloudevent)
@@ -134,45 +142,20 @@ binding.emit(cloudevent)
134142
├── lib
135143
│   ├── bindings
136144
│   │   └── http
137-
│   │   └── structured_0_1.js
138145
│   ├── cloudevent.js
139146
│   ├── format
140-
│   │   └── json_0_1.js
141147
│   └── specs
142-
│   ├── spec_0_1.js
143-
│   └── spec_0_2.js
144148
├── LICENSE
145149
├── package.json
146150
├── README.md
147-
└── test
148-
├── cloudevent_spec_0_1.js
149-
├── cloudevent_spec_0_2.js
150-
└── http_binding_0_1.js
151151
```
152152

153-
* `index.js`: library exports
154-
155-
* `lib/bindings`: every binding implementation goes here
156-
157-
* `lib/bindings/http`: every http binding implementation goes here
158-
159-
* `lib/bindings/http/structured_0_1.js`: implementation of structured HTTP Binding
160-
161-
* `lib/cloudevent.js`: implementation of Cloudevent, an interface
162-
163-
* `lib/format/`: every format implementation goes here
164-
165-
* `lib/format/json_0_1.js`: implementation for JSON formatting [version 0.1](https://github.com/cloudevents/spec/blob/v0.1/json-format.md)
166-
167-
* `lib/specs/`: every spec implementation goes here
168-
169-
* `lib/specs/spec_0_1.js`: implementation for spec [version 0.1](https://github.com/cloudevents/spec/blob/v0.1/spec.md)
170-
171-
* `lib/specs/spec_0_2.js`: implementation for spec [version 0.2](https://github.com/cloudevents/spec/blob/v0.2/spec.md)
172-
173-
* `test/cloudevent_spec_0_1.js`: unit testing for spec 0.1
174-
175-
* `test/cloudevent_spec_0_2.js`: unit testing for spec 0.2
153+
- `index.js`: library exports
154+
- `lib/bindings`: every binding implementation goes here
155+
- `lib/bindings/http`: every http binding implementation goes here
156+
- `lib/cloudevent.js`: implementation of Cloudevent, an interface
157+
- `lib/format/`: every format implementation goes here
158+
- `lib/specs/`: every spec implementation goes here
176159

177160
## Unit Testing
178161

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var Cloudevent = require('./lib/cloudevent.js');
1+
var Cloudevent = require("./lib/cloudevent.js");
22

33
module.exports = Cloudevent;
4-

lib/bindings/http/binary_0_1.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var axios = require("axios");
2+
3+
function HTTPBinary(configuration){
4+
this.config = configuration;
5+
6+
this.config["headers"] = {
7+
"Content-Type":"application/cloudevents+json; charset=utf-8"
8+
};
9+
}
10+
11+
HTTPBinary.prototype.emit = function(cloudevent){
12+
13+
// Create new request object
14+
var _config = JSON.parse(JSON.stringify(this.config));
15+
16+
// Always set stuff in _config
17+
var _headers = _config["headers"];
18+
19+
if(cloudevent.getContenttype()) {
20+
_headers["Content-Type"] = cloudevent.getContenttype();
21+
}
22+
23+
_headers["CE-EventType"] = cloudevent.getType();
24+
if(cloudevent.getEventTypeVersion()) {
25+
_headers["CE-EventTypeVersion"] = cloudevent.getEventTypeVersion();
26+
}
27+
_headers["CE-CloudEventsVersion"] = cloudevent.getSpecversion();
28+
_headers["CE-Source"] = cloudevent.getSource();
29+
_headers["CE-EventID"] = cloudevent.getId();
30+
if(cloudevent.getTime()) {
31+
_headers["CE-EventTime"] = cloudevent.getTime();
32+
}
33+
_headers["CE-SchemaURL"] = cloudevent.getSchemaurl();
34+
35+
// Set the cloudevent payload
36+
_config["data"] = cloudevent.format().data;
37+
38+
// Return the Promise
39+
return axios.request(_config);
40+
};
41+
42+
module.exports = HTTPBinary;

lib/bindings/http/binary_0_2.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var axios = require("axios");
2+
var empty = require("is-empty");
3+
4+
function HTTPBinary(configuration){
5+
this.config = configuration;
6+
7+
this.config["headers"] = {
8+
"Content-Type":"application/cloudevents+json; charset=utf-8"
9+
};
10+
}
11+
12+
HTTPBinary.prototype.emit = function(cloudevent){
13+
14+
// Create new request object
15+
var _config = JSON.parse(JSON.stringify(this.config));
16+
17+
// Always set stuff in _config
18+
var _headers = _config["headers"];
19+
20+
if(cloudevent.getContenttype()) {
21+
_headers["Content-Type"] = cloudevent.getContenttype();
22+
}
23+
24+
_headers["ce-type"] = cloudevent.getType();
25+
_headers["ce-specversion"] = cloudevent.getSpecversion();
26+
_headers["ce-source"] = cloudevent.getSource();
27+
_headers["ce-id"] = cloudevent.getId();
28+
if(cloudevent.getTime()) {
29+
_headers["ce-time"] = cloudevent.getTime();
30+
}
31+
_headers["ce-schemaurl"] = cloudevent.getSchemaurl();
32+
33+
// Set the cloudevent payload
34+
_config["data"] = cloudevent.format().data;
35+
36+
// Have extensions?
37+
var exts = cloudevent.getExtensions();
38+
for(var ext in exts){
39+
_headers["ce-" + ext] = exts[ext];
40+
}
41+
42+
// Return the Promise
43+
return axios.request(_config);
44+
};
45+
46+
module.exports = HTTPBinary;

lib/bindings/http/structured_0_1.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ var axios = require("axios");
33
function HTTPStructured(configuration){
44
this.config = configuration;
55

6-
this.config['headers'] = {
7-
'Content-Type':'application/cloudevents+json; charset=utf-8'
6+
this.config["headers"] = {
7+
"Content-Type":"application/cloudevents+json; charset=utf-8"
88
};
99
}
1010

@@ -14,11 +14,10 @@ HTTPStructured.prototype.emit = function(cloudevent){
1414
var _config = JSON.parse(JSON.stringify(this.config));
1515

1616
// Set the cloudevent payload
17-
_config['data'] = cloudevent.format();
17+
_config["data"] = cloudevent.format();
1818

1919
// Return the Promise
2020
return axios.request(_config);
21-
}
21+
};
2222

2323
module.exports = HTTPStructured;
24-

lib/bindings/http/structured_0_2.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var axios = require("axios");
2+
3+
function HTTPStructured(configuration){
4+
this.config = configuration;
5+
6+
this.config["headers"] = {
7+
"Content-Type":"application/cloudevents+json; charset=utf-8"
8+
};
9+
}
10+
11+
HTTPStructured.prototype.emit = function(cloudevent){
12+
13+
// Create new request object
14+
var _config = JSON.parse(JSON.stringify(this.config));
15+
16+
// Set the cloudevent payload
17+
_config["data"] = cloudevent.format();
18+
19+
// Return the Promise
20+
return axios.request(_config);
21+
};
22+
23+
module.exports = HTTPStructured;

0 commit comments

Comments
 (0)