@backstage/plugin-events-backend 0.4.4-next.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/README.md +36 -62
- package/dist/index.cjs.js +0 -4
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +6 -45
- package/dist/service/EventsPlugin.cjs.js +14 -0
- package/dist/service/EventsPlugin.cjs.js.map +1 -1
- package/dist/service/http/HttpPostIngressEventPublisher.cjs.js +20 -33
- package/dist/service/http/HttpPostIngressEventPublisher.cjs.js.map +1 -1
- package/dist/service/http/body-parser/HttpApplicationJsonBodyParser.cjs.js +23 -0
- package/dist/service/http/body-parser/HttpApplicationJsonBodyParser.cjs.js.map +1 -0
- package/dist/service/http/body-parser/index.cjs.js +10 -0
- package/dist/service/http/body-parser/index.cjs.js.map +1 -0
- package/dist/service/http/errors.cjs.js +24 -0
- package/dist/service/http/errors.cjs.js.map +1 -0
- package/package.json +15 -17
- package/dist/service/DefaultEventBroker.cjs.js +0 -32
- package/dist/service/DefaultEventBroker.cjs.js.map +0 -1
- package/dist/service/EventsBackend.cjs.js +0 -36
- package/dist/service/EventsBackend.cjs.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,37 @@
|
|
|
1
1
|
# @backstage/plugin-events-backend
|
|
2
2
|
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- ee519c5: **BREAKING** Removed deprecated events related code
|
|
8
|
+
- bda96a7: **BREAKING** Removed support for the legacy backend, please migrate to the new backend system. Also removed deprecated code.
|
|
9
|
+
|
|
10
|
+
### Patch Changes
|
|
11
|
+
|
|
12
|
+
- 2f4d3bc: Allow webhook content to be 5mb instead the default 100kb
|
|
13
|
+
- b95aa77: add `addHttpPostBodyParser` to events extension to allow body parse customization. This feature will enhance flexibility in handling HTTP POST requests in event-related operations.
|
|
14
|
+
- Updated dependencies
|
|
15
|
+
- @backstage/plugin-events-node@0.4.9
|
|
16
|
+
- @backstage/backend-openapi-utils@0.5.1
|
|
17
|
+
- @backstage/backend-plugin-api@1.2.1
|
|
18
|
+
- @backstage/config@1.3.2
|
|
19
|
+
- @backstage/errors@1.2.7
|
|
20
|
+
- @backstage/types@1.2.1
|
|
21
|
+
|
|
22
|
+
## 0.4.4-next.2
|
|
23
|
+
|
|
24
|
+
### Patch Changes
|
|
25
|
+
|
|
26
|
+
- b95aa77: add `addHttpPostBodyParser` to events extension to allow body parse customization. This feature will enhance flexibility in handling HTTP POST requests in event-related operations.
|
|
27
|
+
- Updated dependencies
|
|
28
|
+
- @backstage/plugin-events-node@0.4.9-next.2
|
|
29
|
+
- @backstage/backend-openapi-utils@0.5.1-next.1
|
|
30
|
+
- @backstage/backend-plugin-api@1.2.1-next.1
|
|
31
|
+
- @backstage/config@1.3.2
|
|
32
|
+
- @backstage/errors@1.2.7
|
|
33
|
+
- @backstage/types@1.2.1
|
|
34
|
+
|
|
3
35
|
## 0.4.4-next.1
|
|
4
36
|
|
|
5
37
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -26,58 +26,6 @@ yarn --cwd packages/backend add @backstage/plugin-events-backend
|
|
|
26
26
|
backend.add(import('@backstage/plugin-events-backend'));
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
### Legacy Backend System
|
|
30
|
-
|
|
31
|
-
```ts
|
|
32
|
-
// packages/backend/src/plugins/events.ts
|
|
33
|
-
import { HttpPostIngressEventPublisher } from '@backstage/plugin-events-backend';
|
|
34
|
-
import { Router } from 'express';
|
|
35
|
-
import { PluginEnvironment } from '../types';
|
|
36
|
-
|
|
37
|
-
export default async function createPlugin(
|
|
38
|
-
env: PluginEnvironment,
|
|
39
|
-
): Promise<Router> {
|
|
40
|
-
const eventsRouter = Router();
|
|
41
|
-
|
|
42
|
-
const http = HttpPostIngressEventPublisher.fromConfig({
|
|
43
|
-
config: env.config,
|
|
44
|
-
events: env.events,
|
|
45
|
-
logger: env.logger,
|
|
46
|
-
});
|
|
47
|
-
http.bind(eventsRouter);
|
|
48
|
-
|
|
49
|
-
return eventsRouter;
|
|
50
|
-
}
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
### Event-based Entity Providers
|
|
54
|
-
|
|
55
|
-
You can implement the `EventSubscriber` interface on an `EntityProviders` to allow it to handle events from other plugins e.g. the event backend plugin
|
|
56
|
-
mentioned above.
|
|
57
|
-
|
|
58
|
-
Assuming you have configured the `eventBroker` into the `PluginEnvironment` you can pass the broker to the entity provider for it to subscribe.
|
|
59
|
-
|
|
60
|
-
```diff
|
|
61
|
-
// packages/backend/src/plugins/catalog.ts
|
|
62
|
-
import { CatalogBuilder } from '@backstage/plugin-catalog-backend';
|
|
63
|
-
+import { DemoEventBasedEntityProvider } from './DemoEventBasedEntityProvider';
|
|
64
|
-
import { ScaffolderEntitiesProcessor } from '@backstage/plugin-scaffolder-backend';
|
|
65
|
-
import { Router } from 'express';
|
|
66
|
-
import { PluginEnvironment } from '../types';
|
|
67
|
-
|
|
68
|
-
export default async function createPlugin(
|
|
69
|
-
env: PluginEnvironment,
|
|
70
|
-
): Promise<Router> {
|
|
71
|
-
const builder = await CatalogBuilder.create(env);
|
|
72
|
-
builder.addProcessor(new ScaffolderEntitiesProcessor());
|
|
73
|
-
+ const demoProvider = new DemoEventBasedEntityProvider({ logger: env.logger, topics: ['example'], eventBroker: env.eventBroker });
|
|
74
|
-
+ builder.addEntityProvider(demoProvider);
|
|
75
|
-
const { processingEngine, router } = await builder.build();
|
|
76
|
-
await processingEngine.start();
|
|
77
|
-
return router;
|
|
78
|
-
}
|
|
79
|
-
```
|
|
80
|
-
|
|
81
29
|
## Configuration
|
|
82
30
|
|
|
83
31
|
In order to create HTTP endpoints to receive events for a certain
|
|
@@ -140,18 +88,44 @@ export const eventsModuleYourFeature = createBackendModule({
|
|
|
140
88
|
});
|
|
141
89
|
```
|
|
142
90
|
|
|
143
|
-
|
|
91
|
+
### Request Body Parse
|
|
92
|
+
|
|
93
|
+
We need to parse the request body before we can validate it. We have some default parsers but you can provide your own when necessary.
|
|
144
94
|
|
|
145
95
|
```ts
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
96
|
+
import { eventsExtensionPoint } from '@backstage/plugin-events-node/alpha';
|
|
97
|
+
|
|
98
|
+
// [...]
|
|
99
|
+
|
|
100
|
+
export const eventsModuleYourFeature = createBackendModule({
|
|
101
|
+
pluginId: 'events',
|
|
102
|
+
moduleId: 'your-feature',
|
|
103
|
+
register(env) {
|
|
104
|
+
// [...]
|
|
105
|
+
env.registerInit({
|
|
106
|
+
deps: {
|
|
107
|
+
// [...]
|
|
108
|
+
events: eventsExtensionPoint,
|
|
109
|
+
// [...]
|
|
110
|
+
},
|
|
111
|
+
async init({ /* ... */ events /*, ... */ }) {
|
|
112
|
+
// [...]
|
|
113
|
+
events.addHttpPostBodyParser({
|
|
114
|
+
contentType: 'application/x-www-form-urlencoded',
|
|
115
|
+
parser: async (req, _topic) => {
|
|
116
|
+
return {
|
|
117
|
+
bodyParsed: req.body.toString('utf-8'),
|
|
118
|
+
bodyBuffer: req.body,
|
|
119
|
+
encoding: 'utf-8',
|
|
120
|
+
};
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
},
|
|
124
|
+
});
|
|
153
125
|
},
|
|
154
|
-
logger: env.logger,
|
|
155
126
|
});
|
|
156
|
-
http.bind(router);
|
|
157
127
|
```
|
|
128
|
+
|
|
129
|
+
We have the following default parsers:
|
|
130
|
+
|
|
131
|
+
- `application/json`
|
package/dist/index.cjs.js
CHANGED
|
@@ -3,14 +3,10 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var EventsPlugin = require('./service/EventsPlugin.cjs.js');
|
|
6
|
-
var EventsBackend = require('./service/EventsBackend.cjs.js');
|
|
7
|
-
var DefaultEventBroker = require('./service/DefaultEventBroker.cjs.js');
|
|
8
6
|
var HttpPostIngressEventPublisher = require('./service/http/HttpPostIngressEventPublisher.cjs.js');
|
|
9
7
|
|
|
10
8
|
|
|
11
9
|
|
|
12
10
|
exports.default = EventsPlugin.eventsPlugin;
|
|
13
|
-
exports.EventsBackend = EventsBackend.EventsBackend;
|
|
14
|
-
exports.DefaultEventBroker = DefaultEventBroker.DefaultEventBroker;
|
|
15
11
|
exports.HttpPostIngressEventPublisher = HttpPostIngressEventPublisher.HttpPostIngressEventPublisher;
|
|
16
12
|
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import * as _backstage_backend_plugin_api from '@backstage/backend-plugin-api';
|
|
2
2
|
import { LoggerService } from '@backstage/backend-plugin-api';
|
|
3
|
-
import { EventBroker, EventPublisher, EventSubscriber, EventsService, EventParams, HttpPostIngressOptions } from '@backstage/plugin-events-node';
|
|
4
|
-
import { Logger } from 'winston';
|
|
5
3
|
import { Config } from '@backstage/config';
|
|
4
|
+
import { EventsService, HttpPostIngressOptions, HttpBodyParser } from '@backstage/plugin-events-node';
|
|
6
5
|
import express from 'express';
|
|
7
6
|
|
|
8
7
|
/**
|
|
@@ -12,48 +11,6 @@ import express from 'express';
|
|
|
12
11
|
*/
|
|
13
12
|
declare const eventsPlugin: _backstage_backend_plugin_api.BackendFeature;
|
|
14
13
|
|
|
15
|
-
/**
|
|
16
|
-
* A builder that helps wire up all component parts of the event management.
|
|
17
|
-
*
|
|
18
|
-
* @public
|
|
19
|
-
* @deprecated `EventBroker`, `EventPublisher`, and `EventSubscriber` got replaced by `EventsService` and its methods.
|
|
20
|
-
*/
|
|
21
|
-
declare class EventsBackend {
|
|
22
|
-
private eventBroker;
|
|
23
|
-
private publishers;
|
|
24
|
-
private subscribers;
|
|
25
|
-
constructor(logger: Logger);
|
|
26
|
-
setEventBroker(eventBroker: EventBroker): EventsBackend;
|
|
27
|
-
addPublishers(...publishers: Array<EventPublisher | Array<EventPublisher>>): EventsBackend;
|
|
28
|
-
addSubscribers(...subscribers: Array<EventSubscriber | Array<EventSubscriber>>): EventsBackend;
|
|
29
|
-
/**
|
|
30
|
-
* Wires up and returns all component parts of the event management.
|
|
31
|
-
*/
|
|
32
|
-
start(): Promise<void>;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* In process event broker which will pass the event to all registered subscribers
|
|
37
|
-
* interested in it.
|
|
38
|
-
* Events will not be persisted in any form.
|
|
39
|
-
*
|
|
40
|
-
* @public
|
|
41
|
-
* @deprecated use `DefaultEventsService` from `@backstage/plugin-events-node` instead
|
|
42
|
-
*/
|
|
43
|
-
declare class DefaultEventBroker implements EventBroker {
|
|
44
|
-
private readonly events;
|
|
45
|
-
/**
|
|
46
|
-
*
|
|
47
|
-
* @param logger - logger
|
|
48
|
-
* @param events - replacement that gets wrapped to support not yet migrated implementations.
|
|
49
|
-
* An instance can be passed (required for a mixed mode), otherwise a new instance gets created internally.
|
|
50
|
-
* @deprecated use `DefaultEventsService` directly instead
|
|
51
|
-
*/
|
|
52
|
-
constructor(logger: LoggerService, events?: EventsService);
|
|
53
|
-
publish(params: EventParams): Promise<void>;
|
|
54
|
-
subscribe(...subscribers: Array<EventSubscriber | Array<EventSubscriber>>): void;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
14
|
/**
|
|
58
15
|
* Publishes events received from their origin (e.g., webhook events from an SCM system)
|
|
59
16
|
* via HTTP POST endpoint and passes the request body as event payload to the registered subscribers.
|
|
@@ -64,12 +21,16 @@ declare class HttpPostIngressEventPublisher {
|
|
|
64
21
|
private readonly events;
|
|
65
22
|
private readonly logger;
|
|
66
23
|
private readonly ingresses;
|
|
24
|
+
private readonly bodyParsers;
|
|
67
25
|
static fromConfig(env: {
|
|
68
26
|
config: Config;
|
|
69
27
|
events: EventsService;
|
|
70
28
|
ingresses?: {
|
|
71
29
|
[topic: string]: Omit<HttpPostIngressOptions, 'topic'>;
|
|
72
30
|
};
|
|
31
|
+
bodyParsers?: {
|
|
32
|
+
[contentType: string]: HttpBodyParser;
|
|
33
|
+
};
|
|
73
34
|
logger: LoggerService;
|
|
74
35
|
}): HttpPostIngressEventPublisher;
|
|
75
36
|
private constructor();
|
|
@@ -78,4 +39,4 @@ declare class HttpPostIngressEventPublisher {
|
|
|
78
39
|
private addRouteForTopic;
|
|
79
40
|
}
|
|
80
41
|
|
|
81
|
-
export {
|
|
42
|
+
export { HttpPostIngressEventPublisher, eventsPlugin as default };
|
|
@@ -13,6 +13,7 @@ var Router__default = /*#__PURE__*/_interopDefaultCompat(Router);
|
|
|
13
13
|
|
|
14
14
|
class EventsExtensionPointImpl {
|
|
15
15
|
#httpPostIngresses = [];
|
|
16
|
+
#httpBodyParsers = [];
|
|
16
17
|
setEventBroker(_) {
|
|
17
18
|
throw new Error(
|
|
18
19
|
"setEventBroker is not supported anymore; use eventsServiceRef instead"
|
|
@@ -31,9 +32,15 @@ class EventsExtensionPointImpl {
|
|
|
31
32
|
addHttpPostIngress(options) {
|
|
32
33
|
this.#httpPostIngresses.push(options);
|
|
33
34
|
}
|
|
35
|
+
addHttpPostBodyParser(options) {
|
|
36
|
+
this.#httpBodyParsers.push(options);
|
|
37
|
+
}
|
|
34
38
|
get httpPostIngresses() {
|
|
35
39
|
return this.#httpPostIngresses;
|
|
36
40
|
}
|
|
41
|
+
get httpBodyParsers() {
|
|
42
|
+
return this.#httpBodyParsers;
|
|
43
|
+
}
|
|
37
44
|
}
|
|
38
45
|
const eventsPlugin = backendPluginApi.createBackendPlugin({
|
|
39
46
|
pluginId: "events",
|
|
@@ -67,10 +74,17 @@ const eventsPlugin = backendPluginApi.createBackendPlugin({
|
|
|
67
74
|
ingress
|
|
68
75
|
])
|
|
69
76
|
);
|
|
77
|
+
const bodyParsers = Object.fromEntries(
|
|
78
|
+
extensionPoint.httpBodyParsers.map((option) => [
|
|
79
|
+
option.contentType,
|
|
80
|
+
option.parser
|
|
81
|
+
])
|
|
82
|
+
);
|
|
70
83
|
const http = HttpPostIngressEventPublisher.HttpPostIngressEventPublisher.fromConfig({
|
|
71
84
|
config,
|
|
72
85
|
events,
|
|
73
86
|
ingresses,
|
|
87
|
+
bodyParsers,
|
|
74
88
|
logger
|
|
75
89
|
});
|
|
76
90
|
const eventsRouter = Router__default.default();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EventsPlugin.cjs.js","sources":["../../src/service/EventsPlugin.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n createBackendPlugin,\n coreServices,\n} from '@backstage/backend-plugin-api';\nimport {\n eventsExtensionPoint,\n EventsExtensionPoint,\n} from '@backstage/plugin-events-node/alpha';\nimport {\n eventsServiceRef,\n HttpPostIngressOptions,\n} from '@backstage/plugin-events-node';\nimport Router from 'express-promise-router';\nimport { HttpPostIngressEventPublisher } from './http';\nimport { createEventBusRouter } from './hub';\n\nclass EventsExtensionPointImpl implements EventsExtensionPoint {\n #httpPostIngresses: HttpPostIngressOptions[] = [];\n\n setEventBroker(_: any): void {\n throw new Error(\n 'setEventBroker is not supported anymore; use eventsServiceRef instead',\n );\n }\n\n addPublishers(_: any): void {\n throw new Error(\n 'addPublishers is not supported anymore; use EventsService instead',\n );\n }\n\n addSubscribers(_: any): void {\n throw new Error(\n 'addSubscribers is not supported anymore; use EventsService instead',\n );\n }\n\n addHttpPostIngress(options: HttpPostIngressOptions) {\n this.#httpPostIngresses.push(options);\n }\n\n get httpPostIngresses() {\n return this.#httpPostIngresses;\n }\n}\n\n/**\n * Events plugin\n *\n * @public\n */\nexport const eventsPlugin = createBackendPlugin({\n pluginId: 'events',\n register(env) {\n const extensionPoint = new EventsExtensionPointImpl();\n env.registerExtensionPoint(eventsExtensionPoint, extensionPoint);\n\n env.registerInit({\n deps: {\n config: coreServices.rootConfig,\n events: eventsServiceRef,\n database: coreServices.database,\n httpAuth: coreServices.httpAuth,\n httpRouter: coreServices.httpRouter,\n lifecycle: coreServices.lifecycle,\n logger: coreServices.logger,\n scheduler: coreServices.scheduler,\n },\n async init({\n config,\n events,\n database,\n httpAuth,\n httpRouter,\n lifecycle,\n logger,\n scheduler,\n }) {\n const ingresses = Object.fromEntries(\n extensionPoint.httpPostIngresses.map(ingress => [\n ingress.topic,\n ingress as Omit<HttpPostIngressOptions, 'topic'>,\n ]),\n );\n\n const http = HttpPostIngressEventPublisher.fromConfig({\n config,\n events,\n ingresses,\n logger,\n });\n const eventsRouter = Router();\n http.bind(eventsRouter);\n\n // MUST be registered *before* the event bus router.\n // Otherwise, it would already make use of `express.json()`\n // that is used there as part of the middleware stack.\n httpRouter.use(eventsRouter);\n\n const notifyTimeoutMs = config.getOptionalNumber(\n 'events.notifyTimeoutMs',\n );\n\n httpRouter.use(\n await createEventBusRouter({\n database,\n lifecycle,\n logger,\n httpAuth,\n scheduler,\n notifyTimeoutMs,\n }),\n );\n\n httpRouter.addAuthPolicy({\n allow: 'unauthenticated',\n path: '/http',\n });\n },\n });\n },\n});\n"],"names":["createBackendPlugin","eventsExtensionPoint","coreServices","eventsServiceRef","HttpPostIngressEventPublisher","Router","createEventBusRouter"],"mappings":";;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"EventsPlugin.cjs.js","sources":["../../src/service/EventsPlugin.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n createBackendPlugin,\n coreServices,\n} from '@backstage/backend-plugin-api';\nimport {\n eventsExtensionPoint,\n EventsExtensionPoint,\n} from '@backstage/plugin-events-node/alpha';\nimport {\n eventsServiceRef,\n HttpBodyParserOptions,\n HttpPostIngressOptions,\n} from '@backstage/plugin-events-node';\nimport Router from 'express-promise-router';\nimport { HttpPostIngressEventPublisher } from './http';\nimport { createEventBusRouter } from './hub';\n\nclass EventsExtensionPointImpl implements EventsExtensionPoint {\n readonly #httpPostIngresses: HttpPostIngressOptions[] = [];\n readonly #httpBodyParsers: HttpBodyParserOptions[] = [];\n\n setEventBroker(_: any): void {\n throw new Error(\n 'setEventBroker is not supported anymore; use eventsServiceRef instead',\n );\n }\n\n addPublishers(_: any): void {\n throw new Error(\n 'addPublishers is not supported anymore; use EventsService instead',\n );\n }\n\n addSubscribers(_: any): void {\n throw new Error(\n 'addSubscribers is not supported anymore; use EventsService instead',\n );\n }\n\n addHttpPostIngress(options: HttpPostIngressOptions) {\n this.#httpPostIngresses.push(options);\n }\n\n addHttpPostBodyParser(options: HttpBodyParserOptions): void {\n this.#httpBodyParsers.push(options);\n }\n\n get httpPostIngresses() {\n return this.#httpPostIngresses;\n }\n\n get httpBodyParsers() {\n return this.#httpBodyParsers;\n }\n}\n\n/**\n * Events plugin\n *\n * @public\n */\nexport const eventsPlugin = createBackendPlugin({\n pluginId: 'events',\n register(env) {\n const extensionPoint = new EventsExtensionPointImpl();\n env.registerExtensionPoint(eventsExtensionPoint, extensionPoint);\n\n env.registerInit({\n deps: {\n config: coreServices.rootConfig,\n events: eventsServiceRef,\n database: coreServices.database,\n httpAuth: coreServices.httpAuth,\n httpRouter: coreServices.httpRouter,\n lifecycle: coreServices.lifecycle,\n logger: coreServices.logger,\n scheduler: coreServices.scheduler,\n },\n async init({\n config,\n events,\n database,\n httpAuth,\n httpRouter,\n lifecycle,\n logger,\n scheduler,\n }) {\n const ingresses = Object.fromEntries(\n extensionPoint.httpPostIngresses.map(ingress => [\n ingress.topic,\n ingress as Omit<HttpPostIngressOptions, 'topic'>,\n ]),\n );\n\n const bodyParsers = Object.fromEntries(\n extensionPoint.httpBodyParsers.map(option => [\n option.contentType,\n option.parser,\n ]),\n );\n\n const http = HttpPostIngressEventPublisher.fromConfig({\n config,\n events,\n ingresses,\n bodyParsers,\n logger,\n });\n const eventsRouter = Router();\n http.bind(eventsRouter);\n\n // MUST be registered *before* the event bus router.\n // Otherwise, it would already make use of `express.json()`\n // that is used there as part of the middleware stack.\n httpRouter.use(eventsRouter);\n\n const notifyTimeoutMs = config.getOptionalNumber(\n 'events.notifyTimeoutMs',\n );\n\n httpRouter.use(\n await createEventBusRouter({\n database,\n lifecycle,\n logger,\n httpAuth,\n scheduler,\n notifyTimeoutMs,\n }),\n );\n\n httpRouter.addAuthPolicy({\n allow: 'unauthenticated',\n path: '/http',\n });\n },\n });\n },\n});\n"],"names":["createBackendPlugin","eventsExtensionPoint","coreServices","eventsServiceRef","HttpPostIngressEventPublisher","Router","createEventBusRouter"],"mappings":"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"}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var errors = require('@backstage/errors');
|
|
4
3
|
var contentType = require('content-type');
|
|
5
4
|
var express = require('express');
|
|
6
5
|
var Router = require('express-promise-router');
|
|
6
|
+
var index = require('./body-parser/index.cjs.js');
|
|
7
7
|
var RequestValidationContextImpl = require('./validation/RequestValidationContextImpl.cjs.js');
|
|
8
|
+
var errors = require('./errors.cjs.js');
|
|
8
9
|
|
|
9
10
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
10
11
|
|
|
@@ -12,25 +13,12 @@ var contentType__default = /*#__PURE__*/_interopDefaultCompat(contentType);
|
|
|
12
13
|
var express__default = /*#__PURE__*/_interopDefaultCompat(express);
|
|
13
14
|
var Router__default = /*#__PURE__*/_interopDefaultCompat(Router);
|
|
14
15
|
|
|
15
|
-
class UnsupportedCharsetError extends errors.CustomErrorBase {
|
|
16
|
-
name = "UnsupportedCharsetError";
|
|
17
|
-
statusCode = 415;
|
|
18
|
-
constructor(charset) {
|
|
19
|
-
super(`Unsupported charset: ${charset}`);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
class UnsupportedMediaTypeError extends errors.CustomErrorBase {
|
|
23
|
-
name = "UnsupportedMediaTypeError";
|
|
24
|
-
statusCode = 415;
|
|
25
|
-
constructor(mediaType) {
|
|
26
|
-
super(`Unsupported media type: ${mediaType ?? "unknown"}`);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
16
|
class HttpPostIngressEventPublisher {
|
|
30
|
-
constructor(events, logger, ingresses) {
|
|
17
|
+
constructor(events, logger, ingresses, bodyParsers) {
|
|
31
18
|
this.events = events;
|
|
32
19
|
this.logger = logger;
|
|
33
20
|
this.ingresses = ingresses;
|
|
21
|
+
this.bodyParsers = bodyParsers;
|
|
34
22
|
}
|
|
35
23
|
static fromConfig(env) {
|
|
36
24
|
const topics = env.config.getOptionalStringArray("events.http.topics") ?? [];
|
|
@@ -40,7 +28,13 @@ class HttpPostIngressEventPublisher {
|
|
|
40
28
|
ingresses[topic] = {};
|
|
41
29
|
}
|
|
42
30
|
});
|
|
43
|
-
|
|
31
|
+
const parsers = { ...index.defaultHttpBodyParsers, ...env.bodyParsers };
|
|
32
|
+
return new HttpPostIngressEventPublisher(
|
|
33
|
+
env.events,
|
|
34
|
+
env.logger,
|
|
35
|
+
ingresses,
|
|
36
|
+
parsers
|
|
37
|
+
);
|
|
44
38
|
}
|
|
45
39
|
bind(router) {
|
|
46
40
|
router.use("/http", this.createRouter(this.ingresses));
|
|
@@ -57,23 +51,16 @@ class HttpPostIngressEventPublisher {
|
|
|
57
51
|
const path = `/${topic}`;
|
|
58
52
|
const logger = this.logger;
|
|
59
53
|
router.post(path, async (request, response) => {
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
const bodyBuffer = requestBody;
|
|
67
|
-
const parsedContentType = contentType__default.default.parse(request);
|
|
68
|
-
if (!parsedContentType.type || parsedContentType.type !== "application/json") {
|
|
69
|
-
throw new UnsupportedMediaTypeError(parsedContentType.type);
|
|
70
|
-
}
|
|
71
|
-
const encoding = parsedContentType.parameters.charset ?? "utf-8";
|
|
72
|
-
if (!Buffer.isEncoding(encoding)) {
|
|
73
|
-
throw new UnsupportedCharsetError(encoding);
|
|
54
|
+
const requestContentType = contentType__default.default.parse(request);
|
|
55
|
+
const bodyParser = this.bodyParsers[requestContentType.type ?? ""];
|
|
56
|
+
if (!bodyParser) {
|
|
57
|
+
throw new errors.UnsupportedMediaTypeError(requestContentType.type);
|
|
74
58
|
}
|
|
75
|
-
const
|
|
76
|
-
|
|
59
|
+
const { bodyParsed, bodyBuffer, encoding } = await bodyParser(
|
|
60
|
+
request,
|
|
61
|
+
requestContentType,
|
|
62
|
+
topic
|
|
63
|
+
);
|
|
77
64
|
if (validator) {
|
|
78
65
|
const requestDetails = {
|
|
79
66
|
body: bodyParsed,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HttpPostIngressEventPublisher.cjs.js","sources":["../../../src/service/http/HttpPostIngressEventPublisher.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport { Config } from '@backstage/config';\nimport {
|
|
1
|
+
{"version":3,"file":"HttpPostIngressEventPublisher.cjs.js","sources":["../../../src/service/http/HttpPostIngressEventPublisher.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport { Config } from '@backstage/config';\nimport {\n EventsService,\n HttpBodyParser,\n HttpPostIngressOptions,\n RequestValidator,\n} from '@backstage/plugin-events-node';\nimport contentType from 'content-type';\nimport express from 'express';\nimport Router from 'express-promise-router';\nimport { defaultHttpBodyParsers } from './body-parser';\nimport { RequestValidationContextImpl } from './validation';\nimport { UnsupportedMediaTypeError } from './errors';\n/**\n * Publishes events received from their origin (e.g., webhook events from an SCM system)\n * via HTTP POST endpoint and passes the request body as event payload to the registered subscribers.\n *\n * @public\n */\n// TODO(pjungermann): add prom metrics? (see plugins/catalog-backend/src/util/metrics.ts, etc.)\nexport class HttpPostIngressEventPublisher {\n static fromConfig(env: {\n config: Config;\n events: EventsService;\n ingresses?: { [topic: string]: Omit<HttpPostIngressOptions, 'topic'> };\n bodyParsers?: { [contentType: string]: HttpBodyParser };\n logger: LoggerService;\n }): HttpPostIngressEventPublisher {\n const topics =\n env.config.getOptionalStringArray('events.http.topics') ?? [];\n\n const ingresses = env.ingresses ?? {};\n topics.forEach(topic => {\n // don't overwrite topic settings\n // (e.g., added at the config as well as argument)\n if (!ingresses[topic]) {\n ingresses[topic] = {};\n }\n });\n\n const parsers = { ...defaultHttpBodyParsers, ...env.bodyParsers };\n\n return new HttpPostIngressEventPublisher(\n env.events,\n env.logger,\n ingresses,\n parsers,\n );\n }\n\n private constructor(\n private readonly events: EventsService,\n private readonly logger: LoggerService,\n private readonly ingresses: {\n [topic: string]: Omit<HttpPostIngressOptions, 'topic'>;\n },\n private readonly bodyParsers: {\n [contentType: string]: HttpBodyParser;\n },\n ) {}\n\n bind(router: express.Router): void {\n router.use('/http', this.createRouter(this.ingresses));\n }\n\n private createRouter(ingresses: {\n [topic: string]: Omit<HttpPostIngressOptions, 'topic'>;\n }): express.Router {\n const router = Router();\n router.use(express.raw({ type: '*/*', limit: '5mb' }));\n\n Object.keys(ingresses).forEach(topic =>\n this.addRouteForTopic(router, topic, ingresses[topic].validator),\n );\n\n return router;\n }\n\n private addRouteForTopic(\n router: express.Router,\n topic: string,\n validator?: RequestValidator,\n ): void {\n const path = `/${topic}`;\n const logger = this.logger;\n\n router.post(path, async (request, response) => {\n const requestContentType = contentType.parse(request);\n const bodyParser = this.bodyParsers[requestContentType.type ?? ''];\n\n if (!bodyParser) {\n throw new UnsupportedMediaTypeError(requestContentType.type);\n }\n\n const { bodyParsed, bodyBuffer, encoding } = await bodyParser(\n request,\n requestContentType,\n topic,\n );\n\n if (validator) {\n const requestDetails = {\n body: bodyParsed,\n headers: request.headers,\n raw: {\n body: bodyBuffer,\n encoding: encoding as BufferEncoding,\n },\n };\n\n const context = new RequestValidationContextImpl();\n await validator(requestDetails, context);\n\n if (context.wasRejected()) {\n response\n .status(context.rejectionDetails!.status)\n .json(context.rejectionDetails!.payload);\n return;\n }\n }\n\n await this.events.publish({\n topic,\n eventPayload: bodyParsed,\n metadata: request.headers,\n });\n\n response.status(202).json({ status: 'accepted' });\n });\n\n // TODO(pjungermann): We don't really know the externally defined path prefix here,\n // however it is more useful for users to have it. Is there a better way?\n logger.info(`Registered /api/events/http${path} to receive events`);\n }\n}\n"],"names":["defaultHttpBodyParsers","Router","express","contentType","UnsupportedMediaTypeError","RequestValidationContextImpl"],"mappings":";;;;;;;;;;;;;;;AAqCO,MAAM,6BAA8B,CAAA;AAAA,EA8BjC,WACW,CAAA,MAAA,EACA,MACA,EAAA,SAAA,EAGA,WAGjB,EAAA;AARiB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AACA,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AACA,IAAA,IAAA,CAAA,SAAA,GAAA,SAAA;AAGA,IAAA,IAAA,CAAA,WAAA,GAAA,WAAA;AAAA;AAGhB,EAtCH,OAAO,WAAW,GAMgB,EAAA;AAChC,IAAA,MAAM,SACJ,GAAI,CAAA,MAAA,CAAO,sBAAuB,CAAA,oBAAoB,KAAK,EAAC;AAE9D,IAAM,MAAA,SAAA,GAAY,GAAI,CAAA,SAAA,IAAa,EAAC;AACpC,IAAA,MAAA,CAAO,QAAQ,CAAS,KAAA,KAAA;AAGtB,MAAI,IAAA,CAAC,SAAU,CAAA,KAAK,CAAG,EAAA;AACrB,QAAU,SAAA,CAAA,KAAK,IAAI,EAAC;AAAA;AACtB,KACD,CAAA;AAED,IAAA,MAAM,UAAU,EAAE,GAAGA,4BAAwB,EAAA,GAAG,IAAI,WAAY,EAAA;AAEhE,IAAA,OAAO,IAAI,6BAAA;AAAA,MACT,GAAI,CAAA,MAAA;AAAA,MACJ,GAAI,CAAA,MAAA;AAAA,MACJ,SAAA;AAAA,MACA;AAAA,KACF;AAAA;AACF,EAaA,KAAK,MAA8B,EAAA;AACjC,IAAA,MAAA,CAAO,IAAI,OAAS,EAAA,IAAA,CAAK,YAAa,CAAA,IAAA,CAAK,SAAS,CAAC,CAAA;AAAA;AACvD,EAEQ,aAAa,SAEF,EAAA;AACjB,IAAA,MAAM,SAASC,uBAAO,EAAA;AACtB,IAAO,MAAA,CAAA,GAAA,CAAIC,yBAAQ,GAAI,CAAA,EAAE,MAAM,KAAO,EAAA,KAAA,EAAO,KAAM,EAAC,CAAC,CAAA;AAErD,IAAO,MAAA,CAAA,IAAA,CAAK,SAAS,CAAE,CAAA,OAAA;AAAA,MAAQ,CAAA,KAAA,KAC7B,KAAK,gBAAiB,CAAA,MAAA,EAAQ,OAAO,SAAU,CAAA,KAAK,EAAE,SAAS;AAAA,KACjE;AAEA,IAAO,OAAA,MAAA;AAAA;AACT,EAEQ,gBAAA,CACN,MACA,EAAA,KAAA,EACA,SACM,EAAA;AACN,IAAM,MAAA,IAAA,GAAO,IAAI,KAAK,CAAA,CAAA;AACtB,IAAA,MAAM,SAAS,IAAK,CAAA,MAAA;AAEpB,IAAA,MAAA,CAAO,IAAK,CAAA,IAAA,EAAM,OAAO,OAAA,EAAS,QAAa,KAAA;AAC7C,MAAM,MAAA,kBAAA,GAAqBC,4BAAY,CAAA,KAAA,CAAM,OAAO,CAAA;AACpD,MAAA,MAAM,UAAa,GAAA,IAAA,CAAK,WAAY,CAAA,kBAAA,CAAmB,QAAQ,EAAE,CAAA;AAEjE,MAAA,IAAI,CAAC,UAAY,EAAA;AACf,QAAM,MAAA,IAAIC,gCAA0B,CAAA,kBAAA,CAAmB,IAAI,CAAA;AAAA;AAG7D,MAAA,MAAM,EAAE,UAAA,EAAY,UAAY,EAAA,QAAA,KAAa,MAAM,UAAA;AAAA,QACjD,OAAA;AAAA,QACA,kBAAA;AAAA,QACA;AAAA,OACF;AAEA,MAAA,IAAI,SAAW,EAAA;AACb,QAAA,MAAM,cAAiB,GAAA;AAAA,UACrB,IAAM,EAAA,UAAA;AAAA,UACN,SAAS,OAAQ,CAAA,OAAA;AAAA,UACjB,GAAK,EAAA;AAAA,YACH,IAAM,EAAA,UAAA;AAAA,YACN;AAAA;AACF,SACF;AAEA,QAAM,MAAA,OAAA,GAAU,IAAIC,yDAA6B,EAAA;AACjD,QAAM,MAAA,SAAA,CAAU,gBAAgB,OAAO,CAAA;AAEvC,QAAI,IAAA,OAAA,CAAQ,aAAe,EAAA;AACzB,UACG,QAAA,CAAA,MAAA,CAAO,QAAQ,gBAAkB,CAAA,MAAM,EACvC,IAAK,CAAA,OAAA,CAAQ,iBAAkB,OAAO,CAAA;AACzC,UAAA;AAAA;AACF;AAGF,MAAM,MAAA,IAAA,CAAK,OAAO,OAAQ,CAAA;AAAA,QACxB,KAAA;AAAA,QACA,YAAc,EAAA,UAAA;AAAA,QACd,UAAU,OAAQ,CAAA;AAAA,OACnB,CAAA;AAED,MAAA,QAAA,CAAS,OAAO,GAAG,CAAA,CAAE,KAAK,EAAE,MAAA,EAAQ,YAAY,CAAA;AAAA,KACjD,CAAA;AAID,IAAO,MAAA,CAAA,IAAA,CAAK,CAA8B,2BAAA,EAAA,IAAI,CAAoB,kBAAA,CAAA,CAAA;AAAA;AAEtE;;;;"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var errors = require('../errors.cjs.js');
|
|
4
|
+
|
|
5
|
+
const HttpApplicationJsonBodyParser = async (request, parsedMediaType, topic) => {
|
|
6
|
+
const requestBody = request.body;
|
|
7
|
+
if (!Buffer.isBuffer(requestBody)) {
|
|
8
|
+
throw new Error(
|
|
9
|
+
`Failed to retrieve raw body from incoming event for topic ${topic}; not a buffer: ${typeof requestBody}`
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
const bodyBuffer = requestBody;
|
|
13
|
+
const encoding = parsedMediaType.parameters.charset ?? "utf-8";
|
|
14
|
+
if (!Buffer.isEncoding(encoding)) {
|
|
15
|
+
throw new errors.UnsupportedCharsetError(encoding);
|
|
16
|
+
}
|
|
17
|
+
const bodyString = bodyBuffer.toString(encoding);
|
|
18
|
+
const bodyParsed = parsedMediaType.type === "application/json" ? JSON.parse(bodyString) : bodyString;
|
|
19
|
+
return { bodyParsed, bodyBuffer, encoding };
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
exports.HttpApplicationJsonBodyParser = HttpApplicationJsonBodyParser;
|
|
23
|
+
//# sourceMappingURL=HttpApplicationJsonBodyParser.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HttpApplicationJsonBodyParser.cjs.js","sources":["../../../../src/service/http/body-parser/HttpApplicationJsonBodyParser.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { HttpBodyParser } from '@backstage/plugin-events-node';\nimport { UnsupportedCharsetError } from '../errors';\n\nexport const HttpApplicationJsonBodyParser: HttpBodyParser = async (\n request,\n parsedMediaType,\n topic,\n) => {\n const requestBody = request.body;\n if (!Buffer.isBuffer(requestBody)) {\n throw new Error(\n `Failed to retrieve raw body from incoming event for topic ${topic}; not a buffer: ${typeof requestBody}`,\n );\n }\n\n const bodyBuffer: Buffer = requestBody;\n\n const encoding = parsedMediaType.parameters.charset ?? 'utf-8';\n if (!Buffer.isEncoding(encoding)) {\n throw new UnsupportedCharsetError(encoding);\n }\n\n const bodyString = bodyBuffer.toString(encoding);\n const bodyParsed =\n parsedMediaType.type === 'application/json'\n ? JSON.parse(bodyString)\n : bodyString;\n return { bodyParsed, bodyBuffer, encoding };\n};\n"],"names":["UnsupportedCharsetError"],"mappings":";;;;AAkBO,MAAM,6BAAgD,GAAA,OAC3D,OACA,EAAA,eAAA,EACA,KACG,KAAA;AACH,EAAA,MAAM,cAAc,OAAQ,CAAA,IAAA;AAC5B,EAAA,IAAI,CAAC,MAAA,CAAO,QAAS,CAAA,WAAW,CAAG,EAAA;AACjC,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,CAA6D,0DAAA,EAAA,KAAK,CAAmB,gBAAA,EAAA,OAAO,WAAW,CAAA;AAAA,KACzG;AAAA;AAGF,EAAA,MAAM,UAAqB,GAAA,WAAA;AAE3B,EAAM,MAAA,QAAA,GAAW,eAAgB,CAAA,UAAA,CAAW,OAAW,IAAA,OAAA;AACvD,EAAA,IAAI,CAAC,MAAA,CAAO,UAAW,CAAA,QAAQ,CAAG,EAAA;AAChC,IAAM,MAAA,IAAIA,+BAAwB,QAAQ,CAAA;AAAA;AAG5C,EAAM,MAAA,UAAA,GAAa,UAAW,CAAA,QAAA,CAAS,QAAQ,CAAA;AAC/C,EAAA,MAAM,aACJ,eAAgB,CAAA,IAAA,KAAS,qBACrB,IAAK,CAAA,KAAA,CAAM,UAAU,CACrB,GAAA,UAAA;AACN,EAAO,OAAA,EAAE,UAAY,EAAA,UAAA,EAAY,QAAS,EAAA;AAC5C;;;;"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var HttpApplicationJsonBodyParser = require('./HttpApplicationJsonBodyParser.cjs.js');
|
|
4
|
+
|
|
5
|
+
const defaultHttpBodyParsers = {
|
|
6
|
+
"application/json": HttpApplicationJsonBodyParser.HttpApplicationJsonBodyParser
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
exports.defaultHttpBodyParsers = defaultHttpBodyParsers;
|
|
10
|
+
//# sourceMappingURL=index.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../../../../src/service/http/body-parser/index.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { HttpBodyParser } from '@backstage/plugin-events-node';\nimport { HttpApplicationJsonBodyParser } from './HttpApplicationJsonBodyParser';\n\nexport const defaultHttpBodyParsers: { [contentType: string]: HttpBodyParser } =\n {\n 'application/json': HttpApplicationJsonBodyParser,\n };\n"],"names":["HttpApplicationJsonBodyParser"],"mappings":";;;;AAkBO,MAAM,sBACX,GAAA;AAAA,EACE,kBAAoB,EAAAA;AACtB;;;;"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var errors = require('@backstage/errors');
|
|
4
|
+
|
|
5
|
+
class UnsupportedCharsetError extends errors.CustomErrorBase {
|
|
6
|
+
name = "UnsupportedCharsetError";
|
|
7
|
+
statusCode = 415;
|
|
8
|
+
constructor(charset) {
|
|
9
|
+
super(`Unsupported charset: ${charset}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
class UnsupportedMediaTypeError extends errors.CustomErrorBase {
|
|
13
|
+
name = "UnsupportedMediaTypeError";
|
|
14
|
+
statusCode = 415;
|
|
15
|
+
constructor(mediaType) {
|
|
16
|
+
super(
|
|
17
|
+
`Unsupported media type: ${mediaType ?? "unknown"}. You need to provide a custom body parser for this media type using the EventsExtensionPoint.`
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
exports.UnsupportedCharsetError = UnsupportedCharsetError;
|
|
23
|
+
exports.UnsupportedMediaTypeError = UnsupportedMediaTypeError;
|
|
24
|
+
//# sourceMappingURL=errors.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.cjs.js","sources":["../../../src/service/http/errors.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { CustomErrorBase } from '@backstage/errors';\n\nexport class UnsupportedCharsetError extends CustomErrorBase {\n name = 'UnsupportedCharsetError' as const;\n statusCode = 415 as const;\n\n constructor(charset: string) {\n super(`Unsupported charset: ${charset}`);\n }\n}\n\nexport class UnsupportedMediaTypeError extends CustomErrorBase {\n name = 'UnsupportedMediaTypeError' as const;\n statusCode = 415 as const;\n\n constructor(mediaType?: string) {\n super(\n `Unsupported media type: ${\n mediaType ?? 'unknown'\n }. You need to provide a custom body parser for this media type using the EventsExtensionPoint.`,\n );\n }\n}\n"],"names":["CustomErrorBase"],"mappings":";;;;AAiBO,MAAM,gCAAgCA,sBAAgB,CAAA;AAAA,EAC3D,IAAO,GAAA,yBAAA;AAAA,EACP,UAAa,GAAA,GAAA;AAAA,EAEb,YAAY,OAAiB,EAAA;AAC3B,IAAM,KAAA,CAAA,CAAA,qBAAA,EAAwB,OAAO,CAAE,CAAA,CAAA;AAAA;AAE3C;AAEO,MAAM,kCAAkCA,sBAAgB,CAAA;AAAA,EAC7D,IAAO,GAAA,2BAAA;AAAA,EACP,UAAa,GAAA,GAAA;AAAA,EAEb,YAAY,SAAoB,EAAA;AAC9B,IAAA,KAAA;AAAA,MACE,CAAA,wBAAA,EACE,aAAa,SACf,CAAA,8FAAA;AAAA,KACF;AAAA;AAEJ;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/plugin-events-backend",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"backstage": {
|
|
5
5
|
"role": "backend-plugin",
|
|
6
6
|
"pluginId": "events",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"types": "./dist/index.d.ts",
|
|
44
44
|
"typesVersions": {
|
|
45
45
|
"*": {
|
|
46
|
-
"
|
|
46
|
+
"*": [
|
|
47
47
|
"dist/index.d.ts"
|
|
48
48
|
],
|
|
49
49
|
"alpha": [
|
|
@@ -67,27 +67,25 @@
|
|
|
67
67
|
"test": "backstage-cli package test"
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
|
-
"@backstage/backend-
|
|
71
|
-
"@backstage/backend-
|
|
72
|
-
"@backstage/
|
|
73
|
-
"@backstage/
|
|
74
|
-
"@backstage/
|
|
75
|
-
"@backstage/
|
|
76
|
-
"@backstage/types": "1.2.1",
|
|
70
|
+
"@backstage/backend-openapi-utils": "^0.5.1",
|
|
71
|
+
"@backstage/backend-plugin-api": "^1.2.1",
|
|
72
|
+
"@backstage/config": "^1.3.2",
|
|
73
|
+
"@backstage/errors": "^1.2.7",
|
|
74
|
+
"@backstage/plugin-events-node": "^0.4.9",
|
|
75
|
+
"@backstage/types": "^1.2.1",
|
|
77
76
|
"@types/express": "^4.17.6",
|
|
78
77
|
"content-type": "^1.0.5",
|
|
79
78
|
"express": "^4.17.1",
|
|
80
79
|
"express-promise-router": "^4.1.0",
|
|
81
|
-
"knex": "^3.0.0"
|
|
82
|
-
"winston": "^3.2.1"
|
|
80
|
+
"knex": "^3.0.0"
|
|
83
81
|
},
|
|
84
82
|
"devDependencies": {
|
|
85
|
-
"@backstage/backend-app-api": "1.2.1
|
|
86
|
-
"@backstage/backend-defaults": "0.8.2
|
|
87
|
-
"@backstage/backend-test-utils": "1.3.1
|
|
88
|
-
"@backstage/cli": "0.
|
|
89
|
-
"@backstage/plugin-events-backend-test-utils": "0.1.42
|
|
90
|
-
"@backstage/repo-tools": "0.13.1
|
|
83
|
+
"@backstage/backend-app-api": "^1.2.1",
|
|
84
|
+
"@backstage/backend-defaults": "^0.8.2",
|
|
85
|
+
"@backstage/backend-test-utils": "^1.3.1",
|
|
86
|
+
"@backstage/cli": "^0.31.0",
|
|
87
|
+
"@backstage/plugin-events-backend-test-utils": "^0.1.42",
|
|
88
|
+
"@backstage/repo-tools": "^0.13.1",
|
|
91
89
|
"@types/content-type": "^1.1.8",
|
|
92
90
|
"supertest": "^7.0.0"
|
|
93
91
|
},
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var pluginEventsNode = require('@backstage/plugin-events-node');
|
|
4
|
-
|
|
5
|
-
class DefaultEventBroker {
|
|
6
|
-
events;
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
* @param logger - logger
|
|
10
|
-
* @param events - replacement that gets wrapped to support not yet migrated implementations.
|
|
11
|
-
* An instance can be passed (required for a mixed mode), otherwise a new instance gets created internally.
|
|
12
|
-
* @deprecated use `DefaultEventsService` directly instead
|
|
13
|
-
*/
|
|
14
|
-
constructor(logger, events) {
|
|
15
|
-
this.events = events ?? pluginEventsNode.DefaultEventsService.create({ logger, useEventBus: "never" });
|
|
16
|
-
}
|
|
17
|
-
async publish(params) {
|
|
18
|
-
return this.events.publish(params);
|
|
19
|
-
}
|
|
20
|
-
subscribe(...subscribers) {
|
|
21
|
-
subscribers.flat().forEach(async (subscriber) => {
|
|
22
|
-
await this.events.subscribe({
|
|
23
|
-
id: subscriber.constructor.name,
|
|
24
|
-
topics: subscriber.supportsEventTopics(),
|
|
25
|
-
onEvent: subscriber.onEvent.bind(subscriber)
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
exports.DefaultEventBroker = DefaultEventBroker;
|
|
32
|
-
//# sourceMappingURL=DefaultEventBroker.cjs.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DefaultEventBroker.cjs.js","sources":["../../src/service/DefaultEventBroker.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport {\n DefaultEventsService,\n EventBroker,\n EventParams,\n EventsService,\n EventSubscriber,\n} from '@backstage/plugin-events-node';\n\n/**\n * In process event broker which will pass the event to all registered subscribers\n * interested in it.\n * Events will not be persisted in any form.\n *\n * @public\n * @deprecated use `DefaultEventsService` from `@backstage/plugin-events-node` instead\n */\nexport class DefaultEventBroker implements EventBroker {\n private readonly events: EventsService;\n\n /**\n *\n * @param logger - logger\n * @param events - replacement that gets wrapped to support not yet migrated implementations.\n * An instance can be passed (required for a mixed mode), otherwise a new instance gets created internally.\n * @deprecated use `DefaultEventsService` directly instead\n */\n constructor(logger: LoggerService, events?: EventsService) {\n this.events =\n events ?? DefaultEventsService.create({ logger, useEventBus: 'never' });\n }\n\n async publish(params: EventParams): Promise<void> {\n return this.events.publish(params);\n }\n\n subscribe(\n ...subscribers: Array<EventSubscriber | Array<EventSubscriber>>\n ): void {\n subscribers.flat().forEach(async subscriber => {\n await this.events.subscribe({\n id: subscriber.constructor.name,\n topics: subscriber.supportsEventTopics(),\n onEvent: subscriber.onEvent.bind(subscriber),\n });\n });\n }\n}\n"],"names":["DefaultEventsService"],"mappings":";;;;AAiCO,MAAM,kBAA0C,CAAA;AAAA,EACpC,MAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASjB,WAAA,CAAY,QAAuB,MAAwB,EAAA;AACzD,IAAK,IAAA,CAAA,MAAA,GACH,UAAUA,qCAAqB,CAAA,MAAA,CAAO,EAAE,MAAQ,EAAA,WAAA,EAAa,SAAS,CAAA;AAAA;AAC1E,EAEA,MAAM,QAAQ,MAAoC,EAAA;AAChD,IAAO,OAAA,IAAA,CAAK,MAAO,CAAA,OAAA,CAAQ,MAAM,CAAA;AAAA;AACnC,EAEA,aACK,WACG,EAAA;AACN,IAAA,WAAA,CAAY,IAAK,EAAA,CAAE,OAAQ,CAAA,OAAM,UAAc,KAAA;AAC7C,MAAM,MAAA,IAAA,CAAK,OAAO,SAAU,CAAA;AAAA,QAC1B,EAAA,EAAI,WAAW,WAAY,CAAA,IAAA;AAAA,QAC3B,MAAA,EAAQ,WAAW,mBAAoB,EAAA;AAAA,QACvC,OAAS,EAAA,UAAA,CAAW,OAAQ,CAAA,IAAA,CAAK,UAAU;AAAA,OAC5C,CAAA;AAAA,KACF,CAAA;AAAA;AAEL;;;;"}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var DefaultEventBroker = require('./DefaultEventBroker.cjs.js');
|
|
4
|
-
|
|
5
|
-
class EventsBackend {
|
|
6
|
-
eventBroker;
|
|
7
|
-
publishers = [];
|
|
8
|
-
subscribers = [];
|
|
9
|
-
constructor(logger) {
|
|
10
|
-
this.eventBroker = new DefaultEventBroker.DefaultEventBroker(logger);
|
|
11
|
-
}
|
|
12
|
-
setEventBroker(eventBroker) {
|
|
13
|
-
this.eventBroker = eventBroker;
|
|
14
|
-
return this;
|
|
15
|
-
}
|
|
16
|
-
addPublishers(...publishers) {
|
|
17
|
-
this.publishers.push(...publishers.flat());
|
|
18
|
-
return this;
|
|
19
|
-
}
|
|
20
|
-
addSubscribers(...subscribers) {
|
|
21
|
-
this.subscribers.push(...subscribers.flat());
|
|
22
|
-
return this;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Wires up and returns all component parts of the event management.
|
|
26
|
-
*/
|
|
27
|
-
async start() {
|
|
28
|
-
this.eventBroker.subscribe(this.subscribers);
|
|
29
|
-
this.publishers.forEach(
|
|
30
|
-
(publisher) => publisher.setEventBroker(this.eventBroker)
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
exports.EventsBackend = EventsBackend;
|
|
36
|
-
//# sourceMappingURL=EventsBackend.cjs.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"EventsBackend.cjs.js","sources":["../../src/service/EventsBackend.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n EventBroker,\n EventPublisher,\n EventSubscriber,\n} from '@backstage/plugin-events-node';\nimport { Logger } from 'winston';\nimport { DefaultEventBroker } from './DefaultEventBroker';\n\n/**\n * A builder that helps wire up all component parts of the event management.\n *\n * @public\n * @deprecated `EventBroker`, `EventPublisher`, and `EventSubscriber` got replaced by `EventsService` and its methods.\n */\nexport class EventsBackend {\n private eventBroker: EventBroker;\n private publishers: EventPublisher[] = [];\n private subscribers: EventSubscriber[] = [];\n\n constructor(logger: Logger) {\n this.eventBroker = new DefaultEventBroker(logger);\n }\n\n setEventBroker(eventBroker: EventBroker): EventsBackend {\n this.eventBroker = eventBroker;\n return this;\n }\n\n addPublishers(\n ...publishers: Array<EventPublisher | Array<EventPublisher>>\n ): EventsBackend {\n this.publishers.push(...publishers.flat());\n return this;\n }\n\n addSubscribers(\n ...subscribers: Array<EventSubscriber | Array<EventSubscriber>>\n ): EventsBackend {\n this.subscribers.push(...subscribers.flat());\n return this;\n }\n\n /**\n * Wires up and returns all component parts of the event management.\n */\n async start(): Promise<void> {\n this.eventBroker.subscribe(this.subscribers);\n this.publishers.forEach(publisher =>\n publisher.setEventBroker(this.eventBroker),\n );\n }\n}\n"],"names":["DefaultEventBroker"],"mappings":";;;;AA8BO,MAAM,aAAc,CAAA;AAAA,EACjB,WAAA;AAAA,EACA,aAA+B,EAAC;AAAA,EAChC,cAAiC,EAAC;AAAA,EAE1C,YAAY,MAAgB,EAAA;AAC1B,IAAK,IAAA,CAAA,WAAA,GAAc,IAAIA,qCAAA,CAAmB,MAAM,CAAA;AAAA;AAClD,EAEA,eAAe,WAAyC,EAAA;AACtD,IAAA,IAAA,CAAK,WAAc,GAAA,WAAA;AACnB,IAAO,OAAA,IAAA;AAAA;AACT,EAEA,iBACK,UACY,EAAA;AACf,IAAA,IAAA,CAAK,UAAW,CAAA,IAAA,CAAK,GAAG,UAAA,CAAW,MAAM,CAAA;AACzC,IAAO,OAAA,IAAA;AAAA;AACT,EAEA,kBACK,WACY,EAAA;AACf,IAAA,IAAA,CAAK,WAAY,CAAA,IAAA,CAAK,GAAG,WAAA,CAAY,MAAM,CAAA;AAC3C,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA,EAKA,MAAM,KAAuB,GAAA;AAC3B,IAAK,IAAA,CAAA,WAAA,CAAY,SAAU,CAAA,IAAA,CAAK,WAAW,CAAA;AAC3C,IAAA,IAAA,CAAK,UAAW,CAAA,OAAA;AAAA,MAAQ,CACtB,SAAA,KAAA,SAAA,CAAU,cAAe,CAAA,IAAA,CAAK,WAAW;AAAA,KAC3C;AAAA;AAEJ;;;;"}
|