77 lines
2.5 KiB
TypeScript
Raw Normal View History

import { ServerConnection } from "@jupyterlab/services";
import "@jupyterlab/terminal/style/index.css";
import { HttpHeaders, TerminalQueryParams } from "../Common/Constants";
2020-09-16 17:04:34 -07:00
import { Action } from "../Shared/Telemetry/TelemetryConstants";
import * as TelemetryProcessor from "../Shared/Telemetry/TelemetryProcessor";
import { updateUserContext } from "../UserContext";
import "./index.css";
import { JupyterLabAppFactory } from "./JupyterLabAppFactory";
const getUrlVars = (): { [key: string]: string } => {
const vars: { [key: string]: string } = {};
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, (_m, key, value): string => {
vars[key] = decodeURIComponent(value);
return value;
});
return vars;
};
2020-09-16 17:04:34 -07:00
const createServerSettings = (urlVars: { [key: string]: string }): ServerConnection.ISettings => {
let body: BodyInit | undefined;
let headers: HeadersInit | undefined;
if (urlVars.hasOwnProperty(TerminalQueryParams.TerminalEndpoint)) {
body = JSON.stringify({
2021-01-20 09:15:01 -06:00
endpoint: urlVars[TerminalQueryParams.TerminalEndpoint],
});
headers = {
2021-01-20 09:15:01 -06:00
[HttpHeaders.contentType]: "application/json",
};
}
const server = urlVars[TerminalQueryParams.Server];
let options: Partial<ServerConnection.ISettings> = {
baseUrl: server,
init: { body, headers },
2021-01-20 09:15:01 -06:00
fetch: window.parent.fetch,
};
if (urlVars.hasOwnProperty(TerminalQueryParams.Token)) {
options = {
baseUrl: server,
token: urlVars[TerminalQueryParams.Token],
appendToken: true,
init: { body, headers },
2021-01-20 09:15:01 -06:00
fetch: window.parent.fetch,
};
}
2020-09-16 17:04:34 -07:00
return ServerConnection.makeSettings(options);
};
const main = async (): Promise<void> => {
const urlVars = getUrlVars();
// Initialize userContext. Currently only subscriptionId is required by TelemetryProcessor
updateUserContext({
2021-01-20 09:15:01 -06:00
subscriptionId: urlVars[TerminalQueryParams.SubscriptionId],
});
2020-09-16 17:04:34 -07:00
const serverSettings = createServerSettings(urlVars);
const data = { baseUrl: serverSettings.baseUrl };
const startTime = TelemetryProcessor.traceStart(Action.OpenTerminal, data);
2020-09-16 17:04:34 -07:00
try {
if (urlVars.hasOwnProperty(TerminalQueryParams.Terminal)) {
2020-09-16 17:04:34 -07:00
await JupyterLabAppFactory.createTerminalApp(serverSettings);
} else {
throw new Error("Only terminal is supported");
}
TelemetryProcessor.traceSuccess(Action.OpenTerminal, data, startTime);
2020-09-16 17:04:34 -07:00
} catch (error) {
TelemetryProcessor.traceFailure(Action.OpenTerminal, data, startTime);
2020-09-16 17:04:34 -07:00
}
};
window.addEventListener("load", main);