@contello/client 2.2.3 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +36 -344
- package/dist/index.d.cts +1 -65
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +1 -65
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +36 -313
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
package/dist/index.js
CHANGED
|
@@ -1,166 +1,7 @@
|
|
|
1
|
+
import { createOperationTelemetry, graphqlOperationAttributes, injectTraceContext } from "@contello/opentelemetry";
|
|
2
|
+
import { concatAsync, exponentialBackoff, firstAsync, mapAsync } from "@entwico/dash/async";
|
|
1
3
|
import { createClient } from "graphql-ws";
|
|
2
|
-
import dc from "node:diagnostics_channel";
|
|
3
4
|
import { Agent, request } from "undici";
|
|
4
|
-
//#region src/async-iterable-utils.ts
|
|
5
|
-
const MAX_BACKOFF_MS = 3e4;
|
|
6
|
-
const JITTER_MIN_MS$1 = 300;
|
|
7
|
-
const JITTER_MAX_MS$1 = 3e3;
|
|
8
|
-
/**
|
|
9
|
-
* Returns the first value yielded by `iter`, then disposes the iterator. Throws if the iterable
|
|
10
|
-
* completes without yielding. Equivalent of rxjs's `firstValueFrom` for AsyncIterables.
|
|
11
|
-
*/
|
|
12
|
-
async function firstAsync(iter) {
|
|
13
|
-
const it = iter[Symbol.asyncIterator]();
|
|
14
|
-
try {
|
|
15
|
-
const result = await it.next();
|
|
16
|
-
if (result.done) throw new Error("firstAsync: async iterable completed without yielding a value");
|
|
17
|
-
return result.value;
|
|
18
|
-
} finally {
|
|
19
|
-
await it.return?.();
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
/** Lazy map over an async iterable. Cleanup propagates: breaking the outer iter disposes the inner. */
|
|
23
|
-
async function* mapAsync(iter, fn) {
|
|
24
|
-
for await (const value of iter) yield fn(value);
|
|
25
|
-
}
|
|
26
|
-
/** Lazy filter over an async iterable. */
|
|
27
|
-
async function* filterAsync(iter, predicate) {
|
|
28
|
-
for await (const value of iter) if (predicate(value)) yield value;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Concatenates all elements of all arrays yielded by `iter` into a single array. Used to support
|
|
32
|
-
* subscriptions that emit chunks of items before completing.
|
|
33
|
-
*/
|
|
34
|
-
async function collectAsync(iter) {
|
|
35
|
-
const out = [];
|
|
36
|
-
for await (const chunk of iter) out.push(...chunk);
|
|
37
|
-
return out;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Exponential backoff with jitter. `attempt` starts at 0 → ~1s+jitter, 1 → ~2s+jitter, etc.,
|
|
41
|
-
* capped at 30s. Returns a Promise that resolves after the delay.
|
|
42
|
-
*/
|
|
43
|
-
function exponentialBackoff(attempt) {
|
|
44
|
-
return delay(Math.min(Math.pow(2, attempt) * 1e3, MAX_BACKOFF_MS) + Math.floor(Math.random() * (JITTER_MAX_MS$1 - JITTER_MIN_MS$1) + JITTER_MIN_MS$1));
|
|
45
|
-
}
|
|
46
|
-
function delay(ms, signal) {
|
|
47
|
-
if (signal?.aborted) return Promise.reject(signal.reason);
|
|
48
|
-
return new Promise((resolve, reject) => {
|
|
49
|
-
const timer = setTimeout(() => {
|
|
50
|
-
signal?.removeEventListener("abort", onAbort);
|
|
51
|
-
resolve();
|
|
52
|
-
}, ms);
|
|
53
|
-
const onAbort = () => {
|
|
54
|
-
clearTimeout(timer);
|
|
55
|
-
reject(signal.reason);
|
|
56
|
-
};
|
|
57
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Runs `fn` with exponential-backoff retry on rejection. Returns once `fn` resolves successfully.
|
|
62
|
-
* `signal` aborts both the in-flight `fn` (if it observes the signal) and the backoff wait.
|
|
63
|
-
*/
|
|
64
|
-
async function runWithBackoff(fn, signal) {
|
|
65
|
-
let attempt = 0;
|
|
66
|
-
for (;;) {
|
|
67
|
-
if (signal?.aborted) throw signal.reason;
|
|
68
|
-
try {
|
|
69
|
-
await fn();
|
|
70
|
-
return;
|
|
71
|
-
} catch {
|
|
72
|
-
await exponentialBackoffWithSignal(attempt, signal);
|
|
73
|
-
attempt++;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
function exponentialBackoffWithSignal(attempt, signal) {
|
|
78
|
-
return delay(Math.min(Math.pow(2, attempt) * 1e3, MAX_BACKOFF_MS) + Math.floor(Math.random() * (JITTER_MAX_MS$1 - JITTER_MIN_MS$1) + JITTER_MIN_MS$1), signal);
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Long-running stream wrapper: subscribes via `factory()`, yields every value, and on error or
|
|
82
|
-
* unexpected completion re-subscribes with exponential backoff. Stops only when `signal` aborts.
|
|
83
|
-
* Designed for subscriptions that should stay open across reconnects (e.g. the watcher).
|
|
84
|
-
*/
|
|
85
|
-
async function* asyncKeepalive(factory, signal) {
|
|
86
|
-
let attempt = 0;
|
|
87
|
-
while (!signal?.aborted) try {
|
|
88
|
-
for await (const value of factory()) {
|
|
89
|
-
if (signal?.aborted) return;
|
|
90
|
-
yield value;
|
|
91
|
-
attempt = 0;
|
|
92
|
-
}
|
|
93
|
-
attempt = 0;
|
|
94
|
-
} catch (error) {
|
|
95
|
-
if (signal?.aborted) return;
|
|
96
|
-
await exponentialBackoffWithSignal(attempt, signal);
|
|
97
|
-
attempt++;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
//#endregion
|
|
101
|
-
//#region src/diagnostics.ts
|
|
102
|
-
const channels = {
|
|
103
|
-
start: "@contello/client:start",
|
|
104
|
-
end: "@contello/client:end",
|
|
105
|
-
error: "@contello/client:error",
|
|
106
|
-
/** subscribe to decorate outgoing GraphQL WebSocket messages (e.g. inject traceparent) */
|
|
107
|
-
message: "@contello/client:message"
|
|
108
|
-
};
|
|
109
|
-
const onStart = dc.channel(channels.start);
|
|
110
|
-
const onEnd = dc.channel(channels.end);
|
|
111
|
-
const onError = dc.channel(channels.error);
|
|
112
|
-
const onMessage = dc.channel(channels.message);
|
|
113
|
-
function hasOperationSubscribers() {
|
|
114
|
-
return onStart.hasSubscribers || onEnd.hasSubscribers || onError.hasSubscribers;
|
|
115
|
-
}
|
|
116
|
-
function wrap(name, fn) {
|
|
117
|
-
if (!hasOperationSubscribers()) return fn();
|
|
118
|
-
const start = performance.now();
|
|
119
|
-
onStart.publish({ name });
|
|
120
|
-
let result;
|
|
121
|
-
try {
|
|
122
|
-
result = fn();
|
|
123
|
-
} catch (error) {
|
|
124
|
-
onError.publish({
|
|
125
|
-
name,
|
|
126
|
-
error,
|
|
127
|
-
durationMs: performance.now() - start
|
|
128
|
-
});
|
|
129
|
-
throw error;
|
|
130
|
-
}
|
|
131
|
-
if (result instanceof Promise) {
|
|
132
|
-
const promise = result;
|
|
133
|
-
return (async () => {
|
|
134
|
-
try {
|
|
135
|
-
const r = await promise;
|
|
136
|
-
onEnd.publish({
|
|
137
|
-
name,
|
|
138
|
-
durationMs: performance.now() - start
|
|
139
|
-
});
|
|
140
|
-
return r;
|
|
141
|
-
} catch (error) {
|
|
142
|
-
onError.publish({
|
|
143
|
-
name,
|
|
144
|
-
error,
|
|
145
|
-
durationMs: performance.now() - start
|
|
146
|
-
});
|
|
147
|
-
throw error;
|
|
148
|
-
}
|
|
149
|
-
})();
|
|
150
|
-
}
|
|
151
|
-
onEnd.publish({
|
|
152
|
-
name,
|
|
153
|
-
durationMs: performance.now() - start
|
|
154
|
-
});
|
|
155
|
-
return result;
|
|
156
|
-
}
|
|
157
|
-
function decorateMessage(message) {
|
|
158
|
-
if (!onMessage.hasSubscribers) return message;
|
|
159
|
-
const msg = { message };
|
|
160
|
-
onMessage.publish(msg);
|
|
161
|
-
return msg.message;
|
|
162
|
-
}
|
|
163
|
-
//#endregion
|
|
164
5
|
//#region src/http.ts
|
|
165
6
|
const SAFE_SEGMENT = /^[A-Za-z0-9._-]+$/;
|
|
166
7
|
const FORWARDED_PROXY_HEADERS = /* @__PURE__ */ new Set([
|
|
@@ -364,6 +205,9 @@ var ConnectionPool = class {
|
|
|
364
205
|
}
|
|
365
206
|
};
|
|
366
207
|
//#endregion
|
|
208
|
+
//#region src/telemetry.ts
|
|
209
|
+
const { wrap } = createOperationTelemetry("@contello/client");
|
|
210
|
+
//#endregion
|
|
367
211
|
//#region src/scalars.ts
|
|
368
212
|
const LOCAL_DATE_TIME_KEYS = [
|
|
369
213
|
"year",
|
|
@@ -451,7 +295,10 @@ function transformVariables(value) {
|
|
|
451
295
|
//#region src/rpc.ts
|
|
452
296
|
function buildRpc(operations, subscribe) {
|
|
453
297
|
const rpc = {};
|
|
454
|
-
for (const [name, def] of Object.entries(operations)) rpc[name] = def.kind === "subscription" ? (variables) => subscribe(def.document, transformVariables(variables)) : (variables) =>
|
|
298
|
+
for (const [name, def] of Object.entries(operations)) rpc[name] = def.kind === "subscription" ? (variables) => subscribe(def.document, transformVariables(variables)) : (variables) => {
|
|
299
|
+
const vars = transformVariables(variables);
|
|
300
|
+
return wrap(`rpc:${name}`, () => firstAsync(subscribe(def.document, vars)), graphqlOperationAttributes(def.document, vars));
|
|
301
|
+
};
|
|
455
302
|
return rpc;
|
|
456
303
|
}
|
|
457
304
|
//#endregion
|
|
@@ -487,17 +334,29 @@ function createSources(sources, subscribe) {
|
|
|
487
334
|
}
|
|
488
335
|
function createSourceAccessor(name, def, doc, subscribe) {
|
|
489
336
|
switch (def.__cardinality) {
|
|
490
|
-
case "singleton": return { fetch: () => wrap(`source:${name}`, () => firstAsync(mapAsync(subscribe(doc), (r) => r.source))) };
|
|
491
|
-
case "entity": return { fetch: (vars) =>
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
337
|
+
case "singleton": return { fetch: () => wrap(`source:${name}`, () => firstAsync(mapAsync(subscribe(doc), (r) => r.source)), graphqlOperationAttributes(doc)) };
|
|
338
|
+
case "entity": return { fetch: (vars) => {
|
|
339
|
+
const variables = transformVariables({ ids: vars?.ids });
|
|
340
|
+
return wrap(`source:${name}`, () => concatAsync(mapAsync(subscribe(doc, variables), (r) => r.source)), graphqlOperationAttributes(doc, variables));
|
|
341
|
+
} };
|
|
342
|
+
case "route": return { fetch: (vars) => {
|
|
343
|
+
const variables = transformVariables({
|
|
344
|
+
ids: vars?.ids,
|
|
345
|
+
paths: vars?.paths
|
|
346
|
+
});
|
|
347
|
+
return wrap(`source:${name}`, () => concatAsync(mapAsync(subscribe(doc, variables), (r) => r.source)), graphqlOperationAttributes(doc, variables));
|
|
348
|
+
} };
|
|
349
|
+
case "asset": return { fetch: (vars) => {
|
|
350
|
+
const variables = transformVariables({ ids: vars?.ids });
|
|
351
|
+
return wrap(`source:${name}`, () => concatAsync(mapAsync(subscribe(doc, variables), (r) => r.source)), graphqlOperationAttributes(doc, variables));
|
|
352
|
+
} };
|
|
353
|
+
case "i18nMessage": return { fetch: (vars) => {
|
|
354
|
+
const variables = transformVariables({
|
|
355
|
+
collection: vars.collection,
|
|
356
|
+
ids: vars.ids
|
|
357
|
+
});
|
|
358
|
+
return wrap(`source:${name}`, () => concatAsync(mapAsync(subscribe(doc, variables), (r) => r.source)), graphqlOperationAttributes(doc, variables));
|
|
359
|
+
} };
|
|
501
360
|
}
|
|
502
361
|
}
|
|
503
362
|
//#endregion
|
|
@@ -682,15 +541,6 @@ function upload(url, project, token, data, meta, options) {
|
|
|
682
541
|
});
|
|
683
542
|
}
|
|
684
543
|
//#endregion
|
|
685
|
-
//#region src/utils.ts
|
|
686
|
-
const MAX_DELAY_MS = 3e4;
|
|
687
|
-
const JITTER_MIN_MS = 300;
|
|
688
|
-
const JITTER_MAX_MS = 3e3;
|
|
689
|
-
function wsRetryWait(retries) {
|
|
690
|
-
const delay = Math.min(Math.pow(2, retries) * 1e3, MAX_DELAY_MS) + Math.floor(Math.random() * (JITTER_MAX_MS - JITTER_MIN_MS) + JITTER_MIN_MS);
|
|
691
|
-
return new Promise((resolve) => setTimeout(resolve, delay));
|
|
692
|
-
}
|
|
693
|
-
//#endregion
|
|
694
544
|
//#region src/client.ts
|
|
695
545
|
var ContelloClient = class {
|
|
696
546
|
_pool;
|
|
@@ -734,10 +584,10 @@ var ContelloClient = class {
|
|
|
734
584
|
lazy: false,
|
|
735
585
|
keepAlive: 3e4,
|
|
736
586
|
retryAttempts: Infinity,
|
|
737
|
-
retryWait:
|
|
587
|
+
retryWait: exponentialBackoff,
|
|
738
588
|
shouldRetry: () => true,
|
|
739
589
|
jsonMessageReplacer: (key, value) => {
|
|
740
|
-
if (!key) return
|
|
590
|
+
if (!key) return injectTraceContext(value);
|
|
741
591
|
return value;
|
|
742
592
|
},
|
|
743
593
|
...options.onError && { onNonLazyError: (e) => options.onError(e) },
|
|
@@ -873,140 +723,13 @@ var ContelloClient = class {
|
|
|
873
723
|
return wrap("@contello/client:upload", () => upload(this._url, this._project, this._token, data, meta, options));
|
|
874
724
|
}
|
|
875
725
|
execute(query, variables) {
|
|
876
|
-
return wrap("@contello/client:execute", () => firstAsync(this.subscribe(query, variables)));
|
|
726
|
+
return wrap("@contello/client:execute", () => firstAsync(this.subscribe(query, variables)), graphqlOperationAttributes(query, variables));
|
|
877
727
|
}
|
|
878
728
|
};
|
|
879
729
|
function createContelloClient(options) {
|
|
880
730
|
return new ContelloClient(options);
|
|
881
731
|
}
|
|
882
732
|
//#endregion
|
|
883
|
-
|
|
884
|
-
function createAsyncIterableSubject() {
|
|
885
|
-
const consumers = /* @__PURE__ */ new Set();
|
|
886
|
-
let status = { state: "open" };
|
|
887
|
-
const next = (value) => {
|
|
888
|
-
if (status.state !== "open") return;
|
|
889
|
-
for (const consumer of consumers) if (consumer.kind === "listener") consumer.next?.(value);
|
|
890
|
-
else if (consumer.pending) {
|
|
891
|
-
const p = consumer.pending;
|
|
892
|
-
consumer.pending = void 0;
|
|
893
|
-
p.resolve({
|
|
894
|
-
value,
|
|
895
|
-
done: false
|
|
896
|
-
});
|
|
897
|
-
} else consumer.queue.push(value);
|
|
898
|
-
};
|
|
899
|
-
const error = (err) => {
|
|
900
|
-
if (status.state !== "open") return;
|
|
901
|
-
status = {
|
|
902
|
-
state: "errored",
|
|
903
|
-
err
|
|
904
|
-
};
|
|
905
|
-
for (const consumer of consumers) if (consumer.kind === "listener") consumer.error?.(err);
|
|
906
|
-
else if (consumer.pending) {
|
|
907
|
-
const p = consumer.pending;
|
|
908
|
-
consumer.pending = void 0;
|
|
909
|
-
p.reject(err);
|
|
910
|
-
}
|
|
911
|
-
consumers.clear();
|
|
912
|
-
};
|
|
913
|
-
const complete = () => {
|
|
914
|
-
if (status.state !== "open") return;
|
|
915
|
-
status = { state: "completed" };
|
|
916
|
-
for (const consumer of consumers) if (consumer.kind === "listener") consumer.complete?.();
|
|
917
|
-
else if (consumer.pending) {
|
|
918
|
-
const p = consumer.pending;
|
|
919
|
-
consumer.pending = void 0;
|
|
920
|
-
p.resolve({
|
|
921
|
-
value: void 0,
|
|
922
|
-
done: true
|
|
923
|
-
});
|
|
924
|
-
}
|
|
925
|
-
consumers.clear();
|
|
926
|
-
};
|
|
927
|
-
const subscribe = (observerOrNext) => {
|
|
928
|
-
if (status.state !== "open") return () => {};
|
|
929
|
-
const consumer = {
|
|
930
|
-
kind: "listener",
|
|
931
|
-
...typeof observerOrNext === "function" ? { next: observerOrNext } : observerOrNext
|
|
932
|
-
};
|
|
933
|
-
consumers.add(consumer);
|
|
934
|
-
return () => {
|
|
935
|
-
consumers.delete(consumer);
|
|
936
|
-
};
|
|
937
|
-
};
|
|
938
|
-
return Object.assign({ [Symbol.asyncIterator]() {
|
|
939
|
-
if (status.state === "errored") {
|
|
940
|
-
const err = status.err;
|
|
941
|
-
return {
|
|
942
|
-
next: () => Promise.reject(err),
|
|
943
|
-
return: () => Promise.resolve({
|
|
944
|
-
value: void 0,
|
|
945
|
-
done: true
|
|
946
|
-
})
|
|
947
|
-
};
|
|
948
|
-
}
|
|
949
|
-
if (status.state === "completed") return {
|
|
950
|
-
next: () => Promise.resolve({
|
|
951
|
-
value: void 0,
|
|
952
|
-
done: true
|
|
953
|
-
}),
|
|
954
|
-
return: () => Promise.resolve({
|
|
955
|
-
value: void 0,
|
|
956
|
-
done: true
|
|
957
|
-
})
|
|
958
|
-
};
|
|
959
|
-
const consumer = {
|
|
960
|
-
kind: "iterator",
|
|
961
|
-
queue: []
|
|
962
|
-
};
|
|
963
|
-
consumers.add(consumer);
|
|
964
|
-
return {
|
|
965
|
-
next() {
|
|
966
|
-
if (consumer.queue.length > 0) return Promise.resolve({
|
|
967
|
-
value: consumer.queue.shift(),
|
|
968
|
-
done: false
|
|
969
|
-
});
|
|
970
|
-
if (status.state === "errored") return Promise.reject(status.err);
|
|
971
|
-
if (status.state === "completed") return Promise.resolve({
|
|
972
|
-
value: void 0,
|
|
973
|
-
done: true
|
|
974
|
-
});
|
|
975
|
-
return new Promise((resolve, reject) => {
|
|
976
|
-
consumer.pending = {
|
|
977
|
-
resolve,
|
|
978
|
-
reject
|
|
979
|
-
};
|
|
980
|
-
});
|
|
981
|
-
},
|
|
982
|
-
return() {
|
|
983
|
-
consumers.delete(consumer);
|
|
984
|
-
if (consumer.pending) {
|
|
985
|
-
const p = consumer.pending;
|
|
986
|
-
consumer.pending = void 0;
|
|
987
|
-
p.resolve({
|
|
988
|
-
value: void 0,
|
|
989
|
-
done: true
|
|
990
|
-
});
|
|
991
|
-
}
|
|
992
|
-
return Promise.resolve({
|
|
993
|
-
value: void 0,
|
|
994
|
-
done: true
|
|
995
|
-
});
|
|
996
|
-
},
|
|
997
|
-
throw(err) {
|
|
998
|
-
consumers.delete(consumer);
|
|
999
|
-
return Promise.reject(err);
|
|
1000
|
-
}
|
|
1001
|
-
};
|
|
1002
|
-
} }, {
|
|
1003
|
-
next,
|
|
1004
|
-
error,
|
|
1005
|
-
complete,
|
|
1006
|
-
subscribe
|
|
1007
|
-
});
|
|
1008
|
-
}
|
|
1009
|
-
//#endregion
|
|
1010
|
-
export { ContelloClient, asyncKeepalive, collectAsync, createAsyncIterableSubject, createContelloClient, createSourceSubscription, exponentialBackoff, filterAsync, firstAsync, mapAsync, runWithBackoff };
|
|
733
|
+
export { ContelloClient, createContelloClient, createSourceSubscription };
|
|
1011
734
|
|
|
1012
735
|
//# sourceMappingURL=index.js.map
|