|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import type { BrowserContext, Route } from '@playwright/test'; |
| 4 | + |
| 5 | +const frontendRoot = path.resolve(__dirname, '../../..'); |
| 6 | + |
| 7 | +let dracoPackageDir: string; |
| 8 | + |
| 9 | +try { |
| 10 | + dracoPackageDir = path.dirname(require.resolve('draco3d/package.json', { paths: [frontendRoot] })); |
| 11 | +} catch (error) { |
| 12 | + throw new Error( |
| 13 | + 'Unable to locate the draco3d package. Make sure frontend dependencies are installed before running Playwright tests.', |
| 14 | + ); |
| 15 | +} |
| 16 | + |
| 17 | +const decoderBaseUrl = 'https://www.gstatic.com/draco/v1/decoders/'; |
| 18 | + |
| 19 | +const wasmWrapperSource = fs.readFileSync( |
| 20 | + path.join(dracoPackageDir, 'draco_wasm_wrapper.js'), |
| 21 | + 'utf8', |
| 22 | +); |
| 23 | +const wasmBinary = fs.readFileSync(path.join(dracoPackageDir, 'draco_decoder.wasm')); |
| 24 | +const decoderSource = fs.readFileSync(path.join(dracoPackageDir, 'draco_decoder.js'), 'utf8'); |
| 25 | + |
| 26 | +const patchedContexts = new WeakSet<BrowserContext>(); |
| 27 | + |
| 28 | +const fulfill = (route: Route, body: string | Buffer, contentType: string) => |
| 29 | + route.fulfill({ status: 200, body, contentType, headers: { 'cache-control': 'public, max-age=86400' } }); |
| 30 | + |
| 31 | +export async function registerDracoRoutes(context: BrowserContext): Promise<void> { |
| 32 | + if (patchedContexts.has(context)) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + patchedContexts.add(context); |
| 37 | + |
| 38 | + await Promise.all([ |
| 39 | + context.route(`${decoderBaseUrl}draco_wasm_wrapper.js`, (route) => |
| 40 | + fulfill(route, wasmWrapperSource, 'application/javascript'), |
| 41 | + ), |
| 42 | + context.route(`${decoderBaseUrl}draco_decoder.js`, (route) => |
| 43 | + fulfill(route, decoderSource, 'application/javascript'), |
| 44 | + ), |
| 45 | + context.route(`${decoderBaseUrl}draco_decoder.wasm`, (route) => |
| 46 | + fulfill(route, wasmBinary, 'application/wasm'), |
| 47 | + ), |
| 48 | + ]); |
| 49 | +} |
0 commit comments