Skip to content

refactor: emulator simplification — shared infra, domain helpers, server efficiency#107

Open
nicknisi wants to merge 4 commits intomainfrom
nicknisi/emulator-simplification
Open

refactor: emulator simplification — shared infra, domain helpers, server efficiency#107
nicknisi wants to merge 4 commits intomainfrom
nicknisi/emulator-simplification

Conversation

@nicknisi
Copy link
Copy Markdown
Member

Summary

Three-phase refactor of the WorkOS emulator to reduce duplication and improve efficiency:

  • Phase 1 — Shared infrastructure: Extract ID_PREFIXES constant, formatEntity() generic formatter, formatListResponse() wrapper, and typed WorkOSStore class replacing raw store access across 31 route files
  • Phase 2 — Domain helpers and route dedup: Extract findEnvRole()/findOrgRole() helpers (replacing 13 duplicated lookups), registerRoleRoutes() factory (deduplicating env/org role CRUD), evaluateFlags() helper, and adopt Collection.deleteBy() at 9 cascade-delete sites
  • Phase 3 — Server infrastructure and efficiency: Replace ~35 app.use() auth registrations with single catch-all, adopt unauthorized() helper, add EventBus event-type index, fix N+1 patterns in org domain lookups and role priority reorder, add Store.deleteDataByPrefix() for temporal data cleanup

Net result: -380 lines, fewer duplicated patterns, better default security (catch-all auth), and reduced N+1 query patterns. All changes are behavioral no-ops — existing test suite passes unchanged (except EventBus unit tests updated for new rebuildIndex() API).

Test plan

  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (1509/1509 tests)
  • pnpm build passes
  • Auth catch-all verified: public endpoints (health, SSO, authorize, authenticate, logout, JWKS) remain accessible without auth; all other endpoints require auth
  • Spec-aware code review passed (cycle 1/3, 0 critical, 0 high findings)

- Cache getWorkOSStore() on Store instance (avoids 37 map lookups per call)
- Use indexed findOneBy() for auth code + SSO code lookup (O(1) vs O(n))
- Hoist authMiddleware to single instance (was creating ~25 closures)
- Add Collection.deleteBy() for cascade delete patterns
- Consistent getWorkOSStore placement (once at setup, not per-handler)
- Remove dead seedDefaults no-op and phase marker comments
- Create constants.ts with typed STORE_KEYS, STORE_KEY_PREFIXES, and EVENTS
- Unify ID_PREFIXES in core/id.ts with workos/store.ts (add 15 missing entries, fix event prefix)
- Optimize Collection.count() to iterate in-place instead of materializing full array
- Move parseListParams to core/pagination.ts, remove redundant limit clamping
- Add generic formatEntity() and formatListResponse() helpers
- Replace 34 trivial formatters with formatEntity() calls (5 with custom exclude sets)
- Update 22 list response sites to use formatListResponse()
- Replace all magic strings with typed constants across 31 files

Review: 1 cycle, 4 findings (0 critical, 0 high, 2 medium, 2 low — all addressed)
…dedup

Extract role lookup helpers (findEnvRole, findOrgRole, requireEnvRole,
requireOrgRole) and role-permission CRUD helpers (getRolePermissions,
replaceRolePermissions) into role-helpers.ts.

Create registerRoleRoutes() factory that deduplicates ~120 lines of
shared CRUD + permissions logic between authorization-roles.ts and
authorization-org-roles.ts. Both files are now thin wrappers.

Adopt Collection.deleteBy() at all 9 cascade delete sites across 5 files.
Add formatFlagTarget() formatter and extract evaluateFlags() to deduplicate
identical flag evaluation logic for orgs and users.

Review: cycle 1/3, PASS. 0 critical, 0 high findings.
… efficiency

- Replace ~35 individual auth middleware registrations with single catch-all
  using PUBLIC_PATHS set and PUBLIC_PATH_PREFIXES array
- Adopt unauthorized() helper in auth middleware (removes 3 inline 401s)
- Remove defensive array spread in cursorPaginate when no filter applied
- Add optional pre-fetched domains to formatOrganization, batch in list endpoint
- Add event-type index to EventBus with rebuildIndex() for pre-filtering
- Fix N+1 in priority reorder endpoint with slug-to-role Map
- Add Store.deleteDataByPrefix() for temporal store data cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant