贡献者 PR #584(fix: preserve reverse-proxy base paths,head 1ca99d8)的改动面在 dev 上仍然需要——JSON API / upload / file / html / bundle 与两条 WebSocket 都还是 源站绝对路径(带前导斜杠 → 反代子路径下 404 / 1006),目标 issue #753 仍 OPEN, docs/plans/2026-10-04-issue-triage.md 也把 #584 记为优先合入项。但它不能机械解冲突, 故按 dev 现有结构重写。 复用(PR 的可复用部分): - 新增 src/client/host-route-url.ts:hostRouteUrl() / hostWebSocketUrl() 先去前导 斜杠,再按注入基底解析;基底去 search/hash 并强制目录尾斜杠——launch token 查询串 与「缺尾斜杠的前缀」都不会吃掉路径最后一段。 改写(dev 侧已经变掉的东西): - 基底来源并进 dev 的 2718725:desktop-env.ts 的 sidebarWebSocketBase() 泛化为 hostTransportBase()(__DSH_TRANSPORT__.streamBaseUrl 优先,回落 document.baseURI), HTTP 与 WS 从此共用一个基底权威,而不是另起一套。丢弃 PR 的 hostDocumentBase() 「取 document.baseURI 与 location.href 中更长者」启发式:它会顶掉刚落地的桌面壳修复 (dsh-app://app/ 下 location.href 会重新拼出不可达的 ws://app/…),而基线 0.2.0-rc.1 的宿主产物里没有 <base href>(PR 自己的 e2e 也只采集、从不断言它)。 最终解析式与宿主 dsh-api-gateway 逐字同形(去前导斜杠 + 壳注入基底优先)。 - 丢弃 src/client/TerminalView.tsx 的改动:该文件在 dev 已删除(终端已交还宿主), 其覆盖的 WS 语义改由 dev 现存的两条调用点承担,并补齐 PR 文件清单里漏掉的 src/client/use-dir-watch.ts(fs-watch socket)。 调用点(一律相对路径 + 前缀解析):api.ts 的 JSON API / upload / file / html / archive、chunk-loader.ts 的 CHUNK_URL、markdown-images.ts 与 MarkdownHtml / TextEditor / DiffPane 的媒体改写(原 window.location.origin 在桌面壳下是 dsh-app://app,媒体同样不可达)、use-host-feeds.ts 与 use-dir-watch.ts 的两条 WebSocket。archiveDownloadUrl 是 PR 漏掉的同类调用点(FileTree 用它 fetch zip 字节), 一并纳入;open-in-app.ts 打的是宿主路由 /open-in-app/*,与 PR 明确不碰的宿主 /api 同类,不纳入。 消费插件契约(指南「接入 API 即文档」): - BetterSidebarService 新增 hostRouteUrl(path)(features 'hostRouteUrl'), docs/external-plugin-guide.md §6 / §7 / §15 的 fetch('/sidebar/api/…') 示例改为 经它解析,并写明「绝不写前导斜杠」。 验证:pnpm typecheck / pnpm exec vitest run(161 files / 1812 passed / 13 skipped)/ eslint 全绿。tests/base-path.spec.ts 钉前缀保留与基底优先级,并在真实调用点上断言 (api fetch 的 URL、chunk 注入的 script src、jsdom 下 fs-watch 的 socket URL); PR 自带的 base-path e2e 在 page.evaluate 里复制实现后自证,回归守不住,故不引入该 lane。 Refs #584 Co-authored-by: idoall <6040958+idoall@users.noreply.github.com>
141 lines
6.9 KiB
TypeScript
141 lines
6.9 KiB
TypeScript
import './browser-globals.ts'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { decodeHtmlUrl } from '../src/html-route.ts'
|
|
import { isAbsolutePath, relativeTo, workspaceRelativePath } from '../src/client/paths.ts'
|
|
import { resolveSidebarPath } from '../src/client/paths.ts'
|
|
import { downloadUrl, htmlUrl, mediaUrl } from '../src/client/api.ts'
|
|
|
|
describe('path helpers', () => {
|
|
it('derives relative paths under the cwd (and "." for the cwd itself)', () => {
|
|
expect(relativeTo('/Users/me/code', '/Users/me/code/src/main.ts')).toBe('src/main.ts')
|
|
expect(relativeTo('/Users/me/code', '/Users/me/code')).toBe('.')
|
|
expect(relativeTo('/Users/me/code/', '/Users/me/code/src/a/b.ts')).toBe('src/a/b.ts')
|
|
})
|
|
|
|
it('falls back to the path unchanged when it lies outside the cwd', () => {
|
|
expect(relativeTo('/Users/me/code', '/Users/other/x.ts')).toBe('/Users/other/x.ts')
|
|
expect(relativeTo('/Users/me/code', '/Users/me/codex/y.ts')).toBe('/Users/me/codex/y.ts')
|
|
})
|
|
|
|
it('handles windows roots and mixed separators', () => {
|
|
expect(relativeTo('C:\\Users\\me', 'C:\\Users\\me\\src\\a.ts')).toBe('src/a.ts')
|
|
expect(relativeTo('C:\\Users\\me', 'C:/Users/me/src/a.ts')).toBe('src/a.ts')
|
|
expect(relativeTo('C:\\Users\\me\\', 'C:\\Users\\me')).toBe('.')
|
|
})
|
|
|
|
it('containment is case-insensitive (windows/macOS case-insensitive volumes)', () => {
|
|
expect(relativeTo('C:\\Users\\Me', 'c:/users/me/src/a.ts')).toBe('src/a.ts')
|
|
expect(relativeTo('/Users/Me/code', '/users/me/code/src/main.ts')).toBe('src/main.ts')
|
|
// The returned relative text keeps the caller's own casing.
|
|
expect(relativeTo('C:\\Users\\me', 'C:\\Users\\Me\\SRC\\a.ts')).toBe('SRC/a.ts')
|
|
})
|
|
|
|
it('resolves produced paths against windows cwds', () => {
|
|
expect(resolveSidebarPath('C:\\work\\proj', 'src/a.ts')).toBe('C:\\work\\proj\\src/a.ts')
|
|
expect(resolveSidebarPath('C:\\work\\proj', 'C:\\abs\\x.ts')).toBe('C:\\abs\\x.ts')
|
|
expect(resolveSidebarPath('C:\\work\\proj\\', 'C:\\abs\\x.ts')).toBe('C:\\abs\\x.ts')
|
|
})
|
|
|
|
it('keeps UNC produced paths absolute instead of joining them onto the cwd', () => {
|
|
// Pure client function: UNC detection is platform-independent, so these
|
|
// assertions run on every host without a platform guard.
|
|
expect(resolveSidebarPath('C:\\work\\proj', '\\\\server\\share\\abs\\x.ts'))
|
|
.toBe('\\\\server\\share\\abs\\x.ts')
|
|
expect(resolveSidebarPath('C:\\work\\proj', '//server/share/abs/x.ts'))
|
|
.toBe('//server/share/abs/x.ts')
|
|
// A relative path under a UNC cwd joins with backslashes.
|
|
expect(resolveSidebarPath('\\\\server\\share\\proj', 'src/a.ts'))
|
|
.toBe('\\\\server\\share\\proj\\src/a.ts')
|
|
})
|
|
|
|
it('mirrors the host absolute-path notion without node:path', () => {
|
|
expect(isAbsolutePath('/abs/x.ts')).toBe(true)
|
|
expect(isAbsolutePath('C:\\abs\\x.ts')).toBe(true)
|
|
expect(isAbsolutePath('C:/abs/x.ts')).toBe(true)
|
|
expect(isAbsolutePath('\\\\server\\share\\x.ts')).toBe(true)
|
|
expect(isAbsolutePath('//server/share/x.ts')).toBe(true)
|
|
expect(isAbsolutePath('C:relative.ts')).toBe(false)
|
|
expect(isAbsolutePath('rel/x.ts')).toBe(false)
|
|
})
|
|
|
|
it('treats ~ as home-absolute, never a session-relative path (#713)', () => {
|
|
expect(isAbsolutePath('~')).toBe(true)
|
|
expect(isAbsolutePath('~/notes/x.md')).toBe(true)
|
|
expect(isAbsolutePath('~\\notes\\x.md')).toBe(true)
|
|
// `~other-user` is NOT the home shorthand; it stays relative-shaped.
|
|
expect(isAbsolutePath('~other/x.ts')).toBe(false)
|
|
})
|
|
|
|
it('htmlUrl always marks UNC paths (platform-neutral marker)', () => {
|
|
// The marker is platform-neutral now: the host resolves the decoded
|
|
// '//server/share/...' form per-platform, so no cwd/OS signal is needed.
|
|
expect(htmlUrl({ sessionId: 's' }, '\\\\server\\share\\proj\\a.html'))
|
|
.toBe('http://localhost/sidebar/html/s//server/share/proj/a.html')
|
|
expect(htmlUrl({ sessionId: 's', cwd: '/home/me' }, '//server/share/a.html'))
|
|
.toBe('http://localhost/sidebar/html/s//server/share/a.html')
|
|
expect(htmlUrl({ sessionId: 's', cwd: '/home/me' }, '/home/me/index.html'))
|
|
.toBe('http://localhost/sidebar/html/s/home/me/index.html')
|
|
})
|
|
})
|
|
|
|
describe('preview URL path resolution', () => {
|
|
it.each([
|
|
['/home/project', 'pages/report.html', '/home/project/pages/report.html'],
|
|
['/home/project/', '报告 #1.html', '/home/project/报告 #1.html'],
|
|
['C:\\work\\project', 'pages/report.html', 'C:/work/project/pages/report.html'],
|
|
['\\\\server\\share\\project', 'report.html', '//server/share/project/report.html'],
|
|
['/home/project', '/tmp/report.html', '/tmp/report.html'],
|
|
])('resolves %s + %s before encoding', (cwd, path, expected) => {
|
|
const scope = { sessionId: 'session #1', cwd }
|
|
const decoded = decodeHtmlUrl(new URL(htmlUrl(scope, path), 'http://localhost').pathname)
|
|
expect(decoded).toEqual({ ok: true, ref: { sessionId: scope.sessionId, path: expected } })
|
|
for (const build of [mediaUrl, downloadUrl]) {
|
|
const url = new URL(build(scope, path), 'http://localhost')
|
|
expect(url.searchParams.get('path')?.replace(/\\/g, '/')).toBe(expected)
|
|
expect(url.searchParams.get('cwd')).toBe(cwd)
|
|
expect(url.searchParams.get('download')).toBe(build === downloadUrl ? '1' : null)
|
|
}
|
|
})
|
|
|
|
it('keeps nested relative assets in the same session and document directory', () => {
|
|
const page = new URL(htmlUrl({ sessionId: 's', cwd: '/work/project' }, 'pages/report.html'), 'http://localhost')
|
|
for (const [asset, expected] of [['./style.css', '/work/project/pages/style.css'], ['../img/pic.png', '/work/project/img/pic.png']] as const) {
|
|
expect(decodeHtmlUrl(new URL(asset, page).pathname)).toEqual({ ok: true, ref: { sessionId: 's', path: expected } })
|
|
}
|
|
})
|
|
|
|
it('leaves relative file queries for server resolution when cwd is unavailable', () => {
|
|
expect(new URL(mediaUrl({ sessionId: 's' }, 'pic.png'), 'http://localhost').searchParams.get('path')).toBe('pic.png')
|
|
})
|
|
})
|
|
|
|
describe('workspaceRelativePath', () => {
|
|
it.each([
|
|
['/work', '/work/src/a.ts', 'src/a.ts'],
|
|
['/work/', '/work', '.'],
|
|
['/', '/src/a.ts', 'src/a.ts'],
|
|
['/', '/', '.'],
|
|
['C:\\Work', 'c:/work/SRC/a.ts', 'SRC/a.ts'],
|
|
['C:\\Work\\', 'c:/WORK', '.'],
|
|
['C:\\', 'c:/SRC/a.ts', 'SRC/a.ts'],
|
|
['C:\\Work', 'c:/work/my dir\\a.ts', 'my dir/a.ts'],
|
|
['\\\\server\\share\\Work', '//SERVER/share/work/src/a.ts', 'src/a.ts'],
|
|
])('projects %s / %s', (root, path, expected) => {
|
|
expect(workspaceRelativePath(root, path)).toBe(expected)
|
|
})
|
|
|
|
it.each([
|
|
[undefined, '/work/a.ts'],
|
|
['', '/work/a.ts'],
|
|
['work', '/work/a.ts'],
|
|
['/work', '/outside/a.ts'],
|
|
['/work', '/work-other/a.ts'],
|
|
['/work', '/WORK/a.ts'],
|
|
['/work', '/work/../outside/a.ts'],
|
|
['/work', 'src/a.ts'],
|
|
['C:/Work', 'D:/Work/a.ts'],
|
|
])('refuses an unavailable root or unsafe projection %j / %s', (root, path) => {
|
|
expect(workspaceRelativePath(root, path)).toBeUndefined()
|
|
})
|
|
})
|