1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* scirs2-wasm server configuration helpers.
*
* Copy the snippets below into your server framework of choice to enable
* Cross-Origin Isolation (required for SharedArrayBuffer / Atomics).
*
* Without these headers the browser sets SharedArrayBuffer to `undefined`,
* disabling the zero-copy path in scirs2-wasm.
*/
// ---------------------------------------------------------------------------
// Required headers (must appear on EVERY response — page, JS, WASM, workers)
// ---------------------------------------------------------------------------
const COOP_COEP_HEADERS = ;
// ---------------------------------------------------------------------------
// Express.js middleware
// ---------------------------------------------------------------------------
/**
* Express middleware that sets COOP/COEP headers on every response.
*
* @example
* import express from 'express';
* import { coopCoepMiddleware } from './js/setup.js';
*
* const app = express();
* app.use(coopCoepMiddleware);
* app.use(express.static('dist'));
* app.listen(3000);
*/
// ---------------------------------------------------------------------------
// Vite plugin (vite.config.ts snippet)
// ---------------------------------------------------------------------------
/**
* Returns a Vite plugin that injects COOP/COEP headers in dev mode.
*
* @example
* // vite.config.ts
* import { coopCoepVitePlugin } from './js/setup.js';
* export default defineConfig({ plugins: [coopCoepVitePlugin()] });
*/
// ---------------------------------------------------------------------------
// Next.js headers() config (next.config.js snippet)
// ---------------------------------------------------------------------------
/**
* Returns a Next.js `headers()` async function entry that applies COOP/COEP
* headers to all routes.
*
* @example
* // next.config.js
* import { nextCoopCoepHeaders } from './js/setup.js';
* module.exports = { async headers() { return [nextCoopCoepHeaders()]; } };
*/
// ---------------------------------------------------------------------------
// Verification helper (browser only)
// ---------------------------------------------------------------------------
/**
* Check whether Cross-Origin Isolation is active.
*
* Call this in the browser after loading your page. If it returns false,
* SharedArrayBuffer will be unavailable and zero-copy transfers will be
* disabled.
*
* @returns {boolean}
*/
/**
* Check whether SharedArrayBuffer is available.
*
* @returns {boolean}
*/
// ---------------------------------------------------------------------------
// scirs2-wasm initialisation helper
// ---------------------------------------------------------------------------
/**
* Initialise the scirs2-wasm module and return the exports.
*
* Logs a warning when SharedArrayBuffer is unavailable (COOP/COEP not set).
*
* @param {string} wasmUrl - URL to the compiled WASM binary (e.g. '/pkg/scirs2_wasm_bg.wasm')
* @returns {Promise<WebAssembly.Instance>}
*
* @example
* import { initScirs2Wasm } from './js/setup.js';
* const wasm = await initScirs2Wasm('/pkg/scirs2_wasm_bg.wasm');
*/
// ---------------------------------------------------------------------------
// Static configuration strings for copy-paste into server configs
// ---------------------------------------------------------------------------
const NGINX_SNIPPET = `
# Add inside your location block or server block:
add_header Cross-Origin-Opener-Policy same-origin always;
add_header Cross-Origin-Embedder-Policy require-corp always;
`.;
const CADDY_SNIPPET = `
# Add inside your site block:
header Cross-Origin-Opener-Policy same-origin
header Cross-Origin-Embedder-Policy require-corp
`.;
const APACHE_SNIPPET = `
# Add inside <VirtualHost> or <Directory>:
Header always set Cross-Origin-Opener-Policy "same-origin"
Header always set Cross-Origin-Embedder-Policy "require-corp"
`.;
// ---------------------------------------------------------------------------
// Exports
// ---------------------------------------------------------------------------
module.exports = ;