@versori/run 0.6.2 → 0.6.3

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.
@@ -1 +1 @@
1
- {"version":3,"file":"webhookmiddleware.d.ts","sourceRoot":"","sources":["../../../../../src/src/dsl/http/versori/webhookmiddleware.ts"],"names":[],"mappings":"AAaA,OAAO,EAAc,WAAW,EAAE,MAAM,mCAAmC,CAAC;AAC5E,OAAO,EAAyB,cAAc,EAAY,MAAM,SAAS,CAAC;AAE1E,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,MAAM,EAAE,MAAM,0CAA0C,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG3C,MAAM,MAAM,2BAA2B,GAAG;IACtC,EAAE,EAAE,MAAM,CAAC;IACX,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,WAAW,CAAC;IACzB,UAAU,EAAE,iBAAiB,CAAC;IAC9B,SAAS,EAAE,YAAY,CAAC;IACxB,GAAG,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,wBAAgB,6BAA6B,CACzC,IAAI,EAAE,2BAA2B,GAClC,cAAc,CA4GhB;AAED,wBAAgB,mCAAmC,CAC/C,IAAI,EAAE,2BAA2B,GAClC,cAAc,CAkGhB;AAED,wBAAgB,oCAAoC,CAChD,IAAI,EAAE,2BAA2B,GAClC,cAAc,CAqFhB"}
1
+ {"version":3,"file":"webhookmiddleware.d.ts","sourceRoot":"","sources":["../../../../../src/src/dsl/http/versori/webhookmiddleware.ts"],"names":[],"mappings":"AAaA,OAAO,EAAc,WAAW,EAAE,MAAM,mCAAmC,CAAC;AAC5E,OAAO,EAAyB,cAAc,EAAY,MAAM,SAAS,CAAC;AAE1E,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,MAAM,EAAE,MAAM,0CAA0C,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG3C,MAAM,MAAM,2BAA2B,GAAG;IACtC,EAAE,EAAE,MAAM,CAAC;IACX,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,WAAW,CAAC;IACzB,UAAU,EAAE,iBAAiB,CAAC;IAC9B,SAAS,EAAE,YAAY,CAAC;IACxB,GAAG,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,wBAAgB,6BAA6B,CACzC,IAAI,EAAE,2BAA2B,GAClC,cAAc,CA4GhB;AAED,wBAAgB,mCAAmC,CAC/C,IAAI,EAAE,2BAA2B,GAClC,cAAc,CAkGhB;AAED,wBAAgB,oCAAoC,CAChD,IAAI,EAAE,2BAA2B,GAClC,cAAc,CAwHhB"}
@@ -122,13 +122,11 @@ export function createActIdDynamicWebhookMiddleware(opts) {
122
122
  const activationId = req.params.activationId;
123
123
  let activation;
124
124
  try {
125
- activation = await await getActivation(activationId);
126
- ;
125
+ activation = await getActivation(activationId);
127
126
  }
128
127
  catch (err) {
129
- opts.log.error('Failed to get static activation', { error: err });
130
- activation = await await getActivation(activationId, true);
131
- ;
128
+ opts.log.error('Failed to get activation', { activationId, error: err });
129
+ activation = await getActivation(activationId, true);
132
130
  }
133
131
  res.locals.activation = activation;
134
132
  next();
@@ -145,13 +143,11 @@ export function createActIdDynamicWebhookMiddleware(opts) {
145
143
  const activationId = req.params.activationId;
146
144
  let activation;
147
145
  try {
148
- activation = await await getActivation(activationId);
149
- ;
146
+ activation = await getActivation(activationId);
150
147
  }
151
148
  catch (err) {
152
- opts.log.error('Failed to get static activation', { error: err });
153
- activation = await await getActivation(activationId, true);
154
- ;
149
+ opts.log.error('Failed to get activation', { activationId, error: err });
150
+ activation = await getActivation(activationId, true);
155
151
  }
156
152
  const conn = await opts.platformApi.getActivationConnection({
157
153
  path: {
@@ -172,22 +168,51 @@ export function createActIdDynamicWebhookMiddleware(opts) {
172
168
  });
173
169
  }
174
170
  export function createUserIdDynamicWebhookMiddleware(opts) {
171
+ // create lru cache for activations keyed by userId
172
+ const activationCache = new LRUCache({ max: 100, ttl: 5 * 60 * 1000 }); // 5 minute ttl
173
+ const getUserActivations = (userId, forceFetch = false) => {
174
+ const cachedActivations = activationCache.get(userId);
175
+ if (cachedActivations && !forceFetch) {
176
+ return cachedActivations;
177
+ }
178
+ else {
179
+ const fetchActivations = async () => {
180
+ const { data: activations } = await opts.platformApi.listUserActivations({
181
+ path: {
182
+ organisation_id: opts.organisationId,
183
+ environment_id: opts.environmentId,
184
+ external_user_id: userId,
185
+ },
186
+ throwOnError: true,
187
+ });
188
+ if (activations.length === 0) {
189
+ throw new Error(`No activations found for user: ${userId}`);
190
+ }
191
+ return activations;
192
+ };
193
+ const activationsPromise = fetchActivations();
194
+ activationCache.set(userId, activationsPromise);
195
+ return activationsPromise;
196
+ }
197
+ };
175
198
  if (!opts.connName) {
176
199
  opts.log.warn(`No template ID found for webhook, accepting any requests which is unsecure!`, { webhookId: opts.id });
177
200
  return asyncHandler(async (req, res, next) => {
178
201
  const userId = req.params.userId;
179
- const { data: activations } = await opts.platformApi.listUserActivations({
180
- path: {
181
- organisation_id: opts.organisationId,
182
- environment_id: opts.environmentId,
183
- external_user_id: userId,
184
- },
185
- throwOnError: true,
186
- });
187
- if (activations.length === 0) {
188
- opts.log.error(`No activations found for user: ${userId}`);
189
- res.status(401).json({ error: 'Unauthorised' });
190
- return;
202
+ let activations;
203
+ try {
204
+ activations = await getUserActivations(userId);
205
+ }
206
+ catch (err) {
207
+ opts.log.error('Failed to get activations for user', { userId, error: err });
208
+ try {
209
+ activations = await getUserActivations(userId, true);
210
+ }
211
+ catch (refetchErr) {
212
+ opts.log.error(`No activations found for user: ${userId}`, { error: refetchErr });
213
+ res.status(401).json({ error: 'Unauthorised' });
214
+ return;
215
+ }
191
216
  }
192
217
  if (activations.length > 1) {
193
218
  opts.log.warn(`Multiple activations found for user: ${userId}, using the first one: ${activations[0].id}`);
@@ -205,18 +230,20 @@ export function createUserIdDynamicWebhookMiddleware(opts) {
205
230
  }
206
231
  const templateId = opts.cfgReader.getTemplateID(opts.connName);
207
232
  const userId = req.params.userId;
208
- const { data: activations } = await opts.platformApi.listUserActivations({
209
- path: {
210
- organisation_id: opts.organisationId,
211
- environment_id: opts.environmentId,
212
- external_user_id: userId,
213
- },
214
- throwOnError: true,
215
- });
216
- if (activations.length === 0) {
217
- opts.log.error(`No activations found for user: ${userId}`);
218
- res.status(401).json({ error: 'Unauthorised' });
219
- return;
233
+ let activations;
234
+ try {
235
+ activations = await getUserActivations(userId);
236
+ }
237
+ catch (err) {
238
+ opts.log.error('Failed to get activations for user', { userId, error: err });
239
+ try {
240
+ activations = await getUserActivations(userId, true);
241
+ }
242
+ catch (refetchErr) {
243
+ opts.log.error(`No activations found for user: ${userId}`, { error: refetchErr });
244
+ res.status(401).json({ error: 'Unauthorised' });
245
+ return;
246
+ }
220
247
  }
221
248
  const activation = activations[0]; // Right now we only support a user having one activation
222
249
  const conn = await opts.platformApi.getActivationConnection({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versori/run",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
4
4
  "description": "Versori Run",
5
5
  "homepage": "https://github.com/versori/versori-run#readme",
6
6
  "repository": {
@@ -1 +1 @@
1
- {"version":3,"file":"webhookmiddleware.d.ts","sourceRoot":"","sources":["../../../../../src/src/dsl/http/versori/webhookmiddleware.ts"],"names":[],"mappings":"AAaA,OAAO,EAAc,WAAW,EAAE,MAAM,mCAAmC,CAAC;AAC5E,OAAO,EAAyB,cAAc,EAAY,MAAM,SAAS,CAAC;AAE1E,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,MAAM,EAAE,MAAM,0CAA0C,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG3C,MAAM,MAAM,2BAA2B,GAAG;IACtC,EAAE,EAAE,MAAM,CAAC;IACX,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,WAAW,CAAC;IACzB,UAAU,EAAE,iBAAiB,CAAC;IAC9B,SAAS,EAAE,YAAY,CAAC;IACxB,GAAG,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,wBAAgB,6BAA6B,CACzC,IAAI,EAAE,2BAA2B,GAClC,cAAc,CA4GhB;AAED,wBAAgB,mCAAmC,CAC/C,IAAI,EAAE,2BAA2B,GAClC,cAAc,CAkGhB;AAED,wBAAgB,oCAAoC,CAChD,IAAI,EAAE,2BAA2B,GAClC,cAAc,CAqFhB"}
1
+ {"version":3,"file":"webhookmiddleware.d.ts","sourceRoot":"","sources":["../../../../../src/src/dsl/http/versori/webhookmiddleware.ts"],"names":[],"mappings":"AAaA,OAAO,EAAc,WAAW,EAAE,MAAM,mCAAmC,CAAC;AAC5E,OAAO,EAAyB,cAAc,EAAY,MAAM,SAAS,CAAC;AAE1E,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,MAAM,EAAE,MAAM,0CAA0C,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG3C,MAAM,MAAM,2BAA2B,GAAG;IACtC,EAAE,EAAE,MAAM,CAAC;IACX,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,WAAW,CAAC;IACzB,UAAU,EAAE,iBAAiB,CAAC;IAC9B,SAAS,EAAE,YAAY,CAAC;IACxB,GAAG,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,wBAAgB,6BAA6B,CACzC,IAAI,EAAE,2BAA2B,GAClC,cAAc,CA4GhB;AAED,wBAAgB,mCAAmC,CAC/C,IAAI,EAAE,2BAA2B,GAClC,cAAc,CAkGhB;AAED,wBAAgB,oCAAoC,CAChD,IAAI,EAAE,2BAA2B,GAClC,cAAc,CAwHhB"}
@@ -127,13 +127,11 @@ function createActIdDynamicWebhookMiddleware(opts) {
127
127
  const activationId = req.params.activationId;
128
128
  let activation;
129
129
  try {
130
- activation = await await getActivation(activationId);
131
- ;
130
+ activation = await getActivation(activationId);
132
131
  }
133
132
  catch (err) {
134
- opts.log.error('Failed to get static activation', { error: err });
135
- activation = await await getActivation(activationId, true);
136
- ;
133
+ opts.log.error('Failed to get activation', { activationId, error: err });
134
+ activation = await getActivation(activationId, true);
137
135
  }
138
136
  res.locals.activation = activation;
139
137
  next();
@@ -150,13 +148,11 @@ function createActIdDynamicWebhookMiddleware(opts) {
150
148
  const activationId = req.params.activationId;
151
149
  let activation;
152
150
  try {
153
- activation = await await getActivation(activationId);
154
- ;
151
+ activation = await getActivation(activationId);
155
152
  }
156
153
  catch (err) {
157
- opts.log.error('Failed to get static activation', { error: err });
158
- activation = await await getActivation(activationId, true);
159
- ;
154
+ opts.log.error('Failed to get activation', { activationId, error: err });
155
+ activation = await getActivation(activationId, true);
160
156
  }
161
157
  const conn = await opts.platformApi.getActivationConnection({
162
158
  path: {
@@ -177,22 +173,51 @@ function createActIdDynamicWebhookMiddleware(opts) {
177
173
  });
178
174
  }
179
175
  function createUserIdDynamicWebhookMiddleware(opts) {
176
+ // create lru cache for activations keyed by userId
177
+ const activationCache = new lru_cache_1.LRUCache({ max: 100, ttl: 5 * 60 * 1000 }); // 5 minute ttl
178
+ const getUserActivations = (userId, forceFetch = false) => {
179
+ const cachedActivations = activationCache.get(userId);
180
+ if (cachedActivations && !forceFetch) {
181
+ return cachedActivations;
182
+ }
183
+ else {
184
+ const fetchActivations = async () => {
185
+ const { data: activations } = await opts.platformApi.listUserActivations({
186
+ path: {
187
+ organisation_id: opts.organisationId,
188
+ environment_id: opts.environmentId,
189
+ external_user_id: userId,
190
+ },
191
+ throwOnError: true,
192
+ });
193
+ if (activations.length === 0) {
194
+ throw new Error(`No activations found for user: ${userId}`);
195
+ }
196
+ return activations;
197
+ };
198
+ const activationsPromise = fetchActivations();
199
+ activationCache.set(userId, activationsPromise);
200
+ return activationsPromise;
201
+ }
202
+ };
180
203
  if (!opts.connName) {
181
204
  opts.log.warn(`No template ID found for webhook, accepting any requests which is unsecure!`, { webhookId: opts.id });
182
205
  return (0, asyncHandler_js_1.asyncHandler)(async (req, res, next) => {
183
206
  const userId = req.params.userId;
184
- const { data: activations } = await opts.platformApi.listUserActivations({
185
- path: {
186
- organisation_id: opts.organisationId,
187
- environment_id: opts.environmentId,
188
- external_user_id: userId,
189
- },
190
- throwOnError: true,
191
- });
192
- if (activations.length === 0) {
193
- opts.log.error(`No activations found for user: ${userId}`);
194
- res.status(401).json({ error: 'Unauthorised' });
195
- return;
207
+ let activations;
208
+ try {
209
+ activations = await getUserActivations(userId);
210
+ }
211
+ catch (err) {
212
+ opts.log.error('Failed to get activations for user', { userId, error: err });
213
+ try {
214
+ activations = await getUserActivations(userId, true);
215
+ }
216
+ catch (refetchErr) {
217
+ opts.log.error(`No activations found for user: ${userId}`, { error: refetchErr });
218
+ res.status(401).json({ error: 'Unauthorised' });
219
+ return;
220
+ }
196
221
  }
197
222
  if (activations.length > 1) {
198
223
  opts.log.warn(`Multiple activations found for user: ${userId}, using the first one: ${activations[0].id}`);
@@ -210,18 +235,20 @@ function createUserIdDynamicWebhookMiddleware(opts) {
210
235
  }
211
236
  const templateId = opts.cfgReader.getTemplateID(opts.connName);
212
237
  const userId = req.params.userId;
213
- const { data: activations } = await opts.platformApi.listUserActivations({
214
- path: {
215
- organisation_id: opts.organisationId,
216
- environment_id: opts.environmentId,
217
- external_user_id: userId,
218
- },
219
- throwOnError: true,
220
- });
221
- if (activations.length === 0) {
222
- opts.log.error(`No activations found for user: ${userId}`);
223
- res.status(401).json({ error: 'Unauthorised' });
224
- return;
238
+ let activations;
239
+ try {
240
+ activations = await getUserActivations(userId);
241
+ }
242
+ catch (err) {
243
+ opts.log.error('Failed to get activations for user', { userId, error: err });
244
+ try {
245
+ activations = await getUserActivations(userId, true);
246
+ }
247
+ catch (refetchErr) {
248
+ opts.log.error(`No activations found for user: ${userId}`, { error: refetchErr });
249
+ res.status(401).json({ error: 'Unauthorised' });
250
+ return;
251
+ }
225
252
  }
226
253
  const activation = activations[0]; // Right now we only support a user having one activation
227
254
  const conn = await opts.platformApi.getActivationConnection({