Skip to content

Use RFC 8785 canonical state for cache keys, subscription ids, and stream comparison - #3

Open
prateekbhujel wants to merge 1 commit into
NativePHP:mainfrom
prateekbhujel:fix/rfc8785-canonical-state
Open

prateekbhujel wants to merge 1 commit into
NativePHP:mainfrom
prateekbhujel:fix/rfc8785-canonical-state

Conversation

@prateekbhujel

Copy link
Copy Markdown

Aligns Midgard's client-side state keying and comparison with Asgard protocol §5.1 (RFC 8785 / JCS), matching Native\Asgard\Support\CanonicalState in NativePHP/asgard.

Cause

RegionCache::key() and ServerRegion::subscriptionId() each kept their own canonical() helper using ksort(..., SORT_STRING) and JSON_PRESERVE_ZERO_FRACTION, while RegionStreams::shape() compared subscription states using raw PHP ===.

That diverged from protocol §5.1 in three ways:

  • Equivalent numeric values (1 vs 1.0, -0.0 vs 0) produced different cache keys and subscription ids, and caused RegionStreams::watch() to reopen the SSE stream.
  • Non-BMP Unicode member names sorted by UTF-8 bytes (SORT_STRING) instead of RFC 8785 UTF-16 code units.
  • Reordered state keys passed to RegionStreams::watch() compared unequal under PHP === and reopened the stream unnecessarily.

Changes

  • Add Native\Api\Client\Asgard\CanonicalState implementing RFC 8785 canonical state serialization.
  • Use CanonicalState::encode((object) $state) in RegionCache::key(), ServerRegion::subscriptionId(), and RegionStreams::shape(), removing the duplicated canonical() helpers.
  • Return null from RegionCache::key() when a state has no canonical form (for example non-finite floats or invalid UTF-8) instead of throwing.
  • Add tests/Asgard/CanonicalStateTest.php and §5.1 regression tests in FragmentSourceTest, RegionCacheTest, and RegionStreamsTest.

Testing

  • Ran ./vendor/bin/pest (947 passed, 4,286 assertions).
  • Checked all 9 canonical and refuse vectors in asgard-protocol/conformance/vectors/vectors.json against CanonicalState::encode().
  • Ran ./vendor/bin/pint --test.

…ream comparison

Asgard protocol §5.1 requires both client and server to compare and key
states using RFC 8785 (JCS) canonical state. NativePHP/asgard already
implemented CanonicalState, while Midgard still used ksort(..., SORT_STRING)
with JSON_PRESERVE_ZERO_FRACTION in RegionCache and ServerRegion, and raw
PHP array equality in RegionStreams::shape().

Add CanonicalState to Midgard, wire RegionCache::key(),
ServerRegion::subscriptionId(), and RegionStreams::shape() through it,
remove the duplicated canonical() helpers, and add RFC 8785 tests.
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.

1 participant