{"record":{"id":"128d95e948983d4a","repo":"zloirock/core-js","slug":"invalidcharactererror-128d95","errorCode":"InvalidCharacterError","errorMessage":"The string contains characters outside of the Latin1 range","messagePattern":"The string contains characters outside of the Latin1 range","errorType":"exception","errorClass":"DOMException","httpStatus":null,"severity":"error","filePath":"packages/core-js/modules/web.btoa.js","lineNumber":52,"sourceCode":"$({ global: true, bind: true, enumerable: true, forced: !BASIC || NO_ARG_RECEIVING_CHECK || WRONG_ARG_CONVERSION || WRONG_ARITY }, {\n  btoa: function btoa(data) {\n    validateArgumentsLength(arguments.length, 1);\n    // `webpack` dev server bug on IE global methods - use call(fn, global, ...)\n    if (BASIC) return call($btoa, globalThis, toString(data));\n    var string = toString(data);\n    // (string.length + 2) / 3) and then truncating to integer\n    // does the ceil automatically.  << 2 will truncate the integer\n    // while also doing *4.  ceil(length / 3) quanta, 4 bytes output\n    // per quanta for base64.\n    var output = new $Array((string.length + 2) / 3 << 2);\n    var outputIndex = 0;\n    var position = 0;\n    var map = i2c;\n    var block, charCode;\n    while (charAt(string, position) || (map = '=', position % 1)) {\n      charCode = charCodeAt(string, position += 3 / 4);\n      if (charCode > 0xFF) {\n        throw new (getBuiltIn('DOMException'))('The string contains characters outside of the Latin1 range', 'InvalidCharacterError');\n      }\n      block = block << 8 | charCode;\n      output[outputIndex++] = charAt(map, 63 & block >> 8 - position % 1 * 8);\n    } return join(output, '');\n  }\n});\n","sourceCodeStart":34,"sourceCodeEnd":59,"githubUrl":"https://github.com/zloirock/core-js/blob/84e45fba098dd3a177d5cf2247d06ab8e98d3790/packages/core-js/modules/web.btoa.js#L34-L59","documentation":"core-js's btoa polyfill (packages/core-js/modules/web.btoa.js:52) throws a DOMException 'InvalidCharacterError' when the input string contains a character whose char code exceeds 0xFF, because btoa can only encode Latin1 (byte) strings. This matches native window.btoa semantics: it encodes each character as one byte, so anything above U+00FF is rejected.","triggerScenarios":"Calling `btoa(str)` where str contains any non-Latin1 character: emoji, CJK text, accented characters beyond U+00FF (e.g. 'é' is fine at U+00E9, but 'ā' U+0101, '€' U+20AC, or any emoji are not), typically from user input or decoded JSON.","commonSituations":"Base64-encoding user-generated content that includes Unicode; encoding JSON payloads with internationalized text; strings read from UTF-8 sources and passed directly to btoa; code that worked with ASCII test data but fails with real-world multilingual input.","solutions":["Encode to UTF-8 bytes before btoa: `btoa(String.fromCharCode(...new TextEncoder().encode(str)))` (chunk large strings to avoid argument limits).","Alternatively use the classic trick: `btoa(unescape(encodeURIComponent(str)))`.","If the environment allows, use modern `Buffer.from(str, 'utf-8').toString('base64')` (Node) or fetch-base64 helpers instead of btoa.","Pre-validate with a check that every char code is <= 0xFF if you intentionally require Latin1-only input."],"exampleFix":"// before\nconst encoded = btoa(JSON.stringify(payload));\n// after\nconst bytes = new TextEncoder().encode(JSON.stringify(payload));\nconst latin1 = Array.from(bytes, b => String.fromCharCode(b)).join('');\nconst encoded = btoa(latin1);","handlingStrategy":"validation","validationCode":"function isLatin1(s) {\n  if (typeof s !== 'string') return false;\n  for (let i = 0; i < s.length; i++) {\n    if (s.charCodeAt(i) > 0xFF) return false;\n  }\n  return true;\n}\nif (!isLatin1(input)) {\n  input = String.fromCharCode(...new TextEncoder().encode(input));\n}\nconst encoded = btoa(input);","typeGuard":"function isLatin1String(v) {\n  return typeof v === 'string' && !/[^\\u0000-\\u00ff]/.test(v);\n}","tryCatchPattern":"let encoded;\ntry {\n  encoded = btoa(input);\n} catch (e) {\n  if (e.name === 'InvalidCharacterError') {\n    encoded = btoa(String.fromCharCode(...new TextEncoder().encode(input)));\n  } else {\n    throw e;\n  }\n}","preventionTips":["Always route non-ASCII text through TextEncoder (or encodeURIComponent trick) before btoa.","Treat btoa as a byte-string encoder, not a text encoder; document this in team guidelines.","Test base64 encoding with emoji and multilingual fixtures, not just ASCII.","On Node, prefer Buffer.from(str, 'utf-8').toString('base64') which handles Unicode directly."],"tags":["btoa","base64","unicode","latin1","encoding","polyfill"],"backgroundTag":"invalid-base64-encoding","analyzedSha":"84e45fba098dd3a177d5cf2247d06ab8e98d3790","analyzedAt":"2026-08-30T20:36:10.323Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}