{"id":"bd0b57076a6f91ef","repo":"jestjs/jest","slug":"missing-data-uri-encoding","errorCode":null,"errorMessage":"Missing data URI encoding","messagePattern":"Missing data URI encoding","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-runtime/src/internals/EsmLoader.ts","lineNumber":140,"sourceCode":"  return error;\n}\n\n// Decode a `data:` URI specifier into its mime type and decoded code/body.\n// `application/wasm` returns a Buffer; everything else returns a UTF-8 string.\nconst dataURIRegex =\n  /^data:(?<mime>text\\/javascript|application\\/json|application\\/wasm)(?:;(?<encoding>charset=utf-8|base64))?,(?<code>.*)$/;\n\nfunction parseDataUri(specifier: string): {\n  mime: string;\n  code: string | Buffer;\n} {\n  const match = specifier.match(dataURIRegex);\n  if (!match || !match.groups) {\n    throw new Error('Invalid data URI');\n  }\n  const {mime, encoding, code} = match.groups;\n  if (mime === 'application/wasm') {\n    if (!encoding) throw new Error('Missing data URI encoding');\n    if (encoding !== 'base64') {\n      throw new Error(`Invalid data URI encoding: ${encoding}`);\n    }\n    return {code: Buffer.from(code, 'base64'), mime};\n  }\n  if (!encoding || encoding === 'charset=utf-8') {\n    return {code: decodeURIComponent(code), mime};\n  }\n  if (encoding === 'base64') {\n    return {code: Buffer.from(code, 'base64').toString(), mime};\n  }\n  throw new Error(`Invalid data URI encoding: ${encoding}`);\n}\n\n// Mirrors Node's `validateAttributes` in lib/internal/modules/esm/assert.js.\n// The only deliberate divergence: missing `type: 'json'` warns instead of\n// throwing — see the JSON branch below.\nconst warnedMissingJsonAttributePairs = new Set<string>();","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-runtime/src/internals/EsmLoader.ts#L122-L158","documentation":"Thrown by parseDataUri when the MIME type is `application/wasm` but no encoding segment is present. WASM data URIs must be base64-encoded (binary cannot live in a URL-decoded text body), so Jest refuses to parse one without an explicit `;base64`.","triggerScenarios":"Importing a `data:application/wasm,...` URI without the `;base64` encoding token. WASM bodies are binary and cannot be represented as URL-encoded text, so the encoding is mandatory.","commonSituations":"Hand-writing a WASM data URI and forgetting the encoding; copying a JS data URI pattern and swapping only the MIME to wasm; a code generator that omits the encoding for wasm modules.","solutions":["Add `;base64` to the URI: `data:application/wasm;base64,<base64-encoded-bytes>`.","Generate the URI programmatically: `'data:application/wasm;base64,' + Buffer.from(wasmBytes).toString('base64')`."],"exampleFix":"// before\nimport wasm from 'data:application/wasm,AGFzbQ...';\n\n// after\nimport wasm from 'data:application/wasm;base64,AGFzbQ...';","handlingStrategy":"validation","validationCode":"function buildWasmDataUri(bytes) {\n  if (!bytes || !bytes.length) throw new Error('empty wasm bytes');\n  return 'data:application/wasm;base64,' + Buffer.from(bytes).toString('base64');\n}","typeGuard":"function isBase64WasmDataUri(s: string): boolean {\n  return /^data:application\\/wasm;base64,/.test(s);\n}","tryCatchPattern":null,"preventionTips":["Always include `;base64` for wasm data URIs.","Generate wasm URIs from a Buffer/Uint8Array via base64 encoding.","Never hand-type binary content into a data URI."],"tags":["esm","data-uri","wasm","encoding"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}