@oceanum/oceanumlab 4.7.1 → 4.8.0
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/lib/DatameshWidget.d.ts +11 -0
- package/lib/DatameshWidget.js +35 -5
- package/lib/StoredNotebooks.d.ts +27 -5
- package/lib/StoredNotebooks.js +141 -24
- package/lib/auth/serverAuth.js +1 -0
- package/lib/auth/startSignIn.d.ts +11 -0
- package/lib/auth/startSignIn.js +16 -0
- package/lib/auth/tokens.d.ts +13 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.js +43 -7
- package/lib/share/client.d.ts +86 -0
- package/lib/share/client.js +206 -0
- package/lib/share/events.d.ts +7 -0
- package/lib/share/events.js +9 -0
- package/lib/share/notebook.d.ts +216 -0
- package/lib/share/notebook.js +355 -0
- package/lib/share/oceanumOnly.d.ts +72 -0
- package/lib/share/oceanumOnly.js +127 -0
- package/lib/share/plugin.d.ts +23 -0
- package/lib/share/plugin.js +981 -0
- package/lib/share/upload.d.ts +6 -0
- package/lib/share/upload.js +27 -0
- package/lib/share/widgets.d.ts +25 -0
- package/lib/share/widgets.js +154 -0
- package/package.json +5 -1
- package/schema/datamesh-connect.json +6 -0
- package/src/DatameshWidget.tsx +79 -2
- package/src/StoredNotebooks.tsx +274 -24
- package/src/__tests__/OceanumPanel.spec.tsx +159 -5
- package/src/__tests__/StoredNotebooks.spec.tsx +528 -0
- package/src/__tests__/oceanumOnly.spec.ts +317 -0
- package/src/__tests__/settingsSchema.spec.ts +11 -1
- package/src/__tests__/shareClient.spec.ts +328 -0
- package/src/__tests__/shareNotebook.spec.ts +624 -0
- package/src/__tests__/sharePlugin.spec.ts +664 -0
- package/src/auth/serverAuth.ts +1 -0
- package/src/auth/startSignIn.ts +23 -0
- package/src/auth/tokens.ts +13 -0
- package/src/index.ts +43 -7
- package/src/share/client.ts +291 -0
- package/src/share/events.ts +10 -0
- package/src/share/notebook.ts +525 -0
- package/src/share/oceanumOnly.ts +177 -0
- package/src/share/plugin.ts +1220 -0
- package/src/share/upload.ts +27 -0
- package/src/share/widgets.ts +217 -0
- package/style/index.css +212 -18
- package/lib/specStore.d.ts +0 -37
- package/lib/specStore.js +0 -57
- package/src/__tests__/specStore.spec.ts +0 -126
- package/src/specStore.ts +0 -82
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Notebook records in the Oceanum spec store: the link a local notebook keeps to its
|
|
3
|
+
* record, the body that is stored, and the names, paths and addresses around them.
|
|
4
|
+
*
|
|
5
|
+
* Ported from oceanum-notebook's share-oceanum package, which notebook.oceanum.io will
|
|
6
|
+
* drop in favour of this one.
|
|
7
|
+
*/
|
|
8
|
+
import type { ICodeCell, INotebookContent } from '@jupyterlab/nbformat';
|
|
9
|
+
|
|
10
|
+
/** Notebook metadata key linking a local notebook to its spec store record. */
|
|
11
|
+
export const METADATA_KEY = 'oceanum';
|
|
12
|
+
|
|
13
|
+
/** Folder that notebooks opened from Oceanum.io are written to. */
|
|
14
|
+
export const OCEANUM_DIR = 'Oceanum';
|
|
15
|
+
|
|
16
|
+
/** Query parameter carrying a shared notebook id. */
|
|
17
|
+
export const QUERY_PARAM = 'oceanum-notebook';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Largest request body we send. Cloud Run rejects requests over 32 MiB; keep a margin
|
|
21
|
+
* for headers and the server's own encoding.
|
|
22
|
+
*/
|
|
23
|
+
export const MAX_BODY_BYTES = 30 * 1024 * 1024;
|
|
24
|
+
|
|
25
|
+
/** The `oceanum` notebook metadata value. */
|
|
26
|
+
export interface IOceanumLink {
|
|
27
|
+
spec_id: string;
|
|
28
|
+
/** Spec store the id belongs to, so a development id is never PUT to production. */
|
|
29
|
+
specs_url: string;
|
|
30
|
+
description?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A spec store request body for `POST`/`PUT /specs/notebook`. */
|
|
34
|
+
export interface ISpecBody {
|
|
35
|
+
name: string;
|
|
36
|
+
description: string | null;
|
|
37
|
+
spec: INotebookContent;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The fields a `PATCH /specs/notebook/{id}` may change. A field left out keeps its
|
|
42
|
+
* stored value, so a rename need not send the notebook back.
|
|
43
|
+
*
|
|
44
|
+
* The store refuses anything else outright rather than ignoring it, and refuses a patch
|
|
45
|
+
* that carries no field at all, so never send an empty one. `spec` is replaced whole:
|
|
46
|
+
* there is no way to change one key of a stored notebook.
|
|
47
|
+
*/
|
|
48
|
+
export interface ISpecPatch {
|
|
49
|
+
name?: string;
|
|
50
|
+
description?: string | null;
|
|
51
|
+
spec?: INotebookContent;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A spec store list entry (the list omits `spec`). */
|
|
55
|
+
export interface ISpecSummary {
|
|
56
|
+
id: string;
|
|
57
|
+
name: string;
|
|
58
|
+
description: string | null;
|
|
59
|
+
/**
|
|
60
|
+
* When the record was created, or last patched. A `PUT` does not touch it, so for a
|
|
61
|
+
* record nothing has patched this is still a creation time.
|
|
62
|
+
*/
|
|
63
|
+
modified: string;
|
|
64
|
+
/** Null unless the caller created the record: the store hides other creators. */
|
|
65
|
+
creator: string | null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** A full spec store record. */
|
|
69
|
+
export interface ISpecRecord extends ISpecSummary {
|
|
70
|
+
spec: unknown;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export type BuildResult =
|
|
74
|
+
| { ok: true; body: ISpecBody; bytes: number; strippedOutputs: boolean }
|
|
75
|
+
| { ok: false; bytes: number };
|
|
76
|
+
|
|
77
|
+
const UUID_RE =
|
|
78
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
79
|
+
|
|
80
|
+
/** Whether `value` is a UUID, and so safe to put in a spec store URL. */
|
|
81
|
+
export function isSpecId(value: unknown): value is string {
|
|
82
|
+
return typeof value === 'string' && UUID_RE.test(value);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
86
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Read the link from an `oceanum` metadata value, or `null` if it is missing, malformed,
|
|
91
|
+
* or belongs to a different spec store.
|
|
92
|
+
*/
|
|
93
|
+
export function readLink(
|
|
94
|
+
value: unknown,
|
|
95
|
+
specsUrl: string
|
|
96
|
+
): IOceanumLink | null {
|
|
97
|
+
if (
|
|
98
|
+
!isObject(value) ||
|
|
99
|
+
!isSpecId(value.spec_id) ||
|
|
100
|
+
value.specs_url !== specsUrl
|
|
101
|
+
) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
const link: IOceanumLink = { spec_id: value.spec_id, specs_url: specsUrl };
|
|
105
|
+
if (typeof value.description === 'string') {
|
|
106
|
+
link.description = value.description;
|
|
107
|
+
}
|
|
108
|
+
return link;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The notebook name stored on Oceanum.io for a local path. */
|
|
112
|
+
export function nameFromPath(path: string): string {
|
|
113
|
+
const base = path.split('/').pop() ?? '';
|
|
114
|
+
return base.replace(/\.ipynb$/i, '') || 'Untitled';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function byteLength(text: string): number {
|
|
118
|
+
return new TextEncoder().encode(text).length;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function withoutLink(content: INotebookContent): INotebookContent {
|
|
122
|
+
const metadata = { ...content.metadata };
|
|
123
|
+
delete metadata[METADATA_KEY];
|
|
124
|
+
return { ...content, metadata };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The notebook with every cell's `trusted` metadata removed.
|
|
129
|
+
*
|
|
130
|
+
* JupyterLab trusts a cell whose metadata says `trusted: true` and then renders its HTML
|
|
131
|
+
* and Markdown unsanitised, running any script in them. A notebook from the spec store
|
|
132
|
+
* (anyone with write access can edit a record) must arrive untrusted; its outputs
|
|
133
|
+
* become trusted again when the reader re-runs the cells.
|
|
134
|
+
*/
|
|
135
|
+
export function withoutTrust(content: INotebookContent): INotebookContent {
|
|
136
|
+
const cells = content.cells.map(cell => {
|
|
137
|
+
if (!isObject(cell.metadata) || !('trusted' in cell.metadata)) {
|
|
138
|
+
return cell;
|
|
139
|
+
}
|
|
140
|
+
const metadata = { ...cell.metadata };
|
|
141
|
+
delete metadata.trusted;
|
|
142
|
+
return { ...cell, metadata };
|
|
143
|
+
});
|
|
144
|
+
return { ...content, cells };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function withoutOutputs(content: INotebookContent): INotebookContent {
|
|
148
|
+
const cells = content.cells.map(cell =>
|
|
149
|
+
cell.cell_type === 'code'
|
|
150
|
+
? ({ ...cell, outputs: [], execution_count: null } as ICodeCell)
|
|
151
|
+
: cell
|
|
152
|
+
);
|
|
153
|
+
return { ...content, cells };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Build the spec store body for a notebook. The `oceanum` link is not stored in the
|
|
158
|
+
* record. If the body is over `maxBytes`, code-cell outputs and execution counts are
|
|
159
|
+
* dropped; if it is still too large, the result is not ok.
|
|
160
|
+
*/
|
|
161
|
+
export function buildSpecBody(
|
|
162
|
+
content: INotebookContent,
|
|
163
|
+
path: string,
|
|
164
|
+
specsUrl: string,
|
|
165
|
+
maxBytes: number = MAX_BODY_BYTES
|
|
166
|
+
): BuildResult {
|
|
167
|
+
const link = readLink(content.metadata?.[METADATA_KEY], specsUrl);
|
|
168
|
+
const make = (spec: INotebookContent): ISpecBody => ({
|
|
169
|
+
name: nameFromPath(path),
|
|
170
|
+
description: link?.description ?? null,
|
|
171
|
+
spec
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
const full = make(withoutTrust(withoutLink(content)));
|
|
175
|
+
const fullBytes = byteLength(JSON.stringify(full));
|
|
176
|
+
if (fullBytes <= maxBytes) {
|
|
177
|
+
return { ok: true, body: full, bytes: fullBytes, strippedOutputs: false };
|
|
178
|
+
}
|
|
179
|
+
const stripped = make(withoutOutputs(full.spec));
|
|
180
|
+
const strippedBytes = byteLength(JSON.stringify(stripped));
|
|
181
|
+
if (strippedBytes <= maxBytes) {
|
|
182
|
+
return {
|
|
183
|
+
ok: true,
|
|
184
|
+
body: stripped,
|
|
185
|
+
bytes: strippedBytes,
|
|
186
|
+
strippedOutputs: true
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
return { ok: false, bytes: strippedBytes };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The notebook stored in a record, untrusted. Throws if the record does not hold an
|
|
194
|
+
* nbformat 4 notebook.
|
|
195
|
+
*/
|
|
196
|
+
function recordNotebook(record: ISpecRecord): INotebookContent {
|
|
197
|
+
const spec = record.spec;
|
|
198
|
+
if (!isNotebook(spec)) {
|
|
199
|
+
throw new Error('This Oceanum.io record is not a valid notebook.');
|
|
200
|
+
}
|
|
201
|
+
return withoutTrust(spec);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function isNotebook(value: unknown): value is INotebookContent {
|
|
205
|
+
return (
|
|
206
|
+
isObject(value) &&
|
|
207
|
+
value.nbformat === 4 &&
|
|
208
|
+
Array.isArray(value.cells) &&
|
|
209
|
+
value.cells.every(
|
|
210
|
+
cell => isObject(cell) && typeof cell.cell_type === 'string'
|
|
211
|
+
) &&
|
|
212
|
+
isObject(value.metadata)
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* A notebook uploaded from the user's computer, untrusted and linked to nothing: a file
|
|
218
|
+
* someone else downloaded from a record must not save over that record, and its outputs
|
|
219
|
+
* must not run scripts on the origin that holds the Oceanum.io session. Throws if the
|
|
220
|
+
* text is not an nbformat 4 notebook; the message never quotes the file.
|
|
221
|
+
*/
|
|
222
|
+
export function notebookFromUpload(text: string): INotebookContent {
|
|
223
|
+
let parsed: unknown;
|
|
224
|
+
try {
|
|
225
|
+
parsed = JSON.parse(text);
|
|
226
|
+
} catch {
|
|
227
|
+
throw new Error('The file is not a Jupyter notebook: it is not JSON.');
|
|
228
|
+
}
|
|
229
|
+
if (!isNotebook(parsed)) {
|
|
230
|
+
throw new Error('The file is not a Jupyter notebook in nbformat 4.');
|
|
231
|
+
}
|
|
232
|
+
return withoutLink(withoutTrust(parsed));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* The notebook stored in a record, with its `oceanum` link set. Throws if the record
|
|
237
|
+
* does not hold an nbformat 4 notebook.
|
|
238
|
+
*/
|
|
239
|
+
export function notebookFromRecord(
|
|
240
|
+
record: ISpecRecord,
|
|
241
|
+
specsUrl: string
|
|
242
|
+
): INotebookContent {
|
|
243
|
+
const link: IOceanumLink = { spec_id: record.id, specs_url: specsUrl };
|
|
244
|
+
if (record.description) {
|
|
245
|
+
link.description = record.description;
|
|
246
|
+
}
|
|
247
|
+
const content = recordNotebook(record);
|
|
248
|
+
return {
|
|
249
|
+
...content,
|
|
250
|
+
metadata: { ...content.metadata, [METADATA_KEY]: { ...link } }
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* A copy of the notebook stored in a record, linked to nothing: untrusted, and without
|
|
256
|
+
* any `oceanum` link, so saving it creates a record of the user's own instead of
|
|
257
|
+
* writing to this one. Throws if the record does not hold an nbformat 4 notebook.
|
|
258
|
+
*/
|
|
259
|
+
export function unlinkedNotebookFromRecord(
|
|
260
|
+
record: ISpecRecord
|
|
261
|
+
): INotebookContent {
|
|
262
|
+
return withoutLink(recordNotebook(record));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Make a record name safe to use as a single path segment. */
|
|
266
|
+
export function sanitizeName(name: string): string {
|
|
267
|
+
const clean = name
|
|
268
|
+
.trim()
|
|
269
|
+
.replace(/\.ipynb$/i, '')
|
|
270
|
+
.replace(/[/\\]/g, '-')
|
|
271
|
+
.trim();
|
|
272
|
+
return clean === '' || clean === '.' || clean === '..' ? 'Untitled' : clean;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* A path `Oceanum/<name>.ipynb` that does not collide with `existing` (file names in the
|
|
277
|
+
* `Oceanum` folder), appending ` (1)`, ` (2)`, ... as needed.
|
|
278
|
+
*/
|
|
279
|
+
export function uniqueNotebookPath(
|
|
280
|
+
name: string,
|
|
281
|
+
existing: Iterable<string>
|
|
282
|
+
): string {
|
|
283
|
+
const taken = new Set(existing);
|
|
284
|
+
const base = sanitizeName(name);
|
|
285
|
+
let candidate = `${base}.ipynb`;
|
|
286
|
+
for (let i = 1; taken.has(candidate); i++) {
|
|
287
|
+
candidate = `${base} (${i}).ipynb`;
|
|
288
|
+
}
|
|
289
|
+
return `${OCEANUM_DIR}/${candidate}`;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* The names in `existing` that `uniqueNotebookPath` could have given a record called
|
|
294
|
+
* `name`, in the order it would have tried them: `<base>.ipynb`, then `<base> (1).ipynb`,
|
|
295
|
+
* `<base> (2).ipynb` and so on.
|
|
296
|
+
*
|
|
297
|
+
* Opening a record looks through these for the copy a previous session left, so the same
|
|
298
|
+
* record keeps one local file. Without that the second copy is named `<base> (1)`, and
|
|
299
|
+
* since a record takes its name from the file on every save, opening a notebook twice
|
|
300
|
+
* would quietly rename it on Oceanum.
|
|
301
|
+
*/
|
|
302
|
+
export function notebookNamesFor(
|
|
303
|
+
name: string,
|
|
304
|
+
existing: Iterable<string>
|
|
305
|
+
): string[] {
|
|
306
|
+
const base = sanitizeName(name);
|
|
307
|
+
const prefix = `${base} (`;
|
|
308
|
+
const suffix = ').ipynb';
|
|
309
|
+
const plain: string[] = [];
|
|
310
|
+
const numbered: { name: string; index: number }[] = [];
|
|
311
|
+
for (const candidate of existing) {
|
|
312
|
+
if (candidate === `${base}.ipynb`) {
|
|
313
|
+
plain.push(candidate);
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
if (!candidate.startsWith(prefix) || !candidate.endsWith(suffix)) {
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
const index = candidate.slice(
|
|
320
|
+
prefix.length,
|
|
321
|
+
candidate.length - suffix.length
|
|
322
|
+
);
|
|
323
|
+
if (/^\d+$/.test(index)) {
|
|
324
|
+
numbered.push({ name: candidate, index: Number(index) });
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
numbered.sort((a, b) => a.index - b.index);
|
|
328
|
+
return [...plain, ...numbered.map(entry => entry.name)];
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Split a listing into the caller's own notebooks and the rest, newest first.
|
|
333
|
+
*
|
|
334
|
+
* The store only sends `creator` on records the caller created, so anything else it
|
|
335
|
+
* returns is shared with them — directly, or because it is public. Addresses are
|
|
336
|
+
* compared case-insensitively: the store matches entities as case-sensitive globs, so
|
|
337
|
+
* the two sides can differ in case.
|
|
338
|
+
*/
|
|
339
|
+
export function partitionSummaries(
|
|
340
|
+
items: readonly ISpecSummary[],
|
|
341
|
+
email: string | null
|
|
342
|
+
): { mine: ISpecSummary[]; shared: ISpecSummary[] } {
|
|
343
|
+
const me = email?.toLowerCase() ?? null;
|
|
344
|
+
const sorted = [...items].sort((a, b) =>
|
|
345
|
+
b.modified.localeCompare(a.modified)
|
|
346
|
+
);
|
|
347
|
+
const isMine = (item: ISpecSummary): boolean =>
|
|
348
|
+
me !== null && item.creator?.toLowerCase() === me;
|
|
349
|
+
return {
|
|
350
|
+
mine: sorted.filter(isMine),
|
|
351
|
+
shared: sorted.filter(item => !isMine(item))
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** The `kind` of a Notebook Demo record's spec. */
|
|
356
|
+
export const NOTEBOOK_DEMO_KIND = 'notebook-demo';
|
|
357
|
+
|
|
358
|
+
/** One example in a Notebook Demo record: a notebook record and how to list it. */
|
|
359
|
+
export interface INotebookDemoItem {
|
|
360
|
+
id: string;
|
|
361
|
+
title: string;
|
|
362
|
+
summary?: string;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** A titled group of examples. */
|
|
366
|
+
export interface INotebookDemoSection {
|
|
367
|
+
title: string;
|
|
368
|
+
items: INotebookDemoItem[];
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function nonBlank(value: unknown): value is string {
|
|
372
|
+
return typeof value === 'string' && value.trim() !== '';
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* The sections of a Notebook Demo record's spec, keeping only what can be listed: items
|
|
377
|
+
* with a record id and a title, in sections with a title and at least one such item.
|
|
378
|
+
* A spec of another kind or version gives no sections, with a warning.
|
|
379
|
+
*/
|
|
380
|
+
export function parseNotebookDemo(spec: unknown): INotebookDemoSection[] {
|
|
381
|
+
if (
|
|
382
|
+
!isObject(spec) ||
|
|
383
|
+
spec.kind !== NOTEBOOK_DEMO_KIND ||
|
|
384
|
+
spec.version !== 1 ||
|
|
385
|
+
!Array.isArray(spec.sections)
|
|
386
|
+
) {
|
|
387
|
+
console.warn(
|
|
388
|
+
'Oceanum: ignoring a Notebook Demo record this version cannot read.'
|
|
389
|
+
);
|
|
390
|
+
return [];
|
|
391
|
+
}
|
|
392
|
+
const sections: INotebookDemoSection[] = [];
|
|
393
|
+
for (const section of spec.sections) {
|
|
394
|
+
if (
|
|
395
|
+
!isObject(section) ||
|
|
396
|
+
!nonBlank(section.title) ||
|
|
397
|
+
!Array.isArray(section.items)
|
|
398
|
+
) {
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
const items: INotebookDemoItem[] = [];
|
|
402
|
+
for (const item of section.items) {
|
|
403
|
+
if (!isObject(item) || !isSpecId(item.id) || !nonBlank(item.title)) {
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
items.push(
|
|
407
|
+
typeof item.summary === 'string'
|
|
408
|
+
? { id: item.id, title: item.title, summary: item.summary }
|
|
409
|
+
: { id: item.id, title: item.title }
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
if (items.length > 0) {
|
|
413
|
+
sections.push({ title: section.title, items });
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return sections;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** Parse a spec store timestamp, which is UTC but usually has no zone designator. */
|
|
420
|
+
export function parseTimestamp(value: string): Date | null {
|
|
421
|
+
const zoned = /(Z|[+-]\d{2}:?\d{2})$/i.test(value) ? value : `${value}Z`;
|
|
422
|
+
const date = new Date(zoned);
|
|
423
|
+
return Number.isNaN(date.getTime()) ? null : date;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Whether `email` can be granted access. Glob characters are refused: the server
|
|
428
|
+
* matches user entities as globs.
|
|
429
|
+
*/
|
|
430
|
+
export function isShareableEmail(email: string): boolean {
|
|
431
|
+
return /^[^\s@*?[\]]+@[^\s@*?[\]]+\.[^\s@*?[\]]+$/.test(email);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** Addresses typed into the share dialog, split into the ones we can grant and the rest. */
|
|
435
|
+
export interface IParsedShareEmails {
|
|
436
|
+
/** Valid, lower-cased and de-duplicated, in the order first written. */
|
|
437
|
+
emails: string[];
|
|
438
|
+
/** Everything that is not a shareable address, as written, for reporting back. */
|
|
439
|
+
invalid: string[];
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Split what the user typed into individual addresses. Commas, semicolons and any
|
|
444
|
+
* whitespace all separate, so one-per-line and a comma-separated list both work.
|
|
445
|
+
*
|
|
446
|
+
* Bare addresses only: a mail client's "To:" line carries display names, and
|
|
447
|
+
* `"Alice B" <alice@example.com>` splits into tokens that are not addresses. Those
|
|
448
|
+
* come back in `invalid` so the user is told, rather than being silently dropped.
|
|
449
|
+
*
|
|
450
|
+
* Addresses are lower-cased because the spec store matches the user entity as a
|
|
451
|
+
* case-sensitive glob against the Auth0 email claim, which Auth0 stores lower case.
|
|
452
|
+
*/
|
|
453
|
+
export function parseShareEmails(raw: string): IParsedShareEmails {
|
|
454
|
+
const emails: string[] = [];
|
|
455
|
+
const invalid: string[] = [];
|
|
456
|
+
const seen = new Set<string>();
|
|
457
|
+
for (const part of raw.split(/[\s,;]+/)) {
|
|
458
|
+
const entry = part.trim();
|
|
459
|
+
if (entry === '') {
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
const email = entry.toLowerCase();
|
|
463
|
+
if (!isShareableEmail(email)) {
|
|
464
|
+
// Report it as written, so the user recognises what they typed.
|
|
465
|
+
invalid.push(entry);
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
if (!seen.has(email)) {
|
|
469
|
+
seen.add(email);
|
|
470
|
+
emails.push(email);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return { emails, invalid };
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* How to report share failures.
|
|
478
|
+
*
|
|
479
|
+
* `original` means hand the underlying error to the reporter untouched — it holds the
|
|
480
|
+
* only explanation the user gets, and the reporter reads its type to decide whether to
|
|
481
|
+
* offer sign-in. `partial` is only for the case the original error cannot describe on
|
|
482
|
+
* its own: some addresses worked and some did not, so the message has to name them.
|
|
483
|
+
*/
|
|
484
|
+
export type ShareReport =
|
|
485
|
+
{ kind: 'original' } | { kind: 'partial'; named: string[] };
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Decide how to report a set of failed grants.
|
|
489
|
+
*
|
|
490
|
+
* `entities` are the failed grants' entities, in order; the public grant's entity is
|
|
491
|
+
* the empty string, which names nobody.
|
|
492
|
+
*/
|
|
493
|
+
export function reportForShareFailures(
|
|
494
|
+
grantCount: number,
|
|
495
|
+
entities: readonly string[],
|
|
496
|
+
needsSignIn: boolean
|
|
497
|
+
): ShareReport {
|
|
498
|
+
const named = entities.filter(entity => entity !== '');
|
|
499
|
+
// A sign-in problem must reach the reporter as the original error or the user is
|
|
500
|
+
// never offered a way back in; an unnamed or total failure has nothing extra worth
|
|
501
|
+
// saying.
|
|
502
|
+
if (needsSignIn || named.length === 0 || entities.length >= grantCount) {
|
|
503
|
+
return { kind: 'original' };
|
|
504
|
+
}
|
|
505
|
+
return { kind: 'partial', named };
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* The link that opens a shared notebook in this deployment.
|
|
510
|
+
*
|
|
511
|
+
* JupyterLite is static files, so its page is `lab/index.html`; a JupyterLab server
|
|
512
|
+
* serves the application at `lab` and has no `index.html` to route.
|
|
513
|
+
*/
|
|
514
|
+
export function shareLink(
|
|
515
|
+
baseUrl: string,
|
|
516
|
+
pageHref: string,
|
|
517
|
+
id: string
|
|
518
|
+
): string {
|
|
519
|
+
const app = new URL(pageHref).pathname.endsWith('/index.html')
|
|
520
|
+
? 'lab/index.html'
|
|
521
|
+
: 'lab';
|
|
522
|
+
const url = new URL(app, new URL(baseUrl, pageHref));
|
|
523
|
+
url.searchParams.set(QUERY_PARAM, id);
|
|
524
|
+
return url.toString();
|
|
525
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Oceanum-only file management: what a deployment gets when its environment says
|
|
3
|
+
* `fileManagement: 'oceanum'`, as notebook.oceanum.io does.
|
|
4
|
+
*
|
|
5
|
+
* The local drive stays as the working copy underneath, out of sight: the file browser
|
|
6
|
+
* is taken out of the sidebar, the File menu loses the entries that manage local files,
|
|
7
|
+
* and every completed local save is pushed to the spec store, so Ctrl+S, the toolbar
|
|
8
|
+
* button, Save All and autosave all reach Oceanum.
|
|
9
|
+
*/
|
|
10
|
+
import type { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
|
|
11
|
+
import { MessageLoop } from '@lumino/messaging';
|
|
12
|
+
import type { Menu, Widget } from '@lumino/widgets';
|
|
13
|
+
|
|
14
|
+
/** The widget id `@jupyterlab/filebrowser-extension:browser` gives the file browser. */
|
|
15
|
+
export const FILE_BROWSER_ID = 'filebrowser';
|
|
16
|
+
|
|
17
|
+
/** The widget id of this extension's Oceanum.io panel, which takes the browser's place. */
|
|
18
|
+
export const OCEANUM_PANEL_ID = 'datamesh-connect';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* File menu entries that manage local files. Save, Save All and Rename stay: saving is
|
|
22
|
+
* routed to Oceanum, and renaming the local file is how a notebook gets its name.
|
|
23
|
+
*/
|
|
24
|
+
export const LOCAL_FILE_COMMANDS: readonly string[] = [
|
|
25
|
+
'filebrowser:open-path',
|
|
26
|
+
'filebrowser:open-url',
|
|
27
|
+
'docmanager:save-as',
|
|
28
|
+
'docmanager:reload',
|
|
29
|
+
'docmanager:restore-checkpoint',
|
|
30
|
+
'docmanager:duplicate',
|
|
31
|
+
'docmanager:download'
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* File > New entries for files that are not notebooks. The spec store holds notebooks
|
|
36
|
+
* only, so such a file could never be saved anywhere the user can see.
|
|
37
|
+
*/
|
|
38
|
+
export const NON_NOTEBOOK_NEW_COMMANDS: readonly string[] = [
|
|
39
|
+
'fileeditor:create-new',
|
|
40
|
+
'fileeditor:create-new-markdown-file'
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Take the file browser out of the sidebar; true if it was there. It is closed, not
|
|
45
|
+
* disposed: other extensions hold it through IDefaultFileBrowser and still use it to
|
|
46
|
+
* open paths. If it was the open panel, `instead` is opened in its place; a sidebar the
|
|
47
|
+
* user had collapsed stays collapsed.
|
|
48
|
+
*/
|
|
49
|
+
export function hideFileBrowser(
|
|
50
|
+
shell: {
|
|
51
|
+
widgets(area: 'left'): Iterable<Widget>;
|
|
52
|
+
activateById(id: string): void;
|
|
53
|
+
},
|
|
54
|
+
instead?: string
|
|
55
|
+
): boolean {
|
|
56
|
+
for (const widget of shell.widgets('left')) {
|
|
57
|
+
if (widget.id === FILE_BROWSER_ID) {
|
|
58
|
+
const wasOpen = widget.isVisible;
|
|
59
|
+
widget.close();
|
|
60
|
+
if (wasOpen && instead) {
|
|
61
|
+
shell.activateById(instead);
|
|
62
|
+
}
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Put a left-sidebar widget first, and keep it open if it was. True if it moved.
|
|
71
|
+
*
|
|
72
|
+
* A rank alone is not enough: the layout restorer replays the sidebar order each user
|
|
73
|
+
* last had, so a returning user would keep the panel wherever it used to be. Adding it
|
|
74
|
+
* again after the restore re-inserts it by rank, and the shell saves the new order.
|
|
75
|
+
*/
|
|
76
|
+
export function moveToTop(
|
|
77
|
+
shell: {
|
|
78
|
+
widgets(area: 'left'): Iterable<Widget>;
|
|
79
|
+
add(widget: Widget, area: 'left', options: { rank: number }): void;
|
|
80
|
+
activateById(id: string): void;
|
|
81
|
+
},
|
|
82
|
+
id: string
|
|
83
|
+
): boolean {
|
|
84
|
+
const widgets = Array.from(shell.widgets('left'));
|
|
85
|
+
const index = widgets.findIndex(widget => widget.id === id);
|
|
86
|
+
if (index <= 0) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
const widget = widgets[index];
|
|
90
|
+
// Adding hides the widget, so an open panel has to be opened again.
|
|
91
|
+
const wasOpen = widget.isVisible;
|
|
92
|
+
shell.add(widget, 'left', { rank: 0 });
|
|
93
|
+
if (wasOpen) {
|
|
94
|
+
shell.activateById(id);
|
|
95
|
+
}
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Submenus of local files. "Open Recent" is built by the document manager rather than
|
|
101
|
+
* declared in a schema, and its menu has no id, so it is known by the commands it holds.
|
|
102
|
+
*/
|
|
103
|
+
export const LOCAL_FILE_SUBMENU_COMMANDS: readonly string[] = [
|
|
104
|
+
'docmanager:open-recent',
|
|
105
|
+
'docmanager:clear-recents'
|
|
106
|
+
];
|
|
107
|
+
|
|
108
|
+
function isLocalSubmenu(item: Menu.IItem): boolean {
|
|
109
|
+
return (
|
|
110
|
+
item.type === 'submenu' &&
|
|
111
|
+
!!item.submenu &&
|
|
112
|
+
(item.submenu.title.label === 'Open Recent' ||
|
|
113
|
+
item.submenu.items.some(child =>
|
|
114
|
+
LOCAL_FILE_SUBMENU_COMMANDS.includes(child.command)
|
|
115
|
+
))
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Remove the local-file entries from a menu; returns how many went. Idempotent, so it
|
|
121
|
+
* can run every time the menu is about to show: entries arrive from settings schemas as
|
|
122
|
+
* plugins load, not all at once.
|
|
123
|
+
*/
|
|
124
|
+
export function pruneFileMenu(
|
|
125
|
+
menu: Menu,
|
|
126
|
+
commands: readonly string[] = LOCAL_FILE_COMMANDS
|
|
127
|
+
): number {
|
|
128
|
+
let removed = 0;
|
|
129
|
+
for (let i = menu.items.length - 1; i >= 0; i--) {
|
|
130
|
+
const item = menu.items[i];
|
|
131
|
+
if (
|
|
132
|
+
(item.type === 'command' && commands.includes(item.command)) ||
|
|
133
|
+
isLocalSubmenu(item)
|
|
134
|
+
) {
|
|
135
|
+
menu.removeItemAt(i);
|
|
136
|
+
removed++;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return removed;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Prune the menu again every time it opens. Entries arrive from settings schemas as
|
|
144
|
+
* plugins load rather than all at once, and a menu has no "about to show" signal; it
|
|
145
|
+
* is attached to the document each time it opens, which a message hook can watch.
|
|
146
|
+
*/
|
|
147
|
+
export function pruneBeforeEachOpen(
|
|
148
|
+
menu: Menu,
|
|
149
|
+
commands: readonly string[] = LOCAL_FILE_COMMANDS
|
|
150
|
+
): void {
|
|
151
|
+
MessageLoop.installMessageHook(menu, (_, message) => {
|
|
152
|
+
if (message.type === 'before-attach') {
|
|
153
|
+
pruneFileMenu(menu, commands);
|
|
154
|
+
}
|
|
155
|
+
return true;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Call `push` after every completed local save of every notebook, open now or later.
|
|
161
|
+
* `push` must guard against its own saves: writing the record link back to the notebook
|
|
162
|
+
* saves it again.
|
|
163
|
+
*/
|
|
164
|
+
export function routeSaves(
|
|
165
|
+
tracker: INotebookTracker,
|
|
166
|
+
push: (panel: NotebookPanel) => Promise<void>
|
|
167
|
+
): void {
|
|
168
|
+
const watch = (panel: NotebookPanel): void => {
|
|
169
|
+
panel.context.saveState.connect((_, state) => {
|
|
170
|
+
if (state === 'completed') {
|
|
171
|
+
void push(panel);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
tracker.forEach(watch);
|
|
176
|
+
tracker.widgetAdded.connect((_, panel) => watch(panel));
|
|
177
|
+
}
|