Issue 243 Text Encoder Dependency Removal Design
Goal: Remove the deprecated @sinonjs/text-encoding dependency from fake XHR response handling without changing observable behavior for supported runtimes.
Context
Issue #243 was opened when the project still depended on an older jsdom version and a deprecated text-encoding polyfill. The current branch already uses jsdom@28, so the remaining work is narrower than the original issue title suggests: lib/fake-xhr/index.js still imports @sinonjs/text-encoding only to turn response strings into UTF-8 bytes for arraybuffer and blob responses.
The issue comments also point out that the current usage is conceptually wrong: TextEncoder is UTF-8 only, but the code passes an encoding argument as if alternate encodings were supported. In practice, the implementation already behaves as UTF-8-only, so the correct fix is to make that explicit and remove the dead abstraction.
Chosen Approach
Use the smallest dependency-free fix:
- replace the
GlobalTextEncoderfallback with a small local helper that converts a JavaScript string to a UTF-8ArrayBuffer; - keep
convertToArrayBufferas the single production call site for this conversion; - preserve existing behavior for string,
ArrayBuffer, andBlobresponse handling; - update tests so expected byte sequences no longer depend on the deprecated polyfill;
- remove
@sinonjs/text-encodingfrom package metadata and generated artifacts.
This keeps the production change local, removes an unmaintained dependency, and avoids adding @exodus/bytes for a single UTF-8 conversion path.
Architecture
The response conversion flow should remain centered in lib/fake-xhr/index.js:
convertToArrayBuffer(body)should returnbodyunchanged when it is already anArrayBuffer;- string input should flow through a new helper that encodes to UTF-8 bytes and returns a detached
ArrayBuffer; blobresponse creation should continue to build from the same UTF-8 bytes, soarraybufferandblobstay behaviorally aligned.
The helper should be intentionally narrow. It should not claim to support arbitrary encodings, and it should not introduce a general-purpose text codec abstraction.
Testing
Update lib/fake-xhr/index.test.js to pin the intended UTF-8 behavior:
responseType = "arraybuffer"returns UTF-8 bytes for ASCII strings;responseType = "arraybuffer"returns UTF-8 bytes for non-ASCII strings such as"\xFF";- existing binary
ArrayBufferresponses still pass through unchanged; - expected buffers are created without
@sinonjs/text-encoding, using explicit byte arrays or a local test helper.
Package-level verification should also confirm that:
package.jsonno longer lists@sinonjs/text-encoding;package-lock.jsonno longer resolves it;- the browser bundle
nise.jsrebuilds cleanly without that dependency.
Out of Scope
- adding
TextDecodersupport; - implementing non-UTF-8 encodings;
- broader fake XHR compatibility changes unrelated to the deprecated text-encoding dependency;
- unrelated dependency upgrades or lockfile churn.