Compare commits

..
34 Commits
Author SHA1 Message Date
Github Actions 83975e9235 [CI Skip] release/stable 5.2.1
skip-checks: true
2020-12-27 07:44:02 +00:00
Jaco Greeff 3adf475ef4 5.2 (#821)
* 5.2

* Bump
2020-12-27 08:39:14 +01:00
Github Actions ccc6edebbb [CI Skip] release/beta 5.1.2-3
skip-checks: true
2020-12-25 08:58:15 +00:00
Jaco Greeff 0d7ed7420d Add keccak512 (#820)
* Add keccak512

* dedupe

* Adjust hex test

* ss58

* fix expectation
2020-12-25 09:52:49 +01:00
Github Actions dbf1837b97 [CI Skip] release/beta 5.1.2-2
skip-checks: true
2020-12-24 07:56:46 +00:00
Jaco Greeff 9ae738ac51 Add Valiu (#818) 2020-12-24 08:51:22 +01:00
Github Actions a4000efc43 [CI Skip] release/beta 5.1.2-1
skip-checks: true
2020-12-23 11:03:53 +00:00
Jaco Greeff 7871a17942 Add arrayFlatten (#816)
* Add arrayFlatten

* Add test for arrayFlatten

* Add arrayChunk

* Update packages/util/src/array/chunk.ts
2020-12-23 11:58:59 +01:00
Github Actions 5aad1d09b2 [CI Skip] release/beta 5.1.2-0
skip-checks: true
2020-12-22 09:22:25 +00:00
Jaco Greeff 4eb5d7dfd3 Bump deps (#815)
* Bump deps

* Rewrite
2020-12-22 10:16:34 +01:00
Github Actions 0a841e8f4a [CI Skip] release/stable 5.1.1
skip-checks: true
2020-12-19 14:23:05 +00:00
Jaco Greeff 53ffd0c9f0 5.1 (#813) 2020-12-19 15:17:01 +01:00
Github Actions 5d3a9f2e2e [CI Skip] release/beta 5.0.2-9
skip-checks: true
2020-12-19 10:41:44 +00:00
Jaco Greeff 9d6f38edb3 Bump deps 2020-12-19 11:36:55 +01:00
Github Actions 9e5e032e1d [CI Skip] release/beta 5.0.2-8
skip-checks: true
2020-12-18 10:24:04 +00:00
Jaco Greeff 31426858c6 detectPackage import revert 2020-12-18 11:18:02 +01:00
Github Actions 46b4ca0c91 [CI Skip] release/beta 5.0.2-7
skip-checks: true
2020-12-17 13:02:30 +00:00
Jaco Greeff f4cb68c567 Mark detectPackage as sideEffect 2020-12-17 13:57:41 +01:00
Github Actions ab4b234510 [CI Skip] release/beta 5.0.2-6
skip-checks: true
2020-12-17 12:19:30 +00:00
Jaco Greeff d5151c4f01 Adjust detectPackage 2020-12-17 13:14:38 +01:00
Jaco Greeff 6df4689aa3 Cleanup detectPackage 2020-12-17 13:05:26 +01:00
Github Actions 9727a6a9a5 [CI Skip] release/beta 5.0.2-5
skip-checks: true
2020-12-17 11:42:14 +00:00
Jaco Greeff 7f00182fd9 Bump wasm-crypto 2020-12-17 12:37:21 +01:00
Github Actions 261b0add27 [CI Skip] release/beta 5.0.2-4
skip-checks: true
2020-12-17 11:25:06 +00:00
Jaco Greeff 5d6e753384 sideEffects 2020-12-17 12:20:15 +01:00
Github Actions 15c2e79e6e [CI Skip] release/beta 5.0.2-3
skip-checks: true
2020-12-17 11:12:29 +00:00
Jaco Greeff f763a66219 Allow detectPackage with a function (#811) 2020-12-17 12:07:00 +01:00
Jaco Greeff 1061df6e4a Adjust blakejs commonjs imports (#809) 2020-12-17 12:03:21 +01:00
Github Actions 4c121e6f50 [CI Skip] release/beta 5.0.2-2
skip-checks: true
2020-12-17 10:52:01 +00:00
Jaco Greeff 74b3a04c4a Dedupe 2020-12-17 11:46:57 +01:00
Github Actions e4488463ba [CI Skip] release/beta 5.0.2-1
skip-checks: true
2020-12-16 14:48:44 +00:00
Jaco Greeff c6fd67b5d5 detectPackage with import.meta.url (#810)
* detectPackage with import.meta.url

* Bump dev

* Remove duplicate detectPackage

* Fix detectPackage import
2020-12-16 15:44:00 +01:00
Github Actions e5dc596867 [CI Skip] release/beta 5.0.2-0
skip-checks: true
2020-12-15 14:54:43 +00:00
Jaco Greeff a28f8dad37 detectPackage cleanups (#807) 2020-12-15 15:49:00 +01:00
32 changed files with 608 additions and 396 deletions
+24 -1
View File
@@ -1,8 +1,29 @@
# CHANGELOG
## 5.2.1 Dec 27, 2020
Changes:
- Add `arrayFlatten` to merge a series of arrays into a single
- Add `arrayChunk` to chunk an array into multiples of defined size
- Add `bitLength` param (allowed 256, 512) to `keccakAs{Hex, U8a}`
- Align `@polkadot/networks` with Substrate master
## 5.1.1 Dec 19, 2020
Changes:
- `detectPackage` now can take a path-retrieval callback as second argument
- Adjust `blake2js` imports to consistently cater for esm & cjs usage
- Explicitly mark `detectPackage` usage as having side-effects
- Align `@polkadot/networks` with Substrate master
- `@polkadot/wasm-crypto` 3.1.1
## 5.0.1 Dec 13, 2020
**Important** While this package contains no external interface changes, it doe now compile and ship both cjs and esm modules. This means that modern bundlers will have more information for tree-shaking available. Additionally when using Node with `.mjs` extensions, the esm version imports will be used on recent Node versions. Adding an export map, as here, may have some impacts so a major semver bump is advised. There may be (small) dragons hiding in the dark corners...
**Important** While this package contains no external interface changes, it now compiles and ships both cjs and esm modules. This means that modern bundlers will have more information for tree-shaking available. Additionally when using Node with `.mjs` extensions, the esm version imports will be used on recent Node versions. Adding an export map, as here, may have some impacts so a major semver bump is advised. There may be (small) dragons hiding in the dark corners...
Changes:
@@ -12,6 +33,8 @@ Changes:
- Debug logs now don't depend on `NODE_ENV`, but rather `DEBUG=<type,type,...>` (`*` logs all)
- Limit debug console output via `DEBUG_MAX=<number>` value, trimming when passed
- Add `memoize` util, allowing for function memoization (as used in the API)
- Align `@polkadot/networks` with Substrate master
- `@polkadot/wasm-crypto` 3.0.1
## 4.2.1 Nov 23, 2020
+2 -2
View File
@@ -21,9 +21,9 @@
},
"devDependencies": {
"@babel/core": "^7.12.10",
"@polkadot/dev": "^0.61.2",
"@polkadot/dev": "^0.61.16",
"@polkadot/ts": "^0.3.58",
"@types/jest": "^26.0.19"
},
"version": "5.0.1"
"version": "5.2.1"
}
+10 -6
View File
@@ -1,23 +1,27 @@
{
"name": "@polkadot/keyring",
"version": "5.0.1",
"version": "5.2.1",
"description": "Keyring management",
"author": "Jaco Greeff <jacogr@gmail.com>",
"maintainers": [],
"contributors": [],
"license": "Apache-2.0",
"main": "index.js",
"sideEffects": false,
"module": "index.mjs",
"sideEffects": [
"./detectPackage.js",
"./detectPackage.mjs"
],
"repository": "github:polkadot-js/common",
"bugs": "https://github.com/polkadot-js/common/issues",
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/keyring#readme",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@polkadot/util": "5.0.1",
"@polkadot/util-crypto": "5.0.1"
"@polkadot/util": "5.2.1",
"@polkadot/util-crypto": "5.2.1"
},
"peerDependencies": {
"@polkadot/util": "5.0.1",
"@polkadot/util-crypto": "5.0.1"
"@polkadot/util": "5.2.1",
"@polkadot/util-crypto": "5.2.1"
}
}
+19 -3
View File
@@ -1,9 +1,25 @@
// Copyright 2017-2020 @polkadot/keyring authors & contributors
// SPDX-License-Identifier: Apache-2.0
// For esm, this should be import.meta.url or to get the same behavior as __dirname, we can use
//
// new URL(import.meta.url).pathname)
//
// The issue is the WP4 has "some" issues with import.meta.url. So because of bundlers, we can't have
// nice things... In this case it is even worse since import.meta.url won't even make it compile, so
// there is a complete dead end with usage thereof
//
// When that is fixed, a solution is to have both .js & .mjs files, with the following content -
//
// cjs: util.detectPackage(packageInfo, () => __dirname);
// esm: detectPackage(packageInfo, () => import.meta.url);
//
// With the above we additionally need a .d.ts to just export the packageInfo
import { detectPackage } from '@polkadot/util';
import packageJson from './package.json';
import packageInfo from './package-info.json';
// eslint-disable-next-line @typescript-eslint/no-var-requires
detectPackage(packageJson, typeof __dirname !== 'undefined' && __dirname);
detectPackage(packageInfo, typeof __dirname !== 'undefined' && __dirname);
export { packageInfo };
+4 -3
View File
@@ -1,8 +1,9 @@
{
"name": "@polkadot/networks",
"version": "5.0.1",
"version": "5.2.1",
"description": "A list of all available Substrate networks and their applicable prefixes",
"main": "index.js",
"module": "index.mjs",
"sideEffects": false,
"repository": "github:polkadot-js/common",
"author": "Jaco Greeff <jacogr@gmail.com>",
@@ -15,7 +16,7 @@
"@babel/runtime": "^7.12.5"
},
"devDependencies": {
"@polkadot/util": "5.0.1",
"@polkadot/x-fetch": "5.0.1"
"@polkadot/util": "5.2.1",
"@polkadot/x-fetch": "5.2.1"
}
}
+9
View File
@@ -296,6 +296,15 @@ const all: NetworkFromSubstrate[] = [
symbols: null,
website: null
},
{
decimals: [15],
displayName: 'Valiu Liquidity Network',
network: 'vln',
prefix: 35,
standardAccount: '*25519',
symbols: ['USDv'],
website: 'https://valiu.com/'
},
{
decimals: [18],
displayName: 'Centrifuge Chain',
+11 -7
View File
@@ -1,9 +1,13 @@
{
"name": "@polkadot/util-crypto",
"version": "5.0.1",
"version": "5.2.1",
"description": "A collection of useful crypto utilities for @polkadot",
"main": "index.js",
"sideEffects": false,
"module": "index.mjs",
"sideEffects": [
"./detectPackage.js",
"./detectPackage.mjs"
],
"author": "Jaco Greeff <jacogr@gmail.com>",
"maintainers": [],
"contributors": [],
@@ -13,10 +17,10 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@polkadot/networks": "5.0.1",
"@polkadot/util": "5.0.1",
"@polkadot/wasm-crypto": "^3.0.1",
"@polkadot/x-randomvalues": "5.0.1",
"@polkadot/networks": "5.2.1",
"@polkadot/util": "5.2.1",
"@polkadot/wasm-crypto": "^3.1.1",
"@polkadot/x-randomvalues": "5.2.1",
"base-x": "^3.0.8",
"blakejs": "^1.1.0",
"bn.js": "^4.11.9",
@@ -36,6 +40,6 @@
"@types/xxhashjs": "^0.2.2"
},
"peerDependencies": {
"@polkadot/util": "5.0.1"
"@polkadot/util": "5.2.1"
}
}
+2 -2
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { blake2b as blakejs } from 'blakejs';
import js from 'blakejs';
import { u8aToU8a } from '@polkadot/util';
import { blake2b, isReady } from '@polkadot/wasm-crypto';
@@ -25,5 +25,5 @@ export function blake2AsU8a (data: Uint8Array | string, bitLength = 256, key: Ui
return isReady() && !onlyJs
? blake2b(u8aToU8a(data), u8aToU8a(key), byteLength)
: blakejs(u8aToU8a(data), key, byteLength);
: js.blake2b(u8aToU8a(data), key, byteLength);
}
-5
View File
@@ -16,8 +16,3 @@ export function cryptoWaitReady (): Promise<boolean> {
return false;
});
}
// start init process immediately
cryptoWaitReady().catch((): void => {
// shouldn't happen, logged above
});
+25
View File
@@ -0,0 +1,25 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// For esm, this should be import.meta.url or to get the same behavior as __dirname, we can use
//
// new URL(import.meta.url).pathname)
//
// The issue is the WP4 has "some" issues with import.meta.url. So because of bundlers, we can't have
// nice things... In this case it is even worse since import.meta.url won't even make it compile, so
// there is a complete dead end with usage thereof
//
// When that is fixed, a solution is to have both .js & .mjs files, with the following content -
//
// cjs: util.detectPackage(packageInfo, () => __dirname);
// esm: detectPackage(packageInfo, () => import.meta.url);
//
// With the above we additionally need a .d.ts to just export the packageInfo
import { detectPackage } from '@polkadot/util';
import packageInfo from './package-info.json';
detectPackage(packageInfo, typeof __dirname !== 'undefined' && __dirname);
export { packageInfo };
+2 -2
View File
@@ -2,8 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import './cryptoInit';
export * from './crypto';
import './detectPackage';
// all internal exports
export * from './address';
@@ -11,6 +10,7 @@ export * from './base32';
export * from './base58';
export * from './base64';
export * from './blake2';
export * from './crypto';
export * from './ethereum';
export * from './keccak';
export * from './key';
+13 -6
View File
@@ -5,17 +5,24 @@ import { waitReady } from '@polkadot/wasm-crypto';
import { keccakAsHex } from '.';
const value = 'test value';
const result = '2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e';
const BITS: (256 | 512)[] = [256, 512];
const value = 'test';
const result = {
256: '9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658',
512: '1e2e9fc2002b002d75198b7503210c05a1baac4560916a3c6d93bcce3a50d7f00fd395bf1647b9abb8d1afcc9c76c289b0c9383ba386a956da4b38934417789e'
};
describe('keccakAsHex', (): void => {
beforeEach(async (): Promise<void> => {
await waitReady();
});
it('returns a prefixed hex representation', (): void => {
expect(
keccakAsHex(value)
).toEqual(`0x${result}`);
BITS.forEach((bitLength): void => {
it('returns a prefixed hex representation', (): void => {
expect(
keccakAsHex(value, bitLength)
).toEqual(`0x${result[bitLength]}`);
});
});
});
+2 -2
View File
@@ -19,8 +19,8 @@ import { keccakAsU8a } from './asU8a';
* keccakAsHex('123'); // => 0x...
* ```
*/
export function keccakAsHex (value: Buffer | Uint8Array | string): string {
export function keccakAsHex (value: Buffer | Uint8Array | string, bitLength?: 256 | 512): string {
return u8aToHex(
keccakAsU8a(value)
keccakAsU8a(value, bitLength)
);
}
+31 -18
View File
@@ -6,37 +6,50 @@ import { waitReady } from '@polkadot/wasm-crypto';
import { keccakAsU8a } from '.';
const BITS: (256 | 512)[] = [256, 512];
describe('keccakAsU8a', (): void => {
beforeEach(async (): Promise<void> => {
await waitReady();
});
const input = 'test value';
const output = hexToU8a(
'0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e'
);
const output = {
256: hexToU8a(
'0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e'
),
512: hexToU8a(
'0xc1b50cc57f85ccd968a9d7c7a809dcebd140a548c8e0b67f3afcdd6fc14cca2b1d04187aef24ba0081b74f2ec362431e425760febe94a5607790854cafe5b197'
)
};
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (string, onlyJs=${onlyJs.toString()})`, (): void => {
expect(
keccakAsU8a(input, onlyJs)
).toEqual(output);
BITS.forEach((bitLength): void => {
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (string, bitLength=${bitLength}, onlyJs=${onlyJs.toString()})`, (): void => {
expect(
keccakAsU8a(input, bitLength, onlyJs)
).toEqual(output[bitLength]);
});
});
});
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (Buffer, onlyJs=${onlyJs.toString()})`, (): void => {
expect(
keccakAsU8a(Buffer.from(input), onlyJs)
).toEqual(output);
BITS.forEach((bitLength): void => {
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (Buffer, bitLength=${bitLength}, onlyJs=${onlyJs.toString()})`, (): void => {
expect(
keccakAsU8a(Buffer.from(input), bitLength, onlyJs)
).toEqual(output[bitLength]);
});
});
});
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (Uint8Array, onlyJs=${onlyJs.toString()})`, (): void => {
expect(
keccakAsU8a(stringToU8a(input), onlyJs)
).toEqual(output);
BITS.forEach((bitLength): void => {
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (Uint8Array, bitLength=${bitLength}, onlyJs=${onlyJs.toString()})`, (): void => {
expect(
keccakAsU8a(stringToU8a(input), bitLength, onlyJs)
).toEqual(output[bitLength]);
});
});
});
});
+5 -3
View File
@@ -20,10 +20,12 @@ import { isReady, keccak256 } from '@polkadot/wasm-crypto';
* keccakAsU8a('123'); // => Uint8Array
* ```
*/
export function keccakAsU8a (value: Buffer | Uint8Array | string, onlyJs = false): Uint8Array {
return isReady() && !onlyJs
export function keccakAsU8a (value: Buffer | Uint8Array | string, bitLength: 256 | 512 = 256, onlyJs = false): Uint8Array {
const is256 = bitLength === 256;
return isReady() && is256 && !onlyJs
? keccak256(u8aToU8a(value))
: new Uint8Array(
js.keccak256.update(u8aToU8a(value)).arrayBuffer()
(is256 ? js.keccak256 : js.keccak512).update(u8aToU8a(value)).arrayBuffer()
);
}
+1 -1
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
/**
* @summary Create Keccak256 values as hex & Uint8Array output
* @summary Create Keccak256/512 values as hex & Uint8Array output
*/
export { keccakAsHex } from './asHex';
-12
View File
@@ -1,12 +0,0 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
export function isReady (): boolean {
// always false, when true it will try and use non-existent functions
return false;
}
export function waitReady (): Promise<boolean> {
// always immediate true, our process is done
return Promise.resolve(true);
}
+5 -3
View File
@@ -1,8 +1,10 @@
{
"name": "@polkadot/util",
"version": "5.0.1",
"version": "5.2.1",
"description": "A collection of useful utilities for @polkadot",
"main": "index.js",
"module": "index.mjs",
"sideEffects": false,
"author": "Jaco Greeff <jacogr@gmail.com>",
"maintainers": [],
"contributors": [],
@@ -12,8 +14,8 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util#readme",
"dependencies": {
"@babel/runtime": "^7.12.5",
"@polkadot/x-textdecoder": "5.0.1",
"@polkadot/x-textencoder": "5.0.1",
"@polkadot/x-textdecoder": "5.2.1",
"@polkadot/x-textencoder": "5.2.1",
"@types/bn.js": "^4.11.6",
"bn.js": "^4.11.9",
"camelcase": "^5.3.1",
+12
View File
@@ -0,0 +1,12 @@
// Copyright 2017-2020 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { arrayChunk } from '.';
describe('arrayChunk', (): void => {
it('chunks arrays', (): void => {
expect(
arrayChunk([1, 2, 3, 4, 5, 6, 7], 3)
).toEqual([[1, 2, 3], [4, 5, 6], [7]]);
});
});
+29
View File
@@ -0,0 +1,29 @@
// Copyright 2017-2020 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
/**
* @name arrayChunk
* @summary Split T[] into T[][] based on the defind size
* @description
* Returns a set ao arrays based on the chunksize
* @example
* <BR>
*
* ```javascript
* import { arrayChunk } from '@polkadot/util';
*
* arrayChunk([1, 2, 3, 4, 5]); // [[1, 2], [3, 4], [5]]
* ```
*/
export function arrayChunk <T> (array: T[], chunkSize: number): T[][] {
const outputSize = Math.ceil(array.length / chunkSize);
const output = Array<T[]>(outputSize);
for (let index = 0; index < outputSize; index++) {
const offset = index * chunkSize;
output[index] = array.slice(offset, offset + chunkSize);
}
return output;
}
+12
View File
@@ -0,0 +1,12 @@
// Copyright 2017-2020 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { arrayFlatten } from '.';
describe('arrayFlatten', (): void => {
it('flattens arrays', (): void => {
expect(
arrayFlatten([[0], [1, 2, 3], [4, 5], [6], [], [7, 8]])
).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8]);
});
});
+36
View File
@@ -0,0 +1,36 @@
// Copyright 2017-2020 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
// This is supposed to be a faster concat...
// https://dev.to/uilicious/javascript-array-push-is-945x-faster-than-array-concat-1oki
/**
* @name arrayFlatten
* @summary Merge T[][] into T[]
* @description
* Returns a new array with all arrays merged into one
* @example
* <BR>
*
* ```javascript
* import { arrayFlatten } from '@polkadot/util';
*
* arrayFlatten([[1, 2], [3, 4], [5]]); // [1, 2, 3, 4, 5]
* ```
*/
export function arrayFlatten <T> (arrays: T[][]): T[] {
// pre-allocate based on the combined size
const output = new Array<T>(arrays.reduce((length, array) => length + array.length, 0));
let index = -1;
for (let a = 0; a < arrays.length; a++) {
const array = arrays[a];
// instead of pushing, we just set the entries
for (let e = 0; e < array.length; e++) {
output[++index] = array[e];
}
}
return output;
}
+2
View File
@@ -5,4 +5,6 @@
* @summary Utility methods that operates on arrays
*/
export { arrayChunk } from './chunk';
export { arrayFilter } from './filter';
export { arrayFlatten } from './flatten';
+5 -13
View File
@@ -7,36 +7,28 @@ import BN from 'bn.js';
* @name BN_ZERO
* @summary BN constant for 0.
*/
const BN_ZERO: BN = new BN(0);
export const BN_ZERO: BN = new BN(0);
/**
* @name BN_ONE
* @summary BN constant for 1.
*/
const BN_ONE: BN = new BN(1);
export const BN_ONE: BN = new BN(1);
/**
* @name BN_TEN
* @summary BN constant for 10.
*/
const BN_TEN: BN = new BN(10);
export const BN_TEN: BN = new BN(10);
/**
* @name BN_HUNDRED
* @summary BN constant for 100.
*/
const BN_HUNDRED: BN = new BN(100);
export const BN_HUNDRED: BN = new BN(100);
/**
* @name BN_THOUSAND
* @summary BN constant for 1000.
*/
const BN_THOUSAND: BN = new BN(1000);
export {
BN_ZERO,
BN_ONE,
BN_TEN,
BN_HUNDRED,
BN_THOUSAND
};
export const BN_THOUSAND: BN = new BN(1000);
+12 -1
View File
@@ -7,7 +7,10 @@ describe('assertSingletonPackage', (): void => {
const PKG = '@polkadot/util';
const VER1 = '9.8.0-beta.45';
const VER2 = '9.7.1';
const VER3 = '9.6.1';
const PATH = '/Users/jaco/Projects/polkadot-js/api/node_modules/@polkadot/util';
const RES2 = `Multiple instances of @polkadot/util detected, ensure that there is only one package in your dependency tree.\n\t${VER1}\t${PATH}/01\n\t${VER2} \t${PATH}/02`;
const RES3 = `${RES2}\n\t${VER3} \t${PATH}/03`;
it('should not log the first time', (): void => {
const spy = jest.spyOn(console, 'warn');
@@ -21,7 +24,15 @@ describe('assertSingletonPackage', (): void => {
const spy = jest.spyOn(console, 'warn');
detectPackage({ name: PKG, version: VER2 }, `${PATH}/02`);
expect(spy).toHaveBeenCalledWith(`Multiple instances of @polkadot/util detected, ensure that there is only one package in your dependency tree.\n\t${VER1}\t${PATH}/01\n\t${VER2} \t${PATH}/02`);
expect(spy).toHaveBeenCalledWith(RES2);
spy.mockRestore();
});
it('should allow for function use', (): void => {
const spy = jest.spyOn(console, 'warn');
detectPackage({ name: PKG, version: VER3 }, () => `${PATH}/03`);
expect(spy).toHaveBeenCalledWith(RES3);
spy.mockRestore();
});
});
+47 -22
View File
@@ -1,6 +1,7 @@
// Copyright 2017-2020 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { isFunction } from './is/function';
import { isString } from './is/string';
import { assert } from './assert';
@@ -23,30 +24,15 @@ interface PjsChecks extends This {
type PjsGlobal = NodeJS.Global & PjsChecks;
type PjsWindow = Window & PjsChecks;
type FnString = () => string | undefined;
/** @internal */
function expandPath (path?: string): string {
return (!path || path.length < 5) ? '<unknown>' : path;
}
/** @internal */
function flattenVersions (_all: (VersionPath | string)[]): string {
const all: VersionPath[] = _all.map((version: VersionPath | string): VersionPath =>
isString(version)
? { version }
: version
);
const verLength = all.reduce((max, { version }): number => Math.max(max, version.length), 0);
return all
.map(({ path, version }) => `\t${version.padEnd(verLength)}\t${expandPath(path)}`)
.join('\n');
}
/**
* @name detectPackage
* @summary Checks that a specific package is only imported once
*/
export function detectPackage ({ name, version }: PackageJson, path?: string | false): void {
function getEntry (name: string): VersionPath[] {
const _global = typeof window !== 'undefined'
? window as PjsWindow
: global as PjsGlobal;
@@ -55,13 +41,52 @@ export function detectPackage ({ name, version }: PackageJson, path?: string | f
_global.__polkadotjs = {};
}
if (!_global.__polkadotjs[name]) {
_global.__polkadotjs[name] = [];
}
return _global.__polkadotjs[name];
}
/** @internal */
function flattenVersions (entry: VersionPath[]): string {
const all = entry.map((version: VersionPath | string): VersionPath =>
isString(version)
? { version }
: version
);
const verLength = all.reduce((max, { version }) => Math.max(max, version.length), 0);
return all
.map(({ path, version }) => `\t${version.padEnd(verLength)}\t${expandPath(path)}`)
.join('\n');
}
/** @internal */
function getPath (pathOrFn?: FnString | string | false): false | string | undefined {
if (isFunction(pathOrFn)) {
try {
return pathOrFn();
} catch (error) {
return undefined;
}
}
return pathOrFn;
}
/**
* @name detectPackage
* @summary Checks that a specific package is only imported once
*/
export function detectPackage ({ name, version }: PackageJson, pathOrFn?: FnString | string | false): void {
assert(name.startsWith('@polkadot'), `Invalid package descriptor ${name}`);
_global.__polkadotjs[name] = [...(_global.__polkadotjs[name] || []), { path: path || '', version }];
const entry = getEntry(name);
if (_global.__polkadotjs[name].length !== 1) {
const versions = flattenVersions(_global.__polkadotjs[name]);
entry.push({ path: getPath(pathOrFn) || '', version });
console.warn(`Multiple instances of ${name} detected, ensure that there is only one package in your dependency tree.\n${versions}`);
if (entry.length !== 1) {
console.warn(`Multiple instances of ${name} detected, ensure that there is only one package in your dependency tree.\n${flattenVersions(entry)}`);
}
}
+2 -1
View File
@@ -1,10 +1,11 @@
{
"name": "@polkadot/x-fetch",
"version": "5.0.1",
"version": "5.2.1",
"description": "A cross-environment fetch replacement",
"browser": "browser.js",
"main": "node.js",
"react-native": "react-native.js",
"sideEffects": false,
"author": "Jaco Greeff <jacogr@gmail.com>",
"maintainers": [],
"contributors": [],
+3 -2
View File
@@ -1,10 +1,11 @@
{
"name": "@polkadot/x-randomvalues",
"version": "5.0.1",
"version": "5.2.1",
"description": "A cross-environment window.crypto.getRandomValues replacement",
"browser": "browser.js",
"main": "node.js",
"react-native": "react-native.js",
"sideEffects": false,
"author": "Jaco Greeff <jacogr@gmail.com>",
"maintainers": [],
"contributors": [],
@@ -16,6 +17,6 @@
"@babel/runtime": "^7.12.5"
},
"devDependencies": {
"@types/react-native": "^0.63.37"
"@types/react-native": "^0.63.42"
}
}
+2 -1
View File
@@ -1,10 +1,11 @@
{
"name": "@polkadot/x-textdecoder",
"version": "5.0.1",
"version": "5.2.1",
"description": "A TextDecoder replacement",
"browser": "browser.js",
"main": "node.js",
"react-native": "react-native.js",
"sideEffects": false,
"author": "Jaco Greeff <jacogr@gmail.com>",
"maintainers": [],
"contributors": [],
+2 -1
View File
@@ -1,10 +1,11 @@
{
"name": "@polkadot/x-textencoder",
"version": "5.0.1",
"version": "5.2.1",
"description": "A TextDecoder replacement",
"browser": "browser.js",
"main": "node.js",
"react-native": "react-native.js",
"sideEffects": false,
"author": "Jaco Greeff <jacogr@gmail.com>",
"maintainers": [],
"contributors": [],
+2 -1
View File
@@ -1,10 +1,11 @@
{
"name": "@polkadot/x-ws",
"version": "5.0.1",
"version": "5.2.1",
"description": "A cross-environment WebSocket replacement",
"browser": "browser.js",
"main": "node.js",
"react-native": "react-native.js",
"sideEffects": false,
"author": "Jaco Greeff <jacogr@gmail.com>",
"maintainers": [],
"contributors": [],
+277 -278
View File
File diff suppressed because it is too large Load Diff