Skip to content

Commit

Permalink
chore: calculate user agent in websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Oct 8, 2024
1 parent d243542 commit 5122951
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
27 changes: 26 additions & 1 deletion packages/middleware-user-agent/src/check-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ import type {
AwsSdkCredentialsFeatures,
} from "@aws-sdk/types";
import type { IHttpRequest } from "@smithy/protocol-http";
import type { AwsCredentialIdentityProvider, BuildHandlerArguments, Provider } from "@smithy/types";
import type {
AwsCredentialIdentityProvider,
BuildHandlerArguments,
Provider,
RetryStrategy,
RetryStrategyV2,
} from "@smithy/types";

/**
* @internal
*/
type PreviouslyResolved = Partial<{
credentials?: AwsCredentialIdentityProvider;
accountIdEndpointMode?: Provider<AccountIdEndpointMode>;
retryStrategy?: Provider<RetryStrategy | RetryStrategyV2>;
}>;

/**
Expand All @@ -33,6 +40,24 @@ export async function checkFeatures(
): Promise<void> {
// eslint-disable-next-line
const request = args.request as IHttpRequest;

if (request.headers?.["smithy-protocol"] === "rpc-v2-cbor") {
setFeature(context, "PROTOCOL_RPC_V2_CBOR", "M");
}

if (typeof config.retryStrategy === "function") {
const retryStrategy = await config.retryStrategy();
if (typeof (retryStrategy as RetryStrategyV2).acquireInitialRetryToken === "function") {
if (retryStrategy.constructor?.name?.includes("Adaptive")) {
setFeature(context, "RETRY_MODE_ADAPTIVE", "F");
} else {
setFeature(context, "RETRY_MODE_STANDARD", "E");
}
} else {
setFeature(context, "RETRY_MODE_LEGACY", "D");
}
}

if (typeof config.accountIdEndpointMode === "function") {
const endpointV2 = context.endpointV2;
if (String(endpointV2?.url?.hostname).match(ACCOUNT_ID_ENDPOINT_REGEX)) {
Expand Down
18 changes: 9 additions & 9 deletions packages/middleware-user-agent/src/user-agent-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { getUserAgentPrefix } from "@aws-sdk/util-endpoints";
import { HttpRequest } from "@smithy/protocol-http";
import {
AbsoluteLocation,
FinalizeHandler,
FinalizeHandlerArguments,
FinalizeHandlerOutput,
FinalizeRequestHandlerOptions,
BuildHandler,
BuildHandlerArguments,
BuildHandlerOptions,
BuildHandlerOutput,
HandlerExecutionContext,
MetadataBearer,
Pluggable,
Expand Down Expand Up @@ -41,10 +41,10 @@ import { encodeFeatures } from "./encode-features";
export const userAgentMiddleware =
(options: UserAgentResolvedConfig) =>
<Output extends MetadataBearer>(
next: FinalizeHandler<any, any>,
next: BuildHandler<any, any>,
context: HandlerExecutionContext | AwsHandlerExecutionContext
): FinalizeHandler<any, any> =>
async (args: FinalizeHandlerArguments<any>): Promise<FinalizeHandlerOutput<Output>> => {
): BuildHandler<any, any> =>
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
const { request } = args;
if (!HttpRequest.isInstance(request)) {
return next(args);
Expand Down Expand Up @@ -130,9 +130,9 @@ const escapeUserAgent = (userAgentPair: UserAgentPair): string => {
}, "") as string;
};

export const getUserAgentMiddlewareOptions: FinalizeRequestHandlerOptions & AbsoluteLocation = {
export const getUserAgentMiddlewareOptions: BuildHandlerOptions & AbsoluteLocation = {
name: "getUserAgentMiddleware",
step: "finalizeRequest",
step: "build",
priority: "low",
tags: ["SET_USER_AGENT", "USER_AGENT"],
override: true,
Expand Down

0 comments on commit 5122951

Please sign in to comment.