diff options
-rw-r--r-- | op_crates/web/08_text_encoding.js | 8 | ||||
-rw-r--r-- | op_crates/web/text_encoding_test.js | 41 |
2 files changed, 47 insertions, 2 deletions
diff --git a/op_crates/web/08_text_encoding.js b/op_crates/web/08_text_encoding.js index bf2e20049..e938eecd1 100644 --- a/op_crates/web/08_text_encoding.js +++ b/op_crates/web/08_text_encoding.js @@ -395,7 +395,8 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any function isEitherArrayBuffer(x) { - return x instanceof SharedArrayBuffer || x instanceof ArrayBuffer; + return x instanceof SharedArrayBuffer || x instanceof ArrayBuffer || + typeof x === "undefined"; } class TextDecoder { @@ -442,6 +443,7 @@ bytes = new Uint8Array(input); } else if ( typeof input === "object" && + input !== null && "buffer" in input && isEitherArrayBuffer(input.buffer) ) { @@ -451,7 +453,9 @@ input.byteLength, ); } else { - bytes = new Uint8Array(0); + throw new TypeError( + "Provided input is not of type ArrayBuffer or ArrayBufferView", + ); } // For simple utf-8 decoding "Deno.core.decode" can be used for performance diff --git a/op_crates/web/text_encoding_test.js b/op_crates/web/text_encoding_test.js index f741fe409..06537df24 100644 --- a/op_crates/web/text_encoding_test.js +++ b/op_crates/web/text_encoding_test.js @@ -131,6 +131,44 @@ function textDecoderErrorEncoding() { assert(didThrow); } +function textDecoderHandlesUndefined() { + const fixture = undefined; + const decoder = new TextDecoder(); + assert(decoder.decode(fixture) === ""); +} + +function textDecoderThrowsOnEmpty() { + const fixture = ""; + const decoder = new TextDecoder(); + let didThrow = false; + try { + decoder.decode(fixture); + } catch (e) { + didThrow = true; + assert( + e.message === + "Provided input is not of type ArrayBuffer or ArrayBufferView", + ); + } + assert(didThrow); +} + +function textDecoderThrowsOnNull() { + const fixture = null; + const decoder = new TextDecoder(); + let didThrow = false; + try { + decoder.decode(fixture); + } catch (e) { + didThrow = true; + assert( + e.message === + "Provided input is not of type ArrayBuffer or ArrayBufferView", + ); + } + assert(didThrow); +} + function textEncoder() { const fixture = "𝓽𝓮𝔁𝓽"; const encoder = new TextEncoder(); @@ -231,6 +269,9 @@ function main() { textDecoderNotBOM(); textDecoderASCII(); textDecoderErrorEncoding(); + textDecoderHandlesUndefined(); + textDecoderThrowsOnEmpty(); + textDecoderThrowsOnNull(); textEncoder(); textEncodeInto(); textEncodeInto2(); |