@rttnd/gau 1.4.0 → 1.4.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.
@@ -492,6 +492,100 @@ function createAuthClient({ baseUrl, scheme = "gau" }) {
492
492
  };
493
493
  }
494
494
 
495
+ // src/client/solid/solidStartFetchBridge.ts
496
+ init_token();
497
+ import { BROWSER as BROWSER3 } from "esm-env";
498
+ function installSolidStartFetchBridge(options = {}) {
499
+ if (!BROWSER3 || typeof window === "undefined")
500
+ return;
501
+ if (window.__GAU_SOLIDSTART_FETCH_BRIDGE_INSTALLED__)
502
+ return;
503
+ window.__GAU_SOLIDSTART_FETCH_BRIDGE_INSTALLED__ = true;
504
+ const originalFetch = globalThis.fetch.bind(globalThis);
505
+ const serverFunctionTarget = resolveServerFunctionTarget(options.serverBaseUrl);
506
+ const wrappedFetch = async (input, init = {}) => {
507
+ const requestUrl = resolveRequestUrl(input);
508
+ if (!requestUrl || !isSolidStartServerFunctionRequest(requestUrl, serverFunctionTarget))
509
+ return originalFetch(input, init);
510
+ const headers = mergeHeaders(input, init);
511
+ if (!headers.has("Authorization")) {
512
+ const token = getSessionToken();
513
+ if (token)
514
+ headers.set("Authorization", `Bearer ${token}`);
515
+ }
516
+ const response = await originalFetch(input, {
517
+ ...init,
518
+ headers
519
+ });
520
+ handleRefreshedToken(response);
521
+ return response;
522
+ };
523
+ const wrappedFetchWithBunCompat = wrappedFetch;
524
+ wrappedFetchWithBunCompat.preconnect = originalFetch.preconnect?.bind(originalFetch);
525
+ globalThis.fetch = wrappedFetchWithBunCompat;
526
+ window.fetch = wrappedFetchWithBunCompat;
527
+ }
528
+ function resolveServerFunctionTarget(serverBaseUrl) {
529
+ const envBase = serverBaseUrl ?? (import.meta?.env?.SERVER_BASE_URL ?? "");
530
+ const trimmed = envBase.trim();
531
+ if (!trimmed || trimmed === "/") {
532
+ return {
533
+ origin: window.location.origin,
534
+ path: "/_server"
535
+ };
536
+ }
537
+ try {
538
+ const baseUrl = new URL(trimmed, window.location.origin);
539
+ const basePath = normalizePath(baseUrl.pathname);
540
+ const serverPath = basePath === "/" ? "/_server" : `${basePath}/_server`;
541
+ return {
542
+ origin: baseUrl.origin,
543
+ path: normalizePath(serverPath)
544
+ };
545
+ } catch {
546
+ return {
547
+ origin: window.location.origin,
548
+ path: "/_server"
549
+ };
550
+ }
551
+ }
552
+ function resolveRequestUrl(input) {
553
+ try {
554
+ if (input instanceof URL)
555
+ return input;
556
+ if (typeof input === "string")
557
+ return new URL(input, window.location.href);
558
+ return new URL(input.url, window.location.href);
559
+ } catch {
560
+ return null;
561
+ }
562
+ }
563
+ function mergeHeaders(input, init) {
564
+ const headers = new Headers();
565
+ if (input instanceof Request) {
566
+ input.headers.forEach((value, key) => {
567
+ headers.set(key, value);
568
+ });
569
+ }
570
+ if (init.headers) {
571
+ new Headers(init.headers).forEach((value, key) => {
572
+ headers.set(key, value);
573
+ });
574
+ }
575
+ return headers;
576
+ }
577
+ function isSolidStartServerFunctionRequest(requestUrl, target) {
578
+ if (requestUrl.origin !== target.origin)
579
+ return false;
580
+ return normalizePath(requestUrl.pathname) === target.path;
581
+ }
582
+ function normalizePath(pathname) {
583
+ if (!pathname)
584
+ return "/";
585
+ const normalized = pathname.replace(/\/{2,}/g, "/").replace(/\/+$/, "");
586
+ return normalized || "/";
587
+ }
588
+
495
589
  // src/client/solid/Protected.tsx
496
590
  import { useNavigate } from "@solidjs/router";
497
591
  import { onMount, Show } from "solid-js";
@@ -526,6 +620,8 @@ var AuthContext = createContext();
526
620
  function AuthProvider(props) {
527
621
  const scheme = untrack(() => props.scheme ?? "gau");
528
622
  const baseUrl = untrack(() => props.baseUrl ?? "/api/auth");
623
+ if (!isServer2 && isTauri())
624
+ installSolidStartFetchBridge();
529
625
  const client = createAuthClient({
530
626
  baseUrl
531
627
  });
@@ -618,19 +714,22 @@ function AuthProvider(props) {
618
714
  return;
619
715
  }
620
716
  let disposed = false;
717
+ let unlisten;
621
718
  void (async () => {
622
719
  const { startAuthBridge: startAuthBridge2 } = await Promise.resolve().then(() => (init_tauri(), tauri_exports));
623
- const unlisten = await startAuthBridge2(baseUrl, scheme, async (token) => {
720
+ const maybeUnlisten = await startAuthBridge2(baseUrl, scheme, async (token) => {
624
721
  await client.applySessionToken(token);
625
722
  await doRefetch();
626
723
  });
627
724
  if (disposed)
628
- unlisten?.();
629
- else if (unlisten)
630
- onCleanup(() => unlisten());
725
+ maybeUnlisten?.();
726
+ else
727
+ unlisten = maybeUnlisten ?? void 0;
631
728
  })();
632
729
  onCleanup(() => {
633
730
  disposed = true;
731
+ unlisten?.();
732
+ unlisten = void 0;
634
733
  });
635
734
  });
636
735
  return <AuthContext.Provider value={{ session, isLoading, signIn, linkAccount, unlinkAccount, signOut, refresh: refetch, fetch: client.fetch }}>
@@ -0,0 +1,13 @@
1
+ declare global {
2
+ interface Window {
3
+ __GAU_SOLIDSTART_FETCH_BRIDGE_INSTALLED__?: boolean;
4
+ }
5
+ }
6
+ /**
7
+ * In SolidStart, query/action server functions call `/_server` through global fetch.
8
+ * In Tauri token mode this bridge injects Authorization and captures refreshed tokens.
9
+ */
10
+ export declare function installSolidStartFetchBridge(options?: {
11
+ serverBaseUrl?: string;
12
+ }): void;
13
+ //# sourceMappingURL=solidStartFetchBridge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"solidStartFetchBridge.d.ts","sourceRoot":"","sources":["../../../../src/client/solid/solidStartFetchBridge.ts"],"names":[],"mappings":"AAGA,OAAO,CAAC,MAAM,CAAC,CAAC;IACd,UAAU,MAAM;QACd,yCAAyC,CAAC,EAAE,OAAO,CAAA;KACpD;CACF;AAED;;;GAGG;AACH,wBAAgB,4BAA4B,CAAC,OAAO,GAAE;IAAE,aAAa,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,IAAI,CAqC3F"}