Skip to content

refactor: drop json5 dep, handle non-finite floats in socket parse fallback - #6339

Merged
masenf merged 2 commits into
reflex-dev:mainfrom
FarhanAliRaza:remove-json5-dep
Apr 30, 2026
Merged

masenf merged 2 commits into
reflex-dev:mainfrom
FarhanAliRaza:remove-json5-dep

Conversation

@FarhanAliRaza

Copy link
Copy Markdown
Contributor

…

Happy path uses native JSON.parse; the catch rewrites Python"s bare Infinity/-Infinity/NaN tokens outside string literals before retrying, so only payloads that actually contain specials pay the extra cost. NaN has no JSON literal and becomes null (matches JSON.stringify).

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

New Feature Submission:

  • Does your submission pass the tests?
  • Have you linted your code locally prior to submission?

Changes To Core Features:

  • Have you successfully ran tests with your changes locally?

claoses #5820

Will also help with the lighthouse score, I think.

…llback

Happy path uses native JSON.parse; the catch rewrites Python"s bare
Infinity/-Infinity/NaN tokens outside string literals before retrying,
so only payloads that actually contain specials pay the extra cost.
NaN has no JSON literal and becomes null (matches JSON.stringify).
@codspeed

codspeed Bot commented Apr 17, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 9 untouched benchmarks


Comparing FarhanAliRaza:remove-json5-dep (24b214c) with main (d6db9c9)

Open in CodSpeed

@greptile-apps

greptile-apps Bot commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR removes the json5 npm dependency and replaces it with native JSON.parse plus a regex-based fallback that rewrites Python's non-standard bare float tokens (Infinity, -Infinity, NaN) before retrying. The happy path pays no extra cost; only payloads that contain specials go through the rewrite step.

  • NaN → null (not NaN) is an intentional behavior change: state values of float('nan') now render as empty strings in the UI rather than "NaN". The test reflects this, but downstream users who relied on the NaN display will see a silent difference with no deprecation notice.

Confidence Score: 5/5

Safe to merge; all findings are P2 style/documentation suggestions with no runtime correctness issues.

The regex implementation correctly handles Python's non-finite float tokens and the string-literal-first alternation prevents false positives inside quoted values. Removing json5 is a clean, well-scoped change. Both open findings are P2: one asks for word-boundary guards that are not required for real payloads, and one notes the intentional NaN→null behavior change lacks a deprecation/changelog entry.

No files require special attention.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/.templates/web/utils/state.js Replaces JSON5.parse with native JSON.parse + a regex fallback for non-finite floats; regex correctly prioritizes string-literal matching to avoid touching values inside quotes.
packages/reflex-base/src/reflex_base/constants/installer.py Removes the json5 npm dependency entry; clean single-line removal with no other side effects.
tests/integration/test_computed_vars.py Updates the special-floats assertion to expect an empty slot for NaN (now null), reflecting the intentional behavior change; all other float expectations are unchanged.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["Socket.io packet received (str)"] --> B["JSON.parse(str)"]
    B -->|success| C["Return parsed object"]
    B -->|throws| D["rewriteBareNonFiniteFloats(str)"]
    D --> E["Regex: match string literals first\n(pass through unchanged)"]
    E --> F["Replace bare Infinity → 1e999\n-Infinity → -1e999\nNaN → null"]
    F --> G["JSON.parse(rewritten str)"]
    G -->|success| H["Return parsed object\n(NaN becomes null, ±Inf preserved)"]
    G -->|throws| I["Return false\n(malformed payload)"]
Loading

Reviews (1): Last reviewed commit: "refactor: drop json5 dep, handle non-fin..." | Re-trigger Greptile

Comment thread tests/integration/test_computed_vars.py Resolved
…o null

Swap bare NaN for a sentinel string before JSON.parse and revive it back
to a real NaN so Python-side float(nan) round-trips to the frontend.
@BABTUNA

BABTUNA commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

Opened a focused follow-up issue and PR for docs/compatibility clarity around non-finite float behavior:\n\n- Issue: #6386\n- Follow-up PR: https://github.com/FarhanAliRaza/reflex/pull/6\n\nThe follow-up adds a user-facing docs note plus explicit compatibility intent comments in the existing special_floats integration test.

@benedikt-bartscher benedikt-bartscher mentioned this pull request Apr 25, 2026
@masenf
masenf merged commit c57fd41 into reflex-dev:main Apr 30, 2026
48 checks passed
@masenf masenf mentioned this pull request May 18, 2026
FarhanAliRaza pushed a commit that referenced this pull request Sep 16, 2026
* fix(upload): drop json5 from the upload helper

PR #6339 removed json5 as a direct frontend dependency and replaced
JSON5.parse in the socket decoder, but utils/helpers/upload.js kept
importing it. json5 still resolved transitively (via Babel), and its
browser entry point (dist/index.js) is a UMD bundle with core-js 2.6.5
embedded, so production builds shipped an obsolete runtime reachable
through globalThis['__core-js_shared__'].

The upload helper parsed chunks with JSON5 because the ndjson stream is
produced by json_dumps, which emits bare NaN/Infinity/-Infinity tokens.
#6339 already solved that for the socket decoder, so extract that logic
into utils/helpers/json.js and use it from both call sites: JSON.parse
on the fast path, with a rewrite-and-reparse fallback only when the
payload carries non-finite floats.

Tested against a real production build: before this change the emitted
bundle contains __core-js_shared__, after it does not.

Closes #7163

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019AVQ7zFzj4UEysGQPwi2XK

* chore: name the news fragment after the PR

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019AVQ7zFzj4UEysGQPwi2XK

* fix(upload): stop reviving user strings that match the NaN sentinel

The NaN fallback swaps bare NaN for a sentinel string and revives that
value back to NaN after parsing, so a payload carrying both a bare NaN
and a genuine string equal to the sentinel lost the string. Pick a
sentinel the payload does not already contain, so only the placeholder
can survive parsing as that exact value.

Also scope the node skip marker to the tests that shell out to node, so
the json5 import guard still runs on a python-only checkout.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019AVQ7zFzj4UEysGQPwi2XK

* fix(upload): only rewrite non-finite tokens in JSON value positions

The rewrite matched bare Infinity/NaN anywhere outside a string literal,
so `{NaN: 1}` was turned into a valid object with a sentinel key instead
of being rejected. Anchor each bare token to a position where JSON
permits a value (document start, or after ':', ',' or '['), leaving a
token anywhere else for JSON.parse to reject. The streaming upload
parser depends on that rejection to tell a partial chunk from a
complete one.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019AVQ7zFzj4UEysGQPwi2XK

---------

Co-authored-by: Claude <[email protected]>
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

Successfully merging this pull request may close these issues.

3 participants