@cap-js-community/event-queue 1.11.0 → 1.11.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js-community/event-queue",
3
- "version": "1.11.0",
3
+ "version": "1.11.1",
4
4
  "description": "An event queue that enables secure transactional processing of asynchronous and periodic events, featuring instant event processing with Redis Pub/Sub and load distribution across all application instances.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
package/src/config.js CHANGED
@@ -65,6 +65,7 @@ const ALLOWED_EVENT_OPTIONS_AD_HOC = [
65
65
  "processAfterCommit",
66
66
  "checkForNextChunk",
67
67
  "retryFailedAfter",
68
+ "propagateHeaders",
68
69
  "multiInstanceProcessing",
69
70
  "kind",
70
71
  "timeBucket",
@@ -388,6 +389,7 @@ class Config {
388
389
  selectMaxChunkSize: config.selectMaxChunkSize ?? config.chunkSize,
389
390
  parallelEventProcessing: config.parallelEventProcessing ?? (config.parallel && CAP_PARALLEL_DEFAULT),
390
391
  retryAttempts: config.retryAttempts ?? config.maxAttempts ?? CAP_MAX_ATTEMPTS_DEFAULT,
392
+ propagateHeaders: config.propagateHeaders ?? [],
391
393
  ...config,
392
394
  });
393
395
  eventConfig.internalEvent = true;
@@ -583,6 +585,7 @@ class Config {
583
585
  event._appInstancesMap = event.appInstances
584
586
  ? Object.fromEntries(new Map(event.appInstances.map((a) => [a, true])))
585
587
  : null;
588
+ event.propagateHeaders = event.propagateHeaders ?? [];
586
589
  }
587
590
 
588
591
  #basicEventValidation(event) {
@@ -47,13 +47,15 @@ function outboxed(srv, customOpts) {
47
47
  customOpts || {}
48
48
  );
49
49
  config.addCAPOutboxEventBase(srv.name, outboxOpts);
50
- const specificSettings = config.getCdsOutboxEventSpecificConfig(srv.name, req.event);
51
- if (specificSettings) {
50
+ const hasSpecificSettings = !!config.getCdsOutboxEventSpecificConfig(srv.name, req.event);
51
+ if (hasSpecificSettings) {
52
52
  outboxOpts = config.addCAPOutboxEventSpecificAction(srv.name, req.event);
53
53
  }
54
-
54
+ const subType = hasSpecificSettings ? [srv.name, req.event].join(".") : srv.name;
55
+ outboxOpts = config.getEventConfig(CDS_EVENT_TYPE, subType);
56
+ const eventHeaders = getPropagatedHeaders(outboxOpts, req);
55
57
  if (["persistent-outbox", "persistent-queue"].includes(outboxOpts.kind)) {
56
- await _mapToEventAndPublish(context, srv.name, req, !!specificSettings);
58
+ await _mapToEventAndPublish(context, subType, req, eventHeaders);
57
59
  return;
58
60
  }
59
61
  context.on("succeeded", async () => {
@@ -79,30 +81,41 @@ function unboxed(srv) {
79
81
  return srv[UNBOXED] || srv;
80
82
  }
81
83
 
82
- const _mapToEventAndPublish = async (context, name, req, actionSpecific) => {
84
+ const getPropagatedHeaders = (config, req) => {
85
+ const propagateHeaders = config.propagateHeaders.reduce((headers, headerName) => {
86
+ if (headerName in req.tx.context.headers) {
87
+ headers[headerName] = req.tx.context.headers[headerName];
88
+ }
89
+ return headers;
90
+ }, {});
91
+ return Object.assign(propagateHeaders, req.headers);
92
+ };
93
+
94
+ const _mapToEventAndPublish = async (context, subType, req, eventHeaders) => {
83
95
  const eventQueueSpecificValues = {};
84
96
  for (const header in req.headers ?? {}) {
85
97
  for (const field of EVENT_QUEUE_SPECIFIC_FIELDS) {
86
98
  if (header.toLocaleLowerCase() === `x-eventqueue-${field.toLocaleLowerCase()}`) {
87
99
  eventQueueSpecificValues[field] = req.headers[header];
88
- delete req.headers[header];
100
+ delete eventHeaders[header];
89
101
  break;
90
102
  }
91
103
  }
92
104
  }
105
+
93
106
  const event = {
94
107
  contextUser: context.user.id,
95
108
  ...(req._fromSend || (req.reply && { _fromSend: true })), // send or emit
96
109
  ...(req.inbound && { inbound: req.inbound }),
97
110
  ...(req.event && { event: req.event }),
98
111
  ...(req.data && { data: req.data }),
99
- ...(req.headers && { headers: req.headers }),
112
+ ...(eventHeaders && { headers: eventHeaders }),
100
113
  ...(req.query && { query: req.query }),
101
114
  };
102
115
 
103
116
  await publishEvent(cds.tx(context), {
104
117
  type: CDS_EVENT_TYPE,
105
- subType: actionSpecific ? [name, req.event].join(".") : name,
118
+ subType,
106
119
  payload: JSON.stringify(event),
107
120
  ...eventQueueSpecificValues,
108
121
  });