Imports the previously untracked working tree as-is so that the review changes that follow (requirements -> design -> implementation) can be reviewed as diffs and reverted if needed. No content changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
35 lines
3.2 KiB
JavaScript
35 lines
3.2 KiB
JavaScript
import http from 'node:http';
|
|
import { timingSafeEqual } from 'node:crypto';
|
|
import { readConfig, provisionRun } from './provisioner.mjs';
|
|
import { provisionWorkspace } from './workspaces.mjs';
|
|
import { releaseRun } from './run-release.mjs';
|
|
import {observeWorkspaceLifecycle,reconcileWorkspaceLifecycle} from './workspace-lifecycle.mjs';
|
|
import { createRunOperationQueue } from './run-operations.mjs';
|
|
|
|
const config = readConfig();
|
|
const expected = process.env.WF_PROVISIONER_TOKEN;
|
|
const runOperation = createRunOperationQueue();
|
|
if (!expected || expected.length < 32) throw new Error('unconfigured: scoped WF_PROVISIONER_TOKEN must be at least 32 characters');
|
|
const server = http.createServer(async (req, res) => {
|
|
res.setHeader('content-type', 'application/json'); res.setHeader('cache-control', 'no-store');
|
|
try {
|
|
const token = req.headers.authorization?.replace(/^Bearer /, '') ?? '';
|
|
if (token.length !== expected.length || !timingSafeEqual(Buffer.from(token), Buffer.from(expected))) { res.writeHead(401); res.end(JSON.stringify({ error: 'UNAUTHORIZED' })); return; }
|
|
if (req.url === '/healthz' && req.method === 'GET') { res.end(JSON.stringify({ status: 'configured', tenantId: config.tenantId })); return; }
|
|
const target=new URL(req.url,'http://provisioner.local');
|
|
const lifecycle=/^\/workspaces\/([a-zA-Z0-9_-]{1,128})\/lifecycle$/.exec(target.pathname);
|
|
if(lifecycle&&req.method==='GET'){const input={tenantId:target.searchParams.get('tenantId'),projectId:target.searchParams.get('projectId'),workspaceId:lifecycle[1]};res.end(JSON.stringify(await observeWorkspaceLifecycle(config,input)));return;}
|
|
if(lifecycle&&req.method==='PUT'){let body='';for await(const part of req){body+=part;if(body.length>4000)throw new Error('INVALID_REQUEST');}const input=JSON.parse(body);if(input.workspaceId!==lifecycle[1])throw new Error('INVALID_WORKSPACE_SCOPE');const state=await reconcileWorkspaceLifecycle(config,input);res.writeHead(202);res.end(JSON.stringify(state));return;}
|
|
if (!['/runs', '/runs/release', '/workspaces'].includes(req.url) || req.method !== 'POST') { res.writeHead(404); res.end(JSON.stringify({ error: 'NOT_FOUND' })); return; }
|
|
let body = ''; for await (const part of req) { body += part; if (body.length > 40000) throw new Error('INVALID_REQUEST'); }
|
|
const input = JSON.parse(body);
|
|
const result = req.url === '/workspaces' ? await provisionWorkspace(config, input) : await runOperation(input.tenantId, input.runId, () => req.url === '/runs/release' ? releaseRun(config, input) : provisionRun(config, input));
|
|
res.writeHead(202); res.end(JSON.stringify(result));
|
|
} catch (error) {
|
|
const infrastructure = error.message.startsWith('KUBERNETES_');
|
|
res.writeHead(infrastructure ? 502 : 400);
|
|
res.end(JSON.stringify({ error: infrastructure || ['RESOURCE_GRANT_INVALID','RESOURCE_GRANT_EXPIRED','RESOURCE_LIMIT','RUNTIME_IMAGE_MIGRATION_REQUIRED'].includes(error.message) ? error.message : 'INVALID_RUN_REQUEST' }));
|
|
}
|
|
});
|
|
server.listen(Number(process.env.WF_PROVISIONER_PORT ?? 4450), process.env.WF_PROVISIONER_HOST ?? '127.0.0.1', () => console.log('Run provisioner configured; no workloads are launched until an authenticated request arrives.'));
|