2022-08-11 16:46:56 +00:00
|
|
|
import { request } from "undici";
|
2021-11-27 05:05:09 +00:00
|
|
|
import WebSocket from "ws";
|
|
|
|
import * as logger from "./logger.js";
|
|
|
|
import { setTimeout } from "timers/promises";
|
|
|
|
|
|
|
|
const Rerror = 0x01;
|
|
|
|
const Tqueue = 0x02;
|
2021-11-27 05:24:13 +00:00
|
|
|
const Rqueue = 0x03;
|
2021-11-27 05:05:09 +00:00
|
|
|
const Tcancel = 0x04;
|
2021-11-27 05:24:13 +00:00
|
|
|
const Rcancel = 0x05;
|
2021-11-27 05:05:09 +00:00
|
|
|
const Twait = 0x06;
|
2021-11-27 05:24:13 +00:00
|
|
|
const Rwait = 0x07;
|
2021-11-27 05:05:09 +00:00
|
|
|
const Rinit = 0x08;
|
|
|
|
|
|
|
|
class ImageConnection {
|
|
|
|
constructor(host, auth, tls = false) {
|
|
|
|
this.requests = new Map();
|
2022-03-13 19:42:27 +00:00
|
|
|
if (!host.includes(":")) {
|
2021-12-18 04:44:53 +00:00
|
|
|
host += ":3762";
|
|
|
|
}
|
2021-11-27 05:05:09 +00:00
|
|
|
this.host = host;
|
|
|
|
this.auth = auth;
|
2022-06-07 23:06:44 +00:00
|
|
|
this.tag = 0;
|
2021-11-27 05:05:09 +00:00
|
|
|
this.disconnected = false;
|
|
|
|
this.formats = {};
|
|
|
|
this.wsproto = null;
|
|
|
|
if (tls) {
|
|
|
|
this.wsproto = "wss";
|
|
|
|
} else {
|
|
|
|
this.wsproto = "ws";
|
|
|
|
}
|
2021-12-18 04:44:53 +00:00
|
|
|
this.sockurl = `${this.wsproto}://${host}/sock`;
|
2022-03-13 19:42:27 +00:00
|
|
|
const headers = {};
|
|
|
|
if (auth) {
|
2021-12-18 04:44:53 +00:00
|
|
|
headers.Authentication = auth;
|
|
|
|
}
|
2022-03-13 19:42:27 +00:00
|
|
|
this.conn = new WebSocket(this.sockurl, { headers });
|
2021-11-27 05:05:09 +00:00
|
|
|
let httpproto;
|
|
|
|
if (tls) {
|
|
|
|
httpproto = "https";
|
|
|
|
} else {
|
|
|
|
httpproto = "http";
|
|
|
|
}
|
2022-09-21 05:05:03 +00:00
|
|
|
this.httpurl = `${httpproto}://${host}`;
|
2021-11-27 05:05:09 +00:00
|
|
|
this.conn.on("message", (msg) => this.onMessage(msg));
|
|
|
|
this.conn.once("error", (err) => this.onError(err));
|
|
|
|
this.conn.once("close", () => this.onClose());
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:05:03 +00:00
|
|
|
async onMessage(msg) {
|
2021-11-27 05:05:09 +00:00
|
|
|
const op = msg.readUint8(0);
|
|
|
|
if (op === Rinit) {
|
2022-01-05 16:39:50 +00:00
|
|
|
this.formats = JSON.parse(msg.toString("utf8", 7));
|
2021-11-27 05:05:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-11-29 21:27:13 +00:00
|
|
|
const tag = msg.readUint16LE(1);
|
2021-11-27 05:05:09 +00:00
|
|
|
const promise = this.requests.get(tag);
|
2021-12-20 03:58:17 +00:00
|
|
|
if (!promise) {
|
|
|
|
logger.error(`Received response for unknown request ${tag}`);
|
|
|
|
return;
|
|
|
|
}
|
2021-11-27 05:05:09 +00:00
|
|
|
this.requests.delete(tag);
|
|
|
|
if (op === Rerror) {
|
2021-11-29 21:27:13 +00:00
|
|
|
promise.reject(new Error(msg.slice(3, msg.length).toString()));
|
2021-11-27 05:05:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
onError(e) {
|
|
|
|
logger.error(e.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
async onClose() {
|
2022-01-26 21:24:10 +00:00
|
|
|
for (const [tag, obj] of this.requests.entries()) {
|
|
|
|
obj.reject("Request ended prematurely due to a closed connection");
|
|
|
|
this.requests.delete(tag);
|
2021-11-27 05:05:09 +00:00
|
|
|
}
|
|
|
|
if (!this.disconnected) {
|
2021-12-18 04:44:53 +00:00
|
|
|
logger.warn(`Lost connection to ${this.host}, attempting to reconnect in 5 seconds...`);
|
2021-11-27 05:05:09 +00:00
|
|
|
await setTimeout(5000);
|
|
|
|
this.conn = new WebSocket(this.sockurl, {
|
|
|
|
headers: {
|
|
|
|
"Authentication": this.auth
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.conn.on("message", (msg) => this.onMessage(msg));
|
|
|
|
this.conn.once("error", (err) => this.onError(err));
|
|
|
|
this.conn.once("close", () => this.onClose());
|
|
|
|
}
|
|
|
|
this.disconnected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
this.disconnected = true;
|
|
|
|
this.conn.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
queue(jobid, jobobj) {
|
|
|
|
const str = JSON.stringify(jobobj);
|
2022-09-21 05:05:03 +00:00
|
|
|
const buf = Buffer.alloc(8);
|
|
|
|
buf.writeBigUint64LE(jobid);
|
2022-01-26 21:24:10 +00:00
|
|
|
return this.do(Tqueue, jobid, Buffer.concat([buf, Buffer.from(str)]));
|
2021-11-27 05:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wait(jobid) {
|
2022-09-21 05:05:03 +00:00
|
|
|
const buf = Buffer.alloc(8);
|
|
|
|
buf.writeBigUint64LE(jobid);
|
2022-01-26 21:24:10 +00:00
|
|
|
return this.do(Twait, jobid, buf);
|
2021-11-27 05:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel(jobid) {
|
2022-09-21 05:05:03 +00:00
|
|
|
const buf = Buffer.alloc(8);
|
|
|
|
buf.writeBigUint64LE(jobid);
|
2022-01-26 21:24:10 +00:00
|
|
|
return this.do(Tcancel, jobid, buf);
|
2021-11-27 05:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async getOutput(jobid) {
|
2022-09-21 05:05:03 +00:00
|
|
|
const req = await request(`${this.httpurl}/image?id=${jobid}`, {
|
2021-11-27 05:05:09 +00:00
|
|
|
headers: {
|
2022-08-11 16:46:56 +00:00
|
|
|
authentication: this.auth || undefined
|
2021-11-27 05:05:09 +00:00
|
|
|
}
|
|
|
|
});
|
2022-08-11 16:46:56 +00:00
|
|
|
const contentType = req.headers["content-type"];
|
2021-11-27 05:05:09 +00:00
|
|
|
let type;
|
|
|
|
switch (contentType) {
|
|
|
|
case "image/gif":
|
|
|
|
type = "gif";
|
|
|
|
break;
|
|
|
|
case "image/png":
|
|
|
|
type = "png";
|
|
|
|
break;
|
|
|
|
case "image/jpeg":
|
|
|
|
type = "jpg";
|
|
|
|
break;
|
|
|
|
case "image/webp":
|
|
|
|
type = "webp";
|
|
|
|
break;
|
2022-02-17 14:46:02 +00:00
|
|
|
default:
|
|
|
|
type = contentType;
|
|
|
|
break;
|
2021-11-27 05:05:09 +00:00
|
|
|
}
|
2022-09-23 04:44:54 +00:00
|
|
|
return { buffer: Buffer.from(await req.body.arrayBuffer()), type };
|
2022-09-21 05:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async getCount() {
|
|
|
|
const req = await request(`${this.httpurl}/count`, {
|
|
|
|
headers: {
|
|
|
|
authentication: this.auth || undefined
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (req.statusCode !== 200) return;
|
|
|
|
const res = parseInt(await req.body.text());
|
|
|
|
return res;
|
2021-11-27 05:05:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 21:24:10 +00:00
|
|
|
async do(op, id, data) {
|
2021-11-29 21:27:13 +00:00
|
|
|
const buf = Buffer.alloc(1 + 2);
|
2021-12-06 07:25:38 +00:00
|
|
|
let tag = this.tag++;
|
|
|
|
if (tag > 65535) tag = this.tag = 0;
|
2021-11-27 05:05:09 +00:00
|
|
|
buf.writeUint8(op);
|
2021-11-29 21:27:13 +00:00
|
|
|
buf.writeUint16LE(tag, 1);
|
2021-11-27 05:05:09 +00:00
|
|
|
this.conn.send(Buffer.concat([buf, data]));
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
2022-01-26 21:24:10 +00:00
|
|
|
this.requests.set(tag, { resolve, reject, id, op });
|
2021-11-27 05:05:09 +00:00
|
|
|
});
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-18 04:44:53 +00:00
|
|
|
export default ImageConnection;
|