@parallelworks/client 7.120.0 → 7.122.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 +22 -3
- package/dist/credentials.d.ts +89 -0
- package/dist/credentials.js +321 -0
- package/dist/credentials.test.d.ts +1 -0
- package/dist/credentials.test.js +277 -0
- package/dist/index.d.ts +65 -24
- package/dist/index.js +130 -37
- package/dist/index.test.js +1 -20
- package/dist/types/api.d.ts +92 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,6 +31,21 @@ See the [examples](./examples) directory for complete runnable examples.
|
|
|
31
31
|
|
|
32
32
|
## Authentication
|
|
33
33
|
|
|
34
|
+
### Signed In With `pw auth` (Node.js)
|
|
35
|
+
|
|
36
|
+
For scripts you run yourself, sign in once with `pw auth`, then create the client from the pw credentials file:
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { Client } from '@parallelworks/client'
|
|
40
|
+
|
|
41
|
+
const client = Client.fromCredentialConfig()
|
|
42
|
+
const { data } = await client.GET('/api/buckets')
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The credential is picked as the `pw` CLI picks it: `PW_API_KEY`, then the `context` option, `PW_CONTEXT`, and the current context. A signed-in context stays signed in: the client runs `pw auth token --print -o json` for an access token, reads its lifetime from the response (an RFC 6749 token response), and runs it again a minute before the token expires, the way a Kubernetes exec credential plugin or an AWS `credential_process` works, so only the `pw` CLI ever rotates the sign-in's refresh token. `cliCommand` and `cliTimeoutMs` set the `pw` executable (default `pw` on `PATH`) and how long one run may take (default one minute). A `pw` found through a relative `PATH` entry, such as the current directory, is refused, as Go refuses it. Without `pw`, requests reject with `SignInExpiredError`. This needs Node.js 20.16 or later.
|
|
46
|
+
|
|
47
|
+
For unattended jobs such as CI, use an API key in `PW_API_KEY` instead.
|
|
48
|
+
|
|
34
49
|
### Automatic Host Detection
|
|
35
50
|
|
|
36
51
|
API keys (`pwt_...`) and JWT tokens contain the platform host encoded within them. Use `fromCredential` to automatically extract it:
|
|
@@ -43,6 +58,10 @@ const client = Client.fromCredential('pwt_Y2xvdWQucGFyYWxsZWwud29ya3M.xxxxx')
|
|
|
43
58
|
// JWT token - host read from platform_host claim
|
|
44
59
|
const client = Client.fromCredential('eyJhbGci...')
|
|
45
60
|
// Connects to the host in the token's platform_host claim
|
|
61
|
+
|
|
62
|
+
// Access token (pwoa_...), which names no host: PW_PLATFORM_HOST, else the
|
|
63
|
+
// server of the credentials file's selected context (Node.js)
|
|
64
|
+
const client = Client.fromCredential(token)
|
|
46
65
|
```
|
|
47
66
|
|
|
48
67
|
### Explicit Host
|
|
@@ -50,13 +69,13 @@ const client = Client.fromCredential('eyJhbGci...')
|
|
|
50
69
|
If you prefer to specify the host explicitly:
|
|
51
70
|
|
|
52
71
|
```typescript
|
|
53
|
-
// API Key (Basic Auth) - best for
|
|
72
|
+
// API Key (Basic Auth) - best for unattended jobs such as CI
|
|
54
73
|
const client = new Client('https://cloud.parallel.works')
|
|
55
74
|
.withApiKey('pwt_...')
|
|
56
75
|
|
|
57
|
-
//
|
|
76
|
+
// Bearer token, sent as is and never renewed
|
|
58
77
|
const client = new Client('https://cloud.parallel.works')
|
|
59
|
-
.withToken(
|
|
78
|
+
.withToken(token)
|
|
60
79
|
|
|
61
80
|
// Auto-detect credential type
|
|
62
81
|
const client = new Client('https://cloud.parallel.works')
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/** A saved context in the pw credentials file, as the CLI writes it. */
|
|
2
|
+
export interface Identity {
|
|
3
|
+
apikey?: string;
|
|
4
|
+
token?: string;
|
|
5
|
+
server: string;
|
|
6
|
+
name: string;
|
|
7
|
+
canonicalName?: string;
|
|
8
|
+
organization: string;
|
|
9
|
+
/** The state of a `pw auth` sign-in, which only the CLI reads. */
|
|
10
|
+
oauth?: Record<string, unknown>;
|
|
11
|
+
}
|
|
12
|
+
/** The pw credentials file. */
|
|
13
|
+
export interface CredentialConfig {
|
|
14
|
+
identities: Record<string, Identity>;
|
|
15
|
+
currentIdentity: string;
|
|
16
|
+
}
|
|
17
|
+
export interface ResolveOptions {
|
|
18
|
+
/** The context to use, over PW_CONTEXT and the current context. */
|
|
19
|
+
context?: string | undefined;
|
|
20
|
+
/** The platform host to use, over the context's server. */
|
|
21
|
+
platformHost?: string | undefined;
|
|
22
|
+
}
|
|
23
|
+
/** The pw executable CLIAuth runs, looked up on PATH. */
|
|
24
|
+
export declare const DEFAULT_CLI_COMMAND = "pw";
|
|
25
|
+
/** One minute, as the AWS SDK's credential_process provider bounds a run by default. */
|
|
26
|
+
export declare const DEFAULT_CLI_TIMEOUT_MS = 60000;
|
|
27
|
+
/**
|
|
28
|
+
* The credentials file path, ~/.config/pw/credentials (XDG_CONFIG_HOME moves
|
|
29
|
+
* it, PW_CREDENTIALS_DIR replaces its directory), as the CLI and Go SDK resolve it.
|
|
30
|
+
*/
|
|
31
|
+
export declare function defaultCredentialConfigPath(): string;
|
|
32
|
+
/**
|
|
33
|
+
* Reads the credentials file, falling back to the legacy ~/pw/.credentials.
|
|
34
|
+
* A missing, empty or unparseable file reads as no contexts.
|
|
35
|
+
*/
|
|
36
|
+
export declare function loadCredentialConfig(path?: string): CredentialConfig & {
|
|
37
|
+
path: string;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* The identity a client authenticates as, and the context it came from (empty
|
|
41
|
+
* for PW_API_KEY): PW_API_KEY, then the context option, PW_CONTEXT, and the
|
|
42
|
+
* file's current context, as the CLI and Go SDK pick it.
|
|
43
|
+
*/
|
|
44
|
+
export declare function resolveIdentity(config: CredentialConfig, options?: ResolveOptions): {
|
|
45
|
+
context: string;
|
|
46
|
+
identity: Identity;
|
|
47
|
+
};
|
|
48
|
+
/** The platform host for a credential that names none, or undefined outside Node.js. */
|
|
49
|
+
export declare function hostForUnnamedCredential(): string | undefined;
|
|
50
|
+
/** Supplies the Bearer token for each request. */
|
|
51
|
+
export interface TokenProvider {
|
|
52
|
+
token(): Promise<string>;
|
|
53
|
+
}
|
|
54
|
+
export interface CLIAuthOptions {
|
|
55
|
+
/** The pw executable, looked up on PATH unless it is a path. */
|
|
56
|
+
command?: string | undefined;
|
|
57
|
+
/** The context to print a token for; empty means the one the CLI picks. */
|
|
58
|
+
context?: string | undefined;
|
|
59
|
+
/** Bounds one run of the CLI. */
|
|
60
|
+
timeoutMs?: number | undefined;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Authenticates as a `pw auth` sign-in. It runs `pw auth token --print -o json`
|
|
64
|
+
* for a current access token instead of rotating the refresh token itself: the
|
|
65
|
+
* platform signs a device out when two clients rotate the same refresh token,
|
|
66
|
+
* and the CLI serializes its rotations across processes. Like a client-go exec
|
|
67
|
+
* credential plugin or an AWS credential_process, it reads the token and its
|
|
68
|
+
* lifetime from the command's stdout, an RFC 6749 section 5.1 token response,
|
|
69
|
+
* and caches the token in memory until it nears expiry.
|
|
70
|
+
*/
|
|
71
|
+
export declare class CLIAuth implements TokenProvider {
|
|
72
|
+
private readonly options;
|
|
73
|
+
private cached;
|
|
74
|
+
private expiresAt;
|
|
75
|
+
private settled;
|
|
76
|
+
private pending;
|
|
77
|
+
constructor(options?: CLIAuthOptions);
|
|
78
|
+
token(): Promise<string>;
|
|
79
|
+
private usable;
|
|
80
|
+
private renew;
|
|
81
|
+
private run;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Finds command on PATH as Go's exec.LookPath does: a command that names a
|
|
85
|
+
* path is used as is, and a match through a relative PATH entry, such as the
|
|
86
|
+
* current directory, is refused. Windows' own lookup would search the current
|
|
87
|
+
* directory first.
|
|
88
|
+
*/
|
|
89
|
+
export declare function resolveCommand(command: string): string;
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { CredentialError, extractPlatformHost, isToken, NoPlatformHostError, SignInExpiredError, } from './index';
|
|
2
|
+
function nodeProcess() {
|
|
3
|
+
return globalThis.process;
|
|
4
|
+
}
|
|
5
|
+
function env() {
|
|
6
|
+
return nodeProcess()?.env ?? {};
|
|
7
|
+
}
|
|
8
|
+
// getBuiltinModule rather than an import keeps this module loadable in a browser bundle.
|
|
9
|
+
function builtin(id) {
|
|
10
|
+
const get = nodeProcess()?.getBuiltinModule;
|
|
11
|
+
if (!get) {
|
|
12
|
+
throw new CredentialError(`reading pw credentials needs Node.js 20.16 or later (${id})`);
|
|
13
|
+
}
|
|
14
|
+
return get(id);
|
|
15
|
+
}
|
|
16
|
+
/** The pw executable CLIAuth runs, looked up on PATH. */
|
|
17
|
+
export const DEFAULT_CLI_COMMAND = 'pw';
|
|
18
|
+
/** One minute, as the AWS SDK's credential_process provider bounds a run by default. */
|
|
19
|
+
export const DEFAULT_CLI_TIMEOUT_MS = 60000;
|
|
20
|
+
// Inside the CLI's two-minute renewal window, so the CLI always renews a token the SDK asks it for.
|
|
21
|
+
const CLI_RENEW_MARGIN_MS = 60000;
|
|
22
|
+
/**
|
|
23
|
+
* The credentials file path, ~/.config/pw/credentials (XDG_CONFIG_HOME moves
|
|
24
|
+
* it, PW_CREDENTIALS_DIR replaces its directory), as the CLI and Go SDK resolve it.
|
|
25
|
+
*/
|
|
26
|
+
export function defaultCredentialConfigPath() {
|
|
27
|
+
const path = builtin('node:path');
|
|
28
|
+
const override = env()['PW_CREDENTIALS_DIR'];
|
|
29
|
+
if (override) {
|
|
30
|
+
return path.join(override, '.credentials');
|
|
31
|
+
}
|
|
32
|
+
const xdg = env()['XDG_CONFIG_HOME'];
|
|
33
|
+
const base = xdg && path.isAbsolute(xdg)
|
|
34
|
+
? xdg
|
|
35
|
+
: path.join(builtin('node:os').homedir(), '.config');
|
|
36
|
+
return path.join(base, 'pw', 'credentials');
|
|
37
|
+
}
|
|
38
|
+
function legacyCredentialConfigPath() {
|
|
39
|
+
return builtin('node:path').join(builtin('node:os').homedir(), 'pw', '.credentials');
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Reads the credentials file, falling back to the legacy ~/pw/.credentials.
|
|
43
|
+
* A missing, empty or unparseable file reads as no contexts.
|
|
44
|
+
*/
|
|
45
|
+
export function loadCredentialConfig(path) {
|
|
46
|
+
const fs = builtin('node:fs');
|
|
47
|
+
if (!path) {
|
|
48
|
+
path = defaultCredentialConfigPath();
|
|
49
|
+
if (!fs.existsSync(path) && !env()['PW_CREDENTIALS_DIR']) {
|
|
50
|
+
const legacy = legacyCredentialConfigPath();
|
|
51
|
+
if (fs.existsSync(legacy)) {
|
|
52
|
+
path = legacy;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const empty = { identities: {}, currentIdentity: '', path };
|
|
57
|
+
let data;
|
|
58
|
+
try {
|
|
59
|
+
data = fs.readFileSync(path, 'utf8');
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
if (e.code === 'ENOENT') {
|
|
63
|
+
return empty;
|
|
64
|
+
}
|
|
65
|
+
throw e;
|
|
66
|
+
}
|
|
67
|
+
if (!data.trim()) {
|
|
68
|
+
return empty;
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
const parsed = JSON.parse(data);
|
|
72
|
+
return {
|
|
73
|
+
identities: parsed.identities ?? {},
|
|
74
|
+
currentIdentity: parsed.currentIdentity ?? '',
|
|
75
|
+
path,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
console.warn(`warning: credentials file (${path}) is unparseable, treating as missing; re-authenticate with 'pw auth' to recover`);
|
|
80
|
+
return empty;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** The context the CLI uses too: the option, then PW_CONTEXT, then the current context. */
|
|
84
|
+
function selectedContext(config, override) {
|
|
85
|
+
return override || env()['PW_CONTEXT'] || config.currentIdentity;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The identity a client authenticates as, and the context it came from (empty
|
|
89
|
+
* for PW_API_KEY): PW_API_KEY, then the context option, PW_CONTEXT, and the
|
|
90
|
+
* file's current context, as the CLI and Go SDK pick it.
|
|
91
|
+
*/
|
|
92
|
+
export function resolveIdentity(config, options = {}) {
|
|
93
|
+
const credential = env()['PW_API_KEY'];
|
|
94
|
+
if (credential) {
|
|
95
|
+
return {
|
|
96
|
+
context: '',
|
|
97
|
+
identity: identityFromCredential(config, credential, options),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const context = selectedContext(config, options.context);
|
|
101
|
+
if (!context) {
|
|
102
|
+
throw new CredentialError("no context configured; use 'pw auth' to authenticate");
|
|
103
|
+
}
|
|
104
|
+
const identity = config.identities[context];
|
|
105
|
+
if (!identity) {
|
|
106
|
+
throw new CredentialError(`context "${context}" not found`);
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
context,
|
|
110
|
+
identity: options.platformHost
|
|
111
|
+
? { ...identity, server: options.platformHost }
|
|
112
|
+
: { ...identity },
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
// A token that names no platform goes to PW_PLATFORM_HOST, else the selected context's server.
|
|
116
|
+
function identityFromCredential(config, credential, options) {
|
|
117
|
+
let host = options.platformHost;
|
|
118
|
+
if (!host) {
|
|
119
|
+
try {
|
|
120
|
+
host = extractPlatformHost(credential);
|
|
121
|
+
}
|
|
122
|
+
catch (e) {
|
|
123
|
+
if (!(e instanceof NoPlatformHostError)) {
|
|
124
|
+
throw e;
|
|
125
|
+
}
|
|
126
|
+
host =
|
|
127
|
+
env()['PW_PLATFORM_HOST'] ||
|
|
128
|
+
config.identities[selectedContext(config, options.context)]?.server;
|
|
129
|
+
if (!host) {
|
|
130
|
+
throw e;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const identity = { server: host, name: '', organization: '' };
|
|
135
|
+
if (isToken(credential)) {
|
|
136
|
+
identity.token = credential;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
identity.apikey = credential;
|
|
140
|
+
}
|
|
141
|
+
return identity;
|
|
142
|
+
}
|
|
143
|
+
/** The platform host for a credential that names none, or undefined outside Node.js. */
|
|
144
|
+
export function hostForUnnamedCredential() {
|
|
145
|
+
const fromEnv = env()['PW_PLATFORM_HOST'];
|
|
146
|
+
if (fromEnv) {
|
|
147
|
+
return fromEnv;
|
|
148
|
+
}
|
|
149
|
+
if (!nodeProcess()?.getBuiltinModule) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
const config = loadCredentialConfig();
|
|
153
|
+
return config.identities[selectedContext(config)]?.server || undefined;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Authenticates as a `pw auth` sign-in. It runs `pw auth token --print -o json`
|
|
157
|
+
* for a current access token instead of rotating the refresh token itself: the
|
|
158
|
+
* platform signs a device out when two clients rotate the same refresh token,
|
|
159
|
+
* and the CLI serializes its rotations across processes. Like a client-go exec
|
|
160
|
+
* credential plugin or an AWS credential_process, it reads the token and its
|
|
161
|
+
* lifetime from the command's stdout, an RFC 6749 section 5.1 token response,
|
|
162
|
+
* and caches the token in memory until it nears expiry.
|
|
163
|
+
*/
|
|
164
|
+
export class CLIAuth {
|
|
165
|
+
constructor(options = {}) {
|
|
166
|
+
this.cached = '';
|
|
167
|
+
// A token the CLI could not renew is used until it expires rather than asking on every request.
|
|
168
|
+
this.settled = false;
|
|
169
|
+
this.options = options;
|
|
170
|
+
}
|
|
171
|
+
token() {
|
|
172
|
+
if (this.usable()) {
|
|
173
|
+
return Promise.resolve(this.cached);
|
|
174
|
+
}
|
|
175
|
+
this.pending ?? (this.pending = this.renew().finally(() => {
|
|
176
|
+
this.pending = undefined;
|
|
177
|
+
}));
|
|
178
|
+
return this.pending;
|
|
179
|
+
}
|
|
180
|
+
usable() {
|
|
181
|
+
if (!this.cached) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
if (this.expiresAt === undefined) {
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
if (this.settled) {
|
|
188
|
+
return Date.now() < this.expiresAt;
|
|
189
|
+
}
|
|
190
|
+
return this.expiresAt - Date.now() > CLI_RENEW_MARGIN_MS;
|
|
191
|
+
}
|
|
192
|
+
async renew() {
|
|
193
|
+
let printed;
|
|
194
|
+
try {
|
|
195
|
+
printed = await this.run();
|
|
196
|
+
}
|
|
197
|
+
catch (e) {
|
|
198
|
+
if (this.cached &&
|
|
199
|
+
this.expiresAt !== undefined &&
|
|
200
|
+
Date.now() < this.expiresAt) {
|
|
201
|
+
this.settled = true;
|
|
202
|
+
return this.cached;
|
|
203
|
+
}
|
|
204
|
+
throw new SignInExpiredError(e);
|
|
205
|
+
}
|
|
206
|
+
this.cached = printed.token;
|
|
207
|
+
this.expiresAt = printed.expiresAt;
|
|
208
|
+
this.settled =
|
|
209
|
+
this.expiresAt !== undefined &&
|
|
210
|
+
this.expiresAt - Date.now() <= CLI_RENEW_MARGIN_MS;
|
|
211
|
+
return this.cached;
|
|
212
|
+
}
|
|
213
|
+
run() {
|
|
214
|
+
const command = this.options.command || DEFAULT_CLI_COMMAND;
|
|
215
|
+
const timeout = this.options.timeoutMs || DEFAULT_CLI_TIMEOUT_MS;
|
|
216
|
+
const args = ['auth', 'token', '--print', '-o', 'json'];
|
|
217
|
+
if (this.options.context) {
|
|
218
|
+
args.push('--context', this.options.context);
|
|
219
|
+
}
|
|
220
|
+
const describe = [command, ...args].join(' ');
|
|
221
|
+
const file = resolveCommand(command);
|
|
222
|
+
// Without PW_API_KEY, which would make the CLI print it rather than the sign-in's token.
|
|
223
|
+
const { PW_API_KEY: _, ...childEnv } = env();
|
|
224
|
+
const { execFile } = builtin('node:child_process');
|
|
225
|
+
return new Promise((resolve, reject) => {
|
|
226
|
+
execFile(file, args, { env: childEnv, timeout, encoding: 'utf8' }, (error, stdout, stderr) => {
|
|
227
|
+
if (error) {
|
|
228
|
+
reject(cliFailure(describe, timeout, error, stderr));
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
try {
|
|
232
|
+
resolve(parseTokenResponse(describe, stdout, Date.now()));
|
|
233
|
+
}
|
|
234
|
+
catch (e) {
|
|
235
|
+
reject(e);
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function parseTokenResponse(describe, stdout, received) {
|
|
242
|
+
let response;
|
|
243
|
+
try {
|
|
244
|
+
response = JSON.parse(stdout);
|
|
245
|
+
}
|
|
246
|
+
catch (e) {
|
|
247
|
+
throw new Error(`${describe} printed no token response: ${e}`);
|
|
248
|
+
}
|
|
249
|
+
const token = response.access_token;
|
|
250
|
+
if (typeof token !== 'string' || !token || /\s/.test(token)) {
|
|
251
|
+
throw new Error(`${describe} printed no single access_token`);
|
|
252
|
+
}
|
|
253
|
+
if (typeof response.token_type !== 'string' ||
|
|
254
|
+
response.token_type.toLowerCase() !== 'bearer') {
|
|
255
|
+
throw new Error(`${describe} printed token_type ${JSON.stringify(response.token_type)}, want Bearer`);
|
|
256
|
+
}
|
|
257
|
+
const expiresIn = response.expires_in;
|
|
258
|
+
return {
|
|
259
|
+
token,
|
|
260
|
+
expiresAt: typeof expiresIn === 'number' ? received + expiresIn * 1000 : undefined,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Finds command on PATH as Go's exec.LookPath does: a command that names a
|
|
265
|
+
* path is used as is, and a match through a relative PATH entry, such as the
|
|
266
|
+
* current directory, is refused. Windows' own lookup would search the current
|
|
267
|
+
* directory first.
|
|
268
|
+
*/
|
|
269
|
+
export function resolveCommand(command) {
|
|
270
|
+
const windows = nodeProcess()?.platform === 'win32';
|
|
271
|
+
if (command.includes('/') || (windows && /[\\:]/.test(command))) {
|
|
272
|
+
return command;
|
|
273
|
+
}
|
|
274
|
+
const fs = builtin('node:fs');
|
|
275
|
+
const path = builtin('node:path');
|
|
276
|
+
const extensions = windows ? windowsExtensions(command) : [''];
|
|
277
|
+
for (const dir of (env()['PATH'] ?? '').split(path.delimiter)) {
|
|
278
|
+
for (const ext of extensions) {
|
|
279
|
+
const candidate = path.join(dir || '.', command + ext);
|
|
280
|
+
if (!isExecutable(fs, candidate, windows)) {
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
if (!path.isAbsolute(candidate)) {
|
|
284
|
+
throw new CredentialError(`${command} resolves to ${candidate}, relative to the current directory; give the pw command as an absolute path to run it`);
|
|
285
|
+
}
|
|
286
|
+
return candidate;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
throw new CredentialError(`${command}: not found on PATH`);
|
|
290
|
+
}
|
|
291
|
+
// The PATHEXT extensions to try, after the command as is when it already has one.
|
|
292
|
+
function windowsExtensions(command) {
|
|
293
|
+
const extensions = (env()['PATHEXT'] || '.COM;.EXE;.BAT;.CMD')
|
|
294
|
+
.split(';')
|
|
295
|
+
.filter(Boolean);
|
|
296
|
+
const named = extensions.some(ext => command.toLowerCase().endsWith(ext.toLowerCase()));
|
|
297
|
+
return named ? ['', ...extensions] : extensions;
|
|
298
|
+
}
|
|
299
|
+
function isExecutable(fs, file, windows) {
|
|
300
|
+
if (!fs.statSync(file, { throwIfNoEntry: false })?.isFile()) {
|
|
301
|
+
return false;
|
|
302
|
+
}
|
|
303
|
+
if (windows) {
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
try {
|
|
307
|
+
fs.accessSync(file, fs.constants.X_OK);
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
catch {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
function cliFailure(describe, timeout, error, stderr) {
|
|
315
|
+
if (error.killed) {
|
|
316
|
+
return new Error(`${describe} did not finish within ${timeout}ms`);
|
|
317
|
+
}
|
|
318
|
+
const reason = error.code === 'ENOENT' ? 'not found' : `exit status ${error.code}`;
|
|
319
|
+
const detail = stderr.trim();
|
|
320
|
+
return new Error(`${describe}: ${reason}${detail ? `: ${detail}` : ''}`);
|
|
321
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|