From 7563205c24e5c74ef8dc4b4bbbe25854dc04a0cd Mon Sep 17 00:00:00 2001 From: lukebtaylor <21993084+lukebtaylor@users.noreply.github.com> Date: Mon, 9 Jun 2025 07:23:49 -0500 Subject: [PATCH] fix base64 build issue (#4581) * fix base64 build issue * remove leading whitespace * fix import * add explanation --------- Co-authored-by: Luke Taylor --- packages/local-mail-watcher/Dockerfile | 2 +- packages/local-mail-watcher/src/lib/base64.js | 19 --------------- packages/local-mail-watcher/src/lib/base64.ts | 21 +++++++++++++++++ .../src/lib/node-buffer.d.ts | 23 +++++++++++++++++++ 4 files changed, 45 insertions(+), 20 deletions(-) delete mode 100644 packages/local-mail-watcher/src/lib/base64.js create mode 100644 packages/local-mail-watcher/src/lib/base64.ts create mode 100644 packages/local-mail-watcher/src/lib/node-buffer.d.ts diff --git a/packages/local-mail-watcher/Dockerfile b/packages/local-mail-watcher/Dockerfile index 7dfee57df..373d61bcf 100644 --- a/packages/local-mail-watcher/Dockerfile +++ b/packages/local-mail-watcher/Dockerfile @@ -1,4 +1,4 @@ - FROM node:22.12 as builder +FROM node:22.12 as builder WORKDIR /app diff --git a/packages/local-mail-watcher/src/lib/base64.js b/packages/local-mail-watcher/src/lib/base64.js deleted file mode 100644 index 13e117625..000000000 --- a/packages/local-mail-watcher/src/lib/base64.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Utility functions for handling base64 encoding/decoding - */ - -/** - * Decodes a base64 string to UTF-8 text - * Uses Node.js Buffer but with type safety - */ -export function decodeBase64(base64String: string): string { - try { - // Use a type-safe approach without relying on global - // @ts-ignore - Ignore TypeScript error for Buffer - return Buffer.from(base64String, 'base64').toString('utf-8'); - } catch (error) { - console.error('Error decoding base64 string:', error); - return base64String; // Return original string if decoding fails - } - } - \ No newline at end of file diff --git a/packages/local-mail-watcher/src/lib/base64.ts b/packages/local-mail-watcher/src/lib/base64.ts new file mode 100644 index 000000000..9cd01df30 --- /dev/null +++ b/packages/local-mail-watcher/src/lib/base64.ts @@ -0,0 +1,21 @@ +/** + * Utility functions for handling base64 encoding/decoding + */ + +// Node.js Buffer is globally available +// Only import type information - no runtime import +// This will be removed during compilation +import type {} from '../types/node-buffer'; + +/** + * Decodes a base64 string to UTF-8 text + * Uses Node.js Buffer with type safety + */ +export function decodeBase64(base64String: string): string { + try { + return Buffer.from(base64String, 'base64').toString('utf-8'); + } catch (error) { + console.error('Error decoding base64 string:', error); + return base64String; // Return original string if decoding fails + } +} diff --git a/packages/local-mail-watcher/src/lib/node-buffer.d.ts b/packages/local-mail-watcher/src/lib/node-buffer.d.ts new file mode 100644 index 000000000..9184c1ad8 --- /dev/null +++ b/packages/local-mail-watcher/src/lib/node-buffer.d.ts @@ -0,0 +1,23 @@ +declare global { + interface Buffer extends Uint8Array { + write(string: string, encoding?: BufferEncoding): number; + toString(encoding?: BufferEncoding, start?: number, end?: number): string; + } + + interface BufferConstructor { + from(arrayBuffer: WithImplicitCoercion, byteOffset?: number, length?: number): Buffer; + from(data: WithImplicitCoercion): Buffer; + from(str: WithImplicitCoercion, encoding?: BufferEncoding): Buffer; + alloc(size: number): Buffer; + alloc(size: number, fill: string | Buffer | number): Buffer; + } + + type WithImplicitCoercion = T | { valueOf(): T }; + + type BufferEncoding = 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'latin1' | 'binary' | 'hex'; + + const Buffer: BufferConstructor; +} + +// This empty export makes TypeScript treat this file as a module +export {};