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,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