@every-app/sdk 0.1.10 → 0.1.12

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.
Files changed (43) hide show
  1. package/dist/core/authenticatedFetch.d.ts.map +1 -1
  2. package/dist/core/authenticatedFetch.js +4 -0
  3. package/dist/core/index.d.ts +2 -2
  4. package/dist/core/index.d.ts.map +1 -1
  5. package/dist/core/index.js +1 -1
  6. package/dist/core/sessionManager.d.ts +25 -0
  7. package/dist/core/sessionManager.d.ts.map +1 -1
  8. package/dist/core/sessionManager.js +232 -33
  9. package/dist/shared/bypassGatewayLocalOnly.d.ts +14 -0
  10. package/dist/shared/bypassGatewayLocalOnly.d.ts.map +1 -0
  11. package/dist/shared/bypassGatewayLocalOnly.js +41 -0
  12. package/dist/shared/parseMessagePayload.d.ts +3 -0
  13. package/dist/shared/parseMessagePayload.d.ts.map +1 -0
  14. package/dist/shared/parseMessagePayload.js +18 -0
  15. package/dist/tanstack/EmbeddedAppProvider.d.ts.map +1 -1
  16. package/dist/tanstack/EmbeddedAppProvider.js +3 -2
  17. package/dist/tanstack/_internal/useEveryAppSession.d.ts +2 -0
  18. package/dist/tanstack/_internal/useEveryAppSession.d.ts.map +1 -1
  19. package/dist/tanstack/_internal/useEveryAppSession.js +8 -2
  20. package/dist/tanstack/server/authConfig.d.ts.map +1 -1
  21. package/dist/tanstack/server/authConfig.js +7 -1
  22. package/dist/tanstack/server/authenticateRequest.d.ts.map +1 -1
  23. package/dist/tanstack/server/authenticateRequest.js +11 -0
  24. package/dist/tanstack/useEveryAppRouter.d.ts.map +1 -1
  25. package/dist/tanstack/useEveryAppRouter.js +20 -11
  26. package/dist/tanstack/useSessionTokenClientMiddleware.d.ts.map +1 -1
  27. package/dist/tanstack/useSessionTokenClientMiddleware.js +8 -0
  28. package/package.json +1 -1
  29. package/src/cloudflare/server/gateway.test.ts +41 -9
  30. package/src/core/authenticatedFetch.ts +9 -0
  31. package/src/core/index.ts +10 -2
  32. package/src/core/sessionManager.test.ts +143 -0
  33. package/src/core/sessionManager.ts +318 -35
  34. package/src/shared/bypassGatewayLocalOnly.ts +55 -0
  35. package/src/shared/parseMessagePayload.ts +22 -0
  36. package/src/tanstack/EmbeddedAppProvider.tsx +5 -2
  37. package/src/tanstack/_internal/useEveryAppSession.test.ts +40 -0
  38. package/src/tanstack/_internal/useEveryAppSession.tsx +16 -2
  39. package/src/tanstack/server/authConfig.ts +11 -1
  40. package/src/tanstack/server/authenticateRequest.test.ts +32 -0
  41. package/src/tanstack/server/authenticateRequest.ts +21 -0
  42. package/src/tanstack/useEveryAppRouter.tsx +21 -14
  43. package/src/tanstack/useSessionTokenClientMiddleware.ts +12 -0
@@ -8,6 +8,11 @@ import {
8
8
 
9
9
  import type { AuthConfig } from "./types.js";
10
10
  import { env } from "cloudflare:workers";
11
+ import {
12
+ BYPASS_GATEWAY_LOCAL_ONLY_TOKEN,
13
+ createBypassGatewayLocalOnlySessionPayload,
14
+ isBypassGatewayLocalOnlyServer,
15
+ } from "../../shared/bypassGatewayLocalOnly.js";
11
16
 
12
17
  /**
13
18
  * JWT payload structure for embedded app session tokens.
@@ -35,6 +40,14 @@ export async function authenticateRequest(
35
40
  const request = providedRequest || getRequest();
36
41
  const authHeader = request.headers.get("authorization");
37
42
 
43
+ const bypassGatewayLocalOnlyEnv = (
44
+ env as { BYPASS_GATEWAY_LOCAL_ONLY?: string }
45
+ ).BYPASS_GATEWAY_LOCAL_ONLY;
46
+ const isBypassGatewayLocalOnly =
47
+ import.meta.env.PROD !== true &&
48
+ (bypassGatewayLocalOnlyEnv === "true" ||
49
+ isBypassGatewayLocalOnlyServer() === true);
50
+
38
51
  if (!authHeader) {
39
52
  return null;
40
53
  }
@@ -45,6 +58,14 @@ export async function authenticateRequest(
45
58
  return null;
46
59
  }
47
60
 
61
+ if (isBypassGatewayLocalOnly) {
62
+ if (token !== BYPASS_GATEWAY_LOCAL_ONLY_TOKEN) {
63
+ return null;
64
+ }
65
+
66
+ return createBypassGatewayLocalOnlySessionPayload(authConfig.audience);
67
+ }
68
+
48
69
  try {
49
70
  const session = await verifySessionToken(token, authConfig);
50
71
  return session;
@@ -1,6 +1,7 @@
1
1
  import { useEffect } from "react";
2
2
  import { SessionManager } from "../core/sessionManager.js";
3
3
  import { useRouter } from "@tanstack/react-router";
4
+ import { parseMessagePayload } from "../shared/parseMessagePayload.js";
4
5
 
5
6
  interface UseEveryAppRouterParams {
6
7
  sessionManager: SessionManager | null;
@@ -11,15 +12,20 @@ export function useEveryAppRouter({ sessionManager }: UseEveryAppRouterParams) {
11
12
  // Route synchronization effect
12
13
  useEffect(() => {
13
14
  if (!sessionManager) return;
15
+
14
16
  // Listen for route sync messages from parent
15
17
  const handleMessage = (event: MessageEvent) => {
16
- if (event.origin !== sessionManager.parentOrigin) return;
18
+ // Validate origin based on environment
19
+ if (!sessionManager.isTrustedHostMessage(event)) return;
20
+
21
+ const data = parseMessagePayload(event.data);
22
+ if (!data) return;
17
23
 
18
24
  if (
19
- event.data.type === "ROUTE_CHANGE" &&
20
- event.data.direction === "parent-to-child"
25
+ data.type === "ROUTE_CHANGE" &&
26
+ data.direction === "parent-to-child"
21
27
  ) {
22
- const targetRoute = event.data.route;
28
+ const targetRoute = typeof data.route === "string" ? data.route : null;
23
29
  const currentRoute = window.location.pathname;
24
30
 
25
31
  // Only navigate if the route is different from current location
@@ -44,16 +50,17 @@ export function useEveryAppRouter({ sessionManager }: UseEveryAppRouterParams) {
44
50
 
45
51
  lastReportedPath = currentPath;
46
52
 
47
- if (window.parent !== window) {
48
- window.parent.postMessage(
49
- {
50
- type: "ROUTE_CHANGE",
51
- route: currentPath,
52
- appId: sessionManager.appId,
53
- direction: "child-to-parent",
54
- },
55
- sessionManager.parentOrigin,
56
- );
53
+ const message = {
54
+ type: "ROUTE_CHANGE",
55
+ route: currentPath,
56
+ appId: sessionManager.appId,
57
+ direction: "child-to-parent",
58
+ };
59
+
60
+ try {
61
+ sessionManager.postToHost(message);
62
+ } catch {
63
+ return;
57
64
  }
58
65
  };
59
66
  // Listen to popstate for browser back/forward
@@ -1,9 +1,21 @@
1
1
  import { createMiddleware } from "@tanstack/react-start";
2
2
  import type { SessionManager } from "../core/sessionManager.js";
3
+ import {
4
+ BYPASS_GATEWAY_LOCAL_ONLY_TOKEN,
5
+ isBypassGatewayLocalOnlyClient,
6
+ } from "../shared/bypassGatewayLocalOnly.js";
3
7
 
4
8
  export const useSessionTokenClientMiddleware = createMiddleware({
5
9
  type: "function",
6
10
  }).client(async ({ next }) => {
11
+ if (isBypassGatewayLocalOnlyClient()) {
12
+ return next({
13
+ headers: {
14
+ Authorization: `Bearer ${BYPASS_GATEWAY_LOCAL_ONLY_TOKEN}`,
15
+ },
16
+ });
17
+ }
18
+
7
19
  // Get the global sessionManager - this MUST be available for embedded apps
8
20
  const sessionManager = (window as any)
9
21
  .__embeddedSessionManager as SessionManager;