Repository navigation
Extract JSON parsing helper to eliminate json5 dependency - #7165
Conversation
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
Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_019AVQ7zFzj4UEysGQPwi2XK
There was a problem hiding this comment.
All reported issues were addressed across 6 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
There was a problem hiding this comment.
Review completed against the latest diff
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
Merging this PR will improve performance by 5.8%
|
| 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)
Footnotes
-
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. ↩
|
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
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
Type of change
Description
Completes the removal of the
json5dependency 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 importingjson5, causing its bundledcore-js2.6.5 runtime to be embedded in production bundles.Changes:
New helper module (
packages/reflex-base/src/reflex_base/.templates/web/utils/helpers/json.js):parseJson()function that handles Python's bareNaN/Infinity/-InfinitytokensJSON.parse) for well-formed payloadsUpdated state.js:
parseJsonfrom the new helper moduleUpdated upload.js:
json5importparseJsonhelper instead ofJSON5.parse()Comprehensive test coverage (
tests/units/reflex_base/templates/test_json_helper.py):json5Integration test (
tests/integration/test_prod_build_pipeline.py):__core-js_shared__markersCloses
#7163
Testing
json5https://claude.ai/code/session_019AVQ7zFzj4UEysGQPwi2XK