1
0
Fork 0

Work around the Content-Length issue

This commit is contained in:
Cadence Ember 2024-01-20 17:03:03 +13:00
parent 8987107685
commit 845f93e5d0
7 changed files with 40 additions and 3 deletions

View file

@ -2,6 +2,8 @@
const fetch = require("node-fetch").default
const mixin = require("mixin-deep")
const stream = require("stream")
const getStream = require("get-stream")
const passthrough = require("../passthrough")
const { sync } = passthrough
@ -27,9 +29,15 @@ class MatrixServerError extends Error {
* @param {any} [extra]
*/
async function mreq(method, url, body, extra = {}) {
if (body == undefined || Object.is(body.constructor, Object)) {
body = JSON.stringify(body)
} else if (body instanceof stream.Readable && reg.ooye.content_length_workaround) {
body = await getStream.buffer(body)
}
const opts = mixin({
method,
body: (body == undefined || Object.is(body.constructor, Object)) ? JSON.stringify(body) : body,
body,
headers: {
Authorization: `Bearer ${reg.as_token}`
}
@ -39,7 +47,10 @@ async function mreq(method, url, body, extra = {}) {
const res = await fetch(baseUrl + url, opts)
const root = await res.json()
if (!res.ok || root.errcode) throw new MatrixServerError(root, {baseUrl, url, ...opts})
if (!res.ok || root.errcode) {
delete opts.headers.Authorization
throw new MatrixServerError(root, {baseUrl, url, ...opts})
}
return root
}