Skip to content

Commit

Permalink
fix(asyncIterator): stronger guard against importing async generator
Browse files Browse the repository at this point in the history
Adds a stronger spot check against importing the async iterator
for cursors. Handles case where other project pollute the known
symbols of the environment.
  • Loading branch information
daprahamian authored and mbroadst committed Mar 22, 2019
1 parent ae0663c commit e0826fb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/aggregation_cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const MongoError = require('mongodb-core').MongoError;
const Readable = require('stream').Readable;
const CoreCursor = require('./cursor');
const deprecate = require('util').deprecate;
const SUPPORTS = require('./utils').SUPPORTS;

/**
* @fileOverview The **AggregationCursor** class is an internal class that embodies an aggregation cursor on MongoDB
Expand Down Expand Up @@ -130,7 +131,7 @@ inherits(AggregationCursor, Readable);
for (var name in CoreCursor.prototype) {
AggregationCursor.prototype[name] = CoreCursor.prototype[name];
}
if (Symbol.asyncIterator) {
if (SUPPORTS.ASYNC_ITERATOR) {
AggregationCursor.prototype[
Symbol.asyncIterator
] = require('./async/async_iterator').asyncIterator;
Expand Down
3 changes: 2 additions & 1 deletion lib/command_cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ReadPreference = require('mongodb-core').ReadPreference;
const MongoError = require('mongodb-core').MongoError;
const Readable = require('stream').Readable;
const CoreCursor = require('./cursor');
const SUPPORTS = require('./utils').SUPPORTS;

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

if (Symbol.asyncIterator) {
if (SUPPORTS.ASYNC_ITERATOR) {
CommandCursor.prototype[Symbol.asyncIterator] = require('./async/async_iterator').asyncIterator;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const PassThrough = require('stream').PassThrough;
const inherits = require('util').inherits;
const deprecate = require('util').deprecate;
const handleCallback = require('./utils').handleCallback;
const SUPPORTS = require('./utils').SUPPORTS;
const ReadPreference = require('mongodb-core').ReadPreference;
const MongoError = require('mongodb-core').MongoError;
const Readable = require('stream').Readable;
Expand Down Expand Up @@ -203,7 +204,7 @@ function Cursor(bson, ns, cmd, options, topology, topologyOptions) {
// Inherit from Readable
inherits(Cursor, Readable);

if (Symbol.asyncIterator) {
if (SUPPORTS.ASYNC_ITERATOR) {
Cursor.prototype[Symbol.asyncIterator] = require('./async/async_iterator').asyncIterator;
}

Expand Down
12 changes: 11 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,15 @@ function deprecateOptions(config, fn) {
return deprecated;
}

const SUPPORTS = {};
// Test asyncIterator support
try {
require('./async/async_iterator');
SUPPORTS.ASYNC_ITERATOR = true;
} catch (e) {
SUPPORTS.ASYNC_ITERATOR = false;
}

module.exports = {
filterOptions,
mergeOptions,
Expand All @@ -715,5 +724,6 @@ module.exports = {
isPromiseLike,
decorateWithCollation,
decorateWithReadConcern,
deprecateOptions
deprecateOptions,
SUPPORTS
};

0 comments on commit e0826fb

Please sign in to comment.