-
Notifications
You must be signed in to change notification settings - Fork 276
/
app.ts
191 lines (165 loc) · 4.33 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { Event, ServerConnection, ServiceManager } from '@jupyterlab/services';
import { Application, IPlugin } from '@lumino/application';
import { Stream } from '@lumino/signaling';
import { WebSocket } from 'mock-socket';
import { Router } from './router';
export type JupyterLiteServerPlugin<T> = IPlugin<JupyterLiteServer, T>;
/**
* Mock the Event Manager for now
*/
class MockEventManager implements Event.IManager {
constructor(options: { serverSettings: ServerConnection.ISettings }) {
this._stream = new Stream(this);
this._serverSettings = options.serverSettings;
}
async emit(event: Event.Request): Promise<void> {
// no-op
}
dispose(): void {
// no-op
}
get isDisposed(): boolean {
return true;
}
get stream() {
return this._stream;
}
get serverSettings(): ServerConnection.ISettings {
return this._serverSettings;
}
private _stream: Stream<this, Event.Emission>;
private _serverSettings: ServerConnection.ISettings;
}
/**
* Server is the main application class. It is instantiated once and shared.
*/
export class JupyterLiteServer extends Application<never> {
/**
* Construct a new JupyterLite object.
*
* @param options The instantiation options for a JupyterLiteServer application.
*/
constructor(options: Application.IOptions<never>) {
super(options);
const serverSettings = {
...ServerConnection.makeSettings(),
WebSocket,
fetch: this.fetch.bind(this) ?? undefined,
};
this._serviceManager = new ServiceManager({
standby: 'never',
serverSettings,
events: new MockEventManager({ serverSettings }),
});
}
/**
* The name of the application.
*/
readonly name = 'JupyterLite Server';
/**
* A namespace/prefix plugins may use to denote their provenance.
*/
readonly namespace = this.name;
/**
* The version of the application.
*/
readonly version = 'unknown';
/**
* Get the underlying `Router` instance.
*/
get router(): Router {
return this._router;
}
/**
* Get the underlying lite service manager for this app.
*/
get serviceManager(): ServiceManager {
return this._serviceManager;
}
/**
* Handle an incoming request from the client.
*
* @param req The incoming request
* @param init The optional init request
*/
async fetch(
req: RequestInfo,
init?: RequestInit | null | undefined,
): Promise<Response> {
if (!(req instanceof Request)) {
throw Error('Request info is not a Request');
}
return this._router.route(req);
}
/**
* Attach the application shell to the DOM.
*
* @param id - The id of the host node for the shell, or `''`.
*
* #### Notes
* For this server application there is no shell to attach
*/
protected attachShell(id: string): void {
// no-op
}
/**
* A method invoked on a window `'resize'` event.
*
* #### Notes
* For this server application there is no shell to update
*/
protected evtResize(event: Event): void {
// no-op
}
/**
* Register plugins from a plugin module.
*
* @param mod - The plugin module to register.
*/
registerPluginModule(mod: JupyterLiteServer.IPluginModule): void {
let data = mod.default;
// Handle commonjs exports.
if (!Object.prototype.hasOwnProperty.call(mod, '__esModule')) {
data = mod as any;
}
if (!Array.isArray(data)) {
data = [data];
}
data.forEach((item) => {
try {
this.registerPlugin(item);
} catch (error) {
console.error(error);
}
});
}
/**
* Register the plugins from multiple plugin modules.
*
* @param mods - The plugin modules to register.
*/
registerPluginModules(mods: JupyterLiteServer.IPluginModule[]): void {
mods.forEach((mod) => {
this.registerPluginModule(mod);
});
}
private _router = new Router();
private _serviceManager: ServiceManager;
}
/**
* A namespace for Server statics.
*/
export namespace JupyterLiteServer {
/**
* The interface for a module that exports a plugin or plugins as
* the default value.
*/
export interface IPluginModule {
/**
* The default export.
*/
default: IPlugin<JupyterLiteServer, any> | IPlugin<JupyterLiteServer, any>[];
}
}