@jupyterlite/server 0.2.2 → 0.2.3
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 +16 -15
- package/src/app.ts +191 -0
- package/src/index.ts +7 -0
- package/src/router.ts +170 -0
- package/src/service-manager.ts +91 -0
- package/src/service-worker.ts +159 -0
- package/src/status.ts +21 -0
- package/src/tokens.ts +52 -0
- package/src/typings.d.ts +4 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jupyterlite/server",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "JupyterLite - Server",
|
|
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,18 +43,18 @@
|
|
|
42
43
|
"watch": "tsc -b --watch"
|
|
43
44
|
},
|
|
44
45
|
"dependencies": {
|
|
45
|
-
"@jupyterlab/coreutils": "~6.0.
|
|
46
|
-
"@jupyterlab/nbformat": "~4.0.
|
|
47
|
-
"@jupyterlab/observables": "~5.0.
|
|
48
|
-
"@jupyterlab/services": "~7.0.
|
|
49
|
-
"@jupyterlab/settingregistry": "~4.0.
|
|
50
|
-
"@jupyterlab/statedb": "~4.0.
|
|
51
|
-
"@jupyterlite/contents": "^0.2.
|
|
52
|
-
"@jupyterlite/kernel": "^0.2.
|
|
53
|
-
"@jupyterlite/session": "^0.2.
|
|
54
|
-
"@jupyterlite/settings": "^0.2.
|
|
55
|
-
"@jupyterlite/translation": "^0.2.
|
|
56
|
-
"@lumino/application": "^2.
|
|
46
|
+
"@jupyterlab/coreutils": "~6.0.11",
|
|
47
|
+
"@jupyterlab/nbformat": "~4.0.11",
|
|
48
|
+
"@jupyterlab/observables": "~5.0.11",
|
|
49
|
+
"@jupyterlab/services": "~7.0.11",
|
|
50
|
+
"@jupyterlab/settingregistry": "~4.0.11",
|
|
51
|
+
"@jupyterlab/statedb": "~4.0.11",
|
|
52
|
+
"@jupyterlite/contents": "^0.2.3",
|
|
53
|
+
"@jupyterlite/kernel": "^0.2.3",
|
|
54
|
+
"@jupyterlite/session": "^0.2.3",
|
|
55
|
+
"@jupyterlite/settings": "^0.2.3",
|
|
56
|
+
"@jupyterlite/translation": "^0.2.3",
|
|
57
|
+
"@lumino/application": "^2.3.0",
|
|
57
58
|
"@lumino/coreutils": "^2.1.2",
|
|
58
59
|
"@lumino/signaling": "^2.1.2",
|
|
59
60
|
"mock-socket": "^9.1.0"
|
|
@@ -61,7 +62,7 @@
|
|
|
61
62
|
"devDependencies": {
|
|
62
63
|
"@babel/core": "^7.11.6",
|
|
63
64
|
"@babel/preset-env": "^7.12.1",
|
|
64
|
-
"@jupyterlab/testutils": "~4.0.
|
|
65
|
+
"@jupyterlab/testutils": "~4.0.11",
|
|
65
66
|
"@types/jest": "^29.5.3",
|
|
66
67
|
"@types/serviceworker": "^0.0.56",
|
|
67
68
|
"jest": "^29.6.2",
|
package/src/app.ts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
import { Event, ServerConnection, ServiceManager } from '@jupyterlab/services';
|
|
5
|
+
|
|
6
|
+
import { Application, IPlugin } from '@lumino/application';
|
|
7
|
+
|
|
8
|
+
import { Stream } from '@lumino/signaling';
|
|
9
|
+
|
|
10
|
+
import { WebSocket } from 'mock-socket';
|
|
11
|
+
|
|
12
|
+
import { Router } from './router';
|
|
13
|
+
|
|
14
|
+
export type JupyterLiteServerPlugin<T> = IPlugin<JupyterLiteServer, T>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Mock the Event Manager for now
|
|
18
|
+
*/
|
|
19
|
+
class MockEventManager implements Event.IManager {
|
|
20
|
+
constructor(options: { serverSettings: ServerConnection.ISettings }) {
|
|
21
|
+
this._stream = new Stream(this);
|
|
22
|
+
this._serverSettings = options.serverSettings;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async emit(event: Event.Request): Promise<void> {
|
|
26
|
+
// no-op
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
dispose(): void {
|
|
30
|
+
// no-op
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get isDisposed(): boolean {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get stream() {
|
|
38
|
+
return this._stream;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
get serverSettings(): ServerConnection.ISettings {
|
|
42
|
+
return this._serverSettings;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private _stream: Stream<this, Event.Emission>;
|
|
46
|
+
private _serverSettings: ServerConnection.ISettings;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Server is the main application class. It is instantiated once and shared.
|
|
51
|
+
*/
|
|
52
|
+
export class JupyterLiteServer extends Application<never> {
|
|
53
|
+
/**
|
|
54
|
+
* Construct a new JupyterLite object.
|
|
55
|
+
*
|
|
56
|
+
* @param options The instantiation options for a JupyterLiteServer application.
|
|
57
|
+
*/
|
|
58
|
+
constructor(options: Application.IOptions<never>) {
|
|
59
|
+
super(options);
|
|
60
|
+
const serverSettings = {
|
|
61
|
+
...ServerConnection.makeSettings(),
|
|
62
|
+
WebSocket,
|
|
63
|
+
fetch: this.fetch.bind(this) ?? undefined,
|
|
64
|
+
};
|
|
65
|
+
this._serviceManager = new ServiceManager({
|
|
66
|
+
standby: 'never',
|
|
67
|
+
serverSettings,
|
|
68
|
+
events: new MockEventManager({ serverSettings }),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The name of the application.
|
|
74
|
+
*/
|
|
75
|
+
readonly name = 'JupyterLite Server';
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A namespace/prefix plugins may use to denote their provenance.
|
|
79
|
+
*/
|
|
80
|
+
readonly namespace = this.name;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The version of the application.
|
|
84
|
+
*/
|
|
85
|
+
readonly version = 'unknown';
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get the underlying `Router` instance.
|
|
89
|
+
*/
|
|
90
|
+
get router(): Router {
|
|
91
|
+
return this._router;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Get the underlying lite service manager for this app.
|
|
96
|
+
*/
|
|
97
|
+
get serviceManager(): ServiceManager {
|
|
98
|
+
return this._serviceManager;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Handle an incoming request from the client.
|
|
103
|
+
*
|
|
104
|
+
* @param req The incoming request
|
|
105
|
+
* @param init The optional init request
|
|
106
|
+
*/
|
|
107
|
+
async fetch(
|
|
108
|
+
req: RequestInfo,
|
|
109
|
+
init?: RequestInit | null | undefined,
|
|
110
|
+
): Promise<Response> {
|
|
111
|
+
if (!(req instanceof Request)) {
|
|
112
|
+
throw Error('Request info is not a Request');
|
|
113
|
+
}
|
|
114
|
+
return this._router.route(req);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Attach the application shell to the DOM.
|
|
119
|
+
*
|
|
120
|
+
* @param id - The id of the host node for the shell, or `''`.
|
|
121
|
+
*
|
|
122
|
+
* #### Notes
|
|
123
|
+
* For this server application there is no shell to attach
|
|
124
|
+
*/
|
|
125
|
+
protected attachShell(id: string): void {
|
|
126
|
+
// no-op
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* A method invoked on a window `'resize'` event.
|
|
131
|
+
*
|
|
132
|
+
* #### Notes
|
|
133
|
+
* For this server application there is no shell to update
|
|
134
|
+
*/
|
|
135
|
+
protected evtResize(event: Event): void {
|
|
136
|
+
// no-op
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Register plugins from a plugin module.
|
|
141
|
+
*
|
|
142
|
+
* @param mod - The plugin module to register.
|
|
143
|
+
*/
|
|
144
|
+
registerPluginModule(mod: JupyterLiteServer.IPluginModule): void {
|
|
145
|
+
let data = mod.default;
|
|
146
|
+
// Handle commonjs exports.
|
|
147
|
+
if (!Object.prototype.hasOwnProperty.call(mod, '__esModule')) {
|
|
148
|
+
data = mod as any;
|
|
149
|
+
}
|
|
150
|
+
if (!Array.isArray(data)) {
|
|
151
|
+
data = [data];
|
|
152
|
+
}
|
|
153
|
+
data.forEach((item) => {
|
|
154
|
+
try {
|
|
155
|
+
this.registerPlugin(item);
|
|
156
|
+
} catch (error) {
|
|
157
|
+
console.error(error);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Register the plugins from multiple plugin modules.
|
|
164
|
+
*
|
|
165
|
+
* @param mods - The plugin modules to register.
|
|
166
|
+
*/
|
|
167
|
+
registerPluginModules(mods: JupyterLiteServer.IPluginModule[]): void {
|
|
168
|
+
mods.forEach((mod) => {
|
|
169
|
+
this.registerPluginModule(mod);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private _router = new Router();
|
|
174
|
+
private _serviceManager: ServiceManager;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* A namespace for Server statics.
|
|
179
|
+
*/
|
|
180
|
+
export namespace JupyterLiteServer {
|
|
181
|
+
/**
|
|
182
|
+
* The interface for a module that exports a plugin or plugins as
|
|
183
|
+
* the default value.
|
|
184
|
+
*/
|
|
185
|
+
export interface IPluginModule {
|
|
186
|
+
/**
|
|
187
|
+
* The default export.
|
|
188
|
+
*/
|
|
189
|
+
default: IPlugin<JupyterLiteServer, any> | IPlugin<JupyterLiteServer, any>[];
|
|
190
|
+
}
|
|
191
|
+
}
|
package/src/index.ts
ADDED
package/src/router.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { ReadonlyJSONObject } from '@lumino/coreutils';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A simple router.
|
|
5
|
+
*/
|
|
6
|
+
export class Router {
|
|
7
|
+
/**
|
|
8
|
+
* Add a new GET route
|
|
9
|
+
*
|
|
10
|
+
* @param pattern The pattern to match
|
|
11
|
+
* @param callback The function to call on pattern match
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
get(pattern: string | RegExp, callback: Router.Callback): void {
|
|
15
|
+
this._add('GET', pattern, callback);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Add a new PUT route
|
|
20
|
+
*
|
|
21
|
+
* @param pattern The pattern to match
|
|
22
|
+
* @param callback The function to call on pattern match
|
|
23
|
+
*
|
|
24
|
+
*/
|
|
25
|
+
put(pattern: string | RegExp, callback: Router.Callback): void {
|
|
26
|
+
this._add('PUT', pattern, callback);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Add a new POST route
|
|
31
|
+
*
|
|
32
|
+
* @param pattern The pattern to match
|
|
33
|
+
* @param callback The function to call on pattern match
|
|
34
|
+
*
|
|
35
|
+
*/
|
|
36
|
+
post(pattern: string | RegExp, callback: Router.Callback): void {
|
|
37
|
+
this._add('POST', pattern, callback);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Add a new PATCH route
|
|
42
|
+
*
|
|
43
|
+
* @param pattern The pattern to match
|
|
44
|
+
* @param callback The function to call on pattern match
|
|
45
|
+
*
|
|
46
|
+
*/
|
|
47
|
+
patch(pattern: string | RegExp, callback: Router.Callback): void {
|
|
48
|
+
this._add('PATCH', pattern, callback);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Add a new DELETE route
|
|
53
|
+
*
|
|
54
|
+
* @param pattern The pattern to match
|
|
55
|
+
* @param callback The function to call on pattern match
|
|
56
|
+
*
|
|
57
|
+
*/
|
|
58
|
+
delete(pattern: string | RegExp, callback: Router.Callback): void {
|
|
59
|
+
this._add('DELETE', pattern, callback);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Route a request.
|
|
64
|
+
*
|
|
65
|
+
* @param req The request to route.
|
|
66
|
+
*/
|
|
67
|
+
async route(req: Request): Promise<Response> {
|
|
68
|
+
const url = new URL(req.url);
|
|
69
|
+
const { method } = req;
|
|
70
|
+
const { pathname } = url;
|
|
71
|
+
|
|
72
|
+
for (const r of this._routes) {
|
|
73
|
+
if (r.method !== method) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const match = pathname.match(r.pattern);
|
|
77
|
+
if (!match) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const matches = match.slice(1);
|
|
81
|
+
let body;
|
|
82
|
+
if (r.method === 'PATCH' || r.method === 'PUT' || r.method === 'POST') {
|
|
83
|
+
try {
|
|
84
|
+
body = JSON.parse(await req.text());
|
|
85
|
+
} catch {
|
|
86
|
+
body = undefined;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return r.callback.call(
|
|
90
|
+
null,
|
|
91
|
+
{
|
|
92
|
+
pathname,
|
|
93
|
+
body,
|
|
94
|
+
query: Object.fromEntries(url.searchParams),
|
|
95
|
+
},
|
|
96
|
+
...matches,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
throw new Error('Cannot route ' + req.method + ' ' + req.url);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Add a new route.
|
|
105
|
+
*
|
|
106
|
+
* @param method The method
|
|
107
|
+
* @param pattern The pattern
|
|
108
|
+
* @param callback The callback
|
|
109
|
+
*/
|
|
110
|
+
private _add(
|
|
111
|
+
method: Router.Method,
|
|
112
|
+
pattern: string | RegExp,
|
|
113
|
+
callback: Router.Callback,
|
|
114
|
+
): void {
|
|
115
|
+
if (typeof pattern === 'string') {
|
|
116
|
+
pattern = new RegExp(pattern);
|
|
117
|
+
}
|
|
118
|
+
this._routes.push({
|
|
119
|
+
method,
|
|
120
|
+
pattern,
|
|
121
|
+
callback,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private _routes: Router.IRoute[] = [];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* A namespace for Router statics.
|
|
130
|
+
*/
|
|
131
|
+
export namespace Router {
|
|
132
|
+
/**
|
|
133
|
+
* The Callback type.
|
|
134
|
+
*/
|
|
135
|
+
export type Callback = (req: IRequest, ...args: string[]) => Promise<Response>;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The Method type.
|
|
139
|
+
*/
|
|
140
|
+
export type Method = 'GET' | 'PUT' | 'POST' | 'PATCH' | 'DELETE';
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The interface for a parsed request
|
|
144
|
+
*/
|
|
145
|
+
export interface IRequest {
|
|
146
|
+
/**
|
|
147
|
+
* The path for the url.
|
|
148
|
+
*/
|
|
149
|
+
pathname: string;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The optional query parameters.
|
|
153
|
+
*/
|
|
154
|
+
query?: ReadonlyJSONObject;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The optional body parameters.
|
|
158
|
+
*/
|
|
159
|
+
body?: ReadonlyJSONObject;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* An interface for a route.
|
|
164
|
+
*/
|
|
165
|
+
export interface IRoute {
|
|
166
|
+
method: Method;
|
|
167
|
+
pattern: string | RegExp;
|
|
168
|
+
callback: Callback;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { PromiseDelegate } from '@lumino/coreutils';
|
|
2
|
+
import { ISignal, Signal } from '@lumino/signaling';
|
|
3
|
+
|
|
4
|
+
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
|
|
5
|
+
|
|
6
|
+
import { IServiceWorkerManager, WORKER_NAME } from './tokens';
|
|
7
|
+
|
|
8
|
+
export class ServiceWorkerManager implements IServiceWorkerManager {
|
|
9
|
+
constructor(options?: IServiceWorkerManager.IOptions) {
|
|
10
|
+
const workerUrl =
|
|
11
|
+
options?.workerUrl ?? URLExt.join(PageConfig.getBaseUrl(), WORKER_NAME);
|
|
12
|
+
void this.initialize(workerUrl).catch(console.warn);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A signal emitted when the registration changes.
|
|
17
|
+
*/
|
|
18
|
+
get registrationChanged(): ISignal<
|
|
19
|
+
IServiceWorkerManager,
|
|
20
|
+
ServiceWorkerRegistration | null
|
|
21
|
+
> {
|
|
22
|
+
return this._registrationChanged;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Whether the ServiceWorker is enabled or not.
|
|
27
|
+
*/
|
|
28
|
+
get enabled(): boolean {
|
|
29
|
+
return this._registration !== null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get ready(): Promise<void> {
|
|
33
|
+
return this._ready.promise;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private async initialize(workerUrl: string): Promise<void> {
|
|
37
|
+
const { serviceWorker } = navigator;
|
|
38
|
+
|
|
39
|
+
let registration: ServiceWorkerRegistration | null = null;
|
|
40
|
+
|
|
41
|
+
if (!serviceWorker) {
|
|
42
|
+
console.warn('ServiceWorkers not supported in this browser');
|
|
43
|
+
} else if (serviceWorker.controller) {
|
|
44
|
+
registration =
|
|
45
|
+
(await serviceWorker.getRegistration(serviceWorker.controller.scriptURL)) ||
|
|
46
|
+
null;
|
|
47
|
+
// eslint-disable-next-line no-console
|
|
48
|
+
console.info('JupyterLite ServiceWorker was already registered');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!registration && serviceWorker) {
|
|
52
|
+
try {
|
|
53
|
+
// Unregister any existing service workers before registering the new one.
|
|
54
|
+
const registrations = await serviceWorker.getRegistrations();
|
|
55
|
+
await Promise.all(registrations.map((r) => r.unregister()));
|
|
56
|
+
// eslint-disable-next-line no-console
|
|
57
|
+
console.info('Existing JupyterLite ServiceWorkers unregistered');
|
|
58
|
+
|
|
59
|
+
// eslint-disable-next-line no-console
|
|
60
|
+
console.info('Registering new JupyterLite ServiceWorker', workerUrl);
|
|
61
|
+
registration = await serviceWorker.register(workerUrl);
|
|
62
|
+
// eslint-disable-next-line no-console
|
|
63
|
+
console.info('JupyterLite ServiceWorker was sucessfully registered');
|
|
64
|
+
} catch (err: any) {
|
|
65
|
+
console.warn(err);
|
|
66
|
+
console.warn(
|
|
67
|
+
`JupyterLite ServiceWorker registration unexpectedly failed: ${err}`,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
this.setRegistration(registration);
|
|
73
|
+
|
|
74
|
+
if (!registration) {
|
|
75
|
+
this._ready.reject(void 0);
|
|
76
|
+
} else {
|
|
77
|
+
this._ready.resolve(void 0);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private setRegistration(registration: ServiceWorkerRegistration | null) {
|
|
82
|
+
this._registration = registration;
|
|
83
|
+
this._registrationChanged.emit(this._registration);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private _registration: ServiceWorkerRegistration | null = null;
|
|
87
|
+
private _registrationChanged = new Signal<this, ServiceWorkerRegistration | null>(
|
|
88
|
+
this,
|
|
89
|
+
);
|
|
90
|
+
private _ready = new PromiseDelegate<void>();
|
|
91
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
|
|
2
|
+
/// <reference path="../../../node_modules/@types/serviceworker/index.d.ts" />
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The name of the cache
|
|
6
|
+
*/
|
|
7
|
+
const CACHE = 'precache';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Communication channel for drive access
|
|
11
|
+
*/
|
|
12
|
+
const broadcast = new BroadcastChannel('/api/drive.v1');
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Install event listeners
|
|
16
|
+
*/
|
|
17
|
+
self.addEventListener('install', onInstall);
|
|
18
|
+
self.addEventListener('activate', onActivate);
|
|
19
|
+
self.addEventListener('fetch', onFetch);
|
|
20
|
+
|
|
21
|
+
// Event handlers
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Handle installation with the cache
|
|
25
|
+
*/
|
|
26
|
+
function onInstall(event: ExtendableEvent): void {
|
|
27
|
+
void self.skipWaiting();
|
|
28
|
+
event.waitUntil(cacheAll());
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Handle activation.
|
|
33
|
+
*/
|
|
34
|
+
function onActivate(event: ExtendableEvent): void {
|
|
35
|
+
event.waitUntil(self.clients.claim());
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Handle fetching a single resource.
|
|
40
|
+
*/
|
|
41
|
+
async function onFetch(event: FetchEvent): Promise<void> {
|
|
42
|
+
const { request } = event;
|
|
43
|
+
|
|
44
|
+
const url = new URL(event.request.url);
|
|
45
|
+
|
|
46
|
+
let responsePromise: Promise<Response> | null = null;
|
|
47
|
+
|
|
48
|
+
if (shouldBroadcast(url)) {
|
|
49
|
+
responsePromise = broadcastOne(request);
|
|
50
|
+
} else if (!shouldDrop(request, url)) {
|
|
51
|
+
responsePromise = maybeFromCache(event);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (responsePromise) {
|
|
55
|
+
event.respondWith(responsePromise);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// utilities
|
|
60
|
+
|
|
61
|
+
/** Get a cached response, and update cache. */
|
|
62
|
+
async function maybeFromCache(event: FetchEvent): Promise<Response> {
|
|
63
|
+
const { request } = event;
|
|
64
|
+
|
|
65
|
+
let response: Response | null = await fromCache(request);
|
|
66
|
+
|
|
67
|
+
if (response) {
|
|
68
|
+
event.waitUntil(refetch(request));
|
|
69
|
+
} else {
|
|
70
|
+
response = await fetch(request);
|
|
71
|
+
event.waitUntil(updateCache(request, response.clone()));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return response;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Restore a response from the cache based on the request.
|
|
79
|
+
*/
|
|
80
|
+
async function fromCache(request: Request): Promise<Response | null> {
|
|
81
|
+
const cache = await openCache();
|
|
82
|
+
const response = await cache.match(request);
|
|
83
|
+
|
|
84
|
+
if (!response || response.status === 404) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return response;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* This is where we call the server to get the newest version of the
|
|
93
|
+
* file to use the next time we show view
|
|
94
|
+
*/
|
|
95
|
+
async function refetch(request: Request): Promise<Response> {
|
|
96
|
+
const fromServer = await fetch(request);
|
|
97
|
+
await updateCache(request, fromServer);
|
|
98
|
+
return fromServer;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Whether a given URL should be broadcast
|
|
103
|
+
*/
|
|
104
|
+
function shouldBroadcast(url: URL): boolean {
|
|
105
|
+
return url.origin === location.origin && url.pathname.includes('/api/drive');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Whether the fallback behavior should be used
|
|
110
|
+
*/
|
|
111
|
+
function shouldDrop(request: Request, url: URL): boolean {
|
|
112
|
+
return (
|
|
113
|
+
request.method !== 'GET' ||
|
|
114
|
+
url.origin.match(/^http/) === null ||
|
|
115
|
+
url.pathname.includes('/api/')
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Forward request to main using the broadcast channel
|
|
121
|
+
*/
|
|
122
|
+
async function broadcastOne(request: Request): Promise<Response> {
|
|
123
|
+
const promise = new Promise<Response>((resolve) => {
|
|
124
|
+
broadcast.onmessage = (event) => {
|
|
125
|
+
resolve(new Response(JSON.stringify(event.data)));
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const message = await request.json();
|
|
130
|
+
// Mark message as being for broadcast.ts
|
|
131
|
+
// This makes sure we won't get problems with messages
|
|
132
|
+
// across tabs with multiple notebook tabs open
|
|
133
|
+
message.receiver = 'broadcast.ts';
|
|
134
|
+
broadcast.postMessage(message);
|
|
135
|
+
|
|
136
|
+
return await promise;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function openCache(): Promise<Cache> {
|
|
140
|
+
return await caches.open(CACHE);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Cache a request/response pair.
|
|
145
|
+
*/
|
|
146
|
+
async function updateCache(request: Request, response: Response): Promise<void> {
|
|
147
|
+
const cache = await openCache();
|
|
148
|
+
return cache.put(request, response);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Add all to the cache
|
|
153
|
+
*
|
|
154
|
+
* this is where we should (try to) add all relevant files
|
|
155
|
+
*/
|
|
156
|
+
async function cacheAll() {
|
|
157
|
+
const cache = await openCache();
|
|
158
|
+
return await cache.addAll([]);
|
|
159
|
+
}
|
package/src/status.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A class to handle requests to /api/status
|
|
3
|
+
*/
|
|
4
|
+
export class Status {
|
|
5
|
+
/**
|
|
6
|
+
* Get the status
|
|
7
|
+
*/
|
|
8
|
+
get(): any {
|
|
9
|
+
return {};
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A namespace for Status statics.
|
|
15
|
+
*/
|
|
16
|
+
export namespace Status {
|
|
17
|
+
/**
|
|
18
|
+
* The url for the status service.
|
|
19
|
+
*/
|
|
20
|
+
export const STATUS_SERVICE_URL = '/api/status';
|
|
21
|
+
}
|
package/src/tokens.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Token } from '@lumino/coreutils';
|
|
2
|
+
|
|
3
|
+
import { ISignal } from '@lumino/signaling';
|
|
4
|
+
|
|
5
|
+
import SW_URL from './service-worker?text';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The token for the ServiceWorker.
|
|
9
|
+
*/
|
|
10
|
+
export const IServiceWorkerManager = new Token<IServiceWorkerManager>(
|
|
11
|
+
'@jupyterlite/server-extension:IServiceWorkerManager',
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The interface for the ServiceWorkerRegistration.
|
|
16
|
+
*/
|
|
17
|
+
export interface IServiceWorkerManager {
|
|
18
|
+
/**
|
|
19
|
+
* Signal fired when the registration changed.
|
|
20
|
+
*/
|
|
21
|
+
readonly registrationChanged: ISignal<
|
|
22
|
+
IServiceWorkerManager,
|
|
23
|
+
ServiceWorkerRegistration | null
|
|
24
|
+
>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Whether the ServiceWorker is enabled or not.
|
|
28
|
+
*/
|
|
29
|
+
readonly enabled: boolean;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A Promise that resolves when the ServiceWorker is registered, or rejects if it cannot
|
|
33
|
+
*/
|
|
34
|
+
ready: Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A namespace for `ServiceWorkerManager` class.
|
|
39
|
+
*/
|
|
40
|
+
export namespace IServiceWorkerManager {
|
|
41
|
+
/**
|
|
42
|
+
* An options object for initializing a worker manager.
|
|
43
|
+
*/
|
|
44
|
+
export interface IOptions {
|
|
45
|
+
/**
|
|
46
|
+
* URL to load the worker file. Default to "{baseURL}/service-worker.js"
|
|
47
|
+
*/
|
|
48
|
+
workerUrl?: string;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const WORKER_NAME = `${SW_URL}`.split('/').slice(-1)[0];
|
package/src/typings.d.ts
ADDED