122 lines
5.2 KiB
JavaScript
122 lines
5.2 KiB
JavaScript
// A stand-in for the ConusAI platform's routes the showcase calls (plan S4.8), so the
|
|
// end-to-end run needs no platform. It keeps what the app sends in memory, reads it
|
|
// back the way the platform does, and exposes that state under /__e2e/state for the
|
|
// tests. It is not a platform: no tenancy, no auth beyond the bearer header's presence.
|
|
import { createServer } from 'node:http'
|
|
import { gunzipSync } from 'node:zlib'
|
|
|
|
const PORT = 4010
|
|
const state = { audit: [], usage: [], blobs: [], otel: { logs: [], traces: [] } }
|
|
const catalog = {
|
|
kv: { display_name: 'KV Store', description: 'Counts uploads per visitor each minute.' },
|
|
nats: { display_name: 'NATS', description: 'Announces new notes live to subscribers.' },
|
|
qdrant: { display_name: 'Qdrant', description: 'Finds similar notes by vector search.' },
|
|
git: { display_name: 'Git & Packages', description: 'Installs packages from the platform registry.' },
|
|
s3: { display_name: 'S3', description: 'Exports your notes to an S3 bucket.' },
|
|
}
|
|
|
|
function send(res, status, body) {
|
|
res.writeHead(status, { 'content-type': 'application/json' })
|
|
res.end(JSON.stringify(body))
|
|
}
|
|
|
|
async function readBody(req) {
|
|
const chunks = []
|
|
for await (const chunk of req) chunks.push(chunk)
|
|
const raw = Buffer.concat(chunks)
|
|
return req.headers['content-encoding'] === 'gzip' ? gunzipSync(raw) : raw
|
|
}
|
|
|
|
const server = createServer(async (req, res) => {
|
|
const url = new URL(req.url ?? '/', `http://127.0.0.1:${PORT}`)
|
|
// CONUSAI_API_URL is the platform base ending in /api, and the SDKs append only their
|
|
// route. Strip exactly one /api: a request for /api/api/... then matches no route.
|
|
const path = url.pathname.startsWith('/api/') ? url.pathname.slice(4) : url.pathname
|
|
const method = req.method ?? 'GET'
|
|
|
|
if (method === 'GET' && path === '/__e2e/health') return send(res, 200, { ok: true })
|
|
if (method === 'GET' && path === '/__e2e/state') return send(res, 200, state)
|
|
|
|
if (method === 'GET' && path === '/.well-known/conusai.json') {
|
|
return send(res, 200, { name: 'ConusAI e2e platform', version: 'e2e' })
|
|
}
|
|
|
|
const service = path.match(/^\/v1\/service-types\/([a-z0-9_-]+)$/)
|
|
if (method === 'GET' && service) {
|
|
const entry = catalog[service[1]]
|
|
if (!entry) return send(res, 404, { title: 'unknown service type' })
|
|
return send(res, 200, { service_type: service[1], category: 'e2e', color: '#000000', capabilities: {}, ...entry })
|
|
}
|
|
|
|
if (method === 'POST' && path === '/_conusai/audit') {
|
|
const body = JSON.parse((await readBody(req)).toString('utf8'))
|
|
for (const event of body.events) {
|
|
state.audit.push({
|
|
id: state.audit.length + 1,
|
|
action: event.action,
|
|
outcome: event.outcome ?? 'success',
|
|
occurred_at: event.occurred_at ?? new Date().toISOString(),
|
|
resource_type: event.target?.type ?? null,
|
|
resource_id: event.target?.id ?? null,
|
|
})
|
|
}
|
|
return send(res, 202, { accepted: body.events.length, duplicates: 0, redacted: [] })
|
|
}
|
|
|
|
if (method === 'GET' && path === '/_conusai/audit/events') {
|
|
const items = [...state.audit].reverse()
|
|
return send(res, 200, { items, next_cursor: null, has_more: false })
|
|
}
|
|
|
|
// @conusai-sdk/observe-node's BatchExporter (showcase D23): POSTs OTLP/HTTP JSON directly to
|
|
// these `{CONUSAI_API_URL}/otel/v1/{signal}` routes, bearer-authenticated with CONUSAI_API_TOKEN
|
|
// -- not the OTEL_EXPORTER_OTLP_* env vars, which the SDK never reads itself.
|
|
if (method === 'POST' && (path === '/otel/v1/logs' || path === '/otel/v1/traces')) {
|
|
const body = JSON.parse((await readBody(req)).toString('utf8'))
|
|
const bucket = path.endsWith('/logs') ? state.otel.logs : state.otel.traces
|
|
bucket.push({ authorization: req.headers['authorization'] ?? null, body })
|
|
return send(res, 200, {})
|
|
}
|
|
|
|
if (method === 'POST' && path === '/_conusai/usage/events') {
|
|
const body = JSON.parse((await readBody(req)).toString('utf8'))
|
|
state.usage.push(...body.events)
|
|
return send(res, 202, { accepted: body.events.length, duplicates: 0, rejected: [] })
|
|
}
|
|
|
|
if (method === 'POST' && path === '/blob') {
|
|
const bytes = await readBody(req)
|
|
const pathname = url.searchParams.get('pathname') ?? 'blob'
|
|
const info = {
|
|
url: `http://127.0.0.1:${PORT}/api/blob/${pathname}`,
|
|
pathname,
|
|
contentType: req.headers['content-type'] ?? 'application/octet-stream',
|
|
size: bytes.length,
|
|
uploadedAt: new Date().toISOString(),
|
|
bytes,
|
|
}
|
|
state.blobs.push(info)
|
|
return send(res, 200, {
|
|
url: info.url,
|
|
pathname: info.pathname,
|
|
contentType: info.contentType,
|
|
size: info.size,
|
|
uploadedAt: info.uploadedAt,
|
|
})
|
|
}
|
|
|
|
// GET /blob/{pathname}, and the real API's /blob/{project_id}/{pathname} shape.
|
|
if (method === 'GET' && path.startsWith('/blob/')) {
|
|
const info = state.blobs.find((blob) => path === `/blob/${blob.pathname}` || path.endsWith(`/${blob.pathname}`))
|
|
if (!info) return send(res, 404, { title: 'not found', path })
|
|
res.writeHead(200, { 'content-type': info.contentType })
|
|
return res.end(info.bytes)
|
|
}
|
|
|
|
return send(res, 404, { title: 'not found', path })
|
|
})
|
|
|
|
server.listen(PORT, '127.0.0.1', () => {
|
|
console.log(`fake platform listening on ${PORT}`)
|
|
})
|