mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-25 20:21:02 +00:00
feat: initialize helper client
This commit is contained in:
78
client/index.js
Normal file
78
client/index.js
Normal file
@@ -0,0 +1,78 @@
|
||||
import { createConnection } from 'node:net';
|
||||
import { serialize, deserialize } from 'bson';
|
||||
import EventEmitter from 'node:events';
|
||||
|
||||
class HelperClient extends EventEmitter {
|
||||
constructor({ server }) {
|
||||
super();
|
||||
if (!server.port) throw new Error('You did not specify the server port.');
|
||||
this.server = server;
|
||||
};
|
||||
|
||||
connect() {
|
||||
this.client = createConnection(this.server.port, this.server.host ? this.server.host : 'localhost', () => {
|
||||
this.emit('connect');
|
||||
});
|
||||
|
||||
this.client.on('data', (data) => {
|
||||
const eventData = deserialize(data);
|
||||
|
||||
switch (eventData.op) {
|
||||
case 2: {
|
||||
// The 'aiResponse' event.
|
||||
|
||||
this.emit('aiResponse', eventData);
|
||||
};
|
||||
|
||||
case 6: {
|
||||
// The 'ocrResponse' event.
|
||||
|
||||
this.emit('ocrResponse', eventData);
|
||||
};
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
sendData(data) {
|
||||
this.client.write(serialize(data));
|
||||
return;
|
||||
};
|
||||
|
||||
scanText(text, id) {
|
||||
this.sendData({
|
||||
op: 1,
|
||||
id,
|
||||
text
|
||||
});
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
scanImage(url, id) {
|
||||
this.sendData({
|
||||
op: 5,
|
||||
id,
|
||||
url
|
||||
});
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
sendTrainData(text, label) {
|
||||
this.sendData({
|
||||
op: 5,
|
||||
label,
|
||||
text
|
||||
});
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
trainAI() {
|
||||
this.sendData({ op: 4 });
|
||||
|
||||
return;
|
||||
};
|
||||
};
|
||||
|
||||
export default HelperClient;
|
||||
Reference in New Issue
Block a user