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