2020-05-25 21:30:55 -05:00
|
|
|
import { ServerConnection } from "@jupyterlab/services";
|
2022-10-06 11:32:19 -07:00
|
|
|
import { IMessage, ITerminalConnection } from "@jupyterlab/services/lib/terminal/terminal";
|
2021-05-18 18:59:09 -04:00
|
|
|
import "@jupyterlab/terminal/style/index.css";
|
2022-01-10 11:58:35 -08:00
|
|
|
import { MessageTypes } from "Contracts/ExplorerContracts";
|
2021-07-12 14:48:13 -07:00
|
|
|
import postRobot from "post-robot";
|
|
|
|
import { HttpHeaders } from "../Common/Constants";
|
2020-09-16 17:04:34 -07:00
|
|
|
import { Action } from "../Shared/Telemetry/TelemetryConstants";
|
|
|
|
import * as TelemetryProcessor from "../Shared/Telemetry/TelemetryProcessor";
|
2020-09-22 12:19:06 -07:00
|
|
|
import { updateUserContext } from "../UserContext";
|
2021-05-18 18:59:09 -04:00
|
|
|
import "./index.css";
|
|
|
|
import { JupyterLabAppFactory } from "./JupyterLabAppFactory";
|
2021-07-12 14:48:13 -07:00
|
|
|
import { TerminalProps } from "./TerminalProps";
|
2020-05-25 21:30:55 -05:00
|
|
|
|
2021-07-12 14:48:13 -07:00
|
|
|
const createServerSettings = (props: TerminalProps): ServerConnection.ISettings => {
|
2021-01-12 12:55:47 -06:00
|
|
|
let body: BodyInit | undefined;
|
|
|
|
let headers: HeadersInit | undefined;
|
2021-07-12 14:48:13 -07:00
|
|
|
if (props.terminalEndpoint) {
|
2020-05-25 21:30:55 -05:00
|
|
|
body = JSON.stringify({
|
2021-07-12 14:48:13 -07:00
|
|
|
endpoint: props.terminalEndpoint,
|
2020-05-25 21:30:55 -05:00
|
|
|
});
|
2020-10-06 16:55:13 -07:00
|
|
|
headers = {
|
2021-01-20 09:15:01 -06:00
|
|
|
[HttpHeaders.contentType]: "application/json",
|
2020-10-06 16:55:13 -07:00
|
|
|
};
|
2020-05-25 21:30:55 -05:00
|
|
|
}
|
|
|
|
|
2021-07-12 14:48:13 -07:00
|
|
|
const server = props.notebookServerEndpoint;
|
2020-05-25 21:30:55 -05:00
|
|
|
let options: Partial<ServerConnection.ISettings> = {
|
|
|
|
baseUrl: server,
|
2020-10-06 16:55:13 -07:00
|
|
|
init: { body, headers },
|
2021-01-20 09:15:01 -06:00
|
|
|
fetch: window.parent.fetch,
|
2020-05-25 21:30:55 -05:00
|
|
|
};
|
2021-07-12 14:48:13 -07:00
|
|
|
if (props.authToken) {
|
2020-05-25 21:30:55 -05:00
|
|
|
options = {
|
|
|
|
baseUrl: server,
|
2021-07-12 14:48:13 -07:00
|
|
|
token: props.authToken,
|
2020-10-01 14:00:46 +02:00
|
|
|
appendToken: true,
|
2020-10-06 16:55:13 -07:00
|
|
|
init: { body, headers },
|
2021-01-20 09:15:01 -06:00
|
|
|
fetch: window.parent.fetch,
|
2020-05-25 21:30:55 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-16 17:04:34 -07:00
|
|
|
return ServerConnection.makeSettings(options);
|
|
|
|
};
|
|
|
|
|
2022-10-06 11:32:19 -07:00
|
|
|
const initTerminal = async (props: TerminalProps): Promise<ITerminalConnection | undefined> => {
|
2021-07-12 14:48:13 -07:00
|
|
|
// Initialize userContext (only properties which are needed by TelemetryProcessor)
|
2020-09-22 12:19:06 -07:00
|
|
|
updateUserContext({
|
2021-07-12 14:48:13 -07:00
|
|
|
subscriptionId: props.subscriptionId,
|
|
|
|
apiType: props.apiType,
|
|
|
|
authType: props.authType,
|
|
|
|
databaseAccount: props.databaseAccount,
|
2020-09-22 12:19:06 -07:00
|
|
|
});
|
|
|
|
|
2021-07-12 14:48:13 -07:00
|
|
|
const serverSettings = createServerSettings(props);
|
2020-12-10 11:54:21 -08:00
|
|
|
const data = { baseUrl: serverSettings.baseUrl };
|
|
|
|
const startTime = TelemetryProcessor.traceStart(Action.OpenTerminal, data);
|
2020-09-11 23:12:28 -07:00
|
|
|
|
2020-09-16 17:04:34 -07:00
|
|
|
try {
|
2022-10-06 11:32:19 -07:00
|
|
|
const session = await new JupyterLabAppFactory(() => closeTab(props.tabId)).createTerminalApp(serverSettings);
|
2020-12-10 11:54:21 -08:00
|
|
|
TelemetryProcessor.traceSuccess(Action.OpenTerminal, data, startTime);
|
2022-10-06 11:32:19 -07:00
|
|
|
return session;
|
2020-09-16 17:04:34 -07:00
|
|
|
} catch (error) {
|
2020-12-10 11:54:21 -08:00
|
|
|
TelemetryProcessor.traceFailure(Action.OpenTerminal, data, startTime);
|
2022-10-06 11:32:19 -07:00
|
|
|
return undefined;
|
2020-09-16 17:04:34 -07:00
|
|
|
}
|
2020-05-25 21:30:55 -05:00
|
|
|
};
|
|
|
|
|
2022-01-10 11:58:35 -08:00
|
|
|
const closeTab = (tabId: string): void => {
|
|
|
|
window.parent.postMessage(
|
|
|
|
{ type: MessageTypes.CloseTab, data: { tabId: tabId }, signature: "pcIframe" },
|
|
|
|
window.document.referrer
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-07-12 14:48:13 -07:00
|
|
|
const main = async (): Promise<void> => {
|
2022-10-06 11:32:19 -07:00
|
|
|
let session: ITerminalConnection | undefined;
|
2021-07-12 14:48:13 -07:00
|
|
|
postRobot.on(
|
|
|
|
"props",
|
|
|
|
{
|
|
|
|
window: window.parent,
|
|
|
|
domain: window.location.origin,
|
|
|
|
},
|
|
|
|
async (event) => {
|
|
|
|
// Typescript definition for event is wrong. So read props by casting to <any>
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const props = (event as any).data as TerminalProps;
|
2022-10-06 11:32:19 -07:00
|
|
|
session = await initTerminal(props);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
postRobot.on(
|
|
|
|
"sendMessage",
|
|
|
|
{
|
|
|
|
window: window.parent,
|
|
|
|
domain: window.location.origin,
|
|
|
|
},
|
|
|
|
async (event) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const message = (event as any).data as IMessage;
|
|
|
|
if (session) {
|
|
|
|
session.send(message);
|
|
|
|
}
|
2021-07-12 14:48:13 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-05-25 21:30:55 -05:00
|
|
|
window.addEventListener("load", main);
|