fix base64 build issue (#4581)

* fix base64 build issue

* remove leading whitespace

* fix import

* add explanation

---------

Co-authored-by: Luke Taylor <service@lukebtaylor.com>
This commit is contained in:
lukebtaylor 2025-06-09 07:23:49 -05:00 committed by GitHub
parent 0c378da0d2
commit 7563205c24
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 45 additions and 20 deletions

View file

@ -1,4 +1,4 @@
FROM node:22.12 as builder
FROM node:22.12 as builder
WORKDIR /app

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -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<ArrayBuffer | SharedArrayBuffer>, byteOffset?: number, length?: number): Buffer;
from(data: WithImplicitCoercion<Uint8Array | readonly number[]>): Buffer;
from(str: WithImplicitCoercion<string>, encoding?: BufferEncoding): Buffer;
alloc(size: number): Buffer;
alloc(size: number, fill: string | Buffer | number): Buffer;
}
type WithImplicitCoercion<T> = 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 {};