|
| 1 | +import { projectShortKey } from "./project-port-proxy-core.js" |
| 2 | + |
| 3 | +export const browserNoVncPort = 6080 |
| 4 | +export const browserCdpPort = 9223 |
| 5 | +export const browserVncPort = 5900 |
| 6 | + |
| 7 | +export type ProjectBrowserProxyPath = |
| 8 | + | { |
| 9 | + readonly _tag: "NoVnc" |
| 10 | + readonly projectKey: string |
| 11 | + readonly upstreamPath: string |
| 12 | + } |
| 13 | + | { |
| 14 | + readonly _tag: "Cdp" |
| 15 | + readonly projectKey: string |
| 16 | + readonly upstreamPath: string |
| 17 | + } |
| 18 | + |
| 19 | +const browserPathPattern = /^\/(?:api\/)?b\/([a-f0-9]{12})(?:\/(.*))?$/u |
| 20 | + |
| 21 | +export const renderProjectBrowserProxyPath = (projectId: string): string => |
| 22 | + `/b/${projectShortKey(projectId)}/` |
| 23 | + |
| 24 | +export const renderProjectBrowserNoVncPath = (projectId: string): string => { |
| 25 | + const projectKey = projectShortKey(projectId) |
| 26 | + const params = new URLSearchParams({ |
| 27 | + autoconnect: "true", |
| 28 | + resize: "remote", |
| 29 | + path: `b/${projectKey}/websockify` |
| 30 | + }) |
| 31 | + return `/b/${projectKey}/vnc.html?${params.toString()}` |
| 32 | +} |
| 33 | + |
| 34 | +export const renderProjectBrowserCdpPath = (projectId: string): string => |
| 35 | + `/b/${projectShortKey(projectId)}/cdp/json/version` |
| 36 | + |
| 37 | +export const parseProjectBrowserProxyPath = (pathname: string): ProjectBrowserProxyPath | null => { |
| 38 | + const match = browserPathPattern.exec(pathname) |
| 39 | + if (match === null) { |
| 40 | + return null |
| 41 | + } |
| 42 | + const projectKey = match[1] |
| 43 | + const rawPath = match[2] ?? "" |
| 44 | + if (projectKey === undefined) { |
| 45 | + return null |
| 46 | + } |
| 47 | + if (rawPath === "cdp" || rawPath.startsWith("cdp/")) { |
| 48 | + return { |
| 49 | + _tag: "Cdp", |
| 50 | + projectKey, |
| 51 | + upstreamPath: `/${rawPath.slice("cdp".length).replace(/^\/?/u, "")}` |
| 52 | + } |
| 53 | + } |
| 54 | + return { |
| 55 | + _tag: "NoVnc", |
| 56 | + projectKey, |
| 57 | + upstreamPath: `/${rawPath}` |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +export const renderExternalUrl = (origin: string, path: string): string => { |
| 62 | + const trimmed = origin.endsWith("/") ? origin.slice(0, -1) : origin |
| 63 | + return `${trimmed}${path}` |
| 64 | +} |
| 65 | + |
| 66 | +export const rewriteCdpWebSocketUrl = ( |
| 67 | + value: string, |
| 68 | + externalOrigin: string, |
| 69 | + projectId: string |
| 70 | +): string => { |
| 71 | + const match = /^wss?:\/\/[^/]+\/(.+)$/u.exec(value) |
| 72 | + const upstreamPath = match?.[1] |
| 73 | + if (upstreamPath === undefined || upstreamPath.length === 0) { |
| 74 | + return value |
| 75 | + } |
| 76 | + const external = new URL(externalOrigin) |
| 77 | + external.protocol = external.protocol === "https:" ? "wss:" : "ws:" |
| 78 | + external.pathname = `/b/${projectShortKey(projectId)}/cdp/${upstreamPath}` |
| 79 | + external.search = "" |
| 80 | + external.hash = "" |
| 81 | + return external.toString() |
| 82 | +} |
| 83 | + |
| 84 | +export const rewriteCdpVersionPayload = ( |
| 85 | + payload: string, |
| 86 | + externalOrigin: string, |
| 87 | + projectId: string |
| 88 | +): string => { |
| 89 | + let decoded: unknown |
| 90 | + try { |
| 91 | + decoded = JSON.parse(payload) |
| 92 | + } catch { |
| 93 | + return payload |
| 94 | + } |
| 95 | + if (typeof decoded !== "object" || decoded === null || !("webSocketDebuggerUrl" in decoded)) { |
| 96 | + return payload |
| 97 | + } |
| 98 | + const current = decoded.webSocketDebuggerUrl |
| 99 | + if (typeof current !== "string") { |
| 100 | + return payload |
| 101 | + } |
| 102 | + return JSON.stringify({ |
| 103 | + ...decoded, |
| 104 | + webSocketDebuggerUrl: rewriteCdpWebSocketUrl(current, externalOrigin, projectId) |
| 105 | + }) |
| 106 | +} |
0 commit comments