PyMiniRacer icon indicating copy to clipboard operation
PyMiniRacer copied to clipboard

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

Open wwweww opened this issue 1 year ago • 1 comments

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:

wwweww avatar Jul 14 '24 21:07 wwweww

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!

bpcreech avatar Feb 03 '25 03:02 bpcreech