forked from kraenhansen/mocha-remote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.ts
466 lines (429 loc) · 14.6 KB
/
Server.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import Mocha from "mocha";
import WebSocket, { WebSocketServer, AddressInfo } from "ws";
import path from "path";
import type http from "http";
import flatted from "flatted";
import type { ClientMessage, CustomContext, ServerMessage, MochaConfig } from "mocha-remote-common";
export type { CustomContext };
import { createStatsCollector } from "./stats-collector";
import { extend, Debugger } from "./debug";
import { FakeRunner } from "./FakeRunner";
import { ServerEventEmitter } from "./ServerEventEmitter";
import { deserialize } from "./serialization";
type MochaReporters = { [name: string]: typeof Mocha.reporters.Base };
const builtinReporters: MochaReporters = Mocha.reporters;
export type ReporterOptions = { [key: string]: string | boolean };
function createPromiseHandle() {
let resolve: () => void = () => {
throw new Error(
"Expected new Promise callback to be called synchronously"
);
};
const promise = new Promise<void>(r => (resolve = r));
return { promise, resolve };
}
/**
* An error thrown by a connected client
*/
export class ClientError extends Error {
public readonly ws: WebSocket;
constructor(message: string, ws: WebSocket) {
super(message);
this.ws = ws;
}
}
export interface ServerConfig {
/** Network hostname to use when listening for clients */
host: string;
/** Network port to use when listening for clients */
port: number;
/** Start the server as soon as it gets constructed */
autoStart: boolean;
/** Start running tests as soon as a client connects */
autoRun: boolean;
/** An ID expected by the clients connecting */
id: string;
/** Specify reporter to use */
reporter: Mocha.ReporterConstructor | string;
/** Reporter-specific options */
reporterOptions: ReporterOptions;
/** ReporterOptions was previously the only way to specify options to reporter */
reporterOption?: ReporterOptions;
/** Only run tests matching this string or regexp */
grep: string | undefined;
/** Inverts grep matches */
invert: boolean | undefined;
/** Tests needs to complete before this timeout threshold (in milliseconds) */
timeout: number | undefined;
/** Specifies "slow" test threshold (in milliseconds) */
slow: number | undefined;
/** Runtime context sent to client when starting a run */
context: CustomContext | undefined;
}
export class Server extends ServerEventEmitter {
public static DEFAULT_CONFIG: ServerConfig = {
autoStart: false,
host: "localhost",
id: "default",
port: 8090,
autoRun: false,
reporter: "spec",
reporterOptions: {},
grep: undefined,
invert: undefined,
context: undefined,
timeout: undefined,
slow: undefined,
};
private static debugCounter = 0;
private static nextDebug() {
return extend(`Server[${Server.debugCounter++}]`);
}
public readonly stopped: Promise<void>;
public readonly config: ServerConfig;
private readonly debug: Debugger;
private wss?: WebSocketServer;
private client?: WebSocket;
private runner?: FakeRunner;
private stoppedPromiseHandle = createPromiseHandle();
/** The options to send to the next connecting running client */
private clientOptions?: MochaConfig = {};
private _listening = false;
constructor(
config: Partial<ServerConfig> = {},
debug = Server.nextDebug(),
) {
super(debug.extend("events"));
this.debug = debug;
this.debug("Constructing a server");
this.config = { ...Server.DEFAULT_CONFIG, ...config };
this.stopped = this.stoppedPromiseHandle.promise;
}
public async start(): Promise<void> {
this.debug(`Server is starting`);
await new Promise<void>((resolve, reject) => {
this.wss = new WebSocketServer({
host: this.config.host,
port: this.config.port
});
// When a client connects
this.wss.on("connection", this.handleConnection);
// When the server starts listening
this.wss.once("listening", () => {
this.debug(`Server is listening on ${this.url}`);
resolve();
});
// If an error happens while starting
this.wss.once("error", (err: Error) => {
this.debug(`Server failed to start ${err.stack}`);
this.emit("error", err);
reject(err);
});
});
this._listening = true;
this.emit("started", this);
}
public async stop(code = 1000, reason = "Server stopping"): Promise<void> {
this.debug("Server is stopping");
await new Promise<void>((resolve, reject) => {
if (this.wss) {
// Close any client connections, giving a code and reason
for (const ws of this.wss.clients) {
ws.close(code, reason);
}
// Close the server
this.wss.close(err => {
this.handleReset();
// Forget about the server
delete this.wss;
// Reject or resolve the promise
if (err) {
reject(err);
} else {
resolve();
}
});
// Terminate any clients still connected, allowing the server to close
for (const ws of this.wss.clients) {
ws.terminate();
}
} else {
resolve();
}
});
this.debug("Server was stopped");
// Resolve the stopped promise
this._listening = false;
this.stoppedPromiseHandle.resolve();
}
public get listening() {
return this._listening;
}
public run(fn: (failures: number) => void, context?: CustomContext): Mocha.Runner {
this.debug("Server started running tests");
if (!this.wss) {
if (this.config.autoStart) {
this.start().then(undefined, err => {
this.debug(`Auto-starting failed: ${err.stack}`);
});
} else {
throw new Error("Server must be started before run is called");
}
}
if (this.runner) {
throw new Error("A run is already in progress");
}
// this.runner = new Mocha.Runner(this.suite, this.options.delay || false);
// TODO: Stub this to match the Runner's interface even better
this.runner = new FakeRunner();
// Attach event listeners to update stats
createStatsCollector(this.runner as Mocha.Runner);
// Set the client options, to be passed to the next running client
this.clientOptions = {
grep: this.config.grep,
invert: this.config.invert,
timeout: this.config.timeout,
slow: this.config.slow,
context: {
...this.config.context,
...context,
},
};
this.debug(this.clientOptions);
// We need to access the private _reporter field
const Reporter = this.determineReporterConstructor(this.config.reporter);
// When constructing the Reporter we need to (unintuitively) pass all options, not the `options.reporterOptions`
const reporter = new Reporter(this.runner as Mocha.Runner, {
// alias option name is used in public reporters xunit/tap/progress
// https://github.com/mochajs/mocha/issues/4153
reporterOptions: this.config.reporterOptions ?? this.config.reporterOption,
reporterOption: this.config.reporterOption ?? this.config.reporterOptions,
} as { reporterOptions: ReporterOptions, reporterOption: ReporterOptions });
const done = (failures: number) => {
this.debug("Testing is done");
// If the reporter wants to know when we're done, we will tell it
// It will call the fn callback for us
if (reporter.done) {
reporter.done(failures, fn);
} else if (fn) {
fn(failures);
}
};
// Emit event when the tests starts running
this.runner.once(FakeRunner.constants.EVENT_RUN_BEGIN, () => {
this.emit("running", this.runner as Mocha.Runner);
});
// Attach a listener to the run ending
this.runner.once(FakeRunner.constants.EVENT_RUN_END, () => {
const failures = this.runner ? this.runner.failures : 0;
// Call any callbacks to signal completion
done(failures);
this.handleReset();
});
// If we already have a client, tell it to run
if (this.client && this.client.readyState === WebSocket.OPEN) {
this.send({
action: "run",
options: this.clientOptions,
});
}
// Return the runner
return this.runner as Mocha.Runner;
}
public async runAndStop(context?: CustomContext): Promise<void> {
let handleError: undefined | ((err: Error) => void) = undefined;
try {
// Run the tests
// TODO: Consider adding a timeout
const failures = await new Promise<number>((resolve, reject) => {
// Register an error handler and keep a reference to remove it later
handleError = reject;
this.on("error", handleError);
this.run(resolve, context);
});
if (failures > 0) {
throw new Error(`Tests completed with ${failures} failures`);
}
} finally {
// Stop handling errors
if (handleError) {
this.off("error", handleError);
}
// Stop the server
await this.stop();
}
}
public get port(): number {
if (this.wss) {
const { port } = this.wss.address() as AddressInfo;
return port;
} else {
throw new Error("Cannot get port of a server that is not listening");
}
}
public get url(): string {
if (this.wss) {
const { address, port, family } = this.wss.address() as AddressInfo;
if (family === "IPv6") {
return `ws://[${address}]:${port}`;
} else {
return `ws://${address}:${port}`;
}
} else {
throw new Error("Cannot get url of a server that is not listening");
}
}
public send(msg: ClientMessage): void {
if (this.client && this.client.readyState === WebSocket.OPEN) {
const data = flatted.stringify(msg);
this.client.send(data);
} else {
throw new Error("No client connected");
}
}
private handleConnection = (ws: WebSocket, req: http.IncomingMessage) => {
this.debug("Client connected");
// Check that the protocol matches
const expectedProtocol = `mocha-remote-${this.config.id}`;
ws.on("close", (code, reason) => {
this.emit("disconnection", ws, code, reason.toString());
});
// Signal that a client has connected
this.emit("connection", ws, req);
// Disconnect if the protocol mismatch
if (ws.protocol !== expectedProtocol) {
// Protocol mismatch - close the connection
ws.close(
1002,
`Expected "${expectedProtocol}" protocol got "${ws.protocol}"`
);
return;
}
if (this.client) {
this.debug("A client was already connected");
this.client.close(
1013 /* try again later */,
"Got a connection from another client"
);
// Reset the server to prepare for the incoming client
this.handleReset();
}
// Hang onto the client
this.client = ws;
this.client.on("message", this.handleMessage.bind(this, this.client));
this.client.once("close", this.handleReset);
// If we already have a runner, it can run now that we have a client
if (this.runner) {
if (this.clientOptions) {
this.send({ action: "run", options: this.clientOptions });
} else {
throw new Error("Internal error: Expected a clientOptions");
}
} else if (this.config.autoRun) {
this.debug("Start running tests because a client connected");
this.run(() => {
this.debug("Stopped running tests from connection");
});
}
};
private handleMessage = (ws: WebSocket, message: string) => {
try {
const msg = deserialize(message) as ServerMessage;
if (typeof msg.action !== "string") {
throw new Error("Expected message to have an action property");
}
this.debug(`Received a '${msg.action}' message: %o`, msg);
if (msg.action === "event") {
if (this.runner) {
const args = msg.args || [];
this.runner.emit(msg.name, ...args);
} else {
throw new Error(
"Received a message from the client, but server wasn't running"
);
}
} else if (msg.action === "error") {
if (typeof msg.message !== "string") {
throw new Error("Expected 'error' action to have an error argument with a message");
}
const err = new ClientError(msg.message, ws);
this.emit("error", err);
} else {
const { action } = msg;
throw new Error(`Unexpected action "${action}"`);
}
} catch (err) {
if (err instanceof Error) {
this.emit("error", err);
this.send({ action: "error", message: err.message });
} else {
throw err;
}
}
};
/**
* Resets the server for another test run.
*/
private handleReset = () => {
// Forget everything about the runner and the client
const { runner, client } = this;
delete this.runner;
delete this.client;
delete this.clientOptions;
if (runner) {
runner.removeAllListeners();
// Relay this onto the server itself
this.emit("end");
}
if (client) {
if (client.readyState !== WebSocket.CLOSED) {
client.terminate();
}
client.removeAllListeners();
}
};
/**
* @param reporter A constructor or a string containing the name of a builtin reporter or the module name or relative path of one.
* @returns A constructor for the reporter.
* @see {Mocha.prototype.reporter}
*/
private determineReporterConstructor(reporter: string | Mocha.ReporterConstructor): typeof Mocha.reporters.Base {
if (typeof reporter === "function") {
return reporter as unknown as typeof Mocha.reporters.Base;
} else if (typeof reporter === "string") {
// Try to load a built-in reporter
if (reporter in builtinReporters) {
return builtinReporters[reporter];
}
// Try to load reporters from process.cwd() and node_modules
try {
try {
return require(reporter);
} catch (err) {
if (err instanceof Error) {
const { code } = err as NodeJS.ErrnoException;
if (code === 'MODULE_NOT_FOUND') {
// If the absolute require couldn't find the module, let's try resolving it as a relative path
return require(path.resolve(reporter));
}
}
// Fallback to rethrow
throw err;
}
} catch (err) {
if (err instanceof Error) {
const { code, stack } = err as NodeJS.ErrnoException;
if (code === 'MODULE_NOT_FOUND') {
throw new Error(`Unable to find reporter: '${reporter}'`);
} else {
throw new Error(`${reporter} reporter blew up with error: ${stack}`);
}
}
// Fallback to rethrow
throw err;
}
} else {
throw new Error(`Unexpected reporter '${reporter}'`);
}
}
}