export async function compressText(_text: string): Promise<Uint8Array> { // TODO: // 1) TextEncoder로 text → Uint8Array (UTF-8) // 2) Blob 또는 ReadableStream으로 만들어서 CompressionStream('gzip')으로 pipe // 3) 결과를 Response로 받아 arrayBuffer() → Uint8Array 반환 throw new Error('Not implemented'); } export async function decompressText(_bytes: Uint8Array): Promise<string> { // TODO: // 1) Blob([bytes]).stream() → pipeThrough(DecompressionStream('gzip')) // 2) Response.text() 로 문자열 얻기 throw new Error('Not implemented'); } // 데모용 React 컴포넌트 import { useState } from 'react'; export function CompressionDemo() { const [text, setText] = useState('hello hello hello hello hello world! '.repeat(50)); const [stats, setStats] = useState<{ orig: number; compressed: number } | null>(null); async function run() { const compressed = await compressText(text); const restored = await decompressText(compressed); setStats({ orig: new TextEncoder().encode(text).byteLength, compressed: compressed.byteLength, }); if (restored !== text) console.error('Round-trip failed'); } return ( <div> <textarea data-testid="input" value={text} onChange={(e) => setText(e.target.value)} rows={6} cols={60} /> <button data-testid="compress" onClick={run}> Compress </button> {stats && ( <p data-testid="stats"> {stats.orig} bytes → {stats.compressed} bytes ({Math.round((stats.compressed / stats.orig) * 100)}%) </p> )} </div> ); }
Tests