@jupyterlite/contents 0.1.0-beta.1 → 0.1.0-beta.10
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 +184 -111
- package/lib/drivefs.d.ts +169 -0
- package/lib/drivefs.js +327 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/tokens.d.ts +25 -0
- package/lib/tokens.js +54 -0
- package/package.json +8 -5
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,7 @@
|
|
|
1
1
|
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
|
|
2
2
|
import { PathExt } from '@jupyterlab/coreutils';
|
|
3
|
-
import
|
|
3
|
+
import { MIME, FILE } from './tokens';
|
|
4
|
+
import { PromiseDelegate } from '@lumino/coreutils';
|
|
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,44 +18,98 @@ 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
|
-
this.
|
|
29
|
+
this._storageDrivers = null;
|
|
30
|
+
this._localforage = options.localforage;
|
|
31
|
+
this._storageName = options.storageName || DEFAULT_STORAGE_NAME;
|
|
32
|
+
this._storageDrivers = options.storageDrivers || null;
|
|
33
|
+
this._ready = new PromiseDelegate();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Finish any initialization after server has started and all extensions are applied.
|
|
37
|
+
*/
|
|
38
|
+
async initialize() {
|
|
39
|
+
await this.initStorage();
|
|
40
|
+
this._ready.resolve(void 0);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Initialize all storage instances
|
|
44
|
+
*/
|
|
45
|
+
async initStorage() {
|
|
34
46
|
this._storage = this.createDefaultStorage();
|
|
35
47
|
this._counters = this.createDefaultCounters();
|
|
36
48
|
this._checkpoints = this.createDefaultCheckpoints();
|
|
37
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* A promise that resolves once all storage is fully initialized.
|
|
52
|
+
*/
|
|
53
|
+
get ready() {
|
|
54
|
+
return this._ready.promise;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A lazy reference to the underlying storage.
|
|
58
|
+
*/
|
|
59
|
+
get storage() {
|
|
60
|
+
return this.ready.then(() => this._storage);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* A lazy reference to the underlying counters.
|
|
64
|
+
*/
|
|
65
|
+
get counters() {
|
|
66
|
+
return this.ready.then(() => this._counters);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* A lazy reference to the underlying checkpoints.
|
|
70
|
+
*/
|
|
71
|
+
get checkpoints() {
|
|
72
|
+
return this.ready.then(() => this._checkpoints);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Get default options for localForage instances
|
|
76
|
+
*/
|
|
77
|
+
get defaultStorageOptions() {
|
|
78
|
+
const driver = this._storageDrivers && this._storageDrivers.length ? this._storageDrivers : null;
|
|
79
|
+
return {
|
|
80
|
+
version: 1,
|
|
81
|
+
name: this._storageName,
|
|
82
|
+
...(driver ? { driver } : {}),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
38
85
|
/**
|
|
39
86
|
* Initialize the default storage for contents.
|
|
40
87
|
*/
|
|
41
88
|
createDefaultStorage() {
|
|
42
|
-
return
|
|
43
|
-
name: this._storageName,
|
|
89
|
+
return this._localforage.createInstance({
|
|
44
90
|
description: 'Offline Storage for Notebooks and Files',
|
|
45
91
|
storeName: 'files',
|
|
46
|
-
|
|
92
|
+
...this.defaultStorageOptions,
|
|
47
93
|
});
|
|
48
94
|
}
|
|
49
95
|
/**
|
|
50
96
|
* Initialize the default storage for counting file suffixes.
|
|
51
97
|
*/
|
|
52
98
|
createDefaultCounters() {
|
|
53
|
-
return
|
|
54
|
-
name: this._storageName,
|
|
99
|
+
return this._localforage.createInstance({
|
|
55
100
|
description: 'Store the current file suffix counters',
|
|
56
101
|
storeName: 'counters',
|
|
57
|
-
|
|
102
|
+
...this.defaultStorageOptions,
|
|
58
103
|
});
|
|
59
104
|
}
|
|
60
105
|
/**
|
|
61
106
|
* Create the default checkpoint storage.
|
|
62
107
|
*/
|
|
63
108
|
createDefaultCheckpoints() {
|
|
64
|
-
return
|
|
65
|
-
name: this._storageName,
|
|
109
|
+
return this._localforage.createInstance({
|
|
66
110
|
description: 'Offline Storage for Checkpoints',
|
|
67
111
|
storeName: 'checkpoints',
|
|
68
|
-
|
|
112
|
+
...this.defaultStorageOptions,
|
|
69
113
|
});
|
|
70
114
|
}
|
|
71
115
|
/**
|
|
@@ -112,7 +156,7 @@ export class Contents {
|
|
|
112
156
|
path: `${dirname}${name}`,
|
|
113
157
|
last_modified: created,
|
|
114
158
|
created,
|
|
115
|
-
format: '
|
|
159
|
+
format: 'json',
|
|
116
160
|
mimetype: '',
|
|
117
161
|
content: null,
|
|
118
162
|
size: undefined,
|
|
@@ -121,45 +165,55 @@ export class Contents {
|
|
|
121
165
|
};
|
|
122
166
|
break;
|
|
123
167
|
}
|
|
124
|
-
case '
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
name = name || `untitled${counter || ''}${ext}`;
|
|
168
|
+
case 'notebook': {
|
|
169
|
+
const counter = await this._incrementCounter('notebook');
|
|
170
|
+
name = name || `Untitled${counter || ''}.ipynb`;
|
|
128
171
|
file = {
|
|
129
172
|
name,
|
|
130
173
|
path: `${dirname}${name}`,
|
|
131
174
|
last_modified: created,
|
|
132
175
|
created,
|
|
133
|
-
format: '
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
size: 0,
|
|
176
|
+
format: 'json',
|
|
177
|
+
mimetype: MIME.JSON,
|
|
178
|
+
content: Private.EMPTY_NB,
|
|
179
|
+
size: JSON.stringify(Private.EMPTY_NB).length,
|
|
138
180
|
writable: true,
|
|
139
|
-
type: '
|
|
181
|
+
type: 'notebook',
|
|
140
182
|
};
|
|
141
183
|
break;
|
|
142
184
|
}
|
|
143
185
|
default: {
|
|
144
|
-
const
|
|
145
|
-
|
|
186
|
+
const ext = (_c = options === null || options === void 0 ? void 0 : options.ext) !== null && _c !== void 0 ? _c : '.txt';
|
|
187
|
+
const counter = await this._incrementCounter('file');
|
|
188
|
+
const mimetype = FILE.getType(ext) || MIME.OCTET_STREAM;
|
|
189
|
+
let format;
|
|
190
|
+
if (FILE.hasFormat(ext, 'text') || mimetype.indexOf('text') !== -1) {
|
|
191
|
+
format = 'text';
|
|
192
|
+
}
|
|
193
|
+
else if (ext.indexOf('json') !== -1 || ext.indexOf('ipynb') !== -1) {
|
|
194
|
+
format = 'json';
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
format = 'base64';
|
|
198
|
+
}
|
|
199
|
+
name = name || `untitled${counter || ''}${ext}`;
|
|
146
200
|
file = {
|
|
147
201
|
name,
|
|
148
202
|
path: `${dirname}${name}`,
|
|
149
203
|
last_modified: created,
|
|
150
204
|
created,
|
|
151
|
-
format
|
|
152
|
-
mimetype
|
|
153
|
-
content:
|
|
154
|
-
size:
|
|
205
|
+
format,
|
|
206
|
+
mimetype,
|
|
207
|
+
content: '',
|
|
208
|
+
size: 0,
|
|
155
209
|
writable: true,
|
|
156
|
-
type: '
|
|
210
|
+
type: 'file',
|
|
157
211
|
};
|
|
158
212
|
break;
|
|
159
213
|
}
|
|
160
214
|
}
|
|
161
215
|
const key = file.path;
|
|
162
|
-
await this.
|
|
216
|
+
await (await this.storage).setItem(key, file);
|
|
163
217
|
return file;
|
|
164
218
|
}
|
|
165
219
|
/**
|
|
@@ -193,7 +247,7 @@ export class Contents {
|
|
|
193
247
|
name,
|
|
194
248
|
path: toPath,
|
|
195
249
|
};
|
|
196
|
-
await this.
|
|
250
|
+
await (await this.storage).setItem(toPath, item);
|
|
197
251
|
return item;
|
|
198
252
|
}
|
|
199
253
|
/**
|
|
@@ -210,7 +264,8 @@ export class Contents {
|
|
|
210
264
|
if (path === '') {
|
|
211
265
|
return await this._getFolder(path);
|
|
212
266
|
}
|
|
213
|
-
const
|
|
267
|
+
const storage = await this.storage;
|
|
268
|
+
const item = await storage.getItem(path);
|
|
214
269
|
const serverItem = await this._getServerContents(path, options);
|
|
215
270
|
const model = (item || serverItem);
|
|
216
271
|
if (!model) {
|
|
@@ -226,8 +281,7 @@ export class Contents {
|
|
|
226
281
|
// for directories, find all files with the path as the prefix
|
|
227
282
|
if (model.type === 'directory') {
|
|
228
283
|
const contentMap = new Map();
|
|
229
|
-
await
|
|
230
|
-
const file = item;
|
|
284
|
+
await storage.iterate((file, key) => {
|
|
231
285
|
// use an additional slash to not include the directory itself
|
|
232
286
|
if (key === `${path}/${file.name}`) {
|
|
233
287
|
contentMap.set(file.name, file);
|
|
@@ -248,7 +302,7 @@ export class Contents {
|
|
|
248
302
|
last_modified: model.last_modified,
|
|
249
303
|
created: model.created,
|
|
250
304
|
format: 'json',
|
|
251
|
-
mimetype:
|
|
305
|
+
mimetype: MIME.JSON,
|
|
252
306
|
content,
|
|
253
307
|
size: undefined,
|
|
254
308
|
writable: true,
|
|
@@ -279,11 +333,12 @@ export class Contents {
|
|
|
279
333
|
path: newLocalPath,
|
|
280
334
|
last_modified: modified,
|
|
281
335
|
};
|
|
282
|
-
await this.
|
|
336
|
+
const storage = await this.storage;
|
|
337
|
+
await storage.setItem(newLocalPath, newFile);
|
|
283
338
|
// remove the old file
|
|
284
|
-
await
|
|
339
|
+
await storage.removeItem(path);
|
|
285
340
|
// remove the corresponding checkpoint
|
|
286
|
-
await this.
|
|
341
|
+
await (await this.checkpoints).removeItem(path);
|
|
287
342
|
// if a directory, recurse through all children
|
|
288
343
|
if (file.type === 'directory') {
|
|
289
344
|
let child;
|
|
@@ -304,7 +359,12 @@ export class Contents {
|
|
|
304
359
|
async save(path, options = {}) {
|
|
305
360
|
var _a;
|
|
306
361
|
path = decodeURIComponent(path);
|
|
307
|
-
|
|
362
|
+
// process the file if coming from an upload
|
|
363
|
+
const ext = PathExt.extname((_a = options.name) !== null && _a !== void 0 ? _a : '');
|
|
364
|
+
let item = await this.get(path);
|
|
365
|
+
if (!item) {
|
|
366
|
+
item = await this.newUntitled({ path, ext, type: 'file' });
|
|
367
|
+
}
|
|
308
368
|
if (!item) {
|
|
309
369
|
return null;
|
|
310
370
|
}
|
|
@@ -315,42 +375,62 @@ export class Contents {
|
|
|
315
375
|
...options,
|
|
316
376
|
last_modified: modified,
|
|
317
377
|
};
|
|
318
|
-
// process the file if coming from an upload
|
|
319
|
-
const ext = PathExt.extname((_a = options.name) !== null && _a !== void 0 ? _a : '');
|
|
320
378
|
if (options.content && options.format === 'base64') {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
379
|
+
if (ext === '.ipynb') {
|
|
380
|
+
item = {
|
|
381
|
+
...item,
|
|
382
|
+
content: JSON.parse(this.unescapeContent(options.content)),
|
|
383
|
+
format: 'json',
|
|
384
|
+
type: 'notebook',
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
else if (FILE.hasFormat(ext, 'json')) {
|
|
388
|
+
item = {
|
|
389
|
+
...item,
|
|
390
|
+
content: JSON.parse(this.unescapeContent(options.content)),
|
|
391
|
+
format: 'json',
|
|
392
|
+
type: 'file',
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
else if (FILE.hasFormat(ext, 'text')) {
|
|
396
|
+
item = {
|
|
397
|
+
...item,
|
|
398
|
+
content: this.unescapeContent(options.content),
|
|
399
|
+
format: 'text',
|
|
400
|
+
type: 'file',
|
|
401
|
+
};
|
|
402
|
+
}
|
|
330
403
|
}
|
|
331
|
-
await this.
|
|
404
|
+
await (await this.storage).setItem(path, item);
|
|
332
405
|
return item;
|
|
333
406
|
}
|
|
407
|
+
unescapeContent(content) {
|
|
408
|
+
return decodeURIComponent(escape(atob(content)));
|
|
409
|
+
}
|
|
334
410
|
/**
|
|
335
|
-
* Delete a file.
|
|
411
|
+
* Delete a file from browser storage.
|
|
412
|
+
*
|
|
413
|
+
* Has no effect on server-backed files, which will re-appear with their
|
|
414
|
+
* original timestamp.
|
|
336
415
|
*
|
|
337
416
|
* @param path - The path to the file.
|
|
338
417
|
*/
|
|
339
418
|
async delete(path) {
|
|
340
419
|
path = decodeURIComponent(path);
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
await
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
420
|
+
const slashed = `${path}/`;
|
|
421
|
+
const toDelete = (await (await this.storage).keys()).filter((key) => key === path || key.startsWith(slashed));
|
|
422
|
+
await Promise.all(toDelete.map(this.forgetPath, this));
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Remove the localForage and checkpoints for a path.
|
|
426
|
+
*
|
|
427
|
+
* @param path - The path to the file
|
|
428
|
+
*/
|
|
429
|
+
async forgetPath(path) {
|
|
430
|
+
await Promise.all([
|
|
431
|
+
(await this.storage).removeItem(path),
|
|
432
|
+
(await this.checkpoints).removeItem(path),
|
|
433
|
+
]);
|
|
354
434
|
}
|
|
355
435
|
/**
|
|
356
436
|
* Create a checkpoint for a file.
|
|
@@ -362,23 +442,21 @@ export class Contents {
|
|
|
362
442
|
*/
|
|
363
443
|
async createCheckpoint(path) {
|
|
364
444
|
var _a;
|
|
445
|
+
const checkpoints = await this.checkpoints;
|
|
365
446
|
path = decodeURIComponent(path);
|
|
366
447
|
const item = await this.get(path, { content: true });
|
|
367
448
|
if (!item) {
|
|
368
449
|
throw Error(`Could not find file with path ${path}`);
|
|
369
450
|
}
|
|
370
|
-
const copies = ((_a = (await
|
|
451
|
+
const copies = ((_a = (await checkpoints.getItem(path))) !== null && _a !== void 0 ? _a : []).filter(Boolean);
|
|
371
452
|
copies.push(item);
|
|
372
453
|
// keep only a certain amount of checkpoints per file
|
|
373
454
|
if (copies.length > N_CHECKPOINTS) {
|
|
374
455
|
copies.splice(0, copies.length - N_CHECKPOINTS);
|
|
375
456
|
}
|
|
376
|
-
await
|
|
457
|
+
await checkpoints.setItem(path, copies);
|
|
377
458
|
const id = `${copies.length - 1}`;
|
|
378
|
-
return {
|
|
379
|
-
id,
|
|
380
|
-
last_modified: item.last_modified,
|
|
381
|
-
};
|
|
459
|
+
return { id, last_modified: item.last_modified };
|
|
382
460
|
}
|
|
383
461
|
/**
|
|
384
462
|
* List available checkpoints for a file.
|
|
@@ -389,16 +467,11 @@ export class Contents {
|
|
|
389
467
|
* the file.
|
|
390
468
|
*/
|
|
391
469
|
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
|
-
});
|
|
470
|
+
const copies = (await (await this.checkpoints).getItem(path)) || [];
|
|
471
|
+
return copies.filter(Boolean).map(this.normalizeCheckpoint, this);
|
|
472
|
+
}
|
|
473
|
+
normalizeCheckpoint(model, id) {
|
|
474
|
+
return { id: id.toString(), last_modified: model.last_modified };
|
|
402
475
|
}
|
|
403
476
|
/**
|
|
404
477
|
* Restore a file to a known checkpoint state.
|
|
@@ -410,11 +483,10 @@ export class Contents {
|
|
|
410
483
|
*/
|
|
411
484
|
async restoreCheckpoint(path, checkpointID) {
|
|
412
485
|
path = decodeURIComponent(path);
|
|
413
|
-
const copies = ((await this.
|
|
414
|
-
[]);
|
|
486
|
+
const copies = ((await (await this.checkpoints).getItem(path)) || []);
|
|
415
487
|
const id = parseInt(checkpointID);
|
|
416
488
|
const item = copies[id];
|
|
417
|
-
await this.
|
|
489
|
+
await (await this.storage).setItem(path, item);
|
|
418
490
|
}
|
|
419
491
|
/**
|
|
420
492
|
* Delete a checkpoint for a file.
|
|
@@ -426,11 +498,10 @@ export class Contents {
|
|
|
426
498
|
*/
|
|
427
499
|
async deleteCheckpoint(path, checkpointID) {
|
|
428
500
|
path = decodeURIComponent(path);
|
|
429
|
-
const copies = ((await this.
|
|
430
|
-
[]);
|
|
501
|
+
const copies = ((await (await this.checkpoints).getItem(path)) || []);
|
|
431
502
|
const id = parseInt(checkpointID);
|
|
432
503
|
copies.splice(id, 1);
|
|
433
|
-
await this.
|
|
504
|
+
await (await this.checkpoints).setItem(path, copies);
|
|
434
505
|
}
|
|
435
506
|
/**
|
|
436
507
|
* retrieve the contents for this path from the union of local storage and
|
|
@@ -442,11 +513,11 @@ export class Contents {
|
|
|
442
513
|
*/
|
|
443
514
|
async _getFolder(path) {
|
|
444
515
|
const content = new Map();
|
|
445
|
-
await this.
|
|
516
|
+
const storage = await this.storage;
|
|
517
|
+
await storage.iterate((file, key) => {
|
|
446
518
|
if (key.includes('/')) {
|
|
447
519
|
return;
|
|
448
520
|
}
|
|
449
|
-
const file = item;
|
|
450
521
|
content.set(file.path, file);
|
|
451
522
|
});
|
|
452
523
|
// layer in contents that don't have local overwrites
|
|
@@ -464,7 +535,7 @@ export class Contents {
|
|
|
464
535
|
last_modified: new Date(0).toISOString(),
|
|
465
536
|
created: new Date(0).toISOString(),
|
|
466
537
|
format: 'json',
|
|
467
|
-
mimetype:
|
|
538
|
+
mimetype: MIME.JSON,
|
|
468
539
|
content: Array.from(content.values()),
|
|
469
540
|
size: undefined,
|
|
470
541
|
writable: true,
|
|
@@ -489,7 +560,7 @@ export class Contents {
|
|
|
489
560
|
last_modified: new Date(0).toISOString(),
|
|
490
561
|
created: new Date(0).toISOString(),
|
|
491
562
|
format: 'text',
|
|
492
|
-
mimetype:
|
|
563
|
+
mimetype: MIME.PLAIN_TEXT,
|
|
493
564
|
type: 'file',
|
|
494
565
|
writable: true,
|
|
495
566
|
content: null,
|
|
@@ -506,31 +577,32 @@ export class Contents {
|
|
|
506
577
|
return null;
|
|
507
578
|
}
|
|
508
579
|
const mimetype = model.mimetype || response.headers.get('Content-Type');
|
|
580
|
+
const ext = PathExt.extname(name);
|
|
509
581
|
if (model.type === 'notebook' ||
|
|
582
|
+
FILE.hasFormat(ext, 'json') ||
|
|
510
583
|
(mimetype === null || mimetype === void 0 ? void 0 : mimetype.indexOf('json')) !== -1 ||
|
|
511
584
|
path.match(/\.(ipynb|[^/]*json[^/]*)$/)) {
|
|
512
585
|
model = {
|
|
513
586
|
...model,
|
|
514
587
|
content: await response.json(),
|
|
515
588
|
format: 'json',
|
|
516
|
-
mimetype: model.mimetype ||
|
|
589
|
+
mimetype: model.mimetype || MIME.JSON,
|
|
517
590
|
};
|
|
518
591
|
}
|
|
519
|
-
else if (mimetype.indexOf('text') !== -1
|
|
520
|
-
EXTRA_TEXT_MIME_TYPES.has(mimetype)) {
|
|
592
|
+
else if (FILE.hasFormat(ext, 'text') || mimetype.indexOf('text') !== -1) {
|
|
521
593
|
model = {
|
|
522
594
|
...model,
|
|
523
595
|
content: await response.text(),
|
|
524
596
|
format: 'text',
|
|
525
|
-
mimetype: mimetype ||
|
|
597
|
+
mimetype: mimetype || MIME.PLAIN_TEXT,
|
|
526
598
|
};
|
|
527
599
|
}
|
|
528
600
|
else {
|
|
529
601
|
model = {
|
|
530
602
|
...model,
|
|
531
|
-
content: btoa(
|
|
603
|
+
content: btoa(new Uint8Array(await response.arrayBuffer()).reduce(this.reduceBytesToString, '')),
|
|
532
604
|
format: 'base64',
|
|
533
|
-
mimetype: mimetype ||
|
|
605
|
+
mimetype: mimetype || MIME.OCTET_STREAM,
|
|
534
606
|
};
|
|
535
607
|
}
|
|
536
608
|
}
|
|
@@ -557,7 +629,7 @@ export class Contents {
|
|
|
557
629
|
}
|
|
558
630
|
}
|
|
559
631
|
catch (err) {
|
|
560
|
-
console.warn(`don't worry, about ${err}... nothing's broken.
|
|
632
|
+
console.warn(`don't worry, about ${err}... nothing's broken. If there had been a
|
|
561
633
|
file at ${apiURL}, you might see some more files.`);
|
|
562
634
|
}
|
|
563
635
|
this._serverContents.set(path, content);
|
|
@@ -572,9 +644,10 @@ export class Contents {
|
|
|
572
644
|
*/
|
|
573
645
|
async _incrementCounter(type) {
|
|
574
646
|
var _a;
|
|
575
|
-
const
|
|
647
|
+
const counters = await this.counters;
|
|
648
|
+
const current = (_a = (await counters.getItem(type))) !== null && _a !== void 0 ? _a : -1;
|
|
576
649
|
const counter = current + 1;
|
|
577
|
-
await
|
|
650
|
+
await counters.setItem(type, counter);
|
|
578
651
|
return counter;
|
|
579
652
|
}
|
|
580
653
|
}
|
package/lib/drivefs.d.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
export declare const DIR_MODE = 16895;
|
|
2
|
+
export declare const FILE_MODE = 33206;
|
|
3
|
+
export declare const SEEK_CUR = 1;
|
|
4
|
+
export declare const SEEK_END = 2;
|
|
5
|
+
export declare const DRIVE_SEPARATOR = ":";
|
|
6
|
+
export interface IStats {
|
|
7
|
+
dev: number;
|
|
8
|
+
ino: number;
|
|
9
|
+
mode: number;
|
|
10
|
+
nlink: number;
|
|
11
|
+
uid: number;
|
|
12
|
+
gid: number;
|
|
13
|
+
rdev: number;
|
|
14
|
+
size: number;
|
|
15
|
+
blksize: number;
|
|
16
|
+
blocks: number;
|
|
17
|
+
atime: Date;
|
|
18
|
+
mtime: Date;
|
|
19
|
+
ctime: Date;
|
|
20
|
+
timestamp?: number;
|
|
21
|
+
}
|
|
22
|
+
export interface IEmscriptenFSNode {
|
|
23
|
+
name: string;
|
|
24
|
+
mode: number;
|
|
25
|
+
parent: IEmscriptenFSNode;
|
|
26
|
+
mount: {
|
|
27
|
+
opts: {
|
|
28
|
+
root: string;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
stream_ops: IEmscriptenStreamOps;
|
|
32
|
+
node_ops: IEmscriptenNodeOps;
|
|
33
|
+
timestamp: number;
|
|
34
|
+
}
|
|
35
|
+
export interface IEmscriptenStream {
|
|
36
|
+
node: IEmscriptenFSNode;
|
|
37
|
+
nfd: any;
|
|
38
|
+
flags: string;
|
|
39
|
+
position: number;
|
|
40
|
+
file?: DriveFS.IFile;
|
|
41
|
+
}
|
|
42
|
+
export interface IEmscriptenNodeOps {
|
|
43
|
+
getattr(node: IEmscriptenFSNode): IStats;
|
|
44
|
+
setattr(node: IEmscriptenFSNode, attr: IStats): void;
|
|
45
|
+
lookup(parent: IEmscriptenFSNode, name: string): IEmscriptenFSNode;
|
|
46
|
+
mknod(parent: IEmscriptenFSNode, name: string, mode: number, dev: any): IEmscriptenFSNode;
|
|
47
|
+
rename(oldNode: IEmscriptenFSNode, newDir: IEmscriptenFSNode, newName: string): void;
|
|
48
|
+
unlink(parent: IEmscriptenFSNode, name: string): void;
|
|
49
|
+
rmdir(parent: IEmscriptenFSNode, name: string): void;
|
|
50
|
+
readdir(node: IEmscriptenFSNode): string[];
|
|
51
|
+
symlink(parent: IEmscriptenFSNode, newName: string, oldPath: string): void;
|
|
52
|
+
readlink(node: IEmscriptenFSNode): string;
|
|
53
|
+
}
|
|
54
|
+
export interface IEmscriptenStreamOps {
|
|
55
|
+
open(stream: IEmscriptenStream): void;
|
|
56
|
+
close(stream: IEmscriptenStream): void;
|
|
57
|
+
read(stream: IEmscriptenStream, buffer: Uint8Array, offset: number, length: number, position: number): number;
|
|
58
|
+
write(stream: IEmscriptenStream, buffer: Uint8Array, offset: number, length: number, position: number): number;
|
|
59
|
+
llseek(stream: IEmscriptenStream, offset: number, whence: number): number;
|
|
60
|
+
}
|
|
61
|
+
export declare class DriveFSEmscriptenStreamOps implements IEmscriptenStreamOps {
|
|
62
|
+
private fs;
|
|
63
|
+
constructor(fs: DriveFS);
|
|
64
|
+
open(stream: IEmscriptenStream): void;
|
|
65
|
+
close(stream: IEmscriptenStream): void;
|
|
66
|
+
read(stream: IEmscriptenStream, buffer: Uint8Array, offset: number, length: number, position: number): number;
|
|
67
|
+
write(stream: IEmscriptenStream, buffer: Uint8Array, offset: number, length: number, position: number): number;
|
|
68
|
+
llseek(stream: IEmscriptenStream, offset: number, whence: number): number;
|
|
69
|
+
}
|
|
70
|
+
export declare class DriveFSEmscriptenNodeOps implements IEmscriptenNodeOps {
|
|
71
|
+
private fs;
|
|
72
|
+
constructor(fs: DriveFS);
|
|
73
|
+
getattr(node: IEmscriptenFSNode): IStats;
|
|
74
|
+
setattr(node: IEmscriptenFSNode, attr: IStats): void;
|
|
75
|
+
lookup(parent: IEmscriptenFSNode, name: string): IEmscriptenFSNode;
|
|
76
|
+
mknod(parent: IEmscriptenFSNode, name: string, mode: number, dev: any): IEmscriptenFSNode;
|
|
77
|
+
rename(oldNode: IEmscriptenFSNode, newDir: IEmscriptenFSNode, newName: string): void;
|
|
78
|
+
unlink(parent: IEmscriptenFSNode, name: string): void;
|
|
79
|
+
rmdir(parent: IEmscriptenFSNode, name: string): void;
|
|
80
|
+
readdir(node: IEmscriptenFSNode): string[];
|
|
81
|
+
symlink(parent: IEmscriptenFSNode, newName: string, oldPath: string): void;
|
|
82
|
+
readlink(node: IEmscriptenFSNode): string;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Wrap ServiceWorker requests for an Emscripten-compatible synchronous API.
|
|
86
|
+
*/
|
|
87
|
+
export declare class ContentsAPI {
|
|
88
|
+
constructor(baseUrl: string, driveName: string, mountpoint: string, FS: any, ERRNO_CODES: any);
|
|
89
|
+
request(method: 'GET' | 'POST' | 'PUT' | 'DELETE', path: string, data?: string | null): any;
|
|
90
|
+
lookup(path: string): DriveFS.ILookup;
|
|
91
|
+
getmode(path: string): number;
|
|
92
|
+
mknod(path: string, mode: number): any;
|
|
93
|
+
rename(oldPath: string, newPath: string): void;
|
|
94
|
+
readdir(path: string): string[];
|
|
95
|
+
rmdir(path: string): void;
|
|
96
|
+
get(path: string): DriveFS.IFile;
|
|
97
|
+
put(path: string, value: DriveFS.IFile): any;
|
|
98
|
+
getattr(path: string): IStats;
|
|
99
|
+
/**
|
|
100
|
+
* Normalize a Path by making it compliant for the content manager
|
|
101
|
+
*
|
|
102
|
+
* @param path: the path relatively to the Emscripten drive
|
|
103
|
+
*/
|
|
104
|
+
normalizePath(path: string): string;
|
|
105
|
+
/**
|
|
106
|
+
* Get the api/drive endpoint
|
|
107
|
+
*/
|
|
108
|
+
get endpoint(): string;
|
|
109
|
+
private _baseUrl;
|
|
110
|
+
private _driveName;
|
|
111
|
+
private _mountpoint;
|
|
112
|
+
private FS;
|
|
113
|
+
private ERRNO_CODES;
|
|
114
|
+
}
|
|
115
|
+
export declare class DriveFS {
|
|
116
|
+
FS: any;
|
|
117
|
+
API: ContentsAPI;
|
|
118
|
+
PATH: DriveFS.IPath;
|
|
119
|
+
ERRNO_CODES: any;
|
|
120
|
+
driveName: string;
|
|
121
|
+
constructor(options: DriveFS.IOptions);
|
|
122
|
+
node_ops: IEmscriptenNodeOps;
|
|
123
|
+
stream_ops: IEmscriptenStreamOps;
|
|
124
|
+
mount(mount: any): IEmscriptenFSNode;
|
|
125
|
+
createNode(parent: IEmscriptenFSNode | null, name: string, mode: number, dev?: any): IEmscriptenFSNode;
|
|
126
|
+
getMode(path: string): number;
|
|
127
|
+
realPath(node: IEmscriptenFSNode): string;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* A namespace for DriveFS configurations, etc.
|
|
131
|
+
*/
|
|
132
|
+
export declare namespace DriveFS {
|
|
133
|
+
/**
|
|
134
|
+
* A file representation;
|
|
135
|
+
*/
|
|
136
|
+
interface IFile {
|
|
137
|
+
data: Uint8Array;
|
|
138
|
+
format: 'json' | 'text' | 'base64';
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The response to a lookup request;
|
|
142
|
+
*/
|
|
143
|
+
interface ILookup {
|
|
144
|
+
ok: boolean;
|
|
145
|
+
mode: number;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The emscripten FS Path API;
|
|
149
|
+
*/
|
|
150
|
+
interface IPath {
|
|
151
|
+
basename: (path: string) => string;
|
|
152
|
+
dirname: (path: string) => string;
|
|
153
|
+
join: (...parts: string[]) => string;
|
|
154
|
+
join2: (l: string, r: string) => string;
|
|
155
|
+
normalize: (path: string) => string;
|
|
156
|
+
splitPath: (filename: string) => string;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Initialization options for a drive;
|
|
160
|
+
*/
|
|
161
|
+
interface IOptions {
|
|
162
|
+
FS: any;
|
|
163
|
+
PATH: IPath;
|
|
164
|
+
ERRNO_CODES: any;
|
|
165
|
+
baseUrl: string;
|
|
166
|
+
driveName: string;
|
|
167
|
+
mountpoint: string;
|
|
168
|
+
}
|
|
169
|
+
}
|
package/lib/drivefs.js
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
// Types and implementation inspired from https://github.com/jvilk/BrowserFS
|
|
2
|
+
// LICENSE: https://github.com/jvilk/BrowserFS/blob/8977a704ea469d05daf857e4818bef1f4f498326/LICENSE
|
|
3
|
+
// And from https://github.com/gzuidhof/starboard-notebook
|
|
4
|
+
// LICENSE: https://github.com/gzuidhof/starboard-notebook/blob/cd8d3fc30af4bd29cdd8f6b8c207df8138f5d5dd/LICENSE
|
|
5
|
+
export const DIR_MODE = 16895; // 040777
|
|
6
|
+
export const FILE_MODE = 33206; // 100666
|
|
7
|
+
export const SEEK_CUR = 1;
|
|
8
|
+
export const SEEK_END = 2;
|
|
9
|
+
export const DRIVE_SEPARATOR = ':';
|
|
10
|
+
const encoder = new TextEncoder();
|
|
11
|
+
const decoder = new TextDecoder('utf-8');
|
|
12
|
+
// Mapping flag -> do we need to overwrite the file upon closing it
|
|
13
|
+
const flagNeedsWrite = {
|
|
14
|
+
0 /*O_RDONLY*/: false,
|
|
15
|
+
1 /*O_WRONLY*/: true,
|
|
16
|
+
2 /*O_RDWR*/: true,
|
|
17
|
+
64 /*O_CREAT*/: true,
|
|
18
|
+
65 /*O_WRONLY|O_CREAT*/: true,
|
|
19
|
+
66 /*O_RDWR|O_CREAT*/: true,
|
|
20
|
+
129 /*O_WRONLY|O_EXCL*/: true,
|
|
21
|
+
193 /*O_WRONLY|O_CREAT|O_EXCL*/: true,
|
|
22
|
+
514 /*O_RDWR|O_TRUNC*/: true,
|
|
23
|
+
577 /*O_WRONLY|O_CREAT|O_TRUNC*/: true,
|
|
24
|
+
578 /*O_CREAT|O_RDWR|O_TRUNC*/: true,
|
|
25
|
+
705 /*O_WRONLY|O_CREAT|O_EXCL|O_TRUNC*/: true,
|
|
26
|
+
706 /*O_RDWR|O_CREAT|O_EXCL|O_TRUNC*/: true,
|
|
27
|
+
1024 /*O_APPEND*/: true,
|
|
28
|
+
1025 /*O_WRONLY|O_APPEND*/: true,
|
|
29
|
+
1026 /*O_RDWR|O_APPEND*/: true,
|
|
30
|
+
1089 /*O_WRONLY|O_CREAT|O_APPEND*/: true,
|
|
31
|
+
1090 /*O_RDWR|O_CREAT|O_APPEND*/: true,
|
|
32
|
+
1153 /*O_WRONLY|O_EXCL|O_APPEND*/: true,
|
|
33
|
+
1154 /*O_RDWR|O_EXCL|O_APPEND*/: true,
|
|
34
|
+
1217 /*O_WRONLY|O_CREAT|O_EXCL|O_APPEND*/: true,
|
|
35
|
+
1218 /*O_RDWR|O_CREAT|O_EXCL|O_APPEND*/: true,
|
|
36
|
+
4096 /*O_RDONLY|O_DSYNC*/: true,
|
|
37
|
+
4098 /*O_RDWR|O_DSYNC*/: true,
|
|
38
|
+
};
|
|
39
|
+
export class DriveFSEmscriptenStreamOps {
|
|
40
|
+
constructor(fs) {
|
|
41
|
+
this.fs = fs;
|
|
42
|
+
}
|
|
43
|
+
open(stream) {
|
|
44
|
+
const path = this.fs.realPath(stream.node);
|
|
45
|
+
if (this.fs.FS.isFile(stream.node.mode)) {
|
|
46
|
+
stream.file = this.fs.API.get(path);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
close(stream) {
|
|
50
|
+
if (!this.fs.FS.isFile(stream.node.mode) || !stream.file) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const path = this.fs.realPath(stream.node);
|
|
54
|
+
const flags = stream.flags;
|
|
55
|
+
let parsedFlags = typeof flags === 'string' ? parseInt(flags, 10) : flags;
|
|
56
|
+
parsedFlags &= 0x1fff;
|
|
57
|
+
let needsWrite = true;
|
|
58
|
+
if (parsedFlags in flagNeedsWrite) {
|
|
59
|
+
needsWrite = flagNeedsWrite[parsedFlags];
|
|
60
|
+
}
|
|
61
|
+
if (needsWrite) {
|
|
62
|
+
this.fs.API.put(path, stream.file);
|
|
63
|
+
stream.file = undefined;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
read(stream, buffer, offset, length, position) {
|
|
67
|
+
var _a;
|
|
68
|
+
if (length <= 0 || stream.file === undefined) {
|
|
69
|
+
return 0;
|
|
70
|
+
}
|
|
71
|
+
const size = Math.min(((_a = stream.file.data.length) !== null && _a !== void 0 ? _a : 0) - position, length);
|
|
72
|
+
try {
|
|
73
|
+
buffer.set(stream.file.data.subarray(position, position + size), offset);
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
throw new this.fs.FS.ErrnoError(this.fs.ERRNO_CODES['EPERM']);
|
|
77
|
+
}
|
|
78
|
+
return size;
|
|
79
|
+
}
|
|
80
|
+
write(stream, buffer, offset, length, position) {
|
|
81
|
+
var _a, _b;
|
|
82
|
+
if (length <= 0 || stream.file === undefined) {
|
|
83
|
+
return 0;
|
|
84
|
+
}
|
|
85
|
+
stream.node.timestamp = Date.now();
|
|
86
|
+
try {
|
|
87
|
+
if (position + length > ((_b = (_a = stream.file) === null || _a === void 0 ? void 0 : _a.data.length) !== null && _b !== void 0 ? _b : 0)) {
|
|
88
|
+
const oldData = stream.file.data ? stream.file.data : new Uint8Array();
|
|
89
|
+
stream.file.data = new Uint8Array(position + length);
|
|
90
|
+
stream.file.data.set(oldData);
|
|
91
|
+
}
|
|
92
|
+
stream.file.data.set(buffer.subarray(offset, offset + length), position);
|
|
93
|
+
return length;
|
|
94
|
+
}
|
|
95
|
+
catch (e) {
|
|
96
|
+
throw new this.fs.FS.ErrnoError(this.fs.ERRNO_CODES['EPERM']);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
llseek(stream, offset, whence) {
|
|
100
|
+
let position = offset;
|
|
101
|
+
if (whence === SEEK_CUR) {
|
|
102
|
+
position += stream.position;
|
|
103
|
+
}
|
|
104
|
+
else if (whence === SEEK_END) {
|
|
105
|
+
if (this.fs.FS.isFile(stream.node.mode)) {
|
|
106
|
+
if (stream.file !== undefined) {
|
|
107
|
+
position += stream.file.data.length;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
throw new this.fs.FS.ErrnoError(this.fs.ERRNO_CODES['EPERM']);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (position < 0) {
|
|
115
|
+
throw new this.fs.FS.ErrnoError(this.fs.ERRNO_CODES['EINVAL']);
|
|
116
|
+
}
|
|
117
|
+
return position;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
export class DriveFSEmscriptenNodeOps {
|
|
121
|
+
constructor(fs) {
|
|
122
|
+
this.fs = fs;
|
|
123
|
+
}
|
|
124
|
+
getattr(node) {
|
|
125
|
+
return this.fs.API.getattr(this.fs.realPath(node));
|
|
126
|
+
}
|
|
127
|
+
setattr(node, attr) {
|
|
128
|
+
// TODO
|
|
129
|
+
}
|
|
130
|
+
lookup(parent, name) {
|
|
131
|
+
const path = this.fs.PATH.join2(this.fs.realPath(parent), name);
|
|
132
|
+
const result = this.fs.API.lookup(path);
|
|
133
|
+
if (!result.ok) {
|
|
134
|
+
throw this.fs.FS.genericErrors[this.fs.ERRNO_CODES['ENOENT']];
|
|
135
|
+
}
|
|
136
|
+
return this.fs.createNode(parent, name, result.mode);
|
|
137
|
+
}
|
|
138
|
+
mknod(parent, name, mode, dev) {
|
|
139
|
+
const path = this.fs.PATH.join2(this.fs.realPath(parent), name);
|
|
140
|
+
this.fs.API.mknod(path, mode);
|
|
141
|
+
return this.fs.createNode(parent, name, mode, dev);
|
|
142
|
+
}
|
|
143
|
+
rename(oldNode, newDir, newName) {
|
|
144
|
+
this.fs.API.rename(oldNode.parent
|
|
145
|
+
? this.fs.PATH.join2(this.fs.realPath(oldNode.parent), oldNode.name)
|
|
146
|
+
: oldNode.name, this.fs.PATH.join2(this.fs.realPath(newDir), newName));
|
|
147
|
+
// Updating the in-memory node
|
|
148
|
+
oldNode.name = newName;
|
|
149
|
+
oldNode.parent = newDir;
|
|
150
|
+
}
|
|
151
|
+
unlink(parent, name) {
|
|
152
|
+
this.fs.API.rmdir(this.fs.PATH.join2(this.fs.realPath(parent), name));
|
|
153
|
+
}
|
|
154
|
+
rmdir(parent, name) {
|
|
155
|
+
this.fs.API.rmdir(this.fs.PATH.join2(this.fs.realPath(parent), name));
|
|
156
|
+
}
|
|
157
|
+
readdir(node) {
|
|
158
|
+
return this.fs.API.readdir(this.fs.realPath(node));
|
|
159
|
+
}
|
|
160
|
+
symlink(parent, newName, oldPath) {
|
|
161
|
+
throw new this.fs.FS.ErrnoError(this.fs.ERRNO_CODES['EPERM']);
|
|
162
|
+
}
|
|
163
|
+
readlink(node) {
|
|
164
|
+
throw new this.fs.FS.ErrnoError(this.fs.ERRNO_CODES['EPERM']);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Wrap ServiceWorker requests for an Emscripten-compatible synchronous API.
|
|
169
|
+
*/
|
|
170
|
+
export class ContentsAPI {
|
|
171
|
+
constructor(baseUrl, driveName, mountpoint, FS, ERRNO_CODES) {
|
|
172
|
+
this._baseUrl = baseUrl;
|
|
173
|
+
this._driveName = driveName;
|
|
174
|
+
this._mountpoint = mountpoint;
|
|
175
|
+
this.FS = FS;
|
|
176
|
+
this.ERRNO_CODES = ERRNO_CODES;
|
|
177
|
+
}
|
|
178
|
+
request(method, path, data = null) {
|
|
179
|
+
const xhr = new XMLHttpRequest();
|
|
180
|
+
xhr.open(method, encodeURI(`${this.endpoint}${path}`), false);
|
|
181
|
+
try {
|
|
182
|
+
if (data === null) {
|
|
183
|
+
xhr.send();
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
xhr.send(data);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
catch (e) {
|
|
190
|
+
console.error(e);
|
|
191
|
+
}
|
|
192
|
+
if (xhr.status >= 400) {
|
|
193
|
+
throw new this.FS.ErrnoError(this.ERRNO_CODES['EINVAL']);
|
|
194
|
+
}
|
|
195
|
+
return JSON.parse(xhr.responseText);
|
|
196
|
+
}
|
|
197
|
+
lookup(path) {
|
|
198
|
+
return this.request('GET', `${this.normalizePath(path)}?m=lookup`);
|
|
199
|
+
}
|
|
200
|
+
getmode(path) {
|
|
201
|
+
return Number.parseInt(this.request('GET', `${this.normalizePath(path)}?m=getmode`));
|
|
202
|
+
}
|
|
203
|
+
mknod(path, mode) {
|
|
204
|
+
return this.request('GET', `${this.normalizePath(path)}?m=mknod&args=${mode}`);
|
|
205
|
+
}
|
|
206
|
+
rename(oldPath, newPath) {
|
|
207
|
+
return this.request('GET', `${this.normalizePath(oldPath)}?m=rename&args=${this.normalizePath(newPath)}`);
|
|
208
|
+
}
|
|
209
|
+
readdir(path) {
|
|
210
|
+
const dirlist = this.request('GET', `${this.normalizePath(path)}?m=readdir`);
|
|
211
|
+
dirlist.push('.');
|
|
212
|
+
dirlist.push('..');
|
|
213
|
+
return dirlist;
|
|
214
|
+
}
|
|
215
|
+
rmdir(path) {
|
|
216
|
+
return this.request('GET', `${this.normalizePath(path)}?m=rmdir`);
|
|
217
|
+
}
|
|
218
|
+
get(path) {
|
|
219
|
+
const response = this.request('GET', `${this.normalizePath(path)}?m=get`);
|
|
220
|
+
const serializedContent = response.content;
|
|
221
|
+
const format = response.format;
|
|
222
|
+
switch (format) {
|
|
223
|
+
case 'json':
|
|
224
|
+
case 'text':
|
|
225
|
+
return {
|
|
226
|
+
data: encoder.encode(serializedContent),
|
|
227
|
+
format,
|
|
228
|
+
};
|
|
229
|
+
case 'base64': {
|
|
230
|
+
const binString = atob(serializedContent);
|
|
231
|
+
const len = binString.length;
|
|
232
|
+
const data = new Uint8Array(len);
|
|
233
|
+
for (let i = 0; i < len; i++) {
|
|
234
|
+
data[i] = binString.charCodeAt(i);
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
data,
|
|
238
|
+
format,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
default:
|
|
242
|
+
throw new this.FS.ErrnoError(this.ERRNO_CODES['ENOENT']);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
put(path, value) {
|
|
246
|
+
switch (value.format) {
|
|
247
|
+
case 'json':
|
|
248
|
+
case 'text':
|
|
249
|
+
return this.request('PUT', `${this.normalizePath(path)}?m=put&args=${value.format}`, decoder.decode(value.data));
|
|
250
|
+
case 'base64': {
|
|
251
|
+
let binary = '';
|
|
252
|
+
for (let i = 0; i < value.data.byteLength; i++) {
|
|
253
|
+
binary += String.fromCharCode(value.data[i]);
|
|
254
|
+
}
|
|
255
|
+
return this.request('PUT', `${this.normalizePath(path)}?m=put&args=${value.format}`, btoa(binary));
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
getattr(path) {
|
|
260
|
+
const stats = this.request('GET', `${this.normalizePath(path)}?m=getattr`);
|
|
261
|
+
// Turn datetimes into proper objects
|
|
262
|
+
stats.atime = new Date(stats.atime);
|
|
263
|
+
stats.mtime = new Date(stats.mtime);
|
|
264
|
+
stats.ctime = new Date(stats.ctime);
|
|
265
|
+
return stats;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Normalize a Path by making it compliant for the content manager
|
|
269
|
+
*
|
|
270
|
+
* @param path: the path relatively to the Emscripten drive
|
|
271
|
+
*/
|
|
272
|
+
normalizePath(path) {
|
|
273
|
+
// Remove mountpoint prefix
|
|
274
|
+
if (path.startsWith(this._mountpoint)) {
|
|
275
|
+
path = path.slice(this._mountpoint.length);
|
|
276
|
+
}
|
|
277
|
+
// Add JupyterLab drive name
|
|
278
|
+
if (this._driveName) {
|
|
279
|
+
path = `${this._driveName}${DRIVE_SEPARATOR}${path}`;
|
|
280
|
+
}
|
|
281
|
+
return path;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Get the api/drive endpoint
|
|
285
|
+
*/
|
|
286
|
+
get endpoint() {
|
|
287
|
+
return `${this._baseUrl}api/drive/`;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
export class DriveFS {
|
|
291
|
+
constructor(options) {
|
|
292
|
+
this.FS = options.FS;
|
|
293
|
+
this.PATH = options.PATH;
|
|
294
|
+
this.ERRNO_CODES = options.ERRNO_CODES;
|
|
295
|
+
this.API = new ContentsAPI(options.baseUrl, options.driveName, options.mountpoint, this.FS, this.ERRNO_CODES);
|
|
296
|
+
this.driveName = options.driveName;
|
|
297
|
+
this.node_ops = new DriveFSEmscriptenNodeOps(this);
|
|
298
|
+
this.stream_ops = new DriveFSEmscriptenStreamOps(this);
|
|
299
|
+
}
|
|
300
|
+
mount(mount) {
|
|
301
|
+
return this.createNode(null, mount.mountpoint, DIR_MODE | 511, 0);
|
|
302
|
+
}
|
|
303
|
+
createNode(parent, name, mode, dev) {
|
|
304
|
+
const FS = this.FS;
|
|
305
|
+
if (!FS.isDir(mode) && !FS.isFile(mode)) {
|
|
306
|
+
throw new FS.ErrnoError(this.ERRNO_CODES['EINVAL']);
|
|
307
|
+
}
|
|
308
|
+
const node = FS.createNode(parent, name, mode, dev);
|
|
309
|
+
node.node_ops = this.node_ops;
|
|
310
|
+
node.stream_ops = this.stream_ops;
|
|
311
|
+
return node;
|
|
312
|
+
}
|
|
313
|
+
getMode(path) {
|
|
314
|
+
return this.API.getmode(path);
|
|
315
|
+
}
|
|
316
|
+
realPath(node) {
|
|
317
|
+
const parts = [];
|
|
318
|
+
let currentNode = node;
|
|
319
|
+
parts.push(currentNode.name);
|
|
320
|
+
while (currentNode.parent !== currentNode) {
|
|
321
|
+
currentNode = currentNode.parent;
|
|
322
|
+
parts.push(currentNode.name);
|
|
323
|
+
}
|
|
324
|
+
parts.reverse();
|
|
325
|
+
return this.PATH.join.apply(null, parts);
|
|
326
|
+
}
|
|
327
|
+
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
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,24 @@ 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 JSON = "application/json";
|
|
111
|
+
const PLAIN_TEXT = "text/plain";
|
|
112
|
+
const OCTET_STREAM = "octet/stream";
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* A namespace for file constructs.
|
|
116
|
+
*/
|
|
117
|
+
export declare namespace FILE {
|
|
118
|
+
/**
|
|
119
|
+
* Get a mimetype (or fallback).
|
|
120
|
+
*/
|
|
121
|
+
function getType(ext: string, defaultType?: string | null): string;
|
|
122
|
+
/**
|
|
123
|
+
* Determine whether the given extension matches a given fileFormat.
|
|
124
|
+
*/
|
|
125
|
+
function hasFormat(ext: string, fileFormat: 'base64' | 'text' | 'json'): boolean;
|
|
126
|
+
}
|
package/lib/tokens.js
CHANGED
|
@@ -1,5 +1,59 @@
|
|
|
1
|
+
import { PageConfig } from '@jupyterlab/coreutils';
|
|
2
|
+
import mime from 'mime';
|
|
1
3
|
import { Token } from '@lumino/coreutils';
|
|
2
4
|
/**
|
|
3
5
|
* The token for the settings service.
|
|
4
6
|
*/
|
|
5
7
|
export const IContents = new Token('@jupyterlite/contents:IContents');
|
|
8
|
+
/**
|
|
9
|
+
* Commonly-used mimetypes
|
|
10
|
+
*/
|
|
11
|
+
export var MIME;
|
|
12
|
+
(function (MIME) {
|
|
13
|
+
MIME.JSON = 'application/json';
|
|
14
|
+
MIME.PLAIN_TEXT = 'text/plain';
|
|
15
|
+
MIME.OCTET_STREAM = 'octet/stream';
|
|
16
|
+
})(MIME || (MIME = {}));
|
|
17
|
+
/**
|
|
18
|
+
* A namespace for file constructs.
|
|
19
|
+
*/
|
|
20
|
+
export var FILE;
|
|
21
|
+
(function (FILE) {
|
|
22
|
+
/**
|
|
23
|
+
* Build-time configured file types.
|
|
24
|
+
*/
|
|
25
|
+
const TYPES = JSON.parse(PageConfig.getOption('fileTypes') || '{}');
|
|
26
|
+
/**
|
|
27
|
+
* Get a mimetype (or fallback).
|
|
28
|
+
*/
|
|
29
|
+
function getType(ext, defaultType = null) {
|
|
30
|
+
ext = ext.toLowerCase();
|
|
31
|
+
for (const fileType of Object.values(TYPES)) {
|
|
32
|
+
for (const fileExt of fileType.extensions || []) {
|
|
33
|
+
if (fileExt === ext && fileType.mimeTypes && fileType.mimeTypes.length) {
|
|
34
|
+
return fileType.mimeTypes[0];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return mime.getType(ext) || defaultType || MIME.OCTET_STREAM;
|
|
39
|
+
}
|
|
40
|
+
FILE.getType = getType;
|
|
41
|
+
/**
|
|
42
|
+
* Determine whether the given extension matches a given fileFormat.
|
|
43
|
+
*/
|
|
44
|
+
function hasFormat(ext, fileFormat) {
|
|
45
|
+
ext = ext.toLowerCase();
|
|
46
|
+
for (const fileType of Object.values(TYPES)) {
|
|
47
|
+
if (fileType.fileFormat !== fileFormat) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
for (const fileExt of fileType.extensions || []) {
|
|
51
|
+
if (fileExt === ext) {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
FILE.hasFormat = hasFormat;
|
|
59
|
+
})(FILE || (FILE = {}));
|
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.10",
|
|
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
|
|
46
|
-
"@jupyterlab/services": "~6.3
|
|
45
|
+
"@jupyterlab/nbformat": "~3.4.3",
|
|
46
|
+
"@jupyterlab/services": "~6.4.3",
|
|
47
|
+
"@jupyterlite/localforage": "^0.1.0-beta.10",
|
|
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
|
|
55
|
+
"@jupyterlab/testutils": "~3.4.3",
|
|
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",
|