@mountaya/client 0.0.0-stage → 0.1.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 (96) hide show
  1. package/CHANGELOG.md +104 -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 +3521 -0
  6. package/dist/admin/index.cjs.map +1 -0
  7. package/dist/admin/index.d.cts +1070 -0
  8. package/dist/admin/index.d.ts +1070 -0
  9. package/dist/admin/index.js +301 -0
  10. package/dist/admin/index.js.map +1 -0
  11. package/dist/browser.cjs +8073 -0
  12. package/dist/browser.cjs.map +1 -0
  13. package/dist/browser.d.cts +106 -0
  14. package/dist/browser.d.ts +106 -0
  15. package/dist/browser.js +53 -0
  16. package/dist/browser.js.map +1 -0
  17. package/dist/chunk-BM526BHG.js +342 -0
  18. package/dist/chunk-BM526BHG.js.map +1 -0
  19. package/dist/chunk-D2YEVTG7.js +327 -0
  20. package/dist/chunk-D2YEVTG7.js.map +1 -0
  21. package/dist/chunk-OHKVUREN.js +3500 -0
  22. package/dist/chunk-OHKVUREN.js.map +1 -0
  23. package/dist/chunk-TDTK2L5H.js +3842 -0
  24. package/dist/chunk-TDTK2L5H.js.map +1 -0
  25. package/dist/coreOperations.gen-BMYPoBbz.d.ts +1760 -0
  26. package/dist/coreOperations.gen-BxX5xRxw.d.cts +1760 -0
  27. package/dist/errors-B-GqAtQv.d.cts +75960 -0
  28. package/dist/errors-B-GqAtQv.d.ts +75960 -0
  29. package/dist/events/index.cjs +1972 -0
  30. package/dist/events/index.cjs.map +1 -0
  31. package/dist/events/index.d.cts +781 -0
  32. package/dist/events/index.d.ts +781 -0
  33. package/dist/events/index.js +826 -0
  34. package/dist/events/index.js.map +1 -0
  35. package/dist/index.cjs +8072 -0
  36. package/dist/index.cjs.map +1 -0
  37. package/dist/index.d.cts +76 -0
  38. package/dist/index.d.ts +76 -0
  39. package/dist/index.js +52 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/operations.gen-CvTyzKNF.d.cts +3993 -0
  42. package/dist/operations.gen-DUD406TV.d.ts +3993 -0
  43. package/package.json +127 -4
  44. package/src/admin/index.ts +47 -0
  45. package/src/admin/rules/mountayaAdmin.ts +102 -0
  46. package/src/browser.ts +232 -0
  47. package/src/core/engines/sessionToken.ts +870 -0
  48. package/src/core/rules/auth.ts +1017 -0
  49. package/src/core/rules/clientHeader.ts +28 -0
  50. package/src/core/rules/dates.ts +200 -0
  51. package/src/core/rules/decode.ts +154 -0
  52. package/src/core/rules/envelope.ts +41 -0
  53. package/src/core/rules/errors.ts +1486 -0
  54. package/src/core/rules/graphql.ts +576 -0
  55. package/src/core/rules/headerValue.ts +42 -0
  56. package/src/core/rules/idempotencyKey.ts +64 -0
  57. package/src/core/rules/nullMask.ts +127 -0
  58. package/src/core/rules/nullMasks.gen.ts +416 -0
  59. package/src/core/rules/paginate.ts +316 -0
  60. package/src/core/rules/rateLimit.ts +507 -0
  61. package/src/core/rules/redaction.ts +219 -0
  62. package/src/core/rules/resourceRef.ts +110 -0
  63. package/src/core/rules/responseBody.ts +492 -0
  64. package/src/core/rules/retry.ts +386 -0
  65. package/src/core/rules/selection.ts +416 -0
  66. package/src/core/rules/sessionAnswer.ts +90 -0
  67. package/src/core/rules/sessionSchedule.ts +131 -0
  68. package/src/core/rules/sessionWait.ts +112 -0
  69. package/src/core/rules/transport.ts +2386 -0
  70. package/src/core/rules/version.gen.ts +16 -0
  71. package/src/core/rules/webAddress.ts +165 -0
  72. package/src/core/rules/webAddresses.gen.ts +30 -0
  73. package/src/data/operations.gen.ts +603 -0
  74. package/src/data/schema.gen.ts +8078 -0
  75. package/src/data/selection.gen.ts +472 -0
  76. package/src/events/index.ts +67 -0
  77. package/src/events/rules/agent.ts +439 -0
  78. package/src/events/rules/collab.ts +554 -0
  79. package/src/events/rules/collabNulls.gen.ts +17 -0
  80. package/src/events/rules/eventStream.ts +94 -0
  81. package/src/events/rules/openSseStream.ts +594 -0
  82. package/src/events/rules/sse.ts +349 -0
  83. package/src/index.ts +231 -0
  84. package/src/rest/adminOperations.gen.ts +70 -0
  85. package/src/rest/adminResources.gen.ts +136 -0
  86. package/src/rest/coreOperations.gen.ts +74 -0
  87. package/src/rest/coreResources.gen.ts +244 -0
  88. package/src/rest/dates.gen.ts +217 -0
  89. package/src/rest/operations.gen.ts +4341 -0
  90. package/src/rest/resources.gen.ts +369 -0
  91. package/src/rest/schema.gen.ts +56840 -0
  92. package/src/tiles/operations.gen.ts +206 -0
  93. package/src/tiles/schema.gen.ts +8408 -0
  94. package/src/webhooks/rules/constructWebhookEvent.ts +482 -0
  95. package/src/webhooks/rules/webhookEvent.ts +38 -0
  96. package/src/webhooks/rules/webhookEvents.gen.ts +732 -0
@@ -0,0 +1,219 @@
1
+ /**
2
+ * How the client prints a secret it holds: a session token, a share token and a secret key.
3
+ *
4
+ * A credential is one for as long as it lives, and a log is read by more people than hold it, so
5
+ * no holder of one the client keeps prints it (the contract's SPEC §1, in `mountaya/schemas`):
6
+ * the credential a client is built with and a session token engine's snapshot each print a secret
7
+ * by its prefix alone, which says what kind of secret it is and nothing of it. A printed form is
8
+ * not a read: `snapshot.token` is still the token, and the request a credential authenticates
9
+ * still carries the whole of it.
10
+ *
11
+ * A holder answers each way JavaScript prints a value: `JSON.stringify` asks it for `toJSON`,
12
+ * `String` and a template literal ask it for `toString`, and Node's `util.inspect`, which
13
+ * `console.log` prints through, asks it for the method its registered symbol keys. Each is a
14
+ * property of the holder's own that no enumeration reads, so a spread, `Object.keys`, an equality
15
+ * and a structured clone read the holder as they would without them. A browser's console shows a
16
+ * logged object live rather than printing it, so what it shows is the object itself.
17
+ *
18
+ * A decoded answer is not a holder the client keeps: a parsed `Session` is a plain object, and it
19
+ * prints every member, its token included.
20
+ */
21
+
22
+ import type { Credential } from "./auth.js";
23
+
24
+ /**
25
+ * INSPECT is the key Node's `util.inspect` reads a value's own way of printing itself by. It is
26
+ * the registered symbol rather than the one `node:util` exports, which is the same symbol, so a
27
+ * module that runs in a page, a worker or React Native imports nothing of Node to declare it.
28
+ */
29
+ const INSPECT = Symbol.for("nodejs.util.inspect.custom");
30
+
31
+ /**
32
+ * CREDENTIAL_MEMBERS are the members a credential holds, each read from it at every read, and for
33
+ * a secret one the prefix it starts with, the part of it a printed credential shows. A publishable
34
+ * key is no secret: a page carries it to every visitor, so a printed credential shows it whole.
35
+ */
36
+ const CREDENTIAL_MEMBERS: readonly (readonly [member: string, secretPrefix?: string])[] = [
37
+ ["cookie"],
38
+ ["publishableKey"],
39
+ ["secretKey", "sk_"],
40
+ ["sessionToken", "sess_"],
41
+ ["shareToken", "share_"],
42
+ ];
43
+
44
+ /**
45
+ * redactedSecret is how a secret prints: by `prefix` alone where it starts with it, followed by an
46
+ * ellipsis, and by the ellipsis alone where it does not, since the start of a value that is not
47
+ * the secret it should be may be the start of another one.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * redactedSecret("sess_8f14e45fceea", "sess_"); // "sess_…"
52
+ * ```
53
+ */
54
+ export const redactedSecret = (value: string, prefix: string): string =>
55
+ value.startsWith(prefix) ? `${prefix}…` : "…";
56
+
57
+ /**
58
+ * redactedSessionToken is how a session token prints: by the prefix every session token starts
59
+ * with, which {@link CREDENTIAL_MEMBERS} states once for the credential and the snapshot alike.
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * redactedSessionToken("sess_8f14e45fceea"); // "sess_…"
64
+ * ```
65
+ */
66
+ export const redactedSessionToken = (token: string): string => {
67
+ const [, prefix] = CREDENTIAL_MEMBERS.find(([member]) => member === "sessionToken") ?? [];
68
+
69
+ return prefix === undefined ? "…" : redactedSecret(token, prefix);
70
+ };
71
+
72
+ /**
73
+ * printedText is the text `String` prints a holder as: the JSON of its printed form, or what
74
+ * `String` prints any object as where that form holds a member JSON cannot write, such as a cycle.
75
+ */
76
+ const printedText = (printed: Readonly<Record<string, unknown>>): string => {
77
+ try {
78
+ return JSON.stringify(printed);
79
+ } catch {
80
+ return "[object Object]";
81
+ }
82
+ };
83
+
84
+ /**
85
+ * printsAs gives `holder` its printed form, `printed`, in every form JavaScript prints a value in:
86
+ * `JSON.stringify` writes it, `String` writes its JSON, and Node's `util.inspect` and
87
+ * `console.log` print it. It answers `holder` itself, every member of it read as before.
88
+ *
89
+ * `printed` is called at each print with the holder, so a form built from the holder's own members
90
+ * prints what the holder holds then.
91
+ */
92
+ export const printsAs = <THolder extends object>(
93
+ holder: THolder,
94
+ printed: (holder: THolder) => Readonly<Record<string, unknown>>,
95
+ ): THolder =>
96
+ Object.defineProperties(holder, {
97
+ [INSPECT]: { value: () => printed(holder) },
98
+ toJSON: { value: () => printed(holder) },
99
+ toString: { value: () => printedText(printed(holder)) },
100
+ });
101
+
102
+ /**
103
+ * printedSecret is how a credential member holding `value` prints, `secretPrefix` being the prefix
104
+ * of a secret member: a secret string by its prefix alone, and a session token held as a source as
105
+ * the snapshot the source holds now, its token by the same prefix alone, since the source is the
106
+ * caller's own object and what else it holds is not the client's to print. Any other value prints
107
+ * as it is: a function as its name, never its body.
108
+ */
109
+ const printedSecret = (value: unknown, secretPrefix: string | undefined): unknown => {
110
+ if (secretPrefix === undefined) {
111
+ return value;
112
+ }
113
+
114
+ if (typeof value === "string") {
115
+ return redactedSecret(value, secretPrefix);
116
+ }
117
+
118
+ if (typeof value !== "object" || value === null) {
119
+ return value;
120
+ }
121
+
122
+ const getSnapshot: unknown = Reflect.get(value, "getSnapshot");
123
+ if (typeof getSnapshot !== "function") {
124
+ return value;
125
+ }
126
+
127
+ // Read as unknown: a source of the caller's own may answer anything there.
128
+ const snapshot: unknown = Reflect.apply(getSnapshot, value, []);
129
+ if (typeof snapshot !== "object" || snapshot === null) {
130
+ return snapshot;
131
+ }
132
+
133
+ const token: unknown = Reflect.get(snapshot, "token");
134
+
135
+ return {
136
+ ...snapshot,
137
+ token: typeof token === "string" ? redactedSecret(token, secretPrefix) : token,
138
+ };
139
+ };
140
+
141
+ /**
142
+ * printedCredential is a credential's printed form, or that of a projection of one: each member it
143
+ * holds, each secret redacted.
144
+ */
145
+ const printedCredential = (credential: object): Readonly<Record<string, unknown>> => {
146
+ const printed: Record<string, unknown> = {};
147
+
148
+ for (const [member, secretPrefix] of CREDENTIAL_MEMBERS) {
149
+ const value: unknown = Reflect.get(credential, member);
150
+ if (value !== undefined) {
151
+ printed[member] = printedSecret(value, secretPrefix);
152
+ }
153
+ }
154
+
155
+ return printed;
156
+ };
157
+
158
+ /**
159
+ * printableCredential answers the credential a client keeps: a view of `credential` that reads
160
+ * each member from it at every read, as the client reads the caller's own object, and that prints
161
+ * each secret it holds by its prefix alone.
162
+ *
163
+ * A view rather than a copy, so a member the caller's object computes as it is read, or changes
164
+ * once the client is built, reads as it would without it.
165
+ */
166
+ export const printableCredential = (credential: Credential): Credential => {
167
+ const view = {};
168
+
169
+ for (const [member] of CREDENTIAL_MEMBERS) {
170
+ Object.defineProperty(view, member, {
171
+ enumerable: true,
172
+ get: (): unknown => Reflect.get(credential, member),
173
+ });
174
+ }
175
+
176
+ // The boundary of the view: every member a credential declares is a getter reading the caller's
177
+ // own credential, so the view reads as that credential does, and the compiler cannot see it.
178
+ return printsAs(view as Credential, () => printedCredential(credential));
179
+ };
180
+
181
+ /**
182
+ * printableProjection answers `projection`, an object holding members of a credential (the
183
+ * browser-safe half a transport hands a renderer), printing each secret it holds by its prefix
184
+ * alone, as the credential the client keeps prints it. Every member reads as it did: a renderer
185
+ * reads the token whole, and a log of the projection shows its prefix.
186
+ */
187
+ export const printableProjection = <TProjection extends object>(
188
+ projection: TProjection,
189
+ ): TProjection => printsAs(projection, () => printedCredential(projection));
190
+
191
+ /**
192
+ * secretDigest names a secret in a value read by more than its holder, a cache key above all,
193
+ * without holding the secret: the same secret is named alike in every process, so a key written
194
+ * on a server and read in a page agree, and two secrets are named apart. It is a 53-bit digest
195
+ * written in base 36, a name and no protection, from which no secret of a credential's length is
196
+ * recovered.
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * secretDigest("sess_8f14e45fceea"); // the same text at every call, holding no part of the token
201
+ * ```
202
+ */
203
+ export const secretDigest = (secret: string): string => {
204
+ // Two 32-bit lanes multiplied through the text and mixed into each other, written as one
205
+ // 53-bit integer, the most a number holds exactly.
206
+ let first = 0xdeadbeef;
207
+ let second = 0x41c6ce57;
208
+ for (let index = 0; index < secret.length; index += 1) {
209
+ const unit = secret.charCodeAt(index);
210
+ first = Math.imul(first ^ unit, 2654435761);
211
+ second = Math.imul(second ^ unit, 1597334677);
212
+ }
213
+ first =
214
+ Math.imul(first ^ (first >>> 16), 2246822507) ^ Math.imul(second ^ (second >>> 13), 3266489909);
215
+ second =
216
+ Math.imul(second ^ (second >>> 16), 2246822507) ^ Math.imul(first ^ (first >>> 13), 3266489909);
217
+
218
+ return (4294967296 * (2097151 & second) + (first >>> 0)).toString(36);
219
+ };
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Where a studio's server state lives, named as a RESOURCE rather than as a request.
3
+ *
4
+ * The refs name what this package FETCHES: an itinerary, a zone, a collection, a listing of
5
+ * them. A cache above the transport (a binding's query keys, a studio's own store) decides what
6
+ * it holds and when to drop it, and keys its entries by these refs, so a binding builds a query
7
+ * key for a resource without naming whoever holds it. The transport neither caches nor knows who
8
+ * does: a ref is a reference to one resource, which every SDK's transport has a use for.
9
+ */
10
+
11
+ import type { operations } from "../../rest/schema.gen.js";
12
+ import type { operations as tileOperations } from "../../tiles/schema.gen.js";
13
+
14
+ /**
15
+ * ListingFilters is what narrows one listing: the query parameters of its operation, less the
16
+ * page it is cut into. The cursor and the page size choose a page of a listing, not which
17
+ * listing it is, so a ref never carries them.
18
+ */
19
+ type ListingFilters<Op extends "getCollections" | "getItineraries" | "getZones"> = Omit<
20
+ NonNullable<operations[Op]["parameters"]["query"]>,
21
+ "cursor" | "limit"
22
+ >;
23
+
24
+ /**
25
+ * CollectionSpecificationQuery is what a collection's specification is read with: the layers it
26
+ * declares, each the organization's own default where it is left out.
27
+ */
28
+ type CollectionSpecificationQuery = NonNullable<
29
+ tileOperations["getCollectionSpecification"]["parameters"]["query"]
30
+ >;
31
+
32
+ /**
33
+ * ResourceRef names one resource a cache above the transport may hold.
34
+ *
35
+ * A member with an optional narrowing field means both things: supplied, it names one entry;
36
+ * omitted, it names the family. A listing's `filters` narrow the same way, member by member:
37
+ * `{ kind: "collections", filters: { itinerary_id } }` names every listing of the collections
38
+ * that hold one itinerary, whatever else it was filtered or sorted by, and
39
+ * `{ kind: "collections" }` names every collection listing, which is what a write that changes
40
+ * a collection has to drop, since each listing serves the collection's own record.
41
+ */
42
+ export type ResourceRef =
43
+ /** The activity catalog. One entry, no id: it is the same list for every reader. */
44
+ | { kind: "activities" }
45
+ /** One collection's own record. */
46
+ | { kind: "collection"; id: string }
47
+ /**
48
+ * The itineraries one collection holds, as its contents listing (`getCollectionItineraries`)
49
+ * reads them: every walk of it, whatever its order or page size. `id` omitted names every
50
+ * collection's, which a write that does not know where an itinerary was filed has to drop.
51
+ */
52
+ | { kind: "collectionItineraries"; id?: string | undefined }
53
+ /**
54
+ * One collection's contents, as the specification a viewer renders, read with the `query` it
55
+ * names. `query` omitted names every read of the collection's specification, whatever layers it
56
+ * asked for, and `id` omitted names every collection's: a write that changes a specification
57
+ * without knowing which collections hold what it wrote drops the family. A zone's publication
58
+ * is not such a write: the specification declares its zone layers by the toggles alone and its
59
+ * `bbox` counts itineraries alone, so publishing or withdrawing a zone moves the collection's
60
+ * tiles and leaves its specification as it was.
61
+ */
62
+ | {
63
+ kind: "collectionSpecification";
64
+ id?: string | undefined;
65
+ query?: CollectionSpecificationQuery | undefined;
66
+ }
67
+ /**
68
+ * The zones one collection holds, as its contents listing (`getCollectionZones`) reads them:
69
+ * every walk of it, whatever its order or page size. `id` omitted names every collection's.
70
+ */
71
+ | { kind: "collectionZones"; id?: string | undefined }
72
+ /**
73
+ * The collection listing (`getCollections`), narrowed by the `filters` it was read with;
74
+ * omitted, every collection listing.
75
+ */
76
+ | { kind: "collections"; filters?: ListingFilters<"getCollections"> | undefined }
77
+ /**
78
+ * The itinerary listing (`getItineraries`), narrowed by the `filters` it was read with;
79
+ * omitted, every itinerary listing, which a create or a delete has to drop because it does
80
+ * not know which listing a reader currently has open.
81
+ */
82
+ | { kind: "itineraries"; filters?: ListingFilters<"getItineraries"> | undefined }
83
+ /**
84
+ * One itinerary. `surface` selects the DRAFT the editor holds or the PUBLISHED record a
85
+ * visitor reads; omitted means the draft, which is what a write always touches.
86
+ */
87
+ | { kind: "itinerary"; id: string; surface?: "draft" | "published" | undefined }
88
+ /** One itinerary's contents, as the specification a viewer renders. */
89
+ | { kind: "itinerarySpecification"; id: string }
90
+ /**
91
+ * The layers manifest: the terrains, the overlays and the always-on slots a credential may
92
+ * draw. No id: the manifest is narrowed to the credential that reads it, and a cache holds
93
+ * the reads of one credential. `organizationId` names the studio scope it was read for, the
94
+ * `organization_id` of `getLayers`; omitted, it is the read that names none, and the family
95
+ * of every scope's read, which is what a change to the catalogue has to drop.
96
+ */
97
+ | { kind: "layers"; organizationId?: string | undefined }
98
+ /**
99
+ * One zone. `surface` selects the DRAFT the editor holds or the PUBLISHED record a visitor
100
+ * reads; omitted means the draft, which is what a write always touches.
101
+ */
102
+ | { kind: "zone"; id: string; surface?: "draft" | "published" | undefined }
103
+ /**
104
+ * The zone listing (`getZones`), narrowed by the `filters` it was read with; omitted, every
105
+ * zone listing.
106
+ */
107
+ | { kind: "zones"; filters?: ListingFilters<"getZones"> | undefined };
108
+
109
+ /** ResourceKind is the `kind` discriminant {@link ResourceRef} carries, as a union of its own. */
110
+ export type ResourceKind = ResourceRef["kind"];