Skip to content

fix: stop emitting the unused old field on registry tokens - #120

Open
simulieren wants to merge 1 commit into
Julien-R44:mainfrom
simulieren:fix/registry-drop-old-token-field
Open

simulieren wants to merge 1 commit into
Julien-R44:mainfrom
simulieren:fix/registry-drop-old-token-field

Conversation

@simulieren

Copy link
Copy Markdown

Closes #119.

What changes

  • RegistryGenerator#sanitizeTokens keeps type, val and end only. old was matchit's copy of the whole route pattern, repeated once per token, and nothing on the client reads it: createURL uses type/val/end, TuyauRouter matches on pattern.
  • AdonisEndpoint.tokens is typed with a new exported ClientRouteToken, which is ClientRouteMatchItTokens with old optional.
  • Tuyau#createUrlBuilder builds the ClientRouteJSON[] it hands to the http-server URL builder explicitly and asserts the token type, since that builder's type still declares old.
  • Changeset: patch for @tuyau/core.

Backward compatibility

  • A registry generated by an earlier version (tokens with old) still type-checks against the new AdonisEndpoint and still works at runtime, because old is optional and ignored.
  • A registry generated by this version works with the http-server URL builder unchanged; createURL never touched old.
  • The generator and the client ship in the same package, so a user never runs a new registry against an old client. The one visible type change is for code that read token.old off registry.routes[...].tokens, which now sees string | undefined.

Measured

On an AdonisJS app with 1142 named routes, .adonisjs/client/registry/index.ts:

raw gzip
before 681 kB 40 kB
after 419 kB 24 kB

Verification

  • packages/core: 131 tests pass (one added), tsc --noEmit clean, oxlint clean, pnpm build for all packages followed by pnpm typecheck for the whole workspace is clean.
  • generate_registry.spec.ts asserts the emitted token JSON contains no "old".
  • client.spec.ts adds a case that builds /users/42 with urlFor and issues a request through tuyau.api from a registry whose tokens carry no old. The existing fixtures keep old, so the old shape is still exercised too.

Every token in the generated `.adonisjs/client/registry/index.ts` carried
`old`, the whole route pattern repeated once per token, while the client
URL builder (`createURL`) only reads `type`, `val` and `end`. On an app
with ~1100 routes the tokens were two thirds of the generated file
(466 kB of 681 kB) and `old` alone was 262 kB.

The generator now keeps `type`, `val` and `end`. `AdonisEndpoint.tokens`
is typed with a new `ClientRouteToken` where `old` is optional, so a
registry generated by an earlier version still type-checks until it is
regenerated, and the client passes its tokens to the http-server URL
builder with an explicit assertion since that builder's type still
declares `old`.

Tests: the generator spec asserts the emitted token JSON, and a client
spec builds a URL and issues a request from a registry whose tokens have
no `old`.
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.

Generated registry repeats the route pattern in every token's unused old field

1 participant