|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Bytedance, Inc. and its affiliates. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +import puppeteer from 'puppeteer-core'; |
| 6 | +import { createServer } from 'http'; |
| 7 | +import { readFile, stat } from 'fs/promises'; |
| 8 | +import { URL, fileURLToPath } from 'url'; |
| 9 | +import { join, dirname } from 'path'; |
| 10 | + |
| 11 | +async function main() { |
| 12 | + console.log('🚀 launch ...'); |
| 13 | + |
| 14 | + const browser = await puppeteer.launch({ |
| 15 | + executablePath: |
| 16 | + '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', |
| 17 | + headless: false, |
| 18 | + defaultViewport: { |
| 19 | + width: 900, |
| 20 | + height: 900, |
| 21 | + deviceScaleFactor: 0, |
| 22 | + }, |
| 23 | + args: [ |
| 24 | + '--mute-audio', |
| 25 | + '--no-default-browser-check', |
| 26 | + '--window-size=900,990', |
| 27 | + '--remote-allow-origins=http://127.0.0.1:3000', |
| 28 | + 'https://www.bytedance.com/en/', |
| 29 | + ], |
| 30 | + ignoreDefaultArgs: ['--enable-automation'], |
| 31 | + }); |
| 32 | + |
| 33 | + const wsEndpoint = browser.wsEndpoint(); |
| 34 | + |
| 35 | + console.log('✅ launched successfully!'); |
| 36 | + console.log('📡 CDP WebSocket:', wsEndpoint); |
| 37 | + |
| 38 | + const port = 3000; |
| 39 | + const server = createServer(async (req, res) => { |
| 40 | + try { |
| 41 | + console.log(`Request: ${req.method} ${req.url}`); |
| 42 | + |
| 43 | + if (req.url === '/' || req.url === '/index.html') { |
| 44 | + const indexPath = new URL('./index.html', import.meta.url).pathname; |
| 45 | + console.log(`Reading file: ${indexPath}`); |
| 46 | + |
| 47 | + let html = await readFile(indexPath, 'utf-8'); |
| 48 | + console.log('File read successfully'); |
| 49 | + |
| 50 | + // Replace the import.meta.WSEndpoint with the actual wsEndpoint |
| 51 | + html = html.replace( |
| 52 | + 'import.meta.WSEndpoint', |
| 53 | + JSON.stringify(wsEndpoint), |
| 54 | + ); |
| 55 | + |
| 56 | + console.log('HTML length:', html.length); |
| 57 | + |
| 58 | + res.writeHead(200, { 'Content-Type': 'text/html' }); |
| 59 | + res.end(html); |
| 60 | + } else if (req.url?.startsWith('/dist/')) { |
| 61 | + // Handle static files from /dist/ directory |
| 62 | + const __filename = fileURLToPath(import.meta.url); |
| 63 | + const __dirname = dirname(__filename); |
| 64 | + const staticPath = join( |
| 65 | + __dirname, |
| 66 | + '..', |
| 67 | + '..', |
| 68 | + 'dist', |
| 69 | + req.url.replace('/dist/', ''), |
| 70 | + ); |
| 71 | + |
| 72 | + console.log(`Static file request: ${staticPath}`); |
| 73 | + |
| 74 | + try { |
| 75 | + const fileStat = await stat(staticPath); |
| 76 | + if (fileStat.isFile()) { |
| 77 | + const content = await readFile(staticPath); |
| 78 | + res.writeHead(200, { 'Content-Type': 'application/javascript' }); |
| 79 | + res.end(content); |
| 80 | + return; |
| 81 | + } |
| 82 | + } catch (staticError) { |
| 83 | + console.error('Static file not found:', staticError); |
| 84 | + } |
| 85 | + |
| 86 | + res.writeHead(404, { 'Content-Type': 'text/plain' }); |
| 87 | + res.end('Static file not found'); |
| 88 | + } else { |
| 89 | + console.log('404 for:', req.url); |
| 90 | + res.writeHead(404, { 'Content-Type': 'text/plain' }); |
| 91 | + res.end('Not Found'); |
| 92 | + } |
| 93 | + } catch (error) { |
| 94 | + console.error('Server error:', error); |
| 95 | + res.writeHead(500, { 'Content-Type': 'text/plain' }); |
| 96 | + res.end(`Internal Server Error: ${(error as Error).message}`); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + server.listen(port, () => { |
| 101 | + console.log(`🌐 Server running at http://localhost:${port}`); |
| 102 | + console.log('🔄 Ctrl+C to exit...\n'); |
| 103 | + }); |
| 104 | + |
| 105 | + const cleanup = async () => { |
| 106 | + try { |
| 107 | + console.log('\n🛑 closing server and browser...'); |
| 108 | + if (server) { |
| 109 | + server.close(); |
| 110 | + } |
| 111 | + if (browser) { |
| 112 | + await browser.close(); |
| 113 | + } |
| 114 | + } catch (e) { |
| 115 | + // ignore |
| 116 | + } finally { |
| 117 | + process.exit(0); |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + process.on('SIGINT', cleanup); |
| 122 | + process.on('SIGTERM', cleanup); |
| 123 | +} |
| 124 | + |
| 125 | +main().catch(console.error); |
0 commit comments