Work around the Content-Length issue
This commit is contained in:
parent
8987107685
commit
845f93e5d0
7 changed files with 40 additions and 3 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue