UNPKG

4 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Jupyter Development Team.
3// Distributed under the terms of the Modified BSD License.
4Object.defineProperty(exports, "__esModule", { value: true });
5exports.shutdownTerminal = exports.listRunning = exports.startNew = exports.isAvailable = exports.TERMINAL_SERVICE_URL = void 0;
6const coreutils_1 = require("@jupyterlab/coreutils");
7const serverconnection_1 = require("../serverconnection");
8/**
9 * The url for the terminal service.
10 */
11exports.TERMINAL_SERVICE_URL = 'api/terminals';
12/**
13 * Whether the terminal service is available.
14 */
15function isAvailable() {
16 const available = String(coreutils_1.PageConfig.getOption('terminalsAvailable'));
17 return available.toLowerCase() === 'true';
18}
19exports.isAvailable = isAvailable;
20/**
21 * Start a new terminal session.
22 *
23 * @param settings - The server settings to use.
24 *
25 * @param name - The name of the target terminal.
26 *
27 * @param cwd - The path in which the terminal will start.
28 *
29 * @returns A promise that resolves with the session model.
30 */
31async function startNew(settings = serverconnection_1.ServerConnection.makeSettings(), name, cwd) {
32 Private.errorIfNotAvailable();
33 const url = coreutils_1.URLExt.join(settings.baseUrl, exports.TERMINAL_SERVICE_URL);
34 const init = {
35 method: 'POST',
36 body: JSON.stringify({ name, cwd })
37 };
38 const response = await serverconnection_1.ServerConnection.makeRequest(url, init, settings);
39 if (response.status !== 200) {
40 const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
41 throw err;
42 }
43 const data = await response.json();
44 // TODO: Validate model
45 return data;
46}
47exports.startNew = startNew;
48/**
49 * List the running terminal sessions.
50 *
51 * @param settings - The server settings to use.
52 *
53 * @returns A promise that resolves with the list of running session models.
54 */
55async function listRunning(settings = serverconnection_1.ServerConnection.makeSettings()) {
56 Private.errorIfNotAvailable();
57 const url = coreutils_1.URLExt.join(settings.baseUrl, exports.TERMINAL_SERVICE_URL);
58 const response = await serverconnection_1.ServerConnection.makeRequest(url, {}, settings);
59 if (response.status !== 200) {
60 const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
61 throw err;
62 }
63 const data = await response.json();
64 if (!Array.isArray(data)) {
65 throw new Error('Invalid terminal list');
66 }
67 // TODO: validate each model
68 return data;
69}
70exports.listRunning = listRunning;
71/**
72 * Shut down a terminal session by name.
73 *
74 * @param name - The name of the target session.
75 *
76 * @param settings - The server settings to use.
77 *
78 * @returns A promise that resolves when the session is shut down.
79 */
80async function shutdownTerminal(name, settings = serverconnection_1.ServerConnection.makeSettings()) {
81 var _a;
82 Private.errorIfNotAvailable();
83 const url = coreutils_1.URLExt.join(settings.baseUrl, exports.TERMINAL_SERVICE_URL, name);
84 const init = { method: 'DELETE' };
85 const response = await serverconnection_1.ServerConnection.makeRequest(url, init, settings);
86 if (response.status === 404) {
87 const data = await response.json();
88 const msg = (_a = data.message) !== null && _a !== void 0 ? _a : `The terminal session "${name}"" does not exist on the server`;
89 console.warn(msg);
90 }
91 else if (response.status !== 204) {
92 const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
93 throw err;
94 }
95}
96exports.shutdownTerminal = shutdownTerminal;
97var Private;
98(function (Private) {
99 /**
100 * Throw an error if terminals are not available.
101 */
102 function errorIfNotAvailable() {
103 if (!isAvailable()) {
104 throw new Error('Terminals Unavailable');
105 }
106 }
107 Private.errorIfNotAvailable = errorIfNotAvailable;
108})(Private || (Private = {}));
109//# sourceMappingURL=restapi.js.map
\No newline at end of file