@jupyterlite/server 0.6.0-alpha.7 → 0.6.0-alpha.9
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/service-worker-manager.d.ts +17 -11
- package/lib/service-worker-manager.js +63 -53
- package/lib/service-worker-manager.js.map +1 -1
- package/lib/service-worker.d.ts +1 -3
- package/lib/service-worker.js +13 -14
- package/lib/service-worker.js.map +1 -1
- package/lib/tokens.d.ts +6 -2
- package/lib/tokens.js.map +1 -1
- package/package.json +16 -16
- package/src/service-worker-manager.ts +64 -48
- package/src/service-worker.ts +14 -16
- package/src/tokens.ts +8 -3
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ISignal } from '@lumino/signaling';
|
|
2
|
-
import { TDriveMethod, TDriveRequest } from '@jupyterlite/contents';
|
|
3
2
|
import { IServiceWorkerManager } from './tokens';
|
|
4
3
|
/**
|
|
5
|
-
* A class that manages the
|
|
4
|
+
* A class that manages the ServiceWorker registration and communication,
|
|
5
|
+
* used for accessing the file system.
|
|
6
6
|
*/
|
|
7
7
|
export declare class ServiceWorkerManager implements IServiceWorkerManager {
|
|
8
8
|
/**
|
|
@@ -18,32 +18,38 @@ export declare class ServiceWorkerManager implements IServiceWorkerManager {
|
|
|
18
18
|
*/
|
|
19
19
|
get enabled(): boolean;
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* A unique id to identify the browsing context where the ServiceWorkerManager was instantiated.
|
|
22
22
|
*/
|
|
23
|
-
get
|
|
23
|
+
get browsingContextId(): string;
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
25
|
+
* Whether the ServiceWorker is ready or not.
|
|
26
26
|
*/
|
|
27
|
-
|
|
27
|
+
get ready(): Promise<void>;
|
|
28
28
|
/**
|
|
29
|
-
* Initialize the
|
|
29
|
+
* Initialize the ServiceWorkerManager.
|
|
30
30
|
*/
|
|
31
31
|
private _initialize;
|
|
32
32
|
/**
|
|
33
|
-
* Unregister
|
|
33
|
+
* Unregister old service workers if the version has changed.
|
|
34
34
|
*/
|
|
35
35
|
private _unregisterOldServiceWorkers;
|
|
36
36
|
/**
|
|
37
|
-
* Ping the
|
|
37
|
+
* Ping the service worker to keep it alive.
|
|
38
38
|
*/
|
|
39
39
|
private _pingServiceWorker;
|
|
40
40
|
/**
|
|
41
|
-
* Set the
|
|
41
|
+
* Set the registration and emit a signal.
|
|
42
42
|
*/
|
|
43
43
|
private _setRegistration;
|
|
44
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Handle a message received on the BroadcastChannel
|
|
46
|
+
*/
|
|
47
|
+
private _onBroadcastMessage;
|
|
45
48
|
private _registration;
|
|
46
49
|
private _registrationChanged;
|
|
47
50
|
private _ready;
|
|
51
|
+
private _broadcastChannel;
|
|
52
|
+
private _browsingContextId;
|
|
53
|
+
private _contents;
|
|
48
54
|
private _driveContentsProcessor;
|
|
49
55
|
}
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
|
|
2
|
-
import {
|
|
2
|
+
import { DRIVE_API_PATH, DriveContentsProcessor, } from '@jupyterlite/contents';
|
|
3
|
+
import { PromiseDelegate, UUID } from '@lumino/coreutils';
|
|
3
4
|
import { Signal } from '@lumino/signaling';
|
|
4
|
-
import { DriveContentsProcessor, } from '@jupyterlite/contents';
|
|
5
5
|
import { WORKER_NAME } from './tokens';
|
|
6
6
|
/**
|
|
7
|
-
* The version of the app
|
|
7
|
+
* The version of the app
|
|
8
8
|
*/
|
|
9
9
|
const VERSION = PageConfig.getOption('appVersion');
|
|
10
10
|
/**
|
|
11
|
-
* Used to keep the
|
|
11
|
+
* Used to keep the service worker alive
|
|
12
12
|
*/
|
|
13
13
|
const SW_PING_ENDPOINT = '/api/service-worker-heartbeat';
|
|
14
14
|
/**
|
|
15
|
-
* A class that manages the
|
|
15
|
+
* A class that manages the ServiceWorker registration and communication,
|
|
16
|
+
* used for accessing the file system.
|
|
16
17
|
*/
|
|
17
18
|
export class ServiceWorkerManager {
|
|
18
19
|
/**
|
|
@@ -21,55 +22,36 @@ export class ServiceWorkerManager {
|
|
|
21
22
|
constructor(options) {
|
|
22
23
|
var _a;
|
|
23
24
|
/**
|
|
24
|
-
* Handle a message received on the
|
|
25
|
+
* Handle a message received on the BroadcastChannel
|
|
25
26
|
*/
|
|
26
|
-
this.
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Unregister previous service workers if the version has changed.
|
|
33
|
-
*/
|
|
34
|
-
this._unregisterOldServiceWorkers = async (scriptURL) => {
|
|
35
|
-
const versionKey = `${scriptURL}-version`;
|
|
36
|
-
// Check if we have an installed version. If we do, compare it to the current version
|
|
37
|
-
// and unregister all service workers if they are different.
|
|
38
|
-
const installedVersion = localStorage.getItem(versionKey);
|
|
39
|
-
if ((installedVersion && installedVersion !== VERSION) || !installedVersion) {
|
|
40
|
-
// eslint-disable-next-line no-console
|
|
41
|
-
console.info('New version, unregistering existing service workers.');
|
|
42
|
-
const registrations = await navigator.serviceWorker.getRegistrations();
|
|
43
|
-
await Promise.all(registrations.map((registration) => registration.unregister()));
|
|
44
|
-
// eslint-disable-next-line no-console
|
|
45
|
-
console.info('All existing service workers have been unregistered.');
|
|
46
|
-
}
|
|
47
|
-
localStorage.setItem(versionKey, VERSION);
|
|
48
|
-
};
|
|
49
|
-
/**
|
|
50
|
-
* Ping the Service Worker to keep it alive.
|
|
51
|
-
*/
|
|
52
|
-
this._pingServiceWorker = async () => {
|
|
53
|
-
const response = await fetch(SW_PING_ENDPOINT);
|
|
54
|
-
const text = await response.text();
|
|
55
|
-
if (text === 'ok') {
|
|
56
|
-
setTimeout(this._pingServiceWorker, 20000);
|
|
27
|
+
this._onBroadcastMessage = async (event) => {
|
|
28
|
+
const { data, browsingContextId } = event.data;
|
|
29
|
+
if (browsingContextId !== this._browsingContextId) {
|
|
30
|
+
// Message is not meant for us
|
|
31
|
+
return;
|
|
57
32
|
}
|
|
33
|
+
const response = await this._driveContentsProcessor.processDriveRequest(data);
|
|
34
|
+
// pass the browsingContextId along so the Service Worker can identify the request
|
|
35
|
+
this._broadcastChannel.postMessage({
|
|
36
|
+
response,
|
|
37
|
+
browsingContextId: this._browsingContextId,
|
|
38
|
+
});
|
|
58
39
|
};
|
|
59
40
|
this._registration = null;
|
|
60
41
|
this._registrationChanged = new Signal(this);
|
|
61
42
|
this._ready = new PromiseDelegate();
|
|
62
|
-
|
|
63
|
-
// listen to messages from the Service Worker
|
|
64
|
-
this._messageChannel.port1.onmessage = this._onMessage;
|
|
65
|
-
const contents = options.contents;
|
|
66
|
-
this._driveContentsProcessor = new DriveContentsProcessor({
|
|
67
|
-
contentsManager: contents,
|
|
68
|
-
});
|
|
69
|
-
const workerUrl = (_a = options === null || options === void 0 ? void 0 : options.workerUrl) !== null && _a !== void 0 ? _a : URLExt.join(PageConfig.getBaseUrl(), WORKER_NAME);
|
|
43
|
+
const workerUrl = (_a = options.workerUrl) !== null && _a !== void 0 ? _a : URLExt.join(PageConfig.getBaseUrl(), WORKER_NAME);
|
|
70
44
|
const fullWorkerUrl = new URL(workerUrl, window.location.href);
|
|
71
45
|
const enableCache = PageConfig.getOption('enableServiceWorkerCache') || 'false';
|
|
72
46
|
fullWorkerUrl.searchParams.set('enableCache', enableCache);
|
|
47
|
+
// Initialize broadcast channel related properties
|
|
48
|
+
this._browsingContextId = UUID.uuid4();
|
|
49
|
+
this._contents = options.contents;
|
|
50
|
+
this._broadcastChannel = new BroadcastChannel(DRIVE_API_PATH);
|
|
51
|
+
this._broadcastChannel.addEventListener('message', this._onBroadcastMessage);
|
|
52
|
+
this._driveContentsProcessor = new DriveContentsProcessor({
|
|
53
|
+
contentsManager: this._contents,
|
|
54
|
+
});
|
|
73
55
|
void this._initialize(fullWorkerUrl.href).catch(console.warn);
|
|
74
56
|
}
|
|
75
57
|
/**
|
|
@@ -84,6 +66,12 @@ export class ServiceWorkerManager {
|
|
|
84
66
|
get enabled() {
|
|
85
67
|
return this._registration !== null;
|
|
86
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* A unique id to identify the browsing context where the ServiceWorkerManager was instantiated.
|
|
71
|
+
*/
|
|
72
|
+
get browsingContextId() {
|
|
73
|
+
return this._browsingContextId;
|
|
74
|
+
}
|
|
87
75
|
/**
|
|
88
76
|
* Whether the ServiceWorker is ready or not.
|
|
89
77
|
*/
|
|
@@ -91,15 +79,13 @@ export class ServiceWorkerManager {
|
|
|
91
79
|
return this._ready.promise;
|
|
92
80
|
}
|
|
93
81
|
/**
|
|
94
|
-
* Initialize the
|
|
82
|
+
* Initialize the ServiceWorkerManager.
|
|
95
83
|
*/
|
|
96
84
|
async _initialize(workerUrl) {
|
|
97
|
-
var _a;
|
|
98
85
|
const { serviceWorker } = navigator;
|
|
99
86
|
let registration = null;
|
|
100
87
|
if (!serviceWorker) {
|
|
101
88
|
console.warn('ServiceWorkers not supported in this browser');
|
|
102
|
-
this._ready.reject(void 0);
|
|
103
89
|
return;
|
|
104
90
|
}
|
|
105
91
|
else if (serviceWorker.controller) {
|
|
@@ -123,10 +109,6 @@ export class ServiceWorkerManager {
|
|
|
123
109
|
}
|
|
124
110
|
}
|
|
125
111
|
this._setRegistration(registration);
|
|
126
|
-
// transfer the port for communication with the Service Worker
|
|
127
|
-
void ((_a = serviceWorker.controller) === null || _a === void 0 ? void 0 : _a.postMessage({
|
|
128
|
-
type: 'INIT_PORT',
|
|
129
|
-
}, [this._messageChannel.port2]));
|
|
130
112
|
if (!registration) {
|
|
131
113
|
this._ready.reject(void 0);
|
|
132
114
|
}
|
|
@@ -136,7 +118,35 @@ export class ServiceWorkerManager {
|
|
|
136
118
|
}
|
|
137
119
|
}
|
|
138
120
|
/**
|
|
139
|
-
*
|
|
121
|
+
* Unregister old service workers if the version has changed.
|
|
122
|
+
*/
|
|
123
|
+
async _unregisterOldServiceWorkers(scriptURL) {
|
|
124
|
+
const versionKey = `${scriptURL}-version`;
|
|
125
|
+
// Check if we have an installed version. If we do, compare it to the current version
|
|
126
|
+
// and unregister all service workers if they are different.
|
|
127
|
+
const installedVersion = localStorage.getItem(versionKey);
|
|
128
|
+
if ((installedVersion && installedVersion !== VERSION) || !installedVersion) {
|
|
129
|
+
// eslint-disable-next-line no-console
|
|
130
|
+
console.info('New version, unregistering existing service workers.');
|
|
131
|
+
const registrations = await navigator.serviceWorker.getRegistrations();
|
|
132
|
+
await Promise.all(registrations.map((registration) => registration.unregister()));
|
|
133
|
+
// eslint-disable-next-line no-console
|
|
134
|
+
console.info('All existing service workers have been unregistered.');
|
|
135
|
+
}
|
|
136
|
+
localStorage.setItem(versionKey, VERSION);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Ping the service worker to keep it alive.
|
|
140
|
+
*/
|
|
141
|
+
async _pingServiceWorker() {
|
|
142
|
+
const response = await fetch(SW_PING_ENDPOINT);
|
|
143
|
+
const text = await response.text();
|
|
144
|
+
if (text === 'ok') {
|
|
145
|
+
setTimeout(this._pingServiceWorker, 20000);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Set the registration and emit a signal.
|
|
140
150
|
*/
|
|
141
151
|
_setRegistration(registration) {
|
|
142
152
|
this._registration = registration;
|
|
@@ -1 +1 @@
|
|
|
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;
|
|
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,cAAc,EACd,sBAAsB,GAGvB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAE1D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,CAAC,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;CAmCF"}
|
package/lib/service-worker.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="serviceworker" />
|
|
2
1
|
/**
|
|
3
2
|
* The name of the cache
|
|
4
3
|
*/
|
|
@@ -6,8 +5,7 @@ declare const CACHE = "precache";
|
|
|
6
5
|
/**
|
|
7
6
|
* Communication channel for drive access
|
|
8
7
|
*/
|
|
9
|
-
declare
|
|
10
|
-
declare const ready: Promise<void>;
|
|
8
|
+
declare const broadcast: BroadcastChannel;
|
|
11
9
|
/**
|
|
12
10
|
* Whether to enable the cache
|
|
13
11
|
*/
|
package/lib/service-worker.js
CHANGED
|
@@ -8,15 +8,7 @@ const CACHE = 'precache';
|
|
|
8
8
|
/**
|
|
9
9
|
* Communication channel for drive access
|
|
10
10
|
*/
|
|
11
|
-
|
|
12
|
-
const ready = new Promise((resolve) => {
|
|
13
|
-
self.addEventListener('message', (event) => {
|
|
14
|
-
if (event.data && event.data.type === 'INIT_PORT') {
|
|
15
|
-
messagePort = event.ports[0];
|
|
16
|
-
resolve();
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
});
|
|
11
|
+
const broadcast = new BroadcastChannel('/api/drive.v1');
|
|
20
12
|
/**
|
|
21
13
|
* Whether to enable the cache
|
|
22
14
|
*/
|
|
@@ -120,14 +112,21 @@ function shouldDrop(request, url) {
|
|
|
120
112
|
* Forward request to main using the broadcast channel
|
|
121
113
|
*/
|
|
122
114
|
async function broadcastOne(request) {
|
|
123
|
-
await
|
|
115
|
+
const message = await request.json();
|
|
124
116
|
const promise = new Promise((resolve) => {
|
|
125
|
-
|
|
126
|
-
|
|
117
|
+
const messageHandler = (event) => {
|
|
118
|
+
const data = event.data;
|
|
119
|
+
if (data.browsingContextId !== message.browsingContextId) {
|
|
120
|
+
// bail if the message is not for us
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const response = data.response;
|
|
124
|
+
resolve(new Response(JSON.stringify(response)));
|
|
125
|
+
broadcast.removeEventListener('message', messageHandler);
|
|
127
126
|
};
|
|
127
|
+
broadcast.addEventListener('message', messageHandler);
|
|
128
128
|
});
|
|
129
|
-
|
|
130
|
-
messagePort.postMessage(message);
|
|
129
|
+
broadcast.postMessage(message);
|
|
131
130
|
return await promise;
|
|
132
131
|
}
|
|
133
132
|
async function openCache() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service-worker.js","sourceRoot":"","sources":["../src/service-worker.ts"],"names":[],"mappings":";AAAA,qEAAqE;AACrE,8EAA8E;AAE9E;;GAEG;AACH,MAAM,KAAK,GAAG,UAAU,CAAC;AAEzB;;GAEG;AACH,
|
|
1
|
+
{"version":3,"file":"service-worker.js","sourceRoot":"","sources":["../src/service-worker.ts"],"names":[],"mappings":";AAAA,qEAAqE;AACrE,8EAA8E;AAE9E;;GAEG;AACH,MAAM,KAAK,GAAG,UAAU,CAAC;AAEzB;;GAEG;AACH,MAAM,SAAS,GAAG,IAAI,gBAAgB,CAAC,eAAe,CAAC,CAAC;AAExD;;GAEG;AACH,IAAI,WAAW,GAAG,KAAK,CAAC;AAExB;;GAEG;AACH,IAAI,CAAC,gBAAgB,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;AAC5C,IAAI,CAAC,gBAAgB,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;AAC9C,IAAI,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAExC,iBAAiB;AAEjB;;GAEG;AACH,SAAS,SAAS,CAAC,KAAsB;IACvC,KAAK,IAAI,CAAC,WAAW,EAAE,CAAC;IACxB,KAAK,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAC;AAC9B,CAAC;AAED;;GAEG;AACH,SAAS,UAAU,CAAC,KAAsB;IACxC,sCAAsC;IACtC,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,YAAY,CAAC;IACzD,WAAW,GAAG,YAAY,CAAC,GAAG,CAAC,aAAa,CAAC,KAAK,MAAM,CAAC;IACzD,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;AACxC,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,OAAO,CAAC,KAAiB;IACtC,MAAM,EAAE,OAAO,EAAE,GAAG,KAAK,CAAC;IAE1B,M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AC,iBAAiB,EAAE,CAAC;gBACzD,oCAAoC;gBACpC,OAAO;YACT,CAAC;YACD,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC;YAC/B,OAAO,CAAC,IAAI,QAAQ,CAAC,IAAI,CAAC,SAAS,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,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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Contents } from '@jupyterlab/services';
|
|
2
1
|
import { Token } from '@lumino/coreutils';
|
|
3
2
|
import { ISignal } from '@lumino/signaling';
|
|
3
|
+
import { Contents } from '@jupyterlab/services';
|
|
4
4
|
/**
|
|
5
5
|
* The token for the ServiceWorker.
|
|
6
6
|
*/
|
|
@@ -17,6 +17,10 @@ export interface IServiceWorkerManager {
|
|
|
17
17
|
* Whether the ServiceWorker is enabled or not.
|
|
18
18
|
*/
|
|
19
19
|
readonly enabled: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* A unique id to identify the browsing context where the ServiceWorkerManager was instantiated.
|
|
22
|
+
*/
|
|
23
|
+
readonly browsingContextId: string;
|
|
20
24
|
/**
|
|
21
25
|
* A Promise that resolves when the ServiceWorker is registered, or rejects if it cannot
|
|
22
26
|
*/
|
|
@@ -31,7 +35,7 @@ export declare namespace IServiceWorkerManager {
|
|
|
31
35
|
*/
|
|
32
36
|
interface IOptions {
|
|
33
37
|
/**
|
|
34
|
-
* The
|
|
38
|
+
* The contents manager to use for handling drive contents requests
|
|
35
39
|
*/
|
|
36
40
|
contents: Contents.IManager;
|
|
37
41
|
/**
|
package/lib/tokens.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tokens.js","sourceRoot":"","sources":["../src/tokens.ts"],"names":[],"mappings":"
|
|
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;AAkDF,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,MAAM,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jupyterlite/server",
|
|
3
|
-
"version": "0.6.0-alpha.
|
|
3
|
+
"version": "0.6.0-alpha.9",
|
|
4
4
|
"description": "JupyterLite - Server",
|
|
5
5
|
"homepage": "https://github.com/jupyterlite/jupyterlite",
|
|
6
6
|
"bugs": {
|
|
@@ -43,26 +43,26 @@
|
|
|
43
43
|
"watch": "tsc -b --watch"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@jupyterlab/coreutils": "~6.4.0
|
|
47
|
-
"@jupyterlab/nbformat": "~4.4.0
|
|
48
|
-
"@jupyterlab/observables": "~5.4.0
|
|
49
|
-
"@jupyterlab/services": "~7.4.0
|
|
50
|
-
"@jupyterlab/settingregistry": "~4.4.0
|
|
51
|
-
"@jupyterlab/statedb": "~4.4.0
|
|
52
|
-
"@jupyterlite/contents": "^0.6.0-alpha.
|
|
53
|
-
"@jupyterlite/kernel": "^0.6.0-alpha.
|
|
54
|
-
"@jupyterlite/session": "^0.6.0-alpha.
|
|
55
|
-
"@jupyterlite/settings": "^0.6.0-alpha.
|
|
56
|
-
"@jupyterlite/translation": "^0.6.0-alpha.
|
|
57
|
-
"@lumino/application": "^2.4.
|
|
58
|
-
"@lumino/coreutils": "^2.2.
|
|
59
|
-
"@lumino/signaling": "^2.1.
|
|
46
|
+
"@jupyterlab/coreutils": "~6.4.0",
|
|
47
|
+
"@jupyterlab/nbformat": "~4.4.0",
|
|
48
|
+
"@jupyterlab/observables": "~5.4.0",
|
|
49
|
+
"@jupyterlab/services": "~7.4.0",
|
|
50
|
+
"@jupyterlab/settingregistry": "~4.4.0",
|
|
51
|
+
"@jupyterlab/statedb": "~4.4.0",
|
|
52
|
+
"@jupyterlite/contents": "^0.6.0-alpha.9",
|
|
53
|
+
"@jupyterlite/kernel": "^0.6.0-alpha.9",
|
|
54
|
+
"@jupyterlite/session": "^0.6.0-alpha.9",
|
|
55
|
+
"@jupyterlite/settings": "^0.6.0-alpha.9",
|
|
56
|
+
"@jupyterlite/translation": "^0.6.0-alpha.9",
|
|
57
|
+
"@lumino/application": "^2.4.3",
|
|
58
|
+
"@lumino/coreutils": "^2.2.1",
|
|
59
|
+
"@lumino/signaling": "^2.1.4",
|
|
60
60
|
"mock-socket": "^9.3.1"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"@babel/core": "^7.11.6",
|
|
64
64
|
"@babel/preset-env": "^7.12.1",
|
|
65
|
-
"@jupyterlab/testutils": "~4.4.0
|
|
65
|
+
"@jupyterlab/testutils": "~4.4.0",
|
|
66
66
|
"@types/jest": "^29.5.3",
|
|
67
67
|
"@types/serviceworker": "^0.0.56",
|
|
68
68
|
"jest": "^29.6.2",
|
|
@@ -1,51 +1,55 @@
|
|
|
1
1
|
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
import { ISignal, Signal } from '@lumino/signaling';
|
|
3
|
+
import { Contents } from '@jupyterlab/services';
|
|
6
4
|
|
|
7
5
|
import {
|
|
6
|
+
DRIVE_API_PATH,
|
|
8
7
|
DriveContentsProcessor,
|
|
9
|
-
IDriveContentsProcessor,
|
|
10
8
|
TDriveMethod,
|
|
11
9
|
TDriveRequest,
|
|
12
10
|
} from '@jupyterlite/contents';
|
|
13
11
|
|
|
12
|
+
import { PromiseDelegate, UUID } from '@lumino/coreutils';
|
|
13
|
+
|
|
14
|
+
import { ISignal, Signal } from '@lumino/signaling';
|
|
15
|
+
|
|
14
16
|
import { IServiceWorkerManager, WORKER_NAME } from './tokens';
|
|
15
17
|
|
|
16
18
|
/**
|
|
17
|
-
* The version of the app
|
|
19
|
+
* The version of the app
|
|
18
20
|
*/
|
|
19
21
|
const VERSION = PageConfig.getOption('appVersion');
|
|
20
22
|
|
|
21
23
|
/**
|
|
22
|
-
* Used to keep the
|
|
24
|
+
* Used to keep the service worker alive
|
|
23
25
|
*/
|
|
24
26
|
const SW_PING_ENDPOINT = '/api/service-worker-heartbeat';
|
|
25
27
|
|
|
26
28
|
/**
|
|
27
|
-
* A class that manages the
|
|
29
|
+
* A class that manages the ServiceWorker registration and communication,
|
|
30
|
+
* used for accessing the file system.
|
|
28
31
|
*/
|
|
29
32
|
export class ServiceWorkerManager implements IServiceWorkerManager {
|
|
30
33
|
/**
|
|
31
34
|
* Construct a new ServiceWorkerManager.
|
|
32
35
|
*/
|
|
33
36
|
constructor(options: IServiceWorkerManager.IOptions) {
|
|
34
|
-
|
|
37
|
+
const workerUrl =
|
|
38
|
+
options.workerUrl ?? URLExt.join(PageConfig.getBaseUrl(), WORKER_NAME);
|
|
39
|
+
const fullWorkerUrl = new URL(workerUrl, window.location.href);
|
|
40
|
+
const enableCache = PageConfig.getOption('enableServiceWorkerCache') || 'false';
|
|
41
|
+
fullWorkerUrl.searchParams.set('enableCache', enableCache);
|
|
35
42
|
|
|
36
|
-
//
|
|
37
|
-
this.
|
|
43
|
+
// Initialize broadcast channel related properties
|
|
44
|
+
this._browsingContextId = UUID.uuid4();
|
|
45
|
+
this._contents = options.contents;
|
|
46
|
+
this._broadcastChannel = new BroadcastChannel(DRIVE_API_PATH);
|
|
47
|
+
this._broadcastChannel.addEventListener('message', this._onBroadcastMessage);
|
|
38
48
|
|
|
39
|
-
const contents = options.contents;
|
|
40
49
|
this._driveContentsProcessor = new DriveContentsProcessor({
|
|
41
|
-
contentsManager:
|
|
50
|
+
contentsManager: this._contents,
|
|
42
51
|
});
|
|
43
52
|
|
|
44
|
-
const workerUrl =
|
|
45
|
-
options?.workerUrl ?? URLExt.join(PageConfig.getBaseUrl(), WORKER_NAME);
|
|
46
|
-
const fullWorkerUrl = new URL(workerUrl, window.location.href);
|
|
47
|
-
const enableCache = PageConfig.getOption('enableServiceWorkerCache') || 'false';
|
|
48
|
-
fullWorkerUrl.searchParams.set('enableCache', enableCache);
|
|
49
53
|
void this._initialize(fullWorkerUrl.href).catch(console.warn);
|
|
50
54
|
}
|
|
51
55
|
|
|
@@ -67,26 +71,21 @@ export class ServiceWorkerManager implements IServiceWorkerManager {
|
|
|
67
71
|
}
|
|
68
72
|
|
|
69
73
|
/**
|
|
70
|
-
*
|
|
74
|
+
* A unique id to identify the browsing context where the ServiceWorkerManager was instantiated.
|
|
71
75
|
*/
|
|
72
|
-
get
|
|
73
|
-
return this.
|
|
76
|
+
get browsingContextId(): string {
|
|
77
|
+
return this._browsingContextId;
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
/**
|
|
77
|
-
*
|
|
81
|
+
* Whether the ServiceWorker is ready or not.
|
|
78
82
|
*/
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const request = event.data;
|
|
83
|
-
const response = await this._driveContentsProcessor.processDriveRequest(request);
|
|
84
|
-
|
|
85
|
-
this._messageChannel.port1.postMessage(response);
|
|
86
|
-
};
|
|
83
|
+
get ready(): Promise<void> {
|
|
84
|
+
return this._ready.promise;
|
|
85
|
+
}
|
|
87
86
|
|
|
88
87
|
/**
|
|
89
|
-
* Initialize the
|
|
88
|
+
* Initialize the ServiceWorkerManager.
|
|
90
89
|
*/
|
|
91
90
|
private async _initialize(workerUrl: string): Promise<void> {
|
|
92
91
|
const { serviceWorker } = navigator;
|
|
@@ -95,7 +94,6 @@ export class ServiceWorkerManager implements IServiceWorkerManager {
|
|
|
95
94
|
|
|
96
95
|
if (!serviceWorker) {
|
|
97
96
|
console.warn('ServiceWorkers not supported in this browser');
|
|
98
|
-
this._ready.reject(void 0);
|
|
99
97
|
return;
|
|
100
98
|
} else if (serviceWorker.controller) {
|
|
101
99
|
const scriptURL = serviceWorker.controller.scriptURL;
|
|
@@ -123,14 +121,6 @@ export class ServiceWorkerManager implements IServiceWorkerManager {
|
|
|
123
121
|
|
|
124
122
|
this._setRegistration(registration);
|
|
125
123
|
|
|
126
|
-
// transfer the port for communication with the Service Worker
|
|
127
|
-
void serviceWorker.controller?.postMessage(
|
|
128
|
-
{
|
|
129
|
-
type: 'INIT_PORT',
|
|
130
|
-
},
|
|
131
|
-
[this._messageChannel.port2],
|
|
132
|
-
);
|
|
133
|
-
|
|
134
124
|
if (!registration) {
|
|
135
125
|
this._ready.reject(void 0);
|
|
136
126
|
} else {
|
|
@@ -140,9 +130,9 @@ export class ServiceWorkerManager implements IServiceWorkerManager {
|
|
|
140
130
|
}
|
|
141
131
|
|
|
142
132
|
/**
|
|
143
|
-
* Unregister
|
|
133
|
+
* Unregister old service workers if the version has changed.
|
|
144
134
|
*/
|
|
145
|
-
private
|
|
135
|
+
private async _unregisterOldServiceWorkers(scriptURL: string): Promise<void> {
|
|
146
136
|
const versionKey = `${scriptURL}-version`;
|
|
147
137
|
// Check if we have an installed version. If we do, compare it to the current version
|
|
148
138
|
// and unregister all service workers if they are different.
|
|
@@ -160,32 +150,58 @@ export class ServiceWorkerManager implements IServiceWorkerManager {
|
|
|
160
150
|
}
|
|
161
151
|
|
|
162
152
|
localStorage.setItem(versionKey, VERSION);
|
|
163
|
-
}
|
|
153
|
+
}
|
|
164
154
|
|
|
165
155
|
/**
|
|
166
|
-
* Ping the
|
|
156
|
+
* Ping the service worker to keep it alive.
|
|
167
157
|
*/
|
|
168
|
-
private
|
|
158
|
+
private async _pingServiceWorker(): Promise<void> {
|
|
169
159
|
const response = await fetch(SW_PING_ENDPOINT);
|
|
170
160
|
const text = await response.text();
|
|
171
161
|
if (text === 'ok') {
|
|
172
162
|
setTimeout(this._pingServiceWorker, 20000);
|
|
173
163
|
}
|
|
174
|
-
}
|
|
164
|
+
}
|
|
175
165
|
|
|
176
166
|
/**
|
|
177
|
-
* Set the
|
|
167
|
+
* Set the registration and emit a signal.
|
|
178
168
|
*/
|
|
179
169
|
private _setRegistration(registration: ServiceWorkerRegistration | null) {
|
|
180
170
|
this._registration = registration;
|
|
181
171
|
this._registrationChanged.emit(this._registration);
|
|
182
172
|
}
|
|
183
173
|
|
|
184
|
-
|
|
174
|
+
/**
|
|
175
|
+
* Handle a message received on the BroadcastChannel
|
|
176
|
+
*/
|
|
177
|
+
private _onBroadcastMessage = async <T extends TDriveMethod>(
|
|
178
|
+
event: MessageEvent<{
|
|
179
|
+
data: TDriveRequest<T>;
|
|
180
|
+
browsingContextId: string;
|
|
181
|
+
}>,
|
|
182
|
+
): Promise<void> => {
|
|
183
|
+
const { data, browsingContextId } = event.data;
|
|
184
|
+
|
|
185
|
+
if (browsingContextId !== this._browsingContextId) {
|
|
186
|
+
// Message is not meant for us
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const response = await this._driveContentsProcessor.processDriveRequest(data);
|
|
191
|
+
// pass the browsingContextId along so the Service Worker can identify the request
|
|
192
|
+
this._broadcastChannel.postMessage({
|
|
193
|
+
response,
|
|
194
|
+
browsingContextId: this._browsingContextId,
|
|
195
|
+
});
|
|
196
|
+
};
|
|
197
|
+
|
|
185
198
|
private _registration: ServiceWorkerRegistration | null = null;
|
|
186
199
|
private _registrationChanged = new Signal<this, ServiceWorkerRegistration | null>(
|
|
187
200
|
this,
|
|
188
201
|
);
|
|
189
202
|
private _ready = new PromiseDelegate<void>();
|
|
190
|
-
private
|
|
203
|
+
private _broadcastChannel: BroadcastChannel;
|
|
204
|
+
private _browsingContextId: string;
|
|
205
|
+
private _contents: Contents.IManager;
|
|
206
|
+
private _driveContentsProcessor: DriveContentsProcessor;
|
|
191
207
|
}
|
package/src/service-worker.ts
CHANGED
|
@@ -9,16 +9,7 @@ const CACHE = 'precache';
|
|
|
9
9
|
/**
|
|
10
10
|
* Communication channel for drive access
|
|
11
11
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const ready: Promise<void> = new Promise((resolve) => {
|
|
15
|
-
self.addEventListener('message', (event: ExtendableMessageEvent) => {
|
|
16
|
-
if (event.data && event.data.type === 'INIT_PORT') {
|
|
17
|
-
messagePort = event.ports[0];
|
|
18
|
-
resolve();
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
});
|
|
12
|
+
const broadcast = new BroadcastChannel('/api/drive.v1');
|
|
22
13
|
|
|
23
14
|
/**
|
|
24
15
|
* Whether to enable the cache
|
|
@@ -144,16 +135,23 @@ function shouldDrop(request: Request, url: URL): boolean {
|
|
|
144
135
|
* Forward request to main using the broadcast channel
|
|
145
136
|
*/
|
|
146
137
|
async function broadcastOne(request: Request): Promise<Response> {
|
|
147
|
-
await
|
|
148
|
-
|
|
138
|
+
const message = await request.json();
|
|
149
139
|
const promise = new Promise<Response>((resolve) => {
|
|
150
|
-
|
|
151
|
-
|
|
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);
|
|
152
149
|
};
|
|
150
|
+
|
|
151
|
+
broadcast.addEventListener('message', messageHandler);
|
|
153
152
|
});
|
|
154
153
|
|
|
155
|
-
|
|
156
|
-
messagePort.postMessage(message);
|
|
154
|
+
broadcast.postMessage(message);
|
|
157
155
|
|
|
158
156
|
return await promise;
|
|
159
157
|
}
|
package/src/tokens.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Contents } from '@jupyterlab/services';
|
|
2
|
-
|
|
3
1
|
import { Token } from '@lumino/coreutils';
|
|
4
2
|
|
|
5
3
|
import { ISignal } from '@lumino/signaling';
|
|
6
4
|
|
|
5
|
+
import { Contents } from '@jupyterlab/services';
|
|
6
|
+
|
|
7
7
|
import SW_URL from './service-worker?text';
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -30,6 +30,11 @@ export interface IServiceWorkerManager {
|
|
|
30
30
|
*/
|
|
31
31
|
readonly enabled: boolean;
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* A unique id to identify the browsing context where the ServiceWorkerManager was instantiated.
|
|
35
|
+
*/
|
|
36
|
+
readonly browsingContextId: string;
|
|
37
|
+
|
|
33
38
|
/**
|
|
34
39
|
* A Promise that resolves when the ServiceWorker is registered, or rejects if it cannot
|
|
35
40
|
*/
|
|
@@ -45,7 +50,7 @@ export namespace IServiceWorkerManager {
|
|
|
45
50
|
*/
|
|
46
51
|
export interface IOptions {
|
|
47
52
|
/**
|
|
48
|
-
* The
|
|
53
|
+
* The contents manager to use for handling drive contents requests
|
|
49
54
|
*/
|
|
50
55
|
contents: Contents.IManager;
|
|
51
56
|
|