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