fastify-cloudevents
Advanced tools
Comparing version 0.1.4 to 0.2.0
# Change Log | ||
## [0.2.0](https://github.com/smartiniOnGitHub/fastify-cloudevents/releases/tag/0.2.0) (2018-12-17) | ||
Summary Changelog: | ||
- Update [cloudevent.js](https://github.com/smartiniOnGitHub/cloudevent.js/) to '0.2.0', | ||
with some breaking changes inside (like the source parameter moved outside its options, etc) | ||
- Change dependency from [cloudevent.js](https://www.npmjs.com/package/cloudevent.js) to the new name | ||
[cloudevent](https://www.npmjs.com/package/cloudevent) more searchable (the old name is deprecated) | ||
- Update Fastify dependencies to '1.1.0' or higher (but on 1.x) | ||
- Add plugin option `serverUrlMode` (by default null) to specify in which mode source | ||
must be constructed in generated CloudEvent instances; see in the README for related values to use. | ||
As a sample, add in the `example-enhanced` with a value to have the same behavior of its default, | ||
but that way it will be put in CloudEvent extension object. | ||
## [0.1.4](https://github.com/smartiniOnGitHub/fastify-cloudevents/releases/tag/0.1.4) (2018-11-15) | ||
@@ -4,0 +16,0 @@ Summary Changelog: |
@@ -27,3 +27,4 @@ /* | ||
port: 3000, | ||
baseNamespace: 'com.github.smartiniOnGitHub.fastify-cloudevents.example', | ||
serverUrlMode: 'pluginAndRequestUrl', // same behavior as default value, but in this way set in CloudEvent extension object | ||
baseNamespace: 'com.github.smartiniOnGitHub.fastify-cloudevents.example-enhanced', | ||
includeHeaders: true, // change from default value, as a sample | ||
@@ -34,4 +35,4 @@ cloudEventOptions: { | ||
} | ||
k.serverUrl = `${k.protocol}://${k.address}:${k.port}/` | ||
k.cloudEventOptions.source = k.serverUrl | ||
k.serverUrl = `${k.protocol}://${k.address}:${k.port}` | ||
k.source = k.serverUrl | ||
// assert(k !== null) | ||
@@ -59,2 +60,3 @@ | ||
serverUrl: k.serverUrl, | ||
serverUrlMode: k.serverUrlMode, | ||
idGenerator: gen, | ||
@@ -85,2 +87,3 @@ includeHeaders: k.includeHeaders, | ||
`${k.baseNamespace}.server-script.start`, | ||
k.source, | ||
{ | ||
@@ -117,2 +120,3 @@ timestamp: Math.floor(Date.now()), | ||
`${k.baseNamespace}.error`, | ||
k.source, | ||
{ | ||
@@ -135,2 +139,3 @@ timestamp: Math.floor(Date.now()), | ||
`${k.baseNamespace}.listen`, | ||
k.source, | ||
{ | ||
@@ -151,2 +156,3 @@ timestamp: Math.floor(Date.now()), | ||
`${k.baseNamespace}.error`, | ||
k.source, | ||
{ | ||
@@ -170,2 +176,3 @@ timestamp: Math.floor(Date.now()), | ||
`${k.baseNamespace}.ready`, | ||
k.source, | ||
{ | ||
@@ -172,0 +179,0 @@ timestamp: Math.floor(Date.now()), |
@@ -29,4 +29,4 @@ /* | ||
} | ||
k.serverUrl = `${k.protocol}://${k.address}:${k.port}/` | ||
k.cloudEventOptions.source = k.serverUrl | ||
k.serverUrl = `${k.protocol}://${k.address}:${k.port}` | ||
k.source = k.serverUrl | ||
@@ -84,2 +84,3 @@ // define a sample id generator here | ||
`${k.baseNamespace}.listen`, | ||
k.source, | ||
{ | ||
@@ -104,2 +105,3 @@ timestamp: Math.floor(Date.now()), | ||
`${k.baseNamespace}.ready`, | ||
k.source, | ||
{ | ||
@@ -106,0 +108,0 @@ timestamp: Math.floor(Date.now()), |
{ | ||
"name": "fastify-cloudevents", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "Fastify Plugin to serialize events in the CloudEvents standard format", | ||
@@ -14,15 +14,15 @@ "main": "src/plugin", | ||
"dependencies": { | ||
"fastify-plugin": "^1.2.1", | ||
"fast-json-stringify": "^1.9.1", | ||
"cloudevent.js": "^0.1.0" | ||
"fastify-plugin": "^1.3.0", | ||
"fast-json-stringify": "^1.9.2", | ||
"cloudevent": "^0.2.0" | ||
}, | ||
"devDependencies": { | ||
"fastify": "^1.13.0", | ||
"fastify": "^1.13.2", | ||
"simple-get": "^3.0.3", | ||
"standard": "^12.0.1", | ||
"tap": "^12.1.0" | ||
"tap": "^12.1.1" | ||
}, | ||
"peerDependencies": {}, | ||
"engines": { | ||
"node": ">=8.12.0" | ||
"node": ">=8.14.0" | ||
}, | ||
@@ -29,0 +29,0 @@ "homepage": "https://github.com/smartiniOnGitHub/fastify-cloudevents#readme", |
@@ -7,2 +7,3 @@ # fastify-cloudevents | ||
[![Coverage Status](https://coveralls.io/repos/github/smartiniOnGitHub/fastify-cloudevents/badge.svg?branch=master)](https://coveralls.io/github/smartiniOnGitHub/fastify-cloudevents/?branch=master) | ||
[![Dependencies](https://david-dm.org/smartiniOnGitHub/fastify-cloudevents.svg)](https://david-dm.org/smartiniOnGitHub/fastify-cloudevents.svg) | ||
@@ -33,2 +34,3 @@ Fastify Plugin to serialize events in the CloudEvents standard format. | ||
fastify.register(require('fastify-cloudevents'), { | ||
serverUrl: 'http://0.0.0.0:3000', | ||
idGenerator: idExample, | ||
@@ -52,4 +54,4 @@ onRequestCallback: callbackExample, | ||
Fastify 1.x . | ||
Node.js 8.12.x or later. | ||
Fastify ^1.1.0 . | ||
Node.js 8.14.x or later. | ||
@@ -64,4 +66,11 @@ | ||
Plugin options are: | ||
- `serverUrl`, the absolute URL of the current webapp, to use as a base `source` in generated CloudEvents | ||
- `baseNamespace`, a base namespace for the `eventType`, more speficic suffix will be added in any CloudEvent | ||
- `serverUrl`, the URL (absolute, or relative) of the current webapp, to use as a base `source` in generated CloudEvents | ||
- `serverUrlMode`, the mode to build the `source` attribute in generated CloudEvents | ||
(any non null value will cause this setting to be aded to the extension attribute): | ||
- null, (default value) same as 'pluginAndRequestUrl' | ||
- 'pluginServerUrl', use only the given `serverUrl` | ||
- 'pluginAndRequestUrl', use the given `serverUrl` and add the current request url | ||
- 'requestUrl', use only the request url | ||
- anything other, will raise an `Error` | ||
- `baseNamespace`, a base namespace for the `eventType`, more specific suffix will be added in any CloudEvent | ||
- `idGenerator`, a generator function that returns the id (if possible, unique) for any CloudEvent | ||
@@ -68,0 +77,0 @@ - `includeHeaders`, a boolean flag that when `true` tells that request headers will be put in generated CloudEvents (but by default is `false`) |
@@ -27,4 +27,4 @@ /* | ||
*/ | ||
const CloudEvent = require('cloudevent.js') // get CloudEvent definition and related utilities | ||
const CloudEvent = require('cloudevent') // get CloudEvent definition and related utilities | ||
module.exports = CloudEvent |
@@ -19,3 +19,3 @@ /* | ||
const fp = require('fastify-plugin') | ||
const cloudEventHandler = require('cloudevent.js') // get CloudEvent definition and related utilities | ||
const cloudEventHandler = require('cloudevent') // get CloudEvent definition and related utilities | ||
@@ -25,2 +25,3 @@ function fastifyCloudEvents (fastify, options, next) { | ||
serverUrl = '/', | ||
serverUrlMode = null, | ||
baseNamespace = 'com.github.fastify.plugins.fastify-cloudevents', | ||
@@ -40,2 +41,3 @@ idGenerator = idMaker(), | ||
ensureIsString(serverUrl, 'serverUrl') | ||
ensureIsString(serverUrlMode, 'serverUrlMode') | ||
ensureIsString(baseNamespace, 'baseNamespace') | ||
@@ -92,2 +94,3 @@ ensureIsObject(idGenerator, 'idGenerator') | ||
// TODO: handle even non default contentType when serializing the data attribute ... wip | ||
// TODO: for example add an optional argument in the method for already serialized data, then merge it in a new object in the stringify call ... wip | ||
const serialized = stringify(event) | ||
@@ -98,2 +101,25 @@ // console.log(`DEBUG - serialize: serialized = '${serialized}'`) | ||
/** | ||
* Build the value for the source field of the CloudEvent, | ||
* depending on the plugin configuration of options | ||
* `serverUrlMode`, `serverUrl`, | ||
* and the uri part of the current request. | ||
* Note that this is mainly for usage inside the plugin, | ||
* but in some cases could be useful even outside. | ||
* | ||
* @param {string} url the uri part of the current request | ||
* @return {string} the source value to use, as a string | ||
*/ | ||
function buildSourceUrl (url = '') { | ||
if (serverUrlMode === null || serverUrlMode === 'pluginAndRequestUrl') { | ||
return serverUrl + url | ||
} else if (serverUrlMode === 'pluginServerUrl') { | ||
return serverUrl | ||
} else if (serverUrlMode === 'requestUrl') { | ||
return url | ||
} else { | ||
throw new Error(`Illegal value for serverUrlMode: '${serverUrlMode}'`) | ||
} | ||
} | ||
// execute plugin code | ||
@@ -109,5 +135,6 @@ fastify.decorate('CloudEvent', cloudEventHandler) | ||
} | ||
// override source if not already specified | ||
if (cloudEventOptions.source === null || typeof cloudEventOptions.source !== 'string') { | ||
cloudEventOptions.source = serverUrl | ||
// add to extensions the serverUrlMode defined, if set | ||
if (serverUrlMode !== null) { | ||
cloudEventOptions.extensions = cloudEventOptions.extensions || {} | ||
cloudEventOptions.extensions.serverUrlMode = serverUrlMode | ||
} | ||
@@ -119,4 +146,6 @@ | ||
const headers = (includeHeaders === null || includeHeaders === false) ? null : req.headers | ||
const sourceUrl = buildSourceUrl(req.url) | ||
const ce = new fastify.CloudEvent(idGenerator.next().value, | ||
`${baseNamespace}.onRequest`, | ||
sourceUrl, | ||
{ | ||
@@ -149,4 +178,6 @@ id: req.id, | ||
const headers = (includeHeaders === null || includeHeaders === false) ? null : request.headers | ||
const sourceUrl = buildSourceUrl(request.req.url) | ||
const ce = new fastify.CloudEvent(idGenerator.next().value, | ||
`${baseNamespace}.preHandler`, | ||
sourceUrl, | ||
{ | ||
@@ -181,4 +212,6 @@ id: request.id, | ||
const headers = (includeHeaders === null || includeHeaders === false) ? null : request.headers | ||
const sourceUrl = buildSourceUrl(request.req.url) | ||
const ce = new fastify.CloudEvent(idGenerator.next().value, | ||
`${baseNamespace}.onSend`, | ||
sourceUrl, | ||
{ | ||
@@ -213,4 +246,6 @@ id: request.id, | ||
fastify.addHook('onResponse', (res, next) => { | ||
const sourceUrl = buildSourceUrl() | ||
const ce = new fastify.CloudEvent(idGenerator.next().value, | ||
`${baseNamespace}.onResponse`, | ||
sourceUrl, | ||
{ | ||
@@ -235,4 +270,6 @@ // id: res.id, // not available | ||
fastify.addHook('onRoute', (routeOptions) => { | ||
const sourceUrl = buildSourceUrl() | ||
const ce = new fastify.CloudEvent(idGenerator.next().value, | ||
`${baseNamespace}.onRoute`, | ||
sourceUrl, | ||
routeOptions, // data | ||
@@ -248,4 +285,6 @@ cloudEventOptions | ||
fastify.addHook('onClose', (instance, done) => { | ||
const sourceUrl = buildSourceUrl() | ||
const ce = new fastify.CloudEvent(idGenerator.next().value, | ||
`${baseNamespace}.onClose`, | ||
sourceUrl, | ||
{ | ||
@@ -265,4 +304,6 @@ timestamp: Math.floor(Date.now()), | ||
// hook to plugin successful startup, not server | ||
const sourceUrl = buildSourceUrl() | ||
const ce = new fastify.CloudEvent(idGenerator.next().value, | ||
`${baseNamespace}.ready`, | ||
sourceUrl, | ||
{ | ||
@@ -315,4 +356,4 @@ timestamp: Math.floor(Date.now()), | ||
module.exports = fp(fastifyCloudEvents, { | ||
fastify: '1.x', | ||
fastify: '^1.1.0', | ||
name: 'fastify-cloudevents' | ||
}) |
42849
616
103
+ Addedcloudevent@^0.2.0
+ Addedcloudevent@0.2.2(transitive)
- Removedcloudevent.js@^0.1.0
- Removedcloudevent.js@0.1.0(transitive)
Updatedfast-json-stringify@^1.9.2
Updatedfastify-plugin@^1.3.0