1 | "use strict";
|
2 |
|
3 |
|
4 | Object.defineProperty(exports, "__esModule", { value: true });
|
5 | exports.shutdownTerminal = exports.listRunning = exports.startNew = exports.isAvailable = exports.TERMINAL_SERVICE_URL = void 0;
|
6 | const coreutils_1 = require("@jupyterlab/coreutils");
|
7 | const serverconnection_1 = require("../serverconnection");
|
8 |
|
9 |
|
10 |
|
11 | exports.TERMINAL_SERVICE_URL = 'api/terminals';
|
12 |
|
13 |
|
14 |
|
15 | function isAvailable() {
|
16 | const available = String(coreutils_1.PageConfig.getOption('terminalsAvailable'));
|
17 | return available.toLowerCase() === 'true';
|
18 | }
|
19 | exports.isAvailable = isAvailable;
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | async 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 |
|
45 | return data;
|
46 | }
|
47 | exports.startNew = startNew;
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | async 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 |
|
68 | return data;
|
69 | }
|
70 | exports.listRunning = listRunning;
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | async 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 | }
|
96 | exports.shutdownTerminal = shutdownTerminal;
|
97 | var Private;
|
98 | (function (Private) {
|
99 | |
100 |
|
101 |
|
102 | function errorIfNotAvailable() {
|
103 | if (!isAvailable()) {
|
104 | throw new Error('Terminals Unavailable');
|
105 | }
|
106 | }
|
107 | Private.errorIfNotAvailable = errorIfNotAvailable;
|
108 | })(Private || (Private = {}));
|
109 |
|
\ | No newline at end of file |