@dexto/analytics 1.2.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/LICENSE +44 -0
- package/README.md +27 -0
- package/dist/constants.cjs +40 -0
- package/dist/constants.d.ts +19 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +13 -0
- package/dist/index.cjs +39 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/state.cjs +86 -0
- package/dist/state.d.ts +23 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +50 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Elastic License 2.0 (ELv2)
|
|
2
|
+
|
|
3
|
+
**Acceptance**
|
|
4
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
5
|
+
|
|
6
|
+
**Copyright License**
|
|
7
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide, non-sublicensable, non-transferable license to use, copy, distribute, make available, and prepare derivative works of the software, in each case subject to the limitations and conditions below
|
|
8
|
+
|
|
9
|
+
**Limitations**
|
|
10
|
+
You may not provide the software to third parties as a hosted or managed service, where the service provides users with access to any substantial set of the features or functionality of the software.
|
|
11
|
+
|
|
12
|
+
You may not move, change, disable, or circumvent the license key functionality in the software, and you may not remove or obscure any functionality in the software that is protected by the license key.
|
|
13
|
+
|
|
14
|
+
You may not alter, remove, or obscure any licensing, copyright, or other notices of the licensor in the software. Any use of the licensor’s trademarks is subject to applicable law.
|
|
15
|
+
|
|
16
|
+
**Patents**
|
|
17
|
+
The licensor grants you a license, under any patent claims the licensor can license, or becomes able to license, to make, have made, use, sell, offer for sale, import and have imported the software, in each case subject to the limitations and conditions in this license. This license does not cover any patent claims that you cause to be infringed by modifications or additions to the software. If you or your company make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
18
|
+
|
|
19
|
+
**Notices**
|
|
20
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms.
|
|
21
|
+
|
|
22
|
+
If you modify the software, you must include in any modified copies of the software prominent notices stating that you have modified the software.
|
|
23
|
+
|
|
24
|
+
**No Other Rights**
|
|
25
|
+
These terms do not imply any licenses other than those expressly granted in these terms.
|
|
26
|
+
|
|
27
|
+
**Termination**
|
|
28
|
+
If you use the software in violation of these terms, such use is not licensed, and your licenses will automatically terminate. If the licensor provides you with a notice of your violation, and you cease all violation of this license no later than 30 days after you receive that notice, your licenses will be reinstated retroactively. However, if you violate these terms after such reinstatement, any additional violation of these terms will cause your licenses to terminate automatically and permanently.
|
|
29
|
+
|
|
30
|
+
**No Liability**
|
|
31
|
+
As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.
|
|
32
|
+
|
|
33
|
+
**Definitions**
|
|
34
|
+
The _licensor_ is the entity offering these terms, and the _software_ is the software the licensor makes available under these terms, including any portion of it.
|
|
35
|
+
|
|
36
|
+
_you_ refers to the individual or entity agreeing to these terms.
|
|
37
|
+
|
|
38
|
+
_your company_ is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. _control_ means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
39
|
+
|
|
40
|
+
_your licenses_ are all the licenses granted to you for the software under these terms.
|
|
41
|
+
|
|
42
|
+
_use_ means anything you do with the software requiring one of your licenses.
|
|
43
|
+
|
|
44
|
+
_trademark_ means trademarks, service marks, and similar rights.
|
package/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# @dexto/analytics
|
|
2
|
+
|
|
3
|
+
Shared analytics utilities for Dexto CLI and WebUI.
|
|
4
|
+
|
|
5
|
+
## What's included
|
|
6
|
+
|
|
7
|
+
- **constants.ts**: PostHog configuration (keys, host, analytics disabled check)
|
|
8
|
+
- **state.ts**: Analytics state management (distinct ID persistence)
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```typescript
|
|
13
|
+
import { loadState, isAnalyticsDisabled, DEFAULT_POSTHOG_KEY } from '@dexto/analytics';
|
|
14
|
+
|
|
15
|
+
// Check if analytics is disabled
|
|
16
|
+
if (isAnalyticsDisabled()) {
|
|
17
|
+
// Skip analytics
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Load analytics state (distinct ID)
|
|
21
|
+
const state = await loadState();
|
|
22
|
+
console.log(state.distinctId);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Note
|
|
26
|
+
|
|
27
|
+
This is an internal package used by `@dexto/cli` and `@dexto/webui`. It is marked as private and not published to npm.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var constants_exports = {};
|
|
20
|
+
__export(constants_exports, {
|
|
21
|
+
COMMAND_TIMEOUT_MS: () => COMMAND_TIMEOUT_MS,
|
|
22
|
+
DEFAULT_POSTHOG_HOST: () => DEFAULT_POSTHOG_HOST,
|
|
23
|
+
DEFAULT_POSTHOG_KEY: () => DEFAULT_POSTHOG_KEY,
|
|
24
|
+
isAnalyticsDisabled: () => isAnalyticsDisabled
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(constants_exports);
|
|
27
|
+
const DEFAULT_POSTHOG_KEY = "phc_IJHITHjBKOjDyFiVeilfdumcGniXMuLeXeiLQhYvwDW";
|
|
28
|
+
const DEFAULT_POSTHOG_HOST = "https://app.posthog.com";
|
|
29
|
+
function isAnalyticsDisabled() {
|
|
30
|
+
const v = process.env.DEXTO_ANALYTICS_DISABLED;
|
|
31
|
+
return typeof v === "string" && /^(1|true|yes)$/i.test(v);
|
|
32
|
+
}
|
|
33
|
+
const COMMAND_TIMEOUT_MS = 12e4;
|
|
34
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
35
|
+
0 && (module.exports = {
|
|
36
|
+
COMMAND_TIMEOUT_MS,
|
|
37
|
+
DEFAULT_POSTHOG_HOST,
|
|
38
|
+
DEFAULT_POSTHOG_KEY,
|
|
39
|
+
isAnalyticsDisabled
|
|
40
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const DEFAULT_POSTHOG_KEY = "phc_IJHITHjBKOjDyFiVeilfdumcGniXMuLeXeiLQhYvwDW";
|
|
2
|
+
export declare const DEFAULT_POSTHOG_HOST = "https://app.posthog.com";
|
|
3
|
+
/**
|
|
4
|
+
* Single opt-out switch for analytics.
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* DEXTO_ANALYTICS_DISABLED=1 dexto ...
|
|
8
|
+
*
|
|
9
|
+
* When set to a truthy value ("1", "true", "yes"), analytics are fully disabled.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isAnalyticsDisabled(): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Generic per-command timeout (in milliseconds) used by the analytics wrapper.
|
|
14
|
+
*
|
|
15
|
+
* This does NOT terminate the command. It emits a non-terminating timeout
|
|
16
|
+
* event when the duration threshold is crossed to help diagnose long runs.
|
|
17
|
+
*/
|
|
18
|
+
export declare const COMMAND_TIMEOUT_MS = 120000;
|
|
19
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,mBAAmB,oDAAoD,CAAC;AACrF,eAAO,MAAM,oBAAoB,4BAA4B,CAAC;AAE9D;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,IAAI,OAAO,CAG7C;AAED;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,SAAS,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const DEFAULT_POSTHOG_KEY = "phc_IJHITHjBKOjDyFiVeilfdumcGniXMuLeXeiLQhYvwDW";
|
|
2
|
+
const DEFAULT_POSTHOG_HOST = "https://app.posthog.com";
|
|
3
|
+
function isAnalyticsDisabled() {
|
|
4
|
+
const v = process.env.DEXTO_ANALYTICS_DISABLED;
|
|
5
|
+
return typeof v === "string" && /^(1|true|yes)$/i.test(v);
|
|
6
|
+
}
|
|
7
|
+
const COMMAND_TIMEOUT_MS = 12e4;
|
|
8
|
+
export {
|
|
9
|
+
COMMAND_TIMEOUT_MS,
|
|
10
|
+
DEFAULT_POSTHOG_HOST,
|
|
11
|
+
DEFAULT_POSTHOG_KEY,
|
|
12
|
+
isAnalyticsDisabled
|
|
13
|
+
};
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var index_exports = {};
|
|
20
|
+
__export(index_exports, {
|
|
21
|
+
COMMAND_TIMEOUT_MS: () => import_constants.COMMAND_TIMEOUT_MS,
|
|
22
|
+
DEFAULT_POSTHOG_HOST: () => import_constants.DEFAULT_POSTHOG_HOST,
|
|
23
|
+
DEFAULT_POSTHOG_KEY: () => import_constants.DEFAULT_POSTHOG_KEY,
|
|
24
|
+
isAnalyticsDisabled: () => import_constants.isAnalyticsDisabled,
|
|
25
|
+
loadState: () => import_state.loadState,
|
|
26
|
+
saveState: () => import_state.saveState
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
var import_constants = require("./constants.js");
|
|
30
|
+
var import_state = require("./state.js");
|
|
31
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
32
|
+
0 && (module.exports = {
|
|
33
|
+
COMMAND_TIMEOUT_MS,
|
|
34
|
+
DEFAULT_POSTHOG_HOST,
|
|
35
|
+
DEFAULT_POSTHOG_KEY,
|
|
36
|
+
isAnalyticsDisabled,
|
|
37
|
+
loadState,
|
|
38
|
+
saveState
|
|
39
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EACH,mBAAmB,EACnB,oBAAoB,EACpB,kBAAkB,EAClB,mBAAmB,GACtB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAClD,YAAY,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_POSTHOG_KEY,
|
|
3
|
+
DEFAULT_POSTHOG_HOST,
|
|
4
|
+
COMMAND_TIMEOUT_MS,
|
|
5
|
+
isAnalyticsDisabled
|
|
6
|
+
} from "./constants.js";
|
|
7
|
+
import { loadState, saveState } from "./state.js";
|
|
8
|
+
export {
|
|
9
|
+
COMMAND_TIMEOUT_MS,
|
|
10
|
+
DEFAULT_POSTHOG_HOST,
|
|
11
|
+
DEFAULT_POSTHOG_KEY,
|
|
12
|
+
isAnalyticsDisabled,
|
|
13
|
+
loadState,
|
|
14
|
+
saveState
|
|
15
|
+
};
|
package/dist/state.cjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var state_exports = {};
|
|
30
|
+
__export(state_exports, {
|
|
31
|
+
loadState: () => loadState,
|
|
32
|
+
saveState: () => saveState
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(state_exports);
|
|
35
|
+
var import_fs = require("fs");
|
|
36
|
+
var path = __toESM(require("path"), 1);
|
|
37
|
+
var import_os = __toESM(require("os"), 1);
|
|
38
|
+
var import_crypto = require("crypto");
|
|
39
|
+
var import_module = require("module");
|
|
40
|
+
var import_core = require("@dexto/core");
|
|
41
|
+
const import_meta = {};
|
|
42
|
+
const requireCJS = (0, import_module.createRequire)(import_meta.url);
|
|
43
|
+
const { machineIdSync } = requireCJS("node-machine-id");
|
|
44
|
+
const STATE_DIR = (0, import_core.getDextoGlobalPath)("telemetry");
|
|
45
|
+
const STATE_FILE = path.join(STATE_DIR, "state.json");
|
|
46
|
+
async function loadState() {
|
|
47
|
+
try {
|
|
48
|
+
const content = await import_fs.promises.readFile(STATE_FILE, "utf8");
|
|
49
|
+
const parsed = JSON.parse(content);
|
|
50
|
+
if (!parsed.distinctId) throw new Error("invalid state");
|
|
51
|
+
return {
|
|
52
|
+
distinctId: parsed.distinctId,
|
|
53
|
+
createdAt: parsed.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
54
|
+
commandRunCounts: parsed.commandRunCounts ?? {}
|
|
55
|
+
};
|
|
56
|
+
} catch {
|
|
57
|
+
await import_fs.promises.mkdir(STATE_DIR, { recursive: true });
|
|
58
|
+
const state = {
|
|
59
|
+
distinctId: computeDistinctId(),
|
|
60
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
61
|
+
commandRunCounts: {}
|
|
62
|
+
};
|
|
63
|
+
await saveState(state);
|
|
64
|
+
return state;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function saveState(state) {
|
|
68
|
+
await import_fs.promises.mkdir(STATE_DIR, { recursive: true });
|
|
69
|
+
await import_fs.promises.writeFile(STATE_FILE, JSON.stringify(state, null, 2), "utf8");
|
|
70
|
+
}
|
|
71
|
+
function computeDistinctId() {
|
|
72
|
+
try {
|
|
73
|
+
const id = machineIdSync(true);
|
|
74
|
+
if (typeof id === "string" && id.length > 0) return `DEXTO-${id}`;
|
|
75
|
+
} catch {
|
|
76
|
+
}
|
|
77
|
+
const hostname = import_os.default.hostname() || "unknown-host";
|
|
78
|
+
const digest = (0, import_crypto.createHash)("sha256").update(hostname).digest("hex");
|
|
79
|
+
if (digest) return `DEXTO-${digest.slice(0, 32)}`;
|
|
80
|
+
return `DEXTO-${(0, import_crypto.randomUUID)()}`;
|
|
81
|
+
}
|
|
82
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
83
|
+
0 && (module.exports = {
|
|
84
|
+
loadState,
|
|
85
|
+
saveState
|
|
86
|
+
});
|
package/dist/state.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shape of the persisted analytics state written to
|
|
3
|
+
* ~/.dexto/telemetry/state.json.
|
|
4
|
+
*
|
|
5
|
+
* - distinctId: Anonymous ID (UUID) for grouping events by machine.
|
|
6
|
+
* - createdAt: ISO timestamp when the state was first created.
|
|
7
|
+
* - commandRunCounts: Local counters per command for coarse diagnostics.
|
|
8
|
+
*/
|
|
9
|
+
export interface AnalyticsState {
|
|
10
|
+
distinctId: string;
|
|
11
|
+
createdAt: string;
|
|
12
|
+
commandRunCounts?: Record<string, number>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Load the persisted analytics state, creating a new file if missing.
|
|
16
|
+
* Returns a valid state object with defaults populated.
|
|
17
|
+
*/
|
|
18
|
+
export declare function loadState(): Promise<AnalyticsState>;
|
|
19
|
+
/**
|
|
20
|
+
* Persist the analytics state to ~/.dexto/telemetry/state.json.
|
|
21
|
+
*/
|
|
22
|
+
export declare function saveState(state: AnalyticsState): Promise<void>;
|
|
23
|
+
//# sourceMappingURL=state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAcA;;;;;;;GAOG;AACH,MAAM,WAAW,cAAc;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC7C;AAKD;;;GAGG;AACH,wBAAsB,SAAS,IAAI,OAAO,CAAC,cAAc,CAAC,CAqBzD;AAED;;GAEG;AACH,wBAAsB,SAAS,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAGpE"}
|
package/dist/state.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { promises as fs } from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import os from "os";
|
|
4
|
+
import { randomUUID, createHash } from "crypto";
|
|
5
|
+
import { createRequire } from "module";
|
|
6
|
+
const requireCJS = createRequire(import.meta.url);
|
|
7
|
+
const { machineIdSync } = requireCJS("node-machine-id");
|
|
8
|
+
import { getDextoGlobalPath } from "@dexto/core";
|
|
9
|
+
const STATE_DIR = getDextoGlobalPath("telemetry");
|
|
10
|
+
const STATE_FILE = path.join(STATE_DIR, "state.json");
|
|
11
|
+
async function loadState() {
|
|
12
|
+
try {
|
|
13
|
+
const content = await fs.readFile(STATE_FILE, "utf8");
|
|
14
|
+
const parsed = JSON.parse(content);
|
|
15
|
+
if (!parsed.distinctId) throw new Error("invalid state");
|
|
16
|
+
return {
|
|
17
|
+
distinctId: parsed.distinctId,
|
|
18
|
+
createdAt: parsed.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
19
|
+
commandRunCounts: parsed.commandRunCounts ?? {}
|
|
20
|
+
};
|
|
21
|
+
} catch {
|
|
22
|
+
await fs.mkdir(STATE_DIR, { recursive: true });
|
|
23
|
+
const state = {
|
|
24
|
+
distinctId: computeDistinctId(),
|
|
25
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
26
|
+
commandRunCounts: {}
|
|
27
|
+
};
|
|
28
|
+
await saveState(state);
|
|
29
|
+
return state;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function saveState(state) {
|
|
33
|
+
await fs.mkdir(STATE_DIR, { recursive: true });
|
|
34
|
+
await fs.writeFile(STATE_FILE, JSON.stringify(state, null, 2), "utf8");
|
|
35
|
+
}
|
|
36
|
+
function computeDistinctId() {
|
|
37
|
+
try {
|
|
38
|
+
const id = machineIdSync(true);
|
|
39
|
+
if (typeof id === "string" && id.length > 0) return `DEXTO-${id}`;
|
|
40
|
+
} catch {
|
|
41
|
+
}
|
|
42
|
+
const hostname = os.hostname() || "unknown-host";
|
|
43
|
+
const digest = createHash("sha256").update(hostname).digest("hex");
|
|
44
|
+
if (digest) return `DEXTO-${digest.slice(0, 32)}`;
|
|
45
|
+
return `DEXTO-${randomUUID()}`;
|
|
46
|
+
}
|
|
47
|
+
export {
|
|
48
|
+
loadState,
|
|
49
|
+
saveState
|
|
50
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dexto/analytics",
|
|
3
|
+
"version": "1.2.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.cjs"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"node-machine-id": "^1.1.12",
|
|
17
|
+
"@dexto/core": "1.2.2"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsup && tsc -p tsconfig.json --emitDeclarationOnly",
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
30
|
+
"lint": "eslint . --ext .ts"
|
|
31
|
+
}
|
|
32
|
+
}
|