Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with newest versions of Node #167

Merged
merged 1 commit into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js:
- "8"
- "6"
- "4"
- "0.12"
Expand Down
1 change: 1 addition & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var safe = require('safe');

module.exports = function (opts) {
opts = opts || {};
var db = require('./tdb.js');
Expand Down
54 changes: 26 additions & 28 deletions lib/tcoll.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ tcoll.prototype.initFS = function (tdb, name, options, create, cb) {
var b1 = new Buffer(45);
safe.whilst(function () { return self._fsize === null; }, function(cb) {
safe.run(function (cb) {
fs.read(fd, b1, 0, 45, pos, safe.trap_sure(cb, function (bytes, data) {
fs.read(fd, b1, 0, 45, pos, safe.sure(cb, function (bytes, data) {
if (bytes===0) {
self._fsize = pos;
return cb();
Expand Down Expand Up @@ -183,7 +183,7 @@ tcoll.prototype._compact = function (cb) {
fs.open(filename, 'w+', safe.sure(cb, function (fd) {
var b1 = new Buffer(45);
function get(pos, cb) {
fs.read(self._fd, b1, 0, 45, pos, safe.trap_sure(cb, function (bytes, data) {
fs.read(self._fd, b1, 0, 45, pos, safe.sure(cb, function (bytes, data) {
var h1 = JSON.parse(data.toString());
h1.o = parseInt(h1.o, 10);
h1.k = parseInt(h1.k, 10);
Expand All @@ -204,21 +204,19 @@ tcoll.prototype._compact = function (cb) {
cb();
}));
}));
}, function (err) {
if (err) {
fs.close(fd, function () {
fs.unlink(filename, function () {
cb(err);
});
});
return;
}
}, safe.sure(function (err) {
fs.close(fd, safe.sure(cb, function () {
fs.unlink(filename, safe.sure(cb, function () {
cb(err);
}));
}));
}, function () {
if (process.platform.match(/^win/)) {
// WINDOWS: unsafe because if something fail while renaming file it will not
// restore automatically
fs.close(self._fd, safe.sure(cb,function() {
fs.close(fd, safe.sure(cb,function() {
fs.unlink(self._filename, safe.sure(cb,function () {
fs.close(self._fd, safe.sure(cb, function () {
fs.close(fd, safe.sure(cb, function () {
fs.unlink(self._filename, safe.sure(cb, function () {
fs.rename(filename, self._filename, safe.sure(cb, function () {
fs.open(self._filename, 'a+', safe.sure(cb, function (fd) {
self._fd = fd;
Expand All @@ -233,14 +231,15 @@ tcoll.prototype._compact = function (cb) {
} else {
// safe way
fs.rename(filename, self._filename, safe.sure(cb, function () {
fs.close(self._fd);
self._fd = fd;
self._fsize = wpos;
self._store = store;
cb();
fs.close(self._fd, safe.sure(cb, function () {
self._fd = fd;
self._fsize = wpos;
self._store = store;
cb();
}));
}));
}
});
}));
}));
};

Expand Down Expand Up @@ -367,12 +366,12 @@ tcoll.prototype._getFS = function (pos, unsafe, cb) {
if (cached)
return safe.back(cb,null,cached);
var b1 = new Buffer(45);
fs.read(self._fd, b1, 0, 45, pos, safe.trap_sure(cb, function (bytes, data) {
fs.read(self._fd, b1, 0, 45, pos, safe.sure(cb, function (bytes, data) {
var h1 = JSON.parse(data.toString());
h1.o = parseInt(h1.o,10);
h1.k = parseInt(h1.k,10);
var b2 = new Buffer(h1.o);
fs.read(self._fd,b2,0,h1.o,pos+45+2+h1.k, safe.trap_sure(cb, function (bytes, data) {
fs.read(self._fd, b2, 0, h1.o, pos + 45 + 2 + h1.k, safe.sure(cb, function (bytes, data) {
var obj = self._unwrapTypes(JSON.parse(data.toString()));
if (bytes <= self._cmaxobj)
self._cache.set(pos, obj);
Expand Down Expand Up @@ -1204,26 +1203,25 @@ tcoll.prototype.mapReduce = function (map, reduce, opts, cb) {
var doc;
safe.doUntil(
function (cb) {
c.nextObject(safe.trap_sure(cb, function (_doc) {
c.nextObject(safe.sure(cb, function (_doc) {
doc = _doc;
if (doc) map.call(doc);
return cb();
cb();
}));
},
function () {
return doc === null;
},
safe.trap_sure(cb, function () {
safe.sure(cb, function () {
_.each(m,function (v, k) {
v = v.length > 1 ? reduce(k, v) : v[0];
if (finalize) v = finalize(k, v);
m[k] = v;
});

var stats = {};
if (opts.out.inline) return process.nextTick(function () {
cb(null, _.values(m), stats); // execute outside of trap
});
if (opts.out.inline)
return safe.back(cb, null, _.values(m), stats); // execute outside of trap

// write results to collection
safe.waterfall([
Expand Down
26 changes: 12 additions & 14 deletions lib/tcursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ tcursor.prototype.skip = function (v, cb) {
if (!self._err)
this._skip = v;
if (cb)
process.nextTick(function () {cb(self._err,self);});
safe.back(cb, self._err, self);
return this;
};

Expand Down Expand Up @@ -175,7 +175,7 @@ tcursor.prototype.sort = function (v, d, cb) {
});
}
if (cb)
process.nextTick(function () {cb(self._err, self);});
safe.back(cb, self._err, self);
return this;
};

Expand All @@ -193,14 +193,14 @@ tcursor.prototype.limit = function (v, cb) {
this._limit = v==0?null:Math.abs(v);
}
if (cb)
process.nextTick(function () {cb(self._err,self);});
safe.back(cb, self._err, self);
return this;
};

tcursor.prototype.nextObject = function (cb) {
var self = this;
if (self._err) {
if (cb) process.nextTick(function () {cb(self._err);});
if (cb) safe.back(cb, self._err);
return;
}
self._ensure(safe.sure(cb, function () {
Expand All @@ -218,7 +218,7 @@ tcursor.prototype.count = function (applySkipLimit, cb) {
applySkipLimit = false;
}
if (self._err) {
if (cb) process.nextTick(function () {cb(self._err);});
if (cb) safe.back(cb, self._err);
return;
}
if ((!self._skip && self._limit === null) || applySkipLimit) {
Expand All @@ -228,9 +228,7 @@ tcursor.prototype.count = function (applySkipLimit, cb) {
return;
}
if (self._count !== null) {
process.nextTick(function () {
cb(null, self._count);
});
safe.back(cb, null, self._count);
return;
}
self._c._find(self._query, {}, 0, null, null, self._hint, self._arFields, safe.sure(cb, function (data) {
Expand All @@ -242,7 +240,7 @@ tcursor.prototype.count = function (applySkipLimit, cb) {
tcursor.prototype.setReadPreference = function (the, cb) {
var self = this;
if (self._err) {
if (cb) process.nextTick(function () {cb(self._err);});
if (cb) safe.back(cb, self._err);
return;
}
return this;
Expand All @@ -258,7 +256,7 @@ tcursor.prototype.batchSize = function (v, cb) {
self._err = new Error('Cursor is closed');
if (!cb) throw self._err;
}
if (cb) process.nextTick(function () {cb(self._err,self);});
if (cb) safe.back(cb, self._err, self);
return this;
};

Expand All @@ -268,7 +266,7 @@ tcursor.prototype.close = function (cb) {
this._i=-1;
this._err = null;
if (cb)
process.nextTick(function () {cb(self._err,self);});
safe.back(cb, self._err, self);
return this;
};

Expand All @@ -286,7 +284,7 @@ tcursor.prototype.toArray = function (cb) {
self._err = new Error("Cursor is closed");

if (self._err) {
if (cb) process.nextTick(function () {cb(self._err);});
if (cb) safe.back(cb, self._err);
return;
}

Expand All @@ -312,7 +310,7 @@ tcursor.prototype.each = function (cb) {
self._err = new Error("Cursor is closed");

if (self._err) {
if (cb) process.nextTick(function () {cb(self._err);});
if (cb) safe.back(cb, self._err);
return;
}
self._ensure(safe.sure(cb, function () {
Expand All @@ -335,7 +333,7 @@ tcursor.prototype.stream = function (options) {
tcursor.prototype._ensure = function (cb) {
var self = this;
if (self._items!=null)
return process.nextTick(cb);
return safe.back(cb);
self._c._find(self._query, {}, self._skip, self._limit, self._sort, self._hint, self._arFields, safe.sure_result(cb, function (data) {
self._items = data;
self._i=0;
Expand Down
12 changes: 6 additions & 6 deletions lib/tdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ tdb.prototype.collectionNames = function (opts, cb) {
opts = {};
}
if (this._stype=="mem") {
cb(null, _.map(self._mstore, function (v, e) { return opts.namesOnly?e:{name:self._name+"."+e};}));
cb(null, _.map(self._mstore, function (v, e) { return opts.namesOnly ? e : { name: self._name + "." + e }; }));
} else {
fs.readdir(self._path, safe.sure(cb,function(files) {
// some collections ca be on disk and some only in memory, we need both
files = _.union(files,_.keys(self._cols));
cb(null,_(files)
.reject(function (e) {return /^\./.test(e);}) // ignore hidden linux alike files
.map(function (e) { return opts.namesOnly?e:{name:self._name+"."+e};})
files = _(self._cols).keys().union(files);
cb(null, files
.reject(function (e) { return /^\./.test(e); }) // ignore hidden linux alike files
.map(function (e) { return opts.namesOnly ? e : { name: self._name + "." + e }; })
.value());
}));
}
Expand All @@ -151,7 +151,7 @@ tdb.prototype.dropCollection = function (cname, cb) {
if (!c) {
var err = new Error("ns not found");
if (cb) return safe.back(cb, err);
else throw new err;
throw new err;
}
c._stop(safe.sure(cb, function (ondisk) {
delete self._cols[cname];
Expand Down
10 changes: 3 additions & 7 deletions lib/tstream.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
var timers = require('timers');
var _ = require('lodash');
var safe = require('safe');

// Set processor, setImmediate if 0.10 otherwise nextTick
var processor = timers.setImmediate ? timers.setImmediate : process.nextTick;

/**
* Module dependecies.
*/
Expand Down Expand Up @@ -41,7 +37,7 @@ function CursorStream(cursor, options) {

// give time to hook up events
var self = this;
process.nextTick(function() {
safe.back(function() {
self._init();
});
}
Expand Down Expand Up @@ -82,7 +78,7 @@ CursorStream.prototype._next = function () {

var self = this;
// Get the next object
processor(function() {
safe.back(function() {
if(self.paused || self._destroyed) return;

self._cursor.nextObject(function (err, doc) {
Expand Down Expand Up @@ -132,7 +128,7 @@ CursorStream.prototype.resume = function () {
if(!this.paused) return;
//if(!this._cursor.state == 3) return;

process.nextTick(function() {
safe.back(function() {
self.paused = false;
// Only trigger more fetching if the cursor is open
self._next();
Expand Down
4 changes: 3 additions & 1 deletion lib/wqueue.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var safe = require("safe");

function wqueue (limit,first) {
this.limit = limit || 100;
this._rc = 0;
Expand Down Expand Up @@ -42,7 +44,7 @@ wqueue.prototype._exec = function (task,block,cb) {

wqueue.prototype._ping = function () {
var self = this;
process.nextTick(function () {
safe.back(function () {
while (self._q.length>0 && self._rc<self.limit && !self._blocked && (!self._q[0].block || self._rc==0) ) {
var t = self._q.splice(0,1)[0];
if (self._stoped)
Expand Down
Loading