Repository navigation
docs: clarify that origin is optional only for origin-bound dispatchers - #5994
Merged
Merged
Conversation
…hers Fixes nodejs#1011. `Dispatcher.md` documented the `origin` option of `dispatcher.dispatch()` as "(optional)", which reads as "never required". That only holds for a dispatcher already bound to a single origin. `Agent` is not bound to one origin and requires it. Verified against current `main`: new Pool(origin).request({ path, method }) -> 200 new Client(origin).request({ path, method }) -> 200 new Agent().request({ path, method }) -> InvalidArgumentError 'opts.origin must be a non-empty string or URL.' new Agent().request({ origin, path, method }) -> 200 `Client`/`Pool` fill the origin from `this[kUrl].origin` when `kDispatch` builds the Request (lib/dispatcher/client.js), while `Agent` rejects a missing origin in its own `dispatch` (lib/dispatcher/agent.js). `docs/docs/api/Agent.md` already calls `origin` "Required." for `Agent.dispatch()`; this aligns the abstract Dispatcher docs with it. Adds test/node-test/dispatcher-optional-origin.js to pin the distinction, which had no test coverage. Validation: - node --test test/node-test/dispatcher-optional-origin.js 4/4 pass - node --test test/node-test/agent.js 35/35 pass - node --test test/node-test/balanced-pool.js 25/25 pass - eslint test/node-test/dispatcher-optional-origin.js clean Mutation checks, since a passing test is not the same as a protective test: replacing `this[kUrl].origin` with `undefined` in the client's kDispatch Request construction fails 3 of the 4 new tests, and dropping the origin validation in Agent.dispatch fails 1 of 4. Signed-off-by: Attila <128087784+attilayener@users.noreply.github.com>
metcoder95
approved these changes
Oct 19, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5994 +/- ##
=======================================
Coverage 91.54% 91.54%
=======================================
Files 113 113
Lines 43597 43597
=======================================
+ Hits 39910 39912 +2
+ Misses 3687 3685 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merged
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
Dispatcher.mddocumented theoriginoption ofdispatcher.dispatch()as "(optional)", which reads as "never required". That is only true for a dispatcher already bound to a single origin.Agentis not bound to one origin and requires it, so the docs contradicted the runtime.This also answers the question #1011 asked in 2021: are we allowed to specify
originandpathin the options object passed as the second argument torequestand friends? Why or why not?Verified against current
mainI ran each case rather than reasoning from the types:
The two paths differ because
Client/Poolfill the origin fromthis[kUrl].originwhenkDispatchbuilds theRequest, whileAgentrejects a missing origin in its owndispatch:lib/dispatcher/client.js—new Request(this[kUrl].origin, opts, handler)lib/dispatcher/agent.js— throwsInvalidArgumentErrorConsistency check
docs/docs/api/Agent.mdalready documentsoriginas "Required." forAgent.dispatch(), andClient.mdnotesorigin"is omitted because the client is bound to a single origin". This change aligns the abstractDispatcherdocs with both, rather than asserting a rule that the concrete subclasses break.path, by contrast, is documented as required and that is correct —lib/core/request.jsthrowspath must be a string, so I left it alone.Test coverage
test/node-test/dispatcher-optional-origin.jspins the distinction, which previously had no coverage.Validation
Run against Node v24.21.0:
test/andlib/are otherwise untouched — the diff is one docs file plus one new test file.Mutation check
Because a passing test is not the same as a protective test, I checked these tests actually fail when the behaviour breaks:
this[kUrl].origin->undefinedin the client'skDispatchAgent.dispatchOne note on that first mutation: I initially tried removing the origin-filling block in
lib/dispatcher/dispatcher.jsand the tests still passed. That block only runs insidecompose(), for interceptor chains — not on the directdispatchpath the tests exercise. Worth knowing for anyone else reviewing this area.Disclosure
Prepared with AI assistance. Every claim above was executed locally against a real server and a real Node runtime; the mutation checks were run, not assumed.
Fixes #1011