Update dependencies, make stream-type independent

This commit is contained in:
Cadence Ember
2026-03-14 14:25:48 +13:00
parent ff022e8793
commit f90cdfdbb5
6 changed files with 62 additions and 163 deletions

View File

@@ -977,6 +977,21 @@ test("message2event: written @mentions do not match in inline code", async t =>
}])
})
test("message2event: written @mentions do not match in code block", async t => {
const events = await messageToEvent({
...data.message.advanced_written_at_mention_for_matrix,
content: "```java\npublic @Nullable EntityType<?>\n```"
}, data.guild.general, {}, {})
t.deepEqual(events, [{
$type: "m.room.message",
"m.mentions": {},
msgtype: "m.text",
body: "```java\npublic @Nullable EntityType<?>\n```",
format: "org.matrix.custom.html",
formatted_body: `<pre><code class="language-java">public @Nullable EntityType&lt;?&gt;</code></pre>`
}])
})
test("message2event: entire message may match elaborate display name", async t => {
let called = 0
const events = await messageToEvent({

View File

@@ -9,7 +9,7 @@ const sharp = require("sharp")
const api = sync.require("../../matrix/api")
/** @type {import("../../matrix/mreq")} */
const mreq = sync.require("../../matrix/mreq")
const streamMimeType = require("stream-mime-type")
const {streamType} = require("@cloudrac3r/stream-type")
const WIDTH = 160
const HEIGHT = 160
@@ -26,13 +26,13 @@ async function getAndResizeSticker(mxc) {
}
const streamIn = Readable.fromWeb(res.body)
const { stream, mime } = await streamMimeType.getMimeType(streamIn)
const animated = ["image/gif", "image/webp"].includes(mime)
const {streamThrough, type} = await streamType(streamIn)
const animated = ["image/gif", "image/webp"].includes(type)
const transformer = sharp({animated: animated})
.resize(WIDTH, HEIGHT, {fit: "inside", background: {r: 0, g: 0, b: 0, alpha: 0}})
.webp()
stream.pipe(transformer)
streamThrough.pipe(transformer)
return Readable.toWeb(transformer)
}

View File

@@ -6,7 +6,7 @@ const {pipeline} = require("stream").promises
const sharp = require("sharp")
const {GIFrame} = require("@cloudrac3r/giframe")
const {PNG} = require("@cloudrac3r/pngjs")
const streamMimeType = require("stream-mime-type")
const {streamType} = require("@cloudrac3r/stream-type")
const SIZE = 48
const RESULT_WIDTH = 400
@@ -54,11 +54,11 @@ async function compositeMatrixEmojis(mxcs, mxcDownloader) {
* @returns {Promise<Buffer | undefined>} Uncompressed PNG image
*/
async function convertImageStream(streamIn, stopStream) {
const {stream, mime} = await streamMimeType.getMimeType(streamIn)
assert(["image/png", "image/jpeg", "image/webp", "image/gif", "image/apng"].includes(mime), `Mime type ${mime} is impossible for emojis`)
const {streamThrough, type} = await streamType(streamIn)
assert(["image/png", "image/jpeg", "image/webp", "image/gif", "image/apng"].includes(type), `Mime type ${type} is impossible for emojis`)
try {
if (mime === "image/png" || mime === "image/jpeg" || mime === "image/webp") {
if (type === "image/png" || type === "image/jpeg" || type === "image/webp") {
/** @type {{info: sharp.OutputInfo, buffer: Buffer}} */
const result = await new Promise((resolve, reject) => {
const transformer = sharp()
@@ -70,15 +70,15 @@ async function convertImageStream(streamIn, stopStream) {
resolve({info, buffer})
})
pipeline(
stream,
streamThrough,
transformer
)
})
return result.buffer
} else if (mime === "image/gif") {
} else if (type === "image/gif") {
const giframe = new GIFrame(0)
stream.on("data", chunk => {
streamThrough.on("data", chunk => {
giframe.feed(chunk)
})
const frame = await giframe.getFrame()
@@ -91,10 +91,10 @@ async function convertImageStream(streamIn, stopStream) {
.toBuffer({resolveWithObject: true})
return buffer.data
} else if (mime === "image/apng") {
} else if (type === "image/apng") {
const png = new PNG({maxFrames: 1})
// @ts-ignore
stream.pipe(png)
streamThrough.pipe(png)
/** @type {Buffer} */ // @ts-ignore
const frame = await new Promise(resolve => png.on("parsed", resolve))
stopStream()