karma icon indicating copy to clipboard operation
karma copied to clipboard

Support cross-origin isolation

Open taymonbeal opened this issue 3 years ago • 2 comments

I'm testing code that uses SharedArrayBuffer and I'm getting the following warning in the Chrome console:

[Deprecation] SharedArrayBuffer will require cross-origin isolation as of M91, around May 2021. See https://developer.chrome.com/blog/enabling-shared-array-buffer/ for more details.

Will there be a way to enable cross-origin isolation in Karma so that SharedArrayBuffer doesn't break?

taymonbeal avatar Apr 23 '21 00:04 taymonbeal

After some digging through the code, I fould out that you can inject custom middlewares into the http server like this:

karma.config.js:

function CrossOriginIsolationMiddlewareFactory(config) {
    return function crossOriginIsolation(req, res, next) {
        res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
        res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
        next();
    };
}

module.exports = function (config) {
    config.set({

        // ...

        plugins: [
            // load all default karma plugins (this would be the default value if we didn't specify the plugins manually)
            'karma-*',
            // add custom middleware plugin
            { 'middleware:cross-origin-isolation': ['factory', CrossOriginIsolationMiddlewareFactory] }
        ],

        // ...

        beforeMiddleware: [
            'cross-origin-isolation',
        ],

        // ...

    });
};

Would be amazing to have a simple configuration option nonetheless.

TheSlowGrowth avatar Jul 28 '21 11:07 TheSlowGrowth

Thank you so much for posting this code! 100% agree that it would be really useful to have this as a simple configuration option.

emilydolson avatar Feb 13 '22 21:02 emilydolson