Skip to content

Commit e0826fb

Browse files
daprahamianmbroadst
authored andcommitted
fix(asyncIterator): stronger guard against importing async generator
Adds a stronger spot check against importing the async iterator for cursors. Handles case where other project pollute the known symbols of the environment.
1 parent ae0663c commit e0826fb

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

lib/aggregation_cursor.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const MongoError = require('mongodb-core').MongoError;
55
const Readable = require('stream').Readable;
66
const CoreCursor = require('./cursor');
77
const deprecate = require('util').deprecate;
8+
const SUPPORTS = require('./utils').SUPPORTS;
89

910
/**
1011
* @fileOverview The **AggregationCursor** class is an internal class that embodies an aggregation cursor on MongoDB
@@ -130,7 +131,7 @@ inherits(AggregationCursor, Readable);
130131
for (var name in CoreCursor.prototype) {
131132
AggregationCursor.prototype[name] = CoreCursor.prototype[name];
132133
}
133-
if (Symbol.asyncIterator) {
134+
if (SUPPORTS.ASYNC_ITERATOR) {
134135
AggregationCursor.prototype[
135136
Symbol.asyncIterator
136137
] = require('./async/async_iterator').asyncIterator;

lib/command_cursor.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const ReadPreference = require('mongodb-core').ReadPreference;
55
const MongoError = require('mongodb-core').MongoError;
66
const Readable = require('stream').Readable;
77
const CoreCursor = require('./cursor');
8+
const SUPPORTS = require('./utils').SUPPORTS;
89

910
/**
1011
* @fileOverview The **CommandCursor** class is an internal class that embodies a
@@ -156,7 +157,7 @@ for (var i = 0; i < methodsToInherit.length; i++) {
156157
CommandCursor.prototype[methodsToInherit[i]] = CoreCursor.prototype[methodsToInherit[i]];
157158
}
158159

159-
if (Symbol.asyncIterator) {
160+
if (SUPPORTS.ASYNC_ITERATOR) {
160161
CommandCursor.prototype[Symbol.asyncIterator] = require('./async/async_iterator').asyncIterator;
161162
}
162163

lib/cursor.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const PassThrough = require('stream').PassThrough;
55
const inherits = require('util').inherits;
66
const deprecate = require('util').deprecate;
77
const handleCallback = require('./utils').handleCallback;
8+
const SUPPORTS = require('./utils').SUPPORTS;
89
const ReadPreference = require('mongodb-core').ReadPreference;
910
const MongoError = require('mongodb-core').MongoError;
1011
const Readable = require('stream').Readable;
@@ -203,7 +204,7 @@ function Cursor(bson, ns, cmd, options, topology, topologyOptions) {
203204
// Inherit from Readable
204205
inherits(Cursor, Readable);
205206

206-
if (Symbol.asyncIterator) {
207+
if (SUPPORTS.ASYNC_ITERATOR) {
207208
Cursor.prototype[Symbol.asyncIterator] = require('./async/async_iterator').asyncIterator;
208209
}
209210

lib/utils.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,15 @@ function deprecateOptions(config, fn) {
690690
return deprecated;
691691
}
692692

693+
const SUPPORTS = {};
694+
// Test asyncIterator support
695+
try {
696+
require('./async/async_iterator');
697+
SUPPORTS.ASYNC_ITERATOR = true;
698+
} catch (e) {
699+
SUPPORTS.ASYNC_ITERATOR = false;
700+
}
701+
693702
module.exports = {
694703
filterOptions,
695704
mergeOptions,
@@ -715,5 +724,6 @@ module.exports = {
715724
isPromiseLike,
716725
decorateWithCollation,
717726
decorateWithReadConcern,
718-
deprecateOptions
727+
deprecateOptions,
728+
SUPPORTS
719729
};

0 commit comments

Comments
 (0)