Skip to content

Commit 9505899

Browse files
[packaging-common] Add ability to pass request timeouts to node-api and typed-rest-client (#398)
* Add ability to pass request timeouts to packaging-common * Fix null dereferencing --------- Co-authored-by: v-levockina <undefined>
1 parent 3fb261f commit 9505899

File tree

7 files changed

+142
-51
lines changed

7 files changed

+142
-51
lines changed

common-npm-packages/packaging-common/locationUtilities.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as interfaces from 'azure-devops-node-api/interfaces/common/VSSInterfac
44
import * as tl from 'azure-pipelines-task-lib/task';
55
import { IRequestOptions } from 'azure-devops-node-api/interfaces/common/VsoBaseInterfaces';
66
import * as provenance from "./provenance";
7+
import { RequestOptions } from './universal/RequestUtilities';
78

89
tl.setResourcePath(path.join(__dirname, 'module.json'), true);
910

@@ -29,13 +30,17 @@ export interface PackagingLocation {
2930
}
3031

3132
// Getting service urls from resource areas api
32-
export async function getServiceUriFromAreaId(serviceUri: string, accessToken: string, areaId: string): Promise<string> {
33+
export async function getServiceUriFromAreaId(
34+
serviceUri: string,
35+
accessToken: string,
36+
areaId: string,
37+
webApiOptions?: RequestOptions): Promise<string> {
3338
const serverType = tl.getVariable('System.ServerType');
3439
if (!serverType || serverType.toLowerCase() !== 'hosted') {
3540
return serviceUri;
3641
}
3742

38-
const webApi = getWebApiWithProxy(serviceUri, accessToken);
43+
const webApi = getWebApiWithProxy(serviceUri, accessToken, webApiOptions);
3944
const locationApi = await webApi.getLocationsApi();
4045

4146
tl.debug(`Getting URI for area ID ${areaId} from ${serviceUri}`);
@@ -70,7 +75,7 @@ export async function getBlobstoreUriFromBaseServiceUri(serviceUri: string, acce
7075
* The second URI, if existent, will be Packaging's default access point
7176
* The remaining URI's will be alternate Packaging's access points
7277
*/
73-
export async function getPackagingUris(protocolType: ProtocolType): Promise<PackagingLocation> {
78+
export async function getPackagingUris(protocolType: ProtocolType, webApiOptions?: RequestOptions): Promise<PackagingLocation> {
7479
tl.debug('Getting Packaging service access points');
7580
const collectionUrl = tl.getVariable('System.TeamFoundationCollectionUri');
7681

@@ -90,7 +95,7 @@ export async function getPackagingUris(protocolType: ProtocolType): Promise<Pack
9095
const serviceUri = await getServiceUriFromAreaId(collectionUrl, accessToken, areaId);
9196
tl.debug(`Found serviceUri: ${serviceUri}`);
9297

93-
const webApi = getWebApiWithProxy(serviceUri);
98+
const webApi = getWebApiWithProxy(serviceUri, accessToken, webApiOptions);
9499
const locationApi = await webApi.getLocationsApi();
95100

96101
tl.debug('Acquiring Packaging endpoints...');
@@ -140,7 +145,7 @@ function getAreaIdForProtocol(protocolType: ProtocolType): string {
140145
}
141146
}
142147

143-
export function getWebApiWithProxy(serviceUri: string, accessToken?: string): vsts.WebApi {
148+
export function getWebApiWithProxy(serviceUri: string, accessToken?: string, webApiOptions?: RequestOptions): vsts.WebApi {
144149
if (!accessToken) {
145150
accessToken = getSystemAccessToken();
146151
}
@@ -149,7 +154,9 @@ export function getWebApiWithProxy(serviceUri: string, accessToken?: string): vs
149154
const options: IRequestOptions = {
150155
proxy: tl.getHttpProxyConfiguration(serviceUri),
151156
allowRetries: true,
152-
maxRetries: 5
157+
maxRetries: 5,
158+
socketTimeout: webApiOptions && webApiOptions.socketTimeout,
159+
globalAgentOptions: webApiOptions && webApiOptions.globalAgentOptions
153160
};
154161
const webApi = new vsts.WebApi(serviceUri, credentialHandler, options);
155162
tl.debug(`Created webApi client for ${serviceUri}; options: ${JSON.stringify(options)}`);
@@ -210,7 +217,8 @@ export async function getFeedRegistryUrl(
210217
feedId: string,
211218
project: string,
212219
accessToken?: string,
213-
useSession?: boolean): Promise<string> {
220+
useSession?: boolean,
221+
webApiOptions?: RequestOptions): Promise<string> {
214222
let loc : RegistryLocation;
215223
switch (registryType) {
216224
case RegistryType.npm:
@@ -253,7 +261,7 @@ export async function getFeedRegistryUrl(
253261

254262
tl.debug("Getting registry url from " + packagingUrl);
255263

256-
const vssConnection = getWebApiWithProxy(packagingUrl, accessToken);
264+
const vssConnection = getWebApiWithProxy(packagingUrl, accessToken, webApiOptions);
257265

258266
let sessionId = feedId;
259267
if (useSession) {

common-npm-packages/packaging-common/npm/npmregistry.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { IHeaders, IRequestOptions } from 'typed-rest-client/Interfaces';
77
import { NormalizeRegistry } from './npmrcparser';
88
import * as util from '../util';
99
import * as locationUtil from '../locationUtilities';
10+
import { RequestOptions } from '../universal/RequestUtilities';
1011

1112
export interface INpmRegistry {
1213
url: string;
@@ -26,7 +27,7 @@ export class NpmRegistry implements INpmRegistry {
2627
}
2728

2829
/** Return NpmRegistry with masked auth from Service Endpoint. */
29-
public static async FromServiceEndpoint(endpointId: string, authOnly?: boolean): Promise<NpmRegistry> {
30+
public static async FromServiceEndpoint(endpointId: string, authOnly?: boolean, requestOptions?: RequestOptions): Promise<NpmRegistry> {
3031
const lineEnd = os.EOL;
3132
let endpointAuth: tl.EndpointAuthorization;
3233
let url: string;
@@ -48,7 +49,7 @@ export class NpmRegistry implements INpmRegistry {
4849

4950
// To the reader, this could be optimized here but it is broken out for readability
5051
if (endpointAuth.scheme === 'Token') {
51-
isVstsTokenAuth = await NpmRegistry.isEndpointInternal(url);
52+
isVstsTokenAuth = await NpmRegistry.isEndpointInternal(url, requestOptions);
5253
}
5354
nerfed = util.toNerfDart(url);
5455
} catch (exception) {
@@ -93,20 +94,27 @@ export class NpmRegistry implements INpmRegistry {
9394

9495
// make a request to the endpoint uri, and take a look at the response header to
9596
// determine whether this is our service, or an external service.
96-
private static async isEndpointInternal(endpointUri: string): Promise<boolean> {
97-
let requestOptions: IRequestOptions;
97+
private static async isEndpointInternal(endpointUri: string, requestOptions?: RequestOptions): Promise<boolean> {
98+
let options: IRequestOptions;
9899
try {
99100
const proxy = tl.getHttpProxyConfiguration();
100-
requestOptions = proxy ? { proxy } : {};
101+
options = proxy ? {
102+
proxy,
103+
socketTimeout: requestOptions && requestOptions.socketTimeout,
104+
globalAgentOptions: requestOptions && requestOptions.globalAgentOptions
105+
} : {};
101106
} catch (error) {
102107
tl.debug('unable to determine proxy configuration: ' + error);
103-
requestOptions = {};
108+
options = {
109+
socketTimeout: requestOptions && requestOptions.socketTimeout,
110+
globalAgentOptions: requestOptions && requestOptions.globalAgentOptions
111+
};
104112
}
105113

106114
const headers: IHeaders = {};
107115
headers['X-TFS-FedAuthRedirect'] = 'Suppress';
108116

109-
const endpointClient = new HttpClient(tl.getVariable('AZURE_HTTP_USER_AGENT'), null, requestOptions);
117+
const endpointClient = new HttpClient(tl.getVariable('AZURE_HTTP_USER_AGENT'), null, options);
110118
try {
111119
const resp = await endpointClient.get(endpointUri, headers);
112120

common-npm-packages/packaging-common/nuget/NuGetToolGetter.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as os from "os";
99
import {VersionInfo} from "../pe-parser/VersionResource";
1010
import * as peParser from "../pe-parser";
1111
import { getVersionFallback } from './ProductVersionHelper';
12+
import { RequestOptions } from '../universal/RequestUtilities';
1213

1314
interface INuGetTools {
1415
nugetexe: INuGetVersionInfo[]
@@ -38,7 +39,11 @@ export const DEFAULT_NUGET_VERSION: string = '4.9.6';
3839
export const DEFAULT_NUGET_PATH_SUFFIX: string = 'NuGet/4.9.6/';
3940
export const NUGET_EXE_TOOL_PATH_ENV_VAR: string = 'NuGetExeToolPath';
4041

41-
export async function getNuGet(versionSpec: string, checkLatest?: boolean, addNuGetToPath?: boolean): Promise<string> {
42+
export async function getNuGet(
43+
versionSpec: string,
44+
checkLatest?: boolean,
45+
addNuGetToPath?: boolean,
46+
requestOptions?: RequestOptions): Promise<string> {
4247
if (toolLib.isExplicitVersion(versionSpec)) {
4348
// Check latest doesn't make sense when explicit version
4449
checkLatest = false;
@@ -64,7 +69,7 @@ export async function getNuGet(versionSpec: string, checkLatest?: boolean, addNu
6469
console.log(taskLib.loc("Info_ResolvedToolFromCache", version));
6570
}
6671
else {
67-
let versionInfo: INuGetVersionInfo = await getLatestMatchVersionInfo(versionSpec);
72+
let versionInfo: INuGetVersionInfo = await getLatestMatchVersionInfo(versionSpec, requestOptions);
6873

6974
// There is a local version which matches the spec yet we found one on dist.nuget.org
7075
// which is different, so we're about to change the version which was used
@@ -234,12 +239,14 @@ function GetRestClientOptions(): restm.IRequestOptions
234239
return options;
235240
}
236241

237-
async function getLatestMatchVersionInfo(versionSpec: string): Promise<INuGetVersionInfo> {
242+
async function getLatestMatchVersionInfo(versionSpec: string, requestOptions?: RequestOptions): Promise<INuGetVersionInfo> {
238243
taskLib.debug('Querying versions list');
239244

240245
let versionsUrl = 'https://dist.nuget.org/tools.json';
241246
let proxyRequestOptions = {
242-
proxy: taskLib.getHttpProxyConfiguration(versionsUrl)
247+
proxy: taskLib.getHttpProxyConfiguration(versionsUrl),
248+
socketTimeout: requestOptions && requestOptions.socketTimeout,
249+
globalAgentOptions: requestOptions && requestOptions.globalAgentOptions
243250
};
244251
let rest: restm.RestClient = new restm.RestClient('vsts-tasks/NuGetToolInstaller', undefined, undefined, proxyRequestOptions);
245252

common-npm-packages/packaging-common/package-lock.json

Lines changed: 71 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)