@distilled.cloud/core 1.0.0-rc.20 → 1.0.0-rc.21
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/README.md +33 -0
- package/lib/_bundle/aws.js +1 -1
- package/lib/_bundle/aws.js.map +1 -1
- package/lib/_bundle/base.js +2 -2
- package/lib/_bundle/base.js.map +1 -1
- package/lib/_bundle/graphql.js +2 -2
- package/lib/_bundle/graphql.js.map +1 -1
- package/lib/_bundle/grpc.js +2 -1
- package/lib/_bundle/grpc.js.map +1 -1
- package/lib/_bundle/rest.js +1 -1
- package/lib/_bundle/rest.js.map +1 -1
- package/lib/annotations.d.ts.map +1 -1
- package/lib/api.d.ts +71 -58
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -1
- package/lib/category.d.ts +15 -12
- package/lib/category.d.ts.map +1 -1
- package/lib/chunks.d.ts.map +1 -1
- package/lib/config.d.ts +13 -0
- package/lib/config.d.ts.map +1 -1
- package/lib/crypto.d.ts.map +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/errors.d.ts +35 -2
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -1
- package/lib/graphql.d.ts +21 -4
- package/lib/graphql.d.ts.map +1 -1
- package/lib/graphql.types.d.ts +0 -1
- package/lib/graphql.types.d.ts.map +1 -1
- package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -1
- package/lib/grpc-transport-cloudflare-http2.js +2 -2
- package/lib/grpc-transport-cloudflare-http2.js.map +1 -1
- package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -1
- package/lib/grpc-transport-cloudflare-sockets.js +4 -4
- package/lib/grpc-transport-cloudflare-sockets.js.map +1 -1
- package/lib/grpc-transport-node-http2.d.ts.map +1 -1
- package/lib/grpc-transport-node-http2.js +1 -1
- package/lib/grpc-transport-node-http2.js.map +1 -1
- package/lib/grpc-transport.d.ts +53 -18
- package/lib/grpc-transport.d.ts.map +1 -1
- package/lib/grpc-transport.js +1 -1
- package/lib/hpack.d.ts.map +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/max-message-bytes.d.ts +42 -0
- package/lib/max-message-bytes.d.ts.map +1 -0
- package/lib/max-message-bytes.js +2 -0
- package/lib/max-message-bytes.js.map +1 -0
- package/lib/ndjson.d.ts +3 -0
- package/lib/ndjson.d.ts.map +1 -0
- package/lib/ndjson.js +2 -0
- package/lib/ndjson.js.map +1 -0
- package/lib/pagination.d.ts +4 -1
- package/lib/pagination.d.ts.map +1 -1
- package/lib/plain-values.d.ts +11 -0
- package/lib/plain-values.d.ts.map +1 -1
- package/lib/plain-values.js +1 -1
- package/lib/plain-values.js.map +1 -1
- package/lib/protobuf.d.ts +12 -19
- package/lib/protobuf.d.ts.map +1 -1
- package/lib/protobuf.js +1 -1
- package/lib/protocol-grpc.d.ts +47 -10
- package/lib/protocol-grpc.d.ts.map +1 -1
- package/lib/protocol-grpc.js +1 -1
- package/lib/protocol-http.d.ts +110 -8
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +1 -1
- package/lib/protocol-rest.d.ts +161 -37
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +1 -1
- package/lib/protocol.d.ts +113 -0
- package/lib/protocol.d.ts.map +1 -0
- package/lib/protocol.js +0 -0
- package/lib/query.d.ts.map +1 -1
- package/lib/response-validation-strict.d.ts.map +1 -1
- package/lib/response-validation.d.ts.map +1 -1
- package/lib/retry-after.d.ts +1 -1
- package/lib/retry-after.d.ts.map +1 -1
- package/lib/retry-state.d.ts +26 -0
- package/lib/retry-state.d.ts.map +1 -0
- package/lib/retry-state.js +2 -0
- package/lib/retry-state.js.map +1 -0
- package/lib/retry.d.ts +100 -20
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -1
- package/lib/schema-lite-impl.d.ts +25 -4
- package/lib/schema-lite-impl.d.ts.map +1 -1
- package/lib/schema-lite-impl.js +1 -1
- package/lib/schema-lite-typed.d.ts.map +1 -1
- package/lib/schema-lite.d.ts +1 -0
- package/lib/schema-lite.d.ts.map +1 -1
- package/lib/schema-lite.js +1 -1
- package/lib/schema.d.ts.map +1 -1
- package/lib/sdk-error.d.ts +4 -0
- package/lib/sdk-error.d.ts.map +1 -1
- package/lib/send.d.ts +37 -0
- package/lib/send.d.ts.map +1 -0
- package/lib/send.js +2 -0
- package/lib/send.js.map +1 -0
- package/lib/sensitive.d.ts.map +1 -1
- package/lib/sensitive.js +1 -1
- package/lib/sensitive.js.map +1 -1
- package/lib/telemetry-debug.d.ts.map +1 -1
- package/lib/telemetry-debug.js +1 -1
- package/lib/telemetry-debug.js.map +1 -1
- package/lib/telemetry-hooks.d.ts.map +1 -1
- package/lib/telemetry-host-rules.d.ts.map +1 -1
- package/lib/telemetry-lambda-context.d.ts.map +1 -1
- package/lib/telemetry-message-context.d.ts.map +1 -1
- package/lib/telemetry-metrics.d.ts.map +1 -1
- package/lib/telemetry-metrics.js +1 -1
- package/lib/telemetry-metrics.js.map +1 -1
- package/lib/telemetry-name-template.d.ts.map +1 -1
- package/lib/telemetry-stream.d.ts.map +1 -1
- package/lib/telemetry-stream.js +1 -1
- package/lib/telemetry-stream.js.map +1 -1
- package/lib/telemetry-xray.d.ts.map +1 -1
- package/lib/telemetry.d.ts +29 -0
- package/lib/telemetry.d.ts.map +1 -1
- package/lib/telemetry.js +2 -2
- package/lib/telemetry.js.map +1 -1
- package/lib/token-cache.d.ts +31 -0
- package/lib/token-cache.d.ts.map +1 -0
- package/lib/token-cache.js +2 -0
- package/lib/token-cache.js.map +1 -0
- package/lib/trait.d.ts +24 -1
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +1 -1
- package/lib/unknown-error.d.ts.map +1 -1
- package/lib/xml.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/api.ts +402 -209
- package/src/category.ts +21 -28
- package/src/config.ts +17 -1
- package/src/errors.ts +24 -2
- package/src/graphql.ts +65 -26
- package/src/graphql.types.ts +15 -0
- package/src/grpc-transport-cloudflare-http2.ts +374 -299
- package/src/grpc-transport-cloudflare-sockets.ts +150 -81
- package/src/grpc-transport-node-http2.ts +119 -46
- package/src/grpc-transport.ts +211 -42
- package/src/max-message-bytes.ts +58 -0
- package/src/ndjson.ts +55 -0
- package/src/pagination.ts +26 -15
- package/src/plain-values.ts +30 -1
- package/src/protobuf.ts +40 -5
- package/src/protocol-grpc.ts +518 -348
- package/src/protocol-http.ts +474 -100
- package/src/protocol-rest.ts +580 -140
- package/src/protocol.ts +150 -0
- package/src/response-validation-strict.ts +9 -3
- package/src/retry-after.ts +1 -1
- package/src/retry-state.ts +105 -0
- package/src/retry.ts +176 -47
- package/src/schema-lite-impl.ts +81 -10
- package/src/schema-lite.ts +1 -0
- package/src/sdk-error.ts +4 -0
- package/src/send.ts +156 -0
- package/src/sensitive.ts +49 -15
- package/src/telemetry.ts +2 -34
- package/src/token-cache.ts +117 -0
- package/src/trait.ts +34 -1
package/src/api.ts
CHANGED
|
@@ -1,33 +1,38 @@
|
|
|
1
|
+
import * as Cause from "effect/Cause";
|
|
1
2
|
import * as Context from "effect/Context";
|
|
2
|
-
import
|
|
3
|
+
import * as Duration from "effect/Duration";
|
|
3
4
|
import * as Effect from "effect/Effect";
|
|
4
5
|
import * as Exit from "effect/Exit";
|
|
5
6
|
import * as HttpClient from "effect/http/HttpClient";
|
|
6
7
|
import type * as HttpClientError from "effect/http/HttpClientError";
|
|
7
8
|
import type * as HttpClientRequest from "effect/http/HttpClientRequest";
|
|
8
|
-
import type * as HttpClientResponse from "effect/http/HttpClientResponse";
|
|
9
|
-
import * as Layer from "effect/Layer";
|
|
10
9
|
import * as Option from "effect/Option";
|
|
11
10
|
import { pipeArguments } from "effect/Pipeable";
|
|
12
11
|
import * as Ref from "effect/Ref";
|
|
12
|
+
import * as References from "effect/References";
|
|
13
13
|
import * as S from "effect/Schema";
|
|
14
14
|
import type * as AST from "effect/SchemaAST";
|
|
15
|
-
import * as Scope from "effect/Scope";
|
|
16
15
|
import * as Stream from "effect/Stream";
|
|
17
16
|
import { SingleShotGen } from "effect/Utils";
|
|
18
|
-
import { getAnn, rawResponseRootSymbol } from "./annotations.ts";
|
|
17
|
+
import { getAnn, getProps, rawResponseRootSymbol } from "./annotations.ts";
|
|
19
18
|
import { isThrottling } from "./category.ts";
|
|
20
19
|
import * as Pagination from "./pagination.ts";
|
|
20
|
+
import type { Operation, Protocol } from "./protocol.ts";
|
|
21
|
+
import { takeLastRequest } from "./retry-state.ts";
|
|
21
22
|
import {
|
|
22
23
|
type Backoff,
|
|
24
|
+
BOUNDED_HINT_MAX_EXTRA_MS,
|
|
23
25
|
defaultOptions,
|
|
24
26
|
type Options as RetryOptions,
|
|
25
27
|
type Policy as RetryPolicy,
|
|
28
|
+
type RetryOverride,
|
|
29
|
+
defaultWith,
|
|
26
30
|
resolveServerRetryHintCapMs,
|
|
27
|
-
serverHintMillis,
|
|
28
31
|
} from "./retry.ts";
|
|
29
32
|
import { SdkError } from "./sdk-error.ts";
|
|
33
|
+
import * as Send from "./send.ts";
|
|
30
34
|
import * as Telemetry from "./telemetry.ts";
|
|
35
|
+
import { idempotencyTokenSymbol } from "./trait.ts";
|
|
31
36
|
|
|
32
37
|
//#region Protocol
|
|
33
38
|
|
|
@@ -55,62 +60,22 @@ export interface ProtocolOperationConfig {
|
|
|
55
60
|
*/
|
|
56
61
|
readonly endpointHostPrefix?: string;
|
|
57
62
|
readonly pagination?: Pagination.PaginatedTrait;
|
|
63
|
+
/**
|
|
64
|
+
* How a server-streaming operation's body is framed, for protocols that
|
|
65
|
+
* read it per operation (core/protocol-rest takes a `StreamFraming`, e.g.
|
|
66
|
+
* `ndjson` from core/ndjson). Set only on the operations that stream, so
|
|
67
|
+
* programs that call none never bundle the framing.
|
|
68
|
+
*/
|
|
69
|
+
readonly framing?: unknown;
|
|
70
|
+
/**
|
|
71
|
+
* One frame of `.stream()`, for an operation whose plain call has its own
|
|
72
|
+
* output and streams only when its input asks (see
|
|
73
|
+
* {@link OperationConfig.streamInput}). Protocols decode each frame
|
|
74
|
+
* against it instead of an element of the output's `items`.
|
|
75
|
+
*/
|
|
76
|
+
readonly items?: S.Top;
|
|
58
77
|
}
|
|
59
78
|
|
|
60
|
-
/**
|
|
61
|
-
* The Protocol service knows how to turn a value into an HTTP request using
|
|
62
|
-
* only the input schema's trait annotations, and how to turn a response back
|
|
63
|
-
* into an output value using the output schema's trait annotations.
|
|
64
|
-
*
|
|
65
|
-
* Swap implementations by providing a different `Layer<Protocol>`.
|
|
66
|
-
*/
|
|
67
|
-
export class Protocol
|
|
68
|
-
extends /*@__PURE__*/ Context.Service<
|
|
69
|
-
Protocol,
|
|
70
|
-
{
|
|
71
|
-
readonly encode: (args: {
|
|
72
|
-
readonly input: unknown;
|
|
73
|
-
readonly inputAst: AST.AST;
|
|
74
|
-
/** The operation's config (memoized per operation -- see {@link ProtocolOperationConfig}). */
|
|
75
|
-
readonly config: ProtocolOperationConfig;
|
|
76
|
-
}) => Effect.Effect<HttpClientRequest.HttpClientRequest>;
|
|
77
|
-
readonly decode: (args: {
|
|
78
|
-
readonly response: HttpClientResponse.HttpClientResponse;
|
|
79
|
-
readonly outputAst: AST.AST;
|
|
80
|
-
/**
|
|
81
|
-
* The operation's declared error classes (from `OperationConfig.errors`).
|
|
82
|
-
* Protocols use these to surface wire failures as the operation's typed
|
|
83
|
-
* errors -- e.g. by consulting matcher metadata stamped on the class.
|
|
84
|
-
*/
|
|
85
|
-
readonly errors: ReadonlyArray<ApiErrorClass>;
|
|
86
|
-
/** The operation's config (memoized per operation -- see {@link ProtocolOperationConfig}). */
|
|
87
|
-
readonly config: ProtocolOperationConfig;
|
|
88
|
-
}) => Effect.Effect<unknown>;
|
|
89
|
-
/**
|
|
90
|
-
* Optional whole-call implementation. When present, `API.make` runs a
|
|
91
|
-
* call as `call(operation, input)` instead of encode -> HttpClient ->
|
|
92
|
-
* decode, so a protocol can read the context once, build the request
|
|
93
|
-
* synchronously and decode the response without intermediate effects.
|
|
94
|
-
* It must read the `HttpClient` from the calling fiber's context and
|
|
95
|
-
* produce the same request, result and errors as encode/decode.
|
|
96
|
-
*/
|
|
97
|
-
readonly call?: (operation: PreparedOperation, input: unknown) => Effect.Effect<unknown>;
|
|
98
|
-
/**
|
|
99
|
-
* Server streaming (see {@link makeStreaming}): each response message
|
|
100
|
-
* as it arrives, decoded as one element of the output's `items`.
|
|
101
|
-
* Ending the stream early cancels the call. Protocols that can't
|
|
102
|
-
* stream leave it out.
|
|
103
|
-
*/
|
|
104
|
-
readonly stream?: (
|
|
105
|
-
operation: PreparedOperation,
|
|
106
|
-
input: unknown,
|
|
107
|
-
) => Stream.Stream<unknown, unknown>;
|
|
108
|
-
}
|
|
109
|
-
>()("Protocol") {}
|
|
110
|
-
|
|
111
|
-
/** The service a protocol layer provides. */
|
|
112
|
-
export type ProtocolShape = (typeof Protocol)["Service"];
|
|
113
|
-
|
|
114
79
|
/**
|
|
115
80
|
* One operation as a protocol's {@link Protocol.call} sees it. Built once per
|
|
116
81
|
* operation on its first call; the same object is passed on every call.
|
|
@@ -133,14 +98,15 @@ export interface PreparedOperation {
|
|
|
133
98
|
* successful `Exit` (e.g. a `Credentials` service holding
|
|
134
99
|
* `Effect.succeed(config)`), so a resolved value costs no fiber step.
|
|
135
100
|
*/
|
|
136
|
-
export const withValue = <A, B>(
|
|
137
|
-
effect: Effect.Effect<A,
|
|
138
|
-
f: (a: A) => Effect.Effect<B,
|
|
139
|
-
): Effect.Effect<B,
|
|
101
|
+
export const withValue = <A, E, R, B, E2, R2>(
|
|
102
|
+
effect: Effect.Effect<A, E, R>,
|
|
103
|
+
f: (a: A) => Effect.Effect<B, E2, R2>,
|
|
104
|
+
): Effect.Effect<B, E | E2, R | R2> =>
|
|
140
105
|
Exit.isExit(effect)
|
|
141
106
|
? Exit.isSuccess(effect)
|
|
142
107
|
? f(effect.value as A)
|
|
143
|
-
:
|
|
108
|
+
: // A failed Exit never produces its value.
|
|
109
|
+
(effect as Exit.Exit<never, E>)
|
|
144
110
|
: Effect.flatMap(effect, f);
|
|
145
111
|
|
|
146
112
|
export type ApiErrorClass = {
|
|
@@ -190,34 +156,11 @@ export interface OperationSchemas<I, O> {
|
|
|
190
156
|
// nothing extra.
|
|
191
157
|
|
|
192
158
|
/**
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
* same module-level layer const). The build runs against a process-lifetime
|
|
196
|
-
* scope, and concurrent first calls are deduplicated through a private
|
|
197
|
-
* MemoMap.
|
|
198
|
-
*
|
|
199
|
-
* Contract for protocol implementations: the layer build must not capture
|
|
200
|
-
* per-call context. Anything call-dependent -- credentials, per-request
|
|
201
|
-
* options -- must be resolved inside `encode`/`decode`, which execute on the
|
|
202
|
-
* calling fiber and therefore see the caller's services on every request.
|
|
159
|
+
* The operation's protocol, kept on `prepared` from its first call on (the
|
|
160
|
+
* warm path reads it there).
|
|
203
161
|
*/
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
const protocolContexts = /*@__PURE__*/ new WeakMap<
|
|
207
|
-
Layer.Layer<Protocol, any, any>,
|
|
208
|
-
Context.Context<Protocol>
|
|
209
|
-
>();
|
|
210
|
-
|
|
211
|
-
const protocolContext = <PE, PR>(
|
|
212
|
-
layer: Layer.Layer<Protocol, PE, PR>,
|
|
213
|
-
): Effect.Effect<Context.Context<Protocol>, PE, PR> => {
|
|
214
|
-
const cached = protocolContexts.get(layer);
|
|
215
|
-
if (cached) return Effect.succeed(cached);
|
|
216
|
-
return Effect.map(Layer.buildWithMemoMap(layer, protocolMemoMap, protocolScope), (ctx) => {
|
|
217
|
-
protocolContexts.set(layer, ctx);
|
|
218
|
-
return ctx;
|
|
219
|
-
});
|
|
220
|
-
};
|
|
162
|
+
const protocolOf = (prepared: Prepared): Effect.Effect<Protocol<any, any>> =>
|
|
163
|
+
Effect.succeed((prepared.service = prepared.cfg.protocol));
|
|
221
164
|
|
|
222
165
|
export interface OperationConfig<
|
|
223
166
|
I extends S.Top,
|
|
@@ -225,23 +168,38 @@ export interface OperationConfig<
|
|
|
225
168
|
PE,
|
|
226
169
|
PR,
|
|
227
170
|
E extends readonly ApiErrorClass[] = readonly ApiErrorClass[],
|
|
171
|
+
SE extends readonly ApiErrorClass[] = readonly ApiErrorClass[],
|
|
228
172
|
> {
|
|
229
173
|
input?: I;
|
|
230
174
|
output?: O;
|
|
175
|
+
/** The errors declared on the operation: only this operation returns them. */
|
|
231
176
|
errors?: E;
|
|
232
177
|
/**
|
|
233
|
-
* The
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
|
|
178
|
+
* The errors declared on the operation's service: the same list on every
|
|
179
|
+
* operation of the service. The protocol matches a failed response
|
|
180
|
+
* against the operation's classes, then these.
|
|
181
|
+
*/
|
|
182
|
+
serviceErrors?: SE;
|
|
183
|
+
/**
|
|
184
|
+
* What encodes and decodes this operation's wire format: a protocol
|
|
185
|
+
* (core/protocol), an object whose type states the errors the protocol
|
|
186
|
+
* declares (`PE`) and what its methods read (`PR`), so the operation's
|
|
187
|
+
* type gets them from the protocol. It resolves call-dependent services
|
|
188
|
+
* (credentials etc.) inside encode/decode, on the calling fiber.
|
|
237
189
|
*/
|
|
238
|
-
protocol:
|
|
190
|
+
protocol: Protocol<PE, PR>;
|
|
239
191
|
/**
|
|
240
192
|
* SDK-specific retry service tag (see `core/retry`). When provided and the
|
|
241
193
|
* caller's context carries a policy under this tag, every call is retried
|
|
242
194
|
* per that policy (e.g. `Layer.succeed(Cloudflare.Retry.Retry, factory)`).
|
|
243
195
|
*/
|
|
244
196
|
retry?: Context.Key<any, RetryPolicy>;
|
|
197
|
+
/**
|
|
198
|
+
* The operation's `distilled.retry#policy` trait (the service's, with the
|
|
199
|
+
* operation's on top): numbers that replace the default policy's when the
|
|
200
|
+
* caller's context carries no policy. A caller's policy ignores it.
|
|
201
|
+
*/
|
|
202
|
+
retryPolicy?: RetryOverride;
|
|
245
203
|
/**
|
|
246
204
|
* The wire operation name for RPC-style protocols (see
|
|
247
205
|
* {@link ProtocolOperationConfig.operationName}).
|
|
@@ -260,27 +218,57 @@ export interface OperationConfig<
|
|
|
260
218
|
* its `op` (a string is just the op).
|
|
261
219
|
*/
|
|
262
220
|
span?: Telemetry.SpanConfig | string;
|
|
221
|
+
/** The body framing of a server-streaming operation (see {@link ProtocolOperationConfig.framing}). */
|
|
222
|
+
framing?: unknown;
|
|
223
|
+
/** One frame of `.stream()` (see {@link ProtocolOperationConfig.items}). */
|
|
224
|
+
items?: S.Top;
|
|
225
|
+
/**
|
|
226
|
+
* Input members `.stream()` sets over the caller's, for an operation that
|
|
227
|
+
* streams only when its input asks (Boat's `executeSandboxCommand` with
|
|
228
|
+
* `stream: true`). The plain call sends the caller's input as given.
|
|
229
|
+
*/
|
|
230
|
+
streamInput?: Readonly<Record<string, unknown>>;
|
|
263
231
|
}
|
|
264
232
|
|
|
265
233
|
/**
|
|
266
234
|
* Wrap one operation call with the caller-provided retry policy, if the
|
|
267
235
|
* operation declares a retry tag and the context carries a policy for it.
|
|
268
236
|
*/
|
|
269
|
-
/**
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
237
|
+
/**
|
|
238
|
+
* Delay before retry `attempt` (0-based) under `b`, in milliseconds, or
|
|
239
|
+
* `undefined` when the server asked for a wait over the hint cap.
|
|
240
|
+
* `boundedHintMs` is the response's `x-amz-retry-after` /
|
|
241
|
+
* `x-distilled-retry-after`.
|
|
242
|
+
*/
|
|
243
|
+
const backoffDelay = (
|
|
244
|
+
b: Backoff,
|
|
245
|
+
attempt: number,
|
|
246
|
+
error: unknown,
|
|
247
|
+
boundedHintMs: number | undefined,
|
|
248
|
+
): Effect.Effect<number | undefined> => {
|
|
249
|
+
const throttling = isThrottling(error);
|
|
250
|
+
let t: number;
|
|
251
|
+
if (b.fullJitter) {
|
|
252
|
+
const base = throttling && b.throttlingBaseMs !== undefined ? b.throttlingBaseMs : b.baseMs;
|
|
253
|
+
t = Math.random() * Math.min(base * b.factor ** attempt, b.capMs);
|
|
254
|
+
} else {
|
|
255
|
+
const base = Math.min(b.baseMs * b.factor ** attempt, b.capMs);
|
|
256
|
+
const floored =
|
|
257
|
+
b.minThrottleMs !== undefined && base < b.minThrottleMs && throttling
|
|
258
|
+
? b.minThrottleMs
|
|
259
|
+
: base;
|
|
260
|
+
t = floored + Math.random() * (b.jitterMs ?? 0);
|
|
278
261
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
262
|
+
if (!b.serverHint) return Effect.succeed(t);
|
|
263
|
+
if (boundedHintMs !== undefined) {
|
|
264
|
+
return Effect.succeed(Math.min(Math.max(boundedHintMs, t), t + BOUNDED_HINT_MAX_EXTRA_MS));
|
|
265
|
+
}
|
|
266
|
+
const hint = (error as { retryAfter?: unknown } | null)?.retryAfter;
|
|
267
|
+
if (!Duration.isDuration(hint)) return Effect.succeed(t);
|
|
268
|
+
const hintMs = Duration.toMillis(hint);
|
|
269
|
+
return Effect.map(resolveServerRetryHintCapMs(), (capMs) =>
|
|
270
|
+
hintMs > capMs ? undefined : Math.max(t, hintMs),
|
|
271
|
+
);
|
|
284
272
|
};
|
|
285
273
|
|
|
286
274
|
const NO_BACKOFF: Backoff = { baseMs: 0, factor: 1, capMs: 0 };
|
|
@@ -317,15 +305,20 @@ const backoffSleep = (
|
|
|
317
305
|
*/
|
|
318
306
|
const applyRetry = (
|
|
319
307
|
base: Effect.Effect<any, any, any>,
|
|
320
|
-
|
|
308
|
+
cfg: OperationConfig<any, any, any, any, any>,
|
|
321
309
|
debug: Telemetry.DebugHooks | undefined,
|
|
322
310
|
): Effect.Effect<any, any, any> => {
|
|
311
|
+
const retryKey = cfg.retry;
|
|
323
312
|
const first = debug === undefined ? base : debug.attempt(base, 1);
|
|
324
313
|
return retryKey === undefined
|
|
325
314
|
? first
|
|
326
315
|
: Effect.catch(first, (error) =>
|
|
327
316
|
Effect.flatMap(Effect.serviceOption(retryKey), (opt) => {
|
|
328
|
-
if (Option.isNone(opt))
|
|
317
|
+
if (Option.isNone(opt)) {
|
|
318
|
+
const options =
|
|
319
|
+
cfg.retryPolicy === undefined ? defaultOptions : defaultWith(cfg.retryPolicy);
|
|
320
|
+
return retryWith(base, options, error, debug);
|
|
321
|
+
}
|
|
329
322
|
const policy = opt.value;
|
|
330
323
|
if (typeof policy !== "function") return retryWith(base, policy, error, debug);
|
|
331
324
|
return Effect.flatMap(Ref.make<unknown>(error), (lastError) =>
|
|
@@ -362,8 +355,9 @@ const retryWith = (
|
|
|
362
355
|
};
|
|
363
356
|
return Effect.flatMap(schedule.step, (next) => {
|
|
364
357
|
let attempt = 0;
|
|
358
|
+
// A request whose body could be read only once is never sent again.
|
|
365
359
|
const loop = (e: unknown): Effect.Effect<any, any, any> =>
|
|
366
|
-
!keepGoing(e)
|
|
360
|
+
!keepGoing(e) || takeLastRequest().oneShot
|
|
367
361
|
? Effect.fail(e)
|
|
368
362
|
: Effect.clockWith((clock) =>
|
|
369
363
|
Effect.matchEffect(next(clock.currentTimeMillisUnsafe(), e), {
|
|
@@ -378,14 +372,36 @@ const retryWith = (
|
|
|
378
372
|
});
|
|
379
373
|
}
|
|
380
374
|
const b = opts.backoff ?? NO_BACKOFF;
|
|
381
|
-
const
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
375
|
+
const budget = b.budget;
|
|
376
|
+
const step = (e: unknown, n: number): Effect.Effect<any, any, any> => {
|
|
377
|
+
if (!keepGoing(e) || (b.maxRetries !== undefined && n >= b.maxRetries)) {
|
|
378
|
+
return Effect.fail(e);
|
|
379
|
+
}
|
|
380
|
+
// Read now, on the fiber that sent the request that failed.
|
|
381
|
+
const last = takeLastRequest();
|
|
382
|
+
// Its body could be read only once: a retry would send it empty.
|
|
383
|
+
if (last.oneShot) return Effect.fail(e);
|
|
384
|
+
return Effect.flatMap(backoffDelay(b, n, e, last.hintMs), (ms) => {
|
|
385
|
+
if (ms === undefined) return Effect.fail(e);
|
|
386
|
+
// No host: the request went out without `send` (`./send.ts`), so no
|
|
387
|
+
// success would ever refill a bucket. Such calls are not budgeted.
|
|
388
|
+
const budgeted = budget !== undefined && last.host !== "";
|
|
389
|
+
const cost = budgeted ? budget.withdraw(last.host, isThrottling(e)) : undefined;
|
|
390
|
+
if (budgeted && cost === undefined) {
|
|
391
|
+
return b.longPoll ? Effect.flatMap(Effect.sleep(ms), () => Effect.fail(e)) : Effect.fail(e);
|
|
392
|
+
}
|
|
393
|
+
const next =
|
|
394
|
+
cost === undefined
|
|
395
|
+
? again(n + 2)
|
|
396
|
+
: Effect.map(again(n + 2), (value) => {
|
|
397
|
+
budget!.refund(last.host, cost);
|
|
398
|
+
return value;
|
|
399
|
+
});
|
|
400
|
+
return Effect.flatMap(backoffSleep(ms, n + 1, e), () =>
|
|
401
|
+
Effect.catch(next, (failed) => step(failed, n + 1)),
|
|
402
|
+
);
|
|
403
|
+
});
|
|
404
|
+
};
|
|
389
405
|
return step(error, 0);
|
|
390
406
|
};
|
|
391
407
|
|
|
@@ -402,21 +418,58 @@ interface OperationState {
|
|
|
402
418
|
readonly strategy?: Pagination.PaginationStrategy;
|
|
403
419
|
}
|
|
404
420
|
|
|
405
|
-
interface Prepared extends
|
|
421
|
+
interface Prepared extends Operation {
|
|
406
422
|
readonly cfg: OperationConfig<any, any, any, any, any>;
|
|
407
|
-
/** The
|
|
408
|
-
service?:
|
|
409
|
-
/** The operation span's facts, from the telemetry traits
|
|
410
|
-
info
|
|
423
|
+
/** The operation's protocol, from its first call on (see {@link protocolOf}). */
|
|
424
|
+
service?: Protocol<any, any>;
|
|
425
|
+
/** The operation span's facts, from the telemetry traits. */
|
|
426
|
+
readonly info: Telemetry.OperationInfo;
|
|
427
|
+
/** Input members marked `T.IdempotencyToken()`, with their format; read on the first call. */
|
|
428
|
+
idempotencyTokens?: ReadonlyArray<readonly [name: string, format: unknown]>;
|
|
411
429
|
}
|
|
412
430
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
431
|
+
/** A UUIDv7 (RFC 9562): 48-bit Unix milliseconds, then version, variant and random bits. */
|
|
432
|
+
const uuidv7 = (): string => {
|
|
433
|
+
const b = globalThis.crypto.getRandomValues(new Uint8Array(16));
|
|
434
|
+
const ms = Date.now();
|
|
435
|
+
const high = Math.floor(ms / 2 ** 32);
|
|
436
|
+
const low = ms % 2 ** 32;
|
|
437
|
+
b[0] = high >>> 8;
|
|
438
|
+
b[1] = high & 0xff;
|
|
439
|
+
b[2] = low >>> 24;
|
|
440
|
+
b[3] = (low >>> 16) & 0xff;
|
|
441
|
+
b[4] = (low >>> 8) & 0xff;
|
|
442
|
+
b[5] = low & 0xff;
|
|
443
|
+
b[6] = (b[6]! & 0x0f) | 0x70;
|
|
444
|
+
b[8] = (b[8]! & 0x3f) | 0x80;
|
|
445
|
+
let hex = "";
|
|
446
|
+
for (const x of b) hex += x.toString(16).padStart(2, "0");
|
|
447
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* `input` with each missing idempotency token set to a new UUID. Called once
|
|
452
|
+
* per call, outside the retry, so every attempt sends the same token; the
|
|
453
|
+
* caller's own value and object are left as they are.
|
|
454
|
+
*/
|
|
455
|
+
const withIdempotencyTokens = (p: Prepared, input: unknown): unknown => {
|
|
456
|
+
const tokens = (p.idempotencyTokens ??= getProps(p.inputAst).flatMap((prop) => {
|
|
457
|
+
const format = getAnn(prop.type, idempotencyTokenSymbol);
|
|
458
|
+
return typeof prop.name === "string" && format !== undefined
|
|
459
|
+
? [[prop.name, format] as const]
|
|
460
|
+
: [];
|
|
461
|
+
}));
|
|
462
|
+
if (tokens.length === 0 || input === null || typeof input !== "object") return input;
|
|
463
|
+
let filled: Record<string, unknown> | undefined;
|
|
464
|
+
for (const [name, format] of tokens) {
|
|
465
|
+
if ((input as Record<string, unknown>)[name] === undefined) {
|
|
466
|
+
filled ??= { ...(input as Record<string, unknown>) };
|
|
467
|
+
// `globalThis.crypto`, not `node:crypto`, so this runs in browsers too.
|
|
468
|
+
filled[name] = format === "uuidv7" ? uuidv7() : globalThis.crypto.randomUUID();
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
return filled ?? input;
|
|
472
|
+
};
|
|
420
473
|
|
|
421
474
|
const stateKey = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/api/OperationState");
|
|
422
475
|
|
|
@@ -429,12 +482,20 @@ const stateKey = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/api/OperationSt
|
|
|
429
482
|
const prepare = (state: OperationState): Prepared => {
|
|
430
483
|
if (state.prepared) return state.prepared;
|
|
431
484
|
const cfg = state.configFn();
|
|
485
|
+
const inputAst: AST.AST = cfg.input!.ast;
|
|
432
486
|
return (state.prepared = {
|
|
433
487
|
cfg,
|
|
434
488
|
config: cfg,
|
|
435
|
-
inputAst
|
|
489
|
+
inputAst,
|
|
490
|
+
info: Telemetry.operationInfo(cfg.telemetry, cfg.span, cfg.operationName, inputAst),
|
|
436
491
|
outputAst: cfg.output!.ast,
|
|
437
|
-
|
|
492
|
+
// What the protocol matches a failed response against: the operation's
|
|
493
|
+
// declared classes, then its service's. The protocol receives them as
|
|
494
|
+
// `DeclaredErrorClass` (core/protocol).
|
|
495
|
+
errors:
|
|
496
|
+
cfg.serviceErrors === undefined
|
|
497
|
+
? (cfg.errors ?? [])
|
|
498
|
+
: [...(cfg.errors ?? []), ...cfg.serviceErrors],
|
|
438
499
|
compiled: undefined,
|
|
439
500
|
service: undefined,
|
|
440
501
|
});
|
|
@@ -458,13 +519,97 @@ const prepareOr = (state: OperationState): Prepared | SdkError => {
|
|
|
458
519
|
|
|
459
520
|
const stateOf = (op: unknown): OperationState => (op as Record<symbol, OperationState>)[stateKey]!;
|
|
460
521
|
|
|
522
|
+
/**
|
|
523
|
+
* The defect boundary: what a failed operation fails with. Nothing thrown
|
|
524
|
+
* while an operation runs crashes the fiber: a `throw` in a plain helper
|
|
525
|
+
* under its protocol's `encode`, `decode`, `call` or `stream`, a
|
|
526
|
+
* `JSON.parse`, a property read on a `null` the spec says is an object. It
|
|
527
|
+
* becomes the failure of an {@link SdkError} that names the operation and
|
|
528
|
+
* what was thrown, and carries the thrown value as its `cause`. Any other
|
|
529
|
+
* cause (a typed failure, an interrupt) fails as it is.
|
|
530
|
+
*
|
|
531
|
+
* With a `callSite`, the failure also gets the operation as a stack frame
|
|
532
|
+
* at the caller's line, so `Cause.pretty` prints
|
|
533
|
+
* `at S3.GetObject (user.ts:12:5)`; no stack-frame context is provided per
|
|
534
|
+
* call.
|
|
535
|
+
*
|
|
536
|
+
* A call that succeeds pays no Effect step of its own for the boundary: the
|
|
537
|
+
* places that run an operation (the call, `.stream()`, `.pages()` and
|
|
538
|
+
* `.items()`, {@link encodeRequest}) come here from the one failure handler
|
|
539
|
+
* around them.
|
|
540
|
+
*/
|
|
541
|
+
const failed = (
|
|
542
|
+
state: OperationState,
|
|
543
|
+
cause: Cause.Cause<unknown>,
|
|
544
|
+
callSite?: (() => string | undefined) | undefined,
|
|
545
|
+
): Effect.Effect<never, unknown> => {
|
|
546
|
+
// Not loaded: nothing of the operation ran.
|
|
547
|
+
const prepared = state.prepared;
|
|
548
|
+
if (prepared === undefined) return Effect.failCause(cause);
|
|
549
|
+
const name = prepared.info.name;
|
|
550
|
+
const die = cause.reasons.find(Cause.isDieReason);
|
|
551
|
+
if (die !== undefined) {
|
|
552
|
+
const error = new SdkError({ message: `${name} threw: ${String(die.defect)}` });
|
|
553
|
+
error.cause = die.defect;
|
|
554
|
+
cause = Cause.fail(error);
|
|
555
|
+
}
|
|
556
|
+
if (callSite === undefined) return Effect.failCause(cause);
|
|
557
|
+
return Effect.withFiber((fiber) =>
|
|
558
|
+
Effect.failCause(
|
|
559
|
+
Cause.annotate(
|
|
560
|
+
cause,
|
|
561
|
+
Context.make(Cause.StackTrace, {
|
|
562
|
+
name,
|
|
563
|
+
stack: callSite,
|
|
564
|
+
parent:
|
|
565
|
+
Context.getOrUndefined(Cause.annotations(cause), Cause.StackTrace) ??
|
|
566
|
+
fiber.getRef(References.CurrentStackFrame),
|
|
567
|
+
}),
|
|
568
|
+
{ overwrite: true },
|
|
569
|
+
),
|
|
570
|
+
),
|
|
571
|
+
);
|
|
572
|
+
};
|
|
573
|
+
|
|
461
574
|
/**
|
|
462
575
|
* The span facts an operation's calls carry (name, kind, fixed attributes,
|
|
463
576
|
* input rules), as the runtime builds them from its generated config. For
|
|
464
577
|
* tests and tooling.
|
|
465
578
|
*/
|
|
466
579
|
export const describeOperation = (op: unknown): Telemetry.OperationInfo =>
|
|
467
|
-
|
|
580
|
+
prepare(stateOf(op)).info;
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* The HTTP request a call to `op` with `input` would send, built by the
|
|
584
|
+
* operation's protocol (`Protocol.encode`) and not sent: no retry, no span,
|
|
585
|
+
* no response. It fails as the call would before sending (bad input,
|
|
586
|
+
* credentials, endpoint), or with {@link SdkError} when the operation can't
|
|
587
|
+
* be loaded; the operation's type does not tell those failures from a
|
|
588
|
+
* response's, so the error is `unknown`. Presigning builds on it.
|
|
589
|
+
*/
|
|
590
|
+
export const encodeRequest = <I, O, E, R>(
|
|
591
|
+
op: OperationMethod<I, O, E, R>,
|
|
592
|
+
input: I,
|
|
593
|
+
): Effect.Effect<HttpClientRequest.HttpClientRequest, unknown, R> =>
|
|
594
|
+
Effect.suspend(() => {
|
|
595
|
+
const state = stateOf(op) as OperationState | undefined;
|
|
596
|
+
if (state === undefined) return Effect.fail(new SdkError({ message: "not an operation" }));
|
|
597
|
+
const prepared = prepareOr(state);
|
|
598
|
+
if (prepared instanceof SdkError) return Effect.fail(prepared);
|
|
599
|
+
const encode = (service: Protocol<any, any>) =>
|
|
600
|
+
Effect.catchCause(
|
|
601
|
+
Effect.suspend(() =>
|
|
602
|
+
service.encode({
|
|
603
|
+
input: withIdempotencyTokens(prepared, input),
|
|
604
|
+
inputAst: prepared.inputAst,
|
|
605
|
+
config: prepared.cfg,
|
|
606
|
+
}),
|
|
607
|
+
),
|
|
608
|
+
(cause) => failed(state, cause),
|
|
609
|
+
);
|
|
610
|
+
if (prepared.service !== undefined) return encode(prepared.service);
|
|
611
|
+
return Effect.flatMap(protocolOf(prepared), encode);
|
|
612
|
+
});
|
|
468
613
|
|
|
469
614
|
const defineState = (op: unknown, state: OperationState): OperationState => {
|
|
470
615
|
Object.defineProperty(op, stateKey, { value: state });
|
|
@@ -473,7 +618,7 @@ const defineState = (op: unknown, state: OperationState): OperationState => {
|
|
|
473
618
|
|
|
474
619
|
const runWith = (
|
|
475
620
|
p: Prepared,
|
|
476
|
-
protocol:
|
|
621
|
+
protocol: Protocol<any, any>,
|
|
477
622
|
input: unknown,
|
|
478
623
|
): Effect.Effect<any, any, any> => {
|
|
479
624
|
if (protocol.call !== undefined) return protocol.call(p, input);
|
|
@@ -482,7 +627,7 @@ const runWith = (
|
|
|
482
627
|
const context = fiber.context as Context.Context<any>;
|
|
483
628
|
const client = Context.get(context, HttpClient.HttpClient);
|
|
484
629
|
return Effect.flatMap(protocol.encode({ input, inputAst, config: cfg }), (request) =>
|
|
485
|
-
Effect.flatMap(
|
|
630
|
+
Effect.flatMap(Send.send(context, client, request), (response) =>
|
|
486
631
|
protocol.decode({ response, outputAst, errors, config: cfg }),
|
|
487
632
|
),
|
|
488
633
|
);
|
|
@@ -490,29 +635,27 @@ const runWith = (
|
|
|
490
635
|
};
|
|
491
636
|
|
|
492
637
|
const callUntraced = (
|
|
493
|
-
|
|
638
|
+
p: Prepared,
|
|
494
639
|
input: unknown,
|
|
495
640
|
debug: Telemetry.DebugHooks | undefined,
|
|
496
641
|
): Effect.Effect<any, any, any> => {
|
|
497
|
-
// Warm path: the operation
|
|
498
|
-
//
|
|
499
|
-
|
|
500
|
-
|
|
642
|
+
// Warm path: the operation has its protocol, so the call is whatever the
|
|
643
|
+
// protocol returns, with no wrapper effects around it.
|
|
644
|
+
if (p.service !== undefined) {
|
|
645
|
+
return applyRetry(runWith(p, p.service, withIdempotencyTokens(p, input)), p.cfg, debug);
|
|
646
|
+
}
|
|
501
647
|
return Effect.suspend(() => {
|
|
502
|
-
const
|
|
503
|
-
|
|
504
|
-
|
|
648
|
+
const callInput = withIdempotencyTokens(p, input);
|
|
649
|
+
// The retry wraps only `runWith`, as on the warm path, so a protocol's
|
|
650
|
+
// `call` runs once per call.
|
|
651
|
+
return Effect.flatMap(
|
|
505
652
|
debug === undefined
|
|
506
|
-
?
|
|
507
|
-
: debug.phase("distilled.setup",
|
|
653
|
+
? protocolOf(p)
|
|
654
|
+
: debug.phase("distilled.setup", protocolOf(p), {
|
|
508
655
|
"distilled.setup.first_call": true,
|
|
509
656
|
}),
|
|
510
|
-
(
|
|
511
|
-
const service = (prepared.service = Context.get(protocolCtx, Protocol));
|
|
512
|
-
return runWith(prepared, service, input);
|
|
513
|
-
},
|
|
657
|
+
(service) => applyRetry(runWith(p, service, callInput), p.cfg, debug),
|
|
514
658
|
);
|
|
515
|
-
return applyRetry(call, prepared.cfg.retry, debug);
|
|
516
659
|
});
|
|
517
660
|
};
|
|
518
661
|
|
|
@@ -520,6 +663,10 @@ const callUntraced = (
|
|
|
520
663
|
* BRAINSTORM: the call in its operation span (recorded only when an
|
|
521
664
|
* exporter is installed), whose stack frame is the caller (captured here,
|
|
522
665
|
* synchronously, while the caller is still on the JS stack).
|
|
666
|
+
*
|
|
667
|
+
* The one `catchCause` around the call is where every failed call ends up
|
|
668
|
+
* ({@link failed}). The whole call runs under it, the part that runs before
|
|
669
|
+
* any Effect of the protocol does included.
|
|
523
670
|
*/
|
|
524
671
|
const callOperation = (
|
|
525
672
|
state: OperationState,
|
|
@@ -527,20 +674,23 @@ const callOperation = (
|
|
|
527
674
|
entry: Function,
|
|
528
675
|
): Effect.Effect<any, any, any> => {
|
|
529
676
|
const callSite = Telemetry.captureCallSite(entry);
|
|
530
|
-
return Effect.
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
677
|
+
return Effect.catchCause(
|
|
678
|
+
Effect.withFiber((fiber) => {
|
|
679
|
+
const debug = fiber.getRef(Telemetry.Debug);
|
|
680
|
+
const prepared = prepareOr(state);
|
|
681
|
+
if (prepared instanceof SdkError) return Effect.fail(prepared);
|
|
682
|
+
return Telemetry.operationSpan(
|
|
683
|
+
fiber,
|
|
684
|
+
debug,
|
|
685
|
+
prepared.info,
|
|
686
|
+
callSite,
|
|
687
|
+
input,
|
|
688
|
+
(sent) => callUntraced(prepared, sent, debug),
|
|
689
|
+
debug === undefined ? undefined : { input: prepared.inputAst, output: prepared.outputAst },
|
|
690
|
+
);
|
|
691
|
+
}),
|
|
692
|
+
(cause) => failed(state, cause, callSite),
|
|
693
|
+
);
|
|
544
694
|
};
|
|
545
695
|
|
|
546
696
|
/**
|
|
@@ -609,18 +759,36 @@ const makeOperation = (state: OperationState, proto: object): any => {
|
|
|
609
759
|
return Object.setPrototypeOf(fn, proto);
|
|
610
760
|
};
|
|
611
761
|
|
|
762
|
+
/**
|
|
763
|
+
* What an operation `make` builds can fail with: its own errors, its
|
|
764
|
+
* service's, its protocol's, the HTTP client's, and {@link SdkError}, which
|
|
765
|
+
* `make` itself fails with when the operation's config can't be loaded or
|
|
766
|
+
* its protocol can't stream.
|
|
767
|
+
*/
|
|
768
|
+
export type MadeError<
|
|
769
|
+
E extends readonly ApiErrorClass[],
|
|
770
|
+
SE extends readonly ApiErrorClass[],
|
|
771
|
+
PE,
|
|
772
|
+
> =
|
|
773
|
+
| InstanceType<E[number]>
|
|
774
|
+
| InstanceType<SE[number]>
|
|
775
|
+
| PE
|
|
776
|
+
| HttpClientError.HttpClientError
|
|
777
|
+
| SdkError;
|
|
778
|
+
|
|
612
779
|
export function make<
|
|
613
780
|
I extends S.Top,
|
|
614
781
|
O extends S.Top,
|
|
615
782
|
PE,
|
|
616
783
|
PR,
|
|
617
784
|
const E extends readonly ApiErrorClass[] = readonly [],
|
|
785
|
+
const SE extends readonly ApiErrorClass[] = readonly [],
|
|
618
786
|
>(
|
|
619
|
-
configFn: () => OperationConfig<I, O, PE, PR, E>,
|
|
787
|
+
configFn: () => OperationConfig<I, O, PE, PR, E, SE>,
|
|
620
788
|
): OperationMethod<
|
|
621
789
|
S.Schema.Type<I>,
|
|
622
790
|
S.Schema.Type<O>,
|
|
623
|
-
|
|
791
|
+
MadeError<E, SE, PE>,
|
|
624
792
|
PR | HttpClient.HttpClient
|
|
625
793
|
> {
|
|
626
794
|
return makeOperation({ configFn }, OperationProto);
|
|
@@ -685,13 +853,23 @@ export interface PaginatedOperationConfig<
|
|
|
685
853
|
PE,
|
|
686
854
|
PR,
|
|
687
855
|
E extends readonly ApiErrorClass[] = readonly ApiErrorClass[],
|
|
688
|
-
|
|
856
|
+
SE extends readonly ApiErrorClass[] = readonly ApiErrorClass[],
|
|
857
|
+
> extends OperationConfig<I, O, PE, PR, E, SE> {
|
|
689
858
|
/** How to advance between pages (mirrors the `smithy.api#paginated` trait). */
|
|
690
859
|
pagination: Pagination.PaginatedTrait;
|
|
691
860
|
}
|
|
692
861
|
|
|
693
862
|
/** `.pages` / `.items` over any call function (the operation itself or a context-bound one). */
|
|
694
863
|
const streamsOf = (state: OperationState, call: (input: any) => Effect.Effect<any, any, any>) => {
|
|
864
|
+
// The defect boundary of a stream of pages (see `failed`): each page is a
|
|
865
|
+
// call with its own, and this one is for the code that runs between two
|
|
866
|
+
// pages (the strategy that reads the next token, the items of a page).
|
|
867
|
+
// Each pull runs under the handler: `Stream.catchCause` would do the same
|
|
868
|
+
// and adds about 300 bytes to every bundle with a paginated operation.
|
|
869
|
+
const guarded = (make: () => Stream.Stream<any, any, any>): Stream.Stream<any, any, any> =>
|
|
870
|
+
Stream.transformPull(Stream.suspend(make), (pull) =>
|
|
871
|
+
Effect.succeed(Effect.catchCause(pull, (cause) => failed(state, cause))),
|
|
872
|
+
);
|
|
695
873
|
const pagesOf = (prepared: Prepared, input: Record<string, unknown>) => {
|
|
696
874
|
const cfg = prepared.cfg as PaginatedOperationConfig<any, any, any, any> & {
|
|
697
875
|
strategy?: Pagination.PaginationStrategy;
|
|
@@ -700,21 +878,23 @@ const streamsOf = (state: OperationState, call: (input: any) => Effect.Effect<an
|
|
|
700
878
|
const paginate = state.strategy ?? cfg.strategy ?? Pagination.paginateWithDefaults;
|
|
701
879
|
return paginate(call, input, cfg.pagination);
|
|
702
880
|
};
|
|
703
|
-
const pages = (input: Record<string, unknown>): Stream.Stream<any, any, any> =>
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
881
|
+
const pages = (input: Record<string, unknown>): Stream.Stream<any, any, any> =>
|
|
882
|
+
guarded(() => {
|
|
883
|
+
const prepared = prepareOr(state);
|
|
884
|
+
return prepared instanceof SdkError ? Stream.fail(prepared) : pagesOf(prepared, input);
|
|
885
|
+
});
|
|
886
|
+
const items = (input: Record<string, unknown>): Stream.Stream<any, any, any> =>
|
|
887
|
+
guarded(() => {
|
|
888
|
+
const prepared = prepareOr(state);
|
|
889
|
+
if (prepared instanceof SdkError) return Stream.fail(prepared);
|
|
890
|
+
const p = (prepared.cfg as PaginatedOperationConfig<any, any, any, any>).pagination;
|
|
891
|
+
if (!p.items) return pagesOf(prepared, input);
|
|
892
|
+
// A bare-array response's schema is its sole member's value
|
|
893
|
+
// (`RawResponseRoot`), so the member `items` names is the page itself.
|
|
894
|
+
return getAnn(prepared.outputAst, rawResponseRootSymbol) !== undefined
|
|
895
|
+
? Pagination.extractArrayItems(pagesOf(prepared, input))
|
|
896
|
+
: Pagination.extractItems(pagesOf(prepared, input), p.items);
|
|
897
|
+
});
|
|
718
898
|
return { pages, items };
|
|
719
899
|
};
|
|
720
900
|
|
|
@@ -762,13 +942,14 @@ export function makePaginated<
|
|
|
762
942
|
PE,
|
|
763
943
|
PR,
|
|
764
944
|
const E extends readonly ApiErrorClass[] = readonly [],
|
|
945
|
+
const SE extends readonly ApiErrorClass[] = readonly [],
|
|
765
946
|
>(
|
|
766
|
-
configFn: () => PaginatedOperationConfig<I, O, PE, PR, E>,
|
|
947
|
+
configFn: () => PaginatedOperationConfig<I, O, PE, PR, E, SE>,
|
|
767
948
|
strategy?: Pagination.PaginationStrategy,
|
|
768
949
|
): PaginatedOperationMethod<
|
|
769
950
|
S.Schema.Type<I>,
|
|
770
951
|
S.Schema.Type<O>,
|
|
771
|
-
|
|
952
|
+
MadeError<E, SE, PE>,
|
|
772
953
|
PR | HttpClient.HttpClient
|
|
773
954
|
> {
|
|
774
955
|
return makeOperation(strategy ? { configFn, strategy } : { configFn }, PaginatedOperationProto);
|
|
@@ -808,28 +989,35 @@ export type StreamingOperationMethod<I, O, E, R, Item = StreamedItem<O>> = Effec
|
|
|
808
989
|
StreamingCall<I, O, E, R, Item> &
|
|
809
990
|
OperationSchemas<I, O>;
|
|
810
991
|
|
|
992
|
+
/** The `.stream()` of an operation its protocol can't stream: fails with {@link SdkError}. */
|
|
993
|
+
export const cannotStream = (
|
|
994
|
+
operation: Pick<PreparedOperation, "config">,
|
|
995
|
+
): Stream.Stream<never, SdkError> =>
|
|
996
|
+
Stream.fail(
|
|
997
|
+
new SdkError({
|
|
998
|
+
message: `${operation.config.operationName ?? "this operation"}: its protocol can't stream`,
|
|
999
|
+
}),
|
|
1000
|
+
);
|
|
1001
|
+
|
|
811
1002
|
/** `.stream(input)` over an operation's protocol. */
|
|
812
1003
|
const streamCall =
|
|
813
1004
|
(state: OperationState) =>
|
|
814
1005
|
(input: unknown): Stream.Stream<any, any, any> =>
|
|
815
|
-
Stream.
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
:
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
run((prepared.service = Context.get(ctx, Protocol))),
|
|
831
|
-
);
|
|
832
|
-
}),
|
|
1006
|
+
Stream.catchCause(
|
|
1007
|
+
Stream.unwrap(
|
|
1008
|
+
Effect.suspend(() => {
|
|
1009
|
+
const prepared = prepareOr(state);
|
|
1010
|
+
if (prepared instanceof SdkError) return Effect.succeed(Stream.fail(prepared));
|
|
1011
|
+
const fixed = prepared.cfg.streamInput;
|
|
1012
|
+
const sent = fixed === undefined ? input : { ...(input as object), ...fixed };
|
|
1013
|
+
const run = (service: Protocol<any, any>) =>
|
|
1014
|
+
service.stream === undefined ? cannotStream(prepared) : service.stream(prepared, sent);
|
|
1015
|
+
return prepared.service !== undefined
|
|
1016
|
+
? Effect.succeed(run(prepared.service))
|
|
1017
|
+
: Effect.map(protocolOf(prepared), run);
|
|
1018
|
+
}),
|
|
1019
|
+
),
|
|
1020
|
+
(cause) => Stream.fromEffect(failed(state, cause)),
|
|
833
1021
|
);
|
|
834
1022
|
|
|
835
1023
|
const StreamingOperationProto: object = /*@__PURE__*/ Object.create(OperationProto, {
|
|
@@ -863,7 +1051,11 @@ const StreamingOperationProto: object = /*@__PURE__*/ Object.create(OperationPro
|
|
|
863
1051
|
/**
|
|
864
1052
|
* Like {@link make}, for a server-streaming RPC whose output is `{ items }`:
|
|
865
1053
|
* calling it collects the stream, and `.stream(input)` emits each message as
|
|
866
|
-
* the protocol receives it (see {@link
|
|
1054
|
+
* the protocol receives it (see {@link Protocol.stream}).
|
|
1055
|
+
*
|
|
1056
|
+
* An operation that streams only when its input asks keeps its own output:
|
|
1057
|
+
* its config sets `items` (one frame) and `streamInput` (what `.stream()`
|
|
1058
|
+
* sends), and the plain call is a normal call.
|
|
867
1059
|
*/
|
|
868
1060
|
export function makeStreaming<
|
|
869
1061
|
I extends S.Top,
|
|
@@ -871,12 +1063,13 @@ export function makeStreaming<
|
|
|
871
1063
|
PE,
|
|
872
1064
|
PR,
|
|
873
1065
|
const E extends readonly ApiErrorClass[] = readonly [],
|
|
1066
|
+
const SE extends readonly ApiErrorClass[] = readonly [],
|
|
874
1067
|
>(
|
|
875
|
-
configFn: () => OperationConfig<I, O, PE, PR, E>,
|
|
1068
|
+
configFn: () => OperationConfig<I, O, PE, PR, E, SE>,
|
|
876
1069
|
): StreamingOperationMethod<
|
|
877
1070
|
S.Schema.Type<I>,
|
|
878
1071
|
S.Schema.Type<O>,
|
|
879
|
-
|
|
1072
|
+
MadeError<E, SE, PE>,
|
|
880
1073
|
PR | HttpClient.HttpClient
|
|
881
1074
|
> {
|
|
882
1075
|
return makeOperation({ configFn }, StreamingOperationProto);
|