@avalw/search-worker 1.1.0 → 2.1.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/README.md +320 -358
- package/bin/cli.js +239 -38
- package/index.js +887 -0
- package/package.json +15 -22
- package/LICENSE +0 -21
- package/examples/basic-usage.js +0 -32
- package/examples/express-integration.js +0 -73
- package/examples/search-api.js +0 -58
- package/lib/worker.js +0 -319
package/package.json
CHANGED
|
@@ -1,41 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@avalw/search-worker",
|
|
3
|
-
"version": "1.1
|
|
4
|
-
"description": "AVALW
|
|
5
|
-
"main": "
|
|
3
|
+
"version": "2.1.1",
|
|
4
|
+
"description": "AVALW Distributed Cache Worker - Intelligent caching with size limits and auto-cleanup",
|
|
5
|
+
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
7
7
|
"avalw-worker": "./bin/cli.js"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"start": "node bin/cli.js",
|
|
11
|
-
"test": "
|
|
11
|
+
"test": "echo \"Tests coming soon\" && exit 0"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"avalw",
|
|
15
15
|
"search",
|
|
16
16
|
"worker",
|
|
17
17
|
"distributed",
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"search-engine",
|
|
21
|
-
"decentralized"
|
|
18
|
+
"cache",
|
|
19
|
+
"compute"
|
|
22
20
|
],
|
|
23
|
-
"author": "AVALW
|
|
21
|
+
"author": "AVALW Team",
|
|
24
22
|
"license": "MIT",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/avalw/search-worker"
|
|
26
|
+
},
|
|
25
27
|
"homepage": "https://worker.avalw.org",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"ws": "^8.14.2"
|
|
30
|
+
},
|
|
26
31
|
"engines": {
|
|
27
32
|
"node": ">=14.0.0"
|
|
28
|
-
}
|
|
29
|
-
"os": [
|
|
30
|
-
"darwin",
|
|
31
|
-
"linux",
|
|
32
|
-
"win32"
|
|
33
|
-
],
|
|
34
|
-
"files": [
|
|
35
|
-
"bin/",
|
|
36
|
-
"lib/",
|
|
37
|
-
"examples/",
|
|
38
|
-
"README.md",
|
|
39
|
-
"LICENSE"
|
|
40
|
-
]
|
|
33
|
+
}
|
|
41
34
|
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 AVALW
|
|
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/examples/basic-usage.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Basic Usage Example
|
|
3
|
-
*
|
|
4
|
-
* This example shows how to use the AVALW Worker programmatically
|
|
5
|
-
* in your Node.js application.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const { AvalwWorker } = require('@avalw/search-worker');
|
|
9
|
-
|
|
10
|
-
// Replace with your actual token from worker.avalw.org
|
|
11
|
-
const WORKER_TOKEN = 'YOUR_WORKER_TOKEN_HERE';
|
|
12
|
-
|
|
13
|
-
async function main() {
|
|
14
|
-
// Create worker instance
|
|
15
|
-
const worker = new AvalwWorker(WORKER_TOKEN);
|
|
16
|
-
|
|
17
|
-
// Handle shutdown gracefully
|
|
18
|
-
process.on('SIGINT', async () => {
|
|
19
|
-
console.log('\nStopping worker...');
|
|
20
|
-
await worker.stop();
|
|
21
|
-
process.exit(0);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
// Start the worker
|
|
25
|
-
console.log('Starting AVALW Worker...');
|
|
26
|
-
await worker.start();
|
|
27
|
-
|
|
28
|
-
// Worker is now running and contributing compute power
|
|
29
|
-
// Your search API is now active!
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
main().catch(console.error);
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Express.js Integration Example
|
|
3
|
-
*
|
|
4
|
-
* This example shows how to integrate AVALW Search
|
|
5
|
-
* into an Express.js application.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const express = require('express');
|
|
9
|
-
const { AvalwWorker } = require('@avalw/search-worker');
|
|
10
|
-
|
|
11
|
-
// Replace with your actual token
|
|
12
|
-
const WORKER_TOKEN = 'YOUR_WORKER_TOKEN_HERE';
|
|
13
|
-
const API_URL = 'https://worker.avalw.org';
|
|
14
|
-
|
|
15
|
-
const app = express();
|
|
16
|
-
const PORT = 3000;
|
|
17
|
-
|
|
18
|
-
// Start the worker when the server starts
|
|
19
|
-
const worker = new AvalwWorker(WORKER_TOKEN);
|
|
20
|
-
|
|
21
|
-
// Search endpoint for your users
|
|
22
|
-
app.get('/search', async (req, res) => {
|
|
23
|
-
const { q, page = 1 } = req.query;
|
|
24
|
-
|
|
25
|
-
if (!q) {
|
|
26
|
-
return res.status(400).json({ error: 'Query parameter "q" is required' });
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
try {
|
|
30
|
-
const params = new URLSearchParams({
|
|
31
|
-
q,
|
|
32
|
-
token: WORKER_TOKEN,
|
|
33
|
-
page: page.toString()
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
const response = await fetch(`${API_URL}/api/search?${params}`);
|
|
37
|
-
const results = await response.json();
|
|
38
|
-
|
|
39
|
-
res.json(results);
|
|
40
|
-
} catch (error) {
|
|
41
|
-
res.status(500).json({ error: 'Search failed', message: error.message });
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
// Health check
|
|
46
|
-
app.get('/health', (req, res) => {
|
|
47
|
-
res.json({
|
|
48
|
-
status: 'ok',
|
|
49
|
-
worker: worker.running ? 'running' : 'stopped'
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// Start server and worker
|
|
54
|
-
async function start() {
|
|
55
|
-
// Start the AVALW worker
|
|
56
|
-
await worker.start();
|
|
57
|
-
console.log('AVALW Worker started');
|
|
58
|
-
|
|
59
|
-
// Start Express server
|
|
60
|
-
app.listen(PORT, () => {
|
|
61
|
-
console.log(`Server running on http://localhost:${PORT}`);
|
|
62
|
-
console.log(`Try: http://localhost:${PORT}/search?q=test`);
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Graceful shutdown
|
|
67
|
-
process.on('SIGINT', async () => {
|
|
68
|
-
console.log('\nShutting down...');
|
|
69
|
-
await worker.stop();
|
|
70
|
-
process.exit(0);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
start().catch(console.error);
|
package/examples/search-api.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Search API Example
|
|
3
|
-
*
|
|
4
|
-
* This example shows how to use the AVALW Search API
|
|
5
|
-
* once your worker is running.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
// Replace with your actual token
|
|
9
|
-
const WORKER_TOKEN = 'YOUR_WORKER_TOKEN_HERE';
|
|
10
|
-
const API_URL = 'https://worker.avalw.org';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Search using the AVALW API
|
|
14
|
-
* @param {string} query - Search query
|
|
15
|
-
* @param {number} page - Page number (optional)
|
|
16
|
-
* @returns {Promise<object>} Search results
|
|
17
|
-
*/
|
|
18
|
-
async function search(query, page = 1) {
|
|
19
|
-
const params = new URLSearchParams({
|
|
20
|
-
q: query,
|
|
21
|
-
token: WORKER_TOKEN,
|
|
22
|
-
page: page.toString()
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
const response = await fetch(`${API_URL}/api/search?${params}`);
|
|
26
|
-
|
|
27
|
-
if (!response.ok) {
|
|
28
|
-
throw new Error(`Search failed: ${response.statusText}`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return response.json();
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Example usage
|
|
35
|
-
async function main() {
|
|
36
|
-
try {
|
|
37
|
-
console.log('Searching for "privacy search engine"...\n');
|
|
38
|
-
|
|
39
|
-
const results = await search('privacy search engine');
|
|
40
|
-
|
|
41
|
-
if (results.success) {
|
|
42
|
-
console.log(`Found ${results.total || results.results?.length || 0} results:\n`);
|
|
43
|
-
|
|
44
|
-
results.results?.forEach((result, index) => {
|
|
45
|
-
console.log(`${index + 1}. ${result.title}`);
|
|
46
|
-
console.log(` ${result.url}`);
|
|
47
|
-
console.log(` ${result.snippet?.substring(0, 100)}...`);
|
|
48
|
-
console.log('');
|
|
49
|
-
});
|
|
50
|
-
} else {
|
|
51
|
-
console.log('Search error:', results.error);
|
|
52
|
-
}
|
|
53
|
-
} catch (error) {
|
|
54
|
-
console.error('Error:', error.message);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
main();
|
package/lib/worker.js
DELETED
|
@@ -1,319 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AVALW Search Worker v1.1.0
|
|
3
|
-
* Connects to worker.avalw.org and contributes compute power
|
|
4
|
-
* in exchange for free search API access.
|
|
5
|
-
*
|
|
6
|
-
* Uses Core-Hours calculation for fair tier distribution.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const https = require('https');
|
|
10
|
-
const http = require('http');
|
|
11
|
-
const os = require('os');
|
|
12
|
-
|
|
13
|
-
// Tier definitions based on core-hours
|
|
14
|
-
const TIERS = {
|
|
15
|
-
STARTER: { name: 'Starter', coreHours: 3, requests: 1000, minCores: 3 },
|
|
16
|
-
BASIC: { name: 'Basic', coreHours: 12, requests: 5000, minCores: 4 },
|
|
17
|
-
PRO: { name: 'Pro', coreHours: 36, requests: 20000, minCores: 6 },
|
|
18
|
-
BUSINESS: { name: 'Business', coreHours: 72, requests: 50000, minCores: 8 },
|
|
19
|
-
ENTERPRISE: { name: 'Enterprise', coreHours: 240, requests: 500000, minCores: 16 }
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
class AvalwWorker {
|
|
23
|
-
constructor(token, apiUrl = 'https://worker.avalw.org') {
|
|
24
|
-
this.token = token;
|
|
25
|
-
this.apiUrl = apiUrl;
|
|
26
|
-
this.running = false;
|
|
27
|
-
this.heartbeatInterval = null;
|
|
28
|
-
this.startTime = null;
|
|
29
|
-
this.coreHoursToday = 0;
|
|
30
|
-
this.currentTier = null;
|
|
31
|
-
this.cpuInfo = this.getCpuInfo();
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Get detailed CPU information
|
|
36
|
-
*/
|
|
37
|
-
getCpuInfo() {
|
|
38
|
-
const cpus = os.cpus();
|
|
39
|
-
const cores = cpus.length;
|
|
40
|
-
const model = cpus[0]?.model || 'Unknown';
|
|
41
|
-
|
|
42
|
-
// Extract speed from CPU model or use reported speed
|
|
43
|
-
let speedGHz = cpus[0]?.speed ? cpus[0].speed / 1000 : 2.5;
|
|
44
|
-
|
|
45
|
-
// Try to parse speed from model string (e.g., "@ 2.90GHz")
|
|
46
|
-
const speedMatch = model.match(/@\s*([\d.]+)\s*GHz/i);
|
|
47
|
-
if (speedMatch) {
|
|
48
|
-
speedGHz = parseFloat(speedMatch[1]);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Speed factor normalized to 3.0 GHz baseline
|
|
52
|
-
const speedFactor = speedGHz / 3.0;
|
|
53
|
-
|
|
54
|
-
return {
|
|
55
|
-
cores,
|
|
56
|
-
model,
|
|
57
|
-
speedGHz,
|
|
58
|
-
speedFactor: Math.round(speedFactor * 100) / 100,
|
|
59
|
-
totalMemoryGB: Math.round(os.totalmem() / 1024 / 1024 / 1024 * 10) / 10
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Calculate core-hours contribution
|
|
65
|
-
* @param {number} usagePercent - CPU usage percentage (0-100)
|
|
66
|
-
* @param {number} hours - Time in hours
|
|
67
|
-
*/
|
|
68
|
-
calculateCoreHours(usagePercent, hours) {
|
|
69
|
-
const { cores, speedFactor } = this.cpuInfo;
|
|
70
|
-
return cores * speedFactor * (usagePercent / 100) * hours;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Determine tier based on core-hours and minimum cores
|
|
75
|
-
*/
|
|
76
|
-
determineTier(coreHours) {
|
|
77
|
-
const { cores } = this.cpuInfo;
|
|
78
|
-
|
|
79
|
-
// Check from highest to lowest tier
|
|
80
|
-
if (cores >= TIERS.ENTERPRISE.minCores && coreHours >= TIERS.ENTERPRISE.coreHours) {
|
|
81
|
-
return TIERS.ENTERPRISE;
|
|
82
|
-
}
|
|
83
|
-
if (cores >= TIERS.BUSINESS.minCores && coreHours >= TIERS.BUSINESS.coreHours) {
|
|
84
|
-
return TIERS.BUSINESS;
|
|
85
|
-
}
|
|
86
|
-
if (cores >= TIERS.PRO.minCores && coreHours >= TIERS.PRO.coreHours) {
|
|
87
|
-
return TIERS.PRO;
|
|
88
|
-
}
|
|
89
|
-
if (cores >= TIERS.BASIC.minCores && coreHours >= TIERS.BASIC.coreHours) {
|
|
90
|
-
return TIERS.BASIC;
|
|
91
|
-
}
|
|
92
|
-
if (cores >= TIERS.STARTER.minCores && coreHours >= TIERS.STARTER.coreHours) {
|
|
93
|
-
return TIERS.STARTER;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return null; // Not enough cores or core-hours
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Start the worker
|
|
101
|
-
*/
|
|
102
|
-
async start() {
|
|
103
|
-
this.running = true;
|
|
104
|
-
this.startTime = Date.now();
|
|
105
|
-
|
|
106
|
-
// Print banner
|
|
107
|
-
console.log('');
|
|
108
|
-
console.log(' ╔═══════════════════════════════════════════════════════════════════╗');
|
|
109
|
-
console.log(' ║ AVALW Search Worker v1.1.0 ║');
|
|
110
|
-
console.log(' ╠═══════════════════════════════════════════════════════════════════╣');
|
|
111
|
-
console.log(' ║ Free Search API in exchange for compute ║');
|
|
112
|
-
console.log(' ╚═══════════════════════════════════════════════════════════════════╝');
|
|
113
|
-
console.log('');
|
|
114
|
-
|
|
115
|
-
// Print system info
|
|
116
|
-
console.log(' System Info:');
|
|
117
|
-
console.log(` ├─ CPU: ${this.cpuInfo.model}`);
|
|
118
|
-
console.log(` ├─ Cores: ${this.cpuInfo.cores}`);
|
|
119
|
-
console.log(` ├─ Speed: ${this.cpuInfo.speedGHz} GHz (factor: ${this.cpuInfo.speedFactor}x)`);
|
|
120
|
-
console.log(` └─ Memory: ${this.cpuInfo.totalMemoryGB} GB`);
|
|
121
|
-
console.log('');
|
|
122
|
-
|
|
123
|
-
// Check minimum cores requirement
|
|
124
|
-
if (this.cpuInfo.cores < TIERS.STARTER.minCores) {
|
|
125
|
-
console.log(` ⚠ WARNING: Your system has ${this.cpuInfo.cores} cores.`);
|
|
126
|
-
console.log(` ⚠ Minimum ${TIERS.STARTER.minCores} cores required for Starter tier.`);
|
|
127
|
-
console.log(` ⚠ Worker will run but you won't qualify for any tier.`);
|
|
128
|
-
console.log('');
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Print worker status
|
|
132
|
-
console.log(' Worker Status:');
|
|
133
|
-
console.log(` ├─ Token: ${this.token.substring(0, 12)}...`);
|
|
134
|
-
console.log(` ├─ API URL: ${this.apiUrl}`);
|
|
135
|
-
console.log(` └─ Target Usage: 50%`);
|
|
136
|
-
console.log('');
|
|
137
|
-
|
|
138
|
-
// Initial heartbeat
|
|
139
|
-
await this.sendHeartbeat();
|
|
140
|
-
|
|
141
|
-
// Start heartbeat loop (every 30 seconds)
|
|
142
|
-
this.heartbeatInterval = setInterval(() => {
|
|
143
|
-
if (this.running) {
|
|
144
|
-
this.sendHeartbeat();
|
|
145
|
-
}
|
|
146
|
-
}, 30000);
|
|
147
|
-
|
|
148
|
-
console.log(' Press Ctrl+C to stop.');
|
|
149
|
-
console.log('');
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Stop the worker
|
|
154
|
-
*/
|
|
155
|
-
async stop() {
|
|
156
|
-
this.running = false;
|
|
157
|
-
|
|
158
|
-
if (this.heartbeatInterval) {
|
|
159
|
-
clearInterval(this.heartbeatInterval);
|
|
160
|
-
this.heartbeatInterval = null;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// Notify server we're going offline
|
|
164
|
-
try {
|
|
165
|
-
await this.apiRequest('/api/worker/offline', 'POST', {
|
|
166
|
-
worker_token: this.token
|
|
167
|
-
});
|
|
168
|
-
console.log('');
|
|
169
|
-
console.log(' Worker stopped and marked offline.');
|
|
170
|
-
} catch (error) {
|
|
171
|
-
// Ignore errors during shutdown
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Send heartbeat to server with core-hours data
|
|
177
|
-
*/
|
|
178
|
-
async sendHeartbeat() {
|
|
179
|
-
try {
|
|
180
|
-
// Calculate hours online since start
|
|
181
|
-
const hoursOnline = (Date.now() - this.startTime) / 1000 / 60 / 60;
|
|
182
|
-
|
|
183
|
-
// Estimate core-hours (assuming 50% usage for now)
|
|
184
|
-
// In a real implementation, you'd measure actual CPU usage
|
|
185
|
-
const estimatedUsage = 50;
|
|
186
|
-
const sessionCoreHours = this.calculateCoreHours(estimatedUsage, hoursOnline);
|
|
187
|
-
|
|
188
|
-
const response = await this.apiRequest('/api/worker/heartbeat', 'POST', {
|
|
189
|
-
worker_token: this.token,
|
|
190
|
-
system_info: {
|
|
191
|
-
platform: os.platform(),
|
|
192
|
-
arch: os.arch(),
|
|
193
|
-
cores: this.cpuInfo.cores,
|
|
194
|
-
cpu_model: this.cpuInfo.model,
|
|
195
|
-
cpu_speed_ghz: this.cpuInfo.speedGHz,
|
|
196
|
-
speed_factor: this.cpuInfo.speedFactor,
|
|
197
|
-
total_memory_gb: this.cpuInfo.totalMemoryGB,
|
|
198
|
-
free_memory_gb: Math.round(os.freemem() / 1024 / 1024 / 1024 * 10) / 10,
|
|
199
|
-
uptime: os.uptime(),
|
|
200
|
-
worker_uptime_hours: Math.round(hoursOnline * 100) / 100,
|
|
201
|
-
session_core_hours: Math.round(sessionCoreHours * 100) / 100,
|
|
202
|
-
estimated_usage_percent: estimatedUsage
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
if (response.success) {
|
|
207
|
-
this.coreHoursToday = response.core_hours_today || sessionCoreHours;
|
|
208
|
-
this.currentTier = response.tier || this.determineTier(this.coreHoursToday);
|
|
209
|
-
|
|
210
|
-
const time = new Date().toLocaleTimeString();
|
|
211
|
-
const tierName = this.currentTier?.name || 'None';
|
|
212
|
-
const tierRequests = this.currentTier?.requests?.toLocaleString() || '0';
|
|
213
|
-
const nextTier = this.getNextTier();
|
|
214
|
-
|
|
215
|
-
console.log(` [${time}] ✓ Heartbeat OK`);
|
|
216
|
-
console.log(` ├─ Core-Hours Today: ${this.coreHoursToday.toFixed(1)}${nextTier ? ` / ${nextTier.coreHours} needed for ${nextTier.name}` : ''}`);
|
|
217
|
-
console.log(` ├─ Current Tier: ${tierName} (${tierRequests} req/day)`);
|
|
218
|
-
console.log(` ├─ Usage Today: ${(response.usage_today || 0).toLocaleString()} / ${tierRequests}`);
|
|
219
|
-
console.log(` └─ Online: ${this.formatUptime(hoursOnline)}`);
|
|
220
|
-
console.log('');
|
|
221
|
-
} else {
|
|
222
|
-
console.error(` [ERROR] Heartbeat failed: ${response.error}`);
|
|
223
|
-
}
|
|
224
|
-
} catch (error) {
|
|
225
|
-
console.error(` [ERROR] Heartbeat error: ${error.message}`);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Get the next tier to aim for
|
|
231
|
-
*/
|
|
232
|
-
getNextTier() {
|
|
233
|
-
const tierOrder = [TIERS.STARTER, TIERS.BASIC, TIERS.PRO, TIERS.BUSINESS, TIERS.ENTERPRISE];
|
|
234
|
-
|
|
235
|
-
for (const tier of tierOrder) {
|
|
236
|
-
if (this.cpuInfo.cores >= tier.minCores && this.coreHoursToday < tier.coreHours) {
|
|
237
|
-
return tier;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
return null; // Already at max tier or can't qualify
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Format uptime as human readable
|
|
246
|
-
*/
|
|
247
|
-
formatUptime(hours) {
|
|
248
|
-
if (hours < 1) {
|
|
249
|
-
return `${Math.round(hours * 60)}m`;
|
|
250
|
-
}
|
|
251
|
-
const h = Math.floor(hours);
|
|
252
|
-
const m = Math.round((hours - h) * 60);
|
|
253
|
-
return `${h}h ${m}m`;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* Get system information (legacy method for compatibility)
|
|
258
|
-
*/
|
|
259
|
-
getSystemInfo() {
|
|
260
|
-
return {
|
|
261
|
-
platform: os.platform(),
|
|
262
|
-
arch: os.arch(),
|
|
263
|
-
cpus: this.cpuInfo.cores,
|
|
264
|
-
cpu_model: this.cpuInfo.model,
|
|
265
|
-
cpu_speed_ghz: this.cpuInfo.speedGHz,
|
|
266
|
-
speed_factor: this.cpuInfo.speedFactor,
|
|
267
|
-
total_memory: this.cpuInfo.totalMemoryGB,
|
|
268
|
-
free_memory: Math.round(os.freemem() / 1024 / 1024 / 1024 * 10) / 10,
|
|
269
|
-
uptime: os.uptime()
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Make API request
|
|
275
|
-
*/
|
|
276
|
-
apiRequest(endpoint, method = 'GET', body = null) {
|
|
277
|
-
return new Promise((resolve, reject) => {
|
|
278
|
-
const url = new URL(`${this.apiUrl}${endpoint}`);
|
|
279
|
-
const isHttps = url.protocol === 'https:';
|
|
280
|
-
const lib = isHttps ? https : http;
|
|
281
|
-
|
|
282
|
-
const options = {
|
|
283
|
-
hostname: url.hostname,
|
|
284
|
-
port: url.port || (isHttps ? 443 : 80),
|
|
285
|
-
path: url.pathname,
|
|
286
|
-
method: method,
|
|
287
|
-
headers: {
|
|
288
|
-
'Content-Type': 'application/json',
|
|
289
|
-
'User-Agent': 'AvalwWorker/1.1.0'
|
|
290
|
-
}
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
const req = lib.request(options, (res) => {
|
|
294
|
-
let data = '';
|
|
295
|
-
res.on('data', chunk => data += chunk);
|
|
296
|
-
res.on('end', () => {
|
|
297
|
-
try {
|
|
298
|
-
resolve(JSON.parse(data));
|
|
299
|
-
} catch (e) {
|
|
300
|
-
resolve({ raw: data });
|
|
301
|
-
}
|
|
302
|
-
});
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
req.on('error', reject);
|
|
306
|
-
req.setTimeout(15000, () => {
|
|
307
|
-
req.destroy();
|
|
308
|
-
reject(new Error('Request timeout'));
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
if (body) {
|
|
312
|
-
req.write(JSON.stringify(body));
|
|
313
|
-
}
|
|
314
|
-
req.end();
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
module.exports = { AvalwWorker, TIERS };
|