MCPcopy Create free account
hub / github.com/agent-tower/core / parsePreviewPath

Function parsePreviewPath

packages/server/src/utils/preview-path.ts:22–56  ·  view source on GitHub ↗
(url: string)

Source from the content-addressed store, hash-verified

20}
21
22export function parsePreviewPath(url: string): ParsedPreviewPath | null {
23 const parsed = new URL(url, 'http://agent-tower.local');
24 const prefix = `${PREVIEW_PREFIX}/`;
25 if (!parsed.pathname.startsWith(prefix)) return null;
26
27 const rest = parsed.pathname.slice(prefix.length);
28 const slashIndex = rest.indexOf('/');
29 const encodedWorkspaceId = slashIndex === -1 ? rest : rest.slice(0, slashIndex);
30 if (!encodedWorkspaceId) return null;
31
32 const workspaceId = decodeURIComponent(encodedWorkspaceId);
33 let suffix = slashIndex === -1 ? '/' : rest.slice(slashIndex) || '/';
34 let previewToken: string | null = null;
35
36 if (suffix !== '/') {
37 const suffixRest = suffix.slice(1);
38 const markerSlashIndex = suffixRest.indexOf('/');
39 const encodedFirstSegment = markerSlashIndex === -1 ? suffixRest : suffixRest.slice(0, markerSlashIndex);
40 const firstSegment = decodeURIComponent(encodedFirstSegment);
41
42 if (firstSegment === PREVIEW_ACCESS_TOKEN_MARKER && markerSlashIndex !== -1) {
43 const afterMarker = suffixRest.slice(markerSlashIndex + 1);
44 const tokenSlashIndex = afterMarker.indexOf('/');
45 const encodedToken = tokenSlashIndex === -1 ? afterMarker : afterMarker.slice(0, tokenSlashIndex);
46 const token = decodeURIComponent(encodedToken);
47 if (isPreviewAccessTokenSegment(token)) {
48 previewToken = token;
49 suffix = tokenSlashIndex === -1 ? '/' : `/${afterMarker.slice(tokenSlashIndex + 1)}`;
50 if (!suffix) suffix = '/';
51 }
52 }
53 }
54
55 return { workspaceId, previewToken, suffix, search: parsed.search };
56}

Callers 6

proxyHttpRequestFunction · 0.85
proxyWebSocketRequestFunction · 0.85
handlerFunction · 0.85
previews.test.tsFile · 0.85

Calls 1

Tested by

no test coverage detected