-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (49 loc) · 1.42 KB
/
index.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
const API = require('@ladjs/api');
const api = require('./api');
const apiConfig = require('./config/api');
const config = require('./config');
function plugin(opts, Bree) {
opts = {
port: config.port,
jwt: config.jwt,
sse: config.sse,
...opts
};
const api = new API(apiConfig(opts));
const oldInit = Bree.prototype.init;
Bree.prototype.init = async function () {
// hook error handler and message handler
const oldErrorHandler = this.config.errorHandler;
const oldWorkerMessageHandler = this.config.workerMessageHandler;
this.config.errorHandler = function (error, data) {
if (oldErrorHandler) {
oldErrorHandler.call(this, error, data);
}
this.emit('worker error', {
error,
name: data?.name,
data: data ? JSON.stringify(data) : undefined
});
};
this.config.errorHandler = this.config.errorHandler.bind(this);
this.config.workerMessageHandler = function (data) {
if (oldWorkerMessageHandler) {
oldWorkerMessageHandler.call(this, data);
}
this.emit('worker message', data);
};
this.config.workerMessageHandler =
this.config.workerMessageHandler.bind(this);
await oldInit.call(this);
// assign bree to the context
api.app.context.bree = this;
this.api = api;
this.api.listen(opts.port).catch((err) => {
throw err;
});
};
}
module.exports = {
api,
plugin
};