Skip to content

Commit f8fe7d8

Browse files
fboucquezrg911
andauthored
fix: replaced instanceof statement (#819)
fix: fixed new Buffer warning. fix: rxjs precated use Co-authored-by: Steven Liu <xian.f.liu@gmail.com>
1 parent 2274bf3 commit f8fe7d8

25 files changed

+171
-112
lines changed

CHANGELOG.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@ All notable changes to this project will be documented in this file.
44

55
The changelog format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

7+
## [1.0.3] - NEXT
8+
9+
**Milestone**: Symbol Mainnet
10+
Package | Version | Link
11+
---|---|---
12+
SDK Core| v1.0.3 | [symbol-sdk](https://www.npmjs.com/package/symbol-sdk)
13+
Catbuffer | v1.0.1 | [catbuffer-typescript](https://www.npmjs.com/package/catbuffer-typescript)
14+
Client Library | v1.0.2 | [symbol-openapi-typescript-fetch-client](https://www.npmjs.com/package/symbol-openapi-typescript-fetch-client)
15+
16+
- fix: replaced `instanceof` statements. These statements are problematic when npm installs the dependency in multiples modules.
17+
718
## [1.0.2] - 25-Oct-2021
819

920
**Milestone**: Symbol Mainnet
1021
Package | Version | Link
1122
---|---|---
1223
SDK Core| v1.0.2 | [symbol-sdk](https://www.npmjs.com/package/symbol-sdk)
1324
Catbuffer | v1.0.1 | [catbuffer-typescript](https://www.npmjs.com/package/catbuffer-typescript)
14-
Client Library | v1.0.1 | [symbol-openapi-typescript-fetch-client](https://www.npmjs.com/package/symbol-openapi-typescript-fetch-client)
25+
Client Library | v1.0.2 | [symbol-openapi-typescript-fetch-client](https://www.npmjs.com/package/symbol-openapi-typescript-fetch-client)
1526

1627
- feat: Multisig multilevel subscription in web listener.
1728
- feat: Added Deployment data to `ServerInfo`.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"e2e": "npm run bootstrap-stop && npm run bootstrap-start-detached && npm run test:e2e && npm run bootstrap-stop",
1111
"test:all": "mocha --ui bdd --recursive ./dist/ --timeout 90000 --unhandled-rejections=strict",
1212
"build": "shx rm -rf dist/ && tsc",
13+
"watch": "tsc --watch",
1314
"test:cov": "nyc --reporter=lcov --reporter=text-summary npm t",
1415
"test:coveralls": "npm run test:cov | coveralls",
1516
"coveralls-report": "cat ./coverage/lcov.info | coveralls",

src/core/format/RawAddress.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class RawAddress {
8383
const publicKeyHash = sha3_256.arrayBuffer(publicKey);
8484

8585
// step 2: ripemd160 hash of (1)
86-
const ripemdHash = new ripemd160().update(new Buffer(publicKeyHash)).digest();
86+
const ripemdHash = new ripemd160().update(Buffer.from(publicKeyHash)).digest();
8787

8888
// step 3: add network identifier byte in front of (2)
8989
const decodedAddress = new Uint8Array(RawAddress.constants.sizes.addressDecoded);

src/core/utils/UnresolvedMapping.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class UnresolvedMapping {
9393
* @return {Uint8Array}
9494
*/
9595
public static toUnresolvedAddressBytes(unresolvedAddress: UnresolvedAddress, networkType: NetworkType): Uint8Array {
96-
if (unresolvedAddress instanceof NamespaceId) {
96+
if (unresolvedAddress.isNamespaceId()) {
9797
// received hexadecimal notation of namespaceId (alias)
9898
return RawAddress.aliasToRecipient(Convert.hexToUint8((unresolvedAddress as NamespaceId).toHex()), networkType);
9999
} else {

src/infrastructure/Listener.ts

+6-13
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { BlockInfoDTO } from 'symbol-openapi-typescript-fetch-client';
2020
import * as WebSocket from 'ws';
2121
import { parseObjectProperties } from '../core/format/Utilities';
2222
import { DtoMapping, MultisigChildrenTreeObject, MultisigGraphUtils } from '../core/utils';
23-
import { MultisigAccountInfo, UnresolvedAddress } from '../model';
23+
import { MultisigAccountInfo, NamespaceId, UnresolvedAddress } from '../model';
2424
import { Address } from '../model/account/Address';
2525
import { PublicAccount } from '../model/account/PublicAccount';
2626
import { FinalizedBlock } from '../model/blockchain/FinalizedBlock';
@@ -270,7 +270,6 @@ export class Listener implements IListener {
270270
return this.messageSubject.asObservable().pipe(
271271
share(),
272272
filter((_) => _.channelName === ListenerChannelName.block),
273-
filter((_) => _.message instanceof NewBlock),
274273
map((_) => _.message as NewBlock),
275274
);
276275
}
@@ -287,7 +286,6 @@ export class Listener implements IListener {
287286
return this.messageSubject.asObservable().pipe(
288287
share(),
289288
filter((_) => _.channelName === ListenerChannelName.finalizedBlock),
290-
filter((_) => _.message instanceof FinalizedBlock),
291289
map((_) => _.message as FinalizedBlock),
292290
);
293291
}
@@ -360,7 +358,6 @@ export class Listener implements IListener {
360358
switchMap((subscribers) => {
361359
return this.messageSubject.asObservable().pipe(
362360
filter((listenerMessage) => listenerMessage.channelName === channel),
363-
filter((listenerMessage) => listenerMessage.message instanceof Transaction),
364361
distinctUntilChanged((prev, curr) => {
365362
const currentHash = (curr.message as Transaction).transactionInfo!.hash;
366363
const previousHash = (prev.message as Transaction).transactionInfo!.hash;
@@ -470,7 +467,6 @@ export class Listener implements IListener {
470467
this.subscribeTo(`status/${address.plain()}`);
471468
return this.messageSubject.asObservable().pipe(
472469
filter((_) => _.channelName === ListenerChannelName.status),
473-
filter((_) => _.message instanceof TransactionStatusError),
474470
filter((_) => _.channelParam.toUpperCase() === address.plain()),
475471
map((_) => _.message as TransactionStatusError),
476472
filter((_) => !transactionHash || _.hash.toUpperCase() == transactionHash.toUpperCase()),
@@ -507,7 +503,6 @@ export class Listener implements IListener {
507503
switchMap((subscribers) => {
508504
return this.messageSubject.asObservable().pipe(
509505
filter((_) => _.channelName.toUpperCase() === ListenerChannelName.cosignature.toUpperCase()),
510-
filter((_) => _.message instanceof CosignatureSignedTransaction),
511506
filter((_) => subscribers.includes(_.channelParam.toUpperCase())),
512507
map((_) => _.message as CosignatureSignedTransaction),
513508
);
@@ -535,20 +530,18 @@ export class Listener implements IListener {
535530
* @returns {Address}
536531
*/
537532
private getResolvedAddress(unresolvedAddress: UnresolvedAddress): Observable<Address> {
538-
if (unresolvedAddress instanceof Address) {
539-
return of(unresolvedAddress);
533+
if (unresolvedAddress.isAddress()) {
534+
return of(unresolvedAddress as Address);
540535
}
541536

542-
return this.namespaceRepository.getLinkedAddress(unresolvedAddress).pipe(
537+
const namespaceId = unresolvedAddress as NamespaceId;
538+
return this.namespaceRepository.getLinkedAddress(namespaceId).pipe(
543539
map((address) => {
544540
if (!address) {
545-
throw new Error(`Invalid unresolvedAddress: ${unresolvedAddress.toHex()}`);
541+
throw new Error(`Invalid unresolvedAddress: ${namespaceId.toHex()}`);
546542
}
547543
return address;
548544
}),
549-
catchError((err) => {
550-
throw new Error(err);
551-
}),
552545
);
553546
}
554547

src/model/UInt64.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export class UInt64 {
136136
* @returns {boolean}
137137
*/
138138
public equals(other: UInt64): boolean {
139-
return this.lower === other.lower && this.higher === other.higher;
139+
return other && this.lower === other.lower && this.higher === other.higher;
140140
}
141141

142142
/**

src/model/account/Address.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,7 @@ export class Address {
139139
* @returns {boolean}
140140
*/
141141
public equals(address: any): boolean {
142-
if (address instanceof Address) {
143-
return this.plain() === address.plain() && this.networkType === address.networkType;
144-
}
145-
return false;
142+
return address && this.address == address.address && this.networkType === address.networkType;
146143
}
147144

148145
/**
@@ -167,8 +164,27 @@ export class Address {
167164
* zero padded.
168165
* @returns {Uint8Array}
169166
*/
170-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
171-
public encodeUnresolvedAddress(networkType: NetworkType): Uint8Array {
167+
public encodeUnresolvedAddress(): Uint8Array {
172168
return Convert.hexToUint8(this.encoded());
173169
}
170+
171+
/**
172+
* returns that this instance is a resolved, not an alias, address.
173+
*/
174+
public isNamespaceId(): boolean {
175+
return false;
176+
}
177+
/**
178+
* returns that the instance is an address
179+
*/
180+
public isAddress(): boolean {
181+
return true;
182+
}
183+
184+
/**
185+
* returns that the instance is not a mosaic id
186+
*/
187+
public isMosaicId(): boolean {
188+
return false;
189+
}
174190
}

src/model/account/UnresolvedAddress.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,21 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { NamespaceId } from '../namespace/NamespaceId';
17+
import { NamespaceId } from '../namespace';
1818
import { Address } from './Address';
1919

2020
/**
2121
* Custom type for unresolved address
2222
*/
23-
export type UnresolvedAddress = Address | NamespaceId;
23+
export type UnresolvedAddress = (Address | NamespaceId) & {
24+
/**
25+
* returns if the object is instance of NamespaceId.
26+
* It avoid the `instanceof` issue when the sdk lib is referenced from multiple modules
27+
*/
28+
isNamespaceId(): boolean;
29+
/**
30+
* returns if the object is instance of Address.
31+
* It avoid the `instanceof` issue when the sdk lib is referenced from multiple modules
32+
*/
33+
isAddress(): boolean;
34+
};

src/model/mosaic/MosaicId.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ export class MosaicId {
7979
* @return boolean
8080
*/
8181
public equals(other: any): boolean {
82-
if (other instanceof MosaicId) {
83-
return this.id.equals(other.id);
84-
}
85-
return false;
82+
return other && this.id.equals(other.id);
8683
}
8784

8885
/**
@@ -91,4 +88,24 @@ export class MosaicId {
9188
toBuilder(): MosaicIdDto {
9289
return new MosaicIdDto(this.id.toDTO());
9390
}
91+
92+
/**
93+
* returns that this instance is an alias.
94+
*/
95+
public isNamespaceId(): boolean {
96+
return false;
97+
}
98+
/**
99+
* returns that the instance is not address
100+
*/
101+
public isAddress(): boolean {
102+
return false;
103+
}
104+
105+
/**
106+
* returns that the instance is a mosaic id
107+
*/
108+
public isMosaicId(): boolean {
109+
return true;
110+
}
94111
}

src/model/mosaic/UnresolvedMosaicId.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,21 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { NamespaceId } from '../namespace/NamespaceId';
17+
import { NamespaceId } from '../namespace';
1818
import { MosaicId } from './MosaicId';
1919

2020
/**
2121
* Custom type for unresolved mosaicId
2222
*/
23-
export type UnresolvedMosaicId = MosaicId | NamespaceId;
23+
export type UnresolvedMosaicId = (MosaicId | NamespaceId) & {
24+
/**
25+
* returns if the object is instance of NamespaceId.
26+
* It avoid the `instanceof` issue when the sdk lib is referenced from multiple modules
27+
*/
28+
isNamespaceId(): boolean;
29+
/**
30+
* returns if the object is instance of MosaicId.
31+
* It avoid the `instanceof` issue when the sdk lib is referenced from multiple modules
32+
*/
33+
isMosaicId(): boolean;
34+
};

src/model/namespace/AddressAlias.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ export class AddressAlias extends Alias {
4343
* @return boolean
4444
*/
4545
public equals(alias: any): boolean {
46-
if (alias instanceof AddressAlias) {
47-
return this.address.equals(alias.address);
48-
}
49-
return false;
46+
return alias && alias.type === this.type && this.address.equals(alias.address);
5047
}
5148

5249
/**

src/model/namespace/EmptyAlias.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class EmptyAlias extends Alias {
4343
* @return boolean
4444
*/
4545
public equals(alias: any): boolean {
46-
return alias instanceof EmptyAlias || alias.type === 0;
46+
return alias && alias.type === this.type;
4747
}
4848

4949
/**

src/model/namespace/MosaicAlias.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ export class MosaicAlias extends Alias {
4242
* @return boolean
4343
*/
4444
public equals(alias: any): boolean {
45-
if (alias instanceof MosaicAlias) {
46-
return this.mosaicId.equals(alias.mosaicId);
47-
}
48-
return false;
45+
return alias && alias.type === this.type && this.mosaicId.equals(alias.mosaicId);
4946
}
5047

5148
/**

src/model/namespace/NamespaceId.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ export class NamespaceId {
7676
* @return boolean
7777
*/
7878
public equals(id: any): boolean {
79-
if (id instanceof NamespaceId) {
80-
return this.id.equals(id.id);
81-
}
82-
return false;
79+
return id && this.id.equals(id.id);
8380
}
8481

8582
/**
@@ -114,4 +111,25 @@ export class NamespaceId {
114111
public plain(): string {
115112
return this.toHex();
116113
}
114+
115+
/**
116+
* returns that this instance is an alias.
117+
*/
118+
public isNamespaceId(): boolean {
119+
return true;
120+
}
121+
122+
/**
123+
* returns that the instance is not address
124+
*/
125+
public isAddress(): boolean {
126+
return false;
127+
}
128+
129+
/**
130+
* returns that the instance is not a mosaic id
131+
*/
132+
public isMosaicId(): boolean {
133+
return false;
134+
}
117135
}

src/model/node/NodeVersion.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,10 @@ export class NodeVersion {
102102
public formatted(): string {
103103
const placeholderHex = '00000000';
104104
const hexNodeVersion = (placeholderHex + this.nodeVersion.toString(16)).slice(-8);
105-
const formattedNodeVersion = hexNodeVersion
105+
return hexNodeVersion
106106
.match(/.{1,2}/g)!
107107
.map((hex) => parseInt(hex, 16))
108108
.join('.');
109-
110-
return formattedNodeVersion;
111109
}
112110

113111
/**
@@ -124,10 +122,6 @@ export class NodeVersion {
124122
* @returns {boolean}
125123
*/
126124
public equals(nodeVersion: any): boolean {
127-
if (nodeVersion instanceof NodeVersion) {
128-
return this.nodeVersion === nodeVersion.raw();
129-
}
130-
131-
return false;
125+
return nodeVersion && this.nodeVersion === nodeVersion.nodeVersion;
132126
}
133127
}

src/model/receipt/ArtifactExpiryReceipt.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616

1717
import { MosaicExpiryReceiptBuilder, NamespaceExpiryReceiptBuilder } from 'catbuffer-typescript';
18-
import { MosaicId } from '../mosaic/MosaicId';
19-
import { UnresolvedMosaicId } from '../mosaic/UnresolvedMosaicId';
18+
import { MosaicId, UnresolvedMosaicId } from '../mosaic';
19+
import { NamespaceId } from '../namespace';
2020
import { Receipt } from './Receipt';
2121
import { ReceiptType } from './ReceiptType';
2222
import { ReceiptVersion } from './ReceiptVersion';
@@ -42,17 +42,17 @@ export class ArtifactExpiryReceipt extends Receipt {
4242
* @return {Uint8Array}
4343
*/
4444
public serialize(): Uint8Array {
45-
if (this.artifactId instanceof MosaicId) {
45+
if (!this.artifactId.isNamespaceId()) {
4646
return new MosaicExpiryReceiptBuilder(
4747
ReceiptVersion.ARTIFACT_EXPIRY,
4848
this.type.valueOf(),
49-
this.artifactId.toBuilder(),
49+
(this.artifactId as MosaicId).toBuilder(),
5050
).serialize();
5151
}
5252
return new NamespaceExpiryReceiptBuilder(
5353
ReceiptVersion.ARTIFACT_EXPIRY,
5454
this.type.valueOf(),
55-
this.artifactId.toBuilder(),
55+
(this.artifactId as NamespaceId).toBuilder(),
5656
).serialize();
5757
}
5858
}

0 commit comments

Comments
 (0)