1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-13 10:24:20 +01:00
bitwarden-browser/src/proxy/ipc.ts

56 lines
1.6 KiB
TypeScript
Raw Normal View History

/* tslint:disable:no-console */
import * as ipc from 'node-ipc';
import { homedir } from 'os';
ipc.config.id = 'proxy';
ipc.config.retry = 1500;
ipc.config.logger = console.warn; // Stdout is used for native messaging
2021-01-26 19:11:36 +01:00
if (process.platform === 'darwin') {
ipc.config.socketRoot = `${homedir()}/tmp/`;
2021-01-26 19:11:36 +01:00
}
export default class IPC {
2021-02-03 19:21:22 +01:00
onMessage: (message: object) => void;
2020-11-23 18:37:04 +01:00
private connected = false;
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.
2021-02-03 19:21:22 +01:00
this.onMessage({command: 'connected'});
});
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.
2021-02-03 19:21:22 +01:00
this.onMessage({command: 'disconnected'});
});
ipc.of.bitwarden.on('message', (message: any) => {
this.onMessage(message);
});
ipc.of.bitwarden.on('error', (err: any) => {
console.error('error', err);
});
});
}
isConnected(): boolean {
return this.connected;
}
send(json: object) {
ipc.of.bitwarden.emit('message', json);
}
}