{"record":{"id":"59ff33436c4106df","repo":"clockworklabs/SpacetimeDB","slug":"the-encoding-label-provided-is-invalid","errorCode":null,"errorMessage":"The encoding label provided is invalid","messagePattern":"The encoding label provided is invalid","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"crates/core/src/host/v8/builtins/text_encoding.js","lineNumber":32,"sourceCode":"  encode(input = '') {\n    return utf8_encode(input);\n  }\n};\n\nglobalThis.TextDecoder = class TextDecoder {\n  /** @type {string} */\n  #encoding;\n\n  /** @type {boolean} */\n  #fatal;\n\n  /**\n   * @argument {string} label\n   * @argument {any} options\n   */\n  constructor(label = 'utf-8', options = {}) {\n    if (label !== 'utf-8') {\n      throw new RangeError('The encoding label provided is invalid');\n    }\n    this.#encoding = label;\n    this.#fatal = !!options.fatal;\n    if (options.ignoreBOM) {\n      throw new TypeError(\"Option 'ignoreBOM' not supported\");\n    }\n  }\n\n  get encoding() {\n    return this.#encoding;\n  }\n  get fatal() {\n    return this.#fatal;\n  }\n  get ignoreBOM() {\n    return false;\n  }\n","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/core/src/host/v8/builtins/text_encoding.js#L14-L50","documentation":"The V8 host replaces the platform TextDecoder with a minimal UTF-8-only implementation. The constructor accepts exactly one label, the string 'utf-8' (also the default); anything else — 'utf8', 'UTF-8', 'utf-16le', 'windows-1252', 'latin1' — throws RangeError, mirroring the WHATWG error class for invalid encoding labels. Unlike browsers/Node, label aliases are not normalized.","triggerScenarios":"new TextDecoder('utf8'), new TextDecoder('UTF-8'), new TextDecoder('utf-16le'), new TextDecoder('windows-1252') or any label other than exact lowercase 'utf-8' inside a SpacetimeDB V8 module; passing an encoding name sourced from config or user input.","commonSituations":"Code ported from Node/Deno that decodes latin1/base64-adjacent payloads; shared encoding helpers that default to 'utf8' (no hyphen), which browsers accept but the module host rejects.","solutions":["Use `new TextDecoder()` with no arguments — the label defaults to 'utf-8'","If passing a label, use the exact string 'utf-8' (lowercase, hyphenated)","Convert non-UTF-8 data to UTF-8 bytes on the client side before sending it to the module","For genuinely non-UTF-8 encodings, implement a custom decoder inside the module — the builtin will not do it"],"exampleFix":"// before\nconst a = new TextDecoder('utf8').decode(bytes);        // RangeError in module\nconst b = new TextDecoder('utf-16le').decode(bytes);    // RangeError in module\n\n// after\nconst a = new TextDecoder().decode(bytes);              // utf-8 default\nconst b = new TextDecoder('utf-8').decode(bytes);       // exact label only","handlingStrategy":"validation","validationCode":"function isSupportedLabel(label?: string): boolean {\n  return label === undefined || label === 'utf-8';\n}\n\nif (!isSupportedLabel(label)) throw new RangeError(`Encoding '${label}' is not supported; only utf-8`);\nconst dec = new TextDecoder(label);","typeGuard":null,"tryCatchPattern":"try {\n  const dec = new TextDecoder(label);\n} catch (e) {\n  if (e instanceof RangeError) {\n    // normalize or reject the label before constructing\n  }\n}","preventionTips":["Treat the module host as UTF-8-only; omit the label argument","Convert data to UTF-8 on the client before sending","Keep a shared test that runs encoding helpers inside the module sandbox"],"tags":["textdecoder","encoding","v8","module","typescript"],"backgroundTag":"unsupported-encoding","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}