@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.
- package/CHANGELOG.md +104 -0
- package/LICENCE.fr.txt +280 -0
- package/LICENSE.txt +262 -0
- package/README.md +1260 -2
- package/dist/admin/index.cjs +3521 -0
- package/dist/admin/index.cjs.map +1 -0
- package/dist/admin/index.d.cts +1070 -0
- package/dist/admin/index.d.ts +1070 -0
- package/dist/admin/index.js +301 -0
- package/dist/admin/index.js.map +1 -0
- package/dist/browser.cjs +8073 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.cts +106 -0
- package/dist/browser.d.ts +106 -0
- package/dist/browser.js +53 -0
- package/dist/browser.js.map +1 -0
- package/dist/chunk-BM526BHG.js +342 -0
- package/dist/chunk-BM526BHG.js.map +1 -0
- package/dist/chunk-D2YEVTG7.js +327 -0
- package/dist/chunk-D2YEVTG7.js.map +1 -0
- package/dist/chunk-OHKVUREN.js +3500 -0
- package/dist/chunk-OHKVUREN.js.map +1 -0
- package/dist/chunk-TDTK2L5H.js +3842 -0
- package/dist/chunk-TDTK2L5H.js.map +1 -0
- package/dist/coreOperations.gen-BMYPoBbz.d.ts +1760 -0
- package/dist/coreOperations.gen-BxX5xRxw.d.cts +1760 -0
- package/dist/errors-B-GqAtQv.d.cts +75960 -0
- package/dist/errors-B-GqAtQv.d.ts +75960 -0
- package/dist/events/index.cjs +1972 -0
- package/dist/events/index.cjs.map +1 -0
- package/dist/events/index.d.cts +781 -0
- package/dist/events/index.d.ts +781 -0
- package/dist/events/index.js +826 -0
- package/dist/events/index.js.map +1 -0
- package/dist/index.cjs +8072 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +76 -0
- package/dist/index.d.ts +76 -0
- package/dist/index.js +52 -0
- package/dist/index.js.map +1 -0
- package/dist/operations.gen-CvTyzKNF.d.cts +3993 -0
- package/dist/operations.gen-DUD406TV.d.ts +3993 -0
- package/package.json +127 -4
- package/src/admin/index.ts +47 -0
- package/src/admin/rules/mountayaAdmin.ts +102 -0
- package/src/browser.ts +232 -0
- package/src/core/engines/sessionToken.ts +870 -0
- package/src/core/rules/auth.ts +1017 -0
- package/src/core/rules/clientHeader.ts +28 -0
- package/src/core/rules/dates.ts +200 -0
- package/src/core/rules/decode.ts +154 -0
- package/src/core/rules/envelope.ts +41 -0
- package/src/core/rules/errors.ts +1486 -0
- package/src/core/rules/graphql.ts +576 -0
- package/src/core/rules/headerValue.ts +42 -0
- package/src/core/rules/idempotencyKey.ts +64 -0
- package/src/core/rules/nullMask.ts +127 -0
- package/src/core/rules/nullMasks.gen.ts +416 -0
- package/src/core/rules/paginate.ts +316 -0
- package/src/core/rules/rateLimit.ts +507 -0
- package/src/core/rules/redaction.ts +219 -0
- package/src/core/rules/resourceRef.ts +110 -0
- package/src/core/rules/responseBody.ts +492 -0
- package/src/core/rules/retry.ts +386 -0
- package/src/core/rules/selection.ts +416 -0
- package/src/core/rules/sessionAnswer.ts +90 -0
- package/src/core/rules/sessionSchedule.ts +131 -0
- package/src/core/rules/sessionWait.ts +112 -0
- package/src/core/rules/transport.ts +2386 -0
- package/src/core/rules/version.gen.ts +16 -0
- package/src/core/rules/webAddress.ts +165 -0
- package/src/core/rules/webAddresses.gen.ts +30 -0
- package/src/data/operations.gen.ts +603 -0
- package/src/data/schema.gen.ts +8078 -0
- package/src/data/selection.gen.ts +472 -0
- package/src/events/index.ts +67 -0
- package/src/events/rules/agent.ts +439 -0
- package/src/events/rules/collab.ts +554 -0
- package/src/events/rules/collabNulls.gen.ts +17 -0
- package/src/events/rules/eventStream.ts +94 -0
- package/src/events/rules/openSseStream.ts +594 -0
- package/src/events/rules/sse.ts +349 -0
- package/src/index.ts +231 -0
- package/src/rest/adminOperations.gen.ts +70 -0
- package/src/rest/adminResources.gen.ts +136 -0
- package/src/rest/coreOperations.gen.ts +74 -0
- package/src/rest/coreResources.gen.ts +244 -0
- package/src/rest/dates.gen.ts +217 -0
- package/src/rest/operations.gen.ts +4341 -0
- package/src/rest/resources.gen.ts +369 -0
- package/src/rest/schema.gen.ts +56840 -0
- package/src/tiles/operations.gen.ts +206 -0
- package/src/tiles/schema.gen.ts +8408 -0
- package/src/webhooks/rules/constructWebhookEvent.ts +482 -0
- package/src/webhooks/rules/webhookEvent.ts +38 -0
- package/src/webhooks/rules/webhookEvents.gen.ts +732 -0
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { MountayaError, refuseOption } from "../../core/rules/errors.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* SseFrame is one Server-Sent Events record that carried data: its `id`, its `event` name, and
|
|
5
|
+
* the raw `data` text, still a string the caller parses and validates.
|
|
6
|
+
*
|
|
7
|
+
* `id` is the record's own `id:` field, and the empty string where the record named none. It is
|
|
8
|
+
* the record's, not the stream's: the position a reconnect resumes from carries over from record
|
|
9
|
+
* to record, and {@link SseFrameParser.lastEventId} holds it. A record that carries no `data:` (a
|
|
10
|
+
* heartbeat comment, an `id:` alone) dispatches no frame.
|
|
11
|
+
*/
|
|
12
|
+
export interface SseFrame {
|
|
13
|
+
/** The record's `data:` lines, joined with newlines, unparsed. */
|
|
14
|
+
data: string;
|
|
15
|
+
/** The record's last `event:` name, or the empty string where it names none. */
|
|
16
|
+
event: string;
|
|
17
|
+
/** The record's own `id:`, or the empty string where it names none. */
|
|
18
|
+
id: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* BYTE_ORDER_MARK is U+FEFF, which the event-stream standard drops once, at the start of a stream.
|
|
23
|
+
*/
|
|
24
|
+
const BYTE_ORDER_MARK = "\uFEFF";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* MAX_SSE_BUFFER_CHARS caps the text of one record a parser has not seen the end of: every
|
|
28
|
+
* character read since the record began, its line endings and comment lines included. A record
|
|
29
|
+
* ends only at an empty line, so a producer (or a proxy, or a hostile endpoint) that opens a
|
|
30
|
+
* `text/event-stream` and never sends one would grow the parser for as long as the connection
|
|
31
|
+
* lives. The bound turns that resource exhaustion into a fault the caller can act on.
|
|
32
|
+
*
|
|
33
|
+
* 8 MiB is measured against the largest legitimate record rather than the typical one: a
|
|
34
|
+
* collaboration frame is kilobytes (a presence or lock roster, one durable change), so even a
|
|
35
|
+
* megabyte-scale record keeps eightfold headroom while a runaway producer is stopped long before
|
|
36
|
+
* it threatens the tab. The unit is UTF-16 code units, a JavaScript string's own `length`, so a
|
|
37
|
+
* record at the cap costs at most twice this many bytes of heap.
|
|
38
|
+
*/
|
|
39
|
+
export const MAX_SSE_BUFFER_CHARS = 8 * 1024 * 1024;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* SseBufferOverflowError is thrown by a parser whose unterminated record has passed its cap. It
|
|
43
|
+
* carries the `limit` it enforced, so a consumer can tell a broken producer apart from a network
|
|
44
|
+
* fault when it classifies the failure. It is a fault, not an outcome: the read it surfaces
|
|
45
|
+
* through is torn down rather than reported as a stream that stopped or ended.
|
|
46
|
+
*
|
|
47
|
+
* A parser knows no operation, so what it throws names none. `openSseStream` hands the overflow
|
|
48
|
+
* on as an error of this class that names the stream's operation, keeps the `limit`, and carries
|
|
49
|
+
* the parser's own error as its `cause`.
|
|
50
|
+
*
|
|
51
|
+
* It extends {@link MountayaError}, so the one catch clause this package documents for telling a
|
|
52
|
+
* platform failure from a bug in the calling program, `if (error instanceof MountayaError)`,
|
|
53
|
+
* covers it. A stream torn down by a malformed producer is a platform failure, and an error outside
|
|
54
|
+
* the family would read as the application's own crash.
|
|
55
|
+
*/
|
|
56
|
+
export class SseBufferOverflowError extends MountayaError {
|
|
57
|
+
// The family's spelling, and not `this.name = …` in the constructor: every class here
|
|
58
|
+
// declares its name as a string LITERAL on the class, because an identifier is what a
|
|
59
|
+
// consumer's minifier renames and data is what survives it. `errors.test.ts` derives that
|
|
60
|
+
// rule from the barrel, and this class is on it.
|
|
61
|
+
override readonly name = "SseBufferOverflowError";
|
|
62
|
+
|
|
63
|
+
/** The cap the parser enforced, in UTF-16 code units. */
|
|
64
|
+
readonly limit: number;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Builds the error a parser throws once its unterminated record passes the cap.
|
|
68
|
+
*
|
|
69
|
+
* @param limit - the cap the parser enforced, in UTF-16 code units.
|
|
70
|
+
* @param operationId - the operation whose stream overflowed, where the thrower knows it: the
|
|
71
|
+
* message then opens with it and ends with what a reopen would meet.
|
|
72
|
+
* @param options - the standard error options, for the `cause` an opener hands on.
|
|
73
|
+
*/
|
|
74
|
+
constructor(limit: number, operationId?: string, options?: ErrorOptions) {
|
|
75
|
+
const overflow = `sse frame parser: unterminated frame exceeded ${limit} characters`;
|
|
76
|
+
|
|
77
|
+
super(
|
|
78
|
+
operationId === undefined
|
|
79
|
+
? overflow
|
|
80
|
+
: `${operationId}: ${overflow}. The producer never ended the record, and a reopen meets it again.`,
|
|
81
|
+
options,
|
|
82
|
+
);
|
|
83
|
+
this.limit = limit;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* SseFrameParser frames the text of a `text/event-stream` body into records, by the HTML
|
|
89
|
+
* standard's event-stream rules:
|
|
90
|
+
*
|
|
91
|
+
* - A line ends at CRLF, LF or a lone CR. A CR ends its line at once, and an LF that follows it,
|
|
92
|
+
* in the same push or at the start of the next, completes that CRLF rather than ending a
|
|
93
|
+
* second, empty line.
|
|
94
|
+
* - A record ends at an empty line. Its `data:` lines join with newlines, its last `event:` names
|
|
95
|
+
* it, and a comment line (a leading `:`, such as a heartbeat) or a field the frame has no slot
|
|
96
|
+
* for (`retry:`) is read past.
|
|
97
|
+
* - One byte-order mark at the start of the stream is dropped.
|
|
98
|
+
* - A record that carries no `data:` dispatches no frame. Its `id:` still moves
|
|
99
|
+
* {@link SseFrameParser.lastEventId}, so a record naming a position and nothing else moves the
|
|
100
|
+
* position a reconnect resumes from.
|
|
101
|
+
* - Every record that carries data dispatches a frame, whatever the data says. No data is an end
|
|
102
|
+
* marker to the parser: what ends a stream is the caller's vocabulary to name.
|
|
103
|
+
*
|
|
104
|
+
* A push may end anywhere, inside a line or inside a record, so the parser holds what is
|
|
105
|
+
* unterminated for the next push. It searches a character for a line ending in the push that
|
|
106
|
+
* delivers it and never again, so framing a long record costs its length however many pushes it
|
|
107
|
+
* arrives in. A character split between two reads of the body arrives whole only if the bytes
|
|
108
|
+
* are decoded across reads, which `readEventStream` does.
|
|
109
|
+
*
|
|
110
|
+
* What it holds is bounded (see {@link MAX_SSE_BUFFER_CHARS}), and only the unterminated record
|
|
111
|
+
* counts: a push carrying many complete records is legitimate however large. Past the cap the
|
|
112
|
+
* parser throws {@link SseBufferOverflowError} and is definitively failed: it releases the text it
|
|
113
|
+
* was holding and refuses every later push, so a caller that swallowed the throw can never resume
|
|
114
|
+
* mid-record and frame the tail of a refused record as a record of its own.
|
|
115
|
+
*/
|
|
116
|
+
export class SseFrameParser {
|
|
117
|
+
// The unterminated line, in the pieces the pushes delivered it in, and how long they are
|
|
118
|
+
// together. Joined once, when the line ends, so a long line is never copied per push.
|
|
119
|
+
private lineParts: string[] = [];
|
|
120
|
+
private lineLength = 0;
|
|
121
|
+
// What the current record holds: its data lines, its event name, and its own `id:`, which is
|
|
122
|
+
// undefined until it names one.
|
|
123
|
+
private dataLines: string[] = [];
|
|
124
|
+
private event = "";
|
|
125
|
+
private recordId: string | undefined;
|
|
126
|
+
// The characters of the current record's completed lines, line endings included. With the
|
|
127
|
+
// unterminated line, it is what the cap bounds.
|
|
128
|
+
private recordLength = 0;
|
|
129
|
+
// The stream's last event ID, moved when a record naming an `id:` ends, and undefined until one
|
|
130
|
+
// does.
|
|
131
|
+
private lastId: string | undefined;
|
|
132
|
+
// No character has arrived yet, so a byte-order mark opening the next one is still dropped.
|
|
133
|
+
private atStart = true;
|
|
134
|
+
// Whether the last character read is a CR that ended a line, so an LF arriving next completes
|
|
135
|
+
// a CRLF.
|
|
136
|
+
private afterCarriageReturn = false;
|
|
137
|
+
// Set past the cap, for good.
|
|
138
|
+
private overflowed = false;
|
|
139
|
+
|
|
140
|
+
// The cap on the unterminated record, checked at construction.
|
|
141
|
+
private readonly maxBufferChars: number;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Builds a parser that holds at most `maxBufferChars` of a record it has not seen the end of.
|
|
145
|
+
*
|
|
146
|
+
* @param maxBufferChars - bounds the unterminated record this parser holds, in UTF-16 code
|
|
147
|
+
* units: a whole number of 1 or more. A consumer whose frames are known small may tighten
|
|
148
|
+
* it; the shared readers take the default. A cap that is not a number of characters would
|
|
149
|
+
* bound nothing (`NaN` and `Infinity` are exceeded by no length) or refuse every push (zero
|
|
150
|
+
* and below), so it is refused here.
|
|
151
|
+
* @default MAX_SSE_BUFFER_CHARS
|
|
152
|
+
* @throws `MountayaConfigurationError` for a cap that is not a whole number of 1 or more.
|
|
153
|
+
*/
|
|
154
|
+
constructor(maxBufferChars: number = MAX_SSE_BUFFER_CHARS) {
|
|
155
|
+
// Typed as a number, and still whatever a caller outside the compiler passed.
|
|
156
|
+
const given: unknown = maxBufferChars;
|
|
157
|
+
if (typeof given !== "number" || !Number.isInteger(given) || given < 1) {
|
|
158
|
+
throw refuseOption(
|
|
159
|
+
"maxBufferChars",
|
|
160
|
+
given,
|
|
161
|
+
`pass a whole number of characters of 1 or more, or leave it out for the default of ${MAX_SSE_BUFFER_CHARS}.`,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
this.maxBufferChars = given;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* lastEventId is the stream's last event ID, as the HTML standard defines it: the value of the
|
|
170
|
+
* latest `id:` in a record the parser has seen the end of, whatever else that record carried.
|
|
171
|
+
*
|
|
172
|
+
* It is the position a reconnect resumes from, sent as `Last-Event-ID`. It carries over a
|
|
173
|
+
* record that names no `id:`, an empty `id:` clears it to the empty string, and an `id:` holding
|
|
174
|
+
* U+0000 is ignored. An `id:` in a record still unterminated has not moved it: a reconnect
|
|
175
|
+
* discards that record, so resuming past it would skip it.
|
|
176
|
+
*
|
|
177
|
+
* It is `undefined` until the stream names a position, where the standard starts from the empty
|
|
178
|
+
* string, so a stream that names none reads apart from one that clears it. A caller resuming
|
|
179
|
+
* from a position an earlier connection named keeps that position through the first and drops
|
|
180
|
+
* it at the second.
|
|
181
|
+
*/
|
|
182
|
+
get lastEventId(): string | undefined {
|
|
183
|
+
return this.lastId;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* push feeds the next decoded text chunk and returns every frame the records it completes
|
|
188
|
+
* dispatch, in stream order. What it leaves unterminated stays held for the next push, unless it
|
|
189
|
+
* passes the cap: then push throws {@link SseBufferOverflowError} instead of returning, dropping
|
|
190
|
+
* the frames the same chunk completed. The stream is being torn down with them, and a caller
|
|
191
|
+
* that resumes re-reads from the position it last recorded.
|
|
192
|
+
*/
|
|
193
|
+
push(chunk: string): SseFrame[] {
|
|
194
|
+
// A failed parser stays failed: it holds none of the record still arriving,
|
|
195
|
+
// so framing what follows would invent a record.
|
|
196
|
+
if (this.overflowed) {
|
|
197
|
+
throw new SseBufferOverflowError(this.maxBufferChars);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const frames: SseFrame[] = [];
|
|
201
|
+
let start = 0;
|
|
202
|
+
|
|
203
|
+
// A push that carries nothing leaves both of these to the first one that does.
|
|
204
|
+
if (chunk.length > 0) {
|
|
205
|
+
if (this.atStart) {
|
|
206
|
+
this.atStart = false;
|
|
207
|
+
if (chunk.startsWith(BYTE_ORDER_MARK)) {
|
|
208
|
+
start = BYTE_ORDER_MARK.length;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (this.afterCarriageReturn) {
|
|
213
|
+
this.afterCarriageReturn = false;
|
|
214
|
+
if (chunk.startsWith("\n", start)) {
|
|
215
|
+
start += 1;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Each search resumes past the line it found, so neither passes over a character twice.
|
|
221
|
+
let carriageReturn = chunk.indexOf("\r", start);
|
|
222
|
+
let lineFeed = chunk.indexOf("\n", start);
|
|
223
|
+
while (carriageReturn !== -1 || lineFeed !== -1) {
|
|
224
|
+
const end =
|
|
225
|
+
carriageReturn === -1 || (lineFeed !== -1 && lineFeed < carriageReturn)
|
|
226
|
+
? lineFeed
|
|
227
|
+
: carriageReturn;
|
|
228
|
+
let next = end + 1;
|
|
229
|
+
if (end === carriageReturn) {
|
|
230
|
+
if (next === chunk.length) {
|
|
231
|
+
this.afterCarriageReturn = true;
|
|
232
|
+
} else if (chunk.startsWith("\n", next)) {
|
|
233
|
+
next += 1;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const line = this.completeLine(chunk.slice(start, end));
|
|
238
|
+
this.recordLength += line.length + (next - end);
|
|
239
|
+
const frame = this.readLine(line);
|
|
240
|
+
if (frame !== null) {
|
|
241
|
+
frames.push(frame);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
start = next;
|
|
245
|
+
if (carriageReturn !== -1 && carriageReturn < start) {
|
|
246
|
+
carriageReturn = chunk.indexOf("\r", start);
|
|
247
|
+
}
|
|
248
|
+
if (lineFeed !== -1 && lineFeed < start) {
|
|
249
|
+
lineFeed = chunk.indexOf("\n", start);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (start < chunk.length) {
|
|
254
|
+
this.lineParts.push(chunk.slice(start));
|
|
255
|
+
this.lineLength += chunk.length - start;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// What is left is one record the producer has not terminated yet, so this is
|
|
259
|
+
// the only growth a stream can drive without bound. Release it before failing:
|
|
260
|
+
// a caller still holding the parser must not pin the text it refused.
|
|
261
|
+
if (this.recordLength + this.lineLength > this.maxBufferChars) {
|
|
262
|
+
this.lineParts = [];
|
|
263
|
+
this.lineLength = 0;
|
|
264
|
+
this.startRecord();
|
|
265
|
+
this.overflowed = true;
|
|
266
|
+
|
|
267
|
+
throw new SseBufferOverflowError(this.maxBufferChars);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return frames;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* completeLine joins the line held from earlier pushes with its last piece, `tail`, and starts
|
|
275
|
+
* the next line empty.
|
|
276
|
+
*/
|
|
277
|
+
private completeLine(tail: string): string {
|
|
278
|
+
if (this.lineParts.length === 0) {
|
|
279
|
+
return tail;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
this.lineParts.push(tail);
|
|
283
|
+
const line = this.lineParts.join("");
|
|
284
|
+
this.lineParts = [];
|
|
285
|
+
this.lineLength = 0;
|
|
286
|
+
|
|
287
|
+
return line;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* readLine applies one complete line to the current record, and returns the frame the record
|
|
292
|
+
* dispatches when the line is the empty one that ends it.
|
|
293
|
+
*
|
|
294
|
+
* A field line is split at its first colon, and one space after the colon is dropped: only the
|
|
295
|
+
* first space is the standard's separator, and a second one is part of the value. A line with no
|
|
296
|
+
* colon is its whole self as the field name, with an empty value.
|
|
297
|
+
*/
|
|
298
|
+
private readLine(line: string): SseFrame | null {
|
|
299
|
+
if (line === "") {
|
|
300
|
+
return this.endRecord();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (line.startsWith(":")) {
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const colon = line.indexOf(":");
|
|
308
|
+
const field = colon === -1 ? line : line.slice(0, colon);
|
|
309
|
+
const raw = colon === -1 ? "" : line.slice(colon + 1);
|
|
310
|
+
const value = raw.startsWith(" ") ? raw.slice(1) : raw;
|
|
311
|
+
|
|
312
|
+
if (field === "data") {
|
|
313
|
+
this.dataLines.push(value);
|
|
314
|
+
} else if (field === "event") {
|
|
315
|
+
this.event = value;
|
|
316
|
+
} else if (field === "id" && !value.includes("\u0000")) {
|
|
317
|
+
this.recordId = value;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* endRecord closes the current record: an `id:` it named becomes the stream's last event ID,
|
|
325
|
+
* and it returns the frame the record dispatches, or null for a record with no data.
|
|
326
|
+
*/
|
|
327
|
+
private endRecord(): SseFrame | null {
|
|
328
|
+
const { dataLines, event, recordId } = this;
|
|
329
|
+
this.startRecord();
|
|
330
|
+
|
|
331
|
+
if (recordId !== undefined) {
|
|
332
|
+
this.lastId = recordId;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (dataLines.length === 0) {
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return { data: dataLines.join("\n"), event, id: recordId ?? "" };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** startRecord empties the record being read, so the next line begins the next one. */
|
|
343
|
+
private startRecord(): void {
|
|
344
|
+
this.dataLines = [];
|
|
345
|
+
this.event = "";
|
|
346
|
+
this.recordId = undefined;
|
|
347
|
+
this.recordLength = 0;
|
|
348
|
+
}
|
|
349
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Mountaya API client: the Map Studio API, the Tile API and the GraphQL Data API, behind one
|
|
3
|
+
* credential.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { Mountaya } from "@mountaya/client";
|
|
7
|
+
*
|
|
8
|
+
* const mountaya = new Mountaya({ credential: { publishableKey: "pk_…" } });
|
|
9
|
+
*
|
|
10
|
+
* // A Map Studio or Tile API method resolves to the response envelope: the payload is at `data`.
|
|
11
|
+
* const { data: groups } = await mountaya.studio.itineraries.getItineraries();
|
|
12
|
+
* const { data: zones } = await mountaya.studio.zones.getZones();
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* The Map Studio API's map half arrives as `studio` (itineraries, zones, collections,
|
|
16
|
+
* collaboration, catalogs), and `agent` beside it: a run outlives the surface that started it and
|
|
17
|
+
* is addressed by a run id no map holds. The organization behind the map (its keys, settings,
|
|
18
|
+
* members, billing and webhooks, and minting a session token) is the admin unit's, which
|
|
19
|
+
* `@mountaya/client/admin` publishes as `new MountayaAdmin(mountaya)` beside webhook receiving, so
|
|
20
|
+
* an application that never manages an organization never imports it. Which namespace and which
|
|
21
|
+
* unit an operation belongs to is declared in the contract, not decided here, so every language's
|
|
22
|
+
* client divides it the same way.
|
|
23
|
+
*
|
|
24
|
+
* This entry point exposes the secret-key credential, which mints session tokens and must only
|
|
25
|
+
* ever run on a server. Browser code imports `@mountaya/client/browser`, which does not export it
|
|
26
|
+
* at all.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import type { Geometry } from "geojson";
|
|
30
|
+
|
|
31
|
+
import type { ClientOptions } from "./core/rules/transport.js";
|
|
32
|
+
import { MountayaTransport } from "./core/rules/transport.js";
|
|
33
|
+
import { bind as bindData } from "./data/operations.gen.js";
|
|
34
|
+
import { bind as bindRest } from "./rest/operations.gen.js";
|
|
35
|
+
import { bind as bindTiles } from "./tiles/operations.gen.js";
|
|
36
|
+
|
|
37
|
+
export {
|
|
38
|
+
SessionTokenEngine,
|
|
39
|
+
type SessionTokenEngineOptions,
|
|
40
|
+
type SessionTokenSnapshot,
|
|
41
|
+
} from "./core/engines/sessionToken.js";
|
|
42
|
+
export type {
|
|
43
|
+
CookieCredential,
|
|
44
|
+
Credential,
|
|
45
|
+
PublishableCredential,
|
|
46
|
+
SecretCredential,
|
|
47
|
+
SessionTokenSource,
|
|
48
|
+
ShareCredential,
|
|
49
|
+
} from "./core/rules/auth.js";
|
|
50
|
+
// The instants a request names in `RequestArgs.dates`, for a consumer writing their own operation
|
|
51
|
+
// against this transport.
|
|
52
|
+
export type { DateMask } from "./core/rules/dates.js";
|
|
53
|
+
// The decoding a response gets, for JSON a caller already holds: a fixture baked into a bundle,
|
|
54
|
+
// a record read back from a cache.
|
|
55
|
+
export { decodeRestSchema } from "./core/rules/decode.js";
|
|
56
|
+
export type { MountayaResponse } from "./core/rules/envelope.js";
|
|
57
|
+
export {
|
|
58
|
+
type ApiErrorDetails,
|
|
59
|
+
type ErrorCode,
|
|
60
|
+
type ErrorEntry,
|
|
61
|
+
type ErrorMetadata,
|
|
62
|
+
// Published for a consumer writing their own transport-shaped layer: a streaming reader, a
|
|
63
|
+
// proxy, a test double. They hold a status and no response body, which is the one case the
|
|
64
|
+
// error constructors cannot serve on their own.
|
|
65
|
+
errorForStatus,
|
|
66
|
+
type IdempotentAnswer,
|
|
67
|
+
// The structural predicates, for a caller whose value may come from a second copy of this
|
|
68
|
+
// package, where `instanceof` answers false.
|
|
69
|
+
isMountayaApiError,
|
|
70
|
+
isMountayaError,
|
|
71
|
+
MountayaApiError,
|
|
72
|
+
MountayaAuthenticationError,
|
|
73
|
+
MountayaBadRequestError,
|
|
74
|
+
MountayaBillingError,
|
|
75
|
+
MountayaConfigurationError,
|
|
76
|
+
MountayaConflictError,
|
|
77
|
+
MountayaConnectionError,
|
|
78
|
+
MountayaDataError,
|
|
79
|
+
MountayaError,
|
|
80
|
+
MountayaForbiddenError,
|
|
81
|
+
MountayaIdempotencyKeyInUseError,
|
|
82
|
+
MountayaIdempotentAnswerUnavailableError,
|
|
83
|
+
MountayaMethodNotAllowedError,
|
|
84
|
+
MountayaNotFoundError,
|
|
85
|
+
MountayaPayloadTooLargeError,
|
|
86
|
+
MountayaPreconditionFailedError,
|
|
87
|
+
MountayaRateLimitError,
|
|
88
|
+
MountayaServerError,
|
|
89
|
+
PAYMENT_REQUIRED_CODE,
|
|
90
|
+
} from "./core/rules/errors.js";
|
|
91
|
+
// A paged listing read whole: the walk over its cursors, written once.
|
|
92
|
+
export { type PagedResponse, type PaginateOptions, paginate } from "./core/rules/paginate.js";
|
|
93
|
+
// The rate-limit state an answer describes, for an answer this client did not read itself: a
|
|
94
|
+
// tile a map library fetched, say.
|
|
95
|
+
export {
|
|
96
|
+
parseRateLimit,
|
|
97
|
+
type RateLimitInfo,
|
|
98
|
+
type RateLimitPolicy,
|
|
99
|
+
} from "./core/rules/rateLimit.js";
|
|
100
|
+
// The resources a cache entry is keyed by, for a binding that keeps one per resource.
|
|
101
|
+
export type { ResourceKind, ResourceRef } from "./core/rules/resourceRef.js";
|
|
102
|
+
export type { RetryOptions, Surface } from "./core/rules/retry.js";
|
|
103
|
+
// Data API field selection. `Selection` and `Selected` are the two halves a caller names
|
|
104
|
+
// when it holds a selection in a variable rather than passing it inline.
|
|
105
|
+
export {
|
|
106
|
+
type DataOptions,
|
|
107
|
+
type DataResult,
|
|
108
|
+
type FieldTree,
|
|
109
|
+
MountayaSelectionError,
|
|
110
|
+
type Selected,
|
|
111
|
+
type Selection,
|
|
112
|
+
type SelectionValue,
|
|
113
|
+
} from "./core/rules/selection.js";
|
|
114
|
+
export {
|
|
115
|
+
type BaseUrlOverrides,
|
|
116
|
+
type BinaryBody,
|
|
117
|
+
boundFetch,
|
|
118
|
+
type ClientOptions,
|
|
119
|
+
defaultBaseUrls,
|
|
120
|
+
MountayaTransport,
|
|
121
|
+
// The two halves of one call: `RequestArgs` is what an OPERATION builds and `RequestOptions`
|
|
122
|
+
// is what a CALLER passes. The first is published because a consumer writing their own
|
|
123
|
+
// operation against this transport needs it; the second because every generated signature
|
|
124
|
+
// takes one.
|
|
125
|
+
type RequestArgs,
|
|
126
|
+
type RequestOptions,
|
|
127
|
+
type ResponseReport,
|
|
128
|
+
type StreamSeam,
|
|
129
|
+
} from "./core/rules/transport.js";
|
|
130
|
+
export { SCHEMA_VERSION } from "./core/rules/version.gen.js";
|
|
131
|
+
// The free operation functions, for callers who want the smallest possible bundle.
|
|
132
|
+
// Importing one of these pulls in the transport and nothing else.
|
|
133
|
+
export * as data from "./data/operations.gen.js";
|
|
134
|
+
// The generated wire types, so a caller can name what it receives.
|
|
135
|
+
export type * as DataSchema from "./data/schema.gen.js";
|
|
136
|
+
// The answer of each Data API operation, under a name of its own beside the namespace. A
|
|
137
|
+
// module that emits declarations names an inferred answer through a name an entry point
|
|
138
|
+
// publishes directly, and a member of `DataSchema` is no such name, so without these an
|
|
139
|
+
// exported function that returns `mountaya.data.geocode(...)` unannotated does not compile
|
|
140
|
+
// (TS2883). The names are the contract's, and they are the answers alone: the rest of the
|
|
141
|
+
// Data API's types share names with the Map Studio's, which hold the root.
|
|
142
|
+
// `namespaces.test.ts` derives the list from the operations and holds both entries to it.
|
|
143
|
+
export type {
|
|
144
|
+
AnalyzeGeometryResult,
|
|
145
|
+
DirectionsResult,
|
|
146
|
+
ExportItineraryResult,
|
|
147
|
+
GeocodeResult,
|
|
148
|
+
IsochronesResult,
|
|
149
|
+
ItineraryResult,
|
|
150
|
+
MatrixResult,
|
|
151
|
+
PoisResult,
|
|
152
|
+
ReverseGeocodeResult,
|
|
153
|
+
SearchResult,
|
|
154
|
+
SuggestionsResult,
|
|
155
|
+
TerrainGridResult,
|
|
156
|
+
TerrainProfileResult,
|
|
157
|
+
TerrainSamplesResult,
|
|
158
|
+
} from "./data/schema.gen.js";
|
|
159
|
+
// The selectable field tree of each Data API operation, plus a named `select` type per
|
|
160
|
+
// operation (`DirectionsSelection` and its siblings).
|
|
161
|
+
export * as DataSelection from "./data/selection.gen.js";
|
|
162
|
+
// The core's Map Studio operations. The admin unit's are `rest` of `@mountaya/client/admin`.
|
|
163
|
+
export * as rest from "./rest/coreOperations.gen.js";
|
|
164
|
+
// Every resource, params and enum type of the core's Map Studio surface, at the package root, so
|
|
165
|
+
// a caller writes `ItineraryWithDetails` rather than reaching through a namespace for the type an
|
|
166
|
+
// operation just handed them. Generated from the contract, which states each schema's unit, so the
|
|
167
|
+
// list follows it; a schema only the admin unit reaches is named by `@mountaya/client/admin`.
|
|
168
|
+
export type * from "./rest/coreResources.gen.js";
|
|
169
|
+
// The whole Map Studio document, both units: the raw `paths`, `components` and `operations`.
|
|
170
|
+
export type * as RestSchema from "./rest/schema.gen.js";
|
|
171
|
+
export * as tiles from "./tiles/operations.gen.js";
|
|
172
|
+
export type * as TileSchema from "./tiles/schema.gen.js";
|
|
173
|
+
// The geometry a Map Studio answer holds. It is declared by `geojson`, a dependency of this
|
|
174
|
+
// package, and a declaration file names an inferred answer through a name an entry point
|
|
175
|
+
// publishes: without this one, an exported function that returns `getItinerary(...)`
|
|
176
|
+
// unannotated does not compile (TS2883) where the installer keeps `@types/geojson` out of the
|
|
177
|
+
// consumer's own directory.
|
|
178
|
+
export type { Geometry };
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Mountaya is the client. One instance holds one credential and one set of policies.
|
|
182
|
+
*
|
|
183
|
+
* The organization's operations are the admin unit's: `new MountayaAdmin(mountaya)` of
|
|
184
|
+
* `@mountaya/client/admin` answers them, bound to this client's transport.
|
|
185
|
+
*/
|
|
186
|
+
export class Mountaya {
|
|
187
|
+
/**
|
|
188
|
+
* The AI agent: starting a run, answering it, rating it. Each method resolves to the response
|
|
189
|
+
* envelope, with the payload at `data`; a run's events are read through
|
|
190
|
+
* `createAgentTransport` of `@mountaya/client/events`.
|
|
191
|
+
*/
|
|
192
|
+
readonly agent: ReturnType<typeof bindRest>["agent"];
|
|
193
|
+
/**
|
|
194
|
+
* The GraphQL Data API: directions, search, terrain, isochrones, matrices. Each method resolves
|
|
195
|
+
* to the operation's own field, with no envelope.
|
|
196
|
+
*/
|
|
197
|
+
readonly data: ReturnType<typeof bindData>;
|
|
198
|
+
/**
|
|
199
|
+
* The Map Studio API's map half: itineraries, zones, collections, and their catalogs. Each
|
|
200
|
+
* method resolves to the response envelope, with the payload at `data`.
|
|
201
|
+
*/
|
|
202
|
+
readonly studio: ReturnType<typeof bindRest>["studio"];
|
|
203
|
+
/** The Tile API's style specifications. Each method resolves to the response envelope. */
|
|
204
|
+
readonly tiles: ReturnType<typeof bindTiles>;
|
|
205
|
+
/**
|
|
206
|
+
* The transport every method calls through: for a call this client binds no method for, and
|
|
207
|
+
* for the openers of `@mountaya/client/events`.
|
|
208
|
+
*/
|
|
209
|
+
readonly transport: MountayaTransport;
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Builds a client over one credential and its options, checking both before anything is sent.
|
|
213
|
+
*
|
|
214
|
+
* @throws `MountayaConfigurationError` for options that are not an object, a credential that
|
|
215
|
+
* cannot work, a secret key in a page or a React Native application, or an option outside
|
|
216
|
+
* its range: see {@link ClientOptions}.
|
|
217
|
+
*/
|
|
218
|
+
constructor(options: ClientOptions) {
|
|
219
|
+
this.transport = new MountayaTransport(options);
|
|
220
|
+
|
|
221
|
+
// One bind call, destructured: studio and agent are two views of a single surface (same base
|
|
222
|
+
// URL, same quota domain), so binding once per view would build the whole table twice to hand
|
|
223
|
+
// back halves of it. Its admin half is the admin unit's to hand out.
|
|
224
|
+
const api = bindRest(this.transport);
|
|
225
|
+
|
|
226
|
+
this.agent = api.agent;
|
|
227
|
+
this.data = bindData(this.transport);
|
|
228
|
+
this.studio = api.studio;
|
|
229
|
+
this.tiles = bindTiles(this.transport);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// GENERATED FILE. DO NOT EDIT.
|
|
2
|
+
//
|
|
3
|
+
// The operations of the admin unit, re-exported from operations.gen.ts so that an entry point
|
|
4
|
+
// publishes the unit without a hand-maintained list. Printed from the mountaya/schemas
|
|
5
|
+
// contract (sdk/model.json, the `unit` of each of `rest.operations`) by
|
|
6
|
+
// scripts/generate-units.mjs.
|
|
7
|
+
//
|
|
8
|
+
// Edit the contract in mountaya/schemas, not this file.
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
deleteMyInvitation,
|
|
12
|
+
deleteOrganizationAiProviderConfig,
|
|
13
|
+
deleteOrganizationBillingPaymentMethod,
|
|
14
|
+
deleteOrganizationBillingSubscription,
|
|
15
|
+
deleteOrganizationInvitation,
|
|
16
|
+
deleteOrganizationMember,
|
|
17
|
+
deleteOrganizationPublishableKey,
|
|
18
|
+
deleteOrganizationWebhookEndpoint,
|
|
19
|
+
getAiProviders,
|
|
20
|
+
getBillingProducts,
|
|
21
|
+
getMyInvitations,
|
|
22
|
+
getMyOrganizationNotificationPreferences,
|
|
23
|
+
getMyOrganizations,
|
|
24
|
+
getMyPreferences,
|
|
25
|
+
getOrganization,
|
|
26
|
+
getOrganizationAiProviderConfig,
|
|
27
|
+
getOrganizationBillingInfo,
|
|
28
|
+
getOrganizationBillingInvoices,
|
|
29
|
+
getOrganizationBillingPaymentMethods,
|
|
30
|
+
getOrganizationBillingPromotionCode,
|
|
31
|
+
getOrganizationBillingSubscription,
|
|
32
|
+
getOrganizationInvitation,
|
|
33
|
+
getOrganizationInvitations,
|
|
34
|
+
getOrganizationMembers,
|
|
35
|
+
getOrganizationPublishableKey,
|
|
36
|
+
getOrganizationPublishableKeys,
|
|
37
|
+
getOrganizationSettings,
|
|
38
|
+
getOrganizationUsageData,
|
|
39
|
+
getOrganizationUsageTiles,
|
|
40
|
+
getOrganizationWebhookDeliveries,
|
|
41
|
+
getOrganizationWebhookDelivery,
|
|
42
|
+
getOrganizationWebhookEndpoint,
|
|
43
|
+
getOrganizationWebhookEndpoints,
|
|
44
|
+
getOrganizationWebhookEvent,
|
|
45
|
+
getOrganizationWebhookEvents,
|
|
46
|
+
getOrganizationWebhookHealth,
|
|
47
|
+
getWebhookEventTypes,
|
|
48
|
+
patchMyOrganizationNotificationPreferences,
|
|
49
|
+
patchMyPreferences,
|
|
50
|
+
patchOrganization,
|
|
51
|
+
patchOrganizationAiProviderConfig,
|
|
52
|
+
patchOrganizationBillingInfo,
|
|
53
|
+
patchOrganizationMember,
|
|
54
|
+
patchOrganizationPublishableKey,
|
|
55
|
+
patchOrganizationSettings,
|
|
56
|
+
patchOrganizationWebhookEndpoint,
|
|
57
|
+
postMyInvitationAccept,
|
|
58
|
+
postOrganization,
|
|
59
|
+
postOrganizationAiProviderConfig,
|
|
60
|
+
postOrganizationBillingPaymentMethod,
|
|
61
|
+
postOrganizationBillingPaymentMethodSetDefault,
|
|
62
|
+
postOrganizationBillingPaymentMethodSetup,
|
|
63
|
+
postOrganizationBillingProvision,
|
|
64
|
+
postOrganizationBillingSubscription,
|
|
65
|
+
postOrganizationInvitation,
|
|
66
|
+
postOrganizationPublishableKey,
|
|
67
|
+
postOrganizationWebhookDelivery,
|
|
68
|
+
postOrganizationWebhookEndpoint,
|
|
69
|
+
postSession,
|
|
70
|
+
} from "./operations.gen.js";
|