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

JSDOM 23.0.0 atob endless recursion #4602

Closed
6 tasks done
nils4cosee opened this issue Nov 27, 2023 · 6 comments
Closed
6 tasks done

JSDOM 23.0.0 atob endless recursion #4602

nils4cosee opened this issue Nov 27, 2023 · 6 comments
Labels
p4-important Violate documented behavior or significantly improves performance (priority)

Comments

@nils4cosee
Copy link

Describe the bug

We have a test case that uses JSDOM and tests a function that calls "atob".

Since the upgrade to JSDOM 23.0.0, this test hangs.

Reproduction

A very short way to reproduce this is

import {JSDOM} from "jsdom";
import {populateGlobal} from "vitest/environments";

const jsdom = new JSDOM()
populateGlobal(global, jsdom.window)

atob("dGVzdAo=")

It yields the error

[DOMException [InvalidCharacterError]: The string to be decoded contains invalid characters.]

which is a bit misleading, as JSDOM throws this whenever the global "atob" throws any error.

Adding a 'console.log' statement to JSDOM's "atob" implementation shows that "atob" calls itself, resulting in an endless recursion.

System Info

System:
    OS: macOS 13.6
    CPU: (10) arm64 Apple M1 Pro
    Memory: 2.64 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.asdf/installs/nodejs/18.16.0/bin/node
    Yarn: 1.22.19 - ~/.asdf/installs/nodejs/18.16.0/bin/yarn
    npm: 9.5.1 - ~/.asdf/plugins/nodejs/shims/npm
    bun: 0.6.7 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 118.1.59.117
    Chrome: 119.0.6045.159
    Safari: 17.0
  npmPackages:
    @vitest/ui: 0.34.6 => 0.34.6 
    vite: 5.0.2 => 5.0.2 
    vitest: 0.34.6 => 0.34.6

Used Package Manager

npm

Validations

@sheremet-va sheremet-va added bug p4-important Violate documented behavior or significantly improves performance (priority) and removed pending triage labels Nov 27, 2023
@AriPerkkio
Copy link
Member

Work-around for now is to provide atob global in test.setupFiles:

import { atob as NodeAtob } from "buffer";
globalThis.atob = NodeAtob;

Looks like JSDOM now relies on Node's atob: https://github.com/jsdom/jsdom/pull/3625/files#diff-b5cd5c96785357dc930f47c18b45d1626b467e8c16068720a31c0cfc0d8344d3L18

@nils4cosee
Copy link
Author

nils4cosee commented Nov 28, 2023

There is now a related issue at JSDOM: jsdom/jsdom#3628 which mentions the offending MR in JSDOM: jsdom/jsdom#3625

@sheremet-va
Copy link
Member

This is an issue with how Vitest overrides globals, so I would expect it to be fixed on our side. Happy-dom also had a similar problem with setTimeout at one point

@DercilioFontes
Copy link

As a workaround, You can also do this in your setup file.

globalThis.atob = (b64Str: string) => Buffer.from(b64Str, `base64`).toString(`binary`);

Reference

@nils4cosee
Copy link
Author

nils4cosee commented Dec 4, 2023

import { atob as NodeAtob } from "buffer";
globalThis.atob = NodeAtob;

This worked for me, but I had to put the line into a beforeAll block.

@sheremet-va
Copy link
Member

Fixed in da7949d

@github-actions github-actions bot locked and limited conversation to collaborators Dec 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p4-important Violate documented behavior or significantly improves performance (priority)
Projects
None yet
Development

No branches or pull requests

4 participants