-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipc-main-api.ts
46 lines (42 loc) · 1.58 KB
/
ipc-main-api.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
import {app} from "electron";
import {interval} from "rxjs";
import {map, mergeMap, take} from "rxjs/operators";
import {observableToSubscribableLike} from "electron-rpc-api";
import {promisify} from "util";
import sanitizeHtml from "sanitize-html";
import tcpPing from "tcp-ping";
import {IPC_MAIN_API_SERVICE, ScannedIpcMainApiService} from "src/shared/ipc-main-api-definition";
export function register(): ScannedIpcMainApiService["ApiClient"] {
const api: ScannedIpcMainApiService["ApiImpl"] = {
ping: ({domain, times}) => {
return observableToSubscribableLike(
interval(/*one second*/ 1000).pipe(
take(times),
mergeMap(async () => promisify(tcpPing.ping)({address: domain, attempts: times})),
map(({avg: value}) => {
if (typeof value === "undefined" || isNaN(value)) {
throw new Error(`Host "${domain}" is unreachable`);
}
return {domain, value};
}),
)
);
},
async sanitizeHtml(input) {
return sanitizeHtml(
input,
{
allowedTags: sanitizeHtml.defaults.allowedTags.concat(["span"]),
allowedClasses: {
span: ["badge", "bg-secondary", "bg-danger"],
},
},
);
},
async quitApp() {
app.quit();
},
};
IPC_MAIN_API_SERVICE.register(api);
return api;
}