From 1ac2e8b103cbde18a5c3f904ec8d78a62de60636 Mon Sep 17 00:00:00 2001 From: Nahee-Park Date: Sat, 28 Sep 2024 23:40:33 +0900 Subject: [PATCH 1/2] stream: handle undefined chunks correctly in decode stream Align TextDecoderStream behavior with WPT requirements by treating undefined chunks as errors. This change ensures that TextDecoderStream properly handles unexpected chunk types and throws an error when receiving undefined input. This update addresses the failing WPT for decode stream error handling. --- lib/internal/webstreams/encoding.js | 3 +++ test/wpt/status/encoding.json | 7 ------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/internal/webstreams/encoding.js b/lib/internal/webstreams/encoding.js index b5533b4287b65a..9cfea36989a228 100644 --- a/lib/internal/webstreams/encoding.js +++ b/lib/internal/webstreams/encoding.js @@ -133,6 +133,9 @@ class TextDecoderStream { this.#handle = new TextDecoder(encoding, options); this.#transform = new TransformStream({ transform: (chunk, controller) => { + if (chunk === undefined) { + throw new ERR_INVALID_THIS('TextDecoderStream'); + } const value = this.#handle.decode(chunk, { stream: true }); if (value) controller.enqueue(value); diff --git a/test/wpt/status/encoding.json b/test/wpt/status/encoding.json index e78a8b3c0791be..f9378d7195a2a7 100644 --- a/test/wpt/status/encoding.json +++ b/test/wpt/status/encoding.json @@ -66,13 +66,6 @@ "streams/decode-utf8.any.js": { "requires": ["small-icu"] }, - "streams/decode-bad-chunks.any.js": { - "fail": { - "expected": [ - "chunk of type undefined should error the stream" - ] - } - }, "streams/decode-non-utf8.any.js": { "requires": ["full-icu"] }, From cc89cf23dee8854c30ce5b5c3f464307057aad16 Mon Sep 17 00:00:00 2001 From: Nahee-Park Date: Sun, 29 Sep 2024 19:07:21 +0900 Subject: [PATCH 2/2] stream: throw ERR_INVALID_ARG_TYPE instead of ERR_INVALID_THIS --- lib/internal/webstreams/encoding.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/internal/webstreams/encoding.js b/lib/internal/webstreams/encoding.js index 9cfea36989a228..f316222ccbf0e8 100644 --- a/lib/internal/webstreams/encoding.js +++ b/lib/internal/webstreams/encoding.js @@ -20,6 +20,7 @@ const { customInspect } = require('internal/webstreams/util'); const { codes: { + ERR_INVALID_ARG_TYPE, ERR_INVALID_THIS, }, } = require('internal/errors'); @@ -134,7 +135,7 @@ class TextDecoderStream { this.#transform = new TransformStream({ transform: (chunk, controller) => { if (chunk === undefined) { - throw new ERR_INVALID_THIS('TextDecoderStream'); + throw new ERR_INVALID_ARG_TYPE('chunk', 'string', chunk); } const value = this.#handle.decode(chunk, { stream: true }); if (value)