@disposabl/cli 3.0.0 → 4.1.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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/dist/index.js +156 -9
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -37,7 +37,7 @@ The command exits unsuccessfully while manual cleanup is pending. Rerun the same
37
37
 
38
38
  ## Usage analytics
39
39
 
40
- When configured for a published release, Disposabl collects the command category, outcome, package version, operating system and package-manager category. A random ID stored in `~/.config/disposabl/telemetry.json` connects anonymous usage to the Disposabl user ID after login. Logout and account changes reset that ID. Prompts, arguments, paths, error messages, credentials and email addresses are not collected. Set `DO_NOT_TRACK=1` or `DISPOSABL_TELEMETRY_DISABLED=1` to opt out. Local source, development, CI and E2E runs do not send events. This is separate from a generated app's analytics.
40
+ Published Disposabl releases collect command category, outcome, package version, operating system and package-manager category. Requests to the Disposabl API also send operation timings and a trace ID to link CLI requests with backend timing and failure logs. A random ID stored in `~/.config/disposabl/telemetry.json` connects anonymous usage to a pseudonymous ID after login. Logout and account changes reset that ID. Prompts, arguments, paths, error messages, credentials and email addresses are excluded. Set `DO_NOT_TRACK=1` or `DISPOSABL_TELEMETRY_DISABLED=1` to opt out. Local source, development, CI and E2E runs do not send CLI telemetry. Generated apps use their own analytics configuration.
41
41
 
42
42
  Failed commands also send a fixed, coarse error-tracking event with no exception message or stack trace.
43
43
 
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  // auth.ts
4
4
  import { lock } from "proper-lockfile";
5
5
  // package.json
6
- var version = "3.0.0";
6
+ var version = "4.1.0";
7
7
  var package_default = {
8
8
  name: "@disposabl/cli",
9
9
  version,
@@ -342,10 +342,13 @@ function telemetryConfig(version, entry = process.argv[1] ?? "") {
342
342
  const env = process.env;
343
343
  const host = "https://us.i.posthog.com";
344
344
  const token = "phc_AkMtHdssxpHojfRGD985EUbxkX2GuMSxKiULHaTT9TgM";
345
- if (!installedRelease(version, entry) || env.NODE_ENV === "test" || env.NODE_ENV === "development" || env.BUN_ENV === "test" || env.CI || env.E2E_TELEMETRY_DISABLED || ["1", "true"].includes(env.DO_NOT_TRACK?.toLowerCase() ?? "") || env.DISPOSABL_TELEMETRY_DISABLED === "1" || env.DISPOSABL_API_KEY || env.DISPOSABL_API_ORIGIN && env.DISPOSABL_API_ORIGIN !== "https://www.disposabl.dev" || !token?.startsWith("phc_") || !["https://us.i.posthog.com", "https://eu.i.posthog.com"].includes(host ?? ""))
345
+ if (!installedRelease(version, entry) || env.NODE_ENV === "test" || env.NODE_ENV === "development" || env.BUN_ENV === "test" || env.CI || env.E2E_TELEMETRY_DISABLED || telemetryOptedOut() || env.DISPOSABL_API_KEY || env.DISPOSABL_API_ORIGIN && env.DISPOSABL_API_ORIGIN !== "https://www.disposabl.dev" || !token?.startsWith("phc_") || !["https://us.i.posthog.com", "https://eu.i.posthog.com"].includes(host ?? ""))
346
346
  return null;
347
347
  return { token, host };
348
348
  }
349
+ function telemetryOptedOut() {
350
+ return ["1", "true"].includes(process.env.DO_NOT_TRACK?.toLowerCase() ?? "") || process.env.DISPOSABL_TELEMETRY_DISABLED === "1";
351
+ }
349
352
  var directory2 = () => join2(homedir2(), ".config", "disposabl");
350
353
  var telemetryStatePath = () => join2(directory2(), "telemetry.json");
351
354
  async function state(path) {
@@ -383,13 +386,18 @@ async function send(config, event, distinctId, properties) {
383
386
  const response = await fetch(`${config.host}/i/v0/e/`, {
384
387
  method: "POST",
385
388
  redirect: "error",
386
- signal: AbortSignal.timeout(500),
389
+ signal: AbortSignal.timeout(2000),
387
390
  headers: { "Content-Type": "application/json" },
388
391
  body: JSON.stringify({
389
392
  api_key: config.token,
390
393
  event,
391
394
  distinct_id: distinctId,
392
- properties: { ...properties, $geoip_disable: true, $ip: "0.0.0.0" }
395
+ properties: {
396
+ ...properties,
397
+ environment: "production",
398
+ $geoip_disable: true,
399
+ $ip: "0.0.0.0"
400
+ }
393
401
  })
394
402
  });
395
403
  return response.ok;
@@ -431,6 +439,7 @@ async function capture(version, product, command, outcome, path) {
431
439
  const activation = outcome === "started" && await firstExecution(path, product, version);
432
440
  const properties = {
433
441
  product,
442
+ source: product,
434
443
  command: allowed.includes(command) ? command : "other",
435
444
  outcome,
436
445
  version,
@@ -522,6 +531,121 @@ Add credits: ${data.paymentUrl}` : "";
522
531
 
523
532
  // client.ts
524
533
  import { Command } from "commander";
534
+
535
+ // ../disposabl-kit/lib/observability.ts
536
+ var traceparentPattern = /^00-([0-9a-f]{32})-([0-9a-f]{16})-01$/;
537
+ function parseTraceparent(value) {
538
+ const [, traceId, parentSpanId] = traceparentPattern.exec(value ?? "") ?? [];
539
+ if (!traceId || !parentSpanId || /^0+$/.test(traceId) || /^0+$/.test(parentSpanId))
540
+ return null;
541
+ return { traceId, parentSpanId };
542
+ }
543
+ function startSpan(parent) {
544
+ const context = parseTraceparent(parent);
545
+ const traceId = context?.traceId ?? crypto.randomUUID().replaceAll("-", "");
546
+ const spanId = Array.from(crypto.getRandomValues(new Uint8Array(8)), (byte) => byte.toString(16).padStart(2, "0")).join("");
547
+ return {
548
+ traceId,
549
+ spanId,
550
+ parentSpanId: context?.parentSpanId,
551
+ traceparent: `00-${traceId}-${spanId}-01`
552
+ };
553
+ }
554
+ var attribute = (key, stringValue) => ({
555
+ key,
556
+ value: { stringValue }
557
+ });
558
+ var nanoseconds = (ms) => String(BigInt(Math.round(ms)) * 1000000n);
559
+ async function exportOperation(config, operation, timeout) {
560
+ const {
561
+ service,
562
+ name,
563
+ source,
564
+ startedAt,
565
+ endedAt,
566
+ failed,
567
+ distinctId,
568
+ span
569
+ } = operation;
570
+ const resource = {
571
+ attributes: [
572
+ attribute("service.name", service),
573
+ attribute("deployment.environment.name", "production")
574
+ ]
575
+ };
576
+ const attributes = [
577
+ attribute("disposabl.source", source),
578
+ attribute("disposabl.operation", name)
579
+ ];
580
+ const context = { traceId: span.traceId, spanId: span.spanId, attributes };
581
+ const send = (signal, data) => fetch(`${config.host}/i/v1/${signal}`, {
582
+ method: "POST",
583
+ redirect: "error",
584
+ signal: AbortSignal.timeout(timeout),
585
+ headers: {
586
+ Authorization: `Bearer ${config.token}`,
587
+ "Content-Type": "application/json"
588
+ },
589
+ body: JSON.stringify(data)
590
+ });
591
+ const endTime = nanoseconds(Math.max(startedAt, endedAt));
592
+ const pending = [
593
+ send("traces", {
594
+ resourceSpans: [
595
+ {
596
+ resource,
597
+ scopeSpans: [
598
+ {
599
+ spans: [
600
+ {
601
+ ...context,
602
+ attributes: distinctId ? [
603
+ ...attributes,
604
+ attribute("posthog.distinct_id", distinctId)
605
+ ] : attributes,
606
+ ...span.parentSpanId ? { parentSpanId: span.parentSpanId } : {},
607
+ name,
608
+ kind: service === "disposabl-cli" ? 3 : 2,
609
+ startTimeUnixNano: nanoseconds(startedAt),
610
+ endTimeUnixNano: endTime,
611
+ status: { code: failed ? 2 : 1 }
612
+ }
613
+ ]
614
+ }
615
+ ]
616
+ }
617
+ ]
618
+ })
619
+ ];
620
+ if (failed)
621
+ pending.push(send("logs", {
622
+ resourceLogs: [
623
+ {
624
+ resource,
625
+ scopeLogs: [
626
+ {
627
+ logRecords: [
628
+ {
629
+ ...context,
630
+ attributes: distinctId ? [
631
+ ...attributes,
632
+ attribute("posthogDistinctId", distinctId)
633
+ ] : attributes,
634
+ timeUnixNano: endTime,
635
+ severityNumber: 17,
636
+ severityText: "ERROR",
637
+ body: { stringValue: "Disposabl operation failed" }
638
+ }
639
+ ]
640
+ }
641
+ ]
642
+ }
643
+ ]
644
+ }));
645
+ await Promise.allSettled(pending);
646
+ }
647
+
648
+ // client.ts
525
649
  function createProgram(serviceOrigin, centralAnalytics = false, dependencies = {}) {
526
650
  const version = dependencies.version ?? package_default.version;
527
651
  const path = () => dependencies.telemetryPath ?? telemetryStatePath();
@@ -628,7 +752,8 @@ Disposabl collects limited CLI usage when configured. Set DO_NOT_TRACK=1 to opt
628
752
  const send = (token) => fetch(`${base}/api/users/current`, {
629
753
  headers: {
630
754
  Authorization: `Bearer ${token}`,
631
- "X-Disposabl-Source": "cli"
755
+ "X-Disposabl-Source": "cli",
756
+ ...telemetryOptedOut() && { DNT: "1" }
632
757
  },
633
758
  redirect: "error",
634
759
  signal: AbortSignal.timeout(30000)
@@ -717,20 +842,42 @@ function requestOperation(base, path, method, input, timeout = 30000) {
717
842
  if (method === "get")
718
843
  for (const [name, value] of Object.entries(input))
719
844
  target.searchParams.set(name, JSON.stringify(value));
720
- const send = async (refresh = false) => fetch(target, {
845
+ const send = async (traceparent, refresh = false) => fetch(target, {
721
846
  method: method.toUpperCase(),
722
847
  redirect: "error",
723
848
  signal: AbortSignal.timeout(timeout),
724
849
  headers: {
725
850
  Authorization: `Bearer ${await accessToken(base, refresh)}`,
726
851
  "Content-Type": "application/json",
727
- "X-Disposabl-Source": "cli"
852
+ "X-Disposabl-Source": "cli",
853
+ ...telemetryOptedOut() && { DNT: "1" },
854
+ ...traceparent && { traceparent }
728
855
  },
729
856
  body: method === "post" ? JSON.stringify(input) : undefined
730
857
  });
731
858
  return async () => {
732
- const response = await send();
733
- return response.status === 401 && !process.env.DISPOSABL_API_KEY ? send(true) : response;
859
+ const config = base === "https://www.disposabl.dev" && telemetryConfig(package_default.version);
860
+ const span = config && startSpan();
861
+ const startedAt = Date.now();
862
+ let failed = true;
863
+ try {
864
+ let response = await send(span ? span.traceparent : undefined);
865
+ if (response.status === 401 && !process.env.DISPOSABL_API_KEY)
866
+ response = await send(span ? span.traceparent : undefined, true);
867
+ failed = !response.ok;
868
+ return response;
869
+ } finally {
870
+ if (config && span)
871
+ await exportOperation(config, {
872
+ service: "disposabl-cli",
873
+ name: "cli_api_request",
874
+ source: "cli",
875
+ startedAt,
876
+ endedAt: Date.now(),
877
+ failed,
878
+ span
879
+ }, 500).catch(() => {});
880
+ }
734
881
  };
735
882
  }
736
883
  function needsContract(program, argv, origin) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@disposabl/cli",
3
- "version": "3.0.0",
3
+ "version": "4.1.0",
4
4
  "type": "module",
5
5
  "description": "Command-line access to your Disposabl app",
6
6
  "license": "MIT",