4

We have an electron app which uses ShareArrayBuffer. It was written with a very old version of electron. When we updated the electron version in order to use SIMD instructions in WASM, it started showing an error saying ShareArrayBuffer is not defined. It seems it is due to the security update in Chromeum and ShareArrayBuffer is available iff Cross Origin Isolation is set.

Google shows some articiles explaining how to enable Cross Origin Isolation on web-pages, but I have not found any article that explains how to do that on an electron app. Does anyone have a pointer to the info or an idea to try?

Thank you,

2 Answers2

5

It can be enabled by adding the following line in background.js

app.commandLine.appendSwitch('enable-features','SharedArrayBuffer')

Reference

tomerpacific
  • 4,704
  • 13
  • 34
  • 52
  • 1
    This doesn't work for me using latest version of electron – Gillespie Nov 30 '22 at 06:51
  • I am working with Electron 23.2.1 and this solution works well! To be clear; I put the code of @tomerpacific in the main process. Then, SharedArrayBuffer works on the rendering process side. My case, the memory is shared between the main event loop (of the rendering process) and a web worker. – Hiroshi May 30 '23 at 18:32
  • @Hiroshi - I didn't post the answer (I just edited it). – tomerpacific May 30 '23 at 18:48
  • tomerpacific, Sorry for my misunderstanding. Looks like I cannot edit my previous comment any more. Correction: I put the code of @Harunobu Oyama on Electron 23.2.1, and that works well for me. – Hiroshi May 30 '23 at 19:14
5

As of December 2022, this is the only thing that worked for me:

    browserWindow = new BrowserWindow({...});

    // Enable SharedArrayBuffer
    browserWindow.webContents.session.webRequest.onHeadersReceived((details, callback) => {
        details.responseHeaders['Cross-Origin-Opener-Policy'] = ['same-origin'];
        details.responseHeaders['Cross-Origin-Embedder-Policy'] = ['require-corp'];
        callback({ responseHeaders: details.responseHeaders });
    });
Gillespie
  • 5,780
  • 3
  • 32
  • 54