-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.js
276 lines (255 loc) · 7.65 KB
/
runner.js
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
const Promise = require("bluebird");
const vm = require("vm");
module.exports = class Runner {
constructor(steps, debug, options) {
this.forever = false;
this.client = null;
this.$session = {};
this.$options = options || {};
this.debug = {
verbose: (debug && debug.verbose) || (() => {}),
info: (debug && debug.info) || (() => {}),
warn: (debug && debug.warn) || (() => {}),
error: (debug && debug.error) || (() => {}),
end: (debug && debug.end) || (() => {})
};
this.context = vm.createContext({
$session: this.$session,
$options: this.$options,
env: process.env
});
this.funcs = steps.map(step => {
if (this.forever) {
throw new Error("redundant steps after a forever step");
}
if (!step || typeof step !== "object") {
throw new Error("step must be an object");
}
const [key] = Object.keys(step);
if (!key) {
throw new Error("empty step");
}
const value = step[key];
if (key === "connect") {
if (!value) {
throw new Error("connect must be a string or an object");
}
const Client = require("./client");
return () => {
let uri, host, port, ssl;
const options = this.parse(value);
if (typeof options === "string") {
uri = options;
} else if (options.host && options.port) {
host = options.host;
port = options.port;
ssl = options.ssl;
} else {
throw new Error("missing host/port in connect");
}
this.client = new Client({
uri,
host,
port,
ssl,
timeout: options.timeout,
errorHandler: this.onError.bind(this),
messageHandler: this.onMessage.bind(this),
kickHandler: this.onKick.bind(this),
disconnectHandler: this.onDisconnect.bind(this)
});
this.debug.info(`connect: ${this.client.uri}`);
return this.client.connect();
};
}
if (key === "disconnect") {
return () => {
this.debug.info(`disconnect: ${this.client.uri}`);
return this.client.disconnect();
};
}
if (key === "sleep") {
if (typeof value !== "number") {
throw new Error("sleep must be a number");
}
return () =>
new Promise(resolve => {
this.debug.info(`sleep: ${value}`);
setTimeout(resolve, value);
});
}
if (key === "echo") {
if (value === undefined) {
throw new Error("echo must be defined");
}
return () => {
const output = this.parse(value);
this.debug.verbose(JSON.stringify(output));
};
}
if (key === "emit") {
if (!value) {
throw new Error("emit must be a string or an object");
}
if (typeof value === "string") {
return () => {
this.debug.info(`emit: ${value}`);
return this.client
.request(value)
.then(res => this.debug.verbose(JSON.stringify(res)));
};
}
const route = value.route;
if (typeof route !== "string") {
throw new Error("missing route in emit");
}
const data = value.data;
const expect = value.expect;
const success = value.success;
if (expect && typeof expect !== "string") {
throw new Error("done must be a string");
}
if (success && typeof success !== "string") {
throw new Error("success must be a string");
}
const repeat = value.repeat;
if (repeat && typeof repeat !== "object") {
throw new Error("repeat must be an object");
}
const count = repeat && repeat.count;
const sleep = repeat && repeat.sleep;
if (sleep && typeof sleep !== "number") {
throw new Error("sleep must be a number");
}
if (count && typeof count !== "number") {
throw new Error("count must be a number");
}
this.forever = repeat && !count;
return () => {
const msg = this.parse(data);
if (msg === undefined) {
this.debug.info(`emit: ${route}`);
} else {
this.debug.info(`emit: ${route} ${JSON.stringify(msg)}`);
}
const emit = () =>
this.client.request(route, msg).then(res => {
this.debug.verbose(JSON.stringify(res));
const context = vm.createContext(
Object.assign({}, res, {
$session: this.$session,
$options: this.$options
})
);
if (expect) {
if (!vm.runInContext(expect, context)) {
throw new Error(
`unexpected:\n\n${expect}\n\n${JSON.stringify(
res
)}\n\n${JSON.stringify({
$session: this.$session,
$options: this.$options
})}`
);
}
}
if (success) {
vm.runInContext(success, context);
}
});
if (!repeat) {
return emit();
}
if (this.forever) {
const run = () => {
emit().then(() => {
if (!sleep) {
run();
} else {
this.debug.info(`sleep: ${sleep}`);
setTimeout(() => run(), sleep);
}
});
};
this.debug.info("repeat: forever");
return run();
}
let cycle = 0;
return new Promise((resolve, reject) => {
const run = () => {
if (cycle++ < count) {
this.debug.info(`repeat: ${cycle}/${count}`);
emit()
.then(() => {
if (!sleep || cycle >= count) {
run();
} else {
this.debug.info(`sleep: ${sleep}`);
setTimeout(() => run(), sleep);
}
})
.catch(reject);
} else {
resolve();
}
};
run();
});
};
}
throw new Error(`unknown step ${key}`);
});
}
resolve(arg) {
if (!arg) {
return arg;
}
if (Array.isArray(arg)) {
for (let i = 0; i < arg.length; i++) {
arg[i] = this.parse(arg[i]);
}
return arg;
}
if (typeof arg === "object") {
if (typeof arg.$eval === "string") {
const res = vm.runInContext(arg.$eval, this.context);
return res;
}
for (const key in arg) {
arg[key] = this.parse(arg[key]);
}
}
return arg;
}
parse(arg) {
return arg ? this.resolve(JSON.parse(JSON.stringify(arg))) : arg;
}
run() {
return Promise.mapSeries(this.funcs, func =>
Promise.try(() => func(this))
).then(() => {
if (!this.forever) {
this.debug.end();
}
return this;
});
}
onError(err) {
this.debug.error(err);
}
onMessage(route, msg) {
this.debug.verbose("event:", route, msg);
}
onKick(msg) {
this.debug.warn("kicked", msg && JSON.stringify(msg));
}
onDisconnect(code, reason) {
if (code !== 1000) {
this.debug.error(
new Error(`disconnected: code: ${code}, reason: ${reason}`)
);
} else {
this.debug.info("disconnected");
}
}
};