fastify-cloudevents
Advanced tools
Comparing version 0.3.0 to 0.4.0
# Change Log | ||
## [0.4.0](https://github.com/smartiniOnGitHub/fastify-cloudevents/releases/tag/0.4.0) (2019-03-16) | ||
Summary Changelog: | ||
- Update docs and examples to show the usage of new plugin features | ||
- Update all dependencies to latest release, but stay on Fastify v1 for now | ||
- In plugin option `serverUrlMode` add a new value (default choice now) 'pluginAndRequestSimplified' | ||
to simplify (remove URL arguments) when building the value for the 'sourceURL' attribute | ||
- In serialize function, add a boolean option 'onlyValid' (by default false) to serialize | ||
only a valid CloudEvent instance | ||
- Remove some inline logic and instead use methods exposed by Transformer (from the CloudEvent library) | ||
- Other small improvements to go towards plugin '1.0.0' | ||
- Update tests due to some behavior (for edge cases) was fixed in the CloudEvent library | ||
- Improve (a little) test code coverage for functions exposed by the plugin | ||
## [0.3.0](https://github.com/smartiniOnGitHub/fastify-cloudevents/releases/tag/0.3.0) (2019-03-04) | ||
@@ -4,0 +17,0 @@ Summary Changelog: |
@@ -27,3 +27,3 @@ /* | ||
port: 3000, | ||
serverUrlMode: 'pluginAndRequestUrl', // same behavior as default value, but in this way set in CloudEvent extension object | ||
serverUrlMode: 'pluginAndRequestSimplified', // same behavior as default value, but in this way set in CloudEvent extension object | ||
baseNamespace: 'com.github.smartiniOnGitHub.fastify-cloudevents.example-enhanced', | ||
@@ -45,3 +45,3 @@ includeHeaders: true, // change from default value, as a sample | ||
while (true) { | ||
const timestamp = Math.floor(Date.now()) | ||
const timestamp = Date.now() | ||
yield `${idPrefix}@${timestamp}` | ||
@@ -111,3 +111,8 @@ } | ||
fastify.get('/time', async (req, reply) => { | ||
return { timestamp: Math.floor(Date.now()) } | ||
const now = new Date() | ||
const timestamp = now.getTime() | ||
return { | ||
timestamp, | ||
time: now.toISOString() | ||
} | ||
}) | ||
@@ -123,3 +128,3 @@ // example route, to always generate an error | ||
// as a sample, wrap this error into a CloudEvent ... | ||
const path = '/error' // hardcode it, as a simple way to extract | ||
const path = '/error' // hardcode current url path, as a simple way to have it | ||
const processInfoAsData = fastify.CloudEventTransformer.processInfoToData() | ||
@@ -126,0 +131,0 @@ const errorAsData = fastify.CloudEventTransformer.errorToData(err, { |
@@ -73,3 +73,8 @@ /* | ||
fastify.get('/time', async (req, reply) => { | ||
return { timestamp: Math.floor(Date.now()) } | ||
const now = new Date() | ||
const timestamp = now.getTime() | ||
return { | ||
timestamp, | ||
time: now.toISOString() | ||
} | ||
}) | ||
@@ -76,0 +81,0 @@ |
{ | ||
"name": "fastify-cloudevents", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Fastify Plugin to serialize events in the CloudEvents standard format", | ||
@@ -13,2 +13,3 @@ "main": "src/plugin", | ||
"test:unit:debug": "tap -T --strict --node-arg=--inspect-brk test/*.test.js test/*/*.test.js", | ||
"test:coverage": "npm run test:unit -- --cov --coverage-report=html", | ||
"test": "npm run lint && npm run test:unit" | ||
@@ -18,10 +19,9 @@ }, | ||
"fastify-plugin": "^1.5.0", | ||
"fast-json-stringify": "^1.11.2", | ||
"cloudevent": "^0.3.0" | ||
"fast-json-stringify": "^1.11.3", | ||
"cloudevent": "^0.4.0" | ||
}, | ||
"devDependencies": { | ||
"fastify": "^1.14.2", | ||
"simple-get": "^3.0.3", | ||
"fastify": "^1.14.4", | ||
"standard": "^12.0.1", | ||
"tap": "^12.5.3" | ||
"tap": "^12.6.0" | ||
}, | ||
@@ -28,0 +28,0 @@ "peerDependencies": {}, |
@@ -75,5 +75,7 @@ # fastify-cloudevents | ||
(any non null value will cause this setting to be aded to the extension attribute): | ||
- null, (default value) same as 'pluginAndRequestUrl' | ||
- null, (default value) same as 'pluginAndRequestSimplified' | ||
- 'pluginServerUrl', use only the given `serverUrl` | ||
- 'pluginAndRequestUrl', use the given `serverUrl` and add the current request url | ||
- 'pluginAndRequestSimplified', use the given `serverUrl` and add the current request url, | ||
but without arguments (if any) | ||
- 'requestUrl', use only the request url | ||
@@ -80,0 +82,0 @@ - anything other, will raise an `Error` |
@@ -65,10 +65,16 @@ /* | ||
* encodedData (string, no default) already encoded data (but consistency with the contentType is not checked), | ||
* onlyValid (boolean, default false) to serialize only if it's a valid instance, | ||
* @return {string} the serialized event, as a string | ||
* @throws {Error} if event is undefined or null, or an option is undefined/null/wrong | ||
* @throws {Error} if onlyValid is true, and the given event is not a valid CloudEvent instance | ||
*/ | ||
function serialize (event, { encoder, encodedData } = {}) { | ||
function serialize (event, { encoder, encodedData, onlyValid = false } = {}) { | ||
ensureIsObject(event, 'event') | ||
if (event.contentType === 'application/json') { | ||
return stringify(event) | ||
if (event.contentType === CloudEvent.contentTypeDefault()) { | ||
if ((onlyValid === false) || (onlyValid === true && CloudEvent.isValidEvent(event) === true)) { | ||
return stringify(event) | ||
} else { | ||
throw new Error(`Unable to serialize a not valid CloudEvent.`) | ||
} | ||
} | ||
@@ -90,3 +96,9 @@ // else | ||
} | ||
return stringify({ ...event, data: encodedData }) | ||
const newEvent = CloudEventTransformer.mergeObjects(event, { data: encodedData }) | ||
// console.log(`DEBUG - new event details: ${CloudEventTransformer.dumpObject(newEvent, 'newEvent')}`) | ||
if ((onlyValid === false) || (onlyValid === true && CloudEvent.isValidEvent(newEvent) === true)) { | ||
return stringify(newEvent) | ||
} else { | ||
throw new Error(`Unable to serialize a not valid CloudEvent.`) | ||
} | ||
} | ||
@@ -106,3 +118,5 @@ | ||
function buildSourceUrl (url = '') { | ||
if (serverUrlMode === null || serverUrlMode === 'pluginAndRequestUrl') { | ||
if (serverUrlMode === null || serverUrlMode === 'pluginAndRequestSimplified') { | ||
return serverUrl + CloudEventTransformer.uriStripArguments(url) | ||
} else if (serverUrlMode === 'pluginAndRequestUrl') { | ||
return serverUrl + url | ||
@@ -161,3 +175,3 @@ } else if (serverUrlMode === 'pluginServerUrl') { | ||
id: req.id, | ||
timestamp: Math.floor(Date.now()), | ||
timestamp: CloudEventTransformer.timestampToNumber(), | ||
req: { | ||
@@ -177,3 +191,3 @@ httpVersion: req.httpVersion, | ||
) | ||
// console.log(`DEBUG - onRequest: created CloudEvent ${fastify.CloudEventTransformer.dumpObject(ce, 'ce')}`) | ||
// console.log(`DEBUG - onRequest: created CloudEvent ${CloudEventTransformer.dumpObject(ce, 'ce')}`) | ||
// send the event to the callback | ||
@@ -196,3 +210,3 @@ onRequestCallback(ce) | ||
id: request.id, | ||
timestamp: Math.floor(Date.now()), | ||
timestamp: CloudEventTransformer.timestampToNumber(), | ||
request: { | ||
@@ -232,3 +246,3 @@ id: request.id, | ||
id: request.id, | ||
timestamp: Math.floor(Date.now()), | ||
timestamp: CloudEventTransformer.timestampToNumber(), | ||
request: { | ||
@@ -267,3 +281,3 @@ id: request.id, | ||
// id: res.id, // not available | ||
timestamp: Math.floor(Date.now()), | ||
timestamp: CloudEventTransformer.timestampToNumber(), | ||
res: { | ||
@@ -304,3 +318,3 @@ statusCode: res.statusCode, | ||
{ | ||
timestamp: Math.floor(Date.now()), | ||
timestamp: CloudEventTransformer.timestampToNumber(), | ||
description: 'plugin shutdown' | ||
@@ -323,3 +337,3 @@ }, // data | ||
{ | ||
timestamp: Math.floor(Date.now()), | ||
timestamp: CloudEventTransformer.timestampToNumber(), | ||
description: 'plugin startup successfully', | ||
@@ -364,3 +378,3 @@ version: pluginVersion | ||
while (true) { | ||
const timestamp = Math.floor(Date.now()) | ||
const timestamp = CloudEventTransformer.timestampToNumber() | ||
yield `${idPrefix}@${timestamp}` | ||
@@ -367,0 +381,0 @@ } |
50116
3
685
120
+ Addedcloudevent@0.4.0(transitive)
- Removedcloudevent@0.3.0(transitive)
Updatedcloudevent@^0.4.0
Updatedfast-json-stringify@^1.11.3