(args: { id: string; path: string; mimeType: string; bytes: Uint8Array; fileName?: string | null })
| 60 | } |
| 61 | |
| 62 | export function registerGeneratedImage(args: { id: string; path: string; mimeType: string; bytes: Uint8Array; fileName?: string | null }): GeneratedImageMetadata { |
| 63 | const content = Buffer.from(args.bytes) |
| 64 | if (content.byteLength > MAX_GENERATED_IMAGE_BYTES) { |
| 65 | throw new Error('Image is too large to display inline') |
| 66 | } |
| 67 | |
| 68 | const previous = generatedImages.get(args.id) |
| 69 | if (previous) { |
| 70 | generatedImageBytes -= previous.content.byteLength |
| 71 | } |
| 72 | |
| 73 | const metadata: GeneratedImageMetadata = { |
| 74 | id: args.id, |
| 75 | fileName: args.fileName || basename(args.path) || `${args.id}.png`, |
| 76 | content, |
| 77 | mimeType: args.mimeType, |
| 78 | createdAt: Date.now() |
| 79 | } |
| 80 | generatedImages.set(args.id, metadata) |
| 81 | generatedImageBytes += content.byteLength |
| 82 | |
| 83 | evictOldGeneratedImages() |
| 84 | |
| 85 | return metadata |
| 86 | } |
| 87 | |
| 88 | function evictOldGeneratedImages(): void { |
| 89 | while (generatedImages.size > MAX_GENERATED_IMAGE_COUNT || generatedImageBytes > MAX_GENERATED_IMAGE_TOTAL_BYTES) { |
no test coverage detected