@distilled.cloud/core 1.0.0-rc.17 → 1.0.0-rc.19
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/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 -0
- package/lib/_bundle/grpc.js.map +1 -0
- package/lib/_bundle/rest.js +1 -1
- package/lib/_bundle/rest.js.map +1 -1
- package/lib/_bundle/xml.js +1 -1
- package/lib/_bundle/xml.js.map +1 -1
- 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 +52 -1
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -1
- package/lib/category.d.ts.map +1 -1
- package/lib/config.d.ts +54 -13
- package/lib/config.d.ts.map +1 -1
- package/lib/config.js +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/errors.d.ts +3 -13
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -1
- package/lib/graphql.d.ts +26 -5
- package/lib/graphql.d.ts.map +1 -1
- package/lib/graphql.types.d.ts.map +1 -1
- 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 +76 -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 +6 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -0
- package/lib/pagination.d.ts +55 -3
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +1 -1
- package/lib/protobuf.d.ts +92 -0
- package/lib/protobuf.d.ts.map +1 -0
- package/lib/protobuf.js +1 -0
- package/lib/protocol-grpc.d.ts +86 -0
- package/lib/protocol-grpc.d.ts.map +1 -0
- package/lib/protocol-grpc.js +1 -0
- package/lib/protocol-http.d.ts +38 -9
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +1 -1
- package/lib/protocol-rest.d.ts +60 -42
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +1 -1
- package/lib/response-validation.d.ts +5 -4
- package/lib/response-validation.d.ts.map +1 -1
- package/lib/schema-lite-typed.js +1 -2
- package/lib/sdk-error.d.ts +27 -0
- package/lib/sdk-error.d.ts.map +1 -0
- package/lib/sdk-error.js +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 +288 -0
- package/lib/telemetry.d.ts.map +1 -0
- package/lib/telemetry.js +3 -0
- package/lib/telemetry.js.map +1 -0
- package/lib/trait.d.ts +7 -0
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +1 -1
- package/lib/xml.d.ts.map +1 -1
- package/package.json +3 -5
- package/src/annotations.ts +53 -0
- package/src/api.ts +316 -43
- package/src/category.ts +21 -22
- package/src/config.ts +58 -29
- package/src/error-category.ts +12 -14
- package/src/errors.ts +11 -7
- package/src/graphql.ts +191 -63
- package/src/graphql.types.ts +5 -1
- package/src/grpc-transport-cloudflare-http2.ts +494 -0
- package/src/grpc-transport-cloudflare-sockets.ts +299 -0
- package/src/grpc-transport-node-http2.ts +234 -0
- package/src/grpc-transport.ts +118 -0
- package/src/hpack.ts +331 -0
- package/src/index.ts +6 -2
- package/src/pagination.ts +193 -8
- package/src/protobuf.ts +1332 -0
- package/src/protocol-grpc.ts +641 -0
- package/src/protocol-http.ts +87 -67
- package/src/protocol-rest.ts +183 -191
- package/src/response-validation.ts +8 -5
- package/src/sdk-error.ts +33 -0
- package/src/sensitive.ts +154 -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 +832 -0
- package/src/trait.ts +10 -0
- package/src/xml.ts +10 -1
- package/lib/codegen/boolean-string-enums.d.ts +0 -36
- package/lib/codegen/boolean-string-enums.d.ts.map +0 -1
- package/lib/codegen/boolean-string-enums.js +0 -147
- package/lib/codegen/boolean-string-enums.js.map +0 -1
- package/lib/codegen/chunk.d.ts +0 -7
- package/lib/codegen/chunk.d.ts.map +0 -1
- package/lib/codegen/chunk.js +0 -139
- package/lib/codegen/chunk.js.map +0 -1
- package/lib/codegen/cli.d.ts +0 -80
- package/lib/codegen/cli.d.ts.map +0 -1
- package/lib/codegen/cli.js +0 -166
- package/lib/codegen/cli.js.map +0 -1
- package/lib/codegen/datafy.d.ts +0 -14
- package/lib/codegen/datafy.d.ts.map +0 -1
- package/lib/codegen/datafy.js +0 -853
- package/lib/codegen/datafy.js.map +0 -1
- package/lib/codegen/dedupe.d.ts +0 -5
- package/lib/codegen/dedupe.d.ts.map +0 -1
- package/lib/codegen/dedupe.js +0 -153
- package/lib/codegen/dedupe.js.map +0 -1
- package/lib/codegen/emit.d.ts +0 -125
- package/lib/codegen/emit.d.ts.map +0 -1
- package/lib/codegen/emit.js +0 -99
- package/lib/codegen/emit.js.map +0 -1
- package/lib/codegen/format.d.ts +0 -11
- package/lib/codegen/format.d.ts.map +0 -1
- package/lib/codegen/format.js +0 -32
- package/lib/codegen/format.js.map +0 -1
- package/lib/codegen/generated-at.d.ts +0 -29
- package/lib/codegen/generated-at.d.ts.map +0 -1
- package/lib/codegen/generated-at.js +0 -78
- package/lib/codegen/generated-at.js.map +0 -1
- package/lib/codegen/generator.d.ts +0 -353
- package/lib/codegen/generator.d.ts.map +0 -1
- package/lib/codegen/generator.js +0 -810
- 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/graphql-client.d.ts +0 -70
- package/lib/codegen/graphql-client.d.ts.map +0 -1
- package/lib/codegen/graphql-client.js +0 -439
- package/lib/codegen/graphql-client.js.map +0 -1
- package/lib/codegen/graphql.d.ts +0 -207
- package/lib/codegen/graphql.d.ts.map +0 -1
- package/lib/codegen/graphql.js +0 -786
- package/lib/codegen/graphql.js.map +0 -1
- package/lib/codegen/latin1.d.ts +0 -3
- package/lib/codegen/latin1.d.ts.map +0 -1
- package/lib/codegen/latin1.js +0 -126
- package/lib/codegen/latin1.js.map +0 -1
- package/lib/codegen/members.d.ts +0 -32
- package/lib/codegen/members.d.ts.map +0 -1
- package/lib/codegen/members.js +0 -62
- package/lib/codegen/members.js.map +0 -1
- package/lib/codegen/model-diff.d.ts +0 -10
- package/lib/codegen/model-diff.d.ts.map +0 -1
- package/lib/codegen/model-diff.js +0 -67
- package/lib/codegen/model-diff.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 -52
- package/lib/codegen/openapi-cli.d.ts.map +0 -1
- package/lib/codegen/openapi-cli.js +0 -84
- package/lib/codegen/openapi-cli.js.map +0 -1
- package/lib/codegen/openapi.d.ts +0 -209
- package/lib/codegen/openapi.d.ts.map +0 -1
- package/lib/codegen/openapi.js +0 -1548
- 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 -31
- package/lib/codegen/pagination.js.map +0 -1
- package/lib/codegen/patch-audit.d.ts +0 -84
- package/lib/codegen/patch-audit.d.ts.map +0 -1
- package/lib/codegen/patch-audit.js +0 -366
- package/lib/codegen/patch-audit.js.map +0 -1
- package/lib/codegen/patches.d.ts +0 -129
- package/lib/codegen/patches.d.ts.map +0 -1
- package/lib/codegen/patches.js +0 -472
- package/lib/codegen/patches.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/codegen/privatize.d.ts +0 -21
- package/lib/codegen/privatize.d.ts.map +0 -1
- package/lib/codegen/privatize.js +0 -176
- package/lib/codegen/privatize.js.map +0 -1
- package/lib/codegen/proto.d.ts +0 -121
- package/lib/codegen/proto.d.ts.map +0 -1
- package/lib/codegen/proto.js +0 -960
- package/lib/codegen/proto.js.map +0 -1
- package/lib/codegen/pure.d.ts +0 -6
- package/lib/codegen/pure.d.ts.map +0 -1
- package/lib/codegen/pure.js +0 -91
- package/lib/codegen/pure.js.map +0 -1
- package/lib/codegen/rewrite-operation-ids.d.ts +0 -154
- package/lib/codegen/rewrite-operation-ids.d.ts.map +0 -1
- package/lib/codegen/rewrite-operation-ids.js +0 -1117
- package/lib/codegen/rewrite-operation-ids.js.map +0 -1
- package/lib/codegen/spec-path.d.ts +0 -16
- package/lib/codegen/spec-path.d.ts.map +0 -1
- package/lib/codegen/spec-path.js +0 -101
- package/lib/codegen/spec-path.js.map +0 -1
- package/lib/codegen/table.d.ts +0 -5
- package/lib/codegen/table.d.ts.map +0 -1
- package/lib/codegen/table.js +0 -249
- package/lib/codegen/table.js.map +0 -1
- package/lib/debug.d.ts +0 -2
- package/lib/debug.d.ts.map +0 -1
- package/lib/debug.js +0 -1
- package/lib/json-patch.d.ts +0 -58
- package/lib/json-patch.d.ts.map +0 -1
- package/lib/json-patch.js +0 -2
- package/lib/json-patch.js.map +0 -1
- package/lib/schema-lite-typed.js.map +0 -1
- package/lib/testing.d.ts +0 -40
- package/lib/testing.d.ts.map +0 -1
- package/lib/testing.js +0 -2
- package/lib/testing.js.map +0 -1
- package/src/codegen/boolean-string-enums.ts +0 -155
- package/src/codegen/chunk.ts +0 -147
- package/src/codegen/cli.ts +0 -293
- package/src/codegen/datafy.ts +0 -874
- package/src/codegen/dedupe.ts +0 -173
- package/src/codegen/emit.ts +0 -200
- package/src/codegen/format.ts +0 -49
- package/src/codegen/generated-at.ts +0 -88
- package/src/codegen/generator.ts +0 -1229
- package/src/codegen/graph.ts +0 -151
- package/src/codegen/graphql-client.ts +0 -545
- package/src/codegen/graphql.ts +0 -1145
- package/src/codegen/latin1.ts +0 -131
- package/src/codegen/members.ts +0 -79
- package/src/codegen/model-diff.ts +0 -67
- package/src/codegen/naming.ts +0 -85
- package/src/codegen/openapi-cli.ts +0 -141
- package/src/codegen/openapi.ts +0 -1845
- package/src/codegen/operations.ts +0 -73
- package/src/codegen/pagination.ts +0 -64
- package/src/codegen/patch-audit.ts +0 -480
- package/src/codegen/patches.ts +0 -581
- package/src/codegen/prelude.ts +0 -70
- package/src/codegen/privatize.ts +0 -200
- package/src/codegen/proto.ts +0 -1092
- package/src/codegen/pure.ts +0 -94
- package/src/codegen/rewrite-operation-ids.ts +0 -1219
- package/src/codegen/spec-path.ts +0 -112
- package/src/codegen/table.ts +0 -254
- package/src/debug.ts +0 -8
- package/src/json-patch.ts +0 -273
- package/src/testing.ts +0 -74
package/src/hpack.ts
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HPACK (RFC 7541), the header compression of HTTP/2, for a client that
|
|
3
|
+
* speaks HTTP/2 without a platform implementation
|
|
4
|
+
* (`grpc-transport-cloudflare-http2`).
|
|
5
|
+
*
|
|
6
|
+
* {@link makeDecoder} reads everything a server may send: the static and
|
|
7
|
+
* dynamic tables, every literal form, and Huffman-coded strings.
|
|
8
|
+
* {@link encode} writes the simplest valid form, each field as a literal
|
|
9
|
+
* with no indexing and no Huffman coding, so a connection keeps no encoder
|
|
10
|
+
* state.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** A header block that breaks RFC 7541. HTTP/2 treats it as a connection error. */
|
|
14
|
+
export class HpackError extends Error {}
|
|
15
|
+
|
|
16
|
+
/** A header field: a lowercase name and its value. */
|
|
17
|
+
export type HeaderField = readonly [name: string, value: string];
|
|
18
|
+
|
|
19
|
+
/** RFC 7541 Appendix A. Index 1 is the first entry. */
|
|
20
|
+
const STATIC_TABLE: ReadonlyArray<HeaderField> = [
|
|
21
|
+
[":authority", ""],
|
|
22
|
+
[":method", "GET"],
|
|
23
|
+
[":method", "POST"],
|
|
24
|
+
[":path", "/"],
|
|
25
|
+
[":path", "/index.html"],
|
|
26
|
+
[":scheme", "http"],
|
|
27
|
+
[":scheme", "https"],
|
|
28
|
+
[":status", "200"],
|
|
29
|
+
[":status", "204"],
|
|
30
|
+
[":status", "206"],
|
|
31
|
+
[":status", "304"],
|
|
32
|
+
[":status", "400"],
|
|
33
|
+
[":status", "404"],
|
|
34
|
+
[":status", "500"],
|
|
35
|
+
["accept-charset", ""],
|
|
36
|
+
["accept-encoding", "gzip, deflate"],
|
|
37
|
+
["accept-language", ""],
|
|
38
|
+
["accept-ranges", ""],
|
|
39
|
+
["accept", ""],
|
|
40
|
+
["access-control-allow-origin", ""],
|
|
41
|
+
["age", ""],
|
|
42
|
+
["allow", ""],
|
|
43
|
+
["authorization", ""],
|
|
44
|
+
["cache-control", ""],
|
|
45
|
+
["content-disposition", ""],
|
|
46
|
+
["content-encoding", ""],
|
|
47
|
+
["content-language", ""],
|
|
48
|
+
["content-length", ""],
|
|
49
|
+
["content-location", ""],
|
|
50
|
+
["content-range", ""],
|
|
51
|
+
["content-type", ""],
|
|
52
|
+
["cookie", ""],
|
|
53
|
+
["date", ""],
|
|
54
|
+
["etag", ""],
|
|
55
|
+
["expect", ""],
|
|
56
|
+
["expires", ""],
|
|
57
|
+
["from", ""],
|
|
58
|
+
["host", ""],
|
|
59
|
+
["if-match", ""],
|
|
60
|
+
["if-modified-since", ""],
|
|
61
|
+
["if-none-match", ""],
|
|
62
|
+
["if-range", ""],
|
|
63
|
+
["if-unmodified-since", ""],
|
|
64
|
+
["last-modified", ""],
|
|
65
|
+
["link", ""],
|
|
66
|
+
["location", ""],
|
|
67
|
+
["max-forwards", ""],
|
|
68
|
+
["proxy-authenticate", ""],
|
|
69
|
+
["proxy-authorization", ""],
|
|
70
|
+
["range", ""],
|
|
71
|
+
["referer", ""],
|
|
72
|
+
["refresh", ""],
|
|
73
|
+
["retry-after", ""],
|
|
74
|
+
["server", ""],
|
|
75
|
+
["set-cookie", ""],
|
|
76
|
+
["strict-transport-security", ""],
|
|
77
|
+
["transfer-encoding", ""],
|
|
78
|
+
["user-agent", ""],
|
|
79
|
+
["vary", ""],
|
|
80
|
+
["via", ""],
|
|
81
|
+
["www-authenticate", ""],
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* RFC 7541 Appendix B: the bit length of each symbol's code (the 256 octets,
|
|
86
|
+
* then EOS). The code is canonical, so the codes themselves follow from the
|
|
87
|
+
* lengths: symbols ordered by length, then by value, count up from zero.
|
|
88
|
+
*/
|
|
89
|
+
const HUFFMAN_BITS: ReadonlyArray<number> = [
|
|
90
|
+
13, 23, 28, 28, 28, 28, 28, 28, 28, 24, 30, 28, 28, 30, 28, 28, 28, 28, 28, 28, 28, 28, 30, 28,
|
|
91
|
+
28, 28, 28, 28, 28, 28, 28, 28, 6, 10, 10, 12, 13, 6, 8, 11, 10, 10, 8, 11, 8, 6, 6, 6, 5, 5, 5,
|
|
92
|
+
6, 6, 6, 6, 6, 6, 6, 7, 8, 15, 6, 12, 10, 13, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
93
|
+
7, 7, 7, 7, 7, 7, 8, 7, 8, 13, 19, 13, 14, 6, 15, 5, 6, 5, 6, 5, 6, 6, 6, 5, 7, 7, 6, 6, 6, 5, 6,
|
|
94
|
+
7, 6, 5, 5, 6, 7, 7, 7, 7, 7, 15, 11, 14, 13, 28, 20, 22, 20, 20, 22, 22, 22, 23, 22, 23, 23, 23,
|
|
95
|
+
23, 23, 24, 23, 24, 24, 22, 23, 24, 23, 23, 23, 23, 21, 22, 23, 22, 23, 23, 24, 22, 21, 20, 22,
|
|
96
|
+
22, 23, 23, 21, 23, 22, 22, 24, 21, 22, 23, 23, 21, 21, 22, 21, 23, 22, 23, 23, 20, 22, 22, 22,
|
|
97
|
+
23, 22, 22, 23, 26, 26, 20, 19, 22, 23, 22, 25, 26, 26, 26, 27, 27, 26, 24, 25, 19, 21, 26, 27,
|
|
98
|
+
27, 26, 27, 24, 21, 21, 26, 26, 28, 27, 27, 27, 20, 24, 20, 21, 22, 21, 21, 23, 22, 22, 25, 25,
|
|
99
|
+
24, 24, 26, 23, 26, 27, 26, 26, 27, 27, 27, 27, 27, 28, 27, 27, 27, 27, 27, 26, 30,
|
|
100
|
+
];
|
|
101
|
+
|
|
102
|
+
const EOS = 256;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The code as a binary tree in one array. Node `n` keeps its two branches at
|
|
106
|
+
* `2n` and `2n + 1`: a positive entry is the next node, a negative one is a
|
|
107
|
+
* symbol (`-(symbol + 1)`).
|
|
108
|
+
*/
|
|
109
|
+
const buildHuffmanTree = (): number[] => {
|
|
110
|
+
// The sort is stable, so codes of one length stay in symbol order.
|
|
111
|
+
const symbols = HUFFMAN_BITS.map((bits, symbol) => ({ bits, symbol })).sort(
|
|
112
|
+
(a, b) => a.bits - b.bits,
|
|
113
|
+
);
|
|
114
|
+
const tree = [0, 0];
|
|
115
|
+
let code = 0;
|
|
116
|
+
let previous = 0;
|
|
117
|
+
for (const { bits, symbol } of symbols) {
|
|
118
|
+
code <<= bits - previous;
|
|
119
|
+
previous = bits;
|
|
120
|
+
let node = 0;
|
|
121
|
+
for (let bit = bits - 1; bit >= 0; bit--) {
|
|
122
|
+
const slot = node * 2 + ((code >>> bit) & 1);
|
|
123
|
+
if (bit === 0) tree[slot] = -(symbol + 1);
|
|
124
|
+
else {
|
|
125
|
+
if (tree[slot] === 0) {
|
|
126
|
+
tree[slot] = tree.length / 2;
|
|
127
|
+
tree.push(0, 0);
|
|
128
|
+
}
|
|
129
|
+
node = tree[slot]!;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
code++;
|
|
133
|
+
}
|
|
134
|
+
return tree;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
let huffmanTree: number[] | undefined;
|
|
138
|
+
|
|
139
|
+
/** Decode a Huffman-coded string (RFC 7541 section 5.2). */
|
|
140
|
+
export const huffmanDecode = (bytes: Uint8Array): Uint8Array => {
|
|
141
|
+
const tree = (huffmanTree ??= buildHuffmanTree());
|
|
142
|
+
const out: number[] = [];
|
|
143
|
+
let node = 0;
|
|
144
|
+
// The bits read since the last symbol: at the end they are the padding,
|
|
145
|
+
// which must be fewer than 8 bits, all ones.
|
|
146
|
+
let pending = 0;
|
|
147
|
+
let ones = true;
|
|
148
|
+
for (const byte of bytes) {
|
|
149
|
+
for (let bit = 7; bit >= 0; bit--) {
|
|
150
|
+
const value = (byte >>> bit) & 1;
|
|
151
|
+
const next = tree[node * 2 + value]!;
|
|
152
|
+
if (next < 0) {
|
|
153
|
+
const symbol = -next - 1;
|
|
154
|
+
if (symbol === EOS) throw new HpackError("Huffman string contains EOS");
|
|
155
|
+
out.push(symbol);
|
|
156
|
+
node = 0;
|
|
157
|
+
pending = 0;
|
|
158
|
+
ones = true;
|
|
159
|
+
} else {
|
|
160
|
+
node = next;
|
|
161
|
+
pending++;
|
|
162
|
+
if (value === 0) ones = false;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (pending > 7 || !ones) throw new HpackError("invalid Huffman padding");
|
|
167
|
+
return Uint8Array.from(out);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// Header octets are opaque; one character per octet keeps them all, as
|
|
171
|
+
// `node:http2` does.
|
|
172
|
+
const textOf = (bytes: Uint8Array): string => {
|
|
173
|
+
let out = "";
|
|
174
|
+
for (let i = 0; i < bytes.length; i += 8192) {
|
|
175
|
+
out += String.fromCharCode(...bytes.subarray(i, i + 8192));
|
|
176
|
+
}
|
|
177
|
+
return out;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
interface Entry {
|
|
181
|
+
readonly name: string;
|
|
182
|
+
readonly value: string;
|
|
183
|
+
/** RFC 7541 section 4.1: the octets of the name and value, plus 32. */
|
|
184
|
+
readonly size: number;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Reads the header blocks of one connection, in the order they arrive. */
|
|
188
|
+
export interface HpackDecoder {
|
|
189
|
+
/** The fields of one complete header block. */
|
|
190
|
+
decode(block: Uint8Array): HeaderField[];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* A decoder for one connection. `maxTableSize` is the dynamic table size
|
|
195
|
+
* this end allows (`SETTINGS_HEADER_TABLE_SIZE`, 4096 unless set).
|
|
196
|
+
*/
|
|
197
|
+
export const makeDecoder = (maxTableSize = 4096): HpackDecoder => {
|
|
198
|
+
// Newest first, so dynamic index 62 is `dynamic[0]`.
|
|
199
|
+
const dynamic: Entry[] = [];
|
|
200
|
+
let size = 0;
|
|
201
|
+
let limit = maxTableSize;
|
|
202
|
+
|
|
203
|
+
const evict = () => {
|
|
204
|
+
while (size > limit) size -= dynamic.pop()!.size;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const lookup = (index: number): { readonly name: string; readonly value: string } => {
|
|
208
|
+
if (index >= 1 && index <= STATIC_TABLE.length) {
|
|
209
|
+
const [name, value] = STATIC_TABLE[index - 1]!;
|
|
210
|
+
return { name, value };
|
|
211
|
+
}
|
|
212
|
+
const entry = dynamic[index - STATIC_TABLE.length - 1];
|
|
213
|
+
if (entry === undefined) throw new HpackError(`no header at index ${index}`);
|
|
214
|
+
return entry;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
return {
|
|
218
|
+
decode(block) {
|
|
219
|
+
const fields: HeaderField[] = [];
|
|
220
|
+
let at = 0;
|
|
221
|
+
|
|
222
|
+
// Every caller has checked that the octet the integer starts in is there.
|
|
223
|
+
const integer = (prefix: number): number => {
|
|
224
|
+
const max = 2 ** prefix - 1;
|
|
225
|
+
let value = block[at++]! & max;
|
|
226
|
+
if (value < max) return value;
|
|
227
|
+
for (let shift = 0; ; shift += 7) {
|
|
228
|
+
if (at >= block.length) throw new HpackError("header block ends inside an integer");
|
|
229
|
+
if (shift > 28) throw new HpackError("integer is too large");
|
|
230
|
+
const byte = block[at++]!;
|
|
231
|
+
value += (byte & 0x7f) * 2 ** shift;
|
|
232
|
+
if ((byte & 0x80) === 0) return value;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const string = (): { readonly text: string; readonly octets: number } => {
|
|
237
|
+
const first = block[at];
|
|
238
|
+
if (first === undefined) throw new HpackError("header block ends inside a string");
|
|
239
|
+
const huffman = (first & 0x80) !== 0;
|
|
240
|
+
const length = integer(7);
|
|
241
|
+
if (at + length > block.length) throw new HpackError("header block ends inside a string");
|
|
242
|
+
const raw = block.subarray(at, at + length);
|
|
243
|
+
at += length;
|
|
244
|
+
const bytes = huffman ? huffmanDecode(raw) : raw;
|
|
245
|
+
return { text: textOf(bytes), octets: bytes.length };
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const literal = (prefix: number, index: boolean) => {
|
|
249
|
+
const nameIndex = integer(prefix);
|
|
250
|
+
let name: string;
|
|
251
|
+
let nameOctets: number;
|
|
252
|
+
if (nameIndex === 0) {
|
|
253
|
+
const read = string();
|
|
254
|
+
name = read.text;
|
|
255
|
+
nameOctets = read.octets;
|
|
256
|
+
} else {
|
|
257
|
+
name = lookup(nameIndex).name;
|
|
258
|
+
nameOctets = name.length;
|
|
259
|
+
}
|
|
260
|
+
const value = string();
|
|
261
|
+
fields.push([name, value.text]);
|
|
262
|
+
if (index) {
|
|
263
|
+
const entry = { name, value: value.text, size: nameOctets + value.octets + 32 };
|
|
264
|
+
dynamic.unshift(entry);
|
|
265
|
+
size += entry.size;
|
|
266
|
+
evict();
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
while (at < block.length) {
|
|
271
|
+
const first = block[at]!;
|
|
272
|
+
if ((first & 0x80) !== 0) {
|
|
273
|
+
const { name, value } = lookup(integer(7));
|
|
274
|
+
fields.push([name, value]);
|
|
275
|
+
} else if ((first & 0x40) !== 0) {
|
|
276
|
+
literal(6, true);
|
|
277
|
+
} else if ((first & 0x20) !== 0) {
|
|
278
|
+
// RFC 7541 section 4.2: only at the start of a block.
|
|
279
|
+
if (fields.length > 0) {
|
|
280
|
+
throw new HpackError("dynamic table size update after a header field");
|
|
281
|
+
}
|
|
282
|
+
const next = integer(5);
|
|
283
|
+
if (next > maxTableSize) throw new HpackError("dynamic table size exceeds the limit");
|
|
284
|
+
limit = next;
|
|
285
|
+
evict();
|
|
286
|
+
} else {
|
|
287
|
+
// Without indexing (0000) and never indexed (0001) read the same.
|
|
288
|
+
literal(4, false);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return fields;
|
|
292
|
+
},
|
|
293
|
+
};
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
const encoder = /*@__PURE__*/ new TextEncoder();
|
|
297
|
+
|
|
298
|
+
const pushInteger = (out: number[], value: number, prefix: number, flags: number) => {
|
|
299
|
+
const max = 2 ** prefix - 1;
|
|
300
|
+
if (value < max) {
|
|
301
|
+
out.push(flags | value);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
out.push(flags | max);
|
|
305
|
+
let rest = value - max;
|
|
306
|
+
while (rest >= 128) {
|
|
307
|
+
out.push((rest % 128) | 0x80);
|
|
308
|
+
rest = Math.floor(rest / 128);
|
|
309
|
+
}
|
|
310
|
+
out.push(rest);
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
const pushString = (out: number[], text: string) => {
|
|
314
|
+
const bytes = encoder.encode(text);
|
|
315
|
+
pushInteger(out, bytes.length, 7, 0);
|
|
316
|
+
for (const byte of bytes) out.push(byte);
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* A header block for `fields`, each a literal without indexing (RFC 7541
|
|
321
|
+
* section 6.2.2). Names must be lowercase, pseudo-headers first.
|
|
322
|
+
*/
|
|
323
|
+
export const encode = (fields: ReadonlyArray<HeaderField>): Uint8Array => {
|
|
324
|
+
const out: number[] = [];
|
|
325
|
+
for (const [name, value] of fields) {
|
|
326
|
+
out.push(0);
|
|
327
|
+
pushString(out, name);
|
|
328
|
+
pushString(out, value);
|
|
329
|
+
}
|
|
330
|
+
return Uint8Array.from(out);
|
|
331
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -9,14 +9,16 @@
|
|
|
9
9
|
*
|
|
10
10
|
* Each namespace is also importable on its own subpath
|
|
11
11
|
* (`@distilled.cloud/core/response-validation`, ...); generated SDKs use the
|
|
12
|
-
* subpaths.
|
|
13
|
-
*
|
|
12
|
+
* subpaths. The code generator and test helpers live in the unpublished
|
|
13
|
+
* `@distilled.cloud/dev` package.
|
|
14
14
|
*/
|
|
15
15
|
export * as API from "./api.ts";
|
|
16
16
|
export * as Category from "./category.ts";
|
|
17
17
|
export * as Errors from "./errors.ts";
|
|
18
18
|
export * as GraphQL from "./graphql.ts";
|
|
19
|
+
export * as GrpcTransport from "./grpc-transport.ts";
|
|
19
20
|
export * as Pagination from "./pagination.ts";
|
|
21
|
+
export * as ProtocolGrpc from "./protocol-grpc.ts";
|
|
20
22
|
export * as ProtocolHttp from "./protocol-http.ts";
|
|
21
23
|
export * as ProtocolRest from "./protocol-rest.ts";
|
|
22
24
|
export { Query } from "./query.ts";
|
|
@@ -24,4 +26,6 @@ export * as Crypto from "./crypto.ts";
|
|
|
24
26
|
export * as ResponseValidation from "./response-validation.ts";
|
|
25
27
|
export * as Retry from "./retry.ts";
|
|
26
28
|
export * as RetryAfter from "./retry-after.ts";
|
|
29
|
+
export * as Sensitive from "./sensitive.ts";
|
|
30
|
+
export * as Telemetry from "./telemetry.ts";
|
|
27
31
|
export * as Trait from "./trait.ts";
|
package/src/pagination.ts
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
* - Token-based (AWS style): NextToken/MaxResults continuation tokens
|
|
8
8
|
* - Relay (GraphQL connections): after/first with a `pageInfo` block whose
|
|
9
9
|
* `hasNextPage` -- not the cursor -- marks the end
|
|
10
|
+
* - Link: the next page's token is a query parameter of the `rel="next"` URL
|
|
11
|
+
* in the `Link` response header (RFC 8288; GitHub, Okta, Sentry)
|
|
10
12
|
* - Single: one-shot list endpoints that still expose the paginated surface
|
|
11
13
|
*
|
|
12
14
|
* Each SDK stores a {@link PaginatedTrait} on its operations (sourced from the
|
|
@@ -15,8 +17,10 @@
|
|
|
15
17
|
* Ported from the distilled repo's `core/pagination`.
|
|
16
18
|
*/
|
|
17
19
|
import type * as Arr from "effect/Array";
|
|
20
|
+
import * as Context from "effect/Context";
|
|
18
21
|
import * as Effect from "effect/Effect";
|
|
19
22
|
import * as Stream from "effect/Stream";
|
|
23
|
+
import { SdkError } from "./sdk-error.ts";
|
|
20
24
|
|
|
21
25
|
/**
|
|
22
26
|
* Get a value from an object using a dot-separated path (e.g.
|
|
@@ -69,10 +73,17 @@ export const getItems = (obj: unknown, path: string): readonly unknown[] => {
|
|
|
69
73
|
/** Pagination trait describing how to navigate between pages. */
|
|
70
74
|
export interface PaginatedTrait {
|
|
71
75
|
/** Pagination strategy */
|
|
72
|
-
readonly mode?: "token" | "page" | "cursor" | "relay" | "single";
|
|
73
|
-
/**
|
|
76
|
+
readonly mode?: "token" | "page" | "cursor" | "relay" | "link" | "keyset" | "single";
|
|
77
|
+
/**
|
|
78
|
+
* The name of the input member containing the page/cursor token. In link
|
|
79
|
+
* mode it is also the query parameter read from the `rel="next"` URL.
|
|
80
|
+
*/
|
|
74
81
|
readonly inputToken?: string;
|
|
75
|
-
/**
|
|
82
|
+
/**
|
|
83
|
+
* The path to the output member containing the next page/cursor token. In
|
|
84
|
+
* keyset mode it is a path within one item: the key the next page starts
|
|
85
|
+
* after.
|
|
86
|
+
*/
|
|
76
87
|
readonly outputToken?: string;
|
|
77
88
|
/**
|
|
78
89
|
* The path to the output member containing the paginated items. Segments
|
|
@@ -97,7 +108,14 @@ export type PaginationStrategy = <Input extends Record<string, unknown>, Output,
|
|
|
97
108
|
pagination: PaginatedTrait,
|
|
98
109
|
) => Stream.Stream<Output, E, R>;
|
|
99
110
|
|
|
100
|
-
|
|
111
|
+
/**
|
|
112
|
+
* The stream of a paginated operation whose trait lacks a field its
|
|
113
|
+
* strategy reads: it fails with {@link SdkError} (part of every
|
|
114
|
+
* operation's error channel, so the strategy's `E` is cast to hold it).
|
|
115
|
+
* The generator refuses such traits.
|
|
116
|
+
*/
|
|
117
|
+
export const missingPaginationConfig = (message: string): Stream.Stream<never> =>
|
|
118
|
+
Stream.fail(new SdkError({ message })) as Stream.Stream<never>;
|
|
101
119
|
|
|
102
120
|
/**
|
|
103
121
|
* Whether a continuation token/cursor returned by a paginated operation
|
|
@@ -155,9 +173,10 @@ export const paginatePageNumber = <Input extends Record<string, unknown>, Output
|
|
|
155
173
|
// next one (e.g. Cloudflare's `result_info.page`). Taking that value as
|
|
156
174
|
// the next page re-requests the same page forever. Only accept an
|
|
157
175
|
// *advancing* page number; otherwise advance by one and terminate when
|
|
158
|
-
// a page comes back with no items (or the token is absent).
|
|
176
|
+
// a page comes back with no items (or the token is absent). A `null`
|
|
177
|
+
// items member (e.g. `result: null`) is an empty page, as in `getItems`.
|
|
159
178
|
const items = pagination.items
|
|
160
|
-
? (getPath(response, pagination.items) as readonly unknown[] | undefined)
|
|
179
|
+
? (getPath(response, pagination.items) as readonly unknown[] | null | undefined)
|
|
161
180
|
: undefined;
|
|
162
181
|
|
|
163
182
|
const nextState: State = {
|
|
@@ -165,6 +184,7 @@ export const paginatePageNumber = <Input extends Record<string, unknown>, Output
|
|
|
165
184
|
done:
|
|
166
185
|
nextPage === null ||
|
|
167
186
|
nextPage === undefined ||
|
|
187
|
+
items === null ||
|
|
168
188
|
(items !== undefined && items.length === 0),
|
|
169
189
|
};
|
|
170
190
|
|
|
@@ -267,6 +287,64 @@ export const paginateToken = <Input extends Record<string, unknown>, Output, E,
|
|
|
267
287
|
);
|
|
268
288
|
};
|
|
269
289
|
|
|
290
|
+
// ============================================================================
|
|
291
|
+
// Keyset Pagination
|
|
292
|
+
// ============================================================================
|
|
293
|
+
|
|
294
|
+
/** `input` with the value at the dot-separated `path` replaced (copying each level). */
|
|
295
|
+
const setPath = (input: Record<string, unknown>, path: string, value: unknown): any => {
|
|
296
|
+
const [head, ...rest] = path.split(".");
|
|
297
|
+
const key = head!;
|
|
298
|
+
if (rest.length === 0) return { ...input, [key]: value };
|
|
299
|
+
const child = input[key];
|
|
300
|
+
return {
|
|
301
|
+
...input,
|
|
302
|
+
[key]: setPath(
|
|
303
|
+
child !== null && typeof child === "object" ? (child as Record<string, unknown>) : {},
|
|
304
|
+
rest.join("."),
|
|
305
|
+
value,
|
|
306
|
+
),
|
|
307
|
+
};
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Stream of pages using keyset pagination: the next request starts after
|
|
312
|
+
* the key of the page's last item. `inputToken` is the input member the key
|
|
313
|
+
* goes to and `outputToken` the key's path within an item (both may be
|
|
314
|
+
* dot-separated: Modal's `List*` calls take `pagination.createdBefore` and
|
|
315
|
+
* key on each item's `createdAt`). Traversal ends on an empty page, a page
|
|
316
|
+
* smaller than the requested `pageSize`, or a key that doesn't advance.
|
|
317
|
+
*/
|
|
318
|
+
export const paginateKeyset = <Input extends Record<string, unknown>, Output, E, R>(
|
|
319
|
+
operation: (input: Input) => Effect.Effect<Output, E, R>,
|
|
320
|
+
input: Input,
|
|
321
|
+
pagination: PaginatedTrait,
|
|
322
|
+
): Stream.Stream<Output, E, R> => {
|
|
323
|
+
const { inputToken, outputToken, items } = pagination;
|
|
324
|
+
if (!inputToken || !outputToken || !items) {
|
|
325
|
+
return missingPaginationConfig("Keyset pagination requires inputToken, outputToken and items");
|
|
326
|
+
}
|
|
327
|
+
const limit = pagination.pageSize ? getPath(input, pagination.pageSize) : undefined;
|
|
328
|
+
type State = { request: Input; key: unknown } | undefined;
|
|
329
|
+
return Stream.unfold({ request: input, key: getPath(input, inputToken) } as State, (state) =>
|
|
330
|
+
state === undefined
|
|
331
|
+
? Effect.succeed(undefined)
|
|
332
|
+
: Effect.map(operation(state.request), (response) => {
|
|
333
|
+
const page = getItems(response, items);
|
|
334
|
+
const key = page.length > 0 ? getPath(page[page.length - 1], outputToken) : undefined;
|
|
335
|
+
const done =
|
|
336
|
+
page.length === 0 ||
|
|
337
|
+
isTerminalToken(key) ||
|
|
338
|
+
key === state.key ||
|
|
339
|
+
(typeof limit === "number" && page.length < limit);
|
|
340
|
+
return [
|
|
341
|
+
response,
|
|
342
|
+
done ? undefined : { request: setPath(state.request, inputToken, key), key },
|
|
343
|
+
] as const;
|
|
344
|
+
}),
|
|
345
|
+
);
|
|
346
|
+
};
|
|
347
|
+
|
|
270
348
|
// ============================================================================
|
|
271
349
|
// Relay Pagination (GraphQL connections)
|
|
272
350
|
// ============================================================================
|
|
@@ -338,6 +416,97 @@ export const paginateRelay = <Input extends Record<string, unknown>, Output, E,
|
|
|
338
416
|
);
|
|
339
417
|
};
|
|
340
418
|
|
|
419
|
+
// ============================================================================
|
|
420
|
+
// Link-header Pagination (RFC 8288)
|
|
421
|
+
// ============================================================================
|
|
422
|
+
|
|
423
|
+
/** Where the protocol puts a call's response headers during a link traversal. */
|
|
424
|
+
interface PageHeadersSlot {
|
|
425
|
+
headers: Readonly<Record<string, string | undefined>> | undefined;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const PageHeaders = /*@__PURE__*/ Context.Reference<PageHeadersSlot | undefined>(
|
|
429
|
+
"@distilled.cloud/core/Pagination/PageHeaders",
|
|
430
|
+
{ defaultValue: () => undefined },
|
|
431
|
+
);
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Hand a response's headers to the link traversal running on this fiber, if
|
|
435
|
+
* any. Protocols call it for every response they decode; outside
|
|
436
|
+
* {@link paginateLink} it does nothing.
|
|
437
|
+
*/
|
|
438
|
+
export const recordPageHeaders = (
|
|
439
|
+
context: Context.Context<never>,
|
|
440
|
+
headers: Readonly<Record<string, string | undefined>>,
|
|
441
|
+
): void => {
|
|
442
|
+
const slot = Context.get(context, PageHeaders);
|
|
443
|
+
if (slot !== undefined) slot.headers = headers;
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* The URL of the `rel="next"` link in a `Link` header value, e.g.
|
|
448
|
+
* `<https://api.github.com/repositories/1/issues?page=2>; rel="next", <...>; rel="last"`.
|
|
449
|
+
*/
|
|
450
|
+
export const nextLink = (header: string | undefined): string | undefined => {
|
|
451
|
+
if (header === undefined) return undefined;
|
|
452
|
+
for (const link of header.split(/,\s*(?=<)/)) {
|
|
453
|
+
const match = /^\s*<([^>]*)>(.*)$/.exec(link);
|
|
454
|
+
const rel = match && /;\s*rel\s*=\s*"?([^";]*)"?/i.exec(match[2]!);
|
|
455
|
+
if (rel && rel[1]!.toLowerCase().split(/\s+/).includes("next")) return match![1];
|
|
456
|
+
}
|
|
457
|
+
return undefined;
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Stream of pages that follows the `Link` response header: each request
|
|
462
|
+
* takes `inputToken` from the query of the previous response's `rel="next"`
|
|
463
|
+
* URL, and traversal ends when a response has no next link (or its token
|
|
464
|
+
* repeats the current one). The response body carries no token, so this
|
|
465
|
+
* works for APIs whose list responses are bare arrays.
|
|
466
|
+
*
|
|
467
|
+
* The protocol must report response headers through
|
|
468
|
+
* {@link recordPageHeaders} (the REST protocol does); otherwise traversal
|
|
469
|
+
* stops after the first page.
|
|
470
|
+
*/
|
|
471
|
+
export const paginateLink = <Input extends Record<string, unknown>, Output, E, R>(
|
|
472
|
+
operation: (input: Input) => Effect.Effect<Output, E, R>,
|
|
473
|
+
input: Input,
|
|
474
|
+
pagination: PaginatedTrait,
|
|
475
|
+
): Stream.Stream<Output, E, R> => {
|
|
476
|
+
const inputToken = pagination.inputToken;
|
|
477
|
+
if (!inputToken) {
|
|
478
|
+
return missingPaginationConfig("Link pagination requires inputToken");
|
|
479
|
+
}
|
|
480
|
+
type State = { token: unknown; done: boolean };
|
|
481
|
+
|
|
482
|
+
return Stream.unfold({ token: input[inputToken], done: false } as State, (state) =>
|
|
483
|
+
Effect.gen(function* () {
|
|
484
|
+
if (state.done) return undefined;
|
|
485
|
+
|
|
486
|
+
const requestPayload =
|
|
487
|
+
state.token !== undefined ? ({ ...input, [inputToken]: state.token } as Input) : input;
|
|
488
|
+
const slot: PageHeadersSlot = { headers: undefined };
|
|
489
|
+
const response = yield* Effect.provideService(operation(requestPayload), PageHeaders, slot);
|
|
490
|
+
|
|
491
|
+
const next = nextLink(slot.headers?.["link"]);
|
|
492
|
+
// The token goes back as the query text it came as (`page=2` -> "2");
|
|
493
|
+
// query members are sent as text either way.
|
|
494
|
+
const token =
|
|
495
|
+
next === undefined
|
|
496
|
+
? undefined
|
|
497
|
+
: (new URL(next, "http://_").searchParams.get(inputToken) ?? undefined);
|
|
498
|
+
|
|
499
|
+
const nextState: State = {
|
|
500
|
+
token,
|
|
501
|
+
done:
|
|
502
|
+
isTerminalToken(token) || (state.token !== undefined && token === String(state.token)),
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
return [response, nextState] as const;
|
|
506
|
+
}),
|
|
507
|
+
);
|
|
508
|
+
};
|
|
509
|
+
|
|
341
510
|
/**
|
|
342
511
|
* Shared default pagination dispatcher for SDKs that use generic
|
|
343
512
|
* token/cursor/page traversal.
|
|
@@ -352,6 +521,10 @@ export const paginateWithDefaults: PaginationStrategy = (operation, input, pagin
|
|
|
352
521
|
return paginateCursor(operation, input, pagination);
|
|
353
522
|
case "relay":
|
|
354
523
|
return paginateRelay(operation, input, pagination);
|
|
524
|
+
case "link":
|
|
525
|
+
return paginateLink(operation, input, pagination);
|
|
526
|
+
case "keyset":
|
|
527
|
+
return paginateKeyset(operation, input, pagination);
|
|
355
528
|
case "single":
|
|
356
529
|
return paginateSingle(operation, input, pagination);
|
|
357
530
|
case "token":
|
|
@@ -375,6 +548,19 @@ export const paginateWithDefaults: PaginationStrategy = (operation, input, pagin
|
|
|
375
548
|
export const extractItems = <Output, Item, E, R>(
|
|
376
549
|
pages: Stream.Stream<Output, E, R>,
|
|
377
550
|
itemsPath: string,
|
|
551
|
+
): Stream.Stream<Item, E, R> => flattenPages(pages, (page) => getItems(page, itemsPath));
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* The items of a stream of pages that are themselves the lists (bare-array
|
|
555
|
+
* responses).
|
|
556
|
+
*/
|
|
557
|
+
export const extractArrayItems = <Output, Item, E, R>(
|
|
558
|
+
pages: Stream.Stream<Output, E, R>,
|
|
559
|
+
): Stream.Stream<Item, E, R> => flattenPages(pages, (page) => (Array.isArray(page) ? page : []));
|
|
560
|
+
|
|
561
|
+
const flattenPages = <Output, Item, E, R>(
|
|
562
|
+
pages: Stream.Stream<Output, E, R>,
|
|
563
|
+
itemsOf: (page: Output) => readonly unknown[],
|
|
378
564
|
): Stream.Stream<Item, E, R> =>
|
|
379
565
|
// Flattens each pulled chunk of pages directly, skipping pages with no
|
|
380
566
|
// items. `Stream.flatMap` / `flattenIterable` would do the same but pull in
|
|
@@ -386,8 +572,7 @@ export const extractItems = <Output, Item, E, R>(
|
|
|
386
572
|
pull,
|
|
387
573
|
(chunk) => {
|
|
388
574
|
const items: Item[] = [];
|
|
389
|
-
for (const page of chunk)
|
|
390
|
-
for (const item of getItems(page, itemsPath)) items.push(item as Item);
|
|
575
|
+
for (const page of chunk) for (const item of itemsOf(page)) items.push(item as Item);
|
|
391
576
|
return items.length > 0
|
|
392
577
|
? Effect.succeed(items as unknown as Arr.NonEmptyReadonlyArray<Item>)
|
|
393
578
|
: next;
|