Add a packaged-client runtime boundary so the shared UI can talk to local, desktop, remote, and VS Code runtimes through the right transport instead of assuming one same-origin web server. Centralize OpenChamber-owned API access behind RuntimeAPIs, runtimeFetch, and runtime URL helpers, while keeping official OpenCode traffic on the SDK path. Support runtime switching, remote host selection, desktop client credentials, and headless connection links for pairing packaged clients with remote OpenChamber servers. Harden the new auth model by moving long-lived client tokens out of browser URLs, introducing short-lived scoped URL tokens for browser-owned transports, restricting URL-token access to explicit readable/realtime routes, and making client-token management session-scoped or self-scoped as appropriate. Update browser-owned assets and preview proxy flows to work with the split runtime model, including authenticated project icons, preview token propagation, CSP-safe preview bridge injection, and preview proxy auth that survives short-lived URL-token expiry. Tighten Electron security boundaries for packaged clients by gating privileged preload state to trusted origins and requiring explicit confirmation before connect deep-links import or switch remote runtimes. Also refresh agent guidance and project skills so future runtime/API, auth, preview, UI, CLI, settings, locale, and drag-to-reorder work follows the new architecture.
285 lines
9.6 KiB
JavaScript
285 lines
9.6 KiB
JavaScript
import { afterEach, describe, expect, it } from 'vitest';
|
|
import { EventEmitter } from 'node:events';
|
|
import express from 'express';
|
|
import path from 'path';
|
|
|
|
import { createSseBoundaryTracker, registerOpenCodeProxy, writeSseChunkWithBackpressure } from './lib/opencode/proxy.js';
|
|
|
|
const listen = (app, host = '127.0.0.1') => new Promise((resolve, reject) => {
|
|
const server = app.listen(0, host, () => resolve(server));
|
|
server.once('error', reject);
|
|
});
|
|
|
|
const closeServer = (server) => new Promise((resolve, reject) => {
|
|
if (!server) {
|
|
resolve();
|
|
return;
|
|
}
|
|
server.close((error) => {
|
|
if (error) {
|
|
reject(error);
|
|
return;
|
|
}
|
|
resolve();
|
|
});
|
|
});
|
|
|
|
describe('OpenCode proxy SSE forwarding', () => {
|
|
let upstreamServer;
|
|
let proxyServer;
|
|
|
|
afterEach(async () => {
|
|
await closeServer(proxyServer);
|
|
await closeServer(upstreamServer);
|
|
proxyServer = undefined;
|
|
upstreamServer = undefined;
|
|
});
|
|
|
|
it('forwards event streams with nginx-safe headers', async () => {
|
|
let seenAuthorization = null;
|
|
|
|
const upstream = express();
|
|
upstream.get('/global/event', (req, res) => {
|
|
seenAuthorization = req.headers.authorization ?? null;
|
|
res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
|
|
res.setHeader('Cache-Control', 'private, max-age=0');
|
|
res.setHeader('X-Upstream-Test', 'ok');
|
|
res.write('data: {"ok":true}\n\n');
|
|
res.end();
|
|
});
|
|
upstreamServer = await listen(upstream);
|
|
const upstreamPort = upstreamServer.address().port;
|
|
|
|
const app = express();
|
|
registerOpenCodeProxy(app, {
|
|
fs: {},
|
|
os: {},
|
|
path,
|
|
OPEN_CODE_READY_GRACE_MS: 0,
|
|
getRuntime: () => ({
|
|
openCodePort: upstreamPort,
|
|
isOpenCodeReady: true,
|
|
openCodeNotReadySince: 0,
|
|
isRestartingOpenCode: false,
|
|
}),
|
|
getOpenCodeAuthHeaders: () => ({ Authorization: 'Bearer test-token' }),
|
|
buildOpenCodeUrl: (requestPath) => `http://127.0.0.1:${upstreamPort}${requestPath}`,
|
|
ensureOpenCodeApiPrefix: () => {},
|
|
});
|
|
proxyServer = await listen(app);
|
|
const proxyPort = proxyServer.address().port;
|
|
|
|
const response = await fetch(`http://127.0.0.1:${proxyPort}/api/global/event`, {
|
|
headers: { Accept: 'text/event-stream' },
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.headers.get('content-type')).toContain('text/event-stream');
|
|
expect(response.headers.get('cache-control')).toBe('no-cache');
|
|
expect(response.headers.get('x-accel-buffering')).toBe('no');
|
|
expect(response.headers.get('x-upstream-test')).toBe('ok');
|
|
expect(await response.text()).toBe('data: {"ok":true}\n\n');
|
|
expect(seenAuthorization).toBe('Bearer test-token');
|
|
});
|
|
|
|
it('waits for drain when writing to a slow SSE response', async () => {
|
|
const writes = [];
|
|
const res = new EventEmitter();
|
|
res.writableEnded = false;
|
|
res.destroyed = false;
|
|
res.write = (value) => {
|
|
writes.push(value);
|
|
return false;
|
|
};
|
|
const controller = new AbortController();
|
|
|
|
const write = writeSseChunkWithBackpressure(res, Buffer.from('data: {"ok":true}\n\n'), controller.signal);
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
expect(writes).toHaveLength(1);
|
|
|
|
res.emit('drain');
|
|
|
|
await expect(write).resolves.toBe(true);
|
|
});
|
|
|
|
it('tracks whether a raw SSE stream is between event blocks', () => {
|
|
const tracker = createSseBoundaryTracker();
|
|
|
|
expect(tracker.isAtBoundary()).toBe(true);
|
|
expect(tracker.observe(Buffer.from('id: evt-1\n'))).toBe(false);
|
|
expect(tracker.observe(Buffer.from('data: {"ok"'))).toBe(false);
|
|
expect(tracker.observe(Buffer.from(':true}\n'))).toBe(false);
|
|
expect(tracker.observe(Buffer.from('\n'))).toBe(true);
|
|
expect(tracker.observe(Buffer.from('data: next\r\n\r\n'))).toBe(true);
|
|
});
|
|
|
|
it('routes generic API requests through external OpenCode base URL', async () => {
|
|
const upstream = express();
|
|
upstream.get('/config/providers', (_req, res) => {
|
|
res.json({ ok: true, source: 'external-host' });
|
|
});
|
|
upstreamServer = await listen(upstream);
|
|
const upstreamPort = upstreamServer.address().port;
|
|
const externalBaseUrl = `http://127.0.0.1:${upstreamPort}`;
|
|
|
|
const app = express();
|
|
registerOpenCodeProxy(app, {
|
|
fs: {},
|
|
os: {},
|
|
path,
|
|
OPEN_CODE_READY_GRACE_MS: 0,
|
|
getRuntime: () => ({
|
|
openCodePort: 3902,
|
|
openCodeBaseUrl: externalBaseUrl,
|
|
isOpenCodeReady: true,
|
|
openCodeNotReadySince: 0,
|
|
isRestartingOpenCode: false,
|
|
}),
|
|
getOpenCodeAuthHeaders: () => ({}),
|
|
buildOpenCodeUrl: (requestPath) => `${externalBaseUrl}${requestPath}`,
|
|
ensureOpenCodeApiPrefix: () => {},
|
|
});
|
|
proxyServer = await listen(app);
|
|
const proxyPort = proxyServer.address().port;
|
|
|
|
const response = await fetch(`http://127.0.0.1:${proxyPort}/api/config/providers`);
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(await response.json()).toEqual({ ok: true, source: 'external-host' });
|
|
});
|
|
|
|
it('replays parsed urlencoded bodies to generic API proxy requests', async () => {
|
|
const upstream = express();
|
|
upstream.post('/form', express.urlencoded({ extended: true }), (req, res) => {
|
|
res.json({ body: req.body });
|
|
});
|
|
upstreamServer = await listen(upstream);
|
|
const upstreamPort = upstreamServer.address().port;
|
|
const externalBaseUrl = `http://127.0.0.1:${upstreamPort}`;
|
|
|
|
const app = express();
|
|
app.use('/api', express.urlencoded({ extended: true }));
|
|
registerOpenCodeProxy(app, {
|
|
fs: {},
|
|
os: {},
|
|
path,
|
|
OPEN_CODE_READY_GRACE_MS: 0,
|
|
getRuntime: () => ({
|
|
openCodePort: upstreamPort,
|
|
openCodeBaseUrl: externalBaseUrl,
|
|
isOpenCodeReady: true,
|
|
openCodeNotReadySince: 0,
|
|
isRestartingOpenCode: false,
|
|
}),
|
|
getOpenCodeAuthHeaders: () => ({}),
|
|
buildOpenCodeUrl: (requestPath) => `${externalBaseUrl}${requestPath}`,
|
|
ensureOpenCodeApiPrefix: () => {},
|
|
});
|
|
proxyServer = await listen(app);
|
|
const proxyPort = proxyServer.address().port;
|
|
|
|
const response = await fetch(`http://127.0.0.1:${proxyPort}/api/form`, {
|
|
method: 'POST',
|
|
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
body: new URLSearchParams({ messageID: 'msg_1' }),
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(await response.json()).toEqual({ body: { messageID: 'msg_1' } });
|
|
});
|
|
|
|
it('replays parsed JSON bodies to generic API proxy requests', async () => {
|
|
const upstream = express();
|
|
upstream.post('/session/abc/prompt_async', express.json(), (req, res) => {
|
|
res.json({
|
|
body: req.body,
|
|
authorization: req.headers.authorization,
|
|
contentLength: req.headers['content-length'],
|
|
});
|
|
});
|
|
upstreamServer = await listen(upstream);
|
|
const upstreamPort = upstreamServer.address().port;
|
|
const externalBaseUrl = `http://127.0.0.1:${upstreamPort}`;
|
|
|
|
const app = express();
|
|
app.use('/api', express.json());
|
|
registerOpenCodeProxy(app, {
|
|
fs: {},
|
|
os: {},
|
|
path,
|
|
OPEN_CODE_READY_GRACE_MS: 0,
|
|
getRuntime: () => ({
|
|
openCodePort: upstreamPort,
|
|
openCodeBaseUrl: externalBaseUrl,
|
|
isOpenCodeReady: true,
|
|
openCodeNotReadySince: 0,
|
|
isRestartingOpenCode: false,
|
|
}),
|
|
getOpenCodeAuthHeaders: () => ({ Authorization: 'Bearer replay-token' }),
|
|
buildOpenCodeUrl: (requestPath) => `${externalBaseUrl}${requestPath}`,
|
|
ensureOpenCodeApiPrefix: () => {},
|
|
});
|
|
proxyServer = await listen(app);
|
|
const proxyPort = proxyServer.address().port;
|
|
|
|
const payload = { messageID: 'msg_1', parts: [{ type: 'text', text: 'hello' }] };
|
|
const response = await fetch(`http://127.0.0.1:${proxyPort}/api/session/abc/prompt_async`, {
|
|
method: 'POST',
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
const data = await response.json();
|
|
expect(data.body).toEqual(payload);
|
|
expect(data.authorization).toBe('Bearer replay-token');
|
|
expect(Number(data.contentLength)).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('forwards unparsed SDK JSON bodies to generic API proxy requests', async () => {
|
|
const upstream = express();
|
|
upstream.post('/session/abc/revert', express.json(), (req, res) => {
|
|
res.json({
|
|
body: req.body,
|
|
contentLength: req.headers['content-length'],
|
|
});
|
|
});
|
|
upstreamServer = await listen(upstream);
|
|
const upstreamPort = upstreamServer.address().port;
|
|
const externalBaseUrl = `http://127.0.0.1:${upstreamPort}`;
|
|
|
|
const app = express();
|
|
registerOpenCodeProxy(app, {
|
|
fs: {},
|
|
os: {},
|
|
path,
|
|
OPEN_CODE_READY_GRACE_MS: 0,
|
|
getRuntime: () => ({
|
|
openCodePort: upstreamPort,
|
|
openCodeBaseUrl: externalBaseUrl,
|
|
isOpenCodeReady: true,
|
|
openCodeNotReadySince: 0,
|
|
isRestartingOpenCode: false,
|
|
}),
|
|
getOpenCodeAuthHeaders: () => ({}),
|
|
buildOpenCodeUrl: (requestPath) => `${externalBaseUrl}${requestPath}`,
|
|
ensureOpenCodeApiPrefix: () => {},
|
|
});
|
|
proxyServer = await listen(app);
|
|
const proxyPort = proxyServer.address().port;
|
|
|
|
const payload = { messageID: 'msg_1' };
|
|
const response = await fetch(`http://127.0.0.1:${proxyPort}/api/session/abc/revert`, {
|
|
method: 'POST',
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
const data = await response.json();
|
|
expect(data.body).toEqual(payload);
|
|
expect(Number(data.contentLength)).toBeGreaterThan(0);
|
|
});
|
|
});
|