From 866c8276e541071b36919586952323ea02c31f21 Mon Sep 17 00:00:00 2001 From: Caleb Kniffen Date: Wed, 26 Jul 2023 18:08:24 -0500 Subject: [PATCH] feat(deps): remove `assert-browserify` (#2389) Use if statements and manually error throwing to save 20kb gzipped. BREAKING CHANGE: If you were catching AssertionError you need to change to Error. --- UNIQUE_SETUPS.md | 6 ++-- packages/ripple-address-codec/src/index.ts | 13 ++++---- packages/ripple-binary-codec/package.json | 4 ++- packages/ripple-binary-codec/src/index.ts | 33 ++++++++++++++----- .../ripple-binary-codec/src/ledger-hashes.ts | 17 +++++++--- .../src/serdes/binary-parser.ts | 17 +++++++--- .../src/serdes/binary-serializer.ts | 6 ++-- packages/ripple-binary-codec/src/shamap.ts | 5 +-- packages/ripple-keypairs/src/index.ts | 12 +++---- packages/ripple-keypairs/src/utils.ts | 5 +-- packages/xrpl/package.json | 1 - packages/xrpl/src/client/index.ts | 5 +-- packages/xrpl/test/webpack.config.js | 2 -- packages/xrpl/webpack.config.js | 1 - 14 files changed, 77 insertions(+), 50 deletions(-) diff --git a/UNIQUE_SETUPS.md b/UNIQUE_SETUPS.md index f383f925c0..ee74c277f5 100644 --- a/UNIQUE_SETUPS.md +++ b/UNIQUE_SETUPS.md @@ -19,7 +19,6 @@ To use `xrpl.js` with React, you need to install shims for core NodeJS modules. ```shell npm install --save-dev \ - assert \ buffer \ crypto-browserify \ process \ @@ -42,7 +41,6 @@ To use `xrpl.js` with React, you need to install shims for core NodeJS modules. module.exports = function override(config) { const fallback = config.resolve.fallback || {}; Object.assign(fallback, { - assert: require.resolve("assert"), crypto: require.resolve("crypto-browserify"), stream: require.resolve("stream-browserify"), ws: require.resolve("xrpl/dist/npm/client/WSWrapper"), @@ -119,7 +117,7 @@ Similar to above, to get xrpl.js to work with Vite you need to set up a couple a 2. Copy these settings into your `vite.config.ts` file. -``` +```javascript import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill"; @@ -163,7 +161,7 @@ resolve: { 3. Install the config dependencies and xrpl (e.g. using this command) -``` +```shell npm install --save-dev @esbuild-plugins/node-globals-polyfill \ rollup-plugin-polyfill-node \ && npm install diff --git a/packages/ripple-address-codec/src/index.ts b/packages/ripple-address-codec/src/index.ts index 2dced14132..d415848a8e 100644 --- a/packages/ripple-address-codec/src/index.ts +++ b/packages/ripple-address-codec/src/index.ts @@ -1,5 +1,3 @@ -import * as assert from 'assert' - import { codec, encodeSeed, @@ -133,11 +131,12 @@ function tagFromBuffer(buf: Buffer): number | false { // Little-endian to big-endian return buf[23] + buf[24] * 0x100 + buf[25] * 0x10000 + buf[26] * 0x1000000 } - assert.strictEqual(flag, 0, 'flag must be zero to indicate no tag') - assert.ok( - Buffer.from('0000000000000000', 'hex').equals(buf.slice(23, 23 + 8)), - 'remaining bytes must be zero', - ) + if (flag !== 0) { + throw new Error('flag must be zero to indicate no tag') + } + if (!Buffer.from('0000000000000000', 'hex').equals(buf.slice(23, 23 + 8))) { + throw new Error('remaining bytes must be zero') + } return false } diff --git a/packages/ripple-binary-codec/package.json b/packages/ripple-binary-codec/package.json index d31945c7c7..d2679ff33e 100644 --- a/packages/ripple-binary-codec/package.json +++ b/packages/ripple-binary-codec/package.json @@ -11,11 +11,13 @@ "test": "test" }, "dependencies": { - "assert": "^2.0.0", "buffer": "6.0.3", "create-hash": "^1.2.0", "ripple-address-codec": "^4.3.1" }, + "devDependencies": { + "assert": "^2.0.0" + }, "scripts": { "build": "tsc -b && copyfiles ./src/enums/definitions.json ./dist/enums/", "clean": "rm -rf ./dist && rm -rf tsconfig.tsbuildinfo", diff --git a/packages/ripple-binary-codec/src/index.ts b/packages/ripple-binary-codec/src/index.ts index 6d7852d240..c7d4624e3b 100644 --- a/packages/ripple-binary-codec/src/index.ts +++ b/packages/ripple-binary-codec/src/index.ts @@ -1,4 +1,3 @@ -import * as assert from 'assert' import { quality, binary, HashPrefix } from './coretypes' import { decodeLedgerData } from './ledger-hashes' import { ClaimObject } from './binary' @@ -27,7 +26,9 @@ const { * @returns the JSON representation of the transaction */ function decode(binary: string, definitions?: XrplDefinitionsBase): JsonObject { - assert.ok(typeof binary === 'string', 'binary must be a hex string') + if (typeof binary !== 'string') { + throw new Error('binary must be a hex string') + } return binaryToJSON(binary, definitions) } @@ -40,7 +41,9 @@ function decode(binary: string, definitions?: XrplDefinitionsBase): JsonObject { * @returns A hex-string of the encoded transaction */ function encode(json: object, definitions?: XrplDefinitionsBase): string { - assert.ok(typeof json === 'object') + if (typeof json !== 'object') { + throw new Error() + } return serializeObject(json as JsonObject, { definitions }) .toString('hex') .toUpperCase() @@ -58,7 +61,9 @@ function encodeForSigning( json: object, definitions?: XrplDefinitionsBase, ): string { - assert.ok(typeof json === 'object') + if (typeof json !== 'object') { + throw new Error() + } return signingData(json as JsonObject, HashPrefix.transactionSig, { definitions, }) @@ -75,7 +80,9 @@ function encodeForSigning( * @returns a hex string of the encoded transaction */ function encodeForSigningClaim(json: object): string { - assert.ok(typeof json === 'object') + if (typeof json !== 'object') { + throw new Error() + } return signingClaimData(json as ClaimObject) .toString('hex') .toUpperCase() @@ -94,8 +101,12 @@ function encodeForMultisigning( signer: string, definitions?: XrplDefinitionsBase, ): string { - assert.ok(typeof json === 'object') - assert.equal(json['SigningPubKey'], '') + if (typeof json !== 'object') { + throw new Error() + } + if (json['SigningPubKey'] !== '') { + throw new Error() + } const definitionsOpt = definitions ? { definitions } : undefined return multiSigningData(json as JsonObject, signer, definitionsOpt) .toString('hex') @@ -109,7 +120,9 @@ function encodeForMultisigning( * @returns a hex-string representing the quality */ function encodeQuality(value: string): string { - assert.ok(typeof value === 'string') + if (typeof value !== 'string') { + throw new Error() + } return quality.encode(value).toString('hex').toUpperCase() } @@ -120,7 +133,9 @@ function encodeQuality(value: string): string { * @returns a string representing the quality */ function decodeQuality(value: string): string { - assert.ok(typeof value === 'string') + if (typeof value !== 'string') { + throw new Error() + } return quality.decode(value).toString() } diff --git a/packages/ripple-binary-codec/src/ledger-hashes.ts b/packages/ripple-binary-codec/src/ledger-hashes.ts index 5ba28393c3..ce5932d7fd 100644 --- a/packages/ripple-binary-codec/src/ledger-hashes.ts +++ b/packages/ripple-binary-codec/src/ledger-hashes.ts @@ -1,4 +1,3 @@ -import * as assert from 'assert' import { ShaMap, ShaMapNode, ShaMapLeaf } from './shamap' import { HashPrefix } from './hash-prefixes' import { Sha512Half } from './hashes' @@ -45,7 +44,9 @@ interface transactionItemObject extends JsonObject { function transactionItemizer( json: transactionItemObject, ): [Hash256, ShaMapNode, undefined] { - assert.ok(json.hash) + if (!json.hash) { + throw new Error() + } const index = Hash256.from(json.hash) const item = { hashPrefix() { @@ -139,8 +140,12 @@ interface ledgerObject { function ledgerHash(header: ledgerObject): Hash256 { const hash = new Sha512Half() hash.put(HashPrefix.ledgerHeader) - assert.ok(header.parent_close_time !== undefined) - assert.ok(header.close_flags !== undefined) + if ( + header.parent_close_time === undefined || + header.close_flags === undefined + ) { + throw new Error() + } UInt32.from(header.ledger_index).toBytesSink(hash) UInt64.from(BigInt(String(header.total_coins))).toBytesSink(hash) @@ -166,7 +171,9 @@ function decodeLedgerData( binary: string, definitions?: XrplDefinitionsBase, ): object { - assert.ok(typeof binary === 'string', 'binary must be a hex string') + if (typeof binary !== 'string') { + throw new Error('binary must be a hex string') + } const parser = new BinaryParser(binary, definitions) return { ledger_index: parser.readUInt32(), diff --git a/packages/ripple-binary-codec/src/serdes/binary-parser.ts b/packages/ripple-binary-codec/src/serdes/binary-parser.ts index bd6545805e..69fd14c09e 100644 --- a/packages/ripple-binary-codec/src/serdes/binary-parser.ts +++ b/packages/ripple-binary-codec/src/serdes/binary-parser.ts @@ -1,4 +1,3 @@ -import * as assert from 'assert' import { XrplDefinitionsBase, DEFAULT_DEFINITIONS, @@ -35,7 +34,9 @@ class BinaryParser { * @returns The first byte of the BinaryParser */ peek(): number { - assert.ok(this.bytes.byteLength !== 0) + if (this.bytes.byteLength === 0) { + throw new Error() + } return this.bytes[0] } @@ -45,7 +46,9 @@ class BinaryParser { * @param n the number of bytes to skip */ skip(n: number): void { - assert.ok(n <= this.bytes.byteLength) + if (n > this.bytes.byteLength) { + throw new Error() + } this.bytes = this.bytes.slice(n) } @@ -56,7 +59,9 @@ class BinaryParser { * @return The bytes */ read(n: number): Buffer { - assert.ok(n <= this.bytes.byteLength) + if (n > this.bytes.byteLength) { + throw new Error() + } const slice = this.bytes.slice(0, n) this.skip(n) @@ -70,7 +75,9 @@ class BinaryParser { * @return The number represented by those bytes */ readUIntN(n: number): number { - assert.ok(0 < n && n <= 4, 'invalid n') + if (0 >= n && n > 4) { + throw new Error('invalid n') + } return this.read(n).reduce((a, b) => (a << 8) | b) >>> 0 } diff --git a/packages/ripple-binary-codec/src/serdes/binary-serializer.ts b/packages/ripple-binary-codec/src/serdes/binary-serializer.ts index bf060211d1..5ecab893c5 100644 --- a/packages/ripple-binary-codec/src/serdes/binary-serializer.ts +++ b/packages/ripple-binary-codec/src/serdes/binary-serializer.ts @@ -1,4 +1,3 @@ -import * as assert from 'assert' import { FieldInstance } from '../enums' import { type SerializedType } from '../types/serialized-type' import { Buffer } from 'buffer/' @@ -132,8 +131,9 @@ class BinarySerializer { isUnlModifyWorkaround = false, ): void { const associatedValue = field.associatedType.from(value) - assert.ok(associatedValue.toBytesSink !== undefined) - assert.ok(field.name !== undefined) + if (associatedValue.toBytesSink === undefined || field.name === undefined) { + throw new Error() + } this.sink.put(field.header) diff --git a/packages/ripple-binary-codec/src/shamap.ts b/packages/ripple-binary-codec/src/shamap.ts index a2b26bf1e7..f7573ff585 100644 --- a/packages/ripple-binary-codec/src/shamap.ts +++ b/packages/ripple-binary-codec/src/shamap.ts @@ -1,4 +1,3 @@ -import { strict as assert } from 'assert' import { coreTypes } from './types' import { HashPrefix } from './hash-prefixes' import { Sha512Half } from './hashes' @@ -160,7 +159,9 @@ class ShaMapInner extends ShaMapNode { * @param leaf Leaf node to insert when branch doesn't exist */ addItem(index?: Hash256, item?: ShaMapNode, leaf?: ShaMapLeaf): void { - assert.ok(index !== undefined) + if (index === undefined) { + throw new Error() + } if (index !== undefined) { const nibble = index.nibblet(this.depth) const existing = this.branches[nibble] diff --git a/packages/ripple-keypairs/src/index.ts b/packages/ripple-keypairs/src/index.ts index 631e79134d..93da6565b6 100644 --- a/packages/ripple-keypairs/src/index.ts +++ b/packages/ripple-keypairs/src/index.ts @@ -1,4 +1,3 @@ -import * as assert from 'assert' import brorand = require('brorand') import * as hashjs from 'hash.js' import * as elliptic from 'elliptic' @@ -19,10 +18,9 @@ function generateSeed( algorithm?: 'ed25519' | 'ecdsa-secp256k1' } = {}, ): string { - assert.ok( - !options.entropy || options.entropy.length >= 16, - 'entropy too short', - ) + if (!(!options.entropy || options.entropy.length >= 16)) { + throw new Error('entropy too short') + } const entropy = options.entropy ? options.entropy.slice(0, 16) : brorand(16) const type = options.algorithm === 'ed25519' ? 'ed25519' : 'secp256k1' return addressCodec.encodeSeed(Buffer.from(entropy), type) @@ -82,7 +80,9 @@ const ed25519 = { sign(message, privateKey): string { // caution: Ed25519.sign interprets all strings as hex, stripping // any non-hex characters without warning - assert.ok(Array.isArray(message), 'message must be array of octets') + if (!Array.isArray(message)) { + throw new Error('message must be array of octets') + } return bytesToHex( Ed25519.sign(message, hexToBytes(privateKey).slice(1)).toBytes(), ) diff --git a/packages/ripple-keypairs/src/utils.ts b/packages/ripple-keypairs/src/utils.ts index 564c4f65ef..5e066f13e9 100644 --- a/packages/ripple-keypairs/src/utils.ts +++ b/packages/ripple-keypairs/src/utils.ts @@ -1,4 +1,3 @@ -import * as assert from 'assert' import * as hashjs from 'hash.js' import BN = require('bn.js') @@ -10,7 +9,9 @@ function bytesToHex(a: Iterable | ArrayLike): string { } function hexToBytes(a): number[] { - assert.ok(a.length % 2 === 0) + if (a.length % 2 !== 0) { + throw new Error() + } // Special-case length zero to return []. // BN.toArray intentionally returns [0] rather than [] for length zero, // which may make sense for BigNum data, but not for byte strings. diff --git a/packages/xrpl/package.json b/packages/xrpl/package.json index 23c5c88bcc..fe9552188f 100644 --- a/packages/xrpl/package.json +++ b/packages/xrpl/package.json @@ -35,7 +35,6 @@ "devDependencies": { "@geut/browser-node-core": "^2.0.13", "@types/node": "^16.18.38", - "assert-browserify": "^2.0.0", "browserify-fs": "^1.0.0", "constants-browserify": "^1.0.0", "https-browserify": "^1.0.0", diff --git a/packages/xrpl/src/client/index.ts b/packages/xrpl/src/client/index.ts index e13725e052..fa2e5b8dfa 100644 --- a/packages/xrpl/src/client/index.ts +++ b/packages/xrpl/src/client/index.ts @@ -1,7 +1,6 @@ /* eslint-disable jsdoc/require-jsdoc -- Request has many aliases, but they don't need unique docs */ /* eslint-disable @typescript-eslint/member-ordering -- TODO: remove when instance methods aren't members */ /* eslint-disable max-lines -- Client is a large file w/ lots of imports/exports */ -import * as assert from 'assert' import { EventEmitter } from 'events' import { NotFoundError, ValidationError, XrplError } from '../errors' @@ -157,7 +156,9 @@ function getCollectKeyFromCommand(command: string): string | null { } function clamp(value: number, min: number, max: number): number { - assert.ok(min <= max, 'Illegal clamp bounds') + if (min > max) { + throw new Error('Illegal clamp bounds') + } return Math.min(Math.max(value, min), max) } diff --git a/packages/xrpl/test/webpack.config.js b/packages/xrpl/test/webpack.config.js index 07b2a9d28e..66c2d488e2 100644 --- a/packages/xrpl/test/webpack.config.js +++ b/packages/xrpl/test/webpack.config.js @@ -99,11 +99,9 @@ function webpackForTest(testFileName) { extensions: ['.ts', '.js', '.json'], fallback: { module: false, - assert: require.resolve('assert-browserify'), constants: require.resolve('constants-browserify'), fs: require.resolve('browserify-fs'), buffer: require.resolve('buffer/'), - assert: require.resolve('assert/'), stream: require.resolve('stream-browserify'), crypto: require.resolve('crypto-browserify'), path: require.resolve('path-browserify'), diff --git a/packages/xrpl/webpack.config.js b/packages/xrpl/webpack.config.js index e3cffc2d6c..d858e22aa1 100644 --- a/packages/xrpl/webpack.config.js +++ b/packages/xrpl/webpack.config.js @@ -64,7 +64,6 @@ function getDefaultConfiguration() { symlinks: false, fallback: { buffer: require.resolve('buffer/'), - assert: require.resolve('assert/'), stream: require.resolve('stream-browserify'), crypto: require.resolve('crypto-browserify'), },