This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
default.ts
441 lines (396 loc) · 11.1 KB
/
default.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
ArrayExt, each, map, toArray
} from '@phosphor/algorithm';
import {
JSONPrimitive
} from '@phosphor/coreutils';
import {
ISignal, Signal
} from '@phosphor/signaling';
import {
IAjaxSettings
} from '../utils';
import * as utils
from '../utils';
import {
TerminalSession
} from './terminal';
/**
* The url for the terminal service.
*/
const TERMINAL_SERVICE_URL = 'api/terminals';
/**
* An implementation of a terminal interface.
*/
export
class DefaultTerminalSession implements TerminalSession.ISession {
/**
* Construct a new terminal session.
*/
constructor(name: string, options: TerminalSession.IOptions = {}) {
this._name = name;
this._baseUrl = options.baseUrl || utils.getBaseUrl();
this._token = options.token || utils.getConfigOption('token');
this._ajaxSettings = JSON.stringify(
utils.ajaxSettingsWithToken(options.ajaxSettings, this._token)
);
this._wsUrl = options.wsUrl || utils.getWsUrl(this._baseUrl);
this._readyPromise = this._initializeSocket();
this.terminated = new Signal<this, void>(this);
}
/**
* A signal emitted when the session is shut down.
*/
readonly terminated: Signal<this, void>;
/**
* A signal emitted when a message is received from the server.
*/
get messageReceived(): ISignal<this, TerminalSession.IMessage> {
return this._messageReceived;
}
/**
* Get the name of the terminal session.
*/
get name(): string {
return this._name;
}
/**
* Get the model for the terminal session.
*/
get model(): TerminalSession.IModel {
return { name: this._name };
}
/**
* The base url of the terminal.
*/
get baseUrl(): string {
return this._baseUrl;
}
/**
* Get a copy of the default ajax settings for the terminal.
*/
get ajaxSettings(): IAjaxSettings {
return JSON.parse(this._ajaxSettings);
}
/**
* Set the default ajax settings for the terminal.
*/
set ajaxSettings(value: IAjaxSettings) {
this._ajaxSettings = JSON.stringify(value);
}
/**
* Test whether the session is ready.
*/
get isReady(): boolean {
return this._isReady;
}
/**
* A promise that fulfills when the session is ready.
*/
get ready(): Promise<void> {
return this._readyPromise;
}
/**
* Test whether the session is disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Dispose of the resources held by the session.
*/
dispose(): void {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
if (this._ws) {
this._ws.close();
this._ws = null;
}
delete Private.running[this._url];
this._readyPromise = null;
Signal.clearData(this);
}
/**
* Send a message to the terminal session.
*/
send(message: TerminalSession.IMessage): void {
if (this._isDisposed) {
return;
}
let msg: JSONPrimitive[] = [message.type];
msg.push(...message.content);
let value = JSON.stringify(msg);
if (this._isReady) {
this._ws.send(value);
return;
}
this.ready.then(() => {
this._ws.send(value);
});
}
/**
* Reconnect to the terminal.
*
* @returns A promise that resolves when the terminal has reconnected.
*/
reconnect(): Promise<void> {
this._readyPromise = this._initializeSocket();
return this._readyPromise;
}
/**
* Shut down the terminal session.
*/
shutdown(): Promise<void> {
let options = {
baseUrl: this._baseUrl,
ajaxSettings: this.ajaxSettings
};
return DefaultTerminalSession.shutdown(this.name, options);
}
/**
* Connect to the websocket.
*/
private _initializeSocket(): Promise<void> {
let name = this._name;
if (this._ws) {
this._ws.close();
}
this._isReady = false;
this._url = Private.getTermUrl(this._baseUrl, this._name);
Private.running[this._url] = this;
let wsUrl = utils.urlPathJoin(this._wsUrl, `terminals/websocket/${name}`);
if (this._token) {
wsUrl = wsUrl + `?token=${this._token}`;
}
this._ws = new WebSocket(wsUrl);
this._ws.onmessage = (event: MessageEvent) => {
if (this._isDisposed) {
return;
}
let data = JSON.parse(event.data) as JSONPrimitive[];
this._messageReceived.emit({
type: data[0] as TerminalSession.MessageType,
content: data.slice(1)
});
};
return new Promise<void>((resolve, reject) => {
this._ws.onopen = (event: MessageEvent) => {
if (this._isDisposed) {
return;
}
this._isReady = true;
resolve(void 0);
};
this._ws.onerror = (event: Event) => {
if (this._isDisposed) {
return;
}
reject(event);
};
});
}
private _name: string;
private _baseUrl: string;
private _wsUrl: string;
private _url: string;
private _token = '';
private _ajaxSettings = '';
private _ws: WebSocket = null;
private _isDisposed = false;
private _readyPromise: Promise<void>;
private _isReady = false;
private _messageReceived = new Signal<this, TerminalSession.IMessage>(this);
}
/**
* The static namespace for `DefaultTerminalSession`.
*/
export
namespace DefaultTerminalSession {
/**
* Whether the terminal service is available.
*/
export
function isAvailable(): boolean {
let available = String(utils.getConfigOption('terminalsAvailable'));
return available.toLowerCase() === 'true';
}
/**
* Start a new terminal session.
*
* @options - The session options to use.
*
* @returns A promise that resolves with the session instance.
*/
export
function startNew(options: TerminalSession.IOptions = {}): Promise<TerminalSession.ISession> {
if (!TerminalSession.isAvailable()) {
throw Private.unavailableMsg;
}
let baseUrl = options.baseUrl || utils.getBaseUrl();
let url = Private.getBaseUrl(baseUrl);
let ajaxSettings = utils.ajaxSettingsWithToken(options.ajaxSettings, options.token);
ajaxSettings.method = 'POST';
ajaxSettings.dataType = 'json';
return utils.ajaxRequest(url, ajaxSettings).then(success => {
if (success.xhr.status !== 200) {
throw utils.makeAjaxError(success);
}
let name = (success.data as TerminalSession.IModel).name;
return new DefaultTerminalSession(name, options);
});
}
/*
* Connect to a running session.
*
* @param name - The name of the target session.
*
* @param options - The session options to use.
*
* @returns A promise that resolves with the new session instance.
*
* #### Notes
* If the session was already started via `startNew`, the existing
* session object is used as the fulfillment value.
*
* Otherwise, if `options` are given, we resolve the promise after
* confirming that the session exists on the server.
*
* If the session does not exist on the server, the promise is rejected.
*/
export
function connectTo(name: string, options: TerminalSession.IOptions = {}): Promise<TerminalSession.ISession> {
if (!TerminalSession.isAvailable()) {
return Promise.reject(Private.unavailableMsg);
}
let baseUrl = options.baseUrl || utils.getBaseUrl();
let url = Private.getTermUrl(baseUrl, name);
if (url in Private.running) {
return Promise.resolve(Private.running[url]);
}
return listRunning(options).then(models => {
let index = ArrayExt.findFirstIndex(models, model => {
return model.name === name;
});
if (index !== -1) {
let session = new DefaultTerminalSession(name, options);
return Promise.resolve(session);
}
return Promise.reject<TerminalSession.ISession>('Could not find session');
});
}
/**
* List the running terminal sessions.
*
* @param options - The session options to use.
*
* @returns A promise that resolves with the list of running session models.
*/
export
function listRunning(options: TerminalSession.IOptions = {}): Promise<TerminalSession.IModel[]> {
if (!TerminalSession.isAvailable()) {
return Promise.reject(Private.unavailableMsg);
}
let url = Private.getBaseUrl(options.baseUrl);
let ajaxSettings = utils.ajaxSettingsWithToken(options.ajaxSettings, options.token);
ajaxSettings.method = 'GET';
ajaxSettings.dataType = 'json';
return utils.ajaxRequest(url, ajaxSettings).then(success => {
if (success.xhr.status !== 200) {
throw utils.makeAjaxError(success);
}
let data = success.data as TerminalSession.IModel[];
if (!Array.isArray(data)) {
throw utils.makeAjaxError(success, 'Invalid terminal data');
}
// Update the local data store.
let urls = toArray(map(data, item => {
return utils.urlPathJoin(url, item.name);
}));
each(Object.keys(Private.running), runningUrl => {
if (urls.indexOf(runningUrl) === -1) {
let session = Private.running[runningUrl];
session.terminated.emit(void 0);
session.dispose();
}
});
return data;
});
}
/**
* Shut down a terminal session by name.
*
* @param name - The name of the target session.
*
* @param options - The session options to use.
*
* @returns A promise that resolves when the session is shut down.
*/
export
function shutdown(name: string, options: TerminalSession.IOptions = {}): Promise<void> {
if (!TerminalSession.isAvailable()) {
return Promise.reject(Private.unavailableMsg);
}
let url = Private.getTermUrl(options.baseUrl, name);
let ajaxSettings = utils.ajaxSettingsWithToken(options.ajaxSettings, options.token);
ajaxSettings.method = 'DELETE';
return utils.ajaxRequest(url, ajaxSettings).then(success => {
if (success.xhr.status !== 204) {
throw utils.makeAjaxError(success);
}
Private.killTerminal(url);
}, err => {
if (err.xhr.status === 404) {
let response = JSON.parse(err.xhr.responseText) as any;
console.warn(response['message']);
Private.killTerminal(url);
return;
}
return Promise.reject(err);
});
}
}
/**
* A namespace for private data.
*/
namespace Private {
/**
* A mapping of running terminals by url.
*/
export
const running: { [key: string]: DefaultTerminalSession } = Object.create(null);
/**
* A promise returned for when terminals are unavailable.
*/
export
const unavailableMsg = 'Terminals Unavailable';
/**
* Get the url for a terminal.
*/
export
function getTermUrl(baseUrl: string, name: string): string {
return utils.urlPathJoin(baseUrl, TERMINAL_SERVICE_URL, name);
}
/**
* Get the base url.
*/
export
function getBaseUrl(baseUrl: string): string {
return utils.urlPathJoin(baseUrl, TERMINAL_SERVICE_URL);
}
/**
* Kill a terminal by url.
*/
export
function killTerminal(url: string): void {
// Update the local data store.
if (Private.running[url]) {
let session = Private.running[url];
session.terminated.emit(void 0);
session.dispose();
}
}
}