Write an HTTP execution client
Run this code in a local Node.js client with Node 22 or later. It uses the host’s fetch, streams and Node modules. Create the demo disk using the quickstart and replace the project key placeholder.
Save the following as client.mjs:
import {once} from 'node:events';
const key = 'YOUR_PROJECT_API_KEY';
async function* records(response) { const decoder = new TextDecoder(); let pending = ''; for await (const chunk of response.body) { pending += decoder.decode(chunk, {stream:true}); let end; while ((end = pending.indexOf('\n')) !== -1) { const line = pending.slice(0, end); pending = pending.slice(end + 1); if (line.trim()) yield JSON.parse(line); } } pending += decoder.decode(); if (pending.trim()) throw Error('Incomplete NDJSON record');}
async function write(stream, bytes) { if (!stream.write(bytes)) await once(stream, 'drain');}
async function execute(script, kind = 'bash') { const response = await fetch(`https://api.wabe.sh/v1/disks/demo/exec?kind=${kind}`, { method:'POST', headers:{Authorization:`Bearer ${key}`, 'Content-Type':'text/plain'}, body:script, }); if (!response.ok) throw Error(`${response.status}: ${await response.text()}`); let exitCode, result; for await (const event of records(response)) { if (event.type === 'stdout' || event.type === 'stderr') { const stream = event.type === 'stdout' ? process.stdout : process.stderr; await write(stream, Buffer.from(event.data, 'base64')); } else if (event.type === 'result') { result = event.value; } else if (event.type === 'exit') { exitCode = event.code; } } if (exitCode === undefined) throw Error('Missing terminal exit record'); if (exitCode !== 0) throw Error(`Remote execution exited ${exitCode}`); return result;}
await execute("printf 'Hello from wabe Disk\\n'");const result = await execute("return {answer:42};", 'js');console.log(result);node client.mjsThe client checks HTTP status, awaits local stdout backpressure, preserves bytes and rejects an incomplete or failed execution. Keep output streamed when the response can be large. Add client deadlines and application-specific retry handling according to durability and errors.