|
| 1 | +// runs tests/browser/page.mjs in Chromium (Playwright), bundled by Vite (which has to find the worker's module), on a |
| 2 | +// page with cross-origin isolation (threads: workers must start and write the same file) and one without (no |
| 3 | +// SharedArrayBuffer: the same file without threads). Needs the build and |
| 4 | +// npm install --no-save vite playwright && npx playwright install chromium |
| 5 | +// (CHROMIUM: a Chromium to use instead of Playwright's) |
| 6 | +import http from 'node:http'; |
| 7 | +import fs from 'node:fs'; |
| 8 | +import path from 'node:path'; |
| 9 | +import { fileURLToPath } from 'node:url'; |
| 10 | +import { build } from 'vite'; |
| 11 | +import { chromium } from 'playwright'; |
| 12 | + |
| 13 | +const here = path.dirname(fileURLToPath(import.meta.url)), out = path.join(here, 'dist'); |
| 14 | +fs.writeFileSync(path.join(here, 'index.html'), '<!doctype html><script type="module" src="./page.mjs"></script>\n'); |
| 15 | +try { |
| 16 | + await build({ |
| 17 | + root: here, logLevel: 'error', build: { outDir: out, emptyOutDir: true, target: 'es2022', chunkSizeWarningLimit: 1e9, assetsInlineLimit: 0 }, |
| 18 | + resolve: { alias: { imagescript: path.join(here, '../../dist/index.js') } }, |
| 19 | + assetsInclude: ['**/*.ttf'], |
| 20 | + plugins: [{ name: 'binary', enforce: 'pre', load: id => /\.(png|ttf)$/.test(id) ? `export default Uint8Array.from(atob('${fs.readFileSync(id).toString('base64')}'), c => c.charCodeAt(0));` : null }], |
| 21 | + }); |
| 22 | +} finally { |
| 23 | + fs.rmSync(path.join(here, 'index.html')); |
| 24 | +} |
| 25 | +if (!fs.readdirSync(path.join(out, 'assets')).some(f => f.startsWith('worker'))) throw new Error('Vite did not bundle the worker'); |
| 26 | + |
| 27 | +let failed = false; |
| 28 | +const browser = await chromium.launch(process.env.CHROMIUM ? { executablePath: process.env.CHROMIUM } : {}); |
| 29 | +try { |
| 30 | + for (const isolated of [true, false]) { |
| 31 | + const server = http.createServer((request, response) => { |
| 32 | + const file = path.join(out, request.url === '/' ? 'index.html' : decodeURIComponent(request.url)); |
| 33 | + if (!file.startsWith(out) || !fs.existsSync(file)) return response.writeHead(404).end(); |
| 34 | + response.writeHead(200, { |
| 35 | + 'Content-Type': file.endsWith('.js') ? 'text/javascript' : 'text/html', |
| 36 | + ...isolated && { 'Cross-Origin-Opener-Policy': 'same-origin', 'Cross-Origin-Embedder-Policy': 'require-corp' }, |
| 37 | + }); |
| 38 | + fs.createReadStream(file).pipe(response); |
| 39 | + }).listen(0); |
| 40 | + const page = await browser.newPage(); |
| 41 | + let workers = 0; |
| 42 | + page.on('worker', () => workers++); |
| 43 | + page.on('pageerror', e => console.log('page error:', e.message)); |
| 44 | + await page.goto(`http://localhost:${server.address().port}/`); |
| 45 | + await page.waitForFunction(() => globalThis.results, null, { timeout: 120000 }); |
| 46 | + const r = await page.evaluate(() => globalThis.results); |
| 47 | + await page.close(); |
| 48 | + server.close(); |
| 49 | + const problems = []; |
| 50 | + if (r.error) problems.push(r.error); |
| 51 | + if (r.isolated !== isolated) problems.push(`crossOriginIsolated ${r.isolated}`); |
| 52 | + if (r.single !== r.threaded) problems.push('threads wrote a different file'); |
| 53 | + if (isolated ? workers < 2 : workers) problems.push(`${workers} workers started`); |
| 54 | + for (const [step, result] of Object.entries(r.smoke ?? {})) if (String(result).startsWith('FAIL')) problems.push(`${step}: ${result}`); |
| 55 | + console.log(`${isolated ? 'cross-origin isolated' : 'not isolated'}: ${workers} workers, ${problems.length ? problems.join('\n ') : 'ok'}`); |
| 56 | + if (problems.length) failed = true; |
| 57 | + } |
| 58 | +} finally { |
| 59 | + await browser.close(); |
| 60 | + fs.rmSync(out, { recursive: true, force: true }); |
| 61 | +} |
| 62 | +if (failed) process.exit(1); |
| 63 | +console.log('browser passed'); |
0 commit comments