@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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jupyterlite/contents",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0-alpha.0",
|
|
4
4
|
"description": "JupyterLite - Contents",
|
|
5
5
|
"homepage": "https://github.com/jupyterlite/jupyterlite",
|
|
6
6
|
"bugs": {
|
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
"lib/*.js.map",
|
|
28
28
|
"lib/*.js",
|
|
29
29
|
"style/*.css",
|
|
30
|
-
"style/index.js"
|
|
30
|
+
"style/index.js",
|
|
31
|
+
"src/**/*.{ts,tsx}"
|
|
31
32
|
],
|
|
32
33
|
"scripts": {
|
|
33
34
|
"build": "tsc -b",
|
|
@@ -42,9 +43,9 @@
|
|
|
42
43
|
"watch": "tsc -b --watch"
|
|
43
44
|
},
|
|
44
45
|
"dependencies": {
|
|
45
|
-
"@jupyterlab/nbformat": "~4.
|
|
46
|
-
"@jupyterlab/services": "~7.
|
|
47
|
-
"@jupyterlite/localforage": "^0.
|
|
46
|
+
"@jupyterlab/nbformat": "~4.1.1",
|
|
47
|
+
"@jupyterlab/services": "~7.1.1",
|
|
48
|
+
"@jupyterlite/localforage": "^0.3.0-alpha.0",
|
|
48
49
|
"@lumino/coreutils": "^2.1.2",
|
|
49
50
|
"@types/emscripten": "^1.39.6",
|
|
50
51
|
"localforage": "^1.9.0",
|
|
@@ -53,13 +54,13 @@
|
|
|
53
54
|
"devDependencies": {
|
|
54
55
|
"@babel/core": "^7.11.6",
|
|
55
56
|
"@babel/preset-env": "^7.12.1",
|
|
56
|
-
"@jupyterlab/testutils": "~4.
|
|
57
|
+
"@jupyterlab/testutils": "~4.1.1",
|
|
57
58
|
"@types/jest": "^29.5.3",
|
|
58
59
|
"@types/mime": "^2.0.3",
|
|
59
60
|
"jest": "^29.6.2",
|
|
60
61
|
"rimraf": "~5.0.1",
|
|
61
62
|
"ts-jest": "^29.1.1",
|
|
62
|
-
"typescript": "~5.
|
|
63
|
+
"typescript": "~5.1.6"
|
|
63
64
|
},
|
|
64
65
|
"publishConfig": {
|
|
65
66
|
"access": "public"
|
package/src/broadcast.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
import { PathExt } from '@jupyterlab/coreutils';
|
|
5
|
+
|
|
6
|
+
import { Contents as ServerContents } from '@jupyterlab/services';
|
|
7
|
+
|
|
8
|
+
import { DIR_MODE, FILE_MODE } from './emscripten';
|
|
9
|
+
|
|
10
|
+
import { BLOCK_SIZE, IDriveRequest, DRIVE_API_PATH } from './drivefs';
|
|
11
|
+
|
|
12
|
+
import { IModel } from './contents';
|
|
13
|
+
|
|
14
|
+
import { IBroadcastChannelWrapper } from './tokens';
|
|
15
|
+
|
|
16
|
+
/** A broadcaster for the ServiceWorker */
|
|
17
|
+
export class BroadcastChannelWrapper implements IBroadcastChannelWrapper {
|
|
18
|
+
public isDisposed = false;
|
|
19
|
+
|
|
20
|
+
constructor(options: BroadcastChannelWrapper.IOptions) {
|
|
21
|
+
this._contents = options.contents;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get enabled() {
|
|
25
|
+
return this._enabled;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
enable() {
|
|
29
|
+
if (this._channel) {
|
|
30
|
+
console.warn('BroadcastChannel already created and enabled');
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
this._channel = new BroadcastChannel(DRIVE_API_PATH);
|
|
34
|
+
this._channel.addEventListener('message', this._onMessage);
|
|
35
|
+
this._enabled = true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
disable() {
|
|
39
|
+
if (this._channel) {
|
|
40
|
+
this._channel.removeEventListener('message', this._onMessage);
|
|
41
|
+
this._channel = null;
|
|
42
|
+
}
|
|
43
|
+
this._enabled = false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Clean up the broadcaster. */
|
|
47
|
+
dispose() {
|
|
48
|
+
if (this.isDisposed) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
this.disable();
|
|
52
|
+
this.isDisposed = true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Handle a message received on the BroadcastChannel */
|
|
56
|
+
protected _onMessage = async (event: MessageEvent<IDriveRequest>): Promise<void> => {
|
|
57
|
+
if (!this._channel) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const { _contents } = this;
|
|
61
|
+
const request = event.data;
|
|
62
|
+
const path = request?.path;
|
|
63
|
+
const receiver = request?.receiver;
|
|
64
|
+
if (receiver !== 'broadcast.ts') {
|
|
65
|
+
// Message is not meant for us
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// many successful responses default to null
|
|
70
|
+
let response: any = null;
|
|
71
|
+
|
|
72
|
+
// most requests will use a model
|
|
73
|
+
let model: ServerContents.IModel;
|
|
74
|
+
|
|
75
|
+
switch (request?.method) {
|
|
76
|
+
case 'readdir':
|
|
77
|
+
model = await _contents.get(path, { content: true });
|
|
78
|
+
response = [];
|
|
79
|
+
if (model.type === 'directory' && model.content) {
|
|
80
|
+
response = model.content.map((subcontent: IModel) => subcontent.name);
|
|
81
|
+
}
|
|
82
|
+
break;
|
|
83
|
+
case 'rmdir':
|
|
84
|
+
await _contents.delete(path);
|
|
85
|
+
break;
|
|
86
|
+
case 'rename':
|
|
87
|
+
await _contents.rename(path, request.data.newPath);
|
|
88
|
+
break;
|
|
89
|
+
case 'getmode':
|
|
90
|
+
model = await _contents.get(path);
|
|
91
|
+
if (model.type === 'directory') {
|
|
92
|
+
response = DIR_MODE;
|
|
93
|
+
} else {
|
|
94
|
+
response = FILE_MODE;
|
|
95
|
+
}
|
|
96
|
+
break;
|
|
97
|
+
case 'lookup':
|
|
98
|
+
try {
|
|
99
|
+
model = await _contents.get(path);
|
|
100
|
+
response = {
|
|
101
|
+
ok: true,
|
|
102
|
+
mode: model.type === 'directory' ? DIR_MODE : FILE_MODE,
|
|
103
|
+
};
|
|
104
|
+
} catch (e) {
|
|
105
|
+
response = { ok: false };
|
|
106
|
+
}
|
|
107
|
+
break;
|
|
108
|
+
case 'mknod':
|
|
109
|
+
model = await _contents.newUntitled({
|
|
110
|
+
path: PathExt.dirname(path),
|
|
111
|
+
type: Number.parseInt(request.data.mode) === DIR_MODE ? 'directory' : 'file',
|
|
112
|
+
ext: PathExt.extname(path),
|
|
113
|
+
});
|
|
114
|
+
await _contents.rename(model.path, path);
|
|
115
|
+
break;
|
|
116
|
+
case 'getattr': {
|
|
117
|
+
model = await _contents.get(path);
|
|
118
|
+
// create a default date for drives that send incomplete information
|
|
119
|
+
// for nested foldes and files
|
|
120
|
+
const defaultDate = new Date(0).toISOString();
|
|
121
|
+
|
|
122
|
+
response = {
|
|
123
|
+
dev: 1,
|
|
124
|
+
nlink: 1,
|
|
125
|
+
uid: 0,
|
|
126
|
+
gid: 0,
|
|
127
|
+
rdev: 0,
|
|
128
|
+
size: model.size || 0,
|
|
129
|
+
blksize: BLOCK_SIZE,
|
|
130
|
+
blocks: Math.ceil(model.size || 0 / BLOCK_SIZE),
|
|
131
|
+
atime: model.last_modified || defaultDate, // TODO Get the proper atime?
|
|
132
|
+
mtime: model.last_modified || defaultDate,
|
|
133
|
+
ctime: model.created || defaultDate,
|
|
134
|
+
timestamp: 0,
|
|
135
|
+
};
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
case 'get':
|
|
139
|
+
model = await _contents.get(path, { content: true });
|
|
140
|
+
|
|
141
|
+
if (model.type === 'directory') {
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
response = {
|
|
146
|
+
content:
|
|
147
|
+
model.format === 'json' ? JSON.stringify(model.content) : model.content,
|
|
148
|
+
format: model.format,
|
|
149
|
+
};
|
|
150
|
+
break;
|
|
151
|
+
case 'put':
|
|
152
|
+
await _contents.save(path, {
|
|
153
|
+
content:
|
|
154
|
+
request.data.format === 'json'
|
|
155
|
+
? JSON.parse(request.data.data)
|
|
156
|
+
: request.data.data,
|
|
157
|
+
type: 'file',
|
|
158
|
+
format: request.data.format as ServerContents.FileFormat,
|
|
159
|
+
});
|
|
160
|
+
break;
|
|
161
|
+
default:
|
|
162
|
+
response = null as never;
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
this._channel.postMessage(response);
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
protected _channel: BroadcastChannel | null = null;
|
|
170
|
+
protected _contents: ServerContents.IManager;
|
|
171
|
+
protected _enabled = false;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** A namespace for */
|
|
175
|
+
export namespace BroadcastChannelWrapper {
|
|
176
|
+
export interface IOptions {
|
|
177
|
+
contents: ServerContents.IManager;
|
|
178
|
+
}
|
|
179
|
+
export type TBroadcastResponse = any;
|
|
180
|
+
}
|