clockworklabs/SpacetimeDB · error · RangeError

The encoding label provided is invalid

Error message

The encoding label provided is invalid

What it means

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.

Solutions

  1. Use `new TextDecoder()` with no arguments — the label defaults to 'utf-8'
  2. If passing a label, use the exact string 'utf-8' (lowercase, hyphenated)
  3. Convert non-UTF-8 data to UTF-8 bytes on the client side before sending it to the module
  4. For genuinely non-UTF-8 encodings, implement a custom decoder inside the module — the builtin will not do it

Example fix

// before
const a = new TextDecoder('utf8').decode(bytes);        // RangeError in module
const b = new TextDecoder('utf-16le').decode(bytes);    // RangeError in module

// after
const a = new TextDecoder().decode(bytes);              // utf-8 default
const b = new TextDecoder('utf-8').decode(bytes);       // exact label only
Defensive patterns

Strategy: validation

Validate before calling

function isSupportedLabel(label?: string): boolean {
  return label === undefined || label === 'utf-8';
}

if (!isSupportedLabel(label)) throw new RangeError(`Encoding '${label}' is not supported; only utf-8`);
const dec = new TextDecoder(label);

Try / catch

try {
  const dec = new TextDecoder(label);
} catch (e) {
  if (e instanceof RangeError) {
    // normalize or reject the label before constructing
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@6dee26c6ef (2026-08-20). Data as JSON: /api/errors/59ff33436c4106df. Report an issue: GitHub.

Appendix: source

Thrown at crates/core/src/host/v8/builtins/text_encoding.js:32

  encode(input = '') {
    return utf8_encode(input);
  }
};

globalThis.TextDecoder = class TextDecoder {
  /** @type {string} */
  #encoding;

  /** @type {boolean} */
  #fatal;

  /**
   * @argument {string} label
   * @argument {any} options
   */
  constructor(label = 'utf-8', options = {}) {
    if (label !== 'utf-8') {
      throw new RangeError('The encoding label provided is invalid');
    }
    this.#encoding = label;
    this.#fatal = !!options.fatal;
    if (options.ignoreBOM) {
      throw new TypeError("Option 'ignoreBOM' not supported");
    }
  }

  get encoding() {
    return this.#encoding;
  }
  get fatal() {
    return this.#fatal;
  }
  get ignoreBOM() {
    return false;
  }

View on GitHub (pinned to 6dee26c6ef)