@getdial/cli 0.35.0 → 0.36.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/lib/api.js +3 -6
- package/dist/lib/user-agent.js +46 -0
- package/package.json +1 -1
- package/skills.tar.gz +0 -0
package/dist/lib/api.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { request, fetch as undiciFetch, FormData as UndiciFormData, setGlobalDispatcher, EnvHttpProxyAgent, } from "undici";
|
|
2
2
|
import { logger } from "./log.js";
|
|
3
|
-
import { VERSION } from "./version.js";
|
|
4
3
|
import { refParamsHeader } from "./ref-params.js";
|
|
4
|
+
import { userAgent } from "./user-agent.js";
|
|
5
5
|
// Route this package's undici requests through HTTP(S)_PROXY when one is set.
|
|
6
6
|
// The `undici` npm package keeps its OWN global dispatcher, which — unlike
|
|
7
7
|
// Node's built-in fetch (wired by NODE_USE_ENV_PROXY) — ignores the proxy env
|
|
@@ -19,9 +19,6 @@ if (process.env.HTTPS_PROXY ||
|
|
|
19
19
|
// check) — Node's global FormData would be coerced to a text/plain string.
|
|
20
20
|
export { UndiciFormData as ApiFormData };
|
|
21
21
|
const DEFAULT_BASE = "https://api.getdial.ai";
|
|
22
|
-
// Identifies the CLI on every request, so server-side request logs attribute
|
|
23
|
-
// provisioning (and everything else) to the client + version that made the call.
|
|
24
|
-
const USER_AGENT = `@getdial/cli/${VERSION}`;
|
|
25
22
|
export function baseUrl() {
|
|
26
23
|
return process.env.DIAL_API_URL ?? DEFAULT_BASE;
|
|
27
24
|
}
|
|
@@ -69,7 +66,7 @@ async function apiRequest(method, path, body, apiKey, extraHeaders) {
|
|
|
69
66
|
const url = `${baseUrl()}${path}`;
|
|
70
67
|
const headers = applyRefParamsHeader({
|
|
71
68
|
"content-type": "application/json",
|
|
72
|
-
"user-agent":
|
|
69
|
+
"user-agent": userAgent(),
|
|
73
70
|
...(extraHeaders ?? {}),
|
|
74
71
|
});
|
|
75
72
|
if (apiKey)
|
|
@@ -99,7 +96,7 @@ async function sendMultipart(method, path, form, apiKey) {
|
|
|
99
96
|
// No content-type here on purpose: undici's fetch sets the multipart boundary.
|
|
100
97
|
// The user-agent must still be set (server request logs key off it — the
|
|
101
98
|
// POST path once regressed by dropping it, see the git history).
|
|
102
|
-
const headers = applyRefParamsHeader({ "user-agent":
|
|
99
|
+
const headers = applyRefParamsHeader({ "user-agent": userAgent() });
|
|
103
100
|
if (apiKey)
|
|
104
101
|
headers.authorization = `Bearer ${apiKey}`;
|
|
105
102
|
try {
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { VERSION } from "./version.js";
|
|
2
|
+
// Identifies the CLI on every request, so server-side request logs attribute
|
|
3
|
+
// provisioning (and everything else) to the client + version that made the call.
|
|
4
|
+
const STOCK_USER_AGENT = `@getdial/cli/${VERSION}`;
|
|
5
|
+
// A caller-supplied token longer than this is almost certainly a bug, and an
|
|
6
|
+
// unbounded one would bloat every server-side request log line it appears in.
|
|
7
|
+
const MAX_PREFIX_LENGTH = 128;
|
|
8
|
+
/**
|
|
9
|
+
* Reduce a caller-supplied identifier to something safe to put in a header.
|
|
10
|
+
*
|
|
11
|
+
* This never throws and never rejects: the prefix is telemetry, so a malformed
|
|
12
|
+
* value degrades to no prefix at all rather than failing the request. That
|
|
13
|
+
* matters concretely — undici refuses a header value containing CR/LF, so
|
|
14
|
+
* passing one through unsanitized would turn a cosmetic misconfiguration into a
|
|
15
|
+
* hard failure of every Dial call the host makes.
|
|
16
|
+
*
|
|
17
|
+
* Returns "" when there is nothing usable left.
|
|
18
|
+
*/
|
|
19
|
+
export function sanitizeUserAgent(raw) {
|
|
20
|
+
if (!raw)
|
|
21
|
+
return "";
|
|
22
|
+
return (raw
|
|
23
|
+
// Anything outside printable ASCII — CR, LF, tabs, other control chars, and
|
|
24
|
+
// non-ASCII bytes — becomes a space, in one pass.
|
|
25
|
+
.replace(/[^\x20-\x7E]/g, " ")
|
|
26
|
+
.replace(/\s+/g, " ")
|
|
27
|
+
.trim()
|
|
28
|
+
.slice(0, MAX_PREFIX_LENGTH)
|
|
29
|
+
// Truncation can land mid-gap and leave a trailing space behind.
|
|
30
|
+
.trim());
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The full User-Agent for an outgoing API request.
|
|
34
|
+
*
|
|
35
|
+
* `DIAL_USER_AGENT` lets an embedding host runtime (an agent platform, a
|
|
36
|
+
* product wrapping the CLI) identify itself: its token is *prepended*, and the
|
|
37
|
+
* CLI's own identifier is always still sent. Unset or unusable, the header goes
|
|
38
|
+
* out exactly as it always has.
|
|
39
|
+
*
|
|
40
|
+
* Read at call time, not at module load — like `baseUrl()` reading
|
|
41
|
+
* DIAL_API_URL, so the environment is still honored if it changes after import.
|
|
42
|
+
*/
|
|
43
|
+
export function userAgent() {
|
|
44
|
+
const prefix = sanitizeUserAgent(process.env.DIAL_USER_AGENT);
|
|
45
|
+
return prefix ? `${prefix} ${STOCK_USER_AGENT}` : STOCK_USER_AGENT;
|
|
46
|
+
}
|
package/package.json
CHANGED
package/skills.tar.gz
CHANGED
|
Binary file
|