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-238 → lib/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.
Describe the issue
Refactor proposal (no behavior change): extract the
Http2Sessionsclass fromlib/adapters/http.jsinto 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.Http2Sessionsis the one piece that genuinely doesn't belong inline:lib/adapters/http.js:137-238.http2andutil— no axios-specific imports, no closure over per-request state.http2Sessionssingleton atlib/adapters/http.js:240can stay inhttp.jsas a one-line import.AGENTS.mdguidance thatlib/helpers/should hold pieces that are generic and reusable outside axios.Http2Sessionsqualifies; nothing in it knows about axios.Proposed move:
lib/adapters/http.js:137-238→lib/helpers/Http2Sessions.js.Explicitly out of scope (and not recommended in this issue):
setProxy— already a top-level function atlib/adapters/http.js:268and exported as__setProxyfor tests; nothing to do.data:URI handler block atlib/adapters/http.js:651-705— depends onAxiosError,AxiosHeaders,settle, and the surrounding promise'sresolve/reject. Not generic, doesn't fitlib/helpers/, and the path is already covered end-to-end so extraction wouldn't add test value.lib/adapters/http.js:1007-1050) — tightly coupled tostreams/res.headers/method/res.statusCodeinside the response closure; extracting forces threading multiple parameters for marginal gain.enforceMaxContentLengthasync generator atlib/adapters/http.js:1059— has two distinct shapes (async generator forresponseType === '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.jsimportsHttp2Sessionsfromlib/helpers/Http2Sessions.js; thehttp2Sessionssingleton stays inhttp.js.tests/unit/helpers/cover session reuse,sessionTimeoutbehavior, and theremoveSessioncleanup path directly.npm run test:vitest:unit,npm run test:vitest:browser:headless, smoke and module suites.Actual behavior
Http2Sessionsis currently inlined inlib/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
v1.x)AGENTS.mdguidance thatlib/helpers/should hold generic, reusable pieces;Http2Sessionsfits that boundary.