@jupyterlite/contents 0.1.0-alpha.19 → 0.1.0-alpha.23

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
@@ -4,6 +4,22 @@ import { IContents } from './tokens';
4
4
  * A class to handle requests to /api/contents
5
5
  */
6
6
  export declare class Contents implements IContents {
7
+ /**
8
+ * Construct a new localForage-powered contents provider
9
+ */
10
+ constructor(options?: Contents.IOptions);
11
+ /**
12
+ * Initialize the default storage for contents.
13
+ */
14
+ protected createDefaultStorage(): LocalForage;
15
+ /**
16
+ * Initialize the default storage for counting file suffixes.
17
+ */
18
+ protected createDefaultCounters(): LocalForage;
19
+ /**
20
+ * Create the default checkpoint storage.
21
+ */
22
+ protected createDefaultCheckpoints(): LocalForage;
7
23
  /**
8
24
  * Create a new untitled file or directory in the specified directory path.
9
25
  *
@@ -126,7 +142,19 @@ export declare class Contents implements IContents {
126
142
  */
127
143
  private _incrementCounter;
128
144
  private _serverContents;
145
+ private _storageName;
129
146
  private _storage;
130
147
  private _counters;
131
148
  private _checkpoints;
132
149
  }
150
+ /**
151
+ * A namespace for contents information.
152
+ */
153
+ export declare namespace Contents {
154
+ interface IOptions {
155
+ /**
156
+ * The name of the storage instance on e.g. IndexedDB, localStorage
157
+ */
158
+ contentsStorageName: string;
159
+ }
160
+ }
package/lib/contents.js CHANGED
@@ -4,7 +4,7 @@ import localforage from 'localforage';
4
4
  /**
5
5
  * The name of the local storage.
6
6
  */
7
- const STORAGE_NAME = 'JupyterLite Storage';
7
+ const DEFAULT_STORAGE_NAME = 'JupyterLite Storage';
8
8
  /**
9
9
  * The number of checkpoints to save.
10
10
  */
@@ -18,31 +18,54 @@ const EXTRA_TEXT_MIME_TYPES = new Set([
18
18
  'application/manifest+json',
19
19
  'application/x-python-code',
20
20
  'application/xml',
21
- 'image/svg+xml'
21
+ 'image/svg+xml',
22
22
  ]);
23
23
  /**
24
24
  * A class to handle requests to /api/contents
25
25
  */
26
26
  export class Contents {
27
- constructor() {
27
+ /**
28
+ * Construct a new localForage-powered contents provider
29
+ */
30
+ constructor(options) {
28
31
  this._serverContents = new Map();
29
- this._storage = localforage.createInstance({
30
- name: STORAGE_NAME,
32
+ this._storageName = DEFAULT_STORAGE_NAME;
33
+ this._storageName = (options || {}).contentsStorageName || DEFAULT_STORAGE_NAME;
34
+ this._storage = this.createDefaultStorage();
35
+ this._counters = this.createDefaultCounters();
36
+ this._checkpoints = this.createDefaultCheckpoints();
37
+ }
38
+ /**
39
+ * Initialize the default storage for contents.
40
+ */
41
+ createDefaultStorage() {
42
+ return localforage.createInstance({
43
+ name: this._storageName,
31
44
  description: 'Offline Storage for Notebooks and Files',
32
45
  storeName: 'files',
33
- version: 1
46
+ version: 1,
34
47
  });
35
- this._counters = localforage.createInstance({
36
- name: STORAGE_NAME,
48
+ }
49
+ /**
50
+ * Initialize the default storage for counting file suffixes.
51
+ */
52
+ createDefaultCounters() {
53
+ return localforage.createInstance({
54
+ name: this._storageName,
37
55
  description: 'Store the current file suffix counters',
38
56
  storeName: 'counters',
39
- version: 1
57
+ version: 1,
40
58
  });
41
- this._checkpoints = localforage.createInstance({
42
- name: STORAGE_NAME,
59
+ }
60
+ /**
61
+ * Create the default checkpoint storage.
62
+ */
63
+ createDefaultCheckpoints() {
64
+ return localforage.createInstance({
65
+ name: this._storageName,
43
66
  description: 'Offline Storage for Checkpoints',
44
67
  storeName: 'checkpoints',
45
- version: 1
68
+ version: 1,
46
69
  });
47
70
  }
48
71
  /**
@@ -94,7 +117,7 @@ export class Contents {
94
117
  content: null,
95
118
  size: undefined,
96
119
  writable: true,
97
- type: 'directory'
120
+ type: 'directory',
98
121
  };
99
122
  break;
100
123
  }
@@ -113,7 +136,7 @@ export class Contents {
113
136
  content: '',
114
137
  size: 0,
115
138
  writable: true,
116
- type: 'file'
139
+ type: 'file',
117
140
  };
118
141
  break;
119
142
  }
@@ -130,7 +153,7 @@ export class Contents {
130
153
  content: Private.EMPTY_NB,
131
154
  size: JSON.stringify(Private.EMPTY_NB).length,
132
155
  writable: true,
133
- type: 'notebook'
156
+ type: 'notebook',
134
157
  };
135
158
  break;
136
159
  }
@@ -168,7 +191,7 @@ export class Contents {
168
191
  item = {
169
192
  ...item,
170
193
  name,
171
- path: toPath
194
+ path: toPath,
172
195
  };
173
196
  await this._storage.setItem(toPath, item);
174
197
  return item;
@@ -197,7 +220,7 @@ export class Contents {
197
220
  return {
198
221
  ...model,
199
222
  content: null,
200
- size: undefined
223
+ size: undefined,
201
224
  };
202
225
  }
203
226
  // for directories, find all files with the path as the prefix
@@ -229,7 +252,7 @@ export class Contents {
229
252
  content,
230
253
  size: undefined,
231
254
  writable: true,
232
- type: 'directory'
255
+ type: 'directory',
233
256
  };
234
257
  }
235
258
  return model;
@@ -254,7 +277,7 @@ export class Contents {
254
277
  ...file,
255
278
  name,
256
279
  path: newLocalPath,
257
- last_modified: modified
280
+ last_modified: modified,
258
281
  };
259
282
  await this._storage.setItem(newLocalPath, newFile);
260
283
  // remove the old file
@@ -290,7 +313,7 @@ export class Contents {
290
313
  item = {
291
314
  ...item,
292
315
  ...options,
293
- last_modified: modified
316
+ last_modified: modified,
294
317
  };
295
318
  // process the file if coming from an upload
296
319
  const ext = PathExt.extname((_a = options.name) !== null && _a !== void 0 ? _a : '');
@@ -302,7 +325,7 @@ export class Contents {
302
325
  ...item,
303
326
  content: nb ? JSON.parse(content) : content,
304
327
  format: nb ? 'json' : 'text',
305
- type: nb ? 'notebook' : 'file'
328
+ type: nb ? 'notebook' : 'file',
306
329
  };
307
330
  }
308
331
  await this._storage.setItem(path, item);
@@ -325,7 +348,7 @@ export class Contents {
325
348
  await Promise.all(toDelete.map(async (p) => {
326
349
  return Promise.all([
327
350
  this._storage.removeItem(p),
328
- this._checkpoints.removeItem(p)
351
+ this._checkpoints.removeItem(p),
329
352
  ]);
330
353
  }));
331
354
  }
@@ -344,7 +367,7 @@ export class Contents {
344
367
  if (!item) {
345
368
  throw Error(`Could not find file with path ${path}`);
346
369
  }
347
- const copies = ((_a = (await this._checkpoints.getItem(path))) !== null && _a !== void 0 ? _a : []).filter(item => !!item);
370
+ const copies = ((_a = (await this._checkpoints.getItem(path))) !== null && _a !== void 0 ? _a : []).filter((item) => !!item);
348
371
  copies.push(item);
349
372
  // keep only a certain amount of checkpoints per file
350
373
  if (copies.length > N_CHECKPOINTS) {
@@ -354,7 +377,7 @@ export class Contents {
354
377
  const id = `${copies.length - 1}`;
355
378
  return {
356
379
  id,
357
- last_modified: item.last_modified
380
+ last_modified: item.last_modified,
358
381
  };
359
382
  }
360
383
  /**
@@ -369,11 +392,11 @@ export class Contents {
369
392
  const copies = ((await this._checkpoints.getItem(path)) ||
370
393
  []);
371
394
  return copies
372
- .filter(item => !!item)
395
+ .filter((item) => !!item)
373
396
  .map((file, id) => {
374
397
  return {
375
398
  id: id.toString(),
376
- last_modified: file.last_modified
399
+ last_modified: file.last_modified,
377
400
  };
378
401
  });
379
402
  }
@@ -445,7 +468,7 @@ export class Contents {
445
468
  content: Array.from(content.values()),
446
469
  size: undefined,
447
470
  writable: true,
448
- type: 'directory'
471
+ type: 'directory',
449
472
  };
450
473
  }
451
474
  /**
@@ -469,7 +492,7 @@ export class Contents {
469
492
  mimetype: 'text/plain',
470
493
  type: 'file',
471
494
  writable: true,
472
- content: null
495
+ content: null,
473
496
  };
474
497
  if (options === null || options === void 0 ? void 0 : options.content) {
475
498
  if (model.type === 'directory') {
@@ -490,7 +513,7 @@ export class Contents {
490
513
  ...model,
491
514
  content: await response.json(),
492
515
  format: 'json',
493
- mimetype: model.mimetype || 'application/json'
516
+ mimetype: model.mimetype || 'application/json',
494
517
  };
495
518
  }
496
519
  else if (mimetype.indexOf('text') !== -1 ||
@@ -499,7 +522,7 @@ export class Contents {
499
522
  ...model,
500
523
  content: await response.text(),
501
524
  format: 'text',
502
- mimetype: mimetype || 'text/plain'
525
+ mimetype: mimetype || 'text/plain',
503
526
  };
504
527
  }
505
528
  else {
@@ -507,7 +530,7 @@ export class Contents {
507
530
  ...model,
508
531
  content: btoa(String.fromCharCode(...new Uint8Array(await response.arrayBuffer()))),
509
532
  format: 'base64',
510
- mimetype: mimetype || 'octet/stream'
533
+ mimetype: mimetype || 'octet/stream',
511
534
  };
512
535
  }
513
536
  }
@@ -565,10 +588,10 @@ var Private;
565
588
  */
566
589
  Private.EMPTY_NB = {
567
590
  metadata: {
568
- orig_nbformat: 4
591
+ orig_nbformat: 4,
569
592
  },
570
593
  nbformat_minor: 4,
571
594
  nbformat: 4,
572
- cells: []
595
+ cells: [],
573
596
  };
574
597
  })(Private || (Private = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlite/contents",
3
- "version": "0.1.0-alpha.19",
3
+ "version": "0.1.0-alpha.23",
4
4
  "description": "JupyterLite - Contents",
5
5
  "homepage": "https://github.com/jupyterlite/jupyterlite",
6
6
  "bugs": {
@@ -42,20 +42,20 @@
42
42
  "watch": "tsc -b --watch"
43
43
  },
44
44
  "dependencies": {
45
- "@jupyterlab/nbformat": "~3.2.6",
46
- "@jupyterlab/services": "~6.2.6",
45
+ "@jupyterlab/nbformat": "~3.2.9",
46
+ "@jupyterlab/services": "~6.2.9",
47
47
  "@lumino/coreutils": "^1.8.0",
48
48
  "localforage": "^1.9.0"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@babel/core": "^7.11.6",
52
52
  "@babel/preset-env": "^7.12.1",
53
- "@jupyterlab/testutils": "~3.2.6",
53
+ "@jupyterlab/testutils": "~3.2.9",
54
54
  "@types/jest": "^26.0.10",
55
55
  "jest": "^26.4.2",
56
56
  "rimraf": "~3.0.0",
57
57
  "ts-jest": "^26.3.0",
58
- "typescript": "~4.2.3"
58
+ "typescript": "~4.5.2"
59
59
  },
60
60
  "publishConfig": {
61
61
  "access": "public"