forked from Polymer/web-component-tester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.ts
205 lines (185 loc) · 6.58 KB
/
context.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
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
import * as events from 'events';
import * as http from 'spdy';
import * as _ from 'lodash';
import * as socketIO from 'socket.io';
import * as util from 'util';
import {BrowserRunner} from './browserrunner';
import * as config from './config';
import {Plugin} from './plugin';
export type Handler = ((...args: any[]) => Promise<any>) |
((done: (err?: any) => void) => void) |
((arg1: any, done: (err?: any) => void) => void) |
((arg1: any, arg2: any, done: (err?: any) => void) => void) |
((arg1: any, arg2: any, arg3: any, done: (err?: any) => void) => void);
/**
* Exposes the current state of a WCT run, and emits events/hooks for anyone
* downstream to listen to.
*
* TODO(rictic): break back-compat with plugins by moving hooks entirely away
* from callbacks to promises. Easiest way to do this would be to rename
* the hook-related methods on this object, so that downstream callers would
* break in obvious ways.
*
* @param {Object} options Any initially specified options.
*/
export class Context extends events.EventEmitter {
options: config.Config;
private _hookHandlers: {[key: string]: Handler[]} = {};
_socketIOServers: SocketIO.Server[];
_httpServers: http.Server[];
_testRunners: BrowserRunner[];
constructor(options?: config.Config) {
super();
options = options || {};
/**
* The configuration for the current WCT run.
*
* We guarantee that this object is never replaced (e.g. you are free to
* hold a reference to it, and make changes to it).
*/
this.options = config.merge(
config.defaults(),
config.fromDisk(options.enforceJsonConf, options.root), options);
}
// Hooks
//
// In addition to emitting events, a context also exposes "hooks" that
// interested parties can use to inject behavior.
/**
* Registers a handler for a particular hook. Hooks are typically configured
* to run _before_ a particular behavior.
*/
hook(name: string, handler: Handler) {
this._hookHandlers[name] = this._hookHandlers[name] || [];
this._hookHandlers[name].unshift(handler);
};
/**
* Registers a handler that will run after any handlers registered so far.
*
* @param {string} name
* @param {function(!Object, function(*))} handler
*/
hookLate(name: string, handler: Handler) {
this._hookHandlers[name] = this._hookHandlers[name] || [];
this._hookHandlers[name].push(handler);
};
/**
* Once all registered handlers have run for the hook, your callback will be
* triggered. If any of the handlers indicates an error state, any subsequent
* handlers will be canceled, and the error will be passed to the callback for
* the hook.
*
* Any additional arguments passed between `name` and `done` will be passed to
* hooks (before the callback).
*
* @param {string} name
* @param {function(*)} done
* @return {!Context}
*/
emitHook(
name: 'prepare:webserver', app: Express.Application,
done?: (err?: any) => void): Promise<void>;
emitHook(name: 'configure', done?: (err?: any) => void): Promise<void>;
emitHook(name: 'prepare', done?: (err?: any) => void): Promise<void>;
emitHook(name: 'cleanup', done?: (err?: any) => void): Promise<void>;
emitHook(name: string, done?: (err?: any) => void): Promise<void>;
emitHook(name: string, ...args: any[]): Promise<void>;
async emitHook(name: string /*, ...args: any[]*/): Promise<void> {
this.emit('log:debug', 'hook:', name);
// TODO(justinfagnani): remove and uncomment ...args when we drop node 4
const args = Array.from(arguments).slice(1);
const hooks = (this._hookHandlers[name] || []);
type BoundHook = (cb: (err: any) => void) => (void|Promise<any>);
let boundHooks: BoundHook[];
let done: (err?: any) => void = (_err: any) => {};
let argsEnd = args.length - 1;
if (args[argsEnd] instanceof Function) {
done = args[argsEnd];
argsEnd = argsEnd--;
}
const hookArgs = args.slice(0, argsEnd + 1);
boundHooks = hooks.map((hook) => hook.bind.apply(hook, [null].concat(hookArgs)));
if (!boundHooks) {
boundHooks = <any>hooks;
}
// A hook may return a promise or it may call a callback. We want to
// treat hooks as though they always return promises, so this converts.
const hookToPromise = (hook: BoundHook) => {
return new Promise((resolve, reject) => {
const maybePromise = hook((err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
if (maybePromise) {
maybePromise.then(resolve, reject);
}
});
};
// We execute the handlers _sequentially_. This may be slower, but it gives
// us a lighter cognitive load and more obvious logs.
try {
for (const hook of boundHooks) {
await hookToPromise(hook);
}
} catch (err) {
// TODO(rictic): stop silently swallowing the error here and just below.
// Looks like we'll need to track down some error being thrown from
// deep inside the express router.
try {
done(err);
} catch (_) {
}
throw err;
}
try {
done();
} catch (_) {
}
};
/**
* @param {function(*, Array<!Plugin>)} done Asynchronously loads the plugins
* requested by `options.plugins`.
*/
async plugins(): Promise<Plugin[]> {
const plugins: Plugin[] = [];
for (const name of this.enabledPlugins()) {
plugins.push(await Plugin.get(name));
}
return plugins;
}
/**
* @return {!Array<string>} The names of enabled plugins.
*/
enabledPlugins(): string[] {
// Plugins with falsy configuration or disabled: true are _not_ loaded.
const pairs = _.reject(
(<any>_).pairs(this.options.plugins),
(p: [string, {disabled: boolean}]) => !p[1] || p[1].disabled);
return _.map(pairs, (p) => p[0]);
};
/**
* @param {string} name
* @return {!Object}
*/
pluginOptions(name: string) {
return this.options.plugins[Plugin.shortName(name)];
};
static Context = Context;
}
module.exports = Context;