From b708077483359a6b38c103358a27065578f80ca6 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 12 May 2022 15:16:15 +1000 Subject: [PATCH] Remove assert usage --- CHANGELOG.md | 4 +++ package-lock.json | 1 - package.json | 1 - src/lib/discovery/dns.ts | 10 ++----- src/lib/discovery/enrtree.ts | 53 ++++++++++++++++++++---------------- webpack.config.bundle.js | 1 - webpack.config.js | 1 - webpack.config.min.bundle.js | 1 - webpack.config.min.js | 1 - 9 files changed, 37 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ae2d3881f..cdc485cb55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package-lock.json b/package-lock.json index 54cb414cfe..d2eca9d4b0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index c42d2c38e9..f055f2a0c8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/lib/discovery/dns.ts b/src/lib/discovery/dns.ts index 2d9e97067b..44374f8ddd 100644 --- a/src/lib/discovery/dns.ts +++ b/src/lib/discovery/dns.ts @@ -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 diff --git a/src/lib/discovery/enrtree.ts b/src/lib/discovery/enrtree.ts index 4bff987d0d..32b37ccaa9 100644 --- a/src/lib/discovery/enrtree.ts +++ b/src/lib/discovery/enrtree.ts @@ -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(","); } diff --git a/webpack.config.bundle.js b/webpack.config.bundle.js index 9ad2082392..78ad5c0cb0 100644 --- a/webpack.config.bundle.js +++ b/webpack.config.bundle.js @@ -26,7 +26,6 @@ module.exports = { buffer: require.resolve('buffer/'), crypto: false, stream: require.resolve('stream-browserify'), - assert: require.resolve('assert'), }, }, output: { diff --git a/webpack.config.js b/webpack.config.js index 9dc80c76ff..f45e8a483f 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -26,7 +26,6 @@ module.exports = { buffer: require.resolve('buffer/'), crypto: false, stream: require.resolve('stream-browserify'), - assert: require.resolve('assert'), }, }, output: { diff --git a/webpack.config.min.bundle.js b/webpack.config.min.bundle.js index f260cc0805..a2431c172c 100644 --- a/webpack.config.min.bundle.js +++ b/webpack.config.min.bundle.js @@ -26,7 +26,6 @@ module.exports = { buffer: require.resolve('buffer/'), crypto: false, stream: require.resolve('stream-browserify'), - assert: require.resolve('assert'), }, }, output: { diff --git a/webpack.config.min.js b/webpack.config.min.js index 8175c87033..b435bdf0c4 100644 --- a/webpack.config.min.js +++ b/webpack.config.min.js @@ -26,7 +26,6 @@ module.exports = { buffer: require.resolve('buffer/'), crypto: false, stream: require.resolve('stream-browserify'), - assert: require.resolve('assert'), }, }, output: {