Initial commit: Text encoding component with UTF-8 polyfills

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Chris Daßler
2025-08-29 14:54:44 +02:00
commit 3342f7e40b
10 changed files with 1430 additions and 0 deletions

89
TextEncodingService.ts Normal file
View File

@@ -0,0 +1,89 @@
/**
* Text Encoding Service Implementation
*
* Provides shared encoder/decoder instances with automatic polyfill fallback
*
* @module text-encoding@1.0.0
*/
import type { ITextDecoder, ITextEncoder, ITextEncodingService } from './interfaces';
import { TextDecoderPolyfill } from './TextDecoderPolyfill';
import { TextEncoderPolyfill } from './TextEncoderPolyfill';
export class TextEncodingService implements ITextEncodingService {
readonly encoder: ITextEncoder;
readonly decoder: ITextDecoder;
constructor() {
// Check for native support and use it if available
if (this.hasNativeSupport()) {
// Use native implementations if available
// @ts-expect-error - TextEncoder might exist globally
this.encoder =
typeof TextEncoder !== 'undefined' ? new TextEncoder() : new TextEncoderPolyfill();
// @ts-expect-error - TextDecoder might exist globally
this.decoder =
typeof TextDecoder !== 'undefined' ? new TextDecoder() : new TextDecoderPolyfill();
} else {
// Use polyfills
this.encoder = new TextEncoderPolyfill();
this.decoder = new TextDecoderPolyfill();
}
}
/**
* Check if native TextEncoder/TextDecoder are available
*/
private hasNativeSupport(): boolean {
// @ts-expect-error - Check global scope
return typeof TextEncoder !== 'undefined' && typeof TextDecoder !== 'undefined';
}
/**
* Encode string to UTF-8 bytes
*/
encode(text: string): Uint8Array {
return this.encoder.encode(text);
}
/**
* Decode bytes to string
*/
decode(bytes: Uint8Array | ArrayBuffer | number[]): string {
if (Array.isArray(bytes)) {
// Convert number array to Uint8Array
return this.decoder.decode(new Uint8Array(bytes));
}
return this.decoder.decode(bytes as Uint8Array | ArrayBuffer);
}
/**
* Convenience method: string to UTF-8
*/
stringToUtf8(text: string): Uint8Array {
return this.encode(text);
}
/**
* Convenience method: UTF-8 to string
*/
utf8ToString(bytes: Uint8Array | number[]): string {
return this.decode(bytes);
}
}
/**
* Singleton instance for shared use
*/
let serviceInstance: TextEncodingService | null = null;
/**
* Get or create the singleton service instance
*/
export function getTextEncodingService(): ITextEncodingService {
if (!serviceInstance) {
serviceInstance = new TextEncodingService();
}
return serviceInstance;
}