Skip to content

Commit 65ae9bd

Browse files
authored
feature/codespaces proxy url (#4683)
* detect github codespaces proxy url * only print full proxy url if we are running in the vscode terminal (as opposed to in the extension)
1 parent 9000dba commit 65ae9bd

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/command/preview/preview.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ import { projectOutputDir } from "../../project/project-shared.ts";
7474
import { projectContext } from "../../project/project-context.ts";
7575
import { normalizePath, pathWithForwardSlashes } from "../../core/path.ts";
7676
import {
77+
gitHubCodeSpacesProxyUri,
78+
isGitHubCodespaces,
7779
isJupyterHubServer,
7880
isRStudioServer,
7981
isRStudioWorkbench,
@@ -750,6 +752,9 @@ function pdfFileRequestHandler(
750752
: isVSCodeServer()
751753
? vsCodeServerProxyUri()!.replace("{{port}}", `${port}`) +
752754
kPdfJsInitialPath
755+
: isGitHubCodespaces()
756+
? gitHubCodeSpacesProxyUri()!.replace("{{port}}", `${port}`) +
757+
kPdfJsInitialPath
753758
: "/" + kPdfJsInitialPath;
754759
return Promise.resolve(serveRedirect(url));
755760
} else {

src/command/render/render-shared.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { resourceFilesFromRenderedFile } from "./resources.ts";
2121
import { RenderFlags, RenderOptions, RenderResult } from "./types.ts";
2222
import { fileExecutionEngine } from "../../execute/engine.ts";
2323
import {
24+
gitHubCodeSpacesProxyUri,
25+
isGitHubCodespaces,
2426
isJupyterHubServer,
2527
isJupyterServer,
2628
isRStudioServer,
@@ -188,6 +190,10 @@ export async function printBrowsePreviewMessage(
188190
const browseUrl = proxyUrl.replace("{{port}}", `${port}`) +
189191
path;
190192
info(`\nBrowse at ${browseUrl}`, { format: colors.green });
193+
} else if (isVSCodeTerminal() && isGitHubCodespaces()) {
194+
const browseUrl =
195+
gitHubCodeSpacesProxyUri()!.replace("{{port}}", `${port}`) + path;
196+
info(`\nBrowse at ${browseUrl}`, { format: colors.green });
191197
} else if (isJupyterHubServer()) {
192198
const httpReferrer = `${
193199
jupyterHubHttpReferrer() || "<jupyterhub-server-url>/"

src/core/platform.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ export function isVSCodeTerminal() {
3333
return Deno.env.get("TERM_PROGRAM") === "vscode";
3434
}
3535

36+
export function gitHubCodespaceName() {
37+
return Deno.env.get("CODESPACE_NAME");
38+
}
39+
40+
export function gitHubCodespacePortForwardingDomain() {
41+
return Deno.env.get("GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN");
42+
}
43+
44+
export function isGitHubCodespaces() {
45+
return !!gitHubCodespacePortForwardingDomain() || !!gitHubCodespaceName();
46+
}
47+
48+
export function gitHubCodeSpacesProxyUri() {
49+
const CODESPACE_NAME = gitHubCodespaceName();
50+
const GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN =
51+
gitHubCodespacePortForwardingDomain();
52+
if (CODESPACE_NAME && GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN) {
53+
return `https://${CODESPACE_NAME}-{{port}}.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}`;
54+
} else {
55+
return undefined;
56+
}
57+
}
58+
3659
export function isVSCodeServer() {
3760
return !!vsCodeServerProxyUri();
3861
}

0 commit comments

Comments
 (0)