Skip to content

refactor(state): extract shared ensureClientForBucket / region-corrected S3 client helper (3 copies: S3StateBackend, LockManager, ExportIndexStore) #827

Description

@go-to-k

Summary

The ensureClientForBucket() pattern — resolve the state bucket's region via GetBucketLocation (cached) and rebuild a region-corrected S3 client reusing caller credentials without destroying the shared client — now exists in three near-identical copies:

This is pure duplication and a drift risk (a future fix to one copy can miss the others).

Proposed fix

Extract a shared helper, e.g. in src/utils/aws-region-resolver.ts:

async function rebuildClientForBucketRegion(client: S3Client, bucket: string): Promise<S3Client | null>

that encapsulates the probe (resolve creds + region via the cached resolveBucketRegion), the same-region short-circuit, and the credential-reusing rebuild (no .destroy() of the shared client). Each of the three call sites becomes a one-liner. Keep the per-call memoization (clientResolved flag) local to each store, or fold it into a small mixin/helper.

Context

Flagged as a non-blocking nit by the code reviewer on #819 (PR #824). Low priority, pure refactor, no behavior change — bundle with the next state-layer change or take standalone.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions