altcha-org / altcha-lib

A JavaScript library for creating and verifying ALTCHA challenges.
https://altcha.org
MIT License
13 stars 4 forks source link

solveChallengeWorkers Not Working with React 18.3.1 #4

Open bakdakonusuruz opened 2 weeks ago

bakdakonusuruz commented 2 weeks ago

Hi,

I'm trying to solve 'challenges' using solveChallengeWorkers from altcha-lib within React 18.3.1.

I tried following the documentation with await, but the promise doesn't return anything and just waits.

const solvedChallenge = solveChallengeWorkers(
      'altcha-lib/worker', // Worker script URL or path
      4, // Spawn 4 workers
      payload.challenge,
      payload.salt,
      payload.algorithm,
      payload.maxnumber,
  )

I also tried creating a url for the worker, like bellow, but no luck.

const url = new URL('altcha-lib/worker', import.meta.url)

However, worker prints the following error to the console; Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

Do you guys have a React wrapper or a hook for "altcha-lib"? I found a package called altcha-solver but it cannot find "worker_threads" to import.

Any suggestions? It's probably a skill issue, rather than a bug, but since the library is new, I want to report it.

Thanks in advance. Cheers!

ovx commented 1 week ago

Hi, I'm not sure why it fails to properly detect the mime type, but you can also do this:

import AltchaWorker from 'altcha-lib/worker?worker';

const solvedChallenge = solveChallengeWorkers(
      () => new AltchaWorker(),
      ...

This works for me with Vite+React.

bakdakonusuruz commented 1 week ago

Hi Daniel,

Thanks for your reply. Even though the package.json of the library exports the worker, it doesn't import in either CRA or Vite, since worker.js doesn't seem to have any exports.

I've tried both CRA and Vite with ?worker and without, but I encountered the following errors:

CRA Error: Uncaught Error: Cannot find module 'altcha-lib/worker?worker' Vite Error: [plugin: vite:dep-scan] Missing "./worker?worker" export in "altcha-lib" package

Since you were able to run it with React + Vite, could you provide a bit more detail about your setup?

Thanks.

ovx commented 1 week ago

Hi, I just created a new project using:

npm create vite@latest altcha-react-test -- --template react-ts

without any additional configuration or changes.

Importing the worker in the App.tsx is working just fine with ?worker (vite docs):

import AltchaWorker from 'altcha-lib/worker?worker';