add NPROC for communication with updater

This commit is contained in:
Pakin 2025-09-02 17:47:30 +07:00
parent 6ccc186e97
commit 9df9d1b75f
9 changed files with 220 additions and 6 deletions

View file

@ -293,7 +293,8 @@ function saveJsonToFile(filename, content) {
// GOOGLE
// ======================================================================
const { google, sheets_v4 } = require("googleapis");
const { google, sheets_v4, drive_v3 } = require("googleapis");
const { GoogleAuth } = require("google-auth-library");
function authorize() {
const oauthClient = new google.auth.GoogleAuth({
@ -301,7 +302,7 @@ function authorize() {
client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
private_key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/gm, "\n"),
},
scopes: ["https://www.googleapis.com/auth/spreadsheets"],
scopes: ["https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive"],
});
return oauthClient;
}
@ -325,6 +326,7 @@ function getCountrySpreadSheetId(cnt) {
const GoogleFunctions = {
auth: authorize,
SpreadSheets: (auth) => google.sheets({ version: "v4", auth }),
Drive: (auth) => google.drive({ version: 'v3', auth }),
GetCountrySpreadSheet: getCountrySpreadSheetById,
getAllSheetNamesByCountry: getAllSheetNamesByCountry,
getCountrySheetByName: getCountrySheetByName,
@ -1282,6 +1284,40 @@ async function _finalizeSyncProfilePrice(
}
}
// ======================================================================
// DRIVE
// ======================================================================
/**
* @param {drive_v3.Drive} drive instance
*/
async function test_drive(drive){
try {
await drive.files.create({
requestBody: {
name: "test",
mimeType: "text/plain",
parents: [process.env.TAOBIN_ADMIN_SERVER_DRIVE_FOLDER]
},
media: {
mimeType: "text/plain",
body: "Hello from js"
}
}).then((x) => Log.info(`Created file response: ${JSON.parse(x)}`));
} catch(error){
Log.err(`Error test drive: ${error}`);
return JSON.stringify({
error: error
});
}
return JSON.stringify({
status: "success"
});
}
// special
// ======================================================================
@ -1406,4 +1442,5 @@ module.exports = {
getCountrySheetByName,
diff2DArraysCustom,
saveJsonToFile,
test_drive
};

85
lib/nproc.js Normal file
View file

@ -0,0 +1,85 @@
const net = require('net');
// TODO: must change to read by env
const API_KEY = Buffer.from('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
function frame(msgType, topicStr, bodyBuf) {
const topic = Buffer.from(topicStr);
const payloadLen = 1 + 2 + 4 + topic.length + bodyBuf.length;
const totalBytes = payloadLen + 32;
const buf = Buffer.alloc(4 + payloadLen + 32);
buf.writeUInt32BE(totalBytes, 0);
buf.writeUInt8(msgType, 4);
buf.writeUInt16BE(topic.length, 5);
buf.writeUInt32BE(bodyBuf.length, 7);
topic.copy(buf, 11);
bodyBuf.copy(buf, 11 + topic.length);
API_KEY.copy(buf, 4 + payloadLen);
return buf;
}
class NprocClient {
MESSAGE_TYPE = {
SUB: 1,
UNSUB: 2,
PUB: 3,
PING: 4
};
constructor(
addr,
onConnect,
onClose
){
let _addr = addr.toString().split(":");
let host = _addr[0];
let port = parseInt(_addr[1]);
this.client = net.createConnection({
host: host,
port: port
}, () => onConnect());
this.history = [];
this.acc = Buffer.alloc(0);
this.client.on('data', chunk => {
this.acc = Buffer.concat([this.acc, chunk]);
while(this.acc.length >= 4){
const total = this.acc.readUInt32BE(0);
if (this.acc.length < 4 + total) break;
const frm = this.acc.subarray(0, 4 + total);
this.acc = this.acc.subarray(4 + total);
const msgType = frm.readUInt8(4);
const topicLen = frm.readUInt16BE(5);
const bodyLen = frm.readUInt32BE(7);
const topic = frm.subarray(11, 11 + topicLen).toString();
const body = frm.subarray(11 + topicLen, 11 + topicLen + bodyLen);
let res = {
msgType, topic, body: body.toString()
};
console.log(`get msg! ${JSON.stringify(res)}`);
this.history.push(res);
}
});
this.client.on('close', () => onClose());
}
subscribe(topic){
this.client.write(frame(this.MESSAGE_TYPE.SUB, topic, Buffer.alloc(0)));
}
publish(topic, payload){
this.client.write(frame(this.MESSAGE_TYPE.PUB, topic, Buffer.from(JSON.stringify(payload))));
}
// TODO: unsub
// TODO: ping
}
module.exports = {
NprocClient
};

0
lib/package_manager.js Normal file
View file

28
lib/zmq.js Normal file
View file

@ -0,0 +1,28 @@
const zmq = require('zeromq');
const uuid = require('uuid');
/**
* Publish simple message to server to notify service is ready.
*/
async function startup(){
const pub = new zmq.Publisher();
await pub.connect("tcp://127.0.0.1:36541");
const msg = {
id: uuid.v4(),
topic: "news",
message_type: "Data",
data: { content: "GGS Ready!" },
timestamp: Math.floor(Date.now() / 1000)
};
await pub.send(["news", JSON.stringify(msg)]);
console.log("startup send!")
pub.close();
}
module.exports = {
startup
};