@jupyterlite/contents 0.4.0-alpha.2 → 0.4.0-alpha.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/src/drivefs.ts CHANGED
@@ -6,6 +6,8 @@
6
6
  // And from https://github.com/gzuidhof/starboard-notebook
7
7
 
8
8
  // LICENSE: https://github.com/gzuidhof/starboard-notebook/blob/cd8d3fc30af4bd29cdd8f6b8c207df8138f5d5dd/LICENSE
9
+ import { Contents } from '@jupyterlab/services';
10
+
9
11
  import {
10
12
  FS,
11
13
  ERRNO_CODES,
@@ -41,13 +43,42 @@ export type TDriveMethod =
41
43
  | 'put';
42
44
 
43
45
  /**
44
- * Interface of a request on the /api/drive endpoint
46
+ * Type of the data argument for the drive request, based on the request name
45
47
  */
46
- export interface IDriveRequest {
48
+ export type TDriveData = {
49
+ rename: {
50
+ /**
51
+ * The new path for the file
52
+ */
53
+ newPath: string;
54
+ };
55
+ mknod: {
56
+ /**
57
+ * The mode of the file to create
58
+ */
59
+ mode: number;
60
+ };
61
+ put: {
62
+ /**
63
+ * The file content to write
64
+ */
65
+ data: any;
66
+
67
+ /**
68
+ * The file content format
69
+ */
70
+ format: Contents.FileFormat;
71
+ };
72
+ };
73
+
74
+ /**
75
+ * Drive request
76
+ */
77
+ export type TDriveRequest<T extends TDriveMethod> = {
47
78
  /**
48
79
  * The method of the request (rmdir, readdir etc)
49
80
  */
50
- method: TDriveMethod;
81
+ method: T;
51
82
 
52
83
  /**
53
84
  * The expected receiver of the request
@@ -58,12 +89,36 @@ export interface IDriveRequest {
58
89
  * The path to the file/directory for which the request was sent
59
90
  */
60
91
  path: string;
92
+ } & (T extends keyof TDriveData ? { data: TDriveData[T] } : object);
93
+
94
+ type TDriveResponses = {
95
+ readdir: string[];
96
+ rmdir: void;
97
+ rename: void;
98
+ getmode: number;
99
+ lookup: DriveFS.ILookup;
100
+ mknod: void;
101
+ getattr: IStats;
102
+ get:
103
+ | {
104
+ /**
105
+ * The returned file content
106
+ */
107
+ content: any;
108
+
109
+ /**
110
+ * The content format
111
+ */
112
+ format: Contents.FileFormat;
113
+ }
114
+ | undefined;
115
+ put: void;
116
+ };
61
117
 
62
- /**
63
- * Extra data on the request
64
- */
65
- data?: any;
66
- }
118
+ /**
119
+ * Drive response
120
+ */
121
+ export type TDriveResponse<T extends TDriveMethod> = TDriveResponses[T];
67
122
 
68
123
  // Mapping flag -> do we need to overwrite the file upon closing it
69
124
  const flagNeedsWrite: { [flag: number]: boolean } = {
@@ -253,7 +308,7 @@ export class DriveFSEmscriptenNodeOps implements IEmscriptenNodeOps {
253
308
  if (!result.ok) {
254
309
  throw this.fs.FS.genericErrors[this.fs.ERRNO_CODES['ENOENT']];
255
310
  }
256
- return this.fs.createNode(node, name, result.mode, 0);
311
+ return this.fs.createNode(node, name, result.mode!, 0);
257
312
  }
258
313
 
259
314
  mknod(
@@ -313,48 +368,23 @@ export class DriveFSEmscriptenNodeOps implements IEmscriptenNodeOps {
313
368
  }
314
369
 
315
370
  /**
316
- * Wrap ServiceWorker requests for an Emscripten-compatible synchronous API.
371
+ * ContentsAPI base class
317
372
  */
318
- export class ContentsAPI {
319
- constructor(
320
- baseUrl: string,
321
- driveName: string,
322
- mountpoint: string,
323
- FS: FS,
324
- ERRNO_CODES: ERRNO_CODES,
325
- ) {
326
- this._baseUrl = baseUrl;
373
+ export abstract class ContentsAPI {
374
+ constructor(driveName: string, mountpoint: string, FS: FS, ERRNO_CODES: ERRNO_CODES) {
327
375
  this._driveName = driveName;
328
376
  this._mountpoint = mountpoint;
377
+
329
378
  this.FS = FS;
330
379
  this.ERRNO_CODES = ERRNO_CODES;
331
380
  }
332
381
 
333
- request(data: IDriveRequest): any {
334
- const xhr = new XMLHttpRequest();
335
- xhr.open('POST', encodeURI(this.endpoint), false);
336
-
337
- try {
338
- xhr.send(JSON.stringify(data));
339
- } catch (e) {
340
- console.error(e);
341
- }
342
-
343
- if (xhr.status >= 400) {
344
- throw new this.FS.ErrnoError(this.ERRNO_CODES['EINVAL']);
345
- }
346
-
347
- return JSON.parse(xhr.responseText);
348
- }
349
-
350
382
  lookup(path: string): DriveFS.ILookup {
351
383
  return this.request({ method: 'lookup', path: this.normalizePath(path) });
352
384
  }
353
385
 
354
386
  getmode(path: string): number {
355
- return Number.parseInt(
356
- this.request({ method: 'getmode', path: this.normalizePath(path) }),
357
- );
387
+ return this.request({ method: 'getmode', path: this.normalizePath(path) });
358
388
  }
359
389
 
360
390
  mknod(path: string, mode: number) {
@@ -388,7 +418,14 @@ export class ContentsAPI {
388
418
  }
389
419
 
390
420
  get(path: string): DriveFS.IFile {
391
- const response = this.request({ method: 'get', path: this.normalizePath(path) });
421
+ const response = this.request({
422
+ method: 'get',
423
+ path: this.normalizePath(path),
424
+ });
425
+
426
+ if (!response) {
427
+ throw new this.FS.ErrnoError(this.ERRNO_CODES['ENOENT']);
428
+ }
392
429
 
393
430
  const serializedContent = response.content;
394
431
  const format: 'json' | 'text' | 'base64' | null = response.format;
@@ -447,14 +484,20 @@ export class ContentsAPI {
447
484
  }
448
485
 
449
486
  getattr(path: string): IStats {
450
- const stats: IStats = this.request({
487
+ const stats = this.request({
451
488
  method: 'getattr',
452
489
  path: this.normalizePath(path),
453
490
  });
454
491
  // Turn datetimes into proper objects
455
- stats.atime = new Date(stats.atime);
456
- stats.mtime = new Date(stats.mtime);
457
- stats.ctime = new Date(stats.ctime);
492
+ if (stats.atime) {
493
+ stats.atime = new Date(stats.atime);
494
+ }
495
+ if (stats.mtime) {
496
+ stats.mtime = new Date(stats.mtime);
497
+ }
498
+ if (stats.ctime) {
499
+ stats.ctime = new Date(stats.ctime);
500
+ }
458
501
  // ensure a non-undefined size (0 isn't great, though)
459
502
  stats.size = stats.size || 0;
460
503
  return stats;
@@ -479,6 +522,48 @@ export class ContentsAPI {
479
522
  return path;
480
523
  }
481
524
 
525
+ abstract request<T extends TDriveMethod>(data: TDriveRequest<T>): TDriveResponse<T>;
526
+
527
+ private _driveName: string;
528
+ private _mountpoint: string;
529
+
530
+ protected FS: FS;
531
+ protected ERRNO_CODES: ERRNO_CODES;
532
+ }
533
+
534
+ /**
535
+ * An Emscripten-compatible synchronous Contents API using the service worker.
536
+ */
537
+ export class ServiceWorkerContentsAPI extends ContentsAPI {
538
+ constructor(
539
+ baseUrl: string,
540
+ driveName: string,
541
+ mountpoint: string,
542
+ FS: FS,
543
+ ERRNO_CODES: ERRNO_CODES,
544
+ ) {
545
+ super(driveName, mountpoint, FS, ERRNO_CODES);
546
+
547
+ this._baseUrl = baseUrl;
548
+ }
549
+
550
+ request<T extends TDriveMethod>(data: TDriveRequest<T>): TDriveResponse<T> {
551
+ const xhr = new XMLHttpRequest();
552
+ xhr.open('POST', encodeURI(this.endpoint), false);
553
+
554
+ try {
555
+ xhr.send(JSON.stringify(data));
556
+ } catch (e) {
557
+ console.error(e);
558
+ }
559
+
560
+ if (xhr.status >= 400) {
561
+ throw new this.FS.ErrnoError(this.ERRNO_CODES['EINVAL']);
562
+ }
563
+
564
+ return JSON.parse(xhr.responseText);
565
+ }
566
+
482
567
  /**
483
568
  * Get the api/drive endpoint
484
569
  */
@@ -487,10 +572,6 @@ export class ContentsAPI {
487
572
  }
488
573
 
489
574
  private _baseUrl: string;
490
- private _driveName: string;
491
- private _mountpoint: string;
492
- private FS: FS;
493
- private ERRNO_CODES: ERRNO_CODES;
494
575
  }
495
576
 
496
577
  export class DriveFS {
@@ -504,13 +585,8 @@ export class DriveFS {
504
585
  this.FS = options.FS;
505
586
  this.PATH = options.PATH;
506
587
  this.ERRNO_CODES = options.ERRNO_CODES;
507
- this.API = new ContentsAPI(
508
- options.baseUrl,
509
- options.driveName,
510
- options.mountpoint,
511
- this.FS,
512
- this.ERRNO_CODES,
513
- );
588
+ this.API = this.createAPI(options);
589
+
514
590
  this.driveName = options.driveName;
515
591
 
516
592
  this.node_ops = new DriveFSEmscriptenNodeOps(this);
@@ -520,6 +596,21 @@ export class DriveFS {
520
596
  node_ops: IEmscriptenNodeOps;
521
597
  stream_ops: IEmscriptenStreamOps;
522
598
 
599
+ /**
600
+ * Create the ContentsAPI.
601
+ *
602
+ * This is supposed to be overwritten if needed.
603
+ */
604
+ createAPI(options: DriveFS.IOptions): ContentsAPI {
605
+ return new ServiceWorkerContentsAPI(
606
+ options.baseUrl,
607
+ options.driveName,
608
+ options.mountpoint,
609
+ options.FS,
610
+ options.ERRNO_CODES,
611
+ );
612
+ }
613
+
523
614
  mount(mount: any): IEmscriptenFSNode {
524
615
  return this.createNode(null, mount.mountpoint, DIR_MODE | 511, 0);
525
616
  }
@@ -576,7 +667,7 @@ export namespace DriveFS {
576
667
  */
577
668
  export interface ILookup {
578
669
  ok: boolean;
579
- mode: number;
670
+ mode?: number;
580
671
  }
581
672
 
582
673
  /**
package/src/emscripten.ts CHANGED
@@ -24,8 +24,8 @@ export const SEEK_END = 2;
24
24
 
25
25
  export interface IStats {
26
26
  dev: number;
27
- ino: number;
28
- mode: number;
27
+ ino?: number;
28
+ mode?: number;
29
29
  nlink: number;
30
30
  uid: number;
31
31
  gid: number;
@@ -33,9 +33,9 @@ export interface IStats {
33
33
  size: number;
34
34
  blksize: number;
35
35
  blocks: number;
36
- atime: Date;
37
- mtime: Date;
38
- ctime: Date;
36
+ atime: Date | string;
37
+ mtime: Date | string;
38
+ ctime: Date | string;
39
39
  timestamp?: number;
40
40
  }
41
41
 
package/src/index.ts CHANGED
@@ -6,3 +6,4 @@ export * from './drivefs';
6
6
  export * from './tokens';
7
7
  export * from './broadcast';
8
8
  export * from './emscripten';
9
+ export * from './drivecontents';