@contello/client 2.2.2 → 2.3.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/dist/cli.d.ts +2 -0
- package/dist/cli.js +15 -14
- package/dist/cli.js.map +1 -0
- package/dist/generate-DIoN94iO.js +1068 -0
- package/dist/generate-DIoN94iO.js.map +1 -0
- package/dist/index.cjs +897 -1103
- package/dist/index.d.cts +118 -119
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +119 -119
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +887 -1055
- package/dist/index.js.map +1 -0
- package/package.json +4 -3
- package/dist/generate-Z4LVDAB4.js +0 -1241
package/dist/index.js
CHANGED
|
@@ -1,1135 +1,967 @@
|
|
|
1
|
-
|
|
1
|
+
import { createOperationTelemetry, graphqlOperationAttributes, injectTraceContext } from "@contello/opentelemetry";
|
|
2
2
|
import { createClient } from "graphql-ws";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
import { Agent, request } from "undici";
|
|
4
|
+
//#region src/async-iterable-utils.ts
|
|
5
|
+
const MAX_BACKOFF_MS = 3e4;
|
|
6
|
+
const JITTER_MIN_MS$1 = 300;
|
|
7
|
+
const JITTER_MAX_MS$1 = 3e3;
|
|
8
|
+
/**
|
|
9
|
+
* Returns the first value yielded by `iter`, then disposes the iterator. Throws if the iterable
|
|
10
|
+
* completes without yielding. Equivalent of rxjs's `firstValueFrom` for AsyncIterables.
|
|
11
|
+
*/
|
|
8
12
|
async function firstAsync(iter) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
13
|
+
const it = iter[Symbol.asyncIterator]();
|
|
14
|
+
try {
|
|
15
|
+
const result = await it.next();
|
|
16
|
+
if (result.done) throw new Error("firstAsync: async iterable completed without yielding a value");
|
|
17
|
+
return result.value;
|
|
18
|
+
} finally {
|
|
19
|
+
await it.return?.();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Lazy map over an async iterable. Cleanup propagates: breaking the outer iter disposes the inner. */
|
|
20
23
|
async function* mapAsync(iter, fn) {
|
|
21
|
-
|
|
22
|
-
yield fn(value);
|
|
23
|
-
}
|
|
24
|
+
for await (const value of iter) yield fn(value);
|
|
24
25
|
}
|
|
26
|
+
/** Lazy filter over an async iterable. */
|
|
25
27
|
async function* filterAsync(iter, predicate) {
|
|
26
|
-
|
|
27
|
-
if (predicate(value)) {
|
|
28
|
-
yield value;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
28
|
+
for await (const value of iter) if (predicate(value)) yield value;
|
|
31
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Concatenates all elements of all arrays yielded by `iter` into a single array. Used to support
|
|
32
|
+
* subscriptions that emit chunks of items before completing.
|
|
33
|
+
*/
|
|
32
34
|
async function collectAsync(iter) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
const out = [];
|
|
36
|
+
for await (const chunk of iter) out.push(...chunk);
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Exponential backoff with jitter. `attempt` starts at 0 → ~1s+jitter, 1 → ~2s+jitter, etc.,
|
|
41
|
+
* capped at 30s. Returns a Promise that resolves after the delay.
|
|
42
|
+
*/
|
|
39
43
|
function exponentialBackoff(attempt) {
|
|
40
|
-
|
|
41
|
-
const jitter = Math.floor(Math.random() * (JITTER_MAX_MS - JITTER_MIN_MS) + JITTER_MIN_MS);
|
|
42
|
-
return delay(base + jitter);
|
|
44
|
+
return delay(Math.min(Math.pow(2, attempt) * 1e3, MAX_BACKOFF_MS) + Math.floor(Math.random() * (JITTER_MAX_MS$1 - JITTER_MIN_MS$1) + JITTER_MIN_MS$1));
|
|
43
45
|
}
|
|
44
46
|
function delay(ms, signal) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
47
|
+
if (signal?.aborted) return Promise.reject(signal.reason);
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const timer = setTimeout(() => {
|
|
50
|
+
signal?.removeEventListener("abort", onAbort);
|
|
51
|
+
resolve();
|
|
52
|
+
}, ms);
|
|
53
|
+
const onAbort = () => {
|
|
54
|
+
clearTimeout(timer);
|
|
55
|
+
reject(signal.reason);
|
|
56
|
+
};
|
|
57
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Runs `fn` with exponential-backoff retry on rejection. Returns once `fn` resolves successfully.
|
|
62
|
+
* `signal` aborts both the in-flight `fn` (if it observes the signal) and the backoff wait.
|
|
63
|
+
*/
|
|
60
64
|
async function runWithBackoff(fn, signal) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
}
|
|
65
|
+
let attempt = 0;
|
|
66
|
+
for (;;) {
|
|
67
|
+
if (signal?.aborted) throw signal.reason;
|
|
68
|
+
try {
|
|
69
|
+
await fn();
|
|
70
|
+
return;
|
|
71
|
+
} catch {
|
|
72
|
+
await exponentialBackoffWithSignal(attempt, signal);
|
|
73
|
+
attempt++;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
74
76
|
}
|
|
75
77
|
function exponentialBackoffWithSignal(attempt, signal) {
|
|
76
|
-
|
|
77
|
-
const jitter = Math.floor(Math.random() * (JITTER_MAX_MS - JITTER_MIN_MS) + JITTER_MIN_MS);
|
|
78
|
-
return delay(base + jitter, signal);
|
|
78
|
+
return delay(Math.min(Math.pow(2, attempt) * 1e3, MAX_BACKOFF_MS) + Math.floor(Math.random() * (JITTER_MAX_MS$1 - JITTER_MIN_MS$1) + JITTER_MIN_MS$1), signal);
|
|
79
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Long-running stream wrapper: subscribes via `factory()`, yields every value, and on error or
|
|
82
|
+
* unexpected completion re-subscribes with exponential backoff. Stops only when `signal` aborts.
|
|
83
|
+
* Designed for subscriptions that should stay open across reconnects (e.g. the watcher).
|
|
84
|
+
*/
|
|
80
85
|
async function* asyncKeepalive(factory, signal) {
|
|
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
|
-
/** subscribe to decorate outgoing GraphQL WebSocket messages (e.g. inject traceparent) */
|
|
110
|
-
message: "@contello/client:message"
|
|
111
|
-
};
|
|
112
|
-
var onStart = dc.channel(channels.start);
|
|
113
|
-
var onEnd = dc.channel(channels.end);
|
|
114
|
-
var onError = dc.channel(channels.error);
|
|
115
|
-
var onMessage = dc.channel(channels.message);
|
|
116
|
-
function hasOperationSubscribers() {
|
|
117
|
-
return onStart.hasSubscribers || onEnd.hasSubscribers || onError.hasSubscribers;
|
|
118
|
-
}
|
|
119
|
-
function wrap(name, fn) {
|
|
120
|
-
if (!hasOperationSubscribers()) {
|
|
121
|
-
return fn();
|
|
122
|
-
}
|
|
123
|
-
const start = performance.now();
|
|
124
|
-
onStart.publish({ name });
|
|
125
|
-
let result;
|
|
126
|
-
try {
|
|
127
|
-
result = fn();
|
|
128
|
-
} catch (error) {
|
|
129
|
-
onError.publish({ name, error, durationMs: performance.now() - start });
|
|
130
|
-
throw error;
|
|
131
|
-
}
|
|
132
|
-
if (result instanceof Promise) {
|
|
133
|
-
return result.then(
|
|
134
|
-
(r) => {
|
|
135
|
-
onEnd.publish({ name, durationMs: performance.now() - start });
|
|
136
|
-
return r;
|
|
137
|
-
},
|
|
138
|
-
(error) => {
|
|
139
|
-
onError.publish({ name, error, durationMs: performance.now() - start });
|
|
140
|
-
throw error;
|
|
141
|
-
}
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
onEnd.publish({ name, durationMs: performance.now() - start });
|
|
145
|
-
return result;
|
|
146
|
-
}
|
|
147
|
-
function decorateMessage(message) {
|
|
148
|
-
if (!onMessage.hasSubscribers) {
|
|
149
|
-
return message;
|
|
150
|
-
}
|
|
151
|
-
const msg = { message };
|
|
152
|
-
onMessage.publish(msg);
|
|
153
|
-
return msg.message;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// src/http.ts
|
|
157
|
-
import { Agent, request as undiciRequest } from "undici";
|
|
158
|
-
var SAFE_SEGMENT = /^[A-Za-z0-9._-]+$/;
|
|
159
|
-
var FORWARDED_PROXY_HEADERS = /* @__PURE__ */ new Set([
|
|
160
|
-
"content-type",
|
|
161
|
-
"content-length",
|
|
162
|
-
"content-range",
|
|
163
|
-
"content-encoding",
|
|
164
|
-
"accept-ranges",
|
|
165
|
-
"cache-control",
|
|
166
|
-
"etag",
|
|
167
|
-
"last-modified",
|
|
168
|
-
"expires",
|
|
169
|
-
"age"
|
|
86
|
+
let attempt = 0;
|
|
87
|
+
while (!signal?.aborted) try {
|
|
88
|
+
for await (const value of factory()) {
|
|
89
|
+
if (signal?.aborted) return;
|
|
90
|
+
yield value;
|
|
91
|
+
attempt = 0;
|
|
92
|
+
}
|
|
93
|
+
attempt = 0;
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (signal?.aborted) return;
|
|
96
|
+
await exponentialBackoffWithSignal(attempt, signal);
|
|
97
|
+
attempt++;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/http.ts
|
|
102
|
+
const SAFE_SEGMENT = /^[A-Za-z0-9._-]+$/;
|
|
103
|
+
const FORWARDED_PROXY_HEADERS = /* @__PURE__ */ new Set([
|
|
104
|
+
"content-type",
|
|
105
|
+
"content-length",
|
|
106
|
+
"content-range",
|
|
107
|
+
"content-encoding",
|
|
108
|
+
"accept-ranges",
|
|
109
|
+
"cache-control",
|
|
110
|
+
"etag",
|
|
111
|
+
"last-modified",
|
|
112
|
+
"expires",
|
|
113
|
+
"age"
|
|
170
114
|
]);
|
|
171
115
|
function assertSafeSegment(segment, kind) {
|
|
172
|
-
|
|
173
|
-
throw new Error(`invalid ${kind}`);
|
|
174
|
-
}
|
|
116
|
+
if (!SAFE_SEGMENT.test(segment) || segment === "." || segment === "..") throw new Error(`invalid ${kind}`);
|
|
175
117
|
}
|
|
176
118
|
function assertSafeAssetPath(path, kind) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
for (const segment of path.split("/")) {
|
|
181
|
-
assertSafeSegment(segment, kind);
|
|
182
|
-
}
|
|
119
|
+
if (path === "") throw new Error(`invalid ${kind}`);
|
|
120
|
+
for (const segment of path.split("/")) assertSafeSegment(segment, kind);
|
|
183
121
|
}
|
|
184
122
|
function createHttpAgent(options) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
123
|
+
return new Agent({
|
|
124
|
+
pipelining: options?.pipelining ?? 10,
|
|
125
|
+
allowH2: options?.allowH2 ?? true,
|
|
126
|
+
maxConcurrentStreams: options?.maxConcurrentStreams ?? 128,
|
|
127
|
+
keepAliveTimeout: options?.keepAliveTimeout ?? 6e4,
|
|
128
|
+
autoSelectFamily: true,
|
|
129
|
+
autoSelectFamilyAttemptTimeout: 25
|
|
130
|
+
});
|
|
193
131
|
}
|
|
194
132
|
async function downloadFile(agent, url, token, fileId) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
stream: () => toWebStream(response.body)
|
|
211
|
-
};
|
|
133
|
+
assertSafeSegment(fileId, "file id");
|
|
134
|
+
const response = await request(`${url}/api/v1/assets/files/${fileId}`, {
|
|
135
|
+
headers: { token },
|
|
136
|
+
dispatcher: agent
|
|
137
|
+
});
|
|
138
|
+
if (response.statusCode < 200 || response.statusCode >= 300) {
|
|
139
|
+
safeDestroyBody(response.body);
|
|
140
|
+
throw new Error(`download failed: ${response.statusCode}`);
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
mimeType: firstHeader(response.headers, "content-type") ?? "application/octet-stream",
|
|
144
|
+
size: Number(firstHeader(response.headers, "content-length") ?? 0),
|
|
145
|
+
bytes: async () => new Uint8Array(await response.body.arrayBuffer()),
|
|
146
|
+
stream: () => toWebStream(response.body)
|
|
147
|
+
};
|
|
212
148
|
}
|
|
213
149
|
async function proxyHls(agent, url, token, path, signal) {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
return buildProxyResult(response, signal);
|
|
150
|
+
assertSafeAssetPath(path, "hls path");
|
|
151
|
+
return buildProxyResult(await request(`${url}/api/v1/assets/video/hls/${path}`, {
|
|
152
|
+
headers: { token },
|
|
153
|
+
dispatcher: agent,
|
|
154
|
+
signal: signal ?? null
|
|
155
|
+
}), signal);
|
|
221
156
|
}
|
|
222
157
|
function buildProxyResult(response, signal) {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
return {
|
|
235
|
-
status: response.statusCode,
|
|
236
|
-
headers,
|
|
237
|
-
stream: () => toWebStream(response.body, signal)
|
|
238
|
-
};
|
|
158
|
+
const headers = new Headers();
|
|
159
|
+
for (const [key, value] of Object.entries(response.headers)) {
|
|
160
|
+
if (!FORWARDED_PROXY_HEADERS.has(key.toLowerCase())) continue;
|
|
161
|
+
if (Array.isArray(value)) for (const v of value) headers.append(key, v);
|
|
162
|
+
else if (value !== null && value !== void 0) headers.set(key, value);
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
status: response.statusCode,
|
|
166
|
+
headers,
|
|
167
|
+
stream: () => toWebStream(response.body, signal)
|
|
168
|
+
};
|
|
239
169
|
}
|
|
240
170
|
function firstHeader(headers, key) {
|
|
241
|
-
|
|
242
|
-
|
|
171
|
+
const v = headers[key];
|
|
172
|
+
return Array.isArray(v) ? v[0] : v;
|
|
243
173
|
}
|
|
244
174
|
function safeDestroyBody(body) {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
} catch {
|
|
250
|
-
}
|
|
175
|
+
try {
|
|
176
|
+
body.on("error", () => {});
|
|
177
|
+
body.destroy();
|
|
178
|
+
} catch {}
|
|
251
179
|
}
|
|
252
180
|
function toWebStream(body, signal) {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
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
|
-
// src/ping.ts
|
|
305
|
-
var PING_QUERY = `query ContelloPing { contelloPing { response } }`;
|
|
181
|
+
let closed = false;
|
|
182
|
+
const cleanup = () => {
|
|
183
|
+
if (closed) return;
|
|
184
|
+
closed = true;
|
|
185
|
+
signal?.removeEventListener("abort", cleanup);
|
|
186
|
+
safeDestroyBody(body);
|
|
187
|
+
};
|
|
188
|
+
return new ReadableStream({
|
|
189
|
+
start(controller) {
|
|
190
|
+
signal?.addEventListener("abort", cleanup);
|
|
191
|
+
body.on("data", (chunk) => {
|
|
192
|
+
if (closed || signal?.aborted) return;
|
|
193
|
+
try {
|
|
194
|
+
controller.enqueue(chunk);
|
|
195
|
+
} catch {
|
|
196
|
+
cleanup();
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
body.on("end", () => {
|
|
200
|
+
if (closed || signal?.aborted) return;
|
|
201
|
+
closed = true;
|
|
202
|
+
signal?.removeEventListener("abort", cleanup);
|
|
203
|
+
try {
|
|
204
|
+
controller.close();
|
|
205
|
+
} catch {}
|
|
206
|
+
});
|
|
207
|
+
body.on("error", (err) => {
|
|
208
|
+
cleanup();
|
|
209
|
+
if (err.name === "AbortError") {
|
|
210
|
+
try {
|
|
211
|
+
controller.close();
|
|
212
|
+
} catch {}
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
controller.error(err);
|
|
217
|
+
} catch {}
|
|
218
|
+
});
|
|
219
|
+
},
|
|
220
|
+
cancel() {
|
|
221
|
+
cleanup();
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
//#endregion
|
|
226
|
+
//#region src/ping.ts
|
|
227
|
+
const PING_QUERY = `query ContelloPing { contelloPing { response } }`;
|
|
306
228
|
async function ping(execute) {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
throw new Error(`unexpected ping response: ${data.contelloPing.response}`);
|
|
310
|
-
}
|
|
229
|
+
const data = await execute(PING_QUERY);
|
|
230
|
+
if (data.contelloPing.response !== "pong") throw new Error(`unexpected ping response: ${data.contelloPing.response}`);
|
|
311
231
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
232
|
+
//#endregion
|
|
233
|
+
//#region src/pool.ts
|
|
234
|
+
const DEFAULT_CONNECT_TIMEOUT = 1e4;
|
|
315
235
|
var ConnectionPool = class {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
this._clients = [];
|
|
383
|
-
this._connected = [];
|
|
384
|
-
this._currentIndex = 0;
|
|
385
|
-
await Promise.all(closed);
|
|
386
|
-
}
|
|
236
|
+
_createClient;
|
|
237
|
+
_poolSize;
|
|
238
|
+
_connectTimeout;
|
|
239
|
+
_clients = [];
|
|
240
|
+
_connected = [];
|
|
241
|
+
_currentIndex = 0;
|
|
242
|
+
constructor(_createClient, _poolSize, _connectTimeout = DEFAULT_CONNECT_TIMEOUT) {
|
|
243
|
+
this._createClient = _createClient;
|
|
244
|
+
this._poolSize = _poolSize;
|
|
245
|
+
this._connectTimeout = _connectTimeout;
|
|
246
|
+
}
|
|
247
|
+
async connect() {
|
|
248
|
+
const ready = [];
|
|
249
|
+
for (let i = 0; i < this._poolSize; i++) {
|
|
250
|
+
const index = i;
|
|
251
|
+
const client = this._createClient(String(i + 1));
|
|
252
|
+
this._clients.push(client);
|
|
253
|
+
this._connected.push(false);
|
|
254
|
+
client.on("connected", () => {
|
|
255
|
+
this._connected[index] = true;
|
|
256
|
+
});
|
|
257
|
+
client.on("closed", () => {
|
|
258
|
+
this._connected[index] = false;
|
|
259
|
+
});
|
|
260
|
+
ready.push(new Promise((resolve) => {
|
|
261
|
+
let settled = false;
|
|
262
|
+
const done = () => {
|
|
263
|
+
if (settled) return;
|
|
264
|
+
settled = true;
|
|
265
|
+
offConnected();
|
|
266
|
+
clearTimeout(timer);
|
|
267
|
+
resolve();
|
|
268
|
+
};
|
|
269
|
+
const offConnected = client.on("connected", done);
|
|
270
|
+
const timer = setTimeout(done, this._connectTimeout);
|
|
271
|
+
timer.unref?.();
|
|
272
|
+
}));
|
|
273
|
+
}
|
|
274
|
+
await Promise.all(ready);
|
|
275
|
+
}
|
|
276
|
+
get() {
|
|
277
|
+
if (this._clients.length === 0) throw new Error("@contello/client: connection pool is empty — call client.init() first");
|
|
278
|
+
for (let i = 0; i < this._clients.length; i++) {
|
|
279
|
+
const index = (this._currentIndex + i) % this._clients.length;
|
|
280
|
+
if (this._connected[index] === true) {
|
|
281
|
+
this._currentIndex = (index + 1) % this._clients.length;
|
|
282
|
+
return this._clients[index];
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
const client = this._clients[this._currentIndex];
|
|
286
|
+
this._currentIndex = (this._currentIndex + 1) % this._clients.length;
|
|
287
|
+
return client;
|
|
288
|
+
}
|
|
289
|
+
async disconnect() {
|
|
290
|
+
const closed = this._clients.map((client) => new Promise((resolve) => {
|
|
291
|
+
const unsubscribe = client.on("closed", () => {
|
|
292
|
+
unsubscribe();
|
|
293
|
+
resolve();
|
|
294
|
+
});
|
|
295
|
+
client.dispose();
|
|
296
|
+
}));
|
|
297
|
+
this._clients = [];
|
|
298
|
+
this._connected = [];
|
|
299
|
+
this._currentIndex = 0;
|
|
300
|
+
await Promise.all(closed);
|
|
301
|
+
}
|
|
387
302
|
};
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
303
|
+
//#endregion
|
|
304
|
+
//#region src/telemetry.ts
|
|
305
|
+
const { wrap } = createOperationTelemetry("@contello/client");
|
|
306
|
+
//#endregion
|
|
307
|
+
//#region src/scalars.ts
|
|
308
|
+
const LOCAL_DATE_TIME_KEYS = [
|
|
309
|
+
"year",
|
|
310
|
+
"month",
|
|
311
|
+
"day",
|
|
312
|
+
"hour",
|
|
313
|
+
"minute",
|
|
314
|
+
"second"
|
|
315
|
+
];
|
|
316
|
+
const LOCAL_DATE_KEYS = [
|
|
317
|
+
"year",
|
|
318
|
+
"month",
|
|
319
|
+
"day"
|
|
320
|
+
];
|
|
321
|
+
const DATE_TIME_REGEX = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/;
|
|
322
|
+
const DATE_REGEX = /^(\d{4})-(\d{2})-(\d{2})/;
|
|
394
323
|
function pad2(n) {
|
|
395
|
-
|
|
324
|
+
return n.toString().padStart(2, "0");
|
|
396
325
|
}
|
|
397
326
|
function pad4(n) {
|
|
398
|
-
|
|
327
|
+
return n.toString().padStart(4, "0");
|
|
399
328
|
}
|
|
400
329
|
function hasExactKeys(value, keys) {
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
}
|
|
405
|
-
for (const key of keys) {
|
|
406
|
-
if (!Object.prototype.hasOwnProperty.call(value, key)) {
|
|
407
|
-
return false;
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
return true;
|
|
330
|
+
if (Object.keys(value).length !== keys.length) return false;
|
|
331
|
+
for (const key of keys) if (!Object.prototype.hasOwnProperty.call(value, key)) return false;
|
|
332
|
+
return true;
|
|
411
333
|
}
|
|
412
334
|
function allNumbers(value, keys) {
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
return false;
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
return true;
|
|
335
|
+
for (const key of keys) if (typeof value[key] !== "number") return false;
|
|
336
|
+
return true;
|
|
419
337
|
}
|
|
420
338
|
function isLocalDateTime(value) {
|
|
421
|
-
|
|
339
|
+
return !!value && typeof value === "object" && !Array.isArray(value) && hasExactKeys(value, LOCAL_DATE_TIME_KEYS) && allNumbers(value, LOCAL_DATE_TIME_KEYS);
|
|
422
340
|
}
|
|
423
341
|
function isLocalDate(value) {
|
|
424
|
-
|
|
342
|
+
return !!value && typeof value === "object" && !Array.isArray(value) && hasExactKeys(value, LOCAL_DATE_KEYS) && allNumbers(value, LOCAL_DATE_KEYS);
|
|
425
343
|
}
|
|
426
344
|
function decodeLocalDateTime(value) {
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
second: Number(match[6])
|
|
438
|
-
};
|
|
345
|
+
const match = DATE_TIME_REGEX.exec(value);
|
|
346
|
+
if (!match) throw new Error(`@contello/client: cannot decode LocalDateTime from "${value}"`);
|
|
347
|
+
return {
|
|
348
|
+
year: Number(match[1]),
|
|
349
|
+
month: Number(match[2]),
|
|
350
|
+
day: Number(match[3]),
|
|
351
|
+
hour: Number(match[4]),
|
|
352
|
+
minute: Number(match[5]),
|
|
353
|
+
second: Number(match[6])
|
|
354
|
+
};
|
|
439
355
|
}
|
|
440
356
|
function decodeLocalDate(value) {
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
day: Number(match[3])
|
|
449
|
-
};
|
|
357
|
+
const match = DATE_REGEX.exec(value);
|
|
358
|
+
if (!match) throw new Error(`@contello/client: cannot decode LocalDate from "${value}"`);
|
|
359
|
+
return {
|
|
360
|
+
year: Number(match[1]),
|
|
361
|
+
month: Number(match[2]),
|
|
362
|
+
day: Number(match[3])
|
|
363
|
+
};
|
|
450
364
|
}
|
|
451
365
|
function encodeLocalDateTime(value) {
|
|
452
|
-
|
|
366
|
+
return `${pad4(value.year)}-${pad2(value.month)}-${pad2(value.day)}T${pad2(value.hour)}:${pad2(value.minute)}:${pad2(value.second)}Z`;
|
|
453
367
|
}
|
|
454
368
|
function encodeLocalDate(value) {
|
|
455
|
-
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
|
|
369
|
+
return `${pad4(value.year)}-${pad2(value.month)}-${pad2(value.day)}T00:00:00Z`;
|
|
370
|
+
}
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/transform-variables.ts
|
|
373
|
+
/**
|
|
374
|
+
* encodes outgoing variables: LocalDateTime / LocalDate POJOs become wire strings.
|
|
375
|
+
* detection is by shape — generated input types use the struct types, so consumers
|
|
376
|
+
* pass structs in the right places, and stray objects with the same shape would be
|
|
377
|
+
* an extremely unusual coincidence.
|
|
378
|
+
*/
|
|
459
379
|
function transformVariables(value) {
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
if (isLocalDate(value)) {
|
|
473
|
-
return encodeLocalDate(value);
|
|
474
|
-
}
|
|
475
|
-
const obj = value;
|
|
476
|
-
const out = {};
|
|
477
|
-
for (const key of Object.keys(obj)) {
|
|
478
|
-
out[key] = transformVariables(obj[key]);
|
|
479
|
-
}
|
|
480
|
-
return out;
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
// src/rpc.ts
|
|
380
|
+
if (value === null || value === void 0) return value;
|
|
381
|
+
if (Array.isArray(value)) return value.map((item) => transformVariables(item));
|
|
382
|
+
if (typeof value !== "object") return value;
|
|
383
|
+
if (isLocalDateTime(value)) return encodeLocalDateTime(value);
|
|
384
|
+
if (isLocalDate(value)) return encodeLocalDate(value);
|
|
385
|
+
const obj = value;
|
|
386
|
+
const out = {};
|
|
387
|
+
for (const [key, val] of Object.entries(obj)) out[key] = transformVariables(val);
|
|
388
|
+
return out;
|
|
389
|
+
}
|
|
390
|
+
//#endregion
|
|
391
|
+
//#region src/rpc.ts
|
|
484
392
|
function buildRpc(operations, subscribe) {
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
393
|
+
const rpc = {};
|
|
394
|
+
for (const [name, def] of Object.entries(operations)) rpc[name] = def.kind === "subscription" ? (variables) => subscribe(def.document, transformVariables(variables)) : (variables) => {
|
|
395
|
+
const vars = transformVariables(variables);
|
|
396
|
+
return wrap(`rpc:${name}`, () => firstAsync(subscribe(def.document, vars)), graphqlOperationAttributes(def.document, vars));
|
|
397
|
+
};
|
|
398
|
+
return rpc;
|
|
399
|
+
}
|
|
400
|
+
//#endregion
|
|
401
|
+
//#region src/source-subscription.ts
|
|
402
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
494
403
|
function createSelection(source) {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
case "route": {
|
|
504
|
-
return `subscription($ids: [ID!], $paths: [String!]) { source: ${source.subscription}(request: { ids: $ids, paths: $paths }) ${spread} }`;
|
|
505
|
-
}
|
|
506
|
-
case "asset": {
|
|
507
|
-
return `subscription($ids: [ID!]) { source: ${source.subscription}(filter: { ids: $ids }) ${spread} }`;
|
|
508
|
-
}
|
|
509
|
-
case "i18nMessage": {
|
|
510
|
-
return `subscription($collection: String!, $ids: [ID!]) { source: ${source.subscription}(collectionReferenceName: $collection, ids: $ids) ${spread} }`;
|
|
511
|
-
}
|
|
512
|
-
}
|
|
404
|
+
const spread = `{ ...${source.fragment} }`;
|
|
405
|
+
switch (source.__cardinality) {
|
|
406
|
+
case "singleton": return `subscription { source: ${source.subscription} ${spread} }`;
|
|
407
|
+
case "entity": return `subscription($ids: [ID!]) { source: ${source.subscription}(request: { filter: { ids: $ids } }) ${spread} }`;
|
|
408
|
+
case "route": return `subscription($ids: [ID!], $paths: [String!]) { source: ${source.subscription}(request: { ids: $ids, paths: $paths }) ${spread} }`;
|
|
409
|
+
case "asset": return `subscription($ids: [ID!]) { source: ${source.subscription}(filter: { ids: $ids }) ${spread} }`;
|
|
410
|
+
case "i18nMessage": return `subscription($collection: String!, $ids: [ID!]) { source: ${source.subscription}(collectionReferenceName: $collection, ids: $ids) ${spread} }`;
|
|
411
|
+
}
|
|
513
412
|
}
|
|
514
413
|
function createSourceSubscription(source) {
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
// src/sources.ts
|
|
414
|
+
const cached = cache.get(source);
|
|
415
|
+
if (cached !== void 0) return cached;
|
|
416
|
+
const doc = `${source.document}\n${createSelection(source)}`;
|
|
417
|
+
cache.set(source, doc);
|
|
418
|
+
return doc;
|
|
419
|
+
}
|
|
420
|
+
//#endregion
|
|
421
|
+
//#region src/sources.ts
|
|
526
422
|
function createSources(sources, subscribe) {
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
fetch: (vars) => wrap(
|
|
569
|
-
`source:${name}`,
|
|
570
|
-
() => collectAsync(
|
|
571
|
-
mapAsync(
|
|
572
|
-
subscribe(doc, transformVariables({ ids: vars?.ids })),
|
|
573
|
-
(r) => r.source
|
|
574
|
-
)
|
|
575
|
-
)
|
|
576
|
-
)
|
|
577
|
-
};
|
|
578
|
-
break;
|
|
579
|
-
}
|
|
580
|
-
case "i18nMessage": {
|
|
581
|
-
out[name] = {
|
|
582
|
-
fetch: (vars) => wrap(
|
|
583
|
-
`source:${name}`,
|
|
584
|
-
() => collectAsync(
|
|
585
|
-
mapAsync(
|
|
586
|
-
subscribe(
|
|
587
|
-
doc,
|
|
588
|
-
transformVariables({ collection: vars.collection, ids: vars.ids })
|
|
589
|
-
),
|
|
590
|
-
(r) => r.source
|
|
591
|
-
)
|
|
592
|
-
)
|
|
593
|
-
)
|
|
594
|
-
};
|
|
595
|
-
break;
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
return out;
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
// src/transform-response.ts
|
|
603
|
-
var FLAT_PREFIX = "_flat_";
|
|
604
|
-
var LDT_PREFIX = "_ldt_";
|
|
605
|
-
var LD_PREFIX = "_ld_";
|
|
606
|
-
var MODEL_SUFFIXES = ["Entity", "Component"];
|
|
423
|
+
const out = {};
|
|
424
|
+
for (const [name, source] of Object.entries(sources)) {
|
|
425
|
+
const def = source;
|
|
426
|
+
const accessor = createSourceAccessor(name, def, createSourceSubscription(def), subscribe);
|
|
427
|
+
if (accessor) out[name] = accessor;
|
|
428
|
+
}
|
|
429
|
+
return out;
|
|
430
|
+
}
|
|
431
|
+
function createSourceAccessor(name, def, doc, subscribe) {
|
|
432
|
+
switch (def.__cardinality) {
|
|
433
|
+
case "singleton": return { fetch: () => wrap(`source:${name}`, () => firstAsync(mapAsync(subscribe(doc), (r) => r.source)), graphqlOperationAttributes(doc)) };
|
|
434
|
+
case "entity": return { fetch: (vars) => {
|
|
435
|
+
const variables = transformVariables({ ids: vars?.ids });
|
|
436
|
+
return wrap(`source:${name}`, () => collectAsync(mapAsync(subscribe(doc, variables), (r) => r.source)), graphqlOperationAttributes(doc, variables));
|
|
437
|
+
} };
|
|
438
|
+
case "route": return { fetch: (vars) => {
|
|
439
|
+
const variables = transformVariables({
|
|
440
|
+
ids: vars?.ids,
|
|
441
|
+
paths: vars?.paths
|
|
442
|
+
});
|
|
443
|
+
return wrap(`source:${name}`, () => collectAsync(mapAsync(subscribe(doc, variables), (r) => r.source)), graphqlOperationAttributes(doc, variables));
|
|
444
|
+
} };
|
|
445
|
+
case "asset": return { fetch: (vars) => {
|
|
446
|
+
const variables = transformVariables({ ids: vars?.ids });
|
|
447
|
+
return wrap(`source:${name}`, () => collectAsync(mapAsync(subscribe(doc, variables), (r) => r.source)), graphqlOperationAttributes(doc, variables));
|
|
448
|
+
} };
|
|
449
|
+
case "i18nMessage": return { fetch: (vars) => {
|
|
450
|
+
const variables = transformVariables({
|
|
451
|
+
collection: vars.collection,
|
|
452
|
+
ids: vars.ids
|
|
453
|
+
});
|
|
454
|
+
return wrap(`source:${name}`, () => collectAsync(mapAsync(subscribe(doc, variables), (r) => r.source)), graphqlOperationAttributes(doc, variables));
|
|
455
|
+
} };
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
//#endregion
|
|
459
|
+
//#region src/transform-response.ts
|
|
460
|
+
const FLAT_PREFIX = "_flat_";
|
|
461
|
+
const LDT_PREFIX = "_ldt_";
|
|
462
|
+
const LD_PREFIX = "_ld_";
|
|
463
|
+
const MODEL_SUFFIXES = ["Entity", "Component"];
|
|
607
464
|
function decodeValue(value, decoder) {
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
return value.map((item) => decodeValue(item, decoder));
|
|
613
|
-
}
|
|
614
|
-
if (typeof value === "string") {
|
|
615
|
-
return decoder(value);
|
|
616
|
-
}
|
|
617
|
-
return value;
|
|
465
|
+
if (value === null || value === void 0) return value;
|
|
466
|
+
if (Array.isArray(value)) return value.map((item) => decodeValue(item, decoder));
|
|
467
|
+
if (typeof value === "string") return decoder(value);
|
|
468
|
+
return value;
|
|
618
469
|
}
|
|
619
470
|
function deriveModel(typename) {
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
}
|
|
471
|
+
for (const suffix of MODEL_SUFFIXES) if (typename.endsWith(suffix)) {
|
|
472
|
+
const stripped = typename.slice(0, -suffix.length);
|
|
473
|
+
return stripped.charAt(0).toLowerCase() + stripped.slice(1);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* transforms a GraphQL response in a single depth-first pass:
|
|
478
|
+
* - resolves `_flat_{field}` companions into nested component arrays (refs are
|
|
479
|
+
* matched against the companion map by `_flatId`, at any nesting depth — the
|
|
480
|
+
* companion lists the whole subtree, so one map serves every ref below it)
|
|
481
|
+
* - injects `__model` on objects with entity/component `__typename`
|
|
482
|
+
* - decodes managed scalar aliases (`_ldt_` / `_ld_`) into structured values
|
|
483
|
+
*
|
|
484
|
+
* a `seen` set guards against shared or cyclic references so every object is
|
|
485
|
+
* visited exactly once.
|
|
486
|
+
*/
|
|
628
487
|
function transformResponse(data) {
|
|
629
|
-
|
|
630
|
-
|
|
488
|
+
transformNode(data, void 0, /* @__PURE__ */ new WeakSet());
|
|
489
|
+
return data;
|
|
631
490
|
}
|
|
632
491
|
function transformNode(value, flatMap, seen) {
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
const resolveMap = ownMap ?? flatMap;
|
|
674
|
-
for (const key of Object.keys(obj)) {
|
|
675
|
-
if (key.startsWith(LDT_PREFIX)) {
|
|
676
|
-
obj[key.slice(LDT_PREFIX.length)] = decodeValue(obj[key], decodeLocalDateTime);
|
|
677
|
-
delete obj[key];
|
|
678
|
-
continue;
|
|
679
|
-
}
|
|
680
|
-
if (key.startsWith(LD_PREFIX)) {
|
|
681
|
-
obj[key.slice(LD_PREFIX.length)] = decodeValue(obj[key], decodeLocalDate);
|
|
682
|
-
delete obj[key];
|
|
683
|
-
continue;
|
|
684
|
-
}
|
|
685
|
-
const inner = obj[key];
|
|
686
|
-
if (resolveMap && Array.isArray(inner) && inner.length > 0 && inner[0]?._flatId !== void 0) {
|
|
687
|
-
obj[key] = resolveRefs(inner, resolveMap);
|
|
688
|
-
}
|
|
689
|
-
transformNode(obj[key], resolveMap, seen);
|
|
690
|
-
}
|
|
492
|
+
if (!value || typeof value !== "object") return;
|
|
493
|
+
if (seen.has(value)) return;
|
|
494
|
+
seen.add(value);
|
|
495
|
+
if (Array.isArray(value)) {
|
|
496
|
+
for (const item of value) transformNode(item, flatMap, seen);
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
const obj = value;
|
|
500
|
+
const typename = obj["__typename"];
|
|
501
|
+
if (typeof typename === "string") {
|
|
502
|
+
const model = deriveModel(typename);
|
|
503
|
+
if (model) obj["__model"] = model;
|
|
504
|
+
}
|
|
505
|
+
let ownMap;
|
|
506
|
+
for (const key of Object.keys(obj)) {
|
|
507
|
+
if (!key.startsWith(FLAT_PREFIX)) continue;
|
|
508
|
+
const flatArray = obj[key];
|
|
509
|
+
delete obj[key];
|
|
510
|
+
if (!flatArray) continue;
|
|
511
|
+
if (!ownMap) ownMap = flatMap ? new Map(flatMap) : /* @__PURE__ */ new Map();
|
|
512
|
+
for (const component of flatArray) if (component._flatId) ownMap.set(component._flatId, component);
|
|
513
|
+
}
|
|
514
|
+
const resolveMap = ownMap ?? flatMap;
|
|
515
|
+
for (const key of Object.keys(obj)) {
|
|
516
|
+
if (key.startsWith(LDT_PREFIX)) {
|
|
517
|
+
obj[key.slice(5)] = decodeValue(obj[key], decodeLocalDateTime);
|
|
518
|
+
delete obj[key];
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
if (key.startsWith(LD_PREFIX)) {
|
|
522
|
+
obj[key.slice(4)] = decodeValue(obj[key], decodeLocalDate);
|
|
523
|
+
delete obj[key];
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
const inner = obj[key];
|
|
527
|
+
if (resolveMap && Array.isArray(inner) && inner.length > 0 && inner[0]?._flatId !== void 0) obj[key] = resolveRefs(inner, resolveMap);
|
|
528
|
+
transformNode(obj[key], resolveMap, seen);
|
|
529
|
+
}
|
|
691
530
|
}
|
|
692
531
|
function resolveRefs(refs, flatMap) {
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
// src/upload.ts
|
|
707
|
-
var DEFAULT_CHUNK_SIZE = 4 * 1024 * 1024;
|
|
532
|
+
const resolved = [];
|
|
533
|
+
for (const ref of refs) {
|
|
534
|
+
if (!ref._flatId) continue;
|
|
535
|
+
const component = flatMap.get(ref._flatId);
|
|
536
|
+
if (component) resolved.push(component);
|
|
537
|
+
}
|
|
538
|
+
return resolved;
|
|
539
|
+
}
|
|
540
|
+
//#endregion
|
|
541
|
+
//#region src/upload.ts
|
|
542
|
+
const DEFAULT_CHUNK_SIZE = 4 * 1024 * 1024;
|
|
708
543
|
function isUint8Array(data) {
|
|
709
|
-
|
|
544
|
+
return data instanceof Uint8Array;
|
|
710
545
|
}
|
|
711
546
|
function upload(url, project, token, data, meta, options) {
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
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
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
var JITTER_MIN_MS2 = 300;
|
|
810
|
-
var JITTER_MAX_MS2 = 3e3;
|
|
547
|
+
const chunkSize = options?.chunkSize ?? DEFAULT_CHUNK_SIZE;
|
|
548
|
+
const wsUrl = `${url.replace(/^http/i, "ws")}/api/v1/assets/ws`;
|
|
549
|
+
const size = isUint8Array(data) ? data.length : meta.size;
|
|
550
|
+
return new Promise((resolve, reject) => {
|
|
551
|
+
const ws = new WebSocket(wsUrl, "contello-file-upload-v1");
|
|
552
|
+
let done = false;
|
|
553
|
+
const cleanup = () => {
|
|
554
|
+
options?.abort?.removeEventListener("abort", abortHandler);
|
|
555
|
+
};
|
|
556
|
+
const abortHandler = () => {
|
|
557
|
+
if (done) return;
|
|
558
|
+
ws.close();
|
|
559
|
+
reject(/* @__PURE__ */ new Error("upload aborted"));
|
|
560
|
+
};
|
|
561
|
+
options?.abort?.addEventListener("abort", abortHandler);
|
|
562
|
+
function sendBuffer(buf) {
|
|
563
|
+
let offset = 0;
|
|
564
|
+
while (offset < buf.length) {
|
|
565
|
+
const chunk = buf.subarray(offset, offset + chunkSize);
|
|
566
|
+
ws.send(chunk);
|
|
567
|
+
offset += chunk.length;
|
|
568
|
+
}
|
|
569
|
+
ws.send(JSON.stringify({ type: "done" }));
|
|
570
|
+
}
|
|
571
|
+
const MAX_BUFFER_SIZE = 8 * 1024 * 1024;
|
|
572
|
+
function waitForBufferedDrain() {
|
|
573
|
+
return new Promise((resolveDrain, rejectDrain) => {
|
|
574
|
+
const check = () => {
|
|
575
|
+
if (options?.abort?.aborted) {
|
|
576
|
+
rejectDrain(/* @__PURE__ */ new Error("upload aborted"));
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
if (ws.bufferedAmount <= MAX_BUFFER_SIZE) {
|
|
580
|
+
resolveDrain();
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
setTimeout(check, 50);
|
|
584
|
+
};
|
|
585
|
+
check();
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
async function sendStream(stream) {
|
|
589
|
+
const reader = stream.getReader();
|
|
590
|
+
try {
|
|
591
|
+
while (true) {
|
|
592
|
+
const { done: streamDone, value } = await reader.read();
|
|
593
|
+
if (streamDone) break;
|
|
594
|
+
await waitForBufferedDrain();
|
|
595
|
+
ws.send(value);
|
|
596
|
+
}
|
|
597
|
+
ws.send(JSON.stringify({ type: "done" }));
|
|
598
|
+
} catch (error) {
|
|
599
|
+
reject(error);
|
|
600
|
+
ws.close();
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
ws.addEventListener("open", () => {
|
|
604
|
+
const initFrame = {
|
|
605
|
+
type: "init",
|
|
606
|
+
metadata: {
|
|
607
|
+
...meta,
|
|
608
|
+
...size !== void 0 && { size },
|
|
609
|
+
projectRef: project
|
|
610
|
+
},
|
|
611
|
+
token
|
|
612
|
+
};
|
|
613
|
+
ws.send(JSON.stringify(initFrame));
|
|
614
|
+
});
|
|
615
|
+
ws.addEventListener("message", (event) => {
|
|
616
|
+
const message = JSON.parse(String(event.data));
|
|
617
|
+
if (message.type === "ack") {
|
|
618
|
+
if (isUint8Array(data)) sendBuffer(data);
|
|
619
|
+
else sendStream(data);
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
if (message.type === "done") {
|
|
623
|
+
done = true;
|
|
624
|
+
cleanup();
|
|
625
|
+
ws.close();
|
|
626
|
+
resolve(message.id);
|
|
627
|
+
}
|
|
628
|
+
});
|
|
629
|
+
ws.addEventListener("error", () => {
|
|
630
|
+
cleanup();
|
|
631
|
+
reject(/* @__PURE__ */ new Error("upload websocket error"));
|
|
632
|
+
});
|
|
633
|
+
ws.addEventListener("close", () => {
|
|
634
|
+
cleanup();
|
|
635
|
+
if (!done) reject(/* @__PURE__ */ new Error("connection closed before upload completed"));
|
|
636
|
+
});
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
//#endregion
|
|
640
|
+
//#region src/utils.ts
|
|
641
|
+
const MAX_DELAY_MS = 3e4;
|
|
642
|
+
const JITTER_MIN_MS = 300;
|
|
643
|
+
const JITTER_MAX_MS = 3e3;
|
|
811
644
|
function wsRetryWait(retries) {
|
|
812
|
-
|
|
813
|
-
|
|
645
|
+
const delay = Math.min(Math.pow(2, retries) * 1e3, MAX_DELAY_MS) + Math.floor(Math.random() * (JITTER_MAX_MS - JITTER_MIN_MS) + JITTER_MIN_MS);
|
|
646
|
+
return new Promise((resolve) => setTimeout(resolve, delay));
|
|
814
647
|
}
|
|
815
|
-
|
|
816
|
-
|
|
648
|
+
//#endregion
|
|
649
|
+
//#region src/client.ts
|
|
817
650
|
var ContelloClient = class {
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
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
|
-
|
|
898
|
-
|
|
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
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
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
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
}
|
|
1001
|
-
execute(query, variables) {
|
|
1002
|
-
return wrap("@contello/client:execute", () => firstAsync(this.subscribe(query, variables)));
|
|
1003
|
-
}
|
|
651
|
+
_pool;
|
|
652
|
+
_rpc;
|
|
653
|
+
_sources;
|
|
654
|
+
_url;
|
|
655
|
+
_project;
|
|
656
|
+
_token;
|
|
657
|
+
_agent;
|
|
658
|
+
constructor(options) {
|
|
659
|
+
const { url, project, token, schema, connections = 1, connectionEvents } = options;
|
|
660
|
+
this._url = url;
|
|
661
|
+
this._project = project;
|
|
662
|
+
this._token = token;
|
|
663
|
+
this._agent = createHttpAgent(options.http);
|
|
664
|
+
const websocketUrl = `${url}/graphql/projects/${project}`.replace(/^http/i, "ws");
|
|
665
|
+
let state = "disconnected";
|
|
666
|
+
let connectedCount = 0;
|
|
667
|
+
const onConnectionUp = () => {
|
|
668
|
+
connectedCount++;
|
|
669
|
+
if (connectedCount === connections && state !== "connected") {
|
|
670
|
+
state = "connected";
|
|
671
|
+
options.onConnected?.();
|
|
672
|
+
}
|
|
673
|
+
};
|
|
674
|
+
const onConnectionDown = () => {
|
|
675
|
+
if (connectedCount > 0) connectedCount--;
|
|
676
|
+
if (state === "connected") {
|
|
677
|
+
state = "reconnecting";
|
|
678
|
+
options.onReconnecting?.();
|
|
679
|
+
}
|
|
680
|
+
};
|
|
681
|
+
this._pool = new ConnectionPool((id) => {
|
|
682
|
+
const context = Object.freeze({
|
|
683
|
+
connectionId: id,
|
|
684
|
+
websocketUrl
|
|
685
|
+
});
|
|
686
|
+
return createClient({
|
|
687
|
+
url: websocketUrl,
|
|
688
|
+
connectionParams: { token },
|
|
689
|
+
lazy: false,
|
|
690
|
+
keepAlive: 3e4,
|
|
691
|
+
retryAttempts: Infinity,
|
|
692
|
+
retryWait: wsRetryWait,
|
|
693
|
+
shouldRetry: () => true,
|
|
694
|
+
jsonMessageReplacer: (key, value) => {
|
|
695
|
+
if (!key) return injectTraceContext(value);
|
|
696
|
+
return value;
|
|
697
|
+
},
|
|
698
|
+
...options.onError && { onNonLazyError: (e) => options.onError(e) },
|
|
699
|
+
on: {
|
|
700
|
+
connected: () => {
|
|
701
|
+
onConnectionUp();
|
|
702
|
+
connectionEvents?.onConnected?.(context);
|
|
703
|
+
},
|
|
704
|
+
connecting: () => {
|
|
705
|
+
connectionEvents?.onConnecting?.(context);
|
|
706
|
+
},
|
|
707
|
+
closed: () => {
|
|
708
|
+
onConnectionDown();
|
|
709
|
+
connectionEvents?.onClosed?.(context);
|
|
710
|
+
},
|
|
711
|
+
...(options.onError || connectionEvents?.onError) && { error: (e) => {
|
|
712
|
+
options.onError?.(e);
|
|
713
|
+
connectionEvents?.onError?.(context, e);
|
|
714
|
+
} }
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
}, connections);
|
|
718
|
+
this._rpc = schema?.operations ? buildRpc(schema.operations, (q, v) => this.subscribe(q, v)) : void 0;
|
|
719
|
+
this._sources = schema?.sources ? createSources(schema.sources, (q, v) => this.subscribe(q, v)) : void 0;
|
|
720
|
+
}
|
|
721
|
+
get rpc() {
|
|
722
|
+
if (!this._rpc) throw new Error("@contello/client: .rpc accessed without a schema containing operations");
|
|
723
|
+
return this._rpc;
|
|
724
|
+
}
|
|
725
|
+
get sources() {
|
|
726
|
+
if (!this._sources) throw new Error("@contello/client: .sources accessed without a schema containing sources");
|
|
727
|
+
return this._sources;
|
|
728
|
+
}
|
|
729
|
+
async init() {
|
|
730
|
+
await this._pool.connect();
|
|
731
|
+
}
|
|
732
|
+
async destroy() {
|
|
733
|
+
await this._pool.disconnect();
|
|
734
|
+
await this._agent.close();
|
|
735
|
+
}
|
|
736
|
+
async ping() {
|
|
737
|
+
return ping((query) => firstAsync(this.subscribe(query)));
|
|
738
|
+
}
|
|
739
|
+
/**
|
|
740
|
+
* Open a subscription on the websocket. Each `[Symbol.asyncIterator]()` call (the implicit one
|
|
741
|
+
* from `for await`, or via `rxjs.from(this)`) starts a fresh server subscription and tears it
|
|
742
|
+
* down when the iterator is returned (`break`, error, or explicit stop).
|
|
743
|
+
*/
|
|
744
|
+
subscribe(query, variables) {
|
|
745
|
+
const pool = this._pool;
|
|
746
|
+
return { [Symbol.asyncIterator]() {
|
|
747
|
+
const wsClient = pool.get();
|
|
748
|
+
const queue = [];
|
|
749
|
+
let pending;
|
|
750
|
+
let done = false;
|
|
751
|
+
let error;
|
|
752
|
+
const unsubscribe = wsClient.subscribe({
|
|
753
|
+
query,
|
|
754
|
+
variables
|
|
755
|
+
}, {
|
|
756
|
+
next(msg) {
|
|
757
|
+
const value = transformResponse(msg.data);
|
|
758
|
+
if (value === void 0 || value === null) return;
|
|
759
|
+
if (pending) {
|
|
760
|
+
const p = pending;
|
|
761
|
+
pending = void 0;
|
|
762
|
+
p.resolve({
|
|
763
|
+
value,
|
|
764
|
+
done: false
|
|
765
|
+
});
|
|
766
|
+
} else queue.push(value);
|
|
767
|
+
},
|
|
768
|
+
error(err) {
|
|
769
|
+
done = true;
|
|
770
|
+
error = err;
|
|
771
|
+
if (pending) {
|
|
772
|
+
const p = pending;
|
|
773
|
+
pending = void 0;
|
|
774
|
+
p.reject(err);
|
|
775
|
+
}
|
|
776
|
+
},
|
|
777
|
+
complete() {
|
|
778
|
+
done = true;
|
|
779
|
+
if (pending) {
|
|
780
|
+
const p = pending;
|
|
781
|
+
pending = void 0;
|
|
782
|
+
p.resolve({
|
|
783
|
+
value: void 0,
|
|
784
|
+
done: true
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
});
|
|
789
|
+
return {
|
|
790
|
+
next() {
|
|
791
|
+
if (queue.length > 0) return Promise.resolve({
|
|
792
|
+
value: queue.shift(),
|
|
793
|
+
done: false
|
|
794
|
+
});
|
|
795
|
+
if (error !== void 0) return Promise.reject(error);
|
|
796
|
+
if (done) return Promise.resolve({
|
|
797
|
+
value: void 0,
|
|
798
|
+
done: true
|
|
799
|
+
});
|
|
800
|
+
return new Promise((resolve, reject) => {
|
|
801
|
+
pending = {
|
|
802
|
+
resolve,
|
|
803
|
+
reject
|
|
804
|
+
};
|
|
805
|
+
});
|
|
806
|
+
},
|
|
807
|
+
return() {
|
|
808
|
+
unsubscribe();
|
|
809
|
+
return Promise.resolve({
|
|
810
|
+
value: void 0,
|
|
811
|
+
done: true
|
|
812
|
+
});
|
|
813
|
+
},
|
|
814
|
+
throw(err) {
|
|
815
|
+
unsubscribe();
|
|
816
|
+
return Promise.reject(err);
|
|
817
|
+
}
|
|
818
|
+
};
|
|
819
|
+
} };
|
|
820
|
+
}
|
|
821
|
+
download(fileId) {
|
|
822
|
+
return wrap("@contello/client:download", () => downloadFile(this._agent, this._url, this._token, fileId));
|
|
823
|
+
}
|
|
824
|
+
proxyHls(path, signal) {
|
|
825
|
+
return wrap("@contello/client:proxyHls", () => proxyHls(this._agent, this._url, this._token, path, signal));
|
|
826
|
+
}
|
|
827
|
+
upload(data, meta, options) {
|
|
828
|
+
return wrap("@contello/client:upload", () => upload(this._url, this._project, this._token, data, meta, options));
|
|
829
|
+
}
|
|
830
|
+
execute(query, variables) {
|
|
831
|
+
return wrap("@contello/client:execute", () => firstAsync(this.subscribe(query, variables)), graphqlOperationAttributes(query, variables));
|
|
832
|
+
}
|
|
1004
833
|
};
|
|
1005
834
|
function createContelloClient(options) {
|
|
1006
|
-
|
|
835
|
+
return new ContelloClient(options);
|
|
1007
836
|
}
|
|
1008
|
-
|
|
1009
|
-
|
|
837
|
+
//#endregion
|
|
838
|
+
//#region src/async-iterable-subject.ts
|
|
1010
839
|
function createAsyncIterableSubject() {
|
|
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
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
}
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
840
|
+
const consumers = /* @__PURE__ */ new Set();
|
|
841
|
+
let status = { state: "open" };
|
|
842
|
+
const next = (value) => {
|
|
843
|
+
if (status.state !== "open") return;
|
|
844
|
+
for (const consumer of consumers) if (consumer.kind === "listener") consumer.next?.(value);
|
|
845
|
+
else if (consumer.pending) {
|
|
846
|
+
const p = consumer.pending;
|
|
847
|
+
consumer.pending = void 0;
|
|
848
|
+
p.resolve({
|
|
849
|
+
value,
|
|
850
|
+
done: false
|
|
851
|
+
});
|
|
852
|
+
} else consumer.queue.push(value);
|
|
853
|
+
};
|
|
854
|
+
const error = (err) => {
|
|
855
|
+
if (status.state !== "open") return;
|
|
856
|
+
status = {
|
|
857
|
+
state: "errored",
|
|
858
|
+
err
|
|
859
|
+
};
|
|
860
|
+
for (const consumer of consumers) if (consumer.kind === "listener") consumer.error?.(err);
|
|
861
|
+
else if (consumer.pending) {
|
|
862
|
+
const p = consumer.pending;
|
|
863
|
+
consumer.pending = void 0;
|
|
864
|
+
p.reject(err);
|
|
865
|
+
}
|
|
866
|
+
consumers.clear();
|
|
867
|
+
};
|
|
868
|
+
const complete = () => {
|
|
869
|
+
if (status.state !== "open") return;
|
|
870
|
+
status = { state: "completed" };
|
|
871
|
+
for (const consumer of consumers) if (consumer.kind === "listener") consumer.complete?.();
|
|
872
|
+
else if (consumer.pending) {
|
|
873
|
+
const p = consumer.pending;
|
|
874
|
+
consumer.pending = void 0;
|
|
875
|
+
p.resolve({
|
|
876
|
+
value: void 0,
|
|
877
|
+
done: true
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
consumers.clear();
|
|
881
|
+
};
|
|
882
|
+
const subscribe = (observerOrNext) => {
|
|
883
|
+
if (status.state !== "open") return () => {};
|
|
884
|
+
const consumer = {
|
|
885
|
+
kind: "listener",
|
|
886
|
+
...typeof observerOrNext === "function" ? { next: observerOrNext } : observerOrNext
|
|
887
|
+
};
|
|
888
|
+
consumers.add(consumer);
|
|
889
|
+
return () => {
|
|
890
|
+
consumers.delete(consumer);
|
|
891
|
+
};
|
|
892
|
+
};
|
|
893
|
+
return Object.assign({ [Symbol.asyncIterator]() {
|
|
894
|
+
if (status.state === "errored") {
|
|
895
|
+
const err = status.err;
|
|
896
|
+
return {
|
|
897
|
+
next: () => Promise.reject(err),
|
|
898
|
+
return: () => Promise.resolve({
|
|
899
|
+
value: void 0,
|
|
900
|
+
done: true
|
|
901
|
+
})
|
|
902
|
+
};
|
|
903
|
+
}
|
|
904
|
+
if (status.state === "completed") return {
|
|
905
|
+
next: () => Promise.resolve({
|
|
906
|
+
value: void 0,
|
|
907
|
+
done: true
|
|
908
|
+
}),
|
|
909
|
+
return: () => Promise.resolve({
|
|
910
|
+
value: void 0,
|
|
911
|
+
done: true
|
|
912
|
+
})
|
|
913
|
+
};
|
|
914
|
+
const consumer = {
|
|
915
|
+
kind: "iterator",
|
|
916
|
+
queue: []
|
|
917
|
+
};
|
|
918
|
+
consumers.add(consumer);
|
|
919
|
+
return {
|
|
920
|
+
next() {
|
|
921
|
+
if (consumer.queue.length > 0) return Promise.resolve({
|
|
922
|
+
value: consumer.queue.shift(),
|
|
923
|
+
done: false
|
|
924
|
+
});
|
|
925
|
+
if (status.state === "errored") return Promise.reject(status.err);
|
|
926
|
+
if (status.state === "completed") return Promise.resolve({
|
|
927
|
+
value: void 0,
|
|
928
|
+
done: true
|
|
929
|
+
});
|
|
930
|
+
return new Promise((resolve, reject) => {
|
|
931
|
+
consumer.pending = {
|
|
932
|
+
resolve,
|
|
933
|
+
reject
|
|
934
|
+
};
|
|
935
|
+
});
|
|
936
|
+
},
|
|
937
|
+
return() {
|
|
938
|
+
consumers.delete(consumer);
|
|
939
|
+
if (consumer.pending) {
|
|
940
|
+
const p = consumer.pending;
|
|
941
|
+
consumer.pending = void 0;
|
|
942
|
+
p.resolve({
|
|
943
|
+
value: void 0,
|
|
944
|
+
done: true
|
|
945
|
+
});
|
|
946
|
+
}
|
|
947
|
+
return Promise.resolve({
|
|
948
|
+
value: void 0,
|
|
949
|
+
done: true
|
|
950
|
+
});
|
|
951
|
+
},
|
|
952
|
+
throw(err) {
|
|
953
|
+
consumers.delete(consumer);
|
|
954
|
+
return Promise.reject(err);
|
|
955
|
+
}
|
|
956
|
+
};
|
|
957
|
+
} }, {
|
|
958
|
+
next,
|
|
959
|
+
error,
|
|
960
|
+
complete,
|
|
961
|
+
subscribe
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
//#endregion
|
|
965
|
+
export { ContelloClient, asyncKeepalive, collectAsync, createAsyncIterableSubject, createContelloClient, createSourceSubscription, exponentialBackoff, filterAsync, firstAsync, mapAsync, runWithBackoff };
|
|
966
|
+
|
|
967
|
+
//# sourceMappingURL=index.js.map
|