From: Chris Duncan Date: Thu, 19 Jun 2025 07:28:27 +0000 (-0700) Subject: Substitute regular expressions in tool scripts with utility functions. X-Git-Tag: v5.1.0~4^2~1 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=963d59ff0acb7cdafde776a16dc6af22e77eaf16;p=nano-pow.git Substitute regular expressions in tool scripts with utility functions. --- diff --git a/src/bin/cli.ts b/src/bin/cli.ts index c6c17fd..2157c24 100755 --- a/src/bin/cli.ts +++ b/src/bin/cli.ts @@ -5,7 +5,7 @@ import { Serializable, spawn } from 'node:child_process' import { getRandomValues } from 'node:crypto' import { createInterface } from 'node:readline/promises' -import { stats, Logger } from '#utils' +import { isHex8, isHex32, Logger, stats } from '#utils' import { WorkErrorResponse, WorkGenerateResponse, WorkValidateResponse } from '#types' process.title = 'NanoPow CLI' @@ -25,7 +25,7 @@ if (!process.stdin.isTTY) { let i = 0 for await (const line of stdin) { i++ - if (/^[A-Fa-f\d]{64}$/.test(line)) { + if (isHex32(line)) { hashes.push(line) } else { stdinErrors.push(`Skipping invalid stdin input line ${i}`) @@ -65,7 +65,7 @@ Full documentation: const inArgs: string[] = [] let isParsingHash: boolean = true while (isParsingHash) { - if (!/^[A-Fa-f\d]{16}$/.test(args[args.length - 1])) break + if (!isHex8(args[args.length - 1])) break try { inArgs.unshift(args.pop() as string) } catch { @@ -110,7 +110,7 @@ for (let i = 0; i < args.length; i++) { case ('-d'): { const d = args[i + 1] if (d == null) throw new Error('Missing argument for difficulty') - if (!/^[A-Fa-f\d]{16}$/.test(d)) throw new Error('Invalid argument for difficulty') + if (!isHex8(d)) throw new Error('Invalid argument for difficulty') body.difficulty = d break } @@ -134,7 +134,7 @@ for (let i = 0; i < args.length; i++) { case ('-v'): { const v = args[i + 1] if (v == null) throw new Error('Missing argument for work validation') - if (!/^[A-Fa-f\d]{16}$/.test(v)) throw new Error('Invalid argument for work validation') + if (!isHex8(v)) throw new Error('Invalid argument for work validation') if (hashes.length !== 1) throw new Error('Validate accepts exactly one hash') body.action = 'work_validate' body.work = v diff --git a/src/bin/server.ts b/src/bin/server.ts index c5c8193..a31f58c 100755 --- a/src/bin/server.ts +++ b/src/bin/server.ts @@ -10,7 +10,7 @@ import { homedir } from 'node:os' import { join } from 'node:path' import { launch } from 'puppeteer' import { WorkErrorResponse, WorkGenerateResponse, WorkValidateResponse } from '#types' -import { Logger } from '#utils' +import { isHex8, isHex32, Logger } from '#utils' /** * Used to define NanoPow server configuration. @@ -168,19 +168,16 @@ async function work (data: unknown): Promise { } resBody = `${action} failed` - if (typeof hash !== 'string' || !/[A-Fa-f\d]{64}/.test(hash)) { + if (!isHex32(hash)) { LOG: logger.log('Hash must be hex char(64)') throw new Error(resBody) } - if (difficulty !== undefined - && (typeof difficulty !== 'string' - || !/^[A-Fa-f\d]{16}$/.test(difficulty)) - ) { + if (difficulty !== undefined && !isHex8(difficulty)) { LOG: logger.log('Difficulty must be hex char(16).') throw new Error(resBody) } if (action === 'work_validate') { - if (typeof work !== 'string' || !/^[A-Fa-f\d]{16}$/.test(work)) { + if (!isHex8(work)) { LOG: logger.log('Work must be hex char(16).') throw new Error(resBody) }