askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
example.mjs · 1.8 KB · 24 lines
| 1 | // The example caller (examples/caller-network-client.mjs), run for real as a separate process against the browser. |
| 2 | import { execFile } from "node:child_process"; |
| 3 | import { fileURLToPath } from "node:url"; |
| 4 | |
| 5 | export default async function (ctx) { |
| 6 | const { check, base, controlUrl, controlToken } = ctx; |
| 7 | const client = fileURLToPath(new URL("../../examples/caller-network-client.mjs", import.meta.url)); |
| 8 | const UA = "ExampleCaller/1.0 (+https://caller.example/about)"; |
| 9 | const run = (extra) => new Promise((resolve) => { |
| 10 | execFile(process.execPath, [client, "--url", base + "/free", "--user-agent", UA, "--settle-timeout", "15000", ...extra], { env: { ...process.env, DBB_URL: controlUrl, DBB_TOKEN: controlToken }, timeout: 60000 }, (err, stdout, stderr) => { |
| 11 | let verdict = null; |
| 12 | try { verdict = JSON.parse(stdout); } catch {} |
| 13 | resolve({ code: err ? (err.code ?? 1) : 0, verdict, stderr }); |
| 14 | }); |
| 15 | }); |
| 16 | |
| 17 | let r = await run([]); |
| 18 | check(r.code === 3 && r.verdict?.failure?.kind === "blocked" && r.verdict?.failure?.source === "caller", "by default it refuses a loopback/private address, as the caller, with a reason", JSON.stringify(r.verdict?.failure)); |
| 19 | r = await run(["--allow-private", "--min-gap-ms", "0"]); |
| 20 | check(r.code === 0 && r.verdict?.settled && r.verdict?.outcome === "ok" && r.verdict?.status === 200, "with --allow-private it loads the page and exits 0", `${r.verdict?.outcome} ${r.verdict?.status}`); |
| 21 | r = await run(["--allow-private", "--min-gap-ms", "0", "--max-bytes", "100"]); |
| 22 | check(r.code === 3 && r.verdict?.failure?.source === "caller", "a response over the size limit is refused, not handed over", JSON.stringify(r.verdict?.failure)); |
| 23 | check((await ctx.call("net.provider")).enabled === false, "and it turns the caller network off again when done"); |
| 24 | } |