-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
250 lines (216 loc) · 6.3 KB
/
Copy pathindex.js
File metadata and controls
250 lines (216 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* @chittycorp/app-beacon
* Zero-config app tracker
*
* Usage: require('@chittycorp/app-beacon')
* That's it! No configuration needed.
*/
const https = require('https');
const fs = require('fs');
const os = require('os');
const { execSync } = require('child_process');
// Configuration (can be overridden with env vars)
const CONFIG = {
endpoint: process.env.BEACON_ENDPOINT || 'https://beacon.cloudeto.com',
interval: parseInt(process.env.BEACON_INTERVAL) || 300000, // 5 minutes
enabled: process.env.BEACON_DISABLED !== 'true',
silent: process.env.BEACON_VERBOSE !== 'true'
};
// Auto-detect app information
function detectApp() {
const app = {
// Identity
id: generateAppId(),
name: detectAppName(),
version: detectVersion(),
// Platform
platform: detectPlatform(),
environment: process.env.NODE_ENV || 'production',
// System
hostname: os.hostname(),
node_version: process.version,
os: `${os.type()} ${os.release()}`,
// Features
has_claude_code: detectClaudeCode(),
has_git: fs.existsSync('.git'),
// Metadata
started_at: new Date().toISOString(),
pid: process.pid
};
// Add git info if available
if (app.has_git) {
try {
app.git = {
branch: execSync('git branch --show-current', { encoding: 'utf8' }).trim(),
commit: execSync('git rev-parse --short HEAD', { encoding: 'utf8' }).trim(),
remote: execSync('git remote get-url origin', { encoding: 'utf8' }).trim()
};
} catch (e) {}
}
// Platform-specific info
if (process.env.REPL_ID) {
app.replit = {
id: process.env.REPL_ID,
slug: process.env.REPL_SLUG,
owner: process.env.REPL_OWNER,
url: `https://${process.env.REPL_SLUG}.${process.env.REPL_OWNER}.repl.co`
};
}
if (process.env.GITHUB_REPOSITORY) {
app.github = {
repository: process.env.GITHUB_REPOSITORY,
workflow: process.env.GITHUB_WORKFLOW,
run_id: process.env.GITHUB_RUN_ID,
actor: process.env.GITHUB_ACTOR
};
}
if (process.env.VERCEL) {
app.vercel = {
url: process.env.VERCEL_URL,
env: process.env.VERCEL_ENV,
region: process.env.VERCEL_REGION
};
}
return app;
}
function generateAppId() {
if (process.env.REPL_ID) return `replit-${process.env.REPL_ID}`;
if (process.env.GITHUB_REPOSITORY) return `github-${process.env.GITHUB_REPOSITORY.replace('/', '-')}`;
if (process.env.VERCEL_URL) return `vercel-${process.env.VERCEL_URL}`;
if (process.env.HEROKU_APP_NAME) return `heroku-${process.env.HEROKU_APP_NAME}`;
// Generate from package.json or hostname
try {
const pkg = require(process.cwd() + '/package.json');
return `npm-${pkg.name}`;
} catch (e) {
return `host-${os.hostname()}`;
}
}
function detectAppName() {
return process.env.REPL_SLUG ||
process.env.GITHUB_REPOSITORY ||
process.env.VERCEL_URL ||
process.env.HEROKU_APP_NAME ||
process.env.npm_package_name ||
(() => {
try {
return require(process.cwd() + '/package.json').name;
} catch (e) {
return 'unnamed-app';
}
})();
}
function detectVersion() {
try {
return require(process.cwd() + '/package.json').version;
} catch (e) {
return '0.0.0';
}
}
function detectPlatform() {
if (process.env.REPL_ID) return 'replit';
if (process.env.GITHUB_ACTIONS) return 'github-actions';
if (process.env.VERCEL) return 'vercel';
if (process.env.NETLIFY) return 'netlify';
if (process.env.RENDER) return 'render';
if (process.env.HEROKU_APP_NAME) return 'heroku';
if (process.env.AWS_LAMBDA_FUNCTION_NAME) return 'aws-lambda';
if (process.env.GOOGLE_CLOUD_PROJECT) return 'google-cloud';
if (process.env.WEBSITE_INSTANCE_ID) return 'azure';
return 'unknown';
}
function detectClaudeCode() {
return process.env.CLAUDE_CODE === 'true' ||
fs.existsSync('.claude') ||
fs.existsSync('claude.json') ||
(() => {
try {
const pkg = require(process.cwd() + '/package.json');
return pkg.devDependencies?.['@anthropic/claude'] ||
pkg.dependencies?.['@anthropic/claude'];
} catch (e) {
return false;
}
})();
}
// Send beacon
function sendBeacon(data) {
if (!CONFIG.enabled) return;
const payload = JSON.stringify(data);
const url = new URL(CONFIG.endpoint);
const options = {
hostname: url.hostname,
port: url.port || 443,
path: url.pathname + '/track',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': payload.length,
'User-Agent': '@chittycorp/app-beacon/1.0.0'
}
};
const req = https.request(options, (res) => {
if (!CONFIG.silent && res.statusCode !== 200) {
console.log(`[@chittycorp/app-beacon] Response: ${res.statusCode}`);
}
});
req.on('error', (error) => {
if (!CONFIG.silent) {
console.log(`[@chittycorp/app-beacon] Error: ${error.message}`);
}
});
req.write(payload);
req.end();
}
// Initialize
function init() {
if (!CONFIG.enabled) {
if (!CONFIG.silent) {
console.log('[@chittycorp/app-beacon] Disabled');
}
return;
}
const appInfo = detectApp();
// Send initial beacon
sendBeacon({
...appInfo,
event: 'startup',
timestamp: new Date().toISOString()
});
// Send periodic heartbeats
const interval = setInterval(() => {
sendBeacon({
id: appInfo.id,
name: appInfo.name,
event: 'heartbeat',
timestamp: new Date().toISOString(),
uptime: process.uptime()
});
}, CONFIG.interval);
// Don't keep process alive just for beacon
interval.unref();
// Send shutdown beacon
const shutdown = () => {
sendBeacon({
id: appInfo.id,
name: appInfo.name,
event: 'shutdown',
timestamp: new Date().toISOString(),
uptime: process.uptime()
});
};
process.once('exit', shutdown);
process.once('SIGINT', shutdown);
process.once('SIGTERM', shutdown);
if (!CONFIG.silent) {
console.log(`[@chittycorp/app-beacon] Tracking ${appInfo.name} on ${appInfo.platform}`);
}
}
// Auto-initialize on require
init();
// Export for programmatic use
module.exports = {
detectApp,
sendBeacon,
config: CONFIG
};