@mountaya/client 0.0.0-stage → 0.2.0

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.
Files changed (97) hide show
  1. package/CHANGELOG.md +149 -0
  2. package/LICENCE.fr.txt +280 -0
  3. package/LICENSE.txt +262 -0
  4. package/README.md +1260 -2
  5. package/dist/admin/index.cjs +2895 -0
  6. package/dist/admin/index.cjs.map +1 -0
  7. package/dist/admin/index.d.cts +889 -0
  8. package/dist/admin/index.d.ts +889 -0
  9. package/dist/admin/index.js +204 -0
  10. package/dist/admin/index.js.map +1 -0
  11. package/dist/browser.cjs +5993 -0
  12. package/dist/browser.cjs.map +1 -0
  13. package/dist/browser.d.cts +70 -0
  14. package/dist/browser.d.ts +70 -0
  15. package/dist/browser.js +22 -0
  16. package/dist/browser.js.map +1 -0
  17. package/dist/chunk-CeEX7ayk.d.cts +75531 -0
  18. package/dist/chunk-CeEX7ayk.d.ts +75531 -0
  19. package/dist/chunk-Cg7Dtcb4.js +2987 -0
  20. package/dist/chunk-Cg7Dtcb4.js.map +1 -0
  21. package/dist/chunk-DJqUsgsy.d.ts +1463 -0
  22. package/dist/chunk-DNc8T5K1.js +241 -0
  23. package/dist/chunk-DNc8T5K1.js.map +1 -0
  24. package/dist/chunk-DTxzw3hu.d.cts +1463 -0
  25. package/dist/chunk-DmxQID5X.js +2289 -0
  26. package/dist/chunk-DmxQID5X.js.map +1 -0
  27. package/dist/chunk-DoZt_oIV.d.ts +3991 -0
  28. package/dist/chunk-DpJrqANO.js +11 -0
  29. package/dist/chunk-Dx8mPAdC.d.cts +3991 -0
  30. package/dist/chunk-X2ukY3ho.js +419 -0
  31. package/dist/chunk-X2ukY3ho.js.map +1 -0
  32. package/dist/events/index.cjs +1436 -0
  33. package/dist/events/index.cjs.map +1 -0
  34. package/dist/events/index.d.cts +772 -0
  35. package/dist/events/index.d.ts +772 -0
  36. package/dist/events/index.js +543 -0
  37. package/dist/events/index.js.map +1 -0
  38. package/dist/index.cjs +5992 -0
  39. package/dist/index.cjs.map +1 -0
  40. package/dist/index.d.cts +44 -0
  41. package/dist/index.d.ts +44 -0
  42. package/dist/index.js +21 -0
  43. package/dist/index.js.map +1 -0
  44. package/package.json +126 -4
  45. package/src/admin/index.ts +47 -0
  46. package/src/admin/rules/mountayaAdmin.ts +102 -0
  47. package/src/browser.ts +233 -0
  48. package/src/core/engines/sessionToken.ts +870 -0
  49. package/src/core/rules/auth.ts +1017 -0
  50. package/src/core/rules/clientHeader.ts +28 -0
  51. package/src/core/rules/dates.ts +200 -0
  52. package/src/core/rules/decode.ts +154 -0
  53. package/src/core/rules/envelope.ts +41 -0
  54. package/src/core/rules/errors.ts +1486 -0
  55. package/src/core/rules/graphql.ts +576 -0
  56. package/src/core/rules/headerValue.ts +42 -0
  57. package/src/core/rules/idempotencyKey.ts +64 -0
  58. package/src/core/rules/nullMask.ts +127 -0
  59. package/src/core/rules/nullMasks.gen.ts +428 -0
  60. package/src/core/rules/paginate.ts +316 -0
  61. package/src/core/rules/rateLimit.ts +507 -0
  62. package/src/core/rules/redaction.ts +219 -0
  63. package/src/core/rules/resourceRef.ts +110 -0
  64. package/src/core/rules/responseBody.ts +492 -0
  65. package/src/core/rules/retry.ts +386 -0
  66. package/src/core/rules/selection.ts +416 -0
  67. package/src/core/rules/sessionAnswer.ts +90 -0
  68. package/src/core/rules/sessionSchedule.ts +131 -0
  69. package/src/core/rules/sessionWait.ts +112 -0
  70. package/src/core/rules/transport.ts +2386 -0
  71. package/src/core/rules/version.gen.ts +16 -0
  72. package/src/core/rules/webAddress.ts +165 -0
  73. package/src/core/rules/webAddresses.gen.ts +30 -0
  74. package/src/data/operations.gen.ts +603 -0
  75. package/src/data/schema.gen.ts +8078 -0
  76. package/src/data/selection.gen.ts +472 -0
  77. package/src/events/index.ts +63 -0
  78. package/src/events/rules/agent.ts +439 -0
  79. package/src/events/rules/collab.ts +554 -0
  80. package/src/events/rules/collabNulls.gen.ts +17 -0
  81. package/src/events/rules/eventStream.ts +94 -0
  82. package/src/events/rules/openSseStream.ts +594 -0
  83. package/src/events/rules/sse.ts +349 -0
  84. package/src/index.ts +231 -0
  85. package/src/rest/adminOperations.gen.ts +70 -0
  86. package/src/rest/adminResources.gen.ts +138 -0
  87. package/src/rest/coreOperations.gen.ts +74 -0
  88. package/src/rest/coreResources.gen.ts +244 -0
  89. package/src/rest/dates.gen.ts +217 -0
  90. package/src/rest/operations.gen.ts +4341 -0
  91. package/src/rest/resources.gen.ts +371 -0
  92. package/src/rest/schema.gen.ts +57027 -0
  93. package/src/tiles/operations.gen.ts +206 -0
  94. package/src/tiles/schema.gen.ts +8406 -0
  95. package/src/webhooks/rules/constructWebhookEvent.ts +482 -0
  96. package/src/webhooks/rules/webhookEvent.ts +38 -0
  97. package/src/webhooks/rules/webhookEvents.gen.ts +732 -0
@@ -0,0 +1,507 @@
1
+ /**
2
+ * The rate-limit state an answer describes.
3
+ *
4
+ * The Data API and the Tile API describe the limits an answer was counted against, on every answer
5
+ * the limits weighed, under a key and on the Tile API under a share token as well, in the two
6
+ * fields the IETF's RateLimit header fields for HTTP define: `RateLimit-Policy` lists the
7
+ * policies, each a String member naming one with its quota `q` and its window `w` in seconds, and
8
+ * `RateLimit` states what is left of each, `r`, and the seconds until its window resets, `t`. The
9
+ * Map Studio API sends neither.
10
+ *
11
+ * Both are Structured Field Lists (RFC 9651), so they are parsed as one rather than split on
12
+ * commas: a quoted name may hold a comma, a parameter may hold a byte sequence, and a field that
13
+ * does not parse is ignored whole, as the RFC has a recipient do. The state is reported and never
14
+ * counted against: a client delays no request on what it says, since an organization's figures
15
+ * are its agreement's, which may raise, lower or remove them.
16
+ */
17
+
18
+ /**
19
+ * RateLimitPolicy is one limit an answer was counted against. `minute`, a limit per minute, and
20
+ * `month`, the monthly allowance unless an agreement leaves it unlimited, hold the organization's
21
+ * own clients and, on the Tile API, Mountaya's hosted embed. `address` is the bound on one client
22
+ * IP address: on the Tile API a key sent with no session token, the hosted embed and a share token
23
+ * are held to it, and on both APIs a session token minted for `studio-embedded` without `user_id`.
24
+ * `member` and `studio-embedded` are the bounds a session token minted for `studio-embedded` naming
25
+ * a member is held to, its member's and its organization's. `share` is the bound every share link
26
+ * of one organization shares, on the Tile API.
27
+ */
28
+ export interface RateLimitPolicy {
29
+ /** The policy's name, as `RateLimit-Policy` names it. */
30
+ readonly name: string;
31
+ /** The requests the policy allows per window: its `q`, a whole number of zero or more. */
32
+ readonly quota: number;
33
+ /** What is left of the quota in the current window, where the answer states it. */
34
+ readonly remaining?: number | undefined;
35
+ /** The seconds until the window resets, where the answer states it. */
36
+ readonly resetSeconds?: number | undefined;
37
+ /** The window's length in seconds, where the policy states it. */
38
+ readonly windowSeconds?: number | undefined;
39
+ }
40
+
41
+ /**
42
+ * RateLimitInfo is the rate-limit state an answer describes: one policy per member of its
43
+ * `RateLimit-Policy`, in the order the field lists them.
44
+ */
45
+ export interface RateLimitInfo {
46
+ /** The policies the answer was counted against, never empty. */
47
+ readonly policies: readonly RateLimitPolicy[];
48
+ }
49
+
50
+ /** One parameter's value, or an item's: the bare items RFC 9651 knows, by kind. */
51
+ type BareItem =
52
+ | { readonly kind: "boolean"; readonly value: boolean }
53
+ | { readonly kind: "bytes" }
54
+ | { readonly kind: "date"; readonly value: number }
55
+ | { readonly kind: "decimal"; readonly value: number }
56
+ | { readonly kind: "display"; readonly value: string }
57
+ | { readonly kind: "integer"; readonly value: number }
58
+ | { readonly kind: "string"; readonly value: string }
59
+ | { readonly kind: "token"; readonly value: string };
60
+
61
+ /** One member of a List: an item or an inner list, with its parameters. */
62
+ interface Member {
63
+ /** The member's bare item, or none for an inner list. */
64
+ readonly item: BareItem | undefined;
65
+ /** The member's parameters, by key; a key written twice keeps the last value. */
66
+ readonly parameters: ReadonlyMap<string, BareItem>;
67
+ }
68
+
69
+ const digitPattern = /[0-9]/;
70
+ const alphaPattern = /[A-Za-z]/;
71
+ const lowercaseAlphaPattern = /[a-z]/;
72
+ const keyCharPattern = /[a-z0-9_.*-]/;
73
+ // A token's characters past its first: tchar, ":" and "/".
74
+ const tokenCharPattern = /[!#$%&'*+\-.^_`|~0-9A-Za-z:/]/;
75
+ const base64CharPattern = /[A-Za-z0-9+/=]/;
76
+
77
+ /**
78
+ * The parser of RFC 9651 section 4.2, over one field value, as a cursor over its characters. Each
79
+ * method consumes what it parses and answers undefined where the input is no Structured Field,
80
+ * which {@link parseList} turns into no list at all.
81
+ */
82
+ class Cursor {
83
+ private index = 0;
84
+
85
+ constructor(private readonly text: string) {}
86
+
87
+ /** Whether every character is consumed. */
88
+ get done(): boolean {
89
+ return this.index >= this.text.length;
90
+ }
91
+
92
+ /**
93
+ * The next character, or "" at the end. A method rather than a getter, so a comparison with it
94
+ * is not taken to hold after the cursor moves.
95
+ */
96
+ private peek(): string {
97
+ return this.text[this.index] ?? "";
98
+ }
99
+
100
+ /** Consumes one character and answers it. */
101
+ private take(): string {
102
+ const char = this.peek();
103
+ this.index += 1;
104
+
105
+ return char;
106
+ }
107
+
108
+ /** Discards spaces. */
109
+ skipSpaces(): void {
110
+ while (this.peek() === " ") {
111
+ this.index += 1;
112
+ }
113
+ }
114
+
115
+ /** Discards optional whitespace: spaces and tabs. */
116
+ skipWhitespace(): void {
117
+ while (this.peek() === " " || this.peek() === "\t") {
118
+ this.index += 1;
119
+ }
120
+ }
121
+
122
+ /** Consumes `char` and answers whether the next character was it. */
123
+ consume(char: string): boolean {
124
+ return this.take() === char;
125
+ }
126
+
127
+ /** An item or an inner list, and its parameters. */
128
+ member(): Member | undefined {
129
+ if (this.peek() === "(") {
130
+ if (!this.innerList()) {
131
+ return undefined;
132
+ }
133
+
134
+ const parameters = this.parameters();
135
+
136
+ return parameters === undefined ? undefined : { item: undefined, parameters };
137
+ }
138
+
139
+ const item = this.bareItem();
140
+ const parameters = item === undefined ? undefined : this.parameters();
141
+
142
+ return parameters === undefined ? undefined : { item, parameters };
143
+ }
144
+
145
+ /** An inner list, `(item item);params`, whose items are parsed and set aside. */
146
+ private innerList(): boolean {
147
+ this.index += 1;
148
+ for (;;) {
149
+ this.skipSpaces();
150
+ if (this.peek() === ")") {
151
+ this.index += 1;
152
+
153
+ return true;
154
+ }
155
+
156
+ if (this.bareItem() === undefined || this.parameters() === undefined) {
157
+ return false;
158
+ }
159
+
160
+ if (this.peek() !== " " && this.peek() !== ")") {
161
+ return false;
162
+ }
163
+ }
164
+ }
165
+
166
+ /** The parameters after an item: `;key=value`, a key alone being `true`. */
167
+ private parameters(): Map<string, BareItem> | undefined {
168
+ const parameters = new Map<string, BareItem>();
169
+
170
+ while (this.peek() === ";") {
171
+ this.index += 1;
172
+ this.skipSpaces();
173
+ const key = this.key();
174
+ if (key === undefined) {
175
+ return undefined;
176
+ }
177
+
178
+ let value: BareItem | undefined = { kind: "boolean", value: true };
179
+ if (this.peek() === "=") {
180
+ this.index += 1;
181
+ value = this.bareItem();
182
+ }
183
+
184
+ if (value === undefined) {
185
+ return undefined;
186
+ }
187
+
188
+ parameters.set(key, value);
189
+ }
190
+
191
+ return parameters;
192
+ }
193
+
194
+ /** A parameter's key: a lowercase letter or `*`, then lowercase letters, digits, `_-.*`. */
195
+ private key(): string | undefined {
196
+ if (!lowercaseAlphaPattern.test(this.peek()) && this.peek() !== "*") {
197
+ return undefined;
198
+ }
199
+
200
+ let key = this.take();
201
+ while (keyCharPattern.test(this.peek())) {
202
+ key += this.take();
203
+ }
204
+
205
+ return key;
206
+ }
207
+
208
+ /** One bare item, its kind decided by its first character. */
209
+ private bareItem(): BareItem | undefined {
210
+ const first = this.peek();
211
+
212
+ if (first === "-" || digitPattern.test(first)) {
213
+ return this.number();
214
+ }
215
+
216
+ if (first === '"') {
217
+ const value = this.string();
218
+
219
+ return value === undefined ? undefined : { kind: "string", value };
220
+ }
221
+
222
+ if (first === "*" || alphaPattern.test(first)) {
223
+ return { kind: "token", value: this.token() };
224
+ }
225
+
226
+ if (first === ":") {
227
+ return this.bytes() ? { kind: "bytes" } : undefined;
228
+ }
229
+
230
+ if (first === "?") {
231
+ this.index += 1;
232
+ const value = this.take();
233
+
234
+ return value === "0" || value === "1" ? { kind: "boolean", value: value === "1" } : undefined;
235
+ }
236
+
237
+ if (first === "@") {
238
+ this.index += 1;
239
+ const date = this.number();
240
+
241
+ return date?.kind === "integer" ? { kind: "date", value: date.value } : undefined;
242
+ }
243
+
244
+ if (first === "%") {
245
+ this.index += 1;
246
+ const value = this.displayString();
247
+
248
+ return value === undefined ? undefined : { kind: "display", value };
249
+ }
250
+
251
+ return undefined;
252
+ }
253
+
254
+ /** An integer of at most 15 digits, or a decimal of at most 12 before its point and 3 after. */
255
+ private number(): BareItem | undefined {
256
+ let sign = 1;
257
+ if (this.peek() === "-") {
258
+ sign = -1;
259
+ this.index += 1;
260
+ }
261
+
262
+ let digits = "";
263
+ let decimal = false;
264
+ while (digitPattern.test(this.peek()) || (this.peek() === "." && !decimal)) {
265
+ if (this.peek() === ".") {
266
+ if (digits.length > 12) {
267
+ return undefined;
268
+ }
269
+
270
+ decimal = true;
271
+ }
272
+
273
+ digits += this.take();
274
+ if ((!decimal && digits.length > 15) || (decimal && digits.length > 16)) {
275
+ return undefined;
276
+ }
277
+ }
278
+
279
+ if (digits === "" || digits.startsWith(".")) {
280
+ return undefined;
281
+ }
282
+
283
+ if (!decimal) {
284
+ return { kind: "integer", value: sign * Number(digits) };
285
+ }
286
+
287
+ const fraction = digits.slice(digits.indexOf(".") + 1);
288
+ if (fraction.length === 0 || fraction.length > 3) {
289
+ return undefined;
290
+ }
291
+
292
+ return { kind: "decimal", value: sign * Number(digits) };
293
+ }
294
+
295
+ /** A quoted string of printable ASCII, whose only escapes are `\"` and `\\`. */
296
+ private string(): string | undefined {
297
+ this.index += 1;
298
+ let value = "";
299
+
300
+ for (;;) {
301
+ if (this.done) {
302
+ return undefined;
303
+ }
304
+
305
+ const char = this.take();
306
+ if (char === '"') {
307
+ return value;
308
+ }
309
+
310
+ if (char === "\\") {
311
+ const escaped = this.take();
312
+ if (escaped !== '"' && escaped !== "\\") {
313
+ return undefined;
314
+ }
315
+
316
+ value += escaped;
317
+ continue;
318
+ }
319
+
320
+ const code = char.charCodeAt(0);
321
+ if (code < 0x20 || code > 0x7e) {
322
+ return undefined;
323
+ }
324
+
325
+ value += char;
326
+ }
327
+ }
328
+
329
+ /** A token: a letter or `*`, then tchar, `:` and `/`. */
330
+ private token(): string {
331
+ let value = this.take();
332
+ while (tokenCharPattern.test(this.peek())) {
333
+ value += this.take();
334
+ }
335
+
336
+ return value;
337
+ }
338
+
339
+ /** A byte sequence, `:base64:`, read for its shape alone. */
340
+ private bytes(): boolean {
341
+ this.index += 1;
342
+ while (base64CharPattern.test(this.peek())) {
343
+ this.index += 1;
344
+ }
345
+
346
+ return this.consume(":");
347
+ }
348
+
349
+ /** A display string, `"…"` with each byte past ASCII percent-encoded as lowercase hex. */
350
+ private displayString(): string | undefined {
351
+ if (!this.consume('"')) {
352
+ return undefined;
353
+ }
354
+
355
+ const bytes: number[] = [];
356
+
357
+ for (;;) {
358
+ if (this.done) {
359
+ return undefined;
360
+ }
361
+
362
+ const char = this.take();
363
+ const code = char.charCodeAt(0);
364
+ if (code < 0x20 || code > 0x7e) {
365
+ return undefined;
366
+ }
367
+
368
+ if (char === '"') {
369
+ try {
370
+ return new TextDecoder("utf-8", { fatal: true }).decode(Uint8Array.from(bytes));
371
+ } catch {
372
+ // Bytes that are not UTF-8, which no display string carries.
373
+ return undefined;
374
+ }
375
+ }
376
+
377
+ if (char === "%") {
378
+ const hex = this.take() + this.take();
379
+ if (!/^[0-9a-f]{2}$/.test(hex)) {
380
+ return undefined;
381
+ }
382
+
383
+ bytes.push(Number.parseInt(hex, 16));
384
+ continue;
385
+ }
386
+
387
+ bytes.push(code);
388
+ }
389
+ }
390
+ }
391
+
392
+ /**
393
+ * parseList parses one field value as a Structured Field List, or answers undefined where it is
394
+ * none: RFC 9651 has a recipient ignore such a field whole.
395
+ */
396
+ const parseList = (text: string): readonly Member[] | undefined => {
397
+ const cursor = new Cursor(text);
398
+ const members: Member[] = [];
399
+
400
+ cursor.skipSpaces();
401
+ while (!cursor.done) {
402
+ const member = cursor.member();
403
+ if (member === undefined) {
404
+ return undefined;
405
+ }
406
+
407
+ members.push(member);
408
+ cursor.skipWhitespace();
409
+ if (cursor.done) {
410
+ break;
411
+ }
412
+
413
+ if (!cursor.consume(",")) {
414
+ return undefined;
415
+ }
416
+
417
+ cursor.skipWhitespace();
418
+ // A trailing comma is no list.
419
+ if (cursor.done) {
420
+ return undefined;
421
+ }
422
+ }
423
+
424
+ return members;
425
+ };
426
+
427
+ /** A parameter's value where it is a whole number of zero or more, and undefined otherwise. */
428
+ const wholeNumber = (item: BareItem | undefined): number | undefined =>
429
+ item?.kind === "integer" && item.value >= 0 ? item.value : undefined;
430
+
431
+ /** The String a member names its policy by, or undefined where it is no String. */
432
+ const policyName = (member: Member): string | undefined =>
433
+ member.item?.kind === "string" ? member.item.value : undefined;
434
+
435
+ /**
436
+ * parseRateLimit reads the rate-limit state an answer's headers describe, or undefined where they
437
+ * describe none.
438
+ *
439
+ * One policy per String member of `RateLimit-Policy`, in the order it lists them, each with its
440
+ * name, its quota `q` and its window `w`, and with the remaining figure `r` and the reset `t` of
441
+ * the first `RateLimit` member of the same name, so a policy listed twice is kept twice, each read
442
+ * from that one member. A figure the answer does not state is undefined. A
443
+ * member that is no String, or whose `q` is no whole number of zero or more, is left out, and so
444
+ * is a `RateLimit` member naming no policy the other field lists, since its quota is unknown; a
445
+ * parameter this client does not know is ignored; and a field that does not parse as a Structured
446
+ * Field List is ignored whole. An answer whose `RateLimit-Policy` leaves no policy describes none.
447
+ *
448
+ * @param headers - The answer's headers, as a `Response` carries them.
449
+ * @returns The state, or undefined where the answer describes no policy.
450
+ *
451
+ * @example
452
+ * ```ts
453
+ * import { parseRateLimit } from "@mountaya/client";
454
+ *
455
+ * declare const publishableKey: string;
456
+ * declare const sessionToken: string;
457
+ *
458
+ * // An answer made under a key, which the Data API describes the limits of.
459
+ * const response = await fetch("https://data.mountaya.com/graphql", {
460
+ * body: JSON.stringify({ query: "query { __typename }" }),
461
+ * headers: {
462
+ * "Content-Type": "application/json",
463
+ * "X-API-Key": publishableKey,
464
+ * "X-Session-Token": sessionToken,
465
+ * },
466
+ * method: "POST",
467
+ * });
468
+ * const minute = parseRateLimit(response.headers)?.policies.find(({ name }) => name === "minute");
469
+ * ```
470
+ */
471
+ export const parseRateLimit = (headers: Pick<Headers, "get">): RateLimitInfo | undefined => {
472
+ const policyField = headers.get("RateLimit-Policy");
473
+ const declared = policyField === null ? undefined : parseList(policyField);
474
+ if (declared === undefined) {
475
+ return undefined;
476
+ }
477
+
478
+ const stateField = headers.get("RateLimit");
479
+ const states = new Map<string, Member>();
480
+ for (const member of (stateField === null ? undefined : parseList(stateField)) ?? []) {
481
+ const name = policyName(member);
482
+ // A name given twice is read from its first member, as every SDK reads it.
483
+ if (name !== undefined && !states.has(name)) {
484
+ states.set(name, member);
485
+ }
486
+ }
487
+
488
+ const policies: RateLimitPolicy[] = [];
489
+ for (const member of declared) {
490
+ const name = policyName(member);
491
+ const quota = wholeNumber(member.parameters.get("q"));
492
+ if (name === undefined || quota === undefined) {
493
+ continue;
494
+ }
495
+
496
+ const state = states.get(name)?.parameters;
497
+ policies.push({
498
+ name,
499
+ quota,
500
+ remaining: wholeNumber(state?.get("r")),
501
+ resetSeconds: wholeNumber(state?.get("t")),
502
+ windowSeconds: wholeNumber(member.parameters.get("w")),
503
+ });
504
+ }
505
+
506
+ return policies.length === 0 ? undefined : { policies };
507
+ };