@testsprite/testsprite-cli 0.7.0 β 0.8.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 +51 -38
- package/dist/commands/agent.js +47 -3
- package/dist/commands/ci.d.ts +88 -0
- package/dist/commands/ci.js +429 -0
- package/dist/commands/doctor.js +100 -1
- package/dist/commands/init.js +8 -0
- package/dist/commands/project.d.ts +78 -0
- package/dist/commands/project.js +426 -3
- package/dist/commands/test.d.ts +162 -2
- package/dist/commands/test.js +1814 -392
- 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/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 +94 -0
- package/dist/lib/http.js +120 -5
- 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 +32 -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 +24 -0
- package/dist/lib/target-url-preflight.js +3 -3
- package/dist/lib/target-url.d.ts +44 -7
- package/dist/lib/target-url.js +250 -61
- 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 +129 -0
- package/dist/lib/tunnel-session.js +232 -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 +83 -0
- package/dist/vendor/tunnel-client/client.js +871 -0
- package/dist/vendor/tunnel-client/config.d.ts +41 -0
- package/dist/vendor/tunnel-client/config.js +42 -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 +96 -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
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VENDOR DELTA (not upstream). Upstream's `config.ts` reads `TSTUN_*`
|
|
3
|
+
* environment variables and falls back to **hard-coded TestSprite dev
|
|
4
|
+
* ALB/NLB hostnames**. Neither belongs in a package published to the public
|
|
5
|
+
* npm registry:
|
|
6
|
+
*
|
|
7
|
+
* - the endpoints are internal infrastructure names, and a default value is
|
|
8
|
+
* still a value that ships;
|
|
9
|
+
* - a default endpoint is the failure mode the facade exists to prevent.
|
|
10
|
+
* `POST /api/cli/v1/tunnel` returns `controlUrl` and `tunnelAddr` for the
|
|
11
|
+
* environment the caller authenticated against, and those are the ONLY
|
|
12
|
+
* endpoints this client may dial. A default would let a
|
|
13
|
+
* misconfigured/partially-constructed client silently connect somewhere
|
|
14
|
+
* else and fail in a way that reads like an auth problem.
|
|
15
|
+
*
|
|
16
|
+
* So the endpoint defaults are gone and `TunnelClientOptions.controlUrl` /
|
|
17
|
+
* `.tunnelAddr` are required by the caller (`lib/tunnel-session.ts` passes
|
|
18
|
+
* the mint response through verbatim). The timing constants below are
|
|
19
|
+
* upstream's values.
|
|
20
|
+
*/
|
|
21
|
+
/** Heartbeat cadence on the control WebSocket. Upstream default. */
|
|
22
|
+
export declare const DEFAULT_HEARTBEAT_MS = 10000;
|
|
23
|
+
/** Delay between control/tunnel reconnect attempts. Upstream default. */
|
|
24
|
+
export declare const DEFAULT_RECONNECT_MS = 3000;
|
|
25
|
+
/** Log verbosity floor. Upstream default. */
|
|
26
|
+
export declare const DEFAULT_LOG_LEVEL: "info";
|
|
27
|
+
/** Per-attempt dial timeout when connecting to the proxied target. Upstream default. */
|
|
28
|
+
export declare const DEFAULT_TARGET_CONNECT_TIMEOUT_MS = 10000;
|
|
29
|
+
/**
|
|
30
|
+
* Whether a proxied target may resolve into private address space.
|
|
31
|
+
*
|
|
32
|
+
* Upstream reads `TS_TUNNEL_ALLOW_PRIVATE_NETWORK_TARGET`. This CLI hard-codes
|
|
33
|
+
* `false` and exposes no flag for it: the whole point of `--local` is to reach
|
|
34
|
+
* the caller's own loopback, and a run that pivots into the rest of their
|
|
35
|
+
* network is the failure this rail exists to stop. Loopback and public targets
|
|
36
|
+
* are unaffected β the browser under test proxies every request, so a page
|
|
37
|
+
* pulling a font or calling a third-party API is normal traffic (verified on
|
|
38
|
+
* dev 2026-08-24: `accounts.google.com` and `cf.browser-use.com` both crossed
|
|
39
|
+
* the same client during a real run).
|
|
40
|
+
*/
|
|
41
|
+
export declare const DEFAULT_ALLOW_PRIVATE_NETWORK_TARGET = false;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VENDOR DELTA (not upstream). Upstream's `config.ts` reads `TSTUN_*`
|
|
3
|
+
* environment variables and falls back to **hard-coded TestSprite dev
|
|
4
|
+
* ALB/NLB hostnames**. Neither belongs in a package published to the public
|
|
5
|
+
* npm registry:
|
|
6
|
+
*
|
|
7
|
+
* - the endpoints are internal infrastructure names, and a default value is
|
|
8
|
+
* still a value that ships;
|
|
9
|
+
* - a default endpoint is the failure mode the facade exists to prevent.
|
|
10
|
+
* `POST /api/cli/v1/tunnel` returns `controlUrl` and `tunnelAddr` for the
|
|
11
|
+
* environment the caller authenticated against, and those are the ONLY
|
|
12
|
+
* endpoints this client may dial. A default would let a
|
|
13
|
+
* misconfigured/partially-constructed client silently connect somewhere
|
|
14
|
+
* else and fail in a way that reads like an auth problem.
|
|
15
|
+
*
|
|
16
|
+
* So the endpoint defaults are gone and `TunnelClientOptions.controlUrl` /
|
|
17
|
+
* `.tunnelAddr` are required by the caller (`lib/tunnel-session.ts` passes
|
|
18
|
+
* the mint response through verbatim). The timing constants below are
|
|
19
|
+
* upstream's values.
|
|
20
|
+
*/
|
|
21
|
+
/** Heartbeat cadence on the control WebSocket. Upstream default. */
|
|
22
|
+
export const DEFAULT_HEARTBEAT_MS = 10_000;
|
|
23
|
+
/** Delay between control/tunnel reconnect attempts. Upstream default. */
|
|
24
|
+
export const DEFAULT_RECONNECT_MS = 3_000;
|
|
25
|
+
/** Log verbosity floor. Upstream default. */
|
|
26
|
+
export const DEFAULT_LOG_LEVEL = 'info';
|
|
27
|
+
/** Per-attempt dial timeout when connecting to the proxied target. Upstream default. */
|
|
28
|
+
export const DEFAULT_TARGET_CONNECT_TIMEOUT_MS = 10_000;
|
|
29
|
+
/**
|
|
30
|
+
* Whether a proxied target may resolve into private address space.
|
|
31
|
+
*
|
|
32
|
+
* Upstream reads `TS_TUNNEL_ALLOW_PRIVATE_NETWORK_TARGET`. This CLI hard-codes
|
|
33
|
+
* `false` and exposes no flag for it: the whole point of `--local` is to reach
|
|
34
|
+
* the caller's own loopback, and a run that pivots into the rest of their
|
|
35
|
+
* network is the failure this rail exists to stop. Loopback and public targets
|
|
36
|
+
* are unaffected β the browser under test proxies every request, so a page
|
|
37
|
+
* pulling a font or calling a third-party API is normal traffic (verified on
|
|
38
|
+
* dev 2026-08-24: `accounts.google.com` and `cf.browser-use.com` both crossed
|
|
39
|
+
* the same client during a real run).
|
|
40
|
+
*/
|
|
41
|
+
export const DEFAULT_ALLOW_PRIVATE_NETWORK_TARGET = false;
|
|
42
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VENDOR DELTA (not upstream). Upstream's `index.ts` re-exports only
|
|
3
|
+
* `TunnelClient` / `BlockedTargetError`; this adds the two dial helpers,
|
|
4
|
+
* which the CLI's port preflight reuses so the pre-charge probe dials the
|
|
5
|
+
* SAME candidate set the client will dial at run time.
|
|
6
|
+
*/
|
|
7
|
+
export { TunnelClient, BlockedTargetError, resolveDialCandidates, blockedTargetReason, } from './client.js';
|
|
8
|
+
export type { TunnelClientOptions, StreamOpenRequestFrame, LogLevel } from './types.js';
|
|
9
|
+
export { ErrCode } from './types.js';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VENDOR DELTA (not upstream). Upstream's `index.ts` re-exports only
|
|
3
|
+
* `TunnelClient` / `BlockedTargetError`; this adds the two dial helpers,
|
|
4
|
+
* which the CLI's port preflight reuses so the pre-charge probe dials the
|
|
5
|
+
* SAME candidate set the client will dial at run time.
|
|
6
|
+
*/
|
|
7
|
+
export { TunnelClient, BlockedTargetError, resolveDialCandidates, blockedTargetReason, } from './client.js';
|
|
8
|
+
export { ErrCode } from './types.js';
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VENDOR DELTA (not upstream). Upstream's `client.ts` pulls `isNumber`,
|
|
3
|
+
* `isPlainObject` and `isString` from `lodash`; this CLI ships three runtime
|
|
4
|
+
* dependencies on purpose, and these three predicates are a handful of lines.
|
|
5
|
+
*
|
|
6
|
+
* Semantics match lodash's for every value this code path can see (JSON
|
|
7
|
+
* parsed off the wire): `isNumber` accepts the `number` primitive β lodash
|
|
8
|
+
* also accepts `Number` objects and `NaN`, neither of which `JSON.parse`
|
|
9
|
+
* ever produces β and `isPlainObject` accepts an object literal or a
|
|
10
|
+
* null-prototype object, rejecting arrays and `null`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function isString(value: unknown): value is string;
|
|
13
|
+
export declare function isNumber(value: unknown): value is number;
|
|
14
|
+
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VENDOR DELTA (not upstream). Upstream's `client.ts` pulls `isNumber`,
|
|
3
|
+
* `isPlainObject` and `isString` from `lodash`; this CLI ships three runtime
|
|
4
|
+
* dependencies on purpose, and these three predicates are a handful of lines.
|
|
5
|
+
*
|
|
6
|
+
* Semantics match lodash's for every value this code path can see (JSON
|
|
7
|
+
* parsed off the wire): `isNumber` accepts the `number` primitive β lodash
|
|
8
|
+
* also accepts `Number` objects and `NaN`, neither of which `JSON.parse`
|
|
9
|
+
* ever produces β and `isPlainObject` accepts an object literal or a
|
|
10
|
+
* null-prototype object, rejecting arrays and `null`.
|
|
11
|
+
*/
|
|
12
|
+
export function isString(value) {
|
|
13
|
+
return typeof value === 'string';
|
|
14
|
+
}
|
|
15
|
+
export function isNumber(value) {
|
|
16
|
+
return typeof value === 'number';
|
|
17
|
+
}
|
|
18
|
+
export function isPlainObject(value) {
|
|
19
|
+
if (typeof value !== 'object' || value === null)
|
|
20
|
+
return false;
|
|
21
|
+
const proto = Object.getPrototypeOf(value);
|
|
22
|
+
return proto === null || proto === Object.prototype;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=lodash-lite.js.map
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Duplex } from "node:stream";
|
|
2
|
+
/**
|
|
3
|
+
* Hard upper bound on a single length-prefixed tunnel protocol frame. Checked BEFORE
|
|
4
|
+
* `readExactly(stream, bodyLen)` runs, because `bodyLen` is a big-endian u32 read straight off
|
|
5
|
+
* the wire from a peer that has not authenticated yet β without a cap, a malicious/garbled length
|
|
6
|
+
* makes `readExactly` buffer an unbounded number of chunks trying to reach it. This is exactly how
|
|
7
|
+
* dev crashed on 2026-08-14: a stray HTTP request landed on the tunnel port, and its first four
|
|
8
|
+
* bytes (`b"GET "`), interpreted as a length prefix, decoded to 1_195_725_856.
|
|
9
|
+
*
|
|
10
|
+
* Real payloads are a few hundred bytes of JSON (a UUID, a short secret, a couple of prefixed ids
|
|
11
|
+
* and a hostname) β 64 KiB leaves well over 100x headroom. Mirrors the Rust side's
|
|
12
|
+
* `MAX_TUNNEL_FRAME` (`src/protocol/tunnel.rs`); the two must stay in sync.
|
|
13
|
+
*/
|
|
14
|
+
export declare const MAX_TUNNEL_FRAME: number;
|
|
15
|
+
/**
|
|
16
|
+
* How long a peer gets to finish sending one length-prefixed frame, counted from when we start
|
|
17
|
+
* waiting for its length prefix. Bounds a "length prefix is valid and small, but the body trickles
|
|
18
|
+
* in one byte every 30 seconds" (slowloris-style) connection from pinning a read forever. Mirrors
|
|
19
|
+
* the Rust side's `FRAME_READ_TIMEOUT`.
|
|
20
|
+
*/
|
|
21
|
+
export declare const FRAME_READ_TIMEOUT_MS = 10000;
|
|
22
|
+
export declare enum ControlClientMsgType {
|
|
23
|
+
Auth = "Auth",
|
|
24
|
+
Heartbeat = "Heartbeat"
|
|
25
|
+
}
|
|
26
|
+
export interface ControlClientMsgAuth {
|
|
27
|
+
type: ControlClientMsgType.Auth;
|
|
28
|
+
payload: {
|
|
29
|
+
secret: string;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export interface ControlClientMsgHeartbeat {
|
|
33
|
+
type: ControlClientMsgType.Heartbeat;
|
|
34
|
+
}
|
|
35
|
+
export type ControlClientMsg = ControlClientMsgAuth;
|
|
36
|
+
export declare enum ControlServerMsgType {
|
|
37
|
+
Ack = "Ack",
|
|
38
|
+
CloseTunnel = "CloseTunnel",
|
|
39
|
+
RequestTunnel = "RequestTunnel"
|
|
40
|
+
}
|
|
41
|
+
export interface ControlServerMessageAck {
|
|
42
|
+
type: ControlServerMsgType.Ack;
|
|
43
|
+
}
|
|
44
|
+
export interface ControlServerMessageCloseTunnel {
|
|
45
|
+
type: ControlServerMsgType.CloseTunnel;
|
|
46
|
+
payload: {
|
|
47
|
+
reason?: string;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export interface ControlServerMessageRequestTunnel {
|
|
51
|
+
type: ControlServerMsgType.RequestTunnel;
|
|
52
|
+
payload: {
|
|
53
|
+
tunnel_connection_id: string;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export type ControlServerMsg = ControlServerMessageAck | ControlServerMessageRequestTunnel | ControlServerMessageCloseTunnel;
|
|
57
|
+
export declare function encodeFrame(obj: unknown): Buffer;
|
|
58
|
+
export declare function readFrame(stream: Duplex): Promise<unknown>;
|
|
59
|
+
export declare function readTypedFrame<T>(stream: Duplex, validator: (value: unknown) => value is T): Promise<T>;
|
|
60
|
+
export declare function readExactly(stream: Duplex, bytes: number, signal?: AbortSignal): Promise<Buffer>;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hard upper bound on a single length-prefixed tunnel protocol frame. Checked BEFORE
|
|
3
|
+
* `readExactly(stream, bodyLen)` runs, because `bodyLen` is a big-endian u32 read straight off
|
|
4
|
+
* the wire from a peer that has not authenticated yet β without a cap, a malicious/garbled length
|
|
5
|
+
* makes `readExactly` buffer an unbounded number of chunks trying to reach it. This is exactly how
|
|
6
|
+
* dev crashed on 2026-08-14: a stray HTTP request landed on the tunnel port, and its first four
|
|
7
|
+
* bytes (`b"GET "`), interpreted as a length prefix, decoded to 1_195_725_856.
|
|
8
|
+
*
|
|
9
|
+
* Real payloads are a few hundred bytes of JSON (a UUID, a short secret, a couple of prefixed ids
|
|
10
|
+
* and a hostname) β 64 KiB leaves well over 100x headroom. Mirrors the Rust side's
|
|
11
|
+
* `MAX_TUNNEL_FRAME` (`src/protocol/tunnel.rs`); the two must stay in sync.
|
|
12
|
+
*/
|
|
13
|
+
export const MAX_TUNNEL_FRAME = 64 * 1024;
|
|
14
|
+
/**
|
|
15
|
+
* How long a peer gets to finish sending one length-prefixed frame, counted from when we start
|
|
16
|
+
* waiting for its length prefix. Bounds a "length prefix is valid and small, but the body trickles
|
|
17
|
+
* in one byte every 30 seconds" (slowloris-style) connection from pinning a read forever. Mirrors
|
|
18
|
+
* the Rust side's `FRAME_READ_TIMEOUT`.
|
|
19
|
+
*/
|
|
20
|
+
export const FRAME_READ_TIMEOUT_MS = 10_000;
|
|
21
|
+
export var ControlClientMsgType;
|
|
22
|
+
(function (ControlClientMsgType) {
|
|
23
|
+
ControlClientMsgType["Auth"] = "Auth";
|
|
24
|
+
ControlClientMsgType["Heartbeat"] = "Heartbeat";
|
|
25
|
+
})(ControlClientMsgType || (ControlClientMsgType = {}));
|
|
26
|
+
export var ControlServerMsgType;
|
|
27
|
+
(function (ControlServerMsgType) {
|
|
28
|
+
ControlServerMsgType["Ack"] = "Ack";
|
|
29
|
+
ControlServerMsgType["CloseTunnel"] = "CloseTunnel";
|
|
30
|
+
ControlServerMsgType["RequestTunnel"] = "RequestTunnel";
|
|
31
|
+
})(ControlServerMsgType || (ControlServerMsgType = {}));
|
|
32
|
+
export function encodeFrame(obj) {
|
|
33
|
+
const body = Buffer.from(JSON.stringify(obj), "utf8");
|
|
34
|
+
const header = Buffer.allocUnsafe(4);
|
|
35
|
+
header.writeUInt32BE(body.length, 0);
|
|
36
|
+
return Buffer.concat([header, body]);
|
|
37
|
+
}
|
|
38
|
+
export async function readFrame(stream) {
|
|
39
|
+
const controller = new AbortController();
|
|
40
|
+
const timer = setTimeout(() => {
|
|
41
|
+
controller.abort(new Error(`timed out after ${FRAME_READ_TIMEOUT_MS}ms reading tunnel frame`));
|
|
42
|
+
}, FRAME_READ_TIMEOUT_MS);
|
|
43
|
+
try {
|
|
44
|
+
const lenBuf = await readExactly(stream, 4, controller.signal);
|
|
45
|
+
const bodyLen = lenBuf.readUInt32BE(0);
|
|
46
|
+
// Checked BEFORE the body read, which is what would otherwise buffer an unbounded number
|
|
47
|
+
// of chunks trying to reach `bodyLen` β see `MAX_TUNNEL_FRAME`'s doc comment.
|
|
48
|
+
if (bodyLen > MAX_TUNNEL_FRAME) {
|
|
49
|
+
throw new Error(`tunnel frame length ${bodyLen} exceeds the ${MAX_TUNNEL_FRAME}-byte limit`);
|
|
50
|
+
}
|
|
51
|
+
const body = await readExactly(stream, bodyLen, controller.signal);
|
|
52
|
+
const jsonText = body.toString("utf8");
|
|
53
|
+
return JSON.parse(jsonText);
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
clearTimeout(timer);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export async function readTypedFrame(stream, validator) {
|
|
60
|
+
const value = await readFrame(stream);
|
|
61
|
+
if (!validator(value)) {
|
|
62
|
+
throw new Error("Invalid frame payload");
|
|
63
|
+
}
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
// Exported so a test can call it directly: with `bytes === 0`, `onReadable`'s
|
|
67
|
+
// `while (total < bytes)` loop body never runs (`0 < 0` is false), so without this
|
|
68
|
+
// special case the promise below would never settle on its own -- unlike Rust's
|
|
69
|
+
// `read_exact`, which returns `Ok(())` immediately for a zero-length read. In production this
|
|
70
|
+
// surfaces as a 0-length frame (an in-range but never-realistic length prefix) hanging for the
|
|
71
|
+
// full `FRAME_READ_TIMEOUT` instead of resolving at once.
|
|
72
|
+
export function readExactly(stream, bytes, signal) {
|
|
73
|
+
if (bytes <= 0) {
|
|
74
|
+
return Promise.resolve(Buffer.alloc(0));
|
|
75
|
+
}
|
|
76
|
+
return new Promise((resolve, reject) => {
|
|
77
|
+
let chunks = [];
|
|
78
|
+
let total = 0;
|
|
79
|
+
const cleanup = () => {
|
|
80
|
+
stream.off("readable", onReadable);
|
|
81
|
+
stream.off("error", onError);
|
|
82
|
+
stream.off("end", onEnd);
|
|
83
|
+
stream.off("close", onClose);
|
|
84
|
+
signal?.removeEventListener("abort", onAbort);
|
|
85
|
+
};
|
|
86
|
+
const finish = () => {
|
|
87
|
+
const merged = Buffer.concat(chunks, total);
|
|
88
|
+
const wanted = merged.subarray(0, bytes);
|
|
89
|
+
const remaining = merged.subarray(bytes);
|
|
90
|
+
if (remaining.length > 0) {
|
|
91
|
+
stream.unshift(remaining);
|
|
92
|
+
}
|
|
93
|
+
cleanup();
|
|
94
|
+
resolve(wanted);
|
|
95
|
+
};
|
|
96
|
+
const onReadable = () => {
|
|
97
|
+
while (total < bytes) {
|
|
98
|
+
const chunk = stream.read(bytes - total);
|
|
99
|
+
if (!chunk) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
chunks.push(chunk);
|
|
103
|
+
total += chunk.length;
|
|
104
|
+
if (total >= bytes) {
|
|
105
|
+
finish();
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const onError = (err) => {
|
|
111
|
+
cleanup();
|
|
112
|
+
reject(err);
|
|
113
|
+
};
|
|
114
|
+
const onEnd = () => {
|
|
115
|
+
cleanup();
|
|
116
|
+
reject(new Error("Stream ended before enough bytes were read"));
|
|
117
|
+
};
|
|
118
|
+
const onClose = () => {
|
|
119
|
+
cleanup();
|
|
120
|
+
reject(new Error("Stream closed before enough bytes were read"));
|
|
121
|
+
};
|
|
122
|
+
// Lets `readFrame`'s timeout actually stop this read (removing all listeners) instead of
|
|
123
|
+
// just racing it: without this, a slowloris peer that never sends enough bytes leaves
|
|
124
|
+
// this promise β and its listeners on `stream` β alive forever after the caller times out.
|
|
125
|
+
const onAbort = () => {
|
|
126
|
+
cleanup();
|
|
127
|
+
reject(signal?.reason ?? new Error("aborted"));
|
|
128
|
+
};
|
|
129
|
+
if (signal) {
|
|
130
|
+
if (signal.aborted) {
|
|
131
|
+
onAbort();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
signal.addEventListener("abort", onAbort);
|
|
135
|
+
}
|
|
136
|
+
stream.on("readable", onReadable);
|
|
137
|
+
stream.on("error", onError);
|
|
138
|
+
stream.on("end", onEnd);
|
|
139
|
+
stream.on("close", onClose);
|
|
140
|
+
onReadable();
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=protocol.js.map
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
2
|
+
export declare enum ErrCode {
|
|
3
|
+
AuthFailed = "0001",
|
|
4
|
+
ControlDisconnected = "0002",
|
|
5
|
+
TunnelDisconnected = "0003",
|
|
6
|
+
TargetConnectFailed = "0004",
|
|
7
|
+
StreamFailed = "0005",
|
|
8
|
+
BlockedTargetRejected = "0006"
|
|
9
|
+
}
|
|
10
|
+
export interface TunnelClientError {
|
|
11
|
+
code: ErrCode;
|
|
12
|
+
message: string;
|
|
13
|
+
}
|
|
14
|
+
export interface TunnelClientOptions {
|
|
15
|
+
clientId: string;
|
|
16
|
+
secret: string;
|
|
17
|
+
/**
|
|
18
|
+
* VENDOR DELTA: required, not optional. The endpoint defaults upstream
|
|
19
|
+
* carries are hard-coded TestSprite dev hostnames; they are gone here and
|
|
20
|
+
* both values come from `POST /api/cli/v1/tunnel`. See ./config.ts.
|
|
21
|
+
*/
|
|
22
|
+
controlUrl: string;
|
|
23
|
+
/** VENDOR DELTA: required. `host:port` of the tunnel DATA plane. */
|
|
24
|
+
tunnelAddr: string;
|
|
25
|
+
heartbeatMs?: number;
|
|
26
|
+
reconnectMs?: number;
|
|
27
|
+
logLevel?: LogLevel;
|
|
28
|
+
/**
|
|
29
|
+
* Allow a proxied target to resolve into the private address space (RFC1918, CGNAT,
|
|
30
|
+
* link-local incl. cloud metadata, IPv6 ULA/link-local). Off by default.
|
|
31
|
+
*
|
|
32
|
+
* Loopback and public targets are always allowed and are NOT gated by this flag: the browser
|
|
33
|
+
* under test proxies every request through this tunnel (Chromium `bypass='<-loopback>'`), so a
|
|
34
|
+
* page pulling a web font or calling a third-party API is normal traffic, not abuse. What this
|
|
35
|
+
* gates is the pivot β `target_host`/`target_port` are relayed verbatim from the inbound proxy
|
|
36
|
+
* request on the internet-facing proxy port, so nothing otherwise stops a crafted request from
|
|
37
|
+
* steering this client at hosts it can only reach from inside your network. Mirrors the Rust
|
|
38
|
+
* client's `--allow-private-network-target` / `TS_TUNNEL_ALLOW_PRIVATE_NETWORK_TARGET`;
|
|
39
|
+
* enabling it removes a safety rail, it is not a network sandbox.
|
|
40
|
+
*/
|
|
41
|
+
allowPrivateNetworkTarget?: boolean;
|
|
42
|
+
onError?(e: TunnelClientError): void;
|
|
43
|
+
/**
|
|
44
|
+
* VENDOR DELTA (not upstream): where log lines go. Upstream writes them to
|
|
45
|
+
* `console.log`/`console.warn`, i.e. STDOUT, which would corrupt this CLI's
|
|
46
|
+
* `--output json` contract. Absent -> stderr.
|
|
47
|
+
*/
|
|
48
|
+
logSink?(level: LogLevel, line: string): void;
|
|
49
|
+
}
|
|
50
|
+
export type ClientToServerControlMessage = {
|
|
51
|
+
type: "Auth";
|
|
52
|
+
payload: {
|
|
53
|
+
secret: string;
|
|
54
|
+
};
|
|
55
|
+
} | {
|
|
56
|
+
type: "Hello";
|
|
57
|
+
payload: {
|
|
58
|
+
client_id: string;
|
|
59
|
+
version: string;
|
|
60
|
+
};
|
|
61
|
+
} | {
|
|
62
|
+
type: "Heartbeat";
|
|
63
|
+
} | {
|
|
64
|
+
type: "Status";
|
|
65
|
+
payload: {
|
|
66
|
+
status: "Offline" | "Online";
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
export type ServerToClientControlMessage = {
|
|
70
|
+
type: "Ack";
|
|
71
|
+
} | {
|
|
72
|
+
type: "RequestTunnel";
|
|
73
|
+
payload: {
|
|
74
|
+
tunnel_connection_id: string;
|
|
75
|
+
target_host: string;
|
|
76
|
+
target_port: number;
|
|
77
|
+
};
|
|
78
|
+
} | {
|
|
79
|
+
type: "CloseTunnel";
|
|
80
|
+
payload: {
|
|
81
|
+
reason: string;
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
export interface TunnelHelloFrame {
|
|
85
|
+
client_id: string;
|
|
86
|
+
secret: string;
|
|
87
|
+
tunnel_connection_id: string;
|
|
88
|
+
}
|
|
89
|
+
export interface StreamOpenRequestFrame {
|
|
90
|
+
request_id: string;
|
|
91
|
+
inbound_request_id: string;
|
|
92
|
+
tunnel_connection_id: string;
|
|
93
|
+
mux_stream_id: number;
|
|
94
|
+
target_host: string;
|
|
95
|
+
target_port: number;
|
|
96
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export var ErrCode;
|
|
2
|
+
(function (ErrCode) {
|
|
3
|
+
ErrCode["AuthFailed"] = "0001";
|
|
4
|
+
ErrCode["ControlDisconnected"] = "0002";
|
|
5
|
+
ErrCode["TunnelDisconnected"] = "0003";
|
|
6
|
+
ErrCode["TargetConnectFailed"] = "0004";
|
|
7
|
+
ErrCode["StreamFailed"] = "0005";
|
|
8
|
+
ErrCode["BlockedTargetRejected"] = "0006";
|
|
9
|
+
})(ErrCode || (ErrCode = {}));
|
|
10
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VENDOR DELTA (not upstream). A minimal `ws`-shaped facade over undici's
|
|
3
|
+
* WHATWG `WebSocket`, so the vendored tunnel client keeps its upstream body
|
|
4
|
+
* while this CLI adds no `ws` dependency: `undici` is already a runtime
|
|
5
|
+
* dependency here (`lib/proxy.ts` uses its `EnvHttpProxyAgent`), and its
|
|
6
|
+
* `WebSocket` is the same RFC 6455 implementation Node ships as the global.
|
|
7
|
+
*
|
|
8
|
+
* Only the surface `client.ts` actually touches is implemented β `on`,
|
|
9
|
+
* `send`, `close`, `pong`, `readyState`, and the statics `CONNECTING`/`OPEN`. Anything else
|
|
10
|
+
* is deliberately absent, and an unknown event name throws rather than being
|
|
11
|
+
* silently accepted, so a future upstream sync that reaches for a new `ws` API
|
|
12
|
+
* fails loudly instead of quietly never firing.
|
|
13
|
+
*
|
|
14
|
+
* Two behavioural notes, both a consequence of WHATWG's narrower surface:
|
|
15
|
+
*
|
|
16
|
+
* - **`ping` never fires and `pong()` is a no-op.** The WHATWG API does not
|
|
17
|
+
* expose control frames; undici answers a server PING with a PONG inside
|
|
18
|
+
* its own receiver, which is exactly what upstream's handler did by hand.
|
|
19
|
+
* Registering the listener is still accepted so the upstream line stays
|
|
20
|
+
* compilable and diffable.
|
|
21
|
+
* - **The control socket uses the process-global undici dispatcher**, so an
|
|
22
|
+
* `HTTP(S)_PROXY` this CLI honours (`lib/proxy.ts`) also carries the
|
|
23
|
+
* control WebSocket. Deliberate β but note it does NOT help the tunnel
|
|
24
|
+
* DATA plane, which is a raw `net.connect` and bypasses proxies entirely.
|
|
25
|
+
* `lib/tunnel-session.ts` warns about that asymmetry rather than
|
|
26
|
+
* pretending a proxied environment is supported.
|
|
27
|
+
*
|
|
28
|
+
* ## Why `close()` cannot just call `WebSocket.close()` and wait
|
|
29
|
+
*
|
|
30
|
+
* Read straight from undici's own source (`lib/web/websocket/connection.js`):
|
|
31
|
+
* `WebSocket.close()` on an OPEN connection sends a Close frame and sets the
|
|
32
|
+
* ready state to CLOSING β nothing more. It does not abort the underlying
|
|
33
|
+
* connection and does not time out; that only happens for a CONNECTING
|
|
34
|
+
* socket (`failWebsocketConnection`, upstream `client.ts` VENDOR DELTA #8's
|
|
35
|
+
* territory). If the peer never sends its own Close frame back β a redeploy
|
|
36
|
+
* mid-handshake, a proxy that swallows the FIN, a server too busy to answer β
|
|
37
|
+
* the ready state sits at CLOSING forever, the JS `'close'` event never
|
|
38
|
+
* fires, and the socket stays an active libuv handle. Confirmed empirically:
|
|
39
|
+
* against a stub that completes the WS upgrade and then never answers a
|
|
40
|
+
* Close frame, `TunnelClient.stop()` (which awaits exactly this event) never
|
|
41
|
+
* resolves, and `process.getActiveResourcesInfo()` shows the same
|
|
42
|
+
* `TCPSocketWrap` present indefinitely. There is no public undici API to
|
|
43
|
+
* force it β `#handler.socket` is a true private class field, and even a
|
|
44
|
+
* dedicated per-connection `Dispatcher.destroy()` does nothing once the
|
|
45
|
+
* connection has been upgraded (verified: undici hands the socket off to the
|
|
46
|
+
* `WebSocket` instance at that point, detaching it from the dispatcher's own
|
|
47
|
+
* bookkeeping).
|
|
48
|
+
*
|
|
49
|
+
* So `close()` here arms a short local grace timer alongside the real
|
|
50
|
+
* WHATWG close. If the peer answers within it, the real `'close'` event
|
|
51
|
+
* settles everything and the timer is a no-op. If it doesn't, we settle
|
|
52
|
+
* locally instead of waiting forever:
|
|
53
|
+
*
|
|
54
|
+
* - `.unref()` (never `.destroy()`) the raw socket, captured at connect
|
|
55
|
+
* time via undici's documented `undici:client:connected` diagnostics
|
|
56
|
+
* channel β a public, if "Experimental"-labeled, hook that hands us the
|
|
57
|
+
* `net.Socket` undici itself dialed, filtered to this connection's own
|
|
58
|
+
* host/port so an unrelated concurrent undici connection is never
|
|
59
|
+
* mistaken for it. `unref()` is the right tool and `.destroy()` is not:
|
|
60
|
+
* by the time the grace period expires we have already sent our own
|
|
61
|
+
* Close frame and have no further use for the socket β there is no
|
|
62
|
+
* work left that it is the last thing holding open, which is exactly
|
|
63
|
+
* the bar this codebase's timer-`unref` doctrine sets (contrast the
|
|
64
|
+
* rejected fix elsewhere in this repo, where `unref`ing a POLLING
|
|
65
|
+
* timer let a process exit before its work was done β this is the
|
|
66
|
+
* opposite situation, a corpse the peer refuses to bury after the work
|
|
67
|
+
* is already finished). `.destroy()` would instead reset the still-open
|
|
68
|
+
* TCP connection out from under a peer that might complete its side a
|
|
69
|
+
* moment later, for no benefit we need.
|
|
70
|
+
* - Synthesize the `'close'` event ourselves (code 1006, "abnormal
|
|
71
|
+
* closure" β the conventional code for "the connection just went away",
|
|
72
|
+
* and never confused with an auth failure: `client.ts`'s
|
|
73
|
+
* `isAuthFailureClose` only matches code 1008) so `client.ts`'s own
|
|
74
|
+
* unmodified `ws.on("close", ...)` handler runs exactly as it would for
|
|
75
|
+
* a real close: it clears the heartbeat interval, resolves the
|
|
76
|
+
* `connectControl()` promise `runControlLoop` is stuck awaiting, and
|
|
77
|
+
* unwinds `stop()`'s own `Promise.allSettled`. No vendored file needs to
|
|
78
|
+
* change for this β the synthetic close travels through the exact same,
|
|
79
|
+
* already-correct listener.
|
|
80
|
+
*
|
|
81
|
+
* A proxied connection is NOT a miss, which is worth stating because it looks
|
|
82
|
+
* like one: with `EnvHttpProxyAgent` installed as the global dispatcher,
|
|
83
|
+
* undici publishes TWO `undici:client:connected` events β the physical dial to
|
|
84
|
+
* the proxy, whose `connectParams` carry the PROXY's host/port, and a second
|
|
85
|
+
* one from the per-origin `Client` it creates internally, whose `connectParams`
|
|
86
|
+
* carry the TARGET's. The filter below matches the second and captures the
|
|
87
|
+
* same live socket (physically connected to the proxy, tunneled to the
|
|
88
|
+
* target). Verified against undici 7.29.0 with a real CONNECT proxy: a `--local`
|
|
89
|
+
* teardown under `HTTP_PROXY` exits in the same ~1.4s as without one. The `wss:`
|
|
90
|
+
* variant TLS-wraps that tunneled socket and was not exercised β it is
|
|
91
|
+
* analogous by code reading only.
|
|
92
|
+
*
|
|
93
|
+
* If capture DOES miss β the channel never fires, or a future undici publishes
|
|
94
|
+
* a different payload shape β the synthetic close still clears the heartbeat,
|
|
95
|
+
* but the control socket stays ref'd and **the original hang comes back**. That
|
|
96
|
+
* is the honest statement: this is not a socket "left for process exit to
|
|
97
|
+
* reap", because nothing forces this process to exit β a ref'd socket IS the
|
|
98
|
+
* bug. Treat any change to the filter as load-bearing. Never throws either way:
|
|
99
|
+
* a diagnostics hook failing must not break teardown.
|
|
100
|
+
*/
|
|
101
|
+
type CloseListener = (code: number, reason: Buffer) => void;
|
|
102
|
+
type MessageListener = (payload: string | Buffer) => void;
|
|
103
|
+
type ErrorListener = (err: Error) => void;
|
|
104
|
+
type OpenListener = () => void;
|
|
105
|
+
type PingListener = (data: Buffer) => void;
|
|
106
|
+
/**
|
|
107
|
+
* How long `close()` waits for the peer to finish the WHATWG closing
|
|
108
|
+
* handshake before giving up and settling locally (see the class docstring).
|
|
109
|
+
* Deliberately well under `TUNNEL_CLIENT_STOP_TIMEOUT_MS`
|
|
110
|
+
* (`lib/tunnel-session.ts`, 2000ms) so the caller's own bounded race around
|
|
111
|
+
* `client.stop()` sees OUR settle win, not its own timeout race β the
|
|
112
|
+
* process should learn its socket/timer are released, not just stop waiting
|
|
113
|
+
* to hear about it.
|
|
114
|
+
*/
|
|
115
|
+
export declare const CLOSE_GRACE_MS = 1200;
|
|
116
|
+
/** The subset of `undici:client:connected`'s `connectParams` this filter reads. */
|
|
117
|
+
export interface ConnectParams {
|
|
118
|
+
hostname?: unknown;
|
|
119
|
+
port?: unknown;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Does this `undici:client:connected` event belong to the WebSocket we are
|
|
123
|
+
* trying to capture?
|
|
124
|
+
*
|
|
125
|
+
* Exported, and its own function, because it is load-bearing and subtle: a
|
|
126
|
+
* miss here is silent, and its consequence is the original close-hang coming
|
|
127
|
+
* back with every test still green (see the class docstring). Reviewing a
|
|
128
|
+
* predicate is easier than reviewing it inlined in a subscription callback.
|
|
129
|
+
*
|
|
130
|
+
* Both ports are normalized to the scheme default, and that symmetry is the
|
|
131
|
+
* fix: undici publishes `connectParams.port` straight from a WHATWG URL, which
|
|
132
|
+
* leaves a default-scheme port IMPLICIT β `ws://host/control` arrives as `''`,
|
|
133
|
+
* and `Number('') === 0`. The target side was already being defaulted to
|
|
134
|
+
* 80/443 while the event side was not, so capture missed for every
|
|
135
|
+
* implicit-port control URL, proxy or no proxy.
|
|
136
|
+
*
|
|
137
|
+
* Host is compared exactly. A proxied connection publishes two events β the
|
|
138
|
+
* physical dial to the proxy, and a second one from the per-origin `Client`
|
|
139
|
+
* undici creates internally β and only the second carries the target's host,
|
|
140
|
+
* which is precisely the one worth capturing.
|
|
141
|
+
*/
|
|
142
|
+
export declare function connectEventMatchesTarget(connectParams: ConnectParams | undefined, target: URL): boolean;
|
|
143
|
+
/**
|
|
144
|
+
* Normalise a WHATWG message payload to the `string | Buffer` union the
|
|
145
|
+
* vendored client's `typeof payload === "string" ? β¦ : payload.toString()`
|
|
146
|
+
* branch expects. Text frames arrive as strings; a binary frame (which this
|
|
147
|
+
* protocol never sends) arrives as an ArrayBuffer and is wrapped rather than
|
|
148
|
+
* dropped, so an unexpected frame surfaces as a JSON parse failure instead of
|
|
149
|
+
* a `payload.toString is not a function` crash.
|
|
150
|
+
*/
|
|
151
|
+
export declare function toPayload(data: unknown): string | Buffer;
|
|
152
|
+
/**
|
|
153
|
+
* Pull a real `Error` out of undici's ErrorEvent. `.error` is typed as an
|
|
154
|
+
* `Error` but is not guaranteed to be populated for every failure mode, so
|
|
155
|
+
* `.message` is the fallback and a generic error is the floor β the vendored
|
|
156
|
+
* client passes whatever comes out to `toErrorMessage()` and to `reject()`.
|
|
157
|
+
*/
|
|
158
|
+
export declare function toError(event: unknown): Error;
|
|
159
|
+
export declare class WsCompatSocket {
|
|
160
|
+
static readonly CONNECTING = 0;
|
|
161
|
+
static readonly OPEN = 1;
|
|
162
|
+
private readonly socket;
|
|
163
|
+
/** Captured via `undici:client:connected`, if it fired β see the class docstring. */
|
|
164
|
+
private rawSocket;
|
|
165
|
+
private closeListener;
|
|
166
|
+
private closeSettled;
|
|
167
|
+
private closeGraceTimer;
|
|
168
|
+
private readonly unsubscribeConnected;
|
|
169
|
+
constructor(url: URL | string);
|
|
170
|
+
/**
|
|
171
|
+
* Subscribe to `undici:client:connected` just long enough to grab the raw
|
|
172
|
+
* `net.Socket` for THIS connection, filtered by target host/port so an
|
|
173
|
+
* unrelated concurrent undici connection elsewhere in the process (e.g.
|
|
174
|
+
* this CLI's own HTTP polling) is never mistaken for it. See the class
|
|
175
|
+
* docstring for why `close()` needs this at all. Returns an idempotent
|
|
176
|
+
* unsubscribe closure. Never throws: a diagnostics hook failing must not
|
|
177
|
+
* break the connection it exists only to make teardown more graceful for.
|
|
178
|
+
*/
|
|
179
|
+
private captureRawSocket;
|
|
180
|
+
get readyState(): number;
|
|
181
|
+
on(event: 'open', listener: OpenListener): this;
|
|
182
|
+
on(event: 'message', listener: MessageListener): this;
|
|
183
|
+
on(event: 'error', listener: ErrorListener): this;
|
|
184
|
+
on(event: 'close', listener: CloseListener): this;
|
|
185
|
+
on(event: 'ping', listener: PingListener): this;
|
|
186
|
+
send(data: string): void;
|
|
187
|
+
close(code?: number, reason?: string): void;
|
|
188
|
+
/** No-op β see the module docstring. */
|
|
189
|
+
pong(_data?: Buffer): void;
|
|
190
|
+
/**
|
|
191
|
+
* Invoke the registered close listener exactly once, whichever of "the
|
|
192
|
+
* real WHATWG close event" or "our own grace-timeout settle" gets there
|
|
193
|
+
* first β a real close that arrives after the grace timer already fired is
|
|
194
|
+
* a no-op here, and vice versa.
|
|
195
|
+
*/
|
|
196
|
+
private settleClose;
|
|
197
|
+
/**
|
|
198
|
+
* See the class docstring's "Why `close()` cannot just call
|
|
199
|
+
* `WebSocket.close()` and wait" section. A no-op if the real close has
|
|
200
|
+
* already settled synchronously (true for a CONNECTING socket β undici's
|
|
201
|
+
* own `failWebsocketConnection` handles that case completely, with no
|
|
202
|
+
* grace period needed) or if a grace timer is already armed.
|
|
203
|
+
*/
|
|
204
|
+
private armCloseGrace;
|
|
205
|
+
}
|
|
206
|
+
export default WsCompatSocket;
|