Merge pull request #740 from status-im/assert

Remove assert usage
This commit is contained in:
Franck R 2022-05-13 17:46:33 +10:00 committed by GitHub
commit 9fb9da0823
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 37 additions and 36 deletions

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Removed
- No more `assert` usage.
## [0.22.0] - 2022-05-10 ## [0.22.0] - 2022-05-10
### Changed ### Changed

1
package-lock.json generated
View File

@ -42,7 +42,6 @@
"@typescript-eslint/eslint-plugin": "^5.8.1", "@typescript-eslint/eslint-plugin": "^5.8.1",
"@typescript-eslint/parser": "^5.8.1", "@typescript-eslint/parser": "^5.8.1",
"app-root-path": "^3.0.0", "app-root-path": "^3.0.0",
"assert": "^2.0.0",
"buffer": "^6.0.3", "buffer": "^6.0.3",
"chai": "^4.3.4", "chai": "^4.3.4",
"cspell": "^5.14.0", "cspell": "^5.14.0",

View File

@ -101,7 +101,6 @@
"@typescript-eslint/eslint-plugin": "^5.8.1", "@typescript-eslint/eslint-plugin": "^5.8.1",
"@typescript-eslint/parser": "^5.8.1", "@typescript-eslint/parser": "^5.8.1",
"app-root-path": "^3.0.0", "app-root-path": "^3.0.0",
"assert": "^2.0.0",
"buffer": "^6.0.3", "buffer": "^6.0.3",
"chai": "^4.3.4", "chai": "^4.3.4",
"cspell": "^5.14.0", "cspell": "^5.14.0",

View File

@ -1,5 +1,3 @@
import assert from "assert";
import { debug } from "debug"; import { debug } from "debug";
import { ENR } from "../enr"; import { ENR } from "../enr";
@ -134,11 +132,9 @@ export class DnsNodeDiscovery {
const response = await this.dns.resolveTXT(location); const response = await this.dns.resolveTXT(location);
assert( if (!response.length)
response.length, throw new Error("Received empty result array while fetching TXT record");
"Received empty result array while fetching TXT record" if (!response[0].length) throw new Error("Received empty TXT record");
);
assert(response[0].length, "Received empty TXT record");
// Branch entries can be an array of strings of comma delimited subdomains, with // Branch entries can be an array of strings of comma delimited subdomains, with
// some subdomain strings split across the array elements // some subdomain strings split across the array elements

View File

@ -1,5 +1,3 @@
import assert from "assert";
import * as secp from "@noble/secp256k1"; import * as secp from "@noble/secp256k1";
import * as base32 from "hi-base32"; import * as base32 from "hi-base32";
import { fromString } from "uint8arrays/from-string"; import { fromString } from "uint8arrays/from-string";
@ -30,10 +28,10 @@ export class ENRTree {
* the root record signature with its base32 compressed public key. * the root record signature with its base32 compressed public key.
*/ */
static parseAndVerifyRoot(root: string, publicKey: string): string { static parseAndVerifyRoot(root: string, publicKey: string): string {
assert( if (!root.startsWith(this.ROOT_PREFIX))
root.startsWith(this.ROOT_PREFIX), throw new Error(
`ENRTree root entry must start with '${this.ROOT_PREFIX}'` `ENRTree root entry must start with '${this.ROOT_PREFIX}'`
); );
const rootValues = ENRTree.parseRootValues(root); const rootValues = ENRTree.parseRootValues(root);
const decodedPublicKey = base32.decode.asBytes(publicKey); const decodedPublicKey = base32.decode.asBytes(publicKey);
@ -60,7 +58,7 @@ export class ENRTree {
isVerified = false; isVerified = false;
} }
assert(isVerified, "Unable to verify ENRTree root signature"); if (!isVerified) throw new Error("Unable to verify ENRTree root signature");
return rootValues.eRoot; return rootValues.eRoot;
} }
@ -70,15 +68,21 @@ export class ENRTree {
/^enrtree-root:v1 e=([^ ]+) l=([^ ]+) seq=(\d+) sig=([^ ]+)$/ /^enrtree-root:v1 e=([^ ]+) l=([^ ]+) seq=(\d+) sig=([^ ]+)$/
); );
assert.ok(Array.isArray(matches), "Could not parse ENRTree root entry"); if (!Array.isArray(matches))
throw new Error("Could not parse ENRTree root entry");
matches.shift(); // The first entry is the full match matches.shift(); // The first entry is the full match
const [eRoot, lRoot, seq, signature] = matches; const [eRoot, lRoot, seq, signature] = matches;
assert.ok(eRoot, "Could not parse 'e' value from ENRTree root entry"); if (!eRoot)
assert.ok(lRoot, "Could not parse 'l' value from ENRTree root entry"); throw new Error("Could not parse 'e' value from ENRTree root entry");
assert.ok(seq, "Could not parse 'seq' value from ENRTree root entry"); if (!lRoot)
assert.ok(signature, "Could not parse 'sig' value from ENRTree root entry"); throw new Error("Could not parse 'l' value from ENRTree root entry");
if (!seq)
throw new Error("Could not parse 'seq' value from ENRTree root entry");
if (!signature)
throw new Error("Could not parse 'sig' value from ENRTree root entry");
return { eRoot, lRoot, seq: Number(seq), signature }; return { eRoot, lRoot, seq: Number(seq), signature };
} }
@ -89,20 +93,23 @@ export class ENRTree {
* and the public key is used to verify the root entry record * and the public key is used to verify the root entry record
*/ */
static parseTree(tree: string): ENRTreeValues { static parseTree(tree: string): ENRTreeValues {
assert( if (!tree.startsWith(this.TREE_PREFIX))
tree.startsWith(this.TREE_PREFIX), throw new Error(
`ENRTree tree entry must start with '${this.TREE_PREFIX}'` `ENRTree tree entry must start with '${this.TREE_PREFIX}'`
); );
const matches = tree.match(/^enrtree:\/\/([^@]+)@(.+)$/); const matches = tree.match(/^enrtree:\/\/([^@]+)@(.+)$/);
assert.ok(Array.isArray(matches), "Could not parse ENRTree tree entry"); if (!Array.isArray(matches))
throw new Error("Could not parse ENRTree tree entry");
matches.shift(); // The first entry is the full match matches.shift(); // The first entry is the full match
const [publicKey, domain] = matches; const [publicKey, domain] = matches;
assert.ok(publicKey, "Could not parse public key from ENRTree tree entry"); if (!publicKey)
assert.ok(domain, "Could not parse domain from ENRTree tree entry"); throw new Error("Could not parse public key from ENRTree tree entry");
if (!domain)
throw new Error("Could not parse domain from ENRTree tree entry");
return { publicKey, domain }; return { publicKey, domain };
} }
@ -112,10 +119,10 @@ export class ENRTree {
* either further branch entries or ENR records. * either further branch entries or ENR records.
*/ */
static parseBranch(branch: string): string[] { static parseBranch(branch: string): string[] {
assert( if (!branch.startsWith(this.BRANCH_PREFIX))
branch.startsWith(this.BRANCH_PREFIX), throw new Error(
`ENRTree branch entry must start with '${this.BRANCH_PREFIX}'` `ENRTree branch entry must start with '${this.BRANCH_PREFIX}'`
); );
return branch.split(this.BRANCH_PREFIX)[1].split(","); return branch.split(this.BRANCH_PREFIX)[1].split(",");
} }

View File

@ -26,7 +26,6 @@ module.exports = {
buffer: require.resolve('buffer/'), buffer: require.resolve('buffer/'),
crypto: false, crypto: false,
stream: require.resolve('stream-browserify'), stream: require.resolve('stream-browserify'),
assert: require.resolve('assert'),
}, },
}, },
output: { output: {

View File

@ -26,7 +26,6 @@ module.exports = {
buffer: require.resolve('buffer/'), buffer: require.resolve('buffer/'),
crypto: false, crypto: false,
stream: require.resolve('stream-browserify'), stream: require.resolve('stream-browserify'),
assert: require.resolve('assert'),
}, },
}, },
output: { output: {

View File

@ -26,7 +26,6 @@ module.exports = {
buffer: require.resolve('buffer/'), buffer: require.resolve('buffer/'),
crypto: false, crypto: false,
stream: require.resolve('stream-browserify'), stream: require.resolve('stream-browserify'),
assert: require.resolve('assert'),
}, },
}, },
output: { output: {

View File

@ -26,7 +26,6 @@ module.exports = {
buffer: require.resolve('buffer/'), buffer: require.resolve('buffer/'),
crypto: false, crypto: false,
stream: require.resolve('stream-browserify'), stream: require.resolve('stream-browserify'),
assert: require.resolve('assert'),
}, },
}, },
output: { output: {