@calimero-network/mero-js 29.7.5 → 29.7.7

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.mjs CHANGED
@@ -1592,11 +1592,18 @@ function cancelled(message) {
1592
1592
  error.name = "NotAllowedError";
1593
1593
  return error;
1594
1594
  }
1595
- async function ask(method, options) {
1595
+ async function ask(method, options, signal, what) {
1596
1596
  const container = navigator.credentials;
1597
1597
  const own = globalThis.CredentialsContainer?.prototype?.[method];
1598
1598
  const call = own ?? container[method];
1599
- return call.call(container, options);
1599
+ try {
1600
+ return await call.call(container, signal ? { ...options, signal } : options);
1601
+ } catch (error) {
1602
+ if (signal?.aborted || error?.name === "AbortError") {
1603
+ throw Object.assign(cancelled(`${what} was cancelled`), { cause: error });
1604
+ }
1605
+ throw error;
1606
+ }
1600
1607
  }
1601
1608
  function es256First(params) {
1602
1609
  const es256 = params.filter((p) => p.alg === -7);
@@ -1604,7 +1611,7 @@ function es256First(params) {
1604
1611
  }
1605
1612
  function browserPasskey() {
1606
1613
  return {
1607
- async create(options) {
1614
+ async create(options, createOptions) {
1608
1615
  const o = options;
1609
1616
  const cred = await ask("create", {
1610
1617
  publicKey: {
@@ -1620,7 +1627,7 @@ function browserPasskey() {
1620
1627
  attestation: "none",
1621
1628
  timeout: o.timeout
1622
1629
  }
1623
- });
1630
+ }, createOptions?.signal, "passkey creation");
1624
1631
  if (!cred) throw cancelled("passkey creation was cancelled");
1625
1632
  const r = cred.response;
1626
1633
  return {
@@ -1639,7 +1646,7 @@ function browserPasskey() {
1639
1646
  id: buf(fromB64url(id))
1640
1647
  }))
1641
1648
  }
1642
- });
1649
+ }, options?.signal, "passkey sign-in");
1643
1650
  if (!cred) throw cancelled("passkey sign-in was cancelled");
1644
1651
  const r = cred.response;
1645
1652
  return {
@@ -1857,8 +1864,8 @@ function pageDeployment(href) {
1857
1864
  return null;
1858
1865
  }
1859
1866
  }
1860
- function userVerificationOf(named) {
1861
- return named === "required" || named === "preferred" || named === "discouraged" ? named : "required";
1867
+ function userVerificationOf(named, fallback) {
1868
+ return named === "required" || named === "preferred" || named === "discouraged" ? named : fallback;
1862
1869
  }
1863
1870
  function promptFailure(error) {
1864
1871
  const e = error;
@@ -2244,11 +2251,16 @@ var CalimeroClient = class _CalimeroClient {
2244
2251
  origin === void 0 ? { name, ...invite } : { name, origin, ...invite },
2245
2252
  origin
2246
2253
  );
2247
- const registration = await passkey.create(begin.options);
2248
- const session = await this.postConfirmed(
2254
+ const signal = options.signal;
2255
+ const registration = await (signal ? passkey.create(begin.options, { signal }) : passkey.create(begin.options));
2256
+ const session = await this.confirmed(
2257
+ "POST",
2249
2258
  "auth/passkey/register/finish",
2250
2259
  { challengeId: begin.challengeId, registration, ...invite },
2251
- passkey
2260
+ passkey,
2261
+ void 0,
2262
+ "preferred",
2263
+ signal
2252
2264
  );
2253
2265
  this.useSession(session);
2254
2266
  return session;
@@ -2269,7 +2281,8 @@ var CalimeroClient = class _CalimeroClient {
2269
2281
  const origin = this.pageOrigin(options);
2270
2282
  const begin = await this.begin("auth/passkey/login/begin", origin === void 0 ? void 0 : { origin }, origin);
2271
2283
  const rpId = begin.options.rpId ?? begin.options.rp?.id ?? "";
2272
- const assertion = await passkey.get(fromB64url(begin.options.challenge), rpId);
2284
+ const challenge = fromB64url(begin.options.challenge);
2285
+ const assertion = await (options.signal ? passkey.get(challenge, rpId, void 0, { signal: options.signal }) : passkey.get(challenge, rpId));
2273
2286
  const session = await this.post("auth/passkey/login/finish", {
2274
2287
  challengeId: begin.challengeId,
2275
2288
  assertion
@@ -2338,16 +2351,31 @@ var CalimeroClient = class _CalimeroClient {
2338
2351
  if (this.current === current) this.useSession(session);
2339
2352
  return session;
2340
2353
  }
2354
+ /**
2355
+ * End this session. The client is signed out whether or not the node
2356
+ * answers: the revoke (`POST /auth/logout`) is attempted, and its failure
2357
+ * (the node unreachable, say) is thrown after the session is dropped, so a
2358
+ * caller can say the server may still hold it, but no request after this
2359
+ * one carries its token.
2360
+ */
2341
2361
  async signOut() {
2342
- await this.post("auth/logout");
2343
- this.auth = void 0;
2344
- this.current = void 0;
2362
+ await this.signingOut("auth/logout");
2345
2363
  }
2346
- /** End every session of the signed-in account, on every device, this one too. */
2364
+ /**
2365
+ * End every session of the signed-in account, on every device, this one
2366
+ * too. Like {@link signOut}, this client is signed out even when the
2367
+ * request fails, and the failure is thrown.
2368
+ */
2347
2369
  async signOutEverywhere() {
2348
- await this.post("auth/logout-all");
2349
- this.auth = void 0;
2350
- this.current = void 0;
2370
+ await this.signingOut("auth/logout-all");
2371
+ }
2372
+ async signingOut(path) {
2373
+ try {
2374
+ await this.post(path);
2375
+ } finally {
2376
+ this.auth = void 0;
2377
+ this.current = void 0;
2378
+ }
2351
2379
  }
2352
2380
  /** Who the credential is: the account, how it signed in, and the package (`app`) and `origin` a session is bound to. */
2353
2381
  me() {
@@ -2639,7 +2667,7 @@ var CalimeroClient = class _CalimeroClient {
2639
2667
  deleteConfirmed(path, passkey, body = {}) {
2640
2668
  return this.confirmed("DELETE", path, body, passkey);
2641
2669
  }
2642
- async confirmed(method, path, body, passkey, send = (b) => this.json(method, path, b)) {
2670
+ async confirmed(method, path, body, passkey, send = (b) => this.json(method, path, b), userVerification = "required", signal) {
2643
2671
  try {
2644
2672
  return await send(body);
2645
2673
  } catch (error) {
@@ -2648,7 +2676,8 @@ var CalimeroClient = class _CalimeroClient {
2648
2676
  let assertion;
2649
2677
  try {
2650
2678
  assertion = await passkey.get(fromB64url(d.challenge), d.rpId, d.allowCredentials, {
2651
- userVerification: userVerificationOf(d.userVerification)
2679
+ userVerification: userVerificationOf(d.userVerification, userVerification),
2680
+ ...signal ? { signal } : {}
2652
2681
  });
2653
2682
  } catch (declined) {
2654
2683
  throw promptFailure(declined);