Datasets:
Download tool/lib/browser.mjs from codepawl/asset-index: direct link, hf CLI and curl.
- Browser
- Download file 4.23 kB
-
https://huggingface.co/datasets/codepawl/asset-index/resolve/main/tool/lib/browser.mjs
- Command line
-
hf download hf://datasets/codepawl/asset-index/tool/lib/browser.mjs
-
curl -L -o browser.mjs https://huggingface.co/datasets/codepawl/asset-index/resolve/main/tool/lib/browser.mjs
4.23 kB
| // Headless Chrome on the GPU (ANGLE D3D11 + WebGPU) with a static server for the probe pages, the pack and the engines. | |
| // Callers run through lib/common.mjs runOnGpu (optional GPU_LOCK queue). | |
| import fs from 'node:fs'; | |
| import http from 'node:http'; | |
| import path from 'node:path'; | |
| import { createRequire } from 'node:module'; | |
| import { ENGINE_ROOT, CHROME_PATH, TOOLS_FOLDER } from './common.mjs'; | |
| const CONTENT_TYPES = { | |
| '.html': 'text/html; charset=utf-8', | |
| '.js': 'text/javascript; charset=utf-8', | |
| '.mjs': 'text/javascript; charset=utf-8', | |
| '.json': 'application/json', | |
| '.jsonl': 'application/json', | |
| '.wasm': 'application/wasm', | |
| '.glb': 'model/gltf-binary', | |
| '.png': 'image/png', | |
| '.webp': 'image/webp', | |
| '.jpg': 'image/jpeg', | |
| '.ktx2': 'image/ktx2', | |
| }; | |
| // Serves each mount ("/prefix" -> folder), longest prefix first, with range support. Resolves with the base URL. | |
| export function startServer(mounts) { | |
| const table = Object.entries(mounts) | |
| .map(([prefix, folder]) => ({ prefix, folder: path.resolve(folder) })) | |
| .sort((first, second) => second.prefix.length - first.prefix.length); | |
| const server = http.createServer((request, response) => { | |
| const urlPath = decodeURIComponent(new URL(request.url, 'http://local').pathname); | |
| const mount = table.find(({ prefix }) => urlPath === prefix || urlPath.startsWith(prefix === '/' ? '/' : `${prefix}/`)); | |
| const filePath = mount ? path.resolve(mount.folder, `.${urlPath.slice(mount.prefix === '/' ? 0 : mount.prefix.length)}`) : null; | |
| if (!filePath || !filePath.startsWith(mount.folder) || !fs.existsSync(filePath) || fs.statSync(filePath).isDirectory()) { | |
| response.writeHead(404); | |
| response.end('not found'); | |
| return; | |
| } | |
| const size = fs.statSync(filePath).size; | |
| const headers = { | |
| 'Content-Type': CONTENT_TYPES[path.extname(filePath).toLowerCase()] ?? 'application/octet-stream', | |
| 'Accept-Ranges': 'bytes', | |
| 'Cache-Control': 'no-store', | |
| 'Access-Control-Allow-Origin': '*', | |
| }; | |
| const range = request.headers.range?.match(/bytes=(\d+)-(\d*)/); | |
| if (range) { | |
| const start = Number(range[1]); | |
| const end = range[2] ? Number(range[2]) : size - 1; | |
| response.writeHead(206, { ...headers, 'Content-Range': `bytes ${start}-${end}/${size}`, 'Content-Length': end - start + 1 }); | |
| fs.createReadStream(filePath, { start, end }).pipe(response); | |
| return; | |
| } | |
| response.writeHead(200, { ...headers, 'Content-Length': size }); | |
| fs.createReadStream(filePath).pipe(response); | |
| }); | |
| return new Promise((resolve) => { | |
| server.listen(0, '127.0.0.1', () => resolve({ server, base: `http://127.0.0.1:${server.address().port}` })); | |
| }); | |
| } | |
| export function standardMounts(packFolder) { | |
| return { | |
| '/': path.join(TOOLS_FOLDER, 'pages'), | |
| '/three': path.join(ENGINE_ROOT, 'node_modules', 'three'), | |
| '/67js': path.join(ENGINE_ROOT, 'build'), | |
| '/pack': packFolder, | |
| }; | |
| } | |
| export async function launchChrome() { | |
| const { chromium } = createRequire(`${ENGINE_ROOT}/package.json`)('playwright'); | |
| return chromium.launch({ | |
| executablePath: CHROME_PATH, | |
| headless: true, | |
| args: ['--use-angle=d3d11', '--enable-gpu', '--ignore-gpu-blocklist', '--enable-unsafe-webgpu', '--enable-features=WebGPU', '--disable-renderer-backgrounding'], | |
| }); | |
| } | |
| // Opens a probe page and waits until it says it is ready. Console errors are collected on page.problems. | |
| export async function openProbe(context, url) { | |
| const page = await context.newPage(); | |
| page.problems = []; | |
| page.on('pageerror', (error) => page.problems.push(error.message)); | |
| page.on('console', (message) => message.type() === 'error' && page.problems.push(message.text())); | |
| await page.goto(url); | |
| await page.waitForFunction(() => window.probeReady || window.probeError, null, { timeout: 120000 }); | |
| const error = await page.evaluate(() => window.probeError); | |
| if (error) throw new Error(`${url}: ${error}`); | |
| return page; | |
| } | |
| export function engineVersions() { | |
| const read = (file) => JSON.parse(fs.readFileSync(file, 'utf8')).version; | |
| return { | |
| three: read(path.join(ENGINE_ROOT, 'node_modules', 'three', 'package.json')), | |
| js67: read(path.join(ENGINE_ROOT, 'package.json')), | |
| }; | |
| } | |