Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py_mini_racer._objects.JSPromiseError: JavaScript rejected promise with reason: ReferenceError: atob is not defined #73

Open
wwweww opened this issue Jul 14, 2024 · 1 comment

Comments

@wwweww
Copy link

wwweww commented Jul 14, 2024

Steps to reproduce

I have this line of code in my JavaScript " return new Uint8Array(atob(tsv).split("").map(n => n.charCodeAt(0)))
"causing an error
async def get_xcvi():
ctx = MiniRacer()
ctx.eval(js_code)
promise = ctx.eval("new Promise((res, rej) => res(get_xct()))")
res = await promise
print(res)

Expected behavior

Tell us what should happen

Actual behavior

Traceback (most recent call last):
File "J:\code\nbBotTest\nbBotTest\moduleTest\x_spider.py", line 116, in
asyncio.run(get_xcvi())
File "C:\Program Files\Python312\Lib\asyncio\runners.py", line 194, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Program Files\Python312\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python312\Lib\asyncio\base_events.py", line 687, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "J:\code\nbBotTest\nbBotTest\moduleTest\x_spider.py", line 81, in get_xcvi
res = await promise
^^^^^^^^^^^^^
File "J:\code\nbBotTest\nbBotTest.venv\Lib\site-packages\py_mini_racer_objects.py", line 226, in _do_await
return self._unpack_promise_results(results)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "J:\code\nbBotTest\nbBotTest.venv\Lib\site-packages\py_mini_racer_objects.py", line 262, in _unpack_promise_results
raise JSPromiseError(result)
py_mini_racer._objects.JSPromiseError: JavaScript rejected promise with reason: ReferenceError: atob is not defined
at :253:13
at a (:254:9)
at get_xct (:263:21)
at :1:31
at new Promise ()
at :1:1

System configuration

PyMiniRacer 0.12.4:

Python 312:

@bpcreech
Copy link
Owner

bpcreech commented Feb 3, 2025

Hi!

atob is a web Api (defined on the window object), not a JavaScript API, so PyMiniRacer and V8 don't define it.

You could try providing your own atob through wrap_py_function, although it would need to be async (so you'd need an extra await or two in your code).

An alternative would be to contribute it to PyMiniRacer as C++ code. Or, one day, [this proposal](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.frombase64] to add Uint8Array.fromBase64() to JavaScript might eventually land and get picked up by V8!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants