@andorra-labs/code 0.1.15 → 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-definitions.js +29 -6
- package/dist/src/agent-execution.js +15 -10
- package/dist/src/agent-mcp-catalog.js +1 -0
- package/dist/src/agent-mcp.d.ts +4 -1
- package/dist/src/agent-mcp.js +46 -10
- 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 +22 -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 +465 -32
- package/dist/src/app-server.js +3308 -517
- package/dist/src/auth/deployment.js +2 -2
- 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-runner.js +1 -0
- package/dist/src/browser-server.js +7 -1
- package/dist/src/browser-ui/style.css +3 -3
- package/dist/src/browser-ui/tokens.css +28 -17
- package/dist/src/change-authors.d.ts +41 -0
- package/dist/src/change-authors.js +146 -0
- package/dist/src/claude-provider.js +3 -1
- package/dist/src/cli.js +202 -38
- package/dist/src/codex-errors.js +4 -1
- package/dist/src/coding-tools.js +15 -15
- package/dist/src/command-screens.d.ts +6 -0
- package/dist/src/command-screens.js +19 -14
- package/dist/src/commands.d.ts +14 -1
- package/dist/src/compaction-phrases.d.ts +4 -0
- package/dist/src/compaction-phrases.js +20 -0
- 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/context-compaction.js +2 -1
- package/dist/src/deadline.js +13 -1
- package/dist/src/decision/DecisionManager.js +3 -2
- package/dist/src/decision/checks.js +1 -1
- package/dist/src/decision/requestPhrases.d.ts +4 -0
- package/dist/src/decision/requestPhrases.js +22 -0
- 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-inbox.d.ts +55 -0
- package/dist/src/github-inbox.js +153 -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 +13 -4
- 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 +34 -11
- package/dist/src/graph-quota.d.ts +29 -2
- package/dist/src/graph-quota.js +68 -10
- package/dist/src/graph-runner.js +88 -10
- package/dist/src/graph-server.d.ts +1 -1
- package/dist/src/graph-server.js +11 -9
- package/dist/src/graph-sync.js +3 -3
- package/dist/src/graph-ui/app.js +201 -40
- package/dist/src/graph-ui/controls.css +1 -0
- package/dist/src/graph-ui/embed.js +2 -2
- package/dist/src/graph-ui/icons.js +3 -1
- package/dist/src/graph-ui/index.html +1 -1
- package/dist/src/graph-ui/manager.css +1 -1
- package/dist/src/graph-ui/manager.js +2 -2
- package/dist/src/graph-ui/sidebar.js +30 -0
- package/dist/src/graph-ui/studio-canvas.js +26 -13
- package/dist/src/graph-ui/studio.css +15 -7
- package/dist/src/graph-ui/studio.js +26 -5
- package/dist/src/graph-ui/style.css +77 -45
- package/dist/src/graph-ui/theme.js +3 -36
- package/dist/src/graph-ui/tokens.css +28 -20
- package/dist/src/graphs.d.ts +34 -1
- package/dist/src/graphs.js +162 -17
- package/dist/src/image-input.d.ts +16 -0
- package/dist/src/image-input.js +41 -0
- package/dist/src/membership.d.ts +30 -1
- package/dist/src/membership.js +48 -8
- package/dist/src/model-health-cache.js +1 -1
- package/dist/src/model-metadata.d.ts +7 -1
- package/dist/src/model-metadata.js +39 -17
- 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/plan-pass.d.ts +59 -0
- package/dist/src/plan-pass.js +165 -0
- package/dist/src/project-registry.d.ts +21 -0
- package/dist/src/project-registry.js +74 -0
- package/dist/src/provider-engine.js +3 -0
- package/dist/src/provider-import.d.ts +0 -12
- package/dist/src/provider-import.js +7 -195
- package/dist/src/providers.d.ts +40 -2
- package/dist/src/providers.js +93 -23
- package/dist/src/pull-request-checks.d.ts +26 -0
- package/dist/src/pull-request-checks.js +56 -0
- package/dist/src/pull-request-conversation.d.ts +134 -0
- package/dist/src/pull-request-conversation.js +221 -0
- package/dist/src/pull-request-create.d.ts +175 -0
- package/dist/src/pull-request-create.js +392 -0
- package/dist/src/pull-request-details.d.ts +242 -0
- package/dist/src/pull-request-details.js +614 -0
- package/dist/src/pull-request-edit.d.ts +23 -0
- package/dist/src/pull-request-edit.js +91 -0
- package/dist/src/pull-request-logs.d.ts +87 -0
- package/dist/src/pull-request-logs.js +183 -0
- package/dist/src/pull-request-merge.d.ts +112 -0
- package/dist/src/pull-request-merge.js +258 -0
- package/dist/src/pull-request-review.d.ts +137 -0
- package/dist/src/pull-request-review.js +401 -0
- package/dist/src/pull-requests.d.ts +369 -8
- package/dist/src/pull-requests.js +524 -55
- package/dist/src/react-runtime.js +38 -5
- package/dist/src/remote/engine-pool.d.ts +88 -4
- package/dist/src/remote/engine-pool.js +379 -90
- 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 +17 -0
- package/dist/src/run-control.js +47 -0
- package/dist/src/run-progress.d.ts +10 -0
- package/dist/src/run-progress.js +122 -0
- package/dist/src/run-records.d.ts +7 -2
- package/dist/src/run-records.js +71 -6
- package/dist/src/runtime.d.ts +9 -1
- package/dist/src/runtime.js +24 -4
- 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/subagent-run.js +1 -0
- package/dist/src/telemetry/capture-core.d.ts +17 -0
- package/dist/src/telemetry/capture-core.js +59 -1
- package/dist/src/telemetry/classify.d.ts +3 -1
- package/dist/src/telemetry/classify.js +37 -0
- package/dist/src/telemetry/emitter.d.ts +17 -0
- package/dist/src/telemetry/emitter.js +76 -5
- package/dist/src/telemetry/schema.d.ts +47 -2
- package/dist/src/telemetry/schema.js +120 -5
- package/dist/src/telemetry/surface.d.ts +3 -1
- package/dist/src/telemetry/surface.js +4 -1
- package/dist/src/telemetry/uploader.d.ts +6 -1
- package/dist/src/telemetry/uploader.js +22 -3
- 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/dist/src/transcript.js +8 -2
- package/dist/src/types.d.ts +7 -0
- package/dist/src/usage-analytics.js +18 -10
- 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
|
@@ -2,8 +2,8 @@ export const AUTH_DEPLOYMENT = {
|
|
|
2
2
|
issuer: 'https://cognito-idp.us-east-2.amazonaws.com/us-east-2_RBw7LImHf',
|
|
3
3
|
clientId: '5gsaecbpdqnt6a0mh6u6h46gag',
|
|
4
4
|
domain: 'https://auth.andorracode.com',
|
|
5
|
-
apiBaseUrl: 'https://
|
|
6
|
-
telemetryBaseUrl: 'https://
|
|
5
|
+
apiBaseUrl: 'https://api.andorracode.com/v1',
|
|
6
|
+
telemetryBaseUrl: 'https://api.andorracode.com/telemetry/v1',
|
|
7
7
|
inferenceBaseUrl: 'https://inference.andorracode.com',
|
|
8
8
|
redirectUri: 'http://localhost:43187/callback',
|
|
9
9
|
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** How often a Cloud instance tells the service whether anyone is using it. */
|
|
2
|
+
export declare const HEARTBEAT_MS = 60000;
|
|
3
|
+
export declare const MAX_BOOT_INPUT_BYTES = 4096;
|
|
4
|
+
/**
|
|
5
|
+
* The link in progress, next to bridge.json. It holds the secret before the boot code is spent, so a link the service
|
|
6
|
+
* accepted is never lost to a failed write: a later run finds it complete and only renames it into place.
|
|
7
|
+
*/
|
|
8
|
+
export declare const PENDING_IDENTITY = "bridge.json.pending";
|
|
9
|
+
/** Shares ~/work when no folder is shared yet, so Cloud always has somewhere to work. */
|
|
10
|
+
export declare function ensureWorkFolder(directory: string, workDirectory?: string): Promise<void>;
|
|
11
|
+
type BootInput = {
|
|
12
|
+
apiBase: string;
|
|
13
|
+
directory: string;
|
|
14
|
+
print(line: string): void;
|
|
15
|
+
workDirectory?: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Links a Cloud instance to its owner with the one-time boot code read from stdin, or, with only an environment id,
|
|
19
|
+
* finishes a link the service already accepted. A boot whose answer does not prove a refusal keeps the pending secret,
|
|
20
|
+
* which is proved at once when the environment id is known, and otherwise by the next boot. The code and the secret are
|
|
21
|
+
* never printed: the secret is made here, a boot sends only its hash, and a recovery presents it to the token route
|
|
22
|
+
* exactly as `andorra bridge run` does.
|
|
23
|
+
*/
|
|
24
|
+
export declare function runCloudBoot(input: BootInput & {
|
|
25
|
+
stdin: string;
|
|
26
|
+
}): Promise<number>;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { createHash, randomBytes, randomUUID } from 'node:crypto';
|
|
2
|
+
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { loadConfig } from '../config.js';
|
|
6
|
+
import { cloudBoot, ControlError, environmentToken } from './control.js';
|
|
7
|
+
import { readBridgeIdentity, shareFolder } from './store.js';
|
|
8
|
+
/** How often a Cloud instance tells the service whether anyone is using it. */
|
|
9
|
+
export const HEARTBEAT_MS = 60_000;
|
|
10
|
+
export const MAX_BOOT_INPUT_BYTES = 4096;
|
|
11
|
+
const RESTART = 'Stop and start Cloud again.';
|
|
12
|
+
const REFUSED_BOOT = {
|
|
13
|
+
boot_expired: `This instance's start code expired. ${RESTART}`,
|
|
14
|
+
boot_used: `This instance's start code was already used. ${RESTART}`,
|
|
15
|
+
boot_unknown: `This instance's start code was not recognised. ${RESTART}`,
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Boot answers that prove the service refused this code and recorded nothing for it, so no later call with the same
|
|
19
|
+
* secret can link: only then is the pending secret dropped. "Already used" is not one (the first, unanswered attempt may
|
|
20
|
+
* be what used it), and neither is any server, network or unreadable answer.
|
|
21
|
+
*/
|
|
22
|
+
const PROVEN_REFUSALS = new Set(['boot_unknown', 'boot_expired']);
|
|
23
|
+
const REFUSED_SECRET = 'Could not finish linking this instance: the Andorra service did not accept its secret. Delete Cloud and start it again.';
|
|
24
|
+
const ENVIRONMENT_ID = /^env_[A-Za-z0-9_-]{22}$/;
|
|
25
|
+
/**
|
|
26
|
+
* The link in progress, next to bridge.json. It holds the secret before the boot code is spent, so a link the service
|
|
27
|
+
* accepted is never lost to a failed write: a later run finds it complete and only renames it into place.
|
|
28
|
+
*/
|
|
29
|
+
export const PENDING_IDENTITY = 'bridge.json.pending';
|
|
30
|
+
async function readPending(directory) {
|
|
31
|
+
try {
|
|
32
|
+
const parsed = JSON.parse(await readFile(join(directory, PENDING_IDENTITY), 'utf8'));
|
|
33
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
34
|
+
return undefined;
|
|
35
|
+
const found = parsed;
|
|
36
|
+
if (typeof found.secret !== 'string' || !found.secret)
|
|
37
|
+
return undefined;
|
|
38
|
+
const pending = { secret: found.secret };
|
|
39
|
+
for (const key of ['environmentId', 'name', 'linkedAt']) {
|
|
40
|
+
if (typeof found[key] === 'string' && found[key])
|
|
41
|
+
pending[key] = found[key];
|
|
42
|
+
}
|
|
43
|
+
return pending;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function complete(pending) {
|
|
50
|
+
return Boolean(pending?.environmentId && pending.name && pending.linkedAt);
|
|
51
|
+
}
|
|
52
|
+
/** Replaces the pending file whole, so a failed write never leaves it without the secret it already held. */
|
|
53
|
+
async function writePending(directory, pending) {
|
|
54
|
+
await mkdir(directory, { recursive: true });
|
|
55
|
+
const target = join(directory, PENDING_IDENTITY);
|
|
56
|
+
const temporary = `${target}.${randomUUID()}.tmp`;
|
|
57
|
+
try {
|
|
58
|
+
await writeFile(temporary, `${JSON.stringify(pending, null, 2)}\n`, { mode: 0o600 });
|
|
59
|
+
await rename(temporary, target);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
await rm(temporary, { force: true }).catch(() => undefined);
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async function finishLink(directory) {
|
|
67
|
+
await rename(join(directory, PENDING_IDENTITY), join(directory, 'bridge.json'));
|
|
68
|
+
}
|
|
69
|
+
/** Shares ~/work when no folder is shared yet, so Cloud always has somewhere to work. */
|
|
70
|
+
export async function ensureWorkFolder(directory, workDirectory) {
|
|
71
|
+
if (((await loadConfig(directory)).bridge?.folders ?? []).length > 0)
|
|
72
|
+
return;
|
|
73
|
+
const work = workDirectory ?? join(homedir(), 'work');
|
|
74
|
+
await mkdir(work, { recursive: true });
|
|
75
|
+
await shareFolder(directory, work, 'work');
|
|
76
|
+
}
|
|
77
|
+
function bootInput(stdin) {
|
|
78
|
+
if (Buffer.byteLength(stdin) > MAX_BOOT_INPUT_BYTES)
|
|
79
|
+
return {};
|
|
80
|
+
try {
|
|
81
|
+
const parsed = JSON.parse(stdin);
|
|
82
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
83
|
+
return {};
|
|
84
|
+
const { bootCode, environmentId } = parsed;
|
|
85
|
+
return {
|
|
86
|
+
...(typeof bootCode === 'string' && bootCode ? { bootCode } : {}),
|
|
87
|
+
...(typeof environmentId === 'string' && ENVIRONMENT_ID.test(environmentId) ? { environmentId } : {}),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return {};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async function saveLink(directory, environmentId, secret, print) {
|
|
95
|
+
try {
|
|
96
|
+
await writePending(directory, { environmentId, name: 'Cloud', secret, linkedAt: new Date().toISOString() });
|
|
97
|
+
await finishLink(directory);
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
print(`Linked, but could not save this instance's link: ${error instanceof Error ? error.message : String(error)}. Run the boot again to finish.`);
|
|
101
|
+
return 1;
|
|
102
|
+
}
|
|
103
|
+
print('Linked as Cloud.');
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Whether the service accepts the pending secret for this environment: the proof that a link it was asked for was
|
|
108
|
+
* recorded, though its answer was lost. The local file is not trusted alone. Answers the failure when it is not proved.
|
|
109
|
+
*/
|
|
110
|
+
async function proveLink(input, environmentId, secret) {
|
|
111
|
+
try {
|
|
112
|
+
await ensureWorkFolder(input.directory, input.workDirectory);
|
|
113
|
+
await environmentToken(input.apiBase, environmentId, secret);
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
return error ?? new Error('unknown failure');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/** Finishes a link the service accepted but whose answer was lost, on a restart that carries no boot code. */
|
|
121
|
+
async function finishAcceptedLink(input, environmentId, secret) {
|
|
122
|
+
const error = await proveLink(input, environmentId, secret);
|
|
123
|
+
if (error === undefined)
|
|
124
|
+
return saveLink(input.directory, environmentId, secret, input.print);
|
|
125
|
+
if (error instanceof ControlError)
|
|
126
|
+
input.print(error.code === 'unlinked' ? REFUSED_SECRET : `Could not reach Andorra: ${error.message}`);
|
|
127
|
+
else
|
|
128
|
+
input.print(`Could not link this instance: ${error instanceof Error ? error.message : String(error)}`);
|
|
129
|
+
return 1;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Links a Cloud instance to its owner with the one-time boot code read from stdin, or, with only an environment id,
|
|
133
|
+
* finishes a link the service already accepted. A boot whose answer does not prove a refusal keeps the pending secret,
|
|
134
|
+
* which is proved at once when the environment id is known, and otherwise by the next boot. The code and the secret are
|
|
135
|
+
* never printed: the secret is made here, a boot sends only its hash, and a recovery presents it to the token route
|
|
136
|
+
* exactly as `andorra bridge run` does.
|
|
137
|
+
*/
|
|
138
|
+
export async function runCloudBoot(input) {
|
|
139
|
+
const { apiBase, directory, print } = input;
|
|
140
|
+
if (await readBridgeIdentity(directory)) {
|
|
141
|
+
print('Already linked.');
|
|
142
|
+
return 0;
|
|
143
|
+
}
|
|
144
|
+
const pending = await readPending(directory);
|
|
145
|
+
if (complete(pending)) {
|
|
146
|
+
try {
|
|
147
|
+
await finishLink(directory);
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
print(`Could not save this instance's link: ${error instanceof Error ? error.message : String(error)}`);
|
|
151
|
+
return 1;
|
|
152
|
+
}
|
|
153
|
+
print('Linked as Cloud.');
|
|
154
|
+
return 0;
|
|
155
|
+
}
|
|
156
|
+
const { bootCode, environmentId } = bootInput(input.stdin);
|
|
157
|
+
if (bootCode === undefined && pending && environmentId !== undefined)
|
|
158
|
+
return finishAcceptedLink(input, environmentId, pending.secret);
|
|
159
|
+
if (bootCode === undefined) {
|
|
160
|
+
print('Could not link this instance: the start code was missing or malformed.');
|
|
161
|
+
return 1;
|
|
162
|
+
}
|
|
163
|
+
// A link whose answer never arrived is retried with the same secret, in case the service recorded it.
|
|
164
|
+
const secret = pending?.secret ?? randomBytes(32).toString('base64url');
|
|
165
|
+
let linked;
|
|
166
|
+
try {
|
|
167
|
+
await writePending(directory, { secret });
|
|
168
|
+
await ensureWorkFolder(directory, input.workDirectory);
|
|
169
|
+
linked = await cloudBoot(apiBase, bootCode, createHash('sha256').update(secret).digest('hex'));
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
if (!(error instanceof ControlError)) {
|
|
173
|
+
print(`Could not link this instance: ${error instanceof Error ? error.message : String(error)}`);
|
|
174
|
+
return 1;
|
|
175
|
+
}
|
|
176
|
+
if (PROVEN_REFUSALS.has(error.code)) {
|
|
177
|
+
await rm(join(directory, PENDING_IDENTITY), { force: true }).catch(() => undefined);
|
|
178
|
+
}
|
|
179
|
+
else if (environmentId !== undefined && await proveLink(input, environmentId, secret) === undefined) {
|
|
180
|
+
// The service may have recorded the link before its answer was lost; it accepts the secret if so.
|
|
181
|
+
return saveLink(directory, environmentId, secret, print);
|
|
182
|
+
}
|
|
183
|
+
print(REFUSED_BOOT[error.code] ?? `Could not reach Andorra: ${error.message}`);
|
|
184
|
+
return 1;
|
|
185
|
+
}
|
|
186
|
+
return saveLink(directory, linked.environmentId, secret, print);
|
|
187
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { runBridgeHost } from './host.js';
|
|
2
|
+
import { bridgeCommand } from './wording.js';
|
|
3
|
+
export { bridgeCommand };
|
|
4
|
+
export declare function bridgeUsage(launched?: boolean): string;
|
|
5
|
+
export declare const BRIDGE_USAGE: string;
|
|
6
|
+
export declare function cloudApiBase(environment: NodeJS.ProcessEnv): string | undefined;
|
|
7
|
+
/** How often a running bridge checks that this machine is still linked locally (bridge.json unchanged). */
|
|
8
|
+
export declare const IDENTITY_CHECK_MS = 10000;
|
|
9
|
+
export interface BridgeCommandDependencies {
|
|
10
|
+
version?: string;
|
|
11
|
+
apiBase?: string;
|
|
12
|
+
sleep?: (ms: number) => Promise<void>;
|
|
13
|
+
now?: () => number;
|
|
14
|
+
runHost?: typeof runBridgeHost;
|
|
15
|
+
/** Where a Cloud instance's work folder lives; defaults to ~/work. */
|
|
16
|
+
workDirectory?: string;
|
|
17
|
+
/** Whether a person can answer a question on this terminal; defaults to whether stdin is a TTY. */
|
|
18
|
+
interactive?: boolean;
|
|
19
|
+
/** Asks one question on the terminal and resolves with the answer typed. */
|
|
20
|
+
ask?: (question: string) => Promise<string>;
|
|
21
|
+
/** Whether this command runs over SSH, where the person is not at the machine; defaults to SSH_CONNECTION or SSH_TTY being set. */
|
|
22
|
+
overSsh?: boolean;
|
|
23
|
+
/** Opens an address on this computer and says whether it worked; defaults to the platform's opener. */
|
|
24
|
+
open?: (address: string) => Promise<boolean>;
|
|
25
|
+
/** Whether this machine runs under the bridge launcher (no "andorra" command); defaults to ANDORRA_BRIDGE_LAUNCHER being exactly 1. */
|
|
26
|
+
launched?: boolean;
|
|
27
|
+
/** The platform the opener and the stop signals are chosen for; defaults to process.platform. */
|
|
28
|
+
platform?: NodeJS.Platform;
|
|
29
|
+
}
|
|
30
|
+
export declare function runBridgeCommand(args: string[], print: (line: string) => void, dependencies?: BridgeCommandDependencies): Promise<number>;
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
+
import { hostname } from 'node:os';
|
|
3
|
+
import { AUTH_DEPLOYMENT } from '../auth/deployment.js';
|
|
4
|
+
import { changeConfigFile, configDirectory, loadConfig } from '../config.js';
|
|
5
|
+
import { ensureWorkFolder, MAX_BOOT_INPUT_BYTES, runCloudBoot } from './cloud.js';
|
|
6
|
+
import { ControlError, environmentToken, heartbeat, linkPoll, linkStart } from './control.js';
|
|
7
|
+
import { engineStoreFromEnvironment, HOST_RESTART_EXIT_CODE } from './engines.js';
|
|
8
|
+
import { relayUrlFromEnvironment, runBridgeHost } from './host.js';
|
|
9
|
+
import { linkAddress, openCommand, openOnThisComputer, overSshFromEnvironment } from './login-link.js';
|
|
10
|
+
import { clearBridgeIdentity, readBridgeIdentity, shareFolder, writeBridgeIdentity } from './store.js';
|
|
11
|
+
import { bridgeCommand, launchedFromEnvironment } from './wording.js';
|
|
12
|
+
export { bridgeCommand };
|
|
13
|
+
export function bridgeUsage(launched = launchedFromEnvironment()) {
|
|
14
|
+
const rows = [
|
|
15
|
+
['link [--yes] [--no-open]', 'link this machine to your Andorra account'],
|
|
16
|
+
['run', 'serve this machine until stopped (Ctrl+C)'],
|
|
17
|
+
['status', 'show whether this machine is linked'],
|
|
18
|
+
['unlink', 'forget the link on this machine'],
|
|
19
|
+
];
|
|
20
|
+
const width = Math.max(...rows.map(([name]) => bridgeCommand(name, launched).length)) + 2;
|
|
21
|
+
const lines = rows.map(([name, description]) => `${bridgeCommand(name, launched).padEnd(width)}${description}`);
|
|
22
|
+
lines.push(bridgeCommand('folders list|add <path>|remove <id>', launched));
|
|
23
|
+
return `Usage: ${lines.join('\n ')}`;
|
|
24
|
+
}
|
|
25
|
+
export const BRIDGE_USAGE = bridgeUsage(false);
|
|
26
|
+
const POLL_INTERVAL_MS = 3000;
|
|
27
|
+
export function cloudApiBase(environment) {
|
|
28
|
+
const configured = environment.ANDORRA_API_BASE_URL?.trim();
|
|
29
|
+
return configured?.startsWith('https://') ? configured : undefined;
|
|
30
|
+
}
|
|
31
|
+
/** How often a running bridge checks that this machine is still linked locally (bridge.json unchanged). */
|
|
32
|
+
export const IDENTITY_CHECK_MS = 10_000;
|
|
33
|
+
const MAX_NAME_LENGTH = 64;
|
|
34
|
+
const expired = (launched) => `That code expired. Run "${bridgeCommand('link', launched)}" again.`;
|
|
35
|
+
const refused = (launched) => `Not linked. If that was not your account, nothing on this machine was shared. Ask the account owner to remove the machine in Settings → Environments, then run "${bridgeCommand('link', launched)}" again.`;
|
|
36
|
+
const noTerminal = (launched) => `Not linked: there is no terminal to confirm the account in. If that is your account, run "${bridgeCommand('link --yes', launched)}" again.`;
|
|
37
|
+
function message(error) { return error instanceof Error ? error.message : String(error); }
|
|
38
|
+
function defaultSleep(ms) {
|
|
39
|
+
return new Promise(resolveSleep => setTimeout(resolveSleep, ms));
|
|
40
|
+
}
|
|
41
|
+
async function askOnTerminal(question) {
|
|
42
|
+
const { createInterface } = await import('node:readline/promises');
|
|
43
|
+
const terminal = createInterface({ input: process.stdin, output: process.stdout });
|
|
44
|
+
try {
|
|
45
|
+
return await terminal.question(`${question} `);
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
terminal.close();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** At most MAX_BOOT_INPUT_BYTES + 1 bytes of stdin: enough to tell a start code from something that is not one. */
|
|
52
|
+
async function readBoundedStdin() {
|
|
53
|
+
const chunks = [];
|
|
54
|
+
let size = 0;
|
|
55
|
+
for await (const chunk of process.stdin) {
|
|
56
|
+
const bytes = typeof chunk === 'string' ? Buffer.from(chunk) : chunk;
|
|
57
|
+
chunks.push(bytes);
|
|
58
|
+
size += bytes.length;
|
|
59
|
+
if (size > MAX_BOOT_INPUT_BYTES)
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
return Buffer.concat(chunks).subarray(0, MAX_BOOT_INPUT_BYTES + 1).toString('utf8');
|
|
63
|
+
}
|
|
64
|
+
async function offerLoginLink(address, print, skipOpen, interactive, dependencies) {
|
|
65
|
+
const overSsh = dependencies.overSsh ?? overSshFromEnvironment(process.env);
|
|
66
|
+
const platform = dependencies.platform ?? process.platform;
|
|
67
|
+
// A script, container or service has nobody to show a window to, so only a person at a terminal gets an open attempt,
|
|
68
|
+
// and only where the installed desktop app handles the link (Windows and macOS; the Linux build registers no scheme).
|
|
69
|
+
const appHandlesLink = openCommand(platform, address) !== undefined;
|
|
70
|
+
if (!overSsh && !skipOpen && interactive && appHandlesLink) {
|
|
71
|
+
const opened = await (dependencies.open ?? openOnThisComputer)(address).catch(() => false);
|
|
72
|
+
if (opened) {
|
|
73
|
+
print('Opened Andorra Code on this computer to fill it in.');
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
print('Or open this link on your computer to fill it in:');
|
|
78
|
+
print(address);
|
|
79
|
+
}
|
|
80
|
+
async function link(args, print, apiBase, dependencies) {
|
|
81
|
+
const launched = dependencies.launched ?? launchedFromEnvironment();
|
|
82
|
+
if (args.some(arg => arg !== '--yes' && arg !== '--no-open')) {
|
|
83
|
+
print(bridgeUsage(launched));
|
|
84
|
+
return 1;
|
|
85
|
+
}
|
|
86
|
+
const assumeYes = args.includes('--yes');
|
|
87
|
+
const skipOpen = args.includes('--no-open');
|
|
88
|
+
const sleep = dependencies.sleep ?? defaultSleep;
|
|
89
|
+
const now = dependencies.now ?? Date.now;
|
|
90
|
+
const directory = configDirectory();
|
|
91
|
+
const existing = await readBridgeIdentity(directory);
|
|
92
|
+
if (existing) {
|
|
93
|
+
print(`This machine is already linked as ${existing.name}. Run "${bridgeCommand('unlink', launched)}" first.`);
|
|
94
|
+
return 1;
|
|
95
|
+
}
|
|
96
|
+
const interactive = dependencies.interactive ?? process.stdin.isTTY === true;
|
|
97
|
+
// Without a terminal the account cannot be confirmed, so no code is issued for anyone to claim.
|
|
98
|
+
if (!assumeYes && !interactive) {
|
|
99
|
+
print(noTerminal(launched));
|
|
100
|
+
return 1;
|
|
101
|
+
}
|
|
102
|
+
// The secret is made here and never leaves this machine: the service gets only its hash. It stays in memory until
|
|
103
|
+
// the person confirms the account, and is written to bridge.json only then.
|
|
104
|
+
const secret = randomBytes(32).toString('base64url');
|
|
105
|
+
try {
|
|
106
|
+
const started = await linkStart(apiBase, createHash('sha256').update(secret).digest('hex'));
|
|
107
|
+
const address = linkAddress(started.code);
|
|
108
|
+
print('Enter this code in Andorra Code under Settings → Environments → Add a machine:');
|
|
109
|
+
print(started.code);
|
|
110
|
+
await offerLoginLink(address, print, skipOpen, interactive, dependencies);
|
|
111
|
+
const expiresAt = Date.parse(started.expiresAt);
|
|
112
|
+
for (;;) {
|
|
113
|
+
try {
|
|
114
|
+
const polled = await linkPoll(apiBase, started.pollToken);
|
|
115
|
+
if (polled.status === 'linked') {
|
|
116
|
+
// Whoever typed the code owns the machine from now on, so the person here confirms that account first.
|
|
117
|
+
print(polled.account
|
|
118
|
+
? `This machine was claimed by the Andorra account ${polled.account}.`
|
|
119
|
+
: 'This machine was claimed by an Andorra account whose address could not be read.');
|
|
120
|
+
if (!assumeYes) {
|
|
121
|
+
if (!interactive) {
|
|
122
|
+
print(noTerminal(launched));
|
|
123
|
+
return 1;
|
|
124
|
+
}
|
|
125
|
+
const answer = (await (dependencies.ask ?? askOnTerminal)('Link it to that account? [y/N]')).trim().toLowerCase();
|
|
126
|
+
if (answer !== 'y' && answer !== 'yes') {
|
|
127
|
+
print(refused(launched));
|
|
128
|
+
return 1;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const linkedName = polled.name.trim().slice(0, MAX_NAME_LENGTH) || hostname().slice(0, MAX_NAME_LENGTH);
|
|
132
|
+
await writeBridgeIdentity(directory, { environmentId: polled.environmentId, name: linkedName, secret, linkedAt: new Date(now()).toISOString() });
|
|
133
|
+
print(`Linked as ${linkedName}. Run "${bridgeCommand('run', launched)}" to serve this machine.`);
|
|
134
|
+
return 0;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
if (!(error instanceof ControlError) || error.code === 'code_expired' || error.code === 'unlinked')
|
|
139
|
+
throw error;
|
|
140
|
+
// A transient network or server error: keep polling until the code itself runs out.
|
|
141
|
+
}
|
|
142
|
+
if (!Number.isNaN(expiresAt) && now() >= expiresAt)
|
|
143
|
+
throw new ControlError('code_expired', expired(launched));
|
|
144
|
+
await sleep(POLL_INTERVAL_MS);
|
|
145
|
+
if (!Number.isNaN(expiresAt) && now() >= expiresAt)
|
|
146
|
+
throw new ControlError('code_expired', expired(launched));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
if (error instanceof ControlError && error.code === 'code_expired')
|
|
151
|
+
print(expired(launched));
|
|
152
|
+
else if (error instanceof ControlError)
|
|
153
|
+
print(`Could not reach Andorra: ${error.message}`);
|
|
154
|
+
else
|
|
155
|
+
print(`Could not link this machine: ${message(error)}`);
|
|
156
|
+
return 1;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// SIGBREAK is Ctrl+Break on a Windows console; Node ends the process at once when nothing listens for it.
|
|
160
|
+
// On Windows closing the console ends every process attached to it, installs included, so SIGHUP keeps its default there.
|
|
161
|
+
function stopSignals(platform) {
|
|
162
|
+
return platform === 'win32' ? ['SIGINT', 'SIGTERM', 'SIGBREAK'] : ['SIGINT', 'SIGTERM', 'SIGBREAK', 'SIGHUP', 'SIGQUIT'];
|
|
163
|
+
}
|
|
164
|
+
// A hang-up or termination often arrives twice for one stop (a closed terminal signals the launcher's whole group, and the
|
|
165
|
+
// launcher passes it on as well), so those stay caught until shutdown ends; a second Ctrl+C still ends the process at once.
|
|
166
|
+
const REPEATED_DELIVERIES = new Set(['SIGTERM', 'SIGHUP', 'SIGQUIT']);
|
|
167
|
+
/** Resolves on the first stop signal or on `signal` aborting; `release` stops catching the signals still held. */
|
|
168
|
+
function waitForStop(signal, platform) {
|
|
169
|
+
const names = stopSignals(platform);
|
|
170
|
+
const ignore = () => undefined;
|
|
171
|
+
let resolveStop = () => undefined;
|
|
172
|
+
const stopped = new Promise(resolvePromise => { resolveStop = resolvePromise; });
|
|
173
|
+
let stopping = false;
|
|
174
|
+
const stop = () => {
|
|
175
|
+
if (stopping)
|
|
176
|
+
return;
|
|
177
|
+
stopping = true;
|
|
178
|
+
for (const name of names) {
|
|
179
|
+
process.removeListener(name, stop);
|
|
180
|
+
if (REPEATED_DELIVERIES.has(name))
|
|
181
|
+
process.on(name, ignore);
|
|
182
|
+
}
|
|
183
|
+
resolveStop();
|
|
184
|
+
};
|
|
185
|
+
for (const name of names)
|
|
186
|
+
process.on(name, stop);
|
|
187
|
+
signal.addEventListener('abort', stop, { once: true });
|
|
188
|
+
return { stopped, release: () => { for (const name of names)
|
|
189
|
+
process.removeListener(name, ignore); } };
|
|
190
|
+
}
|
|
191
|
+
/** Whether a process exists; a process this user may not signal still exists. */
|
|
192
|
+
function processExists(pid) {
|
|
193
|
+
try {
|
|
194
|
+
process.kill(pid, 0);
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
return error.code === 'EPERM';
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
async function run(print, apiBase, dependencies) {
|
|
202
|
+
const launched = dependencies.launched ?? launchedFromEnvironment();
|
|
203
|
+
const directory = configDirectory();
|
|
204
|
+
const identity = await readBridgeIdentity(directory);
|
|
205
|
+
if (!identity) {
|
|
206
|
+
print(`This machine is not linked. Run "${bridgeCommand('link', launched)}" first.`);
|
|
207
|
+
return 1;
|
|
208
|
+
}
|
|
209
|
+
const controller = new AbortController();
|
|
210
|
+
const stopWatch = waitForStop(controller.signal, dependencies.platform ?? process.platform);
|
|
211
|
+
const stopWatcher = stopWatch.stopped.then(() => controller.abort());
|
|
212
|
+
// "bridge unlink" in another terminal removes bridge.json; a relink replaces it. Either way this process must stop
|
|
213
|
+
// serving with the identity it started with, so it checks the file every IDENTITY_CHECK_MS and before every token.
|
|
214
|
+
let unlinkedHere = false;
|
|
215
|
+
const stillLinked = async () => {
|
|
216
|
+
const current = await readBridgeIdentity(directory);
|
|
217
|
+
const same = current?.environmentId === identity.environmentId && current.secret === identity.secret;
|
|
218
|
+
if (!same && !unlinkedHere) {
|
|
219
|
+
unlinkedHere = true;
|
|
220
|
+
controller.abort();
|
|
221
|
+
}
|
|
222
|
+
return same;
|
|
223
|
+
};
|
|
224
|
+
const identityCheck = setInterval(() => { void stillLinked(); }, IDENTITY_CHECK_MS);
|
|
225
|
+
identityCheck.unref?.();
|
|
226
|
+
print(`Serving as ${identity.name}. Press Ctrl+C to stop.`);
|
|
227
|
+
const version = dependencies.version ?? '0.0.0';
|
|
228
|
+
// Under the bridge launcher tasks run on the engine version of their desktop; a plain "andorra bridge run" has no store.
|
|
229
|
+
const engines = engineStoreFromEnvironment(process.env, { hostVersion: version });
|
|
230
|
+
const launcherPid = Number(process.env.ANDORRA_BRIDGE_LAUNCHER_PID);
|
|
231
|
+
const launcherAlive = Number.isInteger(launcherPid) && launcherPid > 0 ? () => processExists(launcherPid) : undefined;
|
|
232
|
+
try {
|
|
233
|
+
if (process.env.ANDORRA_CLOUD === '1') {
|
|
234
|
+
await ensureWorkFolder(directory, dependencies.workDirectory).catch(error => print(`Could not share the work folder: ${message(error)}`));
|
|
235
|
+
}
|
|
236
|
+
const outcome = await (dependencies.runHost ?? runBridgeHost)({
|
|
237
|
+
directory,
|
|
238
|
+
relayUrl: relayUrlFromEnvironment(),
|
|
239
|
+
version,
|
|
240
|
+
token: async () => {
|
|
241
|
+
if (!(await stillLinked()))
|
|
242
|
+
throw new Error('This machine was unlinked.');
|
|
243
|
+
return environmentToken(apiBase, identity.environmentId, identity.secret);
|
|
244
|
+
},
|
|
245
|
+
signal: controller.signal,
|
|
246
|
+
log: print,
|
|
247
|
+
...(process.env.ANDORRA_CLOUD === '1' ? { heartbeat: activity => heartbeat(apiBase, identity.environmentId, identity.secret, activity) } : {}),
|
|
248
|
+
...(engines ? { engines } : {}),
|
|
249
|
+
...(launcherAlive ? { launcherAlive } : {}),
|
|
250
|
+
});
|
|
251
|
+
if (unlinkedHere) {
|
|
252
|
+
print('This machine was unlinked. Stopped serving.');
|
|
253
|
+
return 0;
|
|
254
|
+
}
|
|
255
|
+
if (outcome === 'unlinked') {
|
|
256
|
+
await clearBridgeIdentity(directory);
|
|
257
|
+
print(`This machine was removed from your account. Run "${bridgeCommand('link', launched)}" to link it again.`);
|
|
258
|
+
return 1;
|
|
259
|
+
}
|
|
260
|
+
if (outcome === 'restart') {
|
|
261
|
+
print('Restarting with a newer Andorra Code.');
|
|
262
|
+
return HOST_RESTART_EXIT_CODE;
|
|
263
|
+
}
|
|
264
|
+
print('Stopped serving.');
|
|
265
|
+
return 0;
|
|
266
|
+
}
|
|
267
|
+
finally {
|
|
268
|
+
await engines?.close();
|
|
269
|
+
clearInterval(identityCheck);
|
|
270
|
+
controller.abort();
|
|
271
|
+
await stopWatcher;
|
|
272
|
+
stopWatch.release();
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
async function folders(args, print, launched) {
|
|
276
|
+
const directory = configDirectory();
|
|
277
|
+
const [action, value] = args;
|
|
278
|
+
if (action === 'list') {
|
|
279
|
+
const list = (await loadConfig(directory)).bridge?.folders ?? [];
|
|
280
|
+
if (!list.length)
|
|
281
|
+
print('No folders shared.');
|
|
282
|
+
for (const folder of list)
|
|
283
|
+
print(`${folder.id} ${folder.path}${folder.label ? ` (${folder.label})` : ''}`);
|
|
284
|
+
return 0;
|
|
285
|
+
}
|
|
286
|
+
if (action === 'add' && value) {
|
|
287
|
+
try {
|
|
288
|
+
const stored = await shareFolder(directory, value);
|
|
289
|
+
print(`Sharing ${stored.path} (id ${stored.id}).`);
|
|
290
|
+
return 0;
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
print(message(error));
|
|
294
|
+
return 1;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (action === 'remove' && value) {
|
|
298
|
+
let removed = false;
|
|
299
|
+
await changeConfigFile(directory, config => {
|
|
300
|
+
const current = config.bridge?.folders ?? [];
|
|
301
|
+
const kept = current.filter(folder => folder.id !== value);
|
|
302
|
+
removed = kept.length !== current.length;
|
|
303
|
+
return removed ? { ...config, bridge: { ...config.bridge, folders: kept } } : config;
|
|
304
|
+
});
|
|
305
|
+
print(removed ? `Stopped sharing folder ${value}.` : `No folder with id ${value}.`);
|
|
306
|
+
return removed ? 0 : 1;
|
|
307
|
+
}
|
|
308
|
+
print(bridgeUsage(launched));
|
|
309
|
+
return 1;
|
|
310
|
+
}
|
|
311
|
+
export async function runBridgeCommand(args, print, dependencies = {}) {
|
|
312
|
+
const [command, ...rest] = args;
|
|
313
|
+
const apiBase = dependencies.apiBase ?? cloudApiBase(process.env) ?? AUTH_DEPLOYMENT.apiBaseUrl;
|
|
314
|
+
switch (command) {
|
|
315
|
+
case 'cloud-boot':
|
|
316
|
+
return runCloudBoot({ apiBase, directory: configDirectory(), stdin: await readBoundedStdin(), print });
|
|
317
|
+
case 'link':
|
|
318
|
+
return link(rest, print, apiBase, dependencies);
|
|
319
|
+
case 'run':
|
|
320
|
+
return run(print, apiBase, dependencies);
|
|
321
|
+
case 'status': {
|
|
322
|
+
const identity = await readBridgeIdentity(configDirectory());
|
|
323
|
+
print(identity ? `Linked as ${identity.name} (${identity.environmentId}).` : 'Not linked.');
|
|
324
|
+
return 0;
|
|
325
|
+
}
|
|
326
|
+
case 'unlink': {
|
|
327
|
+
const directory = configDirectory();
|
|
328
|
+
if (!(await readBridgeIdentity(directory))) {
|
|
329
|
+
print('Not linked.');
|
|
330
|
+
return 0;
|
|
331
|
+
}
|
|
332
|
+
await clearBridgeIdentity(directory);
|
|
333
|
+
print('Unlinked this machine. It can also be removed from your account under Settings → Environments in Andorra Code.');
|
|
334
|
+
return 0;
|
|
335
|
+
}
|
|
336
|
+
case 'folders':
|
|
337
|
+
return folders(rest, print, dependencies.launched ?? launchedFromEnvironment());
|
|
338
|
+
case undefined:
|
|
339
|
+
case '--help':
|
|
340
|
+
case '-h':
|
|
341
|
+
print(bridgeUsage(dependencies.launched ?? launchedFromEnvironment()));
|
|
342
|
+
return 0;
|
|
343
|
+
default:
|
|
344
|
+
print(bridgeUsage(dependencies.launched ?? launchedFromEnvironment()));
|
|
345
|
+
return 1;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export type ControlErrorCode = 'code_expired' | 'code_unknown' | 'code_used' | 'boot_expired' | 'boot_used' | 'boot_unknown' | 'environment_limit' | 'invalid_name' | 'not_found' | 'unauthorized' | 'cloud_not_enabled' | 'cloud_environment' | 'invalid_version' | 'cloud_unavailable' | 'stopping' | 'no_capacity' | 'unlinked' | 'network' | 'server';
|
|
2
|
+
export declare class ControlError extends Error {
|
|
3
|
+
readonly code: ControlErrorCode;
|
|
4
|
+
constructor(code: ControlErrorCode, message: string);
|
|
5
|
+
}
|
|
6
|
+
export interface LinkStart {
|
|
7
|
+
code: string;
|
|
8
|
+
pollToken: string;
|
|
9
|
+
expiresAt: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* `account` is the claiming account's masked email address, or '' when the service could not read it. No secret comes
|
|
13
|
+
* back: the machine made its own and sent only its hash with link/start, so the answer can be asked for again.
|
|
14
|
+
*/
|
|
15
|
+
export type LinkPoll = {
|
|
16
|
+
status: 'pending';
|
|
17
|
+
} | {
|
|
18
|
+
status: 'linked';
|
|
19
|
+
environmentId: string;
|
|
20
|
+
name: string;
|
|
21
|
+
account: string;
|
|
22
|
+
};
|
|
23
|
+
export interface EnvironmentToken {
|
|
24
|
+
token: string;
|
|
25
|
+
expiresAt: string;
|
|
26
|
+
}
|
|
27
|
+
export interface EnvironmentRecord {
|
|
28
|
+
id: string;
|
|
29
|
+
name: string;
|
|
30
|
+
kind: 'machine' | 'cloud';
|
|
31
|
+
createdAt: string;
|
|
32
|
+
}
|
|
33
|
+
export type CloudState = 'none' | 'starting' | 'running' | 'stopping' | 'stopped';
|
|
34
|
+
export interface CloudStatus {
|
|
35
|
+
state: CloudState;
|
|
36
|
+
enabled: boolean;
|
|
37
|
+
environmentId?: string;
|
|
38
|
+
label?: string;
|
|
39
|
+
version?: string;
|
|
40
|
+
step?: 'machine' | 'bridge';
|
|
41
|
+
deleteAt?: string;
|
|
42
|
+
error?: 'boot_timeout' | 'no_capacity';
|
|
43
|
+
/** Why a stopping or stopped instance was stopped. */
|
|
44
|
+
stopReason?: CloudStopReason;
|
|
45
|
+
}
|
|
46
|
+
export type CloudStopReason = 'idle' | 'user' | 'boot_timeout';
|
|
47
|
+
/** Starts a link for a machine whose secret hashes to `secretHash` (SHA-256, hex). */
|
|
48
|
+
export declare function linkStart(base: string, secretHash: string): Promise<LinkStart>;
|
|
49
|
+
export declare function linkPoll(base: string, pollToken: string): Promise<LinkPoll>;
|
|
50
|
+
export declare function environmentToken(base: string, id: string, secret: string): Promise<EnvironmentToken>;
|
|
51
|
+
/** Links a Cloud instance: the boot code proves the caller, and only the hash of the instance's own secret is sent. */
|
|
52
|
+
export declare function cloudBoot(base: string, bootCode: string, secretHash: string): Promise<{
|
|
53
|
+
environmentId: string;
|
|
54
|
+
name: string;
|
|
55
|
+
}>;
|
|
56
|
+
export declare function heartbeat(base: string, environmentId: string, secret: string, activity: {
|
|
57
|
+
running: number;
|
|
58
|
+
attached: number;
|
|
59
|
+
}): Promise<void>;
|
|
60
|
+
export declare function listEnvironments(base: string, accessToken: string): Promise<EnvironmentRecord[]>;
|
|
61
|
+
export declare function claimEnvironment(base: string, accessToken: string, code: string, name: string): Promise<EnvironmentRecord>;
|
|
62
|
+
export declare function renameEnvironment(base: string, accessToken: string, id: string, name: string): Promise<EnvironmentRecord>;
|
|
63
|
+
export declare function removeEnvironment(base: string, accessToken: string, id: string): Promise<void>;
|
|
64
|
+
export declare function cloudGet(base: string, accessToken: string): Promise<CloudStatus>;
|
|
65
|
+
export declare function cloudStart(base: string, accessToken: string, version: string): Promise<CloudStatus>;
|
|
66
|
+
export declare function cloudStop(base: string, accessToken: string): Promise<CloudStatus>;
|
|
67
|
+
/** Deletes the account's Cloud; with `environmentId`, only while that is still the account's Cloud (another answers none). */
|
|
68
|
+
export declare function cloudDelete(base: string, accessToken: string, environmentId?: string): Promise<CloudStatus>;
|