@jupyterlite/contents 0.1.0-alpha.9 → 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 +90 -94
- package/lib/contents.js +362 -323
- 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 +117 -2
- package/lib/tokens.js +54 -0
- package/package.json +10 -8
package/lib/contents.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
|
|
2
|
-
import { ServerConnection } from '@jupyterlab/services';
|
|
3
2
|
import { PathExt } from '@jupyterlab/coreutils';
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
3
|
+
import { MIME, FILE } from './tokens';
|
|
4
|
+
import { PromiseDelegate } from '@lumino/coreutils';
|
|
6
5
|
/**
|
|
7
6
|
* The name of the local storage.
|
|
8
7
|
*/
|
|
9
|
-
const
|
|
8
|
+
const DEFAULT_STORAGE_NAME = 'JupyterLite Storage';
|
|
10
9
|
/**
|
|
11
10
|
* The number of checkpoints to save.
|
|
12
11
|
*/
|
|
@@ -15,53 +14,103 @@ const N_CHECKPOINTS = 5;
|
|
|
15
14
|
* A class to handle requests to /api/contents
|
|
16
15
|
*/
|
|
17
16
|
export class Contents {
|
|
18
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Construct a new localForage-powered contents provider
|
|
19
|
+
*/
|
|
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
|
+
};
|
|
19
27
|
this._serverContents = new Map();
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
28
|
+
this._storageName = DEFAULT_STORAGE_NAME;
|
|
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() {
|
|
46
|
+
this._storage = this.createDefaultStorage();
|
|
47
|
+
this._counters = this.createDefaultCounters();
|
|
48
|
+
this._checkpoints = this.createDefaultCheckpoints();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A promise that resolves once all storage is fully initialized.
|
|
52
|
+
*/
|
|
53
|
+
get ready() {
|
|
54
|
+
return this._ready.promise;
|
|
40
55
|
}
|
|
41
56
|
/**
|
|
42
|
-
* A
|
|
57
|
+
* A lazy reference to the underlying storage.
|
|
43
58
|
*/
|
|
44
|
-
get
|
|
45
|
-
return this.
|
|
59
|
+
get storage() {
|
|
60
|
+
return this.ready.then(() => this._storage);
|
|
46
61
|
}
|
|
47
62
|
/**
|
|
48
|
-
*
|
|
63
|
+
* A lazy reference to the underlying counters.
|
|
49
64
|
*/
|
|
50
|
-
get
|
|
51
|
-
return this.
|
|
65
|
+
get counters() {
|
|
66
|
+
return this.ready.then(() => this._counters);
|
|
52
67
|
}
|
|
53
68
|
/**
|
|
54
|
-
*
|
|
69
|
+
* A lazy reference to the underlying checkpoints.
|
|
55
70
|
*/
|
|
56
|
-
get
|
|
57
|
-
|
|
58
|
-
return ServerConnection.makeSettings();
|
|
71
|
+
get checkpoints() {
|
|
72
|
+
return this.ready.then(() => this._checkpoints);
|
|
59
73
|
}
|
|
60
74
|
/**
|
|
61
|
-
*
|
|
75
|
+
* Get default options for localForage instances
|
|
62
76
|
*/
|
|
63
|
-
|
|
64
|
-
|
|
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
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Initialize the default storage for contents.
|
|
87
|
+
*/
|
|
88
|
+
createDefaultStorage() {
|
|
89
|
+
return this._localforage.createInstance({
|
|
90
|
+
description: 'Offline Storage for Notebooks and Files',
|
|
91
|
+
storeName: 'files',
|
|
92
|
+
...this.defaultStorageOptions,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Initialize the default storage for counting file suffixes.
|
|
97
|
+
*/
|
|
98
|
+
createDefaultCounters() {
|
|
99
|
+
return this._localforage.createInstance({
|
|
100
|
+
description: 'Store the current file suffix counters',
|
|
101
|
+
storeName: 'counters',
|
|
102
|
+
...this.defaultStorageOptions,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Create the default checkpoint storage.
|
|
107
|
+
*/
|
|
108
|
+
createDefaultCheckpoints() {
|
|
109
|
+
return this._localforage.createInstance({
|
|
110
|
+
description: 'Offline Storage for Checkpoints',
|
|
111
|
+
storeName: 'checkpoints',
|
|
112
|
+
...this.defaultStorageOptions,
|
|
113
|
+
});
|
|
65
114
|
}
|
|
66
115
|
/**
|
|
67
116
|
* Create a new untitled file or directory in the specified directory path.
|
|
@@ -75,66 +124,96 @@ export class Contents {
|
|
|
75
124
|
const path = (_a = options === null || options === void 0 ? void 0 : options.path) !== null && _a !== void 0 ? _a : '';
|
|
76
125
|
const type = (_b = options === null || options === void 0 ? void 0 : options.type) !== null && _b !== void 0 ? _b : 'notebook';
|
|
77
126
|
const created = new Date().toISOString();
|
|
78
|
-
|
|
79
|
-
|
|
127
|
+
let dirname = PathExt.dirname(path);
|
|
128
|
+
const basename = PathExt.basename(path);
|
|
129
|
+
const extname = PathExt.extname(path);
|
|
130
|
+
const item = await this.get(dirname);
|
|
131
|
+
// handle the case of "Save As", where the path points to the new file
|
|
132
|
+
// to create, e.g. subfolder/example-copy.ipynb
|
|
80
133
|
let name = '';
|
|
134
|
+
if (path && !extname && item) {
|
|
135
|
+
// directory
|
|
136
|
+
dirname = `${path}/`;
|
|
137
|
+
name = '';
|
|
138
|
+
}
|
|
139
|
+
else if (dirname && basename) {
|
|
140
|
+
// file in a subfolder
|
|
141
|
+
dirname = `${dirname}/`;
|
|
142
|
+
name = basename;
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
// file at the top level
|
|
146
|
+
dirname = '';
|
|
147
|
+
name = path;
|
|
148
|
+
}
|
|
149
|
+
let file;
|
|
81
150
|
switch (type) {
|
|
82
151
|
case 'directory': {
|
|
83
152
|
const counter = await this._incrementCounter('directory');
|
|
84
|
-
name
|
|
153
|
+
name = `Untitled Folder${counter || ''}`;
|
|
85
154
|
file = {
|
|
86
155
|
name,
|
|
87
|
-
path: `${
|
|
156
|
+
path: `${dirname}${name}`,
|
|
88
157
|
last_modified: created,
|
|
89
158
|
created,
|
|
90
|
-
format: '
|
|
159
|
+
format: 'json',
|
|
91
160
|
mimetype: '',
|
|
92
161
|
content: null,
|
|
93
162
|
size: undefined,
|
|
94
163
|
writable: true,
|
|
95
|
-
type: 'directory'
|
|
164
|
+
type: 'directory',
|
|
96
165
|
};
|
|
97
166
|
break;
|
|
98
167
|
}
|
|
99
|
-
case '
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
name += `untitled${counter || ''}${ext}`;
|
|
168
|
+
case 'notebook': {
|
|
169
|
+
const counter = await this._incrementCounter('notebook');
|
|
170
|
+
name = name || `Untitled${counter || ''}.ipynb`;
|
|
103
171
|
file = {
|
|
104
172
|
name,
|
|
105
|
-
path: `${
|
|
173
|
+
path: `${dirname}${name}`,
|
|
106
174
|
last_modified: created,
|
|
107
175
|
created,
|
|
108
|
-
format: '
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
size: 0,
|
|
176
|
+
format: 'json',
|
|
177
|
+
mimetype: MIME.JSON,
|
|
178
|
+
content: Private.EMPTY_NB,
|
|
179
|
+
size: JSON.stringify(Private.EMPTY_NB).length,
|
|
113
180
|
writable: true,
|
|
114
|
-
type: '
|
|
181
|
+
type: 'notebook',
|
|
115
182
|
};
|
|
116
183
|
break;
|
|
117
184
|
}
|
|
118
185
|
default: {
|
|
119
|
-
const
|
|
120
|
-
|
|
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}`;
|
|
121
200
|
file = {
|
|
122
201
|
name,
|
|
123
|
-
path: `${
|
|
202
|
+
path: `${dirname}${name}`,
|
|
124
203
|
last_modified: created,
|
|
125
204
|
created,
|
|
126
|
-
format
|
|
127
|
-
mimetype
|
|
128
|
-
content:
|
|
129
|
-
size:
|
|
205
|
+
format,
|
|
206
|
+
mimetype,
|
|
207
|
+
content: '',
|
|
208
|
+
size: 0,
|
|
130
209
|
writable: true,
|
|
131
|
-
type: '
|
|
210
|
+
type: 'file',
|
|
132
211
|
};
|
|
133
212
|
break;
|
|
134
213
|
}
|
|
135
214
|
}
|
|
136
|
-
const key =
|
|
137
|
-
await this.
|
|
215
|
+
const key = file.path;
|
|
216
|
+
await (await this.storage).setItem(key, file);
|
|
138
217
|
return file;
|
|
139
218
|
}
|
|
140
219
|
/**
|
|
@@ -160,12 +239,15 @@ export class Contents {
|
|
|
160
239
|
}
|
|
161
240
|
const toPath = `${toDir}${name}`;
|
|
162
241
|
let item = await this.get(path, { content: true });
|
|
242
|
+
if (!item) {
|
|
243
|
+
throw Error(`Could not find file with path ${path}`);
|
|
244
|
+
}
|
|
163
245
|
item = {
|
|
164
246
|
...item,
|
|
165
247
|
name,
|
|
166
|
-
path: toPath
|
|
248
|
+
path: toPath,
|
|
167
249
|
};
|
|
168
|
-
await this.
|
|
250
|
+
await (await this.storage).setItem(toPath, item);
|
|
169
251
|
return item;
|
|
170
252
|
}
|
|
171
253
|
/**
|
|
@@ -180,26 +262,26 @@ export class Contents {
|
|
|
180
262
|
// remove leading slash
|
|
181
263
|
path = decodeURIComponent(path.replace(/^\//, ''));
|
|
182
264
|
if (path === '') {
|
|
183
|
-
return await this.
|
|
265
|
+
return await this._getFolder(path);
|
|
184
266
|
}
|
|
185
|
-
const
|
|
186
|
-
const
|
|
267
|
+
const storage = await this.storage;
|
|
268
|
+
const item = await storage.getItem(path);
|
|
269
|
+
const serverItem = await this._getServerContents(path, options);
|
|
187
270
|
const model = (item || serverItem);
|
|
188
271
|
if (!model) {
|
|
189
|
-
|
|
272
|
+
return null;
|
|
190
273
|
}
|
|
191
274
|
if (!(options === null || options === void 0 ? void 0 : options.content)) {
|
|
192
275
|
return {
|
|
193
276
|
...model,
|
|
194
277
|
content: null,
|
|
195
|
-
size: undefined
|
|
278
|
+
size: undefined,
|
|
196
279
|
};
|
|
197
280
|
}
|
|
198
281
|
// for directories, find all files with the path as the prefix
|
|
199
282
|
if (model.type === 'directory') {
|
|
200
283
|
const contentMap = new Map();
|
|
201
|
-
await
|
|
202
|
-
const file = item;
|
|
284
|
+
await storage.iterate((file, key) => {
|
|
203
285
|
// use an additional slash to not include the directory itself
|
|
204
286
|
if (key === `${path}/${file.name}`) {
|
|
205
287
|
contentMap.set(file.name, file);
|
|
@@ -207,7 +289,7 @@ export class Contents {
|
|
|
207
289
|
});
|
|
208
290
|
const serverContents = serverItem
|
|
209
291
|
? serverItem.content
|
|
210
|
-
: Array.from((await this.
|
|
292
|
+
: Array.from((await this._getServerDirectory(path)).values());
|
|
211
293
|
for (const file of serverContents) {
|
|
212
294
|
if (!contentMap.has(file.name)) {
|
|
213
295
|
contentMap.set(file.name, file);
|
|
@@ -220,140 +302,15 @@ export class Contents {
|
|
|
220
302
|
last_modified: model.last_modified,
|
|
221
303
|
created: model.created,
|
|
222
304
|
format: 'json',
|
|
223
|
-
mimetype:
|
|
305
|
+
mimetype: MIME.JSON,
|
|
224
306
|
content,
|
|
225
307
|
size: undefined,
|
|
226
308
|
writable: true,
|
|
227
|
-
type: 'directory'
|
|
309
|
+
type: 'directory',
|
|
228
310
|
};
|
|
229
311
|
}
|
|
230
312
|
return model;
|
|
231
313
|
}
|
|
232
|
-
/**
|
|
233
|
-
* retrieve the contents for this path from the union of local storage and
|
|
234
|
-
* `api/contents/{path}/all.json`.
|
|
235
|
-
*
|
|
236
|
-
* @param path - The contents path to retrieve
|
|
237
|
-
*
|
|
238
|
-
* @returns A promise which resolves with a Map of contents, keyed by local file name
|
|
239
|
-
*/
|
|
240
|
-
async getFolder(path) {
|
|
241
|
-
const content = new Map();
|
|
242
|
-
await this._storage.iterate((item, key) => {
|
|
243
|
-
if (key.includes('/')) {
|
|
244
|
-
return;
|
|
245
|
-
}
|
|
246
|
-
const file = item;
|
|
247
|
-
content.set(file.path, file);
|
|
248
|
-
});
|
|
249
|
-
// layer in contents that don't have local overwrites
|
|
250
|
-
for (const file of (await this.getServerDirectory(path)).values()) {
|
|
251
|
-
if (!content.has(file.path)) {
|
|
252
|
-
content.set(file.path, file);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
return {
|
|
256
|
-
name: '',
|
|
257
|
-
path,
|
|
258
|
-
last_modified: new Date(0).toISOString(),
|
|
259
|
-
created: new Date(0).toISOString(),
|
|
260
|
-
format: 'json',
|
|
261
|
-
mimetype: 'application/json',
|
|
262
|
-
content: Array.from(content.values()),
|
|
263
|
-
size: undefined,
|
|
264
|
-
writable: true,
|
|
265
|
-
type: 'directory'
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* retrieve the contents for this path from `__index__.json` in the appropriate
|
|
270
|
-
* folder.
|
|
271
|
-
*
|
|
272
|
-
* @param newLocalPath - The new file path.
|
|
273
|
-
*
|
|
274
|
-
* @returns A promise which resolves with a Map of contents, keyed by local file name
|
|
275
|
-
*/
|
|
276
|
-
async getServerDirectory(path) {
|
|
277
|
-
const content = this._serverContents.get(path) || new Map();
|
|
278
|
-
if (!this._serverContents.has(path)) {
|
|
279
|
-
const apiURL = URLExt.join(PageConfig.getBaseUrl(), 'api/contents', path, 'all.json');
|
|
280
|
-
try {
|
|
281
|
-
const response = await fetch(apiURL);
|
|
282
|
-
const json = JSON.parse(await response.text());
|
|
283
|
-
for (const file of json['content']) {
|
|
284
|
-
content.set(file.name, file);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
catch (err) {
|
|
288
|
-
console.warn(`don't worry, about ${err}... nothing's broken. if there had been a
|
|
289
|
-
file at ${apiURL}, you might see some more files.`);
|
|
290
|
-
}
|
|
291
|
-
this._serverContents.set(path, content);
|
|
292
|
-
}
|
|
293
|
-
return content;
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* Attempt to recover the model from `{:path}/__all__.json` file, fall back to
|
|
297
|
-
* deriving the model (including content) off the file in `/files/`. Otherwise
|
|
298
|
-
* return `null`.
|
|
299
|
-
*/
|
|
300
|
-
async getServerContents(path, options) {
|
|
301
|
-
const name = PathExt.basename(path);
|
|
302
|
-
const parentContents = await this.getServerDirectory(URLExt.join(path, '..'));
|
|
303
|
-
let model = parentContents.get(name) || {
|
|
304
|
-
name,
|
|
305
|
-
path,
|
|
306
|
-
last_modified: new Date(0).toISOString(),
|
|
307
|
-
created: new Date(0).toISOString(),
|
|
308
|
-
format: 'text',
|
|
309
|
-
mimetype: 'text/plain',
|
|
310
|
-
type: 'file',
|
|
311
|
-
writable: true,
|
|
312
|
-
content: null
|
|
313
|
-
};
|
|
314
|
-
if (options === null || options === void 0 ? void 0 : options.content) {
|
|
315
|
-
if (model.type === 'directory') {
|
|
316
|
-
const serverContents = await this.getServerDirectory(path);
|
|
317
|
-
model = { ...model, content: Array.from(serverContents.values()) };
|
|
318
|
-
}
|
|
319
|
-
else {
|
|
320
|
-
const fileUrl = URLExt.join(PageConfig.getBaseUrl(), 'files', path);
|
|
321
|
-
const response = await fetch(fileUrl);
|
|
322
|
-
if (!response.ok) {
|
|
323
|
-
return null;
|
|
324
|
-
}
|
|
325
|
-
const mimetype = model.mimetype || response.headers.get('Content-Type');
|
|
326
|
-
if (model.type === 'notebook' ||
|
|
327
|
-
(mimetype === null || mimetype === void 0 ? void 0 : mimetype.indexOf('json')) !== -1 ||
|
|
328
|
-
path.match(/\.(ipynb|[^/]*json[^/]*)$/)) {
|
|
329
|
-
model = {
|
|
330
|
-
...model,
|
|
331
|
-
content: await response.json(),
|
|
332
|
-
format: 'json',
|
|
333
|
-
mimetype: model.mimetype || 'application/json'
|
|
334
|
-
};
|
|
335
|
-
// TODO: this is not great, need a better oracle
|
|
336
|
-
}
|
|
337
|
-
else if (mimetype.indexOf('xml') !== -1 || mimetype.indexOf('text') !== -1) {
|
|
338
|
-
model = {
|
|
339
|
-
...model,
|
|
340
|
-
content: await response.text(),
|
|
341
|
-
format: 'text',
|
|
342
|
-
mimetype: mimetype || 'text/plain'
|
|
343
|
-
};
|
|
344
|
-
}
|
|
345
|
-
else {
|
|
346
|
-
model = {
|
|
347
|
-
...model,
|
|
348
|
-
content: btoa(String.fromCharCode(...new Uint8Array(await response.arrayBuffer()))),
|
|
349
|
-
format: 'base64',
|
|
350
|
-
mimetype: mimetype || 'octet/stream'
|
|
351
|
-
};
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
return model;
|
|
356
|
-
}
|
|
357
314
|
/**
|
|
358
315
|
* Rename a file or directory.
|
|
359
316
|
*
|
|
@@ -374,13 +331,14 @@ export class Contents {
|
|
|
374
331
|
...file,
|
|
375
332
|
name,
|
|
376
333
|
path: newLocalPath,
|
|
377
|
-
last_modified: modified
|
|
334
|
+
last_modified: modified,
|
|
378
335
|
};
|
|
379
|
-
await this.
|
|
336
|
+
const storage = await this.storage;
|
|
337
|
+
await storage.setItem(newLocalPath, newFile);
|
|
380
338
|
// remove the old file
|
|
381
|
-
await
|
|
339
|
+
await storage.removeItem(path);
|
|
382
340
|
// remove the corresponding checkpoint
|
|
383
|
-
await this.
|
|
341
|
+
await (await this.checkpoints).removeItem(path);
|
|
384
342
|
// if a directory, recurse through all children
|
|
385
343
|
if (file.type === 'directory') {
|
|
386
344
|
let child;
|
|
@@ -400,53 +358,79 @@ export class Contents {
|
|
|
400
358
|
*/
|
|
401
359
|
async save(path, options = {}) {
|
|
402
360
|
var _a;
|
|
361
|
+
path = decodeURIComponent(path);
|
|
362
|
+
// process the file if coming from an upload
|
|
363
|
+
const ext = PathExt.extname((_a = options.name) !== null && _a !== void 0 ? _a : '');
|
|
403
364
|
let item = await this.get(path);
|
|
404
365
|
if (!item) {
|
|
405
|
-
item = await this.newUntitled({ path });
|
|
366
|
+
item = await this.newUntitled({ path, ext, type: 'file' });
|
|
367
|
+
}
|
|
368
|
+
if (!item) {
|
|
369
|
+
return null;
|
|
406
370
|
}
|
|
407
371
|
// override with the new values
|
|
408
372
|
const modified = new Date().toISOString();
|
|
409
373
|
item = {
|
|
410
374
|
...item,
|
|
411
375
|
...options,
|
|
412
|
-
last_modified: modified
|
|
376
|
+
last_modified: modified,
|
|
413
377
|
};
|
|
414
|
-
// process the file if coming from an upload
|
|
415
|
-
const ext = PathExt.extname((_a = options.name) !== null && _a !== void 0 ? _a : '');
|
|
416
378
|
if (options.content && options.format === 'base64') {
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
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
|
+
}
|
|
426
403
|
}
|
|
427
|
-
await this.
|
|
404
|
+
await (await this.storage).setItem(path, item);
|
|
428
405
|
return item;
|
|
429
406
|
}
|
|
407
|
+
unescapeContent(content) {
|
|
408
|
+
return decodeURIComponent(escape(atob(content)));
|
|
409
|
+
}
|
|
430
410
|
/**
|
|
431
|
-
* 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.
|
|
432
415
|
*
|
|
433
416
|
* @param path - The path to the file.
|
|
434
417
|
*/
|
|
435
418
|
async delete(path) {
|
|
436
419
|
path = decodeURIComponent(path);
|
|
437
|
-
const
|
|
438
|
-
|
|
439
|
-
await
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
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
|
+
]);
|
|
450
434
|
}
|
|
451
435
|
/**
|
|
452
436
|
* Create a checkpoint for a file.
|
|
@@ -458,19 +442,21 @@ export class Contents {
|
|
|
458
442
|
*/
|
|
459
443
|
async createCheckpoint(path) {
|
|
460
444
|
var _a;
|
|
445
|
+
const checkpoints = await this.checkpoints;
|
|
446
|
+
path = decodeURIComponent(path);
|
|
461
447
|
const item = await this.get(path, { content: true });
|
|
462
|
-
|
|
448
|
+
if (!item) {
|
|
449
|
+
throw Error(`Could not find file with path ${path}`);
|
|
450
|
+
}
|
|
451
|
+
const copies = ((_a = (await checkpoints.getItem(path))) !== null && _a !== void 0 ? _a : []).filter(Boolean);
|
|
463
452
|
copies.push(item);
|
|
464
453
|
// keep only a certain amount of checkpoints per file
|
|
465
454
|
if (copies.length > N_CHECKPOINTS) {
|
|
466
455
|
copies.splice(0, copies.length - N_CHECKPOINTS);
|
|
467
456
|
}
|
|
468
|
-
await
|
|
457
|
+
await checkpoints.setItem(path, copies);
|
|
469
458
|
const id = `${copies.length - 1}`;
|
|
470
|
-
return {
|
|
471
|
-
id,
|
|
472
|
-
last_modified: item.last_modified
|
|
473
|
-
};
|
|
459
|
+
return { id, last_modified: item.last_modified };
|
|
474
460
|
}
|
|
475
461
|
/**
|
|
476
462
|
* List available checkpoints for a file.
|
|
@@ -481,16 +467,11 @@ export class Contents {
|
|
|
481
467
|
* the file.
|
|
482
468
|
*/
|
|
483
469
|
async listCheckpoints(path) {
|
|
484
|
-
const copies = ((await this.
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
return {
|
|
490
|
-
id: id.toString(),
|
|
491
|
-
last_modified: file.last_modified
|
|
492
|
-
};
|
|
493
|
-
});
|
|
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 };
|
|
494
475
|
}
|
|
495
476
|
/**
|
|
496
477
|
* Restore a file to a known checkpoint state.
|
|
@@ -501,11 +482,11 @@ export class Contents {
|
|
|
501
482
|
* @returns A promise which resolves when the checkpoint is restored.
|
|
502
483
|
*/
|
|
503
484
|
async restoreCheckpoint(path, checkpointID) {
|
|
504
|
-
|
|
505
|
-
|
|
485
|
+
path = decodeURIComponent(path);
|
|
486
|
+
const copies = ((await (await this.checkpoints).getItem(path)) || []);
|
|
506
487
|
const id = parseInt(checkpointID);
|
|
507
488
|
const item = copies[id];
|
|
508
|
-
await this.
|
|
489
|
+
await (await this.storage).setItem(path, item);
|
|
509
490
|
}
|
|
510
491
|
/**
|
|
511
492
|
* Delete a checkpoint for a file.
|
|
@@ -516,87 +497,144 @@ export class Contents {
|
|
|
516
497
|
* @returns A promise which resolves when the checkpoint is deleted.
|
|
517
498
|
*/
|
|
518
499
|
async deleteCheckpoint(path, checkpointID) {
|
|
519
|
-
|
|
520
|
-
|
|
500
|
+
path = decodeURIComponent(path);
|
|
501
|
+
const copies = ((await (await this.checkpoints).getItem(path)) || []);
|
|
521
502
|
const id = parseInt(checkpointID);
|
|
522
503
|
copies.splice(id, 1);
|
|
523
|
-
await this.
|
|
524
|
-
}
|
|
525
|
-
/**
|
|
526
|
-
* Add an `IDrive` to the manager.
|
|
527
|
-
*/
|
|
528
|
-
addDrive(drive) {
|
|
529
|
-
throw new Error('Method not implemented.');
|
|
530
|
-
}
|
|
531
|
-
/**
|
|
532
|
-
* Given a path of the form `drive:local/portion/of/it.txt`
|
|
533
|
-
* get the local part of it.
|
|
534
|
-
*
|
|
535
|
-
* @param path: the path.
|
|
536
|
-
*
|
|
537
|
-
* @returns The local part of the path.
|
|
538
|
-
*/
|
|
539
|
-
localPath(path) {
|
|
540
|
-
throw new Error('Method not implemented.');
|
|
541
|
-
}
|
|
542
|
-
/**
|
|
543
|
-
* Normalize a global path. Reduces '..' and '.' parts, and removes
|
|
544
|
-
* leading slashes from the local part of the path, while retaining
|
|
545
|
-
* the drive name if it exists.
|
|
546
|
-
*
|
|
547
|
-
* @param path: the path.
|
|
548
|
-
*
|
|
549
|
-
* @returns The normalized path.
|
|
550
|
-
*/
|
|
551
|
-
normalize(path) {
|
|
552
|
-
throw new Error('Method not implemented.');
|
|
504
|
+
await (await this.checkpoints).setItem(path, copies);
|
|
553
505
|
}
|
|
554
506
|
/**
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
* - will never prepend cwd
|
|
558
|
-
* - if root has a drive name, the result is prefixed with "<drive>:"
|
|
559
|
-
* - before adding drive name, leading slashes are removed
|
|
560
|
-
*
|
|
561
|
-
* @param path: the path.
|
|
562
|
-
*
|
|
563
|
-
* @returns The normalized path.
|
|
564
|
-
*/
|
|
565
|
-
resolvePath(root, path) {
|
|
566
|
-
throw new Error('Method not implemented.');
|
|
567
|
-
}
|
|
568
|
-
/**
|
|
569
|
-
* Given a path of the form `drive:local/portion/of/it.txt`
|
|
570
|
-
* get the name of the drive. If the path is missing
|
|
571
|
-
* a drive portion, returns an empty string.
|
|
507
|
+
* retrieve the contents for this path from the union of local storage and
|
|
508
|
+
* `api/contents/{path}/all.json`.
|
|
572
509
|
*
|
|
573
|
-
* @param path
|
|
510
|
+
* @param path - The contents path to retrieve
|
|
574
511
|
*
|
|
575
|
-
* @returns
|
|
512
|
+
* @returns A promise which resolves with a Map of contents, keyed by local file name
|
|
576
513
|
*/
|
|
577
|
-
|
|
578
|
-
|
|
514
|
+
async _getFolder(path) {
|
|
515
|
+
const content = new Map();
|
|
516
|
+
const storage = await this.storage;
|
|
517
|
+
await storage.iterate((file, key) => {
|
|
518
|
+
if (key.includes('/')) {
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
content.set(file.path, file);
|
|
522
|
+
});
|
|
523
|
+
// layer in contents that don't have local overwrites
|
|
524
|
+
for (const file of (await this._getServerDirectory(path)).values()) {
|
|
525
|
+
if (!content.has(file.path)) {
|
|
526
|
+
content.set(file.path, file);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
if (path && content.size === 0) {
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
return {
|
|
533
|
+
name: '',
|
|
534
|
+
path,
|
|
535
|
+
last_modified: new Date(0).toISOString(),
|
|
536
|
+
created: new Date(0).toISOString(),
|
|
537
|
+
format: 'json',
|
|
538
|
+
mimetype: MIME.JSON,
|
|
539
|
+
content: Array.from(content.values()),
|
|
540
|
+
size: undefined,
|
|
541
|
+
writable: true,
|
|
542
|
+
type: 'directory',
|
|
543
|
+
};
|
|
579
544
|
}
|
|
580
545
|
/**
|
|
581
|
-
*
|
|
582
|
-
*
|
|
583
|
-
*
|
|
546
|
+
* Attempt to recover the model from `{:path}/__all__.json` file, fall back to
|
|
547
|
+
* deriving the model (including content) off the file in `/files/`. Otherwise
|
|
548
|
+
* return `null`.
|
|
584
549
|
*/
|
|
585
|
-
|
|
586
|
-
|
|
550
|
+
async _getServerContents(path, options) {
|
|
551
|
+
const name = PathExt.basename(path);
|
|
552
|
+
const parentContents = await this._getServerDirectory(URLExt.join(path, '..'));
|
|
553
|
+
let model = parentContents.get(name);
|
|
554
|
+
if (!model) {
|
|
555
|
+
return null;
|
|
556
|
+
}
|
|
557
|
+
model = model || {
|
|
558
|
+
name,
|
|
559
|
+
path,
|
|
560
|
+
last_modified: new Date(0).toISOString(),
|
|
561
|
+
created: new Date(0).toISOString(),
|
|
562
|
+
format: 'text',
|
|
563
|
+
mimetype: MIME.PLAIN_TEXT,
|
|
564
|
+
type: 'file',
|
|
565
|
+
writable: true,
|
|
566
|
+
content: null,
|
|
567
|
+
};
|
|
568
|
+
if (options === null || options === void 0 ? void 0 : options.content) {
|
|
569
|
+
if (model.type === 'directory') {
|
|
570
|
+
const serverContents = await this._getServerDirectory(path);
|
|
571
|
+
model = { ...model, content: Array.from(serverContents.values()) };
|
|
572
|
+
}
|
|
573
|
+
else {
|
|
574
|
+
const fileUrl = URLExt.join(PageConfig.getBaseUrl(), 'files', path);
|
|
575
|
+
const response = await fetch(fileUrl);
|
|
576
|
+
if (!response.ok) {
|
|
577
|
+
return null;
|
|
578
|
+
}
|
|
579
|
+
const mimetype = model.mimetype || response.headers.get('Content-Type');
|
|
580
|
+
const ext = PathExt.extname(name);
|
|
581
|
+
if (model.type === 'notebook' ||
|
|
582
|
+
FILE.hasFormat(ext, 'json') ||
|
|
583
|
+
(mimetype === null || mimetype === void 0 ? void 0 : mimetype.indexOf('json')) !== -1 ||
|
|
584
|
+
path.match(/\.(ipynb|[^/]*json[^/]*)$/)) {
|
|
585
|
+
model = {
|
|
586
|
+
...model,
|
|
587
|
+
content: await response.json(),
|
|
588
|
+
format: 'json',
|
|
589
|
+
mimetype: model.mimetype || MIME.JSON,
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
else if (FILE.hasFormat(ext, 'text') || mimetype.indexOf('text') !== -1) {
|
|
593
|
+
model = {
|
|
594
|
+
...model,
|
|
595
|
+
content: await response.text(),
|
|
596
|
+
format: 'text',
|
|
597
|
+
mimetype: mimetype || MIME.PLAIN_TEXT,
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
else {
|
|
601
|
+
model = {
|
|
602
|
+
...model,
|
|
603
|
+
content: btoa(new Uint8Array(await response.arrayBuffer()).reduce(this.reduceBytesToString, '')),
|
|
604
|
+
format: 'base64',
|
|
605
|
+
mimetype: mimetype || MIME.OCTET_STREAM,
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
return model;
|
|
587
611
|
}
|
|
588
612
|
/**
|
|
589
|
-
*
|
|
590
|
-
*
|
|
591
|
-
* @param path - An absolute POSIX file path on the server.
|
|
613
|
+
* retrieve the contents for this path from `__index__.json` in the appropriate
|
|
614
|
+
* folder.
|
|
592
615
|
*
|
|
593
|
-
*
|
|
594
|
-
* It is expected that the path contains no relative paths.
|
|
616
|
+
* @param newLocalPath - The new file path.
|
|
595
617
|
*
|
|
596
|
-
*
|
|
618
|
+
* @returns A promise which resolves with a Map of contents, keyed by local file name
|
|
597
619
|
*/
|
|
598
|
-
|
|
599
|
-
|
|
620
|
+
async _getServerDirectory(path) {
|
|
621
|
+
const content = this._serverContents.get(path) || new Map();
|
|
622
|
+
if (!this._serverContents.has(path)) {
|
|
623
|
+
const apiURL = URLExt.join(PageConfig.getBaseUrl(), 'api/contents', path, 'all.json');
|
|
624
|
+
try {
|
|
625
|
+
const response = await fetch(apiURL);
|
|
626
|
+
const json = JSON.parse(await response.text());
|
|
627
|
+
for (const file of json['content']) {
|
|
628
|
+
content.set(file.name, file);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
catch (err) {
|
|
632
|
+
console.warn(`don't worry, about ${err}... nothing's broken. If there had been a
|
|
633
|
+
file at ${apiURL}, you might see some more files.`);
|
|
634
|
+
}
|
|
635
|
+
this._serverContents.set(path, content);
|
|
636
|
+
}
|
|
637
|
+
return content;
|
|
600
638
|
}
|
|
601
639
|
/**
|
|
602
640
|
* Increment the counter for a given file type.
|
|
@@ -606,9 +644,10 @@ export class Contents {
|
|
|
606
644
|
*/
|
|
607
645
|
async _incrementCounter(type) {
|
|
608
646
|
var _a;
|
|
609
|
-
const
|
|
647
|
+
const counters = await this.counters;
|
|
648
|
+
const current = (_a = (await counters.getItem(type))) !== null && _a !== void 0 ? _a : -1;
|
|
610
649
|
const counter = current + 1;
|
|
611
|
-
await
|
|
650
|
+
await counters.setItem(type, counter);
|
|
612
651
|
return counter;
|
|
613
652
|
}
|
|
614
653
|
}
|
|
@@ -622,10 +661,10 @@ var Private;
|
|
|
622
661
|
*/
|
|
623
662
|
Private.EMPTY_NB = {
|
|
624
663
|
metadata: {
|
|
625
|
-
orig_nbformat: 4
|
|
664
|
+
orig_nbformat: 4,
|
|
626
665
|
},
|
|
627
666
|
nbformat_minor: 4,
|
|
628
667
|
nbformat: 4,
|
|
629
|
-
cells: []
|
|
668
|
+
cells: [],
|
|
630
669
|
};
|
|
631
670
|
})(Private || (Private = {}));
|