@contello/client 2.2.2 → 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 -14
- package/dist/cli.js.map +1 -0
- package/dist/generate-DIoN94iO.js +1068 -0
- package/dist/generate-DIoN94iO.js.map +1 -0
- package/dist/index.cjs +936 -1074
- 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 +925 -1048
- package/dist/index.js.map +1 -0
- package/package.json +2 -2
- package/dist/generate-Z4LVDAB4.js +0 -1241
package/dist/index.cjs
CHANGED
|
@@ -1,1182 +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 error;
|
|
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
|
-
onMessage.publish(msg);
|
|
199
|
-
return msg.message;
|
|
182
|
+
if (!onMessage.hasSubscribers) return message;
|
|
183
|
+
const msg = { message };
|
|
184
|
+
onMessage.publish(msg);
|
|
185
|
+
return msg.message;
|
|
200
186
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
"age"
|
|
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"
|
|
216
201
|
]);
|
|
217
202
|
function assertSafeSegment(segment, kind) {
|
|
218
|
-
|
|
219
|
-
throw new Error(`invalid ${kind}`);
|
|
220
|
-
}
|
|
203
|
+
if (!SAFE_SEGMENT.test(segment) || segment === "." || segment === "..") throw new Error(`invalid ${kind}`);
|
|
221
204
|
}
|
|
222
205
|
function assertSafeAssetPath(path, kind) {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
for (const segment of path.split("/")) {
|
|
227
|
-
assertSafeSegment(segment, kind);
|
|
228
|
-
}
|
|
206
|
+
if (path === "") throw new Error(`invalid ${kind}`);
|
|
207
|
+
for (const segment of path.split("/")) assertSafeSegment(segment, kind);
|
|
229
208
|
}
|
|
230
209
|
function createHttpAgent(options) {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
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
|
+
});
|
|
239
218
|
}
|
|
240
219
|
async function downloadFile(agent, url, token, fileId) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
stream: () => toWebStream(response.body)
|
|
257
|
-
};
|
|
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
|
+
};
|
|
258
235
|
}
|
|
259
236
|
async function proxyHls(agent, url, token, path, signal) {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
return buildProxyResult(response, signal);
|
|
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);
|
|
267
243
|
}
|
|
268
244
|
function buildProxyResult(response, signal) {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
return {
|
|
281
|
-
status: response.statusCode,
|
|
282
|
-
headers,
|
|
283
|
-
stream: () => toWebStream(response.body, signal)
|
|
284
|
-
};
|
|
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
|
+
};
|
|
285
256
|
}
|
|
286
257
|
function firstHeader(headers, key) {
|
|
287
|
-
|
|
288
|
-
|
|
258
|
+
const v = headers[key];
|
|
259
|
+
return Array.isArray(v) ? v[0] : v;
|
|
289
260
|
}
|
|
290
261
|
function safeDestroyBody(body) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
} catch {
|
|
296
|
-
}
|
|
262
|
+
try {
|
|
263
|
+
body.on("error", () => {});
|
|
264
|
+
body.destroy();
|
|
265
|
+
} catch {}
|
|
297
266
|
}
|
|
298
267
|
function toWebStream(body, signal) {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
});
|
|
343
|
-
},
|
|
344
|
-
cancel() {
|
|
345
|
-
cleanup();
|
|
346
|
-
}
|
|
347
|
-
});
|
|
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
|
+
});
|
|
348
311
|
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
312
|
+
//#endregion
|
|
313
|
+
//#region src/ping.ts
|
|
314
|
+
const PING_QUERY = `query ContelloPing { contelloPing { response } }`;
|
|
352
315
|
async function ping(execute) {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
throw new Error(`unexpected ping response: ${data.contelloPing.response}`);
|
|
356
|
-
}
|
|
316
|
+
const data = await execute(PING_QUERY);
|
|
317
|
+
if (data.contelloPing.response !== "pong") throw new Error(`unexpected ping response: ${data.contelloPing.response}`);
|
|
357
318
|
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
319
|
+
//#endregion
|
|
320
|
+
//#region src/pool.ts
|
|
321
|
+
const DEFAULT_CONNECT_TIMEOUT = 1e4;
|
|
361
322
|
var ConnectionPool = class {
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
this._clients = [];
|
|
429
|
-
this._connected = [];
|
|
430
|
-
this._currentIndex = 0;
|
|
431
|
-
await Promise.all(closed);
|
|
432
|
-
}
|
|
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
|
+
}
|
|
433
389
|
};
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
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})/;
|
|
440
407
|
function pad2(n) {
|
|
441
|
-
|
|
408
|
+
return n.toString().padStart(2, "0");
|
|
442
409
|
}
|
|
443
410
|
function pad4(n) {
|
|
444
|
-
|
|
411
|
+
return n.toString().padStart(4, "0");
|
|
445
412
|
}
|
|
446
413
|
function hasExactKeys(value, keys) {
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
}
|
|
451
|
-
for (const key of keys) {
|
|
452
|
-
if (!Object.prototype.hasOwnProperty.call(value, key)) {
|
|
453
|
-
return false;
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
return true;
|
|
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;
|
|
457
417
|
}
|
|
458
418
|
function allNumbers(value, keys) {
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
return false;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
return true;
|
|
419
|
+
for (const key of keys) if (typeof value[key] !== "number") return false;
|
|
420
|
+
return true;
|
|
465
421
|
}
|
|
466
422
|
function isLocalDateTime(value) {
|
|
467
|
-
|
|
423
|
+
return !!value && typeof value === "object" && !Array.isArray(value) && hasExactKeys(value, LOCAL_DATE_TIME_KEYS) && allNumbers(value, LOCAL_DATE_TIME_KEYS);
|
|
468
424
|
}
|
|
469
425
|
function isLocalDate(value) {
|
|
470
|
-
|
|
426
|
+
return !!value && typeof value === "object" && !Array.isArray(value) && hasExactKeys(value, LOCAL_DATE_KEYS) && allNumbers(value, LOCAL_DATE_KEYS);
|
|
471
427
|
}
|
|
472
428
|
function decodeLocalDateTime(value) {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
second: Number(match[6])
|
|
484
|
-
};
|
|
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
|
+
};
|
|
485
439
|
}
|
|
486
440
|
function decodeLocalDate(value) {
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
day: Number(match[3])
|
|
495
|
-
};
|
|
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
|
+
};
|
|
496
448
|
}
|
|
497
449
|
function encodeLocalDateTime(value) {
|
|
498
|
-
|
|
450
|
+
return `${pad4(value.year)}-${pad2(value.month)}-${pad2(value.day)}T${pad2(value.hour)}:${pad2(value.minute)}:${pad2(value.second)}Z`;
|
|
499
451
|
}
|
|
500
452
|
function encodeLocalDate(value) {
|
|
501
|
-
|
|
453
|
+
return `${pad4(value.year)}-${pad2(value.month)}-${pad2(value.day)}T00:00:00Z`;
|
|
502
454
|
}
|
|
503
|
-
|
|
504
|
-
|
|
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
|
+
*/
|
|
505
463
|
function transformVariables(value) {
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
if (isLocalDateTime(value)) {
|
|
516
|
-
return encodeLocalDateTime(value);
|
|
517
|
-
}
|
|
518
|
-
if (isLocalDate(value)) {
|
|
519
|
-
return encodeLocalDate(value);
|
|
520
|
-
}
|
|
521
|
-
const obj = value;
|
|
522
|
-
const out = {};
|
|
523
|
-
for (const key of Object.keys(obj)) {
|
|
524
|
-
out[key] = transformVariables(obj[key]);
|
|
525
|
-
}
|
|
526
|
-
return out;
|
|
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;
|
|
527
473
|
}
|
|
528
|
-
|
|
529
|
-
|
|
474
|
+
//#endregion
|
|
475
|
+
//#region src/rpc.ts
|
|
530
476
|
function buildRpc(operations, subscribe) {
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
}
|
|
535
|
-
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;
|
|
536
480
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
481
|
+
//#endregion
|
|
482
|
+
//#region src/source-subscription.ts
|
|
483
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
540
484
|
function createSelection(source) {
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
case "route": {
|
|
550
|
-
return `subscription($ids: [ID!], $paths: [String!]) { source: ${source.subscription}(request: { ids: $ids, paths: $paths }) ${spread} }`;
|
|
551
|
-
}
|
|
552
|
-
case "asset": {
|
|
553
|
-
return `subscription($ids: [ID!]) { source: ${source.subscription}(filter: { ids: $ids }) ${spread} }`;
|
|
554
|
-
}
|
|
555
|
-
case "i18nMessage": {
|
|
556
|
-
return `subscription($collection: String!, $ids: [ID!]) { source: ${source.subscription}(collectionReferenceName: $collection, ids: $ids) ${spread} }`;
|
|
557
|
-
}
|
|
558
|
-
}
|
|
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
|
+
}
|
|
559
493
|
}
|
|
560
494
|
function createSourceSubscription(source) {
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
${createSelection(source)}`;
|
|
567
|
-
cache.set(source, doc);
|
|
568
|
-
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;
|
|
569
500
|
}
|
|
570
|
-
|
|
571
|
-
|
|
501
|
+
//#endregion
|
|
502
|
+
//#region src/sources.ts
|
|
572
503
|
function createSources(sources, subscribe) {
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
};
|
|
596
|
-
break;
|
|
597
|
-
}
|
|
598
|
-
case "route": {
|
|
599
|
-
out[name] = {
|
|
600
|
-
fetch: (vars) => wrap(
|
|
601
|
-
`source:${name}`,
|
|
602
|
-
() => collectAsync(
|
|
603
|
-
mapAsync(
|
|
604
|
-
subscribe(doc, transformVariables({ ids: vars?.ids, paths: vars?.paths })),
|
|
605
|
-
(r) => r.source
|
|
606
|
-
)
|
|
607
|
-
)
|
|
608
|
-
)
|
|
609
|
-
};
|
|
610
|
-
break;
|
|
611
|
-
}
|
|
612
|
-
case "asset": {
|
|
613
|
-
out[name] = {
|
|
614
|
-
fetch: (vars) => wrap(
|
|
615
|
-
`source:${name}`,
|
|
616
|
-
() => collectAsync(
|
|
617
|
-
mapAsync(
|
|
618
|
-
subscribe(doc, transformVariables({ ids: vars?.ids })),
|
|
619
|
-
(r) => r.source
|
|
620
|
-
)
|
|
621
|
-
)
|
|
622
|
-
)
|
|
623
|
-
};
|
|
624
|
-
break;
|
|
625
|
-
}
|
|
626
|
-
case "i18nMessage": {
|
|
627
|
-
out[name] = {
|
|
628
|
-
fetch: (vars) => wrap(
|
|
629
|
-
`source:${name}`,
|
|
630
|
-
() => collectAsync(
|
|
631
|
-
mapAsync(
|
|
632
|
-
subscribe(
|
|
633
|
-
doc,
|
|
634
|
-
transformVariables({ collection: vars.collection, ids: vars.ids })
|
|
635
|
-
),
|
|
636
|
-
(r) => r.source
|
|
637
|
-
)
|
|
638
|
-
)
|
|
639
|
-
)
|
|
640
|
-
};
|
|
641
|
-
break;
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
return out;
|
|
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;
|
|
511
|
+
}
|
|
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
|
+
}
|
|
646
526
|
}
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
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"];
|
|
653
533
|
function decodeValue(value, decoder) {
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
return value.map((item) => decodeValue(item, decoder));
|
|
659
|
-
}
|
|
660
|
-
if (typeof value === "string") {
|
|
661
|
-
return decoder(value);
|
|
662
|
-
}
|
|
663
|
-
return value;
|
|
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;
|
|
664
538
|
}
|
|
665
539
|
function deriveModel(typename) {
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
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
|
+
}
|
|
673
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
|
+
*/
|
|
674
556
|
function transformResponse(data) {
|
|
675
|
-
|
|
676
|
-
|
|
557
|
+
transformNode(data, void 0, /* @__PURE__ */ new WeakSet());
|
|
558
|
+
return data;
|
|
677
559
|
}
|
|
678
560
|
function transformNode(value, flatMap, seen) {
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
const resolveMap = ownMap ?? flatMap;
|
|
720
|
-
for (const key of Object.keys(obj)) {
|
|
721
|
-
if (key.startsWith(LDT_PREFIX)) {
|
|
722
|
-
obj[key.slice(LDT_PREFIX.length)] = decodeValue(obj[key], decodeLocalDateTime);
|
|
723
|
-
delete obj[key];
|
|
724
|
-
continue;
|
|
725
|
-
}
|
|
726
|
-
if (key.startsWith(LD_PREFIX)) {
|
|
727
|
-
obj[key.slice(LD_PREFIX.length)] = decodeValue(obj[key], decodeLocalDate);
|
|
728
|
-
delete obj[key];
|
|
729
|
-
continue;
|
|
730
|
-
}
|
|
731
|
-
const inner = obj[key];
|
|
732
|
-
if (resolveMap && Array.isArray(inner) && inner.length > 0 && inner[0]?._flatId !== void 0) {
|
|
733
|
-
obj[key] = resolveRefs(inner, resolveMap);
|
|
734
|
-
}
|
|
735
|
-
transformNode(obj[key], resolveMap, seen);
|
|
736
|
-
}
|
|
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
|
+
}
|
|
737
599
|
}
|
|
738
600
|
function resolveRefs(refs, flatMap) {
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
resolved.push(component);
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
return resolved;
|
|
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;
|
|
750
608
|
}
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
609
|
+
//#endregion
|
|
610
|
+
//#region src/upload.ts
|
|
611
|
+
const DEFAULT_CHUNK_SIZE = 4 * 1024 * 1024;
|
|
754
612
|
function isUint8Array(data) {
|
|
755
|
-
|
|
613
|
+
return data instanceof Uint8Array;
|
|
756
614
|
}
|
|
757
615
|
function upload(url, project, token, data, meta, options) {
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
});
|
|
850
|
-
});
|
|
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
|
+
});
|
|
851
707
|
}
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
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;
|
|
857
713
|
function wsRetryWait(retries) {
|
|
858
|
-
|
|
859
|
-
|
|
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));
|
|
860
716
|
}
|
|
861
|
-
|
|
862
|
-
|
|
717
|
+
//#endregion
|
|
718
|
+
//#region src/client.ts
|
|
863
719
|
var ContelloClient = class {
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
}
|
|
1047
|
-
execute(query, variables) {
|
|
1048
|
-
return wrap("@contello/client:execute", () => firstAsync(this.subscribe(query, variables)));
|
|
1049
|
-
}
|
|
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
|
+
}
|
|
1050
902
|
};
|
|
1051
903
|
function createContelloClient(options) {
|
|
1052
|
-
|
|
904
|
+
return new ContelloClient(options);
|
|
1053
905
|
}
|
|
1054
|
-
|
|
1055
|
-
|
|
906
|
+
//#endregion
|
|
907
|
+
//#region src/async-iterable-subject.ts
|
|
1056
908
|
function createAsyncIterableSubject() {
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
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
|
+
});
|
|
1168
1032
|
}
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
runWithBackoff
|
|
1182
|
-
});
|
|
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;
|