@origen-ui/admin-cli 1.0.3 → 2.0.1
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 +11 -7
- package/package.json +19 -3
- package/templates/current/clean/CHANGELOG.md +137 -0
- package/templates/current/clean/component-catalog.json +58 -17
- package/templates/current/clean/e2e/admin-shell.spec.ts +6 -0
- package/templates/current/clean/e2e/demo-shell.spec.ts +90 -46
- package/templates/current/clean/package.json +55 -41
- package/templates/current/clean/playwright.config.ts +1 -0
- package/templates/current/clean/playwright.demo.config.ts +1 -1
- package/templates/current/clean/scaffold.lock.json +2 -2
- package/templates/current/clean/scripts/clean-overlay/src/features/errors/pages/Forbidden.vue +7 -2
- package/templates/current/clean/scripts/clean-overlay/src/features/errors/pages/NotFound.vue +7 -2
- package/templates/current/clean/scripts/clean-overlay/src/features/errors/pages/ServerError.vue +7 -2
- package/templates/current/clean/scripts/clean-overlay/src/features/errors/pages/error-pages.spec.ts +11 -2
- package/templates/current/clean/scripts/clean-overlay/src/main.ts +23 -14
- package/templates/current/clean/scripts/clean-overlay/src/router/componentCache.ts +12 -0
- package/templates/current/clean/scripts/clean-overlay/src/router/componentRegistry.ts +2 -13
- package/templates/current/clean/scripts/clean-overlay/src/router/dynamicRoutes.spec.ts +34 -4
- package/templates/current/clean/scripts/clean-overlay/src/router/paths.ts +21 -0
- package/templates/current/clean/scripts/clean-overlay/src/router/staticRoutes.ts +6 -6
- package/templates/current/clean/scripts/demo-api.ts +84 -8
- package/templates/current/clean/scripts/generate-api.ts +87 -3
- package/templates/current/clean/scripts/vite-build-policy.spec.ts +72 -0
- package/templates/current/clean/src/App.vue +10 -3
- package/templates/current/clean/src/api/client.ts +90 -10
- package/templates/current/clean/src/api/generated/data-contracts.ts +0 -3
- package/templates/current/clean/src/api/generated/http-client.ts +28 -26
- package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/345/206/205/345/256/271/346/226/207/347/253/240.ts +5 -8
- package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/345/256/241/350/256/241.ts +1 -4
- package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/346/226/207/344/273/266/344/270/212/344/274/240.ts +2 -5
- package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/347/224/250/346/210/267.ts +6 -9
- package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/217/234/345/215/225.ts +7 -10
- package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/247/222/350/211/262.ts +6 -9
- package/templates/current/clean/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/256/242/345/215/225/350/215/211/347/250/277.ts +6 -9
- package/templates/current/clean/src/api/generated//350/256/244/350/257/201.ts +3 -6
- package/templates/current/clean/src/app/adminLocale.ts +90 -0
- package/templates/current/clean/src/app/createAdminUi.spec.ts +65 -9
- package/templates/current/clean/src/app/createAdminUi.ts +14 -29
- package/templates/current/clean/src/app/embedded-token.spec.ts +40 -0
- package/templates/current/clean/src/app/embedded.spec.ts +135 -0
- package/templates/current/clean/src/app/embedded.ts +255 -0
- package/templates/current/clean/src/app/errorMessage.spec.ts +76 -0
- package/templates/current/clean/src/app/errorMessage.ts +53 -0
- package/templates/current/clean/src/app/localeFormat.ts +52 -0
- package/templates/current/clean/src/app/menu-host.spec.ts +77 -0
- package/templates/current/clean/src/app/menu.spec.ts +65 -0
- package/templates/current/clean/src/app/menu.ts +52 -12
- package/templates/current/clean/src/app/menuTypes.ts +1 -0
- package/templates/current/clean/src/app/origenUiLocale.spec.ts +6 -5
- package/templates/current/clean/src/app/origenUiLocale.ts +3 -58
- package/templates/current/clean/src/app/permission.spec.ts +26 -0
- package/templates/current/clean/src/app/runtimeConfig.ts +9 -8
- package/templates/current/clean/src/demo-api.spec.ts +311 -0
- package/templates/current/clean/src/env.d.ts +18 -0
- package/templates/current/clean/src/features/appearance/components/ThemePanel.vue +21 -17
- package/templates/current/clean/src/features/appearance/store/theme.ts +2 -1
- package/templates/current/clean/src/features/auth/pages/Login.vue +22 -12
- package/templates/current/clean/src/features/auth/store/auth.ts +6 -9
- package/templates/current/clean/src/features/errors/pages/Forbidden.vue +7 -2
- package/templates/current/clean/src/features/errors/pages/NotFound.vue +7 -2
- package/templates/current/clean/src/features/errors/pages/ServerError.vue +7 -2
- package/templates/current/clean/src/features/errors/pages/error-pages.spec.ts +11 -2
- package/templates/current/clean/src/features/security/components/DevtoolsProtectionOverlay.vue +5 -2
- package/templates/current/clean/src/features/security/devtoolsProtection.spec.ts +7 -10
- package/templates/current/clean/src/features/security/devtoolsProtection.ts +2 -1
- package/templates/current/clean/src/features/system/access/api/admin-repository.spec.ts +14 -0
- package/templates/current/clean/src/features/system/access/api/admin-repository.ts +34 -15
- package/templates/current/clean/src/features/system/access/api/rbac.ts +52 -32
- package/templates/current/clean/src/features/system/access/composables/useAuthorizationPolling.spec.ts +134 -0
- package/templates/current/clean/src/features/system/access/composables/useAuthorizationPolling.ts +96 -0
- package/templates/current/clean/src/features/workspace/pages/Empty.vue +4 -1
- package/templates/current/clean/src/features/workspace/pages/Profile.vue +14 -12
- package/templates/current/clean/src/layouts/AdminLayout.vue +6 -1
- package/templates/current/clean/src/layouts/TopBar.vue +60 -11
- package/templates/current/clean/src/layouts/topBarMenuSearch.spec.ts +24 -1
- package/templates/current/clean/src/layouts/topBarMenuSearch.ts +10 -0
- package/templates/current/clean/src/locales/en-US.json +517 -0
- package/templates/current/clean/src/locales/en-US.runtime.ts +22 -0
- package/templates/current/clean/src/locales/index.ts +144 -47
- package/templates/current/clean/src/locales/messages-types.ts +62 -0
- package/templates/current/clean/src/locales/messages.spec.ts +113 -6
- package/templates/current/clean/src/locales/vue-i18n.d.ts +5 -0
- package/templates/current/clean/src/locales/zh-CN.json +517 -0
- package/templates/current/clean/src/locales/zh-CN.runtime.ts +22 -0
- package/templates/current/clean/src/locales/zh-TW.json +517 -0
- package/templates/current/clean/src/locales/zh-TW.runtime.ts +22 -0
- package/templates/current/clean/src/main.ts +23 -14
- package/templates/current/clean/src/router/bootstrap.ts +20 -11
- package/templates/current/clean/src/router/componentCache.ts +12 -0
- package/templates/current/clean/src/router/componentRegistry.ts +2 -13
- package/templates/current/clean/src/router/dynamicMenuRefresh.spec.ts +101 -0
- package/templates/current/clean/src/router/dynamicMenuRefresh.ts +24 -0
- package/templates/current/clean/src/router/dynamicRoutes.spec.ts +34 -4
- package/templates/current/clean/src/router/dynamicRoutes.ts +19 -4
- package/templates/current/clean/src/router/paths.spec.ts +50 -0
- package/templates/current/clean/src/router/paths.ts +21 -0
- package/templates/current/clean/src/router/queryEncryption.spec.ts +30 -4
- package/templates/current/clean/src/router/queryEncryption.ts +48 -44
- package/templates/current/clean/src/router/staticRoutes.ts +6 -6
- package/templates/current/clean/src/router/useRouteQueryDefaults.spec.ts +2 -1
- package/templates/current/clean/src/router/useRouteQueryDefaults.types.ts +2 -1
- package/templates/current/clean/src/templates/og-table.ts +79 -37
- package/templates/current/clean/src/templates/templates.spec.ts +19 -0
- package/templates/current/clean/tsconfig.json +0 -1
- package/templates/current/clean/vite.build-policy.ts +161 -289
- package/templates/current/clean/vite.config.ts +34 -5
- package/templates/current/examples/CHANGELOG.md +137 -0
- package/templates/current/examples/component-catalog.json +58 -17
- package/templates/current/examples/e2e/admin-shell.spec.ts +6 -0
- package/templates/current/examples/e2e/demo-shell.spec.ts +90 -46
- package/templates/current/examples/package.json +55 -41
- package/templates/current/examples/playwright.config.ts +1 -0
- package/templates/current/examples/playwright.demo.config.ts +1 -1
- package/templates/current/examples/scaffold.lock.json +2 -2
- package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/Forbidden.vue +7 -2
- package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/NotFound.vue +7 -2
- package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/ServerError.vue +7 -2
- package/templates/current/examples/scripts/clean-overlay/src/features/errors/pages/error-pages.spec.ts +11 -2
- package/templates/current/examples/scripts/clean-overlay/src/main.ts +23 -14
- package/templates/current/examples/scripts/clean-overlay/src/router/componentCache.ts +12 -0
- package/templates/current/examples/scripts/clean-overlay/src/router/componentRegistry.ts +2 -13
- package/templates/current/examples/scripts/clean-overlay/src/router/dynamicRoutes.spec.ts +34 -4
- package/templates/current/examples/scripts/clean-overlay/src/router/paths.ts +21 -0
- package/templates/current/examples/scripts/clean-overlay/src/router/staticRoutes.ts +6 -6
- package/templates/current/examples/scripts/demo-api.ts +84 -8
- package/templates/current/examples/scripts/generate-api.ts +87 -3
- package/templates/current/examples/scripts/vite-build-policy.spec.ts +72 -0
- package/templates/current/examples/src/App.vue +10 -3
- package/templates/current/examples/src/api/client.ts +90 -10
- package/templates/current/examples/src/api/generated/data-contracts.ts +0 -3
- package/templates/current/examples/src/api/generated/http-client.ts +28 -26
- package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/345/206/205/345/256/271/346/226/207/347/253/240.ts +5 -8
- package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/345/256/241/350/256/241.ts +1 -4
- package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/346/226/207/344/273/266/344/270/212/344/274/240.ts +2 -5
- package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/347/224/250/346/210/267.ts +6 -9
- package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/217/234/345/215/225.ts +7 -10
- package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/247/222/350/211/262.ts +6 -9
- package/templates/current/examples/src/api/generated//347/263/273/347/273/237/347/256/241/347/220/206/350/256/242/345/215/225/350/215/211/347/250/277.ts +6 -9
- package/templates/current/examples/src/api/generated//350/256/244/350/257/201.ts +3 -6
- package/templates/current/examples/src/app/adminLocale.ts +90 -0
- package/templates/current/examples/src/app/createAdminUi.spec.ts +65 -9
- package/templates/current/examples/src/app/createAdminUi.ts +14 -29
- package/templates/current/examples/src/app/embedded-token.spec.ts +40 -0
- package/templates/current/examples/src/app/embedded.spec.ts +135 -0
- package/templates/current/examples/src/app/embedded.ts +255 -0
- package/templates/current/examples/src/app/errorMessage.spec.ts +76 -0
- package/templates/current/examples/src/app/errorMessage.ts +53 -0
- package/templates/current/examples/src/app/localeFormat.ts +52 -0
- package/templates/current/examples/src/app/menu-host.spec.ts +77 -0
- package/templates/current/examples/src/app/menu.spec.ts +65 -0
- package/templates/current/examples/src/app/menu.ts +52 -12
- package/templates/current/examples/src/app/menuTypes.ts +1 -0
- package/templates/current/examples/src/app/origenUiLocale.spec.ts +6 -5
- package/templates/current/examples/src/app/origenUiLocale.ts +3 -58
- package/templates/current/examples/src/app/permission.spec.ts +26 -0
- package/templates/current/examples/src/app/runtimeConfig.ts +9 -8
- package/templates/current/examples/src/demo-api.spec.ts +311 -0
- package/templates/current/examples/src/env.d.ts +18 -0
- package/templates/current/examples/src/features/appearance/components/ThemePanel.vue +21 -17
- package/templates/current/examples/src/features/appearance/store/theme.ts +2 -1
- package/templates/current/examples/src/features/auth/pages/Login.vue +22 -12
- package/templates/current/examples/src/features/auth/store/auth.ts +6 -9
- package/templates/current/examples/src/features/errors/pages/Forbidden.vue +7 -2
- package/templates/current/examples/src/features/errors/pages/NotFound.vue +7 -2
- package/templates/current/examples/src/features/errors/pages/ServerError.vue +7 -2
- package/templates/current/examples/src/features/errors/pages/error-pages.spec.ts +9 -1
- package/templates/current/examples/src/features/feature-composables.spec.ts +47 -10
- package/templates/current/examples/src/features/security/components/DevtoolsProtectionOverlay.vue +5 -2
- package/templates/current/examples/src/features/security/devtoolsProtection.spec.ts +7 -10
- package/templates/current/examples/src/features/security/devtoolsProtection.ts +2 -1
- package/templates/current/examples/src/features/system/access/api/admin-repository.spec.ts +14 -0
- package/templates/current/examples/src/features/system/access/api/admin-repository.ts +34 -15
- package/templates/current/examples/src/features/system/access/api/rbac.ts +52 -32
- package/templates/current/examples/src/features/system/access/composables/useAuthorizationPolling.spec.ts +134 -0
- package/templates/current/examples/src/features/system/access/composables/useAuthorizationPolling.ts +96 -0
- package/templates/current/examples/src/features/system/audit/composables/useAuditManagement.ts +43 -38
- package/templates/current/examples/src/features/system/menus/components/MenuEditorForm.vue +137 -47
- package/templates/current/examples/src/features/system/menus/composables/useMenuManagement.ts +215 -107
- package/templates/current/examples/src/features/system/menus/menuDraft.spec.ts +204 -0
- package/templates/current/examples/src/features/system/menus/menuDraft.ts +116 -0
- package/templates/current/examples/src/features/system/menus/menuTreePrecheck.spec.ts +268 -0
- package/templates/current/examples/src/features/system/menus/menuTreePrecheck.ts +317 -0
- package/templates/current/examples/src/features/system/menus/pages/MenuList.vue +3 -1
- package/templates/current/examples/src/features/system/roles/components/RoleEditorForm.vue +19 -15
- package/templates/current/examples/src/features/system/roles/composables/useRoleManagement.ts +49 -53
- package/templates/current/examples/src/features/system/roles/pages/RoleList.vue +3 -1
- package/templates/current/examples/src/features/system/users/components/UserEditorForm.vue +22 -19
- package/templates/current/examples/src/features/system/users/composables/useUserManagement.ts +59 -64
- package/templates/current/examples/src/features/system/users/pages/UserList.vue +3 -1
- package/templates/current/examples/src/features/workspace/pages/Dashboard.spec.ts +9 -0
- package/templates/current/examples/src/features/workspace/pages/Dashboard.vue +54 -26
- package/templates/current/examples/src/features/workspace/pages/Empty.vue +4 -1
- package/templates/current/examples/src/features/workspace/pages/Profile.vue +14 -12
- package/templates/current/examples/src/layouts/AdminLayout.vue +6 -1
- package/templates/current/examples/src/layouts/TopBar.vue +60 -11
- package/templates/current/examples/src/layouts/topBarMenuSearch.spec.ts +24 -1
- package/templates/current/examples/src/layouts/topBarMenuSearch.ts +10 -0
- package/templates/current/examples/src/locales/en-US.json +517 -0
- package/templates/current/examples/src/locales/en-US.runtime.ts +22 -0
- package/templates/current/examples/src/locales/index.ts +144 -47
- package/templates/current/examples/src/locales/messages-types.ts +62 -0
- package/templates/current/examples/src/locales/messages.spec.ts +113 -6
- package/templates/current/examples/src/locales/vue-i18n.d.ts +5 -0
- package/templates/current/examples/src/locales/zh-CN.json +517 -0
- package/templates/current/examples/src/locales/zh-CN.runtime.ts +22 -0
- package/templates/current/examples/src/locales/zh-TW.json +517 -0
- package/templates/current/examples/src/locales/zh-TW.runtime.ts +22 -0
- package/templates/current/examples/src/main.ts +43 -15
- package/templates/current/examples/src/router/bootstrap.ts +20 -11
- package/templates/current/examples/src/router/componentCache.ts +18 -0
- package/templates/current/examples/src/router/componentRegistry.ts +1 -18
- package/templates/current/examples/src/router/dynamicMenuRefresh.spec.ts +101 -0
- package/templates/current/examples/src/router/dynamicMenuRefresh.ts +24 -0
- package/templates/current/examples/src/router/dynamicRoutes.spec.ts +37 -2
- package/templates/current/examples/src/router/dynamicRoutes.ts +19 -4
- package/templates/current/examples/src/router/paths.spec.ts +50 -0
- package/templates/current/examples/src/router/paths.ts +21 -0
- package/templates/current/examples/src/router/queryEncryption.spec.ts +30 -4
- package/templates/current/examples/src/router/queryEncryption.ts +48 -44
- package/templates/current/examples/src/router/staticRoutes.ts +4 -4
- package/templates/current/examples/src/router/useRouteQueryDefaults.spec.ts +2 -1
- package/templates/current/examples/src/router/useRouteQueryDefaults.types.ts +2 -1
- package/templates/current/examples/src/templates/og-table.ts +79 -37
- package/templates/current/examples/src/templates/templates.spec.ts +19 -0
- package/templates/current/examples/tsconfig.json +0 -1
- package/templates/current/examples/vite.build-policy.ts +161 -289
- package/templates/current/examples/vite.config.ts +34 -5
- package/templates/current/clean/scripts/check-admin-bundle.mjs +0 -214
- package/templates/current/clean/scripts/performance/baselines/bundle-gzip-v2-pre-entry-unification.json +0 -38
- package/templates/current/clean/src/locales/en-US.ts +0 -55
- package/templates/current/clean/src/locales/zh-CN.ts +0 -55
- package/templates/current/clean/src/locales/zh-TW.ts +0 -44
- package/templates/current/examples/scripts/check-admin-bundle.mjs +0 -214
- package/templates/current/examples/scripts/performance/baselines/bundle-gzip-v2-pre-entry-unification.json +0 -38
- package/templates/current/examples/src/locales/en-US.ts +0 -55
- package/templates/current/examples/src/locales/zh-CN.ts +0 -55
- package/templates/current/examples/src/locales/zh-TW.ts +0 -44
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import type { RouterAdapter } from '@origen-ui/router'
|
|
2
2
|
import type { BackendMenuNode } from '../app/menuTypes'
|
|
3
|
-
import {
|
|
4
|
-
createAccessPolicy,
|
|
5
|
-
createRouterAdapter,
|
|
6
|
-
createWebStorageAdapter,
|
|
7
|
-
} from '@origen-ui/router'
|
|
3
|
+
import { createAccessPolicy, createRouterAdapter, createWebStorageAdapter } from '@origen-ui/router'
|
|
8
4
|
import { createWebHistory } from 'vue-router'
|
|
9
|
-
import {
|
|
5
|
+
import { embeddedMode, reportMenusToHost } from '../app/embedded'
|
|
6
|
+
import { loadMenusFromHost } from '../app/menu'
|
|
10
7
|
import { appRuntimeConfig } from '../app/runtimeConfig'
|
|
11
8
|
import { useAuthStore } from '../features/auth/store/auth'
|
|
12
9
|
import { pinia } from '../store'
|
|
13
10
|
import { hasPermission } from '../utils/permissions'
|
|
14
|
-
import { componentRegistry
|
|
11
|
+
import { componentRegistry } from './componentRegistry'
|
|
12
|
+
import { routeTransformer } from './dynamicRoutes'
|
|
13
|
+
import { NOT_FOUND_ROUTE_NAME } from './paths'
|
|
15
14
|
import { staticRoutes } from './staticRoutes'
|
|
16
15
|
|
|
17
16
|
type QueryCodecs = Pick<
|
|
@@ -40,23 +39,33 @@ export function initializeRouting(
|
|
|
40
39
|
if (routing) return routing
|
|
41
40
|
const workspace = appRuntimeConfig.router.workspace
|
|
42
41
|
routing = createRouterAdapter<BackendMenuNode>({
|
|
43
|
-
history: createWebHistory(),
|
|
42
|
+
history: createWebHistory(import.meta.env.BASE_URL),
|
|
44
43
|
staticRoutes,
|
|
45
44
|
...queryCodecs,
|
|
46
45
|
...titleOptions,
|
|
46
|
+
// 嵌入模式:document.title 归主壳所有,子应用路由不再写入
|
|
47
|
+
syncDocumentTitle: !embeddedMode,
|
|
47
48
|
access: createAccessPolicy({
|
|
48
49
|
getSession: () => useAuthStore(pinia).session,
|
|
49
|
-
canAccess: (
|
|
50
|
+
canAccess: (_to, session, meta) => hasPermission(session, meta.permissions ?? []),
|
|
50
51
|
loginRoute: { name: 'login' },
|
|
51
52
|
forbiddenRoute: { name: '403' },
|
|
52
53
|
}),
|
|
53
54
|
dynamic: {
|
|
54
|
-
|
|
55
|
+
// 嵌入模式:菜单装载完成后向主壳上报(sub-app-menu);独立模式不触发。
|
|
56
|
+
// 上报在 embedded.ts 内 try/catch 兜底,装载主流程不受影响。
|
|
57
|
+
load: async () => {
|
|
58
|
+
// 嵌入模式:菜单装载完成后向主壳上报(sub-app-menu);独立模式不触发。
|
|
59
|
+
// loadMenusFromHost 内部处理主壳未下发时的 fetchMenus 回落。
|
|
60
|
+
const menus = await loadMenusFromHost()
|
|
61
|
+
if (embeddedMode) reportMenusToHost(menus)
|
|
62
|
+
return menus
|
|
63
|
+
},
|
|
55
64
|
transform: routeTransformer,
|
|
56
65
|
resolveComponent: (key) => componentRegistry.get(key),
|
|
57
66
|
},
|
|
58
67
|
fallbacks: {
|
|
59
|
-
notFound: { name:
|
|
68
|
+
notFound: { name: NOT_FOUND_ROUTE_NAME },
|
|
60
69
|
error: { name: '500' },
|
|
61
70
|
},
|
|
62
71
|
workspace: {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* componentKey → 页面组件 name 映射,用于生成 KeepAlive 缓存名单。
|
|
3
|
+
*
|
|
4
|
+
* KeepAlive 的 `:include` 匹配的是页面组件渲染后的 name(SFC `defineOptions({ name })`),
|
|
5
|
+
* 因此这里的值必须与各页面组件的 defineOptions name 完全一致,否则缓存失效。
|
|
6
|
+
* 修改组件 name 时必须同步更新此表,并保持 dynamicRoutes.spec.ts 断言一致。
|
|
7
|
+
*/
|
|
8
|
+
export const componentCacheNames: Readonly<Record<string, string>> = {
|
|
9
|
+
workspace: 'EmptyPage',
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const fallbackComponentKey = 'workspace'
|
|
@@ -1,20 +1,9 @@
|
|
|
1
1
|
import type { Component } from 'vue'
|
|
2
2
|
import { RouterViewHost } from '@origen-ui/router'
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* componentKey → 页面组件 name 映射,用于生成 KeepAlive 缓存名单。
|
|
8
|
-
*
|
|
9
|
-
* KeepAlive 的 `:include` 匹配的是页面组件渲染后的 name(SFC `defineOptions({ name })`),
|
|
10
|
-
* 因此这里的值必须与各页面组件的 defineOptions name 完全一致,否则缓存失效。
|
|
11
|
-
* 修改组件 name 时必须同步更新此表,并保持 dynamicRoutes.spec.ts 断言一致。
|
|
12
|
-
*/
|
|
13
|
-
export const componentCacheNames: Readonly<Record<string, string>> = {
|
|
14
|
-
workspace: 'EmptyPage',
|
|
15
|
-
}
|
|
4
|
+
export { componentCacheNames, fallbackComponentKey } from './componentCache'
|
|
16
5
|
|
|
17
|
-
|
|
6
|
+
type ComponentLoader = () => Promise<Component>
|
|
18
7
|
|
|
19
8
|
export const componentRegistry: Map<string, ComponentLoader> = new Map([
|
|
20
9
|
[
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { BackendMenuNode } from '../app/menuTypes'
|
|
2
|
+
import { createRouterAdapter } from '@origen-ui/router'
|
|
3
|
+
import { describe, expect, it } from 'vitest'
|
|
4
|
+
import { defineComponent } from 'vue'
|
|
5
|
+
import { createMemoryHistory } from 'vue-router'
|
|
6
|
+
import { replaceDynamicMenusWithAccessibleFallback } from './dynamicMenuRefresh'
|
|
7
|
+
import { routeTransformer } from './dynamicRoutes'
|
|
8
|
+
|
|
9
|
+
const View = defineComponent({ template: '<div />' })
|
|
10
|
+
|
|
11
|
+
const staticRoutes = [
|
|
12
|
+
{ name: 'login', path: '/login', component: View, meta: { adapter: { public: true, menu: { hidden: true } } } },
|
|
13
|
+
{ name: '404', path: '/404', component: View, meta: { adapter: { public: true, menu: { hidden: true } } } },
|
|
14
|
+
{ name: '500', path: '/500', component: View, meta: { adapter: { public: true, menu: { hidden: true } } } },
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
function createAdaptersWithMenus(menus: readonly BackendMenuNode[]) {
|
|
18
|
+
let current = menus
|
|
19
|
+
const adapter = createRouterAdapter<BackendMenuNode>({
|
|
20
|
+
history: createMemoryHistory(),
|
|
21
|
+
staticRoutes,
|
|
22
|
+
dynamic: {
|
|
23
|
+
load: async () => current,
|
|
24
|
+
transform: routeTransformer,
|
|
25
|
+
resolveComponent: () => () => Promise.resolve(View),
|
|
26
|
+
},
|
|
27
|
+
fallbacks: { notFound: { name: '404' }, error: { name: '500' } },
|
|
28
|
+
})
|
|
29
|
+
return {
|
|
30
|
+
adapter,
|
|
31
|
+
setMenus(next: readonly BackendMenuNode[]) {
|
|
32
|
+
current = [...next]
|
|
33
|
+
},
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
describe('replaceDynamicMenusWithAccessibleFallback', () => {
|
|
38
|
+
it('falls back to the first reachable menu entry when home is revoked', async () => {
|
|
39
|
+
const { adapter, setMenus } = createAdaptersWithMenus([
|
|
40
|
+
{ code: 'dashboard', path: '/dashboard', title: 'Dashboard' },
|
|
41
|
+
{ code: 'reports', path: '/reports', title: 'Reports' },
|
|
42
|
+
])
|
|
43
|
+
await adapter.router.push('/reports')
|
|
44
|
+
await adapter.router.isReady()
|
|
45
|
+
expect(adapter.router.currentRoute.value.path).toBe('/reports')
|
|
46
|
+
|
|
47
|
+
// 主页与当前页同时被裁撤:回落到刷新后菜单里首个真实可达入口。
|
|
48
|
+
setMenus([{ code: 'audit', path: '/audit', title: 'Audit' }])
|
|
49
|
+
await replaceDynamicMenusWithAccessibleFallback(adapter)
|
|
50
|
+
|
|
51
|
+
expect(adapter.router.currentRoute.value.path).toBe('/audit')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('lands on home when the current route disappears but home remains', async () => {
|
|
55
|
+
const { adapter, setMenus } = createAdaptersWithMenus([
|
|
56
|
+
{ code: 'dashboard', path: '/dashboard', title: 'Dashboard' },
|
|
57
|
+
{ code: 'reports', path: '/reports', title: 'Reports' },
|
|
58
|
+
])
|
|
59
|
+
await adapter.router.push('/reports')
|
|
60
|
+
await adapter.router.isReady()
|
|
61
|
+
|
|
62
|
+
setMenus([{ code: 'dashboard', path: '/dashboard', title: 'Dashboard' }])
|
|
63
|
+
await replaceDynamicMenusWithAccessibleFallback(adapter)
|
|
64
|
+
|
|
65
|
+
expect(adapter.router.currentRoute.value.path).toBe('/dashboard')
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('skips a directory whose redirect target was revoked and lands on a real entry', async () => {
|
|
69
|
+
const { adapter, setMenus } = createAdaptersWithMenus([
|
|
70
|
+
{ code: 'system', path: '/system', title: 'System', redirect: '/dashboard' },
|
|
71
|
+
{ code: 'reports', path: '/reports', title: 'Reports' },
|
|
72
|
+
])
|
|
73
|
+
await adapter.router.push('/reports')
|
|
74
|
+
await adapter.router.isReady()
|
|
75
|
+
|
|
76
|
+
// 主页被裁撤后,首个菜单项(目录)的 redirect 仍指向 /dashboard——
|
|
77
|
+
// 可达性校验必须跳过它,否则会再次落回 404。
|
|
78
|
+
setMenus([
|
|
79
|
+
{ code: 'system', path: '/system', title: 'System', redirect: '/dashboard' },
|
|
80
|
+
{ code: 'audit', path: '/audit', title: 'Audit' },
|
|
81
|
+
])
|
|
82
|
+
await replaceDynamicMenusWithAccessibleFallback(adapter)
|
|
83
|
+
|
|
84
|
+
expect(adapter.router.currentRoute.value.path).toBe('/audit')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('does not redirect when the session is already on the not-found page', async () => {
|
|
88
|
+
const { adapter, setMenus } = createAdaptersWithMenus([
|
|
89
|
+
{ code: 'dashboard', path: '/dashboard', title: 'Dashboard' },
|
|
90
|
+
{ code: 'reports', path: '/reports', title: 'Reports' },
|
|
91
|
+
])
|
|
92
|
+
await adapter.router.push('/reports')
|
|
93
|
+
await adapter.router.isReady()
|
|
94
|
+
await adapter.router.push('/404')
|
|
95
|
+
|
|
96
|
+
setMenus([{ code: 'audit', path: '/audit', title: 'Audit' }])
|
|
97
|
+
await replaceDynamicMenusWithAccessibleFallback(adapter)
|
|
98
|
+
|
|
99
|
+
expect(adapter.router.currentRoute.value.name).toBe('404')
|
|
100
|
+
})
|
|
101
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { RouterAdapter } from '@origen-ui/router'
|
|
2
|
+
import type { RouteLocationRaw } from 'vue-router'
|
|
3
|
+
import type { BackendMenuNode } from '../app/menuTypes'
|
|
4
|
+
import { isUnmatchedRoute } from '@origen-ui/router'
|
|
5
|
+
import { firstMenuTarget, HOME_ROUTE, NOT_FOUND_ROUTE_NAME } from './paths'
|
|
6
|
+
|
|
7
|
+
function isReachableTarget(routing: RouterAdapter<BackendMenuNode>, to: RouteLocationRaw): boolean {
|
|
8
|
+
const resolved = routing.router.resolve(to)
|
|
9
|
+
return resolved.matched.length > 0 && !isUnmatchedRoute(resolved)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 替换动态菜单并在当前路由失效时回落:优先主页;主页已被裁撤时取
|
|
14
|
+
* 刷新后菜单树的首个真实可达入口,而不是把会话滞留在 404 页。
|
|
15
|
+
*/
|
|
16
|
+
export async function replaceDynamicMenusWithAccessibleFallback(
|
|
17
|
+
routing: RouterAdapter<BackendMenuNode>,
|
|
18
|
+
): Promise<void> {
|
|
19
|
+
const wasOnNotFound = routing.router.currentRoute.value.name === NOT_FOUND_ROUTE_NAME
|
|
20
|
+
await routing.replaceDynamicRoutes(undefined, { fallback: HOME_ROUTE })
|
|
21
|
+
if (wasOnNotFound || routing.router.currentRoute.value.name !== NOT_FOUND_ROUTE_NAME) return
|
|
22
|
+
const fallback = firstMenuTarget(routing.menu.value, (to) => isReachableTarget(routing, to))
|
|
23
|
+
if (fallback) await routing.router.replace(fallback)
|
|
24
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import { componentCacheNames } from './
|
|
3
|
-
import { componentRegistry
|
|
2
|
+
import { componentCacheNames } from './componentCache'
|
|
3
|
+
import { componentRegistry } from './componentRegistry'
|
|
4
|
+
import { routeTransformer } from './dynamicRoutes'
|
|
4
5
|
import { staticRoutes } from './staticRoutes'
|
|
5
6
|
|
|
6
7
|
describe('clean dynamic component registry', () => {
|
|
@@ -16,6 +17,23 @@ describe('clean dynamic component registry', () => {
|
|
|
16
17
|
expect(route?.componentKey).toBe('workspace')
|
|
17
18
|
expect(componentRegistry.has('workspace')).toBe(true)
|
|
18
19
|
})
|
|
20
|
+
|
|
21
|
+
it('rejects flat parentPath entries whose parent route does not exist', () => {
|
|
22
|
+
expect(() =>
|
|
23
|
+
routeTransformer([
|
|
24
|
+
{ code: 'orphan', path: 'orphan', parentPath: '/missing', title: 'Orphan' },
|
|
25
|
+
]),
|
|
26
|
+
).toThrow('Unresolved dynamic route parent path: /missing')
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('rejects duplicate route codes with an explicit error', () => {
|
|
30
|
+
expect(() =>
|
|
31
|
+
routeTransformer([
|
|
32
|
+
{ code: 'dup', path: '/first', title: 'First' },
|
|
33
|
+
{ code: 'dup', path: '/second', title: 'Second' },
|
|
34
|
+
]),
|
|
35
|
+
).toThrow('Duplicate dynamic route code: dup')
|
|
36
|
+
})
|
|
19
37
|
})
|
|
20
38
|
|
|
21
39
|
describe('clean keep-alive cache names', () => {
|
|
@@ -25,7 +43,13 @@ describe('clean keep-alive cache names', () => {
|
|
|
25
43
|
|
|
26
44
|
it('generates cache.name from the component name mapping for cached dynamic routes', () => {
|
|
27
45
|
const [workspace] = routeTransformer([
|
|
28
|
-
{
|
|
46
|
+
{
|
|
47
|
+
code: 'workspace',
|
|
48
|
+
path: '/workspace',
|
|
49
|
+
title: 'Workspace',
|
|
50
|
+
componentKey: 'workspace',
|
|
51
|
+
cache: true,
|
|
52
|
+
},
|
|
29
53
|
])
|
|
30
54
|
|
|
31
55
|
expect(workspace?.meta?.cache).toEqual({ enabled: true, name: 'EmptyPage' })
|
|
@@ -33,7 +57,13 @@ describe('clean keep-alive cache names', () => {
|
|
|
33
57
|
|
|
34
58
|
it('omits cache meta when the source disables caching', () => {
|
|
35
59
|
const [workspace] = routeTransformer([
|
|
36
|
-
{
|
|
60
|
+
{
|
|
61
|
+
code: 'workspace',
|
|
62
|
+
path: '/workspace',
|
|
63
|
+
title: 'Workspace',
|
|
64
|
+
componentKey: 'workspace',
|
|
65
|
+
cache: false,
|
|
66
|
+
},
|
|
37
67
|
])
|
|
38
68
|
|
|
39
69
|
expect(workspace?.meta?.cache).toBeUndefined()
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import type { RouteSourceNode } from '@origen-ui/router'
|
|
2
2
|
import type { BackendMenuNode } from '../app/menuTypes'
|
|
3
|
-
import { componentCacheNames, fallbackComponentKey } from './
|
|
4
|
-
|
|
5
|
-
export { componentRegistry } from './componentRegistry'
|
|
3
|
+
import { componentCacheNames, fallbackComponentKey } from './componentCache'
|
|
6
4
|
|
|
7
5
|
function routeName(source: BackendMenuNode, used: Set<string>): string {
|
|
8
6
|
const base = `dyn-${source.code.replace(/[^\w-]+/g, '-').replace(/^-|-$/g, '') || 'route'}`
|
|
@@ -29,10 +27,14 @@ function routeMeta(source: BackendMenuNode, hasChildren: boolean): RouteSourceNo
|
|
|
29
27
|
: undefined
|
|
30
28
|
return {
|
|
31
29
|
title: source.title,
|
|
30
|
+
...(source.titleKey ? { titleKey: source.titleKey } : {}),
|
|
32
31
|
icon: source.icon,
|
|
33
32
|
permissions: source.permissions,
|
|
34
33
|
menu: { hidden: source.hidden === true || source.menu === false, order: source.order },
|
|
35
|
-
breadcrumb: {
|
|
34
|
+
breadcrumb: {
|
|
35
|
+
label: source.title,
|
|
36
|
+
...(source.titleKey ? { labelKey: source.titleKey } : {}),
|
|
37
|
+
},
|
|
36
38
|
tab: { hidden: source.tabHidden === true || hasChildren },
|
|
37
39
|
cache,
|
|
38
40
|
}
|
|
@@ -124,6 +126,8 @@ function buildTree(nodes: readonly BackendMenuNode[]): readonly BackendMenuNode[
|
|
|
124
126
|
])
|
|
125
127
|
candidates.delete(item)
|
|
126
128
|
if (candidates.size > 1) throw new Error(`Ambiguous dynamic route parent path: ${parentPath}`)
|
|
129
|
+
if (candidates.size === 0)
|
|
130
|
+
throw new Error(`Unresolved dynamic route parent path: ${parentPath}`)
|
|
127
131
|
}
|
|
128
132
|
|
|
129
133
|
for (const item of entries) {
|
|
@@ -140,7 +144,18 @@ function buildTree(nodes: readonly BackendMenuNode[]): readonly BackendMenuNode[
|
|
|
140
144
|
return roots.map(materialize)
|
|
141
145
|
}
|
|
142
146
|
|
|
147
|
+
function assertUniqueCodes(nodes: readonly BackendMenuNode[], seen = new Set<string>()): void {
|
|
148
|
+
for (const node of nodes) {
|
|
149
|
+
if (seen.has(node.code)) throw new Error(`Duplicate dynamic route code: ${node.code}`)
|
|
150
|
+
seen.add(node.code)
|
|
151
|
+
assertUniqueCodes(node.children ?? [], seen)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
143
155
|
export function routeTransformer(sources: readonly BackendMenuNode[]): readonly RouteSourceNode[] {
|
|
156
|
+
// buildTree 按 code 去重会静默丢弃后到的重复节点,连同其子树一起消失;
|
|
157
|
+
// 在此显式失败,给出可定位的数据错误。
|
|
158
|
+
assertUniqueCodes(sources)
|
|
144
159
|
const usedNames = new Set<string>()
|
|
145
160
|
const tree = buildTree(sources)
|
|
146
161
|
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { RouteMenuItem } from '@origen-ui/router'
|
|
2
|
+
import { describe, expect, it } from 'vitest'
|
|
3
|
+
import { firstMenuTarget, HOME_ROUTE, NOT_FOUND_ROUTE_NAME } from './paths'
|
|
4
|
+
import { staticRoutes } from './staticRoutes'
|
|
5
|
+
|
|
6
|
+
describe('router paths', () => {
|
|
7
|
+
it('keeps the root static redirect aligned with HOME_ROUTE', () => {
|
|
8
|
+
const root = staticRoutes.find((route) => route.name === 'root')
|
|
9
|
+
expect(root?.redirect).toBe(HOME_ROUTE)
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('pins the not-found fallback to the static 404 route', () => {
|
|
13
|
+
expect(staticRoutes.some((route) => route.name === NOT_FOUND_ROUTE_NAME)).toBe(true)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('resolves the first navigable menu target depth-first', () => {
|
|
17
|
+
const items = [
|
|
18
|
+
{ id: 'locked', label: 'Locked', children: [] },
|
|
19
|
+
{
|
|
20
|
+
id: 'section',
|
|
21
|
+
label: 'Section',
|
|
22
|
+
to: { name: 'section' },
|
|
23
|
+
children: [{ id: 'inner', label: 'Inner', to: { name: 'inner' }, children: [] }],
|
|
24
|
+
},
|
|
25
|
+
] as readonly RouteMenuItem[]
|
|
26
|
+
|
|
27
|
+
expect(firstMenuTarget(items)).toEqual({ name: 'section' })
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('returns undefined when no menu item is navigable', () => {
|
|
31
|
+
const items = [{ id: 'empty', label: 'Empty', children: [] }] as readonly RouteMenuItem[]
|
|
32
|
+
|
|
33
|
+
expect(firstMenuTarget(items)).toBeUndefined()
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('skips entries rejected by the reachability predicate', () => {
|
|
37
|
+
const items = [
|
|
38
|
+
{ id: 'gone', label: 'Gone', to: { path: '/gone' }, children: [] },
|
|
39
|
+
{ id: 'live', label: 'Live', to: { name: 'live' }, children: [] },
|
|
40
|
+
] as readonly RouteMenuItem[]
|
|
41
|
+
|
|
42
|
+
expect(
|
|
43
|
+
firstMenuTarget(items, (to) => {
|
|
44
|
+
const path = typeof to === 'string' ? to : to.path
|
|
45
|
+
return path !== '/gone'
|
|
46
|
+
}),
|
|
47
|
+
).toEqual({ name: 'live' })
|
|
48
|
+
expect(firstMenuTarget(items, () => false)).toBeUndefined()
|
|
49
|
+
})
|
|
50
|
+
})
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { RouteMenuItem } from '@origen-ui/router'
|
|
2
|
+
import type { RouteLocationRaw } from 'vue-router'
|
|
3
|
+
|
|
4
|
+
/** 授权后的默认首页路由;与 staticRoutes 根路由 redirect 由 paths.spec 钉住一致。 */
|
|
5
|
+
export const HOME_ROUTE = '/workspace'
|
|
6
|
+
|
|
7
|
+
/** staticRoutes 中 404 兜底页的路由名。 */
|
|
8
|
+
export const NOT_FOUND_ROUTE_NAME = '404'
|
|
9
|
+
|
|
10
|
+
/** 深度优先取菜单树中首个可导航入口;isReachable 用于剔除重定向到已裁撤路由的目录项。 */
|
|
11
|
+
export function firstMenuTarget(
|
|
12
|
+
items: readonly RouteMenuItem[],
|
|
13
|
+
isReachable: (to: RouteLocationRaw) => boolean = () => true,
|
|
14
|
+
): RouteLocationRaw | undefined {
|
|
15
|
+
for (const item of items) {
|
|
16
|
+
if (item.to && isReachable(item.to)) return item.to
|
|
17
|
+
const child = firstMenuTarget(item.children, isReachable)
|
|
18
|
+
if (child) return child
|
|
19
|
+
}
|
|
20
|
+
return undefined
|
|
21
|
+
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import type { RouteRecordRaw } from 'vue-router'
|
|
2
|
+
import { gcm } from '@noble/ciphers/aes.js'
|
|
1
3
|
import { createRouterAdapter } from '@origen-ui/router'
|
|
2
4
|
import { describe, expect, it } from 'vitest'
|
|
3
5
|
import { defineComponent } from 'vue'
|
|
4
|
-
import { createMemoryHistory
|
|
6
|
+
import { createMemoryHistory } from 'vue-router'
|
|
5
7
|
import { createRouteQueryEncryption } from './queryEncryption'
|
|
6
8
|
|
|
7
9
|
const key = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
|
|
@@ -11,6 +13,19 @@ const staticRoutes: RouteRecordRaw[] = [
|
|
|
11
13
|
{ name: '500', path: '/500', component: View, meta: { adapter: { public: true } } },
|
|
12
14
|
]
|
|
13
15
|
|
|
16
|
+
function toUrlSafeBase64(value: Uint8Array): string {
|
|
17
|
+
let binary = ''
|
|
18
|
+
for (const byte of value) binary += String.fromCharCode(byte)
|
|
19
|
+
return btoa(binary).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/u, '')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function encodeV2(plaintext: string): string {
|
|
23
|
+
const nonce = new Uint8Array(12)
|
|
24
|
+
const encryptionKey = Uint8Array.from(key.match(/../gu)!.map((pair) => Number.parseInt(pair, 16)))
|
|
25
|
+
const ciphertextAndTag = gcm(encryptionKey, nonce).encrypt(new TextEncoder().encode(plaintext))
|
|
26
|
+
return `_q=v2.${toUrlSafeBase64(nonce)}.${toUrlSafeBase64(ciphertextAndTag)}`
|
|
27
|
+
}
|
|
28
|
+
|
|
14
29
|
describe('route query encryption', () => {
|
|
15
30
|
it('is disabled without an explicit enablement flag', () => {
|
|
16
31
|
expect(createRouteQueryEncryption({ key })).toBeUndefined()
|
|
@@ -23,13 +38,13 @@ describe('route query encryption', () => {
|
|
|
23
38
|
)
|
|
24
39
|
})
|
|
25
40
|
|
|
26
|
-
it('
|
|
41
|
+
it('uses a random AES-256-GCM nonce and round-trips Unicode, arrays, and null', () => {
|
|
27
42
|
const codec = createRouteQueryEncryption({ enabled: true, key })!
|
|
28
43
|
const query = { count: 2, filter: 'active', ids: ['a', 'b'], nullable: null, unicode: '绠$悊' }
|
|
29
44
|
const first = codec.stringifyQuery(query)
|
|
30
45
|
const second = codec.stringifyQuery(query)
|
|
31
46
|
|
|
32
|
-
expect(first).toMatch(/^_q=
|
|
47
|
+
expect(first).toMatch(/^_q=v2\.[\w-]{16}\.[\w-]+$/)
|
|
33
48
|
expect(first).not.toContain('active')
|
|
34
49
|
expect(first).not.toBe(second)
|
|
35
50
|
expect(codec.parseQuery(first)).toEqual({
|
|
@@ -41,7 +56,7 @@ describe('route query encryption', () => {
|
|
|
41
56
|
})
|
|
42
57
|
})
|
|
43
58
|
|
|
44
|
-
it('
|
|
59
|
+
it('only accepts one well-formed v2 AES-GCM query payload', () => {
|
|
45
60
|
const codec = createRouteQueryEncryption({ enabled: true, key })!
|
|
46
61
|
const encoded = codec.stringifyQuery({ redirect: '/dashboard' })
|
|
47
62
|
const tampered = `${encoded.slice(0, -1)}${encoded.endsWith('a') ? 'b' : 'a'}`
|
|
@@ -49,6 +64,17 @@ describe('route query encryption', () => {
|
|
|
49
64
|
expect(codec.parseQuery(tampered)).toEqual({})
|
|
50
65
|
expect(codec.parseQuery('redirect=%2Fdashboard')).toEqual({})
|
|
51
66
|
expect(codec.parseQuery('_q=invalid')).toEqual({})
|
|
67
|
+
expect(codec.parseQuery('_q=v1.nonce.ciphertext.mac')).toEqual({})
|
|
68
|
+
expect(codec.parseQuery('_q=v2.invalid.ciphertext')).toEqual({})
|
|
69
|
+
expect(codec.parseQuery('_q=v2.aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaa')).toEqual({})
|
|
70
|
+
expect(codec.parseQuery(`${encoded}&_q=${encodeURIComponent('v2.invalid.ciphertext')}`)).toEqual({})
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('rejects authenticated JSON values that are not a route query shape', () => {
|
|
74
|
+
const codec = createRouteQueryEncryption({ enabled: true, key })!
|
|
75
|
+
|
|
76
|
+
expect(codec.parseQuery(encodeV2('["not-an-object"]'))).toEqual({})
|
|
77
|
+
expect(codec.parseQuery(encodeV2('{"nested":{"value":"not-a-query-value"}}'))).toEqual({})
|
|
52
78
|
})
|
|
53
79
|
|
|
54
80
|
it('preserves encrypted query semantics while a dynamic route loads', async () => {
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import type { LocationQuery, LocationQueryRaw } from 'vue-router'
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import Base64 from 'crypto-js/enc-base64'
|
|
5
|
-
import HmacSHA256 from 'crypto-js/hmac-sha256'
|
|
2
|
+
import { gcm } from '@noble/ciphers/aes.js'
|
|
3
|
+
import { randomBytes } from '@noble/ciphers/utils.js'
|
|
6
4
|
|
|
7
5
|
const QUERY_PARAMETER = '_q'
|
|
8
|
-
const VERSION = '
|
|
6
|
+
const VERSION = 'v2'
|
|
9
7
|
const HEX_KEY = /^[\dA-F]{64}$/i
|
|
10
8
|
const URL_SAFE_BASE64 = /^[\w-]+$/
|
|
9
|
+
const NONCE_LENGTH = 12
|
|
10
|
+
const TAG_LENGTH = 16
|
|
11
|
+
const encoder = new TextEncoder()
|
|
12
|
+
const decoder = new TextDecoder('utf-8', { fatal: true })
|
|
11
13
|
|
|
12
14
|
type QueryValue = string | null | string[] | (string | null)[]
|
|
13
15
|
type EncodedQuery = Record<string, QueryValue>
|
|
@@ -22,22 +24,24 @@ export interface RouteQueryEncryption {
|
|
|
22
24
|
stringifyQuery: (query: LocationQueryRaw | undefined) => string
|
|
23
25
|
}
|
|
24
26
|
|
|
25
|
-
function toUrlSafeBase64(value:
|
|
26
|
-
|
|
27
|
+
function toUrlSafeBase64(value: Uint8Array): string {
|
|
28
|
+
let binary = ''
|
|
29
|
+
for (const byte of value) binary += String.fromCharCode(byte)
|
|
30
|
+
return btoa(binary).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/u, '')
|
|
27
31
|
}
|
|
28
32
|
|
|
29
|
-
function fromUrlSafeBase64(value: string):
|
|
30
|
-
if (!URL_SAFE_BASE64.test(value)) return undefined
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
function fromUrlSafeBase64(value: string): Uint8Array | undefined {
|
|
34
|
+
if (!URL_SAFE_BASE64.test(value) || value.length % 4 === 1) return undefined
|
|
35
|
+
try {
|
|
36
|
+
const padded = `${value.replaceAll('-', '+').replaceAll('_', '/')}${'='.repeat(
|
|
37
|
+
(4 - (value.length % 4)) % 4,
|
|
38
|
+
)}`
|
|
39
|
+
const binary = atob(padded)
|
|
40
|
+
const bytes = Uint8Array.from(binary, (character) => character.charCodeAt(0))
|
|
41
|
+
return toUrlSafeBase64(bytes) === value ? bytes : undefined
|
|
42
|
+
} catch {
|
|
43
|
+
return undefined
|
|
44
|
+
}
|
|
41
45
|
}
|
|
42
46
|
|
|
43
47
|
function normalizeQuery(query: LocationQueryRaw | undefined): EncodedQuery {
|
|
@@ -72,6 +76,10 @@ function readCiphertext(search: string): string | undefined {
|
|
|
72
76
|
return values.length === 1 ? values[0] : undefined
|
|
73
77
|
}
|
|
74
78
|
|
|
79
|
+
function keyFromHex(value: string): Uint8Array {
|
|
80
|
+
return Uint8Array.from(value.match(/../gu)!.map((pair) => Number.parseInt(pair, 16)))
|
|
81
|
+
}
|
|
82
|
+
|
|
75
83
|
export function createRouteQueryEncryption(
|
|
76
84
|
options: RouteQueryEncryptionOptions = {},
|
|
77
85
|
): RouteQueryEncryption | undefined {
|
|
@@ -82,47 +90,43 @@ export function createRouteQueryEncryption(
|
|
|
82
90
|
)
|
|
83
91
|
}
|
|
84
92
|
|
|
85
|
-
const encryptionKey =
|
|
86
|
-
const authenticationKey = HmacSHA256('route-query-authentication', encryptionKey)
|
|
87
|
-
|
|
88
|
-
function mac(value: string): string {
|
|
89
|
-
return HmacSHA256(value, authenticationKey).toString(CryptoJS.enc.Hex)
|
|
90
|
-
}
|
|
93
|
+
const encryptionKey = keyFromHex(options.key)
|
|
91
94
|
|
|
92
95
|
return {
|
|
93
96
|
stringifyQuery(query) {
|
|
94
97
|
const normalized = normalizeQuery(query)
|
|
95
98
|
if (Object.keys(normalized).length === 0) return ''
|
|
96
|
-
const
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
99
|
+
const nonce = randomBytes(NONCE_LENGTH)
|
|
100
|
+
const ciphertextAndTag = gcm(encryptionKey, nonce).encrypt(
|
|
101
|
+
encoder.encode(JSON.stringify(normalized)),
|
|
102
|
+
)
|
|
103
|
+
const payload = `${VERSION}.${toUrlSafeBase64(nonce)}.${toUrlSafeBase64(ciphertextAndTag)}`
|
|
104
|
+
return `${QUERY_PARAMETER}=${encodeURIComponent(payload)}`
|
|
100
105
|
},
|
|
101
106
|
parseQuery(search) {
|
|
102
107
|
const value = readCiphertext(search)
|
|
103
108
|
if (!value) return {}
|
|
104
109
|
try {
|
|
105
|
-
const [version,
|
|
110
|
+
const [version, nonceValue, ciphertextValue, ...extra] = value.split('.')
|
|
106
111
|
if (
|
|
107
112
|
version !== VERSION ||
|
|
108
|
-
!
|
|
113
|
+
!nonceValue ||
|
|
109
114
|
!ciphertextValue ||
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
115
|
+
extra.length > 0
|
|
116
|
+
) {
|
|
117
|
+
return {}
|
|
118
|
+
}
|
|
119
|
+
const nonce = fromUrlSafeBase64(nonceValue)
|
|
120
|
+
const ciphertextAndTag = fromUrlSafeBase64(ciphertextValue)
|
|
121
|
+
if (
|
|
122
|
+
!nonce ||
|
|
123
|
+
nonce.length !== NONCE_LENGTH ||
|
|
124
|
+
!ciphertextAndTag ||
|
|
125
|
+
ciphertextAndTag.length <= TAG_LENGTH
|
|
113
126
|
) {
|
|
114
127
|
return {}
|
|
115
128
|
}
|
|
116
|
-
const
|
|
117
|
-
if (!equalsMac(suppliedMac, mac(payload))) return {}
|
|
118
|
-
const iv = fromUrlSafeBase64(ivValue)
|
|
119
|
-
const ciphertext = fromUrlSafeBase64(ciphertextValue)
|
|
120
|
-
if (!iv || !ciphertext) return {}
|
|
121
|
-
const plaintext = AES.decrypt(
|
|
122
|
-
CryptoJS.lib.CipherParams.create({ ciphertext }),
|
|
123
|
-
encryptionKey,
|
|
124
|
-
{ iv },
|
|
125
|
-
).toString(CryptoJS.enc.Utf8)
|
|
129
|
+
const plaintext = decoder.decode(gcm(encryptionKey, nonce).decrypt(ciphertextAndTag))
|
|
126
130
|
const query: unknown = JSON.parse(plaintext)
|
|
127
131
|
return isEncodedQuery(query) ? (query as LocationQuery) : {}
|
|
128
132
|
} catch {
|