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