Skip to content

refactor(http): extract Http2Sessions into lib/helpers for direct unit tests #10860

@carladams1299-lab

Description

@carladams1299-lab

Describe the issue

Refactor proposal (no behavior change): extract the Http2Sessions class from lib/adapters/http.js into its own module to enable direct unit tests.

The Node adapter is 1,312 lines. Most of that is essential — closures over per-request state (req, isDone, rejected, connectPhaseTimer, abortEmitter, boundSockets, etc.) — and shouldn't be split. Http2Sessions is the one piece that genuinely doesn't belong inline:

  • It's already a self-contained class at lib/adapters/http.js:137-238.
  • Only depends on http2 and util — no axios-specific imports, no closure over per-request state.
  • The module-level http2Sessions singleton at lib/adapters/http.js:240 can stay in http.js as a one-line import.
  • Currently has no direct unit tests — coverage is end-to-end through the adapter, which makes the session-pooling, timeout, and cleanup logic awkward to exercise. Extracting gives a real test seam.
  • Fits AGENTS.md guidance that lib/helpers/ should hold pieces that are generic and reusable outside axios. Http2Sessions qualifies; nothing in it knows about axios.

Proposed move: lib/adapters/http.js:137-238lib/helpers/Http2Sessions.js.

Explicitly out of scope (and not recommended in this issue):

  • setProxy — already a top-level function at lib/adapters/http.js:268 and exported as __setProxy for tests; nothing to do.
  • The data: URI handler block at lib/adapters/http.js:651-705 — depends on AxiosError, AxiosHeaders, settle, and the surrounding promise's resolve/reject. Not generic, doesn't fit lib/helpers/, and the path is already covered end-to-end so extraction wouldn't add test value.
  • The decompression block inside the response handler (around lib/adapters/http.js:1007-1050) — tightly coupled to streams/res.headers/method/res.statusCode inside the response closure; extracting forces threading multiple parameters for marginal gain.
  • The inline enforceMaxContentLength async generator at lib/adapters/http.js:1059 — has two distinct shapes (async generator for responseType === 'stream', event listeners for buffered) that don't collapse into a shared helper without contortion.

Per AGENTS.md, the adapter is security-sensitive, so this proposal stays narrow on purpose: one block that moves with no parameter threading, no behavior change, and a concrete test-coverage upside.

Example code

// No example — internal restructuring only. Public API and runtime behavior are unchanged.

Expected behavior

After the refactor:

  • lib/adapters/http.js imports Http2Sessions from lib/helpers/Http2Sessions.js; the http2Sessions singleton stays in http.js.
  • New unit tests in tests/unit/helpers/ cover session reuse, sessionTimeout behavior, and the removeSession cleanup path directly.
  • All existing tests pass unchanged: npm run test:vitest:unit, npm run test:vitest:browser:headless, smoke and module suites.
  • No change to bundle size beyond what the module split implies (Rollup inlines static imports).
  • No change to public API surface, type declarations, or error codes.

Actual behavior

Http2Sessions is currently inlined in lib/adapters/http.js, so its session-pooling and timeout/cleanup logic can only be exercised end-to-end through the adapter. There is no direct unit-test seam for the class.

Environment

  • Axios version: 1.16.0 (current v1.x)
  • Adapter: http (Node)
  • Runtime: N/A — source-level refactor
  • OS: N/A
  • Additional context: aligns with AGENTS.md guidance that lib/helpers/ should hold generic, reusable pieces; Http2Sessions fits that boundary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions