Add singleton package assertion (& add to keyring) (#384)

* Add signleton package assertion (& add to keyring)

* CHANGELOG

* Add test
This commit is contained in:
Jaco Greeff
2019-03-26 16:53:08 +01:00
committed by GitHub
parent 8a78e4237d
commit 5bc4ea2fb3
5 changed files with 53 additions and 2 deletions
+3 -1
View File
@@ -5,7 +5,7 @@
import { KeypairType } from '@polkadot/util-crypto/types';
import { KeyringInstance, KeyringPair, KeyringPair$Json, KeyringPair$Meta, KeyringOptions } from './types';
import { assert, hexToU8a, isNumber, isHex, stringToU8a } from '@polkadot/util';
import { assert, assertSingletonPackage, hexToU8a, isNumber, isHex, stringToU8a } from '@polkadot/util';
import { keyExtractSuri, mnemonicToSeed , naclKeypairFromSeed as naclFromSeed, schnorrkelKeypairFromSeed as schnorrkelFromSeed, mnemonicToMiniSecret, keyFromPath } from '@polkadot/util-crypto';
import { decodeAddress, encodeAddress, setAddressPrefix } from './address';
@@ -13,6 +13,8 @@ import { DEV_PHRASE } from './defaults';
import createPair from './pair';
import Pairs from './pairs';
assertSingletonPackage('@polkadot/keyring');
/**
* # @polkadot/keyring
*
@@ -0,0 +1,21 @@
// Copyright 2017-2019 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { assertSingletonPackage } from '.';
describe('assertSingletonPackage', () => {
const NAME = 'assertSingletonPackage';
it('should not throw the first time', () => {
expect(
() => assertSingletonPackage(NAME)
).not.toThrow();
});
it('should throw the second time', () => {
expect(
() => assertSingletonPackage(NAME)
).toThrow(/Multiple versions of assertSingletonPackage detected/);
});
});
@@ -0,0 +1,22 @@
// Copyright 2017-2019 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import isUndefined from './is/undefined';
import assert from './assert';
/**
* @name assertSingletonPackage
* @summary Checks that a specific package is only imported once
*/
export default function assertSingletonPackage (name: string): void {
const _global: any = isUndefined(window) ? global : window;
if (isUndefined(_global.__polkadotjs)) {
_global.__polkadotjs = {};
}
assert(isUndefined(_global.__polkadotjs[name]), `Multiple versions of ${name} detected, ensure that there is only version one in your dependency tree`);
_global.__polkadotjs[name] = true;
}
+1
View File
@@ -9,6 +9,7 @@ import './polyfill';
*/
export { default as assert } from './assert';
export { default as assertSingletonPackage } from './assertSingletonPackage';
export { default as logger } from './logger';
export { default as promisify } from './promisify';