From: Chris Duncan Date: Sun, 5 Apr 2026 09:33:52 +0000 (-0700) Subject: Refactor build to export utility functions as submodule. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=5bf00b7bf1e7ead59638a72fe111972867efcf6c;p=nano-pow.git Refactor build to export utility functions as submodule. --- diff --git a/esbuild.mjs b/esbuild.mjs index 6b61e33..bf309e3 100644 --- a/esbuild.mjs +++ b/esbuild.mjs @@ -11,6 +11,7 @@ const sharedOptions = { format: 'esm', outdir: 'dist', loader: { + '.d.ts': 'copy', '.wasm': 'binary' }, legalComments: 'inline', @@ -29,7 +30,8 @@ const bundleOptions = { platform: 'browser', target: 'es2022', entryPoints: [ - './src/index.ts' + './src/index.ts', + './src/utils/index.ts' ] } @@ -38,6 +40,7 @@ const cliOptions = { ...sharedOptions, platform: 'node', target: 'node22', + bundle: false, outdir: 'dist/bin', entryPoints: [ './src/bin/cli.ts', diff --git a/package.json b/package.json index de36bf7..186cb06 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,10 @@ ".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" + }, + "./utils": { + "types": "./dist/utils/index.d.ts", + "default": "./dist/utils/index.js" } }, "types": "./dist/index.d.ts" diff --git a/src/bin/cli.ts b/src/bin/cli.ts index 63a3a5f..a1beaf0 100755 --- a/src/bin/cli.ts +++ b/src/bin/cli.ts @@ -3,7 +3,7 @@ //! SPDX-License-Identifier: GPL-3.0-or-later import { WorkErrorResponse, WorkGenerateResponse, WorkValidateResponse } from '#types' -import { isHex32, isHex8, Logger, stats } from '#utils' +import { isHex32, isHex8, Logger, stats } from 'nano-pow/utils' import { Serializable, spawn } from 'node:child_process' import { getRandomValues } from 'node:crypto' import { createInterface } from 'node:readline/promises' diff --git a/src/bin/server.ts b/src/bin/server.ts index 655e23e..2c54c07 100755 --- a/src/bin/server.ts +++ b/src/bin/server.ts @@ -3,7 +3,7 @@ //! SPDX-License-Identifier: GPL-3.0-or-later import { WorkErrorResponse, WorkGenerateResponse, WorkValidateResponse } from '#types' -import { isHex32, isHex8, Logger } from '#utils' +import { isHex32, isHex8, Logger } from 'nano-pow/utils' import { Serializable } from 'node:child_process' import { hash } from 'node:crypto' import { readFile, writeFile } from 'node:fs/promises' diff --git a/src/types.d.ts b/src/index.d.ts similarity index 100% rename from src/types.d.ts rename to src/index.d.ts diff --git a/src/index.ts b/src/index.ts index 3a95d6f..9555e0e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,7 @@ import { generate, validate } from './lib' * @param {number} [options.effort=0x4] - GPU load when generating work. Larger values are not necessarily better since they can quickly overwhelm the GPU. Default: 0x4 * @param {bigint} [options.difficulty=0xfffffff800000000] - Minimum value result of `BLAKE2b(nonce||blockhash)`. Default: 0xFFFFFFF800000000 */ -export async function work_generate (hash: bigint | string, options: NanoPowOptions): Promise { +async function work_generate (hash: bigint | string, options: NanoPowOptions): Promise { return generate(hash, options) } @@ -27,12 +27,10 @@ export async function work_generate (hash: bigint | string, options: NanoPowOpti * @param {boolean} [options.debug=false] - Enables additional debug logging to the console. Default: false * @param {bigint} [options.difficulty=0xfffffff800000000] - Minimum value result of `BLAKE2b(nonce||blockhash)`. Default: 0xFFFFFFF800000000 */ -export async function work_validate (work: bigint | string, hash: bigint | string, options: NanoPowOptions): Promise { +async function work_validate (work: bigint | string, hash: bigint | string, options: NanoPowOptions): Promise { return validate(work, hash, options) } -export const NanoPow = { work_generate, work_validate } +const NanoPow = { work_generate, work_validate } -export default NanoPow - -export { clearCache, stats } from '#utils' +export { NanoPow as default, NanoPow, work_generate, work_validate } diff --git a/test/index.html b/test/index.html index 98bc0c7..882e091 100644 --- a/test/index.html +++ b/test/index.html @@ -11,15 +11,31 @@ SPDX-License-Identifier: GPL-3.0-or-later try { let NanoPow, stats try { - ({ NanoPow, stats } = await import('../dist/main.min.js')) + ({ NanoPow } = await import('../dist/index.js')) } catch (err) { console.warn(err) try { - ({ NanoPow, stats } = await import('https://unpkg.com/nano-pow@5.1/dist/main.min.js')) + ({ NanoPow, stats } = await import('https://unpkg.com/nano-pow@5.1/dist/index.js')) } catch (err) { console.warn(err) try { - ({ NanoPow, stats } = await import('https://cdn.jsdelivr.net/npm/nano-pow@5.1/dist/main.min.js')) + ({ NanoPow, stats } = await import('https://cdn.jsdelivr.net/npm/nano-pow@5.1/dist/index.js')) + } catch (err) { + throw new Error(`Failed to load NanoPow ${err}`) + } + } + } + + try { + ({ stats } = await import('../dist/utils/index.js')) + } catch (err) { + console.warn(err) + try { + ({ NanoPow, stats } = await import('https://unpkg.com/nano-pow@5.1/dist/utils/index.js')) + } catch (err) { + console.warn(err) + try { + ({ NanoPow, stats } = await import('https://cdn.jsdelivr.net/npm/nano-pow@5.1/dist/utils/index.js')) } catch (err) { throw new Error(`Failed to load NanoPow ${err}`) } diff --git a/tsconfig.json b/tsconfig.json index 70c3033..a7f60a7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ "moduleResolution": "bundler", "declaration": true, "emitDeclarationOnly": true, - "declarationDir": "./types", + "declarationDir": "./dist", "alwaysStrict": true, "downlevelIteration": false, "esModuleInterop": true, @@ -19,7 +19,7 @@ "./src/lib/*" ], "#types": [ - "./src/types.d.ts" + "./src/index.d.ts" ], "#utils": [ "./src/utils/index.ts"