]> git.codecow.com Git - nano-pow.git/commitdiff
Substitute regular expressions in tool scripts with utility functions.
authorChris Duncan <chris@zoso.dev>
Thu, 19 Jun 2025 07:28:27 +0000 (00:28 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 19 Jun 2025 07:28:27 +0000 (00:28 -0700)
src/bin/cli.ts
src/bin/server.ts

index c6c17fd61f0570a6661c384382ff9a2c88a3c049..2157c24566c7fa566125623217b829399b32085c 100755 (executable)
@@ -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: <https://www.npmjs.com/package/nano-pow>
 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
index c5c8193373425d53bd7f58bcaa778cbe0ad6cd02..a31f58cb0b40abc0596019627ea1db446bbaea48 100755 (executable)
@@ -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<string> {
        }
        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)
                }