{"record":{"id":"9852ea8a9055fad6","repo":"vitest-dev/vitest","slug":"invalid-data-uri-encoding-encoding","errorCode":null,"errorMessage":"Invalid data URI encoding: ${encoding}","messagePattern":"Invalid data URI encoding: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/vm/esm-executor.ts","lineNumber":48,"sourceCode":"  return module instanceof SourceTextModule && module.hasAsyncGraph()\n}\n\nconst dataURIRegex\n  = /^data:(?<mime>text\\/javascript|application\\/json|application\\/wasm)(?:;(?<encoding>charset=utf-8|base64))?,(?<code>.*)$/\n\nfunction parseDataUri(identifier: string): { mime: string; code: string | Buffer } {\n  const match = identifier.match(dataURIRegex)\n  if (!match || !match.groups) {\n    throw new Error('Invalid data URI')\n  }\n  const { mime, encoding } = match.groups\n  let code: string | Buffer = match.groups.code\n  if (mime === 'application/wasm') {\n    if (!encoding) {\n      throw new Error('Missing data URI encoding')\n    }\n    if (encoding !== 'base64') {\n      throw new Error(`Invalid data URI encoding: ${encoding}`)\n    }\n    return { mime, code: Buffer.from(code, 'base64') }\n  }\n  if (!encoding || encoding === 'charset=utf-8') {\n    code = decodeURIComponent(code)\n  }\n  else if (encoding === 'base64') {\n    code = Buffer.from(code, 'base64').toString()\n  }\n  else {\n    throw new Error(`Invalid data URI encoding: ${encoding}`)\n  }\n  return { mime, code }\n}\n\nfunction getContextExecutor(mod: VMModule): ExternalModulesExecutor {\n  const vmContext = (mod.context as any)?.[VITEST_VM_CONTEXT_SYMBOL]\n  if (!vmContext) {","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/vm/esm-executor.ts#L30-L66","documentation":"Thrown by parseDataUri (line 48) only for WebAssembly data URIs: the MIME is `application/wasm`, an encoding IS present, but it is not `base64`. Vitest supports exclusively base64-encoded wasm data URIs, so any other encoding value (e.g. `charset=utf-8`) for wasm is rejected here, before attempting to decode binary.","triggerScenarios":"Importing a wasm module via a data URI with a non-base64 encoding segment, e.g. `data:application/wasm;charset=utf-8,...`. This is invalid because wasm bytes cannot be represented as UTF-8 text in a URI.","commonSituations":"Mis-generating a wasm URI with the wrong encoding token; concatenating a JS-style `charset=utf-8` header onto a wasm payload; tooling that defaults all data URIs to the same encoding.","solutions":["Switch the encoding to base64 for wasm: `data:application/wasm;base64,...`.","If you have raw text you think is wasm, you likely need real binary bytes base64-encoded, not a text encoding.","Load wasm from a file URL to avoid encoding pitfalls entirely."],"exampleFix":"// before\nimport wasm from 'data:application/wasm;charset=utf-8,...'\n\n// after\nimport wasm from 'data:application/wasm;base64,<base64>'","handlingStrategy":"validation","validationCode":"function validWasmDataUri(uri: string): boolean {\n  return /^data:application\\/wasm;base64,.+$/.test(uri)\n}","typeGuard":"function isBase64WasmDataUri(uri: string): boolean {\n  return /^data:application\\/wasm;base64,[A-Za-z0-9+/=]+$/.test(uri)\n}","tryCatchPattern":null,"preventionTips":["For wasm data URIs, use base64 exclusively.","Reject non-base64 encodings early in URI-building helpers.","Document the wasm-must-be-base64 rule in any asset-emitting plugin."],"tags":["vm","esm","data-uri","wasm","validation"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}