const { parentPort } = require('node:worker_threads') const playwright = require('playwright') const runUserScript = async (code) => { const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor const userFunc = new AsyncFunction( 'console', 'playwright', code ) return await userFunc( console, playwright ) } parentPort.on('message', async (code) => { try { const result = await runUserScript(code) parentPort.postMessage({ result }) } catch (e) { parentPort.postMessage({ error: e }) } })