serverless-spy 2.2.1 → 2.2.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.
@@ -5,7 +5,6 @@ import { ServerlessSpyListenerParams } from './ServerlessSpyListenerParams';
5
5
  import { getTopic } from './topic';
6
6
  import { WaitForParams } from './WaitForParams';
7
7
  import { FunctionRequestSpyEvent } from '../common/spyEvents/FunctionRequestSpyEvent';
8
- import { SpyEvent } from '../common/spyEvents/SpyEvent';
9
8
  import { SpyMessage } from '../common/spyEvents/SpyMessage';
10
9
 
11
10
  export class WsListener<TSpyEvents> {
@@ -229,46 +228,49 @@ export class WsListener<TSpyEvents> {
229
228
  serviceKeyForFunction: string,
230
229
  functionContextAwsRequestId?: string
231
230
  ) {
232
- let tracker: Tracker;
233
-
234
- const promise = new Promise((resolve, reject) => {
235
- tracker = {
231
+ return (paramsW?: WaitForParams) => {
232
+ let resolve: (value: void | PromiseLike<any>) => void;
233
+ const promise = new Promise((res) => {
234
+ resolve = res;
235
+ });
236
+ const tracker: Tracker = {
236
237
  finished: false,
238
+ // @ts-ignore
237
239
  promiseResolve: resolve,
238
- promiseReject: reject,
239
240
  serviceKeyForFunction,
240
241
  functionContextAwsRequestId,
241
242
  };
242
- });
243
243
 
244
- return (paramsW?: WaitForParams<SpyEvent>) => {
245
244
  tracker.condition = paramsW?.condition;
246
245
 
247
- const timer = setTimeout(() => {
248
- if (tracker.finished) return;
249
- tracker.finished = true;
250
- let message = `Timeout waiting for Serverless Spy message ${serviceKeyForFunction}.`;
251
-
252
- if (tracker.possibleSpyMessageDataForDebugging) {
253
- message += ` Similar matching spy event data: ${JSON.stringify(
254
- tracker.possibleSpyMessageDataForDebugging,
255
- null,
256
- 2
257
- )}`;
258
- }
259
-
260
- tracker.promiseReject(new Error(message));
261
- }, paramsW?.timoutMs || 10000);
246
+ let timeoutPid: NodeJS.Timeout | undefined;
247
+ const timer = new Promise((_, reject) => {
248
+ timeoutPid = setTimeout(() => {
249
+ if (tracker.finished) return;
250
+ tracker.finished = true;
251
+ let message = `Timeout waiting for Serverless Spy message ${serviceKeyForFunction}.`;
252
+
253
+ if (tracker.possibleSpyMessageDataForDebugging) {
254
+ message += ` Similar matching spy event data: ${JSON.stringify(
255
+ tracker.possibleSpyMessageDataForDebugging,
256
+ null,
257
+ 2
258
+ )}`;
259
+ }
262
260
 
263
- void promise.finally(() => {
264
- clearTimeout(timer);
261
+ reject(new Error(message));
262
+ }, paramsW?.timoutMs || 10000);
265
263
  });
266
264
 
267
265
  if (!this.resolveTrackerInOldMessages(tracker)) {
268
266
  this.trackers.push(tracker);
269
267
  }
270
268
 
271
- return promise;
269
+ return Promise.race([promise, timer]).finally(() => {
270
+ if (!!timeoutPid) {
271
+ clearTimeout(timeoutPid);
272
+ }
273
+ });
272
274
  };
273
275
  }
274
276
 
@@ -279,10 +281,10 @@ export class WsListener<TSpyEvents> {
279
281
  await this.stop();
280
282
  };
281
283
 
282
- const proxy = new Proxy(spyListener, {
284
+ return new Proxy<ServerlessSpyListener<TSpyEvents>>(spyListener, {
283
285
  get: (target: any, objectKey: string) => {
284
286
  if (target.hasOwnProperty(objectKey)) {
285
- return target[objectKey];
287
+ return target[objectKey].bind(target);
286
288
  } else if (
287
289
  typeof objectKey === 'string' &&
288
290
  objectKey.startsWith(this.functionPrefix)
@@ -295,8 +297,6 @@ export class WsListener<TSpyEvents> {
295
297
  }
296
298
  },
297
299
  });
298
-
299
- return proxy as ServerlessSpyListener<TSpyEvents>;
300
300
  }
301
301
 
302
302
  private log(message: string, ...optionalParams: any[]) {
@@ -313,7 +313,6 @@ export class WsListener<TSpyEvents> {
313
313
 
314
314
  type Tracker = {
315
315
  promiseResolve: (data: any) => void;
316
- promiseReject: (data: any) => void;
317
316
  finished: boolean;
318
317
  serviceKey?: string;
319
318
  serviceKeyForFunction?: string;
package/package.json CHANGED
@@ -136,7 +136,7 @@
136
136
  "require": "./lib/index.js"
137
137
  },
138
138
  "license": "MPL-2.0",
139
- "version": "2.2.1",
139
+ "version": "2.2.3",
140
140
  "types": "lib/index.d.ts",
141
141
  "stability": "stable",
142
142
  "jsii": {