opensecurity 0.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.
@@ -0,0 +1,72 @@
1
+ import { loadGlobalConfig, saveGlobalConfig } from "./config.js";
2
+ const DEFAULT_ENDPOINT = "https://telemetry.opensecurity.dev/v1/events";
3
+ /**
4
+ * Check if telemetry is enabled.
5
+ * Respects:
6
+ * 1. OPENSECURITY_TELEMETRY env var (0/false to disable, 1/true to enable)
7
+ * 2. Global config `telemetry.enabled` field
8
+ * 3. Defaults to false (opt-in)
9
+ */
10
+ export function isTelemetryEnabled(config, env = process.env) {
11
+ const envVal = env.OPENSECURITY_TELEMETRY?.trim().toLowerCase();
12
+ if (envVal === "0" || envVal === "false")
13
+ return false;
14
+ if (envVal === "1" || envVal === "true")
15
+ return true;
16
+ return config.telemetry?.enabled === true;
17
+ }
18
+ /**
19
+ * Enable or disable telemetry in the global config.
20
+ */
21
+ export async function setTelemetryEnabled(enabled, env = process.env) {
22
+ const config = await loadGlobalConfig(env);
23
+ const updated = {
24
+ ...config,
25
+ telemetry: { ...(config.telemetry ?? {}), enabled }
26
+ };
27
+ await saveGlobalConfig(updated, env);
28
+ }
29
+ /**
30
+ * Build a telemetry event with system-level (non-identifying) metadata.
31
+ */
32
+ export function createEvent(event, properties = {}) {
33
+ return {
34
+ event,
35
+ properties: {
36
+ ...properties,
37
+ os: process.platform,
38
+ arch: process.arch,
39
+ nodeVersion: process.version,
40
+ cliVersion: "0.1.0"
41
+ },
42
+ timestamp: new Date().toISOString()
43
+ };
44
+ }
45
+ /**
46
+ * Send a telemetry event. No-ops if telemetry is disabled.
47
+ * Never throws — all errors are silently swallowed to avoid
48
+ * impacting the user experience.
49
+ */
50
+ export async function sendEvent(event, config, env = process.env) {
51
+ if (!isTelemetryEnabled(config, env))
52
+ return;
53
+ const endpoint = config.telemetry?.endpoint ?? DEFAULT_ENDPOINT;
54
+ try {
55
+ await fetch(endpoint, {
56
+ method: "POST",
57
+ headers: { "Content-Type": "application/json" },
58
+ body: JSON.stringify(event),
59
+ signal: AbortSignal.timeout(3000)
60
+ });
61
+ }
62
+ catch {
63
+ // Silently ignore — telemetry must never interrupt the user
64
+ }
65
+ }
66
+ /**
67
+ * Convenience: create + send in one call.
68
+ */
69
+ export async function trackEvent(eventName, properties = {}, config, env = process.env) {
70
+ const event = createEvent(eventName, properties);
71
+ await sendEvent(event, config, env);
72
+ }
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "opensecurity",
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "type": "module",
6
+ "bin": {
7
+ "opensecurity": "dist/cli.js",
8
+ "openSecurity": "dist/cli.js"
9
+ },
10
+ "scripts": {
11
+ "dev": "tsx src/cli.ts",
12
+ "proxy": "tsx src/proxy.ts",
13
+ "build": "tsc",
14
+ "prepack": "npm run build",
15
+ "package": "npm run build && npm pack",
16
+ "test": "vitest run",
17
+ "lint": "eslint ."
18
+ },
19
+ "files": [
20
+ "dist",
21
+ "README.md",
22
+ "LICENSE"
23
+ ],
24
+ "dependencies": {
25
+ "@babel/parser": "^7.26.9",
26
+ "@babel/traverse": "^7.26.9",
27
+ "@babel/types": "^7.26.9",
28
+ "commander": "^12.0.0",
29
+ "picomatch": "^4.0.2",
30
+ "semver": "^7.6.3"
31
+ },
32
+ "devDependencies": {
33
+ "@types/babel__traverse": "^7.20.7",
34
+ "@types/node": "^22.10.0",
35
+ "@types/picomatch": "^3.0.2",
36
+ "@types/semver": "^7.5.8",
37
+ "@typescript-eslint/eslint-plugin": "^8.57.0",
38
+ "@typescript-eslint/parser": "^8.57.0",
39
+ "eslint": "^9.39.4",
40
+ "tsx": "^4.19.0",
41
+ "typescript": "^5.7.3",
42
+ "vitest": "^2.1.8"
43
+ }
44
+ }