@sixb/app 0.1.4 → 0.1.6
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/README.md +30 -4
- package/dist/agents.d.ts +4 -4
- package/dist/agents.d.ts.map +1 -1
- package/dist/agents.js +5 -4
- package/dist/agents.js.map +3 -3
- package/dist/build.d.ts +19 -0
- package/dist/build.d.ts.map +1 -1
- package/dist/codegen.d.ts +8 -2
- package/dist/codegen.d.ts.map +1 -1
- package/dist/createCustomApp.d.ts.map +1 -1
- package/dist/dev-reload.d.ts +3 -0
- package/dist/dev-reload.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +873 -106
- package/dist/index.js.map +8 -7
- package/package.json +4 -4
- package/src/agents.ts +11 -7
- package/src/build.ts +185 -44
- package/src/codegen.ts +530 -63
- package/src/createCustomApp.ts +292 -23
- package/src/dev-reload.ts +54 -0
- package/src/index.ts +1 -0
- package/src/scanner.ts +8 -2
package/src/codegen.ts
CHANGED
|
@@ -19,6 +19,17 @@ export interface GenerateRouteManifestOptions {
|
|
|
19
19
|
|
|
20
20
|
export const AUTH_EXPERIENCE_BOOTSTRAP_PLACEHOLDER = "__SIXB_AUTH_BOOTSTRAP__"
|
|
21
21
|
|
|
22
|
+
function assertNoReservedSharedRoutes(routes: readonly PageRoute[]): void {
|
|
23
|
+
const reservedRoute = routes.find(
|
|
24
|
+
(route) => route.path === "/shared" || route.path.startsWith("/shared/")
|
|
25
|
+
)
|
|
26
|
+
if (!reservedRoute) return
|
|
27
|
+
|
|
28
|
+
throw new Error(
|
|
29
|
+
`[SixbCustomApp] ${reservedRoute.relativePath} maps to the reserved ${JSON.stringify(reservedRoute.path)} route. Shared links reuse ordinary app pages; move this page outside app/shared/.`
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
22
33
|
/**
|
|
23
34
|
* Generates `.sixb/generated/routes.ts` with static page/layout imports. Modules are
|
|
24
35
|
* eager on purpose: project-specific apps bundle small, and a single bundle
|
|
@@ -30,6 +41,7 @@ export async function generateRouteManifest(
|
|
|
30
41
|
generatedDir: string,
|
|
31
42
|
options: GenerateRouteManifestOptions = {}
|
|
32
43
|
): Promise<string> {
|
|
44
|
+
assertNoReservedSharedRoutes(routes)
|
|
33
45
|
await mkdir(generatedDir, { recursive: true })
|
|
34
46
|
|
|
35
47
|
const pages = [...routes].sort(compareRouteModules)
|
|
@@ -411,8 +423,11 @@ ${renderAuthMetadataHead(metadata)}
|
|
|
411
423
|
|
|
412
424
|
/**
|
|
413
425
|
* Generates the app entry files:
|
|
414
|
-
* - `.sixb/generated/index.html` — HTML shell
|
|
415
|
-
* - `.sixb/generated/main.tsx` —
|
|
426
|
+
* - `.sixb/generated/index.html` — ordinary authenticated HTML shell
|
|
427
|
+
* - `.sixb/generated/main.tsx` — ordinary authentication bootstrap
|
|
428
|
+
* - `.sixb/generated/app-runtime.tsx` — shared route, layout, and query runtime
|
|
429
|
+
* - `.sixb/generated/shared-index.html` — isolated shared-link HTML shell
|
|
430
|
+
* - `.sixb/generated/shared-main.tsx` — delegated-session bootstrap
|
|
416
431
|
* - `.sixb/generated/app.webmanifest` — installable app identity
|
|
417
432
|
*/
|
|
418
433
|
export async function generateAppEntry(
|
|
@@ -433,7 +448,14 @@ export async function generateAppEntry(
|
|
|
433
448
|
/** Framework-owned stylesheets imported before the app stylesheet. */
|
|
434
449
|
frameworkStylesheetPaths?: readonly string[]
|
|
435
450
|
} = {}
|
|
436
|
-
): Promise<{
|
|
451
|
+
): Promise<{
|
|
452
|
+
htmlPath: string
|
|
453
|
+
mainPath: string
|
|
454
|
+
manifestPath: string
|
|
455
|
+
runtimePath: string
|
|
456
|
+
sharedHtmlPath: string
|
|
457
|
+
sharedMainPath: string
|
|
458
|
+
}> {
|
|
437
459
|
await mkdir(generatedDir, { recursive: true })
|
|
438
460
|
|
|
439
461
|
const appDir = options.appDir ? resolve(projectRoot, options.appDir) : join(projectRoot, "app")
|
|
@@ -470,8 +492,9 @@ export async function generateAppEntry(
|
|
|
470
492
|
})
|
|
471
493
|
: ""
|
|
472
494
|
|
|
473
|
-
//
|
|
474
|
-
|
|
495
|
+
// Keep the authored route tree in one runtime. The normal entry imports it directly; the
|
|
496
|
+
// shared entry imports it only after establishing a grant-bound session.
|
|
497
|
+
const runtimeContent = `import React from "react"
|
|
475
498
|
import { createRoot, type Root } from "react-dom/client"
|
|
476
499
|
import { signOut } from "@sixb/client"
|
|
477
500
|
import {
|
|
@@ -482,12 +505,7 @@ import {
|
|
|
482
505
|
useRoutes,
|
|
483
506
|
} from "react-router-dom"
|
|
484
507
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
|
|
485
|
-
import {
|
|
486
|
-
configureSixbBrowserClient,
|
|
487
|
-
isSixbApiError,
|
|
488
|
-
readSixbBrowserRuntimeConfig,
|
|
489
|
-
requireSixbBrowserAuthSession,
|
|
490
|
-
} from "@sixb/client/browser"
|
|
508
|
+
import { isSixbApiError } from "@sixb/client/browser"
|
|
491
509
|
import { routePaths, routes } from "./routes"
|
|
492
510
|
${globalsCssImport}
|
|
493
511
|
${layoutImport}
|
|
@@ -497,24 +515,11 @@ interface CustomAppHotData {
|
|
|
497
515
|
queryClient?: QueryClient
|
|
498
516
|
}
|
|
499
517
|
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
if (import.meta.hot) {
|
|
504
|
-
import.meta.hot.accept()
|
|
505
|
-
import.meta.hot.dispose(() => {
|
|
506
|
-
browserClient.dispose()
|
|
507
|
-
})
|
|
518
|
+
export interface StartAppOptions {
|
|
519
|
+
readonly basename?: string
|
|
508
520
|
}
|
|
509
521
|
|
|
510
|
-
|
|
511
|
-
? await requireSixbBrowserAuthSession(runtimeConfig, browserClient)
|
|
512
|
-
: null
|
|
513
|
-
const applicationAccessDenied =
|
|
514
|
-
authSession?.authenticated === true && !authSession.applicationAccess.allowed
|
|
515
|
-
const canRenderApp =
|
|
516
|
-
!runtimeConfig.auth.enabled ||
|
|
517
|
-
(authSession?.authenticated === true && authSession.applicationAccess.allowed)
|
|
522
|
+
if (import.meta.hot) import.meta.hot.accept()
|
|
518
523
|
|
|
519
524
|
const queryClient = getQueryClient()
|
|
520
525
|
|
|
@@ -554,7 +559,7 @@ function createQueryClient(): QueryClient {
|
|
|
554
559
|
})
|
|
555
560
|
}
|
|
556
561
|
|
|
557
|
-
const RESERVED_PATH_PREFIXES = ["/api", "/auth", "/ws", "/docs"]
|
|
562
|
+
const RESERVED_PATH_PREFIXES = ["/api", "/auth", "/ws", "/docs", "/shared"]
|
|
558
563
|
|
|
559
564
|
function isReservedPath(pathname: string) {
|
|
560
565
|
return RESERVED_PATH_PREFIXES.some(
|
|
@@ -562,33 +567,74 @@ function isReservedPath(pathname: string) {
|
|
|
562
567
|
)
|
|
563
568
|
}
|
|
564
569
|
|
|
570
|
+
function isSharedTransportPath(pathname: string): boolean {
|
|
571
|
+
return pathname === "/shared" || pathname.startsWith("/shared/")
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
function primaryNavigationAnchor(event: MouseEvent): HTMLAnchorElement | null {
|
|
575
|
+
if (event.defaultPrevented) return null
|
|
576
|
+
if (event.button !== 0) return null
|
|
577
|
+
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return null
|
|
578
|
+
|
|
579
|
+
const target = event.target
|
|
580
|
+
if (!(target instanceof Element)) return null
|
|
581
|
+
const anchor = target.closest("a")
|
|
582
|
+
if (!(anchor instanceof HTMLAnchorElement)) return null
|
|
583
|
+
if (anchor.target && anchor.target !== "_self") return null
|
|
584
|
+
if (anchor.hasAttribute("download")) return null
|
|
585
|
+
if (anchor.relList.contains("external")) return null
|
|
586
|
+
if (!anchor.getAttribute("href")) return null
|
|
587
|
+
return anchor
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
// A React Router <Link> mutates the current document before effects can reload it. Capture shared
|
|
591
|
+
// transport clicks first so an OIDC document never acquires a bearer fragment.
|
|
592
|
+
function SharedDocumentLinkInterceptor({ basename }: { readonly basename?: string }) {
|
|
593
|
+
React.useLayoutEffect(() => {
|
|
594
|
+
function onClick(event: MouseEvent) {
|
|
595
|
+
const anchor = primaryNavigationAnchor(event)
|
|
596
|
+
if (!anchor) return
|
|
597
|
+
|
|
598
|
+
const url = new URL(anchor.href, window.location.href)
|
|
599
|
+
if (url.origin !== window.location.origin) return
|
|
600
|
+
const appPath = pathInsideApp(url.pathname, basename)
|
|
601
|
+
if (!isSharedTransportPath(appPath)) return
|
|
602
|
+
|
|
603
|
+
// React Router prefixes an absolute <Link to="/shared/..."> with the current basename.
|
|
604
|
+
// Rebuild the transport URL from the path inside the app so switching grants does not nest
|
|
605
|
+
// the new link beneath /shared/:currentGrantId.
|
|
606
|
+
const documentUrl = new URL(appPath + url.search + url.hash, url.origin)
|
|
607
|
+
|
|
608
|
+
event.preventDefault()
|
|
609
|
+
event.stopImmediatePropagation()
|
|
610
|
+
window.location.assign(documentUrl.href)
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
document.addEventListener("click", onClick, true)
|
|
614
|
+
return () => document.removeEventListener("click", onClick, true)
|
|
615
|
+
}, [basename])
|
|
616
|
+
|
|
617
|
+
return null
|
|
618
|
+
}
|
|
619
|
+
|
|
565
620
|
// Makes plain same-origin <a href="/..."> clicks navigate client-side, so app
|
|
566
621
|
// authors get SPA navigation without remembering react-router's <Link>.
|
|
567
622
|
// Deliberately conservative: anything unusual falls through to the browser.
|
|
568
|
-
function InternalLinkInterceptor() {
|
|
623
|
+
function InternalLinkInterceptor({ basename }: { readonly basename?: string }) {
|
|
569
624
|
const navigate = useNavigate()
|
|
570
625
|
|
|
571
626
|
React.useEffect(() => {
|
|
572
627
|
function onClick(event: MouseEvent) {
|
|
573
|
-
|
|
574
|
-
if (
|
|
575
|
-
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return
|
|
576
|
-
|
|
577
|
-
const target = event.target
|
|
578
|
-
if (!(target instanceof Element)) return
|
|
579
|
-
const anchor = target.closest("a")
|
|
580
|
-
if (!(anchor instanceof HTMLAnchorElement)) return
|
|
581
|
-
if (anchor.target && anchor.target !== "_self") return
|
|
582
|
-
if (anchor.hasAttribute("download")) return
|
|
583
|
-
if (anchor.relList.contains("external")) return
|
|
584
|
-
if (!anchor.getAttribute("href")) return
|
|
628
|
+
const anchor = primaryNavigationAnchor(event)
|
|
629
|
+
if (!anchor) return
|
|
585
630
|
|
|
586
631
|
const url = new URL(anchor.href, window.location.href)
|
|
587
632
|
if (url.origin !== window.location.origin) return
|
|
588
|
-
|
|
633
|
+
const appPath = pathInsideApp(url.pathname, basename)
|
|
634
|
+
if (isReservedPath(appPath)) return
|
|
589
635
|
// Only intercept destinations the app actually routes; anything else may
|
|
590
636
|
// be a real server resource and keeps native navigation.
|
|
591
|
-
if (!routePaths.some((path) => matchPath(path,
|
|
637
|
+
if (!routePaths.some((path) => matchPath(path, appPath))) return
|
|
592
638
|
// Same-document hash links keep native scroll behavior.
|
|
593
639
|
if (
|
|
594
640
|
url.hash &&
|
|
@@ -599,16 +645,39 @@ function InternalLinkInterceptor() {
|
|
|
599
645
|
}
|
|
600
646
|
|
|
601
647
|
event.preventDefault()
|
|
602
|
-
navigate(
|
|
648
|
+
navigate(appPath + url.search + url.hash)
|
|
603
649
|
}
|
|
604
650
|
|
|
605
651
|
document.addEventListener("click", onClick)
|
|
606
652
|
return () => document.removeEventListener("click", onClick)
|
|
607
|
-
}, [navigate])
|
|
653
|
+
}, [basename, navigate])
|
|
608
654
|
|
|
609
655
|
return null
|
|
610
656
|
}
|
|
611
657
|
|
|
658
|
+
function pathInsideApp(pathname: string, basename: string | undefined): string {
|
|
659
|
+
if (
|
|
660
|
+
basename &&
|
|
661
|
+
(pathname === basename || pathname.startsWith(basename.endsWith("/") ? basename : basename + "/"))
|
|
662
|
+
) {
|
|
663
|
+
return pathname.slice(basename.length) || "/"
|
|
664
|
+
}
|
|
665
|
+
return pathname
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// Programmatic React Router navigation cannot be blocked by BrowserRouter. Gate the ordinary page
|
|
669
|
+
// tree while forcing the new document so no route can run under the previous OIDC authority.
|
|
670
|
+
function SharedDocumentBoundary({ children }: { readonly children: React.ReactNode }) {
|
|
671
|
+
const location = useLocation()
|
|
672
|
+
const crossingBoundary = isSharedTransportPath(location.pathname)
|
|
673
|
+
|
|
674
|
+
React.useLayoutEffect(() => {
|
|
675
|
+
if (crossingBoundary) window.location.reload()
|
|
676
|
+
}, [crossingBoundary])
|
|
677
|
+
|
|
678
|
+
return crossingBoundary ? null : children
|
|
679
|
+
}
|
|
680
|
+
|
|
612
681
|
// Shared presentational fallback used by the not-found view and the error
|
|
613
682
|
// boundary. Styled with @sixb/ui design tokens (var(--token)) so it adopts the
|
|
614
683
|
// app's theme when those tokens are loaded, but every token has a hardcoded
|
|
@@ -743,20 +812,32 @@ function NotFoundView() {
|
|
|
743
812
|
// of a blank page and a console error. A missing object is an expected state,
|
|
744
813
|
// so a 404 reuses the not-found view rather than the generic crash screen.
|
|
745
814
|
// Apps will be able to override this via app/error.tsx.
|
|
746
|
-
function AppErrorFallback({
|
|
815
|
+
function AppErrorFallback({
|
|
816
|
+
error,
|
|
817
|
+
hideErrorDetails,
|
|
818
|
+
}: {
|
|
819
|
+
error: unknown
|
|
820
|
+
hideErrorDetails: boolean
|
|
821
|
+
}) {
|
|
747
822
|
if (isSixbApiError(error) && error.status === 404) {
|
|
748
823
|
return <NotFoundView />
|
|
749
824
|
}
|
|
750
825
|
return (
|
|
751
826
|
<AppFallback
|
|
752
827
|
title="Something went wrong"
|
|
753
|
-
detail={
|
|
828
|
+
detail={
|
|
829
|
+
hideErrorDetails
|
|
830
|
+
? "This shared page could not be displayed."
|
|
831
|
+
: error instanceof Error
|
|
832
|
+
? error.message
|
|
833
|
+
: String(error)
|
|
834
|
+
}
|
|
754
835
|
/>
|
|
755
836
|
)
|
|
756
837
|
}
|
|
757
838
|
|
|
758
839
|
class AppErrorBoundary extends React.Component<
|
|
759
|
-
{ resetKey: string; children: React.ReactNode },
|
|
840
|
+
{ resetKey: string; hideErrorDetails: boolean; children: React.ReactNode },
|
|
760
841
|
{ error: unknown }
|
|
761
842
|
> {
|
|
762
843
|
state: { error: unknown } = { error: null }
|
|
@@ -766,7 +847,11 @@ class AppErrorBoundary extends React.Component<
|
|
|
766
847
|
}
|
|
767
848
|
|
|
768
849
|
componentDidCatch(error: unknown) {
|
|
769
|
-
|
|
850
|
+
if (this.props.hideErrorDetails) {
|
|
851
|
+
console.error("[SixbApp] A shared page failed to render.")
|
|
852
|
+
} else {
|
|
853
|
+
console.error("[SixbApp] Uncaught render error:", error)
|
|
854
|
+
}
|
|
770
855
|
}
|
|
771
856
|
|
|
772
857
|
// Clear the error when the route changes so navigating away (or an in-app
|
|
@@ -779,16 +864,32 @@ class AppErrorBoundary extends React.Component<
|
|
|
779
864
|
|
|
780
865
|
render() {
|
|
781
866
|
if (this.state.error !== null) {
|
|
782
|
-
return
|
|
867
|
+
return (
|
|
868
|
+
<AppErrorFallback
|
|
869
|
+
error={this.state.error}
|
|
870
|
+
hideErrorDetails={this.props.hideErrorDetails}
|
|
871
|
+
/>
|
|
872
|
+
)
|
|
783
873
|
}
|
|
784
874
|
return this.props.children
|
|
785
875
|
}
|
|
786
876
|
}
|
|
787
877
|
|
|
788
|
-
function RoutedErrorBoundary({
|
|
878
|
+
function RoutedErrorBoundary({
|
|
879
|
+
children,
|
|
880
|
+
hideErrorDetails,
|
|
881
|
+
}: {
|
|
882
|
+
children: React.ReactNode
|
|
883
|
+
hideErrorDetails: boolean
|
|
884
|
+
}) {
|
|
789
885
|
const location = useLocation()
|
|
790
886
|
return (
|
|
791
|
-
<AppErrorBoundary
|
|
887
|
+
<AppErrorBoundary
|
|
888
|
+
resetKey={location.pathname + location.search}
|
|
889
|
+
hideErrorDetails={hideErrorDetails}
|
|
890
|
+
>
|
|
891
|
+
{children}
|
|
892
|
+
</AppErrorBoundary>
|
|
792
893
|
)
|
|
793
894
|
}
|
|
794
895
|
|
|
@@ -798,31 +899,280 @@ function AppRoutes() {
|
|
|
798
899
|
return useRoutes(appRoutes)
|
|
799
900
|
}
|
|
800
901
|
|
|
801
|
-
function
|
|
902
|
+
function RoutedApp({ hideErrorDetails }: { readonly hideErrorDetails: boolean }) {
|
|
903
|
+
return (
|
|
904
|
+
<RoutedErrorBoundary hideErrorDetails={hideErrorDetails}>
|
|
905
|
+
${layoutWrapperStart}
|
|
906
|
+
<AppRoutes />
|
|
907
|
+
${layoutWrapperEnd}
|
|
908
|
+
</RoutedErrorBoundary>
|
|
909
|
+
)
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
function App({ basename }: StartAppOptions) {
|
|
802
913
|
return (
|
|
803
914
|
<QueryClientProvider client={queryClient}>
|
|
804
|
-
<BrowserRouter>
|
|
805
|
-
<
|
|
806
|
-
<
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
915
|
+
<BrowserRouter basename={basename}>
|
|
916
|
+
<SharedDocumentLinkInterceptor basename={basename} />
|
|
917
|
+
<InternalLinkInterceptor basename={basename} />
|
|
918
|
+
{basename ? (
|
|
919
|
+
<RoutedApp hideErrorDetails />
|
|
920
|
+
) : (
|
|
921
|
+
<SharedDocumentBoundary>
|
|
922
|
+
<RoutedApp hideErrorDetails={false} />
|
|
923
|
+
</SharedDocumentBoundary>
|
|
924
|
+
)}
|
|
811
925
|
</BrowserRouter>
|
|
812
926
|
</QueryClientProvider>
|
|
813
927
|
)
|
|
814
928
|
}
|
|
815
929
|
|
|
930
|
+
export function startApp(options: StartAppOptions = {}) {
|
|
931
|
+
getRoot().render(<App basename={options.basename} />)
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
export function renderAccessDenied() {
|
|
935
|
+
getRoot().render(<AccessDeniedView />)
|
|
936
|
+
}
|
|
937
|
+
`
|
|
938
|
+
|
|
939
|
+
const runtimePath = join(generatedDir, "app-runtime.tsx")
|
|
940
|
+
await writeFileIfChanged(runtimePath, runtimeContent)
|
|
941
|
+
|
|
942
|
+
// The normal entry owns only ambient application authentication. Its imported runtime is the
|
|
943
|
+
// exact same module the shared bootstrap loads after establishing delegated authority.
|
|
944
|
+
const mainContent = `import {
|
|
945
|
+
configureSixbBrowserClient,
|
|
946
|
+
readSixbBrowserRuntimeConfig,
|
|
947
|
+
requireSixbBrowserAuthSession,
|
|
948
|
+
} from "@sixb/client/browser"
|
|
949
|
+
import { renderAccessDenied, startApp } from "./app-runtime"
|
|
950
|
+
|
|
951
|
+
const runtimeConfig = readSixbBrowserRuntimeConfig({ audience: "app" })
|
|
952
|
+
const browserClient = configureSixbBrowserClient(runtimeConfig)
|
|
953
|
+
|
|
954
|
+
if (import.meta.hot) {
|
|
955
|
+
import.meta.hot.accept()
|
|
956
|
+
import.meta.hot.dispose(() => browserClient.dispose())
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
const authSession = runtimeConfig.auth.enabled
|
|
960
|
+
? await requireSixbBrowserAuthSession(runtimeConfig, browserClient)
|
|
961
|
+
: null
|
|
962
|
+
const applicationAccessDenied =
|
|
963
|
+
authSession?.authenticated === true && !authSession.applicationAccess.allowed
|
|
964
|
+
const canRenderApp =
|
|
965
|
+
!runtimeConfig.auth.enabled ||
|
|
966
|
+
(authSession?.authenticated === true && authSession.applicationAccess.allowed)
|
|
967
|
+
|
|
816
968
|
if (canRenderApp) {
|
|
817
|
-
|
|
969
|
+
startApp()
|
|
818
970
|
} else if (applicationAccessDenied) {
|
|
819
|
-
|
|
971
|
+
renderAccessDenied()
|
|
820
972
|
}
|
|
821
973
|
`
|
|
822
974
|
|
|
823
975
|
const mainPath = join(generatedDir, "main.tsx")
|
|
824
976
|
await writeFileIfChanged(mainPath, mainContent)
|
|
825
977
|
|
|
978
|
+
// This module contains framework bootstrap only. It must never statically import routes, the
|
|
979
|
+
// authored layout, or any page: that graph is imported after the session is accepted.
|
|
980
|
+
const sharedMainContent = `import {
|
|
981
|
+
configureSixbSharedBrowserClient,
|
|
982
|
+
isSixbApiError,
|
|
983
|
+
readSixbBrowserRuntimeConfig,
|
|
984
|
+
type SixbSharedBrowserClientController,
|
|
985
|
+
} from "@sixb/client/browser"
|
|
986
|
+
|
|
987
|
+
export interface SharedAppBootstrap {
|
|
988
|
+
readonly basename: string
|
|
989
|
+
readonly grantId: string
|
|
990
|
+
readonly requestedPath: string
|
|
991
|
+
readonly secret: string | null
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
export interface SharedAppStartOptions {
|
|
995
|
+
readonly loadAppStyles?: () => Promise<void>
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
let activeController: SixbSharedBrowserClientController | null = null
|
|
999
|
+
|
|
1000
|
+
if (import.meta.hot) {
|
|
1001
|
+
import.meta.hot.accept()
|
|
1002
|
+
import.meta.hot.dispose(() => {
|
|
1003
|
+
activeController?.dispose()
|
|
1004
|
+
activeController = null
|
|
1005
|
+
})
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
export function startSharedApp(
|
|
1009
|
+
bootstrap: SharedAppBootstrap,
|
|
1010
|
+
options: SharedAppStartOptions = {}
|
|
1011
|
+
): void {
|
|
1012
|
+
if (!isValidBootstrap(bootstrap)) {
|
|
1013
|
+
renderSharedFallback("unavailable")
|
|
1014
|
+
return
|
|
1015
|
+
}
|
|
1016
|
+
const { basename, grantId } = bootstrap
|
|
1017
|
+
let loadAppStyles = options.loadAppStyles
|
|
1018
|
+
|
|
1019
|
+
activeController?.dispose()
|
|
1020
|
+
let controller: SixbSharedBrowserClientController
|
|
1021
|
+
try {
|
|
1022
|
+
const runtimeConfig = readSixbBrowserRuntimeConfig({
|
|
1023
|
+
audience: "app",
|
|
1024
|
+
authEnabled: false,
|
|
1025
|
+
})
|
|
1026
|
+
controller = configureSixbSharedBrowserClient(runtimeConfig, {
|
|
1027
|
+
grantId,
|
|
1028
|
+
})
|
|
1029
|
+
activeController = controller
|
|
1030
|
+
} catch {
|
|
1031
|
+
renderSharedFallback("unavailable")
|
|
1032
|
+
return
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
let secret = bootstrap.secret
|
|
1036
|
+
let canonicalDestinationPath: string | null = null
|
|
1037
|
+
let appStylesLoaded = false
|
|
1038
|
+
let pending = false
|
|
1039
|
+
|
|
1040
|
+
const open = async () => {
|
|
1041
|
+
if (pending) return
|
|
1042
|
+
pending = true
|
|
1043
|
+
renderSharedFallback("loading")
|
|
1044
|
+
|
|
1045
|
+
try {
|
|
1046
|
+
const exchangedSecret = secret !== null
|
|
1047
|
+
const session = await controller.establish(secret)
|
|
1048
|
+
// Once a server session exists the one-time link credential must not survive a retry.
|
|
1049
|
+
secret = null
|
|
1050
|
+
if (exchangedSecret) canonicalDestinationPath = session.destinationPath
|
|
1051
|
+
// Keep the trusted destination pending until this document is canonical or a hard
|
|
1052
|
+
// navigation to it has started. Application code never imports under the caller's path.
|
|
1053
|
+
if (canonicalDestinationPath !== null) {
|
|
1054
|
+
try {
|
|
1055
|
+
if (!canonicalizeInitialDestination(basename, canonicalDestinationPath)) return
|
|
1056
|
+
} catch (error) {
|
|
1057
|
+
// Both same-document canonicalization and the hard-navigation fallback failed. Retire
|
|
1058
|
+
// the newly established session before showing a terminal error so a reload cannot
|
|
1059
|
+
// restore it under the caller-controlled path.
|
|
1060
|
+
await controller.signOut().catch(() => undefined)
|
|
1061
|
+
controller.dispose()
|
|
1062
|
+
if (activeController === controller) activeController = null
|
|
1063
|
+
throw error
|
|
1064
|
+
}
|
|
1065
|
+
canonicalDestinationPath = null
|
|
1066
|
+
}
|
|
1067
|
+
if (!appStylesLoaded) {
|
|
1068
|
+
await loadAppStyles?.()
|
|
1069
|
+
loadAppStyles = undefined
|
|
1070
|
+
appStylesLoaded = true
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
const { startApp } = await import("./app-runtime")
|
|
1074
|
+
startApp({ basename })
|
|
1075
|
+
} catch (error) {
|
|
1076
|
+
if (isRetryableSharedFailure(error)) {
|
|
1077
|
+
console.error("[SixbSharedApp] Shared access could not be opened; retry is available.")
|
|
1078
|
+
renderSharedFallback("retryable", () => void open())
|
|
1079
|
+
} else {
|
|
1080
|
+
renderSharedFallback("unavailable")
|
|
1081
|
+
}
|
|
1082
|
+
} finally {
|
|
1083
|
+
pending = false
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
void open()
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
function isValidBootstrap(value: SharedAppBootstrap): boolean {
|
|
1091
|
+
return (
|
|
1092
|
+
typeof value.grantId === "string" &&
|
|
1093
|
+
value.basename === "/shared/" + encodeURIComponent(value.grantId) &&
|
|
1094
|
+
value.requestedPath.startsWith("/") &&
|
|
1095
|
+
(value.secret === null || typeof value.secret === "string")
|
|
1096
|
+
)
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
function canonicalizeInitialDestination(basename: string, destinationPath: string): boolean {
|
|
1100
|
+
const canonicalPath = basename + destinationPath
|
|
1101
|
+
if (
|
|
1102
|
+
window.location.pathname !== canonicalPath ||
|
|
1103
|
+
window.location.search !== "" ||
|
|
1104
|
+
window.location.hash !== ""
|
|
1105
|
+
) {
|
|
1106
|
+
try {
|
|
1107
|
+
window.history.replaceState(window.history.state, "", canonicalPath)
|
|
1108
|
+
} catch {
|
|
1109
|
+
// Preserve the authority boundary even when a browser or embedding policy denies History
|
|
1110
|
+
// mutation: a fresh document at the trusted server destination is still safe.
|
|
1111
|
+
window.location.replace(canonicalPath)
|
|
1112
|
+
return false
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
return true
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
function isRetryableSharedFailure(error: unknown): boolean {
|
|
1119
|
+
if (error instanceof TypeError) return true
|
|
1120
|
+
return (
|
|
1121
|
+
isSixbApiError(error) &&
|
|
1122
|
+
(error.status === 408 || error.status === 429 || error.status >= 500)
|
|
1123
|
+
)
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
function renderSharedFallback(
|
|
1127
|
+
state: "loading" | "retryable" | "unavailable",
|
|
1128
|
+
retry?: () => void
|
|
1129
|
+
): void {
|
|
1130
|
+
const root = document.getElementById("root")
|
|
1131
|
+
if (!root) return
|
|
1132
|
+
|
|
1133
|
+
const main = document.createElement("main")
|
|
1134
|
+
main.setAttribute("role", state === "loading" ? "status" : "alert")
|
|
1135
|
+
main.style.cssText =
|
|
1136
|
+
"min-height:100dvh;display:flex;flex-direction:column;align-items:center;" +
|
|
1137
|
+
"justify-content:center;gap:.75rem;padding:2rem;text-align:center;" +
|
|
1138
|
+
"font-family:system-ui,-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif"
|
|
1139
|
+
|
|
1140
|
+
const title = document.createElement("h1")
|
|
1141
|
+
title.style.cssText = "margin:0;font-size:1.5rem"
|
|
1142
|
+
title.textContent =
|
|
1143
|
+
state === "loading"
|
|
1144
|
+
? "Opening shared access…"
|
|
1145
|
+
: state === "retryable"
|
|
1146
|
+
? "Unable to open this link"
|
|
1147
|
+
: "Link unavailable"
|
|
1148
|
+
const detail = document.createElement("p")
|
|
1149
|
+
detail.style.cssText = "margin:0;max-width:32rem"
|
|
1150
|
+
detail.textContent =
|
|
1151
|
+
state === "loading"
|
|
1152
|
+
? "Please wait while this link is verified."
|
|
1153
|
+
: state === "retryable"
|
|
1154
|
+
? "A temporary problem occurred. Please try again."
|
|
1155
|
+
: "This shared link is invalid, expired, or no longer available."
|
|
1156
|
+
main.append(title, detail)
|
|
1157
|
+
|
|
1158
|
+
if (retry) {
|
|
1159
|
+
const button = document.createElement("button")
|
|
1160
|
+
button.type = "button"
|
|
1161
|
+
button.textContent = "Try again"
|
|
1162
|
+
button.style.cssText =
|
|
1163
|
+
"margin-top:.25rem;padding:.625rem 1rem;border:1px solid currentColor;" +
|
|
1164
|
+
"border-radius:.375rem;background:transparent;color:inherit;cursor:pointer;font:inherit"
|
|
1165
|
+
button.addEventListener("click", retry, { once: true })
|
|
1166
|
+
main.append(button)
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
root.replaceChildren(main)
|
|
1170
|
+
}
|
|
1171
|
+
`
|
|
1172
|
+
|
|
1173
|
+
const sharedMainPath = join(generatedDir, "shared-main.tsx")
|
|
1174
|
+
await writeFileIfChanged(sharedMainPath, sharedMainContent)
|
|
1175
|
+
|
|
826
1176
|
// Generate index.html with static identity and only structural reset rules.
|
|
827
1177
|
// Apps own visual styling through app/globals.css.
|
|
828
1178
|
const metadataHead = renderMetadataHead(metadata)
|
|
@@ -866,7 +1216,124 @@ ${metadataHead}
|
|
|
866
1216
|
const htmlPath = join(generatedDir, "index.html")
|
|
867
1217
|
await writeFileIfChanged(htmlPath, htmlContent)
|
|
868
1218
|
|
|
869
|
-
|
|
1219
|
+
const sharedRuntimeConfigScript = options.apiBaseUrl
|
|
1220
|
+
? renderCustomAppRuntimeScript({
|
|
1221
|
+
api: { baseUrl: options.apiBaseUrl },
|
|
1222
|
+
auth: { audience: "app", enabled: false },
|
|
1223
|
+
})
|
|
1224
|
+
: ""
|
|
1225
|
+
const sharedHtmlContent = `<!DOCTYPE html>
|
|
1226
|
+
<html lang="en">
|
|
1227
|
+
<head>
|
|
1228
|
+
<meta charset="UTF-8" />
|
|
1229
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
|
|
1230
|
+
<meta name="robots" content="noindex, nofollow, noarchive" />
|
|
1231
|
+
<meta name="referrer" content="no-referrer" />
|
|
1232
|
+
${renderSharedMetadataHead(metadata)}
|
|
1233
|
+
${sharedRuntimeConfigScript}
|
|
1234
|
+
<style>
|
|
1235
|
+
* { box-sizing: border-box; }
|
|
1236
|
+
html, body, #root { margin: 0; min-height: 100%; }
|
|
1237
|
+
body, #root { min-height: 100vh; min-height: 100dvh; }
|
|
1238
|
+
</style>
|
|
1239
|
+
</head>
|
|
1240
|
+
<body>
|
|
1241
|
+
<div id="root"></div>
|
|
1242
|
+
<script>
|
|
1243
|
+
function renderUnavailable() {
|
|
1244
|
+
const root = document.getElementById("root")
|
|
1245
|
+
if (!root) return
|
|
1246
|
+
const main = document.createElement("main")
|
|
1247
|
+
main.setAttribute("role", "alert")
|
|
1248
|
+
main.style.cssText =
|
|
1249
|
+
"min-height:100dvh;display:grid;place-items:center;padding:2rem;text-align:center;" +
|
|
1250
|
+
"font-family:system-ui,-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif"
|
|
1251
|
+
const message = document.createElement("p")
|
|
1252
|
+
message.textContent = "This shared link is invalid, expired, or no longer available."
|
|
1253
|
+
main.append(message)
|
|
1254
|
+
root.replaceChildren(main)
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
function consumeBootstrap() {
|
|
1258
|
+
const rawFragment = window.location.hash.startsWith("#")
|
|
1259
|
+
? window.location.hash.slice(1)
|
|
1260
|
+
: window.location.hash
|
|
1261
|
+
// Only the canonical 32-byte bearer belongs to the transport. Ordinary page anchors must
|
|
1262
|
+
// remain intact when an established shared session is reloaded.
|
|
1263
|
+
const secret = /^[A-Za-z0-9_-]{43}$/.test(rawFragment) ? rawFragment : null
|
|
1264
|
+
if (secret !== null) {
|
|
1265
|
+
try {
|
|
1266
|
+
window.history.replaceState(
|
|
1267
|
+
window.history.state,
|
|
1268
|
+
"",
|
|
1269
|
+
window.location.pathname + window.location.search
|
|
1270
|
+
)
|
|
1271
|
+
} catch {
|
|
1272
|
+
return null
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
const match = /^\\/shared\\/([^/]+)(\\/.*)?$/.exec(window.location.pathname)
|
|
1277
|
+
if (!match) return null
|
|
1278
|
+
let grantId
|
|
1279
|
+
try {
|
|
1280
|
+
grantId = decodeURIComponent(match[1])
|
|
1281
|
+
} catch {
|
|
1282
|
+
return null
|
|
1283
|
+
}
|
|
1284
|
+
if (encodeURIComponent(grantId) !== match[1]) return null
|
|
1285
|
+
return {
|
|
1286
|
+
basename: "/shared/" + match[1],
|
|
1287
|
+
grantId,
|
|
1288
|
+
requestedPath: match[2] ?? "/",
|
|
1289
|
+
secret,
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
async function bootstrapSharedApp() {
|
|
1294
|
+
const bootstrap = consumeBootstrap()
|
|
1295
|
+
if (!bootstrap) {
|
|
1296
|
+
renderUnavailable()
|
|
1297
|
+
return
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
try {
|
|
1301
|
+
const { startSharedApp } = await import("./shared-main.tsx")
|
|
1302
|
+
startSharedApp(bootstrap)
|
|
1303
|
+
} catch {
|
|
1304
|
+
console.error("[SixbSharedApp] Shared application bootstrap failed.")
|
|
1305
|
+
renderUnavailable()
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
void bootstrapSharedApp()
|
|
1310
|
+
</script>
|
|
1311
|
+
</body>
|
|
1312
|
+
</html>
|
|
1313
|
+
`
|
|
1314
|
+
const sharedHtmlPath = join(generatedDir, "shared-index.html")
|
|
1315
|
+
await writeFileIfChanged(sharedHtmlPath, sharedHtmlContent)
|
|
1316
|
+
|
|
1317
|
+
return {
|
|
1318
|
+
htmlPath,
|
|
1319
|
+
mainPath,
|
|
1320
|
+
manifestPath,
|
|
1321
|
+
runtimePath,
|
|
1322
|
+
sharedHtmlPath,
|
|
1323
|
+
sharedMainPath,
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
function renderSharedMetadataHead(
|
|
1328
|
+
metadata: Awaited<ReturnType<typeof resolveAppMetadata>>
|
|
1329
|
+
): string {
|
|
1330
|
+
return [
|
|
1331
|
+
` <title>${escapeHtmlText(metadata.title)}</title>`,
|
|
1332
|
+
...(metadata.description
|
|
1333
|
+
? [` <meta name="description" content="${escapeHtmlAttribute(metadata.description)}" />`]
|
|
1334
|
+
: []),
|
|
1335
|
+
` <meta name="theme-color" content="${escapeHtmlAttribute(metadata.themeColor)}" />`,
|
|
1336
|
+
].join("\n")
|
|
870
1337
|
}
|
|
871
1338
|
|
|
872
1339
|
function renderMetadataHead(metadata: Awaited<ReturnType<typeof resolveAppMetadata>>): string {
|