-
Notifications
You must be signed in to change notification settings - Fork 70
feat!: expose a version agnostic event emitter #141
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
6 commits
Select commit
Hold shift + click to select a range
5883096
feat!: expose a version agnostic event emitter
lance d7d1df5
fixup: incorporate comments and add test
lance 1c2856e
docs: add HTTPEmitter examples to README.md
lance 85f610d
fixup: make send accept an alternate URL
lance ffed52d
docs: Update HTTPEmitter examples in the README
lance 0b27c61
squash: add headers() to the HTTPEmitter function
lance 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
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,7 +1,9 @@ | ||
const CloudEvent = require("./lib/cloudevent.js"); | ||
const HTTPReceiver = require("./lib/bindings/http/http_receiver.js"); | ||
const HTTPEmitter = require("./lib/bindings/http/http_emitter.js"); | ||
|
||
module.exports = { | ||
CloudEvent, | ||
HTTPReceiver | ||
HTTPReceiver, | ||
HTTPEmitter | ||
}; |
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,49 +1,85 @@ | ||
const axios = require("axios"); | ||
|
||
const Constants = require("./constants.js"); | ||
const defaults = {}; | ||
defaults[Constants.HEADERS] = {}; | ||
defaults[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE] = | ||
Constants.DEFAULT_CONTENT_TYPE; | ||
|
||
function BinaryHTTPEmitter(config, headerByGetter, extensionPrefix) { | ||
this.config = Object.assign({}, defaults, config); | ||
this.headerByGetter = headerByGetter; | ||
this.extensionPrefix = extensionPrefix; | ||
} | ||
const { | ||
HEADERS, | ||
BINARY_HEADERS_03, | ||
BINARY_HEADERS_1, | ||
HEADER_CONTENT_TYPE, | ||
DEFAULT_CONTENT_TYPE, | ||
DATA_ATTRIBUTE, | ||
SPEC_V1, | ||
SPEC_V03 | ||
} = require("./constants.js"); | ||
|
||
BinaryHTTPEmitter.prototype.emit = function(cloudevent) { | ||
const config = Object.assign({}, this.config); | ||
const headers = Object.assign({}, this.config[Constants.HEADERS]); | ||
|
||
Object.keys(this.headerByGetter) | ||
.filter((getter) => cloudevent[getter]()) | ||
.forEach((getter) => { | ||
const header = this.headerByGetter[getter]; | ||
headers[header.name] = | ||
header.parser( | ||
cloudevent[getter]() | ||
); | ||
}); | ||
|
||
// Set the cloudevent payload | ||
const formatted = cloudevent.format(); | ||
let data = formatted.data; | ||
data = (formatted.data_base64 ? formatted.data_base64 : data); | ||
|
||
// Have extensions? | ||
const exts = cloudevent.getExtensions(); | ||
Object.keys(exts) | ||
.filter((ext) => Object.hasOwnProperty.call(exts, ext)) | ||
.forEach((ext) => { | ||
headers[this.extensionPrefix + ext] = exts[ext]; | ||
}); | ||
|
||
config[Constants.DATA_ATTRIBUTE] = data; | ||
config.headers = headers; | ||
|
||
// Return the Promise | ||
return axios.request(config); | ||
const defaults = { | ||
[HEADERS]: { | ||
[HEADER_CONTENT_TYPE]: DEFAULT_CONTENT_TYPE | ||
}, | ||
method: "POST" | ||
}; | ||
|
||
/** | ||
* A class to emit binary CloudEvents over HTTP. | ||
*/ | ||
class BinaryHTTPEmitter { | ||
/** | ||
* Create a new {BinaryHTTPEmitter} for the provided CloudEvent specification version. | ||
* Once an instance is created for a given spec version, it may only be used to send | ||
* events for that version. | ||
* Default version is 1.0 | ||
* @param {string} version - the CloudEvent HTTP specification version. | ||
* Default: 1.0 | ||
*/ | ||
constructor(version) { | ||
if (version === SPEC_V1) { | ||
this.headerByGetter = require("./emitter_binary_1.js"); | ||
this.extensionPrefix = BINARY_HEADERS_1.EXTENSIONS_PREFIX; | ||
} else if (version === SPEC_V03) { | ||
this.headerByGetter = require("./emitter_binary_0_3.js"); | ||
this.extensionPrefix = BINARY_HEADERS_03.EXTENSIONS_PREFIX; | ||
} | ||
} | ||
|
||
/** | ||
* Sends this cloud event to a receiver over HTTP. | ||
* | ||
* @param {Object} options The configuration options for this event. Options | ||
* provided other than `url` will be passed along to Node.js `http.request`. | ||
* https://nodejs.org/api/http.html#http_http_request_options_callback | ||
* @param {URL} options.url The HTTP/S url that should receive this event | ||
* @param {Object} cloudevent the CloudEvent to be sent | ||
* @returns {Promise} Promise with an eventual response from the receiver | ||
*/ | ||
async emit(options, cloudevent) { | ||
const config = { ...options, ...defaults }; | ||
const headers = config[HEADERS]; | ||
|
||
Object.keys(this.headerByGetter) | ||
.filter((getter) => cloudevent[getter]()) | ||
.forEach((getter) => { | ||
const header = this.headerByGetter[getter]; | ||
headers[header.name] = header.parser(cloudevent[getter]()); | ||
}); | ||
|
||
// Set the cloudevent payload | ||
const formatted = cloudevent.format(); | ||
let data = formatted.data; | ||
data = (formatted.data_base64 ? formatted.data_base64 : data); | ||
|
||
// Have extensions? | ||
const exts = cloudevent.getExtensions(); | ||
Object.keys(exts) | ||
.filter((ext) => Object.hasOwnProperty.call(exts, ext)) | ||
.forEach((ext) => { | ||
headers[this.extensionPrefix + ext] = exts[ext]; | ||
}); | ||
|
||
config[DATA_ATTRIBUTE] = data; | ||
config.headers = headers; | ||
|
||
// Return the Promise | ||
return axios.request(config); | ||
} | ||
} | ||
|
||
module.exports = BinaryHTTPEmitter; |
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,64 +1,53 @@ | ||
const BinaryHTTPEmitter = require("./emitter_binary.js"); | ||
|
||
const Constants = require("./constants.js"); | ||
const { | ||
HEADER_CONTENT_TYPE, | ||
BINARY_HEADERS_03 | ||
} = require("./constants.js"); | ||
|
||
const headerByGetter = {}; | ||
|
||
headerByGetter.getDataContentType = { | ||
name: Constants.HEADER_CONTENT_TYPE, | ||
name: HEADER_CONTENT_TYPE, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getDataContentEncoding = { | ||
name: Constants.BINARY_HEADERS_03.CONTENT_ENCONDING, | ||
name: BINARY_HEADERS_03.CONTENT_ENCONDING, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getSubject = { | ||
name: Constants.BINARY_HEADERS_03.SUBJECT, | ||
name: BINARY_HEADERS_03.SUBJECT, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getType = { | ||
name: Constants.BINARY_HEADERS_03.TYPE, | ||
name: BINARY_HEADERS_03.TYPE, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getSpecversion = { | ||
name: Constants.BINARY_HEADERS_03.SPEC_VERSION, | ||
name: BINARY_HEADERS_03.SPEC_VERSION, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getSource = { | ||
name: Constants.BINARY_HEADERS_03.SOURCE, | ||
name: BINARY_HEADERS_03.SOURCE, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getId = { | ||
name: Constants.BINARY_HEADERS_03.ID, | ||
name: BINARY_HEADERS_03.ID, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getTime = { | ||
name: Constants.BINARY_HEADERS_03.TIME, | ||
name: BINARY_HEADERS_03.TIME, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getSchemaurl = { | ||
name: Constants.BINARY_HEADERS_03.SCHEMA_URL, | ||
name: BINARY_HEADERS_03.SCHEMA_URL, | ||
parser: (v) => v | ||
}; | ||
|
||
function HTTPBinary(configuration) { | ||
this.emitter = new BinaryHTTPEmitter( | ||
configuration, | ||
headerByGetter, | ||
Constants.BINARY_HEADERS_03.EXTENSIONS_PREFIX | ||
); | ||
} | ||
|
||
HTTPBinary.prototype.emit = function(cloudevent) { | ||
return this.emitter.emit(cloudevent); | ||
}; | ||
|
||
module.exports = HTTPBinary; | ||
module.exports = headerByGetter; |
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,59 +1,48 @@ | ||
const BinaryHTTPEmitter = require("./emitter_binary.js"); | ||
|
||
const Constants = require("./constants.js"); | ||
const { | ||
HEADER_CONTENT_TYPE, | ||
BINARY_HEADERS_1 | ||
} = require("./constants.js"); | ||
|
||
const headerByGetter = {}; | ||
|
||
headerByGetter.getDataContentType = { | ||
name: Constants.HEADER_CONTENT_TYPE, | ||
name: HEADER_CONTENT_TYPE, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getSubject = { | ||
name: Constants.BINARY_HEADERS_1.SUBJECT, | ||
name: BINARY_HEADERS_1.SUBJECT, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getType = { | ||
name: Constants.BINARY_HEADERS_1.TYPE, | ||
name: BINARY_HEADERS_1.TYPE, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getSpecversion = { | ||
name: Constants.BINARY_HEADERS_1.SPEC_VERSION, | ||
name: BINARY_HEADERS_1.SPEC_VERSION, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getSource = { | ||
name: Constants.BINARY_HEADERS_1.SOURCE, | ||
name: BINARY_HEADERS_1.SOURCE, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getId = { | ||
name: Constants.BINARY_HEADERS_1.ID, | ||
name: BINARY_HEADERS_1.ID, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getTime = { | ||
name: Constants.BINARY_HEADERS_1.TIME, | ||
name: BINARY_HEADERS_1.TIME, | ||
parser: (v) => v | ||
}; | ||
|
||
headerByGetter.getDataschema = { | ||
name: Constants.BINARY_HEADERS_1.DATA_SCHEMA, | ||
name: BINARY_HEADERS_1.DATA_SCHEMA, | ||
parser: (v) => v | ||
}; | ||
|
||
function HTTPBinary(configuration) { | ||
this.emitter = new BinaryHTTPEmitter( | ||
configuration, | ||
headerByGetter, | ||
Constants.BINARY_HEADERS_1.EXTENSIONS_PREFIX | ||
); | ||
} | ||
|
||
HTTPBinary.prototype.emit = function(cloudevent) { | ||
return this.emitter.emit(cloudevent); | ||
}; | ||
|
||
module.exports = HTTPBinary; | ||
module.exports = headerByGetter; |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"1.0" => "latest supported"?
In general, the version specific comments throughout these docs will have a short shelf life.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@erikerikson that's a valid point. I would like to avoid having that hold up the PR, so I've added a new issue to address it #160. Work for you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me