Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
83975e9235 | ||
|
|
3adf475ef4 | ||
|
|
ccc6edebbb | ||
|
|
0d7ed7420d | ||
|
|
dbf1837b97 | ||
|
|
9ae738ac51 | ||
|
|
a4000efc43 | ||
|
|
7871a17942 | ||
|
|
5aad1d09b2 | ||
|
|
4eb5d7dfd3 | ||
|
|
0a841e8f4a | ||
|
|
53ffd0c9f0 | ||
|
|
5d3a9f2e2e | ||
|
|
9d6f38edb3 | ||
|
|
9e5e032e1d | ||
|
|
31426858c6 | ||
|
|
46b4ca0c91 | ||
|
|
f4cb68c567 | ||
|
|
ab4b234510 | ||
|
|
d5151c4f01 | ||
|
|
6df4689aa3 | ||
|
|
9727a6a9a5 | ||
|
|
7f00182fd9 | ||
|
|
261b0add27 | ||
|
|
5d6e753384 | ||
|
|
15c2e79e6e | ||
|
|
f763a66219 | ||
|
|
1061df6e4a | ||
|
|
4c121e6f50 | ||
|
|
74b3a04c4a | ||
|
|
e4488463ba | ||
|
|
c6fd67b5d5 | ||
|
|
e5dc596867 | ||
|
|
a28f8dad37 |
+24
-1
@@ -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
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 };
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -16,8 +16,3 @@ export function cryptoWaitReady (): Promise<boolean> {
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
// start init process immediately
|
||||
cryptoWaitReady().catch((): void => {
|
||||
// shouldn't happen, logged above
|
||||
});
|
||||
|
||||
@@ -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,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';
|
||||
|
||||
@@ -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]}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
@@ -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]]);
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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]);
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
@@ -5,4 +5,6 @@
|
||||
* @summary Utility methods that operates on arrays
|
||||
*/
|
||||
|
||||
export { arrayChunk } from './chunk';
|
||||
export { arrayFilter } from './filter';
|
||||
export { arrayFlatten } from './flatten';
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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)}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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": [],
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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": [],
|
||||
|
||||
@@ -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": [],
|
||||
|
||||
@@ -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": [],
|
||||
|
||||
Reference in New Issue
Block a user