From 41bd55f97f33afa6c3e53808af4c19ae868e15b7 Mon Sep 17 00:00:00 2001 From: mert <mertcanaltin@mert-MacBook-Pro.local> Date: Wed, 11 Dec 2024 11:54:00 +0300 Subject: [PATCH 1/7] util: fix Latin1 decoding to return string output --- src/encoding_binding.cc | 8 +++++--- test/cctest/test_encoding_binding.cc | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) 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<Value>& args) { env->isolate(), "The encoded data was not valid for encoding latin1"); } - Local<Object> buffer_result = - node::Buffer::Copy(env, result.c_str(), written).ToLocalChecked(); - args.GetReturnValue().Set(buffer_result); + Local<String> 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..d00f285800fa7e 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; } From 3333f7593cdea2eb1295ee4ee671cc4ecc24b4dc Mon Sep 17 00:00:00 2001 From: mert <mertcanaltin@mert-MacBook-Pro.local> Date: Wed, 11 Dec 2024 12:45:19 +0300 Subject: [PATCH 2/7] test: add validation for DecodeLatin1 string output --- test/cctest/test_encoding_binding.cc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/cctest/test_encoding_binding.cc b/test/cctest/test_encoding_binding.cc index d00f285800fa7e..232fb9d666cd96 100644 --- a/test/cctest/test_encoding_binding.cc +++ b/test/cctest/test_encoding_binding.cc @@ -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<ArrayBuffer> ab = ArrayBuffer::New(isolate, sizeof(latin1_data)); + memcpy(ab->GetBackingStore()->Data(), latin1_data, sizeof(latin1_data)); + + Local<Uint8Array> array = Uint8Array::New(ab, 0, sizeof(latin1_data)); + Local<Value> args[] = {array}; + + Local<Value> 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 From 88a6182060c5dd254f82768a9ee12d05dd8eb9b9 Mon Sep 17 00:00:00 2001 From: mert <mertcanaltin@mert-MacBook-Pro.local> Date: Wed, 11 Dec 2024 12:47:50 +0300 Subject: [PATCH 3/7] lint --- test/cctest/test_encoding_binding.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cctest/test_encoding_binding.cc b/test/cctest/test_encoding_binding.cc index 232fb9d666cd96..d5d14c60fedf7e 100644 --- a/test/cctest/test_encoding_binding.cc +++ b/test/cctest/test_encoding_binding.cc @@ -156,7 +156,7 @@ TEST_F(EncodingBindingTest, DecodeLatin1_ReturnsString) { Isolate* isolate = env->isolate(); HandleScope handle_scope(isolate); - const uint8_t latin1_data[] = {0xC1, 0xE9, 0xF3}; + const uint8_t latin1_data[] = {0xC1, 0xE9, 0xF3}; Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, sizeof(latin1_data)); memcpy(ab->GetBackingStore()->Data(), latin1_data, sizeof(latin1_data)); From 19da2910dc8b0dc56b69fe6fb3284f3449f40f3a Mon Sep 17 00:00:00 2001 From: mert <mertcanaltin@mert-MacBook-Pro.local> Date: Wed, 11 Dec 2024 13:09:25 +0300 Subject: [PATCH 4/7] test: add validation for TextDecoder windows-1252 decoding --- test/parallel/test-util-text-decoder.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 test/parallel/test-util-text-decoder.js diff --git a/test/parallel/test-util-text-decoder.js b/test/parallel/test-util-text-decoder.js new file mode 100644 index 00000000000000..3d2a8585f33cdb --- /dev/null +++ b/test/parallel/test-util-text-decoder.js @@ -0,0 +1,17 @@ +'use strict'; + +require('../common'); + +const test = require('node:test'); +const assert = require('node:assert'); + +test('TextDecoder correctly decodes windows-1252 encoded data', (t) => { + const latin1Bytes = new Uint8Array([0xc1, 0xe9, 0xf3]); + + const expectedString = 'Áéó'; + + const decoder = new TextDecoder('windows-1252'); + const decodedString = decoder.decode(latin1Bytes); + + assert.strictEqual(decodedString, expectedString); +}); From a83c4092bdc2d26eb79ed944261de1d412c14d75 Mon Sep 17 00:00:00 2001 From: mert <mertcanaltin@mert-MacBook-Pro.local> Date: Wed, 11 Dec 2024 22:07:31 +0300 Subject: [PATCH 5/7] test: skip TextDecoder test if Intl support is missing --- test/parallel/test-util-text-decoder.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-util-text-decoder.js b/test/parallel/test-util-text-decoder.js index 3d2a8585f33cdb..ac52e7e1a079dc 100644 --- a/test/parallel/test-util-text-decoder.js +++ b/test/parallel/test-util-text-decoder.js @@ -1,11 +1,15 @@ 'use strict'; -require('../common'); +const common = require('../common'); const test = require('node:test'); const assert = require('node:assert'); test('TextDecoder correctly decodes windows-1252 encoded data', (t) => { + if (!common.hasIntl) { + common.skip('missing Intl'); + } + const latin1Bytes = new Uint8Array([0xc1, 0xe9, 0xf3]); const expectedString = 'Áéó'; From d9e8b1af469130520eb2d6d6f31a3e14ed36c6f4 Mon Sep 17 00:00:00 2001 From: Mert Can Altin <mertgold60@gmail.com> Date: Wed, 11 Dec 2024 23:27:55 +0300 Subject: [PATCH 6/7] Update test/parallel/test-util-text-decoder.js Co-authored-by: Yagiz Nizipli <yagiz@nizipli.com> --- test/parallel/test-util-text-decoder.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/parallel/test-util-text-decoder.js b/test/parallel/test-util-text-decoder.js index ac52e7e1a079dc..7e6608fb514e23 100644 --- a/test/parallel/test-util-text-decoder.js +++ b/test/parallel/test-util-text-decoder.js @@ -5,9 +5,7 @@ const common = require('../common'); const test = require('node:test'); const assert = require('node:assert'); -test('TextDecoder correctly decodes windows-1252 encoded data', (t) => { - if (!common.hasIntl) { - common.skip('missing Intl'); +test('TextDecoder correctly decodes windows-1252 encoded data', { skip: !common.hasIntl }, (t) => { } const latin1Bytes = new Uint8Array([0xc1, 0xe9, 0xf3]); From e8cf59a5c885c76bb4d1a1be0477a85523ae5a1e Mon Sep 17 00:00:00 2001 From: mert <mertcanaltin@mert-MacBook-Pro.local> Date: Wed, 11 Dec 2024 23:33:27 +0300 Subject: [PATCH 7/7] lint fix --- test/parallel/test-util-text-decoder.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/parallel/test-util-text-decoder.js b/test/parallel/test-util-text-decoder.js index 7e6608fb514e23..0f6d0463f9da48 100644 --- a/test/parallel/test-util-text-decoder.js +++ b/test/parallel/test-util-text-decoder.js @@ -5,9 +5,7 @@ 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 }, (t) => { - } - +test('TextDecoder correctly decodes windows-1252 encoded data', { skip: !common.hasIntl }, () => { const latin1Bytes = new Uint8Array([0xc1, 0xe9, 0xf3]); const expectedString = 'Áéó';