@distilled.cloud/core 1.0.0-rc.2 → 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/LICENSE +201 -0
- package/README.md +33 -0
- package/lib/_bundle/aws.js +2 -0
- package/lib/_bundle/aws.js.map +1 -0
- package/lib/_bundle/base.js +5 -0
- package/lib/_bundle/base.js.map +1 -0
- package/lib/_bundle/graphql.js +3 -0
- package/lib/_bundle/graphql.js.map +1 -0
- package/lib/_bundle/grpc.js +3 -0
- package/lib/_bundle/grpc.js.map +1 -0
- package/lib/_bundle/rest.js +2 -0
- package/lib/_bundle/rest.js.map +1 -0
- package/lib/_bundle/xml.js +4 -0
- package/lib/_bundle/xml.js.map +1 -0
- package/lib/annotations.d.ts +17 -0
- package/lib/annotations.d.ts.map +1 -0
- package/lib/annotations.js +2 -0
- package/lib/annotations.js.map +1 -0
- package/lib/api.d.ts +220 -51
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -178
- package/lib/category.d.ts +19 -16
- package/lib/category.d.ts.map +1 -1
- package/lib/category.js +1 -280
- package/lib/chunks.d.ts +3 -0
- package/lib/chunks.d.ts.map +1 -0
- package/lib/chunks.js +1 -0
- package/lib/config.d.ts +81 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +1 -0
- package/lib/crypto.d.ts +76 -0
- package/lib/crypto.d.ts.map +1 -0
- package/lib/crypto.js +1 -0
- package/lib/error-category.d.ts +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/error-category.js +1 -46
- package/lib/errors.d.ts +99 -74
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -186
- package/lib/graphql.d.ts +420 -0
- package/lib/graphql.d.ts.map +1 -0
- package/lib/graphql.js +1 -0
- package/lib/graphql.types.d.ts +11 -0
- package/lib/graphql.types.d.ts.map +1 -0
- package/lib/graphql.types.js +1 -0
- package/lib/grpc-transport-cloudflare-http2.d.ts +7 -0
- package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -0
- package/lib/grpc-transport-cloudflare-http2.js +6 -0
- package/lib/grpc-transport-cloudflare-http2.js.map +1 -0
- package/lib/grpc-transport-cloudflare-sockets.d.ts +7 -0
- package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -0
- package/lib/grpc-transport-cloudflare-sockets.js +5 -0
- package/lib/grpc-transport-cloudflare-sockets.js.map +1 -0
- package/lib/grpc-transport-node-http2.d.ts +19 -0
- package/lib/grpc-transport-node-http2.d.ts.map +1 -0
- package/lib/grpc-transport-node-http2.js +2 -0
- package/lib/grpc-transport-node-http2.js.map +1 -0
- package/lib/grpc-transport.d.ts +111 -0
- package/lib/grpc-transport.d.ts.map +1 -0
- package/lib/grpc-transport.js +1 -0
- package/lib/hpack.d.ts +34 -0
- package/lib/hpack.d.ts.map +1 -0
- package/lib/hpack.js +2 -0
- package/lib/hpack.js.map +1 -0
- package/lib/index.d.ts +32 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +17 -0
- 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 +105 -25
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +1 -182
- package/lib/plain-values.d.ts +21 -0
- package/lib/plain-values.d.ts.map +1 -0
- package/lib/plain-values.js +2 -0
- package/lib/plain-values.js.map +1 -0
- package/lib/protobuf.d.ts +85 -0
- package/lib/protobuf.d.ts.map +1 -0
- package/lib/protobuf.js +1 -0
- package/lib/protocol-grpc.d.ts +123 -0
- package/lib/protocol-grpc.d.ts.map +1 -0
- package/lib/protocol-grpc.js +1 -0
- package/lib/protocol-http.d.ts +206 -25
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +1 -554
- package/lib/protocol-rest.d.ts +258 -64
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +1 -242
- 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 +29 -0
- package/lib/query.d.ts.map +1 -0
- package/lib/query.js +1 -0
- package/lib/response-validation-strict.d.ts +48 -0
- package/lib/response-validation-strict.d.ts.map +1 -0
- package/lib/response-validation-strict.js +1 -0
- package/lib/response-validation.d.ts +86 -0
- package/lib/response-validation.d.ts.map +1 -0
- package/lib/response-validation.js +1 -0
- package/lib/retry-after.d.ts +4 -4
- package/lib/retry-after.d.ts.map +1 -1
- package/lib/retry-after.js +1 -112
- 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 +141 -42
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -210
- package/lib/schema-lite-impl.d.ts +179 -0
- package/lib/schema-lite-impl.d.ts.map +1 -0
- package/lib/schema-lite-impl.js +1 -0
- package/lib/schema-lite-typed.d.ts +41 -0
- package/lib/schema-lite-typed.d.ts.map +1 -0
- package/lib/schema-lite-typed.js +1 -0
- package/lib/schema-lite.d.ts +41 -0
- package/lib/schema-lite.d.ts.map +1 -0
- package/lib/schema-lite.js +1 -0
- package/lib/schema.d.ts +21 -7
- package/lib/schema.d.ts.map +1 -1
- package/lib/schema.js +1 -48
- package/lib/sdk-error.d.ts +31 -0
- package/lib/sdk-error.d.ts.map +1 -0
- package/lib/sdk-error.js +1 -0
- 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 +28 -0
- package/lib/sensitive.d.ts.map +1 -0
- package/lib/sensitive.js +2 -0
- package/lib/sensitive.js.map +1 -0
- package/lib/telemetry-debug.d.ts +6 -0
- package/lib/telemetry-debug.d.ts.map +1 -0
- package/lib/telemetry-debug.js +2 -0
- package/lib/telemetry-debug.js.map +1 -0
- package/lib/telemetry-hooks.d.ts +32 -0
- package/lib/telemetry-hooks.d.ts.map +1 -0
- package/lib/telemetry-hooks.js +2 -0
- package/lib/telemetry-hooks.js.map +1 -0
- package/lib/telemetry-host-rules.d.ts +23 -0
- package/lib/telemetry-host-rules.d.ts.map +1 -0
- package/lib/telemetry-host-rules.js +2 -0
- package/lib/telemetry-host-rules.js.map +1 -0
- package/lib/telemetry-lambda-context.d.ts +17 -0
- package/lib/telemetry-lambda-context.d.ts.map +1 -0
- package/lib/telemetry-lambda-context.js +2 -0
- package/lib/telemetry-lambda-context.js.map +1 -0
- package/lib/telemetry-message-context.d.ts +52 -0
- package/lib/telemetry-message-context.d.ts.map +1 -0
- package/lib/telemetry-message-context.js +2 -0
- package/lib/telemetry-message-context.js.map +1 -0
- package/lib/telemetry-metrics.d.ts +6 -0
- package/lib/telemetry-metrics.d.ts.map +1 -0
- package/lib/telemetry-metrics.js +2 -0
- package/lib/telemetry-metrics.js.map +1 -0
- package/lib/telemetry-name-template.d.ts +13 -0
- package/lib/telemetry-name-template.d.ts.map +1 -0
- package/lib/telemetry-name-template.js +2 -0
- package/lib/telemetry-name-template.js.map +1 -0
- package/lib/telemetry-stream.d.ts +11 -0
- package/lib/telemetry-stream.d.ts.map +1 -0
- package/lib/telemetry-stream.js +2 -0
- package/lib/telemetry-stream.js.map +1 -0
- package/lib/telemetry-xray.d.ts +28 -0
- package/lib/telemetry-xray.d.ts.map +1 -0
- package/lib/telemetry-xray.js +2 -0
- package/lib/telemetry-xray.js.map +1 -0
- package/lib/telemetry.d.ts +317 -0
- package/lib/telemetry.d.ts.map +1 -0
- package/lib/telemetry.js +3 -0
- package/lib/telemetry.js.map +1 -0
- 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 +94 -20
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +1 -107
- package/lib/unknown-error.d.ts +18 -0
- package/lib/unknown-error.d.ts.map +1 -0
- package/lib/unknown-error.js +1 -0
- package/lib/xml.d.ts +44 -0
- package/lib/xml.d.ts.map +1 -0
- package/lib/xml.js +1 -0
- package/package.json +13 -13
- package/src/annotations.ts +53 -0
- package/src/api.ts +1597 -234
- package/src/category.ts +97 -134
- package/src/chunks.ts +30 -0
- package/src/config.ts +261 -0
- package/src/crypto.ts +202 -0
- package/src/error-category.ts +16 -29
- package/src/errors.ts +99 -77
- package/src/graphql.ts +1817 -0
- package/src/graphql.types.ts +120 -0
- package/src/grpc-transport-cloudflare-http2.ts +569 -0
- package/src/grpc-transport-cloudflare-sockets.ts +368 -0
- package/src/grpc-transport-node-http2.ts +307 -0
- package/src/grpc-transport.ts +287 -0
- package/src/hpack.ts +331 -0
- package/src/index.ts +31 -0
- package/src/max-message-bytes.ts +58 -0
- package/src/ndjson.ts +55 -0
- package/src/pagination.ts +379 -94
- package/src/plain-values.ts +70 -0
- package/src/protobuf.ts +1367 -0
- package/src/protocol-grpc.ts +811 -0
- package/src/protocol-http.ts +1045 -371
- package/src/protocol-rest.ts +908 -255
- package/src/protocol.ts +150 -0
- package/src/query.ts +39 -0
- package/src/response-validation-strict.ts +333 -0
- package/src/response-validation.ts +161 -0
- package/src/retry-after.ts +8 -14
- package/src/retry-state.ts +105 -0
- package/src/retry.ts +260 -145
- package/src/schema-lite-impl.ts +621 -0
- package/src/schema-lite-typed.ts +44 -0
- package/src/schema-lite.ts +45 -0
- package/src/schema.ts +259 -15
- package/src/sdk-error.ts +37 -0
- package/src/send.ts +156 -0
- package/src/sensitive.ts +188 -0
- package/src/telemetry-debug.ts +179 -0
- package/src/telemetry-hooks.ts +33 -0
- package/src/telemetry-host-rules.ts +39 -0
- package/src/telemetry-lambda-context.ts +46 -0
- package/src/telemetry-message-context.ts +170 -0
- package/src/telemetry-metrics.ts +69 -0
- package/src/telemetry-name-template.ts +26 -0
- package/src/telemetry-stream.ts +100 -0
- package/src/telemetry-xray.ts +66 -0
- package/src/telemetry.ts +800 -0
- package/src/token-cache.ts +117 -0
- package/src/trait.ts +139 -67
- package/src/unknown-error.ts +38 -0
- package/src/xml.ts +346 -0
- package/lib/api.js.map +0 -1
- package/lib/category.js.map +0 -1
- package/lib/codegen/cli.d.ts +0 -29
- package/lib/codegen/cli.d.ts.map +0 -1
- package/lib/codegen/cli.js +0 -165
- package/lib/codegen/cli.js.map +0 -1
- package/lib/codegen/emit.d.ts +0 -129
- package/lib/codegen/emit.d.ts.map +0 -1
- package/lib/codegen/emit.js +0 -105
- package/lib/codegen/emit.js.map +0 -1
- package/lib/codegen/format.d.ts +0 -23
- package/lib/codegen/format.d.ts.map +0 -1
- package/lib/codegen/format.js +0 -28
- package/lib/codegen/format.js.map +0 -1
- package/lib/codegen/generator.d.ts +0 -334
- package/lib/codegen/generator.d.ts.map +0 -1
- package/lib/codegen/generator.js +0 -691
- package/lib/codegen/generator.js.map +0 -1
- package/lib/codegen/graph.d.ts +0 -36
- package/lib/codegen/graph.d.ts.map +0 -1
- package/lib/codegen/graph.js +0 -136
- package/lib/codegen/graph.js.map +0 -1
- package/lib/codegen/members.d.ts +0 -25
- package/lib/codegen/members.d.ts.map +0 -1
- package/lib/codegen/members.js +0 -55
- package/lib/codegen/members.js.map +0 -1
- package/lib/codegen/naming.d.ts +0 -29
- package/lib/codegen/naming.d.ts.map +0 -1
- package/lib/codegen/naming.js +0 -74
- package/lib/codegen/naming.js.map +0 -1
- package/lib/codegen/openapi-cli.d.ts +0 -38
- package/lib/codegen/openapi-cli.d.ts.map +0 -1
- package/lib/codegen/openapi-cli.js +0 -107
- package/lib/codegen/openapi-cli.js.map +0 -1
- package/lib/codegen/openapi.d.ts +0 -115
- package/lib/codegen/openapi.d.ts.map +0 -1
- package/lib/codegen/openapi.js +0 -1220
- package/lib/codegen/openapi.js.map +0 -1
- package/lib/codegen/operations.d.ts +0 -24
- package/lib/codegen/operations.d.ts.map +0 -1
- package/lib/codegen/operations.js +0 -56
- package/lib/codegen/operations.js.map +0 -1
- package/lib/codegen/pagination.d.ts +0 -39
- package/lib/codegen/pagination.d.ts.map +0 -1
- package/lib/codegen/pagination.js +0 -33
- package/lib/codegen/pagination.js.map +0 -1
- package/lib/codegen/prelude.d.ts +0 -15
- package/lib/codegen/prelude.d.ts.map +0 -1
- package/lib/codegen/prelude.js +0 -60
- package/lib/codegen/prelude.js.map +0 -1
- package/lib/error-category.js.map +0 -1
- package/lib/errors.js.map +0 -1
- package/lib/json-patch.d.ts +0 -44
- package/lib/json-patch.d.ts.map +0 -1
- package/lib/json-patch.js +0 -174
- package/lib/json-patch.js.map +0 -1
- package/lib/pagination.js.map +0 -1
- package/lib/protocol-http.js.map +0 -1
- package/lib/protocol-rest.js.map +0 -1
- package/lib/retry-after.js.map +0 -1
- package/lib/retry.js.map +0 -1
- package/lib/schema.js.map +0 -1
- package/lib/trait.js.map +0 -1
- package/src/codegen/cli.ts +0 -268
- package/src/codegen/emit.ts +0 -207
- package/src/codegen/format.ts +0 -47
- package/src/codegen/generator.ts +0 -1153
- package/src/codegen/graph.ts +0 -151
- package/src/codegen/members.ts +0 -71
- package/src/codegen/naming.ts +0 -86
- package/src/codegen/openapi-cli.ts +0 -166
- package/src/codegen/openapi.ts +0 -1450
- package/src/codegen/operations.ts +0 -76
- package/src/codegen/pagination.ts +0 -71
- package/src/codegen/prelude.ts +0 -70
- package/src/json-patch.ts +0 -222
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type * as Effect from "effect/Effect";
|
|
2
|
+
import type * as Stream from "effect/Stream";
|
|
3
|
+
import { mapQuery } from "./graphql.ts";
|
|
4
|
+
import type {
|
|
5
|
+
itemsQuery,
|
|
6
|
+
GqlError,
|
|
7
|
+
GraphQLFailure,
|
|
8
|
+
GraphQLTransportError,
|
|
9
|
+
UnknownGraphQLError,
|
|
10
|
+
} from "./graphql.ts";
|
|
11
|
+
/** Compile-only: Query.fn unwraps Query/Effect fields on the returned plan. */
|
|
12
|
+
import type { FieldWithArgs } from "./graphql.ts";
|
|
13
|
+
import type { PlanError, Query, QueryError, UnwrapPlan } from "./query.ts";
|
|
14
|
+
|
|
15
|
+
type Plan = {
|
|
16
|
+
readonly email: Query<string>;
|
|
17
|
+
readonly projects: Query<ReadonlyArray<{ name: string; stars: number }>>;
|
|
18
|
+
readonly flag: Effect.Effect<boolean>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type Unwrapped = UnwrapPlan<Plan>;
|
|
22
|
+
type Assert<T extends true> = T;
|
|
23
|
+
type Equal<Left, Right> = [Left] extends [Right] ? ([Right] extends [Left] ? true : false) : false;
|
|
24
|
+
|
|
25
|
+
type _Email = Assert<Equal<Unwrapped["email"], string>>;
|
|
26
|
+
type _Projects = Assert<
|
|
27
|
+
Equal<Unwrapped["projects"], ReadonlyArray<{ name: string; stars: number }>>
|
|
28
|
+
>;
|
|
29
|
+
type _Flag = Assert<Equal<Unwrapped["flag"], boolean>>;
|
|
30
|
+
|
|
31
|
+
// U+2500U+2500 Errors U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500
|
|
32
|
+
declare class NotFound {
|
|
33
|
+
readonly _tag: "NotFound";
|
|
34
|
+
readonly message: string;
|
|
35
|
+
}
|
|
36
|
+
declare class RateLimited {
|
|
37
|
+
readonly _tag: "RateLimited";
|
|
38
|
+
readonly message: string;
|
|
39
|
+
}
|
|
40
|
+
declare const project: Query<
|
|
41
|
+
{ name: string; services: ReadonlyArray<{ name: string }> },
|
|
42
|
+
NotFound | RateLimited
|
|
43
|
+
>;
|
|
44
|
+
declare const me: Query<{ email: string }, RateLimited>;
|
|
45
|
+
declare const lookup: Effect.Effect<number, "lookup-failed">;
|
|
46
|
+
|
|
47
|
+
type ErrorPlan = {
|
|
48
|
+
readonly name: typeof project.name;
|
|
49
|
+
readonly services: Query<ReadonlyArray<string>, NotFound>;
|
|
50
|
+
readonly email: typeof me.email;
|
|
51
|
+
readonly lookup: typeof lookup;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
type _FieldsKeepRootErrors = Assert<
|
|
55
|
+
Equal<(typeof project.name)["errorType"], NotFound | RateLimited>
|
|
56
|
+
>;
|
|
57
|
+
type _PlanError = Assert<Equal<PlanError<ErrorPlan>, NotFound | RateLimited | "lookup-failed">>;
|
|
58
|
+
type _NoErrors = Assert<Equal<PlanError<{ literal: 1 }>, never>>;
|
|
59
|
+
type _QueryError = Assert<
|
|
60
|
+
Equal<
|
|
61
|
+
Exclude<
|
|
62
|
+
QueryError<NotFound>,
|
|
63
|
+
| GraphQLFailure<NotFound | UnknownGraphQLError>
|
|
64
|
+
| UnknownGraphQLError
|
|
65
|
+
| GraphQLTransportError
|
|
66
|
+
| GqlError
|
|
67
|
+
>,
|
|
68
|
+
NotFound
|
|
69
|
+
>
|
|
70
|
+
>;
|
|
71
|
+
|
|
72
|
+
// U+2500U+2500 Pagination U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500
|
|
73
|
+
declare const services: Query<ReadonlyArray<{ name: string }>, NotFound>;
|
|
74
|
+
type PagedItems = ReturnType<typeof itemsQuery<{ name: string }, NotFound>>;
|
|
75
|
+
type _ItemsValue = Assert<Equal<Stream.Success<PagedItems>, { name: string }>>;
|
|
76
|
+
type _ItemsError = Assert<
|
|
77
|
+
Equal<
|
|
78
|
+
Extract<Stream.Error<PagedItems>, { _tag: "NotFound" | "GraphQLPaginationError" }>["_tag"],
|
|
79
|
+
"NotFound" | "GraphQLPaginationError"
|
|
80
|
+
>
|
|
81
|
+
>;
|
|
82
|
+
export type _Services = typeof services;
|
|
83
|
+
|
|
84
|
+
// U+2500U+2500 Nullability U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500
|
|
85
|
+
declare const nullable: Query<{
|
|
86
|
+
owner: { name: string; tags: ReadonlyArray<string> | null } | null;
|
|
87
|
+
}>;
|
|
88
|
+
type _NullParent = Assert<Equal<UnwrapPlan<typeof nullable.owner.name>, string | null>>;
|
|
89
|
+
|
|
90
|
+
// U+2500U+2500 Query.map on objects U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500
|
|
91
|
+
declare const service: Query<{
|
|
92
|
+
name: string;
|
|
93
|
+
stars: number;
|
|
94
|
+
latestDeployment: { id: string; status: string } | null;
|
|
95
|
+
tags: ReadonlyArray<{ label: string }>;
|
|
96
|
+
}>;
|
|
97
|
+
const deployment = /*@__PURE__*/ service.latestDeployment.pipe(
|
|
98
|
+
/*@__PURE__*/ mapQuery((d) => ({ id: d.id, status: d.status })),
|
|
99
|
+
);
|
|
100
|
+
type _ObjectMap = Assert<
|
|
101
|
+
Equal<UnwrapPlan<typeof deployment>, { id: string; status: string } | null>
|
|
102
|
+
>;
|
|
103
|
+
const popular = /*@__PURE__*/ service.stars.pipe(/*@__PURE__*/ mapQuery((stars) => stars > 5));
|
|
104
|
+
type _ScalarMap = Assert<Equal<UnwrapPlan<typeof popular>, boolean>>;
|
|
105
|
+
const labels = /*@__PURE__*/ service.tags.pipe(/*@__PURE__*/ mapQuery((tag) => tag.label));
|
|
106
|
+
type _ListMap = Assert<Equal<UnwrapPlan<typeof labels>, ReadonlyArray<string>>>;
|
|
107
|
+
|
|
108
|
+
// Fields with arguments: a required argument makes the field a call only;
|
|
109
|
+
// all-optional arguments also allow the bare read.
|
|
110
|
+
declare const accountQuery: Query<{
|
|
111
|
+
byId: FieldWithArgs<{ name: string }, { readonly accountId: string }>;
|
|
112
|
+
avatarUrl: FieldWithArgs<string, { readonly size?: number | undefined }, false>;
|
|
113
|
+
}>;
|
|
114
|
+
type _ById = Assert<Equal<UnwrapPlan<ReturnType<typeof accountQuery.byId>["name"]>, string>>;
|
|
115
|
+
type _AvatarCall = Assert<Equal<UnwrapPlan<ReturnType<typeof accountQuery.avatarUrl>>, string>>;
|
|
116
|
+
type _AvatarBare = Assert<Equal<UnwrapPlan<typeof accountQuery.avatarUrl>, string>>;
|
|
117
|
+
// @ts-expect-error a required argument cannot be left out
|
|
118
|
+
accountQuery.byId({});
|
|
119
|
+
// @ts-expect-error a field with a required argument has no bare read
|
|
120
|
+
type _NoBareRead = (typeof accountQuery.byId)["pipe"];
|
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A gRPC transport over HTTP/2 on a `cloudflare:sockets` socket, for a
|
|
3
|
+
* Cloudflare Worker calling a gRPC server that speaks only HTTP/2.
|
|
4
|
+
*
|
|
5
|
+
* workerd has no working `node:http2`, and a Worker's `fetch` reads no
|
|
6
|
+
* trailers, so this module speaks HTTP/2 itself (RFC 9113) over a plain TLS
|
|
7
|
+
* socket.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import * as CloudflareHttp2 from "@distilled.cloud/core/grpc-transport-cloudflare-http2";
|
|
11
|
+
*
|
|
12
|
+
* program.pipe(Effect.provide(CloudflareHttp2.layer));
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* `cloudflare:sockets` offers no ALPN, so the connection starts with the
|
|
16
|
+
* HTTP/2 preface straight after the TLS handshake. The server must accept
|
|
17
|
+
* that, as gRPC servers that serve nothing but HTTP/2 do.
|
|
18
|
+
*
|
|
19
|
+
* Each request opens its own connection and uses one stream on it: workerd
|
|
20
|
+
* ties a socket to the request that opened it, so connections are never
|
|
21
|
+
* pooled across events. The exchange is Effect code: the body is an Effect
|
|
22
|
+
* `Stream` that reads frames off the socket in the caller's fiber and closes
|
|
23
|
+
* it when the stream ends or the reader stops, and the trailers settle when
|
|
24
|
+
* it ends.
|
|
25
|
+
*/
|
|
26
|
+
import * as Deferred from "effect/Deferred";
|
|
27
|
+
import * as Effect from "effect/Effect";
|
|
28
|
+
import * as HttpClient from "effect/http/HttpClient";
|
|
29
|
+
import * as HttpClientError from "effect/http/HttpClientError";
|
|
30
|
+
import type * as HttpClientRequest from "effect/http/HttpClientRequest";
|
|
31
|
+
import type * as Layer from "effect/Layer";
|
|
32
|
+
import * as Stream from "effect/Stream";
|
|
33
|
+
import {
|
|
34
|
+
isResponseStatus,
|
|
35
|
+
layer as transportLayer,
|
|
36
|
+
type Reply,
|
|
37
|
+
hasNoBody,
|
|
38
|
+
resetTrailers,
|
|
39
|
+
respond,
|
|
40
|
+
type Trailers,
|
|
41
|
+
} from "./grpc-transport.ts";
|
|
42
|
+
import { encode, type HeaderField, makeDecoder } from "./hpack.ts";
|
|
43
|
+
|
|
44
|
+
/** The part of `cloudflare:sockets` this transport uses. */
|
|
45
|
+
type Connect = (
|
|
46
|
+
address: { hostname: string; port: number },
|
|
47
|
+
options: { secureTransport: "on" | "off"; allowHalfOpen: boolean },
|
|
48
|
+
) => Socket;
|
|
49
|
+
|
|
50
|
+
interface Socket {
|
|
51
|
+
readonly readable: ReadableStream<Uint8Array>;
|
|
52
|
+
readonly writable: WritableStream<Uint8Array>;
|
|
53
|
+
close(): Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// A non-literal specifier keeps bundlers from resolving it for runtimes
|
|
57
|
+
// other than workerd, which resolves its built-in modules at runtime.
|
|
58
|
+
/** Run `f`, failing with what it throws as is. */
|
|
59
|
+
const attempt = <A>(f: () => A): Effect.Effect<A, unknown> =>
|
|
60
|
+
Effect.try({ try: f, catch: (cause) => cause });
|
|
61
|
+
|
|
62
|
+
/** Await `f`, failing with what it rejects with as is. */
|
|
63
|
+
const attemptPromise = <A>(f: () => PromiseLike<A>): Effect.Effect<A, unknown> =>
|
|
64
|
+
Effect.tryPromise({ try: () => f(), catch: (cause) => cause });
|
|
65
|
+
|
|
66
|
+
const SOCKETS_MODULE = "cloudflare:sockets";
|
|
67
|
+
let connectFn: Connect | undefined;
|
|
68
|
+
const loadConnect: Effect.Effect<Connect, unknown> = /*@__PURE__*/ Effect.suspend(() =>
|
|
69
|
+
connectFn !== undefined
|
|
70
|
+
? Effect.succeed(connectFn)
|
|
71
|
+
: attemptPromise(() => import(SOCKETS_MODULE) as Promise<{ connect: Connect }>).pipe(
|
|
72
|
+
Effect.map((module) => (connectFn = module.connect)),
|
|
73
|
+
),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
/** Headers HTTP/2 forbids or derives itself. */
|
|
77
|
+
const CONNECTION_HEADERS = new Set([
|
|
78
|
+
"host",
|
|
79
|
+
"connection",
|
|
80
|
+
"keep-alive",
|
|
81
|
+
"content-length",
|
|
82
|
+
"transfer-encoding",
|
|
83
|
+
"upgrade",
|
|
84
|
+
]);
|
|
85
|
+
|
|
86
|
+
// RFC 9113 section 6: frame types.
|
|
87
|
+
const DATA = 0;
|
|
88
|
+
const HEADERS = 1;
|
|
89
|
+
const RST_STREAM = 3;
|
|
90
|
+
const SETTINGS = 4;
|
|
91
|
+
const PUSH_PROMISE = 5;
|
|
92
|
+
const PING = 6;
|
|
93
|
+
const GOAWAY = 7;
|
|
94
|
+
const WINDOW_UPDATE = 8;
|
|
95
|
+
const CONTINUATION = 9;
|
|
96
|
+
|
|
97
|
+
// Frame flags.
|
|
98
|
+
const END_STREAM = 0x1;
|
|
99
|
+
const ACK = 0x1;
|
|
100
|
+
const END_HEADERS = 0x4;
|
|
101
|
+
const PADDED = 0x8;
|
|
102
|
+
const PRIORITY = 0x20;
|
|
103
|
+
|
|
104
|
+
// Settings.
|
|
105
|
+
const SETTINGS_ENABLE_PUSH = 2;
|
|
106
|
+
const SETTINGS_INITIAL_WINDOW_SIZE = 4;
|
|
107
|
+
const SETTINGS_MAX_FRAME_SIZE = 5;
|
|
108
|
+
|
|
109
|
+
/** The largest flow-control window and the largest frame HTTP/2 allows. */
|
|
110
|
+
const MAX_WINDOW = 0x7fffffff;
|
|
111
|
+
const MAX_FRAME_LIMIT = 0xffffff;
|
|
112
|
+
|
|
113
|
+
/** The flow-control window and frame size both ends start with. */
|
|
114
|
+
const DEFAULT_WINDOW = 65_535;
|
|
115
|
+
const DEFAULT_MAX_FRAME = 16_384;
|
|
116
|
+
/** How much reply this end lets the server send before it has been read. */
|
|
117
|
+
const RECEIVE_WINDOW = 1_048_576;
|
|
118
|
+
/** Read octets are handed back to the server in steps of this size. */
|
|
119
|
+
const WINDOW_STEP = 65_536;
|
|
120
|
+
|
|
121
|
+
/** The one stream of a connection. Client streams are odd. */
|
|
122
|
+
const STREAM = 1;
|
|
123
|
+
|
|
124
|
+
const PREFACE = /*@__PURE__*/ new TextEncoder().encode("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n");
|
|
125
|
+
|
|
126
|
+
class ProtocolViolation extends Error {}
|
|
127
|
+
|
|
128
|
+
const concat = (parts: ReadonlyArray<Uint8Array>): Uint8Array<ArrayBufferLike> => {
|
|
129
|
+
if (parts.length === 1) return parts[0]!;
|
|
130
|
+
let length = 0;
|
|
131
|
+
for (const part of parts) length += part.length;
|
|
132
|
+
const out = new Uint8Array(length);
|
|
133
|
+
let at = 0;
|
|
134
|
+
for (const part of parts) {
|
|
135
|
+
out.set(part, at);
|
|
136
|
+
at += part.length;
|
|
137
|
+
}
|
|
138
|
+
return out;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const uint32 = (bytes: Uint8Array, at: number): number =>
|
|
142
|
+
bytes[at]! * 0x1000000 + ((bytes[at + 1]! << 16) | (bytes[at + 2]! << 8) | bytes[at + 3]!);
|
|
143
|
+
|
|
144
|
+
const frame = (type: number, flags: number, stream: number, payload: Uint8Array): Uint8Array => {
|
|
145
|
+
const out = new Uint8Array(9 + payload.length);
|
|
146
|
+
out[0] = (payload.length >>> 16) & 0xff;
|
|
147
|
+
out[1] = (payload.length >>> 8) & 0xff;
|
|
148
|
+
out[2] = payload.length & 0xff;
|
|
149
|
+
out[3] = type;
|
|
150
|
+
out[4] = flags;
|
|
151
|
+
out[5] = (stream >>> 24) & 0x7f;
|
|
152
|
+
out[6] = (stream >>> 16) & 0xff;
|
|
153
|
+
out[7] = (stream >>> 8) & 0xff;
|
|
154
|
+
out[8] = stream & 0xff;
|
|
155
|
+
out.set(payload, 9);
|
|
156
|
+
return out;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const uint32Payload = (value: number): Uint8Array =>
|
|
160
|
+
Uint8Array.of((value >>> 24) & 0xff, (value >>> 16) & 0xff, (value >>> 8) & 0xff, value & 0xff);
|
|
161
|
+
|
|
162
|
+
const settings = (entries: ReadonlyArray<readonly [id: number, value: number]>): Uint8Array => {
|
|
163
|
+
const payload = new Uint8Array(entries.length * 6);
|
|
164
|
+
entries.forEach(([id, value], index) => {
|
|
165
|
+
payload[index * 6] = id >>> 8;
|
|
166
|
+
payload[index * 6 + 1] = id & 0xff;
|
|
167
|
+
payload.set(uint32Payload(value), index * 6 + 2);
|
|
168
|
+
});
|
|
169
|
+
return frame(SETTINGS, 0, 0, payload);
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/** A header block as one HEADERS frame, continued when it outgrows a frame. */
|
|
173
|
+
const headerFrames = (block: Uint8Array, endStream: boolean): Uint8Array[] => {
|
|
174
|
+
const frames: Uint8Array[] = [];
|
|
175
|
+
for (let at = 0; at < block.length; at += DEFAULT_MAX_FRAME) {
|
|
176
|
+
const last = at + DEFAULT_MAX_FRAME >= block.length;
|
|
177
|
+
frames.push(
|
|
178
|
+
frame(
|
|
179
|
+
at === 0 ? HEADERS : CONTINUATION,
|
|
180
|
+
(last ? END_HEADERS : 0) | (at === 0 && endStream ? END_STREAM : 0),
|
|
181
|
+
STREAM,
|
|
182
|
+
block.subarray(at, at + DEFAULT_MAX_FRAME),
|
|
183
|
+
),
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
return frames;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const transportError = (request: HttpClientRequest.HttpClientRequest, cause: unknown) =>
|
|
190
|
+
new HttpClientError.HttpClientError({
|
|
191
|
+
reason: new HttpClientError.TransportError({
|
|
192
|
+
request,
|
|
193
|
+
cause,
|
|
194
|
+
description: "HTTP/2 request over cloudflare:sockets failed",
|
|
195
|
+
}),
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Close `socket` once, from whichever comes first: the body ends, its reader
|
|
200
|
+
* stops, the exchange fails, or `signal` aborts.
|
|
201
|
+
*/
|
|
202
|
+
const closer = (socket: Socket, signal: AbortSignal): Effect.Effect<void> => {
|
|
203
|
+
let closed = false;
|
|
204
|
+
const markClosed = () => {
|
|
205
|
+
if (closed) return false;
|
|
206
|
+
closed = true;
|
|
207
|
+
signal.removeEventListener("abort", onAbort);
|
|
208
|
+
return true;
|
|
209
|
+
};
|
|
210
|
+
const onAbort = () => {
|
|
211
|
+
// Only for a response nobody reads any more (dropped unread, or its
|
|
212
|
+
// scope closed): no fiber is left to run the close in, and no reader to
|
|
213
|
+
// see that a close failed, which only happens to a socket that already
|
|
214
|
+
// failed.
|
|
215
|
+
if (markClosed()) void socket.close().catch(() => undefined);
|
|
216
|
+
};
|
|
217
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
218
|
+
return Effect.suspend(() =>
|
|
219
|
+
markClosed()
|
|
220
|
+
? attemptPromise(() => socket.close()).pipe(
|
|
221
|
+
Effect.ignore({ log: "Debug", message: "closing a cloudflare:sockets socket failed" }),
|
|
222
|
+
)
|
|
223
|
+
: Effect.void,
|
|
224
|
+
);
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
/** Send `request` and read the response head; the body streams afterwards. */
|
|
228
|
+
const send = (request: HttpClientRequest.HttpClientRequest, url: URL, signal: AbortSignal) =>
|
|
229
|
+
Effect.gen(function* () {
|
|
230
|
+
// A gRPC request is one frame of octets. Any other body would be left
|
|
231
|
+
// behind, so it is refused before a connection is opened.
|
|
232
|
+
if (request.body._tag !== "Uint8Array" && request.body._tag !== "Empty") {
|
|
233
|
+
return yield* Effect.fail(
|
|
234
|
+
new ProtocolViolation(`a ${request.body._tag} request body cannot be sent`),
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
const connect = yield* loadConnect;
|
|
238
|
+
const tls = url.protocol === "https:";
|
|
239
|
+
const socket = yield* attempt(() =>
|
|
240
|
+
connect(
|
|
241
|
+
{ hostname: url.hostname, port: Number(url.port || (tls ? 443 : 80)) },
|
|
242
|
+
{ secureTransport: tls ? "on" : "off", allowHalfOpen: false },
|
|
243
|
+
),
|
|
244
|
+
);
|
|
245
|
+
const close = closer(socket, signal);
|
|
246
|
+
// A call that fails before it returns a body leaves no one else to close the socket.
|
|
247
|
+
return yield* exchange(request, url, socket, close).pipe(
|
|
248
|
+
Effect.flatMap((reply) => respond(request, reply)),
|
|
249
|
+
Effect.onError(() => close),
|
|
250
|
+
);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
/** One request and its response on stream 1 of a fresh connection. */
|
|
254
|
+
const exchange = (
|
|
255
|
+
request: HttpClientRequest.HttpClientRequest,
|
|
256
|
+
url: URL,
|
|
257
|
+
socket: Socket,
|
|
258
|
+
close: Effect.Effect<void>,
|
|
259
|
+
): Effect.Effect<Reply, unknown> =>
|
|
260
|
+
Effect.suspend(() => {
|
|
261
|
+
const tls = url.protocol === "https:";
|
|
262
|
+
const writer = socket.writable.getWriter();
|
|
263
|
+
const reader = socket.readable.getReader();
|
|
264
|
+
const write = (bytes: Uint8Array) => attemptPromise(() => writer.write(bytes));
|
|
265
|
+
const read = attemptPromise(() => reader.read());
|
|
266
|
+
const hpack = makeDecoder();
|
|
267
|
+
// What the server lets this end send.
|
|
268
|
+
let sendWindow = DEFAULT_WINDOW;
|
|
269
|
+
let streamSendWindow = DEFAULT_WINDOW;
|
|
270
|
+
let peerInitialWindow = DEFAULT_WINDOW;
|
|
271
|
+
let peerMaxFrame = DEFAULT_MAX_FRAME;
|
|
272
|
+
// Reply octets read and not yet handed back to the server's windows.
|
|
273
|
+
let unacknowledged = 0;
|
|
274
|
+
|
|
275
|
+
let responseHeaders: Record<string, string> | undefined;
|
|
276
|
+
let status = 0;
|
|
277
|
+
let trailers: Record<string, string> = {};
|
|
278
|
+
/** The server ended the stream; nothing more arrives on it. */
|
|
279
|
+
let ended = false;
|
|
280
|
+
let failure: { readonly cause: unknown } | undefined;
|
|
281
|
+
const chunks: Uint8Array[] = [];
|
|
282
|
+
|
|
283
|
+
// A header block arriving over HEADERS and CONTINUATION frames.
|
|
284
|
+
let block: { readonly fragments: Uint8Array[]; readonly endStream: boolean } | undefined;
|
|
285
|
+
let buffered: Uint8Array<ArrayBufferLike> = new Uint8Array(0);
|
|
286
|
+
|
|
287
|
+
// The first failure ends the stream, so there is never a second.
|
|
288
|
+
const fail = (message: string) => {
|
|
289
|
+
failure = { cause: new ProtocolViolation(message) };
|
|
290
|
+
ended = true;
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
const onHeaders = (fields: ReadonlyArray<HeaderField>, endStream: boolean) => {
|
|
294
|
+
// A map, so a header named like something every object has (`constructor`,
|
|
295
|
+
// `__proto__`) is a header like any other.
|
|
296
|
+
const joined = new Map<string, string>();
|
|
297
|
+
let pseudoStatus: string | undefined;
|
|
298
|
+
for (const [name, value] of fields) {
|
|
299
|
+
if (name === ":status") pseudoStatus = value;
|
|
300
|
+
else if (!name.startsWith(":")) {
|
|
301
|
+
const previous = joined.get(name);
|
|
302
|
+
joined.set(name, previous === undefined ? value : `${previous}, ${value}`);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const headers: Record<string, string> = Object.fromEntries(joined);
|
|
306
|
+
if (responseHeaders === undefined) {
|
|
307
|
+
const code = /^[0-9]{3}$/.test(pseudoStatus ?? "") ? Number(pseudoStatus) : Number.NaN;
|
|
308
|
+
// An interim response (100 Continue) precedes the real one.
|
|
309
|
+
if (code >= 100 && code < 200 && !endStream) return;
|
|
310
|
+
if (!isResponseStatus(code)) {
|
|
311
|
+
return fail(`invalid HTTP status ${pseudoStatus ?? "(none)"}`);
|
|
312
|
+
}
|
|
313
|
+
status = code;
|
|
314
|
+
responseHeaders = headers;
|
|
315
|
+
} else {
|
|
316
|
+
// RFC 9113 section 8.1: a second header block is the trailers.
|
|
317
|
+
if (!endStream) return fail("the trailers do not end the stream");
|
|
318
|
+
trailers = headers;
|
|
319
|
+
}
|
|
320
|
+
if (endStream) ended = true;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const onFrame = (
|
|
324
|
+
type: number,
|
|
325
|
+
flags: number,
|
|
326
|
+
stream: number,
|
|
327
|
+
payload: Uint8Array,
|
|
328
|
+
): Effect.Effect<void, unknown> =>
|
|
329
|
+
Effect.gen(function* () {
|
|
330
|
+
// RFC 9113 section 6.10: nothing may come between the frames of a block.
|
|
331
|
+
if (block !== undefined && (type !== CONTINUATION || stream !== STREAM)) {
|
|
332
|
+
return fail("a header block was interrupted by another frame");
|
|
333
|
+
}
|
|
334
|
+
// Push is disabled, so the server has no other stream to send these on.
|
|
335
|
+
if (stream !== STREAM && (type === DATA || type === HEADERS || type === CONTINUATION)) {
|
|
336
|
+
return fail(`a frame on stream ${stream}, which this end did not open`);
|
|
337
|
+
}
|
|
338
|
+
switch (type) {
|
|
339
|
+
case DATA: {
|
|
340
|
+
if (responseHeaders === undefined) return fail("DATA before the response headers");
|
|
341
|
+
const start = (flags & PADDED) !== 0 ? 1 : 0;
|
|
342
|
+
const end = payload.length - (start === 1 ? (payload[0] ?? 0) : 0);
|
|
343
|
+
if (end < start) return fail("DATA padding exceeds the frame");
|
|
344
|
+
if (end > start) chunks.push(payload.subarray(start, end));
|
|
345
|
+
if ((flags & END_STREAM) !== 0) ended = true;
|
|
346
|
+
// Padding counts against the window too.
|
|
347
|
+
unacknowledged += payload.length;
|
|
348
|
+
if (unacknowledged >= WINDOW_STEP && !ended) {
|
|
349
|
+
const increment = uint32Payload(unacknowledged);
|
|
350
|
+
unacknowledged = 0;
|
|
351
|
+
yield* write(
|
|
352
|
+
concat([
|
|
353
|
+
frame(WINDOW_UPDATE, 0, 0, increment),
|
|
354
|
+
frame(WINDOW_UPDATE, 0, STREAM, increment),
|
|
355
|
+
]),
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
case HEADERS: {
|
|
361
|
+
const padded = (flags & PADDED) !== 0;
|
|
362
|
+
const start = (padded ? 1 : 0) + ((flags & PRIORITY) !== 0 ? 5 : 0);
|
|
363
|
+
const end = payload.length - (padded ? (payload[0] ?? 0) : 0);
|
|
364
|
+
if (start > end) return fail("HEADERS padding or priority exceeds the frame");
|
|
365
|
+
block = {
|
|
366
|
+
fragments: [payload.subarray(start, end)],
|
|
367
|
+
endStream: (flags & END_STREAM) !== 0,
|
|
368
|
+
};
|
|
369
|
+
break;
|
|
370
|
+
}
|
|
371
|
+
case CONTINUATION: {
|
|
372
|
+
if (block === undefined) return fail("CONTINUATION without a header block");
|
|
373
|
+
block.fragments.push(payload);
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
case SETTINGS: {
|
|
377
|
+
if ((flags & ACK) !== 0) return;
|
|
378
|
+
if (payload.length % 6 !== 0) return fail("malformed SETTINGS");
|
|
379
|
+
for (let at = 0; at < payload.length; at += 6) {
|
|
380
|
+
const id = (payload[at]! << 8) | payload[at + 1]!;
|
|
381
|
+
const value = uint32(payload, at + 2);
|
|
382
|
+
if (id === SETTINGS_INITIAL_WINDOW_SIZE) {
|
|
383
|
+
if (value > MAX_WINDOW)
|
|
384
|
+
return fail("SETTINGS_INITIAL_WINDOW_SIZE exceeds the limit");
|
|
385
|
+
// The change applies to the stream's window as it stands, which
|
|
386
|
+
// may leave it negative.
|
|
387
|
+
streamSendWindow += value - peerInitialWindow;
|
|
388
|
+
peerInitialWindow = value;
|
|
389
|
+
} else if (id === SETTINGS_MAX_FRAME_SIZE) {
|
|
390
|
+
if (value < DEFAULT_MAX_FRAME || value > MAX_FRAME_LIMIT) {
|
|
391
|
+
return fail("SETTINGS_MAX_FRAME_SIZE is out of range");
|
|
392
|
+
}
|
|
393
|
+
peerMaxFrame = value;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
yield* write(frame(SETTINGS, ACK, 0, new Uint8Array(0)));
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
case PING: {
|
|
400
|
+
if (payload.length !== 8) return fail("malformed PING");
|
|
401
|
+
if ((flags & ACK) === 0) yield* write(frame(PING, ACK, 0, payload));
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
case WINDOW_UPDATE: {
|
|
405
|
+
if (payload.length !== 4) return fail("malformed WINDOW_UPDATE");
|
|
406
|
+
const increment = uint32(payload, 0) % 0x80000000;
|
|
407
|
+
if (stream === 0) sendWindow += increment;
|
|
408
|
+
else if (stream === STREAM) streamSendWindow += increment;
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
case RST_STREAM: {
|
|
412
|
+
if (payload.length !== 4) return fail("malformed RST_STREAM");
|
|
413
|
+
if (stream !== STREAM) return;
|
|
414
|
+
// A reset ends the call with the gRPC status its code maps to;
|
|
415
|
+
// before the response headers, as a reply of trailers alone.
|
|
416
|
+
if (responseHeaders === undefined) {
|
|
417
|
+
status = 200;
|
|
418
|
+
responseHeaders = { "content-type": "application/grpc" };
|
|
419
|
+
}
|
|
420
|
+
trailers = { ...resetTrailers(uint32(payload, 0)) };
|
|
421
|
+
ended = true;
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
case GOAWAY: {
|
|
425
|
+
if (payload.length < 8) return fail("malformed GOAWAY");
|
|
426
|
+
const lastStream = uint32(payload, 0) % 0x80000000;
|
|
427
|
+
const code = uint32(payload, 4);
|
|
428
|
+
// A graceful shutdown (code 0) that still covers this stream lets it finish.
|
|
429
|
+
if (code !== 0 || lastStream < STREAM) {
|
|
430
|
+
fail(`the server closed the connection (HTTP/2 error ${code})`);
|
|
431
|
+
}
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
case PUSH_PROMISE:
|
|
435
|
+
return fail("the server pushed a stream with push disabled");
|
|
436
|
+
default:
|
|
437
|
+
// PRIORITY and frames this end doesn't know are ignored.
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
if ((flags & END_HEADERS) === 0) return;
|
|
441
|
+
const complete = block;
|
|
442
|
+
block = undefined;
|
|
443
|
+
const fields = yield* attempt(() => hpack.decode(concat(complete.fragments)));
|
|
444
|
+
onHeaders(fields, complete.endStream);
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Read from the socket and handle the complete frames that arrived, up to
|
|
449
|
+
* the end of the stream: the connection carries nothing else, so what
|
|
450
|
+
* follows is never read. Only called while the stream is open.
|
|
451
|
+
*/
|
|
452
|
+
const step: Effect.Effect<void, unknown> = Effect.gen(function* () {
|
|
453
|
+
const { value, done } = yield* read;
|
|
454
|
+
if (done) return fail("connection closed before the response ended");
|
|
455
|
+
buffered = buffered.length === 0 ? value : concat([buffered, value]);
|
|
456
|
+
while (buffered.length >= 9) {
|
|
457
|
+
const length = (buffered[0]! << 16) | (buffered[1]! << 8) | buffered[2]!;
|
|
458
|
+
// This end never raises SETTINGS_MAX_FRAME_SIZE.
|
|
459
|
+
if (length > DEFAULT_MAX_FRAME) return fail(`a ${length}-octet frame exceeds the limit`);
|
|
460
|
+
if (buffered.length < 9 + length) break;
|
|
461
|
+
const payload = buffered.subarray(9, 9 + length);
|
|
462
|
+
const type = buffered[3]!;
|
|
463
|
+
const flags = buffered[4]!;
|
|
464
|
+
const stream = uint32(buffered, 5) % 0x80000000;
|
|
465
|
+
buffered = buffered.subarray(9 + length);
|
|
466
|
+
yield* onFrame(type, flags, stream, payload);
|
|
467
|
+
if (ended) return;
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
const body = request.body._tag === "Uint8Array" ? request.body.body : new Uint8Array(0);
|
|
472
|
+
const fields: HeaderField[] = [
|
|
473
|
+
[":method", request.method],
|
|
474
|
+
[":scheme", tls ? "https" : "http"],
|
|
475
|
+
[":path", `${url.pathname}${url.search}`],
|
|
476
|
+
[":authority", url.host],
|
|
477
|
+
];
|
|
478
|
+
for (const [key, value] of Object.entries(request.headers)) {
|
|
479
|
+
if (!CONNECTION_HEADERS.has(key)) fields.push([key, value]);
|
|
480
|
+
}
|
|
481
|
+
return Effect.gen(function* () {
|
|
482
|
+
yield* write(
|
|
483
|
+
concat([
|
|
484
|
+
PREFACE,
|
|
485
|
+
settings([
|
|
486
|
+
[SETTINGS_ENABLE_PUSH, 0],
|
|
487
|
+
[SETTINGS_INITIAL_WINDOW_SIZE, RECEIVE_WINDOW],
|
|
488
|
+
]),
|
|
489
|
+
frame(WINDOW_UPDATE, 0, 0, uint32Payload(RECEIVE_WINDOW - DEFAULT_WINDOW)),
|
|
490
|
+
...headerFrames(encode(fields), body.length === 0),
|
|
491
|
+
]),
|
|
492
|
+
);
|
|
493
|
+
|
|
494
|
+
// The body goes out as far as the server's windows allow; a full window
|
|
495
|
+
// waits for the server's WINDOW_UPDATE.
|
|
496
|
+
for (let at = 0; at < body.length && !ended;) {
|
|
497
|
+
const size = Math.min(sendWindow, streamSendWindow, peerMaxFrame, body.length - at);
|
|
498
|
+
if (size <= 0) {
|
|
499
|
+
yield* step;
|
|
500
|
+
continue;
|
|
501
|
+
}
|
|
502
|
+
const last = at + size === body.length;
|
|
503
|
+
yield* write(frame(DATA, last ? END_STREAM : 0, STREAM, body.subarray(at, at + size)));
|
|
504
|
+
sendWindow -= size;
|
|
505
|
+
streamSendWindow -= size;
|
|
506
|
+
at += size;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
while (responseHeaders === undefined) {
|
|
510
|
+
// Before its headers a stream only ends by failing.
|
|
511
|
+
if (failure !== undefined) return yield* Effect.fail(failure.cause);
|
|
512
|
+
yield* step;
|
|
513
|
+
}
|
|
514
|
+
const head = responseHeaders;
|
|
515
|
+
// A stream that already ended, or a reply with no body, needs the socket no more.
|
|
516
|
+
if (ended || hasNoBody(status)) yield* close;
|
|
517
|
+
|
|
518
|
+
const trailersDone = yield* Deferred.make<Trailers, unknown>();
|
|
519
|
+
/** The next reply octets, or `undefined` once the stream has ended. */
|
|
520
|
+
const next: Effect.Effect<Uint8Array[] | undefined, unknown> = Effect.gen(function* () {
|
|
521
|
+
for (;;) {
|
|
522
|
+
// What arrived before a failure is handed over before the failure.
|
|
523
|
+
if (chunks.length > 0) return chunks.splice(0);
|
|
524
|
+
if (ended) {
|
|
525
|
+
if (failure !== undefined) return yield* Effect.fail(failure.cause);
|
|
526
|
+
yield* Deferred.succeed(trailersDone, trailers);
|
|
527
|
+
return undefined;
|
|
528
|
+
}
|
|
529
|
+
yield* Effect.catch(step, (cause) =>
|
|
530
|
+
Effect.sync(() => {
|
|
531
|
+
failure = { cause };
|
|
532
|
+
ended = true;
|
|
533
|
+
}),
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
const stream = Stream.unfold(undefined, () =>
|
|
538
|
+
Effect.map(next, (data) => (data === undefined ? undefined : ([data, undefined] as const))),
|
|
539
|
+
).pipe(
|
|
540
|
+
Stream.flattenIterable,
|
|
541
|
+
Stream.ensuring(
|
|
542
|
+
Effect.suspend(() =>
|
|
543
|
+
Effect.andThen(
|
|
544
|
+
// Settles nothing once the stream ended well.
|
|
545
|
+
Deferred.fail(
|
|
546
|
+
trailersDone,
|
|
547
|
+
failure?.cause ?? new ProtocolViolation("the body was not read to its end"),
|
|
548
|
+
),
|
|
549
|
+
close,
|
|
550
|
+
),
|
|
551
|
+
),
|
|
552
|
+
),
|
|
553
|
+
);
|
|
554
|
+
return {
|
|
555
|
+
status,
|
|
556
|
+
headers: head,
|
|
557
|
+
body: stream,
|
|
558
|
+
trailers: Deferred.await(trailersDone),
|
|
559
|
+
} satisfies Reply;
|
|
560
|
+
});
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
/** An `HttpClient` that sends each request as an HTTP/2 stream on its own `cloudflare:sockets` socket. */
|
|
564
|
+
export const client: HttpClient.HttpClient = /*@__PURE__*/ HttpClient.make((request, url, signal) =>
|
|
565
|
+
Effect.mapError(send(request, url, signal), (cause) => transportError(request, cause)),
|
|
566
|
+
);
|
|
567
|
+
|
|
568
|
+
/** Send gRPC calls over HTTP/2 on `cloudflare:sockets` in this scope. */
|
|
569
|
+
export const layer: Layer.Layer<never> = /*@__PURE__*/ transportLayer(client);
|