Skip to content

Conversation

@oleiade
Copy link
Contributor

@oleiade oleiade commented Nov 21, 2025

What?

Add support for text encoding/decoding operations via a new k6/experimental/encoding module. This brings the WHATWG Encoding API standard to k6, implementing the TextEncoder and TextDecoder interfaces for converting between strings and byte arrays.

This code is imported from https://github.com/oleiade/xk6-encoding with modifications to integrate it as a built-in experimental module.

Key Changes

  • New module: k6/experimental/encoding with TextEncoder and TextDecoder classes
  • Supported encodings: UTF-8, UTF-16LE, UTF-16BE
  • Features:
    • UTF-8 encoding via TextEncoder
    • Multi-encoding decoding via TextDecoder with configurable error handling
    • Streaming support for chunked data processing
    • BOM (Byte Order Mark) handling
    • Fatal and non-fatal decoding modes
  • Dependencies: Adds golang.org/x/text v0.31.0
  • Tests: Comprehensive test suite based on Web Platform Tests (WPT)
  • Example: Added usage example in examples/experimental/encoding.js

Usage example

  import encoding from 'k6/experimental/encoding';

  // Encoding
  const encoder = new encoding.TextEncoder();
  const encoded = encoder.encode('Hello, 世界! 🌍');
  console.log(encoded); // Uint8Array with UTF-8 bytes

  // Decoding
  const decoder = new encoding.TextDecoder('utf-8');
  const decoded = decoder.decode(encoded);
  console.log(decoded); // "Hello, 世界! 🌍"

  // Streaming decoding
  const streamDecoder = new encoding.TextDecoder('utf-8');
  const chunk1 = streamDecoder.decode(part1, { stream: true });
  const chunk2 = streamDecoder.decode(part2, { stream: true });
  const final = streamDecoder.decode(part3); // stream: false (default)

Implementation limitations

As documented in the original xk6-encoding extension:

  1. Limited encoding support: Only UTF-8, UTF-16LE, and UTF-16BE are implemented. Legacy encodings from the Encoding Living Standard (ISO-8859-*, Windows code pages, etc.) are intentionally out of scope.
  2. API surface:
    - TextEncoder always outputs UTF-8 (per spec)
    - Streaming helper interfaces like TextDecoderStream are not exposed; use repeated decode() calls with {stream: true} instead
  3. Fatal mode: When fatal: true is set, the decoder throws a TypeError on the first invalid byte sequence

Must do before going out of draft

  • Automate Web Platform Tests import, in the same fashion as what's done for webcrypto.

Checklist

  • I have performed a self-review of my code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added tests for my changes.
  • I have run linter and tests locally (make check) and all pass.
  • I have added the correct milestone and labels to the PR.
  • I have updated the release notes: link
  • I have updated or added an issue to the k6-documentation: grafana/k6-docs#NUMBER if applicable
  • I have updated or added an issue to the TypeScript definitions: grafana/k6-DefinitelyTyped#NUMBER if applicable

Related PR(s)/Issue(s)

closes #2440

@oleiade oleiade self-assigned this Nov 21, 2025
@oleiade oleiade temporarily deployed to azure-trusted-signing November 21, 2025 08:17 — with GitHub Actions Inactive
@oleiade oleiade temporarily deployed to azure-trusted-signing November 21, 2025 08:19 — with GitHub Actions Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TextDecoder and TextEncoder Web APIs

1 participant