@avalw/search-worker 1.0.3 → 2.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.
- package/README.md +323 -184
- package/bin/cli.js +236 -28
- 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 -158
package/package.json
CHANGED
|
@@ -1,41 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@avalw/search-worker",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"description": "AVALW
|
|
5
|
-
"main": "
|
|
3
|
+
"version": "2.1.0",
|
|
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,158 +0,0 @@
|
|
|
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 };
|