@jupyterlite/server 0.6.0-alpha.9 → 0.6.0-beta.1
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 -0
- package/lib/service-worker-manager.js +46 -3
- package/lib/service-worker-manager.js.map +1 -1
- package/lib/service-worker.d.ts +2 -2
- package/lib/service-worker.js +8 -7
- package/lib/service-worker.js.map +1 -1
- package/lib/tokens.d.ts +16 -0
- package/lib/tokens.js.map +1 -1
- package/package.json +14 -14
- package/src/service-worker-manager.ts +55 -5
- package/src/service-worker.ts +10 -8
- package/src/tokens.ts +21 -0
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { ISignal } from '@lumino/signaling';
|
|
2
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";
|
|
3
7
|
/**
|
|
4
8
|
* A class that manages the ServiceWorker registration and communication,
|
|
5
9
|
* used for accessing the file system.
|
|
@@ -25,6 +29,12 @@ export declare class ServiceWorkerManager implements IServiceWorkerManager {
|
|
|
25
29
|
* Whether the ServiceWorker is ready or not.
|
|
26
30
|
*/
|
|
27
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;
|
|
28
38
|
/**
|
|
29
39
|
* Initialize the ServiceWorkerManager.
|
|
30
40
|
*/
|
|
@@ -43,8 +53,14 @@ export declare class ServiceWorkerManager implements IServiceWorkerManager {
|
|
|
43
53
|
private _setRegistration;
|
|
44
54
|
/**
|
|
45
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.
|
|
46
60
|
*/
|
|
47
61
|
private _onBroadcastMessage;
|
|
62
|
+
private _onDriveMessage;
|
|
63
|
+
private _onStdinMessage;
|
|
48
64
|
private _registration;
|
|
49
65
|
private _registrationChanged;
|
|
50
66
|
private _ready;
|
|
@@ -52,4 +68,5 @@ export declare class ServiceWorkerManager implements IServiceWorkerManager {
|
|
|
52
68
|
private _browsingContextId;
|
|
53
69
|
private _contents;
|
|
54
70
|
private _driveContentsProcessor;
|
|
71
|
+
private _stdinHandlers;
|
|
55
72
|
}
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
|
|
2
|
-
import {
|
|
2
|
+
import { DriveContentsProcessor, } from '@jupyterlite/contents';
|
|
3
3
|
import { PromiseDelegate, UUID } from '@lumino/coreutils';
|
|
4
4
|
import { Signal } from '@lumino/signaling';
|
|
5
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';
|
|
6
10
|
/**
|
|
7
11
|
* The version of the app
|
|
8
12
|
*/
|
|
@@ -23,13 +27,25 @@ export class ServiceWorkerManager {
|
|
|
23
27
|
var _a;
|
|
24
28
|
/**
|
|
25
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.
|
|
26
34
|
*/
|
|
27
35
|
this._onBroadcastMessage = async (event) => {
|
|
28
|
-
const { data, browsingContextId } = event.data;
|
|
36
|
+
const { data, browsingContextId, pathname } = event.data;
|
|
29
37
|
if (browsingContextId !== this._browsingContextId) {
|
|
30
38
|
// Message is not meant for us
|
|
31
39
|
return;
|
|
32
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) => {
|
|
33
49
|
const response = await this._driveContentsProcessor.processDriveRequest(data);
|
|
34
50
|
// pass the browsingContextId along so the Service Worker can identify the request
|
|
35
51
|
this._broadcastChannel.postMessage({
|
|
@@ -37,9 +53,28 @@ export class ServiceWorkerManager {
|
|
|
37
53
|
browsingContextId: this._browsingContextId,
|
|
38
54
|
});
|
|
39
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
|
+
};
|
|
40
74
|
this._registration = null;
|
|
41
75
|
this._registrationChanged = new Signal(this);
|
|
42
76
|
this._ready = new PromiseDelegate();
|
|
77
|
+
this._stdinHandlers = new Map();
|
|
43
78
|
const workerUrl = (_a = options.workerUrl) !== null && _a !== void 0 ? _a : URLExt.join(PageConfig.getBaseUrl(), WORKER_NAME);
|
|
44
79
|
const fullWorkerUrl = new URL(workerUrl, window.location.href);
|
|
45
80
|
const enableCache = PageConfig.getOption('enableServiceWorkerCache') || 'false';
|
|
@@ -47,7 +82,7 @@ export class ServiceWorkerManager {
|
|
|
47
82
|
// Initialize broadcast channel related properties
|
|
48
83
|
this._browsingContextId = UUID.uuid4();
|
|
49
84
|
this._contents = options.contents;
|
|
50
|
-
this._broadcastChannel = new BroadcastChannel(
|
|
85
|
+
this._broadcastChannel = new BroadcastChannel(SERVICE_WORKER_BROADCAST_CHANNEL_ID);
|
|
51
86
|
this._broadcastChannel.addEventListener('message', this._onBroadcastMessage);
|
|
52
87
|
this._driveContentsProcessor = new DriveContentsProcessor({
|
|
53
88
|
contentsManager: this._contents,
|
|
@@ -78,6 +113,14 @@ export class ServiceWorkerManager {
|
|
|
78
113
|
get ready() {
|
|
79
114
|
return this._ready.promise;
|
|
80
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
|
+
}
|
|
81
124
|
/**
|
|
82
125
|
* Initialize the ServiceWorkerManager.
|
|
83
126
|
*/
|
|
@@ -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;AAI3D,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"service-worker-manager.js","sourceRoot":"","sources":["../src/service-worker-manager.ts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3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
declare const CACHE = "precache";
|
|
5
5
|
/**
|
|
6
|
-
* Communication channel
|
|
6
|
+
* Communication channel with the main thread
|
|
7
7
|
*/
|
|
8
8
|
declare const broadcast: BroadcastChannel;
|
|
9
9
|
/**
|
|
@@ -44,7 +44,7 @@ declare function shouldDrop(request: Request, url: URL): boolean;
|
|
|
44
44
|
/**
|
|
45
45
|
* Forward request to main using the broadcast channel
|
|
46
46
|
*/
|
|
47
|
-
declare function broadcastOne(request: Request): Promise<Response>;
|
|
47
|
+
declare function broadcastOne(request: Request, url: URL): Promise<Response>;
|
|
48
48
|
declare function openCache(): Promise<Cache>;
|
|
49
49
|
/**
|
|
50
50
|
* Cache a request/response pair.
|
package/lib/service-worker.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
|
|
3
|
-
/// <reference path="../../../node_modules/@types/serviceworker/index.d.ts" />
|
|
4
2
|
/**
|
|
5
3
|
* The name of the cache
|
|
6
4
|
*/
|
|
7
5
|
const CACHE = 'precache';
|
|
8
6
|
/**
|
|
9
|
-
* Communication channel
|
|
7
|
+
* Communication channel with the main thread
|
|
10
8
|
*/
|
|
11
|
-
const broadcast = new BroadcastChannel('/api
|
|
9
|
+
const broadcast = new BroadcastChannel('/sw-api.v1');
|
|
12
10
|
/**
|
|
13
11
|
* Whether to enable the cache
|
|
14
12
|
*/
|
|
@@ -48,7 +46,7 @@ async function onFetch(event) {
|
|
|
48
46
|
}
|
|
49
47
|
let responsePromise = null;
|
|
50
48
|
if (shouldBroadcast(url)) {
|
|
51
|
-
responsePromise = broadcastOne(request);
|
|
49
|
+
responsePromise = broadcastOne(request, url);
|
|
52
50
|
}
|
|
53
51
|
else if (!shouldDrop(request, url)) {
|
|
54
52
|
responsePromise = maybeFromCache(event);
|
|
@@ -98,7 +96,8 @@ async function refetch(request) {
|
|
|
98
96
|
* Whether a given URL should be broadcast
|
|
99
97
|
*/
|
|
100
98
|
function shouldBroadcast(url) {
|
|
101
|
-
return url.origin === location.origin &&
|
|
99
|
+
return (url.origin === location.origin &&
|
|
100
|
+
(url.pathname.includes('/api/drive') || url.pathname.includes('/api/stdin/')));
|
|
102
101
|
}
|
|
103
102
|
/**
|
|
104
103
|
* Whether the fallback behavior should be used
|
|
@@ -111,7 +110,7 @@ function shouldDrop(request, url) {
|
|
|
111
110
|
/**
|
|
112
111
|
* Forward request to main using the broadcast channel
|
|
113
112
|
*/
|
|
114
|
-
async function broadcastOne(request) {
|
|
113
|
+
async function broadcastOne(request, url) {
|
|
115
114
|
const message = await request.json();
|
|
116
115
|
const promise = new Promise((resolve) => {
|
|
117
116
|
const messageHandler = (event) => {
|
|
@@ -126,6 +125,8 @@ async function broadcastOne(request) {
|
|
|
126
125
|
};
|
|
127
126
|
broadcast.addEventListener('message', messageHandler);
|
|
128
127
|
});
|
|
128
|
+
// Add URL pathname to message
|
|
129
|
+
message.pathname = url.pathname;
|
|
129
130
|
broadcast.postMessage(message);
|
|
130
131
|
return await promise;
|
|
131
132
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service-worker.js","sourceRoot":"","sources":["../src/service-worker.ts"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"service-worker.js","sourceRoot":"","sources":["../src/service-worker.ts"],"names":[],"mappings":";AAAA;;GAEG;AACH,MAAM,KAAK,GAAG,UAAU,CAAC;AAEzB;;GAEG;AACH,MAAM,SAAS,GAAG,IAAI,gBAAgB,CAAC,YAAY,CAAC,CAAC;AAErD;;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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vB,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
|
@@ -25,6 +25,12 @@ export interface IServiceWorkerManager {
|
|
|
25
25
|
* A Promise that resolves when the ServiceWorker is registered, or rejects if it cannot
|
|
26
26
|
*/
|
|
27
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;
|
|
28
34
|
}
|
|
29
35
|
/**
|
|
30
36
|
* A namespace for `ServiceWorkerManager` class.
|
|
@@ -43,5 +49,15 @@ export declare namespace IServiceWorkerManager {
|
|
|
43
49
|
*/
|
|
44
50
|
workerUrl?: string;
|
|
45
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
|
+
}
|
|
46
62
|
}
|
|
47
63
|
export declare const WORKER_NAME: string;
|
package/lib/tokens.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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;
|
|
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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jupyterlite/server",
|
|
3
|
-
"version": "0.6.0-
|
|
3
|
+
"version": "0.6.0-beta.1",
|
|
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.
|
|
47
|
-
"@jupyterlab/nbformat": "~4.4.
|
|
48
|
-
"@jupyterlab/observables": "~5.4.
|
|
49
|
-
"@jupyterlab/services": "~7.4.
|
|
50
|
-
"@jupyterlab/settingregistry": "~4.4.
|
|
51
|
-
"@jupyterlab/statedb": "~4.4.
|
|
52
|
-
"@jupyterlite/contents": "^0.6.0-
|
|
53
|
-
"@jupyterlite/kernel": "^0.6.0-
|
|
54
|
-
"@jupyterlite/session": "^0.6.0-
|
|
55
|
-
"@jupyterlite/settings": "^0.6.0-
|
|
56
|
-
"@
|
|
57
|
-
"@lumino/application": "^2.4.3",
|
|
46
|
+
"@jupyterlab/coreutils": "~6.4.2",
|
|
47
|
+
"@jupyterlab/nbformat": "~4.4.2",
|
|
48
|
+
"@jupyterlab/observables": "~5.4.2",
|
|
49
|
+
"@jupyterlab/services": "~7.4.2",
|
|
50
|
+
"@jupyterlab/settingregistry": "~4.4.2",
|
|
51
|
+
"@jupyterlab/statedb": "~4.4.2",
|
|
52
|
+
"@jupyterlite/contents": "^0.6.0-beta.1",
|
|
53
|
+
"@jupyterlite/kernel": "^0.6.0-beta.1",
|
|
54
|
+
"@jupyterlite/session": "^0.6.0-beta.1",
|
|
55
|
+
"@jupyterlite/settings": "^0.6.0-beta.1",
|
|
56
|
+
"@lumino/application": "^2.4.4",
|
|
58
57
|
"@lumino/coreutils": "^2.2.1",
|
|
59
58
|
"@lumino/signaling": "^2.1.4",
|
|
59
|
+
"@types/emscripten": "^1.39.6",
|
|
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.
|
|
65
|
+
"@jupyterlab/testutils": "~4.4.2",
|
|
66
66
|
"@types/jest": "^29.5.3",
|
|
67
67
|
"@types/serviceworker": "^0.0.56",
|
|
68
68
|
"jest": "^29.6.2",
|
|
@@ -3,7 +3,6 @@ import { PageConfig, URLExt } from '@jupyterlab/coreutils';
|
|
|
3
3
|
import { Contents } from '@jupyterlab/services';
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
|
-
DRIVE_API_PATH,
|
|
7
6
|
DriveContentsProcessor,
|
|
8
7
|
TDriveMethod,
|
|
9
8
|
TDriveRequest,
|
|
@@ -15,6 +14,11 @@ import { ISignal, Signal } from '@lumino/signaling';
|
|
|
15
14
|
|
|
16
15
|
import { IServiceWorkerManager, WORKER_NAME } from './tokens';
|
|
17
16
|
|
|
17
|
+
/**
|
|
18
|
+
* The service-worker broadcast channel id
|
|
19
|
+
*/
|
|
20
|
+
export const SERVICE_WORKER_BROADCAST_CHANNEL_ID = '/sw-api.v1';
|
|
21
|
+
|
|
18
22
|
/**
|
|
19
23
|
* The version of the app
|
|
20
24
|
*/
|
|
@@ -43,7 +47,7 @@ export class ServiceWorkerManager implements IServiceWorkerManager {
|
|
|
43
47
|
// Initialize broadcast channel related properties
|
|
44
48
|
this._browsingContextId = UUID.uuid4();
|
|
45
49
|
this._contents = options.contents;
|
|
46
|
-
this._broadcastChannel = new BroadcastChannel(
|
|
50
|
+
this._broadcastChannel = new BroadcastChannel(SERVICE_WORKER_BROADCAST_CHANNEL_ID);
|
|
47
51
|
this._broadcastChannel.addEventListener('message', this._onBroadcastMessage);
|
|
48
52
|
|
|
49
53
|
this._driveContentsProcessor = new DriveContentsProcessor({
|
|
@@ -84,6 +88,18 @@ export class ServiceWorkerManager implements IServiceWorkerManager {
|
|
|
84
88
|
return this._ready.promise;
|
|
85
89
|
}
|
|
86
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
|
+
|
|
87
103
|
/**
|
|
88
104
|
* Initialize the ServiceWorkerManager.
|
|
89
105
|
*/
|
|
@@ -173,20 +189,35 @@ export class ServiceWorkerManager implements IServiceWorkerManager {
|
|
|
173
189
|
|
|
174
190
|
/**
|
|
175
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.
|
|
176
196
|
*/
|
|
177
|
-
private _onBroadcastMessage = async
|
|
197
|
+
private _onBroadcastMessage = async (
|
|
178
198
|
event: MessageEvent<{
|
|
179
|
-
data:
|
|
199
|
+
data: any;
|
|
180
200
|
browsingContextId: string;
|
|
201
|
+
pathname: string;
|
|
181
202
|
}>,
|
|
182
203
|
): Promise<void> => {
|
|
183
|
-
const { data, browsingContextId } = event.data;
|
|
204
|
+
const { data, browsingContextId, pathname } = event.data;
|
|
184
205
|
|
|
185
206
|
if (browsingContextId !== this._browsingContextId) {
|
|
186
207
|
// Message is not meant for us
|
|
187
208
|
return;
|
|
188
209
|
}
|
|
189
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> => {
|
|
190
221
|
const response = await this._driveContentsProcessor.processDriveRequest(data);
|
|
191
222
|
// pass the browsingContextId along so the Service Worker can identify the request
|
|
192
223
|
this._broadcastChannel.postMessage({
|
|
@@ -195,6 +226,24 @@ export class ServiceWorkerManager implements IServiceWorkerManager {
|
|
|
195
226
|
});
|
|
196
227
|
};
|
|
197
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
|
+
|
|
198
247
|
private _registration: ServiceWorkerRegistration | null = null;
|
|
199
248
|
private _registrationChanged = new Signal<this, ServiceWorkerRegistration | null>(
|
|
200
249
|
this,
|
|
@@ -204,4 +253,5 @@ export class ServiceWorkerManager implements IServiceWorkerManager {
|
|
|
204
253
|
private _browsingContextId: string;
|
|
205
254
|
private _contents: Contents.IManager;
|
|
206
255
|
private _driveContentsProcessor: DriveContentsProcessor;
|
|
256
|
+
private _stdinHandlers = new Map<string, IServiceWorkerManager.IStdinHandler>();
|
|
207
257
|
}
|
package/src/service-worker.ts
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
|
|
2
|
-
/// <reference path="../../../node_modules/@types/serviceworker/index.d.ts" />
|
|
3
|
-
|
|
4
1
|
/**
|
|
5
2
|
* The name of the cache
|
|
6
3
|
*/
|
|
7
4
|
const CACHE = 'precache';
|
|
8
5
|
|
|
9
6
|
/**
|
|
10
|
-
* Communication channel
|
|
7
|
+
* Communication channel with the main thread
|
|
11
8
|
*/
|
|
12
|
-
const broadcast = new BroadcastChannel('/api
|
|
9
|
+
const broadcast = new BroadcastChannel('/sw-api.v1');
|
|
13
10
|
|
|
14
11
|
/**
|
|
15
12
|
* Whether to enable the cache
|
|
@@ -57,7 +54,7 @@ async function onFetch(event: FetchEvent): Promise<void> {
|
|
|
57
54
|
|
|
58
55
|
let responsePromise: Promise<Response> | null = null;
|
|
59
56
|
if (shouldBroadcast(url)) {
|
|
60
|
-
responsePromise = broadcastOne(request);
|
|
57
|
+
responsePromise = broadcastOne(request, url);
|
|
61
58
|
} else if (!shouldDrop(request, url)) {
|
|
62
59
|
responsePromise = maybeFromCache(event);
|
|
63
60
|
}
|
|
@@ -117,7 +114,10 @@ async function refetch(request: Request): Promise<Response> {
|
|
|
117
114
|
* Whether a given URL should be broadcast
|
|
118
115
|
*/
|
|
119
116
|
function shouldBroadcast(url: URL): boolean {
|
|
120
|
-
return
|
|
117
|
+
return (
|
|
118
|
+
url.origin === location.origin &&
|
|
119
|
+
(url.pathname.includes('/api/drive') || url.pathname.includes('/api/stdin/'))
|
|
120
|
+
);
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
/**
|
|
@@ -134,7 +134,7 @@ function shouldDrop(request: Request, url: URL): boolean {
|
|
|
134
134
|
/**
|
|
135
135
|
* Forward request to main using the broadcast channel
|
|
136
136
|
*/
|
|
137
|
-
async function broadcastOne(request: Request): Promise<Response> {
|
|
137
|
+
async function broadcastOne(request: Request, url: URL): Promise<Response> {
|
|
138
138
|
const message = await request.json();
|
|
139
139
|
const promise = new Promise<Response>((resolve) => {
|
|
140
140
|
const messageHandler = (event: MessageEvent) => {
|
|
@@ -151,6 +151,8 @@ async function broadcastOne(request: Request): Promise<Response> {
|
|
|
151
151
|
broadcast.addEventListener('message', messageHandler);
|
|
152
152
|
});
|
|
153
153
|
|
|
154
|
+
// Add URL pathname to message
|
|
155
|
+
message.pathname = url.pathname;
|
|
154
156
|
broadcast.postMessage(message);
|
|
155
157
|
|
|
156
158
|
return await promise;
|
package/src/tokens.ts
CHANGED
|
@@ -39,6 +39,16 @@ export interface IServiceWorkerManager {
|
|
|
39
39
|
* A Promise that resolves when the ServiceWorker is registered, or rejects if it cannot
|
|
40
40
|
*/
|
|
41
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;
|
|
42
52
|
}
|
|
43
53
|
|
|
44
54
|
/**
|
|
@@ -59,6 +69,17 @@ export namespace IServiceWorkerManager {
|
|
|
59
69
|
*/
|
|
60
70
|
workerUrl?: string;
|
|
61
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
|
+
}
|
|
62
83
|
}
|
|
63
84
|
|
|
64
85
|
export const WORKER_NAME = `${SW_URL}`.split('/').slice(-1)[0];
|