mirror of
https://gitdab.com/cadence/out-of-your-element.git
synced 2025-09-10 20:32:50 +02:00
m->d: fix image captions spec, fix upload issues
This commit is contained in:
parent
050cc9cee9
commit
a8670323a0
10 changed files with 203 additions and 97 deletions
|
@ -1,9 +1,9 @@
|
|||
// @ts-check
|
||||
|
||||
const mixin = require("@cloudrac3r/mixin-deep")
|
||||
const stream = require("stream")
|
||||
const streamWeb = require("stream/web")
|
||||
const getStream = require("get-stream")
|
||||
const {buffer} = require("stream/consumers")
|
||||
const mixin = require("@cloudrac3r/mixin-deep")
|
||||
|
||||
const {reg, writeRegistration} = require("./read-registration.js")
|
||||
|
||||
|
@ -19,20 +19,33 @@ class MatrixServerError extends Error {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {undefined | string | object | streamWeb.ReadableStream | stream.Readable} body
|
||||
* @returns {Promise<string | streamWeb.ReadableStream | stream.Readable | Buffer>}
|
||||
*/
|
||||
async function _convertBody(body) {
|
||||
if (body == undefined || Object.is(body.constructor, Object)) {
|
||||
return JSON.stringify(body) // almost every POST request is going to follow this one
|
||||
} else if (body instanceof stream.Readable && reg.ooye.content_length_workaround) {
|
||||
return await buffer(body) // content length workaround is set, so convert to buffer. the buffer consumer accepts node streams.
|
||||
} else if (body instanceof stream.Readable) {
|
||||
return stream.Readable.toWeb(body) // native fetch can only consume web streams
|
||||
} else if (body instanceof streamWeb.ReadableStream && reg.ooye.content_length_workaround) {
|
||||
return await buffer(body) // content lenght workaround is set, so convert to buffer. the buffer consumer accepts async iterables, which web streams are.
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
/* c8 ignore start */
|
||||
|
||||
/**
|
||||
* @param {string} method
|
||||
* @param {string} url
|
||||
* @param {string | object | streamWeb.ReadableStream | stream.Readable} [body]
|
||||
* @param {string | object | streamWeb.ReadableStream | stream.Readable} [bodyIn]
|
||||
* @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)
|
||||
} else if (body instanceof streamWeb.ReadableStream && reg.ooye.content_length_workaround) {
|
||||
body = await stream.consumers.buffer(stream.Readable.fromWeb(body))
|
||||
}
|
||||
async function mreq(method, url, bodyIn, extra = {}) {
|
||||
const body = await _convertBody(bodyIn)
|
||||
|
||||
/** @type {RequestInit} */
|
||||
const opts = mixin({
|
||||
|
@ -86,3 +99,4 @@ module.exports.MatrixServerError = MatrixServerError
|
|||
module.exports.baseUrl = baseUrl
|
||||
module.exports.mreq = mreq
|
||||
module.exports.withAccessToken = withAccessToken
|
||||
module.exports._convertBody = _convertBody
|
||||
|
|
47
src/matrix/mreq.test.js
Normal file
47
src/matrix/mreq.test.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
// @ts-check
|
||||
|
||||
const assert = require("assert")
|
||||
const stream = require("stream")
|
||||
const streamWeb = require("stream/web")
|
||||
const {buffer} = require("stream/consumers")
|
||||
const {test} = require("supertape")
|
||||
const {_convertBody} = require("./mreq")
|
||||
const {reg} = require("./read-registration")
|
||||
|
||||
async function *generator() {
|
||||
yield "a"
|
||||
yield "b"
|
||||
}
|
||||
|
||||
reg.ooye.content_length_workaround = false
|
||||
|
||||
test("convert body: converts object to string", async t => {
|
||||
t.equal(await _convertBody({a: "1"}), `{"a":"1"}`)
|
||||
})
|
||||
|
||||
test("convert body: leaves undefined as undefined", async t => {
|
||||
t.equal(await _convertBody(undefined), undefined)
|
||||
})
|
||||
|
||||
test("convert body: leaves web readable as web readable", async t => {
|
||||
const webReadable = stream.Readable.toWeb(stream.Readable.from(generator()))
|
||||
t.equal(await _convertBody(webReadable), webReadable)
|
||||
})
|
||||
|
||||
test("convert body: converts node readable to web readable (for native fetch upload)", async t => {
|
||||
const readable = stream.Readable.from(generator())
|
||||
const webReadable = await _convertBody(readable)
|
||||
assert(webReadable instanceof streamWeb.ReadableStream)
|
||||
t.deepEqual(await buffer(webReadable), Buffer.from("ab"))
|
||||
})
|
||||
|
||||
test("convert body: converts node readable to buffer", async t => {
|
||||
reg.ooye.content_length_workaround = true
|
||||
const readable = stream.Readable.from(generator())
|
||||
t.deepEqual(await _convertBody(readable), Buffer.from("ab"))
|
||||
})
|
||||
|
||||
test("convert body: converts web readable to buffer", async t => {
|
||||
const webReadable = stream.Readable.toWeb(stream.Readable.from(generator()))
|
||||
t.deepEqual(await _convertBody(webReadable), Buffer.from("ab"))
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue