@jupyterlite/contents 0.2.2 → 0.3.0-alpha.0
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/package.json +8 -7
- package/src/broadcast.ts +180 -0
- package/src/contents.ts +819 -0
- package/src/drivefs.ts +566 -0
- package/src/emscripten.ts +126 -0
- package/src/index.ts +8 -0
- package/src/tokens.ts +202 -0
package/src/drivefs.ts
ADDED
|
@@ -0,0 +1,566 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
// Types and implementation inspired from https://github.com/jvilk/BrowserFS
|
|
5
|
+
// LICENSE: https://github.com/jvilk/BrowserFS/blob/8977a704ea469d05daf857e4818bef1f4f498326/LICENSE
|
|
6
|
+
// And from https://github.com/gzuidhof/starboard-notebook
|
|
7
|
+
|
|
8
|
+
// LICENSE: https://github.com/gzuidhof/starboard-notebook/blob/cd8d3fc30af4bd29cdd8f6b8c207df8138f5d5dd/LICENSE
|
|
9
|
+
import {
|
|
10
|
+
FS,
|
|
11
|
+
ERRNO_CODES,
|
|
12
|
+
PATH,
|
|
13
|
+
DIR_MODE,
|
|
14
|
+
SEEK_CUR,
|
|
15
|
+
SEEK_END,
|
|
16
|
+
IEmscriptenStream,
|
|
17
|
+
IEmscriptenStreamOps,
|
|
18
|
+
IEmscriptenNodeOps,
|
|
19
|
+
IEmscriptenFSNode,
|
|
20
|
+
IStats,
|
|
21
|
+
} from './emscripten';
|
|
22
|
+
|
|
23
|
+
export const DRIVE_SEPARATOR = ':';
|
|
24
|
+
export const DRIVE_API_PATH = '/api/drive.v1';
|
|
25
|
+
|
|
26
|
+
export const BLOCK_SIZE = 4096;
|
|
27
|
+
|
|
28
|
+
const encoder = new TextEncoder();
|
|
29
|
+
const decoder = new TextDecoder('utf-8');
|
|
30
|
+
|
|
31
|
+
export type TDriveMethod =
|
|
32
|
+
| 'readdir'
|
|
33
|
+
| 'rmdir'
|
|
34
|
+
| 'rename'
|
|
35
|
+
| 'getmode'
|
|
36
|
+
| 'lookup'
|
|
37
|
+
| 'mknod'
|
|
38
|
+
| 'getattr'
|
|
39
|
+
| 'get'
|
|
40
|
+
| 'put';
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Interface of a request on the /api/drive endpoint
|
|
44
|
+
*/
|
|
45
|
+
export interface IDriveRequest {
|
|
46
|
+
/**
|
|
47
|
+
* The method of the request (rmdir, readdir etc)
|
|
48
|
+
*/
|
|
49
|
+
method: TDriveMethod;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The expected receiver of the request
|
|
53
|
+
*/
|
|
54
|
+
receiver?: 'broadcast.ts';
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The path to the file/directory for which the request was sent
|
|
58
|
+
*/
|
|
59
|
+
path: string;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Extra data on the request
|
|
63
|
+
*/
|
|
64
|
+
data?: any;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Mapping flag -> do we need to overwrite the file upon closing it
|
|
68
|
+
const flagNeedsWrite: { [flag: number]: boolean } = {
|
|
69
|
+
0 /*O_RDONLY*/: false,
|
|
70
|
+
1 /*O_WRONLY*/: true,
|
|
71
|
+
2 /*O_RDWR*/: true,
|
|
72
|
+
64 /*O_CREAT*/: true,
|
|
73
|
+
65 /*O_WRONLY|O_CREAT*/: true,
|
|
74
|
+
66 /*O_RDWR|O_CREAT*/: true,
|
|
75
|
+
129 /*O_WRONLY|O_EXCL*/: true,
|
|
76
|
+
193 /*O_WRONLY|O_CREAT|O_EXCL*/: true,
|
|
77
|
+
514 /*O_RDWR|O_TRUNC*/: true,
|
|
78
|
+
577 /*O_WRONLY|O_CREAT|O_TRUNC*/: true,
|
|
79
|
+
578 /*O_CREAT|O_RDWR|O_TRUNC*/: true,
|
|
80
|
+
705 /*O_WRONLY|O_CREAT|O_EXCL|O_TRUNC*/: true,
|
|
81
|
+
706 /*O_RDWR|O_CREAT|O_EXCL|O_TRUNC*/: true,
|
|
82
|
+
1024 /*O_APPEND*/: true,
|
|
83
|
+
1025 /*O_WRONLY|O_APPEND*/: true,
|
|
84
|
+
1026 /*O_RDWR|O_APPEND*/: true,
|
|
85
|
+
1089 /*O_WRONLY|O_CREAT|O_APPEND*/: true,
|
|
86
|
+
1090 /*O_RDWR|O_CREAT|O_APPEND*/: true,
|
|
87
|
+
1153 /*O_WRONLY|O_EXCL|O_APPEND*/: true,
|
|
88
|
+
1154 /*O_RDWR|O_EXCL|O_APPEND*/: true,
|
|
89
|
+
1217 /*O_WRONLY|O_CREAT|O_EXCL|O_APPEND*/: true,
|
|
90
|
+
1218 /*O_RDWR|O_CREAT|O_EXCL|O_APPEND*/: true,
|
|
91
|
+
4096 /*O_RDONLY|O_DSYNC*/: true,
|
|
92
|
+
4098 /*O_RDWR|O_DSYNC*/: true,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/** Implementation-specifc extension of an open stream, adding the file. */
|
|
96
|
+
export interface IDriveStream extends IEmscriptenStream {
|
|
97
|
+
file?: DriveFS.IFile;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export class DriveFSEmscriptenStreamOps implements IEmscriptenStreamOps {
|
|
101
|
+
private fs: DriveFS;
|
|
102
|
+
|
|
103
|
+
constructor(fs: DriveFS) {
|
|
104
|
+
this.fs = fs;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
open(stream: IDriveStream): void {
|
|
108
|
+
const path = this.fs.realPath(stream.node);
|
|
109
|
+
if (this.fs.FS.isFile(stream.node.mode)) {
|
|
110
|
+
stream.file = this.fs.API.get(path);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
close(stream: IDriveStream): void {
|
|
115
|
+
if (!this.fs.FS.isFile(stream.node.mode) || !stream.file) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const path = this.fs.realPath(stream.node);
|
|
120
|
+
|
|
121
|
+
const flags = stream.flags;
|
|
122
|
+
let parsedFlags = typeof flags === 'string' ? parseInt(flags, 10) : flags;
|
|
123
|
+
parsedFlags &= 0x1fff;
|
|
124
|
+
|
|
125
|
+
let needsWrite = true;
|
|
126
|
+
if (parsedFlags in flagNeedsWrite) {
|
|
127
|
+
needsWrite = flagNeedsWrite[parsedFlags];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (needsWrite) {
|
|
131
|
+
this.fs.API.put(path, stream.file);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
stream.file = undefined;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
read(
|
|
138
|
+
stream: IDriveStream,
|
|
139
|
+
buffer: Uint8Array,
|
|
140
|
+
offset: number,
|
|
141
|
+
length: number,
|
|
142
|
+
position: number,
|
|
143
|
+
): number {
|
|
144
|
+
if (
|
|
145
|
+
length <= 0 ||
|
|
146
|
+
stream.file === undefined ||
|
|
147
|
+
position >= (stream.file.data.length || 0)
|
|
148
|
+
) {
|
|
149
|
+
return 0;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const size = Math.min(stream.file.data.length - position, length);
|
|
153
|
+
buffer.set(stream.file.data.subarray(position, position + size), offset);
|
|
154
|
+
return size;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
write(
|
|
158
|
+
stream: IDriveStream,
|
|
159
|
+
buffer: Uint8Array,
|
|
160
|
+
offset: number,
|
|
161
|
+
length: number,
|
|
162
|
+
position: number,
|
|
163
|
+
): number {
|
|
164
|
+
if (length <= 0 || stream.file === undefined) {
|
|
165
|
+
return 0;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
stream.node.timestamp = Date.now();
|
|
169
|
+
|
|
170
|
+
if (position + length > (stream.file?.data.length || 0)) {
|
|
171
|
+
const oldData = stream.file.data ? stream.file.data : new Uint8Array();
|
|
172
|
+
stream.file.data = new Uint8Array(position + length);
|
|
173
|
+
stream.file.data.set(oldData);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
stream.file.data.set(buffer.subarray(offset, offset + length), position);
|
|
177
|
+
|
|
178
|
+
return length;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
llseek(stream: IDriveStream, offset: number, whence: number): number {
|
|
182
|
+
let position = offset;
|
|
183
|
+
if (whence === SEEK_CUR) {
|
|
184
|
+
position += stream.position;
|
|
185
|
+
} else if (whence === SEEK_END) {
|
|
186
|
+
if (this.fs.FS.isFile(stream.node.mode)) {
|
|
187
|
+
if (stream.file !== undefined) {
|
|
188
|
+
position += stream.file.data.length;
|
|
189
|
+
} else {
|
|
190
|
+
throw new this.fs.FS.ErrnoError(this.fs.ERRNO_CODES.EPERM);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (position < 0) {
|
|
196
|
+
throw new this.fs.FS.ErrnoError(this.fs.ERRNO_CODES.EINVAL);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return position;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export class DriveFSEmscriptenNodeOps implements IEmscriptenNodeOps {
|
|
204
|
+
private fs: DriveFS;
|
|
205
|
+
|
|
206
|
+
constructor(fs: DriveFS) {
|
|
207
|
+
this.fs = fs;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
getattr(node: IEmscriptenFSNode): IStats {
|
|
211
|
+
return {
|
|
212
|
+
...this.fs.API.getattr(this.fs.realPath(node)),
|
|
213
|
+
mode: node.mode,
|
|
214
|
+
ino: node.id,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
setattr(node: IEmscriptenFSNode, attr: IStats): void {
|
|
219
|
+
for (const [key, value] of Object.entries(attr)) {
|
|
220
|
+
switch (key) {
|
|
221
|
+
case 'mode':
|
|
222
|
+
node.mode = value;
|
|
223
|
+
break;
|
|
224
|
+
case 'timestamp':
|
|
225
|
+
node.timestamp = value;
|
|
226
|
+
break;
|
|
227
|
+
default:
|
|
228
|
+
console.warn('setattr', key, 'of', value, 'on', node, 'not yet implemented');
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
lookup(parent: IEmscriptenFSNode, name: string): IEmscriptenFSNode {
|
|
235
|
+
const path = this.fs.PATH.join2(this.fs.realPath(parent), name);
|
|
236
|
+
const result = this.fs.API.lookup(path);
|
|
237
|
+
if (!result.ok) {
|
|
238
|
+
throw this.fs.FS.genericErrors[this.fs.ERRNO_CODES['ENOENT']];
|
|
239
|
+
}
|
|
240
|
+
return this.fs.createNode(parent, name, result.mode, 0);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
mknod(
|
|
244
|
+
parent: IEmscriptenFSNode,
|
|
245
|
+
name: string,
|
|
246
|
+
mode: number,
|
|
247
|
+
dev: number,
|
|
248
|
+
): IEmscriptenFSNode {
|
|
249
|
+
const path = this.fs.PATH.join2(this.fs.realPath(parent), name);
|
|
250
|
+
this.fs.API.mknod(path, mode);
|
|
251
|
+
return this.fs.createNode(parent, name, mode, dev);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
rename(oldNode: IEmscriptenFSNode, newDir: IEmscriptenFSNode, newName: string): void {
|
|
255
|
+
this.fs.API.rename(
|
|
256
|
+
oldNode.parent
|
|
257
|
+
? this.fs.PATH.join2(this.fs.realPath(oldNode.parent), oldNode.name)
|
|
258
|
+
: oldNode.name,
|
|
259
|
+
this.fs.PATH.join2(this.fs.realPath(newDir), newName),
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
// Updating the in-memory node
|
|
263
|
+
oldNode.name = newName;
|
|
264
|
+
oldNode.parent = newDir;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
unlink(parent: IEmscriptenFSNode, name: string): void {
|
|
268
|
+
this.fs.API.rmdir(this.fs.PATH.join2(this.fs.realPath(parent), name));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
rmdir(parent: IEmscriptenFSNode, name: string) {
|
|
272
|
+
this.fs.API.rmdir(this.fs.PATH.join2(this.fs.realPath(parent), name));
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
readdir(node: IEmscriptenFSNode): string[] {
|
|
276
|
+
return this.fs.API.readdir(this.fs.realPath(node));
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
symlink(parent: IEmscriptenFSNode, newName: string, oldPath: string): void {
|
|
280
|
+
throw new this.fs.FS.ErrnoError(this.fs.ERRNO_CODES['EPERM']);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
readlink(node: IEmscriptenFSNode): string {
|
|
284
|
+
throw new this.fs.FS.ErrnoError(this.fs.ERRNO_CODES['EPERM']);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Wrap ServiceWorker requests for an Emscripten-compatible synchronous API.
|
|
290
|
+
*/
|
|
291
|
+
export class ContentsAPI {
|
|
292
|
+
constructor(
|
|
293
|
+
baseUrl: string,
|
|
294
|
+
driveName: string,
|
|
295
|
+
mountpoint: string,
|
|
296
|
+
FS: FS,
|
|
297
|
+
ERRNO_CODES: ERRNO_CODES,
|
|
298
|
+
) {
|
|
299
|
+
this._baseUrl = baseUrl;
|
|
300
|
+
this._driveName = driveName;
|
|
301
|
+
this._mountpoint = mountpoint;
|
|
302
|
+
this.FS = FS;
|
|
303
|
+
this.ERRNO_CODES = ERRNO_CODES;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
request(data: IDriveRequest): any {
|
|
307
|
+
const xhr = new XMLHttpRequest();
|
|
308
|
+
xhr.open('POST', encodeURI(this.endpoint), false);
|
|
309
|
+
|
|
310
|
+
try {
|
|
311
|
+
xhr.send(JSON.stringify(data));
|
|
312
|
+
} catch (e) {
|
|
313
|
+
console.error(e);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (xhr.status >= 400) {
|
|
317
|
+
throw new this.FS.ErrnoError(this.ERRNO_CODES['EINVAL']);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return JSON.parse(xhr.responseText);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
lookup(path: string): DriveFS.ILookup {
|
|
324
|
+
return this.request({ method: 'lookup', path: this.normalizePath(path) });
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
getmode(path: string): number {
|
|
328
|
+
return Number.parseInt(
|
|
329
|
+
this.request({ method: 'getmode', path: this.normalizePath(path) }),
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
mknod(path: string, mode: number) {
|
|
334
|
+
return this.request({
|
|
335
|
+
method: 'mknod',
|
|
336
|
+
path: this.normalizePath(path),
|
|
337
|
+
data: { mode },
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
rename(oldPath: string, newPath: string): void {
|
|
342
|
+
return this.request({
|
|
343
|
+
method: 'rename',
|
|
344
|
+
path: this.normalizePath(oldPath),
|
|
345
|
+
data: { newPath: this.normalizePath(newPath) },
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
readdir(path: string): string[] {
|
|
350
|
+
const dirlist = this.request({
|
|
351
|
+
method: 'readdir',
|
|
352
|
+
path: this.normalizePath(path),
|
|
353
|
+
});
|
|
354
|
+
dirlist.push('.');
|
|
355
|
+
dirlist.push('..');
|
|
356
|
+
return dirlist;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
rmdir(path: string): void {
|
|
360
|
+
return this.request({ method: 'rmdir', path: this.normalizePath(path) });
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
get(path: string): DriveFS.IFile {
|
|
364
|
+
const response = this.request({ method: 'get', path: this.normalizePath(path) });
|
|
365
|
+
|
|
366
|
+
const serializedContent = response.content;
|
|
367
|
+
const format: 'json' | 'text' | 'base64' | null = response.format;
|
|
368
|
+
|
|
369
|
+
switch (format) {
|
|
370
|
+
case 'json':
|
|
371
|
+
case 'text':
|
|
372
|
+
return {
|
|
373
|
+
data: encoder.encode(serializedContent),
|
|
374
|
+
format,
|
|
375
|
+
};
|
|
376
|
+
case 'base64': {
|
|
377
|
+
const binString = atob(serializedContent);
|
|
378
|
+
const len = binString.length;
|
|
379
|
+
const data = new Uint8Array(len);
|
|
380
|
+
for (let i = 0; i < len; i++) {
|
|
381
|
+
data[i] = binString.charCodeAt(i);
|
|
382
|
+
}
|
|
383
|
+
return {
|
|
384
|
+
data,
|
|
385
|
+
format,
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
default:
|
|
389
|
+
throw new this.FS.ErrnoError(this.ERRNO_CODES['ENOENT']);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
put(path: string, value: DriveFS.IFile) {
|
|
394
|
+
switch (value.format) {
|
|
395
|
+
case 'json':
|
|
396
|
+
case 'text':
|
|
397
|
+
return this.request({
|
|
398
|
+
method: 'put',
|
|
399
|
+
path: this.normalizePath(path),
|
|
400
|
+
data: {
|
|
401
|
+
format: value.format,
|
|
402
|
+
data: decoder.decode(value.data),
|
|
403
|
+
},
|
|
404
|
+
});
|
|
405
|
+
case 'base64': {
|
|
406
|
+
let binary = '';
|
|
407
|
+
for (let i = 0; i < value.data.byteLength; i++) {
|
|
408
|
+
binary += String.fromCharCode(value.data[i]);
|
|
409
|
+
}
|
|
410
|
+
return this.request({
|
|
411
|
+
method: 'put',
|
|
412
|
+
path: this.normalizePath(path),
|
|
413
|
+
data: {
|
|
414
|
+
format: value.format,
|
|
415
|
+
data: btoa(binary),
|
|
416
|
+
},
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
getattr(path: string): IStats {
|
|
423
|
+
const stats: IStats = this.request({
|
|
424
|
+
method: 'getattr',
|
|
425
|
+
path: this.normalizePath(path),
|
|
426
|
+
});
|
|
427
|
+
// Turn datetimes into proper objects
|
|
428
|
+
stats.atime = new Date(stats.atime);
|
|
429
|
+
stats.mtime = new Date(stats.mtime);
|
|
430
|
+
stats.ctime = new Date(stats.ctime);
|
|
431
|
+
// ensure a non-undefined size (0 isn't great, though)
|
|
432
|
+
stats.size = stats.size || 0;
|
|
433
|
+
return stats;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Normalize a Path by making it compliant for the content manager
|
|
438
|
+
*
|
|
439
|
+
* @param path: the path relatively to the Emscripten drive
|
|
440
|
+
*/
|
|
441
|
+
normalizePath(path: string): string {
|
|
442
|
+
// Remove mountpoint prefix
|
|
443
|
+
if (path.startsWith(this._mountpoint)) {
|
|
444
|
+
path = path.slice(this._mountpoint.length);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Add JupyterLab drive name
|
|
448
|
+
if (this._driveName) {
|
|
449
|
+
path = `${this._driveName}${DRIVE_SEPARATOR}${path}`;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
return path;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Get the api/drive endpoint
|
|
457
|
+
*/
|
|
458
|
+
get endpoint(): string {
|
|
459
|
+
return `${this._baseUrl}api/drive`;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
private _baseUrl: string;
|
|
463
|
+
private _driveName: string;
|
|
464
|
+
private _mountpoint: string;
|
|
465
|
+
private FS: FS;
|
|
466
|
+
private ERRNO_CODES: ERRNO_CODES;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
export class DriveFS {
|
|
470
|
+
FS: FS;
|
|
471
|
+
API: ContentsAPI;
|
|
472
|
+
PATH: PATH;
|
|
473
|
+
ERRNO_CODES: ERRNO_CODES;
|
|
474
|
+
driveName: string;
|
|
475
|
+
|
|
476
|
+
constructor(options: DriveFS.IOptions) {
|
|
477
|
+
this.FS = options.FS;
|
|
478
|
+
this.PATH = options.PATH;
|
|
479
|
+
this.ERRNO_CODES = options.ERRNO_CODES;
|
|
480
|
+
this.API = new ContentsAPI(
|
|
481
|
+
options.baseUrl,
|
|
482
|
+
options.driveName,
|
|
483
|
+
options.mountpoint,
|
|
484
|
+
this.FS,
|
|
485
|
+
this.ERRNO_CODES,
|
|
486
|
+
);
|
|
487
|
+
this.driveName = options.driveName;
|
|
488
|
+
|
|
489
|
+
this.node_ops = new DriveFSEmscriptenNodeOps(this);
|
|
490
|
+
this.stream_ops = new DriveFSEmscriptenStreamOps(this);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
node_ops: IEmscriptenNodeOps;
|
|
494
|
+
stream_ops: IEmscriptenStreamOps;
|
|
495
|
+
|
|
496
|
+
mount(mount: any): IEmscriptenFSNode {
|
|
497
|
+
return this.createNode(null, mount.mountpoint, DIR_MODE | 511, 0);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
createNode(
|
|
501
|
+
parent: IEmscriptenFSNode | null,
|
|
502
|
+
name: string,
|
|
503
|
+
mode: number,
|
|
504
|
+
dev: number,
|
|
505
|
+
): IEmscriptenFSNode {
|
|
506
|
+
const FS = this.FS;
|
|
507
|
+
if (!FS.isDir(mode) && !FS.isFile(mode)) {
|
|
508
|
+
throw new FS.ErrnoError(this.ERRNO_CODES['EINVAL']);
|
|
509
|
+
}
|
|
510
|
+
const node = FS.createNode(parent, name, mode, dev);
|
|
511
|
+
node.node_ops = this.node_ops;
|
|
512
|
+
node.stream_ops = this.stream_ops;
|
|
513
|
+
return node;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
getMode(path: string): number {
|
|
517
|
+
return this.API.getmode(path);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
realPath(node: IEmscriptenFSNode): string {
|
|
521
|
+
const parts: string[] = [];
|
|
522
|
+
let currentNode: IEmscriptenFSNode = node;
|
|
523
|
+
|
|
524
|
+
parts.push(currentNode.name);
|
|
525
|
+
while (currentNode.parent !== currentNode) {
|
|
526
|
+
currentNode = currentNode.parent;
|
|
527
|
+
parts.push(currentNode.name);
|
|
528
|
+
}
|
|
529
|
+
parts.reverse();
|
|
530
|
+
|
|
531
|
+
return this.PATH.join.apply(null, parts);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* A namespace for DriveFS configurations, etc.
|
|
537
|
+
*/
|
|
538
|
+
export namespace DriveFS {
|
|
539
|
+
/**
|
|
540
|
+
* A file representation;
|
|
541
|
+
*/
|
|
542
|
+
export interface IFile {
|
|
543
|
+
data: Uint8Array;
|
|
544
|
+
format: 'json' | 'text' | 'base64';
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* The response to a lookup request;
|
|
549
|
+
*/
|
|
550
|
+
export interface ILookup {
|
|
551
|
+
ok: boolean;
|
|
552
|
+
mode: number;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* Initialization options for a drive;
|
|
557
|
+
*/
|
|
558
|
+
export interface IOptions {
|
|
559
|
+
FS: FS;
|
|
560
|
+
PATH: PATH;
|
|
561
|
+
ERRNO_CODES: ERRNO_CODES;
|
|
562
|
+
baseUrl: string;
|
|
563
|
+
driveName: string;
|
|
564
|
+
mountpoint: string;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
// Types and implementation inspired from https://github.com/jvilk/BrowserFS
|
|
5
|
+
// LICENSE: https://github.com/jvilk/BrowserFS/blob/8977a704ea469d05daf857e4818bef1f4f498326/LICENSE
|
|
6
|
+
// And from https://github.com/gzuidhof/starboard-notebook
|
|
7
|
+
|
|
8
|
+
// LICENSE: https://github.com/gzuidhof/starboard-notebook/blob/cd8d3fc30af4bd29cdd8f6b8c207df8138f5d5dd/LICENSE
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Types for Emscripten primitives.
|
|
12
|
+
*
|
|
13
|
+
* Ideally, much more of these would be taken from `@types/emscripten`.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
|
|
17
|
+
/// <reference path="../../../node_modules/@types/emscripten/index.d.ts" />
|
|
18
|
+
type EmscriptenFS = typeof FS;
|
|
19
|
+
|
|
20
|
+
export const DIR_MODE = 16895; // 040777
|
|
21
|
+
export const FILE_MODE = 33206; // 100666
|
|
22
|
+
export const SEEK_CUR = 1;
|
|
23
|
+
export const SEEK_END = 2;
|
|
24
|
+
|
|
25
|
+
export interface IStats {
|
|
26
|
+
dev: number;
|
|
27
|
+
ino: number;
|
|
28
|
+
mode: number;
|
|
29
|
+
nlink: number;
|
|
30
|
+
uid: number;
|
|
31
|
+
gid: number;
|
|
32
|
+
rdev: number;
|
|
33
|
+
size: number;
|
|
34
|
+
blksize: number;
|
|
35
|
+
blocks: number;
|
|
36
|
+
atime: Date;
|
|
37
|
+
mtime: Date;
|
|
38
|
+
ctime: Date;
|
|
39
|
+
timestamp?: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface IEmscriptenFSNode {
|
|
43
|
+
id: number;
|
|
44
|
+
name: string;
|
|
45
|
+
mode: number;
|
|
46
|
+
parent: IEmscriptenFSNode;
|
|
47
|
+
mount: { opts: { root: string } };
|
|
48
|
+
stream_ops: IEmscriptenStreamOps;
|
|
49
|
+
node_ops: IEmscriptenNodeOps;
|
|
50
|
+
timestamp: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface IEmscriptenStream {
|
|
54
|
+
node: IEmscriptenFSNode;
|
|
55
|
+
nfd: any;
|
|
56
|
+
flags: string;
|
|
57
|
+
position: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface IEmscriptenNodeOps {
|
|
61
|
+
getattr(node: IEmscriptenFSNode): IStats;
|
|
62
|
+
setattr(node: IEmscriptenFSNode, attr: IStats): void;
|
|
63
|
+
lookup(parent: IEmscriptenFSNode, name: string): IEmscriptenFSNode;
|
|
64
|
+
mknod(
|
|
65
|
+
parent: IEmscriptenFSNode,
|
|
66
|
+
name: string,
|
|
67
|
+
mode: number,
|
|
68
|
+
dev: number,
|
|
69
|
+
): IEmscriptenFSNode;
|
|
70
|
+
rename(oldNode: IEmscriptenFSNode, newDir: IEmscriptenFSNode, newName: string): void;
|
|
71
|
+
unlink(parent: IEmscriptenFSNode, name: string): void;
|
|
72
|
+
rmdir(parent: IEmscriptenFSNode, name: string): void;
|
|
73
|
+
readdir(node: IEmscriptenFSNode): string[];
|
|
74
|
+
symlink(parent: IEmscriptenFSNode, newName: string, oldPath: string): void;
|
|
75
|
+
readlink(node: IEmscriptenFSNode): string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface IEmscriptenStreamOps {
|
|
79
|
+
open(stream: IEmscriptenStream): void;
|
|
80
|
+
close(stream: IEmscriptenStream): void;
|
|
81
|
+
read(
|
|
82
|
+
stream: IEmscriptenStream,
|
|
83
|
+
buffer: Uint8Array,
|
|
84
|
+
offset: number,
|
|
85
|
+
length: number,
|
|
86
|
+
position: number,
|
|
87
|
+
): number;
|
|
88
|
+
write(
|
|
89
|
+
stream: IEmscriptenStream,
|
|
90
|
+
buffer: Uint8Array,
|
|
91
|
+
offset: number,
|
|
92
|
+
length: number,
|
|
93
|
+
position: number,
|
|
94
|
+
): number;
|
|
95
|
+
llseek(stream: IEmscriptenStream, offset: number, whence: number): number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The emscripten filesystem module API.
|
|
100
|
+
*/
|
|
101
|
+
export type FS = EmscriptenFS & {
|
|
102
|
+
ErrnoError: any;
|
|
103
|
+
createNode: (
|
|
104
|
+
parent: IEmscriptenFSNode | null,
|
|
105
|
+
name: string,
|
|
106
|
+
mode: number,
|
|
107
|
+
dev: number,
|
|
108
|
+
) => IEmscriptenFSNode;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The emscripten filesystem error codes.
|
|
113
|
+
*/
|
|
114
|
+
export type ERRNO_CODES = any;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The emscripten FS Path API.
|
|
118
|
+
*/
|
|
119
|
+
export type PATH = {
|
|
120
|
+
basename: (path: string) => string;
|
|
121
|
+
dirname: (path: string) => string;
|
|
122
|
+
join: (...parts: string[]) => string;
|
|
123
|
+
join2: (l: string, r: string) => string;
|
|
124
|
+
normalize: (path: string) => string;
|
|
125
|
+
splitPath: (filename: string) => string;
|
|
126
|
+
};
|
package/src/index.ts
ADDED