@avalw/search-worker 1.0.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.
- package/LICENSE +21 -0
- package/README.md +259 -0
- package/bin/cli.js +47 -0
- package/examples/basic-usage.js +32 -0
- package/examples/express-integration.js +73 -0
- package/examples/search-api.js +58 -0
- package/lib/worker.js +158 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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/README.md
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
# @avalw/search-worker
|
|
2
|
+
|
|
3
|
+
Free privacy-focused search API in exchange for compute power.
|
|
4
|
+
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
[](https://nodejs.org)
|
|
7
|
+
|
|
8
|
+
## What is AVALW Worker?
|
|
9
|
+
|
|
10
|
+
AVALW Worker is a lightweight Node.js application that runs on your server and contributes a small amount of CPU power to the AVALW distributed network. In return, you get **free, unlimited access** to the AVALW Search API - a privacy-focused search engine that doesn't track users.
|
|
11
|
+
|
|
12
|
+
**No tracking. No ads. No data collection. Just search.**
|
|
13
|
+
|
|
14
|
+
## How It Works
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
|
|
18
|
+
│ Your Server │ │ AVALW Network │ │ Your Users │
|
|
19
|
+
│ │ │ │ │ │
|
|
20
|
+
│ ┌───────────┐ │ │ │ │ │
|
|
21
|
+
│ │ Worker │──┼─────►│ Contributes │ │ │
|
|
22
|
+
│ │ Process │ │ │ compute power │ │ │
|
|
23
|
+
│ └───────────┘ │ │ │ │ │
|
|
24
|
+
│ │ │ │ │ │
|
|
25
|
+
│ ┌───────────┐ │ │ │ │ ┌───────────┐ │
|
|
26
|
+
│ │ Your App │◄─┼──────┤ Search API │◄─────┼──│ Search │ │
|
|
27
|
+
│ │ │ │ │ (free access) │ │ │ Queries │ │
|
|
28
|
+
│ └───────────┘ │ │ │ │ └───────────┘ │
|
|
29
|
+
└─────────────────┘ └─────────────────┘ └─────────────────┘
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Tiers
|
|
33
|
+
|
|
34
|
+
Choose the tier that fits your needs:
|
|
35
|
+
|
|
36
|
+
| Tier | Requests/Day | CPU Contribution |
|
|
37
|
+
|------------|--------------|------------------|
|
|
38
|
+
| Starter | 1,000 | 2% |
|
|
39
|
+
| Basic | 5,000 | 5% |
|
|
40
|
+
| Pro | 20,000 | 10% |
|
|
41
|
+
| Business | 50,000 | 20% |
|
|
42
|
+
| Enterprise | 500,000 | 50% |
|
|
43
|
+
|
|
44
|
+
## Quick Start
|
|
45
|
+
|
|
46
|
+
### 1. Get Your Token
|
|
47
|
+
|
|
48
|
+
Visit [worker.avalw.org](https://worker.avalw.org) and create a free account. You'll receive:
|
|
49
|
+
- **Account Hash** - Your unique identifier (save this!)
|
|
50
|
+
- **Worker Token** - Used to run the worker
|
|
51
|
+
|
|
52
|
+
### 2. Install the Worker
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# Install globally via npm
|
|
56
|
+
npm install -g @avalw/search-worker
|
|
57
|
+
|
|
58
|
+
# Or install from GitHub
|
|
59
|
+
npm install -g github:avalw/search-worker
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 3. Run the Worker
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
avalw-worker YOUR_WORKER_TOKEN
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
You should see:
|
|
69
|
+
```
|
|
70
|
+
╔═══════════════════════════════════════════════╗
|
|
71
|
+
║ AVALW Search Worker v1.0.0 ║
|
|
72
|
+
╠═══════════════════════════════════════════════╣
|
|
73
|
+
║ Free Search API in exchange for compute ║
|
|
74
|
+
╚═══════════════════════════════════════════════╝
|
|
75
|
+
|
|
76
|
+
API URL: https://worker.avalw.org
|
|
77
|
+
Token: abc123def456...
|
|
78
|
+
|
|
79
|
+
[10:30:45] Heartbeat OK | Tier: Starter | CPU Limit: 2%
|
|
80
|
+
Worker started. Press Ctrl+C to stop.
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 4. Use the Search API
|
|
84
|
+
|
|
85
|
+
Once your worker is running, make API requests from your whitelisted domains:
|
|
86
|
+
|
|
87
|
+
```javascript
|
|
88
|
+
// Node.js / JavaScript
|
|
89
|
+
const response = await fetch(
|
|
90
|
+
'https://worker.avalw.org/api/search?q=your+query&token=YOUR_TOKEN'
|
|
91
|
+
);
|
|
92
|
+
const results = await response.json();
|
|
93
|
+
console.log(results);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
# Python
|
|
98
|
+
import requests
|
|
99
|
+
|
|
100
|
+
response = requests.get(
|
|
101
|
+
'https://worker.avalw.org/api/search',
|
|
102
|
+
params={'q': 'your query', 'token': 'YOUR_TOKEN'}
|
|
103
|
+
)
|
|
104
|
+
results = response.json()
|
|
105
|
+
print(results)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
# cURL
|
|
110
|
+
curl "https://worker.avalw.org/api/search?q=your+query&token=YOUR_TOKEN"
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Running as a Service
|
|
114
|
+
|
|
115
|
+
### Using PM2 (Recommended)
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
# Install PM2
|
|
119
|
+
npm install -g pm2
|
|
120
|
+
|
|
121
|
+
# Start worker
|
|
122
|
+
pm2 start avalw-worker -- YOUR_WORKER_TOKEN
|
|
123
|
+
|
|
124
|
+
# Save for auto-restart
|
|
125
|
+
pm2 save
|
|
126
|
+
pm2 startup
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Using systemd (Linux)
|
|
130
|
+
|
|
131
|
+
Create `/etc/systemd/system/avalw-worker.service`:
|
|
132
|
+
|
|
133
|
+
```ini
|
|
134
|
+
[Unit]
|
|
135
|
+
Description=AVALW Search Worker
|
|
136
|
+
After=network.target
|
|
137
|
+
|
|
138
|
+
[Service]
|
|
139
|
+
Type=simple
|
|
140
|
+
User=your-user
|
|
141
|
+
ExecStart=/usr/bin/avalw-worker YOUR_WORKER_TOKEN
|
|
142
|
+
Restart=always
|
|
143
|
+
RestartSec=10
|
|
144
|
+
|
|
145
|
+
[Install]
|
|
146
|
+
WantedBy=multi-user.target
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Then:
|
|
150
|
+
```bash
|
|
151
|
+
sudo systemctl enable avalw-worker
|
|
152
|
+
sudo systemctl start avalw-worker
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Using Docker
|
|
156
|
+
|
|
157
|
+
```dockerfile
|
|
158
|
+
FROM node:20-alpine
|
|
159
|
+
RUN npm install -g @avalw/search-worker
|
|
160
|
+
CMD ["avalw-worker", "YOUR_WORKER_TOKEN"]
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
docker build -t avalw-worker .
|
|
165
|
+
docker run -d --name avalw-worker avalw-worker
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Programmatic Usage
|
|
169
|
+
|
|
170
|
+
You can also use the worker programmatically in your Node.js application:
|
|
171
|
+
|
|
172
|
+
```javascript
|
|
173
|
+
const { AvalwWorker } = require('@avalw/search-worker');
|
|
174
|
+
|
|
175
|
+
const worker = new AvalwWorker('YOUR_WORKER_TOKEN');
|
|
176
|
+
|
|
177
|
+
// Start the worker
|
|
178
|
+
await worker.start();
|
|
179
|
+
|
|
180
|
+
// Get system info
|
|
181
|
+
const info = worker.getSystemInfo();
|
|
182
|
+
console.log(info);
|
|
183
|
+
|
|
184
|
+
// Stop the worker
|
|
185
|
+
await worker.stop();
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## API Reference
|
|
189
|
+
|
|
190
|
+
### Search Endpoint
|
|
191
|
+
|
|
192
|
+
```
|
|
193
|
+
GET https://worker.avalw.org/api/search
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
**Parameters:**
|
|
197
|
+
| Parameter | Type | Required | Description |
|
|
198
|
+
|-----------|--------|----------|--------------------------------|
|
|
199
|
+
| q | string | Yes | Search query |
|
|
200
|
+
| token | string | Yes | Your worker token |
|
|
201
|
+
| page | number | No | Page number (default: 1) |
|
|
202
|
+
| limit | number | No | Results per page (default: 10) |
|
|
203
|
+
|
|
204
|
+
**Response:**
|
|
205
|
+
```json
|
|
206
|
+
{
|
|
207
|
+
"success": true,
|
|
208
|
+
"results": [
|
|
209
|
+
{
|
|
210
|
+
"title": "Example Result",
|
|
211
|
+
"url": "https://example.com",
|
|
212
|
+
"snippet": "This is a snippet..."
|
|
213
|
+
}
|
|
214
|
+
],
|
|
215
|
+
"total": 100,
|
|
216
|
+
"page": 1
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Domain Whitelisting
|
|
221
|
+
|
|
222
|
+
For security, API requests only work from whitelisted domains. Manage your domains at [worker.avalw.org](https://worker.avalw.org) dashboard.
|
|
223
|
+
|
|
224
|
+
## Troubleshooting
|
|
225
|
+
|
|
226
|
+
### Worker won't connect
|
|
227
|
+
- Check your internet connection
|
|
228
|
+
- Verify your token is correct
|
|
229
|
+
- Ensure port 443 (HTTPS) is not blocked
|
|
230
|
+
|
|
231
|
+
### API returns "Worker offline"
|
|
232
|
+
- Make sure your worker is running
|
|
233
|
+
- Check the worker console for errors
|
|
234
|
+
- Worker must send heartbeat every 30 seconds
|
|
235
|
+
|
|
236
|
+
### Rate limit exceeded
|
|
237
|
+
- Upgrade to a higher tier for more requests
|
|
238
|
+
- Wait until the daily limit resets (midnight UTC)
|
|
239
|
+
|
|
240
|
+
## Security
|
|
241
|
+
|
|
242
|
+
- **No data collection**: We don't log search queries
|
|
243
|
+
- **No tracking**: No cookies, no fingerprinting
|
|
244
|
+
- **Open protocol**: Transparent heartbeat mechanism
|
|
245
|
+
- **Your server, your control**: Worker runs on your infrastructure
|
|
246
|
+
|
|
247
|
+
## Support
|
|
248
|
+
|
|
249
|
+
- Website: [worker.avalw.org](https://worker.avalw.org)
|
|
250
|
+
- Issues: [GitHub Issues](https://github.com/avalw/search-worker/issues)
|
|
251
|
+
- Email: contact@avalw.com
|
|
252
|
+
|
|
253
|
+
## License
|
|
254
|
+
|
|
255
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
Made with privacy in mind by [AVALW](https://avalw.org)
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { AvalwWorker } = require('../lib/worker');
|
|
4
|
+
|
|
5
|
+
const token = process.argv[2];
|
|
6
|
+
const apiUrl = process.argv[3] || 'https://worker.avalw.org';
|
|
7
|
+
|
|
8
|
+
if (!token) {
|
|
9
|
+
console.log('');
|
|
10
|
+
console.log(' AVALW Search Worker');
|
|
11
|
+
console.log(' ====================');
|
|
12
|
+
console.log('');
|
|
13
|
+
console.log(' Usage: avalw-worker <WORKER_TOKEN> [API_URL]');
|
|
14
|
+
console.log('');
|
|
15
|
+
console.log(' Get your worker token at: https://worker.avalw.org');
|
|
16
|
+
console.log('');
|
|
17
|
+
console.log(' Example:');
|
|
18
|
+
console.log(' avalw-worker abc123def456...');
|
|
19
|
+
console.log('');
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
console.log('');
|
|
24
|
+
console.log(' ╔═══════════════════════════════════════════════╗');
|
|
25
|
+
console.log(' ║ AVALW Search Worker v1.0.0 ║');
|
|
26
|
+
console.log(' ╠═══════════════════════════════════════════════╣');
|
|
27
|
+
console.log(' ║ Free Search API in exchange for compute ║');
|
|
28
|
+
console.log(' ╚═══════════════════════════════════════════════╝');
|
|
29
|
+
console.log('');
|
|
30
|
+
|
|
31
|
+
const worker = new AvalwWorker(token, apiUrl);
|
|
32
|
+
|
|
33
|
+
// Handle graceful shutdown
|
|
34
|
+
process.on('SIGINT', () => {
|
|
35
|
+
console.log('\n Shutting down worker...');
|
|
36
|
+
worker.stop();
|
|
37
|
+
process.exit(0);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
process.on('SIGTERM', () => {
|
|
41
|
+
console.log('\n Shutting down worker...');
|
|
42
|
+
worker.stop();
|
|
43
|
+
process.exit(0);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Start worker
|
|
47
|
+
worker.start();
|
|
@@ -0,0 +1,32 @@
|
|
|
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);
|
|
@@ -0,0 +1,73 @@
|
|
|
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);
|
|
@@ -0,0 +1,58 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AVALW Search Worker
|
|
3
|
+
* Connects to worker.avalw.org and contributes compute power
|
|
4
|
+
* in exchange for free search API access.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const https = require('https');
|
|
8
|
+
const http = require('http');
|
|
9
|
+
const os = require('os');
|
|
10
|
+
|
|
11
|
+
class AvalwWorker {
|
|
12
|
+
constructor(token, apiUrl = 'https://worker.avalw.org') {
|
|
13
|
+
this.token = token;
|
|
14
|
+
this.apiUrl = apiUrl;
|
|
15
|
+
this.running = false;
|
|
16
|
+
this.heartbeatInterval = null;
|
|
17
|
+
this.cpuLimit = 10; // Default 10%, updated by server
|
|
18
|
+
this.tier = 'Unknown';
|
|
19
|
+
this.taskCount = 0;
|
|
20
|
+
this.lastHeartbeat = null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Start the worker
|
|
25
|
+
*/
|
|
26
|
+
async start() {
|
|
27
|
+
this.running = true;
|
|
28
|
+
console.log(` API URL: ${this.apiUrl}`);
|
|
29
|
+
console.log(` Token: ${this.token.substring(0, 12)}...`);
|
|
30
|
+
console.log('');
|
|
31
|
+
|
|
32
|
+
// Initial heartbeat
|
|
33
|
+
await this.sendHeartbeat();
|
|
34
|
+
|
|
35
|
+
// Start heartbeat loop
|
|
36
|
+
this.heartbeatInterval = setInterval(() => {
|
|
37
|
+
if (this.running) {
|
|
38
|
+
this.sendHeartbeat();
|
|
39
|
+
}
|
|
40
|
+
}, 30000); // Every 30 seconds
|
|
41
|
+
|
|
42
|
+
console.log(' Worker started. Press Ctrl+C to stop.');
|
|
43
|
+
console.log('');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Stop the worker
|
|
48
|
+
*/
|
|
49
|
+
async stop() {
|
|
50
|
+
this.running = false;
|
|
51
|
+
|
|
52
|
+
if (this.heartbeatInterval) {
|
|
53
|
+
clearInterval(this.heartbeatInterval);
|
|
54
|
+
this.heartbeatInterval = null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Notify server we're going offline
|
|
58
|
+
try {
|
|
59
|
+
await this.apiRequest('/api/worker/offline', 'POST', {
|
|
60
|
+
worker_token: this.token
|
|
61
|
+
});
|
|
62
|
+
console.log(' Worker marked offline.');
|
|
63
|
+
} catch (error) {
|
|
64
|
+
// Ignore errors during shutdown
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Send heartbeat to server
|
|
70
|
+
*/
|
|
71
|
+
async sendHeartbeat() {
|
|
72
|
+
try {
|
|
73
|
+
const response = await this.apiRequest('/api/worker/heartbeat', 'POST', {
|
|
74
|
+
worker_token: this.token,
|
|
75
|
+
system_info: this.getSystemInfo()
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if (response.success) {
|
|
79
|
+
this.cpuLimit = response.cpu_limit || this.cpuLimit;
|
|
80
|
+
this.tier = response.tier || this.tier;
|
|
81
|
+
this.lastHeartbeat = new Date();
|
|
82
|
+
|
|
83
|
+
const time = this.lastHeartbeat.toLocaleTimeString();
|
|
84
|
+
console.log(` [${time}] Heartbeat OK | Tier: ${this.tier} | CPU Limit: ${this.cpuLimit}%`);
|
|
85
|
+
} else {
|
|
86
|
+
console.error(` [ERROR] Heartbeat failed: ${response.error}`);
|
|
87
|
+
}
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.error(` [ERROR] Heartbeat error: ${error.message}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Get system information
|
|
95
|
+
*/
|
|
96
|
+
getSystemInfo() {
|
|
97
|
+
const cpus = os.cpus();
|
|
98
|
+
const totalMem = os.totalmem();
|
|
99
|
+
const freeMem = os.freemem();
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
platform: os.platform(),
|
|
103
|
+
arch: os.arch(),
|
|
104
|
+
cpus: cpus.length,
|
|
105
|
+
cpu_model: cpus[0]?.model || 'Unknown',
|
|
106
|
+
total_memory: Math.round(totalMem / 1024 / 1024 / 1024 * 10) / 10, // GB
|
|
107
|
+
free_memory: Math.round(freeMem / 1024 / 1024 / 1024 * 10) / 10, // GB
|
|
108
|
+
uptime: os.uptime()
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Make API request
|
|
114
|
+
*/
|
|
115
|
+
apiRequest(endpoint, method = 'GET', body = null) {
|
|
116
|
+
return new Promise((resolve, reject) => {
|
|
117
|
+
const url = new URL(`${this.apiUrl}${endpoint}`);
|
|
118
|
+
const isHttps = url.protocol === 'https:';
|
|
119
|
+
const lib = isHttps ? https : http;
|
|
120
|
+
|
|
121
|
+
const options = {
|
|
122
|
+
hostname: url.hostname,
|
|
123
|
+
port: url.port || (isHttps ? 443 : 80),
|
|
124
|
+
path: url.pathname,
|
|
125
|
+
method: method,
|
|
126
|
+
headers: {
|
|
127
|
+
'Content-Type': 'application/json',
|
|
128
|
+
'User-Agent': 'AvalwWorker/1.0.0'
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const req = lib.request(options, (res) => {
|
|
133
|
+
let data = '';
|
|
134
|
+
res.on('data', chunk => data += chunk);
|
|
135
|
+
res.on('end', () => {
|
|
136
|
+
try {
|
|
137
|
+
resolve(JSON.parse(data));
|
|
138
|
+
} catch (e) {
|
|
139
|
+
resolve({ raw: data });
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
req.on('error', reject);
|
|
145
|
+
req.setTimeout(15000, () => {
|
|
146
|
+
req.destroy();
|
|
147
|
+
reject(new Error('Request timeout'));
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
if (body) {
|
|
151
|
+
req.write(JSON.stringify(body));
|
|
152
|
+
}
|
|
153
|
+
req.end();
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
module.exports = { AvalwWorker };
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@avalw/search-worker",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "AVALW Search Worker - Contribute compute power, get free privacy-focused search API access",
|
|
5
|
+
"main": "lib/worker.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"avalw-worker": "./bin/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "node bin/cli.js",
|
|
11
|
+
"test": "node test/test.js"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"avalw",
|
|
15
|
+
"search",
|
|
16
|
+
"worker",
|
|
17
|
+
"distributed",
|
|
18
|
+
"api",
|
|
19
|
+
"privacy",
|
|
20
|
+
"search-engine",
|
|
21
|
+
"decentralized"
|
|
22
|
+
],
|
|
23
|
+
"author": "AVALW <contact@avalw.com>",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/avalw/search-worker.git"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/avalw/search-worker/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://worker.avalw.org",
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=14.0.0"
|
|
35
|
+
},
|
|
36
|
+
"os": [
|
|
37
|
+
"darwin",
|
|
38
|
+
"linux",
|
|
39
|
+
"win32"
|
|
40
|
+
],
|
|
41
|
+
"files": [
|
|
42
|
+
"bin/",
|
|
43
|
+
"lib/",
|
|
44
|
+
"examples/",
|
|
45
|
+
"README.md",
|
|
46
|
+
"LICENSE"
|
|
47
|
+
]
|
|
48
|
+
}
|