@jupyterlite/contents 0.1.0-beta.1 → 0.1.0-beta.2

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,5 +1,6 @@
1
1
  import { Contents as ServerContents } from '@jupyterlab/services';
2
2
  import { IContents } from './tokens';
3
+ export declare type IModel = ServerContents.IModel;
3
4
  /**
4
5
  * A class to handle requests to /api/contents
5
6
  */
@@ -27,7 +28,7 @@ export declare class Contents implements IContents {
27
28
  *
28
29
  * @returns A promise which resolves with the created file content when the file is created.
29
30
  */
30
- newUntitled(options?: ServerContents.ICreateOptions): Promise<ServerContents.IModel | null>;
31
+ newUntitled(options?: ServerContents.ICreateOptions): Promise<IModel | null>;
31
32
  /**
32
33
  * Copy a file into a given directory.
33
34
  *
@@ -40,7 +41,7 @@ export declare class Contents implements IContents {
40
41
  * #### Notes
41
42
  * The server will select the name of the copied file.
42
43
  */
43
- copy(path: string, toDir: string): Promise<ServerContents.IModel>;
44
+ copy(path: string, toDir: string): Promise<IModel>;
44
45
  /**
45
46
  * Get a file or directory.
46
47
  *
@@ -49,7 +50,7 @@ export declare class Contents implements IContents {
49
50
  *
50
51
  * @returns A promise which resolves with the file content.
51
52
  */
52
- get(path: string, options?: ServerContents.IFetchOptions): Promise<ServerContents.IModel | null>;
53
+ get(path: string, options?: ServerContents.IFetchOptions): Promise<IModel | null>;
53
54
  /**
54
55
  * Rename a file or directory.
55
56
  *
@@ -58,7 +59,7 @@ export declare class Contents implements IContents {
58
59
  *
59
60
  * @returns A promise which resolves with the new file content model when the file is renamed.
60
61
  */
61
- rename(oldLocalPath: string, newLocalPath: string): Promise<ServerContents.IModel>;
62
+ rename(oldLocalPath: string, newLocalPath: string): Promise<IModel>;
62
63
  /**
63
64
  * Save a file.
64
65
  *
@@ -67,13 +68,22 @@ export declare class Contents implements IContents {
67
68
  *
68
69
  * @returns A promise which resolves with the file content model when the file is saved.
69
70
  */
70
- save(path: string, options?: Partial<ServerContents.IModel>): Promise<ServerContents.IModel | null>;
71
+ save(path: string, options?: Partial<IModel>): Promise<IModel | null>;
71
72
  /**
72
- * Delete a file.
73
+ * Delete a file from browser storage.
74
+ *
75
+ * Has no effect on server-backed files, which will re-appear with their
76
+ * original timestamp.
73
77
  *
74
78
  * @param path - The path to the file.
75
79
  */
76
80
  delete(path: string): Promise<void>;
81
+ /**
82
+ * Remove the localForage and checkpoints for a path.
83
+ *
84
+ * @param path - The path to the file
85
+ */
86
+ protected forgetPath(path: string): Promise<void>;
77
87
  /**
78
88
  * Create a checkpoint for a file.
79
89
  *
@@ -92,6 +102,7 @@ export declare class Contents implements IContents {
92
102
  * the file.
93
103
  */
94
104
  listCheckpoints(path: string): Promise<ServerContents.ICheckpointModel[]>;
105
+ protected normalizeCheckpoint(model: IModel, id: number): ServerContents.ICheckpointModel;
95
106
  /**
96
107
  * Restore a file to a known checkpoint state.
97
108
  *
@@ -125,6 +136,10 @@ export declare class Contents implements IContents {
125
136
  * return `null`.
126
137
  */
127
138
  private _getServerContents;
139
+ /**
140
+ * A reducer for turning arbitrary binary into a string
141
+ */
142
+ protected reduceBytesToString: (data: string, byte: number) => string;
128
143
  /**
129
144
  * retrieve the contents for this path from `__index__.json` in the appropriate
130
145
  * folder.
package/lib/contents.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { PageConfig, URLExt } from '@jupyterlab/coreutils';
2
2
  import { PathExt } from '@jupyterlab/coreutils';
3
3
  import localforage from 'localforage';
4
+ import { MIME } from './tokens';
4
5
  /**
5
6
  * The name of the local storage.
6
7
  */
@@ -9,17 +10,6 @@ const DEFAULT_STORAGE_NAME = 'JupyterLite Storage';
9
10
  * The number of checkpoints to save.
10
11
  */
11
12
  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
13
  /**
24
14
  * A class to handle requests to /api/contents
25
15
  */
@@ -28,6 +18,12 @@ export class Contents {
28
18
  * Construct a new localForage-powered contents provider
29
19
  */
30
20
  constructor(options) {
21
+ /**
22
+ * A reducer for turning arbitrary binary into a string
23
+ */
24
+ this.reduceBytesToString = (data, byte) => {
25
+ return data + String.fromCharCode(byte);
26
+ };
31
27
  this._serverContents = new Map();
32
28
  this._storageName = DEFAULT_STORAGE_NAME;
33
29
  this._storageName = (options || {}).contentsStorageName || DEFAULT_STORAGE_NAME;
@@ -112,7 +108,7 @@ export class Contents {
112
108
  path: `${dirname}${name}`,
113
109
  last_modified: created,
114
110
  created,
115
- format: 'text',
111
+ format: 'json',
116
112
  mimetype: '',
117
113
  content: null,
118
114
  size: undefined,
@@ -132,7 +128,7 @@ export class Contents {
132
128
  created,
133
129
  format: 'text',
134
130
  // TODO: handle mimetypes
135
- mimetype: 'text/plain',
131
+ mimetype: MIME.PLAIN_TEXT,
136
132
  content: '',
137
133
  size: 0,
138
134
  writable: true,
@@ -149,7 +145,7 @@ export class Contents {
149
145
  last_modified: created,
150
146
  created,
151
147
  format: 'json',
152
- mimetype: 'application/json',
148
+ mimetype: MIME.JSON,
153
149
  content: Private.EMPTY_NB,
154
150
  size: JSON.stringify(Private.EMPTY_NB).length,
155
151
  writable: true,
@@ -226,8 +222,7 @@ export class Contents {
226
222
  // for directories, find all files with the path as the prefix
227
223
  if (model.type === 'directory') {
228
224
  const contentMap = new Map();
229
- await this._storage.iterate((item, key) => {
230
- const file = item;
225
+ await this._storage.iterate((file, key) => {
231
226
  // use an additional slash to not include the directory itself
232
227
  if (key === `${path}/${file.name}`) {
233
228
  contentMap.set(file.name, file);
@@ -248,7 +243,7 @@ export class Contents {
248
243
  last_modified: model.last_modified,
249
244
  created: model.created,
250
245
  format: 'json',
251
- mimetype: 'application/json',
246
+ mimetype: MIME.JSON,
252
247
  content,
253
248
  size: undefined,
254
249
  writable: true,
@@ -332,25 +327,29 @@ export class Contents {
332
327
  return item;
333
328
  }
334
329
  /**
335
- * Delete a file.
330
+ * Delete a file from browser storage.
331
+ *
332
+ * Has no effect on server-backed files, which will re-appear with their
333
+ * original timestamp.
336
334
  *
337
335
  * @param path - The path to the file.
338
336
  */
339
337
  async delete(path) {
340
338
  path = decodeURIComponent(path);
341
- const toDelete = [];
342
- // handle deleting directories recursively
343
- await this._storage.iterate((item, key) => {
344
- if (key === path || key.startsWith(`${path}/`)) {
345
- toDelete.push(key);
346
- }
347
- });
348
- await Promise.all(toDelete.map(async (p) => {
349
- return Promise.all([
350
- this._storage.removeItem(p),
351
- this._checkpoints.removeItem(p),
352
- ]);
353
- }));
339
+ const slashed = `${path}/`;
340
+ const toDelete = (await this._storage.keys()).filter((key) => key === path || key.startsWith(slashed));
341
+ await Promise.all(toDelete.map(this.forgetPath, this));
342
+ }
343
+ /**
344
+ * Remove the localForage and checkpoints for a path.
345
+ *
346
+ * @param path - The path to the file
347
+ */
348
+ async forgetPath(path) {
349
+ await Promise.all([
350
+ this._storage.removeItem(path),
351
+ this._checkpoints.removeItem(path),
352
+ ]);
354
353
  }
355
354
  /**
356
355
  * Create a checkpoint for a file.
@@ -367,7 +366,7 @@ export class Contents {
367
366
  if (!item) {
368
367
  throw Error(`Could not find file with path ${path}`);
369
368
  }
370
- const copies = ((_a = (await this._checkpoints.getItem(path))) !== null && _a !== void 0 ? _a : []).filter((item) => !!item);
369
+ const copies = ((_a = (await this._checkpoints.getItem(path))) !== null && _a !== void 0 ? _a : []).filter(Boolean);
371
370
  copies.push(item);
372
371
  // keep only a certain amount of checkpoints per file
373
372
  if (copies.length > N_CHECKPOINTS) {
@@ -375,10 +374,7 @@ export class Contents {
375
374
  }
376
375
  await this._checkpoints.setItem(path, copies);
377
376
  const id = `${copies.length - 1}`;
378
- return {
379
- id,
380
- last_modified: item.last_modified,
381
- };
377
+ return { id, last_modified: item.last_modified };
382
378
  }
383
379
  /**
384
380
  * List available checkpoints for a file.
@@ -389,16 +385,11 @@ export class Contents {
389
385
  * the file.
390
386
  */
391
387
  async listCheckpoints(path) {
392
- const copies = ((await this._checkpoints.getItem(path)) ||
393
- []);
394
- return copies
395
- .filter((item) => !!item)
396
- .map((file, id) => {
397
- return {
398
- id: id.toString(),
399
- last_modified: file.last_modified,
400
- };
401
- });
388
+ const copies = (await this._checkpoints.getItem(path)) || [];
389
+ return copies.filter(Boolean).map(this.normalizeCheckpoint, this);
390
+ }
391
+ normalizeCheckpoint(model, id) {
392
+ return { id: id.toString(), last_modified: model.last_modified };
402
393
  }
403
394
  /**
404
395
  * Restore a file to a known checkpoint state.
@@ -410,8 +401,7 @@ export class Contents {
410
401
  */
411
402
  async restoreCheckpoint(path, checkpointID) {
412
403
  path = decodeURIComponent(path);
413
- const copies = ((await this._checkpoints.getItem(path)) ||
414
- []);
404
+ const copies = ((await this._checkpoints.getItem(path)) || []);
415
405
  const id = parseInt(checkpointID);
416
406
  const item = copies[id];
417
407
  await this._storage.setItem(path, item);
@@ -426,8 +416,7 @@ export class Contents {
426
416
  */
427
417
  async deleteCheckpoint(path, checkpointID) {
428
418
  path = decodeURIComponent(path);
429
- const copies = ((await this._checkpoints.getItem(path)) ||
430
- []);
419
+ const copies = ((await this._checkpoints.getItem(path)) || []);
431
420
  const id = parseInt(checkpointID);
432
421
  copies.splice(id, 1);
433
422
  await this._checkpoints.setItem(path, copies);
@@ -442,11 +431,10 @@ export class Contents {
442
431
  */
443
432
  async _getFolder(path) {
444
433
  const content = new Map();
445
- await this._storage.iterate((item, key) => {
434
+ await this._storage.iterate((file, key) => {
446
435
  if (key.includes('/')) {
447
436
  return;
448
437
  }
449
- const file = item;
450
438
  content.set(file.path, file);
451
439
  });
452
440
  // layer in contents that don't have local overwrites
@@ -464,7 +452,7 @@ export class Contents {
464
452
  last_modified: new Date(0).toISOString(),
465
453
  created: new Date(0).toISOString(),
466
454
  format: 'json',
467
- mimetype: 'application/json',
455
+ mimetype: MIME.JSON,
468
456
  content: Array.from(content.values()),
469
457
  size: undefined,
470
458
  writable: true,
@@ -489,7 +477,7 @@ export class Contents {
489
477
  last_modified: new Date(0).toISOString(),
490
478
  created: new Date(0).toISOString(),
491
479
  format: 'text',
492
- mimetype: 'text/plain',
480
+ mimetype: MIME.PLAIN_TEXT,
493
481
  type: 'file',
494
482
  writable: true,
495
483
  content: null,
@@ -513,24 +501,24 @@ export class Contents {
513
501
  ...model,
514
502
  content: await response.json(),
515
503
  format: 'json',
516
- mimetype: model.mimetype || 'application/json',
504
+ mimetype: model.mimetype || MIME.JSON,
517
505
  };
518
506
  }
519
507
  else if (mimetype.indexOf('text') !== -1 ||
520
- EXTRA_TEXT_MIME_TYPES.has(mimetype)) {
508
+ MIME.KNOWN_TEXT_TYPES.has(mimetype)) {
521
509
  model = {
522
510
  ...model,
523
511
  content: await response.text(),
524
512
  format: 'text',
525
- mimetype: mimetype || 'text/plain',
513
+ mimetype: mimetype || MIME.PLAIN_TEXT,
526
514
  };
527
515
  }
528
516
  else {
529
517
  model = {
530
518
  ...model,
531
- content: btoa(String.fromCharCode(...new Uint8Array(await response.arrayBuffer()))),
519
+ content: btoa(new Uint8Array(await response.arrayBuffer()).reduce(this.reduceBytesToString, '')),
532
520
  format: 'base64',
533
- mimetype: mimetype || 'octet/stream',
521
+ mimetype: mimetype || MIME.OCTET_STREAM,
534
522
  };
535
523
  }
536
524
  }
package/lib/tokens.d.ts CHANGED
@@ -99,3 +99,20 @@ export interface IContents {
99
99
  */
100
100
  deleteCheckpoint(path: string, checkpointID: string): Promise<void>;
101
101
  }
102
+ /**
103
+ * Commonly-used mimetypes
104
+ */
105
+ export declare namespace MIME {
106
+ const JS = "application/javascript";
107
+ const JSON = "application/json";
108
+ const MANIFEST_JSON = "application/manifest+json";
109
+ const PLAIN_TEXT = "text/plain";
110
+ const PYTHON = "application/x-python-code";
111
+ const SVG = "image/svg+xml";
112
+ const XML = "application/xml";
113
+ /**
114
+ * A list of mime types of common text file types
115
+ */
116
+ const KNOWN_TEXT_TYPES: Set<string>;
117
+ const OCTET_STREAM = "octet/stream";
118
+ }
package/lib/tokens.js CHANGED
@@ -3,3 +3,21 @@ 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([MIME.JSON, MIME.JS, MIME.MANIFEST_JSON, MIME.PYTHON, MIME.XML, MIME.SVG]);
22
+ MIME.OCTET_STREAM = 'octet/stream';
23
+ })(MIME || (MIME = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlite/contents",
3
- "version": "0.1.0-beta.1",
3
+ "version": "0.1.0-beta.2",
4
4
  "description": "JupyterLite - Contents",
5
5
  "homepage": "https://github.com/jupyterlite/jupyterlite",
6
6
  "bugs": {