@mountaya/client 0.1.0 → 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.
- package/CHANGELOG.md +45 -0
- package/README.md +2 -2
- package/dist/admin/index.cjs +2876 -3502
- package/dist/admin/index.cjs.map +1 -1
- package/dist/admin/index.d.cts +731 -912
- package/dist/admin/index.d.ts +731 -912
- package/dist/admin/index.js +188 -285
- package/dist/admin/index.js.map +1 -1
- package/dist/browser.cjs +5913 -7993
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.d.cts +42 -78
- package/dist/browser.d.ts +42 -78
- package/dist/browser.js +18 -49
- package/dist/browser.js.map +1 -1
- package/dist/chunk-CeEX7ayk.d.cts +75531 -0
- package/dist/chunk-CeEX7ayk.d.ts +75531 -0
- package/dist/chunk-Cg7Dtcb4.js +2987 -0
- package/dist/chunk-Cg7Dtcb4.js.map +1 -0
- package/dist/chunk-DJqUsgsy.d.ts +1463 -0
- package/dist/chunk-DNc8T5K1.js +241 -0
- package/dist/chunk-DNc8T5K1.js.map +1 -0
- package/dist/chunk-DTxzw3hu.d.cts +1463 -0
- package/dist/chunk-DmxQID5X.js +2289 -0
- package/dist/chunk-DmxQID5X.js.map +1 -0
- package/dist/chunk-DoZt_oIV.d.ts +3991 -0
- package/dist/chunk-DpJrqANO.js +11 -0
- package/dist/chunk-Dx8mPAdC.d.cts +3991 -0
- package/dist/chunk-X2ukY3ho.js +419 -0
- package/dist/chunk-X2ukY3ho.js.map +1 -0
- package/dist/events/index.cjs +1338 -1874
- package/dist/events/index.cjs.map +1 -1
- package/dist/events/index.d.cts +373 -382
- package/dist/events/index.d.ts +373 -382
- package/dist/events/index.js +523 -806
- package/dist/events/index.js.map +1 -1
- package/dist/index.cjs +5908 -7988
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +37 -69
- package/dist/index.d.ts +37 -69
- package/dist/index.js +17 -48
- package/dist/index.js.map +1 -1
- package/package.json +7 -8
- package/src/browser.ts +1 -0
- package/src/core/rules/clientHeader.ts +1 -1
- package/src/core/rules/errors.ts +1 -1
- package/src/core/rules/nullMasks.gen.ts +14 -2
- package/src/core/rules/version.gen.ts +1 -1
- package/src/events/index.ts +1 -5
- package/src/rest/adminResources.gen.ts +2 -0
- package/src/rest/dates.gen.ts +1 -1
- package/src/rest/resources.gen.ts +2 -0
- package/src/rest/schema.gen.ts +247 -60
- package/src/tiles/schema.gen.ts +1 -3
- package/dist/chunk-BM526BHG.js +0 -342
- package/dist/chunk-BM526BHG.js.map +0 -1
- package/dist/chunk-D2YEVTG7.js +0 -327
- package/dist/chunk-D2YEVTG7.js.map +0 -1
- package/dist/chunk-OHKVUREN.js +0 -3500
- package/dist/chunk-OHKVUREN.js.map +0 -1
- package/dist/chunk-TDTK2L5H.js +0 -3842
- package/dist/chunk-TDTK2L5H.js.map +0 -1
- package/dist/coreOperations.gen-BMYPoBbz.d.ts +0 -1760
- package/dist/coreOperations.gen-BxX5xRxw.d.cts +0 -1760
- package/dist/errors-B-GqAtQv.d.cts +0 -75960
- package/dist/errors-B-GqAtQv.d.ts +0 -75960
- package/dist/operations.gen-CvTyzKNF.d.cts +0 -3993
- package/dist/operations.gen-DUD406TV.d.ts +0 -3993
package/dist/events/index.cjs
CHANGED
|
@@ -1,1963 +1,1427 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, {
|
|
3
|
+
__esModule: { value: true },
|
|
4
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
5
|
+
});
|
|
6
|
+
const digitPattern = /[0-9]/;
|
|
7
|
+
const alphaPattern = /[A-Za-z]/;
|
|
8
|
+
const lowercaseAlphaPattern = /[a-z]/;
|
|
9
|
+
const keyCharPattern = /[a-z0-9_.*-]/;
|
|
10
|
+
const tokenCharPattern = /[!#$%&'*+\-.^_`|~0-9A-Za-z:/]/;
|
|
11
|
+
const base64CharPattern = /[A-Za-z0-9+/=]/;
|
|
10
12
|
var Cursor = class {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
while (tokenCharPattern.test(this.peek())) {
|
|
209
|
-
value += this.take();
|
|
210
|
-
}
|
|
211
|
-
return value;
|
|
212
|
-
}
|
|
213
|
-
/** A byte sequence, `:base64:`, read for its shape alone. */
|
|
214
|
-
bytes() {
|
|
215
|
-
this.index += 1;
|
|
216
|
-
while (base64CharPattern.test(this.peek())) {
|
|
217
|
-
this.index += 1;
|
|
218
|
-
}
|
|
219
|
-
return this.consume(":");
|
|
220
|
-
}
|
|
221
|
-
/** A display string, `"…"` with each byte past ASCII percent-encoded as lowercase hex. */
|
|
222
|
-
displayString() {
|
|
223
|
-
if (!this.consume('"')) {
|
|
224
|
-
return void 0;
|
|
225
|
-
}
|
|
226
|
-
const bytes = [];
|
|
227
|
-
for (; ; ) {
|
|
228
|
-
if (this.done) {
|
|
229
|
-
return void 0;
|
|
230
|
-
}
|
|
231
|
-
const char = this.take();
|
|
232
|
-
const code = char.charCodeAt(0);
|
|
233
|
-
if (code < 32 || code > 126) {
|
|
234
|
-
return void 0;
|
|
235
|
-
}
|
|
236
|
-
if (char === '"') {
|
|
237
|
-
try {
|
|
238
|
-
return new TextDecoder("utf-8", { fatal: true }).decode(Uint8Array.from(bytes));
|
|
239
|
-
} catch {
|
|
240
|
-
return void 0;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
if (char === "%") {
|
|
244
|
-
const hex = this.take() + this.take();
|
|
245
|
-
if (!/^[0-9a-f]{2}$/.test(hex)) {
|
|
246
|
-
return void 0;
|
|
247
|
-
}
|
|
248
|
-
bytes.push(Number.parseInt(hex, 16));
|
|
249
|
-
continue;
|
|
250
|
-
}
|
|
251
|
-
bytes.push(code);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
13
|
+
text;
|
|
14
|
+
index = 0;
|
|
15
|
+
constructor(text) {
|
|
16
|
+
this.text = text;
|
|
17
|
+
}
|
|
18
|
+
get done() {
|
|
19
|
+
return this.index >= this.text.length;
|
|
20
|
+
}
|
|
21
|
+
peek() {
|
|
22
|
+
return this.text[this.index] ?? "";
|
|
23
|
+
}
|
|
24
|
+
take() {
|
|
25
|
+
const char = this.peek();
|
|
26
|
+
this.index += 1;
|
|
27
|
+
return char;
|
|
28
|
+
}
|
|
29
|
+
skipSpaces() {
|
|
30
|
+
while (this.peek() === " ") this.index += 1;
|
|
31
|
+
}
|
|
32
|
+
skipWhitespace() {
|
|
33
|
+
while (this.peek() === " " || this.peek() === " ") this.index += 1;
|
|
34
|
+
}
|
|
35
|
+
consume(char) {
|
|
36
|
+
return this.take() === char;
|
|
37
|
+
}
|
|
38
|
+
member() {
|
|
39
|
+
if (this.peek() === "(") {
|
|
40
|
+
if (!this.innerList()) return;
|
|
41
|
+
const parameters = this.parameters();
|
|
42
|
+
return parameters === void 0 ? void 0 : {
|
|
43
|
+
item: void 0,
|
|
44
|
+
parameters
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const item = this.bareItem();
|
|
48
|
+
const parameters = item === void 0 ? void 0 : this.parameters();
|
|
49
|
+
return parameters === void 0 ? void 0 : {
|
|
50
|
+
item,
|
|
51
|
+
parameters
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
innerList() {
|
|
55
|
+
this.index += 1;
|
|
56
|
+
for (;;) {
|
|
57
|
+
this.skipSpaces();
|
|
58
|
+
if (this.peek() === ")") {
|
|
59
|
+
this.index += 1;
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
if (this.bareItem() === void 0 || this.parameters() === void 0) return false;
|
|
63
|
+
if (this.peek() !== " " && this.peek() !== ")") return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
parameters() {
|
|
67
|
+
const parameters = /* @__PURE__ */ new Map();
|
|
68
|
+
while (this.peek() === ";") {
|
|
69
|
+
this.index += 1;
|
|
70
|
+
this.skipSpaces();
|
|
71
|
+
const key = this.key();
|
|
72
|
+
if (key === void 0) return;
|
|
73
|
+
let value = {
|
|
74
|
+
kind: "boolean",
|
|
75
|
+
value: true
|
|
76
|
+
};
|
|
77
|
+
if (this.peek() === "=") {
|
|
78
|
+
this.index += 1;
|
|
79
|
+
value = this.bareItem();
|
|
80
|
+
}
|
|
81
|
+
if (value === void 0) return;
|
|
82
|
+
parameters.set(key, value);
|
|
83
|
+
}
|
|
84
|
+
return parameters;
|
|
85
|
+
}
|
|
86
|
+
key() {
|
|
87
|
+
if (!lowercaseAlphaPattern.test(this.peek()) && this.peek() !== "*") return;
|
|
88
|
+
let key = this.take();
|
|
89
|
+
while (keyCharPattern.test(this.peek())) key += this.take();
|
|
90
|
+
return key;
|
|
91
|
+
}
|
|
92
|
+
bareItem() {
|
|
93
|
+
const first = this.peek();
|
|
94
|
+
if (first === "-" || digitPattern.test(first)) return this.number();
|
|
95
|
+
if (first === "\"") {
|
|
96
|
+
const value = this.string();
|
|
97
|
+
return value === void 0 ? void 0 : {
|
|
98
|
+
kind: "string",
|
|
99
|
+
value
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
if (first === "*" || alphaPattern.test(first)) return {
|
|
103
|
+
kind: "token",
|
|
104
|
+
value: this.token()
|
|
105
|
+
};
|
|
106
|
+
if (first === ":") return this.bytes() ? { kind: "bytes" } : void 0;
|
|
107
|
+
if (first === "?") {
|
|
108
|
+
this.index += 1;
|
|
109
|
+
const value = this.take();
|
|
110
|
+
return value === "0" || value === "1" ? {
|
|
111
|
+
kind: "boolean",
|
|
112
|
+
value: value === "1"
|
|
113
|
+
} : void 0;
|
|
114
|
+
}
|
|
115
|
+
if (first === "@") {
|
|
116
|
+
this.index += 1;
|
|
117
|
+
const date = this.number();
|
|
118
|
+
return date?.kind === "integer" ? {
|
|
119
|
+
kind: "date",
|
|
120
|
+
value: date.value
|
|
121
|
+
} : void 0;
|
|
122
|
+
}
|
|
123
|
+
if (first === "%") {
|
|
124
|
+
this.index += 1;
|
|
125
|
+
const value = this.displayString();
|
|
126
|
+
return value === void 0 ? void 0 : {
|
|
127
|
+
kind: "display",
|
|
128
|
+
value
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
number() {
|
|
133
|
+
let sign = 1;
|
|
134
|
+
if (this.peek() === "-") {
|
|
135
|
+
sign = -1;
|
|
136
|
+
this.index += 1;
|
|
137
|
+
}
|
|
138
|
+
let digits = "";
|
|
139
|
+
let decimal = false;
|
|
140
|
+
while (digitPattern.test(this.peek()) || this.peek() === "." && !decimal) {
|
|
141
|
+
if (this.peek() === ".") {
|
|
142
|
+
if (digits.length > 12) return;
|
|
143
|
+
decimal = true;
|
|
144
|
+
}
|
|
145
|
+
digits += this.take();
|
|
146
|
+
if (!decimal && digits.length > 15 || decimal && digits.length > 16) return;
|
|
147
|
+
}
|
|
148
|
+
if (digits === "" || digits.startsWith(".")) return;
|
|
149
|
+
if (!decimal) return {
|
|
150
|
+
kind: "integer",
|
|
151
|
+
value: sign * Number(digits)
|
|
152
|
+
};
|
|
153
|
+
const fraction = digits.slice(digits.indexOf(".") + 1);
|
|
154
|
+
if (fraction.length === 0 || fraction.length > 3) return;
|
|
155
|
+
return {
|
|
156
|
+
kind: "decimal",
|
|
157
|
+
value: sign * Number(digits)
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
string() {
|
|
161
|
+
this.index += 1;
|
|
162
|
+
let value = "";
|
|
163
|
+
for (;;) {
|
|
164
|
+
if (this.done) return;
|
|
165
|
+
const char = this.take();
|
|
166
|
+
if (char === "\"") return value;
|
|
167
|
+
if (char === "\\") {
|
|
168
|
+
const escaped = this.take();
|
|
169
|
+
if (escaped !== "\"" && escaped !== "\\") return;
|
|
170
|
+
value += escaped;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const code = char.charCodeAt(0);
|
|
174
|
+
if (code < 32 || code > 126) return;
|
|
175
|
+
value += char;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
token() {
|
|
179
|
+
let value = this.take();
|
|
180
|
+
while (tokenCharPattern.test(this.peek())) value += this.take();
|
|
181
|
+
return value;
|
|
182
|
+
}
|
|
183
|
+
bytes() {
|
|
184
|
+
this.index += 1;
|
|
185
|
+
while (base64CharPattern.test(this.peek())) this.index += 1;
|
|
186
|
+
return this.consume(":");
|
|
187
|
+
}
|
|
188
|
+
displayString() {
|
|
189
|
+
if (!this.consume("\"")) return;
|
|
190
|
+
const bytes = [];
|
|
191
|
+
for (;;) {
|
|
192
|
+
if (this.done) return;
|
|
193
|
+
const char = this.take();
|
|
194
|
+
const code = char.charCodeAt(0);
|
|
195
|
+
if (code < 32 || code > 126) return;
|
|
196
|
+
if (char === "\"") try {
|
|
197
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(Uint8Array.from(bytes));
|
|
198
|
+
} catch {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (char === "%") {
|
|
202
|
+
const hex = this.take() + this.take();
|
|
203
|
+
if (!/^[0-9a-f]{2}$/.test(hex)) return;
|
|
204
|
+
bytes.push(Number.parseInt(hex, 16));
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
bytes.push(code);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
254
210
|
};
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
return void 0;
|
|
271
|
-
}
|
|
272
|
-
cursor.skipWhitespace();
|
|
273
|
-
if (cursor.done) {
|
|
274
|
-
return void 0;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
return members;
|
|
211
|
+
const parseList = (text) => {
|
|
212
|
+
const cursor = new Cursor(text);
|
|
213
|
+
const members = [];
|
|
214
|
+
cursor.skipSpaces();
|
|
215
|
+
while (!cursor.done) {
|
|
216
|
+
const member = cursor.member();
|
|
217
|
+
if (member === void 0) return;
|
|
218
|
+
members.push(member);
|
|
219
|
+
cursor.skipWhitespace();
|
|
220
|
+
if (cursor.done) break;
|
|
221
|
+
if (!cursor.consume(",")) return;
|
|
222
|
+
cursor.skipWhitespace();
|
|
223
|
+
if (cursor.done) return;
|
|
224
|
+
}
|
|
225
|
+
return members;
|
|
278
226
|
};
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
remaining: wholeNumber(state?.get("r")),
|
|
307
|
-
resetSeconds: wholeNumber(state?.get("t")),
|
|
308
|
-
windowSeconds: wholeNumber(member.parameters.get("w"))
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
return policies.length === 0 ? void 0 : { policies };
|
|
227
|
+
const wholeNumber = (item) => item?.kind === "integer" && item.value >= 0 ? item.value : void 0;
|
|
228
|
+
const policyName = (member) => member.item?.kind === "string" ? member.item.value : void 0;
|
|
229
|
+
const parseRateLimit = (headers) => {
|
|
230
|
+
const policyField = headers.get("RateLimit-Policy");
|
|
231
|
+
const declared = policyField === null ? void 0 : parseList(policyField);
|
|
232
|
+
if (declared === void 0) return;
|
|
233
|
+
const stateField = headers.get("RateLimit");
|
|
234
|
+
const states = /* @__PURE__ */ new Map();
|
|
235
|
+
for (const member of (stateField === null ? void 0 : parseList(stateField)) ?? []) {
|
|
236
|
+
const name = policyName(member);
|
|
237
|
+
if (name !== void 0 && !states.has(name)) states.set(name, member);
|
|
238
|
+
}
|
|
239
|
+
const policies = [];
|
|
240
|
+
for (const member of declared) {
|
|
241
|
+
const name = policyName(member);
|
|
242
|
+
const quota = wholeNumber(member.parameters.get("q"));
|
|
243
|
+
if (name === void 0 || quota === void 0) continue;
|
|
244
|
+
const state = states.get(name)?.parameters;
|
|
245
|
+
policies.push({
|
|
246
|
+
name,
|
|
247
|
+
quota,
|
|
248
|
+
remaining: wholeNumber(state?.get("r")),
|
|
249
|
+
resetSeconds: wholeNumber(state?.get("t")),
|
|
250
|
+
windowSeconds: wholeNumber(member.parameters.get("w"))
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
return policies.length === 0 ? void 0 : { policies };
|
|
312
254
|
};
|
|
313
|
-
|
|
314
|
-
// src/core/rules/errors.ts
|
|
315
255
|
var MountayaError = class extends Error {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
* credential its first attempt refused, or whose wait for a first session token ran out,
|
|
323
|
-
* carries it too.
|
|
324
|
-
*
|
|
325
|
-
* A write that carried one may have been made whatever the error says: an answer that never
|
|
326
|
-
* arrived, a 5xx and a body that broke off do not tell. Send the same call again with this key
|
|
327
|
-
* as its `idempotencyKey`, never a new one: the API answers a key it has seen with the answer it
|
|
328
|
-
* kept, or runs the write where it made none, so the write is made once. A new key is a new
|
|
329
|
-
* write, made a second time where the first one landed.
|
|
330
|
-
*/
|
|
331
|
-
idempotencyKey;
|
|
332
|
-
// The constructor narrows the signature rather than adding behaviour: `Error`'s `message`
|
|
333
|
-
// is optional and this family's is not. Dropping it makes
|
|
334
|
-
// `new MountayaConfigurationError()` compile, which is a thrown value carrying nothing a
|
|
335
|
-
// caller can read.
|
|
336
|
-
/**
|
|
337
|
-
* @param message - what went wrong and what to do about it, naming the call where there is one.
|
|
338
|
-
* @param options - the standard error options, for the `cause` behind it, and the
|
|
339
|
-
* `idempotencyKey` the call sent, where it sent one.
|
|
340
|
-
*/
|
|
341
|
-
constructor(message, options) {
|
|
342
|
-
super(message, options);
|
|
343
|
-
this.idempotencyKey = options?.idempotencyKey;
|
|
344
|
-
}
|
|
256
|
+
name = "MountayaError";
|
|
257
|
+
idempotencyKey;
|
|
258
|
+
constructor(message, options) {
|
|
259
|
+
super(message, options);
|
|
260
|
+
this.idempotencyKey = options?.idempotencyKey;
|
|
261
|
+
}
|
|
345
262
|
};
|
|
346
263
|
var MountayaConfigurationError = class extends MountayaError {
|
|
347
|
-
|
|
264
|
+
name = "MountayaConfigurationError";
|
|
348
265
|
};
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
const type = Array.isArray(value) ? "array" : typeof value;
|
|
354
|
-
return /^[aeiou]/.test(type) ? `an ${type}` : `a ${type}`;
|
|
266
|
+
const describeType = (value) => {
|
|
267
|
+
if (value === null || value === void 0) return String(value);
|
|
268
|
+
const type = Array.isArray(value) ? "array" : typeof value;
|
|
269
|
+
return /^[aeiou]/.test(type) ? `an ${type}` : `a ${type}`;
|
|
355
270
|
};
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
return describeType(value);
|
|
364
|
-
}
|
|
271
|
+
const describeValue = (value) => {
|
|
272
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
273
|
+
try {
|
|
274
|
+
return String(value);
|
|
275
|
+
} catch {
|
|
276
|
+
return describeType(value);
|
|
277
|
+
}
|
|
365
278
|
};
|
|
366
|
-
|
|
367
|
-
`${context === void 0 ? "" : `${context}: `}${path} is ${describeValue(value)}; ${remedy}`
|
|
368
|
-
);
|
|
279
|
+
const refuseOption = (path, value, remedy, context) => new MountayaConfigurationError(`${context === void 0 ? "" : `${context}: `}${path} is ${describeValue(value)}; ${remedy}`);
|
|
369
280
|
var MountayaConnectionError = class extends MountayaError {
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
super(message, { cause, idempotencyKey });
|
|
378
|
-
}
|
|
281
|
+
name = "MountayaConnectionError";
|
|
282
|
+
constructor(message, cause, idempotencyKey) {
|
|
283
|
+
super(message, {
|
|
284
|
+
cause,
|
|
285
|
+
idempotencyKey
|
|
286
|
+
});
|
|
287
|
+
}
|
|
379
288
|
};
|
|
380
289
|
var MountayaApiError = class extends MountayaError {
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
*
|
|
407
|
-
* Every 429 of a limit names one, the wait a spent limit asks for
|
|
408
|
-
* ({@link MountayaRateLimitError}); a ceiling's 429, coded `LIMIT_REACHED`, names none, since no
|
|
409
|
-
* wait lifts it. A 503 names one where the API knows when it can answer, and a 409 whose
|
|
410
|
-
* `Idempotency-Key` is still in use names the second the first request may take. The client waits each out itself where it
|
|
411
|
-
* repeats the call and the wait fits its `retry.maxWaitMs`; an error that arrives carrying one
|
|
412
|
-
* is the wait a caller schedules the next call from, and one carrying none says nothing of when.
|
|
413
|
-
*/
|
|
414
|
-
retryAfter;
|
|
415
|
-
/**
|
|
416
|
-
* @param message - the message {@link failureMessage} spells for the call.
|
|
417
|
-
* @param status - the HTTP status the response carried.
|
|
418
|
-
* @param errors - the envelope's entries, the first of which classifies the response.
|
|
419
|
-
* @param details - the envelope's metadata, the request id, the rate-limit state and the wait
|
|
420
|
-
* `Retry-After` names, where the answer sent them, the `Idempotency-Key` the call sent,
|
|
421
|
-
* where it sent one, and the `cause` that kept the answer from being read, where one did.
|
|
422
|
-
*/
|
|
423
|
-
constructor(message, status, errors, details = {}) {
|
|
424
|
-
super(
|
|
425
|
-
message,
|
|
426
|
-
details.cause === void 0 ? { idempotencyKey: details.idempotencyKey } : { cause: details.cause, idempotencyKey: details.idempotencyKey }
|
|
427
|
-
);
|
|
428
|
-
this.status = status;
|
|
429
|
-
this.errors = errors;
|
|
430
|
-
this.metadata = details.metadata;
|
|
431
|
-
this.rateLimit = details.rateLimit;
|
|
432
|
-
this.requestId = details.requestId;
|
|
433
|
-
this.retryAfter = details.retryAfter;
|
|
434
|
-
}
|
|
435
|
-
/** The classifying code of the response, taken from its first entry. */
|
|
436
|
-
get code() {
|
|
437
|
-
return this.errors[0]?.extensions?.code;
|
|
438
|
-
}
|
|
439
|
-
/** Reports whether any entry carries the given code. */
|
|
440
|
-
hasCode(code) {
|
|
441
|
-
return this.errors.some((entry) => entry.extensions?.code === code);
|
|
442
|
-
}
|
|
290
|
+
name = "MountayaApiError";
|
|
291
|
+
status;
|
|
292
|
+
errors;
|
|
293
|
+
metadata;
|
|
294
|
+
rateLimit;
|
|
295
|
+
requestId;
|
|
296
|
+
retryAfter;
|
|
297
|
+
constructor(message, status, errors, details = {}) {
|
|
298
|
+
super(message, details.cause === void 0 ? { idempotencyKey: details.idempotencyKey } : {
|
|
299
|
+
cause: details.cause,
|
|
300
|
+
idempotencyKey: details.idempotencyKey
|
|
301
|
+
});
|
|
302
|
+
this.status = status;
|
|
303
|
+
this.errors = errors;
|
|
304
|
+
this.metadata = details.metadata;
|
|
305
|
+
this.rateLimit = details.rateLimit;
|
|
306
|
+
this.requestId = details.requestId;
|
|
307
|
+
this.retryAfter = details.retryAfter;
|
|
308
|
+
}
|
|
309
|
+
get code() {
|
|
310
|
+
return this.errors[0]?.extensions?.code;
|
|
311
|
+
}
|
|
312
|
+
hasCode(code) {
|
|
313
|
+
return this.errors.some((entry) => entry.extensions?.code === code);
|
|
314
|
+
}
|
|
443
315
|
};
|
|
444
316
|
var MountayaBadRequestError = class extends MountayaApiError {
|
|
445
|
-
|
|
317
|
+
name = "MountayaBadRequestError";
|
|
446
318
|
};
|
|
447
319
|
var MountayaAuthenticationError = class extends MountayaApiError {
|
|
448
|
-
|
|
320
|
+
name = "MountayaAuthenticationError";
|
|
449
321
|
};
|
|
450
322
|
var MountayaBillingError = class extends MountayaApiError {
|
|
451
|
-
|
|
323
|
+
name = "MountayaBillingError";
|
|
452
324
|
};
|
|
453
325
|
var MountayaForbiddenError = class extends MountayaApiError {
|
|
454
|
-
|
|
326
|
+
name = "MountayaForbiddenError";
|
|
455
327
|
};
|
|
456
328
|
var MountayaNotFoundError = class extends MountayaApiError {
|
|
457
|
-
|
|
329
|
+
name = "MountayaNotFoundError";
|
|
458
330
|
};
|
|
459
331
|
var MountayaMethodNotAllowedError = class extends MountayaApiError {
|
|
460
|
-
|
|
332
|
+
name = "MountayaMethodNotAllowedError";
|
|
461
333
|
};
|
|
462
334
|
var MountayaConflictError = class extends MountayaApiError {
|
|
463
|
-
|
|
464
|
-
name = "MountayaConflictError";
|
|
335
|
+
name = "MountayaConflictError";
|
|
465
336
|
};
|
|
466
|
-
|
|
467
|
-
var CEILING_CODE = "LIMIT_REACHED";
|
|
337
|
+
const KEY_IN_USE_CODE = "IDEMPOTENCY_KEY_IN_USE";
|
|
468
338
|
var MountayaIdempotencyKeyInUseError = class extends MountayaConflictError {
|
|
469
|
-
|
|
339
|
+
name = "MountayaIdempotencyKeyInUseError";
|
|
470
340
|
};
|
|
471
|
-
|
|
341
|
+
const IDEMPOTENT_ANSWER_UNAVAILABLE_CODE = "IDEMPOTENCY_ANSWER_UNAVAILABLE";
|
|
472
342
|
var MountayaIdempotentAnswerUnavailableError = class extends MountayaConflictError {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
super(message, status, errors, details);
|
|
485
|
-
const { etag: taggedAs, location: createdAt, status: answeredWith } = details.metadata ?? {};
|
|
486
|
-
this.answer = {
|
|
487
|
-
etag: typeof taggedAs === "string" ? taggedAs : void 0,
|
|
488
|
-
location: typeof createdAt === "string" ? createdAt : void 0,
|
|
489
|
-
status: typeof answeredWith === "number" && Number.isInteger(answeredWith) ? answeredWith : void 0
|
|
490
|
-
};
|
|
491
|
-
}
|
|
343
|
+
name = "MountayaIdempotentAnswerUnavailableError";
|
|
344
|
+
answer;
|
|
345
|
+
constructor(message, status, errors, details = {}) {
|
|
346
|
+
super(message, status, errors, details);
|
|
347
|
+
const { etag: taggedAs, location: createdAt, status: answeredWith } = details.metadata ?? {};
|
|
348
|
+
this.answer = {
|
|
349
|
+
etag: typeof taggedAs === "string" ? taggedAs : void 0,
|
|
350
|
+
location: typeof createdAt === "string" ? createdAt : void 0,
|
|
351
|
+
status: typeof answeredWith === "number" && Number.isInteger(answeredWith) ? answeredWith : void 0
|
|
352
|
+
};
|
|
353
|
+
}
|
|
492
354
|
};
|
|
493
355
|
var MountayaPreconditionFailedError = class extends MountayaApiError {
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
*/
|
|
501
|
-
etag;
|
|
502
|
-
/**
|
|
503
|
-
* @param message - the message {@link failureMessage} spells for the call.
|
|
504
|
-
* @param status - the HTTP status the response carried, which is 412.
|
|
505
|
-
* @param errors - the envelope's entries, the first of which classifies the response.
|
|
506
|
-
* @param details - what the answer sent beside them, its `ETag` included.
|
|
507
|
-
*/
|
|
508
|
-
constructor(message, status, errors, details = {}) {
|
|
509
|
-
super(message, status, errors, details);
|
|
510
|
-
this.etag = details.etag;
|
|
511
|
-
}
|
|
356
|
+
name = "MountayaPreconditionFailedError";
|
|
357
|
+
etag;
|
|
358
|
+
constructor(message, status, errors, details = {}) {
|
|
359
|
+
super(message, status, errors, details);
|
|
360
|
+
this.etag = details.etag;
|
|
361
|
+
}
|
|
512
362
|
};
|
|
513
363
|
var MountayaPayloadTooLargeError = class extends MountayaApiError {
|
|
514
|
-
|
|
364
|
+
name = "MountayaPayloadTooLargeError";
|
|
515
365
|
};
|
|
516
366
|
var MountayaRateLimitError = class extends MountayaApiError {
|
|
517
|
-
|
|
367
|
+
name = "MountayaRateLimitError";
|
|
518
368
|
};
|
|
519
369
|
var MountayaServerError = class extends MountayaApiError {
|
|
520
|
-
|
|
370
|
+
name = "MountayaServerError";
|
|
521
371
|
};
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
372
|
+
const MOUNTAYA_API_ERROR_NAMES = [
|
|
373
|
+
"MountayaApiError",
|
|
374
|
+
"MountayaAuthenticationError",
|
|
375
|
+
"MountayaBadRequestError",
|
|
376
|
+
"MountayaBillingError",
|
|
377
|
+
"MountayaConflictError",
|
|
378
|
+
"MountayaDataError",
|
|
379
|
+
"MountayaForbiddenError",
|
|
380
|
+
"MountayaIdempotencyKeyInUseError",
|
|
381
|
+
"MountayaIdempotentAnswerUnavailableError",
|
|
382
|
+
"MountayaMethodNotAllowedError",
|
|
383
|
+
"MountayaNotFoundError",
|
|
384
|
+
"MountayaPayloadTooLargeError",
|
|
385
|
+
"MountayaPreconditionFailedError",
|
|
386
|
+
"MountayaRateLimitError",
|
|
387
|
+
"MountayaServerError"
|
|
538
388
|
];
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
389
|
+
const MOUNTAYA_ERROR_NAMES = [
|
|
390
|
+
...MOUNTAYA_API_ERROR_NAMES,
|
|
391
|
+
"MountayaConfigurationError",
|
|
392
|
+
"MountayaConnectionError",
|
|
393
|
+
"MountayaError",
|
|
394
|
+
"MountayaSelectionError",
|
|
395
|
+
"MountayaWebhookSignatureError",
|
|
396
|
+
"SseBufferOverflowError"
|
|
547
397
|
];
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
398
|
+
const declaresName = (value, names) => typeof value === "object" && value !== null && "name" in value && typeof value.name === "string" && names.includes(value.name);
|
|
399
|
+
const isMountayaError = (error) => declaresName(error, MOUNTAYA_ERROR_NAMES);
|
|
400
|
+
const isMountayaApiError = (error) => declaresName(error, MOUNTAYA_API_ERROR_NAMES) && "status" in error && typeof error.status === "number" && "errors" in error && Array.isArray(error.errors) && error.errors.every((entry) => typeof entry === "object" && entry !== null) && "hasCode" in error && typeof error.hasCode === "function";
|
|
401
|
+
const isErrorEntry = (entry) => typeof entry === "object" && entry !== null && "message" in entry && typeof entry.message === "string";
|
|
402
|
+
const errorsOf = (body) => typeof body === "object" && body !== null && "errors" in body ? body.errors : void 0;
|
|
403
|
+
const entriesOf = (body) => {
|
|
404
|
+
const errors = errorsOf(body);
|
|
405
|
+
return Array.isArray(errors) ? errors.filter(isErrorEntry) : [];
|
|
556
406
|
};
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
return void 0;
|
|
565
|
-
}
|
|
566
|
-
const { metadata } = extensions;
|
|
567
|
-
return isMetadata(metadata) ? metadata : void 0;
|
|
407
|
+
const isMetadata = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
408
|
+
const metadataOf = (body) => {
|
|
409
|
+
if (typeof body !== "object" || body === null || !("extensions" in body)) return;
|
|
410
|
+
const { extensions } = body;
|
|
411
|
+
if (typeof extensions !== "object" || extensions === null || !("metadata" in extensions)) return;
|
|
412
|
+
const { metadata } = extensions;
|
|
413
|
+
return isMetadata(metadata) ? metadata : void 0;
|
|
568
414
|
};
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
415
|
+
const BILLING_STEP = "The API's message says which applies: a product of the organization's agreement is changed with Mountaya (https://mountaya.com/en/contact/), and a missing payment method is added, or an invoice still owed paid, in the organization's billing settings.";
|
|
416
|
+
const CEILING_STEP = "No wait lifts a ceiling, so this call is not repeated: free a place in what the error's metadata names, or change the organization's agreement with Mountaya (https://mountaya.com/en/contact/) where it sets that ceiling.";
|
|
417
|
+
const secondsSpelled = (count) => `${count} ${count === 1 ? "second" : "seconds"}`;
|
|
418
|
+
const momentAfter = (seconds) => {
|
|
419
|
+
const end = new Date(Date.now() + seconds * 1e3);
|
|
420
|
+
return Number.isNaN(end.getTime()) ? void 0 : end.toISOString().replace(/\.\d{3}Z$/, "Z");
|
|
575
421
|
};
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
return `The organization's monthly allowance on this API is spent: it renews when the next billing period begins${when}, so this call is not repeated before then. A larger allowance is set in the organization's agreement with Mountaya (https://mountaya.com/en/contact/).`;
|
|
422
|
+
const monthlyAllowanceStep = (retryAfterSeconds) => {
|
|
423
|
+
const moment = retryAfterSeconds === void 0 ? void 0 : momentAfter(retryAfterSeconds);
|
|
424
|
+
return `The organization's monthly allowance on this API is spent: it renews when the next billing period begins${retryAfterSeconds === void 0 || retryAfterSeconds === 0 ? "" : ` in ${secondsSpelled(retryAfterSeconds)}${moment === void 0 ? "" : ` (at ${moment})`}`}, so this call is not repeated before then. A larger allowance is set in the organization's agreement with Mountaya (https://mountaya.com/en/contact/).`;
|
|
580
425
|
};
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
426
|
+
const spendsMonthlyAllowance = (rateLimit) => {
|
|
427
|
+
const policies = rateLimit?.policies ?? [];
|
|
428
|
+
return policies.some((policy) => policy.name === "month" && policy.remaining === 0) && policies.every((policy) => policy.name === "month" || policy.remaining !== 0);
|
|
584
429
|
};
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
if (code === CEILING_CODE) {
|
|
617
|
-
return CEILING_STEP;
|
|
618
|
-
}
|
|
619
|
-
if (monthlyAllowanceSpent) {
|
|
620
|
-
return monthlyAllowanceStep(retryAfterSeconds);
|
|
621
|
-
}
|
|
622
|
-
const wait = retryAfterSeconds === void 0 || retryAfterSeconds === 0 ? "Wait before calling again." : `Wait ${secondsSpelled(retryAfterSeconds)} before calling again.`;
|
|
623
|
-
return mints ? `${wait} ${MINT_LIMIT_STEP}` : wait;
|
|
624
|
-
}
|
|
625
|
-
default:
|
|
626
|
-
return void 0;
|
|
627
|
-
}
|
|
430
|
+
const CREDENTIAL_STEP = "Check the credential this client sends: a key that is unknown, rotated or disabled, and a session token that has expired, are refused.";
|
|
431
|
+
const MEMBER_SESSION_STEP = "This call acts as the user its session token was minted for, so mint the token with postSession's user_id set to that user.";
|
|
432
|
+
const SECRET_KEY_STEP = "Check the secret key this client sends: one that is unknown, revoked or malformed, or one of a canceled organization, is refused.";
|
|
433
|
+
const FORBIDDEN_STEP = "Check that the key carries the scope this call needs, that the resource belongs to the key's organization, and that the page calling is one the organization allows.";
|
|
434
|
+
const ACTING_USER_STEP = "This call acts as the user its session token names (postSession's user_id), who must be a member of the key's organization holding the role the call asks.";
|
|
435
|
+
const MINT_REFUSAL_STEP = "Check that the publishable key in the body is one this secret key's organization issued, and that user_id names a member of that organization or a user holding an invitation to it.";
|
|
436
|
+
const PRECONDITION_STEP = "The resource changed since the etag this write names was read: read it again, and decide what the write should be now.";
|
|
437
|
+
const IDEMPOTENT_ANSWER_STEP = "The write was made, and its answer cannot be given again: read the resource again rather than sending the write once more.";
|
|
438
|
+
const KEY_IN_USE_STEP = "The first request with this Idempotency-Key is still running: send the same call again with the error's idempotencyKey, never a new key, which would make the write twice.";
|
|
439
|
+
const MINT_LIMIT_STEP = "Minting is limited per secret key, per client IP address and secret key whose requests keep failing, and per user_id it refuses, so check the publishable key and the user_id before asking again.";
|
|
440
|
+
const nextStepFor = (status, code, retryAfterSeconds, monthlyAllowanceSpent, declaration) => {
|
|
441
|
+
const mints = declaration.requiresSecretKey === true;
|
|
442
|
+
const actsAsMember = declaration.actsAsMember === true;
|
|
443
|
+
switch (status) {
|
|
444
|
+
case 401:
|
|
445
|
+
if (mints) return SECRET_KEY_STEP;
|
|
446
|
+
return actsAsMember ? `${CREDENTIAL_STEP} ${MEMBER_SESSION_STEP}` : CREDENTIAL_STEP;
|
|
447
|
+
case 402: return BILLING_STEP;
|
|
448
|
+
case 403:
|
|
449
|
+
if (mints) return MINT_REFUSAL_STEP;
|
|
450
|
+
return actsAsMember ? `${FORBIDDEN_STEP} ${ACTING_USER_STEP}` : FORBIDDEN_STEP;
|
|
451
|
+
case 409: return code === "IDEMPOTENCY_ANSWER_UNAVAILABLE" ? IDEMPOTENT_ANSWER_STEP : code === "IDEMPOTENCY_KEY_IN_USE" ? KEY_IN_USE_STEP : void 0;
|
|
452
|
+
case 412: return PRECONDITION_STEP;
|
|
453
|
+
case 429: {
|
|
454
|
+
if (code === "LIMIT_REACHED") return CEILING_STEP;
|
|
455
|
+
if (monthlyAllowanceSpent) return monthlyAllowanceStep(retryAfterSeconds);
|
|
456
|
+
const wait = retryAfterSeconds === void 0 || retryAfterSeconds === 0 ? "Wait before calling again." : `Wait ${secondsSpelled(retryAfterSeconds)} before calling again.`;
|
|
457
|
+
return mints ? `${wait} ${MINT_LIMIT_STEP}` : wait;
|
|
458
|
+
}
|
|
459
|
+
default: return;
|
|
460
|
+
}
|
|
628
461
|
};
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
facts.declaration ?? {}
|
|
641
|
-
);
|
|
642
|
-
const sentence = next === void 0 ? summary : `${/[.!?]$/.test(summary) ? summary : `${summary}.`} ${next}`;
|
|
643
|
-
return facts.requestId === void 0 ? sentence : `${sentence} (request ${facts.requestId})`;
|
|
462
|
+
const failureMessage = (context, status, entries, facts = {}) => {
|
|
463
|
+
const first = entries[0] ?? {};
|
|
464
|
+
const parts = [
|
|
465
|
+
status,
|
|
466
|
+
first.extensions?.code,
|
|
467
|
+
first.message
|
|
468
|
+
].map((part) => typeof part === "string" ? part.trim() : typeof part === "number" ? String(part) : "").filter((part) => part !== "");
|
|
469
|
+
const summary = parts.length === 0 ? context : `${context}: ${parts.join(" ")}`;
|
|
470
|
+
const next = status === void 0 ? void 0 : nextStepFor(status, first.extensions?.code, facts.retryAfterSeconds, spendsMonthlyAllowance(facts.rateLimit), facts.declaration ?? {});
|
|
471
|
+
const sentence = next === void 0 ? summary : `${/[.!?]$/.test(summary) ? summary : `${summary}.`} ${next}`;
|
|
472
|
+
return facts.requestId === void 0 ? sentence : `${sentence} (request ${facts.requestId})`;
|
|
644
473
|
};
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
474
|
+
const MONTHS = [
|
|
475
|
+
"Jan",
|
|
476
|
+
"Feb",
|
|
477
|
+
"Mar",
|
|
478
|
+
"Apr",
|
|
479
|
+
"May",
|
|
480
|
+
"Jun",
|
|
481
|
+
"Jul",
|
|
482
|
+
"Aug",
|
|
483
|
+
"Sep",
|
|
484
|
+
"Oct",
|
|
485
|
+
"Nov",
|
|
486
|
+
"Dec"
|
|
487
|
+
];
|
|
488
|
+
const delaySecondsPattern = /^[ \t]*(\d+)[ \t]*$/;
|
|
489
|
+
const MAX_DELAY_SECONDS_COUNT = 9223372036854775807n;
|
|
490
|
+
const imfFixdatePattern = /^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), (\d{2}) ([A-Z][a-z]{2}) (\d{4}) (\d{2}):(\d{2}):(\d{2}) GMT$/;
|
|
491
|
+
const rfc850DatePattern = /^(?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), (\d{2})-([A-Z][a-z]{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}) GMT$/;
|
|
492
|
+
const asctimeDatePattern = /^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) ([A-Z][a-z]{2}) ([ \d]\d) (\d{2}):(\d{2}):(\d{2}) (\d{4})$/;
|
|
493
|
+
const utcInstant = (year, month, day, hour, minute, second) => {
|
|
494
|
+
const monthIndex = MONTHS.indexOf(month);
|
|
495
|
+
if (monthIndex < 0 || hour > 23 || minute > 59 || second > 60) return;
|
|
496
|
+
const midnight = /* @__PURE__ */ new Date(0);
|
|
497
|
+
midnight.setUTCFullYear(year, monthIndex, day);
|
|
498
|
+
if (midnight.getUTCMonth() !== monthIndex || midnight.getUTCDate() !== day) return;
|
|
499
|
+
return midnight.getTime() + ((hour * 60 + minute) * 60 + second) * 1e3;
|
|
662
500
|
};
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
return utcInstant(year, month ?? "", Number(day), Number(hour), Number(minute), Number(second));
|
|
683
|
-
}
|
|
684
|
-
const asctime = asctimeDatePattern.exec(value);
|
|
685
|
-
if (asctime !== null) {
|
|
686
|
-
const [, month, day, hour, minute, second, year] = asctime;
|
|
687
|
-
return utcInstant(
|
|
688
|
-
Number(year),
|
|
689
|
-
month ?? "",
|
|
690
|
-
Number(day),
|
|
691
|
-
Number(hour),
|
|
692
|
-
Number(minute),
|
|
693
|
-
Number(second)
|
|
694
|
-
);
|
|
695
|
-
}
|
|
696
|
-
return void 0;
|
|
501
|
+
const httpDate = (value, now) => {
|
|
502
|
+
const fixdate = imfFixdatePattern.exec(value);
|
|
503
|
+
if (fixdate !== null) {
|
|
504
|
+
const [, day, month, year, hour, minute, second] = fixdate;
|
|
505
|
+
return utcInstant(Number(year), month ?? "", Number(day), Number(hour), Number(minute), Number(second));
|
|
506
|
+
}
|
|
507
|
+
const rfc850 = rfc850DatePattern.exec(value);
|
|
508
|
+
if (rfc850 !== null) {
|
|
509
|
+
const [, day, month, shortYear, hour, minute, second] = rfc850;
|
|
510
|
+
const currentYear = new Date(now).getUTCFullYear();
|
|
511
|
+
const sameDigits = currentYear - currentYear % 100 + Number(shortYear);
|
|
512
|
+
const year = sameDigits > currentYear + 50 ? sameDigits - 100 : sameDigits;
|
|
513
|
+
return utcInstant(year, month ?? "", Number(day), Number(hour), Number(minute), Number(second));
|
|
514
|
+
}
|
|
515
|
+
const asctime = asctimeDatePattern.exec(value);
|
|
516
|
+
if (asctime !== null) {
|
|
517
|
+
const [, month, day, hour, minute, second, year] = asctime;
|
|
518
|
+
return utcInstant(Number(year), month ?? "", Number(day), Number(hour), Number(minute), Number(second));
|
|
519
|
+
}
|
|
697
520
|
};
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
}
|
|
709
|
-
const current = now();
|
|
710
|
-
const instant = httpDate(header.replace(/^[ \t]+|[ \t]+$/g, ""), current);
|
|
711
|
-
return instant === void 0 ? void 0 : Math.max(0, Math.ceil((instant - current) / 1e3));
|
|
521
|
+
const parseRetryAfter = (header, now = Date.now) => {
|
|
522
|
+
if (header === null) return;
|
|
523
|
+
const digits = delaySecondsPattern.exec(header)?.[1];
|
|
524
|
+
if (digits !== void 0) {
|
|
525
|
+
if (BigInt(digits) > MAX_DELAY_SECONDS_COUNT) return;
|
|
526
|
+
return Math.min(Number(digits), Number.MAX_SAFE_INTEGER);
|
|
527
|
+
}
|
|
528
|
+
const current = now();
|
|
529
|
+
const instant = httpDate(header.replace(/^[ \t]+|[ \t]+$/g, ""), current);
|
|
530
|
+
return instant === void 0 ? void 0 : Math.max(0, Math.ceil((instant - current) / 1e3));
|
|
712
531
|
};
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
532
|
+
const answerDetailsOf = (headers) => {
|
|
533
|
+
const requestId = headers.get("X-Request-Id")?.trim();
|
|
534
|
+
return {
|
|
535
|
+
rateLimit: parseRateLimit(headers),
|
|
536
|
+
requestId: requestId === void 0 || requestId === "" ? void 0 : requestId
|
|
537
|
+
};
|
|
719
538
|
};
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
idempotencyKey,
|
|
737
|
-
metadata: metadataOf(body),
|
|
738
|
-
retryAfter: retryAfterSeconds
|
|
739
|
-
}
|
|
740
|
-
);
|
|
539
|
+
const errorOfBody = (body, answer, context, declaration, idempotencyKey) => {
|
|
540
|
+
const entries = entriesOf(body);
|
|
541
|
+
const retryAfterSeconds = parseRetryAfter(answer.headers.get("Retry-After"));
|
|
542
|
+
const details = answerDetailsOf(answer.headers);
|
|
543
|
+
return errorForStatus(failureMessage(context, answer.status, entries, {
|
|
544
|
+
declaration,
|
|
545
|
+
rateLimit: details.rateLimit,
|
|
546
|
+
requestId: details.requestId,
|
|
547
|
+
retryAfterSeconds
|
|
548
|
+
}), answer.status, entries, {
|
|
549
|
+
...details,
|
|
550
|
+
etag: answer.headers.get("ETag") ?? void 0,
|
|
551
|
+
idempotencyKey,
|
|
552
|
+
metadata: metadataOf(body),
|
|
553
|
+
retryAfter: retryAfterSeconds
|
|
554
|
+
});
|
|
741
555
|
};
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
try {
|
|
752
|
-
body = JSON.parse(text);
|
|
753
|
-
} catch {
|
|
754
|
-
body = void 0;
|
|
755
|
-
}
|
|
756
|
-
return errorOfBody(body, answer, context, declaration, idempotencyKey);
|
|
556
|
+
const errorFromResponse = async (response, context, declaration, idempotencyKey) => errorOfBody(await response.json().catch(() => void 0), response, context, declaration, idempotencyKey);
|
|
557
|
+
const errorFromBody = (text, answer, context, declaration, idempotencyKey) => {
|
|
558
|
+
let body;
|
|
559
|
+
try {
|
|
560
|
+
body = JSON.parse(text);
|
|
561
|
+
} catch {
|
|
562
|
+
body = void 0;
|
|
563
|
+
}
|
|
564
|
+
return errorOfBody(body, answer, context, declaration, idempotencyKey);
|
|
757
565
|
};
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
return new MountayaPayloadTooLargeError(...args);
|
|
787
|
-
case 429:
|
|
788
|
-
return new MountayaRateLimitError(...args);
|
|
789
|
-
default:
|
|
790
|
-
return status >= 500 ? new MountayaServerError(...args) : new MountayaApiError(...args);
|
|
791
|
-
}
|
|
566
|
+
const errorForStatus = (message, status, entries = [], details = {}) => {
|
|
567
|
+
const { etag, ...rest } = details;
|
|
568
|
+
const args = [
|
|
569
|
+
message,
|
|
570
|
+
status,
|
|
571
|
+
entries,
|
|
572
|
+
rest
|
|
573
|
+
];
|
|
574
|
+
switch (status) {
|
|
575
|
+
case 400: return new MountayaBadRequestError(...args);
|
|
576
|
+
case 401: return new MountayaAuthenticationError(...args);
|
|
577
|
+
case 402: return new MountayaBillingError(...args);
|
|
578
|
+
case 403: return new MountayaForbiddenError(...args);
|
|
579
|
+
case 404: return new MountayaNotFoundError(...args);
|
|
580
|
+
case 405: return new MountayaMethodNotAllowedError(...args);
|
|
581
|
+
case 409: switch (entries[0]?.extensions?.code) {
|
|
582
|
+
case IDEMPOTENT_ANSWER_UNAVAILABLE_CODE: return new MountayaIdempotentAnswerUnavailableError(...args);
|
|
583
|
+
case KEY_IN_USE_CODE: return new MountayaIdempotencyKeyInUseError(...args);
|
|
584
|
+
default: return new MountayaConflictError(...args);
|
|
585
|
+
}
|
|
586
|
+
case 412: return new MountayaPreconditionFailedError(message, status, entries, {
|
|
587
|
+
...rest,
|
|
588
|
+
etag
|
|
589
|
+
});
|
|
590
|
+
case 413: return new MountayaPayloadTooLargeError(...args);
|
|
591
|
+
case 429: return new MountayaRateLimitError(...args);
|
|
592
|
+
default: return status >= 500 ? new MountayaServerError(...args) : new MountayaApiError(...args);
|
|
593
|
+
}
|
|
792
594
|
};
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
signal: options?.signal,
|
|
820
|
-
surface: "rest",
|
|
821
|
-
takesIdempotencyKey: true,
|
|
822
|
-
timeoutMs: options?.timeoutMs
|
|
595
|
+
const agentRunDates = { created_at: true };
|
|
596
|
+
const collabStreamEventDatesByType = {
|
|
597
|
+
"collection.changed": { occurred_at: true },
|
|
598
|
+
"itinerary.changed": { occurred_at: true },
|
|
599
|
+
"presence.join": { connected_at: true },
|
|
600
|
+
"presence.snapshot": { members: [{ connected_at: true }] },
|
|
601
|
+
"segment.changed": { occurred_at: true },
|
|
602
|
+
"waypoint.changed": { occurred_at: true },
|
|
603
|
+
"zone.changed": { occurred_at: true }
|
|
604
|
+
};
|
|
605
|
+
const postAgentRun = (transport, args, options) => transport.request({
|
|
606
|
+
actsAsMember: true,
|
|
607
|
+
body: args.body,
|
|
608
|
+
dates: { data: agentRunDates },
|
|
609
|
+
idempotencyKey: args.idempotencyKey,
|
|
610
|
+
idempotent: false,
|
|
611
|
+
method: "POST",
|
|
612
|
+
operationId: "postAgentRun",
|
|
613
|
+
path: "/v1/agent/runs",
|
|
614
|
+
requiresSession: true,
|
|
615
|
+
retry: options?.retry,
|
|
616
|
+
serverBudgetMs: 3e4,
|
|
617
|
+
signal: options?.signal,
|
|
618
|
+
surface: "rest",
|
|
619
|
+
takesIdempotencyKey: true,
|
|
620
|
+
timeoutMs: options?.timeoutMs
|
|
823
621
|
});
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
622
|
+
const postAgentRunInput = (transport, args, options) => transport.request({
|
|
623
|
+
actsAsMember: true,
|
|
624
|
+
body: args.body,
|
|
625
|
+
idempotencyKey: args.idempotencyKey,
|
|
626
|
+
idempotent: false,
|
|
627
|
+
method: "POST",
|
|
628
|
+
operationId: "postAgentRunInput",
|
|
629
|
+
path: `/v1/agent/runs/${encodeURIComponent(String(args.run_id))}/input`,
|
|
630
|
+
requiresSession: true,
|
|
631
|
+
retry: options?.retry,
|
|
632
|
+
serverBudgetMs: 3e4,
|
|
633
|
+
signal: options?.signal,
|
|
634
|
+
surface: "rest",
|
|
635
|
+
takesIdempotencyKey: true,
|
|
636
|
+
timeoutMs: options?.timeoutMs
|
|
839
637
|
});
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
638
|
+
const postItineraryCollab = (transport, args, options) => transport.request({
|
|
639
|
+
actsAsMember: true,
|
|
640
|
+
body: args.body,
|
|
641
|
+
idempotent: true,
|
|
642
|
+
method: "POST",
|
|
643
|
+
operationId: "postItineraryCollab",
|
|
644
|
+
path: `/v1/itineraries/${encodeURIComponent(String(args.itinerary_id))}/collab`,
|
|
645
|
+
requiresSession: true,
|
|
646
|
+
retry: options?.retry,
|
|
647
|
+
serverBudgetMs: 3e4,
|
|
648
|
+
signal: options?.signal,
|
|
649
|
+
surface: "rest",
|
|
650
|
+
timeoutMs: options?.timeoutMs
|
|
853
651
|
});
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
652
|
+
const postZoneCollab = (transport, args, options) => transport.request({
|
|
653
|
+
actsAsMember: true,
|
|
654
|
+
body: args.body,
|
|
655
|
+
idempotent: true,
|
|
656
|
+
method: "POST",
|
|
657
|
+
operationId: "postZoneCollab",
|
|
658
|
+
path: `/v1/zones/${encodeURIComponent(String(args.zone_id))}/collab`,
|
|
659
|
+
requiresSession: true,
|
|
660
|
+
retry: options?.retry,
|
|
661
|
+
serverBudgetMs: 3e4,
|
|
662
|
+
signal: options?.signal,
|
|
663
|
+
surface: "rest",
|
|
664
|
+
timeoutMs: options?.timeoutMs
|
|
867
665
|
});
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
666
|
+
const printableAsciiPattern = /^[\x20-\x7e]*$/;
|
|
667
|
+
const isHeaderValue = (value) => printableAsciiPattern.test(value);
|
|
668
|
+
const isoDate = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-](\d{2}):(\d{2}))$/;
|
|
669
|
+
const ISO_DATE_MIN_LENGTH = 20;
|
|
670
|
+
const ISO_DATE_SEPARATOR = 84;
|
|
671
|
+
const MONTH_DAYS = [
|
|
672
|
+
31,
|
|
673
|
+
28,
|
|
674
|
+
31,
|
|
675
|
+
30,
|
|
676
|
+
31,
|
|
677
|
+
30,
|
|
678
|
+
31,
|
|
679
|
+
31,
|
|
680
|
+
30,
|
|
681
|
+
31,
|
|
682
|
+
30,
|
|
683
|
+
31
|
|
684
|
+
];
|
|
685
|
+
const isLeapYear = (year) => year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
|
|
686
|
+
const daysIn = (year, month) => month === 2 && isLeapYear(year) ? 29 : MONTH_DAYS[month - 1] ?? 0;
|
|
687
|
+
const instantOf = (value) => {
|
|
688
|
+
if (value.length < ISO_DATE_MIN_LENGTH || value.charCodeAt(10) !== ISO_DATE_SEPARATOR) return;
|
|
689
|
+
const fields = isoDate.exec(value);
|
|
690
|
+
if (fields === null) return;
|
|
691
|
+
const field = (index) => Number(fields[index] ?? 0);
|
|
692
|
+
const year = field(1);
|
|
693
|
+
const month = field(2);
|
|
694
|
+
const day = field(3);
|
|
695
|
+
if (!(month >= 1 && month <= 12 && day >= 1 && day <= daysIn(year, month) && field(4) <= 23 && field(5) <= 59 && field(6) <= 59 && field(7) <= 23 && field(8) <= 59)) return;
|
|
696
|
+
const instant = new Date(value);
|
|
697
|
+
return Number.isNaN(instant.getTime()) ? void 0 : instant;
|
|
898
698
|
};
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
return value;
|
|
925
|
-
}
|
|
926
|
-
if (typeof value === "object" && value !== null && !isList(value)) {
|
|
927
|
-
for (const [member, memberMask] of Object.entries(mask)) {
|
|
928
|
-
if (Object.hasOwn(value, member)) {
|
|
929
|
-
const current = Reflect.get(value, member);
|
|
930
|
-
const decoded = decodeDates(current, memberMask, member);
|
|
931
|
-
if (decoded !== current) {
|
|
932
|
-
Reflect.set(value, member, decoded);
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
return value;
|
|
699
|
+
const isListMask$1 = (mask) => Array.isArray(mask);
|
|
700
|
+
const isList = (value) => Array.isArray(value);
|
|
701
|
+
const decodeDates = (value, mask, field = "the answer") => {
|
|
702
|
+
if (mask === void 0) return value;
|
|
703
|
+
if (mask === true) {
|
|
704
|
+
if (typeof value !== "string") return value;
|
|
705
|
+
const instant = instantOf(value);
|
|
706
|
+
if (instant === void 0) throw new MountayaError(`${field}, which the contract declares an instant, holds a string that names none: an instant is a full ISO 8601 timestamp carrying its zone.`);
|
|
707
|
+
return instant;
|
|
708
|
+
}
|
|
709
|
+
if (isListMask$1(mask)) {
|
|
710
|
+
if (isList(value)) {
|
|
711
|
+
const [element] = mask;
|
|
712
|
+
for (let index = 0; index < value.length; index += 1) value[index] = decodeDates(value[index], element, field);
|
|
713
|
+
}
|
|
714
|
+
return value;
|
|
715
|
+
}
|
|
716
|
+
if (typeof value === "object" && value !== null && !isList(value)) {
|
|
717
|
+
for (const [member, memberMask] of Object.entries(mask)) if (Object.hasOwn(value, member)) {
|
|
718
|
+
const current = Reflect.get(value, member);
|
|
719
|
+
const decoded = decodeDates(current, memberMask, member);
|
|
720
|
+
if (decoded !== current) Reflect.set(value, member, decoded);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
return value;
|
|
938
724
|
};
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
};
|
|
967
|
-
});
|
|
968
|
-
try {
|
|
969
|
-
const settled = await Promise.race([work(), aborted]);
|
|
970
|
-
if (isAborted(signal)) {
|
|
971
|
-
throw abortReasonOf(signal);
|
|
972
|
-
}
|
|
973
|
-
return settled;
|
|
974
|
-
} finally {
|
|
975
|
-
stopWatching();
|
|
976
|
-
}
|
|
725
|
+
const parseJsonWithDates = (text, mask) => text.length > 0 ? decodeDates(JSON.parse(text), mask) : void 0;
|
|
726
|
+
const MAX_TIMER_DELAY_MS = 2 ** 31 - 1;
|
|
727
|
+
const namedError = (name, message) => typeof DOMException === "function" ? new DOMException(message, name) : Object.assign(new Error(message), { name });
|
|
728
|
+
const abortReasonOf = (signal) => signal.reason ?? namedError("AbortError", "This operation was aborted");
|
|
729
|
+
const REFUSAL_WAIT_MS = 5e3;
|
|
730
|
+
const MAX_REFUSAL_BYTES = 65536;
|
|
731
|
+
const isAborted = (signal) => signal.aborted;
|
|
732
|
+
const settleOnAbort = async (work, signal) => {
|
|
733
|
+
if (signal === void 0) return work();
|
|
734
|
+
if (isAborted(signal)) throw abortReasonOf(signal);
|
|
735
|
+
let stopWatching = () => {};
|
|
736
|
+
const aborted = new Promise((_resolve, reject) => {
|
|
737
|
+
const onAbort = () => {
|
|
738
|
+
reject(abortReasonOf(signal));
|
|
739
|
+
};
|
|
740
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
741
|
+
stopWatching = () => {
|
|
742
|
+
signal.removeEventListener("abort", onAbort);
|
|
743
|
+
};
|
|
744
|
+
});
|
|
745
|
+
try {
|
|
746
|
+
const settled = await Promise.race([work(), aborted]);
|
|
747
|
+
if (isAborted(signal)) throw abortReasonOf(signal);
|
|
748
|
+
return settled;
|
|
749
|
+
} finally {
|
|
750
|
+
stopWatching();
|
|
751
|
+
}
|
|
977
752
|
};
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
753
|
+
const concat = (chunks, byteLength) => {
|
|
754
|
+
const bytes = new Uint8Array(byteLength);
|
|
755
|
+
let offset = 0;
|
|
756
|
+
for (const chunk of chunks) {
|
|
757
|
+
bytes.set(chunk, offset);
|
|
758
|
+
offset += chunk.byteLength;
|
|
759
|
+
}
|
|
760
|
+
return bytes;
|
|
986
761
|
};
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
signal?.removeEventListener("abort", onAbort);
|
|
1047
|
-
cancel();
|
|
1048
|
-
letGo();
|
|
1049
|
-
}
|
|
1050
|
-
return { brokeOff, bytes: concat(chunks, received) };
|
|
762
|
+
const drain = async (body, bounds) => {
|
|
763
|
+
const { maxBytes, signal, timeoutMs } = bounds;
|
|
764
|
+
let reader;
|
|
765
|
+
try {
|
|
766
|
+
reader = body.getReader();
|
|
767
|
+
} catch (cause) {
|
|
768
|
+
if (signal !== void 0 && isAborted(signal)) throw abortReasonOf(signal);
|
|
769
|
+
return {
|
|
770
|
+
brokeOff: { cause },
|
|
771
|
+
bytes: concat([], 0)
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
const cancel = (reason) => {
|
|
775
|
+
reader.cancel(reason).catch(() => void 0);
|
|
776
|
+
};
|
|
777
|
+
const letGo = () => {
|
|
778
|
+
try {
|
|
779
|
+
reader.releaseLock();
|
|
780
|
+
} catch {}
|
|
781
|
+
};
|
|
782
|
+
if (signal !== void 0 && isAborted(signal)) {
|
|
783
|
+
cancel(abortReasonOf(signal));
|
|
784
|
+
letGo();
|
|
785
|
+
throw abortReasonOf(signal);
|
|
786
|
+
}
|
|
787
|
+
const onAbort = () => {
|
|
788
|
+
if (signal !== void 0) cancel(abortReasonOf(signal));
|
|
789
|
+
};
|
|
790
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
791
|
+
const deadline = timeoutMs === void 0 ? void 0 : setTimeout(cancel, timeoutMs);
|
|
792
|
+
const chunks = [];
|
|
793
|
+
let received = 0;
|
|
794
|
+
let brokeOff;
|
|
795
|
+
try {
|
|
796
|
+
while (true) {
|
|
797
|
+
const { done, value } = await reader.read();
|
|
798
|
+
if (signal !== void 0 && isAborted(signal)) throw abortReasonOf(signal);
|
|
799
|
+
if (done) break;
|
|
800
|
+
received += value.byteLength;
|
|
801
|
+
if (maxBytes !== void 0 && received > maxBytes) {
|
|
802
|
+
chunks.length = 0;
|
|
803
|
+
received = 0;
|
|
804
|
+
break;
|
|
805
|
+
}
|
|
806
|
+
chunks.push(value);
|
|
807
|
+
}
|
|
808
|
+
} catch (cause) {
|
|
809
|
+
if (signal !== void 0 && isAborted(signal)) throw abortReasonOf(signal);
|
|
810
|
+
brokeOff = { cause };
|
|
811
|
+
} finally {
|
|
812
|
+
clearTimeout(deadline);
|
|
813
|
+
signal?.removeEventListener("abort", onAbort);
|
|
814
|
+
cancel();
|
|
815
|
+
letGo();
|
|
816
|
+
}
|
|
817
|
+
return {
|
|
818
|
+
brokeOff,
|
|
819
|
+
bytes: concat(chunks, received)
|
|
820
|
+
};
|
|
1051
821
|
};
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
}
|
|
1057
|
-
return typeof body.getReader === "function" ? body : void 0;
|
|
822
|
+
const streamOf = (response) => {
|
|
823
|
+
const body = response.body;
|
|
824
|
+
if (response.bodyUsed || body === null || body === void 0) return;
|
|
825
|
+
return typeof body.getReader === "function" ? body : void 0;
|
|
1058
826
|
};
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
});
|
|
1069
|
-
return new TextDecoder().decode(bytes);
|
|
827
|
+
const readFailureText = async (response, signal) => {
|
|
828
|
+
const body = streamOf(response);
|
|
829
|
+
if (body === void 0) return settleOnAbort(() => response.text().catch(() => ""), signal);
|
|
830
|
+
const { bytes } = await drain(body, {
|
|
831
|
+
maxBytes: MAX_REFUSAL_BYTES,
|
|
832
|
+
signal,
|
|
833
|
+
timeoutMs: REFUSAL_WAIT_MS
|
|
834
|
+
});
|
|
835
|
+
return new TextDecoder().decode(bytes);
|
|
1070
836
|
};
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
() => errorFromResponse(response, call.operationId, declaration, call.idempotencyKey),
|
|
1079
|
-
call.signal
|
|
1080
|
-
);
|
|
1081
|
-
}
|
|
1082
|
-
return errorFromBody(
|
|
1083
|
-
await readFailureText(response, call.signal),
|
|
1084
|
-
response,
|
|
1085
|
-
call.operationId,
|
|
1086
|
-
declaration,
|
|
1087
|
-
call.idempotencyKey
|
|
1088
|
-
);
|
|
837
|
+
const readFailure = async (response, call) => {
|
|
838
|
+
const declaration = {
|
|
839
|
+
actsAsMember: call.actsAsMember,
|
|
840
|
+
requiresSecretKey: call.requiresSecretKey
|
|
841
|
+
};
|
|
842
|
+
if (streamOf(response) === void 0) return settleOnAbort(() => errorFromResponse(response, call.operationId, declaration, call.idempotencyKey), call.signal);
|
|
843
|
+
return errorFromBody(await readFailureText(response, call.signal), response, call.operationId, declaration, call.idempotencyKey);
|
|
1089
844
|
};
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
var BYTE_ORDER_MARK = "\uFEFF";
|
|
1093
|
-
var MAX_SSE_BUFFER_CHARS = 8 * 1024 * 1024;
|
|
845
|
+
const BYTE_ORDER_MARK = "";
|
|
846
|
+
const MAX_SSE_BUFFER_CHARS = 8388608;
|
|
1094
847
|
var SseBufferOverflowError = class extends MountayaError {
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
/**
|
|
1103
|
-
* Builds the error a parser throws once its unterminated record passes the cap.
|
|
1104
|
-
*
|
|
1105
|
-
* @param limit - the cap the parser enforced, in UTF-16 code units.
|
|
1106
|
-
* @param operationId - the operation whose stream overflowed, where the thrower knows it: the
|
|
1107
|
-
* message then opens with it and ends with what a reopen would meet.
|
|
1108
|
-
* @param options - the standard error options, for the `cause` an opener hands on.
|
|
1109
|
-
*/
|
|
1110
|
-
constructor(limit, operationId, options) {
|
|
1111
|
-
const overflow = `sse frame parser: unterminated frame exceeded ${limit} characters`;
|
|
1112
|
-
super(
|
|
1113
|
-
operationId === void 0 ? overflow : `${operationId}: ${overflow}. The producer never ended the record, and a reopen meets it again.`,
|
|
1114
|
-
options
|
|
1115
|
-
);
|
|
1116
|
-
this.limit = limit;
|
|
1117
|
-
}
|
|
848
|
+
name = "SseBufferOverflowError";
|
|
849
|
+
limit;
|
|
850
|
+
constructor(limit, operationId, options) {
|
|
851
|
+
const overflow = `sse frame parser: unterminated frame exceeded ${limit} characters`;
|
|
852
|
+
super(operationId === void 0 ? overflow : `${operationId}: ${overflow}. The producer never ended the record, and a reopen meets it again.`, options);
|
|
853
|
+
this.limit = limit;
|
|
854
|
+
}
|
|
1118
855
|
};
|
|
1119
856
|
var SseFrameParser = class {
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
}
|
|
1221
|
-
}
|
|
1222
|
-
const line = this.completeLine(chunk.slice(start, end));
|
|
1223
|
-
this.recordLength += line.length + (next - end);
|
|
1224
|
-
const frame = this.readLine(line);
|
|
1225
|
-
if (frame !== null) {
|
|
1226
|
-
frames.push(frame);
|
|
1227
|
-
}
|
|
1228
|
-
start = next;
|
|
1229
|
-
if (carriageReturn !== -1 && carriageReturn < start) {
|
|
1230
|
-
carriageReturn = chunk.indexOf("\r", start);
|
|
1231
|
-
}
|
|
1232
|
-
if (lineFeed !== -1 && lineFeed < start) {
|
|
1233
|
-
lineFeed = chunk.indexOf("\n", start);
|
|
1234
|
-
}
|
|
1235
|
-
}
|
|
1236
|
-
if (start < chunk.length) {
|
|
1237
|
-
this.lineParts.push(chunk.slice(start));
|
|
1238
|
-
this.lineLength += chunk.length - start;
|
|
1239
|
-
}
|
|
1240
|
-
if (this.recordLength + this.lineLength > this.maxBufferChars) {
|
|
1241
|
-
this.lineParts = [];
|
|
1242
|
-
this.lineLength = 0;
|
|
1243
|
-
this.startRecord();
|
|
1244
|
-
this.overflowed = true;
|
|
1245
|
-
throw new SseBufferOverflowError(this.maxBufferChars);
|
|
1246
|
-
}
|
|
1247
|
-
return frames;
|
|
1248
|
-
}
|
|
1249
|
-
/**
|
|
1250
|
-
* completeLine joins the line held from earlier pushes with its last piece, `tail`, and starts
|
|
1251
|
-
* the next line empty.
|
|
1252
|
-
*/
|
|
1253
|
-
completeLine(tail) {
|
|
1254
|
-
if (this.lineParts.length === 0) {
|
|
1255
|
-
return tail;
|
|
1256
|
-
}
|
|
1257
|
-
this.lineParts.push(tail);
|
|
1258
|
-
const line = this.lineParts.join("");
|
|
1259
|
-
this.lineParts = [];
|
|
1260
|
-
this.lineLength = 0;
|
|
1261
|
-
return line;
|
|
1262
|
-
}
|
|
1263
|
-
/**
|
|
1264
|
-
* readLine applies one complete line to the current record, and returns the frame the record
|
|
1265
|
-
* dispatches when the line is the empty one that ends it.
|
|
1266
|
-
*
|
|
1267
|
-
* A field line is split at its first colon, and one space after the colon is dropped: only the
|
|
1268
|
-
* first space is the standard's separator, and a second one is part of the value. A line with no
|
|
1269
|
-
* colon is its whole self as the field name, with an empty value.
|
|
1270
|
-
*/
|
|
1271
|
-
readLine(line) {
|
|
1272
|
-
if (line === "") {
|
|
1273
|
-
return this.endRecord();
|
|
1274
|
-
}
|
|
1275
|
-
if (line.startsWith(":")) {
|
|
1276
|
-
return null;
|
|
1277
|
-
}
|
|
1278
|
-
const colon = line.indexOf(":");
|
|
1279
|
-
const field = colon === -1 ? line : line.slice(0, colon);
|
|
1280
|
-
const raw = colon === -1 ? "" : line.slice(colon + 1);
|
|
1281
|
-
const value = raw.startsWith(" ") ? raw.slice(1) : raw;
|
|
1282
|
-
if (field === "data") {
|
|
1283
|
-
this.dataLines.push(value);
|
|
1284
|
-
} else if (field === "event") {
|
|
1285
|
-
this.event = value;
|
|
1286
|
-
} else if (field === "id" && !value.includes("\0")) {
|
|
1287
|
-
this.recordId = value;
|
|
1288
|
-
}
|
|
1289
|
-
return null;
|
|
1290
|
-
}
|
|
1291
|
-
/**
|
|
1292
|
-
* endRecord closes the current record: an `id:` it named becomes the stream's last event ID,
|
|
1293
|
-
* and it returns the frame the record dispatches, or null for a record with no data.
|
|
1294
|
-
*/
|
|
1295
|
-
endRecord() {
|
|
1296
|
-
const { dataLines, event, recordId } = this;
|
|
1297
|
-
this.startRecord();
|
|
1298
|
-
if (recordId !== void 0) {
|
|
1299
|
-
this.lastId = recordId;
|
|
1300
|
-
}
|
|
1301
|
-
if (dataLines.length === 0) {
|
|
1302
|
-
return null;
|
|
1303
|
-
}
|
|
1304
|
-
return { data: dataLines.join("\n"), event, id: recordId ?? "" };
|
|
1305
|
-
}
|
|
1306
|
-
/** startRecord empties the record being read, so the next line begins the next one. */
|
|
1307
|
-
startRecord() {
|
|
1308
|
-
this.dataLines = [];
|
|
1309
|
-
this.event = "";
|
|
1310
|
-
this.recordId = void 0;
|
|
1311
|
-
this.recordLength = 0;
|
|
1312
|
-
}
|
|
857
|
+
lineParts = [];
|
|
858
|
+
lineLength = 0;
|
|
859
|
+
dataLines = [];
|
|
860
|
+
event = "";
|
|
861
|
+
recordId;
|
|
862
|
+
recordLength = 0;
|
|
863
|
+
lastId;
|
|
864
|
+
atStart = true;
|
|
865
|
+
afterCarriageReturn = false;
|
|
866
|
+
overflowed = false;
|
|
867
|
+
maxBufferChars;
|
|
868
|
+
constructor(maxBufferChars = MAX_SSE_BUFFER_CHARS) {
|
|
869
|
+
const given = maxBufferChars;
|
|
870
|
+
if (typeof given !== "number" || !Number.isInteger(given) || given < 1) throw refuseOption("maxBufferChars", given, `pass a whole number of characters of 1 or more, or leave it out for the default of ${MAX_SSE_BUFFER_CHARS}.`);
|
|
871
|
+
this.maxBufferChars = given;
|
|
872
|
+
}
|
|
873
|
+
get lastEventId() {
|
|
874
|
+
return this.lastId;
|
|
875
|
+
}
|
|
876
|
+
push(chunk) {
|
|
877
|
+
if (this.overflowed) throw new SseBufferOverflowError(this.maxBufferChars);
|
|
878
|
+
const frames = [];
|
|
879
|
+
let start = 0;
|
|
880
|
+
if (chunk.length > 0) {
|
|
881
|
+
if (this.atStart) {
|
|
882
|
+
this.atStart = false;
|
|
883
|
+
if (chunk.startsWith(BYTE_ORDER_MARK)) start = 1;
|
|
884
|
+
}
|
|
885
|
+
if (this.afterCarriageReturn) {
|
|
886
|
+
this.afterCarriageReturn = false;
|
|
887
|
+
if (chunk.startsWith("\n", start)) start += 1;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
let carriageReturn = chunk.indexOf("\r", start);
|
|
891
|
+
let lineFeed = chunk.indexOf("\n", start);
|
|
892
|
+
while (carriageReturn !== -1 || lineFeed !== -1) {
|
|
893
|
+
const end = carriageReturn === -1 || lineFeed !== -1 && lineFeed < carriageReturn ? lineFeed : carriageReturn;
|
|
894
|
+
let next = end + 1;
|
|
895
|
+
if (end === carriageReturn) {
|
|
896
|
+
if (next === chunk.length) this.afterCarriageReturn = true;
|
|
897
|
+
else if (chunk.startsWith("\n", next)) next += 1;
|
|
898
|
+
}
|
|
899
|
+
const line = this.completeLine(chunk.slice(start, end));
|
|
900
|
+
this.recordLength += line.length + (next - end);
|
|
901
|
+
const frame = this.readLine(line);
|
|
902
|
+
if (frame !== null) frames.push(frame);
|
|
903
|
+
start = next;
|
|
904
|
+
if (carriageReturn !== -1 && carriageReturn < start) carriageReturn = chunk.indexOf("\r", start);
|
|
905
|
+
if (lineFeed !== -1 && lineFeed < start) lineFeed = chunk.indexOf("\n", start);
|
|
906
|
+
}
|
|
907
|
+
if (start < chunk.length) {
|
|
908
|
+
this.lineParts.push(chunk.slice(start));
|
|
909
|
+
this.lineLength += chunk.length - start;
|
|
910
|
+
}
|
|
911
|
+
if (this.recordLength + this.lineLength > this.maxBufferChars) {
|
|
912
|
+
this.lineParts = [];
|
|
913
|
+
this.lineLength = 0;
|
|
914
|
+
this.startRecord();
|
|
915
|
+
this.overflowed = true;
|
|
916
|
+
throw new SseBufferOverflowError(this.maxBufferChars);
|
|
917
|
+
}
|
|
918
|
+
return frames;
|
|
919
|
+
}
|
|
920
|
+
completeLine(tail) {
|
|
921
|
+
if (this.lineParts.length === 0) return tail;
|
|
922
|
+
this.lineParts.push(tail);
|
|
923
|
+
const line = this.lineParts.join("");
|
|
924
|
+
this.lineParts = [];
|
|
925
|
+
this.lineLength = 0;
|
|
926
|
+
return line;
|
|
927
|
+
}
|
|
928
|
+
readLine(line) {
|
|
929
|
+
if (line === "") return this.endRecord();
|
|
930
|
+
if (line.startsWith(":")) return null;
|
|
931
|
+
const colon = line.indexOf(":");
|
|
932
|
+
const field = colon === -1 ? line : line.slice(0, colon);
|
|
933
|
+
const raw = colon === -1 ? "" : line.slice(colon + 1);
|
|
934
|
+
const value = raw.startsWith(" ") ? raw.slice(1) : raw;
|
|
935
|
+
if (field === "data") this.dataLines.push(value);
|
|
936
|
+
else if (field === "event") this.event = value;
|
|
937
|
+
else if (field === "id" && !value.includes("\0")) this.recordId = value;
|
|
938
|
+
return null;
|
|
939
|
+
}
|
|
940
|
+
endRecord() {
|
|
941
|
+
const { dataLines, event, recordId } = this;
|
|
942
|
+
this.startRecord();
|
|
943
|
+
if (recordId !== void 0) this.lastId = recordId;
|
|
944
|
+
if (dataLines.length === 0) return null;
|
|
945
|
+
return {
|
|
946
|
+
data: dataLines.join("\n"),
|
|
947
|
+
event,
|
|
948
|
+
id: recordId ?? ""
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
startRecord() {
|
|
952
|
+
this.dataLines = [];
|
|
953
|
+
this.event = "";
|
|
954
|
+
this.recordId = void 0;
|
|
955
|
+
this.recordLength = 0;
|
|
956
|
+
}
|
|
1313
957
|
};
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
const position = parser.lastEventId;
|
|
1337
|
-
if (position !== void 0 && position !== known) {
|
|
1338
|
-
known = position;
|
|
1339
|
-
onLastEventId?.(position);
|
|
1340
|
-
}
|
|
1341
|
-
}
|
|
1342
|
-
} finally {
|
|
1343
|
-
reader.releaseLock();
|
|
1344
|
-
}
|
|
958
|
+
const readEventStream = async (body, onFrame, onLastEventId) => {
|
|
959
|
+
const reader = body.getReader();
|
|
960
|
+
const decoder = new TextDecoder("utf-8", { ignoreBOM: true });
|
|
961
|
+
const parser = new SseFrameParser();
|
|
962
|
+
let known;
|
|
963
|
+
try {
|
|
964
|
+
while (true) {
|
|
965
|
+
const { done, value } = await reader.read();
|
|
966
|
+
if (done) return "ended";
|
|
967
|
+
for (const frame of parser.push(decoder.decode(value, { stream: true }))) {
|
|
968
|
+
if (frame.id !== "") known = frame.id;
|
|
969
|
+
if (await onFrame(frame)) return "stopped";
|
|
970
|
+
}
|
|
971
|
+
const position = parser.lastEventId;
|
|
972
|
+
if (position !== void 0 && position !== known) {
|
|
973
|
+
known = position;
|
|
974
|
+
onLastEventId?.(position);
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
} finally {
|
|
978
|
+
reader.releaseLock();
|
|
979
|
+
}
|
|
1345
980
|
};
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
}
|
|
1391
|
-
if (next === SILENCE) {
|
|
1392
|
-
throw new MountayaConnectionError(
|
|
1393
|
-
`${operationId}: the stream went silent for ${STALL_WINDOW_MS} ms, past the heartbeat every live stream carries. Reopen it from the last position it named, and check the network path to the API if it keeps going silent.`
|
|
1394
|
-
);
|
|
1395
|
-
}
|
|
1396
|
-
if (next.done) {
|
|
1397
|
-
controller.close();
|
|
1398
|
-
} else {
|
|
1399
|
-
controller.enqueue(next.value);
|
|
1400
|
-
}
|
|
1401
|
-
} finally {
|
|
1402
|
-
endWait = void 0;
|
|
1403
|
-
clearTimeout(timer);
|
|
1404
|
-
if (signal.aborted) {
|
|
1405
|
-
letGo();
|
|
1406
|
-
}
|
|
1407
|
-
}
|
|
1408
|
-
}
|
|
1409
|
-
},
|
|
1410
|
-
// A read-ahead queue of none: the next chunk is asked for only when the reader asks.
|
|
1411
|
-
{ highWaterMark: 0 }
|
|
1412
|
-
);
|
|
981
|
+
const STALL_WINDOW_MS = 45e3;
|
|
982
|
+
const SILENCE = Symbol("silence");
|
|
983
|
+
const LET_GO = Symbol("let go");
|
|
984
|
+
const boundSilence = (body, operationId, signal) => {
|
|
985
|
+
const reader = body.getReader();
|
|
986
|
+
const letGo = () => {
|
|
987
|
+
try {
|
|
988
|
+
reader.releaseLock();
|
|
989
|
+
} catch {}
|
|
990
|
+
};
|
|
991
|
+
let endWait;
|
|
992
|
+
if (signal.aborted) letGo();
|
|
993
|
+
else signal.addEventListener("abort", () => {
|
|
994
|
+
endWait?.(LET_GO);
|
|
995
|
+
letGo();
|
|
996
|
+
}, { once: true });
|
|
997
|
+
return new ReadableStream({
|
|
998
|
+
cancel: (reason) => reader.cancel(reason),
|
|
999
|
+
pull: async (controller) => {
|
|
1000
|
+
if (signal.aborted) {
|
|
1001
|
+
controller.close();
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
1004
|
+
let timer;
|
|
1005
|
+
const ended = new Promise((resolve) => {
|
|
1006
|
+
endWait = resolve;
|
|
1007
|
+
timer = setTimeout(() => resolve(SILENCE), STALL_WINDOW_MS);
|
|
1008
|
+
});
|
|
1009
|
+
try {
|
|
1010
|
+
const next = await Promise.race([reader.read(), ended]);
|
|
1011
|
+
if (next === LET_GO) {
|
|
1012
|
+
controller.close();
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
if (next === SILENCE) throw new MountayaConnectionError(`${operationId}: the stream went silent for ${STALL_WINDOW_MS} ms, past the heartbeat every live stream carries. Reopen it from the last position it named, and check the network path to the API if it keeps going silent.`);
|
|
1016
|
+
if (next.done) controller.close();
|
|
1017
|
+
else controller.enqueue(next.value);
|
|
1018
|
+
} finally {
|
|
1019
|
+
endWait = void 0;
|
|
1020
|
+
clearTimeout(timer);
|
|
1021
|
+
if (signal.aborted) letGo();
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
}, { highWaterMark: 0 });
|
|
1413
1025
|
};
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
if (Number.isInteger(timeoutMs) && timeoutMs >= 1 && timeoutMs <= MAX_TIMER_DELAY_MS) {
|
|
1419
|
-
return timeoutMs;
|
|
1420
|
-
}
|
|
1421
|
-
throw refuseOption(
|
|
1422
|
-
"timeoutMs",
|
|
1423
|
-
timeoutMs,
|
|
1424
|
-
`pass a whole number of milliseconds from 1 to ${MAX_TIMER_DELAY_MS}, or leave it out to wait at most ${STALL_WINDOW_MS}.`,
|
|
1425
|
-
operationId
|
|
1426
|
-
);
|
|
1026
|
+
const openTimeoutOf = (timeoutMs, operationId) => {
|
|
1027
|
+
if (timeoutMs === void 0) return STALL_WINDOW_MS;
|
|
1028
|
+
if (Number.isInteger(timeoutMs) && timeoutMs >= 1 && timeoutMs <= MAX_TIMER_DELAY_MS) return timeoutMs;
|
|
1029
|
+
throw refuseOption("timeoutMs", timeoutMs, `pass a whole number of milliseconds from 1 to ${MAX_TIMER_DELAY_MS}, or leave it out to wait at most ${STALL_WINDOW_MS}.`, operationId);
|
|
1427
1030
|
};
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
);
|
|
1471
|
-
} finally {
|
|
1472
|
-
clearTimeout(deadline);
|
|
1473
|
-
}
|
|
1474
|
-
options.onResponse?.(response);
|
|
1475
|
-
if (!response.ok) {
|
|
1476
|
-
throw await readFailure(response, {
|
|
1477
|
-
...declaration,
|
|
1478
|
-
operationId,
|
|
1479
|
-
signal: state.controller.signal
|
|
1480
|
-
});
|
|
1481
|
-
}
|
|
1482
|
-
const body = response.body;
|
|
1483
|
-
if (body === void 0) {
|
|
1484
|
-
throw new MountayaConfigurationError(
|
|
1485
|
-
`${operationId}: this runtime's fetch exposes no response body to read the stream from. Pass a fetch that streams response bodies.`
|
|
1486
|
-
);
|
|
1487
|
-
}
|
|
1488
|
-
if (body === null) {
|
|
1489
|
-
throw new MountayaConnectionError(
|
|
1490
|
-
`${operationId}: the ${response.status} answer carries no body, so something on the network path answered in the API's place. Check the network path to the API.`
|
|
1491
|
-
);
|
|
1492
|
-
}
|
|
1493
|
-
return body;
|
|
1031
|
+
const requestStream = async (options, state, declaration) => {
|
|
1032
|
+
const { operationId } = options;
|
|
1033
|
+
const openTimeout = openTimeoutOf(options.timeoutMs, operationId);
|
|
1034
|
+
const headers = new Headers({ Accept: "text/event-stream" });
|
|
1035
|
+
if (options.lastEventId) {
|
|
1036
|
+
const position = String(options.lastEventId);
|
|
1037
|
+
if (!isHeaderValue(position)) throw new MountayaConfigurationError(`${operationId}: lastEventId holds a character no request header carries, so the stream cannot be resumed from it. Pass the position the stream named, as it named it, or leave it out to read from the start.`);
|
|
1038
|
+
headers.set("Last-Event-ID", position);
|
|
1039
|
+
}
|
|
1040
|
+
const waiting = options.waitForSession?.(state.controller.signal);
|
|
1041
|
+
if (waiting !== void 0) await waiting;
|
|
1042
|
+
const url = typeof options.url === "function" ? options.url() : options.url;
|
|
1043
|
+
const init = options.applyAuth({
|
|
1044
|
+
headers,
|
|
1045
|
+
signal: state.controller.signal
|
|
1046
|
+
}, "GET");
|
|
1047
|
+
let timedOut = false;
|
|
1048
|
+
const deadline = setTimeout(() => {
|
|
1049
|
+
timedOut = true;
|
|
1050
|
+
state.controller.abort();
|
|
1051
|
+
}, openTimeout);
|
|
1052
|
+
let response;
|
|
1053
|
+
try {
|
|
1054
|
+
response = await options.doFetch(url, init);
|
|
1055
|
+
} catch (cause) {
|
|
1056
|
+
if (state.closedByCaller) throw cause;
|
|
1057
|
+
if (timedOut) throw new MountayaConnectionError(options.timeoutMs === void 0 ? `${operationId}: no response within ${openTimeout} ms. Check the network path to the API.` : `${operationId}: no response within ${openTimeout} ms. Raise timeoutMs for a stream that is slow to answer, or check the network path to the API.`);
|
|
1058
|
+
if (isMountayaError(cause)) throw cause;
|
|
1059
|
+
throw new MountayaConnectionError(`${operationId}: the stream failed before a response arrived. Check the network, and that the stream's URL names a host that answers.`, cause);
|
|
1060
|
+
} finally {
|
|
1061
|
+
clearTimeout(deadline);
|
|
1062
|
+
}
|
|
1063
|
+
options.onResponse?.(response);
|
|
1064
|
+
if (!response.ok) throw await readFailure(response, {
|
|
1065
|
+
...declaration,
|
|
1066
|
+
operationId,
|
|
1067
|
+
signal: state.controller.signal
|
|
1068
|
+
});
|
|
1069
|
+
const body = response.body;
|
|
1070
|
+
if (body === void 0) throw new MountayaConfigurationError(`${operationId}: this runtime's fetch exposes no response body to read the stream from. Pass a fetch that streams response bodies.`);
|
|
1071
|
+
if (body === null) throw new MountayaConnectionError(`${operationId}: the ${response.status} answer carries no body, so something on the network path answered in the API's place. Check the network path to the API.`);
|
|
1072
|
+
return body;
|
|
1494
1073
|
};
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
cause
|
|
1555
|
-
);
|
|
1556
|
-
}
|
|
1557
|
-
};
|
|
1558
|
-
void (async () => {
|
|
1559
|
-
if (state.closedByCaller) {
|
|
1560
|
-
return;
|
|
1561
|
-
}
|
|
1562
|
-
let settled;
|
|
1563
|
-
try {
|
|
1564
|
-
settled = { outcome: await read() };
|
|
1565
|
-
} catch (error) {
|
|
1566
|
-
settled = { error };
|
|
1567
|
-
}
|
|
1568
|
-
signal?.removeEventListener("abort", close);
|
|
1569
|
-
state.controller.abort();
|
|
1570
|
-
if (state.closedByCaller) {
|
|
1571
|
-
return;
|
|
1572
|
-
}
|
|
1573
|
-
if ("outcome" in settled) {
|
|
1574
|
-
options.onClose?.(settled.outcome);
|
|
1575
|
-
} else {
|
|
1576
|
-
options.onError?.(settled.error);
|
|
1577
|
-
}
|
|
1578
|
-
})();
|
|
1579
|
-
return close;
|
|
1074
|
+
const openDeclaredSseStream = (options, declaration) => {
|
|
1075
|
+
const { operationId, signal } = options;
|
|
1076
|
+
const state = {
|
|
1077
|
+
closedByCaller: false,
|
|
1078
|
+
controller: new AbortController()
|
|
1079
|
+
};
|
|
1080
|
+
const close = () => {
|
|
1081
|
+
state.closedByCaller = true;
|
|
1082
|
+
state.controller.abort();
|
|
1083
|
+
};
|
|
1084
|
+
if (signal?.aborted) close();
|
|
1085
|
+
else signal?.addEventListener("abort", close, { once: true });
|
|
1086
|
+
let callerFault = false;
|
|
1087
|
+
const handle = async (frame) => {
|
|
1088
|
+
if (state.closedByCaller) return true;
|
|
1089
|
+
try {
|
|
1090
|
+
const event = options.decode(frame);
|
|
1091
|
+
if (event === null) return false;
|
|
1092
|
+
return await options.onFrame(event, frame.id || void 0);
|
|
1093
|
+
} catch (error) {
|
|
1094
|
+
callerFault = true;
|
|
1095
|
+
throw error;
|
|
1096
|
+
}
|
|
1097
|
+
};
|
|
1098
|
+
const report = (lastEventId) => {
|
|
1099
|
+
if (state.closedByCaller) return;
|
|
1100
|
+
try {
|
|
1101
|
+
options.onLastEventId?.(lastEventId || void 0);
|
|
1102
|
+
} catch (error) {
|
|
1103
|
+
callerFault = true;
|
|
1104
|
+
throw error;
|
|
1105
|
+
}
|
|
1106
|
+
};
|
|
1107
|
+
const read = async () => {
|
|
1108
|
+
const body = await requestStream(options, state, declaration);
|
|
1109
|
+
try {
|
|
1110
|
+
return await readEventStream(boundSilence(body, operationId, state.controller.signal), handle, report);
|
|
1111
|
+
} catch (cause) {
|
|
1112
|
+
if (state.closedByCaller || callerFault) throw cause;
|
|
1113
|
+
if (cause instanceof SseBufferOverflowError) throw new SseBufferOverflowError(cause.limit, operationId, { cause });
|
|
1114
|
+
if (isMountayaError(cause)) throw cause;
|
|
1115
|
+
throw new MountayaConnectionError(`${operationId}: the stream broke off before it ended. Reopen it from the last position it named, and check the network path to the API if it keeps breaking off.`, cause);
|
|
1116
|
+
}
|
|
1117
|
+
};
|
|
1118
|
+
(async () => {
|
|
1119
|
+
if (state.closedByCaller) return;
|
|
1120
|
+
let settled;
|
|
1121
|
+
try {
|
|
1122
|
+
settled = { outcome: await read() };
|
|
1123
|
+
} catch (error) {
|
|
1124
|
+
settled = { error };
|
|
1125
|
+
}
|
|
1126
|
+
signal?.removeEventListener("abort", close);
|
|
1127
|
+
state.controller.abort();
|
|
1128
|
+
if (state.closedByCaller) return;
|
|
1129
|
+
if ("outcome" in settled) options.onClose?.(settled.outcome);
|
|
1130
|
+
else options.onError?.(settled.error);
|
|
1131
|
+
})();
|
|
1132
|
+
return close;
|
|
1580
1133
|
};
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
"tool-call": "tool-call",
|
|
1596
|
-
"tool-result": "tool-result"
|
|
1134
|
+
const openSseStream = (options) => openDeclaredSseStream(options, void 0);
|
|
1135
|
+
const agentRunEventTypes = {
|
|
1136
|
+
"awaiting-input": "awaiting-input",
|
|
1137
|
+
"budget-exhausted": "budget-exhausted",
|
|
1138
|
+
"checks-unmet": "checks-unmet",
|
|
1139
|
+
"confirm-required": "confirm-required",
|
|
1140
|
+
done: "done",
|
|
1141
|
+
error: "error",
|
|
1142
|
+
message: "message",
|
|
1143
|
+
"message-delta": "message-delta",
|
|
1144
|
+
reconnect: "reconnect",
|
|
1145
|
+
"tool-abandoned": "tool-abandoned",
|
|
1146
|
+
"tool-call": "tool-call",
|
|
1147
|
+
"tool-result": "tool-result"
|
|
1597
1148
|
};
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
const id = body?.data?.id;
|
|
1603
|
-
return typeof id === "string" && id !== "" ? id : void 0;
|
|
1149
|
+
const agentTerminalEventTypes = /* @__PURE__ */ new Set([agentRunEventTypes.done]);
|
|
1150
|
+
const runIdFrom = (body) => {
|
|
1151
|
+
const id = body?.data?.id;
|
|
1152
|
+
return typeof id === "string" && id !== "" ? id : void 0;
|
|
1604
1153
|
};
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
return { ...payload, type: frame.event };
|
|
1154
|
+
const decodeAgentFrame = (frame) => {
|
|
1155
|
+
if (!Object.hasOwn(agentRunEventTypes, frame.event)) return null;
|
|
1156
|
+
let payload;
|
|
1157
|
+
try {
|
|
1158
|
+
payload = frame.data ? JSON.parse(frame.data) : {};
|
|
1159
|
+
} catch {
|
|
1160
|
+
return null;
|
|
1161
|
+
}
|
|
1162
|
+
if (typeof payload !== "object" || payload === null) return null;
|
|
1163
|
+
return {
|
|
1164
|
+
...payload,
|
|
1165
|
+
type: frame.event
|
|
1166
|
+
};
|
|
1619
1167
|
};
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
if (transport.mapCredential.publishableKey === void 0) {
|
|
1626
|
-
return;
|
|
1627
|
-
}
|
|
1628
|
-
throw new MountayaConfigurationError(
|
|
1629
|
-
`${operationId}: a run under a publishable key calls no server tool, so server_tools must be empty. Turn routesServerTools off.`
|
|
1630
|
-
);
|
|
1168
|
+
const AGENT_EVENTS_OPERATION_ID = "getAgentRunEvents";
|
|
1169
|
+
const refuseServerToolsUnderKey = (transport, operationId, serverTools) => {
|
|
1170
|
+
if (serverTools === void 0 || serverTools.length === 0) return;
|
|
1171
|
+
if (transport.mapCredential.publishableKey === void 0) return;
|
|
1172
|
+
throw new MountayaConfigurationError(`${operationId}: a run under a publishable key calls no server tool, so server_tools must be empty. Turn routesServerTools off.`);
|
|
1631
1173
|
};
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
},
|
|
1705
|
-
onLastEventId: (lastEventId) => handlers.onLastEventId?.(lastEventId),
|
|
1706
|
-
onResponse: (response) => {
|
|
1707
|
-
seam.report(response, AGENT_EVENTS_OPERATION_ID);
|
|
1708
|
-
},
|
|
1709
|
-
operationId: AGENT_EVENTS_OPERATION_ID,
|
|
1710
|
-
signal: options?.signal,
|
|
1711
|
-
// The wait for the stream's response is the client's own per-attempt budget.
|
|
1712
|
-
timeoutMs: seam.timeoutMs,
|
|
1713
|
-
// Resolved when the open is made, so a run id the path cannot carry (an empty one, or a
|
|
1714
|
-
// dot segment) is refused through onError like any other failed open.
|
|
1715
|
-
url: () => seam.url(`/v1/agent/runs/${encodeURIComponent(runId)}/events`, AGENT_EVENTS_OPERATION_ID),
|
|
1716
|
-
waitForSession: (signal) => seam.waitForSession(AGENT_EVENTS_OPERATION_ID, signal)
|
|
1717
|
-
},
|
|
1718
|
-
// The same declaration the credential is stamped by, so a refused open names the step a
|
|
1719
|
-
// member reading their own run takes.
|
|
1720
|
-
{ actsAsMember: true }
|
|
1721
|
-
);
|
|
1722
|
-
}
|
|
1174
|
+
const isReconnect = (event) => event.type === agentRunEventTypes.reconnect;
|
|
1175
|
+
const createAgentTransport = (transport) => ({
|
|
1176
|
+
sendInput: async (runId, input, options) => {
|
|
1177
|
+
if (input.type === "manifest-update") refuseServerToolsUnderKey(transport, "postAgentRunInput", input.server_tools);
|
|
1178
|
+
await postAgentRunInput(transport, {
|
|
1179
|
+
body: input,
|
|
1180
|
+
idempotencyKey: options?.idempotencyKey,
|
|
1181
|
+
run_id: runId
|
|
1182
|
+
});
|
|
1183
|
+
},
|
|
1184
|
+
startRun: async (params, options) => {
|
|
1185
|
+
refuseServerToolsUnderKey(transport, "postAgentRun", params.server_tools);
|
|
1186
|
+
const body = await postAgentRun(transport, {
|
|
1187
|
+
body: params,
|
|
1188
|
+
idempotencyKey: options?.idempotencyKey
|
|
1189
|
+
});
|
|
1190
|
+
const runId = runIdFrom(body);
|
|
1191
|
+
if (runId === void 0) throw new MountayaApiError("postAgentRun: 202 named no run", 202, []);
|
|
1192
|
+
return { runId };
|
|
1193
|
+
},
|
|
1194
|
+
subscribe: (runId, handlers, options) => {
|
|
1195
|
+
const seam = transport.streamSeam;
|
|
1196
|
+
let expiresAt;
|
|
1197
|
+
let reconnected = false;
|
|
1198
|
+
return openDeclaredSseStream({
|
|
1199
|
+
applyAuth: (init, method) => {
|
|
1200
|
+
const applied = seam.applyAuth(init, method, {
|
|
1201
|
+
actsAsMember: true,
|
|
1202
|
+
operationId: AGENT_EVENTS_OPERATION_ID,
|
|
1203
|
+
requiresSession: true
|
|
1204
|
+
});
|
|
1205
|
+
expiresAt = seam.sessionExpiresAt();
|
|
1206
|
+
return applied;
|
|
1207
|
+
},
|
|
1208
|
+
decode: (frame) => {
|
|
1209
|
+
const event = decodeAgentFrame(frame);
|
|
1210
|
+
if (event === null) handlers.onSkippedFrame?.();
|
|
1211
|
+
return event;
|
|
1212
|
+
},
|
|
1213
|
+
doFetch: seam.doFetch,
|
|
1214
|
+
lastEventId: options?.lastEventId,
|
|
1215
|
+
onClose: (outcome) => {
|
|
1216
|
+
if (outcome === "stopped") {
|
|
1217
|
+
handlers.onClose(reconnected ? "reconnect" : "terminal");
|
|
1218
|
+
return;
|
|
1219
|
+
}
|
|
1220
|
+
const expired = expiresAt !== void 0 && Date.now() >= expiresAt.getTime();
|
|
1221
|
+
handlers.onClose(expired ? "reconnect" : "ended");
|
|
1222
|
+
},
|
|
1223
|
+
onError: (error) => handlers.onError(error),
|
|
1224
|
+
onFrame: async (event, id) => {
|
|
1225
|
+
if (isReconnect(event)) {
|
|
1226
|
+
reconnected = true;
|
|
1227
|
+
return true;
|
|
1228
|
+
}
|
|
1229
|
+
await handlers.onFrame({
|
|
1230
|
+
event,
|
|
1231
|
+
id
|
|
1232
|
+
});
|
|
1233
|
+
return agentTerminalEventTypes.has(event.type);
|
|
1234
|
+
},
|
|
1235
|
+
onLastEventId: (lastEventId) => handlers.onLastEventId?.(lastEventId),
|
|
1236
|
+
onResponse: (response) => {
|
|
1237
|
+
seam.report(response, AGENT_EVENTS_OPERATION_ID);
|
|
1238
|
+
},
|
|
1239
|
+
operationId: AGENT_EVENTS_OPERATION_ID,
|
|
1240
|
+
signal: options?.signal,
|
|
1241
|
+
timeoutMs: seam.timeoutMs,
|
|
1242
|
+
url: () => seam.url(`/v1/agent/runs/${encodeURIComponent(runId)}/events`, AGENT_EVENTS_OPERATION_ID),
|
|
1243
|
+
waitForSession: (signal) => seam.waitForSession(AGENT_EVENTS_OPERATION_ID, signal)
|
|
1244
|
+
}, { actsAsMember: true });
|
|
1245
|
+
}
|
|
1723
1246
|
});
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
const held = value[member];
|
|
1766
|
-
const written = withAbsentNulls(held, isHolding(inner) ? inner[1] : inner);
|
|
1767
|
-
if (written !== held) {
|
|
1768
|
-
copy ??= { ...value };
|
|
1769
|
-
copy[member] = written;
|
|
1770
|
-
}
|
|
1771
|
-
}
|
|
1772
|
-
return copy ?? value;
|
|
1247
|
+
const isHolding = (mask) => Array.isArray(mask) && mask.length === 2 && mask[0] === true;
|
|
1248
|
+
const isVariants = (mask) => Array.isArray(mask) && mask.length === 2 && typeof mask[0] === "string";
|
|
1249
|
+
const isListMask = (mask) => Array.isArray(mask) && mask.length === 1;
|
|
1250
|
+
const isMembers = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1251
|
+
const withAbsentNulls = (value, mask) => {
|
|
1252
|
+
if (mask === void 0 || mask === true || isHolding(mask)) return value;
|
|
1253
|
+
if (isListMask(mask)) {
|
|
1254
|
+
if (!Array.isArray(value)) return value;
|
|
1255
|
+
let copy;
|
|
1256
|
+
for (const [index, element] of value.entries()) {
|
|
1257
|
+
const written = withAbsentNulls(element, mask[0]);
|
|
1258
|
+
if (written !== element) {
|
|
1259
|
+
copy ??= [...value];
|
|
1260
|
+
copy[index] = written;
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
return copy ?? value;
|
|
1264
|
+
}
|
|
1265
|
+
if (!isMembers(value)) return value;
|
|
1266
|
+
if (isVariants(mask)) {
|
|
1267
|
+
const [discriminator, variants] = mask;
|
|
1268
|
+
const variant = value[discriminator];
|
|
1269
|
+
return typeof variant === "string" && Object.hasOwn(variants, variant) ? withAbsentNulls(value, variants[variant]) : value;
|
|
1270
|
+
}
|
|
1271
|
+
let copy;
|
|
1272
|
+
for (const [member, inner] of Object.entries(mask)) {
|
|
1273
|
+
if (!Object.hasOwn(value, member)) {
|
|
1274
|
+
if (inner === true || isHolding(inner)) {
|
|
1275
|
+
copy ??= { ...value };
|
|
1276
|
+
copy[member] = null;
|
|
1277
|
+
}
|
|
1278
|
+
continue;
|
|
1279
|
+
}
|
|
1280
|
+
const held = value[member];
|
|
1281
|
+
const written = withAbsentNulls(held, isHolding(inner) ? inner[1] : inner);
|
|
1282
|
+
if (written !== held) {
|
|
1283
|
+
copy ??= { ...value };
|
|
1284
|
+
copy[member] = written;
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
return copy ?? value;
|
|
1773
1288
|
};
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
selection: "selection",
|
|
1796
|
-
"waypoint.changed": "waypoint.changed",
|
|
1797
|
-
"zone.changed": "zone.changed"
|
|
1289
|
+
const collabStreamEventNulls = ["type", {
|
|
1290
|
+
cursor: { position: true },
|
|
1291
|
+
selection: { selection: true }
|
|
1292
|
+
}];
|
|
1293
|
+
const collabStreamEventTypes = {
|
|
1294
|
+
"collection.changed": "collection.changed",
|
|
1295
|
+
cursor: "cursor",
|
|
1296
|
+
"draft.clear": "draft.clear",
|
|
1297
|
+
"draft.update": "draft.update",
|
|
1298
|
+
"itinerary.changed": "itinerary.changed",
|
|
1299
|
+
"lock.acquired": "lock.acquired",
|
|
1300
|
+
"lock.released": "lock.released",
|
|
1301
|
+
"lock.snapshot": "lock.snapshot",
|
|
1302
|
+
"presence.join": "presence.join",
|
|
1303
|
+
"presence.leave": "presence.leave",
|
|
1304
|
+
"presence.snapshot": "presence.snapshot",
|
|
1305
|
+
reconnect: "reconnect",
|
|
1306
|
+
"segment.changed": "segment.changed",
|
|
1307
|
+
selection: "selection",
|
|
1308
|
+
"waypoint.changed": "waypoint.changed",
|
|
1309
|
+
"zone.changed": "zone.changed"
|
|
1798
1310
|
};
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
if (typeof holder !== "object" || holder === null || !("id" in holder)) {
|
|
1809
|
-
return void 0;
|
|
1810
|
-
}
|
|
1811
|
-
const { id } = holder;
|
|
1812
|
-
return typeof id === "string" && id !== "" && id !== NIL_UUID ? id : void 0;
|
|
1311
|
+
const collabEventDates = collabStreamEventDatesByType;
|
|
1312
|
+
const NIL_UUID = "00000000-0000-0000-0000-000000000000";
|
|
1313
|
+
const isTerminalStatus = (status) => status >= 400 && status < 500 && status !== 429;
|
|
1314
|
+
const REOPEN_REPEATS_ERROR_NAMES = ["MountayaConfigurationError", "SseBufferOverflowError"];
|
|
1315
|
+
const holderIdOf = (metadata) => {
|
|
1316
|
+
const holder = metadata?.holder;
|
|
1317
|
+
if (typeof holder !== "object" || holder === null || !("id" in holder)) return;
|
|
1318
|
+
const { id } = holder;
|
|
1319
|
+
return typeof id === "string" && id !== "" && id !== NIL_UUID ? id : void 0;
|
|
1813
1320
|
};
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
return withAbsentNulls({ ...payload, type: frame.event }, collabStreamEventNulls);
|
|
1321
|
+
const decodeCollabFrame = (frame) => {
|
|
1322
|
+
if (!Object.hasOwn(collabStreamEventTypes, frame.event)) return null;
|
|
1323
|
+
let payload;
|
|
1324
|
+
try {
|
|
1325
|
+
payload = frame.data ? parseJsonWithDates(frame.data, collabEventDates[frame.event]) : {};
|
|
1326
|
+
} catch {
|
|
1327
|
+
return null;
|
|
1328
|
+
}
|
|
1329
|
+
if (typeof payload !== "object" || payload === null) return null;
|
|
1330
|
+
return withAbsentNulls({
|
|
1331
|
+
...payload,
|
|
1332
|
+
type: frame.event
|
|
1333
|
+
}, collabStreamEventNulls);
|
|
1828
1334
|
};
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
url: () => seam.url(path, operationId),
|
|
1919
|
-
waitForSession: (signal) => seam.waitForSession(operationId, signal)
|
|
1920
|
-
},
|
|
1921
|
-
// The same declaration the credential is stamped by, so a refused open names the step a
|
|
1922
|
-
// member following the resource takes.
|
|
1923
|
-
{ actsAsMember: true }
|
|
1924
|
-
);
|
|
1925
|
-
};
|
|
1926
|
-
return {
|
|
1927
|
-
sendSignal: async (resource, signal) => {
|
|
1928
|
-
try {
|
|
1929
|
-
if (resource.kind === "itineraries") {
|
|
1930
|
-
await postItineraryCollab(transport, { body: signal, itinerary_id: resource.id });
|
|
1931
|
-
} else {
|
|
1932
|
-
await postZoneCollab(transport, { body: signal, zone_id: resource.id });
|
|
1933
|
-
}
|
|
1934
|
-
} catch (error) {
|
|
1935
|
-
if (isMountayaApiError(error) && error.status === 409) {
|
|
1936
|
-
return { holderId: holderIdOf(error.metadata), ok: false };
|
|
1937
|
-
}
|
|
1938
|
-
throw error;
|
|
1939
|
-
}
|
|
1940
|
-
return { ok: true };
|
|
1941
|
-
},
|
|
1942
|
-
subscribeEditor: (resource, handlers, options) => (
|
|
1943
|
-
// resource.kind is a fixed collection literal, so it rides the path as-is; only the id
|
|
1944
|
-
// is percent-encoded.
|
|
1945
|
-
subscribeStream(
|
|
1946
|
-
resource.kind === "itineraries" ? "getItineraryEvents" : "getZoneEvents",
|
|
1947
|
-
`/v1/${resource.kind}/${encodeURIComponent(resource.id)}/events`,
|
|
1948
|
-
handlers,
|
|
1949
|
-
options
|
|
1950
|
-
)
|
|
1951
|
-
),
|
|
1952
|
-
subscribeList: (organizationId, handlers, options) => subscribeStream(
|
|
1953
|
-
"getOrganizationEvents",
|
|
1954
|
-
`/v1/organizations/${encodeURIComponent(organizationId)}/events`,
|
|
1955
|
-
handlers,
|
|
1956
|
-
options
|
|
1957
|
-
)
|
|
1958
|
-
};
|
|
1335
|
+
const createCollabTransport = (transport) => {
|
|
1336
|
+
const subscribeStream = (operationId, path, handlers, options) => {
|
|
1337
|
+
const seam = transport.streamSeam;
|
|
1338
|
+
let subscriberFault;
|
|
1339
|
+
const asSubscriber = (call) => {
|
|
1340
|
+
try {
|
|
1341
|
+
call();
|
|
1342
|
+
} catch (thrown) {
|
|
1343
|
+
subscriberFault = { thrown };
|
|
1344
|
+
throw thrown;
|
|
1345
|
+
}
|
|
1346
|
+
};
|
|
1347
|
+
let expiresAt;
|
|
1348
|
+
let userId;
|
|
1349
|
+
let reconnected = false;
|
|
1350
|
+
return openDeclaredSseStream({
|
|
1351
|
+
applyAuth: (init, method) => {
|
|
1352
|
+
const applied = seam.applyAuth(init, method, {
|
|
1353
|
+
actsAsMember: true,
|
|
1354
|
+
operationId,
|
|
1355
|
+
requiresSession: true
|
|
1356
|
+
});
|
|
1357
|
+
expiresAt = seam.sessionExpiresAt();
|
|
1358
|
+
userId = seam.sessionUserId() ?? void 0;
|
|
1359
|
+
return applied;
|
|
1360
|
+
},
|
|
1361
|
+
decode: (frame) => {
|
|
1362
|
+
const event = decodeCollabFrame(frame);
|
|
1363
|
+
if (event === null) asSubscriber(() => handlers.onSkippedFrame?.());
|
|
1364
|
+
return event;
|
|
1365
|
+
},
|
|
1366
|
+
doFetch: seam.doFetch,
|
|
1367
|
+
lastEventId: options?.lastEventId,
|
|
1368
|
+
onClose: () => {
|
|
1369
|
+
const expired = expiresAt !== void 0 && Date.now() >= expiresAt.getTime();
|
|
1370
|
+
handlers.onClose(reconnected || expired ? "reconnect" : "ended");
|
|
1371
|
+
},
|
|
1372
|
+
onError: (error) => {
|
|
1373
|
+
const terminal = subscriberFault !== void 0 && subscriberFault.thrown === error || isMountayaError(error) && REOPEN_REPEATS_ERROR_NAMES.includes(error.name) || isMountayaApiError(error) && isTerminalStatus(error.status);
|
|
1374
|
+
handlers.onError(error, { terminal });
|
|
1375
|
+
},
|
|
1376
|
+
onFrame: (event, id) => {
|
|
1377
|
+
if (event.type === collabStreamEventTypes.reconnect) {
|
|
1378
|
+
reconnected = true;
|
|
1379
|
+
return true;
|
|
1380
|
+
}
|
|
1381
|
+
asSubscriber(() => handlers.onFrame({
|
|
1382
|
+
event,
|
|
1383
|
+
id
|
|
1384
|
+
}));
|
|
1385
|
+
return false;
|
|
1386
|
+
},
|
|
1387
|
+
onLastEventId: (lastEventId) => {
|
|
1388
|
+
asSubscriber(() => handlers.onLastEventId?.(lastEventId));
|
|
1389
|
+
},
|
|
1390
|
+
onResponse: (response) => {
|
|
1391
|
+
seam.report(response, operationId);
|
|
1392
|
+
if (response.ok) asSubscriber(() => handlers.onOpen?.({ userId }));
|
|
1393
|
+
},
|
|
1394
|
+
operationId,
|
|
1395
|
+
signal: options?.signal,
|
|
1396
|
+
timeoutMs: seam.timeoutMs,
|
|
1397
|
+
url: () => seam.url(path, operationId),
|
|
1398
|
+
waitForSession: (signal) => seam.waitForSession(operationId, signal)
|
|
1399
|
+
}, { actsAsMember: true });
|
|
1400
|
+
};
|
|
1401
|
+
return {
|
|
1402
|
+
sendSignal: async (resource, signal) => {
|
|
1403
|
+
try {
|
|
1404
|
+
if (resource.kind === "itineraries") await postItineraryCollab(transport, {
|
|
1405
|
+
body: signal,
|
|
1406
|
+
itinerary_id: resource.id
|
|
1407
|
+
});
|
|
1408
|
+
else await postZoneCollab(transport, {
|
|
1409
|
+
body: signal,
|
|
1410
|
+
zone_id: resource.id
|
|
1411
|
+
});
|
|
1412
|
+
} catch (error) {
|
|
1413
|
+
if (isMountayaApiError(error) && error.status === 409) return {
|
|
1414
|
+
holderId: holderIdOf(error.metadata),
|
|
1415
|
+
ok: false
|
|
1416
|
+
};
|
|
1417
|
+
throw error;
|
|
1418
|
+
}
|
|
1419
|
+
return { ok: true };
|
|
1420
|
+
},
|
|
1421
|
+
subscribeEditor: (resource, handlers, options) => subscribeStream(resource.kind === "itineraries" ? "getItineraryEvents" : "getZoneEvents", `/v1/${resource.kind}/${encodeURIComponent(resource.id)}/events`, handlers, options),
|
|
1422
|
+
subscribeList: (organizationId, handlers, options) => subscribeStream("getOrganizationEvents", `/v1/organizations/${encodeURIComponent(organizationId)}/events`, handlers, options)
|
|
1423
|
+
};
|
|
1959
1424
|
};
|
|
1960
|
-
|
|
1961
1425
|
exports.MAX_SSE_BUFFER_CHARS = MAX_SSE_BUFFER_CHARS;
|
|
1962
1426
|
exports.SseBufferOverflowError = SseBufferOverflowError;
|
|
1963
1427
|
exports.SseFrameParser = SseFrameParser;
|
|
@@ -1968,5 +1432,5 @@ exports.decodeAgentFrame = decodeAgentFrame;
|
|
|
1968
1432
|
exports.decodeCollabFrame = decodeCollabFrame;
|
|
1969
1433
|
exports.openSseStream = openSseStream;
|
|
1970
1434
|
exports.readEventStream = readEventStream;
|
|
1971
|
-
|
|
1435
|
+
|
|
1972
1436
|
//# sourceMappingURL=index.cjs.map
|