@jupyterlite/session 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlite/session",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "JupyterLite - Session",
5
5
  "homepage": "https://github.com/jupyterlite/jupyterlite",
6
6
  "bugs": {
@@ -27,7 +27,8 @@
27
27
  "lib/*.js.map",
28
28
  "lib/*.js",
29
29
  "style/*.css",
30
- "style/index.js"
30
+ "style/index.js",
31
+ "src/**/*.{ts,tsx}"
31
32
  ],
32
33
  "scripts": {
33
34
  "build": "tsc -b",
@@ -42,16 +43,16 @@
42
43
  "watch": "tsc -b --watch"
43
44
  },
44
45
  "dependencies": {
45
- "@jupyterlab/coreutils": "~6.0.9",
46
- "@jupyterlab/services": "~7.0.9",
47
- "@jupyterlite/kernel": "^0.2.2",
46
+ "@jupyterlab/coreutils": "~6.0.11",
47
+ "@jupyterlab/services": "~7.0.11",
48
+ "@jupyterlite/kernel": "^0.2.3",
48
49
  "@lumino/algorithm": "^2.0.1",
49
50
  "@lumino/coreutils": "^2.1.2"
50
51
  },
51
52
  "devDependencies": {
52
53
  "@babel/core": "^7.11.6",
53
54
  "@babel/preset-env": "^7.12.1",
54
- "@jupyterlab/testutils": "~4.0.9",
55
+ "@jupyterlab/testutils": "~4.0.11",
55
56
  "@types/jest": "^29.5.3",
56
57
  "jest": "^29.6.2",
57
58
  "rimraf": "~5.0.1",
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ export * from './tokens';
5
+ export * from './sessions';
@@ -0,0 +1,196 @@
1
+ import { Session } from '@jupyterlab/services';
2
+
3
+ import { PathExt } from '@jupyterlab/coreutils';
4
+
5
+ import { IKernels } from '@jupyterlite/kernel';
6
+
7
+ import { ArrayExt } from '@lumino/algorithm';
8
+
9
+ import { UUID } from '@lumino/coreutils';
10
+
11
+ import { ISessions } from './tokens';
12
+
13
+ /**
14
+ * A class to handle requests to /api/sessions
15
+ */
16
+ export class Sessions implements ISessions {
17
+ /**
18
+ * Construct a new Sessions.
19
+ *
20
+ * @param options The instantiation options for a Sessions.
21
+ */
22
+ constructor(options: Sessions.IOptions) {
23
+ this._kernels = options.kernels;
24
+ }
25
+
26
+ /**
27
+ * Get a session by id.
28
+ *
29
+ * @param id The id of the session.
30
+ */
31
+ async get(id: string): Promise<Session.IModel> {
32
+ const session = this._sessions.find((s) => s.id === id);
33
+ if (!session) {
34
+ throw Error(`Session ${id} not found`);
35
+ }
36
+ return session;
37
+ }
38
+
39
+ /**
40
+ * List the running sessions
41
+ */
42
+ async list(): Promise<Session.IModel[]> {
43
+ return this._sessions;
44
+ }
45
+
46
+ /**
47
+ * Path an existing session.
48
+ * This can be used to rename a session.
49
+ *
50
+ * - path updates session to track renamed paths
51
+ * - kernel.name starts a new kernel with a given kernelspec
52
+ *
53
+ * @param options The options to patch the session.
54
+ */
55
+ async patch(options: Session.IModel): Promise<Session.IModel> {
56
+ const { id, path, name, kernel } = options;
57
+ const index = this._sessions.findIndex((s) => s.id === id);
58
+ const session = this._sessions[index];
59
+ if (!session) {
60
+ throw Error(`Session ${id} not found`);
61
+ }
62
+ const patched = {
63
+ ...session,
64
+ path: path ?? session.path,
65
+ name: name ?? session.name,
66
+ };
67
+
68
+ if (kernel) {
69
+ // Kernel id takes precedence over name.
70
+ if (kernel.id) {
71
+ const session = this._sessions.find(
72
+ (session) => session.kernel?.id === kernel?.id,
73
+ );
74
+ if (session) {
75
+ patched.kernel = session.kernel;
76
+ }
77
+ } else if (kernel.name) {
78
+ const newKernel = await this._kernels.startNew({
79
+ id: UUID.uuid4(),
80
+ name: kernel.name,
81
+ location: PathExt.dirname(patched.path),
82
+ });
83
+
84
+ if (newKernel) {
85
+ patched.kernel = newKernel;
86
+ }
87
+
88
+ // clean up the session on kernel shutdown
89
+ void this._handleKernelShutdown({
90
+ kernelId: newKernel.id,
91
+ sessionId: session.id,
92
+ });
93
+ }
94
+ }
95
+
96
+ this._sessions[index] = patched;
97
+ return patched;
98
+ }
99
+
100
+ /**
101
+ * Start a new session
102
+ * TODO: read path and name
103
+ *
104
+ * @param options The options to start a new session.
105
+ */
106
+ async startNew(options: Session.IModel): Promise<Session.IModel> {
107
+ const { path, name } = options;
108
+ const running = this._sessions.find((s) => s.name === name);
109
+ if (running) {
110
+ return running;
111
+ }
112
+ const kernelName = options.kernel?.name ?? '';
113
+ const id = options.id ?? UUID.uuid4();
114
+ const nameOrPath = options.name ?? options.path;
115
+ const dirname = PathExt.dirname(options.name) || PathExt.dirname(options.path);
116
+ const hasDrive = nameOrPath.includes(':');
117
+ const driveName = hasDrive ? nameOrPath.split(':')[0] : '';
118
+ // add drive name if missing (top level directory)
119
+ const location = dirname.includes(driveName) ? dirname : `${driveName}:${dirname}`;
120
+ const kernel = await this._kernels.startNew({
121
+ id,
122
+ name: kernelName,
123
+ location,
124
+ });
125
+ const session: Session.IModel = {
126
+ id,
127
+ path,
128
+ name: name ?? path,
129
+ type: 'notebook',
130
+ kernel: {
131
+ id: kernel.id,
132
+ name: kernel.name,
133
+ },
134
+ };
135
+ this._sessions.push(session);
136
+
137
+ // clean up the session on kernel shutdown
138
+ void this._handleKernelShutdown({ kernelId: id, sessionId: session.id });
139
+
140
+ return session;
141
+ }
142
+
143
+ /**
144
+ * Shut down a session.
145
+ *
146
+ * @param id The id of the session to shut down.
147
+ */
148
+ async shutdown(id: string): Promise<void> {
149
+ const session = this._sessions.find((s) => s.id === id);
150
+ if (!session) {
151
+ throw Error(`Session ${id} not found`);
152
+ }
153
+ const kernelId = session.kernel?.id;
154
+ if (kernelId) {
155
+ await this._kernels.shutdown(kernelId);
156
+ }
157
+ ArrayExt.removeFirstOf(this._sessions, session);
158
+ }
159
+
160
+ /**
161
+ * Handle kernel shutdown
162
+ */
163
+ private async _handleKernelShutdown({
164
+ kernelId,
165
+ sessionId,
166
+ }: {
167
+ kernelId: string;
168
+ sessionId: string;
169
+ }): Promise<void> {
170
+ const runningKernel = await this._kernels.get(kernelId);
171
+ if (runningKernel) {
172
+ runningKernel.disposed.connect(() => {
173
+ this.shutdown(sessionId);
174
+ });
175
+ }
176
+ }
177
+
178
+ private _kernels: IKernels;
179
+ // TODO: offload to a database
180
+ private _sessions: Session.IModel[] = [];
181
+ }
182
+
183
+ /**
184
+ * A namespace for sessions statics.
185
+ */
186
+ export namespace Sessions {
187
+ /**
188
+ * The instantiation options for the sessions.
189
+ */
190
+ export interface IOptions {
191
+ /**
192
+ * A reference to the kernels service.
193
+ */
194
+ kernels: IKernels;
195
+ }
196
+ }
package/src/tokens.ts ADDED
@@ -0,0 +1,47 @@
1
+ import { Session } from '@jupyterlab/services';
2
+
3
+ import { Token } from '@lumino/coreutils';
4
+
5
+ /**
6
+ * The token for the sessions service.
7
+ */
8
+ export const ISessions = new Token<ISessions>('@jupyterlite/session:ISessions');
9
+
10
+ /**
11
+ * The interface for the sessions services.
12
+ */
13
+ export interface ISessions {
14
+ /**
15
+ * Get a session by id.
16
+ *
17
+ * @param id The id of the session.
18
+ */
19
+ get(id: string): Promise<Session.IModel>;
20
+
21
+ /**
22
+ * List the running sessions
23
+ */
24
+ list(): Promise<Session.IModel[]>;
25
+
26
+ /**
27
+ * Path an existing session.
28
+ * This can be used to rename a session.
29
+ *
30
+ * @param options The options to patch the session.
31
+ */
32
+ patch: (options: Session.IModel) => Promise<Session.IModel>;
33
+
34
+ /**
35
+ * Start a new session.
36
+ *
37
+ * @param options The options to start a new session.
38
+ */
39
+ startNew: (options: Session.IModel) => Promise<Session.IModel>;
40
+
41
+ /**
42
+ * Shut down a session.
43
+ *
44
+ * @param id The id of the session to shut down.
45
+ */
46
+ shutdown: (id: string) => Promise<void>;
47
+ }