@bitplanet/deva-sdk 0.8.3 → 0.8.5

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.es.js CHANGED
@@ -1,74 +1,74 @@
1
- import { jsx as ee } from "react/jsx-runtime";
2
- import { useState as D, useCallback as u, useEffect as p, useLayoutEffect as re, useRef as C, useContext as ne } from "react";
3
- import { f as B, D as W, u as H, e as Y } from "./config-CL6PuR8K.js";
4
- const te = () => {
1
+ import { jsx as oe } from "react/jsx-runtime";
2
+ import { useState as U, useCallback as p, useEffect as v, useLayoutEffect as se, useRef as S, useContext as ae } from "react";
3
+ import { f as Y, D as G, u as q, e as K } from "./config-CBh0zg_P.js";
4
+ const ce = () => {
5
5
  const e = new Uint8Array(32);
6
- return crypto.getRandomValues(e), G(e);
7
- }, G = (e) => btoa(String.fromCharCode(...new Uint8Array(e))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""), oe = async (e) => {
8
- const n = new TextEncoder().encode(e), o = await crypto.subtle.digest("SHA-256", n);
9
- return G(o);
10
- }, se = (e) => Math.round(Date.now() / 1e3 + Number(e)), ae = (e) => Math.round(Date.now()) / 1e3 + 30 >= e, P = typeof window < "u", ce = (e, r) => {
11
- if (!P)
6
+ return crypto.getRandomValues(e), Q(e);
7
+ }, Q = (e) => btoa(String.fromCharCode(...new Uint8Array(e))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""), ie = async (e) => {
8
+ const t = new TextEncoder().encode(e), o = await crypto.subtle.digest("SHA-256", t);
9
+ return Q(o);
10
+ }, ue = (e) => Math.round(Date.now() / 1e3 + Number(e)), H = (e) => Math.round(Date.now()) / 1e3 + 60 >= e, D = typeof window < "u", de = (e, r) => {
11
+ if (!D)
12
12
  return r;
13
13
  try {
14
- const n = sessionStorage.getItem(e);
15
- return n ? JSON.parse(n) : r;
16
- } catch (n) {
17
- return console.error(n), r;
14
+ const t = sessionStorage.getItem(e);
15
+ return t ? JSON.parse(t) : r;
16
+ } catch (t) {
17
+ return console.error(t), r;
18
18
  }
19
19
  };
20
- function h(e, r) {
21
- const [n, o] = D(
22
- () => ce(e, r)
23
- ), i = u(
24
- (t) => {
20
+ function k(e, r) {
21
+ const [t, o] = U(
22
+ () => de(e, r)
23
+ ), u = p(
24
+ (n) => {
25
25
  try {
26
- o(t), P && sessionStorage.setItem(e, JSON.stringify(t));
27
- } catch (s) {
28
- console.error(s);
26
+ o(n), D && sessionStorage.setItem(e, JSON.stringify(n));
27
+ } catch (a) {
28
+ console.error(a);
29
29
  }
30
30
  },
31
31
  [e]
32
32
  );
33
- return p(() => {
34
- if (!P)
33
+ return v(() => {
34
+ if (!D)
35
35
  return;
36
- const t = sessionStorage.getItem(e);
37
- if (t)
36
+ const n = sessionStorage.getItem(e);
37
+ if (n)
38
38
  try {
39
- o(JSON.parse(t));
40
- } catch (s) {
41
- console.error(s);
39
+ o(JSON.parse(n));
40
+ } catch (a) {
41
+ console.error(a);
42
42
  }
43
- }, []), p(() => {
44
- if (!P)
43
+ }, []), v(() => {
44
+ if (!D)
45
45
  return;
46
- const t = (s) => {
47
- if (s.storageArea === window.sessionStorage && s.key === e)
48
- if (s.newValue === null)
46
+ const n = (a) => {
47
+ if (a.storageArea === window.sessionStorage && a.key === e)
48
+ if (a.newValue === null)
49
49
  o(r);
50
50
  else
51
51
  try {
52
- o(JSON.parse(s.newValue));
52
+ o(JSON.parse(a.newValue));
53
53
  } catch {
54
54
  console.warn(
55
- `Failed to handle storageEvent's newValue='${s.newValue}' for key '${e}'`
55
+ `Failed to handle storageEvent's newValue='${a.newValue}' for key '${e}'`
56
56
  );
57
57
  }
58
58
  };
59
- return window.addEventListener("storage", t, !1), () => window.removeEventListener("storage", t, !1);
60
- }, [e, r]), [n, i];
59
+ return window.addEventListener("storage", n, !1), () => window.removeEventListener("storage", n, !1);
60
+ }, [e, r]), [t, u];
61
61
  }
62
- class y extends Error {
62
+ class T extends Error {
63
63
  }
64
- y.prototype.name = "InvalidTokenError";
65
- function ie(e) {
66
- return decodeURIComponent(atob(e).replace(/(.)/g, (r, n) => {
67
- let o = n.charCodeAt(0).toString(16).toUpperCase();
64
+ T.prototype.name = "InvalidTokenError";
65
+ function fe(e) {
66
+ return decodeURIComponent(atob(e).replace(/(.)/g, (r, t) => {
67
+ let o = t.charCodeAt(0).toString(16).toUpperCase();
68
68
  return o.length < 2 && (o = "0" + o), "%" + o;
69
69
  }));
70
70
  }
71
- function de(e) {
71
+ function le(e) {
72
72
  let r = e.replace(/-/g, "+").replace(/_/g, "/");
73
73
  switch (r.length % 4) {
74
74
  case 0:
@@ -83,58 +83,58 @@ function de(e) {
83
83
  throw new Error("base64 string is not of the correct length");
84
84
  }
85
85
  try {
86
- return ie(r);
86
+ return fe(r);
87
87
  } catch {
88
88
  return atob(r);
89
89
  }
90
90
  }
91
- function ue(e, r) {
91
+ function he(e, r) {
92
92
  if (typeof e != "string")
93
- throw new y("Invalid token specified: must be a string");
93
+ throw new T("Invalid token specified: must be a string");
94
94
  r || (r = {});
95
- const n = r.header === !0 ? 0 : 1, o = e.split(".")[n];
95
+ const t = r.header === !0 ? 0 : 1, o = e.split(".")[t];
96
96
  if (typeof o != "string")
97
- throw new y(`Invalid token specified: missing part #${n + 1}`);
98
- let i;
97
+ throw new T(`Invalid token specified: missing part #${t + 1}`);
98
+ let u;
99
99
  try {
100
- i = de(o);
101
- } catch (t) {
102
- throw new y(`Invalid token specified: invalid base64 for part #${n + 1} (${t.message})`);
100
+ u = le(o);
101
+ } catch (n) {
102
+ throw new T(`Invalid token specified: invalid base64 for part #${t + 1} (${n.message})`);
103
103
  }
104
104
  try {
105
- return JSON.parse(i);
106
- } catch (t) {
107
- throw new y(`Invalid token specified: invalid json for part #${n + 1} (${t.message})`);
105
+ return JSON.parse(u);
106
+ } catch (n) {
107
+ throw new T(`Invalid token specified: invalid json for part #${t + 1} (${n.message})`);
108
108
  }
109
109
  }
110
- const fe = typeof window < "u" ? re : p;
111
- function le(e, r, n = [], o, i) {
112
- const t = C(e);
113
- fe(() => {
114
- t.current = e;
115
- }, n), p(() => {
110
+ const pe = typeof window < "u" ? se : v;
111
+ function ge(e, r, t = [], o, u) {
112
+ const n = S(e);
113
+ pe(() => {
114
+ n.current = e;
115
+ }, t), v(() => {
116
116
  if (!r && r !== 0)
117
117
  return;
118
- const s = o(() => {
119
- t.current();
118
+ const a = o(() => {
119
+ n.current();
120
120
  }, r);
121
121
  return () => {
122
- i(s);
122
+ u(a);
123
123
  };
124
124
  }, [r]);
125
125
  }
126
- function he(e, r, n = []) {
127
- le(e, r, n, setInterval, clearInterval);
126
+ function we(e, r, t = []) {
127
+ ge(e, r, t, setInterval, clearInterval);
128
128
  }
129
- const $ = typeof window < "u" && typeof document < "u", A = (...e) => {
129
+ const V = typeof window < "u" && typeof document < "u", b = (...e) => {
130
130
  console.log("[sdk/provider]", ...e);
131
- }, pe = (e) => {
132
- const r = Y(e), { data: n, error: o, isLoading: i } = H(
133
- $ ? `${r}/.well-known/openid-configuration` : null
131
+ }, me = (e) => {
132
+ const r = K(e), { data: t, error: o, isLoading: u } = q(
133
+ V ? `${r}/.well-known/openid-configuration` : null
134
134
  );
135
- return { openIdConfig: n, isOpenIdConfigLoading: i, error: o };
136
- }, ge = (e, r) => {
137
- const { data: n, error: o, isLoading: i } = H(
135
+ return { openIdConfig: t, isOpenIdConfigLoading: u, error: o };
136
+ }, ye = (e, r) => {
137
+ const { data: t, error: o, isLoading: u } = q(
138
138
  r ? e : !1,
139
139
  {
140
140
  headers: {
@@ -143,16 +143,16 @@ const $ = typeof window < "u" && typeof document < "u", A = (...e) => {
143
143
  }
144
144
  );
145
145
  return {
146
- userInfo: n,
147
- isUserInfoLoading: i,
146
+ userInfo: t,
147
+ isUserInfoLoading: u,
148
148
  userInfoError: o
149
149
  };
150
- }, we = (e, r, n) => u(async () => {
151
- if (!r || !n) {
152
- A("no refresh token or token endpoint");
150
+ }, ke = (e, r, t) => p(async () => {
151
+ if (!r || !t) {
152
+ b("no refresh token or token endpoint");
153
153
  return;
154
154
  }
155
- return B(n, {
155
+ return Y(t, {
156
156
  method: "POST",
157
157
  headers: {
158
158
  "Content-Type": "application/x-www-form-urlencoded"
@@ -162,8 +162,8 @@ const $ = typeof window < "u" && typeof document < "u", A = (...e) => {
162
162
  refresh_token: r
163
163
  })
164
164
  });
165
- }, [r, e, n]), me = (e, r, n) => u(async () => {
166
- !n || !e || await fetch(n, {
165
+ }, [r, e, t]), ve = (e, r, t) => p(async () => {
166
+ !t || !e || await fetch(t, {
167
167
  method: "POST",
168
168
  headers: {
169
169
  "Content-Type": "application/json"
@@ -173,18 +173,18 @@ const $ = typeof window < "u" && typeof document < "u", A = (...e) => {
173
173
  client_id: r
174
174
  })
175
175
  });
176
- }, [e, r, n]);
177
- function _e({
176
+ }, [e, r, t]);
177
+ function Se({
178
178
  clientId: e,
179
179
  redirectUri: r,
180
- children: n,
180
+ children: t,
181
181
  env: o
182
182
  }) {
183
- const i = Y(o), { openIdConfig: t, isOpenIdConfigLoading: s, error: U } = pe(o);
184
- U && console.error("[sdk/provider] error fetching openid info: ", U);
185
- const V = C(!1), M = C(!1);
186
- p(() => {
187
- o === "development" && !M.current && (M.current = !0, console.warn(
183
+ const u = K(o), { openIdConfig: n, isOpenIdConfigLoading: a, error: M } = me(o);
184
+ M && console.error("[sdk/provider] error fetching openid info: ", M);
185
+ const J = S(!1), F = S(!1);
186
+ v(() => {
187
+ o === "development" && !F.current && (F.current = !0, console.warn(
188
188
  `
189
189
  ========================================
190
190
  ⚠️ WARNING: DEVELOPMENT ENVIRONMENT ⚠️
@@ -198,134 +198,178 @@ Please verify your environment settings and use 'staging' or 'production' instea
198
198
  `
199
199
  ));
200
200
  }, [o]);
201
- const [q, l] = h("code_verifier", ""), [I, g] = h(
201
+ const [X, w] = k("code_verifier", ""), [O, _] = k(
202
202
  "auth_in_progress",
203
203
  !1
204
- ), [K, k] = h("id_token", null), [f, v] = h(
204
+ ), [Z, P] = k("id_token", null), [f, A] = k(
205
205
  "access_token",
206
206
  null
207
- ), [w, _] = h(
207
+ ), [g, I] = k(
208
208
  "refresh_token",
209
209
  null
210
- ), [E, S] = h("expires_in", 0), [T, R] = D(!0), L = u(
211
- (a) => {
212
- let d = E;
213
- v(a.access_token), k(a.id_token);
210
+ ), [m, L] = k("expires_in", 0), [N, l] = U(!0), y = p(
211
+ (c) => {
212
+ let d = m;
213
+ A(c.access_token), P(c.id_token);
214
214
  try {
215
- const O = ue(a.id_token);
216
- d = Math.round(Number(O.exp) - Date.now() / 1e3);
217
- } catch (O) {
215
+ const i = he(c.id_token);
216
+ d = Math.round(Number(i.exp) - Date.now() / 1e3);
217
+ } catch (i) {
218
218
  console.warn(
219
- `[sdk/provider]: Failed to decode idToken: ${O.message}`
219
+ `[sdk/provider]: Failed to decode idToken: ${i.message}`
220
220
  );
221
221
  }
222
- const c = a.expires_in ?? d;
223
- S(se(c)), _(a.refresh_token), l("");
222
+ const s = c.expires_in ?? d;
223
+ L(ue(s)), I(c.refresh_token), w("");
224
224
  },
225
225
  [
226
- v,
227
- k,
228
- S,
229
- _,
230
- l,
231
- E
226
+ A,
227
+ P,
228
+ L,
229
+ I,
230
+ w,
231
+ m
232
232
  ]
233
- ), [Q, N] = D(null), { userInfo: X } = ge(
234
- t == null ? void 0 : t.userinfo_endpoint,
235
- T ? null : f
236
- ), m = u(() => {
237
- l(""), g(!1), v(""), k(""), S(0), _("");
233
+ ), [ee, C] = U(null), { userInfo: re } = ye(
234
+ n == null ? void 0 : n.userinfo_endpoint,
235
+ N ? null : f
236
+ ), R = p(() => {
237
+ w(""), _(!1), A(""), P(""), L(0), I("");
238
238
  }, [
239
- l,
240
- g,
241
- v,
242
- k,
243
- S,
244
- _
245
- ]), J = me(
239
+ w,
240
+ _,
241
+ A,
242
+ P,
243
+ L,
244
+ I
245
+ ]), j = ve(
246
246
  f,
247
247
  e,
248
- t == null ? void 0 : t.revocation_endpoint
249
- ), j = u(async () => {
250
- await J(), m(), N(null);
251
- }, [m, J]), F = u(async () => {
252
- if (m(), g(!0), s)
248
+ n == null ? void 0 : n.revocation_endpoint
249
+ ), z = p(async () => {
250
+ await j(), R(), C(null);
251
+ }, [R, j]), E = ke(
252
+ e,
253
+ g,
254
+ n == null ? void 0 : n.token_endpoint
255
+ ), B = p(async () => {
256
+ if (f && !H(m))
253
257
  return;
254
- const a = te(), d = await oe(a);
255
- l(a);
256
- const c = new URL((t == null ? void 0 : t.authorization_endpoint) ?? "");
257
- c.searchParams.append("client_id", e), c.searchParams.append("redirect_uri", r), c.searchParams.append("response_type", "code"), c.searchParams.append("code_challenge", d), c.searchParams.append("code_challenge_method", "S256"), window.location.assign(c.toString());
258
+ if (g) {
259
+ if (a)
260
+ return;
261
+ l(!0);
262
+ try {
263
+ const i = await E();
264
+ if (i) {
265
+ y(i), h.current = 0;
266
+ return;
267
+ }
268
+ } catch (i) {
269
+ console.error("[sdk/provider]: Error refreshing token during login", i);
270
+ } finally {
271
+ l(!1);
272
+ }
273
+ try {
274
+ await new Promise((ne) => setTimeout(ne, 2e3)), l(!0);
275
+ const i = await E();
276
+ if (i) {
277
+ y(i), h.current = 0;
278
+ return;
279
+ }
280
+ } catch (i) {
281
+ console.error("[sdk/provider]: Retry refresh also failed", i);
282
+ } finally {
283
+ l(!1);
284
+ }
285
+ }
286
+ if (R(), _(!0), a)
287
+ return;
288
+ const c = ce(), d = await ie(c);
289
+ w(c);
290
+ const s = new URL((n == null ? void 0 : n.authorization_endpoint) ?? "");
291
+ s.searchParams.append("client_id", e), s.searchParams.append("redirect_uri", r), s.searchParams.append("response_type", "code"), s.searchParams.append("code_challenge", d), s.searchParams.append("code_challenge_method", "S256"), window.location.assign(s.toString());
258
292
  }, [
259
- e,
260
- r,
261
- l,
293
+ f,
262
294
  m,
263
295
  g,
264
- s,
265
- t
266
- ]), z = we(
296
+ E,
297
+ y,
298
+ l,
267
299
  e,
300
+ r,
268
301
  w,
269
- t == null ? void 0 : t.token_endpoint
270
- ), x = u(
271
- async (a = !1) => {
302
+ R,
303
+ _,
304
+ a,
305
+ n
306
+ ]), h = S(0), W = S(0), $ = p(
307
+ async (c = !1) => {
272
308
  if (!f) {
273
- A("no access token");
309
+ b("no access token");
274
310
  return;
275
311
  }
276
- if (!(!ae(E) && !a)) {
277
- if (T && !a) {
278
- A("already refreshing");
279
- return;
280
- }
281
- if (!w) {
282
- A("no refresh token");
312
+ if (!H(m) && !c) {
313
+ h.current = 0;
314
+ return;
315
+ }
316
+ if (N && !c) {
317
+ b("already refreshing");
318
+ return;
319
+ }
320
+ if (!g) {
321
+ b("no refresh token");
322
+ return;
323
+ }
324
+ const d = h.current;
325
+ if (d > 0 && !c) {
326
+ const s = Math.min(1e3 * Math.pow(2, d), 3e5);
327
+ if (Date.now() - W.current < s)
283
328
  return;
284
- }
285
- R(!0);
286
- try {
287
- const d = await z();
288
- d && L(d);
289
- } catch (d) {
290
- console.error("[sdk/provider]: Error refreshing token", d);
291
- } finally {
292
- R(!1);
293
- }
329
+ }
330
+ W.current = Date.now(), l(!0);
331
+ try {
332
+ const s = await E();
333
+ s ? (y(s), h.current = 0) : (h.current++, b(`refresh returned empty response (attempt ${h.current})`));
334
+ } catch (s) {
335
+ h.current++, console.error(`[sdk/provider]: Error refreshing token (attempt ${h.current})`, s);
336
+ } finally {
337
+ l(!1);
294
338
  }
295
339
  },
296
340
  [
297
341
  f,
298
- w,
299
- z,
300
- L,
301
- T,
342
+ g,
302
343
  E,
303
- R
344
+ y,
345
+ N,
346
+ m,
347
+ l
304
348
  ]
305
- ), Z = 1e4 * Math.random();
306
- he(
349
+ ), te = 1e4 * Math.random();
350
+ we(
307
351
  () => {
308
- x();
352
+ $();
309
353
  },
310
- $ && w ? 1e3 + Z : null,
311
- [x]
312
- ), p(() => {
313
- let a = "";
314
- if ($ && !s) {
315
- if (I) {
316
- R(!1);
354
+ V && g ? 1e4 + te : null,
355
+ [$]
356
+ ), v(() => {
357
+ let c = "";
358
+ if (V && !a) {
359
+ if (O) {
360
+ l(!1);
317
361
  const d = new URLSearchParams(window.location.search);
318
- if (a = d.get("code") ?? "", !a) {
319
- const c = d.get("error_description") ?? "An unknown error occurred";
362
+ if (c = d.get("code") ?? "", !c) {
363
+ const s = d.get("error_description") ?? "An unknown error occurred";
320
364
  console.error(
321
- `[sdk/provider]: ${c}
365
+ `[sdk/provider]: ${s}
322
366
  Expected to find a '?code=' parameter in the URL but found none`
323
- ), N(c), m();
367
+ ), C(s), R();
324
368
  return;
325
369
  }
326
- if (g(!1), V.current)
370
+ if (_(!1), J.current)
327
371
  return;
328
- V.current = !0, B((t == null ? void 0 : t.token_endpoint) ?? "", {
372
+ J.current = !0, Y((n == null ? void 0 : n.token_endpoint) ?? "", {
329
373
  method: "POST",
330
374
  headers: {
331
375
  "Content-Type": "application/x-www-form-urlencoded"
@@ -333,50 +377,50 @@ Expected to find a '?code=' parameter in the URL but found none`
333
377
  body: new URLSearchParams({
334
378
  client_id: e,
335
379
  redirect_uri: r,
336
- code: a,
337
- code_verifier: q,
380
+ code: c,
381
+ code_verifier: X,
338
382
  grant_type: "authorization_code"
339
383
  })
340
- }).then(L).catch((c) => {
341
- console.error("[sdk/provider]: error in fetching", c), N("An error occurred while fetching tokens");
384
+ }).then(y).catch((s) => {
385
+ console.error("[sdk/provider]: error in fetching", s), C("An error occurred while fetching tokens");
342
386
  }), window.history.replaceState({}, document.title, window.location.pathname);
343
387
  }
344
- x(!0);
388
+ $(!0);
345
389
  }
346
- }, [s]);
347
- const b = {
348
- isAuthenticated: !!f && !T,
390
+ }, [a]);
391
+ const x = {
392
+ isAuthenticated: !!f && !N,
349
393
  accessToken: f,
350
- user: X ?? null,
351
- login: F,
352
- logout: j,
353
- isReady: !s && !I
394
+ user: re ?? null,
395
+ login: B,
396
+ logout: z,
397
+ isReady: !a && !O
354
398
  };
355
- return /* @__PURE__ */ ee(
356
- W.Provider,
399
+ return /* @__PURE__ */ oe(
400
+ G.Provider,
357
401
  {
358
402
  value: {
359
403
  clientId: e,
360
404
  redirectUri: r,
361
405
  env: o,
362
- login: F,
363
- logout: j,
364
- authInProgress: I,
365
- authError: Q,
366
- idToken: K,
406
+ login: B,
407
+ logout: z,
408
+ authInProgress: O,
409
+ authError: ee,
410
+ idToken: Z,
367
411
  accessToken: f,
368
- refreshToken: w,
369
- user: b.user,
370
- isAuthenticated: b.isAuthenticated,
371
- isReady: b.isReady,
372
- url: i
412
+ refreshToken: g,
413
+ user: x.user,
414
+ isAuthenticated: x.isAuthenticated,
415
+ isReady: x.isReady,
416
+ url: u
373
417
  },
374
- children: typeof n == "function" ? n(b) : n
418
+ children: typeof t == "function" ? t(x) : t
375
419
  }
376
420
  );
377
421
  }
378
- const Ee = () => {
379
- const e = ne(W);
422
+ const Te = () => {
423
+ const e = ae(G);
380
424
  if (!e)
381
425
  throw new Error("useDeva must be used within a DevaProvider");
382
426
  return {
@@ -389,6 +433,6 @@ const Ee = () => {
389
433
  };
390
434
  };
391
435
  export {
392
- _e as DevaProvider,
393
- Ee as useDeva
436
+ Se as DevaProvider,
437
+ Te as useDeva
394
438
  };