improve guard for prevent more than one instance of bitcore
This commit is contained in:
parent
5fe53ac36d
commit
7e3993578c
17
index.js
17
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 = {};
|
||||
|
|
|
@ -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;
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue