| 171 | // the result to a Base64 string. This is a memory-efficient way to handle |
| 172 | // binary data from a stream before the final encoding. |
| 173 | async function readFileAsBase64Stream(filePath: string): Promise<string> { |
| 174 | return new Promise((resolve, reject) => { |
| 175 | const stream = createReadStream(filePath); |
| 176 | const chunks: Buffer[] = []; |
| 177 | stream.on('data', (chunk) => { |
| 178 | chunks.push(chunk as Buffer); |
| 179 | }); |
| 180 | stream.on('end', () => { |
| 181 | const finalBuffer = Buffer.concat(chunks); |
| 182 | resolve(finalBuffer.toString('base64')); |
| 183 | }); |
| 184 | stream.on('error', (err) => reject(err)); |
| 185 | }); |
| 186 | } |
| 187 | |
| 188 | // Tool registrations |
| 189 | |