fastify-cloudevents
Advanced tools
Comparing version
# Change Log | ||
## [2.6.0](https://github.com/smartiniOnGitHub/fastify-cloudevents/releases/tag/2.6.0) (2021-03-28) | ||
[Full Changelog](https://github.com/smartiniOnGitHub/fastify-cloudevents/compare/2.5.0...2.6.0) | ||
Summary Changelog: | ||
- Update dependency on cloudevent to '0.8.x' which implements the | ||
[v1.0.1 - CloudEvents Spec](https://github.com/cloudevents/spec/releases/tag/v1.0.1) | ||
with many simplifications/clarifications on v1.0 | ||
- Update requirements to latest Fastify 2.x, so currently release '^2.15.3' | ||
- Feature: keep compatibility with Node.js 8 (only for plugin releases 2.x) | ||
- Feature: add to Fastify a decorator to return the JSONSchema (for a CloudEvent) | ||
used in the plugin (with some small tweaks), instead of retrieving it | ||
from the underlying library | ||
- Feature: update fast serialization to handle even data as value (string or boolean or number) | ||
with datacontenttype not default (for example 'text/plain') | ||
- Feature: add to Fastify a decorator to add a function that validates with a schema compiler | ||
and return validation results and errors (if any) | ||
- Feature: generate documentation from sources with JSDoc | ||
## [2.5.0](https://github.com/smartiniOnGitHub/fastify-cloudevents/releases/tag/2.5.0) (2020-10-02) | ||
@@ -4,0 +21,0 @@ [Full Changelog](https://github.com/smartiniOnGitHub/fastify-cloudevents/compare/2.4.0...2.5.0) |
/* | ||
* Copyright 2018 the original author or authors. | ||
* Copyright 2018-2021 the original author or authors. | ||
* | ||
@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License"); |
/* | ||
* Copyright 2018 the original author or authors. | ||
* Copyright 2018-2021 the original author or authors. | ||
* | ||
@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License"); |
{ | ||
"name": "fastify-cloudevents", | ||
"version": "2.5.0", | ||
"version": "2.6.0", | ||
"description": "Fastify Plugin to serialize events in the CloudEvents standard format", | ||
@@ -8,2 +8,5 @@ "main": "src/plugin", | ||
"dependency:log": "npm list > ./temp/dependencies.log", | ||
"docs:clean": "rm -rf ./docs/*", | ||
"docs:generate": "npx jsdoc -c .jsdoc.json -R README.md", | ||
"docs": "npm run docs:clean && npm run docs:generate", | ||
"example:debug": "node --inspect-brk example/example", | ||
@@ -13,3 +16,6 @@ "example-enhanced:debug": "node --inspect-brk example/example-enhanced", | ||
"example-enhanced": "node example/example-enhanced", | ||
"lint": "standard \"./*.js\" \"./src/**/*.js\" \"./test/**/*.test.js\" \"./example/**/*.js\"", | ||
"lint": "npm run lint:standard", | ||
"lint:fix": "standard --fix", | ||
"lint:standard": "standard --verbose", | ||
"test:clean": "rm -rf .nyc_output/* ./coverage/*", | ||
"test:coverage": "npm run test:unit -- --cov --coverage-report=html", | ||
@@ -24,8 +30,9 @@ "test:unit": "tap -J --comments --no-esm --strict test/*.test.js", | ||
"fast-json-stringify": "^1.21.0", | ||
"cloudevent": "~0.7.0" | ||
"cloudevent": "~0.8.0" | ||
}, | ||
"devDependencies": { | ||
"jsdoc": "^3.6.6", | ||
"fastify": "^2.15.3", | ||
"standard": "^14.3.4", | ||
"tap": "^14.10.8" | ||
"standard": "^16.0.3", | ||
"tap": "^14.11.0" | ||
}, | ||
@@ -32,0 +39,0 @@ "peerDependencies": {}, |
@@ -13,3 +13,3 @@ # fastify-cloudevents | ||
Current release uses the v1.0 of the CloudEvents Spec. | ||
Current release uses the v1.0.1 of the CloudEvents Spec. | ||
@@ -63,6 +63,15 @@ The purpose of this plugin is to let Fastify web applications create instances of CloudEvents | ||
Fastify ^2.15.0 , Node.js 8.17.x or later. | ||
Fastify ^2.15.3 , Node.js 8.17.x (but recommended 10.23.1) or later. | ||
Note that plugin releases 2.x are for Fastify 2.x, etc. | ||
## Sources | ||
Source code is all inside main repo: | ||
[fastify-cloudevents](https://github.com/smartiniOnGitHub/fastify-cloudevents). | ||
Documentation generated from source code (library API): | ||
[here](https://smartiniongithub.github.io/fastify-cloudevents/). | ||
## Note | ||
@@ -74,5 +83,8 @@ | ||
- `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 | ||
@@ -79,0 +91,0 @@ Plugin options are: |
/* | ||
* Copyright 2018 the original author or authors. | ||
* Copyright 2018-2021 the original author or authors. | ||
* | ||
@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License"); |
/* | ||
* Copyright 2018 the original author or authors. | ||
* Copyright 2018-2021 the original author or authors. | ||
* | ||
@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License"); |
/* | ||
* Copyright 2018 the original author or authors. | ||
* Copyright 2018-2021 the original author or authors. | ||
* | ||
@@ -78,2 +78,4 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
delete ceSchema.properties.data | ||
// remove the definition of time, or it won't be managed in the right way | ||
delete ceSchema.properties.time | ||
// add additionalProperties, to let serialization export properties not in schema | ||
@@ -106,3 +108,3 @@ // ceSchema.additionalProperties = true // already in schema, so no need to add here | ||
} | ||
// else | ||
// else (non defaut datacontenttype) | ||
if (encoder !== undefined && encoder !== null) { | ||
@@ -115,5 +117,9 @@ if (typeof encoder !== 'function') { | ||
// encoder not defined, check encodedData | ||
if (encodedData === undefined || encodedData === null) { | ||
throw new Error(`Missing encoder function: use encoder function or already encoded data with the given content type: '${event.datacontenttype}'.`) | ||
// but mandatory only for non-value data | ||
if (!isValue(event.data) && !isDefinedAndNotNull(encodedData)) { | ||
throw new Error(`Missing encoder function: use encoder function or already encoded data with the given data content type: '${event.datacontenttype}'.`) | ||
} | ||
if (isValue(event.data) && !isDefinedAndNotNull(encodedData)) { | ||
encodedData = `${event.data}` | ||
} | ||
} | ||
@@ -132,2 +138,24 @@ if (typeof encodedData !== 'string') { | ||
// use 'ajv' (dependency of fast-json-stringify') | ||
const Ajv = require('ajv') | ||
const ajv = new Ajv({ coerceTypes: true, removeAdditional: true }) | ||
const validateFromSchema = ajv.compile(ceSchema) | ||
/** | ||
* Validate the given CloudEvent with the schema compiler already instanced. | ||
* | ||
* @param {!object} event the CloudEvent to serialize | ||
* @return {object} validation results: 'valid' boolean and 'errors' as array of strings or null | ||
* @throws {Error} if event is undefined or null | ||
*/ | ||
function validate (event) { | ||
ensureIsObjectPlain(event, 'event') | ||
const isValid = validateFromSchema(event) | ||
return { | ||
valid: isValid, | ||
errors: validateFromSchema.errors | ||
} | ||
} | ||
// execute plugin code | ||
@@ -137,3 +165,5 @@ fastify.decorate('CloudEvent', CloudEvent) | ||
fastify.decorate('JSONBatch', JSONBatch) | ||
fastify.decorate('cloudEventJSONSchema', ceSchema) | ||
fastify.decorate('cloudEventSerializeFast', serialize) | ||
fastify.decorate('cloudEventValidateFast', validate) | ||
@@ -354,2 +384,12 @@ // add to extensions the serverUrlMode defined, if set | ||
function isDefinedAndNotNull (arg) { | ||
return (arg !== undefined && arg !== null) | ||
} | ||
function isValue (arg) { | ||
return ((arg !== undefined && arg !== null) && | ||
(typeof arg === 'string' || typeof arg === 'boolean' || (typeof arg === 'number' && !isNaN(arg))) | ||
) | ||
} | ||
module.exports = fp(fastifyCloudEvents, { | ||
@@ -356,0 +396,0 @@ fastify: '^2.15.0', |
69251
5.13%928
3.92%170
7.59%4
33.33%+ Added
- Removed
Updated