Migrate to undici, try new method for getting image size/type/data
This commit is contained in:
parent
34ac7b3380
commit
da6f95aad8
19 changed files with 113 additions and 140 deletions
|
@ -1,6 +1,6 @@
|
|||
import { createRequire } from "module";
|
||||
import { isMainThread, parentPort, workerData } from "worker_threads";
|
||||
import fetch from "node-fetch";
|
||||
import { request } from "undici";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
|
||||
|
@ -31,7 +31,7 @@ export default function run(object) {
|
|||
buffer: Buffer.alloc(0),
|
||||
fileExtension: "nogif"
|
||||
});
|
||||
promise = fetch(object.path).then(res => res.arrayBuffer()).then(buf => Buffer.from(buf));
|
||||
promise = request(object.path).then(res => res.body.arrayBuffer()).then(buf => Buffer.from(buf));
|
||||
}
|
||||
// Convert from a MIME type (e.g. "image/png") to something ImageMagick understands (e.g. "png").
|
||||
// Don't set `type` directly on the object we are passed as it will be read afterwards.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import fetch from "node-fetch";
|
||||
import { request } from "undici";
|
||||
import fs from "fs";
|
||||
import { fileTypeFromBuffer, fileTypeFromFile } from "file-type";
|
||||
|
||||
|
@ -19,26 +19,40 @@ export async function getType(image, extraReturnTypes) {
|
|||
return undefined;
|
||||
}
|
||||
let type;
|
||||
const controller = new AbortController(); // eslint-disable-line no-undef
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => {
|
||||
controller.abort();
|
||||
}, 25000);
|
||||
}, 3000);
|
||||
try {
|
||||
const imageRequest = await fetch(image, {
|
||||
signal: controller.signal, headers: {
|
||||
"Range": "bytes=0-1023"
|
||||
}
|
||||
const imageRequest = await request(image, {
|
||||
signal: controller.signal,
|
||||
method: "HEAD"
|
||||
});
|
||||
clearTimeout(timeout);
|
||||
const size = imageRequest.headers.has("Content-Range") ? imageRequest.headers.get("Content-Range").split("/")[1] : imageRequest.headers.get("Content-Length");
|
||||
const size = imageRequest.headers["content-range"] ? imageRequest.headers["content-range"].split("/")[1] : imageRequest.headers["content-length"];
|
||||
if (parseInt(size) > 26214400 && extraReturnTypes) { // 25 MB
|
||||
type = "large";
|
||||
return type;
|
||||
}
|
||||
const imageBuffer = await imageRequest.arrayBuffer();
|
||||
const imageType = await fileTypeFromBuffer(imageBuffer);
|
||||
if (imageType && formats.includes(imageType.mime)) {
|
||||
type = imageType.mime;
|
||||
const typeHeader = imageRequest.headers["content-type"];
|
||||
if (typeHeader) {
|
||||
type = typeHeader;
|
||||
} else {
|
||||
const timeout = setTimeout(() => {
|
||||
controller.abort();
|
||||
}, 3000);
|
||||
const bufRequest = await request(image, {
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
range: "bytes=0-1023"
|
||||
}
|
||||
});
|
||||
clearTimeout(timeout);
|
||||
const imageBuffer = await bufRequest.body.arrayBuffer();
|
||||
const imageType = await fileTypeFromBuffer(imageBuffer);
|
||||
if (imageType && formats.includes(imageType.mime)) {
|
||||
type = imageType.mime;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.name === "AbortError") {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import fetch from "node-fetch";
|
||||
import { request } from "undici";
|
||||
import WebSocket from "ws";
|
||||
import * as logger from "./logger.js";
|
||||
import { setTimeout } from "timers/promises";
|
||||
|
@ -125,12 +125,12 @@ class ImageConnection {
|
|||
}
|
||||
|
||||
async getOutput(jobid) {
|
||||
const req = await fetch(`${this.httpurl}?id=${jobid}`, {
|
||||
const req = await request(`${this.httpurl}?id=${jobid}`, {
|
||||
headers: {
|
||||
"Authentication": this.auth || undefined
|
||||
authentication: this.auth || undefined
|
||||
}
|
||||
});
|
||||
const contentType = req.headers.get("Content-Type");
|
||||
const contentType = req.headers["content-type"];
|
||||
let type;
|
||||
switch (contentType) {
|
||||
case "image/gif":
|
||||
|
@ -149,7 +149,7 @@ class ImageConnection {
|
|||
type = contentType;
|
||||
break;
|
||||
}
|
||||
return { buffer: Buffer.from(await req.arrayBuffer()), type };
|
||||
return { buffer: Buffer.from(await req.body.arrayBuffer()), type };
|
||||
}
|
||||
|
||||
async do(op, id, data) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import fetch from "node-fetch";
|
||||
import { request } from "undici";
|
||||
import { getType } from "./image.js";
|
||||
|
||||
const tenorURLs = [
|
||||
|
@ -52,8 +52,8 @@ const getImage = async (image, image2, video, extraReturnTypes, gifv = false, ty
|
|||
// Tenor doesn't let us access a raw GIF without going through their API,
|
||||
// so we use that if there's a key in the config
|
||||
if (process.env.TENOR !== "") {
|
||||
const data = await fetch(`https://tenor.googleapis.com/v2/posts?ids=${image2.split("-").pop()}&media_filter=gif&limit=1&key=${process.env.TENOR}`);
|
||||
if (data.status === 429) {
|
||||
const data = await request(`https://tenor.googleapis.com/v2/posts?ids=${image2.split("-").pop()}&media_filter=gif&limit=1&key=${process.env.TENOR}`);
|
||||
if (data.statusCode === 429) {
|
||||
if (extraReturnTypes) {
|
||||
payload.type = "tenorlimit";
|
||||
return payload;
|
||||
|
@ -61,7 +61,7 @@ const getImage = async (image, image2, video, extraReturnTypes, gifv = false, ty
|
|||
return;
|
||||
}
|
||||
}
|
||||
const json = await data.json();
|
||||
const json = await data.body.json();
|
||||
if (json.error) throw Error(json.error);
|
||||
payload.path = json.results[0].media_formats.gif.url;
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import { fileURLToPath } from "url";
|
|||
import { Worker } from "worker_threads";
|
||||
import { createRequire } from "module";
|
||||
import { createServer } from "http";
|
||||
import fetch from "node-fetch";
|
||||
import { request } from "undici";
|
||||
import EventEmitter from "events";
|
||||
|
||||
// only requiring this to work around an issue regarding worker threads
|
||||
|
@ -219,7 +219,7 @@ class ImageWorker extends BaseServiceWorker {
|
|||
}
|
||||
} else if (process.env.API_TYPE === "azure") {
|
||||
object.callback = `${process.env.AZURE_CALLBACK_URL}:${this.port}/callback`;
|
||||
const response = await fetch(`${process.env.AZURE_URL}/api/orchestrators/ImageOrchestrator`, { method: "POST", body: JSON.stringify(object) }).then(r => r.json());
|
||||
const response = await request(`${process.env.AZURE_URL}/api/orchestrators/ImageOrchestrator`, { method: "POST", body: JSON.stringify(object) }).then(r => r.body.json());
|
||||
const event = new EventEmitter();
|
||||
this.jobs.set(response.id, event);
|
||||
return await this.waitForAzure(event);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import * as logger from "./logger.js";
|
||||
import fetch from "node-fetch";
|
||||
import { request } from "undici";
|
||||
import fs from "fs";
|
||||
import format from "format-duration";
|
||||
import { Shoukaku, Connectors } from "shoukaku";
|
||||
|
@ -19,7 +19,7 @@ export async function checkStatus() {
|
|||
const newNodes = [];
|
||||
for (const node of nodes) {
|
||||
try {
|
||||
const response = await fetch(`http://${node.url}/version`, { headers: { Authorization: node.auth } }).then(res => res.text());
|
||||
const response = await request(`http://${node.url}/version`, { headers: { authorization: node.auth } }).then(res => res.body.text());
|
||||
if (response) newNodes.push(node);
|
||||
} catch {
|
||||
logger.error(`Failed to get status of Lavalink node ${node.host}.`);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue