Skip to content

Extract JSON parsing helper to eliminate json5 dependency - #7165

Merged
FarhanAliRaza merged 5 commits into
mainfrom
claude/practical-fermat-xjyfb1
Sep 16, 2026
Merged

FarhanAliRaza merged 5 commits into
mainfrom
claude/practical-fermat-xjyfb1

Conversation

@masenf

@masenf masenf commented Sep 16, 2026 •

Copy link
Copy Markdown
Collaborator

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Description

Completes the removal of the json5 dependency from the frontend by extracting the non-finite float parsing logic into a dedicated helper module. This fixes a regression where the upload helper was still importing json5, causing its bundled core-js 2.6.5 runtime to be embedded in production bundles.

Changes:

  1. New helper module (packages/reflex-base/src/reflex_base/.templates/web/utils/helpers/json.js):

    • Exports parseJson() function that handles Python's bare NaN/Infinity/-Infinity tokens
    • Uses a fast path (native JSON.parse) for well-formed payloads
    • Falls back to token rewriting only when parsing fails
    • Includes comprehensive documentation
  2. Updated state.js:

    • Removed inline non-finite float parsing logic (moved to helper)
    • Imports and uses parseJson from the new helper module
    • Simplified socket decoder error handling
  3. Updated upload.js:

    • Removed json5 import
    • Uses parseJson helper instead of JSON5.parse()
  4. Comprehensive test coverage (tests/units/reflex_base/templates/test_json_helper.py):

    • Regression test ensuring no templates import json5
    • Tests for parsing Python's non-finite float tokens
    • Tests for plain JSON round-tripping
    • Tests for string literal preservation
    • Tests for error handling on unparseable payloads
  5. Integration test (tests/integration/test_prod_build_pipeline.py):

    • Verifies that built bundles do not contain __core-js_shared__ markers
    • Ensures the core-js runtime is not embedded in production assets

Closes

#7163

Testing

  • Added unit tests covering all parsing scenarios (non-finite floats, plain JSON, string preservation, error cases)
  • Added regression test to verify no templates import json5
  • Added integration test to verify production bundles don't embed core-js
  • All tests pass with Node.js available (skipped gracefully if missing)

https://claude.ai/code/session_019AVQ7zFzj4UEysGQPwi2XK

Review in cubic

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
@masenf
masenf requested a review from a team as a code owner September 16, 2026 17:38

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 6 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed against the latest diff

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@codspeed

codspeed Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 5.8%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 51 untouched benchmarks
⏩ 8 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_collect_imports[_stateful_page] 1.1 ms 1 ms +5.8%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing claude/practical-fermat-xjyfb1 (8ae14cf) with main (539b209)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@greptile-apps

greptile-apps Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge with both previous parser findings resolved and no new actionable defects identified.

Summary

The PR removes the remaining frontend json5 import by centralizing parsing of Python-generated non-finite JSON numbers in a shared helper.

  • Socket and upload response parsing now use the same native JSON-based implementation.
  • The latest changes generate collision-free NaN sentinels in linear time.
  • Unit tests cover non-finite values, malformed payloads, string preservation, and long adversarial underscore runs.
  • The production build test guards against reintroducing the bundled legacy core-js runtime.

Reviews (4) · Last reviewed commit: "perf(upload): derive the NaN sentinel in..."

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
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
FarhanAliRaza
FarhanAliRaza previously approved these changes Sep 16, 2026
@FarhanAliRaza
FarhanAliRaza enabled auto-merge (squash) September 16, 2026 17:56
Lengthening a candidate until the payload no longer contained it
rescanned the payload per attempt, and a run of N underscores contains
a run of every shorter length, so one such string cost a full scan per
underscore. Parsing is synchronous on the socket and upload paths, so a
large state string could stall the UI: a 32k-underscore run took 332ms,
and scaling was quadratic.

Derive a trailing run one longer than the longest in the payload
instead. It cannot appear by construction and one pass finds it, taking
the same case to 1.7ms. The scan is reached only when the default
sentinel collides.

Payloads now reach the test driver through a file, since these exceed
the argv limit.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019AVQ7zFzj4UEysGQPwi2XK
@FarhanAliRaza
FarhanAliRaza merged commit 11628ea into main Sep 16, 2026
122 checks passed
@FarhanAliRaza
FarhanAliRaza deleted the claude/practical-fermat-xjyfb1 branch September 16, 2026 18:13
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