@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/protocol-grpc.ts
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Proto SDKs are generated as REST-shaped operations: each RPC is
|
|
5
5
|
* `POST /<package>.<Service>/<Method>` with a proto3 JSON body. Servers that
|
|
6
|
-
* accept JSON (Connect, grpc-gateway) use {@link
|
|
6
|
+
* accept JSON (Connect, grpc-gateway) use {@link restProtocol} directly.
|
|
7
7
|
* Servers that only speak gRPC use this protocol, which builds the request
|
|
8
|
-
* with the REST protocol and speaks gRPC on the wire
|
|
8
|
+
* with the REST protocol and speaks gRPC on the wire. `grpcProtocol(options)`
|
|
9
|
+
* builds a core protocol (core/protocol):
|
|
9
10
|
*
|
|
10
11
|
* - **Request**: the proto3 JSON body is transcoded to one protobuf message
|
|
11
12
|
* ({@link encodeMessage}) in one gRPC frame, sent as `application/grpc`.
|
|
@@ -13,6 +14,10 @@
|
|
|
13
14
|
* `GrpcTransport` reference holds, or the caller's `HttpClient` when it
|
|
14
15
|
* holds none (see `@distilled.cloud/core/grpc-transport`). Plain `fetch`
|
|
15
16
|
* works where a proxy bridges HTTP/1.1 to gRPC (envoy).
|
|
17
|
+
* - **gRPC-Web**: with `web: true` the request is sent as
|
|
18
|
+
* `application/grpc-web+proto`, which servers such as `tonic-web` answer
|
|
19
|
+
* over HTTP/1.1. The reply's trailers arrive as the last frame of its body,
|
|
20
|
+
* so plain `fetch` sees every status.
|
|
16
21
|
* - **Status**: the call's `grpc-status` is read from the trailers, or from
|
|
17
22
|
* the headers of a trailers-only reply. A transport that can't see
|
|
18
23
|
* trailers (`fetch`) leaves only the headers; a reply with message frames
|
|
@@ -41,23 +46,36 @@ import * as Duration from "effect/Duration";
|
|
|
41
46
|
import * as Effect from "effect/Effect";
|
|
42
47
|
import type * as HttpBody from "effect/http/HttpBody";
|
|
43
48
|
import * as HttpClient from "effect/http/HttpClient";
|
|
49
|
+
import type * as HttpClientError from "effect/http/HttpClientError";
|
|
44
50
|
import * as HttpClientRequest from "effect/http/HttpClientRequest";
|
|
45
51
|
import * as HttpClientResponse from "effect/http/HttpClientResponse";
|
|
46
|
-
import * as Layer from "effect/Layer";
|
|
47
52
|
import * as Stream from "effect/Stream";
|
|
48
|
-
import * as API from "./api.ts";
|
|
49
53
|
import { GrpcTransport, hasTrailers, trailersOf } from "./grpc-transport.ts";
|
|
54
|
+
import * as MaxMessageBytes from "./max-message-bytes.ts";
|
|
50
55
|
import {
|
|
51
56
|
decodeMessage,
|
|
52
57
|
encodeMessage,
|
|
53
58
|
frame,
|
|
54
59
|
FrameReader,
|
|
60
|
+
FrameTooLargeError,
|
|
55
61
|
type ProtoDescriptors,
|
|
56
62
|
} from "./protobuf.ts";
|
|
57
63
|
import { InvalidInput, matchTypedError } from "./protocol-http.ts";
|
|
58
|
-
import {
|
|
64
|
+
import {
|
|
65
|
+
restProtocolMethods,
|
|
66
|
+
type RestParseErrorInfo,
|
|
67
|
+
type RestProtocolError,
|
|
68
|
+
type RestProtocolOptions,
|
|
69
|
+
} from "./protocol-rest.ts";
|
|
70
|
+
import type {
|
|
71
|
+
DeclaredError,
|
|
72
|
+
DecodeArgs as ProtocolDecodeArgs,
|
|
73
|
+
EncodeArgs,
|
|
74
|
+
Operation,
|
|
75
|
+
Protocol,
|
|
76
|
+
} from "./protocol.ts";
|
|
59
77
|
import { SdkError } from "./sdk-error.ts";
|
|
60
|
-
import * as
|
|
78
|
+
import * as Send from "./send.ts";
|
|
61
79
|
|
|
62
80
|
/** gRPC status code -> name. */
|
|
63
81
|
export const GRPC_STATUS_NAMES: Readonly<Record<number, string>> = {
|
|
@@ -114,7 +132,39 @@ export const Deadline = /*@__PURE__*/ Context.Reference<Duration.Input | undefin
|
|
|
114
132
|
{ defaultValue: () => undefined },
|
|
115
133
|
);
|
|
116
134
|
|
|
117
|
-
|
|
135
|
+
/**
|
|
136
|
+
* What this module's own code fails with, whatever the options: the errors
|
|
137
|
+
* every gRPC protocol declares. They are the REST protocol's: a message the
|
|
138
|
+
* descriptors can't encode (`InvalidInput`), a request or reply gRPC can't
|
|
139
|
+
* use (`SdkError`), the transport, a body or its trailers
|
|
140
|
+
* (`HttpClientError`), and a 5xx status that neither the operation nor its
|
|
141
|
+
* service declares (`InternalServerError`).
|
|
142
|
+
*/
|
|
143
|
+
export type GrpcProtocolError = RestProtocolError;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Options of {@link grpcProtocol}: the REST protocol's (see
|
|
147
|
+
* {@link RestProtocolOptions} for the type parameters) and gRPC's own.
|
|
148
|
+
* `StatusError` is what `statusError` returns.
|
|
149
|
+
*/
|
|
150
|
+
export interface GrpcProtocolOptions<
|
|
151
|
+
Credentials,
|
|
152
|
+
CredentialsError,
|
|
153
|
+
CredentialsRequirements,
|
|
154
|
+
RouteError,
|
|
155
|
+
ClassifiedError,
|
|
156
|
+
UnknownError,
|
|
157
|
+
ParseError,
|
|
158
|
+
StatusError,
|
|
159
|
+
> extends RestProtocolOptions<
|
|
160
|
+
Credentials,
|
|
161
|
+
CredentialsError,
|
|
162
|
+
CredentialsRequirements,
|
|
163
|
+
RouteError,
|
|
164
|
+
ClassifiedError,
|
|
165
|
+
UnknownError,
|
|
166
|
+
ParseError
|
|
167
|
+
> {
|
|
118
168
|
/** The SDK's generated descriptors (`src/descriptors.ts`). */
|
|
119
169
|
readonly descriptors: ProtoDescriptors;
|
|
120
170
|
/**
|
|
@@ -125,7 +175,19 @@ export interface GrpcProtocolOptions<C> extends RestProtocolOptions<C> {
|
|
|
125
175
|
* (`INVALID_ARGUMENT`, `FAILED_PRECONDITION` and `OUT_OF_RANGE` are all
|
|
126
176
|
* `BadRequest`).
|
|
127
177
|
*/
|
|
128
|
-
readonly statusError?: (info: StatusInfo) =>
|
|
178
|
+
readonly statusError?: (info: StatusInfo) => StatusError;
|
|
179
|
+
/**
|
|
180
|
+
* Send gRPC-Web (`application/grpc-web+proto`) instead of gRPC, for a
|
|
181
|
+
* server that answers HTTP/1.1 only that way (`tonic-web`). The reply
|
|
182
|
+
* carries its trailers in its body, so `fetch` sees every status.
|
|
183
|
+
*/
|
|
184
|
+
readonly web?: boolean;
|
|
185
|
+
/**
|
|
186
|
+
* The largest message the server may send, when neither the call's
|
|
187
|
+
* `maxMessageBytes` nor the `MaxMessageBytes` layer sets one. Default
|
|
188
|
+
* `DEFAULT_MAX_MESSAGE_BYTES` (16 MiB). See `max-message-bytes.ts`.
|
|
189
|
+
*/
|
|
190
|
+
readonly maxMessageBytes?: number;
|
|
129
191
|
}
|
|
130
192
|
|
|
131
193
|
/** A non-OK gRPC status, as `statusError` receives it. */
|
|
@@ -184,11 +246,10 @@ const textDecoder = /*@__PURE__*/ new TextDecoder();
|
|
|
184
246
|
|
|
185
247
|
/**
|
|
186
248
|
* The SDK can't make the call (a broken descriptor table, or a REST
|
|
187
|
-
* encode/decode result gRPC can't use).
|
|
188
|
-
* `never`; SdkError is part of every operation's declared error channel.
|
|
249
|
+
* encode/decode result gRPC can't use).
|
|
189
250
|
*/
|
|
190
|
-
const sdkFailure = (message: string): Effect.Effect<never> =>
|
|
191
|
-
Effect.fail(new SdkError({ message }))
|
|
251
|
+
const sdkFailure = (message: string): Effect.Effect<never, SdkError> =>
|
|
252
|
+
Effect.fail(new SdkError({ message }));
|
|
192
253
|
|
|
193
254
|
const methodPath = (url: string): string => {
|
|
194
255
|
try {
|
|
@@ -235,6 +296,10 @@ const plainHeaders = (headers: Headers) => {
|
|
|
235
296
|
return out;
|
|
236
297
|
};
|
|
237
298
|
|
|
299
|
+
/** A gRPC-Web reply, whose trailers are the last frame of its body. */
|
|
300
|
+
const isWeb = (headers: Headers): boolean =>
|
|
301
|
+
(headers["content-type"] ?? "").startsWith("application/grpc-web");
|
|
302
|
+
|
|
238
303
|
const statusOf = (value: string | undefined): number | undefined =>
|
|
239
304
|
value === undefined || value === "" ? undefined : Number(value);
|
|
240
305
|
|
|
@@ -258,250 +323,319 @@ const grpcTimeout = (ms: number): string => {
|
|
|
258
323
|
return seconds < 1e8 ? `${seconds}S` : `${Math.min(99_999_999, Math.ceil(seconds / 3600))}H`;
|
|
259
324
|
};
|
|
260
325
|
|
|
261
|
-
type DecodeArgs = Omit<
|
|
326
|
+
type DecodeArgs = Omit<ProtocolDecodeArgs, "response">;
|
|
327
|
+
|
|
328
|
+
/** The options {@link grpcMethods} reads; the REST methods read the rest. */
|
|
329
|
+
interface GrpcOptions<ParseError, StatusError> {
|
|
330
|
+
readonly descriptors: ProtoDescriptors;
|
|
331
|
+
readonly statusError?: (info: StatusInfo) => StatusError;
|
|
332
|
+
readonly web?: boolean;
|
|
333
|
+
readonly maxMessageBytes?: number;
|
|
334
|
+
readonly parseError: (info: RestParseErrorInfo) => ParseError;
|
|
335
|
+
}
|
|
262
336
|
|
|
263
337
|
/**
|
|
264
|
-
*
|
|
338
|
+
* The gRPC methods over the REST protocol `rest`. See the module
|
|
265
339
|
* documentation for the wire behavior.
|
|
266
340
|
*/
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
);
|
|
302
|
-
})
|
|
303
|
-
|
|
304
|
-
/** A gRPC status as the operation's typed error. */
|
|
305
|
-
const failWith = (
|
|
306
|
-
args: DecodeArgs,
|
|
307
|
-
request: HttpClientRequest.HttpClientRequest,
|
|
308
|
-
headers: Headers,
|
|
309
|
-
status: number,
|
|
310
|
-
message: string | undefined,
|
|
311
|
-
details: ReadonlyArray<unknown> = [],
|
|
312
|
-
): Effect.Effect<never> => {
|
|
313
|
-
const httpStatus = GRPC_HTTP_STATUS[status] ?? 500;
|
|
314
|
-
const body = {
|
|
315
|
-
code: status,
|
|
316
|
-
status: GRPC_STATUS_NAMES[status] ?? String(status),
|
|
317
|
-
message: messageOf(message, status),
|
|
318
|
-
details,
|
|
319
|
-
};
|
|
320
|
-
if (options.statusError !== undefined) {
|
|
321
|
-
const plain = plainHeaders(headers);
|
|
322
|
-
const typed = matchTypedError(
|
|
323
|
-
args.errors,
|
|
324
|
-
httpStatus,
|
|
325
|
-
[{ code: status, message: body.message }],
|
|
326
|
-
{ body, headers: plain },
|
|
327
|
-
);
|
|
341
|
+
const grpcMethods = <RestError, Requirements, ParseError, StatusError>(
|
|
342
|
+
rest: Protocol<RestError, Requirements>,
|
|
343
|
+
options: GrpcOptions<ParseError, StatusError>,
|
|
344
|
+
): Protocol<RestError | ParseError | StatusError | GrpcProtocolError, Requirements> => {
|
|
345
|
+
/** What building the request fails with. */
|
|
346
|
+
type EncodeError = RestError | InvalidInput | SdkError;
|
|
347
|
+
/** Everything a call fails with. */
|
|
348
|
+
type Failure =
|
|
349
|
+
| DeclaredError
|
|
350
|
+
| RestError
|
|
351
|
+
| ParseError
|
|
352
|
+
| StatusError
|
|
353
|
+
| InvalidInput
|
|
354
|
+
| SdkError
|
|
355
|
+
| HttpClientError.HttpClientError;
|
|
356
|
+
|
|
357
|
+
const d = options.descriptors;
|
|
358
|
+
const methodOf = (url: string) => d.methods[methodPath(url)];
|
|
359
|
+
|
|
360
|
+
/** The REST request as one gRPC frame of protobuf. */
|
|
361
|
+
const encode = (
|
|
362
|
+
args: EncodeArgs,
|
|
363
|
+
): Effect.Effect<HttpClientRequest.HttpClientRequest, EncodeError, Requirements> =>
|
|
364
|
+
Effect.flatMap(
|
|
365
|
+
rest.encode(args),
|
|
366
|
+
(request): Effect.Effect<HttpClientRequest.HttpClientRequest, InvalidInput | SdkError> => {
|
|
367
|
+
const method = methodOf(request.url);
|
|
368
|
+
if (!method) return sdkFailure(`no gRPC descriptor for ${request.url}`);
|
|
369
|
+
const body = request.body;
|
|
370
|
+
if (body._tag !== "Empty" && body._tag !== "Uint8Array" && body._tag !== "Raw") {
|
|
371
|
+
return sdkFailure(`gRPC requests need a JSON body, got ${body._tag}`);
|
|
372
|
+
}
|
|
373
|
+
let payload: Uint8Array;
|
|
374
|
+
try {
|
|
375
|
+
payload = encodeMessage(d, method[0], jsonBody(body) ?? {});
|
|
376
|
+
} catch (cause) {
|
|
328
377
|
return Effect.fail(
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
378
|
+
new InvalidInput({
|
|
379
|
+
member: method[0],
|
|
380
|
+
message: cause instanceof Error ? cause.message : String(cause),
|
|
381
|
+
}),
|
|
382
|
+
);
|
|
332
383
|
}
|
|
333
|
-
return Effect.
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
384
|
+
return Effect.succeed(
|
|
385
|
+
options.web
|
|
386
|
+
? request.pipe(
|
|
387
|
+
HttpClientRequest.bodyUint8Array(frame(payload), "application/grpc-web+proto"),
|
|
388
|
+
HttpClientRequest.setHeader("x-grpc-web", "1"),
|
|
389
|
+
)
|
|
390
|
+
: request.pipe(
|
|
391
|
+
HttpClientRequest.bodyUint8Array(frame(payload), "application/grpc"),
|
|
392
|
+
HttpClientRequest.setHeader("te", "trailers"),
|
|
393
|
+
),
|
|
339
394
|
);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
395
|
+
},
|
|
396
|
+
);
|
|
397
|
+
|
|
398
|
+
/** A gRPC status as the operation's typed error. */
|
|
399
|
+
const failWith = (
|
|
400
|
+
args: DecodeArgs,
|
|
401
|
+
request: HttpClientRequest.HttpClientRequest,
|
|
402
|
+
headers: Headers,
|
|
403
|
+
status: number,
|
|
404
|
+
message: string | undefined,
|
|
405
|
+
details: ReadonlyArray<unknown> = [],
|
|
406
|
+
): Effect.Effect<never, Failure, Requirements> => {
|
|
407
|
+
const httpStatus = GRPC_HTTP_STATUS[status] ?? 500;
|
|
408
|
+
const body = {
|
|
409
|
+
code: status,
|
|
410
|
+
status: GRPC_STATUS_NAMES[status] ?? String(status),
|
|
411
|
+
message: messageOf(message, status),
|
|
412
|
+
details,
|
|
413
|
+
};
|
|
414
|
+
if (options.statusError !== undefined) {
|
|
415
|
+
const plain = plainHeaders(headers);
|
|
416
|
+
const typed = matchTypedError(
|
|
417
|
+
args.errors,
|
|
418
|
+
httpStatus,
|
|
419
|
+
[{ code: status, message: body.message }],
|
|
420
|
+
{ body, headers: plain },
|
|
421
|
+
);
|
|
422
|
+
return Effect.fail(
|
|
423
|
+
typed ??
|
|
424
|
+
options.statusError({ code: status, message: body.message, details, headers: plain }),
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
return Effect.flatMap(
|
|
428
|
+
rest.decode({
|
|
429
|
+
...args,
|
|
430
|
+
response: jsonResponse(request, httpStatus, plainHeaders(headers), body),
|
|
431
|
+
}),
|
|
432
|
+
() => sdkFailure(`gRPC status ${status} decoded as a success`),
|
|
433
|
+
);
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* The call's failure, if any, once its body has ended. The status is in
|
|
438
|
+
* the trailers, or in the headers of a trailers-only reply (one with no
|
|
439
|
+
* body); a status in the headers of a reply with a body is ignored. A
|
|
440
|
+
* transport that reads trailers must deliver one, and so must a
|
|
441
|
+
* gRPC-Web reply, in its trailer frame; over `fetch`, which can't read
|
|
442
|
+
* trailers, a gRPC reply with messages and no status is OK.
|
|
443
|
+
*/
|
|
444
|
+
const endOf = (
|
|
445
|
+
args: DecodeArgs,
|
|
446
|
+
response: HttpClientResponse.HttpClientResponse,
|
|
447
|
+
trailers: Readonly<Record<string, string>>,
|
|
448
|
+
sawBody: boolean,
|
|
449
|
+
): Effect.Effect<void, Failure, Requirements> => {
|
|
450
|
+
const headers = response.headers as Headers;
|
|
451
|
+
const readsTrailers = hasTrailers(response) || isWeb(headers);
|
|
452
|
+
const source =
|
|
453
|
+
trailers["grpc-status"] !== undefined
|
|
454
|
+
? trailers
|
|
455
|
+
: !sawBody || !readsTrailers
|
|
456
|
+
? headers
|
|
457
|
+
: undefined;
|
|
458
|
+
const status = statusOf(source?.["grpc-status"]);
|
|
459
|
+
if (source === undefined || status === undefined) {
|
|
460
|
+
if (readsTrailers) {
|
|
461
|
+
return failWith(
|
|
403
462
|
args,
|
|
404
463
|
response.request,
|
|
405
|
-
|
|
464
|
+
headers,
|
|
406
465
|
13,
|
|
407
|
-
|
|
466
|
+
"server ended the call without a grpc-status",
|
|
408
467
|
);
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
* `statusError`, it maps as gRPC specifies (503 -> UNAVAILABLE, ...).
|
|
414
|
-
*/
|
|
415
|
-
const notGrpc = (
|
|
416
|
-
args: DecodeArgs,
|
|
417
|
-
response: HttpClientResponse.HttpClientResponse,
|
|
418
|
-
): Effect.Effect<never> | undefined => {
|
|
419
|
-
const headers = response.headers as Headers;
|
|
420
|
-
const isGrpc = (headers["content-type"] ?? "").startsWith("application/grpc");
|
|
421
|
-
if (response.status === 200 && isGrpc) return undefined;
|
|
422
|
-
if (response.status !== 200 && options.statusError === undefined) {
|
|
423
|
-
return rest.decode({ ...args, response }) as Effect.Effect<never>;
|
|
424
|
-
}
|
|
425
|
-
if (response.status !== 200) {
|
|
426
|
-
return failWith(
|
|
468
|
+
}
|
|
469
|
+
return sawBody
|
|
470
|
+
? Effect.void
|
|
471
|
+
: failWith(
|
|
427
472
|
args,
|
|
428
473
|
response.request,
|
|
429
474
|
headers,
|
|
430
|
-
|
|
431
|
-
|
|
475
|
+
2,
|
|
476
|
+
"gRPC response carried no message and no grpc-status",
|
|
432
477
|
);
|
|
433
|
-
|
|
434
|
-
|
|
478
|
+
}
|
|
479
|
+
return status === 0
|
|
480
|
+
? Effect.void
|
|
481
|
+
: failWith(
|
|
435
482
|
args,
|
|
436
483
|
response.request,
|
|
437
484
|
headers,
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
};
|
|
442
|
-
|
|
443
|
-
/** A reply value decoded by the operation's output schema. */
|
|
444
|
-
const succeedWith = (
|
|
445
|
-
args: DecodeArgs,
|
|
446
|
-
request: HttpClientRequest.HttpClientRequest,
|
|
447
|
-
headers: Headers,
|
|
448
|
-
value: unknown,
|
|
449
|
-
) =>
|
|
450
|
-
rest.decode({
|
|
451
|
-
...args,
|
|
452
|
-
response: jsonResponse(request, 200, plainHeaders(headers), value),
|
|
453
|
-
});
|
|
454
|
-
|
|
455
|
-
const parseFailure = (body: unknown, cause: unknown): Effect.Effect<never> =>
|
|
456
|
-
Effect.fail(options.parseError({ body, cause })) as Effect.Effect<never>;
|
|
457
|
-
|
|
458
|
-
const deadlineMillis = (context: Context.Context<any>): number | undefined => {
|
|
459
|
-
const deadline = Context.get(context, Deadline);
|
|
460
|
-
return deadline === undefined ? undefined : Duration.toMillis(deadline);
|
|
461
|
-
};
|
|
462
|
-
|
|
463
|
-
/** Send a call through the transport in scope, with the deadline in scope. */
|
|
464
|
-
const send = (
|
|
465
|
-
context: Context.Context<any>,
|
|
466
|
-
operation: API.PreparedOperation,
|
|
467
|
-
input: unknown,
|
|
468
|
-
ms: number | undefined,
|
|
469
|
-
) =>
|
|
470
|
-
Effect.flatMap(
|
|
471
|
-
encode({ input, inputAst: operation.inputAst, config: operation.config }),
|
|
472
|
-
(encoded) => {
|
|
473
|
-
const request =
|
|
474
|
-
ms === undefined
|
|
475
|
-
? encoded
|
|
476
|
-
: HttpClientRequest.setHeader(encoded, "grpc-timeout", grpcTimeout(ms));
|
|
477
|
-
const client =
|
|
478
|
-
Context.get(context, GrpcTransport) ?? Context.get(context, HttpClient.HttpClient);
|
|
479
|
-
return Telemetry.execute(context, client, request);
|
|
480
|
-
},
|
|
485
|
+
status,
|
|
486
|
+
source["grpc-message"],
|
|
487
|
+
detailsOf(source["grpc-status-details-bin"]),
|
|
481
488
|
);
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
/** A malformed frame (compressed, truncated): gRPC's INTERNAL. */
|
|
492
|
+
const badFrame = (
|
|
493
|
+
args: DecodeArgs,
|
|
494
|
+
response: HttpClientResponse.HttpClientResponse,
|
|
495
|
+
cause: unknown,
|
|
496
|
+
) =>
|
|
497
|
+
failWith(
|
|
498
|
+
args,
|
|
499
|
+
response.request,
|
|
500
|
+
response.headers as Headers,
|
|
501
|
+
13,
|
|
502
|
+
cause instanceof Error ? cause.message : String(cause),
|
|
503
|
+
);
|
|
482
504
|
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
+
/**
|
|
506
|
+
* A reply that isn't gRPC: a status other than 200, or another content
|
|
507
|
+
* type. The REST decoder classifies a non-200 by its HTTP status (so a
|
|
508
|
+
* status under 400 is its value); with `statusError`, it maps as gRPC
|
|
509
|
+
* specifies (503 -> UNAVAILABLE, ...).
|
|
510
|
+
*/
|
|
511
|
+
const notGrpc = (
|
|
512
|
+
args: DecodeArgs,
|
|
513
|
+
response: HttpClientResponse.HttpClientResponse,
|
|
514
|
+
): Effect.Effect<unknown, Failure, Requirements> | undefined => {
|
|
515
|
+
const headers = response.headers as Headers;
|
|
516
|
+
const isGrpc = (headers["content-type"] ?? "").startsWith("application/grpc");
|
|
517
|
+
if (response.status === 200 && isGrpc) return undefined;
|
|
518
|
+
if (response.status !== 200 && options.statusError === undefined) {
|
|
519
|
+
return rest.decode({ ...args, response });
|
|
520
|
+
}
|
|
521
|
+
if (response.status !== 200) {
|
|
522
|
+
return failWith(
|
|
523
|
+
args,
|
|
524
|
+
response.request,
|
|
525
|
+
headers,
|
|
526
|
+
HTTP_GRPC_STATUS[response.status] ?? 2,
|
|
527
|
+
`HTTP ${response.status}`,
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
return failWith(
|
|
531
|
+
args,
|
|
532
|
+
response.request,
|
|
533
|
+
headers,
|
|
534
|
+
2,
|
|
535
|
+
`unexpected content-type ${JSON.stringify(headers["content-type"] ?? "")}`,
|
|
536
|
+
);
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
/** A reply value decoded by the operation's output schema. */
|
|
540
|
+
const succeedWith = (
|
|
541
|
+
args: DecodeArgs,
|
|
542
|
+
request: HttpClientRequest.HttpClientRequest,
|
|
543
|
+
headers: Headers,
|
|
544
|
+
value: unknown,
|
|
545
|
+
) =>
|
|
546
|
+
rest.decode({
|
|
547
|
+
...args,
|
|
548
|
+
response: jsonResponse(request, 200, plainHeaders(headers), value),
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
const parseFailure = (body: unknown, cause: unknown): Effect.Effect<never, ParseError> =>
|
|
552
|
+
Effect.fail(options.parseError({ body, cause }));
|
|
553
|
+
|
|
554
|
+
const deadlineMillis = (context: Context.Context<any>): number | undefined => {
|
|
555
|
+
const deadline = Context.get(context, Deadline);
|
|
556
|
+
return deadline === undefined ? undefined : Duration.toMillis(deadline);
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
/** Send a call through the transport in scope, with the deadline in scope. */
|
|
560
|
+
const send = (
|
|
561
|
+
context: Context.Context<any>,
|
|
562
|
+
operation: Operation,
|
|
563
|
+
input: unknown,
|
|
564
|
+
ms: number | undefined,
|
|
565
|
+
) =>
|
|
566
|
+
Effect.flatMap(
|
|
567
|
+
encode({ input, inputAst: operation.inputAst, config: operation.config }),
|
|
568
|
+
(encoded) => {
|
|
569
|
+
const request =
|
|
570
|
+
ms === undefined
|
|
571
|
+
? encoded
|
|
572
|
+
: HttpClientRequest.setHeader(encoded, "grpc-timeout", grpcTimeout(ms));
|
|
573
|
+
const client =
|
|
574
|
+
Context.get(context, GrpcTransport) ?? Context.get(context, HttpClient.HttpClient);
|
|
575
|
+
return Send.send(context, client, request);
|
|
576
|
+
},
|
|
577
|
+
);
|
|
578
|
+
|
|
579
|
+
/** The call's message size limit, or `InvalidInput` for a bad `maxMessageBytes`. */
|
|
580
|
+
const limitOf = (
|
|
581
|
+
context: Context.Context<any>,
|
|
582
|
+
input: unknown,
|
|
583
|
+
): Effect.Effect<number, InvalidInput> => {
|
|
584
|
+
const limit = MaxMessageBytes.resolve(context, input, options.maxMessageBytes);
|
|
585
|
+
return typeof limit === "number"
|
|
586
|
+
? Effect.succeed(limit)
|
|
587
|
+
: Effect.fail(new InvalidInput({ member: "maxMessageBytes", message: limit.invalid }));
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
/** A frame the reader refused: over the size limit (the SDK's parse error), or malformed. */
|
|
591
|
+
const frameFailure = (
|
|
592
|
+
args: DecodeArgs,
|
|
593
|
+
response: HttpClientResponse.HttpClientResponse,
|
|
594
|
+
cause: unknown,
|
|
595
|
+
): Effect.Effect<never, Failure, Requirements> =>
|
|
596
|
+
cause instanceof FrameTooLargeError
|
|
597
|
+
? parseFailure(undefined, cause.message)
|
|
598
|
+
: badFrame(args, response, cause);
|
|
599
|
+
|
|
600
|
+
/** Decode a whole reply (its body read to the end), with the limit in scope. */
|
|
601
|
+
const decode = (args: ProtocolDecodeArgs): Effect.Effect<unknown, Failure, Requirements> =>
|
|
602
|
+
Effect.withFiber((fiber) =>
|
|
603
|
+
Effect.flatMap(limitOf(fiber.context as Context.Context<any>, undefined), (limit) =>
|
|
604
|
+
decodeWith(args, limit),
|
|
605
|
+
),
|
|
606
|
+
);
|
|
607
|
+
|
|
608
|
+
/** Decode a whole reply (its body read to the end). */
|
|
609
|
+
const decodeWith = (
|
|
610
|
+
args: ProtocolDecodeArgs,
|
|
611
|
+
limit: number,
|
|
612
|
+
): Effect.Effect<unknown, Failure, Requirements> => {
|
|
613
|
+
const { response } = args;
|
|
614
|
+
const failed = notGrpc(args, response);
|
|
615
|
+
if (failed) return failed;
|
|
616
|
+
const headers = response.headers as Headers;
|
|
617
|
+
const request = response.request;
|
|
618
|
+
const method = methodOf(request.url);
|
|
619
|
+
if (!method) return sdkFailure(`no gRPC descriptor for ${request.url}`);
|
|
620
|
+
// A body or trailers read that fails (reset or abort mid-body) fails the
|
|
621
|
+
// call with its `HttpClientError`, a protocol error.
|
|
622
|
+
return Effect.flatMap(response.arrayBuffer, (buffer) =>
|
|
623
|
+
Effect.flatMap(
|
|
624
|
+
trailersOf(response),
|
|
625
|
+
(trailers): Effect.Effect<unknown, Failure, Requirements> => {
|
|
626
|
+
const reader = new FrameReader(limit);
|
|
627
|
+
let frames: Uint8Array[];
|
|
628
|
+
try {
|
|
629
|
+
frames = reader.push(new Uint8Array(buffer));
|
|
630
|
+
} catch (cause) {
|
|
631
|
+
return Effect.flatMap(endOf(args, response, trailers, true), () =>
|
|
632
|
+
frameFailure(args, response, cause),
|
|
633
|
+
);
|
|
634
|
+
}
|
|
635
|
+
const all = { ...trailers, ...reader.trailers };
|
|
636
|
+
return Effect.flatMap(
|
|
637
|
+
endOf(args, response, all, buffer.byteLength > 0),
|
|
638
|
+
(): Effect.Effect<unknown, Failure, Requirements> => {
|
|
505
639
|
if (reader.rest !== 0) return badFrame(args, response, "truncated gRPC frame");
|
|
506
640
|
const [, responseType, stream] = method;
|
|
507
641
|
if (!stream && frames.length !== 1) {
|
|
@@ -522,120 +656,156 @@ export const makeGrpcProtocol = <C>(options: GrpcProtocolOptions<C>): Layer.Laye
|
|
|
522
656
|
return parseFailure(buffer, cause);
|
|
523
657
|
}
|
|
524
658
|
return succeedWith(args, request, headers, value);
|
|
525
|
-
}
|
|
526
|
-
}),
|
|
527
|
-
);
|
|
528
|
-
};
|
|
529
|
-
|
|
530
|
-
const deadlineExceeded = (args: DecodeArgs, url: string) =>
|
|
531
|
-
failWith(args, HttpClientRequest.post(url), {}, 4, "Deadline Exceeded");
|
|
532
|
-
|
|
533
|
-
return API.Protocol.of({
|
|
534
|
-
encode,
|
|
535
|
-
decode,
|
|
536
|
-
call: (operation, input) =>
|
|
537
|
-
Effect.withFiber((fiber) => {
|
|
538
|
-
const context = fiber.context as Context.Context<any>;
|
|
539
|
-
const args: DecodeArgs = {
|
|
540
|
-
outputAst: operation.outputAst,
|
|
541
|
-
errors: operation.errors,
|
|
542
|
-
config: operation.config,
|
|
543
|
-
};
|
|
544
|
-
const ms = deadlineMillis(context);
|
|
545
|
-
const run = Effect.flatMap(
|
|
546
|
-
send(
|
|
547
|
-
context,
|
|
548
|
-
operation,
|
|
549
|
-
input,
|
|
550
|
-
ms,
|
|
551
|
-
) as Effect.Effect<HttpClientResponse.HttpClientResponse>,
|
|
552
|
-
(response) => decode({ ...args, response }),
|
|
553
|
-
);
|
|
554
|
-
return ms === undefined
|
|
555
|
-
? run
|
|
556
|
-
: Effect.timeoutOrElse(run, {
|
|
557
|
-
duration: ms,
|
|
558
|
-
orElse: () => deadlineExceeded(args, ""),
|
|
559
|
-
});
|
|
560
|
-
}),
|
|
561
|
-
stream: (operation, input) =>
|
|
562
|
-
Stream.unwrap(
|
|
563
|
-
Effect.withFiber((fiber) => {
|
|
564
|
-
const context = fiber.context as Context.Context<any>;
|
|
565
|
-
const args: DecodeArgs = {
|
|
566
|
-
outputAst: operation.outputAst,
|
|
567
|
-
errors: operation.errors,
|
|
568
|
-
config: operation.config,
|
|
569
|
-
};
|
|
570
|
-
const ms = deadlineMillis(context);
|
|
571
|
-
const messages = Effect.map(
|
|
572
|
-
send(
|
|
573
|
-
context,
|
|
574
|
-
operation,
|
|
575
|
-
input,
|
|
576
|
-
ms,
|
|
577
|
-
) as Effect.Effect<HttpClientResponse.HttpClientResponse>,
|
|
578
|
-
(response) => streamReply(args, response),
|
|
579
|
-
);
|
|
580
|
-
const stream = Stream.unwrap(messages);
|
|
581
|
-
return Effect.succeed(
|
|
582
|
-
ms === undefined
|
|
583
|
-
? stream
|
|
584
|
-
: Stream.interruptWhen(
|
|
585
|
-
stream,
|
|
586
|
-
Effect.flatMap(Effect.sleep(ms), () => deadlineExceeded(args, "")),
|
|
587
|
-
),
|
|
588
|
-
);
|
|
589
|
-
}),
|
|
590
|
-
),
|
|
591
|
-
});
|
|
592
|
-
|
|
593
|
-
/** Each message of a server stream, decoded as it arrives. */
|
|
594
|
-
function streamReply(
|
|
595
|
-
args: DecodeArgs,
|
|
596
|
-
response: HttpClientResponse.HttpClientResponse,
|
|
597
|
-
): Stream.Stream<unknown, unknown> {
|
|
598
|
-
const headers = response.headers as Headers;
|
|
599
|
-
const request = response.request;
|
|
600
|
-
const failed = notGrpc(args, response);
|
|
601
|
-
if (failed) return Stream.fromEffect(failed);
|
|
602
|
-
const method = methodOf(request.url);
|
|
603
|
-
if (!method) return Stream.fromEffect(sdkFailure(`no gRPC descriptor for ${request.url}`));
|
|
604
|
-
const [, responseType] = method;
|
|
605
|
-
const reader = new FrameReader();
|
|
606
|
-
const item = (payload: Uint8Array) => {
|
|
607
|
-
let value: unknown;
|
|
608
|
-
try {
|
|
609
|
-
value = decodeMessage(d, responseType, payload);
|
|
610
|
-
} catch (cause) {
|
|
611
|
-
return parseFailure(payload, cause);
|
|
612
|
-
}
|
|
613
|
-
return Effect.map(
|
|
614
|
-
succeedWith(args, request, headers, { items: [value] }),
|
|
615
|
-
(decoded) => (decoded as { readonly items: ReadonlyArray<unknown> }).items[0],
|
|
659
|
+
},
|
|
616
660
|
);
|
|
661
|
+
},
|
|
662
|
+
),
|
|
663
|
+
);
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
const deadlineExceeded = (args: DecodeArgs, url: string) =>
|
|
667
|
+
failWith(args, HttpClientRequest.post(url), {}, 4, "Deadline Exceeded");
|
|
668
|
+
|
|
669
|
+
return {
|
|
670
|
+
encode,
|
|
671
|
+
decode,
|
|
672
|
+
call: (operation, input) =>
|
|
673
|
+
Effect.withFiber((fiber) => {
|
|
674
|
+
const context = fiber.context as Context.Context<any>;
|
|
675
|
+
const args: DecodeArgs = {
|
|
676
|
+
outputAst: operation.outputAst,
|
|
677
|
+
errors: operation.errors,
|
|
678
|
+
config: operation.config,
|
|
617
679
|
};
|
|
618
|
-
const
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
let frames: Uint8Array[];
|
|
623
|
-
try {
|
|
624
|
-
frames = reader.push(chunk);
|
|
625
|
-
} catch (cause) {
|
|
626
|
-
return badFrame(args, response, cause);
|
|
627
|
-
}
|
|
628
|
-
return Effect.forEach(frames, item);
|
|
629
|
-
}),
|
|
680
|
+
const ms = deadlineMillis(context);
|
|
681
|
+
const run = Effect.flatMap(limitOf(context, input), (limit) =>
|
|
682
|
+
Effect.flatMap(send(context, operation, input, ms), (response) =>
|
|
683
|
+
decodeWith({ ...args, response }, limit),
|
|
630
684
|
),
|
|
631
685
|
);
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
Effect.
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
686
|
+
return ms === undefined
|
|
687
|
+
? run
|
|
688
|
+
: Effect.timeoutOrElse(run, {
|
|
689
|
+
duration: ms,
|
|
690
|
+
orElse: () => deadlineExceeded(args, ""),
|
|
691
|
+
});
|
|
692
|
+
}),
|
|
693
|
+
stream: (operation, input) =>
|
|
694
|
+
Stream.unwrap(
|
|
695
|
+
Effect.withFiber((fiber) => {
|
|
696
|
+
const context = fiber.context as Context.Context<any>;
|
|
697
|
+
const args: DecodeArgs = {
|
|
698
|
+
outputAst: operation.outputAst,
|
|
699
|
+
errors: operation.errors,
|
|
700
|
+
config: operation.config,
|
|
701
|
+
};
|
|
702
|
+
const ms = deadlineMillis(context);
|
|
703
|
+
const messages = Effect.flatMap(limitOf(context, input), (limit) =>
|
|
704
|
+
Effect.map(send(context, operation, input, ms), (response) =>
|
|
705
|
+
streamReply(args, response, limit),
|
|
706
|
+
),
|
|
707
|
+
);
|
|
708
|
+
const stream = Stream.unwrap(messages);
|
|
709
|
+
return Effect.succeed(
|
|
710
|
+
ms === undefined
|
|
711
|
+
? stream
|
|
712
|
+
: Stream.interruptWhen(
|
|
713
|
+
stream,
|
|
714
|
+
Effect.flatMap(Effect.sleep(ms), () => deadlineExceeded(args, "")),
|
|
715
|
+
),
|
|
716
|
+
);
|
|
717
|
+
}),
|
|
718
|
+
),
|
|
719
|
+
};
|
|
720
|
+
|
|
721
|
+
/** Each message of a server stream, decoded as it arrives. */
|
|
722
|
+
function streamReply(
|
|
723
|
+
args: DecodeArgs,
|
|
724
|
+
response: HttpClientResponse.HttpClientResponse,
|
|
725
|
+
limit: number,
|
|
726
|
+
): Stream.Stream<unknown, Failure, Requirements> {
|
|
727
|
+
const headers = response.headers as Headers;
|
|
728
|
+
const request = response.request;
|
|
729
|
+
const failed = notGrpc(args, response);
|
|
730
|
+
if (failed) return Stream.fromEffect(failed);
|
|
731
|
+
const method = methodOf(request.url);
|
|
732
|
+
if (!method) return Stream.fromEffect(sdkFailure(`no gRPC descriptor for ${request.url}`));
|
|
733
|
+
const [, responseType] = method;
|
|
734
|
+
const reader = new FrameReader(limit);
|
|
735
|
+
const item = (payload: Uint8Array): Effect.Effect<unknown, Failure, Requirements> => {
|
|
736
|
+
let value: unknown;
|
|
737
|
+
try {
|
|
738
|
+
value = decodeMessage(d, responseType, payload);
|
|
739
|
+
} catch (cause) {
|
|
740
|
+
return parseFailure(payload, cause);
|
|
639
741
|
}
|
|
640
|
-
|
|
641
|
-
|
|
742
|
+
return Effect.map(
|
|
743
|
+
succeedWith(args, request, headers, { items: [value] }),
|
|
744
|
+
(decoded) => (decoded as { readonly items: ReadonlyArray<unknown> }).items[0],
|
|
745
|
+
);
|
|
746
|
+
};
|
|
747
|
+
const messages = Stream.flatMap(response.stream, (chunk) =>
|
|
748
|
+
Stream.fromIterableEffect(
|
|
749
|
+
Effect.suspend((): Effect.Effect<ReadonlyArray<unknown>, Failure, Requirements> => {
|
|
750
|
+
if (chunk.length > 0) sawBody = true;
|
|
751
|
+
let frames: Uint8Array[];
|
|
752
|
+
try {
|
|
753
|
+
frames = reader.push(chunk);
|
|
754
|
+
} catch (cause) {
|
|
755
|
+
return frameFailure(args, response, cause);
|
|
756
|
+
}
|
|
757
|
+
return Effect.forEach(frames, item);
|
|
758
|
+
}),
|
|
759
|
+
),
|
|
760
|
+
);
|
|
761
|
+
let sawBody = false;
|
|
762
|
+
const end = Effect.flatMap(trailersOf(response), (trailers) =>
|
|
763
|
+
Effect.flatMap(endOf(args, response, { ...trailers, ...reader.trailers }, sawBody), () =>
|
|
764
|
+
reader.rest !== 0 ? badFrame(args, response, "truncated gRPC frame") : Effect.void,
|
|
765
|
+
),
|
|
766
|
+
);
|
|
767
|
+
return Stream.concat(messages, Stream.drain(Stream.fromEffect(end)));
|
|
768
|
+
}
|
|
769
|
+
};
|
|
770
|
+
|
|
771
|
+
/**
|
|
772
|
+
* The gRPC protocol (core/protocol), over the options of
|
|
773
|
+
* {@link restProtocol}. Assign the result to a module-level const in the
|
|
774
|
+
* provider's `protocol.ts`:
|
|
775
|
+
*
|
|
776
|
+
* export const ModalProtocol = grpcProtocol({ ... });
|
|
777
|
+
*
|
|
778
|
+
* A non-zero status becomes a class the operation or its service declares,
|
|
779
|
+
* as a failed REST response does (see {@link restProtocol}). The protocol
|
|
780
|
+
* declares what the options and {@link GrpcProtocolError} fail with.
|
|
781
|
+
*/
|
|
782
|
+
export const grpcProtocol = <
|
|
783
|
+
Credentials,
|
|
784
|
+
CredentialsError = never,
|
|
785
|
+
CredentialsRequirements = never,
|
|
786
|
+
RouteError = never,
|
|
787
|
+
ClassifiedError = never,
|
|
788
|
+
UnknownError = never,
|
|
789
|
+
ParseError = never,
|
|
790
|
+
StatusError = never,
|
|
791
|
+
>(
|
|
792
|
+
options: GrpcProtocolOptions<
|
|
793
|
+
Credentials,
|
|
794
|
+
CredentialsError,
|
|
795
|
+
CredentialsRequirements,
|
|
796
|
+
RouteError,
|
|
797
|
+
ClassifiedError,
|
|
798
|
+
UnknownError,
|
|
799
|
+
ParseError,
|
|
800
|
+
StatusError
|
|
801
|
+
>,
|
|
802
|
+
): Protocol<
|
|
803
|
+
| CredentialsError
|
|
804
|
+
| RouteError
|
|
805
|
+
| ClassifiedError
|
|
806
|
+
| UnknownError
|
|
807
|
+
| ParseError
|
|
808
|
+
| StatusError
|
|
809
|
+
| GrpcProtocolError,
|
|
810
|
+
CredentialsRequirements
|
|
811
|
+
> => grpcMethods(restProtocolMethods(options), options);
|