Remove node-fetch

This commit is contained in:
Cadence Ember 2025-02-11 12:51:58 +13:00
commit 984d4362a5
14 changed files with 30 additions and 131 deletions

View file

@ -3,10 +3,9 @@
const assert = require("assert")
const {reg} = require("../../matrix/read-registration")
const Ty = require("../../types")
const fetch = require("node-fetch").default
const passthrough = require("../../passthrough")
const {discord, sync, db, select} = passthrough
const {sync, db, select} = passthrough
/** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api")
/** @type {import("../../matrix/file")} */

View file

@ -1,10 +1,8 @@
// @ts-check
const assert = require("assert")
const fetch = require("node-fetch").default
const utils = require("../converters/utils")
const {Readable} = require("stream")
const {sync} = require("../../passthrough")
const assert = require("assert").strict
/** @type {import("../converters/emoji-sheet")} */
const emojiSheetConverter = sync.require("../converters/emoji-sheet")
@ -18,16 +16,16 @@ const api = sync.require("../../matrix/api")
*/
async function getAndConvertEmoji(mxc) {
const abortController = new AbortController()
/** @type {import("node-fetch").Response} */
// If it turns out to be a GIF, we want to abandon the connection without downloading the whole thing.
// If we were using connection pooling, we would be forced to download the entire GIF.
// So we set no agent to ensure we are not connection pooling.
// @ts-ignore the signal is slightly different from the type it wants (still works fine)
const res = await api.getMedia(mxc, {agent: false, signal: abortController.signal})
return emojiSheetConverter.convertImageStream(res.body, () => {
const res = await api.getMedia(mxc, {signal: abortController.signal})
// @ts-ignore
const readable = Readable.fromWeb(res.body)
return emojiSheetConverter.convertImageStream(readable, () => {
abortController.abort()
res.body.pause()
res.body.emit("end")
readable.emit("end")
readable.on("error", () => {}) // DOMException [AbortError]: This operation was aborted
})
}

View file

@ -5,7 +5,6 @@ const DiscordTypes = require("discord-api-types/v10")
const {Readable} = require("stream")
const assert = require("assert").strict
const crypto = require("crypto")
const fetch = require("node-fetch").default
const passthrough = require("../../passthrough")
const {sync, discord, db, select} = passthrough

View file

@ -48,7 +48,7 @@ async function compositeMatrixEmojis(mxcs, mxcDownloader) {
}
/**
* @param {import("node-fetch").Response["body"]} streamIn
* @param {NodeJS.ReadableStream} streamIn
* @param {() => any} stopStream
* @returns {Promise<Buffer | undefined>} Uncompressed PNG image
*/

View file

@ -3,8 +3,6 @@
const Ty = require("../types")
const assert = require("assert").strict
const fetch = require("node-fetch").default
const passthrough = require("../passthrough")
const {sync} = passthrough
/** @type {import("./mreq")} */
@ -344,7 +342,7 @@ async function ping() {
/**
* @param {string} mxc
* @param {fetch.RequestInit} [init]
* @param {RequestInit} [init]
*/
function getMedia(mxc, init = {}) {
const mediaParts = mxc?.match(/^mxc:\/\/([^/]+)\/(\w+)$/)

View file

@ -1,7 +1,5 @@
// @ts-check
const fetch = require("node-fetch").default
const passthrough = require("../passthrough")
const {sync, db, select} = passthrough
/** @type {import("./mreq")} */
@ -47,7 +45,7 @@ async function uploadDiscordFileToMxc(path) {
}
// Download from Discord
const promise = fetch(url, {}).then(/** @param {import("node-fetch").Response} res */ async res => {
const promise = fetch(url, {}).then(async res => {
// Upload to Matrix
const root = await module.exports._actuallyUploadDiscordFileToMxc(urlNoExpiry, res)
@ -62,6 +60,10 @@ async function uploadDiscordFileToMxc(path) {
return promise
}
/**
* @param {string} url
* @param {Response} res
*/
async function _actuallyUploadDiscordFileToMxc(url, res) {
const body = res.body
/** @type {import("../types").R.FileUploaded} */

View file

@ -1,6 +1,5 @@
// @ts-check
const fetch = require("node-fetch").default
const mixin = require("@cloudrac3r/mixin-deep")
const stream = require("stream")
const getStream = require("get-stream")
@ -22,7 +21,7 @@ class MatrixServerError extends Error {
/**
* @param {string} method
* @param {string} url
* @param {any} [body]
* @param {string | object | ReadableStream | stream.Readable} [body]
* @param {any} [extra]
*/
async function mreq(method, url, body, extra = {}) {
@ -30,16 +29,19 @@ async function mreq(method, url, body, extra = {}) {
body = JSON.stringify(body)
} else if (body instanceof stream.Readable && reg.ooye.content_length_workaround) {
body = await getStream.buffer(body)
} else if (body instanceof ReadableStream && reg.ooye.content_length_workaround) {
body = await stream.consumers.buffer(body)
}
const opts = mixin({
/** @type {RequestInit} */
const opts = {
method,
body,
headers: {
Authorization: `Bearer ${reg.as_token}`
}
}, extra)
},
...extra
}
// console.log(baseUrl + url, opts)
const res = await fetch(baseUrl + url, opts)
const root = await res.json()
@ -55,7 +57,7 @@ async function mreq(method, url, body, extra = {}) {
writeRegistration(reg)
return root
}
delete opts.headers.Authorization
delete opts.headers?.["Authorization"]
throw new MatrixServerError(root, {baseUrl, url, ...opts})
}
return root

View file

@ -3,7 +3,6 @@
const tryToCatch = require("try-to-catch")
const {test} = require("supertape")
const {router} = require("../../../test/web")
const fetch = require("node-fetch")
test("web download matrix: access denied if not a known attachment", async t => {
const [error] = await tryToCatch(() =>
@ -27,7 +26,7 @@ test("web download matrix: works if a known attachment", async t => {
event,
api: {
async getMedia(mxc, init) {
return new fetch.Response("", {status: 200, headers: {"content-type": "image/png"}})
return new Response("", {status: 200, headers: {"content-type": "image/png"}})
}
}
})

View file

@ -5,7 +5,6 @@ const {randomUUID} = require("crypto")
const {defineEventHandler, getValidatedQuery, sendRedirect, createError} = require("h3")
const {SnowTransfer} = require("snowtransfer")
const DiscordTypes = require("discord-api-types/v10")
const fetch = require("node-fetch")
const getRelativePath = require("get-relative-path")
const {as, db, sync} = require("../../passthrough")