Skip to content

Commit

Permalink
Adjust object index typings (#1843)
Browse files Browse the repository at this point in the history
* Adjust object index typings

* Update packages/util/src/is/childClass.ts

* Expand meta definition

* Extend meta with extension info

* Adjust

* CHANGELOG
  • Loading branch information
jacogr committed Jun 10, 2023
1 parent 942b623 commit 5cca95e
Show file tree
Hide file tree
Showing 20 changed files with 219 additions and 135 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
Changes:

- Only allow padding characters in base64 strings
- Expand typings for keyring meta with known keys
- Adjust object index access for stricter tsconfig settings


## 12.2.2 Jun 4, 2023
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"test:one": "polkadot-dev-run-test --env browser"
},
"devDependencies": {
"@polkadot/dev": "^0.75.16",
"@polkadot/dev": "^0.75.19",
"@types/node": "^20.2.5"
},
"resolutions": {
Expand Down
1 change: 1 addition & 0 deletions packages/keyring/src/testingPairs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('testingPairs', (): void => {
'0x4c42532034540267bf568198ccec4cb822a025da542861fcb146a5fab6433ff8',
'0x94c49300a58d576011096bcb006aa06f5a91b34b4383891e8029c21dc39fbb8b'];

// @ts-expect-error We should not delete from the maps, however this is a test
delete ring.nobody;

Object
Expand Down
35 changes: 33 additions & 2 deletions packages/keyring/src/testingPairs.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
// Copyright 2017-2023 @polkadot/keyring authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { KeypairType } from '@polkadot/util-crypto/types';
import type { KeyringOptions, KeyringPair } from './types.js';

import { nobody } from './pair/nobody.js';
import { createTestKeyring } from './testing.js';

export interface TestKeyringMap {
nobody: KeyringPair;

[index: string]: KeyringPair;
}

export function createTestPairs (options?: KeyringOptions, isDerived = true): TestKeyringMap {
export interface TestKeyringMapSubstrate extends TestKeyringMap {
alice: KeyringPair;
bob: KeyringPair;
charlie: KeyringPair;
dave: KeyringPair;
eve: KeyringPair;
ferdie: KeyringPair;
}

export interface TestKeyringMapEthereum extends TestKeyringMap {
Alith: KeyringPair;
Baltathar: KeyringPair;
Charleth: KeyringPair;
Dorothy: KeyringPair;
Ethan: KeyringPair;
Faith: KeyringPair;
}

export type DetectMap<O extends KeyringOptions | undefined> = DetectPairType<O> extends 'ethereum'
? TestKeyringMapEthereum
: TestKeyringMapSubstrate;

export type DetectPairType<O extends KeyringOptions | undefined> = O extends KeyringOptions
? O['type'] extends KeypairType
? O['type']
: 'sr25519'
: 'sr25519';

export function createTestPairs <O extends KeyringOptions, M = DetectMap<O>> (options?: O, isDerived = true): M {
const keyring = createTestKeyring(options, isDerived);
const pairs = keyring.getPairs();
const map: TestKeyringMap = { nobody: nobody() };
Expand All @@ -19,5 +50,5 @@ export function createTestPairs (options?: KeyringOptions, isDerived = true): Te
map[p.meta.name as string] = p;
}

return map;
return map as M;
}
43 changes: 42 additions & 1 deletion packages/keyring/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,48 @@ export interface KeyringOptions {
type?: KeypairType;
}

export type KeyringPair$Meta = Record<string, unknown>;
export interface KeyringPair$MetaHardware {
accountIndex?: number;
addressOffset?: number;
hardwareType?: 'ledger';
}

export interface KeyringPair$MetaFlags {
isDefaultAuthSelected?: boolean;
isExternal?: boolean;
isHardware?: boolean;
isHidden?: boolean;
isInjected?: boolean;
isMultisig?: boolean;
isRecent?: boolean;
isTesting?: boolean;
}

export interface KeyringPair$MetaContract {
abi: string;
genesisHash?: HexString | null;
}

export interface KeyringPair$MetaParent {
parentAddress?: string;
parentName?: string;
}

export interface KeyringPair$Meta extends KeyringPair$MetaHardware, KeyringPair$MetaFlags, KeyringPair$MetaParent {
address?: string;
contract?: KeyringPair$MetaContract;
genesisHash?: HexString | null;
name?: string;
suri?: string;
tags?: string[];
threshold?: number;
type?: KeypairType;
whenCreated?: number;
whenEdited?: number;
whenUsed?: number;

[key: string]: unknown;
}

export interface KeyringPair$Json extends EncryptedJson {
/** The ss58 encoded address or the hex-encoded version (the latter is for ETH-compat chains) */
Expand Down
2 changes: 1 addition & 1 deletion packages/networks/src/ss58registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('@substrate/ss58-registry', (): void => {
try {
expect(JSON.parse(json)).toEqual(other);
} catch (error) {
if (process.env.GITHUB_REPOSITORY) {
if (process.env['GITHUB_REPOSITORY']) {
console.error(json);

throw error;
Expand Down
4 changes: 2 additions & 2 deletions packages/util/src/has.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2017-2023 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { BufferObjConstructor } from './types.js';
import type { BufferObjClass } from './types.js';

import { BigInt } from '@polkadot/x-bigint';
import { xglobal } from '@polkadot/x-global';
Expand Down Expand Up @@ -32,7 +32,7 @@ export const hasWasm = typeof WebAssembly !== 'undefined';
// that some bundlers such as parcel would add (this is a check, not a use)

/** true if the environment has support for Buffer (typically Node.js) */
export const hasBuffer = typeof xglobal.Buffer === 'function' && typeof (xglobal.Buffer as unknown as BufferObjConstructor).isBuffer === 'function';
export const hasBuffer = typeof xglobal.Buffer === 'function' && typeof (xglobal.Buffer as unknown as BufferObjClass).isBuffer === 'function';

/** true if the environment has process available (typically Node.js) */
export const hasProcess = typeof xglobal.process === 'object';
4 changes: 2 additions & 2 deletions packages/util/src/is/buffer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2017-2023 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { BufferObj, BufferObjConstructor } from '../types.js';
import type { BufferObj, BufferObjClass } from '../types.js';

import { xglobal } from '@polkadot/x-global';

Expand All @@ -24,5 +24,5 @@ import { isFunction } from './function.js';
*/
export function isBuffer (value: unknown): value is Buffer {
// we do check a function first, since it is slightly faster than isBuffer itself
return hasBuffer && !!value && isFunction((value as unknown as BufferObj).readDoubleLE) && (xglobal.Buffer as unknown as BufferObjConstructor).isBuffer(value);
return hasBuffer && !!value && isFunction((value as unknown as BufferObj).readDoubleLE) && (xglobal.Buffer as unknown as BufferObjClass).isBuffer(value);
}
4 changes: 2 additions & 2 deletions packages/util/src/is/childClass.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2017-2023 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { Constructor } from '../types.js';
import type { Class } from '../types.js';

import { isClass } from './class.js';

Expand All @@ -20,7 +20,7 @@ import { isClass } from './class.js';
* console.log('isChildClass', isChildClass(BN, Uint8Array); // => false
* ```
*/
export function isChildClass <P extends Constructor> (Parent: P, Child?: unknown): Child is P {
export function isChildClass <P extends Class> (Parent: P, Child?: unknown): Child is P {
// https://stackoverflow.com/questions/30993434/check-if-a-constructor-inherits-another-in-es6/30993664
return isClass(Child) && isClass(Parent)
// eslint-disable-next-line no-prototype-builtins
Expand Down
4 changes: 2 additions & 2 deletions packages/util/src/is/class.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2017-2023 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { Constructor } from '../types.js';
import type { Class } from '../types.js';

import { isOnFunction } from './helpers.js';

/**
* @name isClass
* Tests if the supplied argument is a Class
*/
export const isClass: <T extends Constructor> (value?: unknown) => value is T = /*#__PURE__*/ isOnFunction('isPrototypeOf', 'hasOwnProperty');
export const isClass: <T extends Class> (value?: unknown) => value is T = /*#__PURE__*/ isOnFunction('isPrototypeOf', 'hasOwnProperty');
21 changes: 11 additions & 10 deletions packages/util/src/lazy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { lazyMethod, lazyMethods } from './index.js';

describe('lazyMethod', (): void => {
it('adds a key on the object', (): void => {
const test: Record<string, unknown> = {};
const test: { a?: number } = {};

expect(test.a).not.toBeDefined();

Expand All @@ -17,7 +17,7 @@ describe('lazyMethod', (): void => {
});

it('allows the name to be retrieved', (): void => {
const test: Record<string, unknown> = {};
const test: { a?: number } = {};

expect(test.a).not.toBeDefined();

Expand All @@ -27,7 +27,7 @@ describe('lazyMethod', (): void => {
});

it('calls the getter lazily', (): void => {
const test: Record<string, unknown> = {};
const test: { a?: number } = {};
const getter = jest.fn(() => 123);

expect(test.a).not.toBeDefined();
Expand All @@ -41,7 +41,7 @@ describe('lazyMethod', (): void => {
});

it('calls the getter a single time', (): void => {
const test: Record<string, unknown> = {};
const test: { a?: number } = {};
const getter = jest.fn(() => 123);

lazyMethod(test, 'a', getter);
Expand All @@ -56,15 +56,16 @@ describe('lazyMethod', (): void => {

describe('lazyMethods', (): void => {
it('sets multiple properties', (): void => {
const test: Record<string, unknown> = {};
const test: { a?: string; b?: string; c?: string } = {};
const getter = jest.fn((name: string) => name);
const set = lazyMethods(test, ['a', 'b', 'c'], getter);

expect(set.a).toEqual('a');
expect(set.b).toEqual('b');
lazyMethods(test, ['a', 'b', 'c'], getter);

expect(test.a).toEqual('a');
expect(test.b).toEqual('b');
expect(getter).toHaveBeenCalledTimes(2);
expect(set.a).toEqual('a');
expect(set.b).toEqual('b');
expect(test.a).toEqual('a');
expect(test.b).toEqual('b');
expect(getter).toHaveBeenCalledTimes(2);
});
});
16 changes: 8 additions & 8 deletions packages/util/src/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ describe('logger', (): void => {

beforeEach((): void => {
oldEnv = process.env;
process.env.NODE_ENV = 'development';
process.env['NODE_ENV'] = 'development';

ln = logger('notDebug');

process.env.DEBUG = 'test';
process.env['DEBUG'] = 'test';

l = logger('test');

Expand Down Expand Up @@ -114,7 +114,7 @@ describe('logger', (): void => {
});

it('does debug log when DEBUG partial specified', (): void => {
process.env.DEBUG = 'test*';
process.env['DEBUG'] = 'test*';

l = logger('testing');
l.debug('test');
Expand All @@ -127,7 +127,7 @@ describe('logger', (): void => {
});

it('does not debug log when non-matching DEBUG specified', (): void => {
process.env.DEBUG = 'blah';
process.env['DEBUG'] = 'blah';

l = logger('test');
l.debug('test');
Expand All @@ -136,7 +136,7 @@ describe('logger', (): void => {
});

it('does debug log when DEBUG=* specified', (): void => {
process.env.DEBUG = '*';
process.env['DEBUG'] = '*';

l = logger('test');
l.debug('test');
Expand All @@ -160,7 +160,7 @@ describe('logger', (): void => {
});

it('does not debug log when explicitly excluded', (): void => {
process.env.DEBUG = '*,-test';
process.env['DEBUG'] = '*,-test';

l = logger('test');
l.debug('test');
Expand All @@ -169,7 +169,7 @@ describe('logger', (): void => {
});

it('does not debug log when part of exclusion group', (): void => {
process.env.DEBUG = '*,-test:*';
process.env['DEBUG'] = '*,-test:*';

l = logger('test:sub');
l.debug('test');
Expand All @@ -178,7 +178,7 @@ describe('logger', (): void => {
});

it('does debug log when not part of exclusion groups', (): void => {
process.env.DEBUG = '*,-test:*,-tes,-a:*';
process.env['DEBUG'] = '*,-test:*,-tes,-a:*';

l = logger('test');
l.debug('test');
Expand Down
4 changes: 2 additions & 2 deletions packages/util/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ function getDebugFlag (env: readonly string[], type: string): boolean {

function parseEnv (type: string): [boolean, number] {
const env = (hasProcess ? xglobal.process as { env: Record<string, string> } : {}).env || {};
const maxSize = parseInt(env.DEBUG_MAX || '-1', 10);
const maxSize = parseInt(env['DEBUG_MAX'] || '-1', 10);

return [
getDebugFlag((env.DEBUG || '').toLowerCase().split(','), type),
getDebugFlag((env['DEBUG'] || '').toLowerCase().split(','), type),
isNaN(maxSize)
? -1
: maxSize
Expand Down
Loading

0 comments on commit 5cca95e

Please sign in to comment.