@testsprite/testsprite-cli 0.7.0 → 0.9.0
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/README.md +62 -45
- package/dist/commands/agent.d.ts +3 -0
- package/dist/commands/agent.js +79 -10
- package/dist/commands/ci.d.ts +88 -0
- package/dist/commands/ci.js +432 -0
- package/dist/commands/doctor.js +100 -1
- package/dist/commands/init.d.ts +45 -2
- package/dist/commands/init.js +198 -29
- package/dist/commands/project.d.ts +78 -0
- package/dist/commands/project.js +439 -5
- package/dist/commands/test.d.ts +193 -4
- package/dist/commands/test.js +1948 -398
- package/dist/commands/testlist.js +15 -9
- package/dist/commands/tunnel.d.ts +72 -0
- package/dist/commands/tunnel.js +367 -0
- package/dist/index.js +107 -38
- package/dist/lib/agent-detect.d.ts +53 -0
- package/dist/lib/agent-detect.js +198 -0
- package/dist/lib/dry-run/samples.d.ts +9 -0
- package/dist/lib/dry-run/samples.js +116 -0
- package/dist/lib/gh-output.d.ts +7 -0
- package/dist/lib/gh-output.js +50 -7
- package/dist/lib/http.d.ts +104 -0
- package/dist/lib/http.js +126 -7
- package/dist/lib/interrupt.d.ts +38 -13
- package/dist/lib/interrupt.js +130 -15
- package/dist/lib/local-target.d.ts +95 -0
- package/dist/lib/local-target.js +184 -0
- package/dist/lib/plan-poll.d.ts +171 -0
- package/dist/lib/plan-poll.js +442 -0
- package/dist/lib/plans.types.d.ts +130 -0
- package/dist/lib/plans.types.js +31 -0
- package/dist/lib/poll-support.d.ts +26 -0
- package/dist/lib/poll-support.js +65 -0
- package/dist/lib/poll.d.ts +1 -0
- package/dist/lib/poll.js +2 -40
- package/dist/lib/response-schemas.d.ts +13 -0
- package/dist/lib/response-schemas.js +46 -0
- package/dist/lib/run-progress.d.ts +41 -0
- package/dist/lib/run-progress.js +56 -0
- package/dist/lib/runs.types.d.ts +50 -0
- package/dist/lib/skill-nudge.d.ts +13 -5
- package/dist/lib/skill-nudge.js +40 -14
- package/dist/lib/target-url-preflight.js +3 -3
- package/dist/lib/target-url.d.ts +54 -8
- package/dist/lib/target-url.js +274 -68
- package/dist/lib/telemetry.d.ts +11 -0
- package/dist/lib/telemetry.js +1 -0
- package/dist/lib/ticker.d.ts +8 -0
- package/dist/lib/ticker.js +3 -0
- package/dist/lib/tunnel-session.d.ts +138 -0
- package/dist/lib/tunnel-session.js +249 -0
- package/dist/lib/tunnel.types.d.ts +54 -0
- package/dist/lib/tunnel.types.js +8 -0
- package/dist/vendor/tunnel-client/client.d.ts +82 -0
- package/dist/vendor/tunnel-client/client.js +924 -0
- package/dist/vendor/tunnel-client/config.d.ts +49 -0
- package/dist/vendor/tunnel-client/config.js +50 -0
- package/dist/vendor/tunnel-client/index.d.ts +9 -0
- package/dist/vendor/tunnel-client/index.js +9 -0
- package/dist/vendor/tunnel-client/lodash-lite.d.ts +14 -0
- package/dist/vendor/tunnel-client/lodash-lite.js +24 -0
- package/dist/vendor/tunnel-client/protocol.d.ts +60 -0
- package/dist/vendor/tunnel-client/protocol.js +143 -0
- package/dist/vendor/tunnel-client/types.d.ts +101 -0
- package/dist/vendor/tunnel-client/types.js +10 -0
- package/dist/vendor/tunnel-client/ws-compat.d.ts +206 -0
- package/dist/vendor/tunnel-client/ws-compat.js +316 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -1
- package/skills/testsprite-onboard.skill.md +60 -4
- package/skills/testsprite-verify.codex.md +22 -16
- package/skills/testsprite-verify.skill.md +27 -18
package/dist/lib/ticker.d.ts
CHANGED
|
@@ -25,6 +25,14 @@ export interface Ticker {
|
|
|
25
25
|
* doesn't run into the result block). No-op on non-TTY.
|
|
26
26
|
*/
|
|
27
27
|
finalize(line?: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* True only when `update()` redraws the SAME terminal line (TTY, ANSI
|
|
30
|
+
* allowed). False on a non-TTY (updates are no-ops) and under NO_COLOR
|
|
31
|
+
* (every update prints a new line). The one signal a between-polls
|
|
32
|
+
* refresher may key on — re-deriving it from process globals elsewhere
|
|
33
|
+
* is how the two decisions drift apart.
|
|
34
|
+
*/
|
|
35
|
+
readonly redrawsInPlace: boolean;
|
|
28
36
|
}
|
|
29
37
|
/**
|
|
30
38
|
* Returns true when NO_COLOR is present in the environment and is not
|
package/dist/lib/ticker.js
CHANGED
|
@@ -49,6 +49,7 @@ export function createTicker(stderrWrite, isTTY, stderrRaw, noColor) {
|
|
|
49
49
|
if (!tty) {
|
|
50
50
|
// Non-TTY: completely silent.
|
|
51
51
|
return {
|
|
52
|
+
redrawsInPlace: false,
|
|
52
53
|
update: () => undefined,
|
|
53
54
|
finalize: () => undefined,
|
|
54
55
|
};
|
|
@@ -56,6 +57,7 @@ export function createTicker(stderrWrite, isTTY, stderrRaw, noColor) {
|
|
|
56
57
|
if (suppressAnsi) {
|
|
57
58
|
// TTY but NO_COLOR: emit plain-text lines without ANSI escape sequences.
|
|
58
59
|
return {
|
|
60
|
+
redrawsInPlace: false,
|
|
59
61
|
update(line) {
|
|
60
62
|
const stamped = `${new Date().toISOString()} ${line}`;
|
|
61
63
|
stderrWrite(stamped);
|
|
@@ -72,6 +74,7 @@ export function createTicker(stderrWrite, isTTY, stderrRaw, noColor) {
|
|
|
72
74
|
};
|
|
73
75
|
}
|
|
74
76
|
return {
|
|
77
|
+
redrawsInPlace: true,
|
|
75
78
|
update(line) {
|
|
76
79
|
// ANSI ESC[2K clears the entire line; \r moves to column 0.
|
|
77
80
|
const stamped = `${new Date().toISOString()} ${line}`;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lifecycle of one `--local` tunnel: mint → connect → (run) → tear down.
|
|
3
|
+
*
|
|
4
|
+
* The credential handling is the point of this module, so it is stated once
|
|
5
|
+
* here rather than repeated at every line that touches it. The secret returned
|
|
6
|
+
* by `POST /api/cli/v1/tunnel` lives in exactly one place — a local in this
|
|
7
|
+
* function — for the lifetime of the process. It is never written to disk,
|
|
8
|
+
* never placed in `argv`, never exported into a child's environment, never
|
|
9
|
+
* logged (the log sink is a closure this module owns), and never sent on the
|
|
10
|
+
* run trigger: the trigger body carries only `tunnelClientId`, and the backend
|
|
11
|
+
* builds the proxy string from its own configuration. `config.json`
|
|
12
|
+
* credentials are a settled no in this codebase, and a tunnel secret is worse
|
|
13
|
+
* than an API key — it authorises an inbound network path into the holder's
|
|
14
|
+
* machine.
|
|
15
|
+
*
|
|
16
|
+
* ## Why there is no remint-and-reconnect
|
|
17
|
+
*
|
|
18
|
+
* The design ledger's D3 said a terminal `1008/AUTH_FAILED` mid-run should
|
|
19
|
+
* mint a fresh client, reconnect, and keep polling — "the in-flight step still
|
|
20
|
+
* fails" being the only accepted cost. That is not achievable, and the reason
|
|
21
|
+
* is structural rather than a matter of effort:
|
|
22
|
+
*
|
|
23
|
+
* - The proxy string the execution Lambda uses is
|
|
24
|
+
* `http://<clientId>:<secret>@<proxy-host>:<port>`, built ONCE at trigger
|
|
25
|
+
* time and baked into the Lambda payload (the invoke is async — there is
|
|
26
|
+
* no channel to hand it a new one).
|
|
27
|
+
* - The tunnel's proxy plane authenticates every request against that
|
|
28
|
+
* `(clientId, secret)` pair and looks the client session up **by that
|
|
29
|
+
* clientId** (`src/proxy/runtime.rs`: `extract_proxy_credentials` →
|
|
30
|
+
* `registry.authenticate(client_id, &secret)` → `registry.get(client_id)`).
|
|
31
|
+
* - The facade is mint-only by design — there is no route that re-registers
|
|
32
|
+
* an existing id, and adding one would be the credential-confirmation
|
|
33
|
+
* oracle the MCP surface already has.
|
|
34
|
+
*
|
|
35
|
+
* So a reminted client necessarily has a NEW id that the in-flight run's proxy
|
|
36
|
+
* string can never name. It would connect a tunnel nothing routes to, while
|
|
37
|
+
* the run kept burning credits failing every remaining step. What this module
|
|
38
|
+
* does instead is honest failure: record the loss, let the caller stop the run
|
|
39
|
+
* and say why. See `openTunnelSession`'s `onFatal`.
|
|
40
|
+
*/
|
|
41
|
+
import { ApiError } from './errors.js';
|
|
42
|
+
import type { LogLevel, TunnelClientOptions } from '../vendor/tunnel-client/index.js';
|
|
43
|
+
import type { TunnelMintResponse } from './tunnel.types.js';
|
|
44
|
+
/**
|
|
45
|
+
* How long to wait for the control plane's first successful connection.
|
|
46
|
+
*
|
|
47
|
+
* The vendored client resolves `start()` only on the first
|
|
48
|
+
* `control-connected` event, and its control loop retries every non-auth
|
|
49
|
+
* failure forever. Without a deadline, `--local` against an unreachable
|
|
50
|
+
* control plane hangs with no output at all — the worst possible shape for a
|
|
51
|
+
* command an agent might be driving unattended.
|
|
52
|
+
*/
|
|
53
|
+
export declare const DEFAULT_TUNNEL_CONNECT_TIMEOUT_MS = 20000;
|
|
54
|
+
/** Maximum time teardown lets a wedged client delay credential deletion. */
|
|
55
|
+
export declare const TUNNEL_CLIENT_STOP_TIMEOUT_MS = 2000;
|
|
56
|
+
/** Why a tunnel stopped being usable for the run that was attached to it. */
|
|
57
|
+
export type TunnelFatalReason =
|
|
58
|
+
/** The server refused or dropped a client WE minted and connected. */
|
|
59
|
+
'auth-failed'
|
|
60
|
+
/**
|
|
61
|
+
* A BORROWED client (`--tunnel-client`) is no longer registered. Different
|
|
62
|
+
* cause and different remedy from `auth-failed`: nobody disconnected us,
|
|
63
|
+
* the process that minted the tunnel stopped owning it.
|
|
64
|
+
*/
|
|
65
|
+
| 'owner-gone';
|
|
66
|
+
export interface TunnelSession {
|
|
67
|
+
readonly clientId: string;
|
|
68
|
+
readonly expiresAt: string;
|
|
69
|
+
/** True when this session attached to a client someone else minted. */
|
|
70
|
+
readonly adopted: boolean;
|
|
71
|
+
/** The fatal reason, once one has occurred. `undefined` while healthy. */
|
|
72
|
+
fatalReason(): TunnelFatalReason | undefined;
|
|
73
|
+
/** Give the client a bounded stop window, then delete our binding. Idempotent. */
|
|
74
|
+
close(): Promise<void>;
|
|
75
|
+
}
|
|
76
|
+
/** The slice of the vendored client this module drives. */
|
|
77
|
+
export interface TunnelClientHandle {
|
|
78
|
+
start(): Promise<void>;
|
|
79
|
+
stop(): Promise<void>;
|
|
80
|
+
}
|
|
81
|
+
export interface OpenTunnelSessionOptions {
|
|
82
|
+
/** stderr sink. Every line this module emits goes here, never to stdout. */
|
|
83
|
+
log: (line: string) => void;
|
|
84
|
+
/**
|
|
85
|
+
* Verbosity floor forwarded to the vendored client. Default `'error'` —
|
|
86
|
+
* at `'info'` the client narrates every stream open and close, which on a
|
|
87
|
+
* page pulling dozens of subresources buries the run's own output.
|
|
88
|
+
*/
|
|
89
|
+
logLevel?: LogLevel;
|
|
90
|
+
/** Requested binding lifetime. Server clamps and defaults. */
|
|
91
|
+
ttlSeconds?: number;
|
|
92
|
+
/** See {@link DEFAULT_TUNNEL_CONNECT_TIMEOUT_MS}. */
|
|
93
|
+
connectTimeoutMs?: number;
|
|
94
|
+
/** Called once, the first time the tunnel becomes unusable for this run. */
|
|
95
|
+
onFatal?: (reason: TunnelFatalReason) => void;
|
|
96
|
+
/**
|
|
97
|
+
* Attach to a client someone else minted (`testsprite tunnel start` in
|
|
98
|
+
* another terminal) instead of minting one. No secret is available, so
|
|
99
|
+
* nothing is connected here and nothing is deleted at teardown — the
|
|
100
|
+
* process that minted it owns both.
|
|
101
|
+
*/
|
|
102
|
+
adopt?: {
|
|
103
|
+
clientId: string;
|
|
104
|
+
expiresAt: string;
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
export interface TunnelSessionDeps {
|
|
108
|
+
mint: (ttlSeconds?: number) => Promise<TunnelMintResponse>;
|
|
109
|
+
/**
|
|
110
|
+
* Delete the binding. MUST be issued through a client whose shutdown signal
|
|
111
|
+
* is not the interrupted one — teardown runs from a `finally` that is often
|
|
112
|
+
* reached *because* of a signal, and a request composed with an
|
|
113
|
+
* already-aborted signal never leaves the process.
|
|
114
|
+
*/
|
|
115
|
+
destroy: (clientId: string) => Promise<void>;
|
|
116
|
+
/** Injectable for tests. Defaults to the vendored `TunnelClient`. */
|
|
117
|
+
createClient?: (options: TunnelClientOptions) => TunnelClientHandle;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Thrown when the tunnel a run depends on dies mid-run. `UNAVAILABLE` (exit
|
|
121
|
+
* 10 rather than a run failure: the test did not fail, the transport under it
|
|
122
|
+
* did. An owned run can retry with a fresh tunnel; a borrowed run must inspect
|
|
123
|
+
* the already-started server-side run before deciding whether to retry.
|
|
124
|
+
*/
|
|
125
|
+
export declare class TunnelLostError extends ApiError {
|
|
126
|
+
constructor(reason: TunnelFatalReason, runId: string);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Mint (or adopt) a tunnel client and connect it.
|
|
130
|
+
*
|
|
131
|
+
* On any failure after the mint, the client gets a bounded chance to stop and
|
|
132
|
+
* the binding is always deleted before the error propagates. A
|
|
133
|
+
* minted-but-unconnected binding is a live credential with nothing watching
|
|
134
|
+
* it, while an un-stopped client can leave ref'd heartbeat/reconnect timers
|
|
135
|
+
* that keep the process alive long after the command has printed its last
|
|
136
|
+
* line. Credential deletion cannot wait indefinitely for those timers.
|
|
137
|
+
*/
|
|
138
|
+
export declare function openTunnelSession(options: OpenTunnelSessionOptions, deps: TunnelSessionDeps): Promise<TunnelSession>;
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lifecycle of one `--local` tunnel: mint → connect → (run) → tear down.
|
|
3
|
+
*
|
|
4
|
+
* The credential handling is the point of this module, so it is stated once
|
|
5
|
+
* here rather than repeated at every line that touches it. The secret returned
|
|
6
|
+
* by `POST /api/cli/v1/tunnel` lives in exactly one place — a local in this
|
|
7
|
+
* function — for the lifetime of the process. It is never written to disk,
|
|
8
|
+
* never placed in `argv`, never exported into a child's environment, never
|
|
9
|
+
* logged (the log sink is a closure this module owns), and never sent on the
|
|
10
|
+
* run trigger: the trigger body carries only `tunnelClientId`, and the backend
|
|
11
|
+
* builds the proxy string from its own configuration. `config.json`
|
|
12
|
+
* credentials are a settled no in this codebase, and a tunnel secret is worse
|
|
13
|
+
* than an API key — it authorises an inbound network path into the holder's
|
|
14
|
+
* machine.
|
|
15
|
+
*
|
|
16
|
+
* ## Why there is no remint-and-reconnect
|
|
17
|
+
*
|
|
18
|
+
* The design ledger's D3 said a terminal `1008/AUTH_FAILED` mid-run should
|
|
19
|
+
* mint a fresh client, reconnect, and keep polling — "the in-flight step still
|
|
20
|
+
* fails" being the only accepted cost. That is not achievable, and the reason
|
|
21
|
+
* is structural rather than a matter of effort:
|
|
22
|
+
*
|
|
23
|
+
* - The proxy string the execution Lambda uses is
|
|
24
|
+
* `http://<clientId>:<secret>@<proxy-host>:<port>`, built ONCE at trigger
|
|
25
|
+
* time and baked into the Lambda payload (the invoke is async — there is
|
|
26
|
+
* no channel to hand it a new one).
|
|
27
|
+
* - The tunnel's proxy plane authenticates every request against that
|
|
28
|
+
* `(clientId, secret)` pair and looks the client session up **by that
|
|
29
|
+
* clientId** (`src/proxy/runtime.rs`: `extract_proxy_credentials` →
|
|
30
|
+
* `registry.authenticate(client_id, &secret)` → `registry.get(client_id)`).
|
|
31
|
+
* - The facade is mint-only by design — there is no route that re-registers
|
|
32
|
+
* an existing id, and adding one would be the credential-confirmation
|
|
33
|
+
* oracle the MCP surface already has.
|
|
34
|
+
*
|
|
35
|
+
* So a reminted client necessarily has a NEW id that the in-flight run's proxy
|
|
36
|
+
* string can never name. It would connect a tunnel nothing routes to, while
|
|
37
|
+
* the run kept burning credits failing every remaining step. What this module
|
|
38
|
+
* does instead is honest failure: record the loss, let the caller stop the run
|
|
39
|
+
* and say why. See `openTunnelSession`'s `onFatal`.
|
|
40
|
+
*/
|
|
41
|
+
import { ApiError } from './errors.js';
|
|
42
|
+
import { ErrCode, TunnelClient } from '../vendor/tunnel-client/index.js';
|
|
43
|
+
/**
|
|
44
|
+
* How long to wait for the control plane's first successful connection.
|
|
45
|
+
*
|
|
46
|
+
* The vendored client resolves `start()` only on the first
|
|
47
|
+
* `control-connected` event, and its control loop retries every non-auth
|
|
48
|
+
* failure forever. Without a deadline, `--local` against an unreachable
|
|
49
|
+
* control plane hangs with no output at all — the worst possible shape for a
|
|
50
|
+
* command an agent might be driving unattended.
|
|
51
|
+
*/
|
|
52
|
+
export const DEFAULT_TUNNEL_CONNECT_TIMEOUT_MS = 20_000;
|
|
53
|
+
/** Maximum time teardown lets a wedged client delay credential deletion. */
|
|
54
|
+
export const TUNNEL_CLIENT_STOP_TIMEOUT_MS = 2_000;
|
|
55
|
+
const TUNNEL_SECRET_REDACTION = '[REDACTED]';
|
|
56
|
+
/**
|
|
57
|
+
* Thrown when the tunnel a run depends on dies mid-run. `UNAVAILABLE` (exit
|
|
58
|
+
* 10 rather than a run failure: the test did not fail, the transport under it
|
|
59
|
+
* did. An owned run can retry with a fresh tunnel; a borrowed run must inspect
|
|
60
|
+
* the already-started server-side run before deciding whether to retry.
|
|
61
|
+
*/
|
|
62
|
+
export class TunnelLostError extends ApiError {
|
|
63
|
+
constructor(reason, runId) {
|
|
64
|
+
// Two different things go wrong here and they do NOT share a remedy.
|
|
65
|
+
// Telling a borrower to "run again" both hides the already-started billed
|
|
66
|
+
// run and is false about the tunnel — `--tunnel-client <id>` re-attaches
|
|
67
|
+
// to the same dead id and mints nothing. Naming the wrong cause is the
|
|
68
|
+
// failure mode this whole area keeps repeating, so each reason carries its
|
|
69
|
+
// own copy.
|
|
70
|
+
const ownerGone = reason === 'owner-gone';
|
|
71
|
+
super({
|
|
72
|
+
code: 'UNAVAILABLE',
|
|
73
|
+
message: ownerGone
|
|
74
|
+
? `The borrowed tunnel for run ${runId} is no longer registered. It was minted by ` +
|
|
75
|
+
`another process (\`testsprite tunnel start\`), and that process has stopped or its ` +
|
|
76
|
+
`credential expired. Run ${runId} was left executing server-side and may still ` +
|
|
77
|
+
`finish; if it is cancelled, its verdict is discarded.`
|
|
78
|
+
: `The tunnel carrying run ${runId} was disconnected by the server and cannot be ` +
|
|
79
|
+
`restored for this run (${reason}).`,
|
|
80
|
+
nextAction: ownerGone
|
|
81
|
+
? `Run ${runId} has lost its tunnel and cannot reach your app any more. Stop it now ` +
|
|
82
|
+
`with: testsprite test cancel ${runId} (idempotent). To watch it instead: ` +
|
|
83
|
+
`testsprite test wait ${runId} --timeout <s>.`
|
|
84
|
+
: 'This usually means the tunnel service was redeployed mid-run. Start the run again: ' +
|
|
85
|
+
'the retry mints a fresh tunnel.',
|
|
86
|
+
requestId: 'local',
|
|
87
|
+
details: { reason, runId },
|
|
88
|
+
});
|
|
89
|
+
this.name = 'TunnelLostError';
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function defaultCreateClient(options) {
|
|
93
|
+
return new TunnelClient(options);
|
|
94
|
+
}
|
|
95
|
+
async function stopClientWithDeadline(client) {
|
|
96
|
+
let timer;
|
|
97
|
+
try {
|
|
98
|
+
await Promise.race([
|
|
99
|
+
client.stop(),
|
|
100
|
+
new Promise(resolve => {
|
|
101
|
+
timer = setTimeout(resolve, TUNNEL_CLIENT_STOP_TIMEOUT_MS);
|
|
102
|
+
}),
|
|
103
|
+
]);
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
if (timer !== undefined)
|
|
107
|
+
clearTimeout(timer);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function redactTunnelSecret(line, secret) {
|
|
111
|
+
if (secret.length === 0)
|
|
112
|
+
return line;
|
|
113
|
+
return line.replaceAll(secret, TUNNEL_SECRET_REDACTION);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Mint (or adopt) a tunnel client and connect it.
|
|
117
|
+
*
|
|
118
|
+
* On any failure after the mint, the client gets a bounded chance to stop and
|
|
119
|
+
* the binding is always deleted before the error propagates. A
|
|
120
|
+
* minted-but-unconnected binding is a live credential with nothing watching
|
|
121
|
+
* it, while an un-stopped client can leave ref'd heartbeat/reconnect timers
|
|
122
|
+
* that keep the process alive long after the command has printed its last
|
|
123
|
+
* line. Credential deletion cannot wait indefinitely for those timers.
|
|
124
|
+
*/
|
|
125
|
+
export async function openTunnelSession(options, deps) {
|
|
126
|
+
const { log } = options;
|
|
127
|
+
let fatal;
|
|
128
|
+
let closed = false;
|
|
129
|
+
if (options.adopt) {
|
|
130
|
+
// Adopted: no secret here, so no connection and no teardown obligation.
|
|
131
|
+
return {
|
|
132
|
+
clientId: options.adopt.clientId,
|
|
133
|
+
expiresAt: options.adopt.expiresAt,
|
|
134
|
+
adopted: true,
|
|
135
|
+
fatalReason: () => fatal,
|
|
136
|
+
close: async () => { },
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
const minted = await deps.mint(options.ttlSeconds);
|
|
140
|
+
// Treat the vendored client as untrusted at this boundary: every line is
|
|
141
|
+
// scrubbed before the caller-owned sink can observe it. This remains safe if
|
|
142
|
+
// a future client log message accidentally interpolates its options.
|
|
143
|
+
const sessionLog = (line) => log(redactTunnelSecret(line, minted.secret));
|
|
144
|
+
const createClient = deps.createClient ?? defaultCreateClient;
|
|
145
|
+
let client;
|
|
146
|
+
const stopAndDestroy = async () => {
|
|
147
|
+
// Stop first so cooperative clients release their sockets and ref'd
|
|
148
|
+
// timers. A websocket stuck in CONNECTING makes the real stop() await the
|
|
149
|
+
// same pending control handshake as start(), so the hard deadline below
|
|
150
|
+
// must expire before destroy runs. Deleting the binding revokes the live
|
|
151
|
+
// credential and therefore always gets the final say in this ordering.
|
|
152
|
+
if (client !== undefined) {
|
|
153
|
+
try {
|
|
154
|
+
await stopClientWithDeadline(client);
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
// Best-effort: a failure to stop must never mask the original error,
|
|
158
|
+
// and must never skip the binding delete below.
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
await deps.destroy(minted.clientId);
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
sessionLog(`[advisory] could not delete the tunnel credential (${err instanceof Error ? err.message : String(err)}); it expires on its own at ${minted.expiresAt}.`);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
try {
|
|
169
|
+
client = createClient({
|
|
170
|
+
clientId: minted.clientId,
|
|
171
|
+
secret: minted.secret,
|
|
172
|
+
controlUrl: minted.controlUrl,
|
|
173
|
+
tunnelAddr: minted.tunnelAddr,
|
|
174
|
+
logLevel: options.logLevel ?? 'error',
|
|
175
|
+
// Never opened up. The point of `--local` is the caller's own loopback;
|
|
176
|
+
// a run that pivots into the rest of their network is the failure this
|
|
177
|
+
// rail exists to stop. Loopback and public targets are unaffected.
|
|
178
|
+
allowPrivateNetworkTarget: false,
|
|
179
|
+
logSink: (_level, line) => sessionLog(line),
|
|
180
|
+
onError: e => {
|
|
181
|
+
if (e.code === ErrCode.AuthFailed && fatal === undefined) {
|
|
182
|
+
// Terminal: the vendored client has stopped reconnecting, and a
|
|
183
|
+
// remint cannot rescue this run (see the module docstring).
|
|
184
|
+
fatal = 'auth-failed';
|
|
185
|
+
options.onFatal?.('auth-failed');
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (e.code === ErrCode.AuthFailed)
|
|
189
|
+
return;
|
|
190
|
+
sessionLog(`[tunnel] ${e.message}`);
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
catch (err) {
|
|
195
|
+
await stopAndDestroy();
|
|
196
|
+
throw err;
|
|
197
|
+
}
|
|
198
|
+
const timeoutMs = options.connectTimeoutMs ?? DEFAULT_TUNNEL_CONNECT_TIMEOUT_MS;
|
|
199
|
+
let timer;
|
|
200
|
+
try {
|
|
201
|
+
await new Promise((resolve, reject) => {
|
|
202
|
+
timer = setTimeout(() => {
|
|
203
|
+
reject(ApiError.fromEnvelope({
|
|
204
|
+
error: {
|
|
205
|
+
code: 'UNAVAILABLE',
|
|
206
|
+
message: `The tunnel did not connect within ${Math.round(timeoutMs / 1000)}s.`,
|
|
207
|
+
nextAction: 'Check that outbound WebSocket traffic is allowed from this machine, then retry. ' +
|
|
208
|
+
'An HTTP proxy does not help here: the tunnel data plane is a raw TCP connection ' +
|
|
209
|
+
'and does not go through one.',
|
|
210
|
+
requestId: 'local',
|
|
211
|
+
details: { reason: 'tunnel-connect-timeout', timeoutMs },
|
|
212
|
+
},
|
|
213
|
+
}));
|
|
214
|
+
}, timeoutMs);
|
|
215
|
+
client.start().then(resolve, reject);
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
catch (err) {
|
|
219
|
+
await stopAndDestroy();
|
|
220
|
+
if (err instanceof ApiError)
|
|
221
|
+
throw err;
|
|
222
|
+
throw ApiError.fromEnvelope({
|
|
223
|
+
error: {
|
|
224
|
+
code: 'UNAVAILABLE',
|
|
225
|
+
message: `Could not connect the tunnel: ${err instanceof Error ? err.message : String(err)}`,
|
|
226
|
+
nextAction: 'Retry. If this persists, report it to support@testsprite.com.',
|
|
227
|
+
requestId: 'local',
|
|
228
|
+
details: { reason: 'tunnel-connect-failed' },
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
finally {
|
|
233
|
+
if (timer !== undefined)
|
|
234
|
+
clearTimeout(timer);
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
clientId: minted.clientId,
|
|
238
|
+
expiresAt: minted.expiresAt,
|
|
239
|
+
adopted: false,
|
|
240
|
+
fatalReason: () => fatal,
|
|
241
|
+
close: async () => {
|
|
242
|
+
if (closed)
|
|
243
|
+
return;
|
|
244
|
+
closed = true;
|
|
245
|
+
await stopAndDestroy();
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
//# sourceMappingURL=tunnel-session.js.map
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire types for the `/api/cli/v1/tunnel` facade (DEV-747 piece 1).
|
|
3
|
+
*
|
|
4
|
+
* Three routes and deliberately no fourth — there is no resume/adopt route to
|
|
5
|
+
* hand a `{clientId, secret}` back to, so the CLI mints fresh per invocation.
|
|
6
|
+
*/
|
|
7
|
+
/** Body of `POST /api/cli/v1/tunnel`. */
|
|
8
|
+
export interface MintTunnelBody {
|
|
9
|
+
/**
|
|
10
|
+
* Requested binding lifetime in seconds. Server clamps to [60, 28800] and
|
|
11
|
+
* defaults to 2h. The CLI deletes its own binding at run end, so this is a
|
|
12
|
+
* backstop for a crashed process, not the normal cleanup path.
|
|
13
|
+
*/
|
|
14
|
+
ttlSeconds?: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The mint response — the ONE place a tunnel secret ever appears.
|
|
18
|
+
*
|
|
19
|
+
* It is never written to disk, never placed in `argv`, never exported into a
|
|
20
|
+
* child process's environment, and never logged. `config.json` credentials are
|
|
21
|
+
* a settled no for this codebase (452 leaked keys of history), and a tunnel
|
|
22
|
+
* secret is strictly worse than an API key: it authorises an inbound network
|
|
23
|
+
* path into the holder's machine.
|
|
24
|
+
*/
|
|
25
|
+
export interface TunnelMintResponse {
|
|
26
|
+
clientId: string;
|
|
27
|
+
/** Returned here and nowhere else, ever. */
|
|
28
|
+
secret: string;
|
|
29
|
+
/**
|
|
30
|
+
* WebSocket CONTROL plane the tunnel client authenticates against.
|
|
31
|
+
*
|
|
32
|
+
* Not the proxy plane. The execution Lambda's browser talks to a third,
|
|
33
|
+
* server-side-only endpoint that the CLI never learns; wiring a client at
|
|
34
|
+
* that port produces a failure that reads like an auth error, which is why
|
|
35
|
+
* the facade returns these two explicitly instead of a host/port pair.
|
|
36
|
+
*/
|
|
37
|
+
controlUrl: string;
|
|
38
|
+
/** `host:port` of the tunnel DATA plane the client dials over raw TCP. */
|
|
39
|
+
tunnelAddr: string;
|
|
40
|
+
/** ISO8601. After this the binding is gone and the run can no longer attach. */
|
|
41
|
+
expiresAt: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* `GET /api/cli/v1/tunnel/{clientId}` — deliberately secret-free.
|
|
45
|
+
*
|
|
46
|
+
* `status` is an open string on the wire (resilience rule 2) even though the
|
|
47
|
+
* server has exactly two values today: an unknown value must degrade, never
|
|
48
|
+
* reject the response.
|
|
49
|
+
*/
|
|
50
|
+
export interface TunnelStatusResponse {
|
|
51
|
+
clientId: string;
|
|
52
|
+
status: 'online' | 'offline' | (string & {});
|
|
53
|
+
expiresAt: string;
|
|
54
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire types for the `/api/cli/v1/tunnel` facade (DEV-747 piece 1).
|
|
3
|
+
*
|
|
4
|
+
* Three routes and deliberately no fourth — there is no resume/adopt route to
|
|
5
|
+
* hand a `{clientId, secret}` back to, so the CLI mints fresh per invocation.
|
|
6
|
+
*/
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=tunnel.types.js.map
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
import { TunnelClientOptions } from "./types.js";
|
|
3
|
+
export declare class TunnelClient extends EventEmitter {
|
|
4
|
+
private readonly options;
|
|
5
|
+
private running;
|
|
6
|
+
private controlConnected;
|
|
7
|
+
private controlWs?;
|
|
8
|
+
private controlLoopTask?;
|
|
9
|
+
private controlConnectionGeneration;
|
|
10
|
+
private allowTunnelReconnect;
|
|
11
|
+
private readonly tunnelRuntimes;
|
|
12
|
+
private reconnectDelayController;
|
|
13
|
+
private readonly activeTargetSockets;
|
|
14
|
+
constructor(options: TunnelClientOptions);
|
|
15
|
+
start(): Promise<void>;
|
|
16
|
+
stop(): Promise<void>;
|
|
17
|
+
private runControlLoop;
|
|
18
|
+
private runTunnelLoop;
|
|
19
|
+
private connectControl;
|
|
20
|
+
private sendControlMessage;
|
|
21
|
+
private parseControlMessage;
|
|
22
|
+
private ensureTunnelRuntime;
|
|
23
|
+
private connectTunnel;
|
|
24
|
+
private stopAllTunnelRuntimes;
|
|
25
|
+
private handleIncomingStream;
|
|
26
|
+
private proxyStreams;
|
|
27
|
+
private copyOneWay;
|
|
28
|
+
private connectTarget;
|
|
29
|
+
private connectOnce;
|
|
30
|
+
private log;
|
|
31
|
+
private reportError;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Resolve `host:port` to the exact set of addresses a dial will be attempted against.
|
|
35
|
+
*
|
|
36
|
+
* Mirrors the Rust client's `dial_candidates`: for `localhost` (case-insensitive), `127.0.0.1`
|
|
37
|
+
* and `::1` are added up front; the host is then also resolved normally (an IP literal parses
|
|
38
|
+
* without touching the network, exactly like Rust's `to_socket_addrs`; anything else goes
|
|
39
|
+
* through `dns.lookup`, i.e. getaddrinfo, honoring `/etc/hosts`), and any additional resolved
|
|
40
|
+
* addresses are appended, deduplicated.
|
|
41
|
+
*
|
|
42
|
+
* Callers must validate and dial this exact returned list — re-resolving `host` a second time
|
|
43
|
+
* reopens the DNS-rebind window this function exists to close.
|
|
44
|
+
*/
|
|
45
|
+
export declare function resolveDialCandidates(host: string, port: number): Promise<Array<{
|
|
46
|
+
host: string;
|
|
47
|
+
port: number;
|
|
48
|
+
}>>;
|
|
49
|
+
/**
|
|
50
|
+
* Why `address` (a literal IPv4 or IPv6 address, e.g. from {@link resolveDialCandidates}) must not
|
|
51
|
+
* be dialled, or `undefined` if it is acceptable. Mirrors the Rust client's
|
|
52
|
+
* `blocked_target_reason` — the two must agree exactly, or the same request succeeds on one
|
|
53
|
+
* client and fails on the other.
|
|
54
|
+
*
|
|
55
|
+
* Acceptable = loopback (the app under test) or a globally-routable address (the fonts, CDNs and
|
|
56
|
+
* third-party APIs a real page legitimately loads). Blocked = the private address space, which is
|
|
57
|
+
* only reachable because this client runs inside someone's network.
|
|
58
|
+
*
|
|
59
|
+
* Not a hostname resolver — callers pass already-resolved literals.
|
|
60
|
+
*/
|
|
61
|
+
export declare function blockedTargetReason(address: string): string | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Ensure no candidate in `candidates` sits in a blocked range, unless `allowPrivateNetwork` opts
|
|
64
|
+
* out. Mirrors the Rust client's `ensure_candidates_allowed` / `ensure_target_allowed`: takes an
|
|
65
|
+
* already-resolved candidate set (rather than resolving internally) so callers about to dial that
|
|
66
|
+
* exact set can validate it without a second, independent DNS lookup.
|
|
67
|
+
*
|
|
68
|
+
* Deliberately NOT loopback-only: the browser under test proxies every request through this
|
|
69
|
+
* tunnel (Chromium `bypass='<-loopback>'`), so public targets are normal subresource traffic.
|
|
70
|
+
* See the Rust `ensure_target_allowed` doc comment for the full rationale.
|
|
71
|
+
*/
|
|
72
|
+
export declare function ensureTargetAllowed(host: string, port: number, candidates: Array<{
|
|
73
|
+
host: string;
|
|
74
|
+
port: number;
|
|
75
|
+
}>, allowPrivateNetwork: boolean): void;
|
|
76
|
+
/**
|
|
77
|
+
* Thrown by {@link ensureTargetAllowed} when a proxied target resolves into a blocked
|
|
78
|
+
* (private/internal) range. Mirrors the Rust client's `ensure_candidates_allowed` error.
|
|
79
|
+
*/
|
|
80
|
+
export declare class BlockedTargetError extends Error {
|
|
81
|
+
constructor(host: string, port: number, address?: string, reason?: string);
|
|
82
|
+
}
|