File tree Expand file tree Collapse file tree 3 files changed +21
-0
lines changed
Expand file tree Collapse file tree 3 files changed +21
-0
lines changed Original file line number Diff line number Diff line change 1+ import { Middleware } from 'koa' ;
2+
3+ // Enable cross-origin isolation for more precise timers:
4+ // https://developer.chrome.com/blog/cross-origin-isolated-hr-timers/
5+ export function crossOriginIsolation ( ) : Middleware {
6+ // Based on https://github.com/fishel-feng/koa-isolated
7+ return async function isolated ( ctx , next ) {
8+ ctx . set ( 'Cross-Origin-Opener-Policy' , 'same-origin' ) ;
9+ ctx . set ( 'Cross-Origin-Embedder-Policy' , 'require-corp' ) ;
10+ await next ( ) ;
11+ } ;
12+ }
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import {nodeResolve} from 'koa-node-resolve';
1919
2020import { BenchmarkResponse , Deferred } from './types' ;
2121import { NpmInstall } from './versions' ;
22+ import { crossOriginIsolation } from "./cross-origin-isolation" ;
2223
2324export interface ServerOpts {
2425 host : string ;
@@ -88,6 +89,7 @@ export class Server {
8889 this . server = server ;
8990 const app = new Koa ( ) ;
9091
92+ app . use ( crossOriginIsolation ( ) ) ;
9193 app . use ( bodyParser ( ) ) ;
9294 app . use ( mount ( '/submitResults' , this . submitResults . bind ( this ) ) ) ;
9395 app . use ( this . instrumentRequests . bind ( this ) ) ;
Original file line number Diff line number Diff line change @@ -135,4 +135,11 @@ suite('server', () => {
135135 session = server . endSession ( ) ;
136136 assert . equal ( session . bytesSent , 0 ) ;
137137 } ) ;
138+
139+ test ( 'enables cross-origin isolation' , async ( ) => {
140+ const res = await fetch ( `${ server . url } /import-bare-module.html` ) ;
141+
142+ assert . equal ( res . headers . get ( 'Cross-Origin-Opener-Policy' ) , 'same-origin' ) ;
143+ assert . equal ( res . headers . get ( 'Cross-Origin-Embedder-Policy' ) , 'require-corp' ) ;
144+ } ) ;
138145} ) ;
You can’t perform that action at this time.
0 commit comments