-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackend.js
40 lines (39 loc) · 1.7 KB
/
backend.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
module.exports = (...params) => class backend extends require('ut-port-jsonrpc')(...params) {
get defaults() {
return {
imports: [/\.backend$/],
logLevel: 'trace',
method: 'post'
};
}
handlers() {
const {send, receive} = super.handlers();
let utVersion = null;
return {
start() {
if (!process.browser && !this.config.url) {
const server = this.bus && typeof this.bus.config.server === 'function' && this.bus.config.server();
const rpc = (server && server.serviceBus && server.serviceBus.rpc) || this.bus;
const status = rpc && rpc.info;
const {host, port, protocol} = typeof status === 'function' && rpc.info();
if (host && port && protocol) this.config.url = `${protocol}://${host}:${port}`;
}
},
send(params, {method}) {
if (!params.$http) params.$http = {};
if (!params.$http.uri) params.$http.uri = `/rpc/${method.replace(/\//ig, '%2F').replace(/\./g, '/')}`;
return send.apply(this, arguments);
},
receive(msg, $meta) {
const headers = $meta?.response?.headers;
const serverVersion = headers?.['x-ut-version'];
utVersion = utVersion ?? serverVersion;
if (serverVersion && utVersion !== serverVersion) {
// refresh the current page in order to fetch the latest ui assets
window?.location?.reload?.();
}
return receive.apply(this, arguments);
}
};
}
};