Skip to content

Commit 268233f

Browse files
committed
Project start
Signed-off-by: Fabio José <[email protected]>
1 parent 28b81eb commit 268233f

File tree

9 files changed

+348
-0
lines changed

9 files changed

+348
-0
lines changed

.gitignore

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (https://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# TypeScript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+
60+
# parcel-bundler cache (https://parceljs.org/)
61+
.cache
62+
63+
# next.js build output
64+
.next
65+
66+
# nuxt.js build output
67+
.nuxt
68+
69+
# vuepress build output
70+
.vuepress/dist
71+
72+
# Serverless directories
73+
.serverless
74+
75+
# FuseBox cache
76+
.fusebox/
77+

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var Cloudevent = require('./lib/cloudevent.js');
2+
//var Spec_0_1 = require('./lib/spec_0_1.js');
3+
//var Spec_0_2 = require('./lib/spec_0_2.js');
4+
5+
module.exports = Cloudevent;
6+
//module.exports.Spec_0_1 = Spec_0_1;
7+
//module.exports.Spec_0_2 = Spec_0_2;
8+

lib/cloudevent.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
var Spec_0_1 = require('./specs/spec_0_1.js');
2+
var Spec_0_2 = require('./specs/spec_0_2.js');
3+
var JSONFormatter_0_1 = require('./formats/json_0_1.js');
4+
5+
/*
6+
* Class created using the Builder Design Pattern.
7+
*
8+
* https://en.wikipedia.org/wiki/Builder_pattern
9+
*/
10+
function Cloudevent(_spec, _formatter){
11+
this.spec = (_spec) ? new _spec(Cloudevent) : new Spec_0_1(Cloudevent);
12+
this.formatter = (_formatter) ? _formatter : new JSONFormatter_0_1();
13+
}
14+
15+
/*
16+
* To format the payload using the formatter
17+
*/
18+
Cloudevent.prototype.format = function(){
19+
// Check the constraints
20+
this.spec.check();
21+
22+
// Then, format
23+
return this.formatter.format(this.spec.payload);
24+
}
25+
26+
Cloudevent.prototype.toString = function(){
27+
return this.formatter.toString(this.spec.payload);
28+
}
29+
30+
Cloudevent.prototype.type = function(type){
31+
this.spec.type(type);
32+
return this;
33+
}
34+
35+
Cloudevent.prototype.source = function(_source){
36+
this.spec.source(_source);
37+
return this;
38+
}
39+
40+
/*
41+
* Export the specs
42+
*/
43+
Cloudevent.specs = {
44+
'0.1': Spec_0_1,
45+
'0.2': Spec_0_2
46+
};
47+
48+
/*
49+
* Export the formats
50+
*/
51+
Cloudevent.formats = {
52+
'json' : JSONFormatter_0_1,
53+
'json0.1': JSONFormatter_0_1
54+
};
55+
56+
module.exports = Cloudevent;
57+

lib/formats/json_0_1.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
function JSONFormatter(){
3+
4+
}
5+
6+
JSONFormatter.prototype.format = function(payload){
7+
return payload;
8+
}
9+
10+
JSONFormatter.prototype.toString = function(payload){
11+
return JSON.stringify(payload);
12+
}
13+
14+
module.exports = JSONFormatter;

lib/specs/spec_0_1.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var uuid = require('uuid/v4');
2+
3+
function Spec_0_1(_caller){
4+
this.payload = {
5+
cloudEventsVersion: '0.1',
6+
eventID: uuid()
7+
};
8+
9+
/*
10+
* Used to inject backward compatibility functions or attributes.
11+
*/
12+
this.caller = _caller;
13+
14+
/*
15+
* Inject the method to set the version related to data attribute.
16+
*/
17+
this.caller.prototype.eventTypeVersion = function(_version){
18+
this.spec.eventTypeVersion(_version);
19+
}
20+
}
21+
22+
/*
23+
* Check the constraints.
24+
*
25+
* throw an error if do not pass.
26+
*/
27+
Spec_0_1.prototype.check = function() {
28+
29+
}
30+
31+
Spec_0_1.prototype.type = function(_type){
32+
this.payload['eventType'] = _type;
33+
return this;
34+
}
35+
36+
Spec_0_1.prototype.eventTypeVersion = function(version){
37+
this.payload['eventTypeVersion'] = version;
38+
return this;
39+
}
40+
41+
Spec_0_1.prototype.source = function(_source){
42+
this.payload['source'] = _source;
43+
return this;
44+
}
45+
46+
Spec_0_1.prototype.id = function(_id){
47+
this.payload['eventID'] = _id;
48+
return this;
49+
}
50+
51+
module.exports = Spec_0_1;
52+

lib/specs/spec_0_2.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var uuid = require('uuid/v4');
2+
3+
function Spec_0_2(){
4+
this.payload = {
5+
specversion: '0.2',
6+
id: uuid()
7+
};
8+
}
9+
10+
/*
11+
* Check the spec constraints.
12+
*/
13+
Spec_0_2.prototype.check = function(){
14+
15+
}
16+
17+
Spec_0_2.prototype.type = function(_type){
18+
this.payload['type'] = _type;
19+
return this;
20+
}
21+
22+
Spec_0_2.prototype.source = function(_source){
23+
this.payload['source'] = _source;
24+
return this;
25+
}
26+
27+
Spec_0_2.prototype.id = function(_id){
28+
this.payload['id'] = _id;
29+
return this;
30+
}
31+
32+
module.exports = Spec_0_2;
33+

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "cloudevents",
3+
"version": "0.0.1",
4+
"description": "CloudEvents SDK for JavaScript",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "./node_modules/.bin/mocha -C test/*.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/cloudevents/sdk-javascript.git"
12+
},
13+
"keywords": [
14+
"events",
15+
"cloudevents",
16+
"sdk"
17+
],
18+
"author": "cloudevents.io",
19+
"contributors": {
20+
"name": "Fábio José de Moraes",
21+
"email": "[email protected]",
22+
"url": "https://github.com/fabiojose"
23+
},
24+
"license": "Apache-2.0",
25+
"bugs": {
26+
"url": "https://github.com/cloudevents/sdk-javascript/issues"
27+
},
28+
"homepage": "https://github.com/cloudevents/sdk-javascript#readme",
29+
"dependencies": {
30+
"uuid": "3.3.2"
31+
},
32+
"devDependencies": {
33+
"chai": "4.2.0",
34+
"mocha": "5.2.0"
35+
}
36+
}

test/cloudevent_spec_0_1.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var expect = require("chai").expect;
2+
var Cloudevent = require("../index.js");
3+
4+
var cloudevent = new Cloudevent()
5+
.type("com.github.pull.create")
6+
.source("urn:event:from:myapi/resourse/123");
7+
8+
describe("CloudEvents Spec 0.1 - JavaScript SDK", () => {
9+
10+
describe("JSON Format", () => {
11+
12+
describe("Required context attributes", () => {
13+
it("requires 'eventType'", () => {
14+
expect(cloudevent.format()).to.have.property('eventType');
15+
});
16+
17+
it("requires 'cloudEventsVersion'", () => {
18+
expect(cloudevent.format()).to.have.property('cloudEventsVersion');
19+
});
20+
21+
it("requires 'source'", () => {
22+
expect(cloudevent.format()).to.have.property('source');
23+
});
24+
25+
it("requires 'eventID'", () => {
26+
expect(cloudevent.format()).to.have.property('eventID');
27+
});
28+
});
29+
30+
describe("Backward compatibility", () => {
31+
it("should have 'eventTypeVersion'", () => {
32+
cloudevent.eventTypeVersion("1.0");
33+
expect(cloudevent.format()).to.have.property('eventTypeVersion');
34+
});
35+
});
36+
37+
});
38+
39+
});

test/cloudevent_spec_0_2.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var expect = require("chai").expect;
2+
var Cloudevent = require("../index.js");
3+
4+
var cloudevent = new Cloudevent(Cloudevent.specs['0.2'])
5+
.type("com.github.pull.create")
6+
.source("urn:event:from:myapi/resourse/123");
7+
8+
describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
9+
10+
describe("JSON Format", () => {
11+
12+
describe("Required context attributes", () => {
13+
it("requires 'type'", () => {
14+
expect(cloudevent.format()).to.have.property('type');
15+
});
16+
17+
it("requires 'specversion'", () => {
18+
expect(cloudevent.format()).to.have.property('specversion');
19+
});
20+
21+
it("requires 'source'", () => {
22+
expect(cloudevent.format()).to.have.property('source');
23+
});
24+
25+
it("requires 'id'", () => {
26+
expect(cloudevent.format()).to.have.property('id');
27+
});
28+
});
29+
30+
});
31+
32+
});

0 commit comments

Comments
 (0)