@jupyterlite/contents 0.1.0-beta.2 → 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 CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Contents as ServerContents } from '@jupyterlab/services';
2
+ import type localforage from 'localforage';
2
3
  import { IContents } from './tokens';
3
4
  export declare type IModel = ServerContents.IModel;
4
5
  /**
@@ -8,7 +9,35 @@ export declare class Contents implements IContents {
8
9
  /**
9
10
  * Construct a new localForage-powered contents provider
10
11
  */
11
- constructor(options?: Contents.IOptions);
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;
12
41
  /**
13
42
  * Initialize the default storage for contents.
14
43
  */
@@ -69,6 +98,7 @@ export declare class Contents implements IContents {
69
98
  * @returns A promise which resolves with the file content model when the file is saved.
70
99
  */
71
100
  save(path: string, options?: Partial<IModel>): Promise<IModel | null>;
101
+ unescapeContent(content: string): string;
72
102
  /**
73
103
  * Delete a file from browser storage.
74
104
  *
@@ -158,9 +188,12 @@ export declare class Contents implements IContents {
158
188
  private _incrementCounter;
159
189
  private _serverContents;
160
190
  private _storageName;
191
+ private _storageDrivers;
192
+ private _ready;
161
193
  private _storage;
162
194
  private _counters;
163
195
  private _checkpoints;
196
+ private _localforage;
164
197
  }
165
198
  /**
166
199
  * A namespace for contents information.
@@ -170,6 +203,8 @@ export declare namespace Contents {
170
203
  /**
171
204
  * The name of the storage instance on e.g. IndexedDB, localStorage
172
205
  */
173
- contentsStorageName: string;
206
+ storageName?: string | null;
207
+ storageDrivers?: string[] | null;
208
+ localforage: typeof localforage;
174
209
  }
175
210
  }
package/lib/contents.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { PageConfig, URLExt } from '@jupyterlab/coreutils';
2
2
  import { PathExt } from '@jupyterlab/coreutils';
3
- import localforage from 'localforage';
4
3
  import { MIME } from './tokens';
4
+ import { PromiseDelegate } from '@lumino/coreutils';
5
+ import mime from 'mime';
5
6
  /**
6
7
  * The name of the local storage.
7
8
  */
@@ -26,42 +27,90 @@ export class Contents {
26
27
  };
27
28
  this._serverContents = new Map();
28
29
  this._storageName = DEFAULT_STORAGE_NAME;
29
- this._storageName = (options || {}).contentsStorageName || DEFAULT_STORAGE_NAME;
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() {
30
47
  this._storage = this.createDefaultStorage();
31
48
  this._counters = this.createDefaultCounters();
32
49
  this._checkpoints = this.createDefaultCheckpoints();
33
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
+ }
34
86
  /**
35
87
  * Initialize the default storage for contents.
36
88
  */
37
89
  createDefaultStorage() {
38
- return localforage.createInstance({
39
- name: this._storageName,
90
+ return this._localforage.createInstance({
40
91
  description: 'Offline Storage for Notebooks and Files',
41
92
  storeName: 'files',
42
- version: 1,
93
+ ...this.defaultStorageOptions,
43
94
  });
44
95
  }
45
96
  /**
46
97
  * Initialize the default storage for counting file suffixes.
47
98
  */
48
99
  createDefaultCounters() {
49
- return localforage.createInstance({
50
- name: this._storageName,
100
+ return this._localforage.createInstance({
51
101
  description: 'Store the current file suffix counters',
52
102
  storeName: 'counters',
53
- version: 1,
103
+ ...this.defaultStorageOptions,
54
104
  });
55
105
  }
56
106
  /**
57
107
  * Create the default checkpoint storage.
58
108
  */
59
109
  createDefaultCheckpoints() {
60
- return localforage.createInstance({
61
- name: this._storageName,
110
+ return this._localforage.createInstance({
62
111
  description: 'Offline Storage for Checkpoints',
63
112
  storeName: 'checkpoints',
64
- version: 1,
113
+ ...this.defaultStorageOptions,
65
114
  });
66
115
  }
67
116
  /**
@@ -155,7 +204,7 @@ export class Contents {
155
204
  }
156
205
  }
157
206
  const key = file.path;
158
- await this._storage.setItem(key, file);
207
+ await (await this.storage).setItem(key, file);
159
208
  return file;
160
209
  }
161
210
  /**
@@ -189,7 +238,7 @@ export class Contents {
189
238
  name,
190
239
  path: toPath,
191
240
  };
192
- await this._storage.setItem(toPath, item);
241
+ await (await this.storage).setItem(toPath, item);
193
242
  return item;
194
243
  }
195
244
  /**
@@ -206,7 +255,8 @@ export class Contents {
206
255
  if (path === '') {
207
256
  return await this._getFolder(path);
208
257
  }
209
- const item = await this._storage.getItem(path);
258
+ const storage = await this.storage;
259
+ const item = await storage.getItem(path);
210
260
  const serverItem = await this._getServerContents(path, options);
211
261
  const model = (item || serverItem);
212
262
  if (!model) {
@@ -222,7 +272,7 @@ export class Contents {
222
272
  // for directories, find all files with the path as the prefix
223
273
  if (model.type === 'directory') {
224
274
  const contentMap = new Map();
225
- await this._storage.iterate((file, key) => {
275
+ await storage.iterate((file, key) => {
226
276
  // use an additional slash to not include the directory itself
227
277
  if (key === `${path}/${file.name}`) {
228
278
  contentMap.set(file.name, file);
@@ -274,11 +324,12 @@ export class Contents {
274
324
  path: newLocalPath,
275
325
  last_modified: modified,
276
326
  };
277
- await this._storage.setItem(newLocalPath, newFile);
327
+ const storage = await this.storage;
328
+ await storage.setItem(newLocalPath, newFile);
278
329
  // remove the old file
279
- await this._storage.removeItem(path);
330
+ await storage.removeItem(path);
280
331
  // remove the corresponding checkpoint
281
- await this._checkpoints.removeItem(path);
332
+ await (await this.checkpoints).removeItem(path);
282
333
  // if a directory, recurse through all children
283
334
  if (file.type === 'directory') {
284
335
  let child;
@@ -312,20 +363,31 @@ export class Contents {
312
363
  };
313
364
  // process the file if coming from an upload
314
365
  const ext = PathExt.extname((_a = options.name) !== null && _a !== void 0 ? _a : '');
366
+ const mimetype = mime.getType(ext) || MIME.OCTET_STREAM;
315
367
  if (options.content && options.format === 'base64') {
316
- // TODO: keep base64 if not a text file (image)
317
- const content = decodeURIComponent(escape(atob(options.content)));
318
- const nb = ext === '.ipynb';
319
- item = {
320
- ...item,
321
- content: nb ? JSON.parse(content) : content,
322
- format: nb ? 'json' : 'text',
323
- type: nb ? 'notebook' : 'file',
324
- };
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
+ }
325
384
  }
326
- await this._storage.setItem(path, item);
385
+ await (await this.storage).setItem(path, item);
327
386
  return item;
328
387
  }
388
+ unescapeContent(content) {
389
+ return decodeURIComponent(escape(atob(content)));
390
+ }
329
391
  /**
330
392
  * Delete a file from browser storage.
331
393
  *
@@ -337,7 +399,7 @@ export class Contents {
337
399
  async delete(path) {
338
400
  path = decodeURIComponent(path);
339
401
  const slashed = `${path}/`;
340
- const toDelete = (await this._storage.keys()).filter((key) => key === path || key.startsWith(slashed));
402
+ const toDelete = (await (await this.storage).keys()).filter((key) => key === path || key.startsWith(slashed));
341
403
  await Promise.all(toDelete.map(this.forgetPath, this));
342
404
  }
343
405
  /**
@@ -347,8 +409,8 @@ export class Contents {
347
409
  */
348
410
  async forgetPath(path) {
349
411
  await Promise.all([
350
- this._storage.removeItem(path),
351
- this._checkpoints.removeItem(path),
412
+ (await this.storage).removeItem(path),
413
+ (await this.checkpoints).removeItem(path),
352
414
  ]);
353
415
  }
354
416
  /**
@@ -361,18 +423,19 @@ export class Contents {
361
423
  */
362
424
  async createCheckpoint(path) {
363
425
  var _a;
426
+ const checkpoints = await this.checkpoints;
364
427
  path = decodeURIComponent(path);
365
428
  const item = await this.get(path, { content: true });
366
429
  if (!item) {
367
430
  throw Error(`Could not find file with path ${path}`);
368
431
  }
369
- const copies = ((_a = (await this._checkpoints.getItem(path))) !== null && _a !== void 0 ? _a : []).filter(Boolean);
432
+ const copies = ((_a = (await checkpoints.getItem(path))) !== null && _a !== void 0 ? _a : []).filter(Boolean);
370
433
  copies.push(item);
371
434
  // keep only a certain amount of checkpoints per file
372
435
  if (copies.length > N_CHECKPOINTS) {
373
436
  copies.splice(0, copies.length - N_CHECKPOINTS);
374
437
  }
375
- await this._checkpoints.setItem(path, copies);
438
+ await checkpoints.setItem(path, copies);
376
439
  const id = `${copies.length - 1}`;
377
440
  return { id, last_modified: item.last_modified };
378
441
  }
@@ -385,7 +448,7 @@ export class Contents {
385
448
  * the file.
386
449
  */
387
450
  async listCheckpoints(path) {
388
- const copies = (await this._checkpoints.getItem(path)) || [];
451
+ const copies = (await (await this.checkpoints).getItem(path)) || [];
389
452
  return copies.filter(Boolean).map(this.normalizeCheckpoint, this);
390
453
  }
391
454
  normalizeCheckpoint(model, id) {
@@ -401,10 +464,10 @@ export class Contents {
401
464
  */
402
465
  async restoreCheckpoint(path, checkpointID) {
403
466
  path = decodeURIComponent(path);
404
- const copies = ((await this._checkpoints.getItem(path)) || []);
467
+ const copies = ((await (await this.checkpoints).getItem(path)) || []);
405
468
  const id = parseInt(checkpointID);
406
469
  const item = copies[id];
407
- await this._storage.setItem(path, item);
470
+ await (await this.storage).setItem(path, item);
408
471
  }
409
472
  /**
410
473
  * Delete a checkpoint for a file.
@@ -416,10 +479,10 @@ export class Contents {
416
479
  */
417
480
  async deleteCheckpoint(path, checkpointID) {
418
481
  path = decodeURIComponent(path);
419
- const copies = ((await this._checkpoints.getItem(path)) || []);
482
+ const copies = ((await (await this.checkpoints).getItem(path)) || []);
420
483
  const id = parseInt(checkpointID);
421
484
  copies.splice(id, 1);
422
- await this._checkpoints.setItem(path, copies);
485
+ await (await this.checkpoints).setItem(path, copies);
423
486
  }
424
487
  /**
425
488
  * retrieve the contents for this path from the union of local storage and
@@ -431,7 +494,8 @@ export class Contents {
431
494
  */
432
495
  async _getFolder(path) {
433
496
  const content = new Map();
434
- await this._storage.iterate((file, key) => {
497
+ const storage = await this.storage;
498
+ await storage.iterate((file, key) => {
435
499
  if (key.includes('/')) {
436
500
  return;
437
501
  }
@@ -545,7 +609,7 @@ export class Contents {
545
609
  }
546
610
  }
547
611
  catch (err) {
548
- console.warn(`don't worry, about ${err}... nothing's broken. if there had been a
612
+ console.warn(`don't worry, about ${err}... nothing's broken. If there had been a
549
613
  file at ${apiURL}, you might see some more files.`);
550
614
  }
551
615
  this._serverContents.set(path, content);
@@ -560,9 +624,10 @@ export class Contents {
560
624
  */
561
625
  async _incrementCounter(type) {
562
626
  var _a;
563
- const current = (_a = (await this._counters.getItem(type))) !== null && _a !== void 0 ? _a : -1;
627
+ const counters = await this.counters;
628
+ const current = (_a = (await counters.getItem(type))) !== null && _a !== void 0 ? _a : -1;
564
629
  const counter = current + 1;
565
- await this._counters.setItem(type, counter);
630
+ await counters.setItem(type, counter);
566
631
  return counter;
567
632
  }
568
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
  *
package/lib/tokens.js CHANGED
@@ -18,6 +18,14 @@ export var MIME;
18
18
  /**
19
19
  * A list of mime types of common text file types
20
20
  */
21
- MIME.KNOWN_TEXT_TYPES = new Set([MIME.JSON, MIME.JS, MIME.MANIFEST_JSON, MIME.PYTHON, MIME.XML, MIME.SVG]);
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
+ ]);
22
30
  MIME.OCTET_STREAM = 'octet/stream';
23
31
  })(MIME || (MIME = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlite/contents",
3
- "version": "0.1.0-beta.2",
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.3.0",
46
- "@jupyterlab/services": "~6.3.0",
45
+ "@jupyterlab/nbformat": "~3.3.2",
46
+ "@jupyterlab/services": "~6.3.2",
47
+ "@jupyterlite/localforage": "^0.1.0-beta.3",
47
48
  "@lumino/coreutils": "^1.12.0",
48
- "localforage": "^1.9.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.3.0",
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",