Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions CHANGELOG.md

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions examples/express-ex/discovery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable */

const express = require("express");
const { DiscoveryService } = require("cloudevents");
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.json());

DiscoveryService.registerService({
url: "https://example.com/services/widgetService",
name: "com.example.services.widgetService",
specversions: ["1.0"],
subscriptionurl: "https://events.example.com",
protocols: ["HTTP"],
types: [
{
type: "com.example.widget.create",
},
{
type: "com.example.widget.delete",
},
],
});
DiscoveryService.registerService({
url: "https://example.com/services/catService",
name: "com.example.services.catService",
specversions: ["1.0"],
subscriptionurl: "https://cats.example.com",
protocols: ["HTTP"],
types: [
{
type: "com.example.cats.buy",
},
{
type: "com.example.cats.adopt",
},
{
type: "com.example.cats.feed",
},
{
type: "com.example.cats.give",
},
{
// Just to play with discovery service
type: "com.example.widget.create",
},
],
});

// Serve the discovery service for our services
DiscoveryService.express(app);
// Create a anonymous mapping of cloud events for example
DiscoveryService.express(app, "/anonymous", (name, type, req) => {
// A true life example would have only one call to express and check req for permission
// Only give access to the catService and widgetCreate if anonymous
return name === "com.example.widget.create" || name === "com.example.services.catService";
});

app.listen(3000, () => {
console.log("Example app listening on port 3000!");
});
41 changes: 1 addition & 40 deletions examples/express-ex/index.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,11 @@
/* eslint-disable */

const express = require("express");
const { Receiver, DiscoveryService } = require("cloudevents");
const { Receiver } = require("cloudevents");
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.json());

DiscoveryService.registerService({
url: "https://example.com/services/widgetService",
name: "com.example.services.widgetService",
specversions: ["1.0"],
subscriptionurl: "https://events.example.com",
protocols: ["HTTP"],
types: [
{
type: "com.example.widget.create",
type: "com.example.widget.delete",
},
],
});
DiscoveryService.registerService({
url: "https://example.com/services/catService",
name: "com.example.services.catService",
specversions: ["1.0"],
subscriptionurl: "https://cats.example.com",
protocols: ["HTTP"],
types: [
{
type: "com.example.cats.buy",
type: "com.example.cats.adopt",
type: "com.example.cats.feed",
type: "com.example.cats.give",
// Just to play with discovery service
type: "com.example.widget.create",
},
],
});

DiscoveryService.express(app);
// Create a anonymous mapping of cloud events for example
DiscoveryService.express(app, "/anonymous", (name, type, req) => {
// A true life example would have only one call to express and check req for permission
// Only give access to the catService and widgetCreate if anonymous
return name === "com.example.widget.create" || name === "com.example.services.catService";
});

app.post("/", (req, res) => {
console.log("HEADERS", req.headers);
console.log("BODY", req.body);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cloudevents",
"version": "3.1.0",
"version": "1.0.0",
"description": "CloudEvents SDK for JavaScript",
"main": "dist/index.js",
"scripts": {
Expand Down
11 changes: 11 additions & 0 deletions src/discovery/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { CloudEventV1Service } from "../";

interface RequestHandler {
(
req: { query: { matching: string }; url: string },
res: {
end: () => void;
status: (
code: number,
) => { json: (obj: Record<string, unknown> | CloudEventV1Service[]) => string; end: () => void };
},
): void;
}
/**
* DiscoveryService to implement the discovery spec:
*
Expand Down