terminal.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. IIterator
  5. } from '@phosphor/algorithm';
  6. import {
  7. JSONPrimitive, JSONObject
  8. } from '@phosphor/coreutils';
  9. import {
  10. IDisposable
  11. } from '@phosphor/disposable';
  12. import {
  13. ISignal
  14. } from '@phosphor/signaling';
  15. import {
  16. ServerConnection
  17. } from '..';
  18. import {
  19. DefaultTerminalSession
  20. } from './default';
  21. /**
  22. * The namespace for ISession statics.
  23. */
  24. export
  25. namespace TerminalSession {
  26. /**
  27. * An interface for a terminal session.
  28. */
  29. export
  30. interface ISession extends IDisposable {
  31. /**
  32. * A signal emitted when the session is shut down.
  33. */
  34. terminated: ISignal<ISession, void>;
  35. /**
  36. * A signal emitted when a message is received from the server.
  37. */
  38. messageReceived: ISignal<ISession, IMessage>;
  39. /**
  40. * Get the name of the terminal session.
  41. */
  42. readonly name: string;
  43. /**
  44. * The model associated with the session.
  45. */
  46. readonly model: IModel;
  47. /**
  48. * The server settings for the session.
  49. */
  50. readonly serverSettings: ServerConnection.ISettings;
  51. /**
  52. * Test whether the session is ready.
  53. */
  54. readonly isReady: boolean;
  55. /**
  56. * A promise that fulfills when the session is initially ready.
  57. */
  58. readonly ready: Promise<void>;
  59. /**
  60. * Send a message to the terminal session.
  61. */
  62. send(message: IMessage): void;
  63. /**
  64. * Reconnect to the terminal.
  65. *
  66. * @returns A promise that resolves when the terminal has reconnected.
  67. */
  68. reconnect(): Promise<void>;
  69. /**
  70. * Shut down the terminal session.
  71. */
  72. shutdown(): Promise<void>;
  73. }
  74. /**
  75. * Test whether the terminal service is available.
  76. */
  77. export
  78. function isAvailable(): boolean {
  79. return DefaultTerminalSession.isAvailable();
  80. }
  81. /**
  82. * Start a new terminal session.
  83. *
  84. * @param options - The session options to use.
  85. *
  86. * @returns A promise that resolves with the session instance.
  87. */
  88. export
  89. function startNew(options?: IOptions): Promise<ISession> {
  90. return DefaultTerminalSession.startNew(options);
  91. }
  92. /*
  93. * Connect to a running session.
  94. *
  95. * @param name - The name of the target session.
  96. *
  97. * @param options - The session options to use.
  98. *
  99. * @returns A promise that resolves with the new session instance.
  100. *
  101. * #### Notes
  102. * If the session was already started via `startNew`, the existing
  103. * session object is used as the fulfillment value.
  104. *
  105. * Otherwise, if `options` are given, we resolve the promise after
  106. * confirming that the session exists on the server.
  107. *
  108. * If the session does not exist on the server, the promise is rejected.
  109. */
  110. export
  111. function connectTo(name: string, options?: IOptions): Promise<ISession> {
  112. return DefaultTerminalSession.connectTo(name, options);
  113. }
  114. /**
  115. * List the running terminal sessions.
  116. *
  117. * @param settings - The server settings to use.
  118. *
  119. * @returns A promise that resolves with the list of running session models.
  120. */
  121. export
  122. function listRunning(settings?: ServerConnection.ISettings): Promise<IModel[]> {
  123. return DefaultTerminalSession.listRunning(settings);
  124. }
  125. /**
  126. * Shut down a terminal session by name.
  127. *
  128. * @param name - The name of the target session.
  129. *
  130. * @param settings - The server settings to use.
  131. *
  132. * @returns A promise that resolves when the session is shut down.
  133. */
  134. export
  135. function shutdown(name: string, settings?: ServerConnection.ISettings): Promise<void> {
  136. return DefaultTerminalSession.shutdown(name, settings);
  137. }
  138. /**
  139. * The options for intializing a terminal session object.
  140. */
  141. export
  142. interface IOptions {
  143. /**
  144. * The server settings for the session.
  145. */
  146. serverSettings?: ServerConnection.ISettings;
  147. }
  148. /**
  149. * The server model for a terminal session.
  150. */
  151. export
  152. interface IModel extends JSONObject {
  153. /**
  154. * The name of the terminal session.
  155. */
  156. readonly name: string;
  157. }
  158. /**
  159. * A message from the terminal session.
  160. */
  161. export
  162. interface IMessage {
  163. /**
  164. * The type of the message.
  165. */
  166. readonly type: MessageType;
  167. /**
  168. * The content of the message.
  169. */
  170. readonly content?: JSONPrimitive[];
  171. }
  172. /**
  173. * Valid message types for the terminal.
  174. */
  175. export
  176. type MessageType = 'stdout' | 'disconnect' | 'set_size' | 'stdin';
  177. /**
  178. * The interface for a terminal manager.
  179. *
  180. * #### Notes
  181. * The manager is responsible for maintaining the state of running
  182. * terminal sessions.
  183. */
  184. export
  185. interface IManager extends IDisposable {
  186. /**
  187. * A signal emitted when the running terminals change.
  188. */
  189. runningChanged: ISignal<IManager, IModel[]>;
  190. /**
  191. * The server settings for the manager.
  192. */
  193. readonly serverSettings: ServerConnection.ISettings;
  194. /**
  195. * Test whether the manager is ready.
  196. */
  197. readonly isReady: boolean;
  198. /**
  199. * A promise that fulfills when the manager is ready.
  200. */
  201. readonly ready: Promise<void>;
  202. /**
  203. * Whether the terminal service is available.
  204. */
  205. isAvailable(): boolean;
  206. /**
  207. * Create an iterator over the known running terminals.
  208. *
  209. * @returns A new iterator over the running terminals.
  210. */
  211. running(): IIterator<IModel>;
  212. /**
  213. * Create a new terminal session.
  214. *
  215. * @param options - The options used to create the session.
  216. *
  217. * @returns A promise that resolves with the terminal instance.
  218. *
  219. * #### Notes
  220. * The manager `serverSettings` will be always be used.
  221. */
  222. startNew(options?: IOptions): Promise<ISession>;
  223. /*
  224. * Connect to a running session.
  225. *
  226. * @param name - The name of the target session.
  227. *
  228. * @returns A promise that resolves with the new session instance.
  229. */
  230. connectTo(name: string): Promise<ISession>;
  231. /**
  232. * Shut down a terminal session by name.
  233. *
  234. * @param name - The name of the terminal session.
  235. *
  236. * @returns A promise that resolves when the session is shut down.
  237. */
  238. shutdown(name: string): Promise<void>;
  239. /**
  240. * Force a refresh of the running terminal sessions.
  241. *
  242. * @returns A promise that with the list of running sessions.
  243. *
  244. * #### Notes
  245. * This is not typically meant to be called by the user, since the
  246. * manager maintains its own internal state.
  247. */
  248. refreshRunning(): Promise<void>;
  249. }
  250. }