@contello/client 1.2.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +1 -1
- package/dist/{generate-PGYNYGNX.js → generate-FPYYN7UV.js} +434 -37
- package/dist/index.cjs +530 -23
- package/dist/index.d.cts +138 -10
- package/dist/index.d.ts +138 -10
- package/dist/index.js +520 -22
- package/package.json +1 -7
package/dist/index.cjs
CHANGED
|
@@ -31,13 +31,120 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
ContelloClient: () => ContelloClient,
|
|
34
|
-
|
|
34
|
+
asyncKeepalive: () => asyncKeepalive,
|
|
35
|
+
collectAsync: () => collectAsync,
|
|
36
|
+
createAsyncIterableSubject: () => createAsyncIterableSubject,
|
|
37
|
+
createContelloClient: () => createContelloClient,
|
|
38
|
+
createSourceSubscription: () => createSourceSubscription,
|
|
39
|
+
exponentialBackoff: () => exponentialBackoff,
|
|
40
|
+
filterAsync: () => filterAsync,
|
|
41
|
+
firstAsync: () => firstAsync,
|
|
42
|
+
mapAsync: () => mapAsync,
|
|
43
|
+
runWithBackoff: () => runWithBackoff
|
|
35
44
|
});
|
|
36
45
|
module.exports = __toCommonJS(index_exports);
|
|
37
46
|
|
|
38
47
|
// src/client.ts
|
|
39
48
|
var import_graphql_ws = require("graphql-ws");
|
|
40
|
-
|
|
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;
|
|
54
|
+
async function firstAsync(iter) {
|
|
55
|
+
const it = iter[Symbol.asyncIterator]();
|
|
56
|
+
try {
|
|
57
|
+
const result = await it.next();
|
|
58
|
+
if (result.done) {
|
|
59
|
+
throw new Error("firstAsync: async iterable completed without yielding a value");
|
|
60
|
+
}
|
|
61
|
+
return result.value;
|
|
62
|
+
} finally {
|
|
63
|
+
await it.return?.();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async function* mapAsync(iter, fn) {
|
|
67
|
+
for await (const value of iter) {
|
|
68
|
+
yield fn(value);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function* filterAsync(iter, predicate) {
|
|
72
|
+
for await (const value of iter) {
|
|
73
|
+
if (predicate(value)) {
|
|
74
|
+
yield value;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async function collectAsync(iter) {
|
|
79
|
+
const out = [];
|
|
80
|
+
for await (const chunk of iter) {
|
|
81
|
+
out.push(...chunk);
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
function exponentialBackoff(attempt) {
|
|
86
|
+
const base = Math.min(Math.pow(2, attempt) * 1e3, MAX_BACKOFF_MS);
|
|
87
|
+
const jitter = Math.floor(Math.random() * (JITTER_MAX_MS - JITTER_MIN_MS) + JITTER_MIN_MS);
|
|
88
|
+
return delay(base + jitter);
|
|
89
|
+
}
|
|
90
|
+
function delay(ms, signal) {
|
|
91
|
+
if (signal?.aborted) {
|
|
92
|
+
return Promise.reject(signal.reason);
|
|
93
|
+
}
|
|
94
|
+
return new Promise((resolve, reject) => {
|
|
95
|
+
const timer = setTimeout(() => {
|
|
96
|
+
signal?.removeEventListener("abort", onAbort);
|
|
97
|
+
resolve();
|
|
98
|
+
}, ms);
|
|
99
|
+
const onAbort = () => {
|
|
100
|
+
clearTimeout(timer);
|
|
101
|
+
reject(signal.reason);
|
|
102
|
+
};
|
|
103
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
async function runWithBackoff(fn, signal) {
|
|
107
|
+
let attempt = 0;
|
|
108
|
+
for (; ; ) {
|
|
109
|
+
if (signal?.aborted) {
|
|
110
|
+
throw signal.reason;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
await fn();
|
|
114
|
+
return;
|
|
115
|
+
} catch {
|
|
116
|
+
await exponentialBackoffWithSignal(attempt, signal);
|
|
117
|
+
attempt++;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function exponentialBackoffWithSignal(attempt, signal) {
|
|
122
|
+
const base = Math.min(Math.pow(2, attempt) * 1e3, MAX_BACKOFF_MS);
|
|
123
|
+
const jitter = Math.floor(Math.random() * (JITTER_MAX_MS - JITTER_MIN_MS) + JITTER_MIN_MS);
|
|
124
|
+
return delay(base + jitter, signal);
|
|
125
|
+
}
|
|
126
|
+
async function* asyncKeepalive(factory, signal) {
|
|
127
|
+
let attempt = 0;
|
|
128
|
+
while (!signal?.aborted) {
|
|
129
|
+
try {
|
|
130
|
+
for await (const value of factory()) {
|
|
131
|
+
if (signal?.aborted) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
yield value;
|
|
135
|
+
attempt = 0;
|
|
136
|
+
}
|
|
137
|
+
attempt = 0;
|
|
138
|
+
} catch (err) {
|
|
139
|
+
if (signal?.aborted) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
void err;
|
|
143
|
+
await exponentialBackoffWithSignal(attempt, signal);
|
|
144
|
+
attempt++;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
41
148
|
|
|
42
149
|
// src/diagnostics.ts
|
|
43
150
|
var import_node_diagnostics_channel = __toESM(require("diagnostics_channel"), 1);
|
|
@@ -268,12 +375,92 @@ var ConnectionPool = class {
|
|
|
268
375
|
}
|
|
269
376
|
};
|
|
270
377
|
|
|
271
|
-
// src/
|
|
272
|
-
var
|
|
378
|
+
// src/scalars.ts
|
|
379
|
+
var LOCAL_DATE_TIME_KEYS = ["year", "month", "day", "hour", "minute", "second"];
|
|
380
|
+
var LOCAL_DATE_KEYS = ["year", "month", "day"];
|
|
381
|
+
var DATE_TIME_REGEX = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/;
|
|
382
|
+
var DATE_REGEX = /^(\d{4})-(\d{2})-(\d{2})/;
|
|
383
|
+
function pad2(n) {
|
|
384
|
+
return n.toString().padStart(2, "0");
|
|
385
|
+
}
|
|
386
|
+
function pad4(n) {
|
|
387
|
+
return n.toString().padStart(4, "0");
|
|
388
|
+
}
|
|
389
|
+
function hasExactKeys(value, keys) {
|
|
390
|
+
const ownKeys = Object.keys(value);
|
|
391
|
+
if (ownKeys.length !== keys.length) {
|
|
392
|
+
return false;
|
|
393
|
+
}
|
|
394
|
+
for (const key of keys) {
|
|
395
|
+
if (!Object.prototype.hasOwnProperty.call(value, key)) {
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
function allNumbers(value, keys) {
|
|
402
|
+
for (const key of keys) {
|
|
403
|
+
if (typeof value[key] !== "number") {
|
|
404
|
+
return false;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
function isLocalDateTime(value) {
|
|
410
|
+
return !!value && typeof value === "object" && !Array.isArray(value) && hasExactKeys(value, LOCAL_DATE_TIME_KEYS) && allNumbers(value, LOCAL_DATE_TIME_KEYS);
|
|
411
|
+
}
|
|
412
|
+
function isLocalDate(value) {
|
|
413
|
+
return !!value && typeof value === "object" && !Array.isArray(value) && hasExactKeys(value, LOCAL_DATE_KEYS) && allNumbers(value, LOCAL_DATE_KEYS);
|
|
414
|
+
}
|
|
415
|
+
function decodeLocalDateTime(value) {
|
|
416
|
+
const match = DATE_TIME_REGEX.exec(value);
|
|
417
|
+
if (!match) {
|
|
418
|
+
throw new Error(`@contello/client: cannot decode LocalDateTime from "${value}"`);
|
|
419
|
+
}
|
|
420
|
+
return {
|
|
421
|
+
year: Number(match[1]),
|
|
422
|
+
month: Number(match[2]),
|
|
423
|
+
day: Number(match[3]),
|
|
424
|
+
hour: Number(match[4]),
|
|
425
|
+
minute: Number(match[5]),
|
|
426
|
+
second: Number(match[6])
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
function decodeLocalDate(value) {
|
|
430
|
+
const match = DATE_REGEX.exec(value);
|
|
431
|
+
if (!match) {
|
|
432
|
+
throw new Error(`@contello/client: cannot decode LocalDate from "${value}"`);
|
|
433
|
+
}
|
|
434
|
+
return {
|
|
435
|
+
year: Number(match[1]),
|
|
436
|
+
month: Number(match[2]),
|
|
437
|
+
day: Number(match[3])
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
function encodeLocalDateTime(value) {
|
|
441
|
+
return `${pad4(value.year)}-${pad2(value.month)}-${pad2(value.day)}T${pad2(value.hour)}:${pad2(value.minute)}:${pad2(value.second)}Z`;
|
|
442
|
+
}
|
|
443
|
+
function encodeLocalDate(value) {
|
|
444
|
+
return `${pad4(value.year)}-${pad2(value.month)}-${pad2(value.day)}T00:00:00Z`;
|
|
445
|
+
}
|
|
273
446
|
|
|
274
447
|
// src/transform-response.ts
|
|
275
448
|
var FLAT_PREFIX = "_flat_";
|
|
449
|
+
var LDT_PREFIX = "_ldt_";
|
|
450
|
+
var LD_PREFIX = "_ld_";
|
|
276
451
|
var MODEL_SUFFIXES = ["Entity", "Component"];
|
|
452
|
+
function decodeValue(value, decoder) {
|
|
453
|
+
if (value === null || value === void 0) {
|
|
454
|
+
return value;
|
|
455
|
+
}
|
|
456
|
+
if (Array.isArray(value)) {
|
|
457
|
+
return value.map((item) => decodeValue(item, decoder));
|
|
458
|
+
}
|
|
459
|
+
if (typeof value === "string") {
|
|
460
|
+
return decoder(value);
|
|
461
|
+
}
|
|
462
|
+
return value;
|
|
463
|
+
}
|
|
277
464
|
function deriveModel(typename) {
|
|
278
465
|
for (const suffix of MODEL_SUFFIXES) {
|
|
279
466
|
if (typename.endsWith(suffix)) {
|
|
@@ -318,6 +505,17 @@ function transformResponse(data) {
|
|
|
318
505
|
obj[fieldKey] = resolveRefs(refArray, flatMap);
|
|
319
506
|
delete obj[flatKey];
|
|
320
507
|
}
|
|
508
|
+
for (const key of Object.keys(obj)) {
|
|
509
|
+
if (key.startsWith(LDT_PREFIX)) {
|
|
510
|
+
const original = key.slice(LDT_PREFIX.length);
|
|
511
|
+
obj[original] = decodeValue(obj[key], decodeLocalDateTime);
|
|
512
|
+
delete obj[key];
|
|
513
|
+
} else if (key.startsWith(LD_PREFIX)) {
|
|
514
|
+
const original = key.slice(LD_PREFIX.length);
|
|
515
|
+
obj[original] = decodeValue(obj[key], decodeLocalDate);
|
|
516
|
+
delete obj[key];
|
|
517
|
+
}
|
|
518
|
+
}
|
|
321
519
|
for (const key of Object.keys(obj)) {
|
|
322
520
|
const value = obj[key];
|
|
323
521
|
if (value && typeof value === "object") {
|
|
@@ -363,22 +561,129 @@ function resolveNestedRefs(value, flatMap) {
|
|
|
363
561
|
}
|
|
364
562
|
}
|
|
365
563
|
|
|
564
|
+
// src/transform-variables.ts
|
|
565
|
+
function transformVariables(value) {
|
|
566
|
+
if (value === null || value === void 0) {
|
|
567
|
+
return value;
|
|
568
|
+
}
|
|
569
|
+
if (Array.isArray(value)) {
|
|
570
|
+
return value.map((item) => transformVariables(item));
|
|
571
|
+
}
|
|
572
|
+
if (typeof value !== "object") {
|
|
573
|
+
return value;
|
|
574
|
+
}
|
|
575
|
+
if (isLocalDateTime(value)) {
|
|
576
|
+
return encodeLocalDateTime(value);
|
|
577
|
+
}
|
|
578
|
+
if (isLocalDate(value)) {
|
|
579
|
+
return encodeLocalDate(value);
|
|
580
|
+
}
|
|
581
|
+
const obj = value;
|
|
582
|
+
const out = {};
|
|
583
|
+
for (const key of Object.keys(obj)) {
|
|
584
|
+
out[key] = transformVariables(obj[key]);
|
|
585
|
+
}
|
|
586
|
+
return out;
|
|
587
|
+
}
|
|
588
|
+
|
|
366
589
|
// src/rpc.ts
|
|
367
|
-
function buildRpc(operations,
|
|
590
|
+
function buildRpc(operations, subscribe) {
|
|
368
591
|
const rpc = {};
|
|
369
592
|
for (const [name, def] of Object.entries(operations)) {
|
|
370
593
|
if (def.kind === "subscription") {
|
|
371
|
-
rpc[name] = (variables) =>
|
|
594
|
+
rpc[name] = (variables) => mapAsync(subscribe(def.document, transformVariables(variables)), transformResponse);
|
|
372
595
|
} else {
|
|
373
596
|
rpc[name] = (variables) => wrap(
|
|
374
597
|
`rpc:${name}`,
|
|
375
|
-
() => (
|
|
598
|
+
() => firstAsync(mapAsync(subscribe(def.document, transformVariables(variables)), transformResponse))
|
|
376
599
|
);
|
|
377
600
|
}
|
|
378
601
|
}
|
|
379
602
|
return rpc;
|
|
380
603
|
}
|
|
381
604
|
|
|
605
|
+
// src/source-subscription.ts
|
|
606
|
+
var cache = /* @__PURE__ */ new WeakMap();
|
|
607
|
+
function createSelection(source) {
|
|
608
|
+
const spread = `{ ...${source.fragment} }`;
|
|
609
|
+
switch (source.__cardinality) {
|
|
610
|
+
case "singleton":
|
|
611
|
+
return `subscription { source: ${source.subscription} ${spread} }`;
|
|
612
|
+
case "entity":
|
|
613
|
+
return `subscription($ids: [ID!]) { source: ${source.subscription}(request: { filter: { ids: $ids } }) ${spread} }`;
|
|
614
|
+
case "route":
|
|
615
|
+
case "asset":
|
|
616
|
+
return `subscription { source: ${source.subscription} ${spread} }`;
|
|
617
|
+
case "i18nMessage":
|
|
618
|
+
return `subscription($collection: String!) { source: ${source.subscription}(collectionReferenceName: $collection) ${spread} }`;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
function createSourceSubscription(source) {
|
|
622
|
+
const cached = cache.get(source);
|
|
623
|
+
if (cached !== void 0) {
|
|
624
|
+
return cached;
|
|
625
|
+
}
|
|
626
|
+
const doc = `${source.document}
|
|
627
|
+
${createSelection(source)}`;
|
|
628
|
+
cache.set(source, doc);
|
|
629
|
+
return doc;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// src/sources.ts
|
|
633
|
+
function createSources(sources, subscribe) {
|
|
634
|
+
const out = {};
|
|
635
|
+
for (const [name, source] of Object.entries(sources)) {
|
|
636
|
+
const def = source;
|
|
637
|
+
const doc = createSourceSubscription(def);
|
|
638
|
+
switch (def.__cardinality) {
|
|
639
|
+
case "singleton":
|
|
640
|
+
out[name] = {
|
|
641
|
+
fetch: () => wrap(
|
|
642
|
+
`source:${name}`,
|
|
643
|
+
() => firstAsync(mapAsync(subscribe(doc), (r) => transformResponse(r).source))
|
|
644
|
+
)
|
|
645
|
+
};
|
|
646
|
+
break;
|
|
647
|
+
case "entity":
|
|
648
|
+
out[name] = {
|
|
649
|
+
fetch: (vars) => wrap(
|
|
650
|
+
`source:${name}`,
|
|
651
|
+
() => firstAsync(
|
|
652
|
+
mapAsync(
|
|
653
|
+
subscribe(doc, transformVariables({ ids: vars?.ids })),
|
|
654
|
+
(r) => transformResponse(r).source
|
|
655
|
+
)
|
|
656
|
+
)
|
|
657
|
+
)
|
|
658
|
+
};
|
|
659
|
+
break;
|
|
660
|
+
case "route":
|
|
661
|
+
case "asset":
|
|
662
|
+
out[name] = {
|
|
663
|
+
fetch: () => wrap(
|
|
664
|
+
`source:${name}`,
|
|
665
|
+
() => firstAsync(mapAsync(subscribe(doc), (r) => transformResponse(r).source))
|
|
666
|
+
)
|
|
667
|
+
};
|
|
668
|
+
break;
|
|
669
|
+
case "i18nMessage":
|
|
670
|
+
out[name] = {
|
|
671
|
+
fetch: (vars) => wrap(
|
|
672
|
+
`source:${name}`,
|
|
673
|
+
() => firstAsync(
|
|
674
|
+
mapAsync(
|
|
675
|
+
subscribe(doc, { collection: vars.collection }),
|
|
676
|
+
(r) => transformResponse(r).source
|
|
677
|
+
)
|
|
678
|
+
)
|
|
679
|
+
)
|
|
680
|
+
};
|
|
681
|
+
break;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
return out;
|
|
685
|
+
}
|
|
686
|
+
|
|
382
687
|
// src/upload.ts
|
|
383
688
|
var DEFAULT_CHUNK_SIZE = 4 * 1024 * 1024;
|
|
384
689
|
function isUint8Array(data) {
|
|
@@ -464,23 +769,24 @@ function upload(url, project, token, data, meta, options) {
|
|
|
464
769
|
|
|
465
770
|
// src/utils.ts
|
|
466
771
|
var MAX_DELAY_MS = 3e4;
|
|
467
|
-
var
|
|
468
|
-
var
|
|
469
|
-
function
|
|
470
|
-
const
|
|
471
|
-
return new Promise((resolve) => setTimeout(resolve,
|
|
772
|
+
var JITTER_MIN_MS2 = 300;
|
|
773
|
+
var JITTER_MAX_MS2 = 3e3;
|
|
774
|
+
function wsRetryWait(retries) {
|
|
775
|
+
const delay2 = Math.min(Math.pow(2, retries) * 1e3, MAX_DELAY_MS) + Math.floor(Math.random() * (JITTER_MAX_MS2 - JITTER_MIN_MS2) + JITTER_MIN_MS2);
|
|
776
|
+
return new Promise((resolve) => setTimeout(resolve, delay2));
|
|
472
777
|
}
|
|
473
778
|
|
|
474
779
|
// src/client.ts
|
|
475
780
|
var ContelloClient = class {
|
|
476
781
|
_pool;
|
|
477
782
|
_rpc;
|
|
783
|
+
_sources;
|
|
478
784
|
_url;
|
|
479
785
|
_project;
|
|
480
786
|
_token;
|
|
481
787
|
_agent;
|
|
482
788
|
constructor(options) {
|
|
483
|
-
const { url, project, token,
|
|
789
|
+
const { url, project, token, schema, connections = 1, connectionEvents } = options;
|
|
484
790
|
this._url = url;
|
|
485
791
|
this._project = project;
|
|
486
792
|
this._token = token;
|
|
@@ -512,7 +818,7 @@ var ContelloClient = class {
|
|
|
512
818
|
lazy: false,
|
|
513
819
|
keepAlive: 3e4,
|
|
514
820
|
retryAttempts: Infinity,
|
|
515
|
-
retryWait:
|
|
821
|
+
retryWait: wsRetryWait,
|
|
516
822
|
shouldRetry: () => true,
|
|
517
823
|
jsonMessageReplacer: (key, value) => {
|
|
518
824
|
if (!key) {
|
|
@@ -542,14 +848,21 @@ var ContelloClient = class {
|
|
|
542
848
|
}
|
|
543
849
|
});
|
|
544
850
|
}, connections);
|
|
545
|
-
this._rpc = operations ? buildRpc(operations, this) : void 0;
|
|
851
|
+
this._rpc = schema?.operations ? buildRpc(schema.operations, (q, v) => this.subscribe(q, v)) : void 0;
|
|
852
|
+
this._sources = schema?.sources ? createSources(schema.sources, (q, v) => this.subscribe(q, v)) : void 0;
|
|
546
853
|
}
|
|
547
854
|
get rpc() {
|
|
548
855
|
if (!this._rpc) {
|
|
549
|
-
throw new Error("@contello/client: .rpc accessed without operations");
|
|
856
|
+
throw new Error("@contello/client: .rpc accessed without a schema containing operations");
|
|
550
857
|
}
|
|
551
858
|
return this._rpc;
|
|
552
859
|
}
|
|
860
|
+
get sources() {
|
|
861
|
+
if (!this._sources) {
|
|
862
|
+
throw new Error("@contello/client: .sources accessed without a schema containing sources");
|
|
863
|
+
}
|
|
864
|
+
return this._sources;
|
|
865
|
+
}
|
|
553
866
|
async init() {
|
|
554
867
|
await this._pool.connect();
|
|
555
868
|
}
|
|
@@ -558,13 +871,83 @@ var ContelloClient = class {
|
|
|
558
871
|
await this._agent.close();
|
|
559
872
|
}
|
|
560
873
|
async ping() {
|
|
561
|
-
return ping((query) => (
|
|
874
|
+
return ping((query) => firstAsync(this.subscribe(query)));
|
|
562
875
|
}
|
|
876
|
+
/**
|
|
877
|
+
* Open a subscription on the websocket. Each `[Symbol.asyncIterator]()` call (the implicit one
|
|
878
|
+
* from `for await`, or via `rxjs.from(this)`) starts a fresh server subscription and tears it
|
|
879
|
+
* down when the iterator is returned (`break`, error, or explicit stop).
|
|
880
|
+
*/
|
|
563
881
|
subscribe(query, variables) {
|
|
564
|
-
const
|
|
565
|
-
return
|
|
566
|
-
|
|
567
|
-
|
|
882
|
+
const pool = this._pool;
|
|
883
|
+
return {
|
|
884
|
+
[Symbol.asyncIterator]() {
|
|
885
|
+
const wsClient = pool.get();
|
|
886
|
+
const queue = [];
|
|
887
|
+
let pending;
|
|
888
|
+
let done = false;
|
|
889
|
+
let error;
|
|
890
|
+
const unsubscribe = wsClient.subscribe(
|
|
891
|
+
{ query, variables },
|
|
892
|
+
{
|
|
893
|
+
next(msg) {
|
|
894
|
+
const value = msg.data;
|
|
895
|
+
if (value === void 0 || value === null) {
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
898
|
+
if (pending) {
|
|
899
|
+
const p = pending;
|
|
900
|
+
pending = void 0;
|
|
901
|
+
p.resolve({ value, done: false });
|
|
902
|
+
} else {
|
|
903
|
+
queue.push(value);
|
|
904
|
+
}
|
|
905
|
+
},
|
|
906
|
+
error(err) {
|
|
907
|
+
done = true;
|
|
908
|
+
error = err;
|
|
909
|
+
if (pending) {
|
|
910
|
+
const p = pending;
|
|
911
|
+
pending = void 0;
|
|
912
|
+
p.reject(err);
|
|
913
|
+
}
|
|
914
|
+
},
|
|
915
|
+
complete() {
|
|
916
|
+
done = true;
|
|
917
|
+
if (pending) {
|
|
918
|
+
const p = pending;
|
|
919
|
+
pending = void 0;
|
|
920
|
+
p.resolve({ value: void 0, done: true });
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
);
|
|
925
|
+
return {
|
|
926
|
+
next() {
|
|
927
|
+
if (queue.length > 0) {
|
|
928
|
+
return Promise.resolve({ value: queue.shift(), done: false });
|
|
929
|
+
}
|
|
930
|
+
if (error !== void 0) {
|
|
931
|
+
return Promise.reject(error);
|
|
932
|
+
}
|
|
933
|
+
if (done) {
|
|
934
|
+
return Promise.resolve({ value: void 0, done: true });
|
|
935
|
+
}
|
|
936
|
+
return new Promise((resolve, reject) => {
|
|
937
|
+
pending = { resolve, reject };
|
|
938
|
+
});
|
|
939
|
+
},
|
|
940
|
+
return() {
|
|
941
|
+
unsubscribe();
|
|
942
|
+
return Promise.resolve({ value: void 0, done: true });
|
|
943
|
+
},
|
|
944
|
+
throw(err) {
|
|
945
|
+
unsubscribe();
|
|
946
|
+
return Promise.reject(err);
|
|
947
|
+
}
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
};
|
|
568
951
|
}
|
|
569
952
|
download(fileId) {
|
|
570
953
|
return wrap("@contello/client:download", () => downloadFile(this._agent, this._url, this._token, fileId));
|
|
@@ -579,14 +962,138 @@ var ContelloClient = class {
|
|
|
579
962
|
);
|
|
580
963
|
}
|
|
581
964
|
execute(query, variables) {
|
|
582
|
-
return wrap("@contello/client:execute", () => (
|
|
965
|
+
return wrap("@contello/client:execute", () => firstAsync(this.subscribe(query, variables)));
|
|
583
966
|
}
|
|
584
967
|
};
|
|
585
968
|
function createContelloClient(options) {
|
|
586
969
|
return new ContelloClient(options);
|
|
587
970
|
}
|
|
971
|
+
|
|
972
|
+
// src/async-iterable-subject.ts
|
|
973
|
+
function createAsyncIterableSubject() {
|
|
974
|
+
const consumers = /* @__PURE__ */ new Set();
|
|
975
|
+
let status = { state: "open" };
|
|
976
|
+
const next = (value) => {
|
|
977
|
+
if (status.state !== "open") {
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
for (const consumer of consumers) {
|
|
981
|
+
if (consumer.kind === "listener") {
|
|
982
|
+
consumer.next?.(value);
|
|
983
|
+
} else if (consumer.pending) {
|
|
984
|
+
const p = consumer.pending;
|
|
985
|
+
consumer.pending = void 0;
|
|
986
|
+
p.resolve({ value, done: false });
|
|
987
|
+
} else {
|
|
988
|
+
consumer.queue.push(value);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
};
|
|
992
|
+
const error = (err) => {
|
|
993
|
+
if (status.state !== "open") {
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
status = { state: "errored", err };
|
|
997
|
+
for (const consumer of consumers) {
|
|
998
|
+
if (consumer.kind === "listener") {
|
|
999
|
+
consumer.error?.(err);
|
|
1000
|
+
} else if (consumer.pending) {
|
|
1001
|
+
const p = consumer.pending;
|
|
1002
|
+
consumer.pending = void 0;
|
|
1003
|
+
p.reject(err);
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
consumers.clear();
|
|
1007
|
+
};
|
|
1008
|
+
const complete = () => {
|
|
1009
|
+
if (status.state !== "open") {
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
status = { state: "completed" };
|
|
1013
|
+
for (const consumer of consumers) {
|
|
1014
|
+
if (consumer.kind === "listener") {
|
|
1015
|
+
consumer.complete?.();
|
|
1016
|
+
} else if (consumer.pending) {
|
|
1017
|
+
const p = consumer.pending;
|
|
1018
|
+
consumer.pending = void 0;
|
|
1019
|
+
p.resolve({ value: void 0, done: true });
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
consumers.clear();
|
|
1023
|
+
};
|
|
1024
|
+
const subscribe = (observerOrNext) => {
|
|
1025
|
+
if (status.state !== "open") {
|
|
1026
|
+
return () => {
|
|
1027
|
+
};
|
|
1028
|
+
}
|
|
1029
|
+
const observer = typeof observerOrNext === "function" ? { next: observerOrNext } : observerOrNext;
|
|
1030
|
+
const consumer = { kind: "listener", ...observer };
|
|
1031
|
+
consumers.add(consumer);
|
|
1032
|
+
return () => {
|
|
1033
|
+
consumers.delete(consumer);
|
|
1034
|
+
};
|
|
1035
|
+
};
|
|
1036
|
+
const iterable = {
|
|
1037
|
+
[Symbol.asyncIterator]() {
|
|
1038
|
+
if (status.state === "errored") {
|
|
1039
|
+
const err = status.err;
|
|
1040
|
+
return {
|
|
1041
|
+
next: () => Promise.reject(err),
|
|
1042
|
+
return: () => Promise.resolve({ value: void 0, done: true })
|
|
1043
|
+
};
|
|
1044
|
+
}
|
|
1045
|
+
if (status.state === "completed") {
|
|
1046
|
+
return {
|
|
1047
|
+
next: () => Promise.resolve({ value: void 0, done: true }),
|
|
1048
|
+
return: () => Promise.resolve({ value: void 0, done: true })
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
const consumer = { kind: "iterator", queue: [] };
|
|
1052
|
+
consumers.add(consumer);
|
|
1053
|
+
return {
|
|
1054
|
+
next() {
|
|
1055
|
+
if (consumer.queue.length > 0) {
|
|
1056
|
+
return Promise.resolve({ value: consumer.queue.shift(), done: false });
|
|
1057
|
+
}
|
|
1058
|
+
if (status.state === "errored") {
|
|
1059
|
+
return Promise.reject(status.err);
|
|
1060
|
+
}
|
|
1061
|
+
if (status.state === "completed") {
|
|
1062
|
+
return Promise.resolve({ value: void 0, done: true });
|
|
1063
|
+
}
|
|
1064
|
+
return new Promise((resolve, reject) => {
|
|
1065
|
+
consumer.pending = { resolve, reject };
|
|
1066
|
+
});
|
|
1067
|
+
},
|
|
1068
|
+
return() {
|
|
1069
|
+
consumers.delete(consumer);
|
|
1070
|
+
if (consumer.pending) {
|
|
1071
|
+
const p = consumer.pending;
|
|
1072
|
+
consumer.pending = void 0;
|
|
1073
|
+
p.resolve({ value: void 0, done: true });
|
|
1074
|
+
}
|
|
1075
|
+
return Promise.resolve({ value: void 0, done: true });
|
|
1076
|
+
},
|
|
1077
|
+
throw(err) {
|
|
1078
|
+
consumers.delete(consumer);
|
|
1079
|
+
return Promise.reject(err);
|
|
1080
|
+
}
|
|
1081
|
+
};
|
|
1082
|
+
}
|
|
1083
|
+
};
|
|
1084
|
+
return Object.assign(iterable, { next, error, complete, subscribe });
|
|
1085
|
+
}
|
|
588
1086
|
// Annotate the CommonJS export names for ESM import in node:
|
|
589
1087
|
0 && (module.exports = {
|
|
590
1088
|
ContelloClient,
|
|
591
|
-
|
|
1089
|
+
asyncKeepalive,
|
|
1090
|
+
collectAsync,
|
|
1091
|
+
createAsyncIterableSubject,
|
|
1092
|
+
createContelloClient,
|
|
1093
|
+
createSourceSubscription,
|
|
1094
|
+
exponentialBackoff,
|
|
1095
|
+
filterAsync,
|
|
1096
|
+
firstAsync,
|
|
1097
|
+
mapAsync,
|
|
1098
|
+
runWithBackoff
|
|
592
1099
|
});
|