mongodb 6.5.0-dev.20240418.sha.af18c53 → 6.5.0-dev.20240420.sha.eece8c1

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/src/timeout.ts ADDED
@@ -0,0 +1,100 @@
1
+ import { clearTimeout, setTimeout } from 'timers';
2
+
3
+ import { MongoInvalidArgumentError } from './error';
4
+ import { noop } from './utils';
5
+
6
+ /** @internal */
7
+ export class TimeoutError extends Error {
8
+ override get name(): 'TimeoutError' {
9
+ return 'TimeoutError';
10
+ }
11
+
12
+ constructor(message: string, options?: { cause?: Error }) {
13
+ super(message, options);
14
+ }
15
+
16
+ static is(error: unknown): error is TimeoutError {
17
+ return (
18
+ error != null && typeof error === 'object' && 'name' in error && error.name === 'TimeoutError'
19
+ );
20
+ }
21
+ }
22
+
23
+ type Executor = ConstructorParameters<typeof Promise<never>>[0];
24
+ type Reject = Parameters<ConstructorParameters<typeof Promise<never>>[0]>[1];
25
+ /**
26
+ * @internal
27
+ * This class is an abstraction over timeouts
28
+ * The Timeout class can only be in the pending or rejected states. It is guaranteed not to resolve
29
+ * if interacted with exclusively through its public API
30
+ * */
31
+ export class Timeout extends Promise<never> {
32
+ get [Symbol.toStringTag](): 'MongoDBTimeout' {
33
+ return 'MongoDBTimeout';
34
+ }
35
+
36
+ private timeoutError: TimeoutError;
37
+ private id?: NodeJS.Timeout;
38
+
39
+ public readonly start: number;
40
+ public ended: number | null = null;
41
+ public duration: number;
42
+ public timedOut = false;
43
+
44
+ /** Create a new timeout that expires in `duration` ms */
45
+ private constructor(executor: Executor = () => null, duration: number) {
46
+ let reject!: Reject;
47
+
48
+ if (duration < 0) {
49
+ throw new MongoInvalidArgumentError('Cannot create a Timeout with a negative duration');
50
+ }
51
+
52
+ super((_, promiseReject) => {
53
+ reject = promiseReject;
54
+
55
+ executor(noop, promiseReject);
56
+ });
57
+
58
+ // NOTE: Construct timeout error at point of Timeout instantiation to preserve stack traces
59
+ this.timeoutError = new TimeoutError(`Expired after ${duration}ms`);
60
+
61
+ this.duration = duration;
62
+ this.start = Math.trunc(performance.now());
63
+
64
+ if (this.duration > 0) {
65
+ this.id = setTimeout(() => {
66
+ this.ended = Math.trunc(performance.now());
67
+ this.timedOut = true;
68
+ reject(this.timeoutError);
69
+ }, this.duration);
70
+ // Ensure we do not keep the Node.js event loop running
71
+ if (typeof this.id.unref === 'function') {
72
+ this.id.unref();
73
+ }
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Clears the underlying timeout. This method is idempotent
79
+ */
80
+ clear(): void {
81
+ clearTimeout(this.id);
82
+ this.id = undefined;
83
+ }
84
+
85
+ public static expires(durationMS: number): Timeout {
86
+ return new Timeout(undefined, durationMS);
87
+ }
88
+
89
+ static is(timeout: unknown): timeout is Timeout {
90
+ return (
91
+ typeof timeout === 'object' &&
92
+ timeout != null &&
93
+ Symbol.toStringTag in timeout &&
94
+ timeout[Symbol.toStringTag] === 'MongoDBTimeout' &&
95
+ 'then' in timeout &&
96
+ // eslint-disable-next-line github/no-then
97
+ typeof timeout.then === 'function'
98
+ );
99
+ }
100
+ }
package/src/utils.ts CHANGED
@@ -3,7 +3,6 @@ import type { SrvRecord } from 'dns';
3
3
  import { type EventEmitter } from 'events';
4
4
  import { promises as fs } from 'fs';
5
5
  import * as http from 'http';
6
- import { clearTimeout, setTimeout } from 'timers';
7
6
  import * as url from 'url';
8
7
  import { URL } from 'url';
9
8
  import { promisify } from 'util';
@@ -1203,33 +1202,6 @@ export async function request(
1203
1202
  });
1204
1203
  }
1205
1204
 
1206
- /**
1207
- * A custom AbortController that aborts after a specified timeout.
1208
- *
1209
- * If `timeout` is undefined or \<=0, the abort controller never aborts.
1210
- *
1211
- * This class provides two benefits over the built-in AbortSignal.timeout() method.
1212
- * - This class provides a mechanism for cancelling the timeout
1213
- * - This class supports infinite timeouts by interpreting a timeout of 0 as infinite. This is
1214
- * consistent with existing timeout options in the Node driver (serverSelectionTimeoutMS, for example).
1215
- * @internal
1216
- */
1217
- export class TimeoutController extends AbortController {
1218
- constructor(
1219
- timeout = 0,
1220
- private timeoutId = timeout > 0 ? setTimeout(() => this.abort(), timeout) : null
1221
- ) {
1222
- super();
1223
- }
1224
-
1225
- clear() {
1226
- if (this.timeoutId != null) {
1227
- clearTimeout(this.timeoutId);
1228
- }
1229
- this.timeoutId = null;
1230
- }
1231
- }
1232
-
1233
1205
  /** @internal */
1234
1206
  export const DOCUMENT_DB_CHECK = /(\.docdb\.amazonaws\.com$)|(\.docdb-elastic\.amazonaws\.com$)/;
1235
1207
  /** @internal */
@@ -1344,3 +1316,7 @@ export async function fileIsAccessible(fileName: string, mode?: number) {
1344
1316
  return false;
1345
1317
  }
1346
1318
  }
1319
+
1320
+ export function noop() {
1321
+ return;
1322
+ }