change_stream.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ChangeStream = void 0;
  4. const collection_1 = require("./collection");
  5. const constants_1 = require("./constants");
  6. const change_stream_cursor_1 = require("./cursor/change_stream_cursor");
  7. const db_1 = require("./db");
  8. const error_1 = require("./error");
  9. const mongo_client_1 = require("./mongo_client");
  10. const mongo_types_1 = require("./mongo_types");
  11. const utils_1 = require("./utils");
  12. /** @internal */
  13. const kCursorStream = Symbol('cursorStream');
  14. /** @internal */
  15. const kClosed = Symbol('closed');
  16. /** @internal */
  17. const kMode = Symbol('mode');
  18. const CHANGE_STREAM_OPTIONS = [
  19. 'resumeAfter',
  20. 'startAfter',
  21. 'startAtOperationTime',
  22. 'fullDocument',
  23. 'fullDocumentBeforeChange',
  24. 'showExpandedEvents'
  25. ];
  26. const CHANGE_DOMAIN_TYPES = {
  27. COLLECTION: Symbol('Collection'),
  28. DATABASE: Symbol('Database'),
  29. CLUSTER: Symbol('Cluster')
  30. };
  31. const CHANGE_STREAM_EVENTS = [constants_1.RESUME_TOKEN_CHANGED, constants_1.END, constants_1.CLOSE];
  32. const NO_RESUME_TOKEN_ERROR = 'A change stream document has been received that lacks a resume token (_id).';
  33. const CHANGESTREAM_CLOSED_ERROR = 'ChangeStream is closed';
  34. /**
  35. * Creates a new Change Stream instance. Normally created using {@link Collection#watch|Collection.watch()}.
  36. * @public
  37. */
  38. class ChangeStream extends mongo_types_1.TypedEventEmitter {
  39. /**
  40. * @internal
  41. *
  42. * @param parent - The parent object that created this change stream
  43. * @param pipeline - An array of {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents
  44. */
  45. constructor(parent, pipeline = [], options = {}) {
  46. super();
  47. this.pipeline = pipeline;
  48. this.options = { ...options };
  49. delete this.options.writeConcern;
  50. if (parent instanceof collection_1.Collection) {
  51. this.type = CHANGE_DOMAIN_TYPES.COLLECTION;
  52. }
  53. else if (parent instanceof db_1.Db) {
  54. this.type = CHANGE_DOMAIN_TYPES.DATABASE;
  55. }
  56. else if (parent instanceof mongo_client_1.MongoClient) {
  57. this.type = CHANGE_DOMAIN_TYPES.CLUSTER;
  58. }
  59. else {
  60. throw new error_1.MongoChangeStreamError('Parent provided to ChangeStream constructor must be an instance of Collection, Db, or MongoClient');
  61. }
  62. this.parent = parent;
  63. this.namespace = parent.s.namespace;
  64. if (!this.options.readPreference && parent.readPreference) {
  65. this.options.readPreference = parent.readPreference;
  66. }
  67. // Create contained Change Stream cursor
  68. this.cursor = this._createChangeStreamCursor(options);
  69. this[kClosed] = false;
  70. this[kMode] = false;
  71. // Listen for any `change` listeners being added to ChangeStream
  72. this.on('newListener', eventName => {
  73. if (eventName === 'change' && this.cursor && this.listenerCount('change') === 0) {
  74. this._streamEvents(this.cursor);
  75. }
  76. });
  77. this.on('removeListener', eventName => {
  78. if (eventName === 'change' && this.listenerCount('change') === 0 && this.cursor) {
  79. this[kCursorStream]?.removeAllListeners('data');
  80. }
  81. });
  82. }
  83. /** @internal */
  84. get cursorStream() {
  85. return this[kCursorStream];
  86. }
  87. /** The cached resume token that is used to resume after the most recently returned change. */
  88. get resumeToken() {
  89. return this.cursor?.resumeToken;
  90. }
  91. /** Check if there is any document still available in the Change Stream */
  92. async hasNext() {
  93. this._setIsIterator();
  94. // Change streams must resume indefinitely while each resume event succeeds.
  95. // This loop continues until either a change event is received or until a resume attempt
  96. // fails.
  97. // eslint-disable-next-line no-constant-condition
  98. while (true) {
  99. try {
  100. const hasNext = await this.cursor.hasNext();
  101. return hasNext;
  102. }
  103. catch (error) {
  104. try {
  105. await this._processErrorIteratorMode(error);
  106. }
  107. catch (error) {
  108. try {
  109. await this.close();
  110. }
  111. catch {
  112. // We are not concerned with errors from close()
  113. }
  114. throw error;
  115. }
  116. }
  117. }
  118. }
  119. /** Get the next available document from the Change Stream. */
  120. async next() {
  121. this._setIsIterator();
  122. // Change streams must resume indefinitely while each resume event succeeds.
  123. // This loop continues until either a change event is received or until a resume attempt
  124. // fails.
  125. // eslint-disable-next-line no-constant-condition
  126. while (true) {
  127. try {
  128. const change = await this.cursor.next();
  129. const processedChange = this._processChange(change ?? null);
  130. return processedChange;
  131. }
  132. catch (error) {
  133. try {
  134. await this._processErrorIteratorMode(error);
  135. }
  136. catch (error) {
  137. try {
  138. await this.close();
  139. }
  140. catch {
  141. // We are not concerned with errors from close()
  142. }
  143. throw error;
  144. }
  145. }
  146. }
  147. }
  148. /**
  149. * Try to get the next available document from the Change Stream's cursor or `null` if an empty batch is returned
  150. */
  151. async tryNext() {
  152. this._setIsIterator();
  153. // Change streams must resume indefinitely while each resume event succeeds.
  154. // This loop continues until either a change event is received or until a resume attempt
  155. // fails.
  156. // eslint-disable-next-line no-constant-condition
  157. while (true) {
  158. try {
  159. const change = await this.cursor.tryNext();
  160. return change ?? null;
  161. }
  162. catch (error) {
  163. try {
  164. await this._processErrorIteratorMode(error);
  165. }
  166. catch (error) {
  167. try {
  168. await this.close();
  169. }
  170. catch {
  171. // We are not concerned with errors from close()
  172. }
  173. throw error;
  174. }
  175. }
  176. }
  177. }
  178. async *[Symbol.asyncIterator]() {
  179. if (this.closed) {
  180. return;
  181. }
  182. try {
  183. // Change streams run indefinitely as long as errors are resumable
  184. // So the only loop breaking condition is if `next()` throws
  185. while (true) {
  186. yield await this.next();
  187. }
  188. }
  189. finally {
  190. try {
  191. await this.close();
  192. }
  193. catch {
  194. // we're not concerned with errors from close()
  195. }
  196. }
  197. }
  198. /** Is the cursor closed */
  199. get closed() {
  200. return this[kClosed] || this.cursor.closed;
  201. }
  202. /** Close the Change Stream */
  203. async close() {
  204. this[kClosed] = true;
  205. const cursor = this.cursor;
  206. try {
  207. await cursor.close();
  208. }
  209. finally {
  210. this._endStream();
  211. }
  212. }
  213. /**
  214. * Return a modified Readable stream including a possible transform method.
  215. *
  216. * NOTE: When using a Stream to process change stream events, the stream will
  217. * NOT automatically resume in the case a resumable error is encountered.
  218. *
  219. * @throws MongoChangeStreamError if the underlying cursor or the change stream is closed
  220. */
  221. stream(options) {
  222. if (this.closed) {
  223. throw new error_1.MongoChangeStreamError(CHANGESTREAM_CLOSED_ERROR);
  224. }
  225. this.streamOptions = options;
  226. return this.cursor.stream(options);
  227. }
  228. /** @internal */
  229. _setIsEmitter() {
  230. if (this[kMode] === 'iterator') {
  231. // TODO(NODE-3485): Replace with MongoChangeStreamModeError
  232. throw new error_1.MongoAPIError('ChangeStream cannot be used as an EventEmitter after being used as an iterator');
  233. }
  234. this[kMode] = 'emitter';
  235. }
  236. /** @internal */
  237. _setIsIterator() {
  238. if (this[kMode] === 'emitter') {
  239. // TODO(NODE-3485): Replace with MongoChangeStreamModeError
  240. throw new error_1.MongoAPIError('ChangeStream cannot be used as an iterator after being used as an EventEmitter');
  241. }
  242. this[kMode] = 'iterator';
  243. }
  244. /**
  245. * Create a new change stream cursor based on self's configuration
  246. * @internal
  247. */
  248. _createChangeStreamCursor(options) {
  249. const changeStreamStageOptions = (0, utils_1.filterOptions)(options, CHANGE_STREAM_OPTIONS);
  250. if (this.type === CHANGE_DOMAIN_TYPES.CLUSTER) {
  251. changeStreamStageOptions.allChangesForCluster = true;
  252. }
  253. const pipeline = [{ $changeStream: changeStreamStageOptions }, ...this.pipeline];
  254. const client = this.type === CHANGE_DOMAIN_TYPES.CLUSTER
  255. ? this.parent
  256. : this.type === CHANGE_DOMAIN_TYPES.DATABASE
  257. ? this.parent.client
  258. : this.type === CHANGE_DOMAIN_TYPES.COLLECTION
  259. ? this.parent.client
  260. : null;
  261. if (client == null) {
  262. // This should never happen because of the assertion in the constructor
  263. throw new error_1.MongoRuntimeError(`Changestream type should only be one of cluster, database, collection. Found ${this.type.toString()}`);
  264. }
  265. const changeStreamCursor = new change_stream_cursor_1.ChangeStreamCursor(client, this.namespace, pipeline, options);
  266. for (const event of CHANGE_STREAM_EVENTS) {
  267. changeStreamCursor.on(event, e => this.emit(event, e));
  268. }
  269. if (this.listenerCount(ChangeStream.CHANGE) > 0) {
  270. this._streamEvents(changeStreamCursor);
  271. }
  272. return changeStreamCursor;
  273. }
  274. /** @internal */
  275. _closeEmitterModeWithError(error) {
  276. this.emit(ChangeStream.ERROR, error);
  277. this.close().catch(() => null);
  278. }
  279. /** @internal */
  280. _streamEvents(cursor) {
  281. this._setIsEmitter();
  282. const stream = this[kCursorStream] ?? cursor.stream();
  283. this[kCursorStream] = stream;
  284. stream.on('data', change => {
  285. try {
  286. const processedChange = this._processChange(change);
  287. this.emit(ChangeStream.CHANGE, processedChange);
  288. }
  289. catch (error) {
  290. this.emit(ChangeStream.ERROR, error);
  291. }
  292. });
  293. stream.on('error', error => this._processErrorStreamMode(error));
  294. }
  295. /** @internal */
  296. _endStream() {
  297. const cursorStream = this[kCursorStream];
  298. if (cursorStream) {
  299. ['data', 'close', 'end', 'error'].forEach(event => cursorStream.removeAllListeners(event));
  300. cursorStream.destroy();
  301. }
  302. this[kCursorStream] = undefined;
  303. }
  304. /** @internal */
  305. _processChange(change) {
  306. if (this[kClosed]) {
  307. // TODO(NODE-3485): Replace with MongoChangeStreamClosedError
  308. throw new error_1.MongoAPIError(CHANGESTREAM_CLOSED_ERROR);
  309. }
  310. // a null change means the cursor has been notified, implicitly closing the change stream
  311. if (change == null) {
  312. // TODO(NODE-3485): Replace with MongoChangeStreamClosedError
  313. throw new error_1.MongoRuntimeError(CHANGESTREAM_CLOSED_ERROR);
  314. }
  315. if (change && !change._id) {
  316. throw new error_1.MongoChangeStreamError(NO_RESUME_TOKEN_ERROR);
  317. }
  318. // cache the resume token
  319. this.cursor.cacheResumeToken(change._id);
  320. // wipe the startAtOperationTime if there was one so that there won't be a conflict
  321. // between resumeToken and startAtOperationTime if we need to reconnect the cursor
  322. this.options.startAtOperationTime = undefined;
  323. return change;
  324. }
  325. /** @internal */
  326. _processErrorStreamMode(changeStreamError) {
  327. // If the change stream has been closed explicitly, do not process error.
  328. if (this[kClosed])
  329. return;
  330. if ((0, error_1.isResumableError)(changeStreamError, this.cursor.maxWireVersion)) {
  331. this._endStream();
  332. this.cursor.close().catch(() => null);
  333. const topology = (0, utils_1.getTopology)(this.parent);
  334. topology.selectServer(this.cursor.readPreference, {}, serverSelectionError => {
  335. if (serverSelectionError)
  336. return this._closeEmitterModeWithError(changeStreamError);
  337. this.cursor = this._createChangeStreamCursor(this.cursor.resumeOptions);
  338. });
  339. }
  340. else {
  341. this._closeEmitterModeWithError(changeStreamError);
  342. }
  343. }
  344. /** @internal */
  345. async _processErrorIteratorMode(changeStreamError) {
  346. if (this[kClosed]) {
  347. // TODO(NODE-3485): Replace with MongoChangeStreamClosedError
  348. throw new error_1.MongoAPIError(CHANGESTREAM_CLOSED_ERROR);
  349. }
  350. if (!(0, error_1.isResumableError)(changeStreamError, this.cursor.maxWireVersion)) {
  351. try {
  352. await this.close();
  353. }
  354. catch {
  355. // ignore errors from close
  356. }
  357. throw changeStreamError;
  358. }
  359. await this.cursor.close().catch(() => null);
  360. const topology = (0, utils_1.getTopology)(this.parent);
  361. try {
  362. await topology.selectServerAsync(this.cursor.readPreference, {});
  363. this.cursor = this._createChangeStreamCursor(this.cursor.resumeOptions);
  364. }
  365. catch {
  366. // if the topology can't reconnect, close the stream
  367. await this.close();
  368. throw changeStreamError;
  369. }
  370. }
  371. }
  372. /** @event */
  373. ChangeStream.RESPONSE = constants_1.RESPONSE;
  374. /** @event */
  375. ChangeStream.MORE = constants_1.MORE;
  376. /** @event */
  377. ChangeStream.INIT = constants_1.INIT;
  378. /** @event */
  379. ChangeStream.CLOSE = constants_1.CLOSE;
  380. /**
  381. * Fired for each new matching change in the specified namespace. Attaching a `change`
  382. * event listener to a Change Stream will switch the stream into flowing mode. Data will
  383. * then be passed as soon as it is available.
  384. * @event
  385. */
  386. ChangeStream.CHANGE = constants_1.CHANGE;
  387. /** @event */
  388. ChangeStream.END = constants_1.END;
  389. /** @event */
  390. ChangeStream.ERROR = constants_1.ERROR;
  391. /**
  392. * Emitted each time the change stream stores a new resume token.
  393. * @event
  394. */
  395. ChangeStream.RESUME_TOKEN_CHANGED = constants_1.RESUME_TOKEN_CHANGED;
  396. exports.ChangeStream = ChangeStream;
  397. //# sourceMappingURL=change_stream.js.map