Skip to content

Commit

Permalink
fix (getAgent): add nullish check for uri
Browse files Browse the repository at this point in the history
This modifies the getAgent `uri` parameter type to string or undefined. It then
checks if uri is defined before calling .startsWith().

We need to do this because we can't guarantee that the value is always a string
(due to type assertions) or code calling it without a valid string.
  • Loading branch information
jsjoeio committed Jun 28, 2021
1 parent bf24402 commit ff639b9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/agents.ts
Expand Up @@ -34,10 +34,10 @@ export type HttpAnyAgent = HTTPAgent | HTTPSAgent;
* @returns {HttpAnyAgent|undefined}
*/
export function getAgent(
uri: string,
uri: string | undefined,
reqOpts: Options
): HttpAnyAgent | undefined {
const isHttp = uri.startsWith('http://');
const isHttp = uri?.startsWith('http://');
const proxy =
reqOpts.proxy ||
process.env.HTTP_PROXY ||
Expand Down
5 changes: 5 additions & 0 deletions test/agents.ts
Expand Up @@ -45,6 +45,11 @@ describe('agents', () => {
assert.strictEqual(agent, undefined);
});

it('should return undefined if httpUri is undefined', () => {
const agent = getAgent(undefined, defaultOptions);
assert.strictEqual(agent, undefined);
});

describe('proxy', () => {
const envVars = [
'http_proxy',
Expand Down

0 comments on commit ff639b9

Please sign in to comment.