@plumile/backoffice-react 0.1.157 → 0.1.159

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.
@@ -1,58 +1,60 @@
1
1
  import { isDevEnv as e } from "./envHelpers.js";
2
2
  import { Environment as t, Network as n, Observable as r, RecordSource as i, Store as a } from "relay-runtime";
3
- import { createClient as o } from "graphql-ws";
4
3
  //#region src/relay/environment.ts
5
- var s = "/api/graphql", c = "/api/ws", l, u, d, f, p = {
4
+ var o = "/api/graphql", s = "/api/ws", c, l, u, d, f = "graphql-transport-ws", p = 5e3, m = 1e4, h = 10, g = 500, _ = {
6
5
  generation: 0,
7
6
  reason: null,
8
7
  status: "idle"
9
- }, m = /* @__PURE__ */ new Set();
10
- function h() {}
11
- var g = h;
12
- e() && (g = (e) => {
8
+ }, v = /* @__PURE__ */ new Set();
9
+ function y() {}
10
+ var b = y;
11
+ e() && (b = (e) => {
13
12
  console.log("relayFieldLogger: ", e);
14
13
  });
15
- var _ = (e) => {
14
+ var x = (e) => {
16
15
  if (typeof e != "object" || !e) return null;
17
16
  let { id: t } = e;
18
17
  return typeof t != "string" || t.trim() === "" ? null : t;
19
- }, v = (e, t) => f?.(e, t) ?? _(e, t);
20
- function y(e, t) {
21
- p = {
22
- generation: p.generation + 1,
18
+ }, S = (e, t) => d?.(e, t) ?? x(e, t);
19
+ function C(e, t) {
20
+ _ = {
21
+ generation: _.generation + 1,
23
22
  reason: t,
24
23
  status: e
25
24
  };
26
- for (let e of m) e();
25
+ for (let e of v) e();
27
26
  }
28
- function b() {
29
- if (u != null) {
27
+ function w() {
28
+ if (l != null) {
30
29
  try {
31
- let e = u.dispose();
32
- e instanceof Promise && e.catch(() => {});
30
+ l.dispose();
33
31
  } catch {}
34
- u = void 0;
32
+ l = void 0;
35
33
  }
36
34
  }
37
- function x() {
38
- return p;
35
+ function T() {
36
+ return _;
39
37
  }
40
- function S(e) {
41
- return m.add(e), () => {
42
- m.delete(e);
38
+ function E(e) {
39
+ return v.add(e), () => {
40
+ v.delete(e);
43
41
  };
44
42
  }
45
- function C(e = {}) {
46
- b(), y("reconnecting", e.reason ?? "manual_reconnect");
43
+ function D(e = {}) {
44
+ w(), C("reconnecting", e.reason ?? "manual_reconnect");
45
+ }
46
+ async function O(e = {}) {
47
+ let t = l;
48
+ t?.hasActiveConnection() && (await t.refreshAuthentication() || D({ reason: e.reason ?? "auth_refresh_fallback" }));
47
49
  }
48
- function w(e = {}) {
50
+ function k(e = {}) {
49
51
  let { httpUrl: t, wsUrl: n, logEvents: r, getDataId: i, getAuthHeaders: a } = e;
50
- typeof t == "string" && t !== "" && (s = t), typeof n == "string" && n !== "" && (c = n), r != null && (g = r ? (e) => {
52
+ typeof t == "string" && t !== "" && (o = t), typeof n == "string" && n !== "" && (s = n), r != null && (b = r ? (e) => {
51
53
  console.log("relayFieldLogger: ", e);
52
- } : h), f = i, d = a, b(), l = void 0, y("configured", "configure");
54
+ } : y), d = i, u = a, w(), c = void 0, C("configured", "configure");
53
55
  }
54
- function T() {
55
- let e = c;
56
+ function A() {
57
+ let e = s;
56
58
  if (e.startsWith("ws")) return e;
57
59
  if (e.startsWith("http")) return e.replace(/^http/, "ws");
58
60
  if (e.startsWith("/")) {
@@ -62,53 +64,236 @@ function T() {
62
64
  }
63
65
  return e;
64
66
  }
65
- function E() {
66
- if (u != null) return u;
67
- if (typeof window > "u") throw Error("GraphQL subscriptions unavailable in non-browser environment");
68
- return u = o({
69
- url: T(),
70
- keepAlive: 1e4,
71
- lazy: !0,
72
- retryAttempts: 10,
73
- shouldRetry() {
74
- return !0;
75
- },
76
- async retryWait(e) {
77
- let t = Math.min(8e3, 500 * 2 ** e) + Math.random() * 200;
78
- await new Promise((e) => {
79
- setTimeout(() => {
80
- e();
81
- }, t);
67
+ function j(...t) {
68
+ e() && console.log("[RelayNetwork]", ...t);
69
+ }
70
+ async function M() {
71
+ return u == null ? {} : u();
72
+ }
73
+ var N = class {
74
+ url;
75
+ socket = null;
76
+ connectPromise = null;
77
+ disposed = !1;
78
+ acknowledged = !1;
79
+ retryCount = 0;
80
+ nextId = 1;
81
+ keepAliveTimer = null;
82
+ subscriptions = /* @__PURE__ */ new Map();
83
+ pendingAuthUpdate = null;
84
+ constructor(e) {
85
+ this.url = e;
86
+ }
87
+ hasActiveConnection() {
88
+ return this.socket?.readyState === WebSocket.OPEN && this.acknowledged;
89
+ }
90
+ dispose() {
91
+ this.disposed = !0, this.resolvePendingAuthUpdate(!1), this.stopKeepAlive();
92
+ let { socket: e } = this;
93
+ this.socket = null, this.connectPromise = null, this.acknowledged = !1, e != null && e.readyState !== WebSocket.CLOSED && e.close(1e3, "Normal Closure");
94
+ }
95
+ subscribe(e, t) {
96
+ let n = String(this.nextId);
97
+ return this.nextId += 1, this.subscriptions.set(n, {
98
+ payload: e,
99
+ sink: t
100
+ }), this.connectAndSubscribe(n).catch((e) => {
101
+ j("subscription websocket connect failed", e), this.scheduleReconnect();
102
+ }), () => {
103
+ this.unsubscribe(n);
104
+ };
105
+ }
106
+ async refreshAuthentication() {
107
+ if (!this.hasActiveConnection()) return !0;
108
+ let { socket: e } = this;
109
+ if (e == null) return !0;
110
+ if (this.pendingAuthUpdate != null) return !1;
111
+ let t;
112
+ try {
113
+ t = await M();
114
+ } catch {
115
+ return !1;
116
+ }
117
+ return Object.keys(t).length === 0 ? !1 : new Promise((e) => {
118
+ let n = setTimeout(() => {
119
+ this.pendingAuthUpdate = null, e(!1);
120
+ }, p);
121
+ this.pendingAuthUpdate = {
122
+ resolve: e,
123
+ timeout: n
124
+ }, this.send({
125
+ type: "connection_auth_update",
126
+ payload: t
82
127
  });
83
- },
84
- connectionParams: async () => {
85
- if (d == null) return {};
128
+ });
129
+ }
130
+ async connectAndSubscribe(e) {
131
+ let t = this.hasActiveConnection();
132
+ if (await this.ensureConnected(), !t) return;
133
+ let n = this.subscriptions.get(e);
134
+ n != null && this.send({
135
+ id: e,
136
+ type: "subscribe",
137
+ payload: n.payload
138
+ });
139
+ }
140
+ async ensureConnected() {
141
+ if (!this.hasActiveConnection()) {
142
+ if (this.connectPromise != null) {
143
+ await this.connectPromise;
144
+ return;
145
+ }
146
+ this.connectPromise = this.openSocket();
86
147
  try {
87
- return await d();
88
- } catch {
89
- return {};
148
+ await this.connectPromise;
149
+ } finally {
150
+ this.connectPromise = null;
90
151
  }
91
152
  }
92
- }), u;
153
+ }
154
+ async openSocket() {
155
+ if (this.disposed) throw Error("WebSocket client is disposed");
156
+ await new Promise((e, t) => {
157
+ let n = !1, r = () => {
158
+ n || (n = !0, e());
159
+ }, i = (e) => {
160
+ n || (n = !0, t(e));
161
+ }, a = new WebSocket(this.url, f);
162
+ this.socket = a, this.acknowledged = !1, a.onopen = () => {
163
+ M().catch(() => ({})).then((e) => {
164
+ if (a.readyState === WebSocket.OPEN) {
165
+ if (Object.keys(e).length > 0) {
166
+ this.send({
167
+ type: "connection_init",
168
+ payload: e
169
+ });
170
+ return;
171
+ }
172
+ this.send({ type: "connection_init" });
173
+ }
174
+ }).catch((e) => {
175
+ i(e);
176
+ });
177
+ }, a.onerror = () => {
178
+ i(/* @__PURE__ */ Error("GraphQL subscription websocket error"));
179
+ }, a.onmessage = (e) => {
180
+ this.handleMessage(e.data, r, i);
181
+ }, a.onclose = () => {
182
+ i(/* @__PURE__ */ Error("GraphQL subscription websocket closed")), this.handleClose();
183
+ };
184
+ });
185
+ }
186
+ handleMessage(e, t, n) {
187
+ let r;
188
+ try {
189
+ r = JSON.parse(String(e));
190
+ } catch {
191
+ n(/* @__PURE__ */ Error("Invalid GraphQL websocket message")), this.socket?.close(4400, "Invalid websocket message");
192
+ return;
193
+ }
194
+ switch (r.type) {
195
+ case "connection_ack":
196
+ this.acknowledged = !0, this.retryCount = 0, this.startKeepAlive(), t(), this.resubscribeActiveOperations();
197
+ return;
198
+ case "connection_auth_update_ack":
199
+ this.resolvePendingAuthUpdate(!0);
200
+ return;
201
+ case "next":
202
+ this.subscriptions.get(r.id)?.sink.next(r.payload);
203
+ return;
204
+ case "error":
205
+ r.id == null ? this.resolvePendingAuthUpdate(!1) : this.failSubscription(r.id, r.payload);
206
+ return;
207
+ case "complete":
208
+ this.completeSubscription(r.id);
209
+ return;
210
+ case "ping":
211
+ this.send({
212
+ type: "pong",
213
+ payload: r.payload
214
+ });
215
+ return;
216
+ case "pong": return;
217
+ default: n(/* @__PURE__ */ Error("Unsupported GraphQL websocket message")), this.socket?.close(4400, "Unsupported websocket message");
218
+ }
219
+ }
220
+ handleClose() {
221
+ this.acknowledged = !1, this.socket = null, this.connectPromise = null, this.stopKeepAlive(), this.resolvePendingAuthUpdate(!1), !this.disposed && this.subscriptions.size > 0 && this.scheduleReconnect();
222
+ }
223
+ scheduleReconnect() {
224
+ if (this.retryCount >= h) {
225
+ let e = /* @__PURE__ */ Error("GraphQL subscription websocket retry exhausted");
226
+ for (let t of [...this.subscriptions.keys()]) this.failSubscription(t, e);
227
+ return;
228
+ }
229
+ let e = Math.min(8e3, g * 2 ** this.retryCount) + Math.random() * 200;
230
+ this.retryCount += 1, setTimeout(() => {
231
+ this.disposed || this.subscriptions.size === 0 || this.ensureConnected().catch((e) => {
232
+ this.scheduleReconnect(), j("subscription websocket reconnect failed", e);
233
+ });
234
+ }, e);
235
+ }
236
+ resubscribeActiveOperations() {
237
+ for (let [e, t] of this.subscriptions) this.send({
238
+ id: e,
239
+ type: "subscribe",
240
+ payload: t.payload
241
+ });
242
+ }
243
+ unsubscribe(e) {
244
+ this.subscriptions.delete(e) && this.socket?.readyState === WebSocket.OPEN && this.send({
245
+ id: e,
246
+ type: "complete"
247
+ });
248
+ }
249
+ failSubscription(e, t) {
250
+ let n = this.subscriptions.get(e);
251
+ n != null && (this.subscriptions.delete(e), n.sink.error(t));
252
+ }
253
+ completeSubscription(e) {
254
+ let t = this.subscriptions.get(e);
255
+ t != null && (this.subscriptions.delete(e), t.sink.complete());
256
+ }
257
+ startKeepAlive() {
258
+ this.stopKeepAlive(), this.keepAliveTimer = setInterval(() => {
259
+ this.socket?.readyState === WebSocket.OPEN && this.send({ type: "ping" });
260
+ }, m);
261
+ }
262
+ stopKeepAlive() {
263
+ this.keepAliveTimer != null && (clearInterval(this.keepAliveTimer), this.keepAliveTimer = null);
264
+ }
265
+ resolvePendingAuthUpdate(e) {
266
+ let t = this.pendingAuthUpdate;
267
+ t != null && (clearTimeout(t.timeout), this.pendingAuthUpdate = null, t.resolve(e));
268
+ }
269
+ send(e) {
270
+ let { socket: t } = this;
271
+ t?.readyState === WebSocket.OPEN && t.send(JSON.stringify(e));
272
+ }
273
+ };
274
+ function P() {
275
+ if (l != null) return l;
276
+ if (typeof window > "u") throw Error("GraphQL subscriptions unavailable in non-browser environment");
277
+ return l = new N(A()), l;
93
278
  }
94
- function D(e) {
279
+ function F(e) {
95
280
  return typeof File < "u" && e instanceof File || typeof Blob < "u" && e instanceof Blob;
96
281
  }
97
- function O(e, t, n) {
98
- if (D(e)) return n.push({
282
+ function I(e, t, n) {
283
+ if (F(e)) return n.push({
99
284
  path: t,
100
285
  file: e
101
286
  }), null;
102
- if (Array.isArray(e)) return e.map((e, r) => O(e, `${t}.${r}`, n));
287
+ if (Array.isArray(e)) return e.map((e, r) => I(e, `${t}.${r}`, n));
103
288
  if (typeof e == "object" && e) {
104
289
  let r = {};
105
- for (let [i, a] of Object.entries(e)) r[i] = O(a, `${t}.${i}`, n);
290
+ for (let [i, a] of Object.entries(e)) r[i] = I(a, `${t}.${i}`, n);
106
291
  return r;
107
292
  }
108
293
  return e;
109
294
  }
110
- function k(e, t) {
111
- let n = [], r = O(t, "variables", n), i = new FormData(), a = {
295
+ function L(e, t) {
296
+ let n = [], r = I(t, "variables", n), i = new FormData(), a = {
112
297
  query: e,
113
298
  variables: r
114
299
  };
@@ -120,91 +305,88 @@ function k(e, t) {
120
305
  i.append(String(t), e.file);
121
306
  }), i;
122
307
  }
123
- var A = {
308
+ var R = {
124
309
  maxAttempts: 3,
125
310
  baseDelayMs: 300,
126
311
  maxDelayMs: 4e3,
127
312
  fetchTimeoutMs: 6e5
128
313
  };
129
- async function j(e) {
314
+ async function z(e) {
130
315
  await new Promise((t) => {
131
316
  setTimeout(() => {
132
317
  t();
133
318
  }, e);
134
319
  });
135
320
  }
136
- function M(e, t) {
321
+ function B(e, t) {
137
322
  let n = Math.min(t.maxDelayMs, t.baseDelayMs * 2 ** e);
138
323
  return n + Math.random() * .2 * n;
139
324
  }
140
- function N(e) {
325
+ function V(e) {
141
326
  return e === 408 || e === 429 || e >= 500 && e < 600;
142
327
  }
143
- function P(e) {
328
+ function H(e) {
144
329
  return e instanceof TypeError || e instanceof Error && e.name === "AbortError";
145
330
  }
146
- function F(...t) {
147
- e() && console.log("[RelayNetwork]", ...t);
148
- }
149
- async function I(e, t, n, r) {
331
+ async function U(e, t, n, r) {
150
332
  let i = e.text;
151
333
  if (i == null) throw Error("Missing GraphQL query text");
152
334
  let a = i;
153
- async function o(e) {
335
+ async function s(e) {
154
336
  let n = new AbortController(), r = setTimeout(() => {
155
337
  n.abort();
156
- }, A.fetchTimeoutMs);
338
+ }, R.fetchTimeoutMs);
157
339
  try {
158
340
  let r, i = {};
159
- if (d != null) try {
160
- Object.assign(i, await d());
341
+ if (u != null) try {
342
+ Object.assign(i, await u());
161
343
  } catch {}
162
344
  (() => {
163
345
  try {
164
346
  let e = [t];
165
347
  for (; e.length > 0;) {
166
348
  let t = e.pop();
167
- if (D(t)) return !0;
349
+ if (F(t)) return !0;
168
350
  Array.isArray(t) ? e.push(...t) : typeof t == "object" && t && e.push(...Object.values(t));
169
351
  }
170
352
  } catch {}
171
353
  return !1;
172
- })() ? r = k(a, t) : (i["Content-Type"] = "application/json", r = JSON.stringify({
354
+ })() ? r = L(a, t) : (i["Content-Type"] = "application/json", r = JSON.stringify({
173
355
  query: a,
174
356
  variables: t
175
357
  }));
176
- let o = await fetch(s, {
358
+ let s = await fetch(o, {
177
359
  method: "POST",
178
360
  body: r,
179
361
  headers: i,
180
362
  credentials: "include",
181
363
  signal: n.signal
182
364
  });
183
- if (!o.ok) {
184
- if (e < A.maxAttempts - 1 && N(o.status)) throw Error(`Retryable HTTP status ${o.status}`);
185
- let t = await o.text().catch(() => "");
186
- throw Error(`GraphQL HTTP error ${o.status}: ${t}`);
365
+ if (!s.ok) {
366
+ if (e < R.maxAttempts - 1 && V(s.status)) throw Error(`Retryable HTTP status ${s.status}`);
367
+ let t = await s.text().catch(() => "");
368
+ throw Error(`GraphQL HTTP error ${s.status}: ${t}`);
187
369
  }
188
- let c = await o.json();
189
- return Array.isArray(c.errors) && c.errors.length > 0 && F("GraphQL errors", c.errors), c;
370
+ let c = await s.json();
371
+ return Array.isArray(c.errors) && c.errors.length > 0 && j("GraphQL errors", c.errors), c;
190
372
  } finally {
191
373
  clearTimeout(r);
192
374
  }
193
375
  }
194
- for (let e = 0; e < A.maxAttempts; e += 1) try {
195
- return await o(e);
376
+ for (let e = 0; e < R.maxAttempts; e += 1) try {
377
+ return await s(e);
196
378
  } catch (t) {
197
- let n = e === A.maxAttempts - 1;
198
- if (!(P(t) || t instanceof Error && t.message.includes("Retryable HTTP status")) || n) throw F("GraphQL fetch error", t), t;
199
- await j(M(e, A));
379
+ let n = e === R.maxAttempts - 1;
380
+ if (!(H(t) || t instanceof Error && t.message.includes("Retryable HTTP status")) || n) throw j("GraphQL fetch error", t), t;
381
+ await z(B(e, R));
200
382
  }
201
383
  throw Error("Exhausted retries without returning a result");
202
384
  }
203
- function L(e, t) {
204
- return F("subscription:start", e.name, t), r.create((n) => {
385
+ function W(e, t) {
386
+ return j("subscription:start", e.name, t), r.create((n) => {
205
387
  let r = e.text;
206
388
  if (r == null || r === "") return n.error(/* @__PURE__ */ Error("Subscription operation text is empty")), () => {};
207
- let i = E().subscribe({
389
+ let i = P().subscribe({
208
390
  query: r,
209
391
  variables: t,
210
392
  operationName: e.name
@@ -225,31 +407,32 @@ function L(e, t) {
225
407
  };
226
408
  });
227
409
  }
228
- function R() {
229
- return n.create(I, L);
410
+ function G() {
411
+ return n.create(U, W);
230
412
  }
231
- function z() {
232
- return l ??= new t({
233
- getDataID: v,
234
- relayFieldLogger: g,
235
- network: R(),
413
+ function K() {
414
+ return c ??= new t({
415
+ getDataID: S,
416
+ relayFieldLogger: b,
417
+ network: G(),
236
418
  store: new a(new i())
237
- }), l;
419
+ }), c;
238
420
  }
239
- function B() {
240
- let e = z(), t = new i();
421
+ function q() {
422
+ let e = K(), t = new i();
241
423
  e.getStore().publish(t), e.getStore().notify();
242
424
  }
243
- var V = {
244
- buildFormData: k,
245
- calcBackoff: M,
246
- collectFiles: O,
247
- getDataId: v,
248
- isNetworkError: P,
249
- isRetryableStatus: N,
250
- isUploadable: D
425
+ var J = {
426
+ RelayWebSocketClient: N,
427
+ buildFormData: L,
428
+ calcBackoff: B,
429
+ collectFiles: I,
430
+ getDataId: S,
431
+ isNetworkError: H,
432
+ isRetryableStatus: V,
433
+ isUploadable: F
251
434
  };
252
435
  //#endregion
253
- export { V as __test, w as configureRelayEnvironment, z as getEnvironment, R as getNetwork, x as getRelayTransportSnapshot, C as reconnectRelayWebSocket, B as resetRelayStore, S as subscribeRelayTransport };
436
+ export { J as __test, k as configureRelayEnvironment, K as getEnvironment, G as getNetwork, T as getRelayTransportSnapshot, D as reconnectRelayWebSocket, O as refreshRelayWebSocketAuthentication, q as resetRelayStore, E as subscribeRelayTransport };
254
437
 
255
438
  //# sourceMappingURL=environment.js.map