Skip to content

Commit 7101af7

Browse files
committed
fix: enable cross-origin isolation
1 parent 7bf3c57 commit 7101af7

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/cross-origin-isolation.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

src/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {nodeResolve} from 'koa-node-resolve';
1919

2020
import {BenchmarkResponse, Deferred} from './types';
2121
import {NpmInstall} from './versions';
22+
import {crossOriginIsolation} from "./cross-origin-isolation";
2223

2324
export 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));

src/test/server_test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

0 commit comments

Comments
 (0)