nameslink 1.1.16 → 1.1.17

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 CHANGED
@@ -669,3 +669,9 @@ The bootstrap address and operation semantics remain stable; resource locations
669
669
  Only endpoint-relative URI templates are accepted. Credentials are signed/sent after resolution; redirects remain disabled.
670
670
  A missing GET route can refresh discovery once. Writes are never replayed to follow a moved route; recover the original receipt with the original request key.
671
671
  The existing domain-portfolio and OAuth login protocols are outside this resource migration.
672
+
673
+ ### Startup diagnostics
674
+
675
+ The CLI sends best-effort first-run and sign-in checkpoints to NamesLink. It sends fixed
676
+ event names and random journey IDs, without credentials, verification codes, or command
677
+ arguments. Set `NAMESLINK_TELEMETRY_DISABLED=1` to turn off these client reports.
package/bin/nameslink.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import { main, provisionSkillQuietly, remindAboutUpdate } from '../src/cli.mjs';
4
+ import { createStartupTelemetry } from '../src/startup-telemetry.mjs';
4
5
  import { loadLanguagePreference, saveLanguagePreference } from '../src/config.mjs';
5
6
  import { createI18n, formatRecoveryGuidance, languageOptionFromArgv, localizeError } from '../src/i18n.mjs';
6
7
 
@@ -66,8 +67,9 @@ if (i18n && requestedLanguage) {
66
67
  }
67
68
  }
68
69
 
70
+ const startupTelemetry = createStartupTelemetry();
69
71
  if (i18n)
70
- main(argv, { i18n })
72
+ main(argv, { i18n, startupTelemetry })
71
73
  // 只在命令成功后提醒,且提醒自身吞掉所有异常:一条已经跑成功的命令的退出码不能被它改写。
72
74
  .then(() => remindAboutUpdate(argv, i18n))
73
75
  .then(() => provisionSkillQuietly(argv, i18n))
@@ -79,4 +81,5 @@ if (i18n)
79
81
  }
80
82
  reportError(error, i18n);
81
83
  process.exitCode = 1;
82
- });
84
+ })
85
+ .finally(() => startupTelemetry?.drain());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nameslink",
3
- "version": "1.1.16",
3
+ "version": "1.1.17",
4
4
  "description": "NamesLink command-line client for domains, DNS, intelligent site building, Security Acceleration and automation",
5
5
  "type": "module",
6
6
  "bin": {
@@ -35,7 +35,8 @@
35
35
  "skill/nameslink/SKILL.md",
36
36
  "skill/nameslink/agents/openai.yaml",
37
37
  "README.md",
38
- "scripts/postinstall.mjs"
38
+ "scripts/postinstall.mjs",
39
+ "src/startup-telemetry.mjs"
39
40
  ],
40
41
  "engines": {
41
42
  "node": ">=20"
package/src/cli.mjs CHANGED
@@ -420,6 +420,7 @@ export async function main(argv, runtime = {}) {
420
420
  const i18n = runtime.i18n || createI18n(parsed.options.lang);
421
421
  const noCommand = parsed.positionals.length === 0;
422
422
  const command = parsed.positionals.shift() || 'help';
423
+ const firstRunJourney = await runtime.startupTelemetry?.firstRun(parsed.options.endpoint, 'unknown', cliVersion);
423
424
  const interactive = runtime.interactive ?? Boolean(stdout.isTTY && !parsed.options.json);
424
425
  if (noCommand && !parsed.options.help && !parsed.options.version && interactive) {
425
426
  const write = runtime.write || ((value) => stdout.write(value));
@@ -504,19 +505,28 @@ export async function main(argv, runtime = {}) {
504
505
  config.endpointPersistent = endpointSelection.persistent;
505
506
  }
506
507
  const invocation = prepareJourney(
507
- config,
508
+ config.journey ? config : { journey: firstRunJourney },
508
509
  journeyCommand.replaceAll(' ', ':'),
509
510
  cliVersion,
510
511
  );
511
512
  config.journey = invocation.journey;
512
513
  if (command === 'login') {
514
+ const observe = (checkpoint) => runtime.startupTelemetry?.report(config.endpoint, invocation.context, checkpoint);
515
+ observe('LOGIN_STARTED');
513
516
  config.credentials = await browserLogin(config.endpoint, i18n, {
514
517
  openBrowser: parsed.options['no-browser'] !== true,
515
518
  json: parsed.options.json,
516
519
  writeProgress: runtime.writeProgress,
517
520
  journeyContext: invocation.context,
521
+ observe,
518
522
  });
519
- await saveConfig(config);
523
+ try {
524
+ await saveConfig(config);
525
+ } catch (error) {
526
+ observe('CREDENTIAL_SAVE_FAILED');
527
+ throw error;
528
+ }
529
+ observe('CREDENTIAL_SAVED');
520
530
  if (parsed.options.json) {
521
531
  return print({ loggedIn: true, ...authStatusSummary(config) }, true);
522
532
  }
package/src/oauth.mjs CHANGED
@@ -90,6 +90,8 @@ export async function browserLogin(endpoint, i18n = createI18n('en-US'), options
90
90
  launchFailed = true;
91
91
  }
92
92
  }
93
+ options.observe?.(browserOpened ? 'BROWSER_LAUNCHED'
94
+ : launchFailed ? 'BROWSER_LAUNCH_FAILED' : 'BROWSER_MANUAL');
93
95
  if (options.json) {
94
96
  const writeProgress = options.writeProgress || ((value) => process.stderr.write(value));
95
97
  writeProgress(`${JSON.stringify({
@@ -106,16 +108,23 @@ export async function browserLogin(endpoint, i18n = createI18n('en-US'), options
106
108
  process.stdout.write(`${i18n.t('login.openingHint')}\n`);
107
109
  }
108
110
  code = await waitForOAuthCallback(callback.promise, 300_000, setTimeout, clearTimeout, i18n);
111
+ } catch (error) {
112
+ options.observe?.('CALLBACK_NOT_COMPLETED');
113
+ throw error;
109
114
  } finally {
110
115
  await new Promise((resolve) => server.close(resolve));
111
116
  }
112
- return exchangeOAuthCode(
113
- endpoint,
114
- { code, verifier, redirectUri },
115
- i18n,
116
- options.fetchFn || fetch,
117
- options.journeyContext,
118
- );
117
+ options.observe?.('CALLBACK_RECEIVED');
118
+ try {
119
+ const credentials = await exchangeOAuthCode(
120
+ endpoint, { code, verifier, redirectUri }, i18n, options.fetchFn || fetch, options.journeyContext,
121
+ );
122
+ options.observe?.('TOKEN_RECEIVED');
123
+ return credentials;
124
+ } catch (error) {
125
+ options.observe?.('TOKEN_NOT_RECEIVED');
126
+ throw error;
127
+ }
119
128
  }
120
129
 
121
130
  export async function exchangeOAuthCode(
@@ -168,6 +177,10 @@ export async function exchangeOAuthCode(
168
177
  if (!tokenResponse.ok || payload.code !== '200') {
169
178
  throw new ApiError(payload, tokenResponse.status, i18n, endpoint);
170
179
  }
180
+ if (typeof payload.data?.accessToken !== 'string' || !payload.data.accessToken
181
+ || typeof payload.data?.refreshToken !== 'string' || !payload.data.refreshToken) {
182
+ throw new UnexpectedResponseError(endpoint, tokenResponse.status, 'MISSING_TOKENS');
183
+ }
171
184
  return {
172
185
  type: 'oauth',
173
186
  endpoint: endpoint.replace(/\/$/, ''),
@@ -0,0 +1,98 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { mkdir, readFile, writeFile } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+ import { configPaths, loadConfig, validateEndpoint } from './config.mjs';
5
+ import { prepareJourney, requestJourneyHeaders } from './journey.mjs';
6
+
7
+ const ENDPOINTS = new Set([
8
+ 'nameslink.com', 'www.nameslink.com', 'api.nameslink.com',
9
+ ]);
10
+
11
+ /** Best-effort usage observations. Only the existing random journey and fixed event names leave the CLI. */
12
+ export function createStartupTelemetry({
13
+ environment = process.env, paths = configPaths(), readPreferences = loadConfig,
14
+ transport = () => fetch,
15
+ } = {}) {
16
+ if (environment.NAMESLINK_TELEMETRY_DISABLED === '1' || environment.NODE_TEST_CONTEXT) return undefined;
17
+ const pending = new Set();
18
+ const transports = new Map();
19
+
20
+ function report(endpoint, context, checkpoint) {
21
+ const delivery = send(endpoint, context, checkpoint);
22
+ pending.add(delivery);
23
+ delivery.then(() => pending.delete(delivery));
24
+ return delivery;
25
+ }
26
+
27
+ async function send(endpoint, context, checkpoint) {
28
+ try {
29
+ if (!context || !isTelemetryEndpoint(endpoint)) return false;
30
+ if (!transports.has(endpoint)) transports.set(endpoint, transport(endpoint));
31
+ const response = await transports.get(endpoint)(`${endpoint}/account/oauth/cli/telemetry/${checkpoint}`, {
32
+ method: 'POST',
33
+ credentials: 'omit',
34
+ redirect: 'error',
35
+ headers: requestJourneyHeaders(context),
36
+ signal: AbortSignal.timeout(2500),
37
+ });
38
+ await response.body?.cancel();
39
+ return response.status === 204;
40
+ } catch {
41
+ return false;
42
+ }
43
+ }
44
+
45
+ async function firstRun(endpointOption, command, version) {
46
+ try {
47
+ const preferences = await readPreferences({ readCredentials: false });
48
+ const { endpoint } = validateEndpoint(endpointOption || preferences.endpoint, environment);
49
+ if (!isTelemetryEndpoint(endpoint)) return undefined;
50
+ const key = createHash('sha256').update(endpoint).digest('hex').slice(0, 16);
51
+ const dir = join(paths.dir, 'observability');
52
+ const seed = join(dir, `first-run-${key}.json`);
53
+ const receipt = join(dir, `first-run-${key}.sent`);
54
+ let existing;
55
+ try {
56
+ await readFile(receipt);
57
+ } catch (error) {
58
+ if (error.code !== 'ENOENT') return undefined;
59
+ existing = await prepareFirstRun(dir, seed, preferences, command, version, environment);
60
+ const delivery = report(endpoint, existing.context, 'FIRST_RUN').then(async (accepted) => {
61
+ if (accepted) await writeFile(receipt, '', { mode: 0o600 }).catch(() => {});
62
+ });
63
+ pending.add(delivery);
64
+ delivery.then(() => pending.delete(delivery));
65
+ return existing.journey;
66
+ }
67
+ // A first invocation may have exited after --help without creating config.json.
68
+ return JSON.parse(await readFile(seed, 'utf8')).journey;
69
+ } catch {
70
+ return undefined;
71
+ }
72
+ }
73
+
74
+ return { firstRun, report, drain: () => Promise.all(pending) };
75
+ }
76
+
77
+ async function prepareFirstRun(dir, seed, preferences, command, version, environment) {
78
+ try {
79
+ return JSON.parse(await readFile(seed, 'utf8'));
80
+ } catch (error) {
81
+ if (error.code !== 'ENOENT') throw error;
82
+ }
83
+ const first = prepareJourney(preferences, command, version, environment);
84
+ await mkdir(dir, { recursive: true, mode: 0o700 });
85
+ try {
86
+ await writeFile(seed, JSON.stringify(first), { flag: 'wx', mode: 0o600 });
87
+ return first;
88
+ } catch (error) {
89
+ if (error.code !== 'EEXIST') throw error;
90
+ return JSON.parse(await readFile(seed, 'utf8'));
91
+ }
92
+ }
93
+
94
+ function isTelemetryEndpoint(endpoint) {
95
+ const url = new URL(endpoint);
96
+ return url.protocol === 'https:' && ENDPOINTS.has(url.hostname) && url.pathname === '/api'
97
+ && !url.port && !url.username && !url.password && !url.search && !url.hash;
98
+ }