@getdial/cli 0.34.1 → 0.34.2
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/ref-params.js +48 -10
- package/package.json +1 -1
- package/skills.tar.gz +0 -0
package/dist/lib/ref-params.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
1
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
2
3
|
import { join } from "node:path";
|
|
3
4
|
import { paths } from "./paths.js";
|
|
4
5
|
import { logger } from "./log.js";
|
|
@@ -7,23 +8,60 @@ import { logger } from "./log.js";
|
|
|
7
8
|
// X-Dial-Ref-Params header. The CLI forwards the file verbatim — no parsing, no
|
|
8
9
|
// allowlist here; the server decodes + validates. Cached per process (the file is
|
|
9
10
|
// write-once and stable for the CLI's lifetime).
|
|
11
|
+
//
|
|
12
|
+
// If the file has no dial_attribution_id, one is minted and appended. That covers
|
|
13
|
+
// every install path — `curl … | bash` writes an id itself, but `npm install -g`,
|
|
14
|
+
// Homebrew, a prebaked image, or an agent installing the CLI do not, and those
|
|
15
|
+
// machines had no attribution spine at all until their eventual signup. Doing it
|
|
16
|
+
// here rather than in an installer means there is exactly one code path to cover,
|
|
17
|
+
// since every API request already passes through this function.
|
|
18
|
+
const ATTRIBUTION_KEY = "dial_attribution_id";
|
|
10
19
|
let cache;
|
|
20
|
+
/** True when the file already carries an attribution id line. */
|
|
21
|
+
function hasAttributionId(text) {
|
|
22
|
+
return text.split("\n").some((l) => l.trim().startsWith(`${ATTRIBUTION_KEY}=`));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Append an attribution id to `text` and persist it. Write-once per key, mirroring
|
|
26
|
+
* the installer's add_ref: a real browser-originated id is never overwritten (the
|
|
27
|
+
* caller checks first).
|
|
28
|
+
*
|
|
29
|
+
* A write failure is warned and swallowed — the id is still returned so a signup in
|
|
30
|
+
* this same process aliases correctly. An unwritable dataDir is already-broken
|
|
31
|
+
* territory the CLI surfaces elsewhere.
|
|
32
|
+
*/
|
|
33
|
+
function mintAttributionId(file, text) {
|
|
34
|
+
const separator = text.length > 0 && !text.endsWith("\n") ? "\n" : "";
|
|
35
|
+
const next = `${text}${separator}${ATTRIBUTION_KEY}=${randomUUID()}\n`;
|
|
36
|
+
try {
|
|
37
|
+
mkdirSync(paths().dataDir, { recursive: true });
|
|
38
|
+
writeFileSync(file, next, "utf8");
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
logger.warn({ err }, "couldn't persist the attribution id; using it for this process only");
|
|
42
|
+
}
|
|
43
|
+
return next;
|
|
44
|
+
}
|
|
11
45
|
function compute() {
|
|
12
46
|
const file = join(paths().dataDir, "ref-params.txt");
|
|
47
|
+
let text;
|
|
13
48
|
try {
|
|
14
|
-
|
|
15
|
-
if (!text.trim())
|
|
16
|
-
return null;
|
|
17
|
-
return Buffer.from(text, "utf8").toString("base64");
|
|
49
|
+
text = readFileSync(file, "utf8");
|
|
18
50
|
}
|
|
19
51
|
catch (err) {
|
|
20
52
|
// No file is the normal case (the user never went through an attributed
|
|
21
|
-
// install) — not an error worth logging. Anything else is unexpected
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
53
|
+
// install) — not an error worth logging. Anything else is unexpected, but
|
|
54
|
+
// still recoverable: treat it as empty and mint below.
|
|
55
|
+
if (err?.code !== "ENOENT") {
|
|
56
|
+
logger.warn({ err }, "failed to read ref-params.txt");
|
|
57
|
+
}
|
|
58
|
+
text = "";
|
|
26
59
|
}
|
|
60
|
+
if (!hasAttributionId(text))
|
|
61
|
+
text = mintAttributionId(file, text);
|
|
62
|
+
if (!text.trim())
|
|
63
|
+
return null;
|
|
64
|
+
return Buffer.from(text, "utf8").toString("base64");
|
|
27
65
|
}
|
|
28
66
|
/** Base64 of ref-params.txt for the X-Dial-Ref-Params header, or null if absent. */
|
|
29
67
|
export function refParamsHeader() {
|
package/package.json
CHANGED
package/skills.tar.gz
CHANGED
|
Binary file
|