@backstage/plugin-events-backend 0.3.12 → 0.3.13-next.1

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.
Files changed (27) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/alpha/package.json +1 -1
  3. package/dist/alpha.cjs.js +2 -71
  4. package/dist/alpha.cjs.js.map +1 -1
  5. package/dist/index.cjs.js +5 -61
  6. package/dist/index.cjs.js.map +1 -1
  7. package/dist/schema/openapi.generated.cjs.js +272 -0
  8. package/dist/schema/openapi.generated.cjs.js.map +1 -0
  9. package/dist/service/DefaultEventBroker.cjs.js +32 -0
  10. package/dist/service/DefaultEventBroker.cjs.js.map +1 -0
  11. package/dist/service/EventsBackend.cjs.js +36 -0
  12. package/dist/service/EventsBackend.cjs.js.map +1 -0
  13. package/dist/service/EventsPlugin.cjs.js +98 -0
  14. package/dist/service/EventsPlugin.cjs.js.map +1 -0
  15. package/dist/{cjs/HttpPostIngressEventPublisher-D6pQ6awS.cjs.js → service/http/HttpPostIngressEventPublisher.cjs.js} +3 -18
  16. package/dist/service/http/HttpPostIngressEventPublisher.cjs.js.map +1 -0
  17. package/dist/service/http/validation/RequestValidationContextImpl.cjs.js +20 -0
  18. package/dist/service/http/validation/RequestValidationContextImpl.cjs.js.map +1 -0
  19. package/dist/service/hub/DatabaseEventBusStore.cjs.js +410 -0
  20. package/dist/service/hub/DatabaseEventBusStore.cjs.js.map +1 -0
  21. package/dist/service/hub/MemoryEventBusStore.cjs.js +106 -0
  22. package/dist/service/hub/MemoryEventBusStore.cjs.js.map +1 -0
  23. package/dist/service/hub/createEventBusRouter.cjs.js +130 -0
  24. package/dist/service/hub/createEventBusRouter.cjs.js.map +1 -0
  25. package/migrations/20240523100528_init.js +98 -0
  26. package/package.json +17 -8
  27. package/dist/cjs/HttpPostIngressEventPublisher-D6pQ6awS.cjs.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DatabaseEventBusStore.cjs.js","sources":["../../../src/service/hub/DatabaseEventBusStore.ts"],"sourcesContent":["/*\n * Copyright 2024 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 { EventParams } from '@backstage/plugin-events-node';\nimport { EventBusStore } from './types';\nimport { Knex } from 'knex';\nimport {\n BackstageCredentials,\n BackstageServicePrincipal,\n DatabaseService,\n LifecycleService,\n LoggerService,\n SchedulerService,\n resolvePackagePath,\n} from '@backstage/backend-plugin-api';\nimport { ForwardedError, NotFoundError } from '@backstage/errors';\nimport { HumanDuration, durationToMilliseconds } from '@backstage/types';\n\nconst WINDOW_MAX_COUNT_DEFAULT = 10_000;\nconst WINDOW_MIN_AGE_DEFAULT = { minutes: 10 };\nconst WINDOW_MAX_AGE_DEFAULT = { days: 1 };\n\nconst MAX_BATCH_SIZE = 10;\nconst LISTENER_CONNECTION_TIMEOUT_MS = 60_000;\nconst KEEPALIVE_INTERVAL_MS = 60_000;\n\nconst TABLE_EVENTS = 'event_bus_events';\nconst TABLE_SUBSCRIPTIONS = 'event_bus_subscriptions';\nconst TOPIC_PUBLISH = 'event_bus_publish';\n\ntype EventsRow = {\n id: string;\n created_by: string;\n created_at: Date;\n topic: string;\n data_json: string;\n notified_subscribers: string[];\n};\n\ntype SubscriptionsRow = {\n id: string;\n created_by: string;\n created_at: Date;\n updated_at: Date;\n read_until: string;\n topics: string[];\n};\n\nfunction creatorId(\n credentials: BackstageCredentials<BackstageServicePrincipal>,\n) {\n return `service=${credentials.principal.subject}`;\n}\n\nconst migrationsDir = resolvePackagePath(\n '@backstage/plugin-events-backend',\n 'migrations',\n);\n\ninterface InternalDbClient {\n acquireRawConnection(): Promise<InternalDbConnection>;\n destroyRawConnection(conn: InternalDbConnection): Promise<void>;\n}\n\ninterface InternalDbConnection {\n query(sql: string): Promise<void>;\n end(): Promise<void>;\n on(\n event: 'notification',\n listener: (event: { channel: string; payload: string }) => void,\n ): void;\n on(event: 'error', listener: (error: Error) => void): void;\n on(event: 'end', listener: (error?: Error) => void): void;\n removeAllListeners(): void;\n}\n\n// This internal class manages a single connection to the database that all listeners share\nclass DatabaseEventBusListener {\n readonly #client: InternalDbClient;\n readonly #logger: LoggerService;\n\n readonly #listeners = new Set<{\n topics: Set<string>;\n resolve: (result: { topic: string }) => void;\n reject: (error: Error) => void;\n }>();\n\n #isShuttingDown = false;\n #connPromise?: Promise<InternalDbConnection>;\n #connTimeout?: NodeJS.Timeout;\n #keepaliveInterval?: NodeJS.Timeout;\n\n constructor(client: InternalDbClient, logger: LoggerService) {\n this.#client = client;\n this.#logger = logger.child({ type: 'DatabaseEventBusListener' });\n }\n\n async setupListener(\n topics: Set<string>,\n signal: AbortSignal,\n ): Promise<{ waitForUpdate(): Promise<{ topic: string }> }> {\n if (this.#connTimeout) {\n clearTimeout(this.#connTimeout);\n this.#connTimeout = undefined;\n }\n\n await this.#ensureConnection();\n\n const updatePromise = new Promise<{ topic: string }>((resolve, reject) => {\n const listener = {\n topics,\n resolve(result: { topic: string }) {\n resolve(result);\n cleanup();\n },\n reject(err: Error) {\n reject(err);\n cleanup();\n },\n };\n this.#listeners.add(listener);\n\n const onAbort = () => {\n this.#listeners.delete(listener);\n this.#maybeTimeoutConnection();\n reject(signal.reason);\n cleanup();\n };\n\n function cleanup() {\n signal.removeEventListener('abort', onAbort);\n }\n\n signal.addEventListener('abort', onAbort);\n });\n\n // Ignore unhandled rejections\n updatePromise.catch(() => {});\n\n return { waitForUpdate: () => updatePromise };\n }\n\n async shutdown() {\n if (this.#isShuttingDown) {\n return;\n }\n this.#isShuttingDown = true;\n const conn = await this.#connPromise?.catch(() => undefined);\n if (conn) {\n this.#destroyConnection(conn);\n }\n }\n\n #handleNotify(topic: string) {\n this.#logger.debug(`Listener received notification for topic '${topic}'`);\n for (const l of this.#listeners) {\n if (l.topics.has(topic)) {\n l.resolve({ topic });\n this.#listeners.delete(l);\n }\n }\n this.#maybeTimeoutConnection();\n }\n\n // We don't try to reconnect on error, instead we notify all listeners and let\n // them try to establish a new connection\n #handleError(error: Error) {\n this.#logger.error(\n `Listener connection failed, notifying all listeners`,\n error,\n );\n for (const l of this.#listeners) {\n l.reject(new Error('Listener connection failed'));\n }\n this.#listeners.clear();\n this.#maybeTimeoutConnection();\n }\n\n #maybeTimeoutConnection() {\n // If we don't have any listeners, destroy the connection after a timeout\n if (this.#listeners.size === 0 && !this.#connTimeout) {\n this.#connTimeout = setTimeout(() => {\n this.#connTimeout = undefined;\n this.#connPromise?.then(conn => {\n this.#logger.info('Listener connection timed out, destroying');\n this.#connPromise = undefined;\n this.#destroyConnection(conn);\n });\n }, LISTENER_CONNECTION_TIMEOUT_MS);\n }\n }\n\n #destroyConnection(conn: InternalDbConnection) {\n if (this.#keepaliveInterval) {\n clearInterval(this.#keepaliveInterval);\n this.#keepaliveInterval = undefined;\n }\n this.#client.destroyRawConnection(conn).catch(error => {\n this.#logger.error(`Listener failed to destroy connection`, error);\n });\n conn.removeAllListeners();\n }\n\n async #ensureConnection() {\n if (this.#isShuttingDown) {\n throw new Error('Listener is shutting down');\n }\n if (this.#connPromise) {\n await this.#connPromise;\n return;\n }\n this.#connPromise = Promise.resolve().then(async () => {\n const conn = await this.#client.acquireRawConnection();\n\n try {\n await conn.query(`LISTEN ${TOPIC_PUBLISH}`);\n\n // Set up a keepalive interval to make sure the connection stays alive\n if (this.#keepaliveInterval) {\n clearInterval(this.#keepaliveInterval);\n }\n this.#keepaliveInterval = setInterval(() => {\n conn.query('select 1').catch(error => {\n this.#connPromise = undefined;\n this.#destroyConnection(conn);\n this.#handleError(new ForwardedError('Keepalive failed', error));\n });\n }, KEEPALIVE_INTERVAL_MS);\n\n conn.on('notification', event => {\n this.#handleNotify(event.payload);\n });\n conn.on('error', error => {\n this.#connPromise = undefined;\n this.#destroyConnection(conn);\n this.#handleError(error);\n });\n conn.on('end', error => {\n this.#connPromise = undefined;\n this.#destroyConnection(conn);\n this.#handleError(\n error ?? new Error('Connection ended unexpectedly'),\n );\n });\n return conn;\n } catch (error) {\n this.#destroyConnection(conn);\n throw error;\n }\n });\n try {\n await this.#connPromise;\n } catch (error) {\n this.#connPromise = undefined;\n throw error;\n }\n }\n}\n\nexport class DatabaseEventBusStore implements EventBusStore {\n static async create(options: {\n database: DatabaseService;\n logger: LoggerService;\n scheduler: SchedulerService;\n lifecycle: LifecycleService;\n window?: {\n /** Events within this range will never be deleted */\n minAge?: HumanDuration;\n /** Events outside of this age will always be deleted */\n maxAge?: HumanDuration;\n /** Events outside of this count will be deleted if they are outside the minAge window */\n maxCount?: number;\n };\n }): Promise<DatabaseEventBusStore> {\n const db = await options.database.getClient();\n\n if (db.client.config.client !== 'pg') {\n throw new Error(\n `DatabaseEventBusStore only supports PostgreSQL, got '${db.client.config.client}'`,\n );\n }\n\n if (!options.database.migrations?.skip) {\n await db.migrate.latest({\n directory: migrationsDir,\n });\n }\n\n const listener = new DatabaseEventBusListener(db.client, options.logger);\n\n const store = new DatabaseEventBusStore(\n db,\n options.logger,\n listener,\n options.window?.maxCount ?? WINDOW_MAX_COUNT_DEFAULT,\n durationToMilliseconds(options.window?.minAge ?? WINDOW_MIN_AGE_DEFAULT),\n durationToMilliseconds(options.window?.maxAge ?? WINDOW_MAX_AGE_DEFAULT),\n );\n\n await options.scheduler.scheduleTask({\n id: 'event-bus-cleanup',\n frequency: { seconds: 10 },\n timeout: { minutes: 1 },\n initialDelay: { seconds: 10 },\n fn: () => store.#cleanup(),\n });\n\n options.lifecycle.addShutdownHook(async () => {\n await listener.shutdown();\n });\n\n return store;\n }\n\n /** @internal */\n static async forTest({\n db,\n logger,\n minAge = 0,\n maxAge = 10_000,\n }: {\n db: Knex;\n logger: LoggerService;\n minAge?: number;\n maxAge?: number;\n }) {\n await db.migrate.latest({ directory: migrationsDir });\n\n const store = new DatabaseEventBusStore(\n db,\n logger,\n new DatabaseEventBusListener(db.client, logger),\n 5,\n minAge,\n maxAge,\n );\n\n return Object.assign(store, { clean: () => store.#cleanup() });\n }\n\n readonly #db: Knex;\n readonly #logger: LoggerService;\n readonly #listener: DatabaseEventBusListener;\n readonly #windowMaxCount: number;\n readonly #windowMinAge: number;\n readonly #windowMaxAge: number;\n\n private constructor(\n db: Knex,\n logger: LoggerService,\n listener: DatabaseEventBusListener,\n windowMaxCount: number,\n windowMinAge: number,\n windowMaxAge: number,\n ) {\n this.#db = db;\n this.#logger = logger;\n this.#listener = listener;\n this.#windowMaxCount = windowMaxCount;\n this.#windowMinAge = windowMinAge;\n this.#windowMaxAge = windowMaxAge;\n }\n\n async publish(options: {\n event: EventParams;\n notifiedSubscribers?: string[];\n credentials: BackstageCredentials<BackstageServicePrincipal>;\n }): Promise<{ eventId: string } | undefined> {\n const topic = options.event.topic;\n const notifiedSubscribers = options.notifiedSubscribers ?? [];\n // This query inserts a new event into the database, but only if there are\n // subscribers to the topic that have not already been notified\n const result = await this.#db\n // There's no clean way to create a INSERT INTO .. SELECT with knex, so we end up with quite a lot of .raw(...)\n .into(\n this.#db.raw('?? (??, ??, ??, ??)', [\n TABLE_EVENTS,\n // These are the rows that we insert, and should match the SELECT below\n 'created_by',\n 'topic',\n 'data_json',\n 'notified_subscribers',\n ]),\n )\n .insert<EventsRow>(\n (q: Knex.QueryBuilder) =>\n q\n // We're not reading data to insert from anywhere else, just raw data\n .select(\n this.#db.raw('?', [creatorId(options.credentials)]),\n this.#db.raw('?', [topic]),\n this.#db.raw('?', [\n JSON.stringify({\n payload: options.event.eventPayload,\n metadata: options.event.metadata,\n }),\n ]),\n this.#db.raw('?', [notifiedSubscribers]),\n )\n // The rest of this query is to check whether there are any\n // subscribers that have not been notified yet\n .from(TABLE_SUBSCRIPTIONS)\n .whereNotIn('id', notifiedSubscribers) // Skip notified subscribers\n .andWhere(this.#db.raw('? = ANY(topics)', [topic])) // Match topic\n .having(this.#db.raw('count(*)'), '>', 0), // Check if there are any results\n )\n .returning<{ id: string }[]>('id');\n\n if (result.length === 0) {\n return undefined;\n }\n if (result.length > 1) {\n throw new Error(\n `Failed to insert event, unexpectedly updated ${result.length} rows`,\n );\n }\n\n const [{ id }] = result;\n\n // Notify other event bus instances that an event is available on the topic\n const notifyResult = await this.#db.select(\n this.#db.raw(`pg_notify(?, ?)`, [TOPIC_PUBLISH, topic]),\n );\n if (notifyResult?.length !== 1) {\n this.#logger.warn(\n `Failed to notify subscribers of event with ID '${id}' on topic '${topic}'`,\n );\n }\n\n return { eventId: id };\n }\n\n async upsertSubscription(\n id: string,\n topics: string[],\n credentials: BackstageCredentials<BackstageServicePrincipal>,\n ): Promise<void> {\n const [{ max: maxId }] = await this.#db(TABLE_EVENTS).max('id');\n const result = await this.#db<SubscriptionsRow>(TABLE_SUBSCRIPTIONS)\n .insert({\n id,\n created_by: creatorId(credentials),\n updated_at: this.#db.fn.now(),\n topics,\n read_until: maxId || 0,\n })\n .onConflict('id')\n .merge(['created_by', 'topics', 'updated_at'])\n .returning('*');\n\n if (result.length !== 1) {\n throw new Error(\n `Failed to upsert subscription, updated ${result.length} rows`,\n );\n }\n }\n\n async readSubscription(id: string): Promise<{ events: EventParams[] }> {\n // The below query selects the subscription we're reading from, locks it for\n // an update, reads events for the subscription up to the limit, and then\n // updates the pointer to the last read event.\n //\n // This is written as a plain SQL query to spare us all the horrors of\n // expressing this in knex.\n\n const { rows: result } = await this.#db.raw<{\n rows: [] | [{ events: EventsRow[] }];\n }>(\n `\n WITH subscription AS (\n SELECT topics, read_until\n FROM event_bus_subscriptions\n WHERE id = :id\n FOR UPDATE\n ),\n selected_events AS (\n SELECT event_bus_events.*\n FROM event_bus_events\n INNER JOIN subscription\n ON event_bus_events.topic = ANY(subscription.topics)\n WHERE event_bus_events.id > subscription.read_until\n AND NOT :id = ANY(event_bus_events.notified_subscribers)\n ORDER BY event_bus_events.id ASC LIMIT :limit\n ),\n last_event_id AS (\n SELECT max(id) AS last_event_id\n FROM selected_events\n ),\n events_array AS (\n SELECT json_agg(row_to_json(selected_events)) AS events\n FROM selected_events\n )\n UPDATE event_bus_subscriptions\n SET read_until = COALESCE(last_event_id, (SELECT MAX(id) FROM event_bus_events), 0)\n FROM events_array, last_event_id\n WHERE event_bus_subscriptions.id = :id\n RETURNING events_array.events\n `,\n { id, limit: MAX_BATCH_SIZE },\n );\n\n if (result.length === 0) {\n throw new NotFoundError(`Subscription with ID '${id}' not found`);\n } else if (result.length > 1) {\n throw new Error(\n `Failed to read subscription, unexpectedly updated ${result.length} rows`,\n );\n }\n\n const rows = result[0].events;\n if (!rows || rows.length === 0) {\n return { events: [] };\n }\n\n return {\n events: rows.map(row => {\n const { payload, metadata } = JSON.parse(row.data_json);\n return {\n topic: row.topic,\n eventPayload: payload,\n metadata,\n };\n }),\n };\n }\n\n async setupListener(\n subscriptionId: string,\n options: {\n signal: AbortSignal;\n },\n ): Promise<{ waitForUpdate(): Promise<{ topic: string }> }> {\n const result = await this.#db<SubscriptionsRow>(TABLE_SUBSCRIPTIONS)\n .select('topics')\n .where({ id: subscriptionId })\n .first();\n\n if (!result) {\n throw new NotFoundError(\n `Subscription with ID '${subscriptionId}' not found`,\n );\n }\n\n options.signal.throwIfAborted();\n\n return this.#listener.setupListener(\n new Set(result.topics ?? []),\n options.signal,\n );\n }\n\n async #cleanup() {\n try {\n const eventCount = await this.#db(TABLE_EVENTS)\n .delete()\n // Delete any events that are outside both the min age and size window\n .orWhere(inner =>\n inner\n .whereIn(\n 'id',\n this.#db\n .select('id')\n .from(TABLE_EVENTS)\n .orderBy('id', 'desc')\n .offset(this.#windowMaxCount),\n )\n .andWhere(\n 'created_at',\n '<',\n new Date(Date.now() - this.#windowMinAge),\n ),\n )\n // If events are outside the max age they will always be deleted\n .orWhere('created_at', '<', new Date(Date.now() - this.#windowMaxAge));\n\n if (eventCount > 0) {\n this.#logger.info(\n `Event cleanup resulted in ${eventCount} old events being deleted`,\n );\n }\n } catch (error) {\n this.#logger.error('Event cleanup failed', error);\n }\n\n try {\n // Delete any subscribers that aren't keeping up with current events\n const [{ min: minId }] = await this.#db(TABLE_EVENTS).min('id');\n\n let subscriberCount;\n if (minId === null) {\n // No events left, remove all subscribers. This can happen if no events\n // are published within the max age window.\n subscriberCount = await this.#db(TABLE_SUBSCRIPTIONS).delete();\n } else {\n subscriberCount = await this.#db(TABLE_SUBSCRIPTIONS)\n .delete()\n // Read pointer points to the ID that has been read, so we need an additional offset\n .where('read_until', '<', minId - 1);\n }\n\n if (subscriberCount > 0) {\n this.#logger.info(\n `Subscription cleanup resulted in ${subscriberCount} stale subscribers being deleted`,\n );\n }\n } catch (error) {\n this.#logger.error('Subscription cleanup failed', error);\n }\n }\n}\n"],"names":["resolvePackagePath","ForwardedError","durationToMilliseconds","NotFoundError"],"mappings":";;;;;;AA8BA,MAAM,wBAA2B,GAAA,GAAA,CAAA;AACjC,MAAM,sBAAA,GAAyB,EAAE,OAAA,EAAS,EAAG,EAAA,CAAA;AAC7C,MAAM,sBAAA,GAAyB,EAAE,IAAA,EAAM,CAAE,EAAA,CAAA;AAEzC,MAAM,cAAiB,GAAA,EAAA,CAAA;AACvB,MAAM,8BAAiC,GAAA,GAAA,CAAA;AACvC,MAAM,qBAAwB,GAAA,GAAA,CAAA;AAE9B,MAAM,YAAe,GAAA,kBAAA,CAAA;AACrB,MAAM,mBAAsB,GAAA,yBAAA,CAAA;AAC5B,MAAM,aAAgB,GAAA,mBAAA,CAAA;AAoBtB,SAAS,UACP,WACA,EAAA;AACA,EAAO,OAAA,CAAA,QAAA,EAAW,WAAY,CAAA,SAAA,CAAU,OAAO,CAAA,CAAA,CAAA;AACjD,CAAA;AAEA,MAAM,aAAgB,GAAAA,mCAAA;AAAA,EACpB,kCAAA;AAAA,EACA,YAAA;AACF,CAAA,CAAA;AAoBA,MAAM,wBAAyB,C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Q,KAAM,CAAA,6BAAA,EAA+B,KAAK,CAAA,CAAA;AAAA,KACzD;AAAA,GACF;AACF;;;;"}
@@ -0,0 +1,106 @@
1
+ 'use strict';
2
+
3
+ var errors = require('@backstage/errors');
4
+
5
+ const MAX_BATCH_SIZE = 10;
6
+ const MAX_EVENTS_DEFAULT = 1e3;
7
+ class MemoryEventBusStore {
8
+ #maxEvents;
9
+ #events = new Array();
10
+ #subscribers = /* @__PURE__ */ new Map();
11
+ #listeners = /* @__PURE__ */ new Set();
12
+ constructor(options = {}) {
13
+ this.#maxEvents = options.maxEvents ?? MAX_EVENTS_DEFAULT;
14
+ }
15
+ async publish(options) {
16
+ const topic = options.event.topic;
17
+ const notifiedSubscribers = new Set(options.notifiedSubscribers);
18
+ let hasOtherSubscribers = false;
19
+ for (const sub of this.#subscribers.values()) {
20
+ if (sub.topics.has(topic) && !notifiedSubscribers.has(sub.id)) {
21
+ hasOtherSubscribers = true;
22
+ break;
23
+ }
24
+ }
25
+ if (!hasOtherSubscribers) {
26
+ return void 0;
27
+ }
28
+ const nextSeq = this.#getMaxSeq() + 1;
29
+ this.#events.push({ ...options.event, notifiedSubscribers, seq: nextSeq });
30
+ for (const listener of this.#listeners) {
31
+ if (listener.topics.has(topic)) {
32
+ listener.resolve({ topic });
33
+ this.#listeners.delete(listener);
34
+ }
35
+ }
36
+ if (this.#events.length > this.#maxEvents) {
37
+ this.#events.shift();
38
+ }
39
+ return { eventId: String(nextSeq) };
40
+ }
41
+ #getMaxSeq() {
42
+ return this.#events[this.#events.length - 1]?.seq ?? 0;
43
+ }
44
+ async upsertSubscription(id, topics) {
45
+ const existing = this.#subscribers.get(id);
46
+ if (existing) {
47
+ existing.topics = new Set(topics);
48
+ return;
49
+ }
50
+ const sub = {
51
+ id,
52
+ seq: this.#getMaxSeq(),
53
+ topics: new Set(topics)
54
+ };
55
+ this.#subscribers.set(id, sub);
56
+ }
57
+ async readSubscription(id) {
58
+ const sub = this.#subscribers.get(id);
59
+ if (!sub) {
60
+ throw new errors.NotFoundError(`Subscription not found`);
61
+ }
62
+ const events = this.#events.filter(
63
+ (event) => event.seq > sub.seq && sub.topics.has(event.topic) && !event.notifiedSubscribers.has(id)
64
+ ).slice(0, MAX_BATCH_SIZE);
65
+ sub.seq = events[events.length - 1]?.seq ?? sub.seq;
66
+ return {
67
+ events: events.map(({ topic, eventPayload }) => ({
68
+ topic,
69
+ eventPayload
70
+ }))
71
+ };
72
+ }
73
+ async setupListener(subscriptionId, options) {
74
+ return {
75
+ waitForUpdate: async () => {
76
+ options.signal.throwIfAborted();
77
+ const sub = this.#subscribers.get(subscriptionId);
78
+ if (!sub) {
79
+ throw new errors.NotFoundError(`Subscription not found`);
80
+ }
81
+ return new Promise((resolve, reject) => {
82
+ const listener = {
83
+ topics: sub.topics,
84
+ resolve(result) {
85
+ resolve(result);
86
+ cleanup();
87
+ }
88
+ };
89
+ this.#listeners.add(listener);
90
+ const onAbort = () => {
91
+ this.#listeners.delete(listener);
92
+ reject(options.signal.reason);
93
+ cleanup();
94
+ };
95
+ function cleanup() {
96
+ options.signal.removeEventListener("abort", onAbort);
97
+ }
98
+ options.signal.addEventListener("abort", onAbort);
99
+ });
100
+ }
101
+ };
102
+ }
103
+ }
104
+
105
+ exports.MemoryEventBusStore = MemoryEventBusStore;
106
+ //# sourceMappingURL=MemoryEventBusStore.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MemoryEventBusStore.cjs.js","sources":["../../../src/service/hub/MemoryEventBusStore.ts"],"sourcesContent":["/*\n * Copyright 2024 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 { EventParams } from '@backstage/plugin-events-node';\nimport { EventBusStore } from './types';\nimport { NotFoundError } from '@backstage/errors';\nimport {\n BackstageCredentials,\n BackstageServicePrincipal,\n} from '@backstage/backend-plugin-api';\n\nconst MAX_BATCH_SIZE = 10;\nconst MAX_EVENTS_DEFAULT = 1_000;\n\nexport class MemoryEventBusStore implements EventBusStore {\n #maxEvents: number;\n #events = new Array<\n EventParams & { seq: number; notifiedSubscribers: Set<string> }\n >();\n #subscribers = new Map<\n string,\n { id: string; seq: number; topics: Set<string> }\n >();\n #listeners = new Set<{\n topics: Set<string>;\n resolve(result: { topic: string }): void;\n }>();\n\n constructor(options: { maxEvents?: number } = {}) {\n this.#maxEvents = options.maxEvents ?? MAX_EVENTS_DEFAULT;\n }\n\n async publish(options: {\n event: EventParams;\n notifiedSubscribers: string[];\n credentials: BackstageCredentials<BackstageServicePrincipal>;\n }): Promise<{ eventId: string } | undefined> {\n const topic = options.event.topic;\n const notifiedSubscribers = new Set(options.notifiedSubscribers);\n\n let hasOtherSubscribers = false;\n for (const sub of this.#subscribers.values()) {\n if (sub.topics.has(topic) && !notifiedSubscribers.has(sub.id)) {\n hasOtherSubscribers = true;\n break;\n }\n }\n if (!hasOtherSubscribers) {\n return undefined;\n }\n\n const nextSeq = this.#getMaxSeq() + 1;\n this.#events.push({ ...options.event, notifiedSubscribers, seq: nextSeq });\n\n for (const listener of this.#listeners) {\n if (listener.topics.has(topic)) {\n listener.resolve({ topic });\n this.#listeners.delete(listener);\n }\n }\n\n // Trim old events\n if (this.#events.length > this.#maxEvents) {\n this.#events.shift();\n }\n\n return { eventId: String(nextSeq) };\n }\n\n #getMaxSeq() {\n return this.#events[this.#events.length - 1]?.seq ?? 0;\n }\n\n async upsertSubscription(id: string, topics: string[]): Promise<void> {\n const existing = this.#subscribers.get(id);\n if (existing) {\n existing.topics = new Set(topics);\n return;\n }\n const sub = {\n id: id,\n seq: this.#getMaxSeq(),\n topics: new Set(topics),\n };\n this.#subscribers.set(id, sub);\n }\n\n async readSubscription(id: string): Promise<{ events: EventParams[] }> {\n const sub = this.#subscribers.get(id);\n if (!sub) {\n throw new NotFoundError(`Subscription not found`);\n }\n const events = this.#events\n .filter(\n event =>\n event.seq > sub.seq &&\n sub.topics.has(event.topic) &&\n !event.notifiedSubscribers.has(id),\n )\n .slice(0, MAX_BATCH_SIZE);\n\n sub.seq = events[events.length - 1]?.seq ?? sub.seq;\n\n return {\n events: events.map(({ topic, eventPayload }) => ({\n topic,\n eventPayload,\n })),\n };\n }\n\n async setupListener(\n subscriptionId: string,\n options: {\n signal: AbortSignal;\n },\n ): Promise<{ waitForUpdate(): Promise<{ topic: string }> }> {\n return {\n waitForUpdate: async () => {\n options.signal.throwIfAborted();\n\n const sub = this.#subscribers.get(subscriptionId);\n if (!sub) {\n throw new NotFoundError(`Subscription not found`);\n }\n\n return new Promise<{ topic: string }>((resolve, reject) => {\n const listener = {\n topics: sub.topics,\n resolve(result: { topic: string }) {\n resolve(result);\n cleanup();\n },\n };\n this.#listeners.add(listener);\n\n const onAbort = () => {\n this.#listeners.delete(listener);\n reject(options.signal.reason);\n cleanup();\n };\n\n function cleanup() {\n options.signal.removeEventListener('abort', onAbort);\n }\n\n options.signal.addEventListener('abort', onAbort);\n });\n },\n };\n }\n}\n"],"names":["NotFoundError"],"mappings":";;;;AAuBA,MAAM,cAAiB,GAAA,EAAA,CAAA;AACvB,MAAM,kBAAqB,GAAA,GAAA,CAAA;AAEpB,MAAM,mBAA6C,CAAA;AAAA,EACxD,UAAA,CAAA;AAAA,EACA,OAAA,GAAU,IAAI,KAEZ,EAAA,CAAA;AAAA,EACF,YAAA,uBAAmB,GAGjB,EAAA,CAAA;AAAA,EACF,UAAA,uBAAiB,GAGd,EAAA,CAAA;AAAA,EAEH,WAAA,CAAY,OAAkC,GAAA,EAAI,EAAA;AAChD,IAAK,IAAA,CAAA,UAAA,GAAa,QAAQ,SAAa,IAAA,kBAAA,CAAA;AAAA,GACzC;AAAA,EAEA,MAAM,QAA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jB,YAAQ,OAAA,CAAA,MAAA,CAAO,mBAAoB,CAAA,OAAA,EAAS,OAAO,CAAA,CAAA;AAAA,WACrD;AAEA,UAAQ,OAAA,CAAA,MAAA,CAAO,gBAAiB,CAAA,OAAA,EAAS,OAAO,CAAA,CAAA;AAAA,SACjD,CAAA,CAAA;AAAA,OACH;AAAA,KACF,CAAA;AAAA,GACF;AACF;;;;"}
@@ -0,0 +1,130 @@
1
+ 'use strict';
2
+
3
+ var openapi_generated = require('../../schema/openapi.generated.cjs.js');
4
+ var MemoryEventBusStore = require('./MemoryEventBusStore.cjs.js');
5
+ var DatabaseEventBusStore = require('./DatabaseEventBusStore.cjs.js');
6
+
7
+ const DEFAULT_NOTIFY_TIMEOUT_MS = 55e3;
8
+ async function createEventBusStore(deps) {
9
+ const db = await deps.database.getClient();
10
+ if (db.client.config.client === "pg") {
11
+ deps.logger.info("Database is PostgreSQL, using database store");
12
+ return await DatabaseEventBusStore.DatabaseEventBusStore.create(deps);
13
+ }
14
+ deps.logger.info("Database is not PostgreSQL, using memory store");
15
+ return new MemoryEventBusStore.MemoryEventBusStore();
16
+ }
17
+ async function createEventBusRouter(options) {
18
+ const { httpAuth, notifyTimeoutMs = DEFAULT_NOTIFY_TIMEOUT_MS } = options;
19
+ const logger = options.logger.child({ type: "EventBus" });
20
+ const store = await createEventBusStore(options);
21
+ const apiRouter = await openapi_generated.createOpenApiRouter();
22
+ apiRouter.post("/bus/v1/events", async (req, res) => {
23
+ const credentials = await httpAuth.credentials(req, {
24
+ allow: ["service"]
25
+ });
26
+ const topic = req.body.event.topic;
27
+ const notifiedSubscribers = req.body.notifiedSubscribers;
28
+ const result = await store.publish({
29
+ event: {
30
+ topic,
31
+ eventPayload: req.body.event.payload
32
+ },
33
+ notifiedSubscribers,
34
+ credentials
35
+ });
36
+ if (result) {
37
+ logger.debug(
38
+ `Published event to '${topic}' with ID '${result.eventId}'`,
39
+ {
40
+ subject: credentials.principal.subject
41
+ }
42
+ );
43
+ res.status(201).end();
44
+ } else {
45
+ if (notifiedSubscribers) {
46
+ const notified = `'${notifiedSubscribers.join("', '")}'`;
47
+ logger.debug(
48
+ `Skipped publishing of event to '${topic}', subscribers have already been notified: ${notified}`,
49
+ { subject: credentials.principal.subject }
50
+ );
51
+ } else {
52
+ logger.debug(
53
+ `Skipped publishing of event to '${topic}', no subscribers present`,
54
+ { subject: credentials.principal.subject }
55
+ );
56
+ }
57
+ res.status(204).end();
58
+ }
59
+ });
60
+ apiRouter.get(
61
+ "/bus/v1/subscriptions/:subscriptionId/events",
62
+ async (req, res) => {
63
+ const credentials = await httpAuth.credentials(req, {
64
+ allow: ["service"]
65
+ });
66
+ const id = req.params.subscriptionId;
67
+ const controller = new AbortController();
68
+ req.on("end", () => controller.abort());
69
+ const listener = await store.setupListener(id, {
70
+ signal: controller.signal
71
+ });
72
+ const timeout = setTimeout(() => {
73
+ controller.abort();
74
+ }, notifyTimeoutMs);
75
+ try {
76
+ const { events } = await store.readSubscription(id);
77
+ logger.debug(
78
+ `Reading subscription '${id}' resulted in ${events.length} events`,
79
+ { subject: credentials.principal.subject }
80
+ );
81
+ if (events.length > 0) {
82
+ res.json({
83
+ events: events.map((event) => ({
84
+ topic: event.topic,
85
+ payload: event.eventPayload
86
+ }))
87
+ });
88
+ } else {
89
+ res.status(202);
90
+ res.flushHeaders();
91
+ try {
92
+ const { topic } = await listener.waitForUpdate();
93
+ logger.debug(
94
+ `Received notification for subscription '${id}' for topic '${topic}'`,
95
+ { subject: credentials.principal.subject }
96
+ );
97
+ } catch (error) {
98
+ if (error !== controller.signal.reason) {
99
+ logger.error(`Error listening for subscription '${id}'`, error);
100
+ }
101
+ } finally {
102
+ await new Promise(
103
+ (resolve) => setTimeout(resolve, 1 + Math.random() * 9)
104
+ );
105
+ res.end();
106
+ }
107
+ }
108
+ } finally {
109
+ controller.abort();
110
+ clearTimeout(timeout);
111
+ }
112
+ }
113
+ );
114
+ apiRouter.put("/bus/v1/subscriptions/:subscriptionId", async (req, res) => {
115
+ const credentials = await httpAuth.credentials(req, {
116
+ allow: ["service"]
117
+ });
118
+ const id = req.params.subscriptionId;
119
+ await store.upsertSubscription(id, req.body.topics, credentials);
120
+ logger.debug(
121
+ `New subscription '${id}' for topics '${req.body.topics.join("', '")}'`,
122
+ { subject: credentials.principal.subject }
123
+ );
124
+ res.status(201).end();
125
+ });
126
+ return apiRouter;
127
+ }
128
+
129
+ exports.createEventBusRouter = createEventBusRouter;
130
+ //# sourceMappingURL=createEventBusRouter.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createEventBusRouter.cjs.js","sources":["../../../src/service/hub/createEventBusRouter.ts"],"sourcesContent":["/*\n * Copyright 2024 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 DatabaseService,\n HttpAuthService,\n LifecycleService,\n LoggerService,\n SchedulerService,\n} from '@backstage/backend-plugin-api';\nimport { Handler } from 'express';\nimport { createOpenApiRouter } from '../../schema/openapi.generated';\nimport { MemoryEventBusStore } from './MemoryEventBusStore';\nimport { DatabaseEventBusStore } from './DatabaseEventBusStore';\nimport { EventBusStore } from './types';\nimport { EventParams } from '@backstage/plugin-events-node';\n\nconst DEFAULT_NOTIFY_TIMEOUT_MS = 55_000; // Just below 60s, which is a common HTTP timeout\n\n/*\n\n# Event Bus\n\nThis comment describes the event bus that is implemented here in the events\nbackend, and by default used by the events service.\n\n## Overview\n\nThe events bus implements a subscription mechanism where subscribers must exist\nupfront for events to be stored. It uses a single inbox for all events, with\neach subscriber having its own pointer for how far into the inbox it has read.\n\nIn order to avoid busy polling, the API uses a long-polling mechanism where a\nrequest is left hanging until the client should try to read again.\n\nThe event bus is not implemented with any guarantees of events being consumed,\nbut it does aim to make it unlikely that events are dropped\n\n## API\n\n### POST /bus/v1/events\n\nThis endpoint is used to publish new events to the event bus on a specific\ntopic. It can optionally include a set of subscription IDs for subscribers that\nhave already been notified of the event. This is to enable an optimization where\nwe notify subscribers locally if possible, and avoid the need for events to be\nrelayed through the events bus at all of possible.\n\nFor an event to be published and stored there must already exist a subscriber\nthat is subscribed to the event's topic, and that hasn't already been notified\nof the event. If no such subscriber is found, the event will be discarded.\n\n### PUT /bus/v1/subscriptions/:subscriptionId\n\nThis endpoint is used to create or update a subscriptions. Subscriptions are\nshared across the entire bus and divided by subscription ID. Multiple clients\ncan be reading events from the same subscription at the same time, but only one\nof those clients will receive each event. This enables division of work by using\nthe same subscriber ID across multiple instances, as well as broadcasting by\nensuring separate subscribers IDs.\n\n### GET /bus/v1/subscriptions/:subscriptionId/events\n\nThis endpoint is used to read events from a subscription. It will return a batch\nof events for the subscribed topics that have not yet been read by the\nsubscription. If no such events are available, the endpoint will return a 202\nresponse and then hang end response until an event is available or a timeout is\nreached. This allows clients to call this endpoint in a loop but will keep\ntraffic overhead fairly low.\n\n## Delivery guarantees\n\nWhen reading events from the event bus, clients should always implement a\ngraceful shutdown where they process any events that are received from the\nevents endpoint before shutting down. This is also the reason that the events\nendpoint does not return any events when responding with a 202 blocking the\nresponse, because there would otherwise be a race condition where the events\nmight be lost in transit if the client shuts down. By always sending an empty\nresponse and requiring the client to send another request, we ensure that the\nclient is prepared to halt shutdown until the request had been fully processed.\n\n## Local processing optimization\n\nWhen possible, events will be processed locally before sent to the event bus.\nThe client will also inform the bus of which subscriptions have already been\nnotified of the event, so that the bus can completely avoid storing an event if\nit has already been fully consumed by all subscribers.\n\n## Automated cleanup & event window\n\nEvents are deleted once they are outside the guaranteed storage window. By\ndefault the window 10 minutes for all events, and 24 hours for the last 10000\nevents. This ensures that the event log doesn't grow indefinitely, while still\nallowing subscribers with restarts or outages to catch up to past events,\nensuring that events are likely not lost.\n\nSubscriptions are also cleaned up if their read pointer falls outside of the\ncurrent event window. This ensures that stale subscribers don't accumulate and\ncause unnecessary storage of events.\n\n*/\n\nasync function createEventBusStore(deps: {\n logger: LoggerService;\n database: DatabaseService;\n scheduler: SchedulerService;\n lifecycle: LifecycleService;\n httpAuth: HttpAuthService;\n}): Promise<EventBusStore> {\n const db = await deps.database.getClient();\n if (db.client.config.client === 'pg') {\n deps.logger.info('Database is PostgreSQL, using database store');\n return await DatabaseEventBusStore.create(deps);\n }\n\n deps.logger.info('Database is not PostgreSQL, using memory store');\n return new MemoryEventBusStore();\n}\n\n/**\n * Creates a new event bus router\n * @internal\n */\nexport async function createEventBusRouter(options: {\n logger: LoggerService;\n database: DatabaseService;\n scheduler: SchedulerService;\n lifecycle: LifecycleService;\n httpAuth: HttpAuthService;\n notifyTimeoutMs?: number; // for testing\n}): Promise<Handler> {\n const { httpAuth, notifyTimeoutMs = DEFAULT_NOTIFY_TIMEOUT_MS } = options;\n const logger = options.logger.child({ type: 'EventBus' });\n\n const store = await createEventBusStore(options);\n\n const apiRouter = await createOpenApiRouter();\n\n apiRouter.post('/bus/v1/events', async (req, res) => {\n const credentials = await httpAuth.credentials(req, {\n allow: ['service'],\n });\n const topic = req.body.event.topic;\n const notifiedSubscribers = req.body.notifiedSubscribers;\n const result = await store.publish({\n event: {\n topic,\n eventPayload: req.body.event.payload,\n } as EventParams,\n notifiedSubscribers,\n credentials,\n });\n if (result) {\n logger.debug(\n `Published event to '${topic}' with ID '${result.eventId}'`,\n {\n subject: credentials.principal.subject,\n },\n );\n res.status(201).end();\n } else {\n if (notifiedSubscribers) {\n const notified = `'${notifiedSubscribers.join(\"', '\")}'`;\n logger.debug(\n `Skipped publishing of event to '${topic}', subscribers have already been notified: ${notified}`,\n { subject: credentials.principal.subject },\n );\n } else {\n logger.debug(\n `Skipped publishing of event to '${topic}', no subscribers present`,\n { subject: credentials.principal.subject },\n );\n }\n res.status(204).end();\n }\n });\n\n apiRouter.get(\n '/bus/v1/subscriptions/:subscriptionId/events',\n async (req, res) => {\n const credentials = await httpAuth.credentials(req, {\n allow: ['service'],\n });\n const id = req.params.subscriptionId;\n\n const controller = new AbortController();\n req.on('end', () => controller.abort());\n\n // By setting up the listener first we make sure we don't miss any events\n // that are published while reading. If an event is published we'll receive\n // a notification, which we may ignore depending on the outcome of the read\n const listener = await store.setupListener(id, {\n signal: controller.signal,\n });\n\n // By timing out requests we make sure they don't stall or that events get stuck.\n // For the caller there's no difference between a timeout and a\n // notification, either way they should try reading again.\n const timeout = setTimeout(() => {\n controller.abort();\n }, notifyTimeoutMs);\n\n try {\n const { events } = await store.readSubscription(id);\n\n logger.debug(\n `Reading subscription '${id}' resulted in ${events.length} events`,\n { subject: credentials.principal.subject },\n );\n\n if (events.length > 0) {\n res.json({\n events: events.map(event => ({\n topic: event.topic,\n payload: event.eventPayload,\n })),\n });\n } else {\n res.status(202);\n res.flushHeaders();\n\n try {\n const { topic } = await listener.waitForUpdate();\n logger.debug(\n `Received notification for subscription '${id}' for topic '${topic}'`,\n { subject: credentials.principal.subject },\n );\n } catch (error) {\n if (error !== controller.signal.reason) {\n logger.error(`Error listening for subscription '${id}'`, error);\n }\n } finally {\n // A small extra delay ensures a more even spread of events across\n // consumers in case some consumers are faster than others\n await new Promise(resolve =>\n setTimeout(resolve, 1 + Math.random() * 9),\n );\n res.end();\n }\n }\n } finally {\n controller.abort();\n clearTimeout(timeout);\n }\n },\n );\n\n apiRouter.put('/bus/v1/subscriptions/:subscriptionId', async (req, res) => {\n const credentials = await httpAuth.credentials(req, {\n allow: ['service'],\n });\n const id = req.params.subscriptionId;\n\n await store.upsertSubscription(id, req.body.topics, credentials);\n\n logger.debug(\n `New subscription '${id}' for topics '${req.body.topics.join(\"', '\")}'`,\n { subject: credentials.principal.subject },\n );\n\n res.status(201).end();\n });\n\n return apiRouter;\n}\n"],"names":["DatabaseEventBusStore","MemoryEventBusStore","createOpenApiRouter"],"mappings":";;;;;;AA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,GAAI,CAAA,IAAA,CAAK,QAAQ,WAAW,CAAA,CAAA;AAE/D,IAAO,MAAA,CAAA,KAAA;AAAA,MACL,CAAA,kBAAA,EAAqB,EAAE,CAAiB,cAAA,EAAA,GAAA,CAAI,KAAK,MAAO,CAAA,IAAA,CAAK,MAAM,CAAC,CAAA,CAAA,CAAA;AAAA,MACpE,EAAE,OAAA,EAAS,WAAY,CAAA,SAAA,CAAU,OAAQ,EAAA;AAAA,KAC3C,CAAA;AAEA,IAAI,GAAA,CAAA,MAAA,CAAO,GAAG,CAAA,CAAE,GAAI,EAAA,CAAA;AAAA,GACrB,CAAA,CAAA;AAED,EAAO,OAAA,SAAA,CAAA;AACT;;;;"}
@@ -0,0 +1,98 @@
1
+ /*
2
+ * Copyright 2024 The Backstage Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ // @ts-check
18
+
19
+ /**
20
+ * @param { import("knex").Knex } knex
21
+ * @returns { Promise<void> }
22
+ */
23
+ exports.up = async function up(knex) {
24
+ // The event bus only supports PostgresSQL
25
+ if (knex.client.config.client === 'pg') {
26
+ await knex.schema.createTable('event_bus_events', table => {
27
+ table.comment('Events published to the events bus');
28
+ table
29
+ .bigIncrements('id')
30
+ .primary()
31
+ .comment('The unique ID of this event');
32
+ table
33
+ .text('created_by')
34
+ .notNullable()
35
+ .comment('The principal that published the event');
36
+ table
37
+ .dateTime('created_at')
38
+ .defaultTo(knex.fn.now())
39
+ .notNullable()
40
+ .comment('The time that the event was created');
41
+ table.text('topic').notNullable().comment('The topic of the event');
42
+ table
43
+ .text('data_json')
44
+ .notNullable()
45
+ .comment('The payload data of this event');
46
+ table
47
+ .specificType('notified_subscribers', 'text ARRAY')
48
+ .comment(
49
+ 'The IDs of the subscribers that have already consumed this event',
50
+ );
51
+
52
+ table.index('topic', 'event_bus_events_topic_idx');
53
+ });
54
+
55
+ await knex.schema.createTable('event_bus_subscriptions', table => {
56
+ table.comment('Subscriptions to the event bus');
57
+ table
58
+ .string('id')
59
+ .primary()
60
+ .notNullable()
61
+ .comment('The unique ID of this particular subscription');
62
+ table
63
+ .text('created_by')
64
+ .notNullable()
65
+ .comment('The principal that created the subscription');
66
+ table
67
+ .dateTime('created_at')
68
+ .defaultTo(knex.fn.now())
69
+ .notNullable()
70
+ .comment('The time that the subscription was created');
71
+ table
72
+ .dateTime('updated_at')
73
+ .defaultTo(knex.fn.now())
74
+ .notNullable()
75
+ .comment('The time that the subscription was last updated');
76
+ table
77
+ .bigInteger('read_until')
78
+ .notNullable()
79
+ .comment(
80
+ 'The sequence counter until which the subscription has read events',
81
+ );
82
+ table
83
+ .specificType('topics', 'text ARRAY')
84
+ .comment('The topics that this subscriber is interested in');
85
+ });
86
+ }
87
+ };
88
+
89
+ /**
90
+ * @param { import("knex").Knex } knex
91
+ * @returns { Promise<void> }
92
+ */
93
+ exports.down = async function down(knex) {
94
+ if (knex.client.config.client === 'pg') {
95
+ await knex.schema.dropTable('event_bus_subscriptions');
96
+ await knex.schema.dropTable('event_bus_events');
97
+ }
98
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/plugin-events-backend",
3
- "version": "0.3.12",
3
+ "version": "0.3.13-next.1",
4
4
  "backstage": {
5
5
  "role": "backend-plugin",
6
6
  "pluginId": "events",
@@ -27,6 +27,7 @@
27
27
  "default": "./dist/index.cjs.js"
28
28
  },
29
29
  "./alpha": {
30
+ "backstage": "@backstage/BackendFeature",
30
31
  "require": "./dist/alpha.cjs.js",
31
32
  "types": "./dist/alpha.d.ts",
32
33
  "default": "./dist/alpha.cjs.js"
@@ -38,11 +39,13 @@
38
39
  "files": [
39
40
  "config.d.ts",
40
41
  "dist",
42
+ "migrations",
41
43
  "alpha"
42
44
  ],
43
45
  "scripts": {
44
46
  "build": "backstage-cli package build",
45
47
  "clean": "backstage-cli package clean",
48
+ "generate": "backstage-repo-tools package schema openapi generate --server --client-package plugins/events-node",
46
49
  "lint": "backstage-cli package lint",
47
50
  "prepack": "backstage-cli package prepack",
48
51
  "postpack": "backstage-cli package postpack",
@@ -51,19 +54,25 @@
51
54
  },
52
55
  "dependencies": {
53
56
  "@backstage/backend-common": "^0.25.0",
54
- "@backstage/backend-plugin-api": "^1.0.0",
55
- "@backstage/config": "^1.2.0",
56
- "@backstage/plugin-events-node": "^0.4.0",
57
+ "@backstage/backend-openapi-utils": "0.2.0-next.1",
58
+ "@backstage/backend-plugin-api": "1.0.1-next.1",
59
+ "@backstage/config": "1.2.0",
60
+ "@backstage/errors": "1.2.4",
61
+ "@backstage/plugin-events-node": "0.4.1-next.1",
62
+ "@backstage/types": "1.1.1",
57
63
  "@types/express": "^4.17.6",
58
64
  "express": "^4.17.1",
59
65
  "express-promise-router": "^4.1.0",
66
+ "knex": "^3.0.0",
60
67
  "winston": "^3.2.1"
61
68
  },
62
69
  "devDependencies": {
63
- "@backstage/backend-common": "^0.25.0",
64
- "@backstage/backend-test-utils": "^1.0.0",
65
- "@backstage/cli": "^0.27.1",
66
- "@backstage/plugin-events-backend-test-utils": "^0.1.35",
70
+ "@backstage/backend-app-api": "1.0.1-next.1",
71
+ "@backstage/backend-defaults": "0.5.1-next.2",
72
+ "@backstage/backend-test-utils": "1.0.1-next.2",
73
+ "@backstage/cli": "0.28.0-next.2",
74
+ "@backstage/plugin-events-backend-test-utils": "0.1.36-next.1",
75
+ "@backstage/repo-tools": "0.10.0-next.2",
67
76
  "supertest": "^7.0.0"
68
77
  },
69
78
  "configSchema": "config.d.ts"
@@ -1 +0,0 @@
1
- {"version":3,"file":"HttpPostIngressEventPublisher-D6pQ6awS.cjs.js","sources":["../../src/service/http/validation/RequestValidationContextImpl.ts","../../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 {\n RequestRejectionDetails,\n RequestValidationContext,\n} from '@backstage/plugin-events-node';\n\nexport class RequestValidationContextImpl implements RequestValidationContext {\n #rejectionDetails: RequestRejectionDetails | undefined;\n\n reject(details?: Partial<RequestRejectionDetails>): void {\n this.#rejectionDetails = {\n status: details?.status ?? 403,\n payload: details?.payload ?? {},\n };\n }\n\n wasRejected(): boolean {\n return this.#rejectionDetails !== undefined;\n }\n\n get rejectionDetails() {\n return this.#rejectionDetails;\n }\n}\n","/*\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 { errorHandler } from '@backstage/backend-common';\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport { Config } from '@backstage/config';\nimport {\n EventsService,\n HttpPostIngressOptions,\n RequestValidator,\n} from '@backstage/plugin-events-node';\nimport express from 'express';\nimport Router from 'express-promise-router';\nimport { RequestValidationContextImpl } from './validation';\n\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 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 return new HttpPostIngressEventPublisher(env.events, env.logger, ingresses);\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 ) {}\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.json());\n\n Object.keys(ingresses).forEach(topic =>\n this.addRouteForTopic(router, topic, ingresses[topic].validator),\n );\n\n router.use(errorHandler());\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\n router.post(path, async (request, response) => {\n const requestDetails = {\n body: request.body,\n headers: request.headers,\n };\n const context = new RequestValidationContextImpl();\n await validator?.(requestDetails, context);\n if (context.wasRejected()) {\n response\n .status(context.rejectionDetails!.status)\n .json(context.rejectionDetails!.payload);\n return;\n }\n\n const eventPayload = request.body;\n await this.events.publish({\n topic,\n eventPayload,\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 this.logger.info(`Registered /api/events/http${path} to receive events`);\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