@jupyterlite/server 0.7.0-beta.0 → 0.7.0-rc.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/lib/index.d.ts +16 -2
- package/lib/index.js +16 -2
- package/lib/index.js.map +1 -1
- package/package.json +5 -18
- package/src/index.ts +22 -2
- package/lib/service-worker-manager.d.ts +0 -72
- package/lib/service-worker-manager.js +0 -199
- package/lib/service-worker-manager.js.map +0 -1
- package/lib/service-worker.d.ts +0 -58
- package/lib/service-worker.js +0 -152
- package/lib/service-worker.js.map +0 -1
- package/lib/tokens.d.ts +0 -63
- package/lib/tokens.js +0 -8
- package/lib/tokens.js.map +0 -1
- package/src/service-worker-manager.ts +0 -257
- package/src/service-worker.ts +0 -181
- package/src/tokens.ts +0 -85
- package/src/typings.d.ts +0 -4
package/lib/index.d.ts
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated The `@jupyterlite/server` package is deprecated.
|
|
3
|
+
* Please use `@jupyterlite/apputils` instead.
|
|
4
|
+
*
|
|
5
|
+
* This package now re-exports from `@jupyterlite/apputils` for backward compatibility.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // Before
|
|
10
|
+
* import { IServiceWorkerManager, ServiceWorkerManager } from '@jupyterlite/server';
|
|
11
|
+
*
|
|
12
|
+
* // After
|
|
13
|
+
* import { IServiceWorkerManager, ServiceWorkerManager } from '@jupyterlite/apputils';
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export { IServiceWorkerManager, ServiceWorkerManager, WORKER_NAME, SERVICE_WORKER_BROADCAST_CHANNEL_ID, } from '@jupyterlite/apputils';
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
// Copyright (c) Jupyter Development Team.
|
|
2
2
|
// Distributed under the terms of the Modified BSD License.
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated The `@jupyterlite/server` package is deprecated.
|
|
5
|
+
* Please use `@jupyterlite/apputils` instead.
|
|
6
|
+
*
|
|
7
|
+
* This package now re-exports from `@jupyterlite/apputils` for backward compatibility.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // Before
|
|
12
|
+
* import { IServiceWorkerManager, ServiceWorkerManager } from '@jupyterlite/server';
|
|
13
|
+
*
|
|
14
|
+
* // After
|
|
15
|
+
* import { IServiceWorkerManager, ServiceWorkerManager } from '@jupyterlite/apputils';
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export { IServiceWorkerManager, ServiceWorkerManager, WORKER_NAME, SERVICE_WORKER_BROADCAST_CHANNEL_ID, } from '@jupyterlite/apputils';
|
|
5
19
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,2DAA2D;AAE3D,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,2DAA2D;AAE3D;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EACL,qBAAqB,EACrB,oBAAoB,EACpB,WAAW,EACX,mCAAmC,GACpC,MAAM,uBAAuB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jupyterlite/server",
|
|
3
|
-
"version": "0.7.0-
|
|
4
|
-
"description": "JupyterLite - Server",
|
|
3
|
+
"version": "0.7.0-rc.0",
|
|
4
|
+
"description": "JupyterLite - Server (DEPRECATED: Use @jupyterlite/apputils instead)",
|
|
5
5
|
"homepage": "https://github.com/jupyterlite/jupyterlite",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/jupyterlite/jupyterlite/issues"
|
|
@@ -43,26 +43,12 @@
|
|
|
43
43
|
"watch": "tsc -b --watch"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@
|
|
47
|
-
"@jupyterlab/nbformat": "~4.5.0-beta.1",
|
|
48
|
-
"@jupyterlab/observables": "~5.5.0-beta.1",
|
|
49
|
-
"@jupyterlab/services": "~7.5.0-beta.1",
|
|
50
|
-
"@jupyterlab/settingregistry": "~4.5.0-beta.1",
|
|
51
|
-
"@jupyterlab/statedb": "~4.5.0-beta.1",
|
|
52
|
-
"@jupyterlite/contents": "^0.7.0-beta.0",
|
|
53
|
-
"@jupyterlite/kernel": "^0.7.0-beta.0",
|
|
54
|
-
"@jupyterlite/session": "^0.7.0-beta.0",
|
|
55
|
-
"@jupyterlite/settings": "^0.7.0-beta.0",
|
|
56
|
-
"@lumino/application": "^2.4.4",
|
|
57
|
-
"@lumino/coreutils": "^2.2.1",
|
|
58
|
-
"@lumino/signaling": "^2.1.4",
|
|
59
|
-
"@types/emscripten": "^1.39.6",
|
|
60
|
-
"mock-socket": "^9.3.1"
|
|
46
|
+
"@jupyterlite/apputils": "^0.7.0-rc.0"
|
|
61
47
|
},
|
|
62
48
|
"devDependencies": {
|
|
63
49
|
"@babel/core": "^7.11.6",
|
|
64
50
|
"@babel/preset-env": "^7.12.1",
|
|
65
|
-
"@jupyterlab/testutils": "~4.5.0
|
|
51
|
+
"@jupyterlab/testutils": "~4.5.0",
|
|
66
52
|
"@types/jest": "^29.5.3",
|
|
67
53
|
"@types/serviceworker": "^0.0.56",
|
|
68
54
|
"jest": "^29.6.2",
|
|
@@ -73,5 +59,6 @@
|
|
|
73
59
|
"publishConfig": {
|
|
74
60
|
"access": "public"
|
|
75
61
|
},
|
|
62
|
+
"deprecated": "This package is deprecated. Please use @jupyterlite/apputils instead.",
|
|
76
63
|
"styleModule": "style/index.js"
|
|
77
64
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
// Copyright (c) Jupyter Development Team.
|
|
2
2
|
// Distributed under the terms of the Modified BSD License.
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated The `@jupyterlite/server` package is deprecated.
|
|
6
|
+
* Please use `@jupyterlite/apputils` instead.
|
|
7
|
+
*
|
|
8
|
+
* This package now re-exports from `@jupyterlite/apputils` for backward compatibility.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Before
|
|
13
|
+
* import { IServiceWorkerManager, ServiceWorkerManager } from '@jupyterlite/server';
|
|
14
|
+
*
|
|
15
|
+
* // After
|
|
16
|
+
* import { IServiceWorkerManager, ServiceWorkerManager } from '@jupyterlite/apputils';
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export {
|
|
21
|
+
IServiceWorkerManager,
|
|
22
|
+
ServiceWorkerManager,
|
|
23
|
+
WORKER_NAME,
|
|
24
|
+
SERVICE_WORKER_BROADCAST_CHANNEL_ID,
|
|
25
|
+
} from '@jupyterlite/apputils';
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { ISignal } from '@lumino/signaling';
|
|
2
|
-
import { IServiceWorkerManager } from './tokens';
|
|
3
|
-
/**
|
|
4
|
-
* The service-worker broadcast channel id
|
|
5
|
-
*/
|
|
6
|
-
export declare const SERVICE_WORKER_BROADCAST_CHANNEL_ID = "/sw-api.v1";
|
|
7
|
-
/**
|
|
8
|
-
* A class that manages the ServiceWorker registration and communication,
|
|
9
|
-
* used for accessing the file system.
|
|
10
|
-
*/
|
|
11
|
-
export declare class ServiceWorkerManager implements IServiceWorkerManager {
|
|
12
|
-
/**
|
|
13
|
-
* Construct a new ServiceWorkerManager.
|
|
14
|
-
*/
|
|
15
|
-
constructor(options: IServiceWorkerManager.IOptions);
|
|
16
|
-
/**
|
|
17
|
-
* A signal emitted when the registration changes.
|
|
18
|
-
*/
|
|
19
|
-
get registrationChanged(): ISignal<IServiceWorkerManager, ServiceWorkerRegistration | null>;
|
|
20
|
-
/**
|
|
21
|
-
* Whether the ServiceWorker is enabled or not.
|
|
22
|
-
*/
|
|
23
|
-
get enabled(): boolean;
|
|
24
|
-
/**
|
|
25
|
-
* A unique id to identify the browsing context where the ServiceWorkerManager was instantiated.
|
|
26
|
-
*/
|
|
27
|
-
get browsingContextId(): string;
|
|
28
|
-
/**
|
|
29
|
-
* Whether the ServiceWorker is ready or not.
|
|
30
|
-
*/
|
|
31
|
-
get ready(): Promise<void>;
|
|
32
|
-
/**
|
|
33
|
-
* Register a handler for stdin requests received via ServiceWorker.
|
|
34
|
-
* @param pathnameSuffix URL pathname suffix to match such as "kernel" or "terminal".
|
|
35
|
-
* @param stdinHandler
|
|
36
|
-
*/
|
|
37
|
-
registerStdinHandler(pathnameSuffix: string, stdinHandler: IServiceWorkerManager.IStdinHandler): void;
|
|
38
|
-
/**
|
|
39
|
-
* Initialize the ServiceWorkerManager.
|
|
40
|
-
*/
|
|
41
|
-
private _initialize;
|
|
42
|
-
/**
|
|
43
|
-
* Unregister old service workers if the version has changed.
|
|
44
|
-
*/
|
|
45
|
-
private _unregisterOldServiceWorkers;
|
|
46
|
-
/**
|
|
47
|
-
* Ping the service worker to keep it alive.
|
|
48
|
-
*/
|
|
49
|
-
private _pingServiceWorker;
|
|
50
|
-
/**
|
|
51
|
-
* Set the registration and emit a signal.
|
|
52
|
-
*/
|
|
53
|
-
private _setRegistration;
|
|
54
|
-
/**
|
|
55
|
-
* Handle a message received on the BroadcastChannel
|
|
56
|
-
*
|
|
57
|
-
* Message data is `any` because it can either be a drive message of type `TDriveReqiest<T>`
|
|
58
|
-
* or a stdin message of type `any` as ServiceWorkerManager passes it through to the
|
|
59
|
-
* stdinHandler without understanding or altering it.
|
|
60
|
-
*/
|
|
61
|
-
private _onBroadcastMessage;
|
|
62
|
-
private _onDriveMessage;
|
|
63
|
-
private _onStdinMessage;
|
|
64
|
-
private _registration;
|
|
65
|
-
private _registrationChanged;
|
|
66
|
-
private _ready;
|
|
67
|
-
private _broadcastChannel;
|
|
68
|
-
private _browsingContextId;
|
|
69
|
-
private _contents;
|
|
70
|
-
private _driveContentsProcessor;
|
|
71
|
-
private _stdinHandlers;
|
|
72
|
-
}
|
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
|
|
2
|
-
import { DriveContentsProcessor, } from '@jupyterlite/contents';
|
|
3
|
-
import { PromiseDelegate, UUID } from '@lumino/coreutils';
|
|
4
|
-
import { Signal } from '@lumino/signaling';
|
|
5
|
-
import { WORKER_NAME } from './tokens';
|
|
6
|
-
/**
|
|
7
|
-
* The service-worker broadcast channel id
|
|
8
|
-
*/
|
|
9
|
-
export const SERVICE_WORKER_BROADCAST_CHANNEL_ID = '/sw-api.v1';
|
|
10
|
-
/**
|
|
11
|
-
* The version of the app
|
|
12
|
-
*/
|
|
13
|
-
const VERSION = PageConfig.getOption('appVersion');
|
|
14
|
-
/**
|
|
15
|
-
* Used to keep the service worker alive
|
|
16
|
-
*/
|
|
17
|
-
const SW_PING_ENDPOINT = '/api/service-worker-heartbeat';
|
|
18
|
-
/**
|
|
19
|
-
* A class that manages the ServiceWorker registration and communication,
|
|
20
|
-
* used for accessing the file system.
|
|
21
|
-
*/
|
|
22
|
-
export class ServiceWorkerManager {
|
|
23
|
-
/**
|
|
24
|
-
* Construct a new ServiceWorkerManager.
|
|
25
|
-
*/
|
|
26
|
-
constructor(options) {
|
|
27
|
-
var _a;
|
|
28
|
-
/**
|
|
29
|
-
* Handle a message received on the BroadcastChannel
|
|
30
|
-
*
|
|
31
|
-
* Message data is `any` because it can either be a drive message of type `TDriveReqiest<T>`
|
|
32
|
-
* or a stdin message of type `any` as ServiceWorkerManager passes it through to the
|
|
33
|
-
* stdinHandler without understanding or altering it.
|
|
34
|
-
*/
|
|
35
|
-
this._onBroadcastMessage = async (event) => {
|
|
36
|
-
const { data, browsingContextId, pathname } = event.data;
|
|
37
|
-
if (browsingContextId !== this._browsingContextId) {
|
|
38
|
-
// Message is not meant for us
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
if (pathname.includes('/api/stdin/')) {
|
|
42
|
-
this._onStdinMessage(pathname, data);
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
this._onDriveMessage(data);
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
this._onDriveMessage = async (data) => {
|
|
49
|
-
const response = await this._driveContentsProcessor.processDriveRequest(data);
|
|
50
|
-
// pass the browsingContextId along so the Service Worker can identify the request
|
|
51
|
-
this._broadcastChannel.postMessage({
|
|
52
|
-
response,
|
|
53
|
-
browsingContextId: this._browsingContextId,
|
|
54
|
-
});
|
|
55
|
-
};
|
|
56
|
-
this._onStdinMessage = async (pathname, data) => {
|
|
57
|
-
// Expecting pathname of the form '<optional something>/api/stdin/<suffix>' from which
|
|
58
|
-
// suffix is used to identify which stdinHandler to call.
|
|
59
|
-
// `data: any` because ServiceWorkerManager accepts any data and passes it through
|
|
60
|
-
// to the stdinHandler without understanding or altering it.
|
|
61
|
-
const suffix = pathname.slice(pathname.lastIndexOf('/') + 1);
|
|
62
|
-
const stdinHandler = this._stdinHandlers.get(suffix);
|
|
63
|
-
if (stdinHandler !== undefined) {
|
|
64
|
-
const response = await stdinHandler(data);
|
|
65
|
-
this._broadcastChannel.postMessage({
|
|
66
|
-
response,
|
|
67
|
-
browsingContextId: this._browsingContextId,
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
console.warn(`No stdin handler registered for '${pathname}'`);
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
this._registration = null;
|
|
75
|
-
this._registrationChanged = new Signal(this);
|
|
76
|
-
this._ready = new PromiseDelegate();
|
|
77
|
-
this._stdinHandlers = new Map();
|
|
78
|
-
const workerUrl = (_a = options.workerUrl) !== null && _a !== void 0 ? _a : URLExt.join(PageConfig.getBaseUrl(), WORKER_NAME);
|
|
79
|
-
const fullWorkerUrl = new URL(workerUrl, window.location.href);
|
|
80
|
-
const enableCache = PageConfig.getOption('enableServiceWorkerCache') || 'false';
|
|
81
|
-
fullWorkerUrl.searchParams.set('enableCache', enableCache);
|
|
82
|
-
// Initialize broadcast channel related properties
|
|
83
|
-
this._browsingContextId = UUID.uuid4();
|
|
84
|
-
this._contents = options.contents;
|
|
85
|
-
this._broadcastChannel = new BroadcastChannel(SERVICE_WORKER_BROADCAST_CHANNEL_ID);
|
|
86
|
-
this._broadcastChannel.addEventListener('message', this._onBroadcastMessage);
|
|
87
|
-
this._driveContentsProcessor = new DriveContentsProcessor({
|
|
88
|
-
contentsManager: this._contents,
|
|
89
|
-
});
|
|
90
|
-
void this._initialize(fullWorkerUrl.href).catch(console.warn);
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* A signal emitted when the registration changes.
|
|
94
|
-
*/
|
|
95
|
-
get registrationChanged() {
|
|
96
|
-
return this._registrationChanged;
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Whether the ServiceWorker is enabled or not.
|
|
100
|
-
*/
|
|
101
|
-
get enabled() {
|
|
102
|
-
return this._registration !== null;
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* A unique id to identify the browsing context where the ServiceWorkerManager was instantiated.
|
|
106
|
-
*/
|
|
107
|
-
get browsingContextId() {
|
|
108
|
-
return this._browsingContextId;
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Whether the ServiceWorker is ready or not.
|
|
112
|
-
*/
|
|
113
|
-
get ready() {
|
|
114
|
-
return this._ready.promise;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Register a handler for stdin requests received via ServiceWorker.
|
|
118
|
-
* @param pathnameSuffix URL pathname suffix to match such as "kernel" or "terminal".
|
|
119
|
-
* @param stdinHandler
|
|
120
|
-
*/
|
|
121
|
-
registerStdinHandler(pathnameSuffix, stdinHandler) {
|
|
122
|
-
this._stdinHandlers.set(pathnameSuffix, stdinHandler);
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Initialize the ServiceWorkerManager.
|
|
126
|
-
*/
|
|
127
|
-
async _initialize(workerUrl) {
|
|
128
|
-
const { serviceWorker } = navigator;
|
|
129
|
-
let registration = null;
|
|
130
|
-
if (!serviceWorker) {
|
|
131
|
-
console.warn('ServiceWorkers not supported in this browser');
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
else if (serviceWorker.controller) {
|
|
135
|
-
const scriptURL = serviceWorker.controller.scriptURL;
|
|
136
|
-
await this._unregisterOldServiceWorkers(scriptURL);
|
|
137
|
-
registration = (await serviceWorker.getRegistration(scriptURL)) || null;
|
|
138
|
-
// eslint-disable-next-line no-console
|
|
139
|
-
console.info('JupyterLite ServiceWorker was already registered');
|
|
140
|
-
}
|
|
141
|
-
if (!registration && serviceWorker) {
|
|
142
|
-
try {
|
|
143
|
-
// eslint-disable-next-line no-console
|
|
144
|
-
console.info('Registering new JupyterLite ServiceWorker', workerUrl);
|
|
145
|
-
registration = await serviceWorker.register(workerUrl);
|
|
146
|
-
// eslint-disable-next-line no-console
|
|
147
|
-
console.info('JupyterLite ServiceWorker was sucessfully registered');
|
|
148
|
-
}
|
|
149
|
-
catch (err) {
|
|
150
|
-
console.warn(err);
|
|
151
|
-
console.warn(`JupyterLite ServiceWorker registration unexpectedly failed: ${err}`);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
this._setRegistration(registration);
|
|
155
|
-
if (!registration) {
|
|
156
|
-
this._ready.reject(void 0);
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
this._ready.resolve(void 0);
|
|
160
|
-
setTimeout(this._pingServiceWorker, 20000);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Unregister old service workers if the version has changed.
|
|
165
|
-
*/
|
|
166
|
-
async _unregisterOldServiceWorkers(scriptURL) {
|
|
167
|
-
const versionKey = `${scriptURL}-version`;
|
|
168
|
-
// Check if we have an installed version. If we do, compare it to the current version
|
|
169
|
-
// and unregister all service workers if they are different.
|
|
170
|
-
const installedVersion = localStorage.getItem(versionKey);
|
|
171
|
-
if ((installedVersion && installedVersion !== VERSION) || !installedVersion) {
|
|
172
|
-
// eslint-disable-next-line no-console
|
|
173
|
-
console.info('New version, unregistering existing service workers.');
|
|
174
|
-
const registrations = await navigator.serviceWorker.getRegistrations();
|
|
175
|
-
await Promise.all(registrations.map((registration) => registration.unregister()));
|
|
176
|
-
// eslint-disable-next-line no-console
|
|
177
|
-
console.info('All existing service workers have been unregistered.');
|
|
178
|
-
}
|
|
179
|
-
localStorage.setItem(versionKey, VERSION);
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Ping the service worker to keep it alive.
|
|
183
|
-
*/
|
|
184
|
-
async _pingServiceWorker() {
|
|
185
|
-
const response = await fetch(SW_PING_ENDPOINT);
|
|
186
|
-
const text = await response.text();
|
|
187
|
-
if (text === 'ok') {
|
|
188
|
-
setTimeout(this._pingServiceWorker, 20000);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Set the registration and emit a signal.
|
|
193
|
-
*/
|
|
194
|
-
_setRegistration(registration) {
|
|
195
|
-
this._registration = registration;
|
|
196
|
-
this._registrationChanged.emit(this._registration);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
//# sourceMappingURL=service-worker-manager.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"service-worker-manager.js","sourceRoot":"","sources":["../src/service-worker-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAI3D,OAAO,EACL,sBAAsB,GAGvB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,mBA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,UAAU,CAAC,CAAC;QAE1D,IAAI,CAAC,gBAAgB,IAAI,gBAAgB,KAAK,OAAO,CAAC,IAAI,CAAC,gBAAgB,EAAE,CAAC;YAC5E,sCAAsC;YACtC,OAAO,CAAC,IAAI,CAAC,sDAAsD,CAAC,CAAC;YACrE,MAAM,aAAa,GAAG,MAAM,SAAS,CAAC,aAAa,CAAC,gBAAgB,EAAE,CAAC;YAEvE,MAAM,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,YAAY,EAAE,EAAE,CAAC,YAAY,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC;YAElF,sCAAsC;YACtC,OAAO,CAAC,IAAI,CAAC,sDAAsD,CAAC,CAAC;QACvE,CAAC;QAED,YAAY,CAAC,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAC5C,CAAC;IAED;;OAEG;IACK,KAAK,CAAC,kBAAkB;QAC9B,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,gBAAgB,CAAC,CAAC;QAC/C,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACnC,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YAClB,UAAU,CAAC,IAAI,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAC;QAC7C,CAAC;IACH,CAAC;IAED;;OAEG;IACK,gBAAgB,CAAC,YAA8C;QACrE,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;QAClC,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IACrD,CAAC;CAqEF"}
|
package/lib/service-worker.d.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The name of the cache
|
|
3
|
-
*/
|
|
4
|
-
declare const CACHE = "precache";
|
|
5
|
-
/**
|
|
6
|
-
* Communication channel with the main thread
|
|
7
|
-
*/
|
|
8
|
-
declare const broadcast: BroadcastChannel;
|
|
9
|
-
/**
|
|
10
|
-
* Whether to enable the cache
|
|
11
|
-
*/
|
|
12
|
-
declare let enableCache: boolean;
|
|
13
|
-
/**
|
|
14
|
-
* Handle installation with the cache
|
|
15
|
-
*/
|
|
16
|
-
declare function onInstall(event: ExtendableEvent): void;
|
|
17
|
-
/**
|
|
18
|
-
* Handle activation.
|
|
19
|
-
*/
|
|
20
|
-
declare function onActivate(event: ExtendableEvent): void;
|
|
21
|
-
/**
|
|
22
|
-
* Handle fetching a single resource.
|
|
23
|
-
*/
|
|
24
|
-
declare function onFetch(event: FetchEvent): Promise<void>;
|
|
25
|
-
/** Get a cached response, and update cache. */
|
|
26
|
-
declare function maybeFromCache(event: FetchEvent): Promise<Response>;
|
|
27
|
-
/**
|
|
28
|
-
* Restore a response from the cache based on the request.
|
|
29
|
-
*/
|
|
30
|
-
declare function fromCache(request: Request): Promise<Response | null>;
|
|
31
|
-
/**
|
|
32
|
-
* This is where we call the server to get the newest version of the
|
|
33
|
-
* file to use the next time we show view
|
|
34
|
-
*/
|
|
35
|
-
declare function refetch(request: Request): Promise<Response>;
|
|
36
|
-
/**
|
|
37
|
-
* Whether a given URL should be broadcast
|
|
38
|
-
*/
|
|
39
|
-
declare function shouldBroadcast(url: URL): boolean;
|
|
40
|
-
/**
|
|
41
|
-
* Whether the fallback behavior should be used
|
|
42
|
-
*/
|
|
43
|
-
declare function shouldDrop(request: Request, url: URL): boolean;
|
|
44
|
-
/**
|
|
45
|
-
* Forward request to main using the broadcast channel
|
|
46
|
-
*/
|
|
47
|
-
declare function broadcastOne(request: Request, url: URL): Promise<Response>;
|
|
48
|
-
declare function openCache(): Promise<Cache>;
|
|
49
|
-
/**
|
|
50
|
-
* Cache a request/response pair.
|
|
51
|
-
*/
|
|
52
|
-
declare function updateCache(request: Request, response: Response): Promise<void>;
|
|
53
|
-
/**
|
|
54
|
-
* Add all to the cache
|
|
55
|
-
*
|
|
56
|
-
* this is where we should (try to) add all relevant files
|
|
57
|
-
*/
|
|
58
|
-
declare function cacheAll(): Promise<void>;
|
package/lib/service-worker.js
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* The name of the cache
|
|
4
|
-
*/
|
|
5
|
-
const CACHE = 'precache';
|
|
6
|
-
/**
|
|
7
|
-
* Communication channel with the main thread
|
|
8
|
-
*/
|
|
9
|
-
const broadcast = new BroadcastChannel('/sw-api.v1');
|
|
10
|
-
/**
|
|
11
|
-
* Whether to enable the cache
|
|
12
|
-
*/
|
|
13
|
-
let enableCache = false;
|
|
14
|
-
/**
|
|
15
|
-
* Install event listeners
|
|
16
|
-
*/
|
|
17
|
-
self.addEventListener('install', onInstall);
|
|
18
|
-
self.addEventListener('activate', onActivate);
|
|
19
|
-
self.addEventListener('fetch', onFetch);
|
|
20
|
-
// Event handlers
|
|
21
|
-
/**
|
|
22
|
-
* Handle installation with the cache
|
|
23
|
-
*/
|
|
24
|
-
function onInstall(event) {
|
|
25
|
-
void self.skipWaiting();
|
|
26
|
-
event.waitUntil(cacheAll());
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Handle activation.
|
|
30
|
-
*/
|
|
31
|
-
function onActivate(event) {
|
|
32
|
-
// check if we should enable the cache
|
|
33
|
-
const searchParams = new URL(location.href).searchParams;
|
|
34
|
-
enableCache = searchParams.get('enableCache') === 'true';
|
|
35
|
-
event.waitUntil(self.clients.claim());
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Handle fetching a single resource.
|
|
39
|
-
*/
|
|
40
|
-
async function onFetch(event) {
|
|
41
|
-
const { request } = event;
|
|
42
|
-
const url = new URL(event.request.url);
|
|
43
|
-
if (url.pathname === '/api/service-worker-heartbeat') {
|
|
44
|
-
event.respondWith(new Response('ok'));
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
let responsePromise = null;
|
|
48
|
-
if (shouldBroadcast(url)) {
|
|
49
|
-
responsePromise = broadcastOne(request, url);
|
|
50
|
-
}
|
|
51
|
-
else if (!shouldDrop(request, url)) {
|
|
52
|
-
responsePromise = maybeFromCache(event);
|
|
53
|
-
}
|
|
54
|
-
if (responsePromise) {
|
|
55
|
-
event.respondWith(responsePromise);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
// utilities
|
|
59
|
-
/** Get a cached response, and update cache. */
|
|
60
|
-
async function maybeFromCache(event) {
|
|
61
|
-
const { request } = event;
|
|
62
|
-
if (!enableCache) {
|
|
63
|
-
return await fetch(request);
|
|
64
|
-
}
|
|
65
|
-
let response = await fromCache(request);
|
|
66
|
-
if (response) {
|
|
67
|
-
event.waitUntil(refetch(request));
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
response = await fetch(request);
|
|
71
|
-
event.waitUntil(updateCache(request, response.clone()));
|
|
72
|
-
}
|
|
73
|
-
return response;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Restore a response from the cache based on the request.
|
|
77
|
-
*/
|
|
78
|
-
async function fromCache(request) {
|
|
79
|
-
const cache = await openCache();
|
|
80
|
-
const response = await cache.match(request);
|
|
81
|
-
if (!response || response.status === 404) {
|
|
82
|
-
return null;
|
|
83
|
-
}
|
|
84
|
-
return response;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* This is where we call the server to get the newest version of the
|
|
88
|
-
* file to use the next time we show view
|
|
89
|
-
*/
|
|
90
|
-
async function refetch(request) {
|
|
91
|
-
const fromServer = await fetch(request);
|
|
92
|
-
await updateCache(request, fromServer);
|
|
93
|
-
return fromServer;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Whether a given URL should be broadcast
|
|
97
|
-
*/
|
|
98
|
-
function shouldBroadcast(url) {
|
|
99
|
-
return (url.origin === location.origin &&
|
|
100
|
-
(url.pathname.includes('/api/drive') || url.pathname.includes('/api/stdin/')));
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Whether the fallback behavior should be used
|
|
104
|
-
*/
|
|
105
|
-
function shouldDrop(request, url) {
|
|
106
|
-
return (request.method !== 'GET' ||
|
|
107
|
-
url.origin.match(/^http/) === null ||
|
|
108
|
-
url.pathname.includes('/api/'));
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Forward request to main using the broadcast channel
|
|
112
|
-
*/
|
|
113
|
-
async function broadcastOne(request, url) {
|
|
114
|
-
const message = await request.json();
|
|
115
|
-
const promise = new Promise((resolve) => {
|
|
116
|
-
const messageHandler = (event) => {
|
|
117
|
-
const data = event.data;
|
|
118
|
-
if (data.browsingContextId !== message.browsingContextId) {
|
|
119
|
-
// bail if the message is not for us
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
const response = data.response;
|
|
123
|
-
resolve(new Response(JSON.stringify(response)));
|
|
124
|
-
broadcast.removeEventListener('message', messageHandler);
|
|
125
|
-
};
|
|
126
|
-
broadcast.addEventListener('message', messageHandler);
|
|
127
|
-
});
|
|
128
|
-
// Add URL pathname to message
|
|
129
|
-
message.pathname = url.pathname;
|
|
130
|
-
broadcast.postMessage(message);
|
|
131
|
-
return await promise;
|
|
132
|
-
}
|
|
133
|
-
async function openCache() {
|
|
134
|
-
return await caches.open(CACHE);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Cache a request/response pair.
|
|
138
|
-
*/
|
|
139
|
-
async function updateCache(request, response) {
|
|
140
|
-
const cache = await openCache();
|
|
141
|
-
return cache.put(request, response);
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Add all to the cache
|
|
145
|
-
*
|
|
146
|
-
* this is where we should (try to) add all relevant files
|
|
147
|
-
*/
|
|
148
|
-
async function cacheAll() {
|
|
149
|
-
const cache = await openCache();
|
|
150
|
-
return await cache.addAll([]);
|
|
151
|
-
}
|
|
152
|
-
//# sourceMappingURL=service-worker.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"service-worker.js","sourceRoot":"","sources":["../src/service-worker.ts"],"names":[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AS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;YAChD,SAAS,CAAC,mBAAmB,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC;QAC3D,CAAC,CAAC;QAEF,SAAS,CAAC,gBAAgB,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC,CAAC,CAAC;IAEH,8BAA8B;IAC9B,OAAO,CAAC,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC;IAChC,SAAS,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IAE/B,OAAO,MAAM,OAAO,CAAC;AACvB,CAAC;AAED,KAAK,UAAU,SAAS;IACtB,OAAO,MAAM,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAClC,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,WAAW,CAAC,OAAgB,EAAE,QAAkB;IAC7D,MAAM,KAAK,GAAG,MAAM,SAAS,EAAE,CAAC;IAChC,OAAO,KAAK,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;AACtC,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,QAAQ;IACrB,MAAM,KAAK,GAAG,MAAM,SAAS,EAAE,CAAC;IAChC,OAAO,MAAM,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAChC,CAAC"}
|
package/lib/tokens.d.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { Token } from '@lumino/coreutils';
|
|
2
|
-
import { ISignal } from '@lumino/signaling';
|
|
3
|
-
import { Contents } from '@jupyterlab/services';
|
|
4
|
-
/**
|
|
5
|
-
* The token for the ServiceWorker.
|
|
6
|
-
*/
|
|
7
|
-
export declare const IServiceWorkerManager: Token<IServiceWorkerManager>;
|
|
8
|
-
/**
|
|
9
|
-
* The interface for the ServiceWorkerRegistration.
|
|
10
|
-
*/
|
|
11
|
-
export interface IServiceWorkerManager {
|
|
12
|
-
/**
|
|
13
|
-
* Signal fired when the registration changed.
|
|
14
|
-
*/
|
|
15
|
-
readonly registrationChanged: ISignal<IServiceWorkerManager, ServiceWorkerRegistration | null>;
|
|
16
|
-
/**
|
|
17
|
-
* Whether the ServiceWorker is enabled or not.
|
|
18
|
-
*/
|
|
19
|
-
readonly enabled: boolean;
|
|
20
|
-
/**
|
|
21
|
-
* A unique id to identify the browsing context where the ServiceWorkerManager was instantiated.
|
|
22
|
-
*/
|
|
23
|
-
readonly browsingContextId: string;
|
|
24
|
-
/**
|
|
25
|
-
* A Promise that resolves when the ServiceWorker is registered, or rejects if it cannot
|
|
26
|
-
*/
|
|
27
|
-
ready: Promise<void>;
|
|
28
|
-
/**
|
|
29
|
-
* Register a handler for stdin requests received via ServiceWorker.
|
|
30
|
-
* @param pathnameSuffix URL pathname suffix to match such as "kernel" or "terminal".
|
|
31
|
-
* @param stdinHandler
|
|
32
|
-
*/
|
|
33
|
-
registerStdinHandler(pathnameSuffix: string, stdinHandler: IServiceWorkerManager.IStdinHandler): void;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* A namespace for `ServiceWorkerManager` class.
|
|
37
|
-
*/
|
|
38
|
-
export declare namespace IServiceWorkerManager {
|
|
39
|
-
/**
|
|
40
|
-
* An options object for initializing a worker manager.
|
|
41
|
-
*/
|
|
42
|
-
interface IOptions {
|
|
43
|
-
/**
|
|
44
|
-
* The contents manager to use for handling drive contents requests
|
|
45
|
-
*/
|
|
46
|
-
contents: Contents.IManager;
|
|
47
|
-
/**
|
|
48
|
-
* URL to load the worker file. Default to "{baseURL}/service-worker.js"
|
|
49
|
-
*/
|
|
50
|
-
workerUrl?: string;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Interface for handler of stdin requests received via Service Worker.
|
|
54
|
-
*
|
|
55
|
-
* Types are `any` because they are defined by whatever calls `registerStdinHandler`
|
|
56
|
-
* and the ServiceWorkerManager does not need to understand them, it just passes them
|
|
57
|
-
* through without altering them.
|
|
58
|
-
*/
|
|
59
|
-
interface IStdinHandler {
|
|
60
|
-
(message: any): Promise<any>;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
export declare const WORKER_NAME: string;
|
package/lib/tokens.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Token } from '@lumino/coreutils';
|
|
2
|
-
import SW_URL from './service-worker?text';
|
|
3
|
-
/**
|
|
4
|
-
* The token for the ServiceWorker.
|
|
5
|
-
*/
|
|
6
|
-
export const IServiceWorkerManager = new Token('@jupyterlite/server:IServiceWorkerManager');
|
|
7
|
-
export const WORKER_NAME = `${SW_URL}`.split('/').slice(-1)[0];
|
|
8
|
-
//# sourceMappingURL=tokens.js.map
|
package/lib/tokens.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tokens.js","sourceRoot":"","sources":["../src/tokens.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAM1C,OAAO,MAAM,MAAM,uBAAuB,CAAC;AAE3C;;GAEG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,IAAI,KAAK,CAC5C,2CAA2C,CAC5C,CAAC;AAuEF,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,MAAM,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
|
|
2
|
-
|
|
3
|
-
import { Contents } from '@jupyterlab/services';
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
DriveContentsProcessor,
|
|
7
|
-
TDriveMethod,
|
|
8
|
-
TDriveRequest,
|
|
9
|
-
} from '@jupyterlite/contents';
|
|
10
|
-
|
|
11
|
-
import { PromiseDelegate, UUID } from '@lumino/coreutils';
|
|
12
|
-
|
|
13
|
-
import { ISignal, Signal } from '@lumino/signaling';
|
|
14
|
-
|
|
15
|
-
import { IServiceWorkerManager, WORKER_NAME } from './tokens';
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* The service-worker broadcast channel id
|
|
19
|
-
*/
|
|
20
|
-
export const SERVICE_WORKER_BROADCAST_CHANNEL_ID = '/sw-api.v1';
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* The version of the app
|
|
24
|
-
*/
|
|
25
|
-
const VERSION = PageConfig.getOption('appVersion');
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Used to keep the service worker alive
|
|
29
|
-
*/
|
|
30
|
-
const SW_PING_ENDPOINT = '/api/service-worker-heartbeat';
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* A class that manages the ServiceWorker registration and communication,
|
|
34
|
-
* used for accessing the file system.
|
|
35
|
-
*/
|
|
36
|
-
export class ServiceWorkerManager implements IServiceWorkerManager {
|
|
37
|
-
/**
|
|
38
|
-
* Construct a new ServiceWorkerManager.
|
|
39
|
-
*/
|
|
40
|
-
constructor(options: IServiceWorkerManager.IOptions) {
|
|
41
|
-
const workerUrl =
|
|
42
|
-
options.workerUrl ?? URLExt.join(PageConfig.getBaseUrl(), WORKER_NAME);
|
|
43
|
-
const fullWorkerUrl = new URL(workerUrl, window.location.href);
|
|
44
|
-
const enableCache = PageConfig.getOption('enableServiceWorkerCache') || 'false';
|
|
45
|
-
fullWorkerUrl.searchParams.set('enableCache', enableCache);
|
|
46
|
-
|
|
47
|
-
// Initialize broadcast channel related properties
|
|
48
|
-
this._browsingContextId = UUID.uuid4();
|
|
49
|
-
this._contents = options.contents;
|
|
50
|
-
this._broadcastChannel = new BroadcastChannel(SERVICE_WORKER_BROADCAST_CHANNEL_ID);
|
|
51
|
-
this._broadcastChannel.addEventListener('message', this._onBroadcastMessage);
|
|
52
|
-
|
|
53
|
-
this._driveContentsProcessor = new DriveContentsProcessor({
|
|
54
|
-
contentsManager: this._contents,
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
void this._initialize(fullWorkerUrl.href).catch(console.warn);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* A signal emitted when the registration changes.
|
|
62
|
-
*/
|
|
63
|
-
get registrationChanged(): ISignal<
|
|
64
|
-
IServiceWorkerManager,
|
|
65
|
-
ServiceWorkerRegistration | null
|
|
66
|
-
> {
|
|
67
|
-
return this._registrationChanged;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Whether the ServiceWorker is enabled or not.
|
|
72
|
-
*/
|
|
73
|
-
get enabled(): boolean {
|
|
74
|
-
return this._registration !== null;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* A unique id to identify the browsing context where the ServiceWorkerManager was instantiated.
|
|
79
|
-
*/
|
|
80
|
-
get browsingContextId(): string {
|
|
81
|
-
return this._browsingContextId;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Whether the ServiceWorker is ready or not.
|
|
86
|
-
*/
|
|
87
|
-
get ready(): Promise<void> {
|
|
88
|
-
return this._ready.promise;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Register a handler for stdin requests received via ServiceWorker.
|
|
93
|
-
* @param pathnameSuffix URL pathname suffix to match such as "kernel" or "terminal".
|
|
94
|
-
* @param stdinHandler
|
|
95
|
-
*/
|
|
96
|
-
registerStdinHandler(
|
|
97
|
-
pathnameSuffix: string,
|
|
98
|
-
stdinHandler: IServiceWorkerManager.IStdinHandler,
|
|
99
|
-
): void {
|
|
100
|
-
this._stdinHandlers.set(pathnameSuffix, stdinHandler);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Initialize the ServiceWorkerManager.
|
|
105
|
-
*/
|
|
106
|
-
private async _initialize(workerUrl: string): Promise<void> {
|
|
107
|
-
const { serviceWorker } = navigator;
|
|
108
|
-
|
|
109
|
-
let registration: ServiceWorkerRegistration | null = null;
|
|
110
|
-
|
|
111
|
-
if (!serviceWorker) {
|
|
112
|
-
console.warn('ServiceWorkers not supported in this browser');
|
|
113
|
-
return;
|
|
114
|
-
} else if (serviceWorker.controller) {
|
|
115
|
-
const scriptURL = serviceWorker.controller.scriptURL;
|
|
116
|
-
await this._unregisterOldServiceWorkers(scriptURL);
|
|
117
|
-
|
|
118
|
-
registration = (await serviceWorker.getRegistration(scriptURL)) || null;
|
|
119
|
-
// eslint-disable-next-line no-console
|
|
120
|
-
console.info('JupyterLite ServiceWorker was already registered');
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (!registration && serviceWorker) {
|
|
124
|
-
try {
|
|
125
|
-
// eslint-disable-next-line no-console
|
|
126
|
-
console.info('Registering new JupyterLite ServiceWorker', workerUrl);
|
|
127
|
-
registration = await serviceWorker.register(workerUrl);
|
|
128
|
-
// eslint-disable-next-line no-console
|
|
129
|
-
console.info('JupyterLite ServiceWorker was sucessfully registered');
|
|
130
|
-
} catch (err: any) {
|
|
131
|
-
console.warn(err);
|
|
132
|
-
console.warn(
|
|
133
|
-
`JupyterLite ServiceWorker registration unexpectedly failed: ${err}`,
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
this._setRegistration(registration);
|
|
139
|
-
|
|
140
|
-
if (!registration) {
|
|
141
|
-
this._ready.reject(void 0);
|
|
142
|
-
} else {
|
|
143
|
-
this._ready.resolve(void 0);
|
|
144
|
-
setTimeout(this._pingServiceWorker, 20000);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Unregister old service workers if the version has changed.
|
|
150
|
-
*/
|
|
151
|
-
private async _unregisterOldServiceWorkers(scriptURL: string): Promise<void> {
|
|
152
|
-
const versionKey = `${scriptURL}-version`;
|
|
153
|
-
// Check if we have an installed version. If we do, compare it to the current version
|
|
154
|
-
// and unregister all service workers if they are different.
|
|
155
|
-
const installedVersion = localStorage.getItem(versionKey);
|
|
156
|
-
|
|
157
|
-
if ((installedVersion && installedVersion !== VERSION) || !installedVersion) {
|
|
158
|
-
// eslint-disable-next-line no-console
|
|
159
|
-
console.info('New version, unregistering existing service workers.');
|
|
160
|
-
const registrations = await navigator.serviceWorker.getRegistrations();
|
|
161
|
-
|
|
162
|
-
await Promise.all(registrations.map((registration) => registration.unregister()));
|
|
163
|
-
|
|
164
|
-
// eslint-disable-next-line no-console
|
|
165
|
-
console.info('All existing service workers have been unregistered.');
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
localStorage.setItem(versionKey, VERSION);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Ping the service worker to keep it alive.
|
|
173
|
-
*/
|
|
174
|
-
private async _pingServiceWorker(): Promise<void> {
|
|
175
|
-
const response = await fetch(SW_PING_ENDPOINT);
|
|
176
|
-
const text = await response.text();
|
|
177
|
-
if (text === 'ok') {
|
|
178
|
-
setTimeout(this._pingServiceWorker, 20000);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Set the registration and emit a signal.
|
|
184
|
-
*/
|
|
185
|
-
private _setRegistration(registration: ServiceWorkerRegistration | null) {
|
|
186
|
-
this._registration = registration;
|
|
187
|
-
this._registrationChanged.emit(this._registration);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Handle a message received on the BroadcastChannel
|
|
192
|
-
*
|
|
193
|
-
* Message data is `any` because it can either be a drive message of type `TDriveReqiest<T>`
|
|
194
|
-
* or a stdin message of type `any` as ServiceWorkerManager passes it through to the
|
|
195
|
-
* stdinHandler without understanding or altering it.
|
|
196
|
-
*/
|
|
197
|
-
private _onBroadcastMessage = async (
|
|
198
|
-
event: MessageEvent<{
|
|
199
|
-
data: any;
|
|
200
|
-
browsingContextId: string;
|
|
201
|
-
pathname: string;
|
|
202
|
-
}>,
|
|
203
|
-
): Promise<void> => {
|
|
204
|
-
const { data, browsingContextId, pathname } = event.data;
|
|
205
|
-
|
|
206
|
-
if (browsingContextId !== this._browsingContextId) {
|
|
207
|
-
// Message is not meant for us
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (pathname.includes('/api/stdin/')) {
|
|
212
|
-
this._onStdinMessage(pathname, data);
|
|
213
|
-
} else {
|
|
214
|
-
this._onDriveMessage(data);
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
private _onDriveMessage = async <T extends TDriveMethod>(
|
|
219
|
-
data: TDriveRequest<T>,
|
|
220
|
-
): Promise<void> => {
|
|
221
|
-
const response = await this._driveContentsProcessor.processDriveRequest(data);
|
|
222
|
-
// pass the browsingContextId along so the Service Worker can identify the request
|
|
223
|
-
this._broadcastChannel.postMessage({
|
|
224
|
-
response,
|
|
225
|
-
browsingContextId: this._browsingContextId,
|
|
226
|
-
});
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
private _onStdinMessage = async (pathname: string, data: any): Promise<void> => {
|
|
230
|
-
// Expecting pathname of the form '<optional something>/api/stdin/<suffix>' from which
|
|
231
|
-
// suffix is used to identify which stdinHandler to call.
|
|
232
|
-
// `data: any` because ServiceWorkerManager accepts any data and passes it through
|
|
233
|
-
// to the stdinHandler without understanding or altering it.
|
|
234
|
-
const suffix = pathname.slice(pathname.lastIndexOf('/') + 1);
|
|
235
|
-
const stdinHandler = this._stdinHandlers.get(suffix);
|
|
236
|
-
if (stdinHandler !== undefined) {
|
|
237
|
-
const response = await stdinHandler(data);
|
|
238
|
-
this._broadcastChannel.postMessage({
|
|
239
|
-
response,
|
|
240
|
-
browsingContextId: this._browsingContextId,
|
|
241
|
-
});
|
|
242
|
-
} else {
|
|
243
|
-
console.warn(`No stdin handler registered for '${pathname}'`);
|
|
244
|
-
}
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
private _registration: ServiceWorkerRegistration | null = null;
|
|
248
|
-
private _registrationChanged = new Signal<this, ServiceWorkerRegistration | null>(
|
|
249
|
-
this,
|
|
250
|
-
);
|
|
251
|
-
private _ready = new PromiseDelegate<void>();
|
|
252
|
-
private _broadcastChannel: BroadcastChannel;
|
|
253
|
-
private _browsingContextId: string;
|
|
254
|
-
private _contents: Contents.IManager;
|
|
255
|
-
private _driveContentsProcessor: DriveContentsProcessor;
|
|
256
|
-
private _stdinHandlers = new Map<string, IServiceWorkerManager.IStdinHandler>();
|
|
257
|
-
}
|
package/src/service-worker.ts
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The name of the cache
|
|
3
|
-
*/
|
|
4
|
-
const CACHE = 'precache';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Communication channel with the main thread
|
|
8
|
-
*/
|
|
9
|
-
const broadcast = new BroadcastChannel('/sw-api.v1');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Whether to enable the cache
|
|
13
|
-
*/
|
|
14
|
-
let enableCache = false;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Install event listeners
|
|
18
|
-
*/
|
|
19
|
-
self.addEventListener('install', onInstall);
|
|
20
|
-
self.addEventListener('activate', onActivate);
|
|
21
|
-
self.addEventListener('fetch', onFetch);
|
|
22
|
-
|
|
23
|
-
// Event handlers
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Handle installation with the cache
|
|
27
|
-
*/
|
|
28
|
-
function onInstall(event: ExtendableEvent): void {
|
|
29
|
-
void self.skipWaiting();
|
|
30
|
-
event.waitUntil(cacheAll());
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Handle activation.
|
|
35
|
-
*/
|
|
36
|
-
function onActivate(event: ExtendableEvent): void {
|
|
37
|
-
// check if we should enable the cache
|
|
38
|
-
const searchParams = new URL(location.href).searchParams;
|
|
39
|
-
enableCache = searchParams.get('enableCache') === 'true';
|
|
40
|
-
event.waitUntil(self.clients.claim());
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Handle fetching a single resource.
|
|
45
|
-
*/
|
|
46
|
-
async function onFetch(event: FetchEvent): Promise<void> {
|
|
47
|
-
const { request } = event;
|
|
48
|
-
|
|
49
|
-
const url = new URL(event.request.url);
|
|
50
|
-
if (url.pathname === '/api/service-worker-heartbeat') {
|
|
51
|
-
event.respondWith(new Response('ok'));
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
let responsePromise: Promise<Response> | null = null;
|
|
56
|
-
if (shouldBroadcast(url)) {
|
|
57
|
-
responsePromise = broadcastOne(request, url);
|
|
58
|
-
} else if (!shouldDrop(request, url)) {
|
|
59
|
-
responsePromise = maybeFromCache(event);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (responsePromise) {
|
|
63
|
-
event.respondWith(responsePromise);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// utilities
|
|
68
|
-
|
|
69
|
-
/** Get a cached response, and update cache. */
|
|
70
|
-
async function maybeFromCache(event: FetchEvent): Promise<Response> {
|
|
71
|
-
const { request } = event;
|
|
72
|
-
|
|
73
|
-
if (!enableCache) {
|
|
74
|
-
return await fetch(request);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
let response: Response | null = await fromCache(request);
|
|
78
|
-
|
|
79
|
-
if (response) {
|
|
80
|
-
event.waitUntil(refetch(request));
|
|
81
|
-
} else {
|
|
82
|
-
response = await fetch(request);
|
|
83
|
-
event.waitUntil(updateCache(request, response.clone()));
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return response;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Restore a response from the cache based on the request.
|
|
91
|
-
*/
|
|
92
|
-
async function fromCache(request: Request): Promise<Response | null> {
|
|
93
|
-
const cache = await openCache();
|
|
94
|
-
const response = await cache.match(request);
|
|
95
|
-
|
|
96
|
-
if (!response || response.status === 404) {
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return response;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* This is where we call the server to get the newest version of the
|
|
105
|
-
* file to use the next time we show view
|
|
106
|
-
*/
|
|
107
|
-
async function refetch(request: Request): Promise<Response> {
|
|
108
|
-
const fromServer = await fetch(request);
|
|
109
|
-
await updateCache(request, fromServer);
|
|
110
|
-
return fromServer;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Whether a given URL should be broadcast
|
|
115
|
-
*/
|
|
116
|
-
function shouldBroadcast(url: URL): boolean {
|
|
117
|
-
return (
|
|
118
|
-
url.origin === location.origin &&
|
|
119
|
-
(url.pathname.includes('/api/drive') || url.pathname.includes('/api/stdin/'))
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Whether the fallback behavior should be used
|
|
125
|
-
*/
|
|
126
|
-
function shouldDrop(request: Request, url: URL): boolean {
|
|
127
|
-
return (
|
|
128
|
-
request.method !== 'GET' ||
|
|
129
|
-
url.origin.match(/^http/) === null ||
|
|
130
|
-
url.pathname.includes('/api/')
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Forward request to main using the broadcast channel
|
|
136
|
-
*/
|
|
137
|
-
async function broadcastOne(request: Request, url: URL): Promise<Response> {
|
|
138
|
-
const message = await request.json();
|
|
139
|
-
const promise = new Promise<Response>((resolve) => {
|
|
140
|
-
const messageHandler = (event: MessageEvent) => {
|
|
141
|
-
const data = event.data;
|
|
142
|
-
if (data.browsingContextId !== message.browsingContextId) {
|
|
143
|
-
// bail if the message is not for us
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
const response = data.response;
|
|
147
|
-
resolve(new Response(JSON.stringify(response)));
|
|
148
|
-
broadcast.removeEventListener('message', messageHandler);
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
broadcast.addEventListener('message', messageHandler);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
// Add URL pathname to message
|
|
155
|
-
message.pathname = url.pathname;
|
|
156
|
-
broadcast.postMessage(message);
|
|
157
|
-
|
|
158
|
-
return await promise;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
async function openCache(): Promise<Cache> {
|
|
162
|
-
return await caches.open(CACHE);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Cache a request/response pair.
|
|
167
|
-
*/
|
|
168
|
-
async function updateCache(request: Request, response: Response): Promise<void> {
|
|
169
|
-
const cache = await openCache();
|
|
170
|
-
return cache.put(request, response);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Add all to the cache
|
|
175
|
-
*
|
|
176
|
-
* this is where we should (try to) add all relevant files
|
|
177
|
-
*/
|
|
178
|
-
async function cacheAll() {
|
|
179
|
-
const cache = await openCache();
|
|
180
|
-
return await cache.addAll([]);
|
|
181
|
-
}
|
package/src/tokens.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { Token } from '@lumino/coreutils';
|
|
2
|
-
|
|
3
|
-
import { ISignal } from '@lumino/signaling';
|
|
4
|
-
|
|
5
|
-
import { Contents } from '@jupyterlab/services';
|
|
6
|
-
|
|
7
|
-
import SW_URL from './service-worker?text';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* The token for the ServiceWorker.
|
|
11
|
-
*/
|
|
12
|
-
export const IServiceWorkerManager = new Token<IServiceWorkerManager>(
|
|
13
|
-
'@jupyterlite/server:IServiceWorkerManager',
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* The interface for the ServiceWorkerRegistration.
|
|
18
|
-
*/
|
|
19
|
-
export interface IServiceWorkerManager {
|
|
20
|
-
/**
|
|
21
|
-
* Signal fired when the registration changed.
|
|
22
|
-
*/
|
|
23
|
-
readonly registrationChanged: ISignal<
|
|
24
|
-
IServiceWorkerManager,
|
|
25
|
-
ServiceWorkerRegistration | null
|
|
26
|
-
>;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Whether the ServiceWorker is enabled or not.
|
|
30
|
-
*/
|
|
31
|
-
readonly enabled: boolean;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* A unique id to identify the browsing context where the ServiceWorkerManager was instantiated.
|
|
35
|
-
*/
|
|
36
|
-
readonly browsingContextId: string;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* A Promise that resolves when the ServiceWorker is registered, or rejects if it cannot
|
|
40
|
-
*/
|
|
41
|
-
ready: Promise<void>;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Register a handler for stdin requests received via ServiceWorker.
|
|
45
|
-
* @param pathnameSuffix URL pathname suffix to match such as "kernel" or "terminal".
|
|
46
|
-
* @param stdinHandler
|
|
47
|
-
*/
|
|
48
|
-
registerStdinHandler(
|
|
49
|
-
pathnameSuffix: string,
|
|
50
|
-
stdinHandler: IServiceWorkerManager.IStdinHandler,
|
|
51
|
-
): void;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* A namespace for `ServiceWorkerManager` class.
|
|
56
|
-
*/
|
|
57
|
-
export namespace IServiceWorkerManager {
|
|
58
|
-
/**
|
|
59
|
-
* An options object for initializing a worker manager.
|
|
60
|
-
*/
|
|
61
|
-
export interface IOptions {
|
|
62
|
-
/**
|
|
63
|
-
* The contents manager to use for handling drive contents requests
|
|
64
|
-
*/
|
|
65
|
-
contents: Contents.IManager;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* URL to load the worker file. Default to "{baseURL}/service-worker.js"
|
|
69
|
-
*/
|
|
70
|
-
workerUrl?: string;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Interface for handler of stdin requests received via Service Worker.
|
|
75
|
-
*
|
|
76
|
-
* Types are `any` because they are defined by whatever calls `registerStdinHandler`
|
|
77
|
-
* and the ServiceWorkerManager does not need to understand them, it just passes them
|
|
78
|
-
* through without altering them.
|
|
79
|
-
*/
|
|
80
|
-
export interface IStdinHandler {
|
|
81
|
-
(message: any): Promise<any>;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export const WORKER_NAME = `${SW_URL}`.split('/').slice(-1)[0];
|
package/src/typings.d.ts
DELETED