Skip to content

Commit 8323b64

Browse files
authored
examples(nodejs): bump to the latest 3.x version of cloudevents SDK (#2769)
* examples(nodejs): bump to the latest 3.x version of cloudevents SDK A few changes to the API were made in the recent release of the SDK. This commit updates the dependency and modifies the source to work with the new API. * squash: bump cloudevents to 3.2.0 Signed-off-by: Lance Ball <[email protected]> * fixup: incorporate PR feedback Signed-off-by: Lance Ball <[email protected]>
1 parent e2140fd commit 8323b64

File tree

3 files changed

+38
-24
lines changed

3 files changed

+38
-24
lines changed

docs/serving/samples/cloudevents/cloudevents-nodejs/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ limitations under the License.
1515
*/
1616

1717
const express = require('express')
18-
const { CloudEvent, HTTPEmitter, HTTPReceiver } = require('cloudevents-sdk')
18+
const { CloudEvent, Emitter, HTTP } = require('cloudevents')
1919
const PORT = process.env.PORT || 8080
2020
const target = process.env.K_SINK
2121
const app = express()
22-
const receiver = new HTTPReceiver()
2322

2423
const main = () => {
2524
app.listen(PORT, function () {
@@ -41,11 +40,11 @@ const receiveAndSend = (cloudEvent, res) => {
4140
type: 'dev.knative.docs.sample',
4241
source: 'https://github.com/knative/docs/docs/serving/samples/cloudevents/cloudevents-nodejs',
4342
time: new Date(),
44-
data: data
43+
data
4544
})
4645

4746
// With only an endpoint URL, this creates a v1 emitter
48-
const emitter = new HTTPEmitter({
47+
const emitter = new Emitter({
4948
url: target
5049
})
5150

@@ -55,7 +54,7 @@ const receiveAndSend = (cloudEvent, res) => {
5554
// Send the new Event to the K_SINK
5655
emitter.send(newCloudEvent)
5756
.then((res) => {
58-
console.log(`Sent event: ${JSON.stringify(newCloudEvent.format(), null, 2)}`)
57+
console.log(`Sent event: ${JSON.stringify(newCloudEvent, null, 2)}`)
5958
console.log(`K_SINK responded: ${JSON.stringify({ status: res.status, headers: res.headers, data: res.data }, null, 2)}`)
6059
})
6160
.catch(console.error)
@@ -64,16 +63,17 @@ const receiveAndSend = (cloudEvent, res) => {
6463
// receiveAndReply responds with new event
6564
const receiveAndReply = (cloudEvent, res) => {
6665
const data = handle(cloudEvent.data)
67-
const headers = HTTPEmitter.headers(cloudEvent)
6866
const newCloudEvent = new CloudEvent({
6967
type: 'dev.knative.docs.sample',
7068
source: 'https://github.com/knative/docs/docs/serving/samples/cloudevents/cloudevents-nodejs',
71-
time: new Date()
69+
time: new Date(),
70+
data
7271
})
7372

74-
console.log(`Reply event: ${JSON.stringify(newCloudEvent.format(), null, 2)}`)
75-
res.set(headers)
76-
res.status(200).send(data)
73+
console.log(`Reply event: ${JSON.stringify(newCloudEvent, null, 2)}`)
74+
const message = HTTP.binary(newCloudEvent);
75+
res.set(message.headers)
76+
res.status(200).send(message.body)
7777
}
7878

7979
app.use((req, res, next) => {
@@ -90,8 +90,8 @@ app.use((req, res, next) => {
9090

9191
app.post('/', function (req, res) {
9292
try {
93-
const event = receiver.accept(req.headers, req.body)
94-
console.log(`Accepted event: ${JSON.stringify(event.format(), null, 2)}`)
93+
const event = HTTP.toEvent({headers: req.headers, body: req.body})
94+
console.log(`Accepted event: ${JSON.stringify(event, null, 2)}`)
9595
target ? receiveAndSend(event, res) : receiveAndReply(event, res)
9696
} catch (err) {
9797
console.error(err)

docs/serving/samples/cloudevents/cloudevents-nodejs/package-lock.json

Lines changed: 25 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/serving/samples/cloudevents/cloudevents-nodejs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"author": "",
1414
"license": "Apache-2.0",
1515
"dependencies": {
16-
"cloudevents-sdk": "2.0.1",
16+
"cloudevents": "^3.2.0",
1717
"express": "^4.17.1",
1818
"nodemon": "^2.0.4"
1919
},

0 commit comments

Comments
 (0)