cloudevent
Advanced tools
Comparing version 0.9.0 to 0.10.0
# Change Log | ||
## [0.10.0](https://github.com/smartiniOnGitHub/cloudevent.js/releases/tag/0.10.0) (2022-08-07) | ||
Summary Changelog: | ||
- Update requirements to Node.js 14 LTS (14.15.0) | ||
- Ensure all is good as before | ||
- Change CloudEvent validation methods/functions: the given argument to validate/check in strict mode | ||
now defaults to null, so validation/check will override the value in the event (is any) | ||
only when that flag has a value in arguments | ||
- Add a flag in CloudEvent serialization/deserialization to simplify debugging issues | ||
- Small improvements in Validator functions | ||
- Add some Utility functions, in its own source | ||
- Add an example (minimal) benchmark | ||
- Small improvements in performances (inline some small utility code block), | ||
as seen in flame graph on both examples; later check if do more | ||
- Rewritten some code to use new ES features and operators; | ||
but not (yet) private functions/methods to avoid less code coverage | ||
(to not add a library to mock that code for testing it once private; later check if it worth the effort) | ||
## [0.9.0](https://github.com/smartiniOnGitHub/cloudevent.js/releases/tag/0.9.0) (2022-04-16) | ||
@@ -4,0 +21,0 @@ Summary Changelog: |
@@ -22,4 +22,25 @@ /* | ||
const assert = require('assert').strict | ||
const assert = require('node:assert').strict | ||
// import some common example data | ||
const { | ||
// commonEventTime, | ||
ceCommonData, | ||
ceCommonExtensions, | ||
ceCommonOptions, | ||
ceCommonOptionsForTextData, | ||
ceCommonOptionsStrict, | ||
ceDataAsJSONString, | ||
ceDataAsString, | ||
ceDataAsStringEncoded, | ||
ceNamespace, | ||
ceOptionsNoStrict, | ||
// ceOptionsStrict, | ||
ceServerUrl, | ||
valOnlyValidAllInstance, | ||
valOnlyValidInstance, | ||
valOptionsNoStrict, | ||
valOptionsStrict | ||
} = require('./common-example-data') | ||
console.log('Sample script: start execution ...\n') | ||
@@ -48,26 +69,8 @@ | ||
console.log(`ce dump (but not good for validation): ${T.dumpObject(ceEmpty, 'ceEmpty')}`) | ||
const ceMinimalMandatoryUndefinedNoStrict = new CloudEvent(undefined, undefined, undefined, undefined, { strict: false }) // expected success | ||
const ceMinimalMandatoryUndefinedNoStrict = new CloudEvent(undefined, undefined, undefined, undefined, ceOptionsNoStrict) // expected success | ||
assert(ceMinimalMandatoryUndefinedNoStrict !== null) | ||
console.log(`ce dump (but not good for validation): ${T.dumpObject(ceMinimalMandatoryUndefinedNoStrict, 'ceMinimalMandatoryUndefinedNoStrict')}`) | ||
// const ceMinimalMandatoryUndefinedStrict = new CloudEvent(undefined, undefined, undefined, undefined, { strict: true }) // expected failure | ||
// const ceMinimalMandatoryUndefinedStrict = new CloudEvent(undefined, undefined, undefined, undefined, ceOptionsStrict) // expected failure | ||
// assert(ceMinimalMandatoryUndefinedStrict == null) // no, ReferenceError: ceMinimalMandatoryUndefinedStrict is not defined | ||
// define some common attributes | ||
const ceCommonOptions = { | ||
time: new Date(), // same as default | ||
datacontenttype: 'application/json', | ||
dataschema: 'http://my-schema.localhost.localdomain/v1/', | ||
subject: 'subject', | ||
strict: false // same as default | ||
} | ||
const ceCommonOptionsStrict = { ...ceCommonOptions, strict: true } | ||
const ceCommonOptionsForTextData = { ...ceCommonOptions, datacontenttype: 'text/plain' } | ||
const ceCommonExtensions = { exampleextension: 'value' } | ||
const ceNamespace = 'com.github.smartiniOnGitHub.cloudeventjs.testevent-v1.0.0' | ||
const ceServerUrl = '/test' | ||
const ceCommonData = { hello: 'world', year: 2020, enabled: true } | ||
const ceDataAsJSONString = '{ "hello": "world", "year": 2020, "enabled": true }' | ||
const ceDataAsString = 'Hello World, 2020' | ||
const ceDataEncoded = 'SGVsbG8gV29ybGQsIDIwMjA=' | ||
// create a sample minimal instance good for normal validation but not for strict validation ... | ||
@@ -173,3 +176,3 @@ const ceMinimalBadSource = new CloudEvent('1', ceNamespace, 'source (bad)', null) | ||
console.log(`ce validation results on ceFullTextData (no strict validation) = ${CloudEvent.validateEvent(ceFullTextData)}`) | ||
console.log(`ce validation results on ceFullTextData (with strict validation) = ${CloudEvent.validateEvent(ceFullTextData, { strict: true })}`) | ||
console.log(`ce validation results on ceFullTextData (with strict validation) = ${CloudEvent.validateEvent(ceFullTextData, valOptionsStrict)}`) | ||
// create an instance with data encoded in base64 | ||
@@ -180,3 +183,3 @@ const ceFullStrictBinaryData = new CloudEvent('6/full-strict-binary-data', | ||
null, // data | ||
{ ...ceCommonOptionsStrict, datainbase64: ceDataEncoded }, // use common strict options, and set binary data in base64 | ||
{ ...ceCommonOptionsStrict, datainbase64: ceDataAsStringEncoded }, // use common strict options, and set binary data in base64 | ||
ceCommonExtensions | ||
@@ -194,8 +197,8 @@ ) | ||
assert(!ceMinimalMandatoryUndefinedNoStrict.isValid()) | ||
// console.log(`DEBUG - ${CloudEvent.dumpValidationResults(ceMinimalBadSource, null, 'ceMinimalBadSource')}`) | ||
// console.log(`DEBUG | ${CloudEvent.dumpValidationResults(ceMinimalBadSource, null, 'ceMinimalBadSource')}`) | ||
assert(ceMinimalBadSource.isValid()) | ||
// console.log(`DEBUG - ${CloudEvent.dumpValidationResults(ceMinimalBadSource, { strict: true }, 'ceMinimalBadSource')}`) | ||
assert(!ceMinimalBadSource.isValid({ strict: true })) | ||
// console.log(`DEBUG | ${CloudEvent.dumpValidationResults(ceMinimalBadSource, valOptionsStrict, 'ceMinimalBadSource')}`) | ||
assert(!ceMinimalBadSource.isValid(valOptionsStrict)) | ||
assert(ceMinimal.isValid()) | ||
assert(ceMinimal.isValid({ strict: true })) | ||
assert(ceMinimal.isValid(valOptionsStrict)) | ||
assert(ceFull.isValid()) | ||
@@ -207,3 +210,3 @@ assert(ceFullStrict.isValid()) | ||
assert(ceFullTextData.isValid()) | ||
assert(ceFullTextData.isValid({ strict: true })) | ||
assert(ceFullTextData.isValid(valOptionsStrict)) | ||
assert(ceFullStrictBinaryData.isValid()) | ||
@@ -220,33 +223,33 @@ // the same, but using static method | ||
assert(CloudEvent.isValidEvent(ceFullTextData)) | ||
assert(CloudEvent.isValidEvent(ceFullTextData, { strict: true })) | ||
assert(CloudEvent.isValidEvent(ceFullTextData, valOptionsStrict)) | ||
assert(CloudEvent.isValidEvent(ceFullStrictBinaryData)) | ||
// console.log(`DEBUG - ${CloudEvent.dumpValidationResults(ceEmpty, null, 'ceEmpty')}`) | ||
// console.log(`DEBUG | ${CloudEvent.dumpValidationResults(ceEmpty, null, 'ceEmpty')}`) | ||
assert(CloudEvent.validateEvent(ceEmpty).length === 3) | ||
// console.log(`DEBUG - ${CloudEvent.dumpValidationResults(ceEmpty, { strict: true }, 'ceEmpty')}`) | ||
assert(CloudEvent.validateEvent(ceEmpty, { strict: true }).length === 4) | ||
// console.log(`DEBUG | ${CloudEvent.dumpValidationResults(ceEmpty, valOptionsStrict, 'ceEmpty')}`) | ||
assert(CloudEvent.validateEvent(ceEmpty, valOptionsStrict).length === 4) | ||
assert(CloudEvent.validateEvent(ceMinimalMandatoryUndefinedNoStrict).length > 0) | ||
assert(CloudEvent.validateEvent(ceMinimal).length === 0) | ||
assert(CloudEvent.validateEvent(ceFull).length === 0) | ||
assert(CloudEvent.validateEvent(ceFull, { strict: false }).length === 0) | ||
assert(CloudEvent.validateEvent(ceFull, { strict: true }).length === 0) | ||
assert(CloudEvent.validateEvent(ceFull, valOptionsNoStrict).length === 0) | ||
assert(CloudEvent.validateEvent(ceFull, valOptionsStrict).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrict).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrict, { strict: false }).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrict, { strict: true }).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrict, valOptionsNoStrict).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrict, valOptionsStrict).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictJSONTextData).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictJSONTextData, { strict: false }).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictJSONTextData, { strict: true }).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictJSONTextData, valOptionsNoStrict).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictJSONTextData, valOptionsStrict).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictOtherContentType).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictOtherContentType, { strict: false }).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictOtherContentType, { strict: true }).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictOtherContentType, valOptionsNoStrict).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictOtherContentType, valOptionsStrict).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullTextData).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullTextData, { strict: false }).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullTextData, { strict: true }).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullTextData, valOptionsNoStrict).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullTextData, valOptionsStrict).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictBinaryData).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictBinaryData, { strict: false }).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictBinaryData, { strict: true }).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictBinaryData, valOptionsNoStrict).length === 0) | ||
assert(CloudEvent.validateEvent(ceFullStrictBinaryData, valOptionsStrict).length === 0) | ||
// some diagnostic info | ||
console.log('\nSome expected validation errors:') | ||
console.log(`Validation output for ceEmpty (default strict mode) is: size: ${CloudEvent.validateEvent(ceEmpty).length}, details:\n` + CloudEvent.validateEvent(ceEmpty)) | ||
console.log(`Validation output for ceEmpty (force strict mode to true) is: size: ${CloudEvent.validateEvent(ceEmpty, { strict: true }).length}, details:\n` + CloudEvent.validateEvent(ceEmpty, { strict: true })) | ||
console.log(`Validation output for ceEmpty, alternative way: ${CloudEvent.dumpValidationResults(ceEmpty, { strict: true }, 'ceEmpty')}`) | ||
console.log(`Validation output for ceEmpty (force strict mode to true) is: size: ${CloudEvent.validateEvent(ceEmpty, valOptionsStrict).length}, details:\n` + CloudEvent.validateEvent(ceEmpty, valOptionsStrict)) | ||
console.log(`Validation output for ceEmpty, alternative way: ${CloudEvent.dumpValidationResults(ceEmpty, valOptionsStrict, 'ceEmpty')}`) | ||
@@ -265,3 +268,3 @@ // serialization examples | ||
console.log('Serialization output for ceFullStrict, details:\n' + ceFullStrictSerialized) | ||
const ceFullStrictSerializedOnlyValid = CloudEvent.serializeEvent(ceFullStrict, { onlyValid: true }) | ||
const ceFullStrictSerializedOnlyValid = CloudEvent.serializeEvent(ceFullStrict, valOnlyValidInstance) | ||
assert(ceFullStrictSerializedOnlyValid !== null) | ||
@@ -272,3 +275,3 @@ // non default contenttype | ||
encodedData: '<data "hello"="world" "year"="2020" />', | ||
onlyValid: true | ||
...valOnlyValidInstance | ||
}) | ||
@@ -279,3 +282,3 @@ assert(ceFullStrictOtherContentTypeSerializedStatic !== null) | ||
encodedData: '<data "hello"="world" "year"="2020" />', | ||
onlyValid: true | ||
...valOnlyValidInstance | ||
}) | ||
@@ -285,6 +288,6 @@ assert(ceFullStrictOtherContentTypeSerialized !== null) | ||
console.log('Serialization output for ceFullStrictOtherContentType, details:\n' + ceFullStrictOtherContentTypeSerialized) | ||
const ceFullTextDataSerialized = CloudEvent.serializeEvent(ceFullTextData, { onlyValid: true }) | ||
const ceFullTextDataSerialized = CloudEvent.serializeEvent(ceFullTextData, valOnlyValidInstance) | ||
assert(ceFullTextDataSerialized !== null) | ||
console.log('Serialization output for ceFullTextData, details:\n' + ceFullTextDataSerialized) | ||
const ceFullStrictBinaryDataSerialized = CloudEvent.serializeEvent(ceFullStrictBinaryData, { onlyValid: true }) | ||
const ceFullStrictBinaryDataSerialized = CloudEvent.serializeEvent(ceFullStrictBinaryData, valOnlyValidInstance) | ||
assert(ceFullStrictBinaryDataSerialized !== null) | ||
@@ -304,3 +307,3 @@ console.log('Serialization output for ceFullStrictBinaryData, details:\n' + ceFullStrictBinaryDataSerialized) | ||
console.log(`ce dump: ${T.dumpObject(ceFullDeserialized, 'ceFullDeserialized')}`) | ||
const ceFullStrictDeserializedOnlyValid = CloudEvent.deserializeEvent(ceFullStrictSerialized, { onlyValid: true }) | ||
const ceFullStrictDeserializedOnlyValid = CloudEvent.deserializeEvent(ceFullStrictSerialized, valOnlyValidInstance) | ||
assert(ceFullStrictDeserializedOnlyValid !== null) | ||
@@ -312,3 +315,3 @@ console.log(`ce dump: ${T.dumpObject(ceFullStrictDeserializedOnlyValid, 'ceFullStrictDeserializedOnlyValid')}`) | ||
decodedData: { hello: 'world', year: 2020 }, | ||
onlyValid: true | ||
...valOnlyValidInstance | ||
}) | ||
@@ -320,3 +323,3 @@ assert(ceFullStrictOtherContentTypeDeserialized !== null) | ||
console.log(`ce dump: ${T.dumpObject(ceFullStrictOtherContentTypeDeserialized, 'ceFullStrictOtherContentTypeDeserialized')}`) | ||
const ceFullTextDataDeserialized = CloudEvent.deserializeEvent(ceFullTextDataSerialized, { onlyValid: true }) | ||
const ceFullTextDataDeserialized = CloudEvent.deserializeEvent(ceFullTextDataSerialized, valOnlyValidInstance) | ||
assert(ceFullTextDataDeserialized !== null) | ||
@@ -327,3 +330,3 @@ assert(ceFullTextDataDeserialized.isValid()) | ||
console.log(`ce dump: ${T.dumpObject(ceFullTextDataDeserialized, 'ceFullTextDataDeserialized')}`) | ||
const ceFullStrictBinaryDataDeserialized = CloudEvent.deserializeEvent(ceFullStrictBinaryDataSerialized, { onlyValid: true }) | ||
const ceFullStrictBinaryDataDeserialized = CloudEvent.deserializeEvent(ceFullStrictBinaryDataSerialized, valOnlyValidInstance) | ||
assert(ceFullStrictBinaryDataDeserialized !== null) | ||
@@ -365,20 +368,20 @@ assert(ceFullStrictBinaryDataDeserialized.isValid()) | ||
console.log(`JSONBatch contains ${batch.length} items, but only some are valid CloudEvent instances, see related sample code:`) | ||
console.log(`CloudEvent instances valid: ${JSONBatch.getEvents(batch, { onlyValid: true, strict: false }).length}`) | ||
console.log(`CloudEvent instances valid in strict mode: ${JSONBatch.getEvents(batch, { onlyValid: true, strict: true }).length}`) | ||
console.log(`CloudEvent instances valid: ${JSONBatch.getEvents(batch, { ...valOnlyValidInstance, strict: false }).length}`) | ||
console.log(`CloudEvent instances valid in strict mode: ${JSONBatch.getEvents(batch, { ...valOnlyValidInstance, strict: true }).length}`) | ||
// sample validation, in normal and in strict mode | ||
console.log(`JSONBatch validation errors, num: ${JSONBatch.validateBatch(batch, { strict: false }).length}`) | ||
console.log(`JSONBatch validation errors in strict mode, num: ${JSONBatch.validateBatch(batch, { strict: true }).length}`) | ||
console.log(`JSONBatch validation errors in strict mode, details:\n${JSONBatch.validateBatch(batch, { strict: true })}\n`) | ||
assert(JSONBatch.validateBatch(batch, { strict: false }).length === 8) // expected validation errors | ||
assert(JSONBatch.validateBatch(batch, { strict: true }).length === 13) // expected validation errors | ||
console.log(`JSONBatch validation errors, num: ${JSONBatch.validateBatch(batch, valOptionsNoStrict).length}`) | ||
console.log(`JSONBatch validation errors in strict mode, num: ${JSONBatch.validateBatch(batch, valOptionsStrict).length}`) | ||
console.log(`JSONBatch validation errors in strict mode, details:\n${JSONBatch.validateBatch(batch, valOptionsStrict)}\n`) | ||
assert(JSONBatch.validateBatch(batch, valOptionsNoStrict).length === 8) // expected validation errors | ||
assert(JSONBatch.validateBatch(batch, valOptionsStrict).length === 13) // expected validation errors | ||
// sample filtering of events | ||
// console.log(`DEBUG - JSONBatch.getEvents, num: ${JSONBatch.getEvents(batch, { onlyValid: true, strict: true }).length}`) | ||
assert(JSONBatch.getEvents(batch, { onlyValid: false, strict: false }).length === 8) // no filtering | ||
assert(JSONBatch.getEvents(batch, { onlyValid: false, strict: true }).length === 8) // no filtering (neither in strict mode) | ||
assert(JSONBatch.getEvents(batch, { onlyValid: true, strict: false }).length === 8) // only valid | ||
assert(JSONBatch.getEvents(batch, { onlyValid: true, strict: true }).length === 7) // only valid in strict mode | ||
// console.log(`DEBUG | JSONBatch.getEvents, num: ${JSONBatch.getEvents(batch, { ...valOnlyValidInstance, strict: true }).length}`) | ||
assert(JSONBatch.getEvents(batch, { ...valOnlyValidAllInstance, strict: false }).length === 8) // no filtering | ||
assert(JSONBatch.getEvents(batch, { ...valOnlyValidAllInstance, strict: true }).length === 8) // no filtering (neither in strict mode) | ||
assert(JSONBatch.getEvents(batch, { ...valOnlyValidInstance, strict: false }).length === 8) // only valid | ||
assert(JSONBatch.getEvents(batch, { ...valOnlyValidInstance, strict: true }).length === 7) // only valid in strict mode | ||
console.log('JSONBatch events: get only valid instances, as a sample') | ||
const events = JSONBatch.getEvents(batch, { | ||
onlyValid: true, | ||
strict: false | ||
...valOnlyValidInstance, | ||
...valOptionsNoStrict | ||
}) | ||
@@ -395,3 +398,3 @@ console.log(`JSONBatch events: length = ${events.length}, summary: ${events}`) | ||
throwError: true, | ||
onlyValid: true // sample, to filter out not valid serialized instances ... | ||
...valOnlyValidInstance // sample, to filter out not valid serialized instances ... | ||
// onlyIfLessThan64KB: true | ||
@@ -406,3 +409,4 @@ }) | ||
console.log('\nSample script: end execution.') | ||
console.log('----') | ||
assert(true) // all good here | ||
// end of script |
{ | ||
"name": "cloudevent", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"description": "JavaScript/Node.js implementation of the CloudEvents standard format", | ||
@@ -14,2 +14,3 @@ "main": "src/index", | ||
"docs": "npm run docs:clean && npm run docs:generate", | ||
"example:benchmark": "node example/nodejs-bench", | ||
"example:debug": "node --inspect-brk example/nodejs-base", | ||
@@ -26,6 +27,7 @@ "example": "node example/nodejs-base", | ||
"test:clean": "rm -rf .nyc_output/* ./coverage/*", | ||
"test:coverage:all": "npm run test:unit -- --cov", | ||
"test:coverage": "npm run test:unit -- --cov --coverage-report=html", | ||
"test:unit:debug": "tap -T --node-arg=--inspect-brk test/*.test.js", | ||
"test:unit:dev": "tap -J --watch test/*.test.js", | ||
"test:unit": "tap -J test/*.test.js", | ||
"test:unit:dev": "tap --watch --cov test/*.test.js", | ||
"test:unit": "tap test/*.test.js", | ||
"test": "npm run lint && npm run test:unit" | ||
@@ -35,9 +37,9 @@ }, | ||
"devDependencies": { | ||
"jsdoc": "^3.6.10", | ||
"standard": "^16.0.4", | ||
"tap": "^15.2.3" | ||
"jsdoc": "^3.6.11", | ||
"standard": "^17.0.0", | ||
"tap": "^16.3.0" | ||
}, | ||
"peerDependencies": {}, | ||
"engines": { | ||
"node": ">=10.13.0" | ||
"node": ">=14.15.0" | ||
}, | ||
@@ -44,0 +46,0 @@ "homepage": "https://github.com/smartiniOnGitHub/cloudevent.js#readme", |
@@ -244,4 +244,3 @@ # cloudevent / cloudevent.js | ||
Node.js 10 LTS (but recommended 10.24.1) or later | ||
(recommended an active LTS version). | ||
Node.js 14 LTS (14.15.0) or later (anyway, recommended an active LTS version). | ||
@@ -248,0 +247,0 @@ |
@@ -24,3 +24,3 @@ /* | ||
/** | ||
* Get a reference to cloudevent Validator class. | ||
* Reference to cloudevent Validator class. | ||
* @private | ||
@@ -32,3 +32,3 @@ * @see Validator | ||
/** | ||
* Get a reference to cloudevent Transformer class. | ||
* Reference to cloudevent Transformer class. | ||
* @private | ||
@@ -333,3 +333,3 @@ * @see Transformer | ||
/** | ||
* Get all extensions from the given object. | ||
* Get all extensions (non standard) properties from the given object. | ||
* Should not be used outside CloudEvent. | ||
@@ -370,3 +370,3 @@ * | ||
* @param {object} [options={}] containing: | ||
* - strict (boolean, default false) to validate it in a more strict way, | ||
* - strict (boolean, default null so no override) to validate it in a more strict way (if null it will be used strict mode in the given event), | ||
* - dataschemavalidator (function(data, dataschema) boolean, optional) a function to validate data of current CloudEvent instance with its dataschema | ||
@@ -377,3 +377,3 @@ * - timezoneOffset (number, default 0) to apply a different timezone offset | ||
static validateEvent (event, { | ||
strict = false, | ||
strict = null, | ||
dataschemavalidator = null, | ||
@@ -411,3 +411,3 @@ timezoneOffset = 0 | ||
// additional validation if strict mode enabled, or if enabled in the event ... | ||
if (strict === true || CloudEvent.isStrictEvent(event) === true) { | ||
if (strict === true || (strict === null && CloudEvent.isStrictEvent(event) === true)) { | ||
ve.push(V.ensureIsVersion(event.specversion, 'specversion')) | ||
@@ -472,4 +472,5 @@ if (V.isDefinedAndNotNull(event.data)) { | ||
* @param {object} [options={}] containing: | ||
* - strict (boolean, default false) to validate it in a more strict way, | ||
* - strict (boolean, default null so no override) to validate it in a more strict way (if null it will be used strict mode in the given event), | ||
* - dataschemavalidator (function(data, dataschema) boolean, optional) a function to validate data of current CloudEvent instance with its dataschema | ||
* - printDebugInfo (boolean, default false) to print some debug info to the console, | ||
* - timezoneOffset (number, default 0) to apply a different timezone offset | ||
@@ -479,4 +480,5 @@ * @return {boolean} true if valid, otherwise false | ||
static isValidEvent (event, { | ||
strict = false, | ||
strict = null, | ||
dataschemavalidator = null, | ||
printDebugInfo = false, | ||
timezoneOffset = 0 | ||
@@ -486,2 +488,5 @@ } = {}) { | ||
const size = V.getSize(validationErrors) | ||
if (printDebugInfo === true) { // print some debug info | ||
console.log(`DEBUG | validation errors found: ${size}, details: ${JSON.stringify(validationErrors)}`) | ||
} | ||
return (size === 0) | ||
@@ -517,2 +522,3 @@ } | ||
* - onlyIfLessThan64KB (boolean, default false) to return the serialized string only if it's less than 64 KB, | ||
* - printDebugInfo (boolean, default false) to print some debug info to the console, | ||
* - timezoneOffset (number, default 0) to apply a different timezone offset | ||
@@ -525,5 +531,9 @@ * @return {string} the serialized event, as a string | ||
onlyValid = false, onlyIfLessThan64KB = false, | ||
printDebugInfo = false, | ||
timezoneOffset = 0 | ||
} = {}) { | ||
if (V.isUndefinedOrNull(event)) throw new Error('CloudEvent undefined or null') | ||
if (printDebugInfo === true) { | ||
console.log(`DEBUG | trying to serialize ce: ${JSON.stringify(event)}`) | ||
} | ||
if (event.datacontenttype === CloudEvent.datacontenttypeDefault()) { | ||
@@ -540,2 +550,5 @@ if ((onlyValid === false) || (onlyValid === true && CloudEvent.isValidEvent(event, { timezoneOffset }) === true)) { | ||
}) | ||
if (printDebugInfo === true) { | ||
console.log(`DEBUG | ce successfully serialized as: ${ser}`) | ||
} | ||
if ((onlyIfLessThan64KB === false) || (onlyIfLessThan64KB === true && V.getSizeInBytes(ser) < 65536)) return ser | ||
@@ -561,2 +574,5 @@ else throw new Error('Unable to return a serialized CloudEvent bigger than 64 KB.') | ||
const ser = JSON.stringify(newEvent) | ||
if (printDebugInfo === true) { | ||
console.log(`DEBUG | ce successfully serialized as: ${ser}`) | ||
} | ||
if ((onlyIfLessThan64KB === false) || (onlyIfLessThan64KB === true && V.getSizeInBytes(ser) < 65536)) return ser | ||
@@ -579,2 +595,3 @@ else throw new Error('Unable to return a serialized CloudEvent bigger than 64 KB.') | ||
* - onlyIfLessThan64KB (boolean, default false) to return the deserialized string only if it's less than 64 KB, | ||
* - printDebugInfo (boolean, default false) to print some debug info to the console, | ||
* - timezoneOffset (number, default 0) to apply a different timezone offset | ||
@@ -588,2 +605,3 @@ * @return {object} the deserialized event as a CloudEvent instance | ||
onlyValid = false, onlyIfLessThan64KB = false, | ||
printDebugInfo = false, | ||
timezoneOffset = 0 | ||
@@ -593,2 +611,5 @@ } = {}) { | ||
if (!V.isStringNotEmpty(ser)) throw new Error(`Missing or wrong serialized data: '${ser}' must be a string and not a: '${typeof ser}'.`) | ||
if (printDebugInfo === true) { | ||
console.log(`DEBUG | trying to deserialize as ce: ${ser}`) | ||
} | ||
// deserialize standard attributes, always in JSON format | ||
@@ -614,3 +635,3 @@ const parsed = JSON.parse(ser) | ||
subject: parsed.subject, | ||
strict: strict | ||
strict | ||
}, | ||
@@ -622,2 +643,5 @@ extensions | ||
// return ce, depending on its validation option | ||
if (printDebugInfo === true) { | ||
console.log(`DEBUG | ce successfully deserialized as: ${JSON.stringify(ce)}`) | ||
} | ||
if ((onlyValid === false) || (onlyValid === true && CloudEvent.isValidEvent(ce, { timezoneOffset }) === true)) { | ||
@@ -645,2 +669,5 @@ if ((onlyIfLessThan64KB === false) || (onlyIfLessThan64KB === true && V.getSizeInBytes(ser) < 65536)) return ce | ||
if ((onlyValid === false) || (onlyValid === true && CloudEvent.isValidEvent(ce, { timezoneOffset }) === true)) { | ||
if (printDebugInfo === true) { | ||
console.log(`DEBUG | ce successfully deserialized as: ${JSON.stringify(ce)}`) | ||
} | ||
if ((onlyIfLessThan64KB === false) || (onlyIfLessThan64KB === true && V.getSizeInBytes(ser) < 65536)) return ce | ||
@@ -790,5 +817,5 @@ else throw new Error('Unable to return a deserialized CloudEvent bigger than 64 KB.') | ||
} else if (CloudEvent.isCloudEvent(ce)) { | ||
const opts = (options == null) ? {} : options | ||
const opts = options ?? {} | ||
const ve = CloudEvent.validateEvent(ce, opts) | ||
return `${name}: ${JSON.stringify(ve.map((i) => i.message))}` | ||
return `${name}, validation with options (${JSON.stringify(options)}): ${JSON.stringify(ve.map((i) => i.message))}` | ||
} else { | ||
@@ -800,2 +827,17 @@ return `${name}: 'is not a CloudEvent, no validation possible'` | ||
/** | ||
* Getter method to return the list of standard property names, as an array of strings. | ||
* | ||
* @type {Array} | ||
* @static | ||
*/ | ||
static get standardProps () { | ||
return [ | ||
'specversion', | ||
'id', 'type', 'source', 'data', | ||
'time', 'data_base64', 'datacontenttype', | ||
'dataschema', 'subject' | ||
] | ||
} | ||
/** | ||
* Serialize the current CloudEvent. | ||
@@ -820,7 +862,7 @@ * | ||
* @param {object} [options={}] containing: | ||
* - strict (boolean, default false) to validate it in a more strict way, | ||
* - strict (boolean, default null so no override) to validate it in a more strict way (if null it will be used strict mode in the given event), | ||
* - dataschemavalidator (function(data, dataschema) boolean, optional) a function to validate data of current CloudEvent instance with its dataschema | ||
* @return {object[]} an array of (non null) validation errors, or at least an empty array | ||
*/ | ||
validate ({ strict = false, dataschemavalidator = null } = {}) { | ||
validate ({ strict = null, dataschemavalidator = null } = {}) { | ||
return this.constructor.validateEvent(this, { strict, dataschemavalidator }) | ||
@@ -835,9 +877,10 @@ } | ||
* @param {object} [options={}] containing: | ||
* - strict (boolean, default false) to validate it in a more strict way, | ||
* - strict (boolean, default null so no override) to validate it in a more strict way (if null it will be used strict mode in the given event), | ||
* - dataschemavalidator (function(data, dataschema) boolean, optional) a function to validate data of current CloudEvent instance with its dataschema | ||
* - printDebugInfo (boolean, default false) to print some debug info to the console, | ||
* - timezoneOffset (number, default 0) to apply a different timezone offset | ||
* @return {boolean} true if valid, otherwise false | ||
*/ | ||
isValid ({ strict = false, dataschemavalidator = null, timezoneOffset = 0 } = {}) { | ||
return this.constructor.isValidEvent(this, { strict, dataschemavalidator, timezoneOffset }) | ||
isValid ({ strict = null, dataschemavalidator = null, printDebugInfo = false, timezoneOffset = 0 } = {}) { | ||
return this.constructor.isValidEvent(this, { strict, dataschemavalidator, printDebugInfo, timezoneOffset }) | ||
} | ||
@@ -990,16 +1033,2 @@ | ||
/** | ||
* Utility variable that returns all standard property names, in an array. | ||
* | ||
* @readonly | ||
* @type {string[]} | ||
* @static | ||
*/ | ||
CloudEvent.standardProps = [ | ||
'specversion', | ||
'id', 'type', 'source', 'data', | ||
'time', 'data_base64', 'datacontenttype', | ||
'dataschema', 'subject' | ||
] | ||
module.exports = CloudEvent |
@@ -52,2 +52,9 @@ /* | ||
/** | ||
* Get a reference to cloudevent utility functions. | ||
* See {@link CloudEventUtilities}. | ||
* @private | ||
*/ | ||
const cloudEventUtilities = require('./utility') | ||
module.exports = { | ||
@@ -64,3 +71,6 @@ /** CloudEvent class definition. */ | ||
/** CloudEvent class Transformer. */ | ||
CloudEventTransformer: cloudEventTransformer | ||
CloudEventTransformer: cloudEventTransformer, | ||
/** CloudEvent utility functions. */ | ||
CloudEventUtilities: cloudEventUtilities | ||
} |
@@ -34,3 +34,3 @@ /* | ||
*/ | ||
const hostname = require('os').hostname() | ||
const hostname = require('node:os').hostname() | ||
@@ -41,3 +41,3 @@ /** | ||
*/ | ||
const pid = require('process').pid | ||
const pid = require('node:process').pid | ||
@@ -257,4 +257,4 @@ /** | ||
return { | ||
hostname: hostname, | ||
pid: pid | ||
hostname, | ||
pid | ||
} | ||
@@ -261,0 +261,0 @@ } |
@@ -27,3 +27,3 @@ /* | ||
*/ | ||
const url = require('url') | ||
const url = require('node:url') | ||
@@ -100,3 +100,5 @@ /** | ||
static isString (arg) { | ||
return (Validator.isDefinedAndNotNull(arg) && (typeof arg === 'string')) | ||
// return (Validator.isDefinedAndNotNull(arg) && (typeof arg === 'string')) | ||
// rewritten inline and using new ES features for more performances | ||
return ((arg?.length >= 0) && (typeof arg === 'string')) | ||
} | ||
@@ -114,3 +116,5 @@ | ||
static isStringNotEmpty (arg) { | ||
return (Validator.isString(arg) && (arg.length > 0)) | ||
// return (Validator.isString(arg) && (arg.length > 0)) | ||
// rewritten inline and using new ES features for more performances | ||
return ((arg?.length > 0) && (typeof arg === 'string')) | ||
} | ||
@@ -246,3 +250,7 @@ | ||
static isFunction (arg) { | ||
return (Validator.isDefinedAndNotNull(arg) && (typeof arg === 'function')) | ||
// return (Validator.isDefinedAndNotNull(arg) && (typeof arg === 'function')) | ||
// rewritten (inline same code here) for more performances | ||
// return ((arg !== undefined && arg !== null) && (typeof arg === 'function')) | ||
// rewritten using new ES features, but same behavior | ||
return ((arg ?? false) && (typeof arg === 'function')) | ||
} | ||
@@ -462,2 +470,24 @@ | ||
/** | ||
* Tell if the given object contains the given property. | ||
* | ||
* @static | ||
* @param {?object} obj the object to check | ||
* @param {?string} propName the name of the property | ||
* @param {boolean} [includeInherited=false] specify if search in nested/inherited properties | ||
* @return {boolean} true if the object contains theat property, otherwise false | ||
*/ | ||
static doesObjectContainsProperty (obj, propName, includeInherited = false) { | ||
if (!Validator.isObject(obj)) return false | ||
if (!Validator.isStringNotEmpty(propName)) return false | ||
if (!Validator.isBoolean(includeInherited)) return false | ||
let propFound = false | ||
if (includeInherited === false) { | ||
propFound = Object.prototype.hasOwnProperty.call(obj, propName) | ||
} else { | ||
propFound = propName in obj | ||
} | ||
return propFound | ||
} | ||
/** | ||
* Tell if the given object contains at least one property | ||
@@ -958,2 +988,20 @@ * that has a standard (reserved) property name. | ||
/** | ||
* Ensure that the given object contains the given property. | ||
* | ||
* See {@link Validator.doesObjectContainsProperty}. | ||
* | ||
* @static | ||
* @param {?object} obj the object to check | ||
* @param {?string} propName the name of the property | ||
* @param {boolean} [includeInherited=false] specify if search in nested/inherited properties | ||
* @param {string} [name='arg'] the name to use in generated error (for the first argument) | ||
* @return {boolean} true if the object contains theat property, otherwise false | ||
*/ | ||
static ensureObjectContainsProperty (obj, propName, includeInherited = false, name = 'arg') { | ||
if (!Validator.doesObjectContainsProperty(obj, propName, includeInherited)) { | ||
return new Error(`The object with name '${name}' does not contains the property '${propName}' with search in inherited: ${includeInherited}`) | ||
} | ||
} | ||
/** | ||
* Ensure that the given object does not contain any property | ||
@@ -967,6 +1015,6 @@ * that has a standard (reserved) property name. | ||
* @param {?function} isPropStandard the function that tell the given argument (property), if it's standard | ||
* @param {string} [name='arg'] the name to use in generated error (or the value of first argument if not given) | ||
* @param {string} [name='arg'] the name to use in generated error (for the first argument) | ||
* @return {Error} if at least one property with a standard name is found, nothing otherwise | ||
*/ | ||
static ensureObjectDoesNotContainStandardProperty (arg, isPropStandard, name = 'arg') { | ||
static ensureObjectDoesNotContainsStandardProperty (arg, isPropStandard, name = 'arg') { | ||
if (Validator.doesObjectContainsStandardProperty(arg, isPropStandard)) { | ||
@@ -973,0 +1021,0 @@ return new Error(`The object with name '${name}' contains at least one property with a standard name`) |
203274
14
3888
301