|
| 1 | +#!/usr/bin/env bun |
| 2 | +// One-off server for the §11 iOS crash-rescue experiment (not part of the |
| 3 | +// regular e2e suites). Update chain: no hash → brick; brick → fix. The /state |
| 4 | +// endpoint reports what was served, so the driver can assert the sequence. |
| 5 | + |
| 6 | +import * as fs from 'node:fs'; |
| 7 | +import * as path from 'node:path'; |
| 8 | +import { fileURLToPath } from 'node:url'; |
| 9 | + |
| 10 | +declare const Bun: any; |
| 11 | + |
| 12 | +const moduleDir = path.dirname(fileURLToPath(import.meta.url)); |
| 13 | +const artifactsDir = path.resolve(moduleDir, '../.e2e-artifacts/ios-rescue'); |
| 14 | +// The app hardcodes this port (localUpdateConfig LOCAL_UPDATE_PORT). |
| 15 | +const port = 31337; |
| 16 | + |
| 17 | +const BRICK_HASH = 'e2e-rescue-brick'; |
| 18 | +const FIX_HASH = 'e2e-rescue-fix'; |
| 19 | + |
| 20 | +const served: string[] = []; |
| 21 | + |
| 22 | +function json(body: unknown, status = 200) { |
| 23 | + return new Response(JSON.stringify(body), { |
| 24 | + status, |
| 25 | + headers: { |
| 26 | + 'Content-Type': 'application/json; charset=utf-8', |
| 27 | + 'Cache-Control': 'no-store', |
| 28 | + }, |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +const server = Bun.serve({ |
| 33 | + port, |
| 34 | + hostname: '0.0.0.0', |
| 35 | + async fetch(request: Request) { |
| 36 | + const url = new URL(request.url); |
| 37 | + |
| 38 | + if (url.pathname === '/state') { |
| 39 | + return json({ served }); |
| 40 | + } |
| 41 | + |
| 42 | + if (url.pathname.startsWith('/checkUpdate/')) { |
| 43 | + const payload = (await request.json().catch(() => ({}))) as { |
| 44 | + hash?: unknown; |
| 45 | + }; |
| 46 | + const currentHash = typeof payload.hash === 'string' ? payload.hash : ''; |
| 47 | + const assetBasePath = `${url.origin}/artifacts`; |
| 48 | + let response: Record<string, unknown>; |
| 49 | + if (!currentHash) { |
| 50 | + response = { |
| 51 | + update: true, |
| 52 | + name: 'rescue-brick', |
| 53 | + hash: BRICK_HASH, |
| 54 | + description: 'brick: crashes on launch after arming', |
| 55 | + paths: [assetBasePath], |
| 56 | + full: 'brick.ppk', |
| 57 | + // Deliver the brick via forceBoot (the app runs checkStrategy:null / |
| 58 | + // afterDownload:none, so nothing else would activate it). The FIX |
| 59 | + // response deliberately has no forceBoot: its activation must come |
| 60 | + // from the crash-rescue window itself. |
| 61 | + config: { forceBoot: true }, |
| 62 | + }; |
| 63 | + } else if (currentHash === BRICK_HASH) { |
| 64 | + response = { |
| 65 | + update: true, |
| 66 | + name: 'rescue-fix', |
| 67 | + hash: FIX_HASH, |
| 68 | + description: 'fix: disarms the brick flag', |
| 69 | + paths: [assetBasePath], |
| 70 | + full: 'fix.ppk', |
| 71 | + }; |
| 72 | + } else { |
| 73 | + response = { upToDate: true }; |
| 74 | + } |
| 75 | + served.push(`${currentHash || '(base)'} -> ${JSON.stringify(response.hash ?? 'upToDate')}`); |
| 76 | + console.log(`[checkUpdate] hash=${currentHash || '(base)'} ->`, response.hash ?? 'upToDate'); |
| 77 | + return json(response); |
| 78 | + } |
| 79 | + |
| 80 | + if (url.pathname.startsWith('/artifacts/')) { |
| 81 | + const name = path.basename(url.pathname); |
| 82 | + const filePath = path.join(artifactsDir, name); |
| 83 | + if (!fs.existsSync(filePath)) { |
| 84 | + return new Response('not found', { status: 404 }); |
| 85 | + } |
| 86 | + console.log(`[artifact] ${name}`); |
| 87 | + const file = Bun.file(filePath); |
| 88 | + return new Response(request.method === 'HEAD' ? null : file, { |
| 89 | + headers: { |
| 90 | + 'Content-Type': 'application/octet-stream', |
| 91 | + 'Content-Length': String(file.size), |
| 92 | + 'Cache-Control': 'no-store', |
| 93 | + }, |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + return new Response('not found', { status: 404 }); |
| 98 | + }, |
| 99 | +}); |
| 100 | + |
| 101 | +console.log(`ios rescue server listening on ${server.hostname}:${server.port}`); |
0 commit comments