Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Add optional agent option to WebResourceLike #32590

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions sdk/core/core-http-compat/review/core-http-compat.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ import type { ProxySettings } from '@azure/core-rest-pipeline';
import { ServiceClient } from '@azure/core-client';
import type { ServiceClientOptions } from '@azure/core-client';

// @public
export interface Agent {
destroy(): void;
maxFreeSockets: number;
maxSockets: number;
requests: unknown;
sockets: unknown;
}

// @public
export interface CompatResponse extends Omit<FullOperationResponse, "request" | "headers"> {
headers: HttpHeadersLike;
Expand Down Expand Up @@ -134,6 +143,7 @@ export type TransferProgressEvent = {
// @public
export interface WebResourceLike {
abortSignal?: AbortSignalLike;
agent?: Agent;
body?: any;
clone(): WebResourceLike;
decompressResponse?: boolean;
Expand Down
1 change: 1 addition & 0 deletions sdk/core/core-http-compat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export { RedirectOptions } from "./policies/redirectOptions.js";
export { disableKeepAlivePolicyName } from "./policies/disableKeepAlivePolicy.js";
export { convertHttpClient } from "./httpClientAdapter.js";
export {
Agent,
WebResourceLike,
HttpHeadersLike,
RawHttpHeaders,
Expand Down
39 changes: 39 additions & 0 deletions sdk/core/core-http-compat/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function toPipelineRequest(
onUploadProgress: webResource.onUploadProgress,
proxySettings: webResource.proxySettings,
streamResponseStatusCodes: webResource.streamResponseStatusCodes,
agent: webResource.agent,
});
if (options.originalRequest) {
(newRequest as PipelineRequestWithOriginal)[originalClientRequestSymbol] =
Expand Down Expand Up @@ -76,6 +77,7 @@ export function toWebResourceLike(
onUploadProgress: request.onUploadProgress,
proxySettings: request.proxySettings,
streamResponseStatusCodes: request.streamResponseStatusCodes,
agent: request.agent,
clone(): WebResourceLike {
throw new Error("Cannot clone a non-proxied WebResourceLike");
},
Expand Down Expand Up @@ -119,6 +121,7 @@ export function toWebResourceLike(
"onUploadProgress",
"proxySettings",
"streamResponseStatusCodes",
"agent",
];

if (typeof prop === "string" && passThroughProps.includes(prop)) {
Expand Down Expand Up @@ -361,6 +364,34 @@ export class HttpHeaders implements HttpHeadersLike {
}
}

/**
* An interface compatible with NodeJS's `http.Agent`.
* We want to avoid publicly re-exporting the actual interface,
* since it might vary across runtime versions.
*/
export interface Agent {
/**
* Destroy any sockets that are currently in use by the agent.
*/
destroy(): void;
/**
* For agents with keepAlive enabled, this sets the maximum number of sockets that will be left open in the free state.
*/
maxFreeSockets: number;
/**
* Determines how many concurrent sockets the agent can have open per origin.
*/
maxSockets: number;
/**
* An object which contains queues of requests that have not yet been assigned to sockets.
*/
requests: unknown;
/**
* An object which contains arrays of sockets currently in use by the agent.
*/
sockets: unknown;
}

/**
* A description of a HTTP request to be made to a remote server.
*/
Expand Down Expand Up @@ -437,6 +468,14 @@ export interface WebResourceLike {
/** Callback which fires upon download progress. */
onDownloadProgress?: (progress: TransferProgressEvent) => void;

/**
* NODEJS ONLY
*
* A Node-only option to provide a custom `http.Agent`/`https.Agent`.
* Does nothing when running in the browser.
*/
agent?: Agent;

/**
* Clone this request object.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ export interface PipelineRequest {
// @public
export interface PipelineRequestOptions {
abortSignal?: AbortSignalLike;
agent?: Agent;
allowInsecureConnection?: boolean;
body?: RequestBodyType;
disableKeepAlive?: boolean;
Expand All @@ -297,6 +298,7 @@ export interface PipelineRequestOptions {
requestId?: string;
streamResponseStatusCodes?: Set<number>;
timeout?: number;
tlsSettings?: TlsSettings;
tracingOptions?: OperationTracingOptions;
url: string;
withCredentials?: boolean;
Expand Down
18 changes: 17 additions & 1 deletion sdk/core/core-rest-pipeline/src/pipelineRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
// Licensed under the MIT License.

import type {
Agent,
FormDataMap,
HttpHeaders,
MultipartRequestBody,
PipelineRequest,
ProxySettings,
RequestBodyType,
TlsSettings,
TransferProgressEvent,
} from "./interfaces.js";
import { createHttpHeaders } from "./httpHeaders.js";
Expand Down Expand Up @@ -74,6 +76,14 @@ export interface PipelineRequestOptions {
*/
streamResponseStatusCodes?: Set<number>;

/**
* NODEJS ONLY
*
* A Node-only option to provide a custom `http.Agent`/`https.Agent`.
* Does nothing when running in the browser.
*/
agent?: Agent;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a specific reason why we don't have agent and tlsSettings in PipelineRequestOptions, or we missed adding them?


/**
* BROWSER ONLY
*
Expand All @@ -85,6 +95,9 @@ export interface PipelineRequestOptions {
*/
enableBrowserStreams?: boolean;

/** Settings for configuring TLS authentication */
tlsSettings?: TlsSettings;

/**
* Proxy configuration.
*/
Expand Down Expand Up @@ -128,7 +141,6 @@ class PipelineRequestImpl implements PipelineRequest {
public formData?: FormDataMap;
public streamResponseStatusCodes?: Set<number>;
public enableBrowserStreams: boolean;

public proxySettings?: ProxySettings;
public disableKeepAlive: boolean;
public abortSignal?: AbortSignalLike;
Expand All @@ -137,6 +149,8 @@ class PipelineRequestImpl implements PipelineRequest {
public allowInsecureConnection?: boolean;
public onUploadProgress?: (progress: TransferProgressEvent) => void;
public onDownloadProgress?: (progress: TransferProgressEvent) => void;
public agent?: Agent;
public tlsSettings?: TlsSettings;

constructor(options: PipelineRequestOptions) {
this.url = options.url;
Expand All @@ -157,6 +171,8 @@ class PipelineRequestImpl implements PipelineRequest {
this.requestId = options.requestId || randomUUID();
this.allowInsecureConnection = options.allowInsecureConnection ?? false;
this.enableBrowserStreams = options.enableBrowserStreams ?? false;
this.agent = options.agent;
this.tlsSettings = options.tlsSettings;
}
}

Expand Down
Loading