refactor: emulator simplification — shared infra, domain helpers, server efficiency#107
Open
refactor: emulator simplification — shared infra, domain helpers, server efficiency#107
Conversation
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three-phase refactor of the WorkOS emulator to reduce duplication and improve efficiency:
ID_PREFIXESconstant,formatEntity()generic formatter,formatListResponse()wrapper, and typedWorkOSStoreclass replacing raw store access across 31 route filesfindEnvRole()/findOrgRole()helpers (replacing 13 duplicated lookups),registerRoleRoutes()factory (deduplicating env/org role CRUD),evaluateFlags()helper, and adoptCollection.deleteBy()at 9 cascade-delete sitesapp.use()auth registrations with single catch-all, adoptunauthorized()helper, add EventBus event-type index, fix N+1 patterns in org domain lookups and role priority reorder, addStore.deleteDataByPrefix()for temporal data cleanupNet 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 typecheckpassespnpm lintpassespnpm testpasses (1509/1509 tests)pnpm buildpasses