Remove assert usage

This commit is contained in:
Franck Royer 2022-05-12 15:16:15 +10:00
parent 34d93a72d1
commit b708077483
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
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]
### Removed
- No more `assert` usage.
## [0.22.0] - 2022-05-10
### Changed

1
package-lock.json generated
View File

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

View File

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

View File

@ -1,5 +1,3 @@
import assert from "assert";
import { debug } from "debug";
import { ENR } from "../enr";
@ -134,11 +132,9 @@ export class DnsNodeDiscovery {
const response = await this.dns.resolveTXT(location);
assert(
response.length,
"Received empty result array while fetching TXT record"
);
assert(response[0].length, "Received empty TXT record");
if (!response.length)
throw new Error("Received empty result array while fetching TXT record");
if (!response[0].length) throw new Error("Received empty TXT record");
// Branch entries can be an array of strings of comma delimited subdomains, with
// 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 base32 from "hi-base32";
import { fromString } from "uint8arrays/from-string";
@ -30,10 +28,10 @@ export class ENRTree {
* the root record signature with its base32 compressed public key.
*/
static parseAndVerifyRoot(root: string, publicKey: string): string {
assert(
root.startsWith(this.ROOT_PREFIX),
`ENRTree root entry must start with '${this.ROOT_PREFIX}'`
);
if (!root.startsWith(this.ROOT_PREFIX))
throw new Error(
`ENRTree root entry must start with '${this.ROOT_PREFIX}'`
);
const rootValues = ENRTree.parseRootValues(root);
const decodedPublicKey = base32.decode.asBytes(publicKey);
@ -60,7 +58,7 @@ export class ENRTree {
isVerified = false;
}
assert(isVerified, "Unable to verify ENRTree root signature");
if (!isVerified) throw new Error("Unable to verify ENRTree root signature");
return rootValues.eRoot;
}
@ -70,15 +68,21 @@ export class ENRTree {
/^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
const [eRoot, lRoot, seq, signature] = matches;
assert.ok(eRoot, "Could not parse 'e' value from ENRTree root entry");
assert.ok(lRoot, "Could not parse 'l' value from ENRTree root entry");
assert.ok(seq, "Could not parse 'seq' value from ENRTree root entry");
assert.ok(signature, "Could not parse 'sig' value from ENRTree root entry");
if (!eRoot)
throw new Error("Could not parse 'e' value from ENRTree root entry");
if (!lRoot)
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 };
}
@ -89,20 +93,23 @@ export class ENRTree {
* and the public key is used to verify the root entry record
*/
static parseTree(tree: string): ENRTreeValues {
assert(
tree.startsWith(this.TREE_PREFIX),
`ENRTree tree entry must start with '${this.TREE_PREFIX}'`
);
if (!tree.startsWith(this.TREE_PREFIX))
throw new Error(
`ENRTree tree entry must start with '${this.TREE_PREFIX}'`
);
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
const [publicKey, domain] = matches;
assert.ok(publicKey, "Could not parse public key from ENRTree tree entry");
assert.ok(domain, "Could not parse domain from ENRTree tree entry");
if (!publicKey)
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 };
}
@ -112,10 +119,10 @@ export class ENRTree {
* either further branch entries or ENR records.
*/
static parseBranch(branch: string): string[] {
assert(
branch.startsWith(this.BRANCH_PREFIX),
`ENRTree branch entry must start with '${this.BRANCH_PREFIX}'`
);
if (!branch.startsWith(this.BRANCH_PREFIX))
throw new Error(
`ENRTree branch entry must start with '${this.BRANCH_PREFIX}'`
);
return branch.split(this.BRANCH_PREFIX)[1].split(",");
}

View File

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

View File

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

View File

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

View File

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