@andorra-labs/code 0.1.16 → 0.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/acp-agent.d.ts +6 -0
- package/dist/src/acp-agent.js +15 -4
- package/dist/src/agent-execution.js +11 -8
- package/dist/src/agent-mcp-catalog.js +1 -0
- package/dist/src/agent-mcp.d.ts +3 -0
- package/dist/src/agent-mcp.js +33 -1
- package/dist/src/andorra/entry.d.ts +10 -0
- package/dist/src/andorra/entry.js +18 -4
- package/dist/src/andorra/hosted.d.ts +2 -1
- package/dist/src/andorra/hosted.js +2 -1
- package/dist/src/andorra/notices.d.ts +5 -0
- package/dist/src/andorra/notices.js +7 -0
- package/dist/src/andorra/state.d.ts +3 -2
- package/dist/src/andorra/state.js +4 -4
- package/dist/src/andorra/terminal.js +7 -2
- package/dist/src/anthropic-credentials.js +2 -0
- package/dist/src/app-server/error.d.ts +5 -0
- package/dist/src/app-server/error.js +7 -0
- package/dist/src/app-server/protocol.d.ts +19 -44
- package/dist/src/app-server/protocol.js +5 -1
- package/dist/src/app-server/workspace.d.ts +89 -0
- package/dist/src/app-server/workspace.js +457 -11
- package/dist/src/app-server.d.ts +358 -32
- package/dist/src/app-server.js +2429 -509
- package/dist/src/bridge/cloud.d.ts +27 -0
- package/dist/src/bridge/cloud.js +187 -0
- package/dist/src/bridge/command.d.ts +30 -0
- package/dist/src/bridge/command.js +347 -0
- package/dist/src/bridge/control.d.ts +68 -0
- package/dist/src/bridge/control.js +233 -0
- package/dist/src/bridge/engines.d.ts +90 -0
- package/dist/src/bridge/engines.js +601 -0
- package/dist/src/bridge/host-ops.d.ts +9 -0
- package/dist/src/bridge/host-ops.js +129 -0
- package/dist/src/bridge/host.d.ts +47 -0
- package/dist/src/bridge/host.js +540 -0
- package/dist/src/bridge/link.d.ts +38 -0
- package/dist/src/bridge/link.js +508 -0
- package/dist/src/bridge/login-link.d.ts +29 -0
- package/dist/src/bridge/login-link.js +54 -0
- package/dist/src/bridge/protocol.d.ts +136 -0
- package/dist/src/bridge/protocol.js +11 -0
- package/dist/src/bridge/store.d.ts +12 -0
- package/dist/src/bridge/store.js +61 -0
- package/dist/src/bridge/wording.d.ts +6 -0
- package/dist/src/bridge/wording.js +10 -0
- package/dist/src/browser-server.js +7 -1
- package/dist/src/browser-ui/tokens.css +1 -1
- package/dist/src/change-authors.d.ts +41 -0
- package/dist/src/change-authors.js +146 -0
- package/dist/src/cli.js +182 -32
- package/dist/src/coding-tools.js +15 -15
- package/dist/src/command-screens.d.ts +6 -0
- package/dist/src/command-screens.js +14 -11
- package/dist/src/commands.d.ts +14 -2
- package/dist/src/compaction-phrases.js +1 -1
- package/dist/src/completion.js +82 -5
- package/dist/src/config.d.ts +71 -3
- package/dist/src/config.js +202 -77
- package/dist/src/deadline.js +13 -1
- package/dist/src/decision/checks.js +1 -1
- package/dist/src/decision/requestPhrases.js +0 -1
- package/dist/src/default-graph.js +1 -1
- package/dist/src/file-diff.d.ts +4 -0
- package/dist/src/file-diff.js +2 -1
- package/dist/src/github-app/agent-writes.d.ts +102 -0
- package/dist/src/github-app/agent-writes.js +320 -0
- package/dist/src/github-app/client.d.ts +349 -0
- package/dist/src/github-app/client.js +810 -0
- package/dist/src/github-app/event-source.d.ts +208 -0
- package/dist/src/github-app/event-source.js +684 -0
- package/dist/src/github-app/metrics.d.ts +32 -0
- package/dist/src/github-app/metrics.js +49 -0
- package/dist/src/github-app/review-loop.d.ts +274 -0
- package/dist/src/github-app/review-loop.js +1177 -0
- package/dist/src/github-app/review-node.d.ts +81 -0
- package/dist/src/github-app/review-node.js +205 -0
- package/dist/src/github-app/review-post.d.ts +73 -0
- package/dist/src/github-app/review-post.js +139 -0
- package/dist/src/github-app/review-text.d.ts +107 -0
- package/dist/src/github-app/review-text.js +1133 -0
- package/dist/src/github-app/review-threads.d.ts +47 -0
- package/dist/src/github-app/review-threads.js +107 -0
- package/dist/src/github-app/reviewer.d.ts +121 -0
- package/dist/src/github-app/reviewer.js +598 -0
- package/dist/src/github-app/store.d.ts +59 -0
- package/dist/src/github-app/store.js +148 -0
- package/dist/src/github-app/triggers.d.ts +141 -0
- package/dist/src/github-app/triggers.js +441 -0
- package/dist/src/github-names.d.ts +8 -0
- package/dist/src/github-names.js +8 -0
- package/dist/src/glob-search.d.ts +61 -0
- package/dist/src/glob-search.js +180 -0
- package/dist/src/graph-health.js +6 -1
- package/dist/src/graph-manager-chat.js +1 -1
- package/dist/src/graph-preview.d.ts +46 -0
- package/dist/src/graph-preview.js +55 -0
- package/dist/src/graph-provider-portability.js +2 -1
- package/dist/src/graph-runner.js +64 -3
- package/dist/src/graph-server.js +3 -3
- package/dist/src/graph-sync.js +3 -3
- package/dist/src/graph-ui/app.js +91 -7
- package/dist/src/graph-ui/icons.js +2 -1
- package/dist/src/graph-ui/studio.js +15 -1
- package/dist/src/graph-ui/tokens.css +1 -1
- package/dist/src/graphs.d.ts +34 -1
- package/dist/src/graphs.js +158 -14
- package/dist/src/image-input.d.ts +16 -0
- package/dist/src/image-input.js +41 -0
- package/dist/src/membership.d.ts +10 -0
- package/dist/src/membership.js +19 -4
- package/dist/src/model-health-cache.js +1 -1
- package/dist/src/model-metadata.d.ts +6 -0
- package/dist/src/model-metadata.js +35 -15
- package/dist/src/model-relay/local.d.ts +116 -0
- package/dist/src/model-relay/local.js +468 -0
- package/dist/src/model-relay/remote.d.ts +129 -0
- package/dist/src/model-relay/remote.js +252 -0
- package/dist/src/model-relay/runs.d.ts +34 -0
- package/dist/src/model-relay/runs.js +146 -0
- package/dist/src/model-relay/wire.d.ts +5 -0
- package/dist/src/model-relay/wire.js +5 -0
- package/dist/src/project-registry.d.ts +21 -0
- package/dist/src/project-registry.js +74 -0
- package/dist/src/provider-import.d.ts +0 -12
- package/dist/src/provider-import.js +7 -195
- package/dist/src/providers.d.ts +23 -2
- package/dist/src/providers.js +25 -4
- package/dist/src/pull-request-logs.d.ts +12 -1
- package/dist/src/pull-request-logs.js +14 -3
- package/dist/src/pull-requests.d.ts +327 -7
- package/dist/src/pull-requests.js +429 -51
- package/dist/src/react-runtime.js +1 -1
- package/dist/src/remote/engine-pool.d.ts +88 -4
- package/dist/src/remote/engine-pool.js +366 -89
- package/dist/src/remote/roots.d.ts +19 -0
- package/dist/src/remote/roots.js +45 -7
- package/dist/src/remote/types.d.ts +0 -44
- package/dist/src/run-control.d.ts +7 -0
- package/dist/src/run-control.js +23 -0
- package/dist/src/run-records.d.ts +7 -2
- package/dist/src/run-records.js +71 -6
- package/dist/src/runtime.d.ts +5 -1
- package/dist/src/runtime.js +10 -2
- package/dist/src/session-changes.d.ts +35 -0
- package/dist/src/session-changes.js +194 -6
- package/dist/src/session-monitor.js +2 -1
- package/dist/src/session-reaper.d.ts +1 -1
- package/dist/src/session-reaper.js +6 -1
- package/dist/src/session-summary.d.ts +2 -1
- package/dist/src/session-summary.js +2 -1
- package/dist/src/sessions.d.ts +11 -1
- package/dist/src/sessions.js +17 -4
- package/dist/src/skills/bundled/diagram/SKILL.md +5 -3
- package/dist/src/skills/bundled/graph-manager/SKILL.md +7 -4
- package/dist/src/skills/bundled/provenance.json +2 -2
- package/dist/src/skills/runtime-skills.js +1 -1
- package/dist/src/telemetry/emitter.d.ts +15 -0
- package/dist/src/telemetry/emitter.js +35 -1
- package/dist/src/telemetry/schema.d.ts +25 -2
- package/dist/src/telemetry/schema.js +52 -4
- package/dist/src/telemetry/surface.d.ts +2 -1
- package/dist/src/telemetry/surface.js +4 -1
- package/dist/src/terminal-ui.d.ts +1 -0
- package/dist/src/terminal-ui.js +10 -3
- package/dist/src/test-edit-guard.d.ts +7 -0
- package/dist/src/test-edit-guard.js +49 -1
- package/dist/src/timers.d.ts +2 -0
- package/dist/src/tools.js +59 -9
- package/package.json +6 -1
- package/dist/src/remote/cert.d.ts +0 -11
- package/dist/src/remote/cert.js +0 -87
- package/dist/src/remote/client.d.ts +0 -119
- package/dist/src/remote/client.js +0 -544
- package/dist/src/remote/command.d.ts +0 -4
- package/dist/src/remote/command.js +0 -194
- package/dist/src/remote/errors.d.ts +0 -11
- package/dist/src/remote/errors.js +0 -14
- package/dist/src/remote/network.d.ts +0 -22
- package/dist/src/remote/network.js +0 -102
- package/dist/src/remote/pairing.d.ts +0 -63
- package/dist/src/remote/pairing.js +0 -299
- package/dist/src/remote/pem.d.ts +0 -2
- package/dist/src/remote/pem.js +0 -7
- package/dist/src/remote/serve.d.ts +0 -68
- package/dist/src/remote/serve.js +0 -244
- package/dist/src/remote/server.d.ts +0 -21
- package/dist/src/remote/server.js +0 -406
|
@@ -1,406 +0,0 @@
|
|
|
1
|
-
import { createServer } from 'node:http';
|
|
2
|
-
import { createServer as createHttpsServer } from 'node:https';
|
|
3
|
-
import { timingSafeEqual } from 'node:crypto';
|
|
4
|
-
import { homedir } from 'node:os';
|
|
5
|
-
import { opendir, realpath, stat } from 'node:fs/promises';
|
|
6
|
-
import { dirname, basename, isAbsolute, join } from 'node:path';
|
|
7
|
-
import { URL } from 'node:url';
|
|
8
|
-
import { changeConfigFile, loadConfig } from '../config.js';
|
|
9
|
-
import { InviteRejectedError } from './errors.js';
|
|
10
|
-
import { redeemRemoteInvitation, addProjectEntry } from './pairing.js';
|
|
11
|
-
import { authorizedRoots, projectRootId } from './roots.js';
|
|
12
|
-
import { RemoteEngineRelay, RELAY_CAPABILITY_REQUIREMENT } from './engine-pool.js';
|
|
13
|
-
export const REMOTE_BROWSE_ENTRY_CAP = 1000;
|
|
14
|
-
const SKIP_NAMES = new Set(['.git', 'node_modules', '.andorracode']);
|
|
15
|
-
const MAX_BODY_BYTES = 64 * 1024;
|
|
16
|
-
const MAX_ENGINE_BODY_BYTES = 12 * 1024 * 1024;
|
|
17
|
-
const ENGINE_EVENTS_HEARTBEAT_MS = 15_000;
|
|
18
|
-
const MAX_SSE_BACKLOG_BYTES = 4 * 1024 * 1024;
|
|
19
|
-
function json(res, status, value) {
|
|
20
|
-
const body = JSON.stringify(value);
|
|
21
|
-
res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff' });
|
|
22
|
-
res.end(body);
|
|
23
|
-
}
|
|
24
|
-
function secretMatches(supplied, expected) {
|
|
25
|
-
if (supplied === undefined)
|
|
26
|
-
return false;
|
|
27
|
-
const left = Buffer.from(supplied);
|
|
28
|
-
const right = Buffer.from(expected);
|
|
29
|
-
return left.length === right.length && timingSafeEqual(left, right);
|
|
30
|
-
}
|
|
31
|
-
async function authenticateDevice(config, authorization) {
|
|
32
|
-
const match = /^Bearer ([A-Za-z0-9_-]{43})$/.exec(authorization ?? '');
|
|
33
|
-
if (!match)
|
|
34
|
-
return undefined;
|
|
35
|
-
const token = Buffer.from(match[1]);
|
|
36
|
-
for (const device of config.remote?.hosting?.devices ?? []) {
|
|
37
|
-
const stored = Buffer.from(device.token);
|
|
38
|
-
if (stored.length === token.length && timingSafeEqual(stored, token))
|
|
39
|
-
return { id: device.id, grant: device.grant === 'full' ? 'full' : 'browse' };
|
|
40
|
-
}
|
|
41
|
-
return undefined;
|
|
42
|
-
}
|
|
43
|
-
async function readBody(req, cap = MAX_BODY_BYTES) {
|
|
44
|
-
const chunks = [];
|
|
45
|
-
let size = 0;
|
|
46
|
-
for await (const chunk of req) {
|
|
47
|
-
size += chunk.length;
|
|
48
|
-
if (size > cap)
|
|
49
|
-
throw new Error('Request body too large');
|
|
50
|
-
chunks.push(chunk);
|
|
51
|
-
}
|
|
52
|
-
if (!chunks.length)
|
|
53
|
-
return {};
|
|
54
|
-
return JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
55
|
-
}
|
|
56
|
-
async function listDirectory(directory) {
|
|
57
|
-
const entries = [];
|
|
58
|
-
let hasMore = false;
|
|
59
|
-
for await (const entry of await opendir(directory)) {
|
|
60
|
-
if (entry.isSymbolicLink() || SKIP_NAMES.has(entry.name))
|
|
61
|
-
continue;
|
|
62
|
-
if (!(entry.isDirectory() || entry.isFile()))
|
|
63
|
-
continue;
|
|
64
|
-
if (entries.length >= REMOTE_BROWSE_ENTRY_CAP) {
|
|
65
|
-
hasMore = true;
|
|
66
|
-
break;
|
|
67
|
-
}
|
|
68
|
-
entries.push({ name: entry.name, type: entry.isDirectory() ? 'directory' : 'file' });
|
|
69
|
-
}
|
|
70
|
-
entries.sort((left, right) => left.type === right.type ? left.name.localeCompare(right.name) : left.type === 'directory' ? -1 : 1);
|
|
71
|
-
return { entries, hasMore };
|
|
72
|
-
}
|
|
73
|
-
class LastSeenTracker {
|
|
74
|
-
directory;
|
|
75
|
-
pending = new Map();
|
|
76
|
-
dirty = false;
|
|
77
|
-
timer;
|
|
78
|
-
constructor(directory) {
|
|
79
|
-
this.directory = directory;
|
|
80
|
-
}
|
|
81
|
-
note(deviceId) {
|
|
82
|
-
this.pending.set(deviceId, new Date().toISOString());
|
|
83
|
-
this.dirty = true;
|
|
84
|
-
if (this.timer)
|
|
85
|
-
return;
|
|
86
|
-
this.timer = setInterval(() => void this.flush(), 60_000);
|
|
87
|
-
this.timer.unref?.();
|
|
88
|
-
}
|
|
89
|
-
async flush() {
|
|
90
|
-
if (!this.dirty)
|
|
91
|
-
return;
|
|
92
|
-
this.dirty = false;
|
|
93
|
-
const seen = [...this.pending];
|
|
94
|
-
try {
|
|
95
|
-
await changeConfigFile(this.directory, config => {
|
|
96
|
-
const devices = config.remote?.hosting?.devices ?? [];
|
|
97
|
-
if (!devices.length)
|
|
98
|
-
return config;
|
|
99
|
-
let changed = false;
|
|
100
|
-
const next = devices.map(device => {
|
|
101
|
-
const stamp = seen.find(([id]) => id === device.id)?.[1];
|
|
102
|
-
if (!stamp || device.lastSeenAt === stamp)
|
|
103
|
-
return device;
|
|
104
|
-
changed = true;
|
|
105
|
-
return { ...device, lastSeenAt: stamp };
|
|
106
|
-
});
|
|
107
|
-
return changed ? { ...config, remote: { ...config.remote, hosting: { ...config.remote?.hosting, devices: next } } } : config;
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
catch { }
|
|
111
|
-
}
|
|
112
|
-
async stop() {
|
|
113
|
-
if (this.timer)
|
|
114
|
-
clearInterval(this.timer);
|
|
115
|
-
await this.flush();
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
export async function startRemoteServer(options, bindPort = 0) {
|
|
119
|
-
const lastSeen = new LastSeenTracker(options.directory);
|
|
120
|
-
const relay = new RemoteEngineRelay(options.directory);
|
|
121
|
-
const handler = async (req, res) => {
|
|
122
|
-
try {
|
|
123
|
-
const url = new URL(req.url ?? '/', 'http://peer.invalid');
|
|
124
|
-
const path = url.pathname;
|
|
125
|
-
if (req.method === 'GET' && path === '/api/health') {
|
|
126
|
-
const ownerHeader = req.headers['x-andorra-owner'];
|
|
127
|
-
const ownerValue = Array.isArray(ownerHeader) ? ownerHeader[0] : ownerHeader;
|
|
128
|
-
if (!secretMatches(ownerValue, options.ownerSecret)) {
|
|
129
|
-
json(res, 401, { error: 'Unauthenticated' });
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
json(res, 200, { ok: true, installId: options.installId, transport: options.transport });
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
if (req.method === 'POST' && path === '/api/pair/redeem') {
|
|
136
|
-
const body = await readBody(req);
|
|
137
|
-
if (typeof body.invite !== 'string' || !/^[A-Za-z0-9_-]{43}$/.test(body.invite)) {
|
|
138
|
-
json(res, 400, { error: 'A pairing invitation token is required' });
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
try {
|
|
142
|
-
const { deviceId, credential, grant } = await redeemRemoteInvitation(body.invite, {
|
|
143
|
-
directory: options.directory,
|
|
144
|
-
label: typeof body.label === 'string' ? body.label.slice(0, 64) : undefined,
|
|
145
|
-
});
|
|
146
|
-
json(res, 200, { deviceId, credential, hostname: options.hostname, platform: options.platform, version: options.version, grant: options.transport === 'https' ? grant : 'browse' });
|
|
147
|
-
}
|
|
148
|
-
catch (error) {
|
|
149
|
-
if (error instanceof InviteRejectedError)
|
|
150
|
-
json(res, 403, { error: error.message });
|
|
151
|
-
else
|
|
152
|
-
json(res, 500, { error: 'Pairing failed' });
|
|
153
|
-
}
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
const config = await loadConfig(options.directory);
|
|
157
|
-
const deviceId = await authenticateDevice(config, req.headers.authorization);
|
|
158
|
-
if (!deviceId) {
|
|
159
|
-
json(res, 401, { error: 'This device is not paired, or its pairing was revoked.' });
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
lastSeen.note(deviceId.id);
|
|
163
|
-
if (req.method === 'GET' && path === '/api/whoami') {
|
|
164
|
-
const { APP_SERVER_CAPABILITIES } = await import('../app-server/protocol.js');
|
|
165
|
-
json(res, 200, { hostname: options.hostname, platform: options.platform, version: options.version, grant: options.transport === 'https' ? deviceId.grant : 'browse', relay: options.transport === 'https' && deviceId.grant === 'full', capabilities: [...new Set([...APP_SERVER_CAPABILITIES, 'remote.engine.relay', 'remote.providers.export', RELAY_CAPABILITY_REQUIREMENT])] });
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
if (path.startsWith('/api/owner/')) {
|
|
169
|
-
if (options.transport !== 'https' || deviceId.grant !== 'full') {
|
|
170
|
-
json(res, 403, { error: 'Full-control consent over TLS is required. Re-pair with a full-access invitation.' });
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
if (req.method === 'GET' && path === '/api/owner/roots') {
|
|
174
|
-
const roots = [{ path: homedir(), name: 'Home' }];
|
|
175
|
-
if (process.platform === 'win32') {
|
|
176
|
-
for (const drive of 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') {
|
|
177
|
-
const root = drive + ':\\';
|
|
178
|
-
if ((await stat(root).catch(() => undefined))?.isDirectory())
|
|
179
|
-
roots.push({ path: root, name: root });
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
else
|
|
183
|
-
roots.push({ path: '/', name: 'Filesystem' });
|
|
184
|
-
json(res, 200, { roots });
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
if (req.method === 'GET' && path === '/api/owner/browse') {
|
|
188
|
-
const requested = url.searchParams.get('path') ?? '';
|
|
189
|
-
if (!requested || requested.length > 4096 || requested.includes('\0') || !isAbsolute(requested)) {
|
|
190
|
-
json(res, 400, { error: 'An absolute host directory is required' });
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
const actual = await realpath(requested).catch(() => undefined);
|
|
194
|
-
if (!actual || !(await stat(actual).catch(() => undefined))?.isDirectory()) {
|
|
195
|
-
json(res, 404, { error: 'Directory does not exist' });
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
const listing = await listDirectory(actual);
|
|
199
|
-
json(res, 200, { path: actual, parent: dirname(actual) === actual ? null : dirname(actual),
|
|
200
|
-
...listing, entries: listing.entries.map(entry => ({ ...entry, path: join(actual, entry.name) })) });
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
if (req.method === 'POST' && path === '/api/owner/projects') {
|
|
204
|
-
const body = await readBody(req);
|
|
205
|
-
if (typeof body.path !== 'string' || body.path.length > 4096 || body.path.includes('\0') || !isAbsolute(body.path)
|
|
206
|
-
|| (body.name !== undefined && (typeof body.name !== 'string' || !body.name.trim() || body.name.length > 100 || /[\0\r\n]/.test(body.name)))) {
|
|
207
|
-
json(res, 400, { error: 'Invalid host project path/name' });
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
const actual = await realpath(body.path).catch(() => undefined);
|
|
211
|
-
if (!actual || !(await stat(actual).catch(() => undefined))?.isDirectory()) {
|
|
212
|
-
json(res, 404, { error: 'Project directory does not exist' });
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
const projectName = typeof body.name === 'string' ? body.name.trim() : basename(actual) || actual;
|
|
216
|
-
await addProjectEntry(actual, { directory: options.directory, name: projectName });
|
|
217
|
-
const recorded = (await loadConfig(options.directory)).projects?.find(project => project.path === actual);
|
|
218
|
-
json(res, 200, { rootId: projectRootId(actual), name: recorded?.name?.trim() || basename(actual) || actual });
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
json(res, 404, { error: 'Unknown owner action' });
|
|
222
|
-
return;
|
|
223
|
-
}
|
|
224
|
-
if (path === '/api/engine/request' || path === '/api/engine/events') {
|
|
225
|
-
if (options.transport !== 'https' || deviceId.grant !== 'full') {
|
|
226
|
-
json(res, 403, { error: 'Full-control consent over TLS is required. Re-pair with a full-access invitation.' });
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
if (req.method === 'POST' && path === '/api/engine/request') {
|
|
230
|
-
const body = await readBody(req, MAX_ENGINE_BODY_BYTES);
|
|
231
|
-
const answer = await relay.request(deviceId.id, {
|
|
232
|
-
connectionId: typeof body.connectionId === 'string' ? body.connectionId : '',
|
|
233
|
-
...(typeof body.viewerWorkspaceId === 'string' ? { viewerWorkspaceId: body.viewerWorkspaceId } : {}),
|
|
234
|
-
engine: typeof body.engine === 'string' ? body.engine : '',
|
|
235
|
-
method: typeof body.method === 'string' ? body.method : '',
|
|
236
|
-
...(body.params !== undefined && typeof body.params === 'object' && !Array.isArray(body.params) ? { params: body.params } : {}),
|
|
237
|
-
...(typeof body.deadlineMs === 'number' ? { deadlineMs: body.deadlineMs } : {}),
|
|
238
|
-
});
|
|
239
|
-
json(res, 200, answer);
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
if (req.method === 'GET' && path === '/api/engine/events') {
|
|
243
|
-
const connectionId = url.searchParams.get('connectionId') ?? '';
|
|
244
|
-
const viewerWorkspaceId = url.searchParams.get('viewerWorkspaceId') ?? undefined;
|
|
245
|
-
if (!/^[A-Za-z0-9-]{8,64}$/.test(connectionId)) {
|
|
246
|
-
json(res, 400, { error: 'A connectionId query parameter is required.' });
|
|
247
|
-
return;
|
|
248
|
-
}
|
|
249
|
-
req.setTimeout(0);
|
|
250
|
-
res.writeHead(200, {
|
|
251
|
-
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
252
|
-
'Cache-Control': 'no-store',
|
|
253
|
-
'X-Accel-Buffering': 'no',
|
|
254
|
-
'X-Content-Type-Options': 'nosniff',
|
|
255
|
-
});
|
|
256
|
-
res.write(': connected\n\n');
|
|
257
|
-
let closed = false;
|
|
258
|
-
const endStream = () => {
|
|
259
|
-
if (closed)
|
|
260
|
-
return;
|
|
261
|
-
closed = true;
|
|
262
|
-
clearInterval(heartbeat);
|
|
263
|
-
res.end();
|
|
264
|
-
};
|
|
265
|
-
const heartbeat = setInterval(() => { try {
|
|
266
|
-
if (!closed)
|
|
267
|
-
res.write(': ping\n\n');
|
|
268
|
-
}
|
|
269
|
-
catch {
|
|
270
|
-
endStream();
|
|
271
|
-
} }, ENGINE_EVENTS_HEARTBEAT_MS);
|
|
272
|
-
heartbeat.unref?.();
|
|
273
|
-
const stop = relay.events(deviceId.id, connectionId, viewerWorkspaceId, frame => {
|
|
274
|
-
if (closed)
|
|
275
|
-
return;
|
|
276
|
-
if (res.writableLength > MAX_SSE_BACKLOG_BYTES) {
|
|
277
|
-
endStream();
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
res.write(`data: ${JSON.stringify(frame)}\n\n`);
|
|
281
|
-
}, () => {
|
|
282
|
-
endStream();
|
|
283
|
-
});
|
|
284
|
-
res.on('error', () => endStream());
|
|
285
|
-
res.on('close', () => { endStream(); stop(); });
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
json(res, 404, { error: 'Not found' });
|
|
289
|
-
return;
|
|
290
|
-
}
|
|
291
|
-
if (req.method === 'GET' && path === '/api/providers/export') {
|
|
292
|
-
if (options.transport !== 'https' || deviceId.grant !== 'full') {
|
|
293
|
-
json(res, 403, { error: 'Full-control consent over TLS is required. Re-pair with a full-access invitation.' });
|
|
294
|
-
return;
|
|
295
|
-
}
|
|
296
|
-
json(res, 200, { providers: config.providers });
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
if (req.method === 'GET' && path === '/api/projects') {
|
|
300
|
-
const roots = await authorizedRoots(config);
|
|
301
|
-
json(res, 200, { projects: roots.filter(root => root.kind === 'project').map(root => ({ rootId: root.id, name: root.label })) });
|
|
302
|
-
return;
|
|
303
|
-
}
|
|
304
|
-
if (req.method === 'GET' && path === '/api/roots') {
|
|
305
|
-
const roots = await authorizedRoots(config);
|
|
306
|
-
json(res, 200, { roots: roots.map(root => ({ id: root.id, label: root.label, kind: root.kind })) });
|
|
307
|
-
return;
|
|
308
|
-
}
|
|
309
|
-
if (req.method === 'GET' && path === '/api/browse') {
|
|
310
|
-
const rootId = url.searchParams.get('root');
|
|
311
|
-
const roots = await authorizedRoots(config);
|
|
312
|
-
const root = roots.find(entry => entry.id === rootId);
|
|
313
|
-
if (!root) {
|
|
314
|
-
json(res, 404, { error: 'Unknown root' });
|
|
315
|
-
return;
|
|
316
|
-
}
|
|
317
|
-
const rel = url.searchParams.get('rel') ?? '';
|
|
318
|
-
if (rel.includes('\0') || rel.length > 4096
|
|
319
|
-
|| rel.startsWith('/') || rel.startsWith('\\') || /^[A-Za-z]:/.test(rel)
|
|
320
|
-
|| rel.split(/[\\/]/).includes('..')) {
|
|
321
|
-
json(res, 400, { error: 'Invalid path' });
|
|
322
|
-
return;
|
|
323
|
-
}
|
|
324
|
-
const { resolveInside } = await import('../app-server/workspace.js');
|
|
325
|
-
const target = await resolveInside(root.path, rel).catch(() => undefined);
|
|
326
|
-
if (target === undefined) {
|
|
327
|
-
json(res, 403, { error: 'That path is outside the shared folder' });
|
|
328
|
-
return;
|
|
329
|
-
}
|
|
330
|
-
const info = await stat(target).catch(() => undefined);
|
|
331
|
-
if (!info) {
|
|
332
|
-
json(res, 404, { error: 'That folder no longer exists' });
|
|
333
|
-
return;
|
|
334
|
-
}
|
|
335
|
-
if (!info.isDirectory()) {
|
|
336
|
-
json(res, 400, { error: 'Only folders can be listed' });
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
const listing = await listDirectory(target);
|
|
340
|
-
json(res, 200, {
|
|
341
|
-
rootId: root.id,
|
|
342
|
-
rel,
|
|
343
|
-
parent: rel === '' || rel === '.' ? null : dirname(rel.split('\\').join('/')) === '.' ? '' : dirname(rel.split('\\').join('/')),
|
|
344
|
-
...listing,
|
|
345
|
-
});
|
|
346
|
-
return;
|
|
347
|
-
}
|
|
348
|
-
json(res, 404, { error: 'Not found' });
|
|
349
|
-
}
|
|
350
|
-
catch {
|
|
351
|
-
json(res, 500, { error: 'Remote request failed' });
|
|
352
|
-
}
|
|
353
|
-
};
|
|
354
|
-
const listeners = [];
|
|
355
|
-
const makeServer = () => {
|
|
356
|
-
const server = options.transport === 'https'
|
|
357
|
-
? createHttpsServer({ key: options.keyPem, cert: options.certPem }, handler)
|
|
358
|
-
: createServer(handler);
|
|
359
|
-
server.requestTimeout = 30_000;
|
|
360
|
-
server.headersTimeout = 10_000;
|
|
361
|
-
return server;
|
|
362
|
-
};
|
|
363
|
-
const listenOn = (server, port, host, ipv6Only) => new Promise((resolve, reject) => {
|
|
364
|
-
server.once('error', reject);
|
|
365
|
-
server.listen(ipv6Only === undefined ? { port, host } : { port, host, ipv6Only }, () => resolve());
|
|
366
|
-
});
|
|
367
|
-
const closeListeners = async () => {
|
|
368
|
-
await Promise.all(listeners.map(server => new Promise(resolve => {
|
|
369
|
-
server.close(() => resolve());
|
|
370
|
-
server.closeIdleConnections?.();
|
|
371
|
-
})));
|
|
372
|
-
};
|
|
373
|
-
const primary = makeServer();
|
|
374
|
-
listeners.push(primary);
|
|
375
|
-
try {
|
|
376
|
-
await listenOn(primary, bindPort, '0.0.0.0');
|
|
377
|
-
}
|
|
378
|
-
catch (error) {
|
|
379
|
-
await closeListeners().catch(() => undefined);
|
|
380
|
-
throw error;
|
|
381
|
-
}
|
|
382
|
-
const address = primary.address();
|
|
383
|
-
if (!address || typeof address === 'string')
|
|
384
|
-
throw new Error('Remote listener could not determine its port');
|
|
385
|
-
const secondary = makeServer();
|
|
386
|
-
try {
|
|
387
|
-
await listenOn(secondary, address.port, '::', true);
|
|
388
|
-
listeners.push(secondary);
|
|
389
|
-
}
|
|
390
|
-
catch (error) {
|
|
391
|
-
if (error.code === 'EADDRINUSE') {
|
|
392
|
-
await closeListeners().catch(() => undefined);
|
|
393
|
-
throw error;
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
return {
|
|
397
|
-
port: address.port,
|
|
398
|
-
transport: options.transport,
|
|
399
|
-
close: async () => {
|
|
400
|
-
await lastSeen.stop();
|
|
401
|
-
await relay.close();
|
|
402
|
-
await closeListeners();
|
|
403
|
-
},
|
|
404
|
-
};
|
|
405
|
-
}
|
|
406
|
-
export { projectRootId };
|