{"record":{"id":"7d17d28b93237fe4","repo":"can1357/oh-my-pi","slug":"unsupported-image-encoding-encoding","errorCode":null,"errorMessage":"Unsupported image encoding: ${encoding}","messagePattern":"Unsupported image encoding: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/utils/src/docx/converter.ts","lineNumber":393,"sourceCode":"\tconst memberPath = relationshipPath(relationship.target);\n\tconst bytes = context.entries.get(memberPath);\n\tif (!bytes) {\n\t\tcontext.messages.push({ type: \"warning\", message: `Could not find image ${memberPath}` });\n\t\treturn \"\";\n\t}\n\tconst documentProperties = descendants(element, \"docPr\")[0];\n\tconst altText = attribute(documentProperties, \"descr\") ?? attribute(documentProperties, \"title\") ?? \"\";\n\tconst extension = path.posix.extname(memberPath).toLowerCase();\n\tconst contentType =\n\t\tcontext.contentTypes.get(memberPath) ??\n\t\tcontext.contentTypes.get(extension) ??\n\t\tIMAGE_CONTENT_TYPE_BY_EXTENSION[extension.slice(1)] ??\n\t\t\"application/octet-stream\";\n\tconst image: DocxImage = {\n\t\tcontentType,\n\t\taltText,\n\t\tasync read(encoding: \"base64\"): Promise<string> {\n\t\t\tif (encoding !== \"base64\") throw new Error(`Unsupported image encoding: ${encoding}`);\n\t\t\treturn Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString(\"base64\");\n\t\t},\n\t};\n\tconst converted = await context.convertImage.convert(image);\n\tconst attributes = Object.entries(converted)\n\t\t.filter((entry): entry is [string, string] => typeof entry[1] === \"string\")\n\t\t.sort(([left], [right]) => left.localeCompare(right))\n\t\t.map(([name, value]) => `${name}=\"${escapeAttribute(value)}\"`)\n\t\t.join(\" \");\n\treturn attributes ? `<img ${attributes} />` : \"<img />\";\n}\n\nfunction renderFootnoteReference(element: XmlElement, context: ConversionContext): string {\n\tconst id = attribute(element, \"w:id\");\n\tif (!id || !context.footnotes.has(id)) return \"\";\n\tlet ordinal = context.footnoteOrdinals.get(id);\n\tif (ordinal === undefined) {\n\t\tordinal = context.usedFootnotes.length + 1;","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/utils/src/docx/converter.ts#L375-L411","documentation":"Embedded DOCX images expose a read(encoding) method that only supports \"base64\". Any other encoding string is rejected with this error before the image bytes are converted. The mammoth conversion pipeline calls read(\"base64\") internally, so this only fires when custom image conversion code passes a different encoding.","triggerScenarios":"Calling the DocxImage.read() method with an encoding other than \"base64\" (e.g. \"utf8\", \"hex\", \"binary\"), or supplying a custom convertImage handler that invokes image.read() with a non-base64 encoding argument.","commonSituations":"Writing a custom mammoth convertImage transform that defaults to \"utf8\"; assuming read() accepts Buffer encodings like \"hex\" for hashing; copying mammoth examples that use \"data-uri\" style encodings.","solutions":["Call read(\"base64\") — it is the only supported encoding; decode the returned base64 yourself if you need raw bytes.","For raw bytes, use Buffer.from(await image.read(\"base64\"), \"base64\") instead of asking read() for another encoding.","If you control the custom convertImage transform, hard-code the encoding argument to \"base64\"."],"exampleFix":"// before\nconst data = await image.read(\"utf8\"); // throws\n\n// after\nconst b64 = await image.read(\"base64\");\nconst bytes = Buffer.from(b64, \"base64\");","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isBase64Encoding(encoding: string): encoding is \"base64\" {\n  return encoding === \"base64\";\n}\nif (!isBase64Encoding(encoding)) throw new Error(\"only base64 is supported\");","tryCatchPattern":"let data: string;\ntry {\n  data = await image.read(encoding);\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith(\"Unsupported image encoding\")) {\n    data = await image.read(\"base64\");\n  } else throw err;\n}","preventionTips":["Always call read(\"base64\") — it is the only supported encoding.","Convert base64 to Buffer/bytes yourself for other representations.","In custom mammoth convertImage transforms, hard-code \"base64\" as the argument.","Type the parameter as the literal \"base64\" so TypeScript rejects other strings at compile time."],"tags":["docx","image","encoding","api-misuse"],"backgroundTag":"unsupported-encoding","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}