Files
threshold-ui/ui/desktop/electron-app/src/preload.js
T
85913acc61 Desktop Client Session Terminal (#1884)
* refactor: 💡 Turn on sandboxing for renderer process (#1870)

* chore: 🤖 add tabs for details and terminal view (#1871)

* feat: 🎸 Allow creating multiple terminals (#1872)

* chore: 🤖 add remove method to terminal (#1875)

* feat: 🎸 Clean up terminal tab (#1876)

* feat: 🎸 Handle resizing xterm and node-pty (#1879)

* feat: 🎸 Clean up killing terminals (#1881)

* refactor: 💡 Rename tabs component (#1882)

---------

Co-authored-by: Carlos Alcaide Corvo <ccorvo@hashicorp.com>
Co-authored-by: Dharini Jeeva <dharini@hashicorp.com>
2023-09-22 18:58:14 -04:00

64 lines
2.2 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
const { ipcRenderer, contextBridge } = require('electron');
// Messages must originate from this origin
const emberAppOrigin = window.location.origin;
/**
* Exposing terminal creation to an isolated context (Ember)
* More information about contextBridge https://www.electronjs.org/docs/latest/api/context-bridge
* usage example: window.terminal.send(data);
*/
contextBridge.exposeInMainWorld('terminal', {
// We could've sent data through our established postMessage pattern
// but we don't need a response back so we can make it include it here
// to make it simpler. This keeps sending and receiving handlers symmetrical.
send: (data, id) => {
ipcRenderer.send(`terminalKeystroke-${id}`, data);
},
receive: (callback, id) => {
const incomingDataChannel = `terminalIncomingData-${id}`;
ipcRenderer.on(incomingDataChannel, callback);
// Return a function for the caller to handle cleaning up the listener
return () => {
return ipcRenderer.removeListener(incomingDataChannel, callback);
};
},
create: (vars) => {
ipcRenderer.send('createTerminal', vars);
},
remove: (id) => {
ipcRenderer.send(`removeTerminal-${id}`);
},
resize: (size, id) => {
ipcRenderer.send(`resize-${id}`, size);
},
});
process.once('loaded', () => {
/**
* Ember-land has no access to the renderer or node modules, and thus
* cannot call into the main process or make invocations directly.
* In order to communicate with the main process, the Ember app uses
* the `postMessage` interface to send messages to its browser window.
* These messages include message ports with which to receive responses from
* the main process.
*
* This preload script simply wires up the forwarding of
* messages received on the window and responses received from main.
*/
window.addEventListener('message', async function (event) {
if (event.origin !== emberAppOrigin) return;
const { method, payload } = event?.data ?? {};
if (method) {
const response = await ipcRenderer.invoke(method, payload);
event.ports[0].postMessage(response);
}
});
});