Skip to content

fix(fastify): #1293 route (request as any).json()/.body through external-fastify handle dispatch - #1308

Merged
proggeramlug merged 1 commit into
mainfrom
worktree-fix-1293-json-dce
May 22, 2026
Merged

fix(fastify): #1293 route (request as any).json()/.body through external-fastify handle dispatch#1308
proggeramlug merged 1 commit into
mainfrom
worktree-fix-1293-json-dce

Conversation

@proggeramlug

Copy link
Copy Markdown
Contributor

Fixes #1293.

Root cause

The canonical Fastify body-parse pattern casts the receiver:

const body = (request as any).json();
if (!body) return reply.status(400).send({ error: "Invalid body" });

The as any cast erases the static type, so codegen lowers .json() to a generic dynamic dispatch (Call { callee: PropertyGet }) instead of a NativeMethodCall { module: "fastify" } — confirmed via --print-hir. At runtime that lands in perry-stdlib's js_handle_method_dispatch / js_handle_property_dispatch.

Those dispatchers probe only perry-stdlib's own handle registry (with_handle::<crate::fastify::FastifyContext>). But the well-known flip routes fastifyperry-ext-fastify, whose FastifyContext lives in perry-ffi's separate registry (the two are documented as disjoint integer spaces). The probe missed, the call fell through, and:

  • (request as any).json() returned a bare NaN (typeof "number", String(j) = "NaN")
  • (request as any).body returned undefined

…silently 400-ing every POST that used the Fetch-style accessor. The if(!body)-vs-typeof distinction in the report was incidental — the value was wrong in all cases on current main; the typed request.json() (no cast) path from #1240 was the only one that worked.

Fix

Mirrors the established external-net-pump pattern:

  • perry-ext-fastify exports js_ext_fastify_is_context_handle — a membership probe over its own perry-ffi registry.
  • perry-stdlib dispatch grows external-fastify-pump arms (gated not(http-server)) for both method and property dispatch that consult the probe and forward to the linked js_fastify_* exports — full parity with the bundled http-server arms (json, body, method, url, params, headers, send, status, code, header, type / query, rawBody, text, user).

Validation

Exact issue repro now matches the "Expected" output — all three routes return 200 with the parsed body:

=== no-anchor ===   {"where":"no-anchor","body":{"hello":"world"}}
=== with-anchor === {"where":"with-anchor","body":{"hello":"world"}}
=== use-body ===    {"where":"use-body","body":{"hello":"world"}}
  • New wire-level harness test-files/run_test_issue_1293.sh (typed json(), (as any).json(), (as any).body) — PASS
  • Existing run_test_issue_1240.sh (typed path) — PASS (no regression)
  • New perry-ext-fastify unit test for the membership probe — PASS
  • cargo test -p perry-ext-fastify (12) + cargo test -p perry-stdlib (74) — PASS
  • cargo fmt --all -- --check — clean

Note for merge: version bump + CHANGELOG entry intentionally omitted (folded in at merge per repo convention).

…nal-fastify handle dispatch

The `as any` cast in the canonical Fastify body-parse pattern
(`const body = (request as any).json(); if (!body) ...`) erases the
static type, so codegen lowers the access to a *generic* dynamic
dispatch (`Call { callee: PropertyGet }` / `PropertyGet`) instead of a
`NativeMethodCall { module: "fastify" }`. At runtime that lands in
perry-stdlib's `js_handle_method_dispatch` / `js_handle_property_dispatch`.

Those dispatchers probed only perry-stdlib's own handle registry
(`with_handle::<crate::fastify::FastifyContext>`). But the well-known
flip routes `fastify` to perry-ext-fastify, whose FastifyContext lives
in perry-ffi's *separate* registry. The probe missed, the call fell
through, and `(request as any).json()` came back as a bare NaN
(`typeof "number"`) while `(request as any).body` came back `undefined`
— a silent 400 on every POST that used the Fetch-style accessor. The
`if(!body)`-vs-`typeof` distinction in the report was incidental: the
value was wrong in all cases.

Fix mirrors the established `external-net-pump` pattern:
- perry-ext-fastify exports `js_ext_fastify_is_context_handle`, a
  membership probe over its own perry-ffi registry.
- perry-stdlib's method/property dispatch grow `external-fastify-pump`
  arms (gated `not(http-server)`) that consult it and forward to the
  linked `js_fastify_*` exports — full parity with the bundled
  `http-server` arms.

Adds test_issue_1293_fastify_request_json_as_any.ts + run_test_issue_1293.sh
(wire-level: typed json(), (as any).json(), (as any).body all return the
parsed object) and a perry-ext-fastify unit test for the membership probe.
@proggeramlug
proggeramlug merged commit 0845a8c into main May 22, 2026
9 checks passed
@proggeramlug
proggeramlug deleted the worktree-fix-1293-json-dce branch May 22, 2026 05:46
proggeramlug added a commit that referenced this pull request May 22, 2026
…sweep (#1414)

Rolls up 26 PRs that merged to main post-v0.5.1023 without version
bumps:

- node:crypto gap-fixes (#1386 #1393 #1394 #1402 #1405): randomInt,
  timingSafeEqual, getHashes/getCiphers, sha224/sha384, base64 digest,
  Buffer hash input, no-arg digest() → Buffer, pbkdf2Sync digest arg,
  scryptSync.
- node:perf_hooks (#1321 + #1328 #1342 coverage): performance + User
  Timing + PerformanceObserver native impl, granular node-suite +
  edge-case coverage.
- #1090 GC checkpoint runtime work (#1324).
- #1311 geisterhand on iOS (#1316 #1383 #1384 #1385).
- #1312 process.env.X (unset) is nullish undefined (#1314).
- #1319 thread-safety hardening for cross-thread runtime statics.
- #1322 exact-head GC evidence packet.
- #1323 wasm timers dispatch through mem_call bridge (#1329).
- #1317 node:timers/promises shadow-segfault fix (#1326).
- #1330 node:process suite (#1331).
- #1292 bcrypt.hash() returns String (#1307).
- #1293 fastify .json()/.body external-fastify dispatch (#1308).
- #1296 app pattern performance gaps.
- #1297 diagnostics_channel parity.
- #1301 iOS App Groups capability (#1313).
- #1318 #1325 os/methods/modern-methods static dispatch.
- #1315 expanded Node parity test coverage.
- #1382 ui-ios stdlib pump for async fetch.
- #1392 ui-wasm reactive state + setText (#1404).
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.

#1240 follow-up: request.json() result is DCE'd when only used in if(!body) — needs to be 'observed' (e.g. typeof) to survive

1 participant