@tradedal/aws-client-core 0.0.0-stage → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/awsArn.ts +97 -0
- package/awsClient.ts +564 -0
- package/awsClientConfig.ts +203 -0
- package/awsClientModel.ts +200 -0
- package/awsHttpOperation.ts +153 -0
- package/awsQueryOperation.ts +351 -0
- package/awsRequestSigner.ts +294 -0
- package/awsResponseDecoder.ts +133 -0
- package/awsRetryPolicy.ts +61 -0
- package/awsServiceAddress.ts +407 -0
- package/awsSharedProfiles.ts +189 -0
- package/generated/partitions.ts +148 -0
- package/package.json +33 -3
- package/README.md +0 -3
package/awsArn.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Schema, SchemaTransformation } from "effect";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The arnLabel pattern accepts a partition or service label such as
|
|
5
|
+
* "aws-us-gov" or "sts". A colon in "aws:sts" would end one ARN field and
|
|
6
|
+
* start another, so the pattern rejects it.
|
|
7
|
+
*/
|
|
8
|
+
const arnLabel = /^[a-z0-9][a-z0-9-]*$/;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The accountIdPattern regular expression requires twelve AWS account digits. It
|
|
12
|
+
* accepts "123456789012" and rejects "12345", which cannot name an account.
|
|
13
|
+
*/
|
|
14
|
+
const accountIdPattern = /^\d{12}$/;
|
|
15
|
+
|
|
16
|
+
const label = Schema.String.check(Schema.isPattern(arnLabel));
|
|
17
|
+
|
|
18
|
+
/** The AwsAccountId schema distinguishes the AWS account number from a role or session ID. */
|
|
19
|
+
export const AwsAccountId = Schema.String.check(
|
|
20
|
+
Schema.isPattern(accountIdPattern),
|
|
21
|
+
).pipe(Schema.brand("AwsAccountId"));
|
|
22
|
+
export type AwsAccountId = typeof AwsAccountId.Type;
|
|
23
|
+
|
|
24
|
+
/** The AwsPartition schema identifies the AWS partition in an ARN, such as aws-cn. */
|
|
25
|
+
export const AwsPartition = label.pipe(Schema.brand("AwsPartition"));
|
|
26
|
+
export type AwsPartition = typeof AwsPartition.Type;
|
|
27
|
+
|
|
28
|
+
/** The AwsRegionName schema requires a concrete region for an AWS client configuration. */
|
|
29
|
+
export const AwsRegionName = label.pipe(Schema.brand("AwsRegionName"));
|
|
30
|
+
export type AwsRegionName = typeof AwsRegionName.Type;
|
|
31
|
+
|
|
32
|
+
/** The AwsArnComponents schema keeps an ARN's resource suffix intact across a parse and format. */
|
|
33
|
+
export const AwsArnComponents = Schema.Struct({
|
|
34
|
+
partition: AwsPartition,
|
|
35
|
+
service: label.pipe(Schema.brand("AwsServiceName")),
|
|
36
|
+
region: Schema.Union([Schema.Literal(""), AwsRegionName]),
|
|
37
|
+
account: Schema.Union([Schema.Literal(""), AwsAccountId]),
|
|
38
|
+
resource: Schema.NonEmptyString.pipe(Schema.brand("AwsResourceName")),
|
|
39
|
+
});
|
|
40
|
+
export type AwsArnComponents = typeof AwsArnComponents.Type;
|
|
41
|
+
|
|
42
|
+
const fields = AwsArnComponents.fields;
|
|
43
|
+
|
|
44
|
+
const arnParts = [
|
|
45
|
+
"arn:",
|
|
46
|
+
fields.partition,
|
|
47
|
+
":",
|
|
48
|
+
fields.service,
|
|
49
|
+
":",
|
|
50
|
+
fields.region,
|
|
51
|
+
":",
|
|
52
|
+
fields.account,
|
|
53
|
+
":",
|
|
54
|
+
fields.resource,
|
|
55
|
+
] as const;
|
|
56
|
+
|
|
57
|
+
/** The AwsArn schema brands a complete AWS identifier for callers that pass it as text. */
|
|
58
|
+
export const AwsArn = Schema.TemplateLiteral(arnParts).pipe(
|
|
59
|
+
Schema.brand("AwsArn"),
|
|
60
|
+
);
|
|
61
|
+
export type AwsArn = typeof AwsArn.Type;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The AwsArnFromString codec reads and formats a concrete AWS identifier. IAM and
|
|
65
|
+
* STS can use an empty region, and an S3 bucket ARN has an empty account.
|
|
66
|
+
* The resource keeps its own separators, so a Lambda version suffix
|
|
67
|
+
* remains part of the resource instead of a separate ARN field.
|
|
68
|
+
*
|
|
69
|
+
* @see ../../../refs/aws-cdk/packages/aws-cdk-lib/core/lib/arn.ts (Arn.format and split)
|
|
70
|
+
* @see ../../../refs/effect/packages/effect/src/Schema.ts (TemplateLiteralParser and decodeTo)
|
|
71
|
+
*/
|
|
72
|
+
export const AwsArnFromString = Schema.TemplateLiteralParser(arnParts).pipe(
|
|
73
|
+
Schema.decodeTo(
|
|
74
|
+
Schema.toType(AwsArnComponents),
|
|
75
|
+
SchemaTransformation.transform({
|
|
76
|
+
decode: ([, partition, , service, , region, , account, , resource]) => ({
|
|
77
|
+
partition,
|
|
78
|
+
service,
|
|
79
|
+
region,
|
|
80
|
+
account,
|
|
81
|
+
resource,
|
|
82
|
+
}),
|
|
83
|
+
encode: ({ partition, service, region, account, resource }) => [
|
|
84
|
+
"arn:",
|
|
85
|
+
partition,
|
|
86
|
+
":",
|
|
87
|
+
service,
|
|
88
|
+
":",
|
|
89
|
+
region,
|
|
90
|
+
":",
|
|
91
|
+
account,
|
|
92
|
+
":",
|
|
93
|
+
resource,
|
|
94
|
+
],
|
|
95
|
+
}),
|
|
96
|
+
),
|
|
97
|
+
);
|
package/awsClient.ts
ADDED
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Array as Arr,
|
|
3
|
+
Context,
|
|
4
|
+
Effect,
|
|
5
|
+
Layer,
|
|
6
|
+
Match,
|
|
7
|
+
Option,
|
|
8
|
+
Predicate,
|
|
9
|
+
Record,
|
|
10
|
+
Ref,
|
|
11
|
+
Schema,
|
|
12
|
+
String as Str,
|
|
13
|
+
Stream,
|
|
14
|
+
Tuple,
|
|
15
|
+
} from "effect";
|
|
16
|
+
import { SecureRandom } from "effect/crypto";
|
|
17
|
+
import {
|
|
18
|
+
Headers,
|
|
19
|
+
HttpClient,
|
|
20
|
+
HttpClientRequest,
|
|
21
|
+
type HttpClientResponse,
|
|
22
|
+
} from "effect/http";
|
|
23
|
+
import { HttpApi, HttpApiClient, HttpApiGroup } from "effect/http-api";
|
|
24
|
+
import {
|
|
25
|
+
AwsClientConfig,
|
|
26
|
+
type AwsClientConfiguration,
|
|
27
|
+
} from "./awsClientConfig.js";
|
|
28
|
+
import {
|
|
29
|
+
AwsJsonRpcPayload,
|
|
30
|
+
type AwsOperation,
|
|
31
|
+
AwsPagination,
|
|
32
|
+
type AwsQueryOperation,
|
|
33
|
+
AwsRequestError,
|
|
34
|
+
type AwsServiceDefinition,
|
|
35
|
+
} from "./awsClientModel.js";
|
|
36
|
+
import { awsHttpEndpoint, awsHttpRequest } from "./awsHttpOperation.js";
|
|
37
|
+
import { awsQueryOutput, awsQueryRequest } from "./awsQueryOperation.js";
|
|
38
|
+
import {
|
|
39
|
+
AwsRequestSigner,
|
|
40
|
+
type AwsSigningContext,
|
|
41
|
+
awsRequestSignerLayer,
|
|
42
|
+
} from "./awsRequestSigner.js";
|
|
43
|
+
import { AwsResponseDecoder } from "./awsResponseDecoder.js";
|
|
44
|
+
import { AwsRetryPolicy } from "./awsRetryPolicy.js";
|
|
45
|
+
import { resolveAwsServiceAddress } from "./awsServiceAddress.js";
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The AwsRetryProgress reference lets an application show retry messages in
|
|
49
|
+
* its own progress display. Return true after displaying a message to suppress
|
|
50
|
+
* the client's duplicate log entry. Effect uses the default per
|
|
51
|
+
* Context.Reference when the caller has no display.
|
|
52
|
+
*
|
|
53
|
+
* @see ../../../refs/effect/packages/effect/src/Context.ts (Reference)
|
|
54
|
+
*/
|
|
55
|
+
export const AwsRetryProgress = Context.Reference(
|
|
56
|
+
"@tradedal/aws-client-core/retry-progress",
|
|
57
|
+
{ defaultValue: () => (_message: string) => Effect.succeed<boolean>(false) },
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
type OperationContract<Input, Output, Encoded> = AwsOperation & {
|
|
61
|
+
readonly input: Schema.Codec<Input, AwsJsonRpcPayload>;
|
|
62
|
+
readonly output: Schema.Codec<Output, Encoded>;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const PaginationToken = Schema.Union([
|
|
66
|
+
Schema.NonEmptyString,
|
|
67
|
+
AwsJsonRpcPayload,
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The paginationMember function lets the paginator read a token even when
|
|
72
|
+
* AWS places it inside a nested response object. It returns None when the
|
|
73
|
+
* response omits any member of the modeled path.
|
|
74
|
+
*
|
|
75
|
+
* @see ../../../refs/smithy/docs/source-2.0/spec/behavior-traits.rst (Pagination paths)
|
|
76
|
+
*/
|
|
77
|
+
const paginationMember = (output: unknown, path: string) =>
|
|
78
|
+
Arr.reduce(
|
|
79
|
+
Str.split(path, "."),
|
|
80
|
+
Option.some<unknown>(output),
|
|
81
|
+
(value, member) =>
|
|
82
|
+
value.pipe(
|
|
83
|
+
Option.filter(Predicate.isObject),
|
|
84
|
+
Option.flatMap((record) => Record.get(record, member)),
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The AwsClient service executes generated AWS operations through their input, output, and
|
|
90
|
+
* invocation contracts. Generated clients share its request preparation and
|
|
91
|
+
* retry behavior, while applications provide credentials, signing, transport,
|
|
92
|
+
* and error decoding through the same service graph.
|
|
93
|
+
*/
|
|
94
|
+
export class AwsClient extends Context.Service<AwsClient>()(
|
|
95
|
+
"@tradedal/aws-client-core/awsClient",
|
|
96
|
+
{
|
|
97
|
+
make: Effect.gen(function* () {
|
|
98
|
+
const config = yield* AwsClientConfig;
|
|
99
|
+
const signer = yield* AwsRequestSigner;
|
|
100
|
+
const decoder = yield* AwsResponseDecoder;
|
|
101
|
+
const retryPolicy = yield* AwsRetryPolicy;
|
|
102
|
+
const random = yield* SecureRandom.SecureRandom;
|
|
103
|
+
const transport = yield* HttpClient.HttpClient;
|
|
104
|
+
const redactedHeaderNames = yield* Headers.CurrentRedactedNames;
|
|
105
|
+
|
|
106
|
+
const redactions = Arr.appendAll(redactedHeaderNames, [
|
|
107
|
+
"x-amz-security-token",
|
|
108
|
+
"x-amz-sso_bearer_token",
|
|
109
|
+
]);
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The preparePayload service method fills in missing idempotency
|
|
113
|
+
* tokens for one invocation. Reuse its result across retries so AWS
|
|
114
|
+
* receives the same token on each attempt.
|
|
115
|
+
*
|
|
116
|
+
* @see ../../../refs/smithy/docs/source-2.0/spec/behavior-traits.rst (idempotencyToken)
|
|
117
|
+
*/
|
|
118
|
+
const preparePayload = Effect.fn("AwsClient.preparePayload")(function* (
|
|
119
|
+
operation: AwsOperation,
|
|
120
|
+
payload: AwsJsonRpcPayload,
|
|
121
|
+
) {
|
|
122
|
+
const missingTokens = Arr.filter(
|
|
123
|
+
operation.idempotencyTokens,
|
|
124
|
+
(token) => !Record.has(payload, token),
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const prepared: AwsJsonRpcPayload = yield* Effect.reduce(
|
|
128
|
+
missingTokens,
|
|
129
|
+
() => payload,
|
|
130
|
+
(current, token) =>
|
|
131
|
+
random
|
|
132
|
+
.uuidV4()
|
|
133
|
+
.pipe(Effect.map((value) => Record.set(current, token, value))),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
return prepared;
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The authorizeRequest operation signs with the identity available
|
|
141
|
+
* for this attempt. An unsigned AWS operation can run before the
|
|
142
|
+
* application has acquired credentials.
|
|
143
|
+
*/
|
|
144
|
+
const authorizeRequest = Effect.fnUntraced(function* (
|
|
145
|
+
request: HttpClientRequest.HttpClientRequest,
|
|
146
|
+
signing: Option.Option<Omit<AwsSigningContext, "credentials">>,
|
|
147
|
+
) {
|
|
148
|
+
const signed = yield* signing.pipe(
|
|
149
|
+
Option.map((scope) =>
|
|
150
|
+
config
|
|
151
|
+
.resolveCredentials()
|
|
152
|
+
.pipe(
|
|
153
|
+
Effect.flatMap((credentials) =>
|
|
154
|
+
signer.signRequest(request, { credentials, ...scope }),
|
|
155
|
+
),
|
|
156
|
+
),
|
|
157
|
+
),
|
|
158
|
+
Effect.transposeOption,
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
return Option.getOrElse(signed, () => request);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The sendRequest service method lets the retry policy distinguish an AWS
|
|
166
|
+
* rejection from a transport failure. Each attempt uses the credentials
|
|
167
|
+
* available at the time it sends the request.
|
|
168
|
+
*
|
|
169
|
+
* @see ./awsRequestSigner.ts (AwsRequestSigner)
|
|
170
|
+
* @see ./awsResponseDecoder.ts (AwsResponseDecoder)
|
|
171
|
+
*/
|
|
172
|
+
const sendRequest = Effect.fn("AwsClient.sendRequest")(function* (
|
|
173
|
+
service: AwsServiceDefinition,
|
|
174
|
+
operation: AwsOperation,
|
|
175
|
+
signing: Option.Option<Omit<AwsSigningContext, "credentials">>,
|
|
176
|
+
request: HttpClientRequest.HttpClientRequest,
|
|
177
|
+
) {
|
|
178
|
+
yield* Effect.annotateCurrentSpan({
|
|
179
|
+
"aws.service": service.service,
|
|
180
|
+
"aws.operation": operation.name,
|
|
181
|
+
"aws.region": Option.map(signing, ({ region }) => region).pipe(
|
|
182
|
+
Option.getOrUndefined,
|
|
183
|
+
),
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const authorized = yield* authorizeRequest(request, signing);
|
|
187
|
+
|
|
188
|
+
const received = yield* transport
|
|
189
|
+
.postprocess(Effect.succeed(authorized))
|
|
190
|
+
.pipe(
|
|
191
|
+
Effect.provideService(Headers.CurrentRedactedNames, redactions),
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
yield* Effect.annotateCurrentSpan({
|
|
195
|
+
"http.response.status_code": received.status,
|
|
196
|
+
"aws.request_id": received.headers["x-amzn-requestid"],
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
yield* Effect.when(
|
|
200
|
+
decoder
|
|
201
|
+
.decodeError(service, operation, received)
|
|
202
|
+
.pipe(Effect.flatMap(Effect.fail)),
|
|
203
|
+
Effect.succeed(received.status < 200 || received.status >= 300),
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
const response: HttpClientResponse.HttpClientResponse = received;
|
|
207
|
+
|
|
208
|
+
return response;
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The waitForRetry operation reports when the caller can expect the
|
|
213
|
+
* next attempt. AWS can extend that wait with Retry-After. A stack's
|
|
214
|
+
* progress display takes precedence over the client's log.
|
|
215
|
+
*/
|
|
216
|
+
const waitForRetry = Effect.fnUntraced(function* (
|
|
217
|
+
operation: string,
|
|
218
|
+
attempt: number,
|
|
219
|
+
retryAfter: string | undefined,
|
|
220
|
+
reason: string,
|
|
221
|
+
) {
|
|
222
|
+
const delay = yield* retryPolicy.retryDelay({ attempt, retryAfter });
|
|
223
|
+
const report = yield* AwsRetryProgress;
|
|
224
|
+
const reported = yield* report(
|
|
225
|
+
`Retrying ${operation} in ${Math.round(delay)}ms`,
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
yield* Effect.logInfo("Retrying AWS request").pipe(
|
|
229
|
+
Effect.annotateLogs({
|
|
230
|
+
"aws.operation": operation,
|
|
231
|
+
"aws.retry.attempt": attempt,
|
|
232
|
+
"aws.retry.delay_ms": delay,
|
|
233
|
+
"aws.retry.reason": reason,
|
|
234
|
+
}),
|
|
235
|
+
Effect.when(Effect.succeed(!reported)),
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
yield* Effect.sleep(delay);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* The execute service method lets callers retry a transient AWS failure
|
|
243
|
+
* without changing the request's idempotency token. Interruption stops
|
|
244
|
+
* local requests and retry waits.
|
|
245
|
+
*
|
|
246
|
+
* @see ../../../refs/effect/packages/effect/src/Effect.ts (retry)
|
|
247
|
+
*/
|
|
248
|
+
const execute = Effect.fn("AwsClient.execute")(function* (
|
|
249
|
+
service: AwsServiceDefinition,
|
|
250
|
+
operation: AwsOperation,
|
|
251
|
+
configuration: AwsClientConfiguration,
|
|
252
|
+
signing: Option.Option<Omit<AwsSigningContext, "credentials">>,
|
|
253
|
+
request: HttpClientRequest.HttpClientRequest,
|
|
254
|
+
) {
|
|
255
|
+
const attempts = yield* Ref.make(0);
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* The shouldRetry operation leaves authorization and validation
|
|
259
|
+
* failures with the caller and waits only for retryable failures.
|
|
260
|
+
*/
|
|
261
|
+
const shouldRetry = Effect.fnUntraced(function* (
|
|
262
|
+
failure: Effect.Error<ReturnType<typeof sendRequest>>,
|
|
263
|
+
) {
|
|
264
|
+
const retryable = Match.value(failure).pipe(
|
|
265
|
+
Match.tag("AwsServiceError", (error) => error.retryable),
|
|
266
|
+
Match.tag("HttpClientError", () => true),
|
|
267
|
+
Match.orElse(() => false),
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
const attempt = yield* Ref.updateAndGet(
|
|
271
|
+
attempts,
|
|
272
|
+
(count) => count + 1,
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
const retryAfter = Match.value(failure).pipe(
|
|
276
|
+
Match.tag("AwsServiceError", (error) => error.retryAfter),
|
|
277
|
+
Match.orElse(() => undefined),
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
yield* waitForRetry(
|
|
281
|
+
operation.name,
|
|
282
|
+
attempt,
|
|
283
|
+
retryAfter,
|
|
284
|
+
failure._tag,
|
|
285
|
+
).pipe(Effect.when(Effect.succeed(retryable)));
|
|
286
|
+
|
|
287
|
+
return retryable;
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
const response: HttpClientResponse.HttpClientResponse =
|
|
291
|
+
yield* sendRequest(service, operation, signing, request).pipe(
|
|
292
|
+
Effect.retry({
|
|
293
|
+
times: configuration.maxAttempts - 1,
|
|
294
|
+
while: shouldRetry,
|
|
295
|
+
}),
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
return response;
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* The createHttpClient service method applies the resolved AWS address
|
|
303
|
+
* to the operation path before signing. The generated path and query
|
|
304
|
+
* parameters must reach the signer and transport together.
|
|
305
|
+
*
|
|
306
|
+
* @see ../../../refs/effect/packages/effect/src/http/HttpClientRequest.ts (prependUrl)
|
|
307
|
+
*/
|
|
308
|
+
const createHttpClient = Effect.fn("AwsClient.createHttpClient")(
|
|
309
|
+
function* (service: AwsServiceDefinition, operation: AwsOperation) {
|
|
310
|
+
const configuration = yield* config.resolveConfiguration();
|
|
311
|
+
|
|
312
|
+
const address = yield* resolveAwsServiceAddress(
|
|
313
|
+
service.endpointRules,
|
|
314
|
+
{ ...configuration },
|
|
315
|
+
Option.liftPredicate(
|
|
316
|
+
service.signingName,
|
|
317
|
+
() => operation.unsigned !== true,
|
|
318
|
+
).pipe(Option.getOrUndefined),
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
const client = HttpClient.transform(
|
|
322
|
+
HttpClient.mapRequest(transport, (request) =>
|
|
323
|
+
request.pipe(
|
|
324
|
+
HttpClientRequest.prependUrl(address.url.toString()),
|
|
325
|
+
HttpClientRequest.setHeaders(operation.headers),
|
|
326
|
+
HttpClientRequest.setHeaders(address.headers),
|
|
327
|
+
),
|
|
328
|
+
),
|
|
329
|
+
(_send, request) =>
|
|
330
|
+
execute(
|
|
331
|
+
service,
|
|
332
|
+
operation,
|
|
333
|
+
configuration,
|
|
334
|
+
address.signing,
|
|
335
|
+
request,
|
|
336
|
+
),
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
return client;
|
|
340
|
+
},
|
|
341
|
+
);
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* The executeJson method gives HttpApiClient the modeled request and
|
|
345
|
+
* expected response status for JSON operations. The supplied HTTP
|
|
346
|
+
* client signs and sends the request after it has been encoded.
|
|
347
|
+
*
|
|
348
|
+
* @see ../../../refs/effect/packages/effect/src/http-api/HttpApiClient.ts (makeWith)
|
|
349
|
+
*/
|
|
350
|
+
const executeJson = <Output, Encoded>(
|
|
351
|
+
operation: AwsOperation & {
|
|
352
|
+
readonly output: Schema.Codec<Output, Encoded>;
|
|
353
|
+
},
|
|
354
|
+
httpClient: Effect.Success<ReturnType<typeof createHttpClient>>,
|
|
355
|
+
payload: AwsJsonRpcPayload,
|
|
356
|
+
) => {
|
|
357
|
+
const api = HttpApi.make("AwsJsonOperation").add(
|
|
358
|
+
HttpApiGroup.make("operation", { topLevel: true }).add(
|
|
359
|
+
awsHttpEndpoint("execute", operation),
|
|
360
|
+
),
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
return HttpApiClient.makeWith(api, { httpClient }).pipe(
|
|
364
|
+
Effect.flatMap((client) =>
|
|
365
|
+
client.execute(awsHttpRequest(operation, payload)),
|
|
366
|
+
),
|
|
367
|
+
);
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* The executeQuery method uses the Smithy wire shapes for the form
|
|
372
|
+
* request and XML reply. It validates the decoded reply against the
|
|
373
|
+
* same Effect schema that the generated IAM client exposes.
|
|
374
|
+
*
|
|
375
|
+
* @see ../../../refs/smithy/docs/source-2.0/aws/protocols/aws-query-protocol.rst (Overview)
|
|
376
|
+
*/
|
|
377
|
+
const executeQuery = <Output, Encoded>(
|
|
378
|
+
service: AwsServiceDefinition,
|
|
379
|
+
operation: AwsQueryOperation & {
|
|
380
|
+
readonly output: Schema.Codec<Output, Encoded>;
|
|
381
|
+
},
|
|
382
|
+
httpClient: Effect.Success<ReturnType<typeof createHttpClient>>,
|
|
383
|
+
payload: AwsJsonRpcPayload,
|
|
384
|
+
) =>
|
|
385
|
+
httpClient.execute(awsQueryRequest(service, operation, payload)).pipe(
|
|
386
|
+
Effect.flatMap((response) => awsQueryOutput(operation, response)),
|
|
387
|
+
Effect.flatMap(Schema.decodeUnknownEffect(operation.output)),
|
|
388
|
+
);
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* The bindOperation service method turns a generated AWS operation into
|
|
392
|
+
* a callable Effect. It encodes inputs before signing and uses the
|
|
393
|
+
* operation's success schema and HTTP status to decode the response.
|
|
394
|
+
*
|
|
395
|
+
* @see ../../../refs/effect/packages/effect/src/http-api/HttpApiClient.ts (makeWith)
|
|
396
|
+
*/
|
|
397
|
+
const bindOperation =
|
|
398
|
+
(service: AwsServiceDefinition) =>
|
|
399
|
+
<Input, Output, Encoded>(
|
|
400
|
+
operation: OperationContract<Input, Output, Encoded>,
|
|
401
|
+
) => {
|
|
402
|
+
/**
|
|
403
|
+
* The invoke operation fills any missing idempotency token before
|
|
404
|
+
* execution so retries can recover the same AWS result.
|
|
405
|
+
*/
|
|
406
|
+
const invoke = Effect.fn(`${service.signingName}.${operation.name}`, {
|
|
407
|
+
attributes: {
|
|
408
|
+
"aws.service": service.service,
|
|
409
|
+
"aws.operation": operation.name,
|
|
410
|
+
},
|
|
411
|
+
})(
|
|
412
|
+
function* (input: Input) {
|
|
413
|
+
const payload: AwsJsonRpcPayload = yield* Schema.encodeEffect(
|
|
414
|
+
operation.input,
|
|
415
|
+
)(input);
|
|
416
|
+
|
|
417
|
+
const prepared: AwsJsonRpcPayload = yield* preparePayload(
|
|
418
|
+
operation,
|
|
419
|
+
payload,
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
yield* Effect.annotateCurrentSpan(
|
|
423
|
+
Record.filter(
|
|
424
|
+
{ "aws.cloudcontrol.type_name": prepared.TypeName },
|
|
425
|
+
(value) => value !== undefined,
|
|
426
|
+
),
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
const httpClient = yield* createHttpClient(service, operation);
|
|
430
|
+
|
|
431
|
+
const result: Output = yield* Match.value(operation).pipe(
|
|
432
|
+
Match.when(
|
|
433
|
+
(
|
|
434
|
+
operation,
|
|
435
|
+
): operation is typeof operation & AwsQueryOperation =>
|
|
436
|
+
operation.query !== undefined,
|
|
437
|
+
(query) => executeQuery(service, query, httpClient, prepared),
|
|
438
|
+
),
|
|
439
|
+
Match.orElse((operation) =>
|
|
440
|
+
executeJson(operation, httpClient, prepared),
|
|
441
|
+
),
|
|
442
|
+
);
|
|
443
|
+
|
|
444
|
+
return result;
|
|
445
|
+
},
|
|
446
|
+
Effect.annotateSpans({
|
|
447
|
+
"aws.service": service.service,
|
|
448
|
+
"aws.operation": operation.name,
|
|
449
|
+
}),
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
return invoke;
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* The bindOperationPages service method lets a generated client read all
|
|
457
|
+
* results that match one request, one response page at a time. Callers
|
|
458
|
+
* can stop after any page. If AWS Query reports more results but omits
|
|
459
|
+
* the continuation token, the stream fails so the caller can distinguish
|
|
460
|
+
* an incomplete result from the end of the list.
|
|
461
|
+
*
|
|
462
|
+
* @see ../../../refs/smithy/docs/source-2.0/spec/behavior-traits.rst (Pagination client behavior)
|
|
463
|
+
* @see ../../../refs/effect/packages/effect/src/Stream.ts (paginate and unwrap)
|
|
464
|
+
*/
|
|
465
|
+
const bindOperationPages =
|
|
466
|
+
(service: AwsServiceDefinition) =>
|
|
467
|
+
<Input, Output, Encoded>(
|
|
468
|
+
operation: OperationContract<Input, Output, Encoded>,
|
|
469
|
+
) => {
|
|
470
|
+
const invoke = bindOperation(service)(operation);
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* The paginate operation reads the pagination contract before the
|
|
474
|
+
* first request, so invalid model metadata fails without sending it.
|
|
475
|
+
*/
|
|
476
|
+
const paginate = Effect.fnUntraced(function* (input: Input) {
|
|
477
|
+
const pagination = yield* Schema.decodeUnknownEffect(AwsPagination)(
|
|
478
|
+
operation.pagination,
|
|
479
|
+
);
|
|
480
|
+
const { inputToken, outputToken } = pagination;
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* The nextRequest operation leaves the original filters and
|
|
484
|
+
* page size in place when it adds AWS's continuation token.
|
|
485
|
+
*/
|
|
486
|
+
const nextRequest = Effect.fnUntraced(function* (value: unknown) {
|
|
487
|
+
const token =
|
|
488
|
+
yield* Schema.decodeUnknownEffect(PaginationToken)(value);
|
|
489
|
+
|
|
490
|
+
const original = yield* Schema.encodeEffect(operation.input)(
|
|
491
|
+
input,
|
|
492
|
+
);
|
|
493
|
+
const continued = Record.set(original, inputToken, token);
|
|
494
|
+
|
|
495
|
+
return yield* Schema.decodeEffect(operation.input)(continued);
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* The readPage operation refuses to return an incomplete
|
|
500
|
+
* list as final when AWS signals a continuation without a token.
|
|
501
|
+
*/
|
|
502
|
+
const readPage = Effect.fnUntraced(function* (request: Input) {
|
|
503
|
+
const page = yield* invoke(request);
|
|
504
|
+
|
|
505
|
+
const token = paginationMember(page, outputToken).pipe(
|
|
506
|
+
Option.filter(Predicate.isNotNullish),
|
|
507
|
+
Option.filter((value) => value !== ""),
|
|
508
|
+
Option.filter(
|
|
509
|
+
(value) =>
|
|
510
|
+
!Predicate.isObject(value) || !Record.isEmptyRecord(value),
|
|
511
|
+
),
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
// A truncated AWS Query page promises a continuation, even if
|
|
515
|
+
// the response omitted its marker. Report the incomplete read.
|
|
516
|
+
const queryTruncated =
|
|
517
|
+
operation.query !== undefined &&
|
|
518
|
+
Option.contains(paginationMember(page, "IsTruncated"), true);
|
|
519
|
+
const missingContinuation =
|
|
520
|
+
queryTruncated && Option.isNone(token);
|
|
521
|
+
|
|
522
|
+
yield* Effect.succeed(missingContinuation).pipe(
|
|
523
|
+
Effect.filterOrFail(
|
|
524
|
+
(missing) => !missing,
|
|
525
|
+
() =>
|
|
526
|
+
new AwsRequestError({
|
|
527
|
+
message: `AWS ${operation.name} returned a truncated page without ${outputToken}`,
|
|
528
|
+
}),
|
|
529
|
+
),
|
|
530
|
+
);
|
|
531
|
+
|
|
532
|
+
const next = yield* token.pipe(
|
|
533
|
+
Option.map(nextRequest),
|
|
534
|
+
Effect.transposeOption,
|
|
535
|
+
);
|
|
536
|
+
|
|
537
|
+
return Tuple.make([page], next);
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
return Stream.paginate(input, readPage);
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
return (input: Input) => Stream.unwrap(paginate(input));
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
return { bindOperation, bindOperationPages };
|
|
547
|
+
}),
|
|
548
|
+
},
|
|
549
|
+
) {}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Callers of generated AWS operations can use the awsClientLayer Layer with
|
|
553
|
+
* their own configuration and HTTP transport. Credential refresh and signing
|
|
554
|
+
* follow that configuration whether the operation comes from a stack command
|
|
555
|
+
* or a direct API call.
|
|
556
|
+
*/
|
|
557
|
+
export const awsClientLayer = Layer.effect(AwsClient, AwsClient.make).pipe(
|
|
558
|
+
Layer.provide([
|
|
559
|
+
awsRequestSignerLayer,
|
|
560
|
+
Layer.effect(AwsResponseDecoder, AwsResponseDecoder.make),
|
|
561
|
+
Layer.effect(AwsRetryPolicy, AwsRetryPolicy.make),
|
|
562
|
+
SecureRandom.layerWebCrypto,
|
|
563
|
+
]),
|
|
564
|
+
);
|