Fastify Plugin to serialize events in the CloudEvents standard format
npm install fastify-cloudeventsjs
const fastify = require('fastify')()
// define functions to use in plugin configuration:
// idExample generator, callbackExample(ce) , etc ...
// register the plugin with some options, for example:
fastify.register(require('fastify-cloudevents'), {
serverUrl: 'http://0.0.0.0:3000',
idGenerator: idExample,
onRequestCallback: callbackExample,
onErrorCallback: callbackExample,
onResponseCallback: callbackExample,
cloudEventOptions: { }
})
// implementation ...
fastify.listen({ port: 3000, host: 'localhost' }, (err, address) => {
if (err) throw err
console.log(server listening on ${address})
})
`
In the example folder there are some simple server scripts
that uses the plugin (inline but it's the same using it from npm registry):
- example is a simple one
- example-enhanced is a more complex sample
to show even how to raise own events (normal, errors, and some custom)
Requirements
Fastify ^4.0.1 , Node.js 14 LTS (14.15.0) or later.
Note that plugin releases 3.x are for Fastify 3.x, 4.x are for Fastify 4.x, etc.
Sources
Source code is all inside main repo:
fastify-cloudevents.
Documentation generated from source code (library API):
here.
Note
The plugin decorate Fastify and expose some functions:
- CloudEvent, the CloudEvent implementation, as a class
- CloudEventTransformer, the CloudEventTransformer utility class
- JSONBatch, the class to handle JSONBatch instances
- cloudEventJSONSchema, the JSONSchema for a CloudEvent used in the plugin, as an object
- cloudEventSerializeFast, a serialize function implemented here using fast-json-stringify
and not standard JSON serialization functions; note that similar features of the underlying library
has been implemented here (like serialization options)
- cloudEventValidateFast, a validation function implemented here using ajv
(which is a dependency of fast-json-stringify) that uses a schema compiler
Plugin options are:
- baseNamespace, a base namespace for the type; more specific suffix
should be added to it in any CloudEvent
- cloudEventExtensions, CloudEvent extensions to add in all generated events
- cloudEventOptions, CloudEvent options common to all generated events;
anyway objects are copied to not be shared between instances
- idGenerator, a generator function that returns the id (if possible, unique) for any CloudEvent
- includeHeaders, a boolean flag to add request headers in generated CloudEvents when true
(by default is false)
- includeHttpAttributes, a boolean flag to add some HTTP attributes in generated CloudEvents when true
(by default is false)
- includeRedundantAttributes, a boolean flag to add some redundant attributes
in the data section of generated CloudEvents when true (by default is false)
- 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 not null value will cause this setting to be added to extensions (if set not null in plugin options):
- null, (default value) same as 'pluginAndRequestSimplified'
but without arguments (if any)
- 'pluginAndRequestSimplified', use the given serverUrl and add the current request url,
- 'pluginAndRequestUrl', use the given serverUrl and add the current request url
- 'pluginServerUrl', use only the given serverUrl
- 'requestUrl', use only the request url
- anything other, will raise an Error
- onCloseCallback, callback to handle generated CloudEvents in Fastify hook onClose, for the plugin
- onErrorCallback, callback to handle generated CloudEvents in Fastify hook onError
- onReadyCallback, callback to handle the generated CloudEvent in Fastify lifecycle function ready,
for the plugin (when the plugin has been loaded)
- onRegisterCallback, callback to handle generated CloudEvents in Fastify hook onRegister
- onRequestCallback, callback to handle generated CloudEvents in Fastify hook onRequest
- onResponseCallback, callback to handle generated CloudEvents in Fastify hook onResponse
- onRouteCallback, callback to handle generated CloudEvents in Fastify hook onRoute
- onSendCallback, callback to handle generated CloudEvents in Fastify hook onSend
- onTimeoutCallback, callback to handle generated CloudEvents in Fastify hook onTimeout
- preHandlerCallback, callback to handle generated CloudEvents in Fastify hook preHandler
- preParsingCallback, callback to handle generated CloudEvents in Fastify hook preParsing
- preSerializationCallback, callback to handle generated CloudEvents in Fastify hook preSerialization
- preValidationCallback, callback to handle generated CloudEvents in Fastify hook preValidation`