feat: add router cycle detection to prevent infinite loops by aprici7y · Pull Request #183 · pillarjs/router

@aprici7y

Router cycles cause silent runtime hangs when a router is mounted
on itself (directly or indirectly). This change detects such cycles
and emits a warning to help developers identify the issue quickly.

The implementation:
- Detects cycles in Router.prototype.use before mounting
- Uses recursive traversal with visited set to avoid infinite loops
- Detects both direct self-mounts and indirect cycles
- Emits process warning but keeps behavior unchanged (warn-only)
- Can be turned into an error in future major version

Tests added for:
- Direct self-loop (router.use(router))
- Indirect cycle (routerA -> routerB -> routerA)
- Nested indirect cycles (routerA -> routerB -> routerC -> routerA)
- Ensuring non-cyclic mounts don't trigger warnings
- Verifying behavior remains unchanged (mount succeeds with warning)

Related to expressjs/express#6809