summary refs log tree commit diff
path: root/src/api
diff options
context:
space:
mode:
authorTomatoCake <60300461+DEVTomatoCake@users.noreply.github.com>2024-06-28 12:43:53 +0200
committerTomatoCake <60300461+DEVTomatoCake@users.noreply.github.com>2024-06-28 12:43:53 +0200
commita987671e4a1249ae23914165c1b3edd0f29d9ffd (patch)
treea9a1ae7f2b70d5dccb7158b0a8b5f8455dd4d64e /src/api
parentAdd hashes.json to .prettierignore as this is a generated file (diff)
downloadserver-ts-a987671e4a1249ae23914165c1b3edd0f29d9ffd.tar.xz
"Fix" jimp import typings
Diffstat (limited to 'src/api')
-rw-r--r--src/api/middlewares/ImageProxy.ts21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/api/middlewares/ImageProxy.ts b/src/api/middlewares/ImageProxy.ts

index 4c324afd8..27c69ae2a 100644 --- a/src/api/middlewares/ImageProxy.ts +++ b/src/api/middlewares/ImageProxy.ts
@@ -16,14 +16,22 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import { Config } from "@spacebar/util"; +import { Config, JimpType } from "@spacebar/util"; import { Request, Response } from "express"; import { yellow } from "picocolors"; import crypto from "crypto"; import fetch from "node-fetch"; let sharp: undefined | false | { default: typeof import("sharp") } = undefined; -let Jimp: undefined | false | typeof import("jimp") = undefined; + +let Jimp: JimpType | undefined = undefined; +try { + Jimp = require("jimp") as JimpType; +} catch { + // empty +} + +let sentImageProxyWarning = false; const sharpSupported = new Set([ "image/jpeg", @@ -112,20 +120,21 @@ export async function ImageProxy(req: Request, res: Response) { const arrayBuffer = await request.arrayBuffer(); let resultBuffer = Buffer.from(arrayBuffer); - if (/^\d+x\d+$/.test(path[1]) && resizeSupported.has(contentType)) { + if (!sentImageProxyWarning && resizeSupported.has(contentType) && /^\d+x\d+$/.test(path[1])) { if (sharp !== false) { try { sharp = await import("sharp"); - } catch (e) { + } catch { sharp = false; } } - if (sharp === false && Jimp !== false) { + + if (sharp === false && !Jimp) { try { // @ts-expect-error Typings don't fit Jimp = await import("jimp"); } catch { - Jimp = false; + sentImageProxyWarning = true; console.log( `[ImageProxy] ${yellow( 'Neither "sharp" or "jimp" NPM packages are installed, image resizing will be disabled',