From 25f7c0de5da70095040c6a5c33b5e6f1e0cdaf08 Mon Sep 17 00:00:00 2001 From: Rory& Date: Mon, 1 Dec 2025 20:19:14 +0100 Subject: [PATCH] Drop node-fetch-commonjs, proxy-agent as deprecated --- assets/openapi.json | Bin 830463 -> 838012 bytes package-lock.json | Bin 355586 -> 344869 bytes package.json | 3 --- scripts/stress/login.js | 1 - scripts/stress/users.js | 1 - src/api/middlewares/ImageProxy.ts | 1 - .../channels/#channel_id/attachments.ts | 1 - src/api/routes/gifs/search.ts | 5 ----- src/api/routes/gifs/trending-gifs.ts | 6 ------ src/api/routes/gifs/trending.ts | 7 ------- src/api/routes/guilds/templates/index.ts | 1 - src/api/util/handlers/Message.ts | 3 +-- src/api/util/utility/EmbedHandlers.ts | 13 ++++++++----- src/api/util/utility/captcha.ts | 1 - src/api/util/utility/ipAddress.ts | 1 - src/util/util/AutoUpdate.ts | 11 +++-------- src/util/util/cdn.ts | 1 - 17 files changed, 12 insertions(+), 44 deletions(-) diff --git a/assets/openapi.json b/assets/openapi.json index 05474f8875796bc6ef6729dfa0334887184fad06..4129af5d2eb4ac6ec4173424ddd594ad98683f38 100644 GIT binary patch delta 714 zcmZvYT}V@57{__fdCz+`_nGuz+M2X1`fxJ`yO1zN`@l#iK|&X8r=4j7=MLx0&OcIHPAbn&}*pXd3%&-4GUEj9j{ z^k-*n2>aL}eEdnOkUq-XlhF$(UFPUBhy5`5Pw!ruSah4ibSvW+%Q%PuXTTfKe8wS6 zA20=k&u0AR7(u1k*Ma2M>S3Ai+Yrez4e># zdM&9AMv{Zr)@0dHz9X-s$#0^EpUY5sgj4wbl5a$Ifz|10&v6uH`Brqc3L>NrTqB%q zTo~c6v>U#$uXPrEff*!3{Z-*Gb8{fWi7}Kk*d@b=_bMMPO2Kpd*U9m wuL=gtKr}EJnDh44ig%uT$8GaBAwR+_l?0{Fmit(nv6#n-E=#`Xvic_e0KRAnUjP6A delta 134 zcmew}$@u?uqYbNhr$1FEpRFzHSI zaD_>6dg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y0}4a=nTgIa z3?uH5uon5ht_iz#Vea@37+Ks!JWzWV-DECj+Z4F+8nSx-zT3Fb0v0?$I-qbp`#d_A zkT2is+B)Tj5oWI)&$n#KR$p~wb1~l!uI->(ODvq^1J6B$odv%MTFu{1+sv7C zB5QNhWE>7ObgmI?8uXYTbDcs~?>5+Es;1CvS>>a&7OIYGd$k3>qh zi=658b@{Ye3&ne7fo3K=p|o)wk8?qh?&k-cdMjC=dR$b|igKH9+ksxatVGInmL9aq zf-3dE^`j_r_P_-%4jJBI4-)Ezf-eRrfIqx+ZnqCTfnc>f_%4YahnDPu8=+3z+#=x? z4z9)?X8ZF&WJ};R(dMd6lS->aB}j!Qks%u&6xnE3?C^0~tkHV4+nmNG{dz+Su^lsH z@e@v-c7#l_n%2Wa*JyQBO{vrFs2HC1lX_{0FG}S$rso;&^6n+}%g@7{T_iSmC4#K3 z!>aJkE$XiOz9o6VpbEX)%Uj6W-UO>J^`O2SY-mBoEj4iXrtuFo|KAVf|6lIUUMpgE zy1`$f=_4>gt4A0z!uFt9`mj3pk{{`gRxIq8_ diff --git a/package.json b/package.json index 01a705db0..97bc56714 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,6 @@ "@types/multer": "^2.0.0", "@types/murmurhash-js": "^1.0.6", "@types/node": "^24.10.1", - "@types/node-fetch": "^2.6.13", "@types/nodemailer": "^7.0.4", "@types/probe-image-size": "^7.2.5", "@types/sharp": "^0.31.1", @@ -104,11 +103,9 @@ "multer": "^2.0.2", "murmurhash-js": "^1.0.0", "node-2fa": "^2.0.3", - "node-fetch-commonjs": "^3.3.2", "pg": "^8.16.3", "picocolors": "^1.1.1", "probe-image-size": "^7.2.3", - "proxy-agent": "^6.5.0", "reflect-metadata": "^0.2.2", "sqlite3": "^5.1.7", "tslib": "^2.8.1", diff --git a/scripts/stress/login.js b/scripts/stress/login.js index 473e2e956..5cb254d16 100644 --- a/scripts/stress/login.js +++ b/scripts/stress/login.js @@ -1,4 +1,3 @@ -const fetch = require("node-fetch"); const ENDPOINT = process.env.API || "http://localhost:3001"; async function main() { diff --git a/scripts/stress/users.js b/scripts/stress/users.js index 053943d01..67fa3ec88 100644 --- a/scripts/stress/users.js +++ b/scripts/stress/users.js @@ -17,7 +17,6 @@ */ require("dotenv").config({ quiet: true }); -const fetch = require("node-fetch"); const count = Number(process.env.COUNT) || 50; const endpoint = process.env.API || "http://localhost:3001"; diff --git a/src/api/middlewares/ImageProxy.ts b/src/api/middlewares/ImageProxy.ts index 814f0e6d4..d87a8de91 100644 --- a/src/api/middlewares/ImageProxy.ts +++ b/src/api/middlewares/ImageProxy.ts @@ -20,7 +20,6 @@ import { Config, JimpType } from "@spacebar/util"; import { Request, Response } from "express"; import { yellow } from "picocolors"; import crypto from "crypto"; -import fetch from "node-fetch-commonjs"; let sharp: undefined | false | { default: typeof import("sharp") } = undefined; diff --git a/src/api/routes/channels/#channel_id/attachments.ts b/src/api/routes/channels/#channel_id/attachments.ts index 2c08931e8..4699fe408 100644 --- a/src/api/routes/channels/#channel_id/attachments.ts +++ b/src/api/routes/channels/#channel_id/attachments.ts @@ -25,7 +25,6 @@ import { } from "@spacebar/util"; import { Request, Response, Router } from "express"; import { CloudAttachment } from "@spacebar/util"; -import fetch from "node-fetch-commonjs"; import { UploadAttachmentRequestSchema, UploadAttachmentResponseSchema } from "@spacebar/schemas" const router: Router = Router({ mergeParams: true }); diff --git a/src/api/routes/gifs/search.ts b/src/api/routes/gifs/search.ts index 378e3981d..199d43da2 100644 --- a/src/api/routes/gifs/search.ts +++ b/src/api/routes/gifs/search.ts @@ -22,8 +22,6 @@ import { parseGifResult, } from "@spacebar/util"; import { Request, Response, Router } from "express"; -import fetch from "node-fetch-commonjs"; -import { ProxyAgent } from "proxy-agent"; import http from "http"; import { TenorGif, TenorMediaTypes } from "@spacebar/schemas" @@ -62,12 +60,9 @@ router.get( const apiKey = getGifApiKey(); - const agent = new ProxyAgent(); - const response = await fetch( `https://g.tenor.com/v1/search?q=${q}&media_format=${media_format}&locale=${locale}&key=${apiKey}`, { - agent: agent as http.Agent, method: "get", headers: { "Content-Type": "application/json" }, }, diff --git a/src/api/routes/gifs/trending-gifs.ts b/src/api/routes/gifs/trending-gifs.ts index 48c2a3bf6..8de10650a 100644 --- a/src/api/routes/gifs/trending-gifs.ts +++ b/src/api/routes/gifs/trending-gifs.ts @@ -22,9 +22,6 @@ import { parseGifResult, } from "@spacebar/util"; import { Request, Response, Router } from "express"; -import fetch from "node-fetch-commonjs"; -import { ProxyAgent } from "proxy-agent"; -import http from "http"; import { TenorGif, TenorMediaTypes } from "@spacebar/schemas" const router = Router({ mergeParams: true }); @@ -57,12 +54,9 @@ router.get( const apiKey = getGifApiKey(); - const agent = new ProxyAgent(); - const response = await fetch( `https://g.tenor.com/v1/trending?media_format=${media_format}&locale=${locale}&key=${apiKey}`, { - agent: agent as http.Agent, method: "get", headers: { "Content-Type": "application/json" }, }, diff --git a/src/api/routes/gifs/trending.ts b/src/api/routes/gifs/trending.ts index bcbd450dc..59314afb2 100644 --- a/src/api/routes/gifs/trending.ts +++ b/src/api/routes/gifs/trending.ts @@ -22,9 +22,6 @@ import { parseGifResult, } from "@spacebar/util"; import { Request, Response, Router } from "express"; -import fetch from "node-fetch-commonjs"; -import { ProxyAgent } from "proxy-agent"; -import http from "http"; import { TenorCategoriesResults, TenorTrendingResults } from "@spacebar/schemas" const router = Router({ mergeParams: true }); @@ -52,13 +49,10 @@ router.get( const apiKey = getGifApiKey(); - const agent = new ProxyAgent(); - const [responseSource, trendGifSource] = await Promise.all([ fetch( `https://g.tenor.com/v1/categories?locale=${locale}&key=${apiKey}`, { - agent: agent as http.Agent, method: "get", headers: { "Content-Type": "application/json" }, }, @@ -66,7 +60,6 @@ router.get( fetch( `https://g.tenor.com/v1/trending?locale=${locale}&key=${apiKey}`, { - agent: agent as http.Agent, method: "get", headers: { "Content-Type": "application/json" }, }, diff --git a/src/api/routes/guilds/templates/index.ts b/src/api/routes/guilds/templates/index.ts index 7937e9831..d5950a463 100644 --- a/src/api/routes/guilds/templates/index.ts +++ b/src/api/routes/guilds/templates/index.ts @@ -19,7 +19,6 @@ import { route } from "@spacebar/api"; import { Config, DiscordApiErrors, Guild, Member, Template } from "@spacebar/util"; import { Request, Response, Router } from "express"; -import fetch from "node-fetch-commonjs"; import { HTTPError } from "lambert-server"; import { GuildTemplateCreateSchema } from "@spacebar/schemas" diff --git a/src/api/util/handlers/Message.ts b/src/api/util/handlers/Message.ts index fd1630149..828576abe 100644 --- a/src/api/util/handlers/Message.ts +++ b/src/api/util/handlers/Message.ts @@ -50,7 +50,6 @@ import { } from "@spacebar/util"; import { HTTPError } from "lambert-server"; import { In, Or, Equal, IsNull } from "typeorm"; -import fetch from "node-fetch-commonjs"; import { ChannelType, Embed, EmbedType, MessageCreateAttachment, MessageCreateCloudAttachment, MessageCreateSchema, MessageType, Reaction } from "@spacebar/schemas"; const allow_empty = false; // TODO: check webhook, application, system author, stickers @@ -206,7 +205,7 @@ export async function handleMessage(opts: MessageOptions): Promise { } if (opts.avatar_url) { const avatarData = await fetch(opts.avatar_url); - const base64 = await avatarData.buffer().then((x) => x.toString("base64")); + const base64 = await avatarData.text().then((x) => btoa(x)); const dataUri = "data:" + avatarData.headers.get("content-type") + ";base64," + base64; diff --git a/src/api/util/utility/EmbedHandlers.ts b/src/api/util/utility/EmbedHandlers.ts index f93df3e2d..96fbe1c95 100644 --- a/src/api/util/utility/EmbedHandlers.ts +++ b/src/api/util/utility/EmbedHandlers.ts @@ -20,19 +20,16 @@ import { Config }from "@spacebar/util"; import { Embed, EmbedImage, EmbedType } from "@spacebar/schemas"; import * as cheerio from "cheerio"; import crypto from "crypto"; -import fetch, { RequestInit } from "node-fetch-commonjs"; import { yellow } from "picocolors"; import probe from "probe-image-size"; export const DEFAULT_FETCH_OPTIONS: RequestInit = { redirect: "follow", - follow: 1, headers: { "user-agent": "Mozilla/5.0 (compatible; Spacebar/1.0; +https://github.com/spacebarchat/server)", }, // size: 1024 * 1024 * 5, // grabbed from config later - compress: true, method: "GET", }; @@ -130,10 +127,16 @@ export const getMetaDescriptions = (text: string) => { const doFetch = async (url: URL) => { try { - return await fetch(url, { + const res = await fetch(url, { ...DEFAULT_FETCH_OPTIONS, - size: Config.get().limits.message.maxEmbedDownloadSize, }); + if (res.headers.get("content-length")) { + const contentLength = parseInt(res.headers.get("content-length")!); + if (Config.get().limits.message.maxEmbedDownloadSize && contentLength > Config.get().limits.message.maxEmbedDownloadSize) { + return null; + } + } + return res; } catch (e) { return null; } diff --git a/src/api/util/utility/captcha.ts b/src/api/util/utility/captcha.ts index 3c0ac7669..3f654a48d 100644 --- a/src/api/util/utility/captcha.ts +++ b/src/api/util/utility/captcha.ts @@ -17,7 +17,6 @@ */ import { Config } from "@spacebar/util"; -import fetch from "node-fetch-commonjs"; export interface hcaptchaResponse { success: boolean; diff --git a/src/api/util/utility/ipAddress.ts b/src/api/util/utility/ipAddress.ts index c19c7c352..2609dae12 100644 --- a/src/api/util/utility/ipAddress.ts +++ b/src/api/util/utility/ipAddress.ts @@ -19,7 +19,6 @@ import { Config } from "@spacebar/util"; import { Request } from "express"; // use ipdata package instead of simple fetch because of integrated caching -import fetch from "node-fetch-commonjs"; const exampleData = { ip: "", diff --git a/src/util/util/AutoUpdate.ts b/src/util/util/AutoUpdate.ts index e62a5c508..efd0954e8 100644 --- a/src/util/util/AutoUpdate.ts +++ b/src/util/util/AutoUpdate.ts @@ -16,12 +16,9 @@ along with this program. If not, see . */ -import fetch from "node-fetch-commonjs"; -import { ProxyAgent } from "proxy-agent"; import readline from "readline"; import fs from "fs/promises"; import path from "path"; -import http from "http"; const rl = readline.createInterface({ input: process.stdin, @@ -74,9 +71,8 @@ async function download(url: string, dir: string) { try { // TODO: use file stream instead of buffer (to prevent crash because of high memory usage for big files) // TODO check file hash - const agent = new ProxyAgent(); - const response = await fetch(url, { agent: agent as http.Agent }); - const buffer = await response.buffer(); + const response = await fetch(url); + const buffer = await response.bytes(); const tempDir = await fs.mkdtemp("spacebar"); await fs.writeFile(path.join(tempDir, "Spacebar.zip"), buffer); } catch (error) { @@ -97,8 +93,7 @@ async function getCurrentVersion(dir: string) { async function getLatestVersion(url: string) { try { - const agent = new ProxyAgent(); - const response = await fetch(url, { agent: agent as http.Agent }); + const response = await fetch(url); const content = (await response.json()) as { version: string }; return content.version; } catch (error) { diff --git a/src/util/util/cdn.ts b/src/util/util/cdn.ts index d5ca9ba9a..01f03fc19 100644 --- a/src/util/util/cdn.ts +++ b/src/util/util/cdn.ts @@ -18,7 +18,6 @@ import FormData from "form-data"; import { HTTPError } from "lambert-server"; -import fetch from "node-fetch-commonjs"; import { Attachment } from "../entities"; import { Config } from "./Config";