Migrate to undici, try new method for getting image size/type/data

This commit is contained in:
Essem 2022-08-11 11:46:56 -05:00
parent 34ac7b3380
commit da6f95aad8
No known key found for this signature in database
GPG Key ID: 7D497397CC3A2A8C
19 changed files with 113 additions and 140 deletions

View File

@ -1,4 +1,4 @@
import fetch from "node-fetch"; import { request } from "undici";
import Command from "../../classes/command.js"; import Command from "../../classes/command.js";
class AncientCommand extends Command { class AncientCommand extends Command {
@ -9,16 +9,9 @@ class AncientCommand extends Command {
controller.abort(); controller.abort();
}, 15000); }, 15000);
try { try {
const data = await fetch("https://projectlounge.pw/meme/", { redirect: "manual", signal: controller.signal }); const data = await request("https://projectlounge.pw/meme/", { method: "HEAD", signal: controller.signal });
clearTimeout(timeout); clearTimeout(timeout);
return { return data.headers.location;
embeds: [{
color: 16711680,
image: {
url: data.headers.get("location")
}
}]
};
} catch (e) { } catch (e) {
if (e.name === "AbortError") { if (e.name === "AbortError") {
return "I couldn't get a meme in time. Maybe try again?"; return "I couldn't get a meme in time. Maybe try again?";

View File

@ -1,19 +1,23 @@
import fetch from "node-fetch"; import { request } from "undici";
import Command from "../../classes/command.js"; import Command from "../../classes/command.js";
class BirdCommand extends Command { class BirdCommand extends Command {
async run() { async run() {
await this.acknowledge(); await this.acknowledge();
const imageData = await fetch("http://shibe.online/api/birds"); const controller = new AbortController();
const json = await imageData.json(); const timeout = setTimeout(() => {
return { controller.abort();
embeds: [{ }, 15000);
color: 16711680, try {
image: { const imageData = await request("http://shibe.online/api/birds", { signal: controller.signal });
url: json[0] clearTimeout(timeout);
} const json = await imageData.body.json();
}] return json[0];
}; } catch (e) {
if (e.name === "AbortError") {
return "I couldn't get a bird image in time. Maybe try again?";
}
}
} }
static description = "Gets a random bird picture"; static description = "Gets a random bird picture";

View File

@ -1,4 +1,4 @@
import fetch from "node-fetch"; import { request } from "undici";
import Command from "../../classes/command.js"; import Command from "../../classes/command.js";
class CatCommand extends Command { class CatCommand extends Command {
@ -9,16 +9,9 @@ class CatCommand extends Command {
controller.abort(); controller.abort();
}, 15000); }, 15000);
try { try {
const data = await fetch("https://projectlounge.pw/cta/", { redirect: "manual", signal: controller.signal }); const data = await request("https://projectlounge.pw/cta/", { method: "HEAD", signal: controller.signal });
clearTimeout(timeout); clearTimeout(timeout);
return { return data.headers.location;
embeds: [{
color: 16711680,
image: {
url: data.headers.get("location")
}
}]
};
} catch (e) { } catch (e) {
if (e.name === "AbortError") { if (e.name === "AbortError") {
return "I couldn't get a cat image in time. Maybe try again?"; return "I couldn't get a cat image in time. Maybe try again?";

View File

@ -1,19 +1,23 @@
import fetch from "node-fetch"; import { request } from "undici";
import Command from "../../classes/command.js"; import Command from "../../classes/command.js";
class DogCommand extends Command { class DogCommand extends Command {
async run() { async run() {
await this.acknowledge(); await this.acknowledge();
const imageData = await fetch("https://dog.ceo/api/breeds/image/random"); const controller = new AbortController();
const json = await imageData.json(); const timeout = setTimeout(() => {
return { controller.abort();
embeds: [{ }, 15000);
color: 16711680, try {
image: { const imageData = await request("https://dog.ceo/api/breeds/image/random", { signal: controller.signal });
url: json.message clearTimeout(timeout);
} const json = await imageData.body.json();
}] return json.message;
}; } catch (e) {
if (e.name === "AbortError") {
return "I couldn't get a dog image in time. Maybe try again?";
}
}
} }
static description = "Gets a random dog picture"; static description = "Gets a random dog picture";

View File

@ -1,24 +1,28 @@
import fetch from "node-fetch"; import { request } from "undici";
import Command from "../../classes/command.js"; import Command from "../../classes/command.js";
class WikihowCommand extends Command { class WikihowCommand extends Command {
async run() { async run() {
await this.acknowledge(); await this.acknowledge();
const request = await fetch("https://www.wikihow.com/api.php?action=query&generator=random&prop=imageinfo&format=json&iiprop=url&grnnamespace=6"); const controller = new AbortController();
const json = await request.json(); const timeout = setTimeout(() => {
const id = Object.keys(json.query.pages)[0]; controller.abort();
const data = json.query.pages[id]; }, 15000);
if (data.imageinfo) { try {
return { const req = await request("https://www.wikihow.com/api.php?action=query&generator=random&prop=imageinfo&format=json&iiprop=url&grnnamespace=6", { signal: controller.signal });
embeds: [{ clearTimeout(timeout);
color: 16711680, const json = await req.body.json();
image: { const id = Object.keys(json.query.pages)[0];
url: json.query.pages[id].imageinfo[0].url const data = json.query.pages[id];
} if (data.imageinfo) {
}] return json.query.pages[id].imageinfo[0].url;
}; } else {
} else { return await this.run();
return await this.run(); }
} catch (e) {
if (e.name === "AbortError") {
return "I couldn't get a WikiHow image in time. Maybe try again?";
}
} }
} }

View File

@ -1,4 +1,4 @@
import fetch from "node-fetch"; import { request } from "undici";
import Command from "../../classes/command.js"; import Command from "../../classes/command.js";
class DonateCommand extends Command { class DonateCommand extends Command {
@ -10,7 +10,7 @@ class DonateCommand extends Command {
controller.abort(); controller.abort();
}, 5000); }, 5000);
try { try {
const patrons = await fetch("https://projectlounge.pw/patrons", { signal: controller.signal }).then(data => data.json()); const patrons = await request("https://projectlounge.pw/patrons", { signal: controller.signal }).then(data => data.body.json());
clearTimeout(timeout); clearTimeout(timeout);
prefix = "Thanks to the following patrons for their support:\n"; prefix = "Thanks to the following patrons for their support:\n";
for (const patron of patrons) { for (const patron of patrons) {

View File

@ -2,7 +2,7 @@ import paginator from "../../utils/pagination/pagination.js";
import { readFileSync } from "fs"; import { readFileSync } from "fs";
const { searx } = JSON.parse(readFileSync(new URL("../../config/servers.json", import.meta.url))); const { searx } = JSON.parse(readFileSync(new URL("../../config/servers.json", import.meta.url)));
import { random } from "../../utils/misc.js"; import { random } from "../../utils/misc.js";
import fetch from "node-fetch"; import { request } from "undici";
import Command from "../../classes/command.js"; import Command from "../../classes/command.js";
class ImageSearchCommand extends Command { class ImageSearchCommand extends Command {
@ -12,7 +12,7 @@ class ImageSearchCommand extends Command {
if (!query || !query.trim()) return "You need to provide something to search for!"; if (!query || !query.trim()) return "You need to provide something to search for!";
await this.acknowledge(); await this.acknowledge();
const embeds = []; const embeds = [];
const rawImages = await fetch(`${random(searx)}/search?format=json&safesearch=2&categories=images&q=!goi%20!ddi%20${encodeURIComponent(query)}`).then(res => res.json()); const rawImages = await request(`${random(searx)}/search?format=json&safesearch=2&categories=images&q=!goi%20!ddi%20${encodeURIComponent(query)}`).then(res => res.body.json());
if (rawImages.results.length === 0) return "I couldn't find any results!"; if (rawImages.results.length === 0) return "I couldn't find any results!";
const images = rawImages.results.filter((val) => !val.img_src.startsWith("data:")); const images = rawImages.results.filter((val) => !val.img_src.startsWith("data:"));
for (const [i, value] of images.entries()) { for (const [i, value] of images.entries()) {

View File

@ -1,5 +1,5 @@
import urlCheck from "../../utils/urlcheck.js"; import urlCheck from "../../utils/urlcheck.js";
import fetch from "node-fetch"; import { request } from "undici";
import Command from "../../classes/command.js"; import Command from "../../classes/command.js";
class LengthenCommand extends Command { class LengthenCommand extends Command {
@ -8,8 +8,8 @@ class LengthenCommand extends Command {
const input = this.options.url ?? this.args.join(" "); const input = this.options.url ?? this.args.join(" ");
if (!input || !input.trim() || !urlCheck(input)) return "You need to provide a short URL to lengthen!"; if (!input || !input.trim() || !urlCheck(input)) return "You need to provide a short URL to lengthen!";
if (urlCheck(input)) { if (urlCheck(input)) {
const url = await fetch(encodeURI(input), { redirect: "manual" }); const url = await request(encodeURI(input), { method: "HEAD" });
return url.headers.get("location") || input; return url.headers.location || input;
} else { } else {
return "That isn't a URL!"; return "That isn't a URL!";
} }

View File

@ -1,5 +1,5 @@
import jsqr from "jsqr"; import jsqr from "jsqr";
import fetch from "node-fetch"; import { request } from "undici";
import sharp from "sharp"; import sharp from "sharp";
import { clean } from "../../utils/misc.js"; import { clean } from "../../utils/misc.js";
import Command from "../../classes/command.js"; import Command from "../../classes/command.js";
@ -10,7 +10,7 @@ class QrReadCommand extends Command {
const image = await imageDetect(this.client, this.message, this.interaction, this.options); const image = await imageDetect(this.client, this.message, this.interaction, this.options);
if (image === undefined) return "You need to provide an image/GIF with a QR code to read!"; if (image === undefined) return "You need to provide an image/GIF with a QR code to read!";
await this.acknowledge(); await this.acknowledge();
const data = Buffer.from(await (await fetch(image.path)).arrayBuffer()); const data = Buffer.from(await (await request(image.path)).body.arrayBuffer());
const rawData = await sharp(data).ensureAlpha().raw().toBuffer({ resolveWithObject: true }); const rawData = await sharp(data).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
const qrBuffer = jsqr(rawData.data, rawData.info.width, rawData.info.height); const qrBuffer = jsqr(rawData.data, rawData.info.width, rawData.info.height);
if (!qrBuffer) return "I couldn't find a QR code!"; if (!qrBuffer) return "I couldn't find a QR code!";

View File

@ -1,4 +1,4 @@
import fetch from "node-fetch"; import { request } from "undici";
import { readFileSync } from "fs"; import { readFileSync } from "fs";
const { searx } = JSON.parse(readFileSync(new URL("../../config/servers.json", import.meta.url))); const { searx } = JSON.parse(readFileSync(new URL("../../config/servers.json", import.meta.url)));
import { random } from "../../utils/misc.js"; import { random } from "../../utils/misc.js";
@ -11,7 +11,7 @@ class YouTubeCommand extends Command {
if (!query || !query.trim()) return "You need to provide something to search for!"; if (!query || !query.trim()) return "You need to provide something to search for!";
await this.acknowledge(); await this.acknowledge();
const messages = []; const messages = [];
const videos = await fetch(`${random(searx)}/search?format=json&safesearch=1&categories=videos&q=!youtube%20${encodeURIComponent(query)}`).then(res => res.json()); const videos = await request(`${random(searx)}/search?format=json&safesearch=1&categories=videos&q=!youtube%20${encodeURIComponent(query)}`).then(res => res.body.json());
if (videos.results.length === 0) return "I couldn't find any results!"; if (videos.results.length === 0) return "I couldn't find any results!";
for (const [i, value] of videos.results.entries()) { for (const [i, value] of videos.results.entries()) {
messages.push({ content: `Page ${i + 1} of ${videos.results.length}\n<:youtube:637020823005167626> **${value.title.replaceAll("*", "\\*")}**\nUploaded by **${value.author.replaceAll("*", "\\*")}**\n${value.url}` }); messages.push({ content: `Page ${i + 1} of ${videos.results.length}\n<:youtube:637020823005167626> **${value.title.replaceAll("*", "\\*")}**\nUploaded by **${value.author.replaceAll("*", "\\*")}**\n${value.url}` });

View File

@ -1,4 +1,4 @@
import fetch from "node-fetch"; import { request } from "undici";
import format from "format-duration"; import format from "format-duration";
import { nodes } from "../../utils/soundplayer.js"; import { nodes } from "../../utils/soundplayer.js";
import paginator from "../../utils/pagination/pagination.js"; import paginator from "../../utils/pagination/pagination.js";
@ -12,7 +12,7 @@ class QueueCommand extends MusicCommand {
if (!this.channel.permissionsOf(this.client.user.id).has("embedLinks")) return "I don't have the `Embed Links` permission!"; if (!this.channel.permissionsOf(this.client.user.id).has("embedLinks")) return "I don't have the `Embed Links` permission!";
const player = this.connection; const player = this.connection;
const node = nodes.filter((val) => val.name === player.player.node.name)[0]; const node = nodes.filter((val) => val.name === player.player.node.name)[0];
const tracks = await fetch(`http://${node.url}/decodetracks`, { method: "POST", body: JSON.stringify(this.queue), headers: { Authorization: node.auth, "Content-Type": "application/json" } }).then(res => res.json()); const tracks = await request(`http://${node.url}/decodetracks`, { method: "POST", body: JSON.stringify(this.queue), headers: { authorization: node.auth, "content-type": "application/json" } }).then(res => res.body.json());
const trackList = []; const trackList = [];
const firstTrack = tracks.shift(); const firstTrack = tracks.shift();
for (const [i, track] of tracks.entries()) { for (const [i, track] of tracks.entries()) {

View File

@ -34,10 +34,10 @@
"jsqr": "^1.4.0", "jsqr": "^1.4.0",
"node-addon-api": "^5.0.0", "node-addon-api": "^5.0.0",
"node-emoji": "^1.11.0", "node-emoji": "^1.11.0",
"node-fetch": "^3.2.10",
"qrcode": "^1.5.1", "qrcode": "^1.5.1",
"sharp": "^0.30.7", "sharp": "^0.30.7",
"shoukaku": "github:Deivu/shoukaku", "shoukaku": "github:Deivu/shoukaku",
"undici": "^5.8.2",
"winston": "^3.8.1", "winston": "^3.8.1",
"winston-daily-rotate-file": "^4.7.1" "winston-daily-rotate-file": "^4.7.1"
}, },

View File

@ -21,11 +21,11 @@ specifiers:
jsqr: ^1.4.0 jsqr: ^1.4.0
node-addon-api: ^5.0.0 node-addon-api: ^5.0.0
node-emoji: ^1.11.0 node-emoji: ^1.11.0
node-fetch: ^3.2.10
pg: ^8.7.3 pg: ^8.7.3
qrcode: ^1.5.1 qrcode: ^1.5.1
sharp: ^0.30.7 sharp: ^0.30.7
shoukaku: github:Deivu/shoukaku shoukaku: github:Deivu/shoukaku
undici: ^5.8.2
uuid: ^8.3.2 uuid: ^8.3.2
winston: ^3.8.1 winston: ^3.8.1
winston-daily-rotate-file: ^4.7.1 winston-daily-rotate-file: ^4.7.1
@ -44,10 +44,10 @@ dependencies:
jsqr: 1.4.0 jsqr: 1.4.0
node-addon-api: 5.0.0 node-addon-api: 5.0.0
node-emoji: 1.11.0 node-emoji: 1.11.0
node-fetch: 3.2.10
qrcode: 1.5.1 qrcode: 1.5.1
sharp: 0.30.7 sharp: 0.30.7
shoukaku: github.com/Deivu/shoukaku/b6c724bf6e72a5dcf14beb6f51d35bc4b6a17647_bufferutil@4.0.6 shoukaku: github.com/Deivu/shoukaku/b6c724bf6e72a5dcf14beb6f51d35bc4b6a17647_bufferutil@4.0.6
undici: 5.8.2
winston: 3.8.1 winston: 3.8.1
winston-daily-rotate-file: 4.7.1_winston@3.8.1 winston-daily-rotate-file: 4.7.1_winston@3.8.1
@ -860,11 +860,6 @@ packages:
which: 2.0.2 which: 2.0.2
dev: true dev: true
/data-uri-to-buffer/4.0.0:
resolution: {integrity: sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA==}
engines: {node: '>= 12'}
dev: false
/debug/4.3.4: /debug/4.3.4:
resolution: {integrity: sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==} resolution: {integrity: sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==}
engines: {node: '>=6.0'} engines: {node: '>=6.0'}
@ -1175,14 +1170,6 @@ packages:
resolution: {integrity: sha512-OP2IUU6HeYKJi3i0z4A19kHMQoLVs4Hc+DPqqxI2h/DPZHTm/vjsfC6P0b4jCMy14XizLBqvndQ+UilD7707Jw==} resolution: {integrity: sha512-OP2IUU6HeYKJi3i0z4A19kHMQoLVs4Hc+DPqqxI2h/DPZHTm/vjsfC6P0b4jCMy14XizLBqvndQ+UilD7707Jw==}
dev: false dev: false
/fetch-blob/3.2.0:
resolution: {integrity: sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==}
engines: {node: ^12.20 || >= 14.13}
dependencies:
node-domexception: 1.0.0
web-streams-polyfill: 3.2.1
dev: false
/file-entry-cache/6.0.1: /file-entry-cache/6.0.1:
resolution: {integrity: sha512-7Gps/XWymbLk2QLYK4NzpMOrYjMhdIxXuIvy2QBsLE6ljuodKvdkWs/cpyJJ3CVIVpH0Oi1Hvg1ovbMzLdFBBg==} resolution: {integrity: sha512-7Gps/XWymbLk2QLYK4NzpMOrYjMhdIxXuIvy2QBsLE6ljuodKvdkWs/cpyJJ3CVIVpH0Oi1Hvg1ovbMzLdFBBg==}
engines: {node: ^10.12.0 || >=12.0.0} engines: {node: ^10.12.0 || >=12.0.0}
@ -1264,13 +1251,6 @@ packages:
resolution: {integrity: sha512-ARqJ9qXm71pw3SGAY7bibf8lRLvltOXLjWjzzR3UrUjHu1zdeYpA/Z+u+ltdhrfRa440OjEsHNzdmuZViqqQWQ==} resolution: {integrity: sha512-ARqJ9qXm71pw3SGAY7bibf8lRLvltOXLjWjzzR3UrUjHu1zdeYpA/Z+u+ltdhrfRa440OjEsHNzdmuZViqqQWQ==}
dev: false dev: false
/formdata-polyfill/4.0.10:
resolution: {integrity: sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==}
engines: {node: '>=12.20.0'}
dependencies:
fetch-blob: 3.2.0
dev: false
/fs-constants/1.0.0: /fs-constants/1.0.0:
resolution: {integrity: sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==} resolution: {integrity: sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==}
dev: false dev: false
@ -1765,11 +1745,6 @@ packages:
resolution: {integrity: sha512-CvkDw2OEnme7ybCykJpVcKH+uAOLV2qLqiyla128dN9TkEWfrYmxG6C2boDe5KcNQqZF3orkqzGgOMvZ/JNekA==} resolution: {integrity: sha512-CvkDw2OEnme7ybCykJpVcKH+uAOLV2qLqiyla128dN9TkEWfrYmxG6C2boDe5KcNQqZF3orkqzGgOMvZ/JNekA==}
dev: false dev: false
/node-domexception/1.0.0:
resolution: {integrity: sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==}
engines: {node: '>=10.5.0'}
dev: false
/node-emoji/1.11.0: /node-emoji/1.11.0:
resolution: {integrity: sha512-wo2DpQkQp7Sjm2A0cq+sN7EHKO6Sl0ctXeBdFZrL9T9+UywORbufTcTZxom8YqpLQt/FqNMUkOpkZrJVYSKD3A==} resolution: {integrity: sha512-wo2DpQkQp7Sjm2A0cq+sN7EHKO6Sl0ctXeBdFZrL9T9+UywORbufTcTZxom8YqpLQt/FqNMUkOpkZrJVYSKD3A==}
dependencies: dependencies:
@ -1788,15 +1763,6 @@ packages:
whatwg-url: 5.0.0 whatwg-url: 5.0.0
dev: false dev: false
/node-fetch/3.2.10:
resolution: {integrity: sha512-MhuzNwdURnZ1Cp4XTazr69K0BTizsBroX7Zx3UgDSVcZYKF/6p0CBe4EUb/hLqmzVhl0UpYfgRljQ4yxE+iCxA==}
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
dependencies:
data-uri-to-buffer: 4.0.0
fetch-blob: 3.2.0
formdata-polyfill: 4.0.10
dev: false
/node-gyp-build/4.5.0: /node-gyp-build/4.5.0:
resolution: {integrity: sha512-2iGbaQBV+ITgCz76ZEjmhUKAKVf7xfY1sRl4UiKQspfZMH2h06SyhNsnSVy50cwkFQDGLyif6m/6uFXHkOZ6rg==} resolution: {integrity: sha512-2iGbaQBV+ITgCz76ZEjmhUKAKVf7xfY1sRl4UiKQspfZMH2h06SyhNsnSVy50cwkFQDGLyif6m/6uFXHkOZ6rg==}
hasBin: true hasBin: true
@ -2678,11 +2644,6 @@ packages:
spdx-expression-parse: 3.0.1 spdx-expression-parse: 3.0.1
dev: true dev: true
/web-streams-polyfill/3.2.1:
resolution: {integrity: sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==}
engines: {node: '>= 8'}
dev: false
/webidl-conversions/3.0.1: /webidl-conversions/3.0.1:
resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==} resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==}
dev: false dev: false

View File

@ -1,6 +1,6 @@
import { createRequire } from "module"; import { createRequire } from "module";
import { isMainThread, parentPort, workerData } from "worker_threads"; import { isMainThread, parentPort, workerData } from "worker_threads";
import fetch from "node-fetch"; import { request } from "undici";
import path from "path"; import path from "path";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
@ -31,7 +31,7 @@ export default function run(object) {
buffer: Buffer.alloc(0), buffer: Buffer.alloc(0),
fileExtension: "nogif" 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"). // 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. // Don't set `type` directly on the object we are passed as it will be read afterwards.

View File

@ -1,4 +1,4 @@
import fetch from "node-fetch"; import { request } from "undici";
import fs from "fs"; import fs from "fs";
import { fileTypeFromBuffer, fileTypeFromFile } from "file-type"; import { fileTypeFromBuffer, fileTypeFromFile } from "file-type";
@ -19,26 +19,40 @@ export async function getType(image, extraReturnTypes) {
return undefined; return undefined;
} }
let type; let type;
const controller = new AbortController(); // eslint-disable-line no-undef const controller = new AbortController();
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
controller.abort(); controller.abort();
}, 25000); }, 3000);
try { try {
const imageRequest = await fetch(image, { const imageRequest = await request(image, {
signal: controller.signal, headers: { signal: controller.signal,
"Range": "bytes=0-1023" method: "HEAD"
}
}); });
clearTimeout(timeout); 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 if (parseInt(size) > 26214400 && extraReturnTypes) { // 25 MB
type = "large"; type = "large";
return type; return type;
} }
const imageBuffer = await imageRequest.arrayBuffer(); const typeHeader = imageRequest.headers["content-type"];
const imageType = await fileTypeFromBuffer(imageBuffer); if (typeHeader) {
if (imageType && formats.includes(imageType.mime)) { type = typeHeader;
type = imageType.mime; } 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) { } catch (error) {
if (error.name === "AbortError") { if (error.name === "AbortError") {

View File

@ -1,4 +1,4 @@
import fetch from "node-fetch"; import { request } from "undici";
import WebSocket from "ws"; import WebSocket from "ws";
import * as logger from "./logger.js"; import * as logger from "./logger.js";
import { setTimeout } from "timers/promises"; import { setTimeout } from "timers/promises";
@ -125,12 +125,12 @@ class ImageConnection {
} }
async getOutput(jobid) { async getOutput(jobid) {
const req = await fetch(`${this.httpurl}?id=${jobid}`, { const req = await request(`${this.httpurl}?id=${jobid}`, {
headers: { headers: {
"Authentication": this.auth || undefined authentication: this.auth || undefined
} }
}); });
const contentType = req.headers.get("Content-Type"); const contentType = req.headers["content-type"];
let type; let type;
switch (contentType) { switch (contentType) {
case "image/gif": case "image/gif":
@ -149,7 +149,7 @@ class ImageConnection {
type = contentType; type = contentType;
break; break;
} }
return { buffer: Buffer.from(await req.arrayBuffer()), type }; return { buffer: Buffer.from(await req.body.arrayBuffer()), type };
} }
async do(op, id, data) { async do(op, id, data) {

View File

@ -1,4 +1,4 @@
import fetch from "node-fetch"; import { request } from "undici";
import { getType } from "./image.js"; import { getType } from "./image.js";
const tenorURLs = [ 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, // 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 // so we use that if there's a key in the config
if (process.env.TENOR !== "") { 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}`); 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.status === 429) { if (data.statusCode === 429) {
if (extraReturnTypes) { if (extraReturnTypes) {
payload.type = "tenorlimit"; payload.type = "tenorlimit";
return payload; return payload;
@ -61,7 +61,7 @@ const getImage = async (image, image2, video, extraReturnTypes, gifv = false, ty
return; return;
} }
} }
const json = await data.json(); const json = await data.body.json();
if (json.error) throw Error(json.error); if (json.error) throw Error(json.error);
payload.path = json.results[0].media_formats.gif.url; payload.path = json.results[0].media_formats.gif.url;
} }

View File

@ -6,7 +6,7 @@ import { fileURLToPath } from "url";
import { Worker } from "worker_threads"; import { Worker } from "worker_threads";
import { createRequire } from "module"; import { createRequire } from "module";
import { createServer } from "http"; import { createServer } from "http";
import fetch from "node-fetch"; import { request } from "undici";
import EventEmitter from "events"; import EventEmitter from "events";
// only requiring this to work around an issue regarding worker threads // 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") { } else if (process.env.API_TYPE === "azure") {
object.callback = `${process.env.AZURE_CALLBACK_URL}:${this.port}/callback`; 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(); const event = new EventEmitter();
this.jobs.set(response.id, event); this.jobs.set(response.id, event);
return await this.waitForAzure(event); return await this.waitForAzure(event);

View File

@ -1,5 +1,5 @@
import * as logger from "./logger.js"; import * as logger from "./logger.js";
import fetch from "node-fetch"; import { request } from "undici";
import fs from "fs"; import fs from "fs";
import format from "format-duration"; import format from "format-duration";
import { Shoukaku, Connectors } from "shoukaku"; import { Shoukaku, Connectors } from "shoukaku";
@ -19,7 +19,7 @@ export async function checkStatus() {
const newNodes = []; const newNodes = [];
for (const node of nodes) { for (const node of nodes) {
try { 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); if (response) newNodes.push(node);
} catch { } catch {
logger.error(`Failed to get status of Lavalink node ${node.host}.`); logger.error(`Failed to get status of Lavalink node ${node.host}.`);