-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into expose-constants
- Loading branch information
Showing
15 changed files
with
588 additions
and
322 deletions.
There are no files selected for viewing
This file contains 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 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,9 +1,11 @@ | ||
const CloudEvent = require("./lib/cloudevent.js"); | ||
const HTTPReceiver = require("./lib/bindings/http/http_receiver.js"); | ||
const HTTPEmitter = require("./lib/bindings/http/http_emitter.js"); | ||
const Constants = require("./lib/bindings/http/constants.js"); | ||
|
||
module.exports = { | ||
CloudEvent, | ||
HTTPReceiver, | ||
HTTPEmitter, | ||
Constants | ||
}; |
This file contains 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 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 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.