@distilled.cloud/core 1.0.0-rc.18 → 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 +2 -0
- package/lib/annotations.d.ts.map +1 -1
- package/lib/annotations.js +1 -1
- package/lib/annotations.js.map +1 -1
- package/lib/api.d.ts +37 -1
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -1
- package/lib/errors.d.ts +1 -0
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -1
- package/lib/graphql.d.ts +12 -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 +3 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -0
- package/lib/pagination.d.ts +22 -2
- 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 +11 -6
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-rest.d.ts +12 -8
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/response-validation.d.ts +1 -1
- package/lib/response-validation.d.ts.map +1 -1
- 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 +2 -10
- 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-stream.d.ts +3 -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.d.ts +23 -4
- package/lib/telemetry.d.ts.map +1 -1
- package/lib/telemetry.js +2 -2
- package/lib/telemetry.js.map +1 -1
- 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 -3
- package/src/annotations.ts +16 -0
- package/src/api.ts +199 -25
- package/src/errors.ts +4 -0
- package/src/graphql.ts +68 -45
- 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 +3 -0
- package/src/pagination.ts +75 -3
- package/src/protobuf.ts +1332 -0
- package/src/protocol-grpc.ts +641 -0
- package/src/protocol-http.ts +30 -30
- package/src/protocol-rest.ts +49 -118
- package/src/response-validation.ts +4 -2
- package/src/sdk-error.ts +33 -0
- package/src/sensitive.ts +154 -0
- package/src/telemetry-debug.ts +65 -21
- package/src/telemetry-stream.ts +16 -7
- package/src/telemetry.ts +91 -27
- package/src/trait.ts +10 -0
- package/src/xml.ts +10 -1
- package/lib/debug.d.ts +0 -2
- package/lib/debug.d.ts.map +0 -1
- package/lib/debug.js +0 -1
- package/src/debug.ts +0 -11
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
|
@@ -16,7 +16,9 @@ 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,5 +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";
|
|
27
30
|
export * as Telemetry from "./telemetry.ts";
|
|
28
31
|
export * as Trait from "./trait.ts";
|
package/src/pagination.ts
CHANGED
|
@@ -20,6 +20,7 @@ import type * as Arr from "effect/Array";
|
|
|
20
20
|
import * as Context from "effect/Context";
|
|
21
21
|
import * as Effect from "effect/Effect";
|
|
22
22
|
import * as Stream from "effect/Stream";
|
|
23
|
+
import { SdkError } from "./sdk-error.ts";
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* Get a value from an object using a dot-separated path (e.g.
|
|
@@ -72,13 +73,17 @@ export const getItems = (obj: unknown, path: string): readonly unknown[] => {
|
|
|
72
73
|
/** Pagination trait describing how to navigate between pages. */
|
|
73
74
|
export interface PaginatedTrait {
|
|
74
75
|
/** Pagination strategy */
|
|
75
|
-
readonly mode?: "token" | "page" | "cursor" | "relay" | "link" | "single";
|
|
76
|
+
readonly mode?: "token" | "page" | "cursor" | "relay" | "link" | "keyset" | "single";
|
|
76
77
|
/**
|
|
77
78
|
* The name of the input member containing the page/cursor token. In link
|
|
78
79
|
* mode it is also the query parameter read from the `rel="next"` URL.
|
|
79
80
|
*/
|
|
80
81
|
readonly inputToken?: string;
|
|
81
|
-
/**
|
|
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
|
+
*/
|
|
82
87
|
readonly outputToken?: string;
|
|
83
88
|
/**
|
|
84
89
|
* The path to the output member containing the paginated items. Segments
|
|
@@ -103,7 +108,14 @@ export type PaginationStrategy = <Input extends Record<string, unknown>, Output,
|
|
|
103
108
|
pagination: PaginatedTrait,
|
|
104
109
|
) => Stream.Stream<Output, E, R>;
|
|
105
110
|
|
|
106
|
-
|
|
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>;
|
|
107
119
|
|
|
108
120
|
/**
|
|
109
121
|
* Whether a continuation token/cursor returned by a paginated operation
|
|
@@ -275,6 +287,64 @@ export const paginateToken = <Input extends Record<string, unknown>, Output, E,
|
|
|
275
287
|
);
|
|
276
288
|
};
|
|
277
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
|
+
|
|
278
348
|
// ============================================================================
|
|
279
349
|
// Relay Pagination (GraphQL connections)
|
|
280
350
|
// ============================================================================
|
|
@@ -453,6 +523,8 @@ export const paginateWithDefaults: PaginationStrategy = (operation, input, pagin
|
|
|
453
523
|
return paginateRelay(operation, input, pagination);
|
|
454
524
|
case "link":
|
|
455
525
|
return paginateLink(operation, input, pagination);
|
|
526
|
+
case "keyset":
|
|
527
|
+
return paginateKeyset(operation, input, pagination);
|
|
456
528
|
case "single":
|
|
457
529
|
return paginateSingle(operation, input, pagination);
|
|
458
530
|
case "token":
|