Skip to content

Commit

Permalink
feat(cli): use hpagent (#2513)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Jul 25, 2023
1 parent ed6b65c commit 9b2d347
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 282 deletions.
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
"@stoplight/types": "^13.6.0",
"chalk": "4.1.2",
"fast-glob": "~3.2.12",
"hpagent": "~1.2.0",
"lodash": "~4.17.21",
"pony-cause": "^1.0.0",
"proxy-agent": "5.0.0",
"stacktracey": "^2.1.7",
"tslib": "^2.3.0",
"yargs": "17.3.1"
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import * as yargs from 'yargs';

import { DEFAULT_REQUEST_OPTIONS } from '@stoplight/spectral-runtime';
import lintCommand from './commands/lint';
import type * as Agent from 'proxy-agent';

if (typeof process.env.PROXY === 'string') {
const { protocol } = new URL(process.env.PROXY);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const ProxyAgent = require('proxy-agent') as typeof Agent['default'];
DEFAULT_REQUEST_OPTIONS.agent = new ProxyAgent(process.env.PROXY);
const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent') as typeof import('hpagent');
DEFAULT_REQUEST_OPTIONS.agent = new (protocol === 'https:' ? HttpsProxyAgent : HttpProxyAgent)({
proxy: process.env.PROXY,
});
}

export default yargs
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/services/linter/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CLIError } from '../../errors';

export async function lint(documents: Array<number | string>, flags: ILintConfig): Promise<IRuleResult[]> {
const spectral = new Spectral({
resolver: getResolver(flags.resolver, process.env.PROXY),
resolver: getResolver(flags.resolver),
});

const ruleset = await getRuleset(flags.ruleset);
Expand Down
8 changes: 1 addition & 7 deletions packages/cli/src/services/linter/utils/getResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createHttpAndFileResolver, Resolver } from '@stoplight/spectral-ref-res
import { isError } from 'lodash';
import { CLIError } from '../../../errors';

export const getResolver = (resolver: Optional<string>, proxy: Optional<string>): Resolver => {
export const getResolver = (resolver: Optional<string>): Resolver => {
if (resolver !== void 0) {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
Expand All @@ -14,12 +14,6 @@ export const getResolver = (resolver: Optional<string>, proxy: Optional<string>)
}
}

if (typeof proxy === 'string') {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const ProxyAgent = require('proxy-agent') as typeof import('proxy-agent');
return createHttpAndFileResolver({ agent: new ProxyAgent(process.env.PROXY) });
}

return createHttpAndFileResolver();
};

Expand Down
Loading

0 comments on commit 9b2d347

Please sign in to comment.