diff --git a/CHANGELOG.md b/CHANGELOG.md index 270ed459..32b55868 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,26 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [4.0.1](https://www.github.com/cloudevents/sdk-javascript/compare/v4.0.0...v4.0.1) (2021-03-08) + + +### Bug Fixes + +* cloudevents from 3.2.0 to 4.0.0 ([#376](https://www.github.com/cloudevents/sdk-javascript/issues/376)) ([6be3b27](https://www.github.com/cloudevents/sdk-javascript/commit/6be3b2751401e529fce6ccf8a406a057472c10a9)) +* Emitter should not extend EventEmitter ([1af3d43](https://www.github.com/cloudevents/sdk-javascript/commit/1af3d4334170432767d672af2fca3b748fe297a1)) +* examples/websocket/package.json to reduce vulnerabilities ([#375](https://www.github.com/cloudevents/sdk-javascript/issues/375)) ([2b1e1ec](https://www.github.com/cloudevents/sdk-javascript/commit/2b1e1ec5a2aa670625e992fd060ae7d7fb40f258)) +* package.json & package-lock.json to reduce vulnerabilities ([#384](https://www.github.com/cloudevents/sdk-javascript/issues/384)) ([39812f7](https://www.github.com/cloudevents/sdk-javascript/commit/39812f77d086d75dcf562618a3cde46c3ca57d6d)) + + +### Miscellaneous + +* **docs:** Fix minor import problems in README ([#374](https://www.github.com/cloudevents/sdk-javascript/issues/374)) ([ed81483](https://www.github.com/cloudevents/sdk-javascript/commit/ed8148326b2c1890845cf97c469c645ec76e3f51)) + + +### Tests + +* add a test for extension names with all caps. ([#389](https://www.github.com/cloudevents/sdk-javascript/issues/389)) ([e7d99eb](https://www.github.com/cloudevents/sdk-javascript/commit/e7d99eb8822d8b590bdd7018d8491a803d83ab1e)) + ## [4.0.0](https://github.com/cloudevents/sdk-javascript/compare/v3.1.0...v4.0.0) (2020-12-10) diff --git a/README.md b/README.md index f274a409..ee18f986 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ for `headers` and `body`. ```js const axios = require("axios").default; -const { HTTP } = require("cloudevents"); +const { HTTP, CloudEvent } = require("cloudevents"); const ce = new CloudEvent({ type, source, data }); const message = HTTP.binary(ce); // Or HTTP.structured(ce) @@ -69,7 +69,7 @@ You may also use the `emitterFor()` function as a convenience. ```js const axios = require("axios").default; -const { emitterFor, Mode } = require("cloudevents"); +const { emitterFor, Mode, CloudEvent } = require("cloudevents"); function sendWithAxios(message) { // Do what you need with the message headers diff --git a/examples/express-ex/index.js b/examples/express-ex/index.js index cc8e2349..10ef8605 100644 --- a/examples/express-ex/index.js +++ b/examples/express-ex/index.js @@ -1,17 +1,29 @@ /* eslint-disable */ const express = require("express"); -const { Receiver } = require("cloudevents"); +const { CloudEvent, HTTP } = require("cloudevents"); const app = express(); -const bodyParser = require('body-parser') -app.use(bodyParser.json()) + +app.use((req, res, next) => { + let data = ""; + + req.setEncoding("utf8"); + req.on("data", function (chunk) { + data += chunk; + }); + + req.on("end", function () { + req.body = data; + next(); + }); +}); app.post("/", (req, res) => { console.log("HEADERS", req.headers); console.log("BODY", req.body); try { - const event = Receiver.accept(req.headers, req.body); + const event = HTTP.toEvent({ headers: req.headers, body: req.body }); // respond as an event const responseEventMessage = new CloudEvent({ source: '/', diff --git a/examples/express-ex/package.json b/examples/express-ex/package.json index 74fa5232..963560aa 100644 --- a/examples/express-ex/package.json +++ b/examples/express-ex/package.json @@ -14,8 +14,7 @@ "author": "fabiojose@gmail.com", "license": "Apache-2.0", "dependencies": { - "body-parser": "^1.19.0", - "cloudevents": "^3.1.0", + "cloudevents": "^4.0.0", "express": "^4.17.1" } } diff --git a/examples/typescript-ex/package.json b/examples/typescript-ex/package.json index 7a2c9c48..38a14f45 100644 --- a/examples/typescript-ex/package.json +++ b/examples/typescript-ex/package.json @@ -23,11 +23,11 @@ "posttest": "npm run check" }, "devDependencies": { - "@types/node": "^8.9.0", + "@types/node": "^14.14.10", "gts": "^1.1.0", - "typescript": "~3.9.5" + "typescript": "~4.1.3" }, "dependencies": { - "cloudevents": "~3.1.0" + "cloudevents": "~4.0.0" } } diff --git a/examples/typescript-ex/src/index.ts b/examples/typescript-ex/src/index.ts index af084a6b..a6daa694 100644 --- a/examples/typescript-ex/src/index.ts +++ b/examples/typescript-ex/src/index.ts @@ -1,14 +1,15 @@ -import { CloudEvent, CloudEventV1, Receiver } from "cloudevents"; +/* eslint-disable no-console */ +import { CloudEvent, HTTP } from "cloudevents"; export function doSomeStuff(): void { - const myevent: CloudEventV1 = new CloudEvent({ + const myevent: CloudEvent = new CloudEvent({ source: "/source", type: "type", datacontenttype: "text/plain", dataschema: "https://d.schema.com/my.json", subject: "cha.json", data: "my-data", - extension1: "some extension data" + extension1: "some extension data", }); console.log("My structured event:", myevent); @@ -21,7 +22,7 @@ export function doSomeStuff(): void { // Typically used with an incoming HTTP request where myevent.format() is the actual // body of the HTTP - console.log("Received structured event:", Receiver.accept(headers, myevent)); + console.log("Received structured event:", HTTP.toEvent({ headers, body: myevent })); // ------ receiver binary const data = { @@ -38,9 +39,8 @@ export function doSomeStuff(): void { "ce-extension1": "extension1", }; - console.log("My binary event:", Receiver.accept(attributes, data)); - console.log("My binary event extensions:", Receiver.accept(attributes, data)); - + console.log("My binary event:", HTTP.toEvent({ headers: attributes, body: data })); + console.log("My binary event extensions:", HTTP.toEvent({ headers: attributes, body: data })); } doSomeStuff(); diff --git a/examples/websocket/package.json b/examples/websocket/package.json index 3837ca6b..0514e1bc 100644 --- a/examples/websocket/package.json +++ b/examples/websocket/package.json @@ -15,7 +15,7 @@ "author": "", "license": "ISC", "dependencies": { - "cloudevents": "~3.0.1", + "cloudevents": "~4.0.0", "got": "^11.3.0", "ws": "^7.3.0" } diff --git a/package-lock.json b/package-lock.json index 6b759bdb..c95d8fbd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "cloudevents", - "version": "4.0.0", + "version": "4.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -775,9 +775,9 @@ "dev": true }, "@types/node": { - "version": "13.13.30", - "resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.30.tgz", - "integrity": "sha512-HmqFpNzp3TSELxU/bUuRK+xzarVOAsR00hzcvM0TXrMlt/+wcSLa5q6YhTb6/cA6wqDCZLDcfd8fSL95x5h7AA==", + "version": "14.14.31", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.31.tgz", + "integrity": "sha512-vFHy/ezP5qI0rFgJ7aQnjDXwAMrG0KqqIH7tQG5PPv3BWBayOPIQNBjVc/P6hhdZfMx51REc6tfDNXHUio893g==", "dev": true }, "@types/normalize-package-data": { @@ -5633,12 +5633,6 @@ "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==", "dev": true }, - "highlight.js": { - "version": "10.4.1", - "resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-10.4.1.tgz", - "integrity": "sha512-yR5lWvNz7c85OhVAEAeFhVCc/GV4C30Fjzc/rCP0aCWzc1UUOPUk55dK/qdwTZHBvMZo+eZ2jpk62ndX/xMFlg==", - "dev": true - }, "homedir-polyfill": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/homedir-polyfill/-/homedir-polyfill-1.0.3.tgz", @@ -5829,9 +5823,9 @@ "dev": true }, "ini": { - "version": "1.3.5", - "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.5.tgz", - "integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==", + "version": "1.3.7", + "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.7.tgz", + "integrity": "sha512-iKpRpXP+CrP2jyrxvg1kMUpXDyRUFDWurxbnVT1vQPx+Wz9uCYsMIqYuSBLV+PAaZG/d7kRLKRFc9oDMsH+mFQ==", "dev": true }, "inquirer": { @@ -6933,9 +6927,9 @@ } }, "marked": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/marked/-/marked-1.2.3.tgz", - "integrity": "sha512-RQuL2i6I6Gn+9n81IDNGbL0VHnta4a+8ZhqvryXEniTb/hQNtf3i26hi1XWUhzb9BgVyWHKR3UO8MaHtKoYibw==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/marked/-/marked-2.0.0.tgz", + "integrity": "sha512-NqRSh2+LlN2NInpqTQnS614Y/3NkVMFFU6sJlRFEpxJ/LHuK/qJECH7/fXZjk4VZstPW/Pevjil/VtSONsLc7Q==", "dev": true }, "mem-fs": { @@ -8034,6 +8028,32 @@ "mimic-fn": "^2.1.0" } }, + "onigasm": { + "version": "2.2.5", + "resolved": "https://registry.npmjs.org/onigasm/-/onigasm-2.2.5.tgz", + "integrity": "sha512-F+th54mPc0l1lp1ZcFMyL/jTs2Tlq4SqIHKIXGZOR/VkHkF9A7Fr5rRr5+ZG/lWeRsyrClLYRq7s/yFQ/XhWCA==", + "dev": true, + "requires": { + "lru-cache": "^5.1.1" + }, + "dependencies": { + "lru-cache": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", + "integrity": "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==", + "dev": true, + "requires": { + "yallist": "^3.0.2" + } + }, + "yallist": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz", + "integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==", + "dev": true + } + } + }, "optionator": { "version": "0.9.1", "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.1.tgz", @@ -8988,6 +9008,16 @@ "rechoir": "^0.6.2" } }, + "shiki": { + "version": "0.9.2", + "resolved": "https://registry.npmjs.org/shiki/-/shiki-0.9.2.tgz", + "integrity": "sha512-BjUCxVbxMnvjs8jC4b+BQ808vwjJ9Q8NtLqPwXShZ307HdXiDFYP968ORSVfaTNNSWYDBYdMnVKJ0fYNsoZUBA==", + "dev": true, + "requires": { + "onigasm": "^2.2.5", + "vscode-textmate": "^5.2.0" + } + }, "signal-exit": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.3.tgz", @@ -10313,33 +10343,34 @@ } }, "typedoc": { - "version": "0.18.0", - "resolved": "https://registry.npmjs.org/typedoc/-/typedoc-0.18.0.tgz", - "integrity": "sha512-UgDQwapCGQCCdYhEQzQ+kGutmcedklilgUGf62Vw6RdI29u6FcfAXFQfRTiJEbf16aK3YnkB20ctQK1JusCRbA==", + "version": "0.20.24", + "resolved": "https://registry.npmjs.org/typedoc/-/typedoc-0.20.24.tgz", + "integrity": "sha512-TadOYtcw8agrk7WTZlXUcct4jLZZcGcYe3xbmARkI+rBpXI6Mw+0P8oUo13+9oFreQvK5zZgMem4YEi7lCXLIw==", "dev": true, "requires": { - "fs-extra": "^9.0.1", + "colors": "^1.4.0", + "fs-extra": "^9.1.0", "handlebars": "^4.7.6", - "highlight.js": "^10.0.0", - "lodash": "^4.17.15", - "lunr": "^2.3.8", - "marked": "^1.1.1", + "lodash": "^4.17.20", + "lunr": "^2.3.9", + "marked": "^2.0.0", "minimatch": "^3.0.0", "progress": "^2.0.3", "shelljs": "^0.8.4", - "typedoc-default-themes": "^0.10.2" + "shiki": "^0.9.2", + "typedoc-default-themes": "^0.12.7" }, "dependencies": { "fs-extra": { - "version": "9.0.1", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.0.1.tgz", - "integrity": "sha512-h2iAoN838FqAFJY2/qVpzFXy+EBxfVE220PalAqQLDVsFOHLJrZvut5puAbCdNv6WJk+B8ihI+k0c7JK5erwqQ==", + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", + "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", "dev": true, "requires": { "at-least-node": "^1.0.0", "graceful-fs": "^4.2.0", "jsonfile": "^6.0.1", - "universalify": "^1.0.0" + "universalify": "^2.0.0" } }, "jsonfile": { @@ -10350,20 +10381,12 @@ "requires": { "graceful-fs": "^4.1.6", "universalify": "^2.0.0" - }, - "dependencies": { - "universalify": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", - "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", - "dev": true - } } }, "universalify": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/universalify/-/universalify-1.0.0.tgz", - "integrity": "sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", "dev": true } } @@ -10375,13 +10398,10 @@ "dev": true }, "typedoc-default-themes": { - "version": "0.10.2", - "resolved": "https://registry.npmjs.org/typedoc-default-themes/-/typedoc-default-themes-0.10.2.tgz", - "integrity": "sha512-zo09yRj+xwLFE3hyhJeVHWRSPuKEIAsFK5r2u47KL/HBKqpwdUSanoaz5L34IKiSATFrjG5ywmIu98hPVMfxZg==", - "dev": true, - "requires": { - "lunr": "^2.3.8" - } + "version": "0.12.7", + "resolved": "https://registry.npmjs.org/typedoc-default-themes/-/typedoc-default-themes-0.12.7.tgz", + "integrity": "sha512-0XAuGEqID+gon1+fhi4LycOEFM+5Mvm2PjwaiVZNAzU7pn3G2DEpsoXnFOPlLDnHY6ZW0BY0nO7ur9fHOFkBLQ==", + "dev": true }, "typescript": { "version": "3.9.7", @@ -10608,6 +10628,12 @@ } } }, + "vscode-textmate": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/vscode-textmate/-/vscode-textmate-5.2.0.tgz", + "integrity": "sha512-Uw5ooOQxRASHgu6C7GVvUxisKXfSgW4oFlO+aa+PAkgmH89O3CXxEEzNRNtHSqtXFTl0nAC1uYj0GMSH27uwtQ==", + "dev": true + }, "watchpack": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.0.1.tgz", diff --git a/package.json b/package.json index 51aec923..e05a80de 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cloudevents", - "version": "4.0.0", + "version": "4.0.1", "description": "CloudEvents SDK for JavaScript", "main": "dist/index.js", "scripts": { @@ -108,7 +108,7 @@ "@types/cucumber": "^6.0.1", "@types/got": "^9.6.11", "@types/mocha": "^7.0.2", - "@types/node": "^13.13.9", + "@types/node": "^14.14.10", "@types/superagent": "^4.1.10", "@types/uuid": "^8.0.0", "@typescript-eslint/eslint-plugin": "^3.4.0", @@ -133,7 +133,7 @@ "standard-version": "^9.0.0", "superagent": "^6.1.0", "ts-node": "^8.10.2", - "typedoc": "^0.18.0", + "typedoc": "^0.20.24", "typedoc-clarity-theme": "~1.1.0", "typescript": "^3.8.3", "webpack": "^5.1.1", diff --git a/src/transport/emitter.ts b/src/transport/emitter.ts index aad34f1d..617f8d14 100644 --- a/src/transport/emitter.ts +++ b/src/transport/emitter.ts @@ -46,14 +46,14 @@ export function emitterFor(fn: TransportFunction, options = { binding: HTTP, mod throw new TypeError("A TransportFunction is required"); } const { binding, mode } = options; - return function emit(event: CloudEvent, options?: Options): Promise { - options = options || {}; + return function emit(event: CloudEvent, opts?: Options): Promise { + opts = opts || {}; switch (mode) { case Mode.BINARY: - return fn(binding.binary(event), options); + return fn(binding.binary(event), opts); case Mode.STRUCTURED: - return fn(binding.structured(event), options); + return fn(binding.structured(event), opts); default: throw new TypeError(`Unexpected transport mode: ${mode}`); } @@ -63,29 +63,20 @@ export function emitterFor(fn: TransportFunction, options = { binding: HTTP, mod /** * A static class to emit CloudEvents within an application */ -export class Emitter extends EventEmitter { +export class Emitter { /** * Singleton store */ - static instance: Emitter | undefined = undefined; - - /** - * Create an Emitter - * On v4.0.0 this class will only remains as Singleton to allow using the - * EventEmitter of NodeJS - */ - private constructor() { - super(); - } + static instance: EventEmitter | undefined = undefined; /** * Return or create the Emitter singleton * * @return {Emitter} return Emitter singleton */ - static getInstance(): Emitter { + static getInstance(): EventEmitter { if (!Emitter.instance) { - Emitter.instance = new Emitter(); + Emitter.instance = new EventEmitter(); } return Emitter.instance; } @@ -98,7 +89,7 @@ export class Emitter extends EventEmitter { * @return {void} */ static on(event: "cloudevent" | "newListener" | "removeListener", listener: (...args: any[]) => void): void { - this.getInstance().on(event, listener); + Emitter.getInstance().on(event, listener); } /** diff --git a/test/integration/cloud_event_test.ts b/test/integration/cloud_event_test.ts index 86f0c4c0..cdb34a29 100644 --- a/test/integration/cloud_event_test.ts +++ b/test/integration/cloud_event_test.ts @@ -61,6 +61,12 @@ describe("A CloudEvent", () => { new CloudEvent({ "123456789012345678901": "extension1", ...fixture }); }).throw("invalid extension name"); }); + + it("Throws a validation error for invalid uppercase extension names", () => { + expect(() => { + new CloudEvent({ ExtensionWithCaps: "extension value", ...fixture }); + }).throw("invalid extension name"); + }); }); describe("A 1.0 CloudEvent", () => {