]> zoso.dev Git - nano-pow.git/commitdiff
Intercept dummy https request to get a secure context and set up page content instead...
authorChris Duncan <chris@zoso.dev>
Tue, 22 Apr 2025 03:23:38 +0000 (20:23 -0700)
committerChris Duncan <chris@zoso.dev>
Tue, 22 Apr 2025 03:23:38 +0000 (20:23 -0700)
src/bin/server.ts

index 68514ac2e856f5cb2f4fc3234ebb34fc268b944b..b4f2a8d686b54bf6f00e4180c751ea32cba84ab0 100755 (executable)
@@ -4,7 +4,7 @@
 
 import { launch } from 'puppeteer'
 import { subtle } from 'node:crypto'
-import { readFile, unlink, writeFile } from 'node:fs/promises'
+import { readFile } from 'node:fs/promises'
 import * as http from 'node:http'
 import { AddressInfo, Socket } from 'node:net'
 import { homedir } from 'node:os'
@@ -242,35 +242,36 @@ const browser = await launch({
        headless: true,
        args: [
                '--headless=new',
-               '--use-angle=vulkan',
-               '--enable-features=Vulkan',
                '--disable-vulkan-surface',
-               '--enable-unsafe-webgpu'
+               '--enable-features=Vulkan,DefaultANGLEVulkan,VulkanFromANGLE',
+               '--enable-gpu',
+               '--enable-unsafe-webgpu',
+               '--disable-backgrounding-occluded-windows',
+               '--disable-background-timer-throttling',
+               '--disable-renderer-backgrounding'
        ]
 })
 const page = await browser.newPage()
-page.on('console', msg => log(msg.text()))
-
-const path: string = new URL(import.meta.url).pathname
-const dir = path.slice(0, path.lastIndexOf('/'))
-const filename = join(dir, `${process.pid}.html`)
-await writeFile(filename, '<!DOCTYPE html><link rel="icon" href="data:,">')
-await page.goto(import.meta.resolve(filename))
-await page.waitForFunction(async (): Promise<GPUAdapter | null> => {
-       return await navigator['gpu'].requestAdapter()
-})
 
-const src = `${NanoPow};window.NanoPow=NanoPow;`
+const src = `${NanoPow};window.NanoPow=NanoPowGpu;`
 const hash = await subtle.digest('SHA-256', Buffer.from(src))
 const enc = `sha256-${Buffer.from(hash).toString('base64')}`
-
-await page.setContent(`
+const body = `
        <!DOCTYPE html>
        <link rel="icon" href="data:,">
        <meta http-equiv="Content-Security-Policy" content="default-src 'none'; base-uri 'none'; form-action 'none'; script-src '${enc}';">
        <script type="module">${src}</script>
-`)
-await unlink(filename)
+`
+
+await page.setRequestInterception(true)
+page.on('request', async (req): Promise<void> => {
+       if (req.url() === 'https://nanopow.invalid/') {
+               req.respond({ status: 200, contentType: 'text/html', body })
+       }
+})
+page.on('console', msg => log(msg.text()))
+await page.goto('https://nanopow.invalid/')
+
 log('Puppeteer initialized')
 
 // Listen on configured port