2020-10-05 15:11:37 +02:00
|
|
|
/* tslint:disable:no-console */
|
|
|
|
import * as ipc from 'node-ipc';
|
|
|
|
|
|
|
|
ipc.config.id = 'proxy';
|
|
|
|
ipc.config.retry = 1500;
|
|
|
|
ipc.config.logger = console.warn; // Stdout is used for native messaging
|
|
|
|
|
|
|
|
export default class IPC {
|
|
|
|
private connected = false;
|
2020-10-11 20:41:10 +02:00
|
|
|
public onMessage: (message: object) => void
|
2020-10-05 15:11:37 +02:00
|
|
|
|
|
|
|
connect() {
|
|
|
|
ipc.connectTo('bitwarden', () => {
|
|
|
|
ipc.of.bitwarden.on('connect', () => {
|
|
|
|
this.connected = true;
|
|
|
|
console.error(
|
|
|
|
'## connected to bitwarden desktop ##',
|
|
|
|
ipc.config.delay
|
|
|
|
);
|
2020-10-21 16:48:40 +02:00
|
|
|
|
|
|
|
// Notify browser extension, connection is established to desktop application.
|
2020-10-21 15:56:27 +02:00
|
|
|
this.onMessage({command: 'connected'})
|
2020-10-05 15:11:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
ipc.of.bitwarden.on('disconnect', () => {
|
|
|
|
this.connected = false;
|
|
|
|
console.error('disconnected from world');
|
2020-10-21 16:48:40 +02:00
|
|
|
|
|
|
|
// Notify browser extension, no connection to desktop application.
|
2020-10-21 15:56:27 +02:00
|
|
|
this.onMessage({command: 'disconnected'})
|
2020-10-05 15:11:37 +02:00
|
|
|
});
|
|
|
|
|
2020-10-11 20:41:10 +02:00
|
|
|
ipc.of.bitwarden.on('message', (message: any) => {
|
|
|
|
this.onMessage(message);
|
2020-10-05 15:11:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
ipc.of.bitwarden.on('error', (err: any) => {
|
|
|
|
console.error('error', err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-05 19:48:51 +02:00
|
|
|
isConnected(): boolean {
|
2020-10-05 15:11:37 +02:00
|
|
|
return this.connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
send(json: object) {
|
|
|
|
ipc.of.bitwarden.emit('message', json);
|
|
|
|
}
|
|
|
|
}
|