mongodb 4.1.0 → 4.1.4

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.
Files changed (209) hide show
  1. package/README.md +64 -32
  2. package/lib/admin.js +5 -5
  3. package/lib/admin.js.map +1 -1
  4. package/lib/bulk/common.js +93 -71
  5. package/lib/bulk/common.js.map +1 -1
  6. package/lib/change_stream.js +35 -27
  7. package/lib/change_stream.js.map +1 -1
  8. package/lib/cmap/auth/auth_provider.js +2 -2
  9. package/lib/cmap/auth/auth_provider.js.map +1 -1
  10. package/lib/cmap/auth/gssapi.js +3 -2
  11. package/lib/cmap/auth/gssapi.js.map +1 -1
  12. package/lib/cmap/auth/mongo_credentials.js +8 -7
  13. package/lib/cmap/auth/mongo_credentials.js.map +1 -1
  14. package/lib/cmap/auth/mongocr.js +2 -2
  15. package/lib/cmap/auth/mongocr.js.map +1 -1
  16. package/lib/cmap/auth/mongodb_aws.js +32 -32
  17. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  18. package/lib/cmap/auth/plain.js +1 -1
  19. package/lib/cmap/auth/plain.js.map +1 -1
  20. package/lib/cmap/auth/scram.js +11 -8
  21. package/lib/cmap/auth/scram.js.map +1 -1
  22. package/lib/cmap/auth/x509.js +1 -1
  23. package/lib/cmap/auth/x509.js.map +1 -1
  24. package/lib/cmap/command_monitoring_events.js +15 -15
  25. package/lib/cmap/command_monitoring_events.js.map +1 -1
  26. package/lib/cmap/commands.js +6 -6
  27. package/lib/cmap/commands.js.map +1 -1
  28. package/lib/cmap/connect.js +5 -4
  29. package/lib/cmap/connect.js.map +1 -1
  30. package/lib/cmap/connection.js +27 -23
  31. package/lib/cmap/connection.js.map +1 -1
  32. package/lib/cmap/connection_pool.js +7 -5
  33. package/lib/cmap/connection_pool.js.map +1 -1
  34. package/lib/cmap/message_stream.js +3 -3
  35. package/lib/cmap/message_stream.js.map +1 -1
  36. package/lib/cmap/stream_description.js +1 -1
  37. package/lib/cmap/stream_description.js.map +1 -1
  38. package/lib/cmap/wire_protocol/compression.js +22 -9
  39. package/lib/cmap/wire_protocol/compression.js.map +1 -1
  40. package/lib/collection.js +53 -48
  41. package/lib/collection.js.map +1 -1
  42. package/lib/connection_string.js +42 -36
  43. package/lib/connection_string.js.map +1 -1
  44. package/lib/cursor/abstract_cursor.js +18 -18
  45. package/lib/cursor/abstract_cursor.js.map +1 -1
  46. package/lib/cursor/aggregation_cursor.js +56 -15
  47. package/lib/cursor/aggregation_cursor.js.map +1 -1
  48. package/lib/cursor/find_cursor.js +63 -23
  49. package/lib/cursor/find_cursor.js.map +1 -1
  50. package/lib/db.js +35 -30
  51. package/lib/db.js.map +1 -1
  52. package/lib/deps.js +8 -1
  53. package/lib/deps.js.map +1 -1
  54. package/lib/error.js +16 -54
  55. package/lib/error.js.map +1 -1
  56. package/lib/gridfs/download.js +14 -24
  57. package/lib/gridfs/download.js.map +1 -1
  58. package/lib/gridfs/index.js +5 -5
  59. package/lib/gridfs/index.js.map +1 -1
  60. package/lib/gridfs/upload.js +13 -21
  61. package/lib/gridfs/upload.js.map +1 -1
  62. package/lib/index.js +22 -2
  63. package/lib/index.js.map +1 -1
  64. package/lib/logger.js +5 -5
  65. package/lib/logger.js.map +1 -1
  66. package/lib/mongo_client.js +9 -8
  67. package/lib/mongo_client.js.map +1 -1
  68. package/lib/mongo_types.js.map +1 -1
  69. package/lib/operations/add_user.js +3 -3
  70. package/lib/operations/add_user.js.map +1 -1
  71. package/lib/operations/aggregate.js +2 -2
  72. package/lib/operations/aggregate.js.map +1 -1
  73. package/lib/operations/bulk_write.js +1 -1
  74. package/lib/operations/bulk_write.js.map +1 -1
  75. package/lib/operations/command.js +4 -6
  76. package/lib/operations/command.js.map +1 -1
  77. package/lib/operations/common_functions.js +2 -2
  78. package/lib/operations/common_functions.js.map +1 -1
  79. package/lib/operations/connect.js +3 -2
  80. package/lib/operations/connect.js.map +1 -1
  81. package/lib/operations/count.js +1 -1
  82. package/lib/operations/count.js.map +1 -1
  83. package/lib/operations/count_documents.js.map +1 -1
  84. package/lib/operations/create_collection.js +1 -1
  85. package/lib/operations/create_collection.js.map +1 -1
  86. package/lib/operations/delete.js +6 -6
  87. package/lib/operations/delete.js.map +1 -1
  88. package/lib/operations/distinct.js +5 -5
  89. package/lib/operations/distinct.js.map +1 -1
  90. package/lib/operations/drop.js +2 -2
  91. package/lib/operations/drop.js.map +1 -1
  92. package/lib/operations/estimated_document_count.js +2 -2
  93. package/lib/operations/estimated_document_count.js.map +1 -1
  94. package/lib/operations/eval.js.map +1 -1
  95. package/lib/operations/execute_operation.js +10 -15
  96. package/lib/operations/execute_operation.js.map +1 -1
  97. package/lib/operations/find.js +8 -9
  98. package/lib/operations/find.js.map +1 -1
  99. package/lib/operations/find_and_modify.js +7 -7
  100. package/lib/operations/find_and_modify.js.map +1 -1
  101. package/lib/operations/indexes.js +14 -14
  102. package/lib/operations/indexes.js.map +1 -1
  103. package/lib/operations/insert.js +8 -6
  104. package/lib/operations/insert.js.map +1 -1
  105. package/lib/operations/is_capped.js +2 -1
  106. package/lib/operations/is_capped.js.map +1 -1
  107. package/lib/operations/list_collections.js +4 -4
  108. package/lib/operations/list_collections.js.map +1 -1
  109. package/lib/operations/list_databases.js +1 -1
  110. package/lib/operations/list_databases.js.map +1 -1
  111. package/lib/operations/map_reduce.js +6 -6
  112. package/lib/operations/map_reduce.js.map +1 -1
  113. package/lib/operations/operation.js +1 -1
  114. package/lib/operations/operation.js.map +1 -1
  115. package/lib/operations/options_operation.js +2 -1
  116. package/lib/operations/options_operation.js.map +1 -1
  117. package/lib/operations/profiling_level.js +2 -2
  118. package/lib/operations/profiling_level.js.map +1 -1
  119. package/lib/operations/remove_user.js +1 -1
  120. package/lib/operations/remove_user.js.map +1 -1
  121. package/lib/operations/rename.js +2 -2
  122. package/lib/operations/rename.js.map +1 -1
  123. package/lib/operations/set_profiling_level.js +2 -2
  124. package/lib/operations/set_profiling_level.js.map +1 -1
  125. package/lib/operations/stats.js +2 -2
  126. package/lib/operations/stats.js.map +1 -1
  127. package/lib/operations/update.js +12 -12
  128. package/lib/operations/update.js.map +1 -1
  129. package/lib/operations/validate_collection.js +4 -4
  130. package/lib/operations/validate_collection.js.map +1 -1
  131. package/lib/sdam/monitor.js +14 -14
  132. package/lib/sdam/monitor.js.map +1 -1
  133. package/lib/sdam/server.js +16 -17
  134. package/lib/sdam/server.js.map +1 -1
  135. package/lib/sdam/server_description.js +3 -3
  136. package/lib/sdam/server_description.js.map +1 -1
  137. package/lib/sdam/srv_polling.js +1 -2
  138. package/lib/sdam/srv_polling.js.map +1 -1
  139. package/lib/sdam/topology.js +21 -18
  140. package/lib/sdam/topology.js.map +1 -1
  141. package/lib/sdam/topology_description.js +1 -1
  142. package/lib/sdam/topology_description.js.map +1 -1
  143. package/lib/sessions.js +33 -33
  144. package/lib/sessions.js.map +1 -1
  145. package/lib/transactions.js +2 -2
  146. package/lib/transactions.js.map +1 -1
  147. package/lib/utils.js +15 -37
  148. package/lib/utils.js.map +1 -1
  149. package/mongodb.d.ts +384 -121
  150. package/mongodb.ts34.d.ts +365 -124
  151. package/package.json +53 -58
  152. package/src/bulk/common.ts +105 -61
  153. package/src/change_stream.ts +41 -22
  154. package/src/cmap/auth/auth_provider.ts +3 -3
  155. package/src/cmap/auth/gssapi.ts +3 -2
  156. package/src/cmap/auth/mongo_credentials.ts +21 -10
  157. package/src/cmap/auth/mongodb_aws.ts +41 -36
  158. package/src/cmap/auth/scram.ts +6 -4
  159. package/src/cmap/command_monitoring_events.ts +1 -1
  160. package/src/cmap/commands.ts +6 -6
  161. package/src/cmap/connect.ts +5 -4
  162. package/src/cmap/connection.ts +15 -12
  163. package/src/cmap/connection_pool.ts +5 -3
  164. package/src/cmap/message_stream.ts +2 -4
  165. package/src/cmap/wire_protocol/compression.ts +26 -13
  166. package/src/collection.ts +54 -56
  167. package/src/connection_string.ts +37 -26
  168. package/src/cursor/abstract_cursor.ts +45 -35
  169. package/src/cursor/aggregation_cursor.ts +30 -9
  170. package/src/cursor/find_cursor.ts +27 -7
  171. package/src/db.ts +14 -14
  172. package/src/deps.ts +103 -23
  173. package/src/error.ts +33 -66
  174. package/src/gridfs/download.ts +37 -27
  175. package/src/gridfs/index.ts +3 -3
  176. package/src/gridfs/upload.ts +32 -33
  177. package/src/index.ts +34 -3
  178. package/src/mongo_client.ts +9 -13
  179. package/src/mongo_types.ts +22 -26
  180. package/src/operations/command.ts +2 -5
  181. package/src/operations/common_functions.ts +2 -3
  182. package/src/operations/connect.ts +3 -2
  183. package/src/operations/count.ts +1 -1
  184. package/src/operations/count_documents.ts +1 -1
  185. package/src/operations/distinct.ts +1 -1
  186. package/src/operations/eval.ts +1 -1
  187. package/src/operations/execute_operation.ts +9 -10
  188. package/src/operations/find.ts +7 -5
  189. package/src/operations/indexes.ts +2 -2
  190. package/src/operations/insert.ts +4 -1
  191. package/src/operations/is_capped.ts +3 -2
  192. package/src/operations/list_collections.ts +3 -1
  193. package/src/operations/list_databases.ts +6 -1
  194. package/src/operations/operation.ts +0 -1
  195. package/src/operations/options_operation.ts +3 -2
  196. package/src/operations/profiling_level.ts +3 -3
  197. package/src/operations/set_profiling_level.ts +2 -2
  198. package/src/operations/validate_collection.ts +5 -5
  199. package/src/sdam/server.ts +6 -7
  200. package/src/sdam/srv_polling.ts +2 -3
  201. package/src/sdam/topology.ts +15 -9
  202. package/src/sdam/topology_description.ts +2 -2
  203. package/src/sessions.ts +20 -16
  204. package/src/transactions.ts +3 -3
  205. package/src/utils.ts +20 -41
  206. package/HISTORY.md +0 -3026
  207. package/lib/operations/find_one.js +0 -34
  208. package/lib/operations/find_one.js.map +0 -1
  209. package/src/operations/find_one.ts +0 -43
package/src/db.ts CHANGED
@@ -9,7 +9,7 @@ import {
9
9
  import { AggregationCursor } from './cursor/aggregation_cursor';
10
10
  import { Document, BSONSerializeOptions, resolveBSONOptions } from './bson';
11
11
  import { ReadPreference, ReadPreferenceLike } from './read_preference';
12
- import { MongoDriverError, MongoInvalidArgumentError } from './error';
12
+ import { MongoAPIError, MongoInvalidArgumentError } from './error';
13
13
  import { Collection, CollectionOptions } from './collection';
14
14
  import { ChangeStream, ChangeStreamOptions } from './change_stream';
15
15
  import * as CONSTANTS from './constants';
@@ -289,7 +289,10 @@ export class Db {
289
289
  * @param pipeline - An array of aggregation stages to be executed
290
290
  * @param options - Optional settings for the command
291
291
  */
292
- aggregate(pipeline: Document[] = [], options?: AggregateOptions): AggregationCursor {
292
+ aggregate<T = Document>(
293
+ pipeline: Document[] = [],
294
+ options?: AggregateOptions
295
+ ): AggregationCursor<T> {
293
296
  if (arguments.length > 2) {
294
297
  throw new MongoInvalidArgumentError('Method "db.aggregate()" accepts at most two arguments');
295
298
  }
@@ -319,16 +322,12 @@ export class Db {
319
322
  * @param name - the collection name we wish to access.
320
323
  * @returns return the new Collection instance
321
324
  */
322
- collection<TSchema extends Document = Document>(name: string): Collection<TSchema>;
323
325
  collection<TSchema extends Document = Document>(
324
326
  name: string,
325
- options?: CollectionOptions
327
+ options: CollectionOptions = {}
326
328
  ): Collection<TSchema> {
327
- if (!options) {
328
- options = {};
329
- } else if (typeof options === 'function') {
330
- // TODO(NODE-3485): Replace this with MongoDeprecationError
331
- throw new MongoDriverError('The callback form of this helper has been removed.');
329
+ if (typeof options === 'function') {
330
+ throw new MongoInvalidArgumentError('The callback form of this helper has been removed.');
332
331
  }
333
332
  const finalOptions = resolveOptions(this, options);
334
333
  return new Collection<TSchema>(this, name, finalOptions);
@@ -701,7 +700,10 @@ export class Db {
701
700
  );
702
701
  }
703
702
 
704
- /** Unref all sockets */
703
+ /**
704
+ * Unref all sockets
705
+ * @deprecated This function is deprecated and will be removed in the next major version.
706
+ */
705
707
  unref(): void {
706
708
  getTopology(this).unref();
707
709
  }
@@ -737,6 +739,7 @@ export class Db {
737
739
  }
738
740
  }
739
741
 
742
+ // TODO(NODE-3484): Refactor into MongoDBNamespace
740
743
  // Validate the database name
741
744
  function validateDatabaseName(databaseName: string) {
742
745
  if (typeof databaseName !== 'string')
@@ -748,9 +751,6 @@ function validateDatabaseName(databaseName: string) {
748
751
  const invalidChars = [' ', '.', '$', '/', '\\'];
749
752
  for (let i = 0; i < invalidChars.length; i++) {
750
753
  if (databaseName.indexOf(invalidChars[i]) !== -1)
751
- // TODO(NODE-3405): Change this out for a child of MongoParseError
752
- throw new MongoDriverError(
753
- `database names cannot contain the character '${invalidChars[i]}'`
754
- );
754
+ throw new MongoAPIError(`database names cannot contain the character '${invalidChars[i]}'`);
755
755
  }
756
756
  }
package/src/deps.ts CHANGED
@@ -1,7 +1,10 @@
1
+ /* eslint-disable @typescript-eslint/no-var-requires */
1
2
  import { MongoMissingDependencyError } from './error';
2
3
  import type { MongoClient } from './mongo_client';
3
4
  import type { deserialize, Document, serialize } from './bson';
4
- import type { Callback } from './utils';
5
+ import { Callback, parsePackageVersion } from './utils';
6
+
7
+ export const PKG_VERSION = Symbol('kPkgVersion');
5
8
 
6
9
  function makeErrorModule(error: any) {
7
10
  const props = error ? { kModuleError: error } : {};
@@ -18,13 +21,12 @@ function makeErrorModule(error: any) {
18
21
  });
19
22
  }
20
23
 
21
- export let Kerberos:
22
- | typeof import('kerberos')
23
- | { kModuleError: MongoMissingDependencyError } = makeErrorModule(
24
- new MongoMissingDependencyError(
25
- 'Optional module `kerberos` not found. Please install it to enable kerberos authentication'
26
- )
27
- );
24
+ export let Kerberos: typeof import('kerberos') | { kModuleError: MongoMissingDependencyError } =
25
+ makeErrorModule(
26
+ new MongoMissingDependencyError(
27
+ 'Optional module `kerberos` not found. Please install it to enable kerberos authentication'
28
+ )
29
+ );
28
30
 
29
31
  try {
30
32
  // Ensure you always wrap an optional require in the try block NODE-3199
@@ -41,9 +43,46 @@ export interface KerberosClient {
41
43
  unwrap: (challenge: string, callback?: Callback<string>) => Promise<string> | void;
42
44
  }
43
45
 
44
- export let Snappy:
45
- | typeof import('snappy')
46
- | { kModuleError: MongoMissingDependencyError } = makeErrorModule(
46
+ type SnappyLib = {
47
+ [PKG_VERSION]: { major: number; minor: number; patch: number };
48
+
49
+ /**
50
+ * - Snappy 6.x takes a callback and returns void
51
+ * - Snappy 7.x returns a promise
52
+ *
53
+ * In order to support both we must check the return value of the function
54
+ * @param buf - Buffer to be compressed
55
+ * @param callback - ONLY USED IN SNAPPY 6.x
56
+ */
57
+ compress(buf: Buffer): Promise<Buffer>;
58
+ compress(buf: Buffer, callback: (error?: Error, buffer?: Buffer) => void): Promise<Buffer> | void;
59
+ compress(
60
+ buf: Buffer,
61
+ callback?: (error?: Error, buffer?: Buffer) => void
62
+ ): Promise<Buffer> | void;
63
+
64
+ /**
65
+ * - Snappy 6.x takes a callback and returns void
66
+ * - Snappy 7.x returns a promise
67
+ *
68
+ * In order to support both we must check the return value of the function
69
+ * @param buf - Buffer to be compressed
70
+ * @param callback - ONLY USED IN SNAPPY 6.x
71
+ */
72
+ uncompress(buf: Buffer, opt: { asBuffer: true }): Promise<Buffer>;
73
+ uncompress(
74
+ buf: Buffer,
75
+ opt: { asBuffer: true },
76
+ callback: (error?: Error, buffer?: Buffer) => void
77
+ ): Promise<Buffer> | void;
78
+ uncompress(
79
+ buf: Buffer,
80
+ opt: { asBuffer: true },
81
+ callback?: (error?: Error, buffer?: Buffer) => void
82
+ ): Promise<Buffer> | void;
83
+ };
84
+
85
+ export let Snappy: SnappyLib | { kModuleError: MongoMissingDependencyError } = makeErrorModule(
47
86
  new MongoMissingDependencyError(
48
87
  'Optional module `snappy` not found. Please install it to enable snappy compression'
49
88
  )
@@ -52,25 +91,65 @@ export let Snappy:
52
91
  try {
53
92
  // Ensure you always wrap an optional require in the try block NODE-3199
54
93
  Snappy = require('snappy');
94
+ try {
95
+ (Snappy as any)[PKG_VERSION] = parsePackageVersion(require('snappy/package.json'));
96
+ } catch {} // eslint-disable-line
55
97
  } catch {} // eslint-disable-line
56
98
 
57
- export let saslprep:
58
- | typeof import('saslprep')
59
- | { kModuleError: MongoMissingDependencyError } = makeErrorModule(
60
- new MongoMissingDependencyError(
61
- 'Optional module `saslprep` not found.' +
62
- ' Please install it to enable Stringprep Profile for User Names and Passwords'
63
- )
64
- );
99
+ export let saslprep: typeof import('saslprep') | { kModuleError: MongoMissingDependencyError } =
100
+ makeErrorModule(
101
+ new MongoMissingDependencyError(
102
+ 'Optional module `saslprep` not found.' +
103
+ ' Please install it to enable Stringprep Profile for User Names and Passwords'
104
+ )
105
+ );
65
106
 
66
107
  try {
67
108
  // Ensure you always wrap an optional require in the try block NODE-3199
68
109
  saslprep = require('saslprep');
69
110
  } catch {} // eslint-disable-line
70
111
 
71
- export let aws4:
72
- | typeof import('aws4')
73
- | { kModuleError: MongoMissingDependencyError } = makeErrorModule(
112
+ interface AWS4 {
113
+ /**
114
+ * Created these inline types to better assert future usage of this API
115
+ * @param options - options for request
116
+ * @param credentials - AWS credential details, sessionToken should be omitted entirely if its false-y
117
+ */
118
+ sign(
119
+ options: {
120
+ path: '/';
121
+ body: string;
122
+ host: string;
123
+ method: 'POST';
124
+ headers: {
125
+ 'Content-Type': 'application/x-www-form-urlencoded';
126
+ 'Content-Length': number;
127
+ 'X-MongoDB-Server-Nonce': string;
128
+ 'X-MongoDB-GS2-CB-Flag': 'n';
129
+ };
130
+ service: string;
131
+ region: string;
132
+ },
133
+ credentials:
134
+ | {
135
+ accessKeyId: string;
136
+ secretAccessKey: string;
137
+ sessionToken: string;
138
+ }
139
+ | {
140
+ accessKeyId: string;
141
+ secretAccessKey: string;
142
+ }
143
+ | undefined
144
+ ): {
145
+ headers: {
146
+ Authorization: string;
147
+ 'X-Amz-Date': string;
148
+ };
149
+ };
150
+ }
151
+
152
+ export let aws4: AWS4 | { kModuleError: MongoMissingDependencyError } = makeErrorModule(
74
153
  new MongoMissingDependencyError(
75
154
  'Optional module `aws4` not found. Please install it to enable AWS authentication'
76
155
  )
@@ -91,7 +170,8 @@ export const AutoEncryptionLoggerLevel = Object.freeze({
91
170
  } as const);
92
171
 
93
172
  /** @public */
94
- export type AutoEncryptionLoggerLevel = typeof AutoEncryptionLoggerLevel[keyof typeof AutoEncryptionLoggerLevel];
173
+ export type AutoEncryptionLoggerLevel =
174
+ typeof AutoEncryptionLoggerLevel[keyof typeof AutoEncryptionLoggerLevel];
95
175
 
96
176
  /** @public */
97
177
  export interface AutoEncryptionOptions {
package/src/error.ts CHANGED
@@ -60,12 +60,12 @@ export const GET_MORE_RESUMABLE_CODES = new Set<number>([
60
60
  ]);
61
61
 
62
62
  /** @public */
63
- export interface ErrorDescription {
63
+ export interface ErrorDescription extends Document {
64
64
  message?: string;
65
65
  errmsg?: string;
66
66
  $err?: string;
67
67
  errorLabels?: string[];
68
- [key: string]: any;
68
+ errInfo?: Document;
69
69
  }
70
70
 
71
71
  /**
@@ -78,13 +78,17 @@ export interface ErrorDescription {
78
78
  export class MongoError extends Error {
79
79
  /** @internal */
80
80
  [kErrorLabels]: Set<string>;
81
+ /**
82
+ * This is a number in MongoServerError and a string in MongoDriverError
83
+ * @privateRemarks
84
+ * Define the type override on the subclasses when we can use the override keyword
85
+ */
81
86
  code?: number | string;
82
87
  topologyVersion?: TopologyVersion;
83
88
 
84
89
  constructor(message: string | Error) {
85
90
  if (message instanceof Error) {
86
91
  super(message.message);
87
- this.stack = message.stack;
88
92
  } else {
89
93
  super(message);
90
94
  }
@@ -133,26 +137,21 @@ export class MongoError extends Error {
133
137
  * @category Error
134
138
  */
135
139
  export class MongoServerError extends MongoError {
136
- code?: number;
137
140
  codeName?: string;
138
141
  writeConcernError?: Document;
142
+ errInfo?: Document;
143
+ ok?: number;
144
+ [key: string]: any;
139
145
 
140
- constructor(message: Error | ErrorDescription) {
141
- if (message instanceof Error) {
142
- super(message);
143
- } else {
144
- super(message.message || message.errmsg || message.$err || 'n/a');
145
- if (message.errorLabels) {
146
- this[kErrorLabels] = new Set(message.errorLabels);
147
- }
148
-
149
- for (const name in message) {
150
- if (name === 'errorLabels' || name === 'errmsg' || name === 'message') {
151
- continue;
152
- }
153
-
154
- (this as any)[name] = message[name];
155
- }
146
+ constructor(message: ErrorDescription) {
147
+ super(message.message || message.errmsg || message.$err || 'n/a');
148
+ if (message.errorLabels) {
149
+ this[kErrorLabels] = new Set(message.errorLabels);
150
+ }
151
+
152
+ for (const name in message) {
153
+ if (name !== 'errorLabels' && name !== 'errmsg' && name !== 'message')
154
+ this[name] = message[name];
156
155
  }
157
156
  }
158
157
 
@@ -168,7 +167,6 @@ export class MongoServerError extends MongoError {
168
167
  * @category Error
169
168
  */
170
169
  export class MongoDriverError extends MongoError {
171
- code?: string;
172
170
  constructor(message: string) {
173
171
  super(message);
174
172
  }
@@ -189,7 +187,7 @@ export class MongoDriverError extends MongoError {
189
187
  */
190
188
 
191
189
  export class MongoAPIError extends MongoDriverError {
192
- protected constructor(message: string) {
190
+ constructor(message: string) {
193
191
  super(message);
194
192
  }
195
193
 
@@ -209,7 +207,7 @@ export class MongoAPIError extends MongoDriverError {
209
207
  * @category Error
210
208
  */
211
209
  export class MongoRuntimeError extends MongoDriverError {
212
- protected constructor(message: string) {
210
+ constructor(message: string) {
213
211
  super(message);
214
212
  }
215
213
 
@@ -226,45 +224,12 @@ export class MongoRuntimeError extends MongoDriverError {
226
224
  * @category Error
227
225
  */
228
226
  export class MongoBatchReExecutionError extends MongoAPIError {
229
- constructor(message?: string) {
230
- super(message || 'This batch has already been executed, create new batch to execute');
231
- }
232
-
233
- get name(): string {
234
- return 'MongoBatchReExecutionError';
235
- }
236
- }
237
-
238
- /**
239
- * An error generated when the user supplies an incorrect URI to the driver.
240
- *
241
- * @public
242
- * @category Error
243
- */
244
- export class MongoURIError extends MongoRuntimeError {
245
- constructor(message: string) {
227
+ constructor(message = 'This batch has already been executed, create new batch to execute') {
246
228
  super(message);
247
229
  }
248
230
 
249
231
  get name(): string {
250
- return 'MongoURIError';
251
- }
252
- }
253
-
254
- /**
255
- * An error generated when the driver fails to compress data
256
- * before sending it to the server.
257
- *
258
- * @public
259
- * @category Error
260
- */
261
- export class MongoCompressionError extends MongoRuntimeError {
262
- constructor(message: string) {
263
- super(message);
264
- }
265
-
266
- get name(): string {
267
- return 'MongoCompressionError';
232
+ return 'MongoBatchReExecutionError';
268
233
  }
269
234
  }
270
235
 
@@ -327,7 +292,7 @@ export class MongoTransactionError extends MongoAPIError {
327
292
  * @category Error
328
293
  */
329
294
  export class MongoExpiredSessionError extends MongoAPIError {
330
- constructor(message: string) {
295
+ constructor(message = 'Cannot use a session that has ended') {
331
296
  super(message);
332
297
  }
333
298
 
@@ -376,8 +341,8 @@ export class MongoChangeStreamError extends MongoRuntimeError {
376
341
  * @category Error
377
342
  */
378
343
  export class MongoTailableCursorError extends MongoAPIError {
379
- constructor(message?: string) {
380
- super(message || 'Tailable cursor does not support this operation');
344
+ constructor(message = 'Tailable cursor does not support this operation') {
345
+ super(message);
381
346
  }
382
347
 
383
348
  get name(): string {
@@ -425,8 +390,8 @@ export class MongoGridFSChunkError extends MongoRuntimeError {
425
390
  * @category Error
426
391
  */
427
392
  export class MongoCursorInUseError extends MongoAPIError {
428
- constructor(message?: string) {
429
- super(message || 'Cursor is already initialized');
393
+ constructor(message = 'Cursor is already initialized') {
394
+ super(message);
430
395
  }
431
396
 
432
397
  get name(): string {
@@ -442,7 +407,7 @@ export class MongoCursorInUseError extends MongoAPIError {
442
407
  * @category Error
443
408
  */
444
409
  export class MongoServerClosedError extends MongoAPIError {
445
- constructor(message: string) {
410
+ constructor(message = 'Server is closed') {
446
411
  super(message);
447
412
  }
448
413
 
@@ -475,7 +440,7 @@ export class MongoCursorExhaustedError extends MongoAPIError {
475
440
  * @category Error
476
441
  */
477
442
  export class MongoTopologyClosedError extends MongoAPIError {
478
- constructor(message: string) {
443
+ constructor(message = 'Topology is closed') {
479
444
  super(message);
480
445
  }
481
446
 
@@ -682,13 +647,15 @@ function makeWriteConcernResultObject(input: any) {
682
647
  export class MongoWriteConcernError extends MongoServerError {
683
648
  /** The result document (provided if ok: 1) */
684
649
  result?: Document;
650
+ errInfo?: Document;
685
651
 
686
- constructor(message: ErrorDescription, result: Document) {
652
+ constructor(message: ErrorDescription, result?: Document) {
687
653
  if (result && Array.isArray(result.errorLabels)) {
688
654
  message.errorLabels = result.errorLabels;
689
655
  }
690
656
 
691
657
  super(message);
658
+ this.errInfo = message.errInfo;
692
659
 
693
660
  if (result != null) {
694
661
  this.result = makeWriteConcernResultObject(result);
@@ -1,5 +1,10 @@
1
1
  import { Readable } from 'stream';
2
- import { AnyError, MongoDriverError, MongoInvalidArgumentError } from '../error';
2
+ import {
3
+ MongoRuntimeError,
4
+ MongoGridFSChunkError,
5
+ MongoGridFSStreamError,
6
+ MongoInvalidArgumentError
7
+ } from '../error';
3
8
  import type { Document } from '../bson';
4
9
  import type { FindOptions } from '../operations/find';
5
10
  import type { Sort } from '../sort';
@@ -68,7 +73,7 @@ export interface GridFSBucketReadStreamPrivate {
68
73
  * Do not instantiate this class directly. Use `openDownloadStream()` instead.
69
74
  * @public
70
75
  */
71
- export class GridFSBucketReadStream extends Readable {
76
+ export class GridFSBucketReadStream extends Readable implements NodeJS.ReadableStream {
72
77
  /** @internal */
73
78
  s: GridFSBucketReadStreamPrivate;
74
79
 
@@ -195,7 +200,7 @@ export class GridFSBucketReadStream extends Readable {
195
200
 
196
201
  function throwIfInitialized(stream: GridFSBucketReadStream): void {
197
202
  if (stream.s.init) {
198
- throw new MongoDriverError('Options cannot be changed after the stream is initialized');
203
+ throw new MongoGridFSStreamError('Options cannot be changed after the stream is initialized');
199
204
  }
200
205
  }
201
206
 
@@ -209,7 +214,8 @@ function doRead(stream: GridFSBucketReadStream): void {
209
214
  return;
210
215
  }
211
216
  if (error) {
212
- return __handleError(stream, error);
217
+ stream.emit(GridFSBucketReadStream.ERROR, error);
218
+ return;
213
219
  }
214
220
  if (!doc) {
215
221
  stream.push(null);
@@ -218,7 +224,7 @@ function doRead(stream: GridFSBucketReadStream): void {
218
224
  if (!stream.s.cursor) return;
219
225
  stream.s.cursor.close(error => {
220
226
  if (error) {
221
- __handleError(stream, error);
227
+ stream.emit(GridFSBucketReadStream.ERROR, error);
222
228
  return;
223
229
  }
224
230
 
@@ -234,31 +240,38 @@ function doRead(stream: GridFSBucketReadStream): void {
234
240
  const bytesRemaining = stream.s.file.length - stream.s.bytesRead;
235
241
  const expectedN = stream.s.expected++;
236
242
  const expectedLength = Math.min(stream.s.file.chunkSize, bytesRemaining);
237
- let errmsg: string;
238
243
  if (doc.n > expectedN) {
239
- errmsg = `ChunkIsMissing: Got unexpected n: ${doc.n}, expected: ${expectedN}`;
240
- return __handleError(stream, new MongoDriverError(errmsg));
244
+ return stream.emit(
245
+ GridFSBucketReadStream.ERROR,
246
+ new MongoGridFSChunkError(
247
+ `ChunkIsMissing: Got unexpected n: ${doc.n}, expected: ${expectedN}`
248
+ )
249
+ );
241
250
  }
242
251
 
243
252
  if (doc.n < expectedN) {
244
- errmsg = `ExtraChunk: Got unexpected n: ${doc.n}, expected: ${expectedN}`;
245
- return __handleError(stream, new MongoDriverError(errmsg));
253
+ return stream.emit(
254
+ GridFSBucketReadStream.ERROR,
255
+ new MongoGridFSChunkError(`ExtraChunk: Got unexpected n: ${doc.n}, expected: ${expectedN}`)
256
+ );
246
257
  }
247
258
 
248
259
  let buf = Buffer.isBuffer(doc.data) ? doc.data : doc.data.buffer;
249
260
 
250
261
  if (buf.byteLength !== expectedLength) {
251
262
  if (bytesRemaining <= 0) {
252
- errmsg = `ExtraChunk: Got unexpected n: ${doc.n}`;
253
- return __handleError(stream, new MongoDriverError(errmsg));
263
+ return stream.emit(
264
+ GridFSBucketReadStream.ERROR,
265
+ new MongoGridFSChunkError(`ExtraChunk: Got unexpected n: ${doc.n}`)
266
+ );
254
267
  }
255
268
 
256
- errmsg =
257
- 'ChunkIsWrongSize: Got unexpected length: ' +
258
- buf.byteLength +
259
- ', expected: ' +
260
- expectedLength;
261
- return __handleError(stream, new MongoDriverError(errmsg));
269
+ return stream.emit(
270
+ GridFSBucketReadStream.ERROR,
271
+ new MongoGridFSChunkError(
272
+ `ChunkIsWrongSize: Got unexpected length: ${buf.byteLength}, expected: ${expectedLength}`
273
+ )
274
+ );
262
275
  }
263
276
 
264
277
  stream.s.bytesRead += buf.byteLength;
@@ -305,7 +318,7 @@ function init(stream: GridFSBucketReadStream): void {
305
318
 
306
319
  stream.s.files.findOne(stream.s.filter, findOneOptions, (error, doc) => {
307
320
  if (error) {
308
- return __handleError(stream, error);
321
+ return stream.emit(GridFSBucketReadStream.ERROR, error);
309
322
  }
310
323
 
311
324
  if (!doc) {
@@ -313,9 +326,10 @@ function init(stream: GridFSBucketReadStream): void {
313
326
  ? stream.s.filter._id.toString()
314
327
  : stream.s.filter.filename;
315
328
  const errmsg = `FileNotFound: file ${identifier} was not found`;
316
- const err = new MongoDriverError(errmsg);
329
+ // TODO(NODE-3483)
330
+ const err = new MongoRuntimeError(errmsg);
317
331
  err.code = 'ENOENT'; // TODO: NODE-3338 set property as part of constructor
318
- return __handleError(stream, err);
332
+ return stream.emit(GridFSBucketReadStream.ERROR, err);
319
333
  }
320
334
 
321
335
  // If document is empty, kill the stream immediately and don't
@@ -336,7 +350,7 @@ function init(stream: GridFSBucketReadStream): void {
336
350
  try {
337
351
  stream.s.bytesToSkip = handleStartOption(stream, doc, stream.s.options);
338
352
  } catch (error) {
339
- return __handleError(stream, error);
353
+ return stream.emit(GridFSBucketReadStream.ERROR, error);
340
354
  }
341
355
 
342
356
  const filter: Document = { files_id: doc._id };
@@ -362,7 +376,7 @@ function init(stream: GridFSBucketReadStream): void {
362
376
  try {
363
377
  stream.s.bytesToTrim = handleEndOption(stream, doc, stream.s.cursor, stream.s.options);
364
378
  } catch (error) {
365
- return __handleError(stream, error);
379
+ return stream.emit(GridFSBucketReadStream.ERROR, error);
366
380
  }
367
381
 
368
382
  stream.emit(GridFSBucketReadStream.FILE, doc);
@@ -438,7 +452,3 @@ function handleEndOption(
438
452
  }
439
453
  throw new MongoInvalidArgumentError('End option must be defined');
440
454
  }
441
-
442
- function __handleError(stream: GridFSBucketReadStream, error?: AnyError): void {
443
- stream.emit(GridFSBucketReadStream.ERROR, error);
444
- }
@@ -1,4 +1,4 @@
1
- import { MongoDriverError } from '../error';
1
+ import { MongoRuntimeError } from '../error';
2
2
  import {
3
3
  GridFSBucketReadStream,
4
4
  GridFSBucketReadStreamOptions,
@@ -229,7 +229,7 @@ function _delete(bucket: GridFSBucket, id: ObjectId, callback: Callback<void>):
229
229
  if (!res?.deletedCount) {
230
230
  // TODO(NODE-3483): Replace with more appropriate error
231
231
  // Consider creating new error MongoGridFSFileNotFoundError
232
- return callback(new MongoDriverError(`File not found for id ${id}`));
232
+ return callback(new MongoRuntimeError(`File not found for id ${id}`));
233
233
  }
234
234
 
235
235
  return callback();
@@ -251,7 +251,7 @@ function _rename(
251
251
  }
252
252
 
253
253
  if (!res?.matchedCount) {
254
- return callback(new MongoDriverError(`File with id ${id} not found`));
254
+ return callback(new MongoRuntimeError(`File with id ${id} not found`));
255
255
  }
256
256
 
257
257
  return callback();