diff --git a/src/encoding_binding.cc b/src/encoding_binding.cc index a132eeb62306c6..885a0d072312e9 100644 --- a/src/encoding_binding.cc +++ b/src/encoding_binding.cc @@ -286,9 +286,11 @@ void BindingData::DecodeLatin1(const FunctionCallbackInfo& args) { env->isolate(), "The encoded data was not valid for encoding latin1"); } - Local buffer_result = - node::Buffer::Copy(env, result.c_str(), written).ToLocalChecked(); - args.GetReturnValue().Set(buffer_result); + Local output = + String::NewFromUtf8( + env->isolate(), result.c_str(), v8::NewStringType::kNormal, written) + .ToLocalChecked(); + args.GetReturnValue().Set(output); } } // namespace encoding_binding diff --git a/test/cctest/test_encoding_binding.cc b/test/cctest/test_encoding_binding.cc index 06cc36d8f6ae34..d5d14c60fedf7e 100644 --- a/test/cctest/test_encoding_binding.cc +++ b/test/cctest/test_encoding_binding.cc @@ -26,7 +26,7 @@ bool RunDecodeLatin1(Environment* env, return false; } - *result = try_catch.Exception(); + *result = args[0]; return true; } @@ -151,5 +151,26 @@ TEST_F(EncodingBindingTest, DecodeLatin1_BOMPresent) { EXPECT_STREQ(*utf8_result, "Áéó"); } +TEST_F(EncodingBindingTest, DecodeLatin1_ReturnsString) { + Environment* env = CreateEnvironment(); + Isolate* isolate = env->isolate(); + HandleScope handle_scope(isolate); + + const uint8_t latin1_data[] = {0xC1, 0xE9, 0xF3}; + Local ab = ArrayBuffer::New(isolate, sizeof(latin1_data)); + memcpy(ab->GetBackingStore()->Data(), latin1_data, sizeof(latin1_data)); + + Local array = Uint8Array::New(ab, 0, sizeof(latin1_data)); + Local args[] = {array}; + + Local result; + ASSERT_TRUE(RunDecodeLatin1(env, args, false, false, &result)); + + ASSERT_TRUE(result->IsString()); + + String::Utf8Value utf8_result(isolate, result); + EXPECT_STREQ(*utf8_result, "Áéó"); +} + } // namespace encoding_binding } // namespace node diff --git a/test/parallel/test-util-text-decoder.js b/test/parallel/test-util-text-decoder.js new file mode 100644 index 00000000000000..0f6d0463f9da48 --- /dev/null +++ b/test/parallel/test-util-text-decoder.js @@ -0,0 +1,17 @@ +'use strict'; + +const common = require('../common'); + +const test = require('node:test'); +const assert = require('node:assert'); + +test('TextDecoder correctly decodes windows-1252 encoded data', { skip: !common.hasIntl }, () => { + const latin1Bytes = new Uint8Array([0xc1, 0xe9, 0xf3]); + + const expectedString = 'Áéó'; + + const decoder = new TextDecoder('windows-1252'); + const decodedString = decoder.decode(latin1Bytes); + + assert.strictEqual(decodedString, expectedString); +});