@jupyterlite/contents 0.1.0-beta.0 → 0.1.0-beta.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/lib/contents.d.ts +58 -8
- package/lib/contents.js +145 -92
- package/lib/tokens.d.ts +21 -0
- package/lib/tokens.js +26 -0
- package/package.json +9 -6
package/lib/contents.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { Contents as ServerContents } from '@jupyterlab/services';
|
|
2
|
+
import type localforage from 'localforage';
|
|
2
3
|
import { IContents } from './tokens';
|
|
4
|
+
export declare type IModel = ServerContents.IModel;
|
|
3
5
|
/**
|
|
4
6
|
* A class to handle requests to /api/contents
|
|
5
7
|
*/
|
|
@@ -7,7 +9,35 @@ export declare class Contents implements IContents {
|
|
|
7
9
|
/**
|
|
8
10
|
* Construct a new localForage-powered contents provider
|
|
9
11
|
*/
|
|
10
|
-
constructor(options
|
|
12
|
+
constructor(options: Contents.IOptions);
|
|
13
|
+
/**
|
|
14
|
+
* Finish any initialization after server has started and all extensions are applied.
|
|
15
|
+
*/
|
|
16
|
+
initialize(): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Initialize all storage instances
|
|
19
|
+
*/
|
|
20
|
+
protected initStorage(): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* A promise that resolves once all storage is fully initialized.
|
|
23
|
+
*/
|
|
24
|
+
get ready(): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* A lazy reference to the underlying storage.
|
|
27
|
+
*/
|
|
28
|
+
protected get storage(): Promise<LocalForage>;
|
|
29
|
+
/**
|
|
30
|
+
* A lazy reference to the underlying counters.
|
|
31
|
+
*/
|
|
32
|
+
protected get counters(): Promise<LocalForage>;
|
|
33
|
+
/**
|
|
34
|
+
* A lazy reference to the underlying checkpoints.
|
|
35
|
+
*/
|
|
36
|
+
protected get checkpoints(): Promise<LocalForage>;
|
|
37
|
+
/**
|
|
38
|
+
* Get default options for localForage instances
|
|
39
|
+
*/
|
|
40
|
+
protected get defaultStorageOptions(): LocalForageOptions;
|
|
11
41
|
/**
|
|
12
42
|
* Initialize the default storage for contents.
|
|
13
43
|
*/
|
|
@@ -27,7 +57,7 @@ export declare class Contents implements IContents {
|
|
|
27
57
|
*
|
|
28
58
|
* @returns A promise which resolves with the created file content when the file is created.
|
|
29
59
|
*/
|
|
30
|
-
newUntitled(options?: ServerContents.ICreateOptions): Promise<
|
|
60
|
+
newUntitled(options?: ServerContents.ICreateOptions): Promise<IModel | null>;
|
|
31
61
|
/**
|
|
32
62
|
* Copy a file into a given directory.
|
|
33
63
|
*
|
|
@@ -40,7 +70,7 @@ export declare class Contents implements IContents {
|
|
|
40
70
|
* #### Notes
|
|
41
71
|
* The server will select the name of the copied file.
|
|
42
72
|
*/
|
|
43
|
-
copy(path: string, toDir: string): Promise<
|
|
73
|
+
copy(path: string, toDir: string): Promise<IModel>;
|
|
44
74
|
/**
|
|
45
75
|
* Get a file or directory.
|
|
46
76
|
*
|
|
@@ -49,7 +79,7 @@ export declare class Contents implements IContents {
|
|
|
49
79
|
*
|
|
50
80
|
* @returns A promise which resolves with the file content.
|
|
51
81
|
*/
|
|
52
|
-
get(path: string, options?: ServerContents.IFetchOptions): Promise<
|
|
82
|
+
get(path: string, options?: ServerContents.IFetchOptions): Promise<IModel | null>;
|
|
53
83
|
/**
|
|
54
84
|
* Rename a file or directory.
|
|
55
85
|
*
|
|
@@ -58,7 +88,7 @@ export declare class Contents implements IContents {
|
|
|
58
88
|
*
|
|
59
89
|
* @returns A promise which resolves with the new file content model when the file is renamed.
|
|
60
90
|
*/
|
|
61
|
-
rename(oldLocalPath: string, newLocalPath: string): Promise<
|
|
91
|
+
rename(oldLocalPath: string, newLocalPath: string): Promise<IModel>;
|
|
62
92
|
/**
|
|
63
93
|
* Save a file.
|
|
64
94
|
*
|
|
@@ -67,13 +97,23 @@ export declare class Contents implements IContents {
|
|
|
67
97
|
*
|
|
68
98
|
* @returns A promise which resolves with the file content model when the file is saved.
|
|
69
99
|
*/
|
|
70
|
-
save(path: string, options?: Partial<
|
|
100
|
+
save(path: string, options?: Partial<IModel>): Promise<IModel | null>;
|
|
101
|
+
unescapeContent(content: string): string;
|
|
71
102
|
/**
|
|
72
|
-
* Delete a file.
|
|
103
|
+
* Delete a file from browser storage.
|
|
104
|
+
*
|
|
105
|
+
* Has no effect on server-backed files, which will re-appear with their
|
|
106
|
+
* original timestamp.
|
|
73
107
|
*
|
|
74
108
|
* @param path - The path to the file.
|
|
75
109
|
*/
|
|
76
110
|
delete(path: string): Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* Remove the localForage and checkpoints for a path.
|
|
113
|
+
*
|
|
114
|
+
* @param path - The path to the file
|
|
115
|
+
*/
|
|
116
|
+
protected forgetPath(path: string): Promise<void>;
|
|
77
117
|
/**
|
|
78
118
|
* Create a checkpoint for a file.
|
|
79
119
|
*
|
|
@@ -92,6 +132,7 @@ export declare class Contents implements IContents {
|
|
|
92
132
|
* the file.
|
|
93
133
|
*/
|
|
94
134
|
listCheckpoints(path: string): Promise<ServerContents.ICheckpointModel[]>;
|
|
135
|
+
protected normalizeCheckpoint(model: IModel, id: number): ServerContents.ICheckpointModel;
|
|
95
136
|
/**
|
|
96
137
|
* Restore a file to a known checkpoint state.
|
|
97
138
|
*
|
|
@@ -125,6 +166,10 @@ export declare class Contents implements IContents {
|
|
|
125
166
|
* return `null`.
|
|
126
167
|
*/
|
|
127
168
|
private _getServerContents;
|
|
169
|
+
/**
|
|
170
|
+
* A reducer for turning arbitrary binary into a string
|
|
171
|
+
*/
|
|
172
|
+
protected reduceBytesToString: (data: string, byte: number) => string;
|
|
128
173
|
/**
|
|
129
174
|
* retrieve the contents for this path from `__index__.json` in the appropriate
|
|
130
175
|
* folder.
|
|
@@ -143,9 +188,12 @@ export declare class Contents implements IContents {
|
|
|
143
188
|
private _incrementCounter;
|
|
144
189
|
private _serverContents;
|
|
145
190
|
private _storageName;
|
|
191
|
+
private _storageDrivers;
|
|
192
|
+
private _ready;
|
|
146
193
|
private _storage;
|
|
147
194
|
private _counters;
|
|
148
195
|
private _checkpoints;
|
|
196
|
+
private _localforage;
|
|
149
197
|
}
|
|
150
198
|
/**
|
|
151
199
|
* A namespace for contents information.
|
|
@@ -155,6 +203,8 @@ export declare namespace Contents {
|
|
|
155
203
|
/**
|
|
156
204
|
* The name of the storage instance on e.g. IndexedDB, localStorage
|
|
157
205
|
*/
|
|
158
|
-
|
|
206
|
+
storageName?: string | null;
|
|
207
|
+
storageDrivers?: string[] | null;
|
|
208
|
+
localforage: typeof localforage;
|
|
159
209
|
}
|
|
160
210
|
}
|
package/lib/contents.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
|
|
2
2
|
import { PathExt } from '@jupyterlab/coreutils';
|
|
3
|
-
import
|
|
3
|
+
import { MIME } from './tokens';
|
|
4
|
+
import { PromiseDelegate } from '@lumino/coreutils';
|
|
5
|
+
import mime from 'mime';
|
|
4
6
|
/**
|
|
5
7
|
* The name of the local storage.
|
|
6
8
|
*/
|
|
@@ -9,17 +11,6 @@ const DEFAULT_STORAGE_NAME = 'JupyterLite Storage';
|
|
|
9
11
|
* The number of checkpoints to save.
|
|
10
12
|
*/
|
|
11
13
|
const N_CHECKPOINTS = 5;
|
|
12
|
-
/**
|
|
13
|
-
* A list of mime types of text files
|
|
14
|
-
*/
|
|
15
|
-
const EXTRA_TEXT_MIME_TYPES = new Set([
|
|
16
|
-
'application/javascript',
|
|
17
|
-
'application/json',
|
|
18
|
-
'application/manifest+json',
|
|
19
|
-
'application/x-python-code',
|
|
20
|
-
'application/xml',
|
|
21
|
-
'image/svg+xml',
|
|
22
|
-
]);
|
|
23
14
|
/**
|
|
24
15
|
* A class to handle requests to /api/contents
|
|
25
16
|
*/
|
|
@@ -28,44 +19,98 @@ export class Contents {
|
|
|
28
19
|
* Construct a new localForage-powered contents provider
|
|
29
20
|
*/
|
|
30
21
|
constructor(options) {
|
|
22
|
+
/**
|
|
23
|
+
* A reducer for turning arbitrary binary into a string
|
|
24
|
+
*/
|
|
25
|
+
this.reduceBytesToString = (data, byte) => {
|
|
26
|
+
return data + String.fromCharCode(byte);
|
|
27
|
+
};
|
|
31
28
|
this._serverContents = new Map();
|
|
32
29
|
this._storageName = DEFAULT_STORAGE_NAME;
|
|
33
|
-
this.
|
|
30
|
+
this._storageDrivers = null;
|
|
31
|
+
this._localforage = options.localforage;
|
|
32
|
+
this._storageName = options.storageName || DEFAULT_STORAGE_NAME;
|
|
33
|
+
this._storageDrivers = options.storageDrivers || null;
|
|
34
|
+
this._ready = new PromiseDelegate();
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Finish any initialization after server has started and all extensions are applied.
|
|
38
|
+
*/
|
|
39
|
+
async initialize() {
|
|
40
|
+
await this.initStorage();
|
|
41
|
+
this._ready.resolve(void 0);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Initialize all storage instances
|
|
45
|
+
*/
|
|
46
|
+
async initStorage() {
|
|
34
47
|
this._storage = this.createDefaultStorage();
|
|
35
48
|
this._counters = this.createDefaultCounters();
|
|
36
49
|
this._checkpoints = this.createDefaultCheckpoints();
|
|
37
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* A promise that resolves once all storage is fully initialized.
|
|
53
|
+
*/
|
|
54
|
+
get ready() {
|
|
55
|
+
return this._ready.promise;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A lazy reference to the underlying storage.
|
|
59
|
+
*/
|
|
60
|
+
get storage() {
|
|
61
|
+
return this.ready.then(() => this._storage);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A lazy reference to the underlying counters.
|
|
65
|
+
*/
|
|
66
|
+
get counters() {
|
|
67
|
+
return this.ready.then(() => this._counters);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* A lazy reference to the underlying checkpoints.
|
|
71
|
+
*/
|
|
72
|
+
get checkpoints() {
|
|
73
|
+
return this.ready.then(() => this._checkpoints);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Get default options for localForage instances
|
|
77
|
+
*/
|
|
78
|
+
get defaultStorageOptions() {
|
|
79
|
+
const driver = this._storageDrivers && this._storageDrivers.length ? this._storageDrivers : null;
|
|
80
|
+
return {
|
|
81
|
+
version: 1,
|
|
82
|
+
name: this._storageName,
|
|
83
|
+
...(driver ? { driver } : {}),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
38
86
|
/**
|
|
39
87
|
* Initialize the default storage for contents.
|
|
40
88
|
*/
|
|
41
89
|
createDefaultStorage() {
|
|
42
|
-
return
|
|
43
|
-
name: this._storageName,
|
|
90
|
+
return this._localforage.createInstance({
|
|
44
91
|
description: 'Offline Storage for Notebooks and Files',
|
|
45
92
|
storeName: 'files',
|
|
46
|
-
|
|
93
|
+
...this.defaultStorageOptions,
|
|
47
94
|
});
|
|
48
95
|
}
|
|
49
96
|
/**
|
|
50
97
|
* Initialize the default storage for counting file suffixes.
|
|
51
98
|
*/
|
|
52
99
|
createDefaultCounters() {
|
|
53
|
-
return
|
|
54
|
-
name: this._storageName,
|
|
100
|
+
return this._localforage.createInstance({
|
|
55
101
|
description: 'Store the current file suffix counters',
|
|
56
102
|
storeName: 'counters',
|
|
57
|
-
|
|
103
|
+
...this.defaultStorageOptions,
|
|
58
104
|
});
|
|
59
105
|
}
|
|
60
106
|
/**
|
|
61
107
|
* Create the default checkpoint storage.
|
|
62
108
|
*/
|
|
63
109
|
createDefaultCheckpoints() {
|
|
64
|
-
return
|
|
65
|
-
name: this._storageName,
|
|
110
|
+
return this._localforage.createInstance({
|
|
66
111
|
description: 'Offline Storage for Checkpoints',
|
|
67
112
|
storeName: 'checkpoints',
|
|
68
|
-
|
|
113
|
+
...this.defaultStorageOptions,
|
|
69
114
|
});
|
|
70
115
|
}
|
|
71
116
|
/**
|
|
@@ -112,7 +157,7 @@ export class Contents {
|
|
|
112
157
|
path: `${dirname}${name}`,
|
|
113
158
|
last_modified: created,
|
|
114
159
|
created,
|
|
115
|
-
format: '
|
|
160
|
+
format: 'json',
|
|
116
161
|
mimetype: '',
|
|
117
162
|
content: null,
|
|
118
163
|
size: undefined,
|
|
@@ -132,7 +177,7 @@ export class Contents {
|
|
|
132
177
|
created,
|
|
133
178
|
format: 'text',
|
|
134
179
|
// TODO: handle mimetypes
|
|
135
|
-
mimetype:
|
|
180
|
+
mimetype: MIME.PLAIN_TEXT,
|
|
136
181
|
content: '',
|
|
137
182
|
size: 0,
|
|
138
183
|
writable: true,
|
|
@@ -149,7 +194,7 @@ export class Contents {
|
|
|
149
194
|
last_modified: created,
|
|
150
195
|
created,
|
|
151
196
|
format: 'json',
|
|
152
|
-
mimetype:
|
|
197
|
+
mimetype: MIME.JSON,
|
|
153
198
|
content: Private.EMPTY_NB,
|
|
154
199
|
size: JSON.stringify(Private.EMPTY_NB).length,
|
|
155
200
|
writable: true,
|
|
@@ -159,7 +204,7 @@ export class Contents {
|
|
|
159
204
|
}
|
|
160
205
|
}
|
|
161
206
|
const key = file.path;
|
|
162
|
-
await this.
|
|
207
|
+
await (await this.storage).setItem(key, file);
|
|
163
208
|
return file;
|
|
164
209
|
}
|
|
165
210
|
/**
|
|
@@ -193,7 +238,7 @@ export class Contents {
|
|
|
193
238
|
name,
|
|
194
239
|
path: toPath,
|
|
195
240
|
};
|
|
196
|
-
await this.
|
|
241
|
+
await (await this.storage).setItem(toPath, item);
|
|
197
242
|
return item;
|
|
198
243
|
}
|
|
199
244
|
/**
|
|
@@ -210,7 +255,8 @@ export class Contents {
|
|
|
210
255
|
if (path === '') {
|
|
211
256
|
return await this._getFolder(path);
|
|
212
257
|
}
|
|
213
|
-
const
|
|
258
|
+
const storage = await this.storage;
|
|
259
|
+
const item = await storage.getItem(path);
|
|
214
260
|
const serverItem = await this._getServerContents(path, options);
|
|
215
261
|
const model = (item || serverItem);
|
|
216
262
|
if (!model) {
|
|
@@ -226,8 +272,7 @@ export class Contents {
|
|
|
226
272
|
// for directories, find all files with the path as the prefix
|
|
227
273
|
if (model.type === 'directory') {
|
|
228
274
|
const contentMap = new Map();
|
|
229
|
-
await
|
|
230
|
-
const file = item;
|
|
275
|
+
await storage.iterate((file, key) => {
|
|
231
276
|
// use an additional slash to not include the directory itself
|
|
232
277
|
if (key === `${path}/${file.name}`) {
|
|
233
278
|
contentMap.set(file.name, file);
|
|
@@ -248,7 +293,7 @@ export class Contents {
|
|
|
248
293
|
last_modified: model.last_modified,
|
|
249
294
|
created: model.created,
|
|
250
295
|
format: 'json',
|
|
251
|
-
mimetype:
|
|
296
|
+
mimetype: MIME.JSON,
|
|
252
297
|
content,
|
|
253
298
|
size: undefined,
|
|
254
299
|
writable: true,
|
|
@@ -279,11 +324,12 @@ export class Contents {
|
|
|
279
324
|
path: newLocalPath,
|
|
280
325
|
last_modified: modified,
|
|
281
326
|
};
|
|
282
|
-
await this.
|
|
327
|
+
const storage = await this.storage;
|
|
328
|
+
await storage.setItem(newLocalPath, newFile);
|
|
283
329
|
// remove the old file
|
|
284
|
-
await
|
|
330
|
+
await storage.removeItem(path);
|
|
285
331
|
// remove the corresponding checkpoint
|
|
286
|
-
await this.
|
|
332
|
+
await (await this.checkpoints).removeItem(path);
|
|
287
333
|
// if a directory, recurse through all children
|
|
288
334
|
if (file.type === 'directory') {
|
|
289
335
|
let child;
|
|
@@ -317,40 +363,55 @@ export class Contents {
|
|
|
317
363
|
};
|
|
318
364
|
// process the file if coming from an upload
|
|
319
365
|
const ext = PathExt.extname((_a = options.name) !== null && _a !== void 0 ? _a : '');
|
|
366
|
+
const mimetype = mime.getType(ext) || MIME.OCTET_STREAM;
|
|
320
367
|
if (options.content && options.format === 'base64') {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
368
|
+
if (ext === '.ipynb') {
|
|
369
|
+
item = {
|
|
370
|
+
...item,
|
|
371
|
+
content: JSON.parse(this.unescapeContent(options.content)),
|
|
372
|
+
format: 'json',
|
|
373
|
+
type: 'notebook',
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
else if (MIME.KNOWN_TEXT_TYPES.has(mimetype)) {
|
|
377
|
+
item = {
|
|
378
|
+
...item,
|
|
379
|
+
content: this.unescapeContent(options.content),
|
|
380
|
+
format: 'text',
|
|
381
|
+
type: 'file',
|
|
382
|
+
};
|
|
383
|
+
}
|
|
330
384
|
}
|
|
331
|
-
await this.
|
|
385
|
+
await (await this.storage).setItem(path, item);
|
|
332
386
|
return item;
|
|
333
387
|
}
|
|
388
|
+
unescapeContent(content) {
|
|
389
|
+
return decodeURIComponent(escape(atob(content)));
|
|
390
|
+
}
|
|
334
391
|
/**
|
|
335
|
-
* Delete a file.
|
|
392
|
+
* Delete a file from browser storage.
|
|
393
|
+
*
|
|
394
|
+
* Has no effect on server-backed files, which will re-appear with their
|
|
395
|
+
* original timestamp.
|
|
336
396
|
*
|
|
337
397
|
* @param path - The path to the file.
|
|
338
398
|
*/
|
|
339
399
|
async delete(path) {
|
|
340
400
|
path = decodeURIComponent(path);
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
await
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
401
|
+
const slashed = `${path}/`;
|
|
402
|
+
const toDelete = (await (await this.storage).keys()).filter((key) => key === path || key.startsWith(slashed));
|
|
403
|
+
await Promise.all(toDelete.map(this.forgetPath, this));
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Remove the localForage and checkpoints for a path.
|
|
407
|
+
*
|
|
408
|
+
* @param path - The path to the file
|
|
409
|
+
*/
|
|
410
|
+
async forgetPath(path) {
|
|
411
|
+
await Promise.all([
|
|
412
|
+
(await this.storage).removeItem(path),
|
|
413
|
+
(await this.checkpoints).removeItem(path),
|
|
414
|
+
]);
|
|
354
415
|
}
|
|
355
416
|
/**
|
|
356
417
|
* Create a checkpoint for a file.
|
|
@@ -362,23 +423,21 @@ export class Contents {
|
|
|
362
423
|
*/
|
|
363
424
|
async createCheckpoint(path) {
|
|
364
425
|
var _a;
|
|
426
|
+
const checkpoints = await this.checkpoints;
|
|
365
427
|
path = decodeURIComponent(path);
|
|
366
428
|
const item = await this.get(path, { content: true });
|
|
367
429
|
if (!item) {
|
|
368
430
|
throw Error(`Could not find file with path ${path}`);
|
|
369
431
|
}
|
|
370
|
-
const copies = ((_a = (await
|
|
432
|
+
const copies = ((_a = (await checkpoints.getItem(path))) !== null && _a !== void 0 ? _a : []).filter(Boolean);
|
|
371
433
|
copies.push(item);
|
|
372
434
|
// keep only a certain amount of checkpoints per file
|
|
373
435
|
if (copies.length > N_CHECKPOINTS) {
|
|
374
436
|
copies.splice(0, copies.length - N_CHECKPOINTS);
|
|
375
437
|
}
|
|
376
|
-
await
|
|
438
|
+
await checkpoints.setItem(path, copies);
|
|
377
439
|
const id = `${copies.length - 1}`;
|
|
378
|
-
return {
|
|
379
|
-
id,
|
|
380
|
-
last_modified: item.last_modified,
|
|
381
|
-
};
|
|
440
|
+
return { id, last_modified: item.last_modified };
|
|
382
441
|
}
|
|
383
442
|
/**
|
|
384
443
|
* List available checkpoints for a file.
|
|
@@ -389,16 +448,11 @@ export class Contents {
|
|
|
389
448
|
* the file.
|
|
390
449
|
*/
|
|
391
450
|
async listCheckpoints(path) {
|
|
392
|
-
const copies = ((await this.
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
return {
|
|
398
|
-
id: id.toString(),
|
|
399
|
-
last_modified: file.last_modified,
|
|
400
|
-
};
|
|
401
|
-
});
|
|
451
|
+
const copies = (await (await this.checkpoints).getItem(path)) || [];
|
|
452
|
+
return copies.filter(Boolean).map(this.normalizeCheckpoint, this);
|
|
453
|
+
}
|
|
454
|
+
normalizeCheckpoint(model, id) {
|
|
455
|
+
return { id: id.toString(), last_modified: model.last_modified };
|
|
402
456
|
}
|
|
403
457
|
/**
|
|
404
458
|
* Restore a file to a known checkpoint state.
|
|
@@ -410,11 +464,10 @@ export class Contents {
|
|
|
410
464
|
*/
|
|
411
465
|
async restoreCheckpoint(path, checkpointID) {
|
|
412
466
|
path = decodeURIComponent(path);
|
|
413
|
-
const copies = ((await this.
|
|
414
|
-
[]);
|
|
467
|
+
const copies = ((await (await this.checkpoints).getItem(path)) || []);
|
|
415
468
|
const id = parseInt(checkpointID);
|
|
416
469
|
const item = copies[id];
|
|
417
|
-
await this.
|
|
470
|
+
await (await this.storage).setItem(path, item);
|
|
418
471
|
}
|
|
419
472
|
/**
|
|
420
473
|
* Delete a checkpoint for a file.
|
|
@@ -426,11 +479,10 @@ export class Contents {
|
|
|
426
479
|
*/
|
|
427
480
|
async deleteCheckpoint(path, checkpointID) {
|
|
428
481
|
path = decodeURIComponent(path);
|
|
429
|
-
const copies = ((await this.
|
|
430
|
-
[]);
|
|
482
|
+
const copies = ((await (await this.checkpoints).getItem(path)) || []);
|
|
431
483
|
const id = parseInt(checkpointID);
|
|
432
484
|
copies.splice(id, 1);
|
|
433
|
-
await this.
|
|
485
|
+
await (await this.checkpoints).setItem(path, copies);
|
|
434
486
|
}
|
|
435
487
|
/**
|
|
436
488
|
* retrieve the contents for this path from the union of local storage and
|
|
@@ -442,11 +494,11 @@ export class Contents {
|
|
|
442
494
|
*/
|
|
443
495
|
async _getFolder(path) {
|
|
444
496
|
const content = new Map();
|
|
445
|
-
await this.
|
|
497
|
+
const storage = await this.storage;
|
|
498
|
+
await storage.iterate((file, key) => {
|
|
446
499
|
if (key.includes('/')) {
|
|
447
500
|
return;
|
|
448
501
|
}
|
|
449
|
-
const file = item;
|
|
450
502
|
content.set(file.path, file);
|
|
451
503
|
});
|
|
452
504
|
// layer in contents that don't have local overwrites
|
|
@@ -464,7 +516,7 @@ export class Contents {
|
|
|
464
516
|
last_modified: new Date(0).toISOString(),
|
|
465
517
|
created: new Date(0).toISOString(),
|
|
466
518
|
format: 'json',
|
|
467
|
-
mimetype:
|
|
519
|
+
mimetype: MIME.JSON,
|
|
468
520
|
content: Array.from(content.values()),
|
|
469
521
|
size: undefined,
|
|
470
522
|
writable: true,
|
|
@@ -489,7 +541,7 @@ export class Contents {
|
|
|
489
541
|
last_modified: new Date(0).toISOString(),
|
|
490
542
|
created: new Date(0).toISOString(),
|
|
491
543
|
format: 'text',
|
|
492
|
-
mimetype:
|
|
544
|
+
mimetype: MIME.PLAIN_TEXT,
|
|
493
545
|
type: 'file',
|
|
494
546
|
writable: true,
|
|
495
547
|
content: null,
|
|
@@ -513,24 +565,24 @@ export class Contents {
|
|
|
513
565
|
...model,
|
|
514
566
|
content: await response.json(),
|
|
515
567
|
format: 'json',
|
|
516
|
-
mimetype: model.mimetype ||
|
|
568
|
+
mimetype: model.mimetype || MIME.JSON,
|
|
517
569
|
};
|
|
518
570
|
}
|
|
519
571
|
else if (mimetype.indexOf('text') !== -1 ||
|
|
520
|
-
|
|
572
|
+
MIME.KNOWN_TEXT_TYPES.has(mimetype)) {
|
|
521
573
|
model = {
|
|
522
574
|
...model,
|
|
523
575
|
content: await response.text(),
|
|
524
576
|
format: 'text',
|
|
525
|
-
mimetype: mimetype ||
|
|
577
|
+
mimetype: mimetype || MIME.PLAIN_TEXT,
|
|
526
578
|
};
|
|
527
579
|
}
|
|
528
580
|
else {
|
|
529
581
|
model = {
|
|
530
582
|
...model,
|
|
531
|
-
content: btoa(
|
|
583
|
+
content: btoa(new Uint8Array(await response.arrayBuffer()).reduce(this.reduceBytesToString, '')),
|
|
532
584
|
format: 'base64',
|
|
533
|
-
mimetype: mimetype ||
|
|
585
|
+
mimetype: mimetype || MIME.OCTET_STREAM,
|
|
534
586
|
};
|
|
535
587
|
}
|
|
536
588
|
}
|
|
@@ -557,7 +609,7 @@ export class Contents {
|
|
|
557
609
|
}
|
|
558
610
|
}
|
|
559
611
|
catch (err) {
|
|
560
|
-
console.warn(`don't worry, about ${err}... nothing's broken.
|
|
612
|
+
console.warn(`don't worry, about ${err}... nothing's broken. If there had been a
|
|
561
613
|
file at ${apiURL}, you might see some more files.`);
|
|
562
614
|
}
|
|
563
615
|
this._serverContents.set(path, content);
|
|
@@ -572,9 +624,10 @@ export class Contents {
|
|
|
572
624
|
*/
|
|
573
625
|
async _incrementCounter(type) {
|
|
574
626
|
var _a;
|
|
575
|
-
const
|
|
627
|
+
const counters = await this.counters;
|
|
628
|
+
const current = (_a = (await counters.getItem(type))) !== null && _a !== void 0 ? _a : -1;
|
|
576
629
|
const counter = current + 1;
|
|
577
|
-
await
|
|
630
|
+
await counters.setItem(type, counter);
|
|
578
631
|
return counter;
|
|
579
632
|
}
|
|
580
633
|
}
|
package/lib/tokens.d.ts
CHANGED
|
@@ -8,6 +8,10 @@ export declare const IContents: Token<IContents>;
|
|
|
8
8
|
* The interface for the contents service.
|
|
9
9
|
*/
|
|
10
10
|
export interface IContents {
|
|
11
|
+
/**
|
|
12
|
+
* A promise that resolves after the contents have been full initialized.
|
|
13
|
+
*/
|
|
14
|
+
ready: Promise<void>;
|
|
11
15
|
/**
|
|
12
16
|
* Create a new untitled file or directory in the specified directory path.
|
|
13
17
|
*
|
|
@@ -99,3 +103,20 @@ export interface IContents {
|
|
|
99
103
|
*/
|
|
100
104
|
deleteCheckpoint(path: string, checkpointID: string): Promise<void>;
|
|
101
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Commonly-used mimetypes
|
|
108
|
+
*/
|
|
109
|
+
export declare namespace MIME {
|
|
110
|
+
const JS = "application/javascript";
|
|
111
|
+
const JSON = "application/json";
|
|
112
|
+
const MANIFEST_JSON = "application/manifest+json";
|
|
113
|
+
const PLAIN_TEXT = "text/plain";
|
|
114
|
+
const PYTHON = "application/x-python-code";
|
|
115
|
+
const SVG = "image/svg+xml";
|
|
116
|
+
const XML = "application/xml";
|
|
117
|
+
/**
|
|
118
|
+
* A list of mime types of common text file types
|
|
119
|
+
*/
|
|
120
|
+
const KNOWN_TEXT_TYPES: Set<string>;
|
|
121
|
+
const OCTET_STREAM = "octet/stream";
|
|
122
|
+
}
|
package/lib/tokens.js
CHANGED
|
@@ -3,3 +3,29 @@ import { Token } from '@lumino/coreutils';
|
|
|
3
3
|
* The token for the settings service.
|
|
4
4
|
*/
|
|
5
5
|
export const IContents = new Token('@jupyterlite/contents:IContents');
|
|
6
|
+
/**
|
|
7
|
+
* Commonly-used mimetypes
|
|
8
|
+
*/
|
|
9
|
+
export var MIME;
|
|
10
|
+
(function (MIME) {
|
|
11
|
+
MIME.JS = 'application/javascript';
|
|
12
|
+
MIME.JSON = 'application/json';
|
|
13
|
+
MIME.MANIFEST_JSON = 'application/manifest+json';
|
|
14
|
+
MIME.PLAIN_TEXT = 'text/plain';
|
|
15
|
+
MIME.PYTHON = 'application/x-python-code';
|
|
16
|
+
MIME.SVG = 'image/svg+xml';
|
|
17
|
+
MIME.XML = 'application/xml';
|
|
18
|
+
/**
|
|
19
|
+
* A list of mime types of common text file types
|
|
20
|
+
*/
|
|
21
|
+
MIME.KNOWN_TEXT_TYPES = new Set([
|
|
22
|
+
MIME.JS,
|
|
23
|
+
MIME.JSON,
|
|
24
|
+
MIME.MANIFEST_JSON,
|
|
25
|
+
MIME.PLAIN_TEXT,
|
|
26
|
+
MIME.PYTHON,
|
|
27
|
+
MIME.SVG,
|
|
28
|
+
MIME.XML,
|
|
29
|
+
]);
|
|
30
|
+
MIME.OCTET_STREAM = 'octet/stream';
|
|
31
|
+
})(MIME || (MIME = {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jupyterlite/contents",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.3",
|
|
4
4
|
"description": "JupyterLite - Contents",
|
|
5
5
|
"homepage": "https://github.com/jupyterlite/jupyterlite",
|
|
6
6
|
"bugs": {
|
|
@@ -42,16 +42,19 @@
|
|
|
42
42
|
"watch": "tsc -b --watch"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@jupyterlab/nbformat": "~3.2
|
|
46
|
-
"@jupyterlab/services": "~6.2
|
|
47
|
-
"@
|
|
48
|
-
"
|
|
45
|
+
"@jupyterlab/nbformat": "~3.3.2",
|
|
46
|
+
"@jupyterlab/services": "~6.3.2",
|
|
47
|
+
"@jupyterlite/localforage": "^0.1.0-beta.3",
|
|
48
|
+
"@lumino/coreutils": "^1.12.0",
|
|
49
|
+
"localforage": "^1.9.0",
|
|
50
|
+
"mime": "^3.0.0"
|
|
49
51
|
},
|
|
50
52
|
"devDependencies": {
|
|
51
53
|
"@babel/core": "^7.11.6",
|
|
52
54
|
"@babel/preset-env": "^7.12.1",
|
|
53
|
-
"@jupyterlab/testutils": "~3.2
|
|
55
|
+
"@jupyterlab/testutils": "~3.3.2",
|
|
54
56
|
"@types/jest": "^26.0.10",
|
|
57
|
+
"@types/mime": "^2.0.3",
|
|
55
58
|
"jest": "^26.4.2",
|
|
56
59
|
"rimraf": "~3.0.0",
|
|
57
60
|
"ts-jest": "^26.3.0",
|