Skip to content

Commit 5122951

Browse files
committed
chore: calculate user agent in websocket
1 parent d243542 commit 5122951

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

packages/middleware-user-agent/src/check-features.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ import type {
66
AwsSdkCredentialsFeatures,
77
} from "@aws-sdk/types";
88
import type { IHttpRequest } from "@smithy/protocol-http";
9-
import type { AwsCredentialIdentityProvider, BuildHandlerArguments, Provider } from "@smithy/types";
9+
import type {
10+
AwsCredentialIdentityProvider,
11+
BuildHandlerArguments,
12+
Provider,
13+
RetryStrategy,
14+
RetryStrategyV2,
15+
} from "@smithy/types";
1016

1117
/**
1218
* @internal
1319
*/
1420
type PreviouslyResolved = Partial<{
1521
credentials?: AwsCredentialIdentityProvider;
1622
accountIdEndpointMode?: Provider<AccountIdEndpointMode>;
23+
retryStrategy?: Provider<RetryStrategy | RetryStrategyV2>;
1724
}>;
1825

1926
/**
@@ -33,6 +40,24 @@ export async function checkFeatures(
3340
): Promise<void> {
3441
// eslint-disable-next-line
3542
const request = args.request as IHttpRequest;
43+
44+
if (request.headers?.["smithy-protocol"] === "rpc-v2-cbor") {
45+
setFeature(context, "PROTOCOL_RPC_V2_CBOR", "M");
46+
}
47+
48+
if (typeof config.retryStrategy === "function") {
49+
const retryStrategy = await config.retryStrategy();
50+
if (typeof (retryStrategy as RetryStrategyV2).acquireInitialRetryToken === "function") {
51+
if (retryStrategy.constructor?.name?.includes("Adaptive")) {
52+
setFeature(context, "RETRY_MODE_ADAPTIVE", "F");
53+
} else {
54+
setFeature(context, "RETRY_MODE_STANDARD", "E");
55+
}
56+
} else {
57+
setFeature(context, "RETRY_MODE_LEGACY", "D");
58+
}
59+
}
60+
3661
if (typeof config.accountIdEndpointMode === "function") {
3762
const endpointV2 = context.endpointV2;
3863
if (String(endpointV2?.url?.hostname).match(ACCOUNT_ID_ENDPOINT_REGEX)) {

packages/middleware-user-agent/src/user-agent-middleware.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { getUserAgentPrefix } from "@aws-sdk/util-endpoints";
33
import { HttpRequest } from "@smithy/protocol-http";
44
import {
55
AbsoluteLocation,
6-
FinalizeHandler,
7-
FinalizeHandlerArguments,
8-
FinalizeHandlerOutput,
9-
FinalizeRequestHandlerOptions,
6+
BuildHandler,
7+
BuildHandlerArguments,
8+
BuildHandlerOptions,
9+
BuildHandlerOutput,
1010
HandlerExecutionContext,
1111
MetadataBearer,
1212
Pluggable,
@@ -41,10 +41,10 @@ import { encodeFeatures } from "./encode-features";
4141
export const userAgentMiddleware =
4242
(options: UserAgentResolvedConfig) =>
4343
<Output extends MetadataBearer>(
44-
next: FinalizeHandler<any, any>,
44+
next: BuildHandler<any, any>,
4545
context: HandlerExecutionContext | AwsHandlerExecutionContext
46-
): FinalizeHandler<any, any> =>
47-
async (args: FinalizeHandlerArguments<any>): Promise<FinalizeHandlerOutput<Output>> => {
46+
): BuildHandler<any, any> =>
47+
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
4848
const { request } = args;
4949
if (!HttpRequest.isInstance(request)) {
5050
return next(args);
@@ -130,9 +130,9 @@ const escapeUserAgent = (userAgentPair: UserAgentPair): string => {
130130
}, "") as string;
131131
};
132132

133-
export const getUserAgentMiddlewareOptions: FinalizeRequestHandlerOptions & AbsoluteLocation = {
133+
export const getUserAgentMiddlewareOptions: BuildHandlerOptions & AbsoluteLocation = {
134134
name: "getUserAgentMiddleware",
135-
step: "finalizeRequest",
135+
step: "build",
136136
priority: "low",
137137
tags: ["SET_USER_AGENT", "USER_AGENT"],
138138
override: true,

0 commit comments

Comments
 (0)