From c314ba936fd590dd79628a3e95ff4f7302cae37e Mon Sep 17 00:00:00 2001 From: Hajin Lim Date: Tue, 9 Nov 2021 21:48:20 -0300 Subject: [PATCH] feat: WidgetMakeOptions#convertOptions and HEIF image widget support (#128) --- src/managers/WidgetManager.ts | 20 ++++++++++++++------ src/utils/types.ts | 3 ++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/managers/WidgetManager.ts b/src/managers/WidgetManager.ts index 90e6b0c..7316de8 100644 --- a/src/managers/WidgetManager.ts +++ b/src/managers/WidgetManager.ts @@ -111,11 +111,13 @@ export class WidgetManager { target: options.target, type: options.type } - + const key = createHash("sha256").update(JSON.stringify(cacheKey)).digest("hex") - const cache = this.cache.get(key) - if (!fetchOptions?.force && cache) return cache + if (!fetchOptions?.force && cache) { + const cache = this.cache.get(key) + return cache + } for (const queryOption of queryOptions) { const value = options[queryOption]?.toString?.() || options[queryOption] as string @@ -146,13 +148,19 @@ export class WidgetManager { switch (options?.format) { case "jpeg": case "jpg": - converted = sh(buffer).jpeg().toBuffer() + converted = sh(buffer).jpeg(options.convertOptions).toBuffer() break case "png": - converted = sh(buffer).png().toBuffer() + converted = sh(buffer).png(options.convertOptions).toBuffer() break case "webp": - converted = sh(buffer).webp().toBuffer() + converted = sh(buffer).webp(options.convertOptions).toBuffer() + break + case "avif": + converted = sh(buffer).avif(options.convertOptions).toBuffer() + break + case "heif": + converted = sh(buffer).heif(options.convertOptions).toBuffer() break case "avif": converted = sh(buffer).avif().toBuffer() diff --git a/src/utils/types.ts b/src/utils/types.ts index a758c77..6498485 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -14,7 +14,7 @@ export type Nullable = T | null export type WidgetType = "status" | "servers" | "votes" export type WidgetTarget = "bots" export type WidgetStyle = "classic" | "flat" -export type WidgetFormat = "webp" | "png" | "jpg" | "jpeg" | "svg" | "avif" +export type WidgetFormat = "webp" | "png" | "jpg" | "jpeg" | "svg" | "avif" | "heif" export interface KoreanbotsClientOptions extends ClientOptions { koreanbots: Omit & { clientID?: string } @@ -33,6 +33,7 @@ export interface WidgetOptions { scale?: number icon?: boolean format?: WidgetFormat + convertOptions?: Record } export interface FetchOptions {