cayasso / mongo-oplog

Watch mongodb oplog in a simple way
381 stars 91 forks source link

Filter broken in 2.1.0 release #58

Closed airs0urce closed 6 years ago

airs0urce commented 6 years ago

Looks like filter feature broken in version 2.1.0.

Code to reproduce:

const MongoOplog = require('mongo-oplog');

(async () => { 
    const oplog = MongoOplog('mongodb://127.0.0.1:27017/local', {});
    const filter = oplog.filter('*.users')
    await oplog.tail();

    filter.on('update', async (doc) => {
           // ...
    });
})();

process.on("uncaughtException", function (err) {
        console.log(err);
});

Run this code and then update any record in "users" collection. You will get exception:

TypeError: Cannot read property 'u' of undefined
    at EventEmitter.onop (/var/www/project/node_modules/mongo-oplog/lib/filter.js:28:23)
    at EventEmitter.emit (/var/www/project/node_modules/eventemitter3/index.js:129:35)
    at Cursor.ondata (/var/www/project/node_modules/mongo-oplog/lib/index.js:128:11)
    at Cursor.emit (events.js:159:13)
    at addChunk (_stream_readable.js:265:12)
    at readableAddChunk (_stream_readable.js:252:11)
    at Cursor.Readable.push (_stream_readable.js:209:10)
    at /var/www/project/node_modules/mongodb/lib/cursor.js:1110:10
    at handleCallback (/var/www/project/node_modules/mongodb/lib/utils.js:120:56)
    at /var/www/project/node_modules/mongodb/lib/cursor.js:683:5

I suppose it happens because index.js includes filter.js:

const createFilter = require('./filter');

and filter.js includes index.js:

var _require = require('./');

and you can see "_require" is empty object. Probably, circular dependencies issue.