From 7e3993578c7cad9a83695a9fbac6a77a4f7a07db Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Wed, 13 May 2015 08:44:52 +0300 Subject: [PATCH 1/2] improve guard for prevent more than one instance of bitcore --- index.js | 17 ++++++----------- test/index.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 test/index.js diff --git a/index.js b/index.js index e6d380c..bb59eb6 100644 --- a/index.js +++ b/index.js @@ -3,24 +3,19 @@ var bitcore = module.exports; // module information bitcore.version = 'v' + require('./package.json').version; -var inBrowser = typeof process === 'undefined' || typeof process.versions === 'undefined'; -if ((inBrowser && window._bitcore) || (!inBrowser && global._bitcore)) { - var versions = bitcore.version + ' and ' + (inBrowser ? window._bitcore : global._bitcore); +if (global._bitcore !== undefined) { + var versions = bitcore.version + ' and ' + global._bitcore; var message = 'More than one instance of bitcore found with different versions: ' + versions; - if (inBrowser) { - message += '. Make sure any scripts included don\'t contain their own bitcore bundle.'; - } else { + if (typeof window === 'undefined') { message += '. Make sure there are no version conflicts between package.json files of your ' + 'dependencies. This could also happen when a package depends on a git repository.'; + } else { + message += '. Make sure any scripts included don\'t contain their own bitcore bundle.'; } throw new Error(message); } -if (inBrowser) { - window._bitcore = bitcore.version; -} else { - global._bitcore = bitcore.version; -} +global._bitcore = bitcore.version; // crypto bitcore.crypto = {}; diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..608072a --- /dev/null +++ b/test/index.js @@ -0,0 +1,39 @@ +'use strict'; + +var expect = require('chai').expect; +var bitcore = require('../'); + +// current tests works only in node.js +var bdescribe = typeof window === 'undefined' ? describe : xdescribe +bdescribe('index.js', function() { + var bitcoreModulePath; + var bitcoreModule; + + before(function() { + bitcoreModulePath = require.resolve('../'); + bitcoreModule = require.cache[bitcoreModulePath]; + delete require.cache[bitcoreModulePath]; + }); + + after(function() { + require.cache[bitcoreModulePath] = bitcoreModule; + }); + + function importBitcore() { + require('../'); + } + + it('global._bitcore should be defined', function() { + expect(global._bitcore).to.equal(bitcore.version); + }); + + it('throw error on importing other bitcore module', function() { + expect(importBitcore).to.throw(Error); + }); + + it('throw error on importing with defined window', function () { + global.window = 'window hack'; + expect(importBitcore).to.throw(Error); + delete global.window; + }); +}); From 1c8ebc0eb5b62c27824034928a6b87bc29eaf482 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 18 May 2015 14:49:29 -0400 Subject: [PATCH 2/2] Simplify version guard - Updated index.js test to run in Node.js and browsers - Simplified message and clarified case where two of the same versions would conflict --- index.js | 24 +++++++++++------------- test/index.js | 37 +++++++------------------------------ 2 files changed, 18 insertions(+), 43 deletions(-) diff --git a/index.js b/index.js index bb59eb6..dd0b97b 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,21 @@ +'use strict'; + var bitcore = module.exports; // module information bitcore.version = 'v' + require('./package.json').version; - -if (global._bitcore !== undefined) { - var versions = bitcore.version + ' and ' + global._bitcore; - var message = 'More than one instance of bitcore found with different versions: ' + versions; - if (typeof window === 'undefined') { - message += '. Make sure there are no version conflicts between package.json files of your ' + - 'dependencies. This could also happen when a package depends on a git repository.'; - } else { - message += '. Make sure any scripts included don\'t contain their own bitcore bundle.'; +bitcore.versionGuard = function(version) { + if (version !== undefined) { + var message = 'More than one instance of bitcore found with versions: ' + bitcore.version + + ' and ' + version + '. Please make sure to require bitcore and check that submodules do' + + ' not also include their own bitcore dependency.'; + throw new Error(message); } - - throw new Error(message); -} +}; +bitcore.versionGuard(global._bitcore); global._bitcore = bitcore.version; -// crypto +// crypto bitcore.crypto = {}; bitcore.crypto.BN = require('./lib/crypto/bn'); bitcore.crypto.ECDSA = require('./lib/crypto/ecdsa'); diff --git a/test/index.js b/test/index.js index 608072a..d62d998 100644 --- a/test/index.js +++ b/test/index.js @@ -1,39 +1,16 @@ 'use strict'; -var expect = require('chai').expect; +var should = require('chai').should(); var bitcore = require('../'); -// current tests works only in node.js -var bdescribe = typeof window === 'undefined' ? describe : xdescribe -bdescribe('index.js', function() { - var bitcoreModulePath; - var bitcoreModule; - - before(function() { - bitcoreModulePath = require.resolve('../'); - bitcoreModule = require.cache[bitcoreModulePath]; - delete require.cache[bitcoreModulePath]; - }); - - after(function() { - require.cache[bitcoreModulePath] = bitcoreModule; - }); - - function importBitcore() { - require('../'); - } - +describe('#versionGuard', function() { it('global._bitcore should be defined', function() { - expect(global._bitcore).to.equal(bitcore.version); - }); - - it('throw error on importing other bitcore module', function() { - expect(importBitcore).to.throw(Error); + should.equal(global._bitcore, bitcore.version); }); - it('throw error on importing with defined window', function () { - global.window = 'window hack'; - expect(importBitcore).to.throw(Error); - delete global.window; + it('throw an error if version is already defined', function() { + (function() { + bitcore.versionGuard('version'); + }).should.throw('More than one instance of bitcore'); }); });