@cloudpdf/engine 3.0.0-next.1 → 3.0.0-next.2

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.cjs CHANGED
@@ -2444,6 +2444,65 @@ async function settleLinked(pending, signal) {
2444
2444
  }
2445
2445
  }
2446
2446
  //#endregion
2447
+ //#region src/share.ts
2448
+ /**
2449
+ * Thrown on a non-OK exchange. `code` is the server's wire code:
2450
+ * `SharePasswordRequired` (prompt and retry with a password),
2451
+ * `OriginNotAllowed` / `OriginRequired`, `ShareExpired`, `NotFound`
2452
+ * (unknown or revoked — indistinguishable by design), or
2453
+ * `TooManyRequests`.
2454
+ */
2455
+ var ShareExchangeError = class extends Error {
2456
+ constructor(code, message, status) {
2457
+ super(message);
2458
+ this.code = code;
2459
+ this.status = status;
2460
+ this.name = "ShareExchangeError";
2461
+ }
2462
+ };
2463
+ async function exchangeShareToken(baseUrl, shareToken, opts = {}) {
2464
+ const res = await (opts.fetch ?? globalThis.fetch)(`${baseUrl.replace(/\/$/, "")}/v1/share-sessions`, {
2465
+ method: "POST",
2466
+ headers: { "content-type": "application/json" },
2467
+ body: JSON.stringify({
2468
+ shareToken,
2469
+ ...opts.password !== void 0 ? { password: opts.password } : {}
2470
+ })
2471
+ });
2472
+ if (!res.ok) {
2473
+ let code = "Unknown";
2474
+ let message = `share exchange failed with status ${res.status}`;
2475
+ try {
2476
+ const payload = await res.json();
2477
+ if (payload.error?.code) code = payload.error.code;
2478
+ if (payload.error?.message) message = payload.error.message;
2479
+ } catch {}
2480
+ throw new ShareExchangeError(code, message, res.status);
2481
+ }
2482
+ return await res.json();
2483
+ }
2484
+ /** Renew this many seconds before `expiresAt` so in-flight requests never race expiry. */
2485
+ const RENEW_MARGIN_SECONDS = 60;
2486
+ /**
2487
+ * A caching, self-renewing {@link TokenSource} over the exchange.
2488
+ * Concurrent callers share one in-flight exchange; a failed exchange
2489
+ * clears it so the next request retries rather than caching the error.
2490
+ */
2491
+ function shareSessionSource(baseUrl, shareToken, opts = {}) {
2492
+ let session = null;
2493
+ let inflight = null;
2494
+ return async () => {
2495
+ if (session && session.expiresAt - Date.now() / 1e3 > RENEW_MARGIN_SECONDS) return session.token;
2496
+ inflight ?? (inflight = exchangeShareToken(baseUrl, shareToken, opts).then((next) => {
2497
+ session = next;
2498
+ return next.token;
2499
+ }).finally(() => {
2500
+ inflight = null;
2501
+ }));
2502
+ return inflight;
2503
+ };
2504
+ }
2505
+ //#endregion
2447
2506
  //#region src/index.ts
2448
2507
  function createCloudEngine(opts) {
2449
2508
  return CloudEngine.fromOptions(opts);
@@ -2506,8 +2565,11 @@ Object.defineProperty(exports, "EngineErrorCode", {
2506
2565
  }
2507
2566
  });
2508
2567
  exports.HttpClient = HttpClient;
2568
+ exports.ShareExchangeError = ShareExchangeError;
2509
2569
  exports.cloudEngine = cloudEngine;
2510
2570
  exports.createCloudEngine = createCloudEngine;
2511
2571
  exports.decodeUnverifiedClaims = decodeUnverifiedClaims;
2572
+ exports.exchangeShareToken = exchangeShareToken;
2573
+ exports.shareSessionSource = shareSessionSource;
2512
2574
 
2513
2575
  //# sourceMappingURL=index.cjs.map