Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib/__tests__/dweb-update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type FetchResponse = {

function stubGlobals() {
vi.stubGlobal('__KEYAPP_SITE_ORIGIN__', 'https://example.com/KeyApp/')
vi.stubGlobal('__KEYAPP_BASE_URL__', './')
vi.stubGlobal('__DEV_MODE__', false)
vi.stubGlobal('__APP_VERSION__', '1.2.3')
Object.defineProperty(window, '__native_close_watcher_kit__', {
Expand All @@ -33,6 +34,13 @@ describe('dweb update', () => {
expect(url).toBe('https://example.com/KeyApp/dweb-dev/metadata.json')
})

it('resolves metadata url with base url when origin has no path', () => {
vi.stubGlobal('__KEYAPP_SITE_ORIGIN__', 'https://example.com')
vi.stubGlobal('__KEYAPP_BASE_URL__', '/KeyApp/')
const url = resolveDwebMetadataUrl()
expect(url).toBe('https://example.com/KeyApp/dweb/metadata.json')
})

it('builds install url', () => {
const url = resolveInstallUrl('https://example.com/KeyApp/dweb/metadata.json')
expect(url).toBe('dweb://install?url=https%3A%2F%2Fexample.com%2FKeyApp%2Fdweb%2Fmetadata.json')
Expand Down
23 changes: 22 additions & 1 deletion src/lib/dweb-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,29 @@ function normalizeOrigin(origin: string): string {
return origin.endsWith('/') ? origin : `${origin}/`
}

function normalizeBasePath(basePath: string): string {
if (!basePath) return './'
return basePath.endsWith('/') ? basePath : `${basePath}/`
}

function resolveSiteBase(): string {
const origin = normalizeOrigin(__KEYAPP_SITE_ORIGIN__)
try {
const originUrl = new URL(origin)
const originPath = originUrl.pathname
if (originPath && originPath !== '/') {
return normalizeOrigin(origin)
}
} catch {
return origin
}

const basePath = normalizeBasePath(__KEYAPP_BASE_URL__)
return normalizeOrigin(new URL(basePath, origin).toString())
}

export function resolveDwebMetadataUrl(): string {
const base = normalizeOrigin(__KEYAPP_SITE_ORIGIN__)
const base = resolveSiteBase()
const path = __DEV_MODE__ ? 'dweb-dev/metadata.json' : 'dweb/metadata.json'
return new URL(path, base).toString()
}
Expand Down
3 changes: 3 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ declare global {
/** KeyApp 官网 Origin - 通过 vite.config.ts define 配置 */
const __KEYAPP_SITE_ORIGIN__: string

/** KeyApp Base URL - 通过 vite.config.ts define 配置 */
const __KEYAPP_BASE_URL__: string

/** 默认生态源列表 - 通过 vite.config.ts define 配置 */
const __ECOSYSTEM_SOURCES__: Array<{ name: string; url: string; icon?: string }>

Expand Down
2 changes: 2 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ export default defineConfig(({ mode }) => {
__APP_VERSION__: JSON.stringify(appVersion),
// KeyApp 官网 Origin(DWEB 升级检查使用)
__KEYAPP_SITE_ORIGIN__: JSON.stringify(siteOrigin),
// KeyApp Base URL(DWEB 升级链接拼接使用)
__KEYAPP_BASE_URL__: JSON.stringify(BASE_URL),
// 默认生态源列表(用于订阅源管理展示)
__ECOSYSTEM_SOURCES__: JSON.stringify(ecosystemSources),
// API Keys 对象(用于动态读取环境变量)
Expand Down