@jupyterlite/server 0.6.0-alpha.0 → 0.6.0-alpha.10

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/src/router.ts DELETED
@@ -1,170 +0,0 @@
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
- }
@@ -1,118 +0,0 @@
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
- const VERSION = PageConfig.getOption('appVersion');
9
- const SW_PING_ENDPOINT = '/api/service-worker-heartbeat';
10
- export class ServiceWorkerManager implements IServiceWorkerManager {
11
- constructor(options?: IServiceWorkerManager.IOptions) {
12
- const workerUrl =
13
- options?.workerUrl ?? URLExt.join(PageConfig.getBaseUrl(), WORKER_NAME);
14
- const fullWorkerUrl = new URL(workerUrl, window.location.href);
15
- const enableCache = PageConfig.getOption('enableServiceWorkerCache') || 'false';
16
- fullWorkerUrl.searchParams.set('enableCache', enableCache);
17
- void this.initialize(fullWorkerUrl.href).catch(console.warn);
18
- }
19
-
20
- /**
21
- * A signal emitted when the registration changes.
22
- */
23
- get registrationChanged(): ISignal<
24
- IServiceWorkerManager,
25
- ServiceWorkerRegistration | null
26
- > {
27
- return this._registrationChanged;
28
- }
29
-
30
- /**
31
- * Whether the ServiceWorker is enabled or not.
32
- */
33
- get enabled(): boolean {
34
- return this._registration !== null;
35
- }
36
-
37
- get ready(): Promise<void> {
38
- return this._ready.promise;
39
- }
40
-
41
- private unregisterOldServiceWorkers = async (scriptURL: string) => {
42
- const versionKey = `${scriptURL}-version`;
43
- // Check if we have an installed version. If we do, compare it to the current version
44
- // and unregister all service workers if they are different.
45
- const installedVersion = localStorage.getItem(versionKey);
46
-
47
- if ((installedVersion && installedVersion !== VERSION) || !installedVersion) {
48
- // eslint-disable-next-line no-console
49
- console.info('New version, unregistering existing service workers.');
50
- const registrations = await navigator.serviceWorker.getRegistrations();
51
-
52
- await Promise.all(registrations.map((registration) => registration.unregister()));
53
-
54
- // eslint-disable-next-line no-console
55
- console.info('All existing service workers have been unregistered.');
56
- }
57
-
58
- localStorage.setItem(versionKey, VERSION);
59
- };
60
-
61
- private async initialize(workerUrl: string): Promise<void> {
62
- const { serviceWorker } = navigator;
63
-
64
- let registration: ServiceWorkerRegistration | null = null;
65
-
66
- if (!serviceWorker) {
67
- console.warn('ServiceWorkers not supported in this browser');
68
- } else if (serviceWorker.controller) {
69
- const scriptURL = serviceWorker.controller.scriptURL;
70
- await this.unregisterOldServiceWorkers(scriptURL);
71
-
72
- registration = (await serviceWorker.getRegistration(scriptURL)) || null;
73
- // eslint-disable-next-line no-console
74
- console.info('JupyterLite ServiceWorker was already registered');
75
- }
76
-
77
- if (!registration && serviceWorker) {
78
- try {
79
- // eslint-disable-next-line no-console
80
- console.info('Registering new JupyterLite ServiceWorker', workerUrl);
81
- registration = await serviceWorker.register(workerUrl);
82
- // eslint-disable-next-line no-console
83
- console.info('JupyterLite ServiceWorker was sucessfully registered');
84
- } catch (err: any) {
85
- console.warn(err);
86
- console.warn(
87
- `JupyterLite ServiceWorker registration unexpectedly failed: ${err}`,
88
- );
89
- }
90
- }
91
-
92
- this._setRegistration(registration);
93
-
94
- if (!registration) {
95
- this._ready.reject(void 0);
96
- } else {
97
- this._ready.resolve(void 0);
98
- setTimeout(this._pingServiceWorker, 20000);
99
- }
100
- }
101
- private _pingServiceWorker = async (): Promise<void> => {
102
- const response = await fetch(SW_PING_ENDPOINT);
103
- const text = await response.text();
104
- if (text === 'ok') {
105
- setTimeout(this._pingServiceWorker, 20000);
106
- }
107
- };
108
- private _setRegistration(registration: ServiceWorkerRegistration | null) {
109
- this._registration = registration;
110
- this._registrationChanged.emit(this._registration);
111
- }
112
-
113
- private _registration: ServiceWorkerRegistration | null = null;
114
- private _registrationChanged = new Signal<this, ServiceWorkerRegistration | null>(
115
- this,
116
- );
117
- private _ready = new PromiseDelegate<void>();
118
- }
package/src/status.ts DELETED
@@ -1,21 +0,0 @@
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
- }