|
1 | 1 | import { Hub } from '@sentry/core'; |
2 | 2 | import { EventProcessor, Integration, SpanContext } from '@sentry/types'; |
3 | | -import { fill, isInstanceOf, isThenable, loadModule, logger } from '@sentry/utils'; |
4 | | -import { EventEmitter } from 'events'; |
| 3 | +import { fill, isThenable, loadModule, logger } from '@sentry/utils'; |
5 | 4 |
|
6 | 5 | import { shouldDisableAutoInstrumentation } from './utils/node-utils'; |
7 | 6 |
|
@@ -91,6 +90,14 @@ interface MongoOptions { |
91 | 90 | useMongoose?: boolean; |
92 | 91 | } |
93 | 92 |
|
| 93 | +interface MongoCursor { |
| 94 | + once(event: 'close', listener: () => void): void; |
| 95 | +} |
| 96 | + |
| 97 | +function isCursor(maybeCursor: MongoCursor): maybeCursor is MongoCursor { |
| 98 | + return maybeCursor && typeof maybeCursor === 'object' && maybeCursor.once && typeof maybeCursor.once === 'function'; |
| 99 | +} |
| 100 | + |
94 | 101 | /** Tracing integration for mongo package */ |
95 | 102 | export class Mongo implements Integration { |
96 | 103 | /** |
@@ -169,10 +176,10 @@ export class Mongo implements Integration { |
169 | 176 | return res; |
170 | 177 | }); |
171 | 178 | } |
172 | | - // If the operation returns a cursor (which is an EventEmitter), |
| 179 | + // If the operation returns a Cursor |
173 | 180 | // we need to attach a listener to it to finish the span when the cursor is closed. |
174 | | - else if (isInstanceOf(maybePromiseOrCursor, EventEmitter)) { |
175 | | - const cursor = maybePromiseOrCursor as EventEmitter; |
| 181 | + else if (isCursor(maybePromiseOrCursor)) { |
| 182 | + const cursor = maybePromiseOrCursor as MongoCursor; |
176 | 183 |
|
177 | 184 | try { |
178 | 185 | cursor.once('close', () => { |
|
0 commit comments