@ant0/cli 0.0.0-stage → 0.3.22-beta.8
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/LICENSE +28 -0
- package/README.md +97 -2
- package/dist/api/generated/schema.d.ts +11615 -0
- package/dist/api/generated/schema.js +6 -0
- package/dist/api/index.d.ts +219 -0
- package/dist/api/index.js +342 -0
- package/dist/bin.d.ts +3 -0
- package/dist/bin.js +12 -0
- package/dist/bulk.d.ts +38 -0
- package/dist/bulk.js +66 -0
- package/dist/commands/account.d.ts +48 -0
- package/dist/commands/account.js +337 -0
- package/dist/commands/api-keys.d.ts +5 -0
- package/dist/commands/api-keys.js +310 -0
- package/dist/commands/browsers.d.ts +4 -0
- package/dist/commands/browsers.js +165 -0
- package/dist/commands/daemon.d.ts +15 -0
- package/dist/commands/daemon.js +202 -0
- package/dist/commands/events.d.ts +8 -0
- package/dist/commands/events.js +81 -0
- package/dist/commands/extensions.d.ts +4 -0
- package/dist/commands/extensions.js +74 -0
- package/dist/commands/profiles.d.ts +36 -0
- package/dist/commands/profiles.js +552 -0
- package/dist/commands/proxies.d.ts +23 -0
- package/dist/commands/proxies.js +170 -0
- package/dist/commands/sync.d.ts +4 -0
- package/dist/commands/sync.js +435 -0
- package/dist/commands/team.d.ts +7 -0
- package/dist/commands/team.js +403 -0
- package/dist/commands/trash.d.ts +10 -0
- package/dist/commands/trash.js +54 -0
- package/dist/commands/workspace.d.ts +10 -0
- package/dist/commands/workspace.js +56 -0
- package/dist/context.d.ts +111 -0
- package/dist/context.js +300 -0
- package/dist/errors.d.ts +27 -0
- package/dist/errors.js +96 -0
- package/dist/format.d.ts +23 -0
- package/dist/format.js +85 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +35 -0
- package/dist/inventory.d.ts +50 -0
- package/dist/inventory.js +202 -0
- package/dist/management-transport.d.ts +3 -0
- package/dist/management-transport.js +90 -0
- package/dist/native-terminal.d.ts +50 -0
- package/dist/native-terminal.js +228 -0
- package/dist/program.d.ts +10 -0
- package/dist/program.js +217 -0
- package/dist/runtime-types.d.ts +20 -0
- package/dist/runtime-types.js +2 -0
- package/dist/set.d.ts +15 -0
- package/dist/set.js +87 -0
- package/dist/shared/parse-error.d.ts +6 -0
- package/dist/shared/parse-error.js +8 -0
- package/dist/shared/proxy-parse.d.ts +72 -0
- package/dist/shared/proxy-parse.js +294 -0
- package/dist/shared/proxy-types.d.ts +21 -0
- package/dist/shared/proxy-types.js +2 -0
- package/dist/shared/recovery-code.d.ts +7 -0
- package/dist/shared/recovery-code.js +53 -0
- package/dist/version.d.ts +8 -0
- package/dist/version.js +18 -0
- package/licenses/README.md +5 -0
- package/licenses/THIRD_PARTY_NOTICES +96 -0
- package/package.json +63 -4
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { Ant0ApiError, isAnt0ApiError } from "./api/index.js";
|
|
2
|
+
import { CliError, isConnectionRefused } from "./errors.js";
|
|
3
|
+
const invalid = () => new CliError("invalid inventory response");
|
|
4
|
+
function object(value) {
|
|
5
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
6
|
+
throw invalid();
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
function text(value, max = 256) {
|
|
10
|
+
if (typeof value !== "string" || value.length > max)
|
|
11
|
+
throw invalid();
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
function bool(value) {
|
|
15
|
+
if (typeof value !== "boolean")
|
|
16
|
+
throw invalid();
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
function oneOf(value, values) {
|
|
20
|
+
if (typeof value !== "string" || !values.includes(value))
|
|
21
|
+
throw invalid();
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
function nullable(value, parse) {
|
|
25
|
+
return value === null ? null : parse(value);
|
|
26
|
+
}
|
|
27
|
+
function uuid(value) {
|
|
28
|
+
const result = text(value, 36);
|
|
29
|
+
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(result))
|
|
30
|
+
throw invalid();
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
function folderUuid(value) {
|
|
34
|
+
const result = text(value, 36);
|
|
35
|
+
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(result))
|
|
36
|
+
throw invalid();
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
function date(value) {
|
|
40
|
+
const result = text(value, 40);
|
|
41
|
+
if (!/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d+)?(?:Z|[+-]\d\d:\d\d)$/.test(result) ||
|
|
42
|
+
!Number.isFinite(Date.parse(result)) ||
|
|
43
|
+
new Date(`${result.slice(0, 10)}T00:00:00Z`).toISOString().slice(0, 10) !== result.slice(0, 10))
|
|
44
|
+
throw invalid();
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
function finite(value) {
|
|
48
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
49
|
+
throw invalid();
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
function count(value) {
|
|
53
|
+
const result = finite(value);
|
|
54
|
+
if (!Number.isSafeInteger(result) || result < 0)
|
|
55
|
+
throw invalid();
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
function summary(value) {
|
|
59
|
+
const p = object(value);
|
|
60
|
+
if (!Array.isArray(p.tags) || p.tags.length > 1000)
|
|
61
|
+
throw invalid();
|
|
62
|
+
// Explicit copies keep future or unexpected secret fields out of JSON and table output.
|
|
63
|
+
return {
|
|
64
|
+
id: uuid(p.id),
|
|
65
|
+
name: text(p.name),
|
|
66
|
+
group: nullable(p.group, text),
|
|
67
|
+
local_folder_id: nullable(p.local_folder_id, folderUuid),
|
|
68
|
+
tags: p.tags.map((tag) => text(tag, 128)),
|
|
69
|
+
notes: text(p.notes, 16000),
|
|
70
|
+
status: nullable(p.status, text),
|
|
71
|
+
pinned: bool(p.pinned),
|
|
72
|
+
platform: nullable(p.platform, (v) => oneOf(v, ["windows", "linux", "macos", "android"])),
|
|
73
|
+
proxy_id: nullable(p.proxy_id, uuid),
|
|
74
|
+
browser_version: nullable(p.browser_version, text),
|
|
75
|
+
identity_mode: oneOf(p.identity_mode, ["synthetic", "captured", "native"]),
|
|
76
|
+
created_at: date(p.created_at),
|
|
77
|
+
updated_at: date(p.updated_at),
|
|
78
|
+
last_launched_at: nullable(p.last_launched_at, date),
|
|
79
|
+
deleted_at: nullable(p.deleted_at, date),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function lifecycle(value) {
|
|
83
|
+
const p = object(value);
|
|
84
|
+
const result = {
|
|
85
|
+
profile_id: uuid(p.profile_id),
|
|
86
|
+
state: oneOf(p.state, ["starting", "running", "stopping", "stopped", "crashed"]),
|
|
87
|
+
started_at: nullable(p.started_at, date),
|
|
88
|
+
browser_version: nullable(p.browser_version, text),
|
|
89
|
+
headless: nullable(p.headless, bool),
|
|
90
|
+
};
|
|
91
|
+
const fields = [result.started_at, result.browser_version, result.headless];
|
|
92
|
+
if (!fields.every((field) => field !== null) &&
|
|
93
|
+
!(result.state !== "running" && fields.every((field) => field === null)))
|
|
94
|
+
throw invalid();
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
function proxy(value) {
|
|
98
|
+
const p = object(value);
|
|
99
|
+
return {
|
|
100
|
+
id: uuid(p.id),
|
|
101
|
+
type: oneOf(p.type, ["http", "https", "socks5", "socks4"]),
|
|
102
|
+
last_check: nullable(p.last_check, (value) => {
|
|
103
|
+
const check = object(value);
|
|
104
|
+
return {
|
|
105
|
+
ok: bool(check.ok),
|
|
106
|
+
country: nullable(check.country, text),
|
|
107
|
+
latency_ms: nullable(check.latency_ms, finite),
|
|
108
|
+
at: date(check.at),
|
|
109
|
+
};
|
|
110
|
+
}),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
export function projectProfileInventory(value, readOnly = false) {
|
|
114
|
+
const item = object(value);
|
|
115
|
+
const permissions = object(item.permissions);
|
|
116
|
+
const result = {
|
|
117
|
+
profile: summary(item.profile),
|
|
118
|
+
lifecycle: lifecycle(item.lifecycle),
|
|
119
|
+
proxy: nullable(item.proxy, proxy),
|
|
120
|
+
permissions: { edit: bool(permissions.edit), cdp: bool(permissions.cdp) },
|
|
121
|
+
// Absent on a daemon that does not report a per-row status: treat as ok.
|
|
122
|
+
authority: item.authority === undefined ? "ok" : oneOf(item.authority, ["ok", "needs_review"]),
|
|
123
|
+
authority_reason: item.authority_reason === undefined
|
|
124
|
+
? null
|
|
125
|
+
: nullable(item.authority_reason, (value) => oneOf(value, ["legacy_unclassified", "restricted_origin_missing"])),
|
|
126
|
+
};
|
|
127
|
+
if ((readOnly && (result.permissions.edit || result.permissions.cdp)) ||
|
|
128
|
+
result.profile.id !== result.lifecycle.profile_id ||
|
|
129
|
+
(result.proxy !== null && result.proxy.id !== result.profile.proxy_id))
|
|
130
|
+
throw invalid();
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
export function projectProfileList(value, limit = 100, offset = 0, running, readOnly = false) {
|
|
134
|
+
const list = object(value);
|
|
135
|
+
const total = count(list.total);
|
|
136
|
+
if (!Array.isArray(list.items) ||
|
|
137
|
+
list.items.length > 1000 ||
|
|
138
|
+
list.items.length !== Math.min(limit, Math.max(0, total - offset)))
|
|
139
|
+
throw invalid();
|
|
140
|
+
const items = list.items.map((item) => projectProfileInventory(item, readOnly));
|
|
141
|
+
if (new Set(items.map((item) => item.profile.id)).size !== items.length ||
|
|
142
|
+
items.some((item) => running !== undefined && (item.lifecycle.state === "running") !== running))
|
|
143
|
+
throw invalid();
|
|
144
|
+
const result = { items: items.map((item) => item.profile), total };
|
|
145
|
+
if (Buffer.byteLength(JSON.stringify(result)) > 8 * 1024 * 1024)
|
|
146
|
+
throw new CliError("inventory result exceeds limit; request fewer items");
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
export function projectProfileStatus(value, ref, readOnly = false) {
|
|
150
|
+
const item = projectProfileInventory(value, readOnly);
|
|
151
|
+
if (item.profile.id.toLowerCase() !== ref.toLowerCase() && item.profile.name !== ref)
|
|
152
|
+
throw invalid();
|
|
153
|
+
const s = item.lifecycle;
|
|
154
|
+
return {
|
|
155
|
+
state: s.state,
|
|
156
|
+
session: s.started_at === null
|
|
157
|
+
? null
|
|
158
|
+
: {
|
|
159
|
+
profile_id: s.profile_id,
|
|
160
|
+
started_at: s.started_at,
|
|
161
|
+
browser_version: s.browser_version,
|
|
162
|
+
headless: s.headless,
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
export function projectProxyList(value, limit, offset) {
|
|
167
|
+
const list = object(value);
|
|
168
|
+
const total = count(list.total);
|
|
169
|
+
if (!Array.isArray(list.items) ||
|
|
170
|
+
list.items.length !== Math.min(limit, Math.max(0, total - offset)))
|
|
171
|
+
throw invalid();
|
|
172
|
+
const items = list.items.map(proxy);
|
|
173
|
+
if (new Set(items.map((item) => item.id)).size !== items.length)
|
|
174
|
+
throw invalid();
|
|
175
|
+
return { items, total };
|
|
176
|
+
}
|
|
177
|
+
/** Keep API error identity/status while rejecting raw upstream diagnostics on metadata reads. */
|
|
178
|
+
export async function inventoryRead(read) {
|
|
179
|
+
try {
|
|
180
|
+
return await read();
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
if (isAnt0ApiError(error)) {
|
|
184
|
+
const known = [
|
|
185
|
+
"profile.not_found",
|
|
186
|
+
"team.forbidden",
|
|
187
|
+
"sync.store_unavailable",
|
|
188
|
+
"sync.conflict",
|
|
189
|
+
"sync.offline",
|
|
190
|
+
"sync.unauthorized",
|
|
191
|
+
"unauthorized",
|
|
192
|
+
"forbidden",
|
|
193
|
+
"validation_failed",
|
|
194
|
+
];
|
|
195
|
+
throw new Ant0ApiError(error.status, known.includes(error.code) ? error.code : `http_${error.status}`, "inventory unavailable");
|
|
196
|
+
}
|
|
197
|
+
if (error instanceof CliError || isConnectionRefused(error))
|
|
198
|
+
throw error;
|
|
199
|
+
throw new CliError("inventory unavailable");
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
//# sourceMappingURL=inventory.js.map
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { request } from "node:http";
|
|
2
|
+
import { canonicalOrigin } from "@ant0/mcp";
|
|
3
|
+
import { CliError } from "./errors.js";
|
|
4
|
+
/** Direct loopback HTTP: deliberately independent of global fetch/Undici/environment proxies. */
|
|
5
|
+
export const directManagementFetch = async (input) => {
|
|
6
|
+
if (!(input instanceof Request))
|
|
7
|
+
throw new CliError("invalid API key request");
|
|
8
|
+
const url = new URL(input.url);
|
|
9
|
+
if (canonicalOrigin(url.origin) !== url.origin ||
|
|
10
|
+
url.username ||
|
|
11
|
+
url.password ||
|
|
12
|
+
url.hash ||
|
|
13
|
+
!/^\/v1\/automation\/api-keys(?:\/(?:context|provision|[0-9a-f-]{36}))?$/.test(url.pathname) ||
|
|
14
|
+
!["GET", "POST", "PATCH", "DELETE"].includes(input.method))
|
|
15
|
+
throw new CliError("invalid local API key management destination");
|
|
16
|
+
const body = Buffer.from(await input.arrayBuffer());
|
|
17
|
+
if (body.length > 64 * 1024)
|
|
18
|
+
throw new CliError("API key request exceeds limit");
|
|
19
|
+
const signal = AbortSignal.any([input.signal, AbortSignal.timeout(10_000)]);
|
|
20
|
+
if (signal.aborted)
|
|
21
|
+
throw new CliError("API key request cancelled");
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
let settled = false;
|
|
24
|
+
const req = request(url, {
|
|
25
|
+
method: input.method,
|
|
26
|
+
agent: false,
|
|
27
|
+
maxHeaderSize: 16 * 1024,
|
|
28
|
+
headers: {
|
|
29
|
+
authorization: input.headers.get("authorization") ?? "",
|
|
30
|
+
accept: "application/json",
|
|
31
|
+
"accept-encoding": "identity",
|
|
32
|
+
...(body.length
|
|
33
|
+
? { "content-type": "application/json", "content-length": body.length }
|
|
34
|
+
: {}),
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
const closed = new Promise((done) => req.once("close", done));
|
|
38
|
+
const finish = (error, response) => {
|
|
39
|
+
if (settled)
|
|
40
|
+
return;
|
|
41
|
+
settled = true;
|
|
42
|
+
signal.removeEventListener("abort", abort);
|
|
43
|
+
if (error)
|
|
44
|
+
req.destroy();
|
|
45
|
+
void closed.then(() => (error ? reject(new CliError(error)) : resolve(response)));
|
|
46
|
+
};
|
|
47
|
+
const abort = () => finish("API key request cancelled or timed out");
|
|
48
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
49
|
+
req.on("error", () => finish("API key operation unavailable"));
|
|
50
|
+
req.on("response", (res) => {
|
|
51
|
+
const status = res.statusCode ?? 500;
|
|
52
|
+
if (status >= 300 && status < 400)
|
|
53
|
+
return finish("API key redirect refused");
|
|
54
|
+
if (res.headers["content-encoding"] && res.headers["content-encoding"] !== "identity")
|
|
55
|
+
return finish("invalid API key response");
|
|
56
|
+
if (Number(res.headers["content-length"]) > 1024 * 1024)
|
|
57
|
+
return finish("API key response exceeds limit");
|
|
58
|
+
let size = 0;
|
|
59
|
+
const chunks = [];
|
|
60
|
+
res.on("error", () => finish("invalid API key response"));
|
|
61
|
+
res.on("data", (chunk) => {
|
|
62
|
+
size += chunk.length;
|
|
63
|
+
if (size > 1024 * 1024)
|
|
64
|
+
return finish("API key response exceeds limit");
|
|
65
|
+
chunks.push(chunk);
|
|
66
|
+
});
|
|
67
|
+
res.on("end", () => {
|
|
68
|
+
try {
|
|
69
|
+
const text = new TextDecoder("utf-8", { fatal: true }).decode(Buffer.concat(chunks));
|
|
70
|
+
finish(undefined, new Response(status === 204 ? null : text, {
|
|
71
|
+
status,
|
|
72
|
+
headers: { "content-type": "application/json" },
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
finish("invalid API key response");
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
res.on("close", () => {
|
|
80
|
+
if (!res.complete)
|
|
81
|
+
finish("invalid API key response");
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
if (signal.aborted)
|
|
85
|
+
abort();
|
|
86
|
+
else
|
|
87
|
+
req.end(body.length ? body : undefined);
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
//# sourceMappingURL=management-transport.js.map
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export interface NativeTerminal {
|
|
2
|
+
readSecret(question: string): Promise<Buffer>;
|
|
3
|
+
writeLine(text: string): void;
|
|
4
|
+
writeSecret(label: string, value: Uint8Array): void;
|
|
5
|
+
close(): void;
|
|
6
|
+
}
|
|
7
|
+
/** The subset of a TTY read stream the secret prompt needs (a `tty.ReadStream` or `process.stdin`). */
|
|
8
|
+
export interface TerminalInput {
|
|
9
|
+
isTTY?: boolean;
|
|
10
|
+
isRaw?: boolean;
|
|
11
|
+
setRawMode(mode: boolean): unknown;
|
|
12
|
+
resume(): unknown;
|
|
13
|
+
pause(): unknown;
|
|
14
|
+
destroy(): unknown;
|
|
15
|
+
on(event: string, listener: (...args: any[]) => void): unknown;
|
|
16
|
+
once(event: string, listener: (...args: any[]) => void): unknown;
|
|
17
|
+
off(event: string, listener: (...args: any[]) => void): unknown;
|
|
18
|
+
}
|
|
19
|
+
/** Overrides for tests; production code uses the defaults. */
|
|
20
|
+
export interface OpenNativeTerminalOptions {
|
|
21
|
+
/** Process stdin; on Windows it is the secret input, but only when it is a real console. */
|
|
22
|
+
stdin?: TerminalInput;
|
|
23
|
+
open?: (path: string, flags: string) => number;
|
|
24
|
+
close?: (fd: number) => void;
|
|
25
|
+
isTty?: (fd: number) => boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface TerminalDevicePaths {
|
|
28
|
+
input: string;
|
|
29
|
+
/** `fs.open` flags for the input device. */
|
|
30
|
+
inputFlags: string;
|
|
31
|
+
output: string;
|
|
32
|
+
/** `fs.open` flags for the output device. */
|
|
33
|
+
outputFlags: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Console device paths per platform. Windows needs the `\\.\` device namespace: under Node a bare
|
|
37
|
+
* `CONIN$` fails with ENOENT and `CONOUT$` opened write-only (create/truncate) with EPERM. Both
|
|
38
|
+
* are opened read+write without create/truncate: `CONIN$` opened read-only lacks the access
|
|
39
|
+
* `setRawMode` needs (EPERM). {@link openNativeTerminal} does not open `CONIN$` on Windows at all
|
|
40
|
+
* (it uses a TTY `process.stdin`), but the path stays correct for any caller that does.
|
|
41
|
+
*/
|
|
42
|
+
export declare function terminalDevicePaths(platform: NodeJS.Platform): TerminalDevicePaths;
|
|
43
|
+
/**
|
|
44
|
+
* Open `/dev/tty`, or on Windows the console. A hidden console (for example a non-pty SSH session)
|
|
45
|
+
* still opens as `CONIN$` but nobody can type into it, so on Windows the secret input is
|
|
46
|
+
* `process.stdin` only when it is itself a TTY (the real console, not a redirect); otherwise this
|
|
47
|
+
* throws and the caller shows its "needs a terminal" message.
|
|
48
|
+
*/
|
|
49
|
+
export declare function openNativeTerminal(platform?: NodeJS.Platform, options?: OpenNativeTerminalOptions): NativeTerminal;
|
|
50
|
+
//# sourceMappingURL=native-terminal.d.ts.map
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/** Direct controlling-terminal I/O for secrets that must never use redirected process streams. */
|
|
2
|
+
import { closeSync, openSync, writeSync } from "node:fs";
|
|
3
|
+
import { isatty, ReadStream } from "node:tty";
|
|
4
|
+
import { UsageError } from "./errors.js";
|
|
5
|
+
const READ_FAILURE = "could not read from the controlling terminal";
|
|
6
|
+
/** Step plus errno code only; never anything derived from input bytes. */
|
|
7
|
+
function readFailure(step, error) {
|
|
8
|
+
const failure = error;
|
|
9
|
+
const code = typeof failure?.code === "string" && /^[A-Z0-9_]{1,32}$/.test(failure.code)
|
|
10
|
+
? failure.code
|
|
11
|
+
: "unknown";
|
|
12
|
+
const failedStep = failure?.syscall === "setRawMode" ||
|
|
13
|
+
(typeof failure?.message === "string" && failure.message.includes("setRawMode"))
|
|
14
|
+
? "setRawMode"
|
|
15
|
+
: step;
|
|
16
|
+
return new UsageError(`${READ_FAILURE} (${failedStep}: ${code})`);
|
|
17
|
+
}
|
|
18
|
+
class DeviceTerminal {
|
|
19
|
+
input;
|
|
20
|
+
outputFd;
|
|
21
|
+
ownsInput;
|
|
22
|
+
closeFd;
|
|
23
|
+
closed = false;
|
|
24
|
+
/** Keeps a late stream error (after a prompt finished) from becoming an uncaught exception. */
|
|
25
|
+
swallowError = () => { };
|
|
26
|
+
constructor(input, outputFd,
|
|
27
|
+
/** False for process.stdin, which is shared and must be left open. */
|
|
28
|
+
ownsInput, closeFd = closeSync) {
|
|
29
|
+
this.input = input;
|
|
30
|
+
this.outputFd = outputFd;
|
|
31
|
+
this.ownsInput = ownsInput;
|
|
32
|
+
this.closeFd = closeFd;
|
|
33
|
+
input.on("error", this.swallowError);
|
|
34
|
+
}
|
|
35
|
+
write(value) {
|
|
36
|
+
const bytes = typeof value === "string" ? Buffer.from(value) : value;
|
|
37
|
+
let offset = 0;
|
|
38
|
+
while (offset < bytes.byteLength) {
|
|
39
|
+
offset += writeSync(this.outputFd, bytes, offset, bytes.byteLength - offset);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async readSecret(question) {
|
|
43
|
+
if (this.closed)
|
|
44
|
+
throw new UsageError("controlling terminal is closed");
|
|
45
|
+
const input = this.input;
|
|
46
|
+
const wasRaw = input.isRaw;
|
|
47
|
+
const value = Buffer.alloc(1024);
|
|
48
|
+
let length = 0;
|
|
49
|
+
this.write(question);
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
let settled = false;
|
|
52
|
+
const finish = (error) => {
|
|
53
|
+
if (settled)
|
|
54
|
+
return;
|
|
55
|
+
settled = true;
|
|
56
|
+
input.off("data", onData);
|
|
57
|
+
input.off("error", onError);
|
|
58
|
+
input.off("end", onEnd);
|
|
59
|
+
input.off("close", onClose);
|
|
60
|
+
let finalError = error;
|
|
61
|
+
try {
|
|
62
|
+
input.setRawMode(wasRaw === true);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
finalError ??= new UsageError("could not restore the controlling terminal");
|
|
66
|
+
}
|
|
67
|
+
input.pause();
|
|
68
|
+
try {
|
|
69
|
+
this.write("\n");
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
finalError ??= new UsageError("could not write to the controlling terminal");
|
|
73
|
+
}
|
|
74
|
+
if (finalError) {
|
|
75
|
+
value.fill(0);
|
|
76
|
+
reject(finalError);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const result = Buffer.from(value.subarray(0, length));
|
|
80
|
+
value.fill(0);
|
|
81
|
+
resolve(result);
|
|
82
|
+
};
|
|
83
|
+
const onError = (error) => finish(readFailure("read", error));
|
|
84
|
+
const onEnd = () => finish(new UsageError("controlling terminal closed before input"));
|
|
85
|
+
const onClose = () => finish(new UsageError("controlling terminal closed before input"));
|
|
86
|
+
const onData = (chunk) => {
|
|
87
|
+
const bytes = typeof chunk === "string" ? Buffer.from(chunk, "utf8") : chunk;
|
|
88
|
+
try {
|
|
89
|
+
for (const byte of bytes) {
|
|
90
|
+
if (byte === 0x0d || byte === 0x0a)
|
|
91
|
+
return finish();
|
|
92
|
+
if (byte === 0x03 || byte === 0x04)
|
|
93
|
+
return finish(new UsageError("aborted"));
|
|
94
|
+
if (byte === 0x08 || byte === 0x7f) {
|
|
95
|
+
if (length > 0)
|
|
96
|
+
value[--length] = 0;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (byte < 0x20 || byte > 0x7e) {
|
|
100
|
+
return finish(new UsageError("secret input must contain printable ASCII only"));
|
|
101
|
+
}
|
|
102
|
+
if (length === value.length) {
|
|
103
|
+
return finish(new UsageError("secret input is too long"));
|
|
104
|
+
}
|
|
105
|
+
value[length++] = byte;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
bytes.fill(0);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
// Listeners first: Node reports a failed setRawMode as an 'error' event, which would
|
|
113
|
+
// otherwise be thrown or lost.
|
|
114
|
+
input.on("data", onData);
|
|
115
|
+
input.once("error", onError);
|
|
116
|
+
input.once("end", onEnd);
|
|
117
|
+
input.once("close", onClose);
|
|
118
|
+
try {
|
|
119
|
+
input.setRawMode(true);
|
|
120
|
+
input.resume();
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
finish(readFailure("setRawMode", error));
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
writeLine(text) {
|
|
128
|
+
if (this.closed)
|
|
129
|
+
throw new UsageError("controlling terminal is closed");
|
|
130
|
+
this.write(text.endsWith("\n") ? text : `${text}\n`);
|
|
131
|
+
}
|
|
132
|
+
writeSecret(label, value) {
|
|
133
|
+
if (this.closed)
|
|
134
|
+
throw new UsageError("controlling terminal is closed");
|
|
135
|
+
this.write(label);
|
|
136
|
+
this.write(value);
|
|
137
|
+
this.write("\n");
|
|
138
|
+
}
|
|
139
|
+
close() {
|
|
140
|
+
if (this.closed)
|
|
141
|
+
return;
|
|
142
|
+
this.closed = true;
|
|
143
|
+
try {
|
|
144
|
+
if (this.input.isRaw)
|
|
145
|
+
this.input.setRawMode(false);
|
|
146
|
+
}
|
|
147
|
+
finally {
|
|
148
|
+
try {
|
|
149
|
+
if (this.ownsInput)
|
|
150
|
+
this.input.destroy();
|
|
151
|
+
else
|
|
152
|
+
this.input.pause();
|
|
153
|
+
this.input.off("error", this.swallowError);
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
this.closeFd(this.outputFd);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Console device paths per platform. Windows needs the `\\.\` device namespace: under Node a bare
|
|
163
|
+
* `CONIN$` fails with ENOENT and `CONOUT$` opened write-only (create/truncate) with EPERM. Both
|
|
164
|
+
* are opened read+write without create/truncate: `CONIN$` opened read-only lacks the access
|
|
165
|
+
* `setRawMode` needs (EPERM). {@link openNativeTerminal} does not open `CONIN$` on Windows at all
|
|
166
|
+
* (it uses a TTY `process.stdin`), but the path stays correct for any caller that does.
|
|
167
|
+
*/
|
|
168
|
+
export function terminalDevicePaths(platform) {
|
|
169
|
+
if (platform === "win32") {
|
|
170
|
+
return {
|
|
171
|
+
input: "\\\\.\\CONIN$",
|
|
172
|
+
inputFlags: "r+",
|
|
173
|
+
output: "\\\\.\\CONOUT$",
|
|
174
|
+
outputFlags: "r+",
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
return { input: "/dev/tty", inputFlags: "r", output: "/dev/tty", outputFlags: "w" };
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Open `/dev/tty`, or on Windows the console. A hidden console (for example a non-pty SSH session)
|
|
181
|
+
* still opens as `CONIN$` but nobody can type into it, so on Windows the secret input is
|
|
182
|
+
* `process.stdin` only when it is itself a TTY (the real console, not a redirect); otherwise this
|
|
183
|
+
* throws and the caller shows its "needs a terminal" message.
|
|
184
|
+
*/
|
|
185
|
+
export function openNativeTerminal(platform = process.platform, options = {}) {
|
|
186
|
+
const open = options.open ?? openSync;
|
|
187
|
+
const closeFd = options.close ?? closeSync;
|
|
188
|
+
const isTty = options.isTty ?? isatty;
|
|
189
|
+
const paths = terminalDevicePaths(platform);
|
|
190
|
+
let inputFd;
|
|
191
|
+
let outputFd;
|
|
192
|
+
try {
|
|
193
|
+
let input;
|
|
194
|
+
let ownsInput;
|
|
195
|
+
if (platform === "win32") {
|
|
196
|
+
const stdin = options.stdin ?? process.stdin;
|
|
197
|
+
if (stdin.isTTY !== true || typeof stdin.setRawMode !== "function") {
|
|
198
|
+
throw new Error("stdin is not a console");
|
|
199
|
+
}
|
|
200
|
+
input = stdin;
|
|
201
|
+
ownsInput = false;
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
inputFd = open(paths.input, paths.inputFlags);
|
|
205
|
+
const stream = new ReadStream(inputFd);
|
|
206
|
+
inputFd = undefined;
|
|
207
|
+
input = stream;
|
|
208
|
+
ownsInput = true;
|
|
209
|
+
}
|
|
210
|
+
outputFd = open(paths.output, paths.outputFlags);
|
|
211
|
+
if (!input.isTTY || !isTty(outputFd)) {
|
|
212
|
+
if (ownsInput)
|
|
213
|
+
input.destroy();
|
|
214
|
+
throw new Error("terminal device is not a TTY");
|
|
215
|
+
}
|
|
216
|
+
const terminal = new DeviceTerminal(input, outputFd, ownsInput, closeFd);
|
|
217
|
+
outputFd = undefined;
|
|
218
|
+
return terminal;
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
if (inputFd !== undefined)
|
|
222
|
+
closeFd(inputFd);
|
|
223
|
+
if (outputFd !== undefined)
|
|
224
|
+
closeFd(outputFd);
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=native-terminal.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `ant0` command tree. Commander is configured to never call
|
|
3
|
+
* `process.exit` and to write through the injected streams, so `run()` is testable in-process.
|
|
4
|
+
*/
|
|
5
|
+
import { Command } from "commander";
|
|
6
|
+
import { Context } from "./context.js";
|
|
7
|
+
import { CLI_VERSION } from "./version.js";
|
|
8
|
+
export { CLI_VERSION };
|
|
9
|
+
export declare function buildProgram(ctx: Context): Command;
|
|
10
|
+
//# sourceMappingURL=program.d.ts.map
|