@bitplanet/deva-sdk 0.8.4 → 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 re } from "react/jsx-runtime";
2
- import { useState as C, useCallback as l, useEffect as y, useLayoutEffect as ne, useRef as $, useContext as te } from "react";
3
- import { f as W, D as H, u as Y, e as G } from "./config-CL6PuR8K.js";
4
- const oe = () => {
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), q(e);
7
- }, q = (e) => btoa(String.fromCharCode(...new Uint8Array(e))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""), se = async (e) => {
8
- const n = new TextEncoder().encode(e), o = await crypto.subtle.digest("SHA-256", n);
9
- return q(o);
10
- }, ae = (e) => Math.round(Date.now() / 1e3 + Number(e)), B = (e) => Math.round(Date.now()) / 1e3 + 30 >= e, L = typeof window < "u", ce = (e, r) => {
11
- if (!L)
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 m(e, r) {
21
- const [n, o] = C(
22
- () => ce(e, r)
23
- ), i = l(
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), L && 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 y(() => {
34
- if (!L)
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
- }, []), y(() => {
44
- if (!L)
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 E extends Error {
62
+ class T extends Error {
63
63
  }
64
- E.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 E("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 E(`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 E(`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 E(`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" ? ne : y;
111
- function le(e, r, n = [], o, i) {
112
- const t = $(e);
113
- fe(() => {
114
- t.current = e;
115
- }, n), y(() => {
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 U = typeof window < "u" && typeof document < "u", N = (...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 = G(e), { data: n, error: o, isLoading: i } = Y(
133
- U ? `${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 } = Y(
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 U = typeof window < "u" && typeof document < "u", N = (...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) => l(async () => {
151
- if (!r || !n) {
152
- N("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 W(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 U = typeof window < "u" && typeof document < "u", N = (...e) => {
162
162
  refresh_token: r
163
163
  })
164
164
  });
165
- }, [r, e, n]), me = (e, r, n) => l(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 U = typeof window < "u" && typeof document < "u", N = (...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 = G(o), { openIdConfig: t, isOpenIdConfigLoading: s, error: V } = pe(o);
184
- V && console.error("[sdk/provider] error fetching openid info: ", V);
185
- const M = $(!1), J = $(!1);
186
- y(() => {
187
- o === "development" && !J.current && (J.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,158 +198,178 @@ Please verify your environment settings and use 'staging' or 'production' instea
198
198
  `
199
199
  ));
200
200
  }, [o]);
201
- const [K, g] = m("code_verifier", ""), [x, k] = m(
201
+ const [X, w] = k("code_verifier", ""), [O, _] = k(
202
202
  "auth_in_progress",
203
203
  !1
204
- ), [Q, S] = m("id_token", null), [u, T] = m(
204
+ ), [Z, P] = k("id_token", null), [f, A] = k(
205
205
  "access_token",
206
206
  null
207
- ), [h, R] = m(
207
+ ), [g, I] = k(
208
208
  "refresh_token",
209
209
  null
210
- ), [w, b] = m("expires_in", 0), [P, p] = C(!0), v = l(
211
- (a) => {
212
- let d = w;
213
- T(a.access_token), S(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 f = ue(a.id_token);
216
- d = Math.round(Number(f.exp) - Date.now() / 1e3);
217
- } catch (f) {
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: ${f.message}`
219
+ `[sdk/provider]: Failed to decode idToken: ${i.message}`
220
220
  );
221
221
  }
222
- const c = a.expires_in ?? d;
223
- b(ae(c)), R(a.refresh_token), g("");
222
+ const s = c.expires_in ?? d;
223
+ L(ue(s)), I(c.refresh_token), w("");
224
224
  },
225
225
  [
226
- T,
227
- S,
228
- b,
229
- R,
230
- g,
231
- w
226
+ A,
227
+ P,
228
+ L,
229
+ I,
230
+ w,
231
+ m
232
232
  ]
233
- ), [X, O] = C(null), { userInfo: Z } = ge(
234
- t == null ? void 0 : t.userinfo_endpoint,
235
- P ? null : u
236
- ), _ = l(() => {
237
- g(""), k(!1), T(""), S(""), b(0), R("");
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
- g,
240
- k,
241
- T,
242
- S,
243
- b,
244
- R
245
- ]), j = me(
246
- u,
239
+ w,
240
+ _,
241
+ A,
242
+ P,
243
+ L,
244
+ I
245
+ ]), j = ve(
246
+ f,
247
247
  e,
248
- t == null ? void 0 : t.revocation_endpoint
249
- ), F = l(async () => {
250
- await j(), _(), O(null);
251
- }, [_, j]), A = we(
248
+ n == null ? void 0 : n.revocation_endpoint
249
+ ), z = p(async () => {
250
+ await j(), R(), C(null);
251
+ }, [R, j]), E = ke(
252
252
  e,
253
- h,
254
- t == null ? void 0 : t.token_endpoint
255
- ), z = l(async () => {
256
- if (u && !B(w))
253
+ g,
254
+ n == null ? void 0 : n.token_endpoint
255
+ ), B = p(async () => {
256
+ if (f && !H(m))
257
257
  return;
258
- if (h) {
259
- if (s)
258
+ if (g) {
259
+ if (a)
260
260
  return;
261
- p(!0);
261
+ l(!0);
262
262
  try {
263
- const f = await A();
264
- if (f) {
265
- v(f);
263
+ const i = await E();
264
+ if (i) {
265
+ y(i), h.current = 0;
266
266
  return;
267
267
  }
268
- } catch (f) {
269
- console.error("[sdk/provider]: Error refreshing token", f);
268
+ } catch (i) {
269
+ console.error("[sdk/provider]: Error refreshing token during login", i);
270
270
  } finally {
271
- p(!1);
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);
272
284
  }
273
285
  }
274
- if (_(), k(!0), s)
286
+ if (R(), _(!0), a)
275
287
  return;
276
- const a = oe(), d = await se(a);
277
- g(a);
278
- const c = new URL((t == null ? void 0 : t.authorization_endpoint) ?? "");
279
- 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());
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());
280
292
  }, [
281
- u,
282
- w,
283
- h,
284
- A,
285
- v,
286
- p,
293
+ f,
294
+ m,
295
+ g,
296
+ E,
297
+ y,
298
+ l,
287
299
  e,
288
300
  r,
289
- g,
301
+ w,
302
+ R,
290
303
  _,
291
- k,
292
- s,
293
- t
294
- ]), D = l(
295
- async (a = !1) => {
296
- if (!u) {
297
- N("no access token");
304
+ a,
305
+ n
306
+ ]), h = S(0), W = S(0), $ = p(
307
+ async (c = !1) => {
308
+ if (!f) {
309
+ b("no access token");
298
310
  return;
299
311
  }
300
- if (!(!B(w) && !a)) {
301
- if (P && !a) {
302
- N("already refreshing");
303
- return;
304
- }
305
- if (!h) {
306
- N("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)
307
328
  return;
308
- }
309
- p(!0);
310
- try {
311
- const d = await A();
312
- d && v(d);
313
- } catch (d) {
314
- console.error("[sdk/provider]: Error refreshing token", d);
315
- } finally {
316
- p(!1);
317
- }
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);
318
338
  }
319
339
  },
320
340
  [
321
- u,
322
- h,
323
- A,
324
- v,
325
- P,
326
- w,
327
- p
341
+ f,
342
+ g,
343
+ E,
344
+ y,
345
+ N,
346
+ m,
347
+ l
328
348
  ]
329
- ), ee = 1e4 * Math.random();
330
- he(
349
+ ), te = 1e4 * Math.random();
350
+ we(
331
351
  () => {
332
- D();
352
+ $();
333
353
  },
334
- U && h ? 1e3 + ee : null,
335
- [D]
336
- ), y(() => {
337
- let a = "";
338
- if (U && !s) {
339
- if (x) {
340
- p(!1);
354
+ V && g ? 1e4 + te : null,
355
+ [$]
356
+ ), v(() => {
357
+ let c = "";
358
+ if (V && !a) {
359
+ if (O) {
360
+ l(!1);
341
361
  const d = new URLSearchParams(window.location.search);
342
- if (a = d.get("code") ?? "", !a) {
343
- 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";
344
364
  console.error(
345
- `[sdk/provider]: ${c}
365
+ `[sdk/provider]: ${s}
346
366
  Expected to find a '?code=' parameter in the URL but found none`
347
- ), O(c), _();
367
+ ), C(s), R();
348
368
  return;
349
369
  }
350
- if (k(!1), M.current)
370
+ if (_(!1), J.current)
351
371
  return;
352
- M.current = !0, W((t == null ? void 0 : t.token_endpoint) ?? "", {
372
+ J.current = !0, Y((n == null ? void 0 : n.token_endpoint) ?? "", {
353
373
  method: "POST",
354
374
  headers: {
355
375
  "Content-Type": "application/x-www-form-urlencoded"
@@ -357,50 +377,50 @@ Expected to find a '?code=' parameter in the URL but found none`
357
377
  body: new URLSearchParams({
358
378
  client_id: e,
359
379
  redirect_uri: r,
360
- code: a,
361
- code_verifier: K,
380
+ code: c,
381
+ code_verifier: X,
362
382
  grant_type: "authorization_code"
363
383
  })
364
- }).then(v).catch((c) => {
365
- console.error("[sdk/provider]: error in fetching", c), O("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");
366
386
  }), window.history.replaceState({}, document.title, window.location.pathname);
367
387
  }
368
- D(!0);
388
+ $(!0);
369
389
  }
370
- }, [s]);
371
- const I = {
372
- isAuthenticated: !!u && !P,
373
- accessToken: u,
374
- user: Z ?? null,
375
- login: z,
376
- logout: F,
377
- isReady: !s && !x
390
+ }, [a]);
391
+ const x = {
392
+ isAuthenticated: !!f && !N,
393
+ accessToken: f,
394
+ user: re ?? null,
395
+ login: B,
396
+ logout: z,
397
+ isReady: !a && !O
378
398
  };
379
- return /* @__PURE__ */ re(
380
- H.Provider,
399
+ return /* @__PURE__ */ oe(
400
+ G.Provider,
381
401
  {
382
402
  value: {
383
403
  clientId: e,
384
404
  redirectUri: r,
385
405
  env: o,
386
- login: z,
387
- logout: F,
388
- authInProgress: x,
389
- authError: X,
390
- idToken: Q,
391
- accessToken: u,
392
- refreshToken: h,
393
- user: I.user,
394
- isAuthenticated: I.isAuthenticated,
395
- isReady: I.isReady,
396
- url: i
406
+ login: B,
407
+ logout: z,
408
+ authInProgress: O,
409
+ authError: ee,
410
+ idToken: Z,
411
+ accessToken: f,
412
+ refreshToken: g,
413
+ user: x.user,
414
+ isAuthenticated: x.isAuthenticated,
415
+ isReady: x.isReady,
416
+ url: u
397
417
  },
398
- children: typeof n == "function" ? n(I) : n
418
+ children: typeof t == "function" ? t(x) : t
399
419
  }
400
420
  );
401
421
  }
402
- const Ee = () => {
403
- const e = te(H);
422
+ const Te = () => {
423
+ const e = ae(G);
404
424
  if (!e)
405
425
  throw new Error("useDeva must be used within a DevaProvider");
406
426
  return {
@@ -413,6 +433,6 @@ const Ee = () => {
413
433
  };
414
434
  };
415
435
  export {
416
- _e as DevaProvider,
417
- Ee as useDeva
436
+ Se as DevaProvider,
437
+ Te as useDeva
418
438
  };