@simbatech/simbatechsite 1.0.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Wanosoft
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Simbatechsite
2
+
3
+ > Simple localhost tunneling powered by Cloudflare
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install -g @simbatech/simbatechsite
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```bash
14
+ # Authenticate (first time only)
15
+ simbatechsite auth
16
+
17
+ # Expose port 3000
18
+ simbatechsite 3000
19
+
20
+ # With custom subdomain
21
+ simbatechsite 3000 -s myapp
22
+ # → https://myapp.simbatech.link
23
+ ```
24
+
25
+ ## CLI Options
26
+
27
+ ```bash
28
+ simbatechsite <port> [options]
29
+ ```
30
+
31
+ | Option | Description |
32
+ |--------|-------------|
33
+ | `-s, --subdomain` | Custom subdomain |
34
+ | `-t, --timeout` | Timeout in hours (default: 4, 0 = unlimited) |
35
+ | `-h, --help` | Show help |
36
+ | `auth` | Authenticate with token |
37
+ | `logout` | Remove saved token |
38
+
39
+ ## Examples
40
+
41
+ ```bash
42
+ # Unlimited duration
43
+ simbatechsite 3000 -s myapp -t 0
44
+
45
+ # Custom timeout (2 hours)
46
+ simbatechsite 8080 --timeout 2
47
+ ```
48
+
49
+ ## Features
50
+
51
+ - ⚡ Instant setup
52
+ - 🔒 HTTPS via Cloudflare
53
+ - 🌐 Custom subdomains
54
+ - 💻 Cross-platform (Windows, macOS, Linux)
55
+
56
+ ## Credits
57
+
58
+ Based on [nport](https://www.npmjs.com/package/nport)
59
+
60
+ ## License
61
+
62
+ MIT
package/analytics.js ADDED
@@ -0,0 +1,227 @@
1
+ // ============================================================================
2
+ // Analytics for CLI (Optional)
3
+ // Using Google Analytics 4 Measurement Protocol
4
+ // ============================================================================
5
+
6
+ import axios from "axios";
7
+ import { createHash, randomUUID } from "crypto";
8
+ import os from "os";
9
+ import fs from "fs";
10
+ import path from "path";
11
+ import { fileURLToPath } from "url";
12
+
13
+ const __filename = fileURLToPath(import.meta.url);
14
+ const __dirname = path.dirname(__filename);
15
+
16
+ // Analytics-specific config (for GA4 Measurement Protocol)
17
+ const FIREBASE_CONFIG = {
18
+ measurementId: process.env.SIMBATECHSITE_GA_MEASUREMENT_ID || "",
19
+ apiSecret: process.env.SIMBATECHSITE_ANALYTICS_SECRET || "",
20
+ };
21
+
22
+ // Analytics Configuration
23
+ const ANALYTICS_CONFIG = {
24
+ enabled: true,
25
+ debug: process.env.SIMBATECHSITE_DEBUG === "true",
26
+ timeout: 2000,
27
+ userIdFile: path.join(os.homedir(), ".simbatechsite-analytics"),
28
+ };
29
+
30
+ // ============================================================================
31
+ // Analytics Manager
32
+ // ============================================================================
33
+
34
+ class AnalyticsManager {
35
+ constructor() {
36
+ this.userId = null;
37
+ this.sessionId = null;
38
+ this.disabled = false;
39
+
40
+ // Disable analytics if environment variable is set
41
+ if (process.env.SIMBATECHSITE_ANALYTICS === "false") {
42
+ this.disabled = true;
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Initialize analytics - must be called before tracking
48
+ */
49
+ async initialize() {
50
+ if (this.disabled) return;
51
+
52
+ // Check if API secret is configured
53
+ if (!FIREBASE_CONFIG.apiSecret || !FIREBASE_CONFIG.measurementId) {
54
+ this.disabled = true;
55
+ return;
56
+ }
57
+
58
+ try {
59
+ this.userId = await this.getUserId();
60
+ this.sessionId = this.generateSessionId();
61
+
62
+ if (ANALYTICS_CONFIG.debug) {
63
+ console.log("[Analytics] Initialized successfully");
64
+ }
65
+ } catch (error) {
66
+ if (ANALYTICS_CONFIG.debug) {
67
+ console.error("[Analytics] Initialization failed:", error.message);
68
+ }
69
+ this.disabled = true;
70
+ }
71
+ }
72
+
73
+ /**
74
+ * Get or create a persistent user ID
75
+ */
76
+ async getUserId() {
77
+ try {
78
+ if (fs.existsSync(ANALYTICS_CONFIG.userIdFile)) {
79
+ const userId = fs.readFileSync(ANALYTICS_CONFIG.userIdFile, "utf8").trim();
80
+ if (userId) return userId;
81
+ }
82
+
83
+ const userId = this.generateAnonymousId();
84
+ fs.writeFileSync(ANALYTICS_CONFIG.userIdFile, userId, "utf8");
85
+
86
+ return userId;
87
+ } catch (error) {
88
+ return this.generateAnonymousId();
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Generate anonymous user ID based on machine characteristics
94
+ */
95
+ generateAnonymousId() {
96
+ const machineId = [
97
+ os.hostname(),
98
+ os.platform(),
99
+ os.arch(),
100
+ os.homedir(),
101
+ ].join("-");
102
+
103
+ return createHash("sha256").update(machineId).digest("hex").substring(0, 32);
104
+ }
105
+
106
+ /**
107
+ * Generate session ID
108
+ */
109
+ generateSessionId() {
110
+ return randomUUID();
111
+ }
112
+
113
+ /**
114
+ * Track an event
115
+ */
116
+ async trackEvent(eventName, params = {}) {
117
+ if (this.disabled || !ANALYTICS_CONFIG.enabled) return;
118
+
119
+ try {
120
+ const payload = this.buildPayload(eventName, params);
121
+
122
+ axios.post(
123
+ `https://www.google-analytics.com/mp/collect?measurement_id=${FIREBASE_CONFIG.measurementId}&api_secret=${FIREBASE_CONFIG.apiSecret}`,
124
+ payload,
125
+ {
126
+ timeout: ANALYTICS_CONFIG.timeout,
127
+ headers: { "Content-Type": "application/json" },
128
+ }
129
+ ).catch(() => { });
130
+
131
+ if (ANALYTICS_CONFIG.debug) {
132
+ console.log("[Analytics] Event tracked:", eventName, params);
133
+ }
134
+ } catch (error) {
135
+ // Silently fail
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Build GA4 Measurement Protocol payload
141
+ */
142
+ buildPayload(eventName, params) {
143
+ return {
144
+ client_id: this.userId,
145
+ events: [
146
+ {
147
+ name: eventName,
148
+ params: {
149
+ session_id: this.sessionId,
150
+ engagement_time_msec: "100",
151
+ ...this.getSystemInfo(),
152
+ ...params,
153
+ },
154
+ },
155
+ ],
156
+ };
157
+ }
158
+
159
+ /**
160
+ * Get system information for context
161
+ */
162
+ getSystemInfo() {
163
+ return {
164
+ os_platform: os.platform(),
165
+ os_version: os.release(),
166
+ os_arch: os.arch(),
167
+ node_version: process.version,
168
+ };
169
+ }
170
+
171
+ /**
172
+ * Track CLI start
173
+ */
174
+ async trackCliStart(port, subdomain, version) {
175
+ await this.trackEvent("cli_start", {
176
+ port: String(port),
177
+ has_custom_subdomain: subdomain && !subdomain.startsWith("user-"),
178
+ cli_version: version,
179
+ });
180
+ }
181
+
182
+ /**
183
+ * Track tunnel creation
184
+ */
185
+ async trackTunnelCreated(subdomain, port) {
186
+ await this.trackEvent("tunnel_created", {
187
+ subdomain_type: subdomain.startsWith("user-") ? "random" : "custom",
188
+ port: String(port),
189
+ });
190
+ }
191
+
192
+ /**
193
+ * Track tunnel error
194
+ */
195
+ async trackTunnelError(errorType, errorMessage) {
196
+ await this.trackEvent("tunnel_error", {
197
+ error_type: errorType,
198
+ error_message: errorMessage.substring(0, 100),
199
+ });
200
+ }
201
+
202
+ /**
203
+ * Track tunnel shutdown
204
+ */
205
+ async trackTunnelShutdown(reason, durationSeconds) {
206
+ await this.trackEvent("tunnel_shutdown", {
207
+ shutdown_reason: reason,
208
+ duration_seconds: String(Math.floor(durationSeconds)),
209
+ });
210
+ }
211
+
212
+ /**
213
+ * Track CLI update notification shown
214
+ */
215
+ async trackUpdateAvailable(currentVersion, latestVersion) {
216
+ await this.trackEvent("update_available", {
217
+ current_version: currentVersion,
218
+ latest_version: latestVersion,
219
+ });
220
+ }
221
+ }
222
+
223
+ // ============================================================================
224
+ // Export singleton instance
225
+ // ============================================================================
226
+
227
+ export const analytics = new AnalyticsManager();