hopwire 0.2.0 → 0.3.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/dist/account.js +180 -35
- package/dist/agent/redact.js +17 -8
- package/dist/bin.js +136 -6
- package/dist/cli.js +116 -69
- package/dist/commands/doctor.js +19 -14
- package/dist/commands/http.js +3 -2
- package/dist/commands/init.js +10 -7
- package/dist/commands/login.js +47 -8
- package/dist/commands/up.js +3 -2
- package/dist/commands/watch.js +3 -3
- package/dist/config.js +7 -2
- package/dist/inspector/assets/{index-CIOfyIbK.js → index-CdyIIKdA.js} +1 -1
- package/dist/inspector/index.html +1 -1
- package/dist/live.js +226 -0
- package/dist/main.js +14 -3
- package/dist/output.js +19 -0
- package/dist/p2p.js +59 -1
- package/dist/relay.js +1 -1
- package/dist/serve.js +1 -0
- package/dist/shortcuts.js +217 -0
- package/dist/terminal.js +619 -0
- package/dist/version.js +12 -0
- package/dist/webhook.js +91 -0
- package/package.json +1 -1
package/dist/account.js
CHANGED
|
@@ -17,19 +17,86 @@ export function isApiToken(value) {
|
|
|
17
17
|
export function accountFile(dataDir) {
|
|
18
18
|
return path.join(dataDir, ACCOUNT_FILE);
|
|
19
19
|
}
|
|
20
|
+
/** The profile a machine has before anyone names one, and the one an older config.json is read as. */
|
|
21
|
+
export const DEFAULT_PROFILE = 'default';
|
|
22
|
+
/** A profile name: a letter or digit, then up to 31 letters, digits, - or _. */
|
|
23
|
+
const PROFILE_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_-]{0,31}$/;
|
|
24
|
+
export function isProfileName(value) {
|
|
25
|
+
return PROFILE_PATTERN.test(value);
|
|
26
|
+
}
|
|
27
|
+
function dataDirOf(ref) {
|
|
28
|
+
return typeof ref === 'string' ? ref : ref.dataDir;
|
|
29
|
+
}
|
|
30
|
+
function namedProfile(ref) {
|
|
31
|
+
return typeof ref === 'string' ? null : ref.profile;
|
|
32
|
+
}
|
|
33
|
+
/** The profile a command uses: the named one, else the current one of the file, else `default`. */
|
|
34
|
+
function profileOf(ref, file) {
|
|
35
|
+
return namedProfile(ref) ?? file?.currentProfile ?? DEFAULT_PROFILE;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The login of the profile; null when there is none. A profile that is named but not on this machine is refused
|
|
39
|
+
* with what to do, so a typo is not taken for a missing login.
|
|
40
|
+
*/
|
|
41
|
+
async function readProfile(ref) {
|
|
42
|
+
const file = await readStored(dataDirOf(ref));
|
|
43
|
+
const name = profileOf(ref, file);
|
|
44
|
+
const stored = file?.profiles.get(name) ?? null;
|
|
45
|
+
if (stored === null && namedProfile(ref) !== null && (file?.profiles.size ?? 0) > 0) {
|
|
46
|
+
throw new HopwireError(`There is no profile "${name}" on this machine; run hopwire login --profile ${name} to create it, or hopwire profiles to list the ones there are.`);
|
|
47
|
+
}
|
|
48
|
+
return { name, stored };
|
|
49
|
+
}
|
|
20
50
|
/** The stored account, null when nobody is logged in. A file that is not what `hopwire login` wrote is refused. */
|
|
21
|
-
export async function readAccount(
|
|
22
|
-
const stored = await
|
|
51
|
+
export async function readAccount(ref) {
|
|
52
|
+
const { stored } = await readProfile(ref);
|
|
23
53
|
return stored === null || stored.apiToken === null
|
|
24
54
|
? null
|
|
25
55
|
: { apiToken: stored.apiToken, controlPlaneUrl: stored.controlPlaneUrl };
|
|
26
56
|
}
|
|
27
57
|
/**
|
|
28
|
-
* The control plane of the last login, also after a logout
|
|
29
|
-
*
|
|
58
|
+
* The control plane of the last login of the profile, also after a logout. A profile that has none yet takes the
|
|
59
|
+
* one of the current profile, so a second organisation at the same control plane needs no --api. Null when this
|
|
60
|
+
* machine was never logged in. A file that is not what `hopwire login` wrote is refused.
|
|
30
61
|
*/
|
|
31
|
-
export async function readControlPlaneUrl(
|
|
32
|
-
|
|
62
|
+
export async function readControlPlaneUrl(ref) {
|
|
63
|
+
const file = await readStored(dataDirOf(ref));
|
|
64
|
+
if (file === null) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
const stored = file.profiles.get(profileOf(ref, file)) ?? file.profiles.get(file.currentProfile);
|
|
68
|
+
return stored?.controlPlaneUrl ?? null;
|
|
69
|
+
}
|
|
70
|
+
/** The name of the profile a command with this ref uses; no profile has to exist for it. */
|
|
71
|
+
export async function profileName(ref) {
|
|
72
|
+
return profileOf(ref, await readStored(dataDirOf(ref)));
|
|
73
|
+
}
|
|
74
|
+
/** Every profile of the data directory, in the order of their names; empty before the first login. */
|
|
75
|
+
export async function listProfiles(dataDir) {
|
|
76
|
+
const file = await readStored(dataDir);
|
|
77
|
+
if (file === null) {
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
return [...file.profiles.entries()]
|
|
81
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
82
|
+
.map(([name, stored]) => ({
|
|
83
|
+
name,
|
|
84
|
+
current: name === file.currentProfile,
|
|
85
|
+
loggedIn: stored.apiToken !== null,
|
|
86
|
+
controlPlaneUrl: stored.controlPlaneUrl,
|
|
87
|
+
edgeUrl: stored.edgeUrl,
|
|
88
|
+
organisation: stored.organisation,
|
|
89
|
+
plan: stored.plan,
|
|
90
|
+
}));
|
|
91
|
+
}
|
|
92
|
+
/** Makes a profile the current one. Nothing is contacted. Resolves with false when there is no such profile. */
|
|
93
|
+
export async function useProfile(dataDir, name) {
|
|
94
|
+
const file = await readStored(dataDir);
|
|
95
|
+
if (file === null || !file.profiles.has(name)) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
await writeStored(dataDir, { ...file, currentProfile: name });
|
|
99
|
+
return true;
|
|
33
100
|
}
|
|
34
101
|
async function readStored(dataDir) {
|
|
35
102
|
const file = accountFile(dataDir);
|
|
@@ -52,15 +119,36 @@ async function readStored(dataDir) {
|
|
|
52
119
|
return stored;
|
|
53
120
|
}
|
|
54
121
|
/**
|
|
55
|
-
* Stores the account, replacing an earlier
|
|
56
|
-
*
|
|
122
|
+
* Stores the account in the profile, replacing an earlier login of that profile, and makes the profile the current
|
|
123
|
+
* one. Other profiles stay as they are. The file is complete and owner-only from the moment it exists. A new login
|
|
124
|
+
* binds the account to the edge its control plane names; without one, the next public start binds it.
|
|
57
125
|
*/
|
|
58
|
-
export async function writeAccount(
|
|
59
|
-
|
|
126
|
+
export async function writeAccount(ref, account, edgeUrl = null, details = { organisation: null, plan: null }) {
|
|
127
|
+
const dataDir = dataDirOf(ref);
|
|
128
|
+
let file;
|
|
129
|
+
try {
|
|
130
|
+
file = await readStored(dataDir);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
// A damaged file is replaced, as before profiles: the login is what repairs it
|
|
134
|
+
if (!(error instanceof HopwireError)) {
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
file = null;
|
|
138
|
+
}
|
|
139
|
+
const name = profileOf(ref, file);
|
|
140
|
+
const profiles = new Map(file?.profiles ?? []);
|
|
141
|
+
profiles.set(name, {
|
|
142
|
+
...account,
|
|
143
|
+
edgeUrl: edgeUrl === null ? null : new URL(edgeUrl.origin),
|
|
144
|
+
organisation: details.organisation,
|
|
145
|
+
plan: details.plan,
|
|
146
|
+
});
|
|
147
|
+
await writeStored(dataDir, { currentProfile: name, profiles });
|
|
60
148
|
}
|
|
61
149
|
/** The edge the logged-in account is bound to; null when nobody is logged in or no edge is bound yet. */
|
|
62
|
-
export async function boundEdge(
|
|
63
|
-
const stored = await
|
|
150
|
+
export async function boundEdge(ref) {
|
|
151
|
+
const { stored } = await readProfile(ref);
|
|
64
152
|
return stored === null || stored.apiToken === null ? null : stored.edgeUrl;
|
|
65
153
|
}
|
|
66
154
|
/**
|
|
@@ -68,28 +156,40 @@ export async function boundEdge(dataDir) {
|
|
|
68
156
|
* stores the origin of its edge; every later one gets that origin back and has to use the same edge, so a project
|
|
69
157
|
* file or an environment cannot send the account's tokens (and a channel into this machine's services) to an edge
|
|
70
158
|
* of its choosing. The caller of a first start passes the edge the control plane of the account names when it names
|
|
71
|
-
* one (cli.ts, accountEdge). Resolves with the bound edge. A new `hopwire login` clears the binding.
|
|
159
|
+
* one (cli.ts, accountEdge). Resolves with the bound edge. A new `hopwire login` clears the binding. Each profile has
|
|
160
|
+
* its own binding.
|
|
72
161
|
*/
|
|
73
|
-
export async function bindEdge(
|
|
74
|
-
const
|
|
75
|
-
|
|
162
|
+
export async function bindEdge(ref, edgeUrl) {
|
|
163
|
+
const dataDir = dataDirOf(ref);
|
|
164
|
+
const file = await readStored(dataDir);
|
|
165
|
+
const name = profileOf(ref, file);
|
|
166
|
+
const stored = file?.profiles.get(name) ?? null;
|
|
167
|
+
if (file === null || stored === null || stored.apiToken === null) {
|
|
76
168
|
throw new HopwireError('This machine is not logged in; run hopwire login first.');
|
|
77
169
|
}
|
|
78
170
|
if (stored.edgeUrl !== null) {
|
|
79
171
|
return stored.edgeUrl;
|
|
80
172
|
}
|
|
81
|
-
|
|
173
|
+
const profiles = new Map(file.profiles);
|
|
174
|
+
profiles.set(name, { ...stored, edgeUrl: new URL(edgeUrl.origin) });
|
|
175
|
+
await writeStored(dataDir, { ...file, profiles });
|
|
82
176
|
return edgeUrl;
|
|
83
177
|
}
|
|
84
178
|
async function writeStored(dataDir, stored) {
|
|
85
179
|
// A data directory that has to be created holds key material and traffic: owner only
|
|
86
180
|
await mkdir(dataDir, { recursive: true, mode: 0o700 });
|
|
87
181
|
const file = accountFile(dataDir);
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
182
|
+
const profiles = {};
|
|
183
|
+
for (const [name, profile] of [...stored.profiles.entries()].sort(([a], [b]) => a.localeCompare(b))) {
|
|
184
|
+
profiles[name] = {
|
|
185
|
+
...(profile.apiToken === null ? {} : { apiToken: profile.apiToken }),
|
|
186
|
+
controlPlaneUrl: profile.controlPlaneUrl.origin,
|
|
187
|
+
...(profile.edgeUrl === null ? {} : { edgeUrl: profile.edgeUrl.origin }),
|
|
188
|
+
...(profile.organisation === null ? {} : { organisation: profile.organisation }),
|
|
189
|
+
...(profile.plan === null ? {} : { plan: profile.plan }),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
const content = JSON.stringify({ currentProfile: stored.currentProfile, profiles }, null, 4);
|
|
93
193
|
const draft = `${file}.${process.pid}.${randomBytes(6).toString('hex')}.tmp`;
|
|
94
194
|
await writeFile(draft, `${content}\n`, { mode: 0o600, flag: 'wx' });
|
|
95
195
|
try {
|
|
@@ -101,13 +201,14 @@ async function writeStored(dataDir, stored) {
|
|
|
101
201
|
}
|
|
102
202
|
}
|
|
103
203
|
/**
|
|
104
|
-
* Removes the stored API token and keeps the URL of the control plane it belonged to. A damaged file
|
|
105
|
-
* a whole. Resolves with false when no token was stored.
|
|
204
|
+
* Removes the stored API token of the profile and keeps the URL of the control plane it belonged to. A damaged file
|
|
205
|
+
* is removed as a whole. Resolves with false when no token was stored in the profile.
|
|
106
206
|
*/
|
|
107
|
-
export async function removeAccount(
|
|
108
|
-
|
|
207
|
+
export async function removeAccount(ref) {
|
|
208
|
+
const dataDir = dataDirOf(ref);
|
|
209
|
+
let file;
|
|
109
210
|
try {
|
|
110
|
-
|
|
211
|
+
file = await readStored(dataDir);
|
|
111
212
|
}
|
|
112
213
|
catch (error) {
|
|
113
214
|
if (!(error instanceof HopwireError)) {
|
|
@@ -121,13 +222,19 @@ export async function removeAccount(dataDir) {
|
|
|
121
222
|
});
|
|
122
223
|
return true;
|
|
123
224
|
}
|
|
124
|
-
|
|
225
|
+
const name = profileOf(ref, file);
|
|
226
|
+
const stored = file?.profiles.get(name) ?? null;
|
|
227
|
+
if (file === null || stored === null || stored.apiToken === null) {
|
|
125
228
|
return false;
|
|
126
229
|
}
|
|
127
230
|
// Written anew and renamed into place: the token is in no file any more once this resolves
|
|
128
|
-
|
|
231
|
+
const profiles = new Map(file.profiles);
|
|
232
|
+
profiles.set(name, { ...stored, apiToken: null });
|
|
233
|
+
await writeStored(dataDir, { ...file, profiles });
|
|
129
234
|
return true;
|
|
130
235
|
}
|
|
236
|
+
/** The longest organisation or plan name kept for listing; text of the control plane, cut rather than refused. */
|
|
237
|
+
const DETAIL_MAX = 200;
|
|
131
238
|
function parse(text) {
|
|
132
239
|
let value;
|
|
133
240
|
try {
|
|
@@ -136,11 +243,37 @@ function parse(text) {
|
|
|
136
243
|
catch {
|
|
137
244
|
return null;
|
|
138
245
|
}
|
|
139
|
-
if (
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
246
|
+
if (!isObject(value)) {
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
// Written by a CLI before profiles: one login, read as the profile `default` and rewritten on the next write
|
|
250
|
+
if (!('profiles' in value)) {
|
|
251
|
+
const stored = parseProfile(value);
|
|
252
|
+
return stored === null
|
|
253
|
+
? null
|
|
254
|
+
: { currentProfile: DEFAULT_PROFILE, profiles: new Map([[DEFAULT_PROFILE, stored]]) };
|
|
255
|
+
}
|
|
256
|
+
if (!isObject(value.profiles) ||
|
|
257
|
+
!('currentProfile' in value) ||
|
|
258
|
+
typeof value.currentProfile !== 'string' ||
|
|
259
|
+
!isProfileName(value.currentProfile)) {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
const profiles = new Map();
|
|
263
|
+
for (const [name, entry] of Object.entries(value.profiles)) {
|
|
264
|
+
const stored = isProfileName(name) && isObject(entry) ? parseProfile(entry) : null;
|
|
265
|
+
if (stored === null) {
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
profiles.set(name, stored);
|
|
269
|
+
}
|
|
270
|
+
return { currentProfile: value.currentProfile, profiles };
|
|
271
|
+
}
|
|
272
|
+
function isObject(value) {
|
|
273
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
274
|
+
}
|
|
275
|
+
function parseProfile(value) {
|
|
276
|
+
if (typeof value.controlPlaneUrl !== 'string') {
|
|
144
277
|
return null;
|
|
145
278
|
}
|
|
146
279
|
// Without a token: logged out. With one, it has to be a token
|
|
@@ -164,5 +297,17 @@ function parse(text) {
|
|
|
164
297
|
}
|
|
165
298
|
edgeUrl = parsed;
|
|
166
299
|
}
|
|
167
|
-
|
|
300
|
+
const organisation = detail(value.organisation);
|
|
301
|
+
const plan = detail(value.plan);
|
|
302
|
+
if (organisation === undefined || plan === undefined) {
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
return { apiToken, controlPlaneUrl, edgeUrl, organisation, plan };
|
|
306
|
+
}
|
|
307
|
+
/** An optional text of the control plane; undefined when it is there but no text. */
|
|
308
|
+
function detail(value) {
|
|
309
|
+
if (value === undefined) {
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
return typeof value === 'string' ? value.slice(0, DETAIL_MAX) : undefined;
|
|
168
313
|
}
|
package/dist/agent/redact.js
CHANGED
|
@@ -7,10 +7,13 @@
|
|
|
7
7
|
export const REDACTION_MARKER = '[redacted';
|
|
8
8
|
/** Headers that are always redacted, besides the names that match SENSITIVE_NAME */
|
|
9
9
|
const SENSITIVE_HEADERS = new Set(['authorization', 'proxy-authorization', 'cookie', 'set-cookie', 'x-api-key']);
|
|
10
|
-
/**
|
|
11
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Names of headers that carry credentials or signatures. `hmac` covers Shopify's `X-Shopify-Hmac-SHA256`; `sig` as a
|
|
12
|
+
* word of its own covers PayPal's `PAYPAL-TRANSMISSION-SIG` (and not names such as `x-signal`).
|
|
13
|
+
*/
|
|
14
|
+
const SENSITIVE_NAME = /(token|secret|signature|password|passwd|session|api[-_]?key|auth|hmac|(?:^|[-_])sig(?:[-_]|$))/i;
|
|
12
15
|
/** Names of query parameters, form fields and JSON keys: the header names plus payment data */
|
|
13
|
-
const SENSITIVE_FIELD = /(token|secret|signature|password|passwd|session|api[-_]?key|auth|card|cvc|cvv|iban)/i;
|
|
16
|
+
const SENSITIVE_FIELD = /(token|secret|signature|password|passwd|session|api[-_]?key|auth|hmac|(?:^|[-_])sig(?:[-_]|$)|card|cvc|cvv|iban)/i;
|
|
14
17
|
/** What is shown instead of a value: its length, nothing of its content. */
|
|
15
18
|
export function redacted(value) {
|
|
16
19
|
return `${REDACTION_MARKER}, ${[...value].length} chars]`;
|
|
@@ -32,6 +35,10 @@ export function redactHeaders(headers) {
|
|
|
32
35
|
return [name, value];
|
|
33
36
|
}
|
|
34
37
|
const lower = name.toLowerCase();
|
|
38
|
+
// the time of a signature in a header of its own (Discord's X-Signature-Timestamp): digits only, no secret
|
|
39
|
+
if (lower.endsWith('timestamp') && /^\d{1,13}$/.test(value)) {
|
|
40
|
+
return [name, value];
|
|
41
|
+
}
|
|
35
42
|
if (lower === 'authorization' || lower === 'proxy-authorization') {
|
|
36
43
|
const scheme = /^([A-Za-z][A-Za-z0-9!#$%&'*+.^_`|~-]*) +(.*)$/s.exec(value);
|
|
37
44
|
if (scheme?.[1] !== undefined && scheme[2] !== undefined) {
|
|
@@ -47,12 +54,14 @@ export function redactHeaders(headers) {
|
|
|
47
54
|
/** Names of the parts of a signature header that carry its time, shown when they are digits only */
|
|
48
55
|
const SIGNATURE_TIME_PARTS = new Set(['t', 'ts', 'timestamp']);
|
|
49
56
|
/**
|
|
50
|
-
* A signature header of `name=value` parts separated by commas (Stripe's `t=…,v1=…`, GitHub's
|
|
51
|
-
* value redacted but a timestamp, so an agent can tell whether a
|
|
52
|
-
* Null when the value has another form; the caller then redacts it
|
|
57
|
+
* A signature header of `name=value` parts separated by commas or semicolons (Stripe's `t=…,v1=…`, GitHub's
|
|
58
|
+
* `sha256=…`, Paddle's `ts=…;h1=…`) with every value redacted but a timestamp, so an agent can tell whether a
|
|
59
|
+
* provider's tolerance has passed before it replays. Null when the value has another form; the caller then redacts it
|
|
60
|
+
* as a whole.
|
|
53
61
|
*/
|
|
54
62
|
function redactSignature(value) {
|
|
55
|
-
const
|
|
63
|
+
const separator = value.includes(',') ? ',' : ';';
|
|
64
|
+
const parts = value.split(separator);
|
|
56
65
|
const shown = [];
|
|
57
66
|
for (const part of parts) {
|
|
58
67
|
const match = /^(\s*)([A-Za-z0-9_-]{1,32})=(.*)$/s.exec(part);
|
|
@@ -63,7 +72,7 @@ function redactSignature(value) {
|
|
|
63
72
|
const isTime = SIGNATURE_TIME_PARTS.has(key.toLowerCase()) && /^\d{1,13}$/.test(inner);
|
|
64
73
|
shown.push(`${space}${key}=${isTime ? inner : redacted(inner)}`);
|
|
65
74
|
}
|
|
66
|
-
return shown.join(
|
|
75
|
+
return shown.join(separator);
|
|
67
76
|
}
|
|
68
77
|
/** A path with the values of sensitive query parameters redacted; the rest is kept as it was written. */
|
|
69
78
|
export function redactPath(requestPath) {
|
package/dist/bin.js
CHANGED
|
@@ -1,26 +1,156 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { openBrowser } from './browser.js';
|
|
3
3
|
import { main } from './main.js';
|
|
4
|
+
import { formatExchange } from './output.js';
|
|
4
5
|
import { promptHidden, promptLine } from './prompt.js';
|
|
5
|
-
import {
|
|
6
|
+
import { copyToClipboard, replayThroughInspector, shortcutTarget, startShortcuts } from './shortcuts.js';
|
|
7
|
+
import { wantsColor } from './style.js';
|
|
8
|
+
import { LiveFeed } from './live.js';
|
|
9
|
+
import { STARTING, Spinner, TerminalScreen, hopFrames, terminalLook } from './terminal.js';
|
|
10
|
+
import { readVersion } from './version.js';
|
|
11
|
+
const argv = process.argv.slice(2);
|
|
12
|
+
const look = terminalLook(process.env);
|
|
6
13
|
const color = wantsColor(process.stdout, process.env);
|
|
14
|
+
const colorErrors = wantsColor(process.stderr, process.env);
|
|
15
|
+
// A command that runs a tunnel, not its help: only these get the spinner and the keys
|
|
16
|
+
const command = argv[0] ?? '';
|
|
17
|
+
const tunnel = command in STARTING && !argv.some((arg) => arg === '--help' || arg === '-h' || arg === '--version');
|
|
18
|
+
const keys = tunnel && process.stdin.isTTY === true && command !== 'connect';
|
|
19
|
+
// hopwire --version on a terminal shows the mark beside the version; a script reading it gets the bare version
|
|
20
|
+
const versionOnly = argv.length === 1 && argv[0] === '--version';
|
|
21
|
+
const screen = color || colorErrors
|
|
22
|
+
? new TerminalScreen({
|
|
23
|
+
look,
|
|
24
|
+
width: () => process.stdout.columns ?? 100,
|
|
25
|
+
version: readVersion(),
|
|
26
|
+
shortcuts: keys,
|
|
27
|
+
})
|
|
28
|
+
: null;
|
|
29
|
+
// Cleared before anything else is written: a prompt, a line, a failure
|
|
30
|
+
const spinner = tunnel && colorErrors && screen !== null ? new Spinner(process.stderr, look, `${STARTING[command] ?? ''}...`) : null;
|
|
31
|
+
spinner?.start();
|
|
32
|
+
const quiet = () => spinner?.stop();
|
|
33
|
+
// A tunnel's feed on a colour terminal: rows scroll above a status line that stays put
|
|
34
|
+
const live = tunnel && color && screen !== null
|
|
35
|
+
? new LiveFeed(screen, process.stdout, () => process.stdout.columns ?? 100)
|
|
36
|
+
: null;
|
|
37
|
+
process.once('exit', () => live?.stop());
|
|
38
|
+
const out = (text) => {
|
|
39
|
+
quiet();
|
|
40
|
+
if (live === null) {
|
|
41
|
+
process.stdout.write(`${text}\n`);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
live.write(text);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* The rabbit hops into its place once, about a third of a second, before the mark is drawn: on --version and after a
|
|
49
|
+
* login. Not on CI, and never where there is no colour. The wait blocks the process on purpose: nothing else runs yet.
|
|
50
|
+
*/
|
|
51
|
+
const hop = () => {
|
|
52
|
+
if (!color || process.env.CI !== undefined) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const pause = new Int32Array(new SharedArrayBuffer(4));
|
|
56
|
+
const height = 6;
|
|
57
|
+
for (const frame of hopFrames(look)) {
|
|
58
|
+
process.stdout.write(`${frame}\n`);
|
|
59
|
+
Atomics.wait(pause, 0, 0, 60);
|
|
60
|
+
// Back to the first line of the frame, and away with it
|
|
61
|
+
process.stdout.write(`\u001b[${height}A\r\u001b[J`);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
// Without the live feed (no colour), the newest exchange is still noted for the r key
|
|
65
|
+
let last = null;
|
|
66
|
+
let markShown = false;
|
|
7
67
|
await main({
|
|
8
|
-
argv
|
|
68
|
+
argv,
|
|
9
69
|
env: process.env,
|
|
10
|
-
print: (line) =>
|
|
11
|
-
|
|
70
|
+
print: (line) => {
|
|
71
|
+
if (color && screen !== null && (versionOnly || line.startsWith('Logged in '))) {
|
|
72
|
+
hop();
|
|
73
|
+
}
|
|
74
|
+
out(color && screen !== null ? (versionOnly ? screen.markBlock() : screen.render(line)) : line);
|
|
75
|
+
},
|
|
76
|
+
error: (line) => {
|
|
77
|
+
quiet();
|
|
78
|
+
const write = () => process.stderr.write(`${line}\n`);
|
|
79
|
+
if (live === null) {
|
|
80
|
+
write();
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
live.around(write);
|
|
84
|
+
}
|
|
85
|
+
},
|
|
12
86
|
on: (signal, listener) => process.on(signal, listener),
|
|
13
87
|
exit: (code) => process.exit(code),
|
|
14
88
|
// Opened only when a command reads it
|
|
15
89
|
input: () => process.stdin,
|
|
16
90
|
// Only a terminal can be asked without showing the answer
|
|
17
91
|
promptSecret: process.stdin.isTTY
|
|
18
|
-
? (question, stop) =>
|
|
92
|
+
? (question, stop) => {
|
|
93
|
+
quiet();
|
|
94
|
+
return promptHidden(question, process.stdin, process.stderr, stop);
|
|
95
|
+
}
|
|
19
96
|
: undefined,
|
|
20
97
|
// hopwire init asks its questions on standard error, so standard output keeps the lines of the result
|
|
21
98
|
promptLine: process.stdin.isTTY
|
|
22
|
-
? (question, stop) =>
|
|
99
|
+
? (question, stop) => {
|
|
100
|
+
quiet();
|
|
101
|
+
// hopwire init: the mark above its first question
|
|
102
|
+
if (colorErrors && screen !== null && !markShown) {
|
|
103
|
+
markShown = true;
|
|
104
|
+
process.stderr.write(`${screen.markBlock()}\n\n`);
|
|
105
|
+
}
|
|
106
|
+
return promptLine(question, process.stdin, process.stderr, stop);
|
|
107
|
+
}
|
|
23
108
|
: undefined,
|
|
24
109
|
// hopwire login opens its sign-in page; the URL is printed too
|
|
25
110
|
openBrowser: (url) => openBrowser(url),
|
|
111
|
+
// A row of columns on a colour terminal; the plain line otherwise, still noted for the r key
|
|
112
|
+
exchange: color || keys
|
|
113
|
+
? (exchange, label = '') => {
|
|
114
|
+
quiet();
|
|
115
|
+
if (exchange.source !== 'replay') {
|
|
116
|
+
last = exchange;
|
|
117
|
+
}
|
|
118
|
+
if (live !== null) {
|
|
119
|
+
live.exchange(exchange, label);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
out(color && screen !== null ? screen.row(exchange, label) : `${label}${formatExchange(exchange)}`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
: undefined,
|
|
126
|
+
started: (running) => {
|
|
127
|
+
quiet();
|
|
128
|
+
const target = shortcutTarget(running);
|
|
129
|
+
if (target !== null) {
|
|
130
|
+
live?.start();
|
|
131
|
+
}
|
|
132
|
+
if (!keys || target === null) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const say = (text, problem = false) => out(color && screen !== null ? screen.say(text, problem) : text);
|
|
136
|
+
startShortcuts(process.stdin, {
|
|
137
|
+
target,
|
|
138
|
+
lastExchange: () => (live === null ? last : live.target()),
|
|
139
|
+
move: live === null ? undefined : (step) => live.move(step),
|
|
140
|
+
unpick: live === null ? undefined : () => live.unpick(),
|
|
141
|
+
details: live === null ? undefined : () => live.details(target.inspectorUrl !== null),
|
|
142
|
+
say,
|
|
143
|
+
openBrowser: (url) => openBrowser(url),
|
|
144
|
+
copy: (text) => copyToClipboard(text, (escape) => process.stdout.write(escape)),
|
|
145
|
+
replay: replayThroughInspector,
|
|
146
|
+
// The same way out as Ctrl+C in a terminal that is not in raw mode: the supervisor's shutdown
|
|
147
|
+
interrupt: () => process.kill(process.pid, 'SIGINT'),
|
|
148
|
+
});
|
|
149
|
+
},
|
|
150
|
+
failure: colorErrors && screen !== null
|
|
151
|
+
? (message, hint) => {
|
|
152
|
+
quiet();
|
|
153
|
+
process.stderr.write(`${screen.failure(message, hint)}\n`);
|
|
154
|
+
}
|
|
155
|
+
: undefined,
|
|
26
156
|
});
|