Skip to content

Commit

Permalink
Was ensured compatibility with node v8
Browse files Browse the repository at this point in the history
  • Loading branch information
ex1st committed Feb 20, 2018
1 parent 4478b3a commit 87ae178
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 66 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: node_js
node_js:
- "7"
- "6"
- "4"
- "0.12"
Expand Down
21 changes: 9 additions & 12 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var safe = require('safe');
module.exports = function (opts) {
opts = opts || {};
var db = require('./tdb.js');
Expand All @@ -7,23 +8,19 @@ module.exports = function (opts) {
return this.toString();
}
}
function gdb (path,optsLocal) {
gdb.superclass.constructor.call(this, path, optsLocal, opts);
function gdb(path, optsLocal) {
db.call(this, path, optsLocal, opts);
this.ObjectID = ObjectID;
this.Code = require('./tcode.js').Code;
this.Binary = require('./tbinary.js').Binary;
this.Finder = require("./finder")(this);
}
var F = function() { }
F.prototype = db.prototype
gdb.prototype = new F()
gdb.prototype.constructor = gdb
gdb.superclass = db.prototype
safe.inherits(gdb, db);
return {
Db:gdb,
Collection:require('./tcoll.js'),
Code:require('./tcode.js').Code,
Binary:require('./tbinary.js').Binary,
ObjectID:ObjectID
Db: gdb,
Collection: require('./tcoll.js'),
Code: require('./tcode.js').Code,
Binary: require('./tbinary.js').Binary,
ObjectID: ObjectID
}
}
40 changes: 11 additions & 29 deletions lib/tbinary.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
var _ = require("lodash");

/**
* Module dependencies.
*/
if (typeof window === 'undefined') {
var Buffer = require('buffer').Buffer; // TODO just use global Buffer
}
var Buffer = require("safe-buffer").Buffer;

// Binary default subtype
var BSON_BINARY_SUBTYPE_DEFAULT = 0;
Expand Down Expand Up @@ -72,25 +66,13 @@ function Binary(buffer, subType) {
// Only accept Buffer, Uint8Array or Arrays
if(_.isString(buffer)) {
// Different ways of writing the length of the string for the different types
if(typeof Buffer !== 'undefined') {
this.buffer = new Buffer(buffer);
} else if(typeof Uint8Array !== 'undefined' || _.isArray(buffer)) {
this.buffer = writeStringToArray(buffer);
} else {
throw new Error("only String, Buffer, Uint8Array or Array accepted");
}
this.buffer = new Buffer(buffer);
} else {
this.buffer = buffer;
}
this.position = buffer.length;
} else {
if(typeof Buffer !== 'undefined') {
this.buffer = new Buffer(Binary.BUFFER_SIZE);
} else if(typeof Uint8Array !== 'undefined'){
this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE));
} else {
this.buffer = new Array(Binary.BUFFER_SIZE);
}
// Set position to start of buffer
this.position = 0;
}
Expand Down Expand Up @@ -120,7 +102,7 @@ Binary.prototype.put = function put(byte_value) {
if(this.buffer.length > this.position) {
this.buffer[this.position++] = decoded_byte;
} else {
if(typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
if(Buffer.isBuffer(this.buffer)) {
// Create additional overflow buffer
var buffer = new Buffer(Binary.BUFFER_SIZE + this.buffer.length);
// Combine the two buffers together
Expand All @@ -130,7 +112,7 @@ Binary.prototype.put = function put(byte_value) {
} else {
var buffer = null;
// Create a new buffer (typed or normal array)
if (_.isBuffer(this.buffer)) {
if (Buffer.isBuffer(this.buffer)) {
buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE + this.buffer.length));
} else {
buffer = new Array(Binary.BUFFER_SIZE + this.buffer.length);
Expand Down Expand Up @@ -163,10 +145,10 @@ Binary.prototype.write = function write(string, offset) {
if(this.buffer.length < offset + string.length) {
var buffer = null;
// If we are in node.js
if(typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
if(Buffer.isBuffer(this.buffer)) {
buffer = new Buffer(this.buffer.length + string.length);
this.buffer.copy(buffer, 0, 0, this.buffer.length);
} else if(_.isBuffer(this.buffer)) {
} else if (Buffer.isBuffer(this.buffer)) {
// Create a new buffer
buffer = new Uint8Array(new ArrayBuffer(this.buffer.length + string.length))
// Copy the content
Expand All @@ -179,15 +161,15 @@ Binary.prototype.write = function write(string, offset) {
this.buffer = buffer;
}

if(typeof Buffer !== 'undefined' && Buffer.isBuffer(string) && Buffer.isBuffer(this.buffer)) {
if(Buffer.isBuffer(string) && Buffer.isBuffer(this.buffer)) {
string.copy(this.buffer, offset, 0, string.length);
this.position = (offset + string.length) > this.position ? (offset + string.length) : this.position;
// offset = string.length
} else if(typeof Buffer !== 'undefined' && _.isString(string) && _.isBuffer(this.buffer)) {
} else if (_.isString(string) && Buffer.isBuffer(this.buffer)) {
this.buffer.write(string, 'binary', offset);
this.position = (offset + string.length) > this.position ? (offset + string.length) : this.position;
// offset = string.length;
} else if (_.isBuffer(string) || _.isArray(string) && !_.isString(string)) {
} else if (Buffer.isBuffer(string) || _.isArray(string) && !_.isString(string)) {
for(var i = 0; i < string.length; i++) {
this.buffer[offset++] = string[i];
}
Expand Down Expand Up @@ -239,7 +221,7 @@ Binary.prototype.value = function value(asRaw) {
asRaw = asRaw == null ? false : asRaw;

// If it's a node.js buffer object
if(typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
if(Buffer.isBuffer(this.buffer)) {
return asRaw ? this.buffer.slice(0, this.position) : this.buffer.toString('binary', 0, this.position);
} else {
if(asRaw) {
Expand All @@ -248,7 +230,7 @@ Binary.prototype.value = function value(asRaw) {
return this.buffer.slice(0, this.position);
} else {
// Create a new buffer to copy content to
var newBuffer = _.isBuffer(this.buffer) ? new Uint8Array(new ArrayBuffer(this.position)) : new Array(this.position);
var newBuffer = Buffer.isBuffer(this.buffer) ? new Uint8Array(new ArrayBuffer(this.position)) : new Array(this.position);
// Copy content
for(var i = 0; i < this.position; i++) {
newBuffer[i] = this.buffer[i];
Expand Down
5 changes: 3 additions & 2 deletions lib/tcoll.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var tcache = require("./tcache");
var Code = require('./tcode').Code;
var tutils = require('./utils');
var Updater = require('./updater');
var Buffer = require('safe-buffer').Buffer;

function tcoll(tdb) {
this._tdb = null;
Expand Down Expand Up @@ -345,7 +346,7 @@ tcoll.prototype.createIndex = tcoll.prototype.ensureIndex = function (obj, optio
tcoll.prototype.indexExists = function (idx, cb) {
if (!_.isArray(idx))
idx = [idx];
var i = _.intersection(idx,_(this._idx).values().map('name').value());
var i = _.intersection(idx, _.map(this._idx, 'name'));
cb(null,i.length == idx.length);
};

Expand Down Expand Up @@ -1043,7 +1044,7 @@ tcoll.prototype.__find = function (query, fields, skip, limit, sort, hint, arFie
// range.reverse();
sort = null;
} else
range = _(self._store).values().map('pos').value();
range = _.map(self._store, 'pos');
}

if (sort && si) {
Expand Down
8 changes: 6 additions & 2 deletions lib/tdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;
var Buffer = require("safe-buffer").Buffer;

var mstore = Object.create ? Object.create(null) : {};

function tdb(path_, opts, gopts) {
EventEmitter.call(this);

this._gopts = gopts;
this._path = path.resolve(path_);
this._cols = Object.create ? Object.create(null) : {};
Expand All @@ -18,7 +22,7 @@ function tdb(path_, opts, gopts) {
this.openCalled = false;
}

tdb.prototype.__proto__ = EventEmitter.prototype;
safe.inherits(tdb, EventEmitter);

module.exports = tdb;

Expand Down Expand Up @@ -117,7 +121,7 @@ tdb.prototype.collectionNames = function (opts, cb) {
opts = {};
}
if (this._stype=="mem") {
cb(null,_(self._mstore).keys().map(function (e) { return opts.namesOnly?e:{name:self._name+"."+e};}).value());
cb(null, _(self._mstore).keys().map(function (e) { return opts.namesOnly?e:{name:self._name+"."+e};}).value());
} else {
fs.readdir(self._path, safe.sure(cb,function(files) {
// some collections ca be on disk and some only in memory, we need both
Expand Down
4 changes: 2 additions & 2 deletions lib/tstream.js
Original file line number Diff line number Diff line change
@@ -1,5 +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;
Expand Down Expand Up @@ -50,8 +51,7 @@ function CursorStream(cursor, options) {
* @ignore
* @api private
*/
CursorStream.prototype.__proto__ = Stream.prototype;

safe.inherits(CursorStream, Stream);
/**
* Flag stating whether or not this stream is readable.
*/
Expand Down
27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,24 @@
"node": ">= v0.8.x"
},
"dependencies": {
"safe": "~0.3.9",
"lodash": "~4.11.x"
"lodash": "^4.17.5",
"safe": "^0.4.5",
"safe-buffer": "^5.1.1"
},
"scripts": {
"test": "./test.sh"
"test": "./test.sh"
},
"optionalDependencies":{
"bson":"0.2.x"
"optionalDependencies": {
"bson": "^1.0.4"
},
"devDependencies": {
"csv": "0.3.x",
"mongodb": "1.4.x",
"temp": "0.4.x",
"lorem-ipsum":"1.0.x",
"optimist":"0.6.x",
"step":"0.0.x",
"benchmark":"1.0.x",
"mocha":"1.21.x"
"csv": "0.3.x",
"mongodb": "^1.4.40",
"temp": "^0.8.x",
"lorem-ipsum": "1.0.x",
"optimist": "0.6.x",
"step": "^1.0.0",
"benchmark": "^2.1.4",
"mocha": "^1.x.x"
}
}
1 change: 1 addition & 0 deletions test/contrib/cursorstream_tests.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var Buffer = require("safe-buffer").Buffer;
/**
* A simple example showing the use of the cursorstream pause function.
*
Expand Down
2 changes: 2 additions & 0 deletions test/contrib/find_tests.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var Buffer = require("safe-buffer").Buffer;

/**
* Test a simple find
* @ignore
Expand Down
1 change: 1 addition & 0 deletions test/contrib/insert_tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Step = require('step')
, Buffer = require("safe-buffer").Buffer
, Script = require('vm');

/**
Expand Down
13 changes: 8 additions & 5 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var optimist = require('optimist')
.alias('R',"reporter")
.default('db', 'tingodb')
.describe('db', 'tingodb | mongodb')
.describe('h', 'Display the usage')
.alias('h', 'help')
.describe('h', 'Display the usage')
.alias('h', 'help')
.boolean('quick')
.alias('q', 'quick')
.describe('quick', 'Run only quick tests')
Expand All @@ -25,8 +25,8 @@ var optimist = require('optimist')
var argv = optimist.argv;

if (argv.help) {
optimist.showHelp();
process.exit(0);
optimist.showHelp();
process.exit(0);
}

var config = {
Expand Down Expand Up @@ -121,4 +121,7 @@ if (!config.mongo && !argv.default) {
})
}

safe.series(sessions, function (err) { if (err) console.log(err); process.exit(0)});
safe.series(sessions, function (err) {
if (err) console.log(err);
process.exit(err ? 1 : 0);
});

0 comments on commit 87ae178

Please sign in to comment.