-
Notifications
You must be signed in to change notification settings - Fork 70
feat: expose a mode and version agnostic event receiver #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
const CloudEvent = require("./lib/cloudevent.js"); | ||
const HTTPReceiver = require("./lib/bindings/http/http_receiver.js"); | ||
|
||
module.exports = CloudEvent; | ||
module.exports = { | ||
CloudEvent, | ||
HTTPReceiver | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const V03Binary = require("./receiver_binary_0_3"); | ||
const V03Structured = require("./receiver_structured_0_3.js"); | ||
const V1Binary = require("./receiver_binary_1.js"); | ||
const V1Structured = require("./receiver_structured_1.js"); | ||
const constants = require("./constants"); | ||
|
||
class HTTPReceiver { | ||
constructor() { | ||
this.receivers = { | ||
v1: { | ||
structured: new V1Structured(), | ||
binary: new V1Binary() | ||
}, | ||
v03: { | ||
structured: new V03Structured(), | ||
binary: new V03Binary() | ||
} | ||
}; | ||
} | ||
|
||
accept(headers, body) { | ||
const mode = getMode(headers); | ||
const version = getVersion(mode, headers, body); | ||
switch (version) { | ||
case constants.SPEC_V1: | ||
return this.receivers.v1[mode].parse(body, headers); | ||
case constants.SPEC_V03: | ||
return this.receivers.v03[mode].parse(body, headers); | ||
default: | ||
console.error( | ||
`Unknown spec version ${version}. Default to ${constants.SPEC_V1}`); | ||
return this.receivers.v1[mode].parse(body, headers); | ||
} | ||
} | ||
} | ||
|
||
function getMode(headers) { | ||
let mode = "binary"; | ||
const contentType = headers[constants.HEADER_CONTENT_TYPE]; | ||
if (contentType && contentType.startsWith(constants.MIME_CE)) { | ||
mode = "structured"; | ||
} | ||
return mode; | ||
} | ||
|
||
function getVersion(mode, headers, body) { | ||
let version = constants.SPEC_V1; // default to 1.0 | ||
|
||
if (mode === "binary") { | ||
// Check the headers for the version | ||
const versionHeader = headers[constants.DEFAULT_SPEC_VERSION_HEADER]; | ||
if (versionHeader) { version = versionHeader; } | ||
} else { | ||
// structured mode - the version is in the body | ||
version = body instanceof String | ||
? JSON.parse(body).specversion : body.specversion; | ||
} | ||
return version; | ||
} | ||
|
||
module.exports = HTTPReceiver; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const { expect } = require("chai"); | ||
const { CloudEvent, HTTPReceiver } = require("../../../index.js"); | ||
const constants = require("../../../lib/bindings/http/constants.js"); | ||
|
||
const receiver = new HTTPReceiver(); | ||
const id = "1234"; | ||
const type = "org.cncf.cloudevents.test"; | ||
const source = "urn:event:from:myapi/resourse/123"; | ||
const data = { | ||
lunch: "sushi" | ||
}; | ||
|
||
describe("HTTP Transport Binding Receiver for CloudEvents", () => { | ||
describe("V1", () => { | ||
const specversion = "1.0"; | ||
|
||
it("Structured data returns a CloudEvent", () => { | ||
const payload = { | ||
id, | ||
type, | ||
source, | ||
data, | ||
specversion | ||
}; | ||
|
||
const headers = { | ||
[constants.HEADER_CONTENT_TYPE]: constants.MIME_CE_JSON | ||
}; | ||
|
||
const event = receiver.accept(headers, payload); | ||
validateEvent(event, specversion); | ||
}); | ||
|
||
it("Binary data returns a CloudEvent", () => { | ||
const headers = { | ||
[constants.HEADER_CONTENT_TYPE]: constants.DEFAULT_CONTENT_TYPE, | ||
[constants.DEFAULT_SPEC_VERSION_HEADER]: specversion, | ||
[constants.BINARY_HEADERS_1.ID]: id, | ||
[constants.BINARY_HEADERS_1.TYPE]: type, | ||
[constants.BINARY_HEADERS_1.SOURCE]: source | ||
}; | ||
|
||
const event = receiver.accept(headers, data); | ||
validateEvent(event, specversion); | ||
}); | ||
}); | ||
|
||
describe("V03", () => { | ||
const specversion = "0.3"; | ||
lance marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
it("Structured data returns a CloudEvent", () => { | ||
const payload = { | ||
id, | ||
type, | ||
source, | ||
data, | ||
specversion | ||
}; | ||
|
||
const headers = { | ||
[constants.HEADER_CONTENT_TYPE]: constants.MIME_CE_JSON | ||
}; | ||
|
||
const event = receiver.accept(headers, payload); | ||
validateEvent(event, specversion); | ||
}); | ||
|
||
it("Binary data returns a CloudEvent", () => { | ||
const headers = { | ||
[constants.HEADER_CONTENT_TYPE]: constants.DEFAULT_CONTENT_TYPE, | ||
[constants.DEFAULT_SPEC_VERSION_HEADER]: specversion, | ||
[constants.BINARY_HEADERS_03.ID]: id, | ||
[constants.BINARY_HEADERS_03.TYPE]: type, | ||
[constants.BINARY_HEADERS_03.SOURCE]: source | ||
}; | ||
|
||
const event = receiver.accept(headers, data); | ||
validateEvent(event, specversion); | ||
}); | ||
}); | ||
}); | ||
|
||
function validateEvent(event, specversion) { | ||
expect(event instanceof CloudEvent).to.equal(true); | ||
expect(event.getId()).to.equal(id); | ||
expect(event.getType()).to.equal(type); | ||
expect(event.getSource()).to.equal(source); | ||
expect(event.getData()).to.deep.equal(data); | ||
expect(event.getSpecversion()).to.equal(specversion); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.